Add ELFv2 .localentry support.
[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. */
128#define PLT_ENTRY_SIZE 24
129
130/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 131#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
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. */
ad8e1ba5 143#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7 144#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
71a39c98
AM
145#define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
146#define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
147#define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
148#define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
149#define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
5d1634d7
AM
150#define BCTR 0x4e800420 /* bctr */
151
71a39c98 152#define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
ad8e1ba5
AM
153#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
154#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
155
71a39c98
AM
156#define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
157#define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
158#define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
794e51c0
AM
159#define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
160#define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
161#define BNECTR 0x4ca20420 /* bnectr+ */
162#define BNECTR_P4 0x4ce20420 /* bnectr+ */
163
71a39c98 164#define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
ac2df442
AM
165#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
166#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
167
ad8e1ba5
AM
168#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
169
ee4bf8d2 170/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 171#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
172 /* 0: */
173 /* .quad plt0-1f */
174 /* __glink: */
175#define MFLR_R12 0x7d8802a6 /* mflr %12 */
176#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
177 /* 1: */
178#define MFLR_R11 0x7d6802a6 /* mflr %11 */
71a39c98 179 /* ld %2,(0b-1b)(%11) */
ee4bf8d2 180#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
71a39c98
AM
181#define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
182 /* ld %12,0(%11) */
183 /* ld %2,8(%11) */
184 /* mtctr %12 */
185 /* ld %11,16(%11) */
ee4bf8d2 186 /* bctr */
5d1634d7
AM
187
188/* Pad with this. */
189#define NOP 0x60000000
190
721956f4
AM
191/* Some other nops. */
192#define CROR_151515 0x4def7b82
193#define CROR_313131 0x4ffffb82
194
cedb70c5 195/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
196#define LI_R0_0 0x38000000 /* li %r0,0 */
197#define B_DOT 0x48000000 /* b . */
198
199/* After that, we need two instructions to load the index, followed by
200 a branch. */
201#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 202#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 203
deb0e272
AM
204/* Instructions used by the save and restore reg functions. */
205#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
206#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
207#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
208#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
209#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
210#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
211#define LI_R12_0 0x39800000 /* li %r12,0 */
212#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
213#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
214#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
215#define BLR 0x4e800020 /* blr */
216
41bd81ab
AM
217/* Since .opd is an array of descriptors and each entry will end up
218 with identical R_PPC64_RELATIVE relocs, there is really no need to
219 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 220 relocate .opd without reloc entries. */
41bd81ab
AM
221#ifndef NO_OPD_RELOCS
222#define NO_OPD_RELOCS 0
223#endif
5bd4f169 224\f
f5e87a1d 225#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 226
5bd4f169 227/* Relocation HOWTO's. */
04c9666a 228static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
229
230static reloc_howto_type ppc64_elf_howto_raw[] = {
231 /* This reloc does nothing. */
232 HOWTO (R_PPC64_NONE, /* type */
233 0, /* rightshift */
411e1bfb
AM
234 2, /* size (0 = byte, 1 = short, 2 = long) */
235 32, /* bitsize */
b34976b6 236 FALSE, /* pc_relative */
5bd4f169 237 0, /* bitpos */
f5e87a1d 238 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
239 bfd_elf_generic_reloc, /* special_function */
240 "R_PPC64_NONE", /* name */
b34976b6 241 FALSE, /* partial_inplace */
d006db6c 242 0, /* src_mask */
5bd4f169 243 0, /* dst_mask */
b34976b6 244 FALSE), /* pcrel_offset */
5bd4f169
AM
245
246 /* A standard 32 bit relocation. */
247 HOWTO (R_PPC64_ADDR32, /* type */
248 0, /* rightshift */
249 2, /* size (0 = byte, 1 = short, 2 = long) */
250 32, /* bitsize */
b34976b6 251 FALSE, /* pc_relative */
5bd4f169
AM
252 0, /* bitpos */
253 complain_overflow_bitfield, /* complain_on_overflow */
254 bfd_elf_generic_reloc, /* special_function */
255 "R_PPC64_ADDR32", /* name */
b34976b6 256 FALSE, /* partial_inplace */
5bd4f169
AM
257 0, /* src_mask */
258 0xffffffff, /* dst_mask */
b34976b6 259 FALSE), /* pcrel_offset */
5bd4f169
AM
260
261 /* An absolute 26 bit branch; the lower two bits must be zero.
262 FIXME: we don't check that, we just clear them. */
263 HOWTO (R_PPC64_ADDR24, /* type */
264 0, /* rightshift */
265 2, /* size (0 = byte, 1 = short, 2 = long) */
266 26, /* bitsize */
b34976b6 267 FALSE, /* pc_relative */
5bd4f169
AM
268 0, /* bitpos */
269 complain_overflow_bitfield, /* complain_on_overflow */
270 bfd_elf_generic_reloc, /* special_function */
271 "R_PPC64_ADDR24", /* name */
b34976b6 272 FALSE, /* partial_inplace */
d006db6c 273 0, /* src_mask */
f5e87a1d 274 0x03fffffc, /* dst_mask */
b34976b6 275 FALSE), /* pcrel_offset */
5bd4f169
AM
276
277 /* A standard 16 bit relocation. */
278 HOWTO (R_PPC64_ADDR16, /* type */
279 0, /* rightshift */
280 1, /* size (0 = byte, 1 = short, 2 = long) */
281 16, /* bitsize */
b34976b6 282 FALSE, /* pc_relative */
5bd4f169
AM
283 0, /* bitpos */
284 complain_overflow_bitfield, /* complain_on_overflow */
285 bfd_elf_generic_reloc, /* special_function */
286 "R_PPC64_ADDR16", /* name */
b34976b6 287 FALSE, /* partial_inplace */
5bd4f169
AM
288 0, /* src_mask */
289 0xffff, /* dst_mask */
b34976b6 290 FALSE), /* pcrel_offset */
5bd4f169
AM
291
292 /* A 16 bit relocation without overflow. */
293 HOWTO (R_PPC64_ADDR16_LO, /* type */
294 0, /* rightshift */
295 1, /* size (0 = byte, 1 = short, 2 = long) */
296 16, /* bitsize */
b34976b6 297 FALSE, /* pc_relative */
5bd4f169
AM
298 0, /* bitpos */
299 complain_overflow_dont,/* complain_on_overflow */
300 bfd_elf_generic_reloc, /* special_function */
301 "R_PPC64_ADDR16_LO", /* name */
b34976b6 302 FALSE, /* partial_inplace */
5bd4f169
AM
303 0, /* src_mask */
304 0xffff, /* dst_mask */
b34976b6 305 FALSE), /* pcrel_offset */
5bd4f169
AM
306
307 /* Bits 16-31 of an address. */
308 HOWTO (R_PPC64_ADDR16_HI, /* type */
309 16, /* rightshift */
310 1, /* size (0 = byte, 1 = short, 2 = long) */
311 16, /* bitsize */
b34976b6 312 FALSE, /* pc_relative */
5bd4f169 313 0, /* bitpos */
f9c6b907 314 complain_overflow_signed, /* complain_on_overflow */
5bd4f169
AM
315 bfd_elf_generic_reloc, /* special_function */
316 "R_PPC64_ADDR16_HI", /* name */
b34976b6 317 FALSE, /* partial_inplace */
5bd4f169
AM
318 0, /* src_mask */
319 0xffff, /* dst_mask */
b34976b6 320 FALSE), /* pcrel_offset */
5bd4f169
AM
321
322 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323 bits, treated as a signed number, is negative. */
324 HOWTO (R_PPC64_ADDR16_HA, /* type */
325 16, /* rightshift */
326 1, /* size (0 = byte, 1 = short, 2 = long) */
327 16, /* bitsize */
b34976b6 328 FALSE, /* pc_relative */
5bd4f169 329 0, /* bitpos */
f9c6b907 330 complain_overflow_signed, /* complain_on_overflow */
805fc799 331 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 332 "R_PPC64_ADDR16_HA", /* name */
b34976b6 333 FALSE, /* partial_inplace */
5bd4f169
AM
334 0, /* src_mask */
335 0xffff, /* dst_mask */
b34976b6 336 FALSE), /* pcrel_offset */
5bd4f169
AM
337
338 /* An absolute 16 bit branch; the lower two bits must be zero.
339 FIXME: we don't check that, we just clear them. */
340 HOWTO (R_PPC64_ADDR14, /* type */
341 0, /* rightshift */
342 2, /* size (0 = byte, 1 = short, 2 = long) */
343 16, /* bitsize */
b34976b6 344 FALSE, /* pc_relative */
5bd4f169
AM
345 0, /* bitpos */
346 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 347 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 348 "R_PPC64_ADDR14", /* name */
b34976b6 349 FALSE, /* partial_inplace */
d006db6c 350 0, /* src_mask */
f5e87a1d 351 0x0000fffc, /* dst_mask */
b34976b6 352 FALSE), /* pcrel_offset */
5bd4f169
AM
353
354 /* An absolute 16 bit branch, for which bit 10 should be set to
355 indicate that the branch is expected to be taken. The lower two
356 bits must be zero. */
357 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
358 0, /* rightshift */
359 2, /* size (0 = byte, 1 = short, 2 = long) */
360 16, /* bitsize */
b34976b6 361 FALSE, /* pc_relative */
5bd4f169
AM
362 0, /* bitpos */
363 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 364 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 365 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 366 FALSE, /* partial_inplace */
d006db6c 367 0, /* src_mask */
f5e87a1d 368 0x0000fffc, /* dst_mask */
b34976b6 369 FALSE), /* pcrel_offset */
5bd4f169
AM
370
371 /* An absolute 16 bit branch, for which bit 10 should be set to
372 indicate that the branch is not expected to be taken. The lower
373 two bits must be zero. */
374 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
375 0, /* rightshift */
376 2, /* size (0 = byte, 1 = short, 2 = long) */
377 16, /* bitsize */
b34976b6 378 FALSE, /* pc_relative */
5bd4f169
AM
379 0, /* bitpos */
380 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 381 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 382 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 383 FALSE, /* partial_inplace */
d006db6c 384 0, /* src_mask */
f5e87a1d 385 0x0000fffc, /* dst_mask */
b34976b6 386 FALSE), /* pcrel_offset */
5bd4f169
AM
387
388 /* A relative 26 bit branch; the lower two bits must be zero. */
389 HOWTO (R_PPC64_REL24, /* type */
390 0, /* rightshift */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
392 26, /* bitsize */
b34976b6 393 TRUE, /* pc_relative */
5bd4f169
AM
394 0, /* bitpos */
395 complain_overflow_signed, /* complain_on_overflow */
2441e016 396 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 397 "R_PPC64_REL24", /* name */
b34976b6 398 FALSE, /* partial_inplace */
d006db6c 399 0, /* src_mask */
f5e87a1d 400 0x03fffffc, /* dst_mask */
b34976b6 401 TRUE), /* pcrel_offset */
5bd4f169
AM
402
403 /* A relative 16 bit branch; the lower two bits must be zero. */
404 HOWTO (R_PPC64_REL14, /* type */
405 0, /* rightshift */
406 2, /* size (0 = byte, 1 = short, 2 = long) */
407 16, /* bitsize */
b34976b6 408 TRUE, /* pc_relative */
5bd4f169
AM
409 0, /* bitpos */
410 complain_overflow_signed, /* complain_on_overflow */
2441e016 411 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 412 "R_PPC64_REL14", /* name */
b34976b6 413 FALSE, /* partial_inplace */
d006db6c 414 0, /* src_mask */
f5e87a1d 415 0x0000fffc, /* dst_mask */
b34976b6 416 TRUE), /* pcrel_offset */
5bd4f169
AM
417
418 /* A relative 16 bit branch. Bit 10 should be set to indicate that
419 the branch is expected to be taken. The lower two bits must be
420 zero. */
421 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
422 0, /* rightshift */
423 2, /* size (0 = byte, 1 = short, 2 = long) */
424 16, /* bitsize */
b34976b6 425 TRUE, /* pc_relative */
5bd4f169
AM
426 0, /* bitpos */
427 complain_overflow_signed, /* complain_on_overflow */
805fc799 428 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 429 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 430 FALSE, /* partial_inplace */
d006db6c 431 0, /* src_mask */
f5e87a1d 432 0x0000fffc, /* dst_mask */
b34976b6 433 TRUE), /* pcrel_offset */
5bd4f169
AM
434
435 /* A relative 16 bit branch. Bit 10 should be set to indicate that
436 the branch is not expected to be taken. The lower two bits must
437 be zero. */
438 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
439 0, /* rightshift */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
441 16, /* bitsize */
b34976b6 442 TRUE, /* pc_relative */
5bd4f169
AM
443 0, /* bitpos */
444 complain_overflow_signed, /* complain_on_overflow */
805fc799 445 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 446 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 447 FALSE, /* partial_inplace */
d006db6c 448 0, /* src_mask */
f5e87a1d 449 0x0000fffc, /* dst_mask */
b34976b6 450 TRUE), /* pcrel_offset */
5bd4f169
AM
451
452 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
453 symbol. */
454 HOWTO (R_PPC64_GOT16, /* type */
455 0, /* rightshift */
456 1, /* size (0 = byte, 1 = short, 2 = long) */
457 16, /* bitsize */
b34976b6 458 FALSE, /* pc_relative */
5bd4f169
AM
459 0, /* bitpos */
460 complain_overflow_signed, /* complain_on_overflow */
805fc799 461 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 462 "R_PPC64_GOT16", /* name */
b34976b6 463 FALSE, /* partial_inplace */
5bd4f169
AM
464 0, /* src_mask */
465 0xffff, /* dst_mask */
b34976b6 466 FALSE), /* pcrel_offset */
5bd4f169
AM
467
468 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
469 the symbol. */
470 HOWTO (R_PPC64_GOT16_LO, /* type */
471 0, /* rightshift */
472 1, /* size (0 = byte, 1 = short, 2 = long) */
473 16, /* bitsize */
b34976b6 474 FALSE, /* pc_relative */
5bd4f169
AM
475 0, /* bitpos */
476 complain_overflow_dont, /* complain_on_overflow */
805fc799 477 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 478 "R_PPC64_GOT16_LO", /* name */
b34976b6 479 FALSE, /* partial_inplace */
5bd4f169
AM
480 0, /* src_mask */
481 0xffff, /* dst_mask */
b34976b6 482 FALSE), /* pcrel_offset */
5bd4f169
AM
483
484 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
485 the symbol. */
486 HOWTO (R_PPC64_GOT16_HI, /* type */
487 16, /* rightshift */
488 1, /* size (0 = byte, 1 = short, 2 = long) */
489 16, /* bitsize */
b34976b6 490 FALSE, /* pc_relative */
5bd4f169 491 0, /* bitpos */
f9c6b907 492 complain_overflow_signed,/* complain_on_overflow */
805fc799 493 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 494 "R_PPC64_GOT16_HI", /* name */
b34976b6 495 FALSE, /* partial_inplace */
5bd4f169
AM
496 0, /* src_mask */
497 0xffff, /* dst_mask */
b34976b6 498 FALSE), /* pcrel_offset */
5bd4f169
AM
499
500 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
501 the symbol. */
502 HOWTO (R_PPC64_GOT16_HA, /* type */
503 16, /* rightshift */
504 1, /* size (0 = byte, 1 = short, 2 = long) */
505 16, /* bitsize */
b34976b6 506 FALSE, /* pc_relative */
5bd4f169 507 0, /* bitpos */
f9c6b907 508 complain_overflow_signed,/* complain_on_overflow */
805fc799 509 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 510 "R_PPC64_GOT16_HA", /* name */
b34976b6 511 FALSE, /* partial_inplace */
5bd4f169
AM
512 0, /* src_mask */
513 0xffff, /* dst_mask */
b34976b6 514 FALSE), /* pcrel_offset */
5bd4f169
AM
515
516 /* This is used only by the dynamic linker. The symbol should exist
517 both in the object being run and in some shared library. The
518 dynamic linker copies the data addressed by the symbol from the
519 shared library into the object, because the object being
520 run has to have the data at some particular address. */
521 HOWTO (R_PPC64_COPY, /* type */
522 0, /* rightshift */
f5e87a1d
AM
523 0, /* this one is variable size */
524 0, /* bitsize */
b34976b6 525 FALSE, /* pc_relative */
5bd4f169 526 0, /* bitpos */
f5e87a1d
AM
527 complain_overflow_dont, /* complain_on_overflow */
528 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 529 "R_PPC64_COPY", /* name */
b34976b6 530 FALSE, /* partial_inplace */
5bd4f169
AM
531 0, /* src_mask */
532 0, /* dst_mask */
b34976b6 533 FALSE), /* pcrel_offset */
5bd4f169
AM
534
535 /* Like R_PPC64_ADDR64, but used when setting global offset table
536 entries. */
537 HOWTO (R_PPC64_GLOB_DAT, /* type */
538 0, /* rightshift */
539 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
540 64, /* bitsize */
b34976b6 541 FALSE, /* pc_relative */
5bd4f169
AM
542 0, /* bitpos */
543 complain_overflow_dont, /* complain_on_overflow */
805fc799 544 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 545 "R_PPC64_GLOB_DAT", /* name */
b34976b6 546 FALSE, /* partial_inplace */
5bd4f169 547 0, /* src_mask */
f5e87a1d 548 ONES (64), /* dst_mask */
b34976b6 549 FALSE), /* pcrel_offset */
5bd4f169
AM
550
551 /* Created by the link editor. Marks a procedure linkage table
552 entry for a symbol. */
553 HOWTO (R_PPC64_JMP_SLOT, /* type */
554 0, /* rightshift */
555 0, /* size (0 = byte, 1 = short, 2 = long) */
556 0, /* bitsize */
b34976b6 557 FALSE, /* pc_relative */
5bd4f169
AM
558 0, /* bitpos */
559 complain_overflow_dont, /* complain_on_overflow */
805fc799 560 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 561 "R_PPC64_JMP_SLOT", /* name */
b34976b6 562 FALSE, /* partial_inplace */
5bd4f169
AM
563 0, /* src_mask */
564 0, /* dst_mask */
b34976b6 565 FALSE), /* pcrel_offset */
5bd4f169
AM
566
567 /* Used only by the dynamic linker. When the object is run, this
568 doubleword64 is set to the load address of the object, plus the
569 addend. */
570 HOWTO (R_PPC64_RELATIVE, /* type */
571 0, /* rightshift */
572 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
573 64, /* bitsize */
b34976b6 574 FALSE, /* pc_relative */
5bd4f169
AM
575 0, /* bitpos */
576 complain_overflow_dont, /* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_PPC64_RELATIVE", /* name */
b34976b6 579 FALSE, /* partial_inplace */
5bd4f169 580 0, /* src_mask */
f5e87a1d 581 ONES (64), /* dst_mask */
b34976b6 582 FALSE), /* pcrel_offset */
5bd4f169
AM
583
584 /* Like R_PPC64_ADDR32, but may be unaligned. */
585 HOWTO (R_PPC64_UADDR32, /* type */
586 0, /* rightshift */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
588 32, /* bitsize */
b34976b6 589 FALSE, /* pc_relative */
5bd4f169
AM
590 0, /* bitpos */
591 complain_overflow_bitfield, /* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_PPC64_UADDR32", /* name */
b34976b6 594 FALSE, /* partial_inplace */
5bd4f169
AM
595 0, /* src_mask */
596 0xffffffff, /* dst_mask */
b34976b6 597 FALSE), /* pcrel_offset */
5bd4f169
AM
598
599 /* Like R_PPC64_ADDR16, but may be unaligned. */
600 HOWTO (R_PPC64_UADDR16, /* type */
601 0, /* rightshift */
602 1, /* size (0 = byte, 1 = short, 2 = long) */
603 16, /* bitsize */
b34976b6 604 FALSE, /* pc_relative */
5bd4f169
AM
605 0, /* bitpos */
606 complain_overflow_bitfield, /* complain_on_overflow */
607 bfd_elf_generic_reloc, /* special_function */
608 "R_PPC64_UADDR16", /* name */
b34976b6 609 FALSE, /* partial_inplace */
5bd4f169
AM
610 0, /* src_mask */
611 0xffff, /* dst_mask */
b34976b6 612 FALSE), /* pcrel_offset */
5bd4f169
AM
613
614 /* 32-bit PC relative. */
615 HOWTO (R_PPC64_REL32, /* type */
616 0, /* rightshift */
617 2, /* size (0 = byte, 1 = short, 2 = long) */
618 32, /* bitsize */
b34976b6 619 TRUE, /* pc_relative */
5bd4f169 620 0, /* bitpos */
cedb70c5 621 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
622 complain_overflow_signed, /* complain_on_overflow */
623 bfd_elf_generic_reloc, /* special_function */
624 "R_PPC64_REL32", /* name */
b34976b6 625 FALSE, /* partial_inplace */
5bd4f169
AM
626 0, /* src_mask */
627 0xffffffff, /* dst_mask */
b34976b6 628 TRUE), /* pcrel_offset */
5bd4f169 629
10ed1bba 630 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
631 HOWTO (R_PPC64_PLT32, /* type */
632 0, /* rightshift */
633 2, /* size (0 = byte, 1 = short, 2 = long) */
634 32, /* bitsize */
b34976b6 635 FALSE, /* pc_relative */
5bd4f169
AM
636 0, /* bitpos */
637 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 638 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 639 "R_PPC64_PLT32", /* name */
b34976b6 640 FALSE, /* partial_inplace */
5bd4f169 641 0, /* src_mask */
f5e87a1d 642 0xffffffff, /* dst_mask */
b34976b6 643 FALSE), /* pcrel_offset */
5bd4f169
AM
644
645 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
646 FIXME: R_PPC64_PLTREL32 not supported. */
647 HOWTO (R_PPC64_PLTREL32, /* type */
648 0, /* rightshift */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
650 32, /* bitsize */
b34976b6 651 TRUE, /* pc_relative */
5bd4f169
AM
652 0, /* bitpos */
653 complain_overflow_signed, /* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_PPC64_PLTREL32", /* name */
b34976b6 656 FALSE, /* partial_inplace */
5bd4f169 657 0, /* src_mask */
f5e87a1d 658 0xffffffff, /* dst_mask */
b34976b6 659 TRUE), /* pcrel_offset */
5bd4f169
AM
660
661 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
662 the symbol. */
663 HOWTO (R_PPC64_PLT16_LO, /* type */
664 0, /* rightshift */
665 1, /* size (0 = byte, 1 = short, 2 = long) */
666 16, /* bitsize */
b34976b6 667 FALSE, /* pc_relative */
5bd4f169
AM
668 0, /* bitpos */
669 complain_overflow_dont, /* complain_on_overflow */
805fc799 670 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 671 "R_PPC64_PLT16_LO", /* name */
b34976b6 672 FALSE, /* partial_inplace */
5bd4f169
AM
673 0, /* src_mask */
674 0xffff, /* dst_mask */
b34976b6 675 FALSE), /* pcrel_offset */
5bd4f169
AM
676
677 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
678 the symbol. */
679 HOWTO (R_PPC64_PLT16_HI, /* type */
680 16, /* rightshift */
681 1, /* size (0 = byte, 1 = short, 2 = long) */
682 16, /* bitsize */
b34976b6 683 FALSE, /* pc_relative */
5bd4f169 684 0, /* bitpos */
f9c6b907 685 complain_overflow_signed, /* complain_on_overflow */
805fc799 686 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 687 "R_PPC64_PLT16_HI", /* name */
b34976b6 688 FALSE, /* partial_inplace */
5bd4f169
AM
689 0, /* src_mask */
690 0xffff, /* dst_mask */
b34976b6 691 FALSE), /* pcrel_offset */
5bd4f169
AM
692
693 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
694 the symbol. */
695 HOWTO (R_PPC64_PLT16_HA, /* type */
696 16, /* rightshift */
697 1, /* size (0 = byte, 1 = short, 2 = long) */
698 16, /* bitsize */
b34976b6 699 FALSE, /* pc_relative */
5bd4f169 700 0, /* bitpos */
f9c6b907 701 complain_overflow_signed, /* complain_on_overflow */
805fc799 702 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 703 "R_PPC64_PLT16_HA", /* name */
b34976b6 704 FALSE, /* partial_inplace */
5bd4f169
AM
705 0, /* src_mask */
706 0xffff, /* dst_mask */
b34976b6 707 FALSE), /* pcrel_offset */
5bd4f169 708
c061c2d8 709 /* 16-bit section relative relocation. */
5bd4f169
AM
710 HOWTO (R_PPC64_SECTOFF, /* type */
711 0, /* rightshift */
c061c2d8
AM
712 1, /* size (0 = byte, 1 = short, 2 = long) */
713 16, /* bitsize */
b34976b6 714 FALSE, /* pc_relative */
5bd4f169
AM
715 0, /* bitpos */
716 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 717 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 718 "R_PPC64_SECTOFF", /* name */
b34976b6 719 FALSE, /* partial_inplace */
5bd4f169 720 0, /* src_mask */
c061c2d8 721 0xffff, /* dst_mask */
b34976b6 722 FALSE), /* pcrel_offset */
5bd4f169 723
c061c2d8 724 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
725 HOWTO (R_PPC64_SECTOFF_LO, /* type */
726 0, /* rightshift */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
728 16, /* bitsize */
b34976b6 729 FALSE, /* pc_relative */
5bd4f169
AM
730 0, /* bitpos */
731 complain_overflow_dont, /* complain_on_overflow */
805fc799 732 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 733 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 734 FALSE, /* partial_inplace */
5bd4f169
AM
735 0, /* src_mask */
736 0xffff, /* dst_mask */
b34976b6 737 FALSE), /* pcrel_offset */
5bd4f169
AM
738
739 /* 16-bit upper half section relative relocation. */
740 HOWTO (R_PPC64_SECTOFF_HI, /* type */
741 16, /* rightshift */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
743 16, /* bitsize */
b34976b6 744 FALSE, /* pc_relative */
5bd4f169 745 0, /* bitpos */
f9c6b907 746 complain_overflow_signed, /* complain_on_overflow */
805fc799 747 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 748 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 749 FALSE, /* partial_inplace */
5bd4f169
AM
750 0, /* src_mask */
751 0xffff, /* dst_mask */
b34976b6 752 FALSE), /* pcrel_offset */
5bd4f169
AM
753
754 /* 16-bit upper half adjusted section relative relocation. */
755 HOWTO (R_PPC64_SECTOFF_HA, /* type */
756 16, /* rightshift */
757 1, /* size (0 = byte, 1 = short, 2 = long) */
758 16, /* bitsize */
b34976b6 759 FALSE, /* pc_relative */
5bd4f169 760 0, /* bitpos */
f9c6b907 761 complain_overflow_signed, /* complain_on_overflow */
805fc799 762 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 763 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 764 FALSE, /* partial_inplace */
5bd4f169
AM
765 0, /* src_mask */
766 0xffff, /* dst_mask */
b34976b6 767 FALSE), /* pcrel_offset */
5bd4f169 768
04c9666a
AM
769 /* Like R_PPC64_REL24 without touching the two least significant bits. */
770 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
771 2, /* rightshift */
772 2, /* size (0 = byte, 1 = short, 2 = long) */
773 30, /* bitsize */
b34976b6 774 TRUE, /* pc_relative */
5bd4f169
AM
775 0, /* bitpos */
776 complain_overflow_dont, /* complain_on_overflow */
777 bfd_elf_generic_reloc, /* special_function */
04c9666a 778 "R_PPC64_REL30", /* name */
b34976b6 779 FALSE, /* partial_inplace */
d006db6c 780 0, /* src_mask */
5bd4f169 781 0xfffffffc, /* dst_mask */
b34976b6 782 TRUE), /* pcrel_offset */
5bd4f169
AM
783
784 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
785
786 /* A standard 64-bit relocation. */
787 HOWTO (R_PPC64_ADDR64, /* type */
788 0, /* rightshift */
789 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
790 64, /* bitsize */
b34976b6 791 FALSE, /* pc_relative */
5bd4f169
AM
792 0, /* bitpos */
793 complain_overflow_dont, /* complain_on_overflow */
794 bfd_elf_generic_reloc, /* special_function */
795 "R_PPC64_ADDR64", /* name */
b34976b6 796 FALSE, /* partial_inplace */
5bd4f169 797 0, /* src_mask */
f5e87a1d 798 ONES (64), /* dst_mask */
b34976b6 799 FALSE), /* pcrel_offset */
5bd4f169
AM
800
801 /* The bits 32-47 of an address. */
802 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
803 32, /* rightshift */
804 1, /* size (0 = byte, 1 = short, 2 = long) */
805 16, /* bitsize */
b34976b6 806 FALSE, /* pc_relative */
5bd4f169
AM
807 0, /* bitpos */
808 complain_overflow_dont, /* complain_on_overflow */
809 bfd_elf_generic_reloc, /* special_function */
810 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 811 FALSE, /* partial_inplace */
5bd4f169
AM
812 0, /* src_mask */
813 0xffff, /* dst_mask */
b34976b6 814 FALSE), /* pcrel_offset */
5bd4f169
AM
815
816 /* The bits 32-47 of an address, plus 1 if the contents of the low
817 16 bits, treated as a signed number, is negative. */
818 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
819 32, /* rightshift */
820 1, /* size (0 = byte, 1 = short, 2 = long) */
821 16, /* bitsize */
b34976b6 822 FALSE, /* pc_relative */
5bd4f169
AM
823 0, /* bitpos */
824 complain_overflow_dont, /* complain_on_overflow */
805fc799 825 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 826 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 827 FALSE, /* partial_inplace */
5bd4f169
AM
828 0, /* src_mask */
829 0xffff, /* dst_mask */
b34976b6 830 FALSE), /* pcrel_offset */
5bd4f169
AM
831
832 /* The bits 48-63 of an address. */
833 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
834 48, /* rightshift */
835 1, /* size (0 = byte, 1 = short, 2 = long) */
836 16, /* bitsize */
b34976b6 837 FALSE, /* pc_relative */
5bd4f169
AM
838 0, /* bitpos */
839 complain_overflow_dont, /* complain_on_overflow */
840 bfd_elf_generic_reloc, /* special_function */
841 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 842 FALSE, /* partial_inplace */
5bd4f169
AM
843 0, /* src_mask */
844 0xffff, /* dst_mask */
b34976b6 845 FALSE), /* pcrel_offset */
5bd4f169
AM
846
847 /* The bits 48-63 of an address, plus 1 if the contents of the low
848 16 bits, treated as a signed number, is negative. */
849 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
850 48, /* rightshift */
851 1, /* size (0 = byte, 1 = short, 2 = long) */
852 16, /* bitsize */
b34976b6 853 FALSE, /* pc_relative */
5bd4f169
AM
854 0, /* bitpos */
855 complain_overflow_dont, /* complain_on_overflow */
805fc799 856 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 857 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 858 FALSE, /* partial_inplace */
5bd4f169
AM
859 0, /* src_mask */
860 0xffff, /* dst_mask */
b34976b6 861 FALSE), /* pcrel_offset */
5bd4f169
AM
862
863 /* Like ADDR64, but may be unaligned. */
864 HOWTO (R_PPC64_UADDR64, /* type */
865 0, /* rightshift */
866 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
867 64, /* bitsize */
b34976b6 868 FALSE, /* pc_relative */
5bd4f169
AM
869 0, /* bitpos */
870 complain_overflow_dont, /* complain_on_overflow */
871 bfd_elf_generic_reloc, /* special_function */
872 "R_PPC64_UADDR64", /* name */
b34976b6 873 FALSE, /* partial_inplace */
5bd4f169 874 0, /* src_mask */
f5e87a1d 875 ONES (64), /* dst_mask */
b34976b6 876 FALSE), /* pcrel_offset */
5bd4f169
AM
877
878 /* 64-bit relative relocation. */
879 HOWTO (R_PPC64_REL64, /* type */
880 0, /* rightshift */
881 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
882 64, /* bitsize */
b34976b6 883 TRUE, /* pc_relative */
5bd4f169
AM
884 0, /* bitpos */
885 complain_overflow_dont, /* complain_on_overflow */
886 bfd_elf_generic_reloc, /* special_function */
887 "R_PPC64_REL64", /* name */
b34976b6 888 FALSE, /* partial_inplace */
5bd4f169 889 0, /* src_mask */
f5e87a1d 890 ONES (64), /* dst_mask */
b34976b6 891 TRUE), /* pcrel_offset */
5bd4f169 892
cedb70c5 893 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
894 HOWTO (R_PPC64_PLT64, /* type */
895 0, /* rightshift */
896 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
897 64, /* bitsize */
b34976b6 898 FALSE, /* pc_relative */
5bd4f169
AM
899 0, /* bitpos */
900 complain_overflow_dont, /* complain_on_overflow */
805fc799 901 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 902 "R_PPC64_PLT64", /* name */
b34976b6 903 FALSE, /* partial_inplace */
5bd4f169 904 0, /* src_mask */
f5e87a1d 905 ONES (64), /* dst_mask */
b34976b6 906 FALSE), /* pcrel_offset */
5bd4f169
AM
907
908 /* 64-bit PC relative relocation to the symbol's procedure linkage
909 table. */
910 /* FIXME: R_PPC64_PLTREL64 not supported. */
911 HOWTO (R_PPC64_PLTREL64, /* type */
912 0, /* rightshift */
913 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
914 64, /* bitsize */
b34976b6 915 TRUE, /* pc_relative */
5bd4f169
AM
916 0, /* bitpos */
917 complain_overflow_dont, /* complain_on_overflow */
805fc799 918 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 919 "R_PPC64_PLTREL64", /* name */
b34976b6 920 FALSE, /* partial_inplace */
5bd4f169 921 0, /* src_mask */
f5e87a1d 922 ONES (64), /* dst_mask */
b34976b6 923 TRUE), /* pcrel_offset */
5bd4f169
AM
924
925 /* 16 bit TOC-relative relocation. */
926
927 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
928 HOWTO (R_PPC64_TOC16, /* type */
929 0, /* rightshift */
930 1, /* size (0 = byte, 1 = short, 2 = long) */
931 16, /* bitsize */
b34976b6 932 FALSE, /* pc_relative */
5bd4f169
AM
933 0, /* bitpos */
934 complain_overflow_signed, /* complain_on_overflow */
805fc799 935 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 936 "R_PPC64_TOC16", /* name */
b34976b6 937 FALSE, /* partial_inplace */
5bd4f169
AM
938 0, /* src_mask */
939 0xffff, /* dst_mask */
b34976b6 940 FALSE), /* pcrel_offset */
5bd4f169
AM
941
942 /* 16 bit TOC-relative relocation without overflow. */
943
944 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
945 HOWTO (R_PPC64_TOC16_LO, /* type */
946 0, /* rightshift */
947 1, /* size (0 = byte, 1 = short, 2 = long) */
948 16, /* bitsize */
b34976b6 949 FALSE, /* pc_relative */
5bd4f169
AM
950 0, /* bitpos */
951 complain_overflow_dont, /* complain_on_overflow */
805fc799 952 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 953 "R_PPC64_TOC16_LO", /* name */
b34976b6 954 FALSE, /* partial_inplace */
5bd4f169
AM
955 0, /* src_mask */
956 0xffff, /* dst_mask */
b34976b6 957 FALSE), /* pcrel_offset */
5bd4f169
AM
958
959 /* 16 bit TOC-relative relocation, high 16 bits. */
960
961 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
962 HOWTO (R_PPC64_TOC16_HI, /* type */
963 16, /* rightshift */
964 1, /* size (0 = byte, 1 = short, 2 = long) */
965 16, /* bitsize */
b34976b6 966 FALSE, /* pc_relative */
5bd4f169 967 0, /* bitpos */
f9c6b907 968 complain_overflow_signed, /* complain_on_overflow */
805fc799 969 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 970 "R_PPC64_TOC16_HI", /* name */
b34976b6 971 FALSE, /* partial_inplace */
5bd4f169
AM
972 0, /* src_mask */
973 0xffff, /* dst_mask */
b34976b6 974 FALSE), /* pcrel_offset */
5bd4f169
AM
975
976 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
977 contents of the low 16 bits, treated as a signed number, is
978 negative. */
979
980 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
981 HOWTO (R_PPC64_TOC16_HA, /* type */
982 16, /* rightshift */
983 1, /* size (0 = byte, 1 = short, 2 = long) */
984 16, /* bitsize */
b34976b6 985 FALSE, /* pc_relative */
5bd4f169 986 0, /* bitpos */
f9c6b907 987 complain_overflow_signed, /* complain_on_overflow */
805fc799 988 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 989 "R_PPC64_TOC16_HA", /* name */
b34976b6 990 FALSE, /* partial_inplace */
5bd4f169
AM
991 0, /* src_mask */
992 0xffff, /* dst_mask */
b34976b6 993 FALSE), /* pcrel_offset */
5bd4f169
AM
994
995 /* 64-bit relocation; insert value of TOC base (.TOC.). */
996
997 /* R_PPC64_TOC 51 doubleword64 .TOC. */
998 HOWTO (R_PPC64_TOC, /* type */
999 0, /* rightshift */
1000 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1001 64, /* bitsize */
b34976b6 1002 FALSE, /* pc_relative */
5bd4f169
AM
1003 0, /* bitpos */
1004 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 1005 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 1006 "R_PPC64_TOC", /* name */
b34976b6 1007 FALSE, /* partial_inplace */
5bd4f169 1008 0, /* src_mask */
f5e87a1d 1009 ONES (64), /* dst_mask */
b34976b6 1010 FALSE), /* pcrel_offset */
5bd4f169
AM
1011
1012 /* Like R_PPC64_GOT16, but also informs the link editor that the
1013 value to relocate may (!) refer to a PLT entry which the link
1014 editor (a) may replace with the symbol value. If the link editor
1015 is unable to fully resolve the symbol, it may (b) create a PLT
1016 entry and store the address to the new PLT entry in the GOT.
1017 This permits lazy resolution of function symbols at run time.
1018 The link editor may also skip all of this and just (c) emit a
1019 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1020 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1021 HOWTO (R_PPC64_PLTGOT16, /* type */
1022 0, /* rightshift */
1023 1, /* size (0 = byte, 1 = short, 2 = long) */
1024 16, /* bitsize */
b34976b6 1025 FALSE, /* pc_relative */
5bd4f169
AM
1026 0, /* bitpos */
1027 complain_overflow_signed, /* complain_on_overflow */
805fc799 1028 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1029 "R_PPC64_PLTGOT16", /* name */
1030 FALSE, /* partial_inplace */
1031 0, /* src_mask */
1032 0xffff, /* dst_mask */
1033 FALSE), /* pcrel_offset */
1034
1035 /* Like R_PPC64_PLTGOT16, but without overflow. */
1036 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1037 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1038 0, /* rightshift */
1039 1, /* size (0 = byte, 1 = short, 2 = long) */
1040 16, /* bitsize */
1041 FALSE, /* pc_relative */
1042 0, /* bitpos */
1043 complain_overflow_dont, /* complain_on_overflow */
1044 ppc64_elf_unhandled_reloc, /* special_function */
1045 "R_PPC64_PLTGOT16_LO", /* name */
1046 FALSE, /* partial_inplace */
1047 0, /* src_mask */
1048 0xffff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
1050
1051 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1052 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1053 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1054 16, /* rightshift */
1055 1, /* size (0 = byte, 1 = short, 2 = long) */
1056 16, /* bitsize */
1057 FALSE, /* pc_relative */
1058 0, /* bitpos */
f9c6b907 1059 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1060 ppc64_elf_unhandled_reloc, /* special_function */
1061 "R_PPC64_PLTGOT16_HI", /* name */
1062 FALSE, /* partial_inplace */
1063 0, /* src_mask */
1064 0xffff, /* dst_mask */
1065 FALSE), /* pcrel_offset */
1066
1067 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1068 1 if the contents of the low 16 bits, treated as a signed number,
1069 is negative. */
1070 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1071 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1072 16, /* rightshift */
1073 1, /* size (0 = byte, 1 = short, 2 = long) */
1074 16, /* bitsize */
1075 FALSE, /* pc_relative */
1076 0, /* bitpos */
f9c6b907 1077 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1078 ppc64_elf_unhandled_reloc, /* special_function */
1079 "R_PPC64_PLTGOT16_HA", /* name */
1080 FALSE, /* partial_inplace */
1081 0, /* src_mask */
1082 0xffff, /* dst_mask */
1083 FALSE), /* pcrel_offset */
1084
1085 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1086 HOWTO (R_PPC64_ADDR16_DS, /* type */
1087 0, /* rightshift */
1088 1, /* size (0 = byte, 1 = short, 2 = long) */
1089 16, /* bitsize */
1090 FALSE, /* pc_relative */
1091 0, /* bitpos */
1092 complain_overflow_bitfield, /* complain_on_overflow */
1093 bfd_elf_generic_reloc, /* special_function */
1094 "R_PPC64_ADDR16_DS", /* name */
1095 FALSE, /* partial_inplace */
1096 0, /* src_mask */
1097 0xfffc, /* dst_mask */
1098 FALSE), /* pcrel_offset */
1099
1100 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1101 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1102 0, /* rightshift */
1103 1, /* size (0 = byte, 1 = short, 2 = long) */
1104 16, /* bitsize */
1105 FALSE, /* pc_relative */
1106 0, /* bitpos */
1107 complain_overflow_dont,/* complain_on_overflow */
1108 bfd_elf_generic_reloc, /* special_function */
1109 "R_PPC64_ADDR16_LO_DS",/* name */
1110 FALSE, /* partial_inplace */
1111 0, /* src_mask */
1112 0xfffc, /* dst_mask */
1113 FALSE), /* pcrel_offset */
1114
1115 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1116 HOWTO (R_PPC64_GOT16_DS, /* type */
1117 0, /* rightshift */
1118 1, /* size (0 = byte, 1 = short, 2 = long) */
1119 16, /* bitsize */
1120 FALSE, /* pc_relative */
1121 0, /* bitpos */
1122 complain_overflow_signed, /* complain_on_overflow */
1123 ppc64_elf_unhandled_reloc, /* special_function */
1124 "R_PPC64_GOT16_DS", /* name */
1125 FALSE, /* partial_inplace */
1126 0, /* src_mask */
1127 0xfffc, /* dst_mask */
1128 FALSE), /* pcrel_offset */
1129
1130 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1131 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1132 0, /* rightshift */
1133 1, /* size (0 = byte, 1 = short, 2 = long) */
1134 16, /* bitsize */
1135 FALSE, /* pc_relative */
1136 0, /* bitpos */
1137 complain_overflow_dont, /* complain_on_overflow */
1138 ppc64_elf_unhandled_reloc, /* special_function */
1139 "R_PPC64_GOT16_LO_DS", /* name */
1140 FALSE, /* partial_inplace */
1141 0, /* src_mask */
1142 0xfffc, /* dst_mask */
1143 FALSE), /* pcrel_offset */
1144
1145 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1146 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1147 0, /* rightshift */
1148 1, /* size (0 = byte, 1 = short, 2 = long) */
1149 16, /* bitsize */
1150 FALSE, /* pc_relative */
1151 0, /* bitpos */
1152 complain_overflow_dont, /* complain_on_overflow */
1153 ppc64_elf_unhandled_reloc, /* special_function */
1154 "R_PPC64_PLT16_LO_DS", /* name */
1155 FALSE, /* partial_inplace */
1156 0, /* src_mask */
1157 0xfffc, /* dst_mask */
1158 FALSE), /* pcrel_offset */
1159
1160 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1161 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1162 0, /* rightshift */
1163 1, /* size (0 = byte, 1 = short, 2 = long) */
1164 16, /* bitsize */
1165 FALSE, /* pc_relative */
1166 0, /* bitpos */
1167 complain_overflow_bitfield, /* complain_on_overflow */
1168 ppc64_elf_sectoff_reloc, /* special_function */
1169 "R_PPC64_SECTOFF_DS", /* name */
1170 FALSE, /* partial_inplace */
1171 0, /* src_mask */
1172 0xfffc, /* dst_mask */
1173 FALSE), /* pcrel_offset */
1174
1175 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1176 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1177 0, /* rightshift */
1178 1, /* size (0 = byte, 1 = short, 2 = long) */
1179 16, /* bitsize */
1180 FALSE, /* pc_relative */
1181 0, /* bitpos */
1182 complain_overflow_dont, /* complain_on_overflow */
1183 ppc64_elf_sectoff_reloc, /* special_function */
1184 "R_PPC64_SECTOFF_LO_DS",/* name */
1185 FALSE, /* partial_inplace */
1186 0, /* src_mask */
1187 0xfffc, /* dst_mask */
1188 FALSE), /* pcrel_offset */
1189
1190 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1191 HOWTO (R_PPC64_TOC16_DS, /* type */
1192 0, /* rightshift */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1194 16, /* bitsize */
1195 FALSE, /* pc_relative */
1196 0, /* bitpos */
1197 complain_overflow_signed, /* complain_on_overflow */
1198 ppc64_elf_toc_reloc, /* special_function */
1199 "R_PPC64_TOC16_DS", /* name */
1200 FALSE, /* partial_inplace */
1201 0, /* src_mask */
1202 0xfffc, /* dst_mask */
1203 FALSE), /* pcrel_offset */
1204
1205 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1206 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1207 0, /* rightshift */
1208 1, /* size (0 = byte, 1 = short, 2 = long) */
1209 16, /* bitsize */
1210 FALSE, /* pc_relative */
1211 0, /* bitpos */
1212 complain_overflow_dont, /* complain_on_overflow */
1213 ppc64_elf_toc_reloc, /* special_function */
1214 "R_PPC64_TOC16_LO_DS", /* name */
1215 FALSE, /* partial_inplace */
1216 0, /* src_mask */
1217 0xfffc, /* dst_mask */
1218 FALSE), /* pcrel_offset */
1219
1220 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1221 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1222 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1223 0, /* rightshift */
1224 1, /* size (0 = byte, 1 = short, 2 = long) */
1225 16, /* bitsize */
1226 FALSE, /* pc_relative */
1227 0, /* bitpos */
1228 complain_overflow_signed, /* complain_on_overflow */
1229 ppc64_elf_unhandled_reloc, /* special_function */
1230 "R_PPC64_PLTGOT16_DS", /* name */
1231 FALSE, /* partial_inplace */
1232 0, /* src_mask */
1233 0xfffc, /* dst_mask */
1234 FALSE), /* pcrel_offset */
1235
1236 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1237 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1238 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1239 0, /* rightshift */
1240 1, /* size (0 = byte, 1 = short, 2 = long) */
1241 16, /* bitsize */
1242 FALSE, /* pc_relative */
1243 0, /* bitpos */
1244 complain_overflow_dont, /* complain_on_overflow */
1245 ppc64_elf_unhandled_reloc, /* special_function */
1246 "R_PPC64_PLTGOT16_LO_DS",/* name */
1247 FALSE, /* partial_inplace */
1248 0, /* src_mask */
1249 0xfffc, /* dst_mask */
1250 FALSE), /* pcrel_offset */
1251
727fc41e 1252 /* Marker relocs for TLS. */
411e1bfb
AM
1253 HOWTO (R_PPC64_TLS,
1254 0, /* rightshift */
1255 2, /* size (0 = byte, 1 = short, 2 = long) */
1256 32, /* bitsize */
1257 FALSE, /* pc_relative */
1258 0, /* bitpos */
1259 complain_overflow_dont, /* complain_on_overflow */
1260 bfd_elf_generic_reloc, /* special_function */
1261 "R_PPC64_TLS", /* name */
1262 FALSE, /* partial_inplace */
1263 0, /* src_mask */
1264 0, /* dst_mask */
1265 FALSE), /* pcrel_offset */
1266
727fc41e
AM
1267 HOWTO (R_PPC64_TLSGD,
1268 0, /* rightshift */
1269 2, /* size (0 = byte, 1 = short, 2 = long) */
1270 32, /* bitsize */
1271 FALSE, /* pc_relative */
1272 0, /* bitpos */
1273 complain_overflow_dont, /* complain_on_overflow */
1274 bfd_elf_generic_reloc, /* special_function */
1275 "R_PPC64_TLSGD", /* name */
1276 FALSE, /* partial_inplace */
1277 0, /* src_mask */
1278 0, /* dst_mask */
1279 FALSE), /* pcrel_offset */
1280
1281 HOWTO (R_PPC64_TLSLD,
1282 0, /* rightshift */
1283 2, /* size (0 = byte, 1 = short, 2 = long) */
1284 32, /* bitsize */
1285 FALSE, /* pc_relative */
1286 0, /* bitpos */
1287 complain_overflow_dont, /* complain_on_overflow */
1288 bfd_elf_generic_reloc, /* special_function */
1289 "R_PPC64_TLSLD", /* name */
1290 FALSE, /* partial_inplace */
1291 0, /* src_mask */
1292 0, /* dst_mask */
1293 FALSE), /* pcrel_offset */
1294
3b421ab3
AM
1295 HOWTO (R_PPC64_TOCSAVE,
1296 0, /* rightshift */
1297 2, /* size (0 = byte, 1 = short, 2 = long) */
1298 32, /* bitsize */
1299 FALSE, /* pc_relative */
1300 0, /* bitpos */
1301 complain_overflow_dont, /* complain_on_overflow */
1302 bfd_elf_generic_reloc, /* special_function */
1303 "R_PPC64_TOCSAVE", /* name */
1304 FALSE, /* partial_inplace */
1305 0, /* src_mask */
1306 0, /* dst_mask */
1307 FALSE), /* pcrel_offset */
1308
411e1bfb
AM
1309 /* Computes the load module index of the load module that contains the
1310 definition of its TLS sym. */
1311 HOWTO (R_PPC64_DTPMOD64,
1312 0, /* rightshift */
1313 4, /* size (0 = byte, 1 = short, 2 = long) */
1314 64, /* bitsize */
1315 FALSE, /* pc_relative */
1316 0, /* bitpos */
1317 complain_overflow_dont, /* complain_on_overflow */
1318 ppc64_elf_unhandled_reloc, /* special_function */
1319 "R_PPC64_DTPMOD64", /* name */
1320 FALSE, /* partial_inplace */
1321 0, /* src_mask */
1322 ONES (64), /* dst_mask */
1323 FALSE), /* pcrel_offset */
1324
1325 /* Computes a dtv-relative displacement, the difference between the value
1326 of sym+add and the base address of the thread-local storage block that
1327 contains the definition of sym, minus 0x8000. */
1328 HOWTO (R_PPC64_DTPREL64,
1329 0, /* rightshift */
1330 4, /* size (0 = byte, 1 = short, 2 = long) */
1331 64, /* bitsize */
1332 FALSE, /* pc_relative */
1333 0, /* bitpos */
1334 complain_overflow_dont, /* complain_on_overflow */
1335 ppc64_elf_unhandled_reloc, /* special_function */
1336 "R_PPC64_DTPREL64", /* name */
1337 FALSE, /* partial_inplace */
1338 0, /* src_mask */
1339 ONES (64), /* dst_mask */
1340 FALSE), /* pcrel_offset */
1341
1342 /* A 16 bit dtprel reloc. */
1343 HOWTO (R_PPC64_DTPREL16,
1344 0, /* rightshift */
1345 1, /* size (0 = byte, 1 = short, 2 = long) */
1346 16, /* bitsize */
1347 FALSE, /* pc_relative */
1348 0, /* bitpos */
1349 complain_overflow_signed, /* complain_on_overflow */
1350 ppc64_elf_unhandled_reloc, /* special_function */
1351 "R_PPC64_DTPREL16", /* name */
1352 FALSE, /* partial_inplace */
1353 0, /* src_mask */
1354 0xffff, /* dst_mask */
1355 FALSE), /* pcrel_offset */
1356
1357 /* Like DTPREL16, but no overflow. */
1358 HOWTO (R_PPC64_DTPREL16_LO,
1359 0, /* rightshift */
1360 1, /* size (0 = byte, 1 = short, 2 = long) */
1361 16, /* bitsize */
1362 FALSE, /* pc_relative */
1363 0, /* bitpos */
1364 complain_overflow_dont, /* complain_on_overflow */
1365 ppc64_elf_unhandled_reloc, /* special_function */
1366 "R_PPC64_DTPREL16_LO", /* name */
1367 FALSE, /* partial_inplace */
1368 0, /* src_mask */
1369 0xffff, /* dst_mask */
1370 FALSE), /* pcrel_offset */
1371
1372 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1373 HOWTO (R_PPC64_DTPREL16_HI,
1374 16, /* rightshift */
1375 1, /* size (0 = byte, 1 = short, 2 = long) */
1376 16, /* bitsize */
1377 FALSE, /* pc_relative */
1378 0, /* bitpos */
f9c6b907 1379 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1380 ppc64_elf_unhandled_reloc, /* special_function */
1381 "R_PPC64_DTPREL16_HI", /* name */
1382 FALSE, /* partial_inplace */
1383 0, /* src_mask */
1384 0xffff, /* dst_mask */
1385 FALSE), /* pcrel_offset */
1386
1387 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1388 HOWTO (R_PPC64_DTPREL16_HA,
1389 16, /* rightshift */
1390 1, /* size (0 = byte, 1 = short, 2 = long) */
1391 16, /* bitsize */
1392 FALSE, /* pc_relative */
1393 0, /* bitpos */
f9c6b907 1394 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1395 ppc64_elf_unhandled_reloc, /* special_function */
1396 "R_PPC64_DTPREL16_HA", /* name */
1397 FALSE, /* partial_inplace */
1398 0, /* src_mask */
1399 0xffff, /* dst_mask */
1400 FALSE), /* pcrel_offset */
1401
1402 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1403 HOWTO (R_PPC64_DTPREL16_HIGHER,
1404 32, /* rightshift */
1405 1, /* size (0 = byte, 1 = short, 2 = long) */
1406 16, /* bitsize */
1407 FALSE, /* pc_relative */
1408 0, /* bitpos */
1409 complain_overflow_dont, /* complain_on_overflow */
1410 ppc64_elf_unhandled_reloc, /* special_function */
1411 "R_PPC64_DTPREL16_HIGHER", /* name */
1412 FALSE, /* partial_inplace */
1413 0, /* src_mask */
1414 0xffff, /* dst_mask */
1415 FALSE), /* pcrel_offset */
1416
1417 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1418 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1419 32, /* rightshift */
1420 1, /* size (0 = byte, 1 = short, 2 = long) */
1421 16, /* bitsize */
1422 FALSE, /* pc_relative */
1423 0, /* bitpos */
1424 complain_overflow_dont, /* complain_on_overflow */
1425 ppc64_elf_unhandled_reloc, /* special_function */
1426 "R_PPC64_DTPREL16_HIGHERA", /* name */
1427 FALSE, /* partial_inplace */
1428 0, /* src_mask */
1429 0xffff, /* dst_mask */
1430 FALSE), /* pcrel_offset */
1431
1432 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1433 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1434 48, /* rightshift */
1435 1, /* size (0 = byte, 1 = short, 2 = long) */
1436 16, /* bitsize */
1437 FALSE, /* pc_relative */
1438 0, /* bitpos */
1439 complain_overflow_dont, /* complain_on_overflow */
1440 ppc64_elf_unhandled_reloc, /* special_function */
1441 "R_PPC64_DTPREL16_HIGHEST", /* name */
1442 FALSE, /* partial_inplace */
1443 0, /* src_mask */
1444 0xffff, /* dst_mask */
1445 FALSE), /* pcrel_offset */
1446
1447 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1448 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1449 48, /* rightshift */
1450 1, /* size (0 = byte, 1 = short, 2 = long) */
1451 16, /* bitsize */
1452 FALSE, /* pc_relative */
1453 0, /* bitpos */
1454 complain_overflow_dont, /* complain_on_overflow */
1455 ppc64_elf_unhandled_reloc, /* special_function */
1456 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1457 FALSE, /* partial_inplace */
1458 0, /* src_mask */
1459 0xffff, /* dst_mask */
1460 FALSE), /* pcrel_offset */
1461
1462 /* Like DTPREL16, but for insns with a DS field. */
1463 HOWTO (R_PPC64_DTPREL16_DS,
1464 0, /* rightshift */
1465 1, /* size (0 = byte, 1 = short, 2 = long) */
1466 16, /* bitsize */
1467 FALSE, /* pc_relative */
1468 0, /* bitpos */
1469 complain_overflow_signed, /* complain_on_overflow */
1470 ppc64_elf_unhandled_reloc, /* special_function */
1471 "R_PPC64_DTPREL16_DS", /* name */
1472 FALSE, /* partial_inplace */
1473 0, /* src_mask */
1474 0xfffc, /* dst_mask */
1475 FALSE), /* pcrel_offset */
1476
1477 /* Like DTPREL16_DS, but no overflow. */
1478 HOWTO (R_PPC64_DTPREL16_LO_DS,
1479 0, /* rightshift */
1480 1, /* size (0 = byte, 1 = short, 2 = long) */
1481 16, /* bitsize */
1482 FALSE, /* pc_relative */
1483 0, /* bitpos */
1484 complain_overflow_dont, /* complain_on_overflow */
1485 ppc64_elf_unhandled_reloc, /* special_function */
1486 "R_PPC64_DTPREL16_LO_DS", /* name */
1487 FALSE, /* partial_inplace */
1488 0, /* src_mask */
1489 0xfffc, /* dst_mask */
1490 FALSE), /* pcrel_offset */
1491
1492 /* Computes a tp-relative displacement, the difference between the value of
1493 sym+add and the value of the thread pointer (r13). */
1494 HOWTO (R_PPC64_TPREL64,
1495 0, /* rightshift */
1496 4, /* size (0 = byte, 1 = short, 2 = long) */
1497 64, /* bitsize */
1498 FALSE, /* pc_relative */
1499 0, /* bitpos */
1500 complain_overflow_dont, /* complain_on_overflow */
1501 ppc64_elf_unhandled_reloc, /* special_function */
1502 "R_PPC64_TPREL64", /* name */
1503 FALSE, /* partial_inplace */
1504 0, /* src_mask */
1505 ONES (64), /* dst_mask */
1506 FALSE), /* pcrel_offset */
1507
1508 /* A 16 bit tprel reloc. */
1509 HOWTO (R_PPC64_TPREL16,
1510 0, /* rightshift */
1511 1, /* size (0 = byte, 1 = short, 2 = long) */
1512 16, /* bitsize */
1513 FALSE, /* pc_relative */
1514 0, /* bitpos */
1515 complain_overflow_signed, /* complain_on_overflow */
1516 ppc64_elf_unhandled_reloc, /* special_function */
1517 "R_PPC64_TPREL16", /* name */
1518 FALSE, /* partial_inplace */
1519 0, /* src_mask */
1520 0xffff, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1522
1523 /* Like TPREL16, but no overflow. */
1524 HOWTO (R_PPC64_TPREL16_LO,
1525 0, /* rightshift */
1526 1, /* size (0 = byte, 1 = short, 2 = long) */
1527 16, /* bitsize */
1528 FALSE, /* pc_relative */
1529 0, /* bitpos */
1530 complain_overflow_dont, /* complain_on_overflow */
1531 ppc64_elf_unhandled_reloc, /* special_function */
1532 "R_PPC64_TPREL16_LO", /* name */
1533 FALSE, /* partial_inplace */
1534 0, /* src_mask */
1535 0xffff, /* dst_mask */
1536 FALSE), /* pcrel_offset */
1537
1538 /* Like TPREL16_LO, but next higher group of 16 bits. */
1539 HOWTO (R_PPC64_TPREL16_HI,
1540 16, /* rightshift */
1541 1, /* size (0 = byte, 1 = short, 2 = long) */
1542 16, /* bitsize */
1543 FALSE, /* pc_relative */
1544 0, /* bitpos */
f9c6b907 1545 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1546 ppc64_elf_unhandled_reloc, /* special_function */
1547 "R_PPC64_TPREL16_HI", /* name */
1548 FALSE, /* partial_inplace */
1549 0, /* src_mask */
1550 0xffff, /* dst_mask */
1551 FALSE), /* pcrel_offset */
1552
1553 /* Like TPREL16_HI, but adjust for low 16 bits. */
1554 HOWTO (R_PPC64_TPREL16_HA,
1555 16, /* rightshift */
1556 1, /* size (0 = byte, 1 = short, 2 = long) */
1557 16, /* bitsize */
1558 FALSE, /* pc_relative */
1559 0, /* bitpos */
f9c6b907 1560 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1561 ppc64_elf_unhandled_reloc, /* special_function */
1562 "R_PPC64_TPREL16_HA", /* name */
1563 FALSE, /* partial_inplace */
1564 0, /* src_mask */
1565 0xffff, /* dst_mask */
1566 FALSE), /* pcrel_offset */
1567
1568 /* Like TPREL16_HI, but next higher group of 16 bits. */
1569 HOWTO (R_PPC64_TPREL16_HIGHER,
1570 32, /* rightshift */
1571 1, /* size (0 = byte, 1 = short, 2 = long) */
1572 16, /* bitsize */
1573 FALSE, /* pc_relative */
1574 0, /* bitpos */
1575 complain_overflow_dont, /* complain_on_overflow */
1576 ppc64_elf_unhandled_reloc, /* special_function */
1577 "R_PPC64_TPREL16_HIGHER", /* name */
1578 FALSE, /* partial_inplace */
1579 0, /* src_mask */
1580 0xffff, /* dst_mask */
1581 FALSE), /* pcrel_offset */
1582
1583 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1584 HOWTO (R_PPC64_TPREL16_HIGHERA,
1585 32, /* rightshift */
1586 1, /* size (0 = byte, 1 = short, 2 = long) */
1587 16, /* bitsize */
1588 FALSE, /* pc_relative */
1589 0, /* bitpos */
1590 complain_overflow_dont, /* complain_on_overflow */
1591 ppc64_elf_unhandled_reloc, /* special_function */
1592 "R_PPC64_TPREL16_HIGHERA", /* name */
1593 FALSE, /* partial_inplace */
1594 0, /* src_mask */
1595 0xffff, /* dst_mask */
1596 FALSE), /* pcrel_offset */
1597
1598 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1599 HOWTO (R_PPC64_TPREL16_HIGHEST,
1600 48, /* rightshift */
1601 1, /* size (0 = byte, 1 = short, 2 = long) */
1602 16, /* bitsize */
1603 FALSE, /* pc_relative */
1604 0, /* bitpos */
1605 complain_overflow_dont, /* complain_on_overflow */
1606 ppc64_elf_unhandled_reloc, /* special_function */
1607 "R_PPC64_TPREL16_HIGHEST", /* name */
1608 FALSE, /* partial_inplace */
1609 0, /* src_mask */
1610 0xffff, /* dst_mask */
1611 FALSE), /* pcrel_offset */
1612
1613 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1614 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1615 48, /* rightshift */
1616 1, /* size (0 = byte, 1 = short, 2 = long) */
1617 16, /* bitsize */
1618 FALSE, /* pc_relative */
1619 0, /* bitpos */
1620 complain_overflow_dont, /* complain_on_overflow */
1621 ppc64_elf_unhandled_reloc, /* special_function */
1622 "R_PPC64_TPREL16_HIGHESTA", /* name */
1623 FALSE, /* partial_inplace */
1624 0, /* src_mask */
1625 0xffff, /* dst_mask */
1626 FALSE), /* pcrel_offset */
1627
1628 /* Like TPREL16, but for insns with a DS field. */
1629 HOWTO (R_PPC64_TPREL16_DS,
1630 0, /* rightshift */
1631 1, /* size (0 = byte, 1 = short, 2 = long) */
1632 16, /* bitsize */
1633 FALSE, /* pc_relative */
1634 0, /* bitpos */
1635 complain_overflow_signed, /* complain_on_overflow */
1636 ppc64_elf_unhandled_reloc, /* special_function */
1637 "R_PPC64_TPREL16_DS", /* name */
1638 FALSE, /* partial_inplace */
1639 0, /* src_mask */
1640 0xfffc, /* dst_mask */
1641 FALSE), /* pcrel_offset */
1642
1643 /* Like TPREL16_DS, but no overflow. */
1644 HOWTO (R_PPC64_TPREL16_LO_DS,
1645 0, /* rightshift */
1646 1, /* size (0 = byte, 1 = short, 2 = long) */
1647 16, /* bitsize */
1648 FALSE, /* pc_relative */
1649 0, /* bitpos */
1650 complain_overflow_dont, /* complain_on_overflow */
1651 ppc64_elf_unhandled_reloc, /* special_function */
1652 "R_PPC64_TPREL16_LO_DS", /* name */
1653 FALSE, /* partial_inplace */
1654 0, /* src_mask */
1655 0xfffc, /* dst_mask */
1656 FALSE), /* pcrel_offset */
1657
1658 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1659 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1660 to the first entry relative to the TOC base (r2). */
1661 HOWTO (R_PPC64_GOT_TLSGD16,
1662 0, /* rightshift */
1663 1, /* size (0 = byte, 1 = short, 2 = long) */
1664 16, /* bitsize */
1665 FALSE, /* pc_relative */
1666 0, /* bitpos */
1667 complain_overflow_signed, /* complain_on_overflow */
1668 ppc64_elf_unhandled_reloc, /* special_function */
1669 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1670 FALSE, /* partial_inplace */
5bd4f169
AM
1671 0, /* src_mask */
1672 0xffff, /* dst_mask */
b34976b6 1673 FALSE), /* pcrel_offset */
5bd4f169 1674
411e1bfb
AM
1675 /* Like GOT_TLSGD16, but no overflow. */
1676 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1677 0, /* rightshift */
1678 1, /* size (0 = byte, 1 = short, 2 = long) */
1679 16, /* bitsize */
b34976b6 1680 FALSE, /* pc_relative */
5bd4f169
AM
1681 0, /* bitpos */
1682 complain_overflow_dont, /* complain_on_overflow */
805fc799 1683 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1684 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1685 FALSE, /* partial_inplace */
5bd4f169
AM
1686 0, /* src_mask */
1687 0xffff, /* dst_mask */
b34976b6 1688 FALSE), /* pcrel_offset */
5bd4f169 1689
411e1bfb
AM
1690 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1691 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1692 16, /* rightshift */
1693 1, /* size (0 = byte, 1 = short, 2 = long) */
1694 16, /* bitsize */
b34976b6 1695 FALSE, /* pc_relative */
5bd4f169 1696 0, /* bitpos */
f9c6b907 1697 complain_overflow_signed, /* complain_on_overflow */
805fc799 1698 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1699 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1700 FALSE, /* partial_inplace */
5bd4f169
AM
1701 0, /* src_mask */
1702 0xffff, /* dst_mask */
b34976b6 1703 FALSE), /* pcrel_offset */
5bd4f169 1704
411e1bfb
AM
1705 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1706 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1707 16, /* rightshift */
1708 1, /* size (0 = byte, 1 = short, 2 = long) */
1709 16, /* bitsize */
b34976b6 1710 FALSE, /* pc_relative */
5bd4f169 1711 0, /* bitpos */
f9c6b907 1712 complain_overflow_signed, /* complain_on_overflow */
805fc799 1713 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1714 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1715 FALSE, /* partial_inplace */
5bd4f169
AM
1716 0, /* src_mask */
1717 0xffff, /* dst_mask */
b34976b6 1718 FALSE), /* pcrel_offset */
5bd4f169 1719
411e1bfb
AM
1720 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1721 with values (sym+add)@dtpmod and zero, and computes the offset to the
1722 first entry relative to the TOC base (r2). */
1723 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1724 0, /* rightshift */
1725 1, /* size (0 = byte, 1 = short, 2 = long) */
1726 16, /* bitsize */
b34976b6 1727 FALSE, /* pc_relative */
5bd4f169 1728 0, /* bitpos */
411e1bfb
AM
1729 complain_overflow_signed, /* complain_on_overflow */
1730 ppc64_elf_unhandled_reloc, /* special_function */
1731 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1732 FALSE, /* partial_inplace */
d006db6c 1733 0, /* src_mask */
411e1bfb 1734 0xffff, /* dst_mask */
b34976b6 1735 FALSE), /* pcrel_offset */
5bd4f169 1736
411e1bfb
AM
1737 /* Like GOT_TLSLD16, but no overflow. */
1738 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1739 0, /* rightshift */
1740 1, /* size (0 = byte, 1 = short, 2 = long) */
1741 16, /* bitsize */
b34976b6 1742 FALSE, /* pc_relative */
5bd4f169 1743 0, /* bitpos */
411e1bfb
AM
1744 complain_overflow_dont, /* complain_on_overflow */
1745 ppc64_elf_unhandled_reloc, /* special_function */
1746 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1747 FALSE, /* partial_inplace */
d006db6c 1748 0, /* src_mask */
411e1bfb 1749 0xffff, /* dst_mask */
b34976b6 1750 FALSE), /* pcrel_offset */
5bd4f169 1751
411e1bfb
AM
1752 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1753 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1754 16, /* rightshift */
5bd4f169
AM
1755 1, /* size (0 = byte, 1 = short, 2 = long) */
1756 16, /* bitsize */
b34976b6 1757 FALSE, /* pc_relative */
5bd4f169 1758 0, /* bitpos */
f9c6b907 1759 complain_overflow_signed, /* complain_on_overflow */
805fc799 1760 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1761 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1762 FALSE, /* partial_inplace */
d006db6c 1763 0, /* src_mask */
411e1bfb 1764 0xffff, /* dst_mask */
b34976b6 1765 FALSE), /* pcrel_offset */
5bd4f169 1766
411e1bfb
AM
1767 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1768 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1769 16, /* rightshift */
5bd4f169
AM
1770 1, /* size (0 = byte, 1 = short, 2 = long) */
1771 16, /* bitsize */
b34976b6 1772 FALSE, /* pc_relative */
5bd4f169 1773 0, /* bitpos */
f9c6b907 1774 complain_overflow_signed, /* complain_on_overflow */
805fc799 1775 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1776 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1777 FALSE, /* partial_inplace */
d006db6c 1778 0, /* src_mask */
411e1bfb 1779 0xffff, /* dst_mask */
b34976b6 1780 FALSE), /* pcrel_offset */
5bd4f169 1781
411e1bfb
AM
1782 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1783 the offset to the entry relative to the TOC base (r2). */
1784 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1785 0, /* rightshift */
1786 1, /* size (0 = byte, 1 = short, 2 = long) */
1787 16, /* bitsize */
b34976b6 1788 FALSE, /* pc_relative */
5bd4f169 1789 0, /* bitpos */
411e1bfb 1790 complain_overflow_signed, /* complain_on_overflow */
805fc799 1791 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1792 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1793 FALSE, /* partial_inplace */
d006db6c 1794 0, /* src_mask */
5bd4f169 1795 0xfffc, /* dst_mask */
b34976b6 1796 FALSE), /* pcrel_offset */
5bd4f169 1797
411e1bfb
AM
1798 /* Like GOT_DTPREL16_DS, but no overflow. */
1799 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1800 0, /* rightshift */
c061c2d8
AM
1801 1, /* size (0 = byte, 1 = short, 2 = long) */
1802 16, /* bitsize */
b34976b6 1803 FALSE, /* pc_relative */
5bd4f169 1804 0, /* bitpos */
411e1bfb
AM
1805 complain_overflow_dont, /* complain_on_overflow */
1806 ppc64_elf_unhandled_reloc, /* special_function */
1807 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1808 FALSE, /* partial_inplace */
d006db6c 1809 0, /* src_mask */
c061c2d8 1810 0xfffc, /* dst_mask */
b34976b6 1811 FALSE), /* pcrel_offset */
5bd4f169 1812
411e1bfb
AM
1813 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1814 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1815 16, /* rightshift */
5bd4f169
AM
1816 1, /* size (0 = byte, 1 = short, 2 = long) */
1817 16, /* bitsize */
b34976b6 1818 FALSE, /* pc_relative */
5bd4f169 1819 0, /* bitpos */
f9c6b907 1820 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1821 ppc64_elf_unhandled_reloc, /* special_function */
1822 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1823 FALSE, /* partial_inplace */
d006db6c 1824 0, /* src_mask */
411e1bfb 1825 0xffff, /* dst_mask */
b34976b6 1826 FALSE), /* pcrel_offset */
5bd4f169 1827
411e1bfb
AM
1828 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1829 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1830 16, /* rightshift */
1831 1, /* size (0 = byte, 1 = short, 2 = long) */
1832 16, /* bitsize */
1833 FALSE, /* pc_relative */
1834 0, /* bitpos */
f9c6b907 1835 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1836 ppc64_elf_unhandled_reloc, /* special_function */
1837 "R_PPC64_GOT_DTPREL16_HA", /* name */
1838 FALSE, /* partial_inplace */
1839 0, /* src_mask */
1840 0xffff, /* dst_mask */
1841 FALSE), /* pcrel_offset */
1842
1843 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1844 offset to the entry relative to the TOC base (r2). */
1845 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1846 0, /* rightshift */
1847 1, /* size (0 = byte, 1 = short, 2 = long) */
1848 16, /* bitsize */
b34976b6 1849 FALSE, /* pc_relative */
5bd4f169
AM
1850 0, /* bitpos */
1851 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1852 ppc64_elf_unhandled_reloc, /* special_function */
1853 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1854 FALSE, /* partial_inplace */
d006db6c 1855 0, /* src_mask */
ad8e1ba5 1856 0xfffc, /* dst_mask */
b34976b6 1857 FALSE), /* pcrel_offset */
5bd4f169 1858
411e1bfb
AM
1859 /* Like GOT_TPREL16_DS, but no overflow. */
1860 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1861 0, /* rightshift */
1862 1, /* size (0 = byte, 1 = short, 2 = long) */
1863 16, /* bitsize */
b34976b6 1864 FALSE, /* pc_relative */
5bd4f169
AM
1865 0, /* bitpos */
1866 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1867 ppc64_elf_unhandled_reloc, /* special_function */
1868 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1869 FALSE, /* partial_inplace */
d006db6c 1870 0, /* src_mask */
ad8e1ba5 1871 0xfffc, /* dst_mask */
b34976b6 1872 FALSE), /* pcrel_offset */
5bd4f169 1873
411e1bfb
AM
1874 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1875 HOWTO (R_PPC64_GOT_TPREL16_HI,
1876 16, /* rightshift */
5bd4f169
AM
1877 1, /* size (0 = byte, 1 = short, 2 = long) */
1878 16, /* bitsize */
b34976b6 1879 FALSE, /* pc_relative */
5bd4f169 1880 0, /* bitpos */
f9c6b907 1881 complain_overflow_signed, /* complain_on_overflow */
805fc799 1882 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1883 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1884 FALSE, /* partial_inplace */
d006db6c 1885 0, /* src_mask */
411e1bfb 1886 0xffff, /* dst_mask */
b34976b6 1887 FALSE), /* pcrel_offset */
5bd4f169 1888
411e1bfb
AM
1889 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1890 HOWTO (R_PPC64_GOT_TPREL16_HA,
1891 16, /* rightshift */
5bd4f169
AM
1892 1, /* size (0 = byte, 1 = short, 2 = long) */
1893 16, /* bitsize */
b34976b6 1894 FALSE, /* pc_relative */
5bd4f169 1895 0, /* bitpos */
f9c6b907 1896 complain_overflow_signed, /* complain_on_overflow */
805fc799 1897 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1898 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1899 FALSE, /* partial_inplace */
d006db6c 1900 0, /* src_mask */
411e1bfb 1901 0xffff, /* dst_mask */
b34976b6 1902 FALSE), /* pcrel_offset */
5bd4f169 1903
25f23106
AM
1904 HOWTO (R_PPC64_JMP_IREL, /* type */
1905 0, /* rightshift */
1906 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1907 0, /* bitsize */
1908 FALSE, /* pc_relative */
1909 0, /* bitpos */
1910 complain_overflow_dont, /* complain_on_overflow */
1911 ppc64_elf_unhandled_reloc, /* special_function */
1912 "R_PPC64_JMP_IREL", /* name */
1913 FALSE, /* partial_inplace */
1914 0, /* src_mask */
1915 0, /* dst_mask */
1916 FALSE), /* pcrel_offset */
1917
e054468f
AM
1918 HOWTO (R_PPC64_IRELATIVE, /* type */
1919 0, /* rightshift */
1920 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1921 64, /* bitsize */
1922 FALSE, /* pc_relative */
1923 0, /* bitpos */
1924 complain_overflow_dont, /* complain_on_overflow */
1925 bfd_elf_generic_reloc, /* special_function */
1926 "R_PPC64_IRELATIVE", /* name */
1927 FALSE, /* partial_inplace */
1928 0, /* src_mask */
1929 ONES (64), /* dst_mask */
1930 FALSE), /* pcrel_offset */
1931
25f23106
AM
1932 /* A 16 bit relative relocation. */
1933 HOWTO (R_PPC64_REL16, /* type */
1934 0, /* rightshift */
1935 1, /* size (0 = byte, 1 = short, 2 = long) */
1936 16, /* bitsize */
1937 TRUE, /* pc_relative */
1938 0, /* bitpos */
1939 complain_overflow_bitfield, /* complain_on_overflow */
1940 bfd_elf_generic_reloc, /* special_function */
1941 "R_PPC64_REL16", /* name */
1942 FALSE, /* partial_inplace */
1943 0, /* src_mask */
1944 0xffff, /* dst_mask */
1945 TRUE), /* pcrel_offset */
1946
1947 /* A 16 bit relative relocation without overflow. */
1948 HOWTO (R_PPC64_REL16_LO, /* type */
1949 0, /* rightshift */
1950 1, /* size (0 = byte, 1 = short, 2 = long) */
1951 16, /* bitsize */
1952 TRUE, /* pc_relative */
1953 0, /* bitpos */
1954 complain_overflow_dont,/* complain_on_overflow */
1955 bfd_elf_generic_reloc, /* special_function */
1956 "R_PPC64_REL16_LO", /* name */
1957 FALSE, /* partial_inplace */
1958 0, /* src_mask */
1959 0xffff, /* dst_mask */
1960 TRUE), /* pcrel_offset */
1961
1962 /* The high order 16 bits of a relative address. */
1963 HOWTO (R_PPC64_REL16_HI, /* type */
1964 16, /* rightshift */
1965 1, /* size (0 = byte, 1 = short, 2 = long) */
1966 16, /* bitsize */
1967 TRUE, /* pc_relative */
1968 0, /* bitpos */
f9c6b907 1969 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1970 bfd_elf_generic_reloc, /* special_function */
1971 "R_PPC64_REL16_HI", /* name */
1972 FALSE, /* partial_inplace */
1973 0, /* src_mask */
1974 0xffff, /* dst_mask */
1975 TRUE), /* pcrel_offset */
1976
1977 /* The high order 16 bits of a relative address, plus 1 if the contents of
1978 the low 16 bits, treated as a signed number, is negative. */
1979 HOWTO (R_PPC64_REL16_HA, /* type */
1980 16, /* rightshift */
1981 1, /* size (0 = byte, 1 = short, 2 = long) */
1982 16, /* bitsize */
1983 TRUE, /* pc_relative */
1984 0, /* bitpos */
f9c6b907 1985 complain_overflow_signed, /* complain_on_overflow */
25f23106
AM
1986 ppc64_elf_ha_reloc, /* special_function */
1987 "R_PPC64_REL16_HA", /* name */
1988 FALSE, /* partial_inplace */
1989 0, /* src_mask */
1990 0xffff, /* dst_mask */
1991 TRUE), /* pcrel_offset */
1992
f9c6b907
AM
1993 /* Like R_PPC64_ADDR16_HI, but no overflow. */
1994 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
1995 16, /* rightshift */
1996 1, /* size (0 = byte, 1 = short, 2 = long) */
1997 16, /* bitsize */
1998 FALSE, /* pc_relative */
1999 0, /* bitpos */
2000 complain_overflow_dont, /* complain_on_overflow */
2001 bfd_elf_generic_reloc, /* special_function */
2002 "R_PPC64_ADDR16_HIGH", /* name */
2003 FALSE, /* partial_inplace */
2004 0, /* src_mask */
2005 0xffff, /* dst_mask */
2006 FALSE), /* pcrel_offset */
2007
2008 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2009 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2010 16, /* rightshift */
2011 1, /* size (0 = byte, 1 = short, 2 = long) */
2012 16, /* bitsize */
2013 FALSE, /* pc_relative */
2014 0, /* bitpos */
2015 complain_overflow_dont, /* complain_on_overflow */
2016 ppc64_elf_ha_reloc, /* special_function */
2017 "R_PPC64_ADDR16_HIGHA", /* name */
2018 FALSE, /* partial_inplace */
2019 0, /* src_mask */
2020 0xffff, /* dst_mask */
2021 FALSE), /* pcrel_offset */
2022
2023 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2024 HOWTO (R_PPC64_DTPREL16_HIGH,
2025 16, /* rightshift */
2026 1, /* size (0 = byte, 1 = short, 2 = long) */
2027 16, /* bitsize */
2028 FALSE, /* pc_relative */
2029 0, /* bitpos */
2030 complain_overflow_dont, /* complain_on_overflow */
2031 ppc64_elf_unhandled_reloc, /* special_function */
2032 "R_PPC64_DTPREL16_HIGH", /* name */
2033 FALSE, /* partial_inplace */
2034 0, /* src_mask */
2035 0xffff, /* dst_mask */
2036 FALSE), /* pcrel_offset */
2037
2038 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2039 HOWTO (R_PPC64_DTPREL16_HIGHA,
2040 16, /* rightshift */
2041 1, /* size (0 = byte, 1 = short, 2 = long) */
2042 16, /* bitsize */
2043 FALSE, /* pc_relative */
2044 0, /* bitpos */
2045 complain_overflow_dont, /* complain_on_overflow */
2046 ppc64_elf_unhandled_reloc, /* special_function */
2047 "R_PPC64_DTPREL16_HIGHA", /* name */
2048 FALSE, /* partial_inplace */
2049 0, /* src_mask */
2050 0xffff, /* dst_mask */
2051 FALSE), /* pcrel_offset */
2052
2053 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2054 HOWTO (R_PPC64_TPREL16_HIGH,
2055 16, /* rightshift */
2056 1, /* size (0 = byte, 1 = short, 2 = long) */
2057 16, /* bitsize */
2058 FALSE, /* pc_relative */
2059 0, /* bitpos */
2060 complain_overflow_dont, /* complain_on_overflow */
2061 ppc64_elf_unhandled_reloc, /* special_function */
2062 "R_PPC64_TPREL16_HIGH", /* name */
2063 FALSE, /* partial_inplace */
2064 0, /* src_mask */
2065 0xffff, /* dst_mask */
2066 FALSE), /* pcrel_offset */
2067
2068 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2069 HOWTO (R_PPC64_TPREL16_HIGHA,
2070 16, /* rightshift */
2071 1, /* size (0 = byte, 1 = short, 2 = long) */
2072 16, /* bitsize */
2073 FALSE, /* pc_relative */
2074 0, /* bitpos */
2075 complain_overflow_dont, /* complain_on_overflow */
2076 ppc64_elf_unhandled_reloc, /* special_function */
2077 "R_PPC64_TPREL16_HIGHA", /* name */
2078 FALSE, /* partial_inplace */
2079 0, /* src_mask */
2080 0xffff, /* dst_mask */
2081 FALSE), /* pcrel_offset */
2082
5bd4f169
AM
2083 /* GNU extension to record C++ vtable hierarchy. */
2084 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2085 0, /* rightshift */
2086 0, /* size (0 = byte, 1 = short, 2 = long) */
2087 0, /* bitsize */
b34976b6 2088 FALSE, /* pc_relative */
5bd4f169
AM
2089 0, /* bitpos */
2090 complain_overflow_dont, /* complain_on_overflow */
2091 NULL, /* special_function */
2092 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 2093 FALSE, /* partial_inplace */
5bd4f169
AM
2094 0, /* src_mask */
2095 0, /* dst_mask */
b34976b6 2096 FALSE), /* pcrel_offset */
5bd4f169
AM
2097
2098 /* GNU extension to record C++ vtable member usage. */
2099 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2100 0, /* rightshift */
2101 0, /* size (0 = byte, 1 = short, 2 = long) */
2102 0, /* bitsize */
b34976b6 2103 FALSE, /* pc_relative */
5bd4f169
AM
2104 0, /* bitpos */
2105 complain_overflow_dont, /* complain_on_overflow */
2106 NULL, /* special_function */
2107 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2108 FALSE, /* partial_inplace */
5bd4f169
AM
2109 0, /* src_mask */
2110 0, /* dst_mask */
b34976b6 2111 FALSE), /* pcrel_offset */
5bd4f169
AM
2112};
2113
2114\f
2115/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2116 be done. */
2117
2118static void
4ce794b7 2119ppc_howto_init (void)
5bd4f169
AM
2120{
2121 unsigned int i, type;
2122
2123 for (i = 0;
2124 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2125 i++)
2126 {
2127 type = ppc64_elf_howto_raw[i].type;
2128 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2129 / sizeof (ppc64_elf_howto_table[0])));
2130 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2131 }
2132}
2133
2134static reloc_howto_type *
4ce794b7
AM
2135ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2136 bfd_reloc_code_real_type code)
5bd4f169 2137{
411e1bfb 2138 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2139
2140 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2141 /* Initialize howto table if needed. */
2142 ppc_howto_init ();
2143
4ce794b7 2144 switch (code)
5bd4f169
AM
2145 {
2146 default:
4ce794b7 2147 return NULL;
5bd4f169 2148
411e1bfb
AM
2149 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2150 break;
2151 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2152 break;
2153 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2154 break;
2155 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2156 break;
2157 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2158 break;
2159 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2160 break;
f9c6b907
AM
2161 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2162 break;
411e1bfb 2163 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2164 break;
f9c6b907
AM
2165 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2166 break;
411e1bfb 2167 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2168 break;
411e1bfb 2169 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2170 break;
411e1bfb 2171 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2172 break;
411e1bfb 2173 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2174 break;
411e1bfb 2175 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2176 break;
411e1bfb 2177 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2178 break;
411e1bfb 2179 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2180 break;
411e1bfb 2181 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2182 break;
411e1bfb 2183 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2184 break;
411e1bfb 2185 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2186 break;
411e1bfb 2187 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2188 break;
411e1bfb 2189 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2190 break;
411e1bfb 2191 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2192 break;
411e1bfb 2193 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2194 break;
411e1bfb 2195 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2196 break;
411e1bfb 2197 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2198 break;
411e1bfb 2199 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2200 break;
411e1bfb 2201 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2202 break;
411e1bfb 2203 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2204 break;
411e1bfb 2205 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2206 break;
411e1bfb 2207 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2208 break;
411e1bfb 2209 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2210 break;
411e1bfb 2211 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2212 break;
411e1bfb 2213 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2214 break;
411e1bfb 2215 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2216 break;
411e1bfb 2217 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2218 break;
411e1bfb 2219 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2220 break;
411e1bfb 2221 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2222 break;
411e1bfb 2223 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2224 break;
411e1bfb 2225 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2226 break;
411e1bfb 2227 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2228 break;
411e1bfb 2229 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2230 break;
411e1bfb 2231 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2232 break;
411e1bfb 2233 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2234 break;
411e1bfb 2235 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2236 break;
411e1bfb 2237 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2238 break;
411e1bfb 2239 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2240 break;
411e1bfb 2241 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2242 break;
411e1bfb 2243 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2244 break;
411e1bfb 2245 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2246 break;
411e1bfb 2247 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2248 break;
411e1bfb 2249 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2250 break;
411e1bfb 2251 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2252 break;
411e1bfb 2253 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2254 break;
411e1bfb 2255 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2256 break;
411e1bfb 2257 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2258 break;
411e1bfb 2259 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2260 break;
411e1bfb 2261 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2262 break;
411e1bfb 2263 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2264 break;
411e1bfb 2265 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2266 break;
411e1bfb 2267 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2268 break;
411e1bfb 2269 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2270 break;
411e1bfb 2271 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2272 break;
727fc41e
AM
2273 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2274 break;
2275 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2276 break;
411e1bfb 2277 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2278 break;
411e1bfb 2279 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2280 break;
411e1bfb 2281 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2282 break;
411e1bfb 2283 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2284 break;
f9c6b907
AM
2285 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2286 break;
411e1bfb 2287 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2288 break;
f9c6b907
AM
2289 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2290 break;
411e1bfb 2291 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2292 break;
411e1bfb
AM
2293 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2294 break;
2295 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2296 break;
2297 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2298 break;
f9c6b907
AM
2299 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2300 break;
411e1bfb
AM
2301 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2302 break;
f9c6b907
AM
2303 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2304 break;
411e1bfb
AM
2305 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2306 break;
2307 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2308 break;
2309 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2310 break;
2311 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2312 break;
2313 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2314 break;
2315 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2316 break;
2317 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2318 break;
2319 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2320 break;
2321 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2322 break;
2323 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2324 break;
2325 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2326 break;
2327 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2328 break;
2329 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2330 break;
2331 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2332 break;
2333 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2334 break;
2335 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2336 break;
2337 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2338 break;
2339 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2340 break;
2341 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2342 break;
2343 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2344 break;
2345 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2346 break;
2347 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2348 break;
2349 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2350 break;
2351 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2352 break;
2353 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2354 break;
2355 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2356 break;
2357 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2358 break;
2359 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2360 break;
2361 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2362 break;
25f23106
AM
2363 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2364 break;
2365 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2366 break;
2367 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2368 break;
2369 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2370 break;
411e1bfb
AM
2371 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2372 break;
2373 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2374 break;
2375 }
2376
4ce794b7 2377 return ppc64_elf_howto_table[r];
5bd4f169
AM
2378};
2379
157090f7
AM
2380static reloc_howto_type *
2381ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2382 const char *r_name)
2383{
2384 unsigned int i;
2385
2386 for (i = 0;
2387 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2388 i++)
2389 if (ppc64_elf_howto_raw[i].name != NULL
2390 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2391 return &ppc64_elf_howto_raw[i];
2392
2393 return NULL;
2394}
2395
5bd4f169
AM
2396/* Set the howto pointer for a PowerPC ELF reloc. */
2397
2398static void
4ce794b7
AM
2399ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2400 Elf_Internal_Rela *dst)
5bd4f169 2401{
65f38f15
AM
2402 unsigned int type;
2403
ef60b7ff 2404 /* Initialize howto table if needed. */
5bd4f169 2405 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2406 ppc_howto_init ();
2407
65f38f15 2408 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2409 if (type >= (sizeof (ppc64_elf_howto_table)
2410 / sizeof (ppc64_elf_howto_table[0])))
2411 {
2412 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2413 abfd, (int) type);
2414 type = R_PPC64_NONE;
d0fb9a8d 2415 }
65f38f15 2416 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2417}
2418
04c9666a 2419/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2420
2421static bfd_reloc_status_type
4ce794b7
AM
2422ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2423 void *data, asection *input_section,
2424 bfd *output_bfd, char **error_message)
5bd4f169 2425{
805fc799
AM
2426 /* If this is a relocatable link (output_bfd test tells us), just
2427 call the generic function. Any adjustment will be done at final
2428 link time. */
2429 if (output_bfd != NULL)
cedb70c5 2430 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2431 input_section, output_bfd, error_message);
2432
2433 /* Adjust the addend for sign extension of the low 16 bits.
2434 We won't actually be using the low 16 bits, so trashing them
2435 doesn't matter. */
2436 reloc_entry->addend += 0x8000;
2437 return bfd_reloc_continue;
2438}
5bd4f169 2439
2441e016
AM
2440static bfd_reloc_status_type
2441ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2442 void *data, asection *input_section,
2443 bfd *output_bfd, char **error_message)
2444{
2445 if (output_bfd != NULL)
2446 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2447 input_section, output_bfd, error_message);
2448
699733f6
AM
2449 if (strcmp (symbol->section->name, ".opd") == 0
2450 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2451 {
2452 bfd_vma dest = opd_entry_value (symbol->section,
2453 symbol->value + reloc_entry->addend,
aef36ac1 2454 NULL, NULL, FALSE);
2441e016
AM
2455 if (dest != (bfd_vma) -1)
2456 reloc_entry->addend = dest - (symbol->value
2457 + symbol->section->output_section->vma
2458 + symbol->section->output_offset);
2459 }
2460 return bfd_reloc_continue;
2461}
2462
805fc799 2463static bfd_reloc_status_type
4ce794b7
AM
2464ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2465 void *data, asection *input_section,
2466 bfd *output_bfd, char **error_message)
805fc799
AM
2467{
2468 long insn;
04c9666a 2469 enum elf_ppc64_reloc_type r_type;
805fc799 2470 bfd_size_type octets;
794e51c0
AM
2471 /* Assume 'at' branch hints. */
2472 bfd_boolean is_isa_v2 = TRUE;
805fc799
AM
2473
2474 /* If this is a relocatable link (output_bfd test tells us), just
2475 call the generic function. Any adjustment will be done at final
2476 link time. */
5bd4f169 2477 if (output_bfd != NULL)
cedb70c5 2478 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2479 input_section, output_bfd, error_message);
2480
2481 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2482 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2483 insn &= ~(0x01 << 21);
4ce794b7 2484 r_type = reloc_entry->howto->type;
805fc799
AM
2485 if (r_type == R_PPC64_ADDR14_BRTAKEN
2486 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2487 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799 2488
794e51c0 2489 if (is_isa_v2)
5bd4f169 2490 {
805fc799
AM
2491 /* Set 'a' bit. This is 0b00010 in BO field for branch
2492 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2493 for branch on CTR insns (BO == 1a00t or 1a01t). */
2494 if ((insn & (0x14 << 21)) == (0x04 << 21))
2495 insn |= 0x02 << 21;
2496 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2497 insn |= 0x08 << 21;
2498 else
2441e016 2499 goto out;
5bd4f169 2500 }
805fc799
AM
2501 else
2502 {
2503 bfd_vma target = 0;
2504 bfd_vma from;
5bd4f169 2505
805fc799
AM
2506 if (!bfd_is_com_section (symbol->section))
2507 target = symbol->value;
2508 target += symbol->section->output_section->vma;
2509 target += symbol->section->output_offset;
2510 target += reloc_entry->addend;
5bd4f169 2511
805fc799
AM
2512 from = (reloc_entry->address
2513 + input_section->output_offset
2514 + input_section->output_section->vma);
5bd4f169 2515
805fc799
AM
2516 /* Invert 'y' bit if not the default. */
2517 if ((bfd_signed_vma) (target - from) < 0)
2518 insn ^= 0x01 << 21;
2519 }
4ce794b7 2520 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2521 out:
2522 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2523 input_section, output_bfd, error_message);
805fc799 2524}
5bd4f169 2525
805fc799 2526static bfd_reloc_status_type
4ce794b7
AM
2527ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2528 void *data, asection *input_section,
2529 bfd *output_bfd, char **error_message)
805fc799
AM
2530{
2531 /* If this is a relocatable link (output_bfd test tells us), just
2532 call the generic function. Any adjustment will be done at final
2533 link time. */
2534 if (output_bfd != NULL)
cedb70c5 2535 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2536 input_section, output_bfd, error_message);
5bd4f169 2537
805fc799
AM
2538 /* Subtract the symbol section base address. */
2539 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2540 return bfd_reloc_continue;
2541}
2542
805fc799 2543static bfd_reloc_status_type
4ce794b7
AM
2544ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2545 void *data, asection *input_section,
2546 bfd *output_bfd, char **error_message)
805fc799
AM
2547{
2548 /* If this is a relocatable link (output_bfd test tells us), just
2549 call the generic function. Any adjustment will be done at final
2550 link time. */
2551 if (output_bfd != NULL)
cedb70c5 2552 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2553 input_section, output_bfd, error_message);
2554
2555 /* Subtract the symbol section base address. */
2556 reloc_entry->addend -= symbol->section->output_section->vma;
2557
2558 /* Adjust the addend for sign extension of the low 16 bits. */
2559 reloc_entry->addend += 0x8000;
2560 return bfd_reloc_continue;
2561}
2562
2563static bfd_reloc_status_type
4ce794b7
AM
2564ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2565 void *data, asection *input_section,
2566 bfd *output_bfd, char **error_message)
805fc799
AM
2567{
2568 bfd_vma TOCstart;
2569
2570 /* If this is a relocatable link (output_bfd test tells us), just
2571 call the generic function. Any adjustment will be done at final
2572 link time. */
2573 if (output_bfd != NULL)
cedb70c5 2574 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2575 input_section, output_bfd, error_message);
2576
2577 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2578 if (TOCstart == 0)
1c865ab2 2579 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2580
2581 /* Subtract the TOC base address. */
2582 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2583 return bfd_reloc_continue;
2584}
2585
2586static bfd_reloc_status_type
4ce794b7
AM
2587ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2588 void *data, asection *input_section,
2589 bfd *output_bfd, char **error_message)
805fc799
AM
2590{
2591 bfd_vma TOCstart;
2592
2593 /* If this is a relocatable link (output_bfd test tells us), just
2594 call the generic function. Any adjustment will be done at final
2595 link time. */
2596 if (output_bfd != NULL)
cedb70c5 2597 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2598 input_section, output_bfd, error_message);
2599
2600 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2601 if (TOCstart == 0)
1c865ab2 2602 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2603
2604 /* Subtract the TOC base address. */
2605 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2606
2607 /* Adjust the addend for sign extension of the low 16 bits. */
2608 reloc_entry->addend += 0x8000;
2609 return bfd_reloc_continue;
2610}
2611
2612static bfd_reloc_status_type
4ce794b7
AM
2613ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2614 void *data, asection *input_section,
2615 bfd *output_bfd, char **error_message)
805fc799
AM
2616{
2617 bfd_vma TOCstart;
2618 bfd_size_type octets;
2619
2620 /* If this is a relocatable link (output_bfd test tells us), just
2621 call the generic function. Any adjustment will be done at final
2622 link time. */
2623 if (output_bfd != NULL)
cedb70c5 2624 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2625 input_section, output_bfd, error_message);
2626
2627 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2628 if (TOCstart == 0)
1c865ab2 2629 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
805fc799
AM
2630
2631 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2632 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2633 return bfd_reloc_ok;
2634}
2635
2636static bfd_reloc_status_type
4ce794b7
AM
2637ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2638 void *data, asection *input_section,
2639 bfd *output_bfd, char **error_message)
805fc799
AM
2640{
2641 /* If this is a relocatable link (output_bfd test tells us), just
2642 call the generic function. Any adjustment will be done at final
2643 link time. */
2644 if (output_bfd != NULL)
cedb70c5 2645 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2646 input_section, output_bfd, error_message);
2647
2648 if (error_message != NULL)
2649 {
2650 static char buf[60];
2651 sprintf (buf, "generic linker can't handle %s",
2652 reloc_entry->howto->name);
2653 *error_message = buf;
2654 }
2655 return bfd_reloc_dangerous;
2656}
2657
927be08e
AM
2658/* Track GOT entries needed for a given symbol. We might need more
2659 than one got entry per symbol. */
2660struct got_entry
2661{
2662 struct got_entry *next;
2663
2664 /* The symbol addend that we'll be placing in the GOT. */
2665 bfd_vma addend;
2666
2667 /* Unlike other ELF targets, we use separate GOT entries for the same
2668 symbol referenced from different input files. This is to support
2669 automatic multiple TOC/GOT sections, where the TOC base can vary
2670 from one input file to another. After partitioning into TOC groups
2671 we merge entries within the group.
2672
2673 Point to the BFD owning this GOT entry. */
2674 bfd *owner;
2675
2676 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2677 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2678 unsigned char tls_type;
927be08e
AM
2679
2680 /* Non-zero if got.ent points to real entry. */
f961d9dd 2681 unsigned char is_indirect;
927be08e
AM
2682
2683 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2684 union
2685 {
2686 bfd_signed_vma refcount;
2687 bfd_vma offset;
2688 struct got_entry *ent;
2689 } got;
2690};
2691
2692/* The same for PLT. */
2693struct plt_entry
2694{
2695 struct plt_entry *next;
2696
2697 bfd_vma addend;
2698
2699 union
2700 {
2701 bfd_signed_vma refcount;
2702 bfd_vma offset;
2703 } plt;
2704};
2705
e717da7e
AM
2706struct ppc64_elf_obj_tdata
2707{
2708 struct elf_obj_tdata elf;
2709
2710 /* Shortcuts to dynamic linker sections. */
2711 asection *got;
2712 asection *relgot;
2713
b3fac117
AM
2714 /* Used during garbage collection. We attach global symbols defined
2715 on removed .opd entries to this section so that the sym is removed. */
2716 asection *deleted_section;
81688140 2717
927be08e 2718 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2719 sections means we potentially need one of these for each input bfd. */
927be08e 2720 struct got_entry tlsld_got;
8860955f 2721
729eabd5
AM
2722 union {
2723 /* A copy of relocs before they are modified for --emit-relocs. */
2724 Elf_Internal_Rela *relocs;
2725
2726 /* Section contents. */
2727 bfd_byte *contents;
2728 } opd;
d77c8a4b
AM
2729
2730 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2731 the reloc to be in the range -32768 to 32767. */
98528052
AM
2732 unsigned int has_small_toc_reloc : 1;
2733
560c8763
AM
2734 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2735 instruction not one we handle. */
2736 unsigned int unexpected_toc_insn : 1;
e717da7e
AM
2737};
2738
2739#define ppc64_elf_tdata(bfd) \
2740 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2741
2742#define ppc64_tlsld_got(bfd) \
2743 (&ppc64_elf_tdata (bfd)->tlsld_got)
2744
0c8d6e5c
AM
2745#define is_ppc64_elf(bfd) \
2746 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2747 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2748
e717da7e
AM
2749/* Override the generic function because we store some extras. */
2750
2751static bfd_boolean
2752ppc64_elf_mkobject (bfd *abfd)
2753{
0ffa91dd 2754 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2755 PPC64_ELF_DATA);
e717da7e
AM
2756}
2757
feee612b
AM
2758/* Fix bad default arch selected for a 64 bit input bfd when the
2759 default is 32 bit. */
2760
b34976b6 2761static bfd_boolean
4ce794b7 2762ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2763{
2764 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2765 {
2766 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2767
2768 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2769 {
2770 /* Relies on arch after 32 bit default being 64 bit default. */
2771 abfd->arch_info = abfd->arch_info->next;
2772 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2773 }
2774 }
b34976b6 2775 return TRUE;
feee612b
AM
2776}
2777
d37c89e5
AM
2778/* Support for core dump NOTE sections. */
2779
2780static bfd_boolean
2781ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2782{
eea6121a 2783 size_t offset, size;
d37c89e5
AM
2784
2785 if (note->descsz != 504)
2786 return FALSE;
2787
2788 /* pr_cursig */
228e534f 2789 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
d37c89e5
AM
2790
2791 /* pr_pid */
228e534f 2792 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2793
2794 /* pr_reg */
2795 offset = 112;
eea6121a 2796 size = 384;
d37c89e5
AM
2797
2798 /* Make a ".reg/999" section. */
2799 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2800 size, note->descpos + offset);
d37c89e5
AM
2801}
2802
2803static bfd_boolean
2804ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2805{
2806 if (note->descsz != 136)
2807 return FALSE;
2808
228e534f 2809 elf_tdata (abfd)->core->pid
bc989cdc 2810 = bfd_get_32 (abfd, note->descdata + 24);
228e534f 2811 elf_tdata (abfd)->core->program
d37c89e5 2812 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
228e534f 2813 elf_tdata (abfd)->core->command
d37c89e5
AM
2814 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2815
2816 return TRUE;
2817}
2818
183e98be
AM
2819static char *
2820ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2821 ...)
2822{
2823 switch (note_type)
2824 {
2825 default:
2826 return NULL;
2827
2828 case NT_PRPSINFO:
2829 {
2830 char data[136];
2831 va_list ap;
2832
2833 va_start (ap, note_type);
75cd47ed 2834 memset (data, 0, sizeof (data));
183e98be
AM
2835 strncpy (data + 40, va_arg (ap, const char *), 16);
2836 strncpy (data + 56, va_arg (ap, const char *), 80);
2837 va_end (ap);
2838 return elfcore_write_note (abfd, buf, bufsiz,
2839 "CORE", note_type, data, sizeof (data));
2840 }
2841
2842 case NT_PRSTATUS:
2843 {
2844 char data[504];
2845 va_list ap;
2846 long pid;
2847 int cursig;
2848 const void *greg;
2849
2850 va_start (ap, note_type);
2851 memset (data, 0, 112);
2852 pid = va_arg (ap, long);
2853 bfd_put_32 (abfd, pid, data + 32);
2854 cursig = va_arg (ap, int);
2855 bfd_put_16 (abfd, cursig, data + 12);
2856 greg = va_arg (ap, const void *);
2857 memcpy (data + 112, greg, 384);
2858 memset (data + 496, 0, 8);
2859 va_end (ap);
2860 return elfcore_write_note (abfd, buf, bufsiz,
2861 "CORE", note_type, data, sizeof (data));
2862 }
2863 }
2864}
2865
5d35169e
AM
2866/* Add extra PPC sections. */
2867
b35d266b 2868static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2869{
0112cd26
NC
2870 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2871 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2872 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2873 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2874 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2875 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2876 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2877};
2878
7c8fe5c4
AM
2879enum _ppc64_sec_type {
2880 sec_normal = 0,
2881 sec_opd = 1,
2882 sec_toc = 2
2883};
2884
f0abc2a1
AM
2885struct _ppc64_elf_section_data
2886{
2887 struct bfd_elf_section_data elf;
411e1bfb 2888
f0abc2a1
AM
2889 union
2890 {
74f0fb50
AM
2891 /* An array with one entry for each opd function descriptor. */
2892 struct _opd_sec_data
2893 {
2894 /* Points to the function code section for local opd entries. */
2895 asection **func_sec;
2896
2897 /* After editing .opd, adjust references to opd local syms. */
2898 long *adjust;
2899 } opd;
7c8fe5c4 2900
3a71aa26
AM
2901 /* An array for toc sections, indexed by offset/8. */
2902 struct _toc_sec_data
2903 {
2904 /* Specifies the relocation symbol index used at a given toc offset. */
2905 unsigned *symndx;
2906
2907 /* And the relocation addend. */
2908 bfd_vma *add;
2909 } toc;
7c8fe5c4
AM
2910 } u;
2911
2912 enum _ppc64_sec_type sec_type:2;
411e1bfb 2913
7c8fe5c4
AM
2914 /* Flag set when small branches are detected. Used to
2915 select suitable defaults for the stub group size. */
2916 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2917};
2918
2919#define ppc64_elf_section_data(sec) \
411e1bfb 2920 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2921
2922static bfd_boolean
4ce794b7 2923ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2924{
f592407e
AM
2925 if (!sec->used_by_bfd)
2926 {
2927 struct _ppc64_elf_section_data *sdata;
2928 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2929
f592407e
AM
2930 sdata = bfd_zalloc (abfd, amt);
2931 if (sdata == NULL)
2932 return FALSE;
2933 sec->used_by_bfd = sdata;
2934 }
f0abc2a1
AM
2935
2936 return _bfd_elf_new_section_hook (abfd, sec);
2937}
4025353c 2938
74f0fb50 2939static struct _opd_sec_data *
4025353c
AM
2940get_opd_info (asection * sec)
2941{
2942 if (sec != NULL
2943 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2944 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2945 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2946 return NULL;
2947}
ee67d69a
AM
2948
2949static inline int
2950abiversion (bfd *abfd)
2951{
2952 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2953}
2954
2955static inline void
2956set_abiversion (bfd *abfd, int ver)
2957{
2958 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2959 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2960}
90e3cdf2
JJ
2961\f
2962/* Parameters for the qsort hook. */
90e3cdf2
JJ
2963static bfd_boolean synthetic_relocatable;
2964
699733f6 2965/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2966
2967static int
2968compare_symbols (const void *ap, const void *bp)
2969{
2970 const asymbol *a = * (const asymbol **) ap;
2971 const asymbol *b = * (const asymbol **) bp;
2972
699733f6
AM
2973 /* Section symbols first. */
2974 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2975 return -1;
699733f6 2976 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2977 return 1;
2978
699733f6 2979 /* then .opd symbols. */
ffcfec52
AM
2980 if (strcmp (a->section->name, ".opd") == 0
2981 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2982 return -1;
ffcfec52
AM
2983 if (strcmp (a->section->name, ".opd") != 0
2984 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2985 return 1;
2986
699733f6 2987 /* then other code symbols. */
90e3cdf2
JJ
2988 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2989 == (SEC_CODE | SEC_ALLOC)
2990 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2991 != (SEC_CODE | SEC_ALLOC))
2992 return -1;
2993
2994 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2995 != (SEC_CODE | SEC_ALLOC)
2996 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2997 == (SEC_CODE | SEC_ALLOC))
2998 return 1;
2999
3000 if (synthetic_relocatable)
3001 {
3002 if (a->section->id < b->section->id)
3003 return -1;
3004
3005 if (a->section->id > b->section->id)
3006 return 1;
3007 }
3008
3009 if (a->value + a->section->vma < b->value + b->section->vma)
3010 return -1;
3011
3012 if (a->value + a->section->vma > b->value + b->section->vma)
3013 return 1;
3014
4d35a0aa
AM
3015 /* For syms with the same value, prefer strong dynamic global function
3016 syms over other syms. */
3017 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3018 return -1;
3019
3020 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3021 return 1;
3022
3023 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3024 return -1;
3025
3026 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3027 return 1;
3028
3029 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3030 return -1;
3031
3032 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3033 return 1;
3034
3035 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3036 return -1;
3037
3038 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3039 return 1;
3040
90e3cdf2
JJ
3041 return 0;
3042}
3043
699733f6 3044/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 3045
699733f6
AM
3046static asymbol *
3047sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 3048{
699733f6 3049 long mid;
90e3cdf2 3050
699733f6
AM
3051 if (id == -1)
3052 {
3053 while (lo < hi)
3054 {
3055 mid = (lo + hi) >> 1;
3056 if (syms[mid]->value + syms[mid]->section->vma < value)
3057 lo = mid + 1;
3058 else if (syms[mid]->value + syms[mid]->section->vma > value)
3059 hi = mid;
3060 else
3061 return syms[mid];
3062 }
3063 }
3064 else
3065 {
3066 while (lo < hi)
3067 {
3068 mid = (lo + hi) >> 1;
3069 if (syms[mid]->section->id < id)
3070 lo = mid + 1;
3071 else if (syms[mid]->section->id > id)
3072 hi = mid;
3073 else if (syms[mid]->value < value)
3074 lo = mid + 1;
3075 else if (syms[mid]->value > value)
3076 hi = mid;
3077 else
3078 return syms[mid];
3079 }
3080 }
3081 return NULL;
90e3cdf2
JJ
3082}
3083
468392fb
AM
3084static bfd_boolean
3085section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3086{
3087 bfd_vma vma = *(bfd_vma *) ptr;
3088 return ((section->flags & SEC_ALLOC) != 0
3089 && section->vma <= vma
3090 && vma < section->vma + section->size);
3091}
3092
699733f6 3093/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 3094 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
3095
3096static long
a7535cf3
AM
3097ppc64_elf_get_synthetic_symtab (bfd *abfd,
3098 long static_count, asymbol **static_syms,
3099 long dyn_count, asymbol **dyn_syms,
c9727e01 3100 asymbol **ret)
90e3cdf2
JJ
3101{
3102 asymbol *s;
699733f6
AM
3103 long i;
3104 long count;
90e3cdf2 3105 char *names;
a7535cf3 3106 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
ee67d69a 3107 asection *opd = NULL;
90e3cdf2 3108 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 3109 asymbol **syms;
ee67d69a 3110 int abi = abiversion (abfd);
90e3cdf2
JJ
3111
3112 *ret = NULL;
3113
ee67d69a
AM
3114 if (abi < 2)
3115 {
3116 opd = bfd_get_section_by_name (abfd, ".opd");
3117 if (opd == NULL && abi == 1)
3118 return 0;
3119 }
90e3cdf2 3120
a7535cf3 3121 symcount = static_count;
c9727e01 3122 if (!relocatable)
a7535cf3 3123 symcount += dyn_count;
90e3cdf2 3124 if (symcount == 0)
c9727e01 3125 return 0;
90e3cdf2 3126
a7535cf3
AM
3127 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3128 if (syms == NULL)
7356fed5 3129 return -1;
a7535cf3
AM
3130
3131 if (!relocatable && static_count != 0 && dyn_count != 0)
3132 {
3133 /* Use both symbol tables. */
3134 memcpy (syms, static_syms, static_count * sizeof (*syms));
3135 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3136 }
3137 else if (!relocatable && static_count == 0)
3138 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3139 else
3140 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3141
90e3cdf2 3142 synthetic_relocatable = relocatable;
595da8c5 3143 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3144
c9727e01
AM
3145 if (!relocatable && symcount > 1)
3146 {
3147 long j;
3148 /* Trim duplicate syms, since we may have merged the normal and
3149 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3150 different values, so trim any with the same value. */
c9727e01
AM
3151 for (i = 1, j = 1; i < symcount; ++i)
3152 if (syms[i - 1]->value + syms[i - 1]->section->vma
3153 != syms[i]->value + syms[i]->section->vma)
3154 syms[j++] = syms[i];
3155 symcount = j;
3156 }
3157
699733f6 3158 i = 0;
ffcfec52 3159 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3160 ++i;
3161 codesecsym = i;
90e3cdf2 3162
699733f6
AM
3163 for (; i < symcount; ++i)
3164 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3165 != (SEC_CODE | SEC_ALLOC))
3166 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3167 break;
3168 codesecsymend = i;
90e3cdf2 3169
699733f6
AM
3170 for (; i < symcount; ++i)
3171 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3172 break;
3173 secsymend = i;
90e3cdf2 3174
699733f6 3175 for (; i < symcount; ++i)
ffcfec52 3176 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3177 break;
3178 opdsymend = i;
90e3cdf2 3179
699733f6
AM
3180 for (; i < symcount; ++i)
3181 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3182 != (SEC_CODE | SEC_ALLOC))
3183 break;
3184 symcount = i;
3185
c9727e01 3186 count = 0;
90e3cdf2 3187
699733f6 3188 if (relocatable)
90e3cdf2 3189 {
699733f6
AM
3190 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3191 arelent *r;
3192 size_t size;
3193 long relcount;
90e3cdf2 3194
468392fb
AM
3195 if (opdsymend == secsymend)
3196 goto done;
3197
699733f6 3198 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3199 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3200 if (relcount == 0)
c9727e01 3201 goto done;
90e3cdf2 3202
7356fed5
AM
3203 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3204 {
3205 count = -1;
3206 goto done;
3207 }
3208
699733f6 3209 size = 0;
595da8c5 3210 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3211 {
3212 asymbol *sym;
90e3cdf2 3213
595da8c5 3214 while (r < opd->relocation + relcount
699733f6
AM
3215 && r->address < syms[i]->value + opd->vma)
3216 ++r;
90e3cdf2 3217
595da8c5 3218 if (r == opd->relocation + relcount)
699733f6 3219 break;
90e3cdf2 3220
699733f6
AM
3221 if (r->address != syms[i]->value + opd->vma)
3222 continue;
90e3cdf2 3223
699733f6
AM
3224 if (r->howto->type != R_PPC64_ADDR64)
3225 continue;
90e3cdf2 3226
699733f6
AM
3227 sym = *r->sym_ptr_ptr;
3228 if (!sym_exists_at (syms, opdsymend, symcount,
3229 sym->section->id, sym->value + r->addend))
3230 {
3231 ++count;
3232 size += sizeof (asymbol);
3233 size += strlen (syms[i]->name) + 2;
3234 }
3235 }
90e3cdf2 3236
699733f6
AM
3237 s = *ret = bfd_malloc (size);
3238 if (s == NULL)
3239 {
7356fed5 3240 count = -1;
c9727e01 3241 goto done;
699733f6 3242 }
90e3cdf2 3243
699733f6 3244 names = (char *) (s + count);
90e3cdf2 3245
595da8c5 3246 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3247 {
699733f6 3248 asymbol *sym;
90e3cdf2 3249
595da8c5 3250 while (r < opd->relocation + relcount
699733f6
AM
3251 && r->address < syms[i]->value + opd->vma)
3252 ++r;
90e3cdf2 3253
595da8c5 3254 if (r == opd->relocation + relcount)
699733f6
AM
3255 break;
3256
3257 if (r->address != syms[i]->value + opd->vma)
3258 continue;
3259
3260 if (r->howto->type != R_PPC64_ADDR64)
3261 continue;
90e3cdf2 3262
699733f6
AM
3263 sym = *r->sym_ptr_ptr;
3264 if (!sym_exists_at (syms, opdsymend, symcount,
3265 sym->section->id, sym->value + r->addend))
3266 {
3267 size_t len;
3268
3269 *s = *syms[i];
6ba2a415 3270 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3271 s->section = sym->section;
3272 s->value = sym->value + r->addend;
3273 s->name = names;
3274 *names++ = '.';
3275 len = strlen (syms[i]->name);
3276 memcpy (names, syms[i]->name, len + 1);
3277 names += len + 1;
6f610d07
UW
3278 /* Have udata.p point back to the original symbol this
3279 synthetic symbol was derived from. */
3280 s->udata.p = syms[i];
699733f6
AM
3281 s++;
3282 }
3283 }
3284 }
3285 else
90e3cdf2 3286 {
468392fb 3287 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
ee67d69a 3288 bfd_byte *contents = NULL;
699733f6 3289 size_t size;
468392fb
AM
3290 long plt_count = 0;
3291 bfd_vma glink_vma = 0, resolv_vma = 0;
3292 asection *dynamic, *glink = NULL, *relplt = NULL;
3293 arelent *p;
90e3cdf2 3294
ee67d69a 3295 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
699733f6 3296 {
ee67d69a 3297 free_contents_and_exit:
699733f6 3298 if (contents)
ee67d69a 3299 free (contents);
7356fed5 3300 count = -1;
c9727e01 3301 goto done;
699733f6 3302 }
90e3cdf2 3303
699733f6
AM
3304 size = 0;
3305 for (i = secsymend; i < opdsymend; ++i)
3306 {
3307 bfd_vma ent;
90e3cdf2 3308
5ef11c02
AM
3309 /* Ignore bogus symbols. */
3310 if (syms[i]->value > opd->size - 8)
3311 continue;
3312
699733f6
AM
3313 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3314 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3315 {
3316 ++count;
3317 size += sizeof (asymbol);
3318 size += strlen (syms[i]->name) + 2;
3319 }
3320 }
90e3cdf2 3321
468392fb 3322 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3323 if (dyn_count != 0
3324 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3325 {
3326 bfd_byte *dynbuf, *extdyn, *extdynend;
3327 size_t extdynsize;
3328 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3329
3330 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3331 goto free_contents_and_exit;
3332
3333 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3334 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3335
3336 extdyn = dynbuf;
3337 extdynend = extdyn + dynamic->size;
3338 for (; extdyn < extdynend; extdyn += extdynsize)
3339 {
3340 Elf_Internal_Dyn dyn;
3341 (*swap_dyn_in) (abfd, extdyn, &dyn);
3342
3343 if (dyn.d_tag == DT_NULL)
3344 break;
3345
3346 if (dyn.d_tag == DT_PPC64_GLINK)
3347 {
3348 /* The first glink stub starts at offset 32; see comment in
3349 ppc64_elf_finish_dynamic_sections. */
3350 glink_vma = dyn.d_un.d_val + 32;
3351 /* The .glink section usually does not survive the final
3352 link; search for the section (usually .text) where the
3353 glink stubs now reside. */
3354 glink = bfd_sections_find_if (abfd, section_covers_vma,
3355 &glink_vma);
3356 break;
3357 }
3358 }
3359
3360 free (dynbuf);
3361 }
3362
3363 if (glink != NULL)
3364 {
3365 /* Determine __glink trampoline by reading the relative branch
3366 from the first glink stub. */
3367 bfd_byte buf[4];
3368 if (bfd_get_section_contents (abfd, glink, buf,
3369 glink_vma + 4 - glink->vma, 4))
3370 {
3371 unsigned int insn = bfd_get_32 (abfd, buf);
3372 insn ^= B_DOT;
3373 if ((insn & ~0x3fffffc) == 0)
3374 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3375 }
3376
3377 if (resolv_vma)
3378 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3379
066ee829
AM
3380 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3381 if (relplt != NULL)
3382 {
3383 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3384 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3385 goto free_contents_and_exit;
68ffbac6 3386
066ee829
AM
3387 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3388 size += plt_count * sizeof (asymbol);
468392fb 3389
066ee829
AM
3390 p = relplt->relocation;
3391 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3392 {
3393 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3394 if (p->addend != 0)
3395 size += sizeof ("+0x") - 1 + 16;
3396 }
066ee829 3397 }
468392fb
AM
3398 }
3399
699733f6
AM
3400 s = *ret = bfd_malloc (size);
3401 if (s == NULL)
7356fed5 3402 goto free_contents_and_exit;
90e3cdf2 3403
468392fb 3404 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3405
699733f6 3406 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3407 {
699733f6 3408 bfd_vma ent;
90e3cdf2 3409
5ef11c02
AM
3410 if (syms[i]->value > opd->size - 8)
3411 continue;
3412
699733f6
AM
3413 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3414 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3415 {
c9727e01 3416 long lo, hi;
699733f6 3417 size_t len;
c9727e01 3418 asection *sec = abfd->sections;
90e3cdf2 3419
699733f6
AM
3420 *s = *syms[i];
3421 lo = codesecsym;
3422 hi = codesecsymend;
3423 while (lo < hi)
3424 {
c9727e01 3425 long mid = (lo + hi) >> 1;
699733f6
AM
3426 if (syms[mid]->section->vma < ent)
3427 lo = mid + 1;
3428 else if (syms[mid]->section->vma > ent)
3429 hi = mid;
3430 else
c9727e01
AM
3431 {
3432 sec = syms[mid]->section;
3433 break;
3434 }
699733f6
AM
3435 }
3436
c9727e01 3437 if (lo >= hi && lo > codesecsym)
699733f6 3438 sec = syms[lo - 1]->section;
699733f6
AM
3439
3440 for (; sec != NULL; sec = sec->next)
3441 {
3442 if (sec->vma > ent)
3443 break;
63524580
JK
3444 /* SEC_LOAD may not be set if SEC is from a separate debug
3445 info file. */
3446 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3447 break;
3448 if ((sec->flags & SEC_CODE) != 0)
3449 s->section = sec;
3450 }
6ba2a415 3451 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3452 s->value = ent - s->section->vma;
3453 s->name = names;
3454 *names++ = '.';
3455 len = strlen (syms[i]->name);
3456 memcpy (names, syms[i]->name, len + 1);
3457 names += len + 1;
6f610d07
UW
3458 /* Have udata.p point back to the original symbol this
3459 synthetic symbol was derived from. */
3460 s->udata.p = syms[i];
699733f6 3461 s++;
90e3cdf2 3462 }
90e3cdf2 3463 }
699733f6 3464 free (contents);
468392fb
AM
3465
3466 if (glink != NULL && relplt != NULL)
3467 {
3468 if (resolv_vma)
3469 {
3470 /* Add a symbol for the main glink trampoline. */
86a4952b 3471 memset (s, 0, sizeof *s);
468392fb 3472 s->the_bfd = abfd;
6ba2a415 3473 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3474 s->section = glink;
3475 s->value = resolv_vma - glink->vma;
3476 s->name = names;
3477 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3478 names += sizeof ("__glink_PLTresolve");
3479 s++;
3480 count++;
3481 }
3482
3483 /* FIXME: It would be very much nicer to put sym@plt on the
3484 stub rather than on the glink branch table entry. The
3485 objdump disassembler would then use a sensible symbol
3486 name on plt calls. The difficulty in doing so is
3487 a) finding the stubs, and,
3488 b) matching stubs against plt entries, and,
3489 c) there can be multiple stubs for a given plt entry.
3490
3491 Solving (a) could be done by code scanning, but older
3492 ppc64 binaries used different stubs to current code.
3493 (b) is the tricky one since you need to known the toc
3494 pointer for at least one function that uses a pic stub to
3495 be able to calculate the plt address referenced.
3496 (c) means gdb would need to set multiple breakpoints (or
3497 find the glink branch itself) when setting breakpoints
3498 for pending shared library loads. */
3499 p = relplt->relocation;
3500 for (i = 0; i < plt_count; i++, p++)
3501 {
3502 size_t len;
3503
3504 *s = **p->sym_ptr_ptr;
3505 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3506 we are defining a symbol, ensure one of them is set. */
3507 if ((s->flags & BSF_LOCAL) == 0)
3508 s->flags |= BSF_GLOBAL;
6ba2a415 3509 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3510 s->section = glink;
3511 s->value = glink_vma - glink->vma;
3512 s->name = names;
3513 s->udata.p = NULL;
3514 len = strlen ((*p->sym_ptr_ptr)->name);
3515 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3516 names += len;
e054468f
AM
3517 if (p->addend != 0)
3518 {
3519 memcpy (names, "+0x", sizeof ("+0x") - 1);
3520 names += sizeof ("+0x") - 1;
3521 bfd_sprintf_vma (abfd, names, p->addend);
3522 names += strlen (names);
3523 }
468392fb
AM
3524 memcpy (names, "@plt", sizeof ("@plt"));
3525 names += sizeof ("@plt");
3526 s++;
3527 glink_vma += 8;
3528 if (i >= 0x8000)
3529 glink_vma += 4;
3530 }
3531 count += plt_count;
3532 }
90e3cdf2
JJ
3533 }
3534
c9727e01 3535 done:
a7535cf3 3536 free (syms);
90e3cdf2
JJ
3537 return count;
3538}
5bd4f169 3539\f
65f38f15
AM
3540/* The following functions are specific to the ELF linker, while
3541 functions above are used generally. Those named ppc64_elf_* are
3542 called by the main ELF linker code. They appear in this file more
3543 or less in the order in which they are called. eg.
3544 ppc64_elf_check_relocs is called early in the link process,
3545 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3546 called.
3547
3548 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3549 functions have both a function code symbol and a function descriptor
3550 symbol. A call to foo in a relocatable object file looks like:
3551
3552 . .text
3553 . x:
3554 . bl .foo
3555 . nop
3556
3557 The function definition in another object file might be:
3558
3559 . .section .opd
3560 . foo: .quad .foo
3561 . .quad .TOC.@tocbase
3562 . .quad 0
3563 .
3564 . .text
3565 . .foo: blr
3566
3567 When the linker resolves the call during a static link, the branch
3568 unsurprisingly just goes to .foo and the .opd information is unused.
3569 If the function definition is in a shared library, things are a little
3570 different: The call goes via a plt call stub, the opd information gets
3571 copied to the plt, and the linker patches the nop.
3572
3573 . x:
3574 . bl .foo_stub
3575 . ld 2,40(1)
3576 .
3577 .
3578 . .foo_stub:
71a39c98
AM
3579 . std 2,40(1) # in practice, the call stub
3580 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3581 . addi 11,11,Lfoo@toc@l # this is the general idea
3582 . ld 12,0(11)
3583 . ld 2,8(11)
3584 . mtctr 12
3585 . ld 11,16(11)
e86ce104
AM
3586 . bctr
3587 .
3588 . .section .plt
3589 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3590
3591 The "reloc ()" notation is supposed to indicate that the linker emits
3592 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3593 copying.
3594
3595 What are the difficulties here? Well, firstly, the relocations
3596 examined by the linker in check_relocs are against the function code
3597 sym .foo, while the dynamic relocation in the plt is emitted against
3598 the function descriptor symbol, foo. Somewhere along the line, we need
3599 to carefully copy dynamic link information from one symbol to the other.
3600 Secondly, the generic part of the elf linker will make .foo a dynamic
3601 symbol as is normal for most other backends. We need foo dynamic
3602 instead, at least for an application final link. However, when
3603 creating a shared library containing foo, we need to have both symbols
3604 dynamic so that references to .foo are satisfied during the early
3605 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3606 definition from some other object, eg. a static library.
3607
3608 Update: As of August 2004, we support a new convention. Function
3609 calls may use the function descriptor symbol, ie. "bl foo". This
3610 behaves exactly as "bl .foo". */
65f38f15 3611
1d483afe 3612/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3613 selects those that must be copied when linking a shared library,
3614 even when the symbol is local. */
65f38f15 3615
1d483afe
AM
3616static int
3617must_be_dyn_reloc (struct bfd_link_info *info,
3618 enum elf_ppc64_reloc_type r_type)
3619{
3620 switch (r_type)
3621 {
3622 default:
3623 return 1;
3624
3625 case R_PPC64_REL32:
3626 case R_PPC64_REL64:
3627 case R_PPC64_REL30:
3628 return 0;
3629
3630 case R_PPC64_TPREL16:
3631 case R_PPC64_TPREL16_LO:
3632 case R_PPC64_TPREL16_HI:
3633 case R_PPC64_TPREL16_HA:
3634 case R_PPC64_TPREL16_DS:
3635 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
3636 case R_PPC64_TPREL16_HIGH:
3637 case R_PPC64_TPREL16_HIGHA:
1d483afe
AM
3638 case R_PPC64_TPREL16_HIGHER:
3639 case R_PPC64_TPREL16_HIGHERA:
3640 case R_PPC64_TPREL16_HIGHEST:
3641 case R_PPC64_TPREL16_HIGHESTA:
3642 case R_PPC64_TPREL64:
3643 return !info->executable;
3644 }
3645}
65f38f15 3646
f4656909
AM
3647/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3648 copying dynamic variables from a shared lib into an app's dynbss
3649 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3650 shared lib. With code that gcc generates, it's vital that this be
3651 enabled; In the PowerPC64 ABI, the address of a function is actually
3652 the address of a function descriptor, which resides in the .opd
3653 section. gcc uses the descriptor directly rather than going via the
3654 GOT as some other ABI's do, which means that initialized function
3655 pointers must reference the descriptor. Thus, a function pointer
3656 initialized to the address of a function in a shared library will
3657 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3658 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3659 presents a problem as a plt entry for that function is also
3660 initialized from the function descriptor symbol and the copy reloc
3661 may not be initialized first. */
a23b6845 3662#define ELIMINATE_COPY_RELOCS 1
f4656909 3663
721956f4 3664/* Section name for stubs is the associated section name plus this
29942be8
NC
3665 string. */
3666#define STUB_SUFFIX ".stub"
721956f4
AM
3667
3668/* Linker stubs.
3669 ppc_stub_long_branch:
3670 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3671 destination, but a 24 bit branch in a stub section will reach.
3672 . b dest
3673
3674 ppc_stub_plt_branch:
3675 Similar to the above, but a 24 bit branch in the stub section won't
3676 reach its destination.
71a39c98
AM
3677 . addis %r11,%r2,xxx@toc@ha
3678 . ld %r12,xxx@toc@l(%r11)
3679 . mtctr %r12
721956f4
AM
3680 . bctr
3681
3682 ppc_stub_plt_call:
2c66dc6c
AM
3683 Used to call a function in a shared library. If it so happens that
3684 the plt entry referenced crosses a 64k boundary, then an extra
71a39c98 3685 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
721956f4 3686 . std %r2,40(%r1)
71a39c98
AM
3687 . addis %r11,%r2,xxx@toc@ha
3688 . ld %r12,xxx+0@toc@l(%r11)
3689 . mtctr %r12
3690 . ld %r2,xxx+8@toc@l(%r11)
3691 . ld %r11,xxx+16@toc@l(%r11)
721956f4 3692 . bctr
ad8e1ba5
AM
3693
3694 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3695 code to adjust the value and save r2 to support multiple toc sections.
3696 A ppc_stub_long_branch with an r2 offset looks like:
3697 . std %r2,40(%r1)
3698 . addis %r2,%r2,off@ha
3699 . addi %r2,%r2,off@l
3700 . b dest
3701
3702 A ppc_stub_plt_branch with an r2 offset looks like:
3703 . std %r2,40(%r1)
71a39c98
AM
3704 . addis %r11,%r2,xxx@toc@ha
3705 . ld %r12,xxx@toc@l(%r11)
ad8e1ba5
AM
3706 . addis %r2,%r2,off@ha
3707 . addi %r2,%r2,off@l
71a39c98 3708 . mtctr %r12
ad8e1ba5 3709 . bctr
ac2df442
AM
3710
3711 In cases where the "addis" instruction would add zero, the "addis" is
3712 omitted and following instructions modified slightly in some cases.
721956f4
AM
3713*/
3714
3715enum ppc_stub_type {
3716 ppc_stub_none,
3717 ppc_stub_long_branch,
ad8e1ba5 3718 ppc_stub_long_branch_r2off,
721956f4 3719 ppc_stub_plt_branch,
ad8e1ba5 3720 ppc_stub_plt_branch_r2off,
794e51c0
AM
3721 ppc_stub_plt_call,
3722 ppc_stub_plt_call_r2save
721956f4
AM
3723};
3724
3725struct ppc_stub_hash_entry {
3726
3727 /* Base hash table entry structure. */
3728 struct bfd_hash_entry root;
3729
ad8e1ba5
AM
3730 enum ppc_stub_type stub_type;
3731
721956f4
AM
3732 /* The stub section. */
3733 asection *stub_sec;
3734
3735 /* Offset within stub_sec of the beginning of this stub. */
3736 bfd_vma stub_offset;
3737
3738 /* Given the symbol's value and its section we can determine its final
3739 value when building the stubs (so the stub knows where to jump. */
3740 bfd_vma target_value;
3741 asection *target_section;
3742
721956f4
AM
3743 /* The symbol table entry, if any, that this was derived from. */
3744 struct ppc_link_hash_entry *h;
e054468f 3745 struct plt_entry *plt_ent;
721956f4
AM
3746
3747 /* Where this stub is being called from, or, in the case of combined
3748 stub sections, the first input section in the group. */
3749 asection *id_sec;
6911b7dc
AM
3750
3751 /* Symbol st_other. */
3752 unsigned char other;
721956f4
AM
3753};
3754
3755struct ppc_branch_hash_entry {
3756
3757 /* Base hash table entry structure. */
3758 struct bfd_hash_entry root;
3759
c456f082 3760 /* Offset within branch lookup table. */
721956f4
AM
3761 unsigned int offset;
3762
3763 /* Generation marker. */
3764 unsigned int iter;
3765};
65f38f15 3766
19e08130
AM
3767/* Used to track dynamic relocations for local symbols. */
3768struct ppc_dyn_relocs
3769{
3770 struct ppc_dyn_relocs *next;
3771
3772 /* The input section of the reloc. */
3773 asection *sec;
3774
3775 /* Total number of relocs copied for the input section. */
3776 unsigned int count : 31;
3777
3778 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3779 unsigned int ifunc : 1;
3780};
3781
65f38f15
AM
3782struct ppc_link_hash_entry
3783{
3784 struct elf_link_hash_entry elf;
3785
b3fac117
AM
3786 union {
3787 /* A pointer to the most recently used stub hash entry against this
3788 symbol. */
3789 struct ppc_stub_hash_entry *stub_cache;
3790
3791 /* A pointer to the next symbol starting with a '.' */
3792 struct ppc_link_hash_entry *next_dot_sym;
3793 } u;
721956f4 3794
65f38f15 3795 /* Track dynamic relocs copied for this symbol. */
6061a67d 3796 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3797
721956f4 3798 /* Link between function code and descriptor symbols. */
34814b9f 3799 struct ppc_link_hash_entry *oh;
721956f4 3800
e86ce104
AM
3801 /* Flag function code and descriptor symbols. */
3802 unsigned int is_func:1;
3803 unsigned int is_func_descriptor:1;
908b32fc 3804 unsigned int fake:1;
411e1bfb 3805
c5614fa4
AM
3806 /* Whether global opd/toc sym has been adjusted or not.
3807 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3808 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3809 unsigned int adjust_done:1;
3810
99877b66
AM
3811 /* Set if we twiddled this symbol to weak at some stage. */
3812 unsigned int was_undefined:1;
3813
411e1bfb 3814 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3815 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3816 corresponding relocs are encountered during check_relocs.
3817 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3818 indicate the corresponding GOT entry type is not needed.
3819 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3820 a TPREL one. We use a separate flag rather than setting TPREL
3821 just for convenience in distinguishing the two cases. */
3822#define TLS_GD 1 /* GD reloc. */
3823#define TLS_LD 2 /* LD reloc. */
3824#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3825#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3826#define TLS_TLS 16 /* Any TLS reloc. */
3827#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3828#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3829#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3830 unsigned char tls_mask;
65f38f15
AM
3831};
3832
3833/* ppc64 ELF linker hash table. */
3834
3835struct ppc_link_hash_table
3836{
3837 struct elf_link_hash_table elf;
3838
721956f4
AM
3839 /* The stub hash table. */
3840 struct bfd_hash_table stub_hash_table;
3841
3842 /* Another hash table for plt_branch stubs. */
3843 struct bfd_hash_table branch_hash_table;
3844
3b421ab3
AM
3845 /* Hash table for function prologue tocsave. */
3846 htab_t tocsave_htab;
3847
721956f4
AM
3848 /* Linker stub bfd. */
3849 bfd *stub_bfd;
3850
3851 /* Linker call-backs. */
4ce794b7
AM
3852 asection * (*add_stub_section) (const char *, asection *);
3853 void (*layout_sections_again) (void);
721956f4
AM
3854
3855 /* Array to keep track of which stub sections have been created, and
3856 information on stub grouping. */
3857 struct map_stub {
3858 /* This is the section to which stubs in the group will be attached. */
3859 asection *link_sec;
3860 /* The stub section. */
3861 asection *stub_sec;
ad8e1ba5
AM
3862 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3863 bfd_vma toc_off;
721956f4
AM
3864 } *stub_group;
3865
ad8e1ba5
AM
3866 /* Temp used when calculating TOC pointers. */
3867 bfd_vma toc_curr;
bf102f86
AM
3868 bfd *toc_bfd;
3869 asection *toc_first_sec;
ad8e1ba5 3870
8f3bab57
AM
3871 /* Highest input section id. */
3872 int top_id;
3873
734b6cf9
AM
3874 /* Highest output section index. */
3875 int top_index;
3876
b3fac117
AM
3877 /* Used when adding symbols. */
3878 struct ppc_link_hash_entry *dot_syms;
3879
734b6cf9
AM
3880 /* List of input sections for each output section. */
3881 asection **input_list;
721956f4 3882
65f38f15 3883 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3884 asection *got;
4ce794b7
AM
3885 asection *plt;
3886 asection *relplt;
e054468f
AM
3887 asection *iplt;
3888 asection *reliplt;
4ce794b7
AM
3889 asection *dynbss;
3890 asection *relbss;
3891 asection *glink;
82bd7b59 3892 asection *sfpr;
4ce794b7
AM
3893 asection *brlt;
3894 asection *relbrlt;
58d180e8 3895 asection *glink_eh_frame;
ec338859 3896
8387904d
AM
3897 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3898 struct ppc_link_hash_entry *tls_get_addr;
3899 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3900
927be08e
AM
3901 /* The size of reliplt used by got entry relocs. */
3902 bfd_size_type got_reli_size;
3903
9b5ecbd0 3904 /* Statistics. */
794e51c0 3905 unsigned long stub_count[ppc_stub_plt_call_r2save];
9b5ecbd0 3906
ee75fd95
AM
3907 /* Number of stubs against global syms. */
3908 unsigned long stub_globals;
3909
794e51c0
AM
3910 /* Alignment of PLT call stubs. */
3911 unsigned int plt_stub_align:4;
3912
ee67d69a
AM
3913 /* Set if we're linking code with function descriptors. */
3914 unsigned int opd_abi:1;
3915
9df0ef5f
AM
3916 /* Set if PLT call stubs should load r11. */
3917 unsigned int plt_static_chain:1;
3918
794e51c0
AM
3919 /* Set if PLT call stubs need a read-read barrier. */
3920 unsigned int plt_thread_safe:1;
3921
ad8e1ba5 3922 /* Set if we should emit symbols for stubs. */
99877b66 3923 unsigned int emit_stub_syms:1;
ad8e1ba5 3924
a7f2871e
AM
3925 /* Set if __tls_get_addr optimization should not be done. */
3926 unsigned int no_tls_get_addr_opt:1;
3927
4c52953f 3928 /* Support for multiple toc sections. */
33c0ec9d 3929 unsigned int do_multi_toc:1;
4c52953f 3930 unsigned int multi_toc_needed:1;
927be08e 3931 unsigned int second_toc_pass:1;
67f0cbdb 3932 unsigned int do_toc_opt:1;
4c52953f 3933
5d1634d7 3934 /* Set on error. */
99877b66 3935 unsigned int stub_error:1;
721956f4 3936
7d9616d7 3937 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3938 unsigned int twiddled_syms:1;
721956f4
AM
3939
3940 /* Incremented every time we size stubs. */
3941 unsigned int stub_iteration;
5d1634d7 3942
87d72d41
AM
3943 /* Small local sym cache. */
3944 struct sym_cache sym_cache;
65f38f15
AM
3945};
3946
4c52953f
AM
3947/* Rename some of the generic section flags to better document how they
3948 are used here. */
b0dddeec
AM
3949
3950/* Nonzero if this section has TLS related relocations. */
3951#define has_tls_reloc sec_flg0
3952
3953/* Nonzero if this section has a call to __tls_get_addr. */
3954#define has_tls_get_addr_call sec_flg1
3955
3956/* Nonzero if this section has any toc or got relocs. */
3957#define has_toc_reloc sec_flg2
3958
3959/* Nonzero if this section has a call to another section that uses
3960 the toc or got. */
d77c8a4b 3961#define makes_toc_func_call sec_flg3
b0dddeec
AM
3962
3963/* Recursion protection when determining above flag. */
d77c8a4b 3964#define call_check_in_progress sec_flg4
70cc837d 3965#define call_check_done sec_flg5
4c52953f 3966
65f38f15
AM
3967/* Get the ppc64 ELF linker hash table from a link_info structure. */
3968
3969#define ppc_hash_table(p) \
4dfe6ac6
NC
3970 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3971 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3972
721956f4
AM
3973#define ppc_stub_hash_lookup(table, string, create, copy) \
3974 ((struct ppc_stub_hash_entry *) \
3975 bfd_hash_lookup ((table), (string), (create), (copy)))
3976
3977#define ppc_branch_hash_lookup(table, string, create, copy) \
3978 ((struct ppc_branch_hash_entry *) \
3979 bfd_hash_lookup ((table), (string), (create), (copy)))
3980
3981/* Create an entry in the stub hash table. */
3982
3983static struct bfd_hash_entry *
4ce794b7
AM
3984stub_hash_newfunc (struct bfd_hash_entry *entry,
3985 struct bfd_hash_table *table,
3986 const char *string)
721956f4
AM
3987{
3988 /* Allocate the structure if it has not already been allocated by a
3989 subclass. */
3990 if (entry == NULL)
3991 {
3992 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3993 if (entry == NULL)
3994 return entry;
3995 }
3996
3997 /* Call the allocation method of the superclass. */
3998 entry = bfd_hash_newfunc (entry, table, string);
3999 if (entry != NULL)
4000 {
4001 struct ppc_stub_hash_entry *eh;
4002
4003 /* Initialize the local fields. */
4004 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 4005 eh->stub_type = ppc_stub_none;
721956f4
AM
4006 eh->stub_sec = NULL;
4007 eh->stub_offset = 0;
4008 eh->target_value = 0;
4009 eh->target_section = NULL;
721956f4 4010 eh->h = NULL;
6911b7dc 4011 eh->plt_ent = NULL;
721956f4 4012 eh->id_sec = NULL;
6911b7dc 4013 eh->other = 0;
721956f4
AM
4014 }
4015
4016 return entry;
4017}
4018
4019/* Create an entry in the branch hash table. */
4020
4021static struct bfd_hash_entry *
4ce794b7
AM
4022branch_hash_newfunc (struct bfd_hash_entry *entry,
4023 struct bfd_hash_table *table,
4024 const char *string)
721956f4
AM
4025{
4026 /* Allocate the structure if it has not already been allocated by a
4027 subclass. */
4028 if (entry == NULL)
4029 {
4030 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4031 if (entry == NULL)
4032 return entry;
4033 }
4034
4035 /* Call the allocation method of the superclass. */
4036 entry = bfd_hash_newfunc (entry, table, string);
4037 if (entry != NULL)
4038 {
4039 struct ppc_branch_hash_entry *eh;
4040
4041 /* Initialize the local fields. */
4042 eh = (struct ppc_branch_hash_entry *) entry;
4043 eh->offset = 0;
4044 eh->iter = 0;
4045 }
4046
4047 return entry;
4048}
4049
65f38f15
AM
4050/* Create an entry in a ppc64 ELF linker hash table. */
4051
4052static struct bfd_hash_entry *
4ce794b7
AM
4053link_hash_newfunc (struct bfd_hash_entry *entry,
4054 struct bfd_hash_table *table,
4055 const char *string)
65f38f15
AM
4056{
4057 /* Allocate the structure if it has not already been allocated by a
4058 subclass. */
4059 if (entry == NULL)
4060 {
4061 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4062 if (entry == NULL)
4063 return entry;
4064 }
4065
4066 /* Call the allocation method of the superclass. */
4067 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4068 if (entry != NULL)
4069 {
4070 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4071
b3fac117 4072 memset (&eh->u.stub_cache, 0,
908b32fc 4073 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
4074 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4075
4076 /* When making function calls, old ABI code references function entry
4077 points (dot symbols), while new ABI code references the function
4078 descriptor symbol. We need to make any combination of reference and
4079 definition work together, without breaking archive linking.
4080
4081 For a defined function "foo" and an undefined call to "bar":
4082 An old object defines "foo" and ".foo", references ".bar" (possibly
4083 "bar" too).
4084 A new object defines "foo" and references "bar".
4085
4086 A new object thus has no problem with its undefined symbols being
4087 satisfied by definitions in an old object. On the other hand, the
4088 old object won't have ".bar" satisfied by a new object.
4089
4090 Keep a list of newly added dot-symbols. */
4091
4092 if (string[0] == '.')
4093 {
4094 struct ppc_link_hash_table *htab;
4095
4096 htab = (struct ppc_link_hash_table *) table;
4097 eh->u.next_dot_sym = htab->dot_syms;
4098 htab->dot_syms = eh;
4099 }
65f38f15
AM
4100 }
4101
4102 return entry;
4103}
4104
3b421ab3
AM
4105struct tocsave_entry {
4106 asection *sec;
4107 bfd_vma offset;
4108};
4109
4110static hashval_t
4111tocsave_htab_hash (const void *p)
4112{
4113 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4114 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4115}
4116
4117static int
4118tocsave_htab_eq (const void *p1, const void *p2)
4119{
4120 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4121 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4122 return e1->sec == e2->sec && e1->offset == e2->offset;
4123}
4124
65f38f15
AM
4125/* Create a ppc64 ELF linker hash table. */
4126
4127static struct bfd_link_hash_table *
4ce794b7 4128ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
4129{
4130 struct ppc_link_hash_table *htab;
4131 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4132
4ce794b7 4133 htab = bfd_zmalloc (amt);
65f38f15
AM
4134 if (htab == NULL)
4135 return NULL;
4136
66eb6687 4137 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
4138 sizeof (struct ppc_link_hash_entry),
4139 PPC64_ELF_DATA))
65f38f15 4140 {
e2d34d7d 4141 free (htab);
65f38f15
AM
4142 return NULL;
4143 }
4144
721956f4 4145 /* Init the stub hash table too. */
66eb6687
AM
4146 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4147 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
4148 return NULL;
4149
4150 /* And the branch hash table. */
66eb6687
AM
4151 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4152 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
4153 return NULL;
4154
3b421ab3
AM
4155 htab->tocsave_htab = htab_try_create (1024,
4156 tocsave_htab_hash,
4157 tocsave_htab_eq,
4158 NULL);
4159 if (htab->tocsave_htab == NULL)
4160 return NULL;
4161
3254fd24
AM
4162 /* Initializing two fields of the union is just cosmetic. We really
4163 only care about glist, but when compiled on a 32-bit host the
4164 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4165 debugger inspection of these fields look nicer. */
a6aa5195
AM
4166 htab->elf.init_got_refcount.refcount = 0;
4167 htab->elf.init_got_refcount.glist = NULL;
4168 htab->elf.init_plt_refcount.refcount = 0;
4169 htab->elf.init_plt_refcount.glist = NULL;
4170 htab->elf.init_got_offset.offset = 0;
4171 htab->elf.init_got_offset.glist = NULL;
4172 htab->elf.init_plt_offset.offset = 0;
4173 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4174
65f38f15
AM
4175 return &htab->elf.root;
4176}
4177
721956f4
AM
4178/* Free the derived linker hash table. */
4179
4180static void
4ce794b7 4181ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4 4182{
3b421ab3 4183 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
721956f4 4184
3b421ab3
AM
4185 bfd_hash_table_free (&htab->stub_hash_table);
4186 bfd_hash_table_free (&htab->branch_hash_table);
4187 if (htab->tocsave_htab)
4188 htab_delete (htab->tocsave_htab);
9f7c3e5e 4189 _bfd_elf_link_hash_table_free (hash);
721956f4
AM
4190}
4191
bfeb4a28
AM
4192/* Create sections for linker generated code. */
4193
4194static bfd_boolean
4195create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4196{
4197 struct ppc_link_hash_table *htab;
4198 flagword flags;
4199
4200 htab = ppc_hash_table (info);
4201
4202 /* Create .sfpr for code to save and restore fp regs. */
4203 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4204 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4205 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4206 flags);
4207 if (htab->sfpr == NULL
4208 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4209 return FALSE;
4210
4211 /* Create .glink for lazy dynamic linking support. */
4212 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4213 flags);
4214 if (htab->glink == NULL
4215 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4216 return FALSE;
4217
4218 if (!info->no_ld_generated_unwind_info)
4219 {
4220 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4221 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4222 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4223 ".eh_frame",
4224 flags);
4225 if (htab->glink_eh_frame == NULL
4226 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4227 return FALSE;
4228 }
4229
4230 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4231 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4232 if (htab->iplt == NULL
4233 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4234 return FALSE;
4235
4236 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4237 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4238 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4239 ".rela.iplt",
4240 flags);
4241 if (htab->reliplt == NULL
4242 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4243 return FALSE;
4244
4245 /* Create branch lookup table for plt_branch stubs. */
4246 flags = (SEC_ALLOC | SEC_LOAD
4247 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4248 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4249 flags);
4250 if (htab->brlt == NULL
4251 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4252 return FALSE;
4253
4254 if (!info->shared)
4255 return TRUE;
4256
4257 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4258 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4259 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4260 ".rela.branch_lt",
4261 flags);
4262 if (htab->relbrlt == NULL
4263 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4264 return FALSE;
4265
4266 return TRUE;
4267}
4268
e717da7e
AM
4269/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4270
bfeb4a28 4271bfd_boolean
e717da7e
AM
4272ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4273{
4274 struct ppc_link_hash_table *htab;
4275
4276 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4277
4278/* Always hook our dynamic sections into the first bfd, which is the
4279 linker created stub bfd. This ensures that the GOT header is at
4280 the start of the output TOC section. */
4281 htab = ppc_hash_table (info);
4dfe6ac6 4282 if (htab == NULL)
bfeb4a28 4283 return FALSE;
e717da7e
AM
4284 htab->stub_bfd = abfd;
4285 htab->elf.dynobj = abfd;
bfeb4a28
AM
4286
4287 if (info->relocatable)
4288 return TRUE;
4289
4290 return create_linkage_sections (htab->elf.dynobj, info);
e717da7e
AM
4291}
4292
721956f4
AM
4293/* Build a name for an entry in the stub hash table. */
4294
4295static char *
4ce794b7
AM
4296ppc_stub_name (const asection *input_section,
4297 const asection *sym_sec,
4298 const struct ppc_link_hash_entry *h,
4299 const Elf_Internal_Rela *rel)
721956f4
AM
4300{
4301 char *stub_name;
bcaa2f82 4302 ssize_t len;
721956f4
AM
4303
4304 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4305 offsets from a sym as a branch target? In fact, we could
4306 probably assume the addend is always zero. */
4307 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4308
4309 if (h)
4310 {
4311 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4312 stub_name = bfd_malloc (len);
46de2a7c
AM
4313 if (stub_name == NULL)
4314 return stub_name;
4315
bcaa2f82
AM
4316 len = sprintf (stub_name, "%08x.%s+%x",
4317 input_section->id & 0xffffffff,
4318 h->elf.root.root.string,
4319 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4320 }
4321 else
4322 {
ad8e1ba5 4323 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4324 stub_name = bfd_malloc (len);
46de2a7c
AM
4325 if (stub_name == NULL)
4326 return stub_name;
4327
bcaa2f82
AM
4328 len = sprintf (stub_name, "%08x.%x:%x+%x",
4329 input_section->id & 0xffffffff,
4330 sym_sec->id & 0xffffffff,
4331 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4332 (int) rel->r_addend & 0xffffffff);
721956f4 4333 }
bcaa2f82 4334 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
ee75fd95 4335 stub_name[len - 2] = 0;
721956f4
AM
4336 return stub_name;
4337}
4338
4339/* Look up an entry in the stub hash. Stub entries are cached because
4340 creating the stub name takes a bit of time. */
4341
4342static struct ppc_stub_hash_entry *
4ce794b7
AM
4343ppc_get_stub_entry (const asection *input_section,
4344 const asection *sym_sec,
039b3fef 4345 struct ppc_link_hash_entry *h,
4ce794b7
AM
4346 const Elf_Internal_Rela *rel,
4347 struct ppc_link_hash_table *htab)
721956f4
AM
4348{
4349 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4350 const asection *id_sec;
4351
4352 /* If this input section is part of a group of sections sharing one
4353 stub section, then use the id of the first section in the group.
4354 Stub names need to include a section id, as there may well be
4355 more than one stub used to reach say, printf, and we need to
4356 distinguish between them. */
4357 id_sec = htab->stub_group[input_section->id].link_sec;
4358
b3fac117
AM
4359 if (h != NULL && h->u.stub_cache != NULL
4360 && h->u.stub_cache->h == h
4361 && h->u.stub_cache->id_sec == id_sec)
721956f4 4362 {
b3fac117 4363 stub_entry = h->u.stub_cache;
721956f4
AM
4364 }
4365 else
4366 {
4367 char *stub_name;
4368
4369 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4370 if (stub_name == NULL)
4371 return NULL;
4372
4373 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4374 stub_name, FALSE, FALSE);
721956f4 4375 if (h != NULL)
b3fac117 4376 h->u.stub_cache = stub_entry;
721956f4
AM
4377
4378 free (stub_name);
4379 }
4380
4381 return stub_entry;
4382}
4383
4384/* Add a new stub entry to the stub hash. Not all fields of the new
4385 stub entry are initialised. */
4386
4387static struct ppc_stub_hash_entry *
4ce794b7
AM
4388ppc_add_stub (const char *stub_name,
4389 asection *section,
25f53a85 4390 struct bfd_link_info *info)
721956f4 4391{
25f53a85 4392 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4393 asection *link_sec;
4394 asection *stub_sec;
4395 struct ppc_stub_hash_entry *stub_entry;
4396
4397 link_sec = htab->stub_group[section->id].link_sec;
4398 stub_sec = htab->stub_group[section->id].stub_sec;
4399 if (stub_sec == NULL)
4400 {
4401 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4402 if (stub_sec == NULL)
4403 {
d4c88bbb 4404 size_t namelen;
721956f4
AM
4405 bfd_size_type len;
4406 char *s_name;
4407
d4c88bbb
AM
4408 namelen = strlen (link_sec->name);
4409 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4410 s_name = bfd_alloc (htab->stub_bfd, len);
4411 if (s_name == NULL)
4412 return NULL;
4413
d4c88bbb
AM
4414 memcpy (s_name, link_sec->name, namelen);
4415 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4416 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4417 if (stub_sec == NULL)
4418 return NULL;
4419 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4420 }
4421 htab->stub_group[section->id].stub_sec = stub_sec;
4422 }
4423
4424 /* Enter this entry into the linker stub hash table. */
4425 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4426 TRUE, FALSE);
721956f4
AM
4427 if (stub_entry == NULL)
4428 {
8de848d8 4429 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4430 section->owner, stub_name);
721956f4
AM
4431 return NULL;
4432 }
4433
4434 stub_entry->stub_sec = stub_sec;
4435 stub_entry->stub_offset = 0;
4436 stub_entry->id_sec = link_sec;
4437 return stub_entry;
4438}
4439
e717da7e
AM
4440/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4441 not already done. */
65f38f15 4442
b34976b6 4443static bfd_boolean
e717da7e 4444create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4445{
e717da7e
AM
4446 asection *got, *relgot;
4447 flagword flags;
4448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4449
0c8d6e5c 4450 if (!is_ppc64_elf (abfd))
0ffa91dd 4451 return FALSE;
4dfe6ac6
NC
4452 if (htab == NULL)
4453 return FALSE;
0ffa91dd 4454
4ce794b7 4455 if (!htab->got)
e717da7e
AM
4456 {
4457 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4458 return FALSE;
4459
3d4d4302 4460 htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
e717da7e
AM
4461 if (!htab->got)
4462 abort ();
4463 }
4464
4465 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4466 | SEC_LINKER_CREATED);
4467
c456f082 4468 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4469 if (!got
e717da7e
AM
4470 || !bfd_set_section_alignment (abfd, got, 3))
4471 return FALSE;
65f38f15 4472
c456f082
AM
4473 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4474 flags | SEC_READONLY);
e717da7e 4475 if (!relgot
e717da7e 4476 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4477 return FALSE;
e717da7e
AM
4478
4479 ppc64_elf_tdata (abfd)->got = got;
4480 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4481 return TRUE;
65f38f15 4482}
5bd4f169 4483
82bd7b59 4484/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4485
b34976b6 4486static bfd_boolean
4ce794b7 4487ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4488{
65f38f15 4489 struct ppc_link_hash_table *htab;
5bd4f169 4490
65f38f15 4491 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4492 return FALSE;
65f38f15 4493
e717da7e 4494 htab = ppc_hash_table (info);
4dfe6ac6
NC
4495 if (htab == NULL)
4496 return FALSE;
4497
e717da7e 4498 if (!htab->got)
3d4d4302
AM
4499 htab->got = bfd_get_linker_section (dynobj, ".got");
4500 htab->plt = bfd_get_linker_section (dynobj, ".plt");
4501 htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4502 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
65f38f15 4503 if (!info->shared)
3d4d4302 4504 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
65f38f15 4505
e717da7e 4506 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4507 || (!info->shared && !htab->relbss))
65f38f15
AM
4508 abort ();
4509
b34976b6 4510 return TRUE;
5bd4f169
AM
4511}
4512
b31867b6
AM
4513/* Follow indirect and warning symbol links. */
4514
4515static inline struct bfd_link_hash_entry *
4516follow_link (struct bfd_link_hash_entry *h)
4517{
4518 while (h->type == bfd_link_hash_indirect
4519 || h->type == bfd_link_hash_warning)
4520 h = h->u.i.link;
4521 return h;
4522}
4523
4524static inline struct elf_link_hash_entry *
4525elf_follow_link (struct elf_link_hash_entry *h)
4526{
4527 return (struct elf_link_hash_entry *) follow_link (&h->root);
4528}
4529
4530static inline struct ppc_link_hash_entry *
4531ppc_follow_link (struct ppc_link_hash_entry *h)
4532{
4533 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4534}
4535
40d16e0b
AM
4536/* Merge PLT info on FROM with that on TO. */
4537
4538static void
4539move_plt_plist (struct ppc_link_hash_entry *from,
4540 struct ppc_link_hash_entry *to)
4541{
4542 if (from->elf.plt.plist != NULL)
4543 {
4544 if (to->elf.plt.plist != NULL)
4545 {
4546 struct plt_entry **entp;
4547 struct plt_entry *ent;
4548
4549 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4550 {
4551 struct plt_entry *dent;
4552
4553 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4554 if (dent->addend == ent->addend)
4555 {
4556 dent->plt.refcount += ent->plt.refcount;
4557 *entp = ent->next;
4558 break;
4559 }
4560 if (dent == NULL)
4561 entp = &ent->next;
4562 }
4563 *entp = to->elf.plt.plist;
4564 }
4565
4566 to->elf.plt.plist = from->elf.plt.plist;
4567 from->elf.plt.plist = NULL;
4568 }
4569}
4570
65f38f15
AM
4571/* Copy the extra info we tack onto an elf_link_hash_entry. */
4572
4573static void
fcfa13d2
AM
4574ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4575 struct elf_link_hash_entry *dir,
4576 struct elf_link_hash_entry *ind)
65f38f15
AM
4577{
4578 struct ppc_link_hash_entry *edir, *eind;
4579
4580 edir = (struct ppc_link_hash_entry *) dir;
4581 eind = (struct ppc_link_hash_entry *) ind;
4582
c79d6685
AM
4583 edir->is_func |= eind->is_func;
4584 edir->is_func_descriptor |= eind->is_func_descriptor;
4585 edir->tls_mask |= eind->tls_mask;
4586 if (eind->oh != NULL)
4587 edir->oh = ppc_follow_link (eind->oh);
4588
4589 /* If called to transfer flags for a weakdef during processing
4590 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4591 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4592 if (!(ELIMINATE_COPY_RELOCS
4593 && eind->elf.root.type != bfd_link_hash_indirect
4594 && edir->elf.dynamic_adjusted))
4595 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4596
4597 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4598 edir->elf.ref_regular |= eind->elf.ref_regular;
4599 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4600 edir->elf.needs_plt |= eind->elf.needs_plt;
4601
411e1bfb 4602 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4603 if (eind->dyn_relocs != NULL)
65f38f15 4604 {
bbd7ec4a
AM
4605 if (edir->dyn_relocs != NULL)
4606 {
6061a67d
AM
4607 struct elf_dyn_relocs **pp;
4608 struct elf_dyn_relocs *p;
bbd7ec4a 4609
fcfa13d2 4610 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4611 list. Merge any entries against the same section. */
4612 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4613 {
6061a67d 4614 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4615
4616 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4617 if (q->sec == p->sec)
4618 {
4619 q->pc_count += p->pc_count;
4620 q->count += p->count;
4621 *pp = p->next;
4622 break;
4623 }
4624 if (q == NULL)
4625 pp = &p->next;
4626 }
4627 *pp = edir->dyn_relocs;
4628 }
4629
65f38f15
AM
4630 edir->dyn_relocs = eind->dyn_relocs;
4631 eind->dyn_relocs = NULL;
4632 }
65f38f15 4633
68ba6d40
AM
4634 /* If we were called to copy over info for a weak sym, that's all.
4635 You might think dyn_relocs need not be copied over; After all,
4636 both syms will be dynamic or both non-dynamic so we're just
68ffbac6 4637 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
68ba6d40
AM
4638 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4639 dyn_relocs in read-only sections, and it does so on what is the
4640 DIR sym here. */
4641 if (eind->elf.root.type != bfd_link_hash_indirect)
4642 return;
4643
81848ca0
AM
4644 /* Copy over got entries that we may have already seen to the
4645 symbol which just became indirect. */
411e1bfb
AM
4646 if (eind->elf.got.glist != NULL)
4647 {
4648 if (edir->elf.got.glist != NULL)
4649 {
4650 struct got_entry **entp;
4651 struct got_entry *ent;
4652
4653 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4654 {
4655 struct got_entry *dent;
4656
4657 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4658 if (dent->addend == ent->addend
e717da7e 4659 && dent->owner == ent->owner
411e1bfb
AM
4660 && dent->tls_type == ent->tls_type)
4661 {
4662 dent->got.refcount += ent->got.refcount;
4663 *entp = ent->next;
4664 break;
4665 }
4666 if (dent == NULL)
4667 entp = &ent->next;
4668 }
4669 *entp = edir->elf.got.glist;
4670 }
4671
4672 edir->elf.got.glist = eind->elf.got.glist;
4673 eind->elf.got.glist = NULL;
4674 }
4675
4676 /* And plt entries. */
40d16e0b 4677 move_plt_plist (eind, edir);
411e1bfb 4678
fcfa13d2 4679 if (eind->elf.dynindx != -1)
411e1bfb 4680 {
fcfa13d2
AM
4681 if (edir->elf.dynindx != -1)
4682 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4683 edir->elf.dynstr_index);
411e1bfb
AM
4684 edir->elf.dynindx = eind->elf.dynindx;
4685 edir->elf.dynstr_index = eind->elf.dynstr_index;
4686 eind->elf.dynindx = -1;
4687 eind->elf.dynstr_index = 0;
4688 }
411e1bfb
AM
4689}
4690
8387904d
AM
4691/* Find the function descriptor hash entry from the given function code
4692 hash entry FH. Link the entries via their OH fields. */
4693
4694static struct ppc_link_hash_entry *
b31867b6 4695lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4696{
4697 struct ppc_link_hash_entry *fdh = fh->oh;
4698
4699 if (fdh == NULL)
4700 {
4701 const char *fd_name = fh->elf.root.root.string + 1;
4702
4703 fdh = (struct ppc_link_hash_entry *)
4704 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4705 if (fdh == NULL)
4706 return fdh;
4707
4708 fdh->is_func_descriptor = 1;
4709 fdh->oh = fh;
4710 fh->is_func = 1;
4711 fh->oh = fdh;
8387904d
AM
4712 }
4713
b31867b6 4714 return ppc_follow_link (fdh);
8387904d
AM
4715}
4716
bb700d78
AM
4717/* Make a fake function descriptor sym for the code sym FH. */
4718
4719static struct ppc_link_hash_entry *
4720make_fdh (struct bfd_link_info *info,
908b32fc 4721 struct ppc_link_hash_entry *fh)
bb700d78
AM
4722{
4723 bfd *abfd;
4724 asymbol *newsym;
4725 struct bfd_link_hash_entry *bh;
4726 struct ppc_link_hash_entry *fdh;
4727
4728 abfd = fh->elf.root.u.undef.abfd;
4729 newsym = bfd_make_empty_symbol (abfd);
4730 newsym->name = fh->elf.root.root.string + 1;
4731 newsym->section = bfd_und_section_ptr;
4732 newsym->value = 0;
908b32fc 4733 newsym->flags = BSF_WEAK;
bb700d78
AM
4734
4735 bh = NULL;
4736 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4737 newsym->flags, newsym->section,
4738 newsym->value, NULL, FALSE, FALSE,
4739 &bh))
4740 return NULL;
4741
4742 fdh = (struct ppc_link_hash_entry *) bh;
4743 fdh->elf.non_elf = 0;
908b32fc
AM
4744 fdh->fake = 1;
4745 fdh->is_func_descriptor = 1;
4746 fdh->oh = fh;
4747 fh->is_func = 1;
4748 fh->oh = fdh;
bb700d78
AM
4749 return fdh;
4750}
4751
8387904d
AM
4752/* Fix function descriptor symbols defined in .opd sections to be
4753 function type. */
555cd476
AM
4754
4755static bfd_boolean
c16153ae 4756ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4757 struct bfd_link_info *info,
555cd476 4758 Elf_Internal_Sym *isym,
6911b7dc 4759 const char **name,
555cd476
AM
4760 flagword *flags ATTRIBUTE_UNUSED,
4761 asection **sec,
4762 bfd_vma *value ATTRIBUTE_UNUSED)
4763{
f64b2e8d
NC
4764 if ((ibfd->flags & DYNAMIC) == 0
4765 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4766 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4767
e054468f 4768 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4769 {
4770 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4771 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4772 }
e054468f
AM
4773 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4774 ;
4775 else if (*sec != NULL
70cc837d 4776 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4777 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4778
6911b7dc
AM
4779 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4780 {
4781 if (abiversion (ibfd) == 0)
4782 set_abiversion (ibfd, 2);
4783 else if (abiversion (ibfd) == 1)
4784 {
4785 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4786 " for ABI version 1\n"), name);
4787 bfd_set_error (bfd_error_bad_value);
4788 return FALSE;
4789 }
4790 }
4791
555cd476
AM
4792 return TRUE;
4793}
4794
6911b7dc
AM
4795/* Merge non-visibility st_other attributes: local entry point. */
4796
4797static void
4798ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4799 const Elf_Internal_Sym *isym,
4800 bfd_boolean definition,
4801 bfd_boolean dynamic)
4802{
4803 if (definition && !dynamic)
4804 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4805 | ELF_ST_VISIBILITY (h->other));
4806}
4807
8387904d 4808/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4809 inclusion of a new ABI object archive that defines "bar".
4810 NAME is a symbol defined in an archive. Return a symbol in the hash
4811 table that might be satisfied by the archive symbols. */
8387904d
AM
4812
4813static struct elf_link_hash_entry *
4814ppc64_elf_archive_symbol_lookup (bfd *abfd,
4815 struct bfd_link_info *info,
4816 const char *name)
4817{
4818 struct elf_link_hash_entry *h;
4819 char *dot_name;
4820 size_t len;
4821
4822 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4823 if (h != NULL
4824 /* Don't return this sym if it is a fake function descriptor
4825 created by add_symbol_adjust. */
4826 && !(h->root.type == bfd_link_hash_undefweak
4827 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4828 return h;
4829
4830 if (name[0] == '.')
4831 return h;
4832
4833 len = strlen (name);
4834 dot_name = bfd_alloc (abfd, len + 2);
4835 if (dot_name == NULL)
4836 return (struct elf_link_hash_entry *) 0 - 1;
4837 dot_name[0] = '.';
4838 memcpy (dot_name + 1, name, len + 1);
4839 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4840 bfd_release (abfd, dot_name);
4841 return h;
4842}
4843
4844/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4845 new ABI object defines "bar". Well, at least, undefined dot symbols
4846 are made weak. This stops later archive searches from including an
4847 object if we already have a function descriptor definition. It also
35b0ce59
AM
4848 prevents the linker complaining about undefined symbols.
4849 We also check and correct mismatched symbol visibility here. The
4850 most restrictive visibility of the function descriptor and the
4851 function entry symbol is used. */
8387904d
AM
4852
4853static bfd_boolean
b3fac117 4854add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4855{
8387904d
AM
4856 struct ppc_link_hash_table *htab;
4857 struct ppc_link_hash_entry *fdh;
4858
b3fac117 4859 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4860 return TRUE;
4861
b3fac117
AM
4862 if (eh->elf.root.type == bfd_link_hash_warning)
4863 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4864
b3fac117
AM
4865 if (eh->elf.root.root.string[0] != '.')
4866 abort ();
8387904d 4867
b3fac117 4868 htab = ppc_hash_table (info);
4dfe6ac6
NC
4869 if (htab == NULL)
4870 return FALSE;
4871
b31867b6
AM
4872 fdh = lookup_fdh (eh, htab);
4873 if (fdh == NULL)
4874 {
4875 if (!info->relocatable
4876 && (eh->elf.root.type == bfd_link_hash_undefined
4877 || eh->elf.root.type == bfd_link_hash_undefweak)
4878 && eh->elf.ref_regular)
4879 {
4880 /* Make an undefweak function descriptor sym, which is enough to
4881 pull in an --as-needed shared lib, but won't cause link
4882 errors. Archives are handled elsewhere. */
4883 fdh = make_fdh (info, eh);
4884 if (fdh == NULL)
4885 return FALSE;
4886 fdh->elf.ref_regular = 1;
4887 }
bb700d78 4888 }
b31867b6 4889 else
8387904d 4890 {
35b0ce59
AM
4891 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4892 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4893 if (entry_vis < descr_vis)
4894 fdh->elf.other += entry_vis - descr_vis;
4895 else if (entry_vis > descr_vis)
4896 eh->elf.other += descr_vis - entry_vis;
4897
e87d886e
AM
4898 if ((fdh->elf.root.type == bfd_link_hash_defined
4899 || fdh->elf.root.type == bfd_link_hash_defweak)
4900 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4901 {
4902 eh->elf.root.type = bfd_link_hash_undefweak;
4903 eh->was_undefined = 1;
4904 htab->twiddled_syms = 1;
4905 }
8387904d 4906 }
99877b66 4907
8387904d
AM
4908 return TRUE;
4909}
4910
b3fac117
AM
4911/* Process list of dot-symbols we made in link_hash_newfunc. */
4912
8387904d 4913static bfd_boolean
7d9616d7 4914ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4915{
99877b66 4916 struct ppc_link_hash_table *htab;
b3fac117 4917 struct ppc_link_hash_entry **p, *eh;
433817dd 4918
0c8d6e5c 4919 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4920 return TRUE;
4dfe6ac6
NC
4921 htab = ppc_hash_table (info);
4922 if (htab == NULL)
4923 return FALSE;
35b0ce59 4924
0c8d6e5c 4925 if (is_ppc64_elf (ibfd))
b3fac117
AM
4926 {
4927 p = &htab->dot_syms;
4928 while ((eh = *p) != NULL)
4929 {
4930 *p = NULL;
1c865ab2
AM
4931 if (&eh->elf == htab->elf.hgot)
4932 ;
4933 else if (htab->elf.hgot == NULL
4934 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4935 htab->elf.hgot = &eh->elf;
4936 else if (!add_symbol_adjust (eh, info))
b3fac117
AM
4937 return FALSE;
4938 p = &eh->u.next_dot_sym;
4939 }
4940 }
4941
4942 /* Clear the list for non-ppc64 input files. */
4943 p = &htab->dot_syms;
4944 while ((eh = *p) != NULL)
4945 {
4946 *p = NULL;
4947 p = &eh->u.next_dot_sym;
4948 }
99877b66
AM
4949
4950 /* We need to fix the undefs list for any syms we have twiddled to
4951 undef_weak. */
4952 if (htab->twiddled_syms)
4953 {
77cfaee6 4954 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4955 htab->twiddled_syms = 0;
4956 }
b3fac117 4957 return TRUE;
8387904d
AM
4958}
4959
97fed1c9
JJ
4960/* Undo hash table changes when an --as-needed input file is determined
4961 not to be needed. */
4962
4963static bfd_boolean
e5034e59
AM
4964ppc64_elf_notice_as_needed (bfd *ibfd,
4965 struct bfd_link_info *info,
4966 enum notice_asneeded_action act)
97fed1c9 4967{
e5034e59
AM
4968 if (act == notice_not_needed)
4969 {
4970 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6 4971
e5034e59
AM
4972 if (htab == NULL)
4973 return FALSE;
4dfe6ac6 4974
e5034e59
AM
4975 htab->dot_syms = NULL;
4976 }
4977 return _bfd_elf_notice_as_needed (ibfd, info, act);
97fed1c9
JJ
4978}
4979
aa374f67
AM
4980/* If --just-symbols against a final linked binary, then assume we need
4981 toc adjusting stubs when calling functions defined there. */
4982
4983static void
4984ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4985{
4986 if ((sec->flags & SEC_CODE) != 0
4987 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4988 && is_ppc64_elf (sec->owner))
4989 {
4990 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4991 if (got != NULL
4992 && got->size >= elf_backend_got_header_size
4993 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4994 sec->has_toc_reloc = 1;
4995 }
4996 _bfd_elf_link_just_syms (sec, info);
4997}
4998
e054468f 4999static struct plt_entry **
4ce794b7
AM
5000update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5001 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
5002{
5003 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 5004 struct plt_entry **local_plt;
f961d9dd 5005 unsigned char *local_got_tls_masks;
411e1bfb
AM
5006
5007 if (local_got_ents == NULL)
5008 {
5009 bfd_size_type size = symtab_hdr->sh_info;
5010
e054468f
AM
5011 size *= (sizeof (*local_got_ents)
5012 + sizeof (*local_plt)
5013 + sizeof (*local_got_tls_masks));
4ce794b7 5014 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 5015 if (local_got_ents == NULL)
e054468f 5016 return NULL;
411e1bfb
AM
5017 elf_local_got_ents (abfd) = local_got_ents;
5018 }
5019
e054468f 5020 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
5021 {
5022 struct got_entry *ent;
5023
5024 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
5025 if (ent->addend == r_addend
5026 && ent->owner == abfd
5027 && ent->tls_type == tls_type)
411e1bfb
AM
5028 break;
5029 if (ent == NULL)
5030 {
5031 bfd_size_type amt = sizeof (*ent);
4ce794b7 5032 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5033 if (ent == NULL)
5034 return FALSE;
5035 ent->next = local_got_ents[r_symndx];
5036 ent->addend = r_addend;
e717da7e 5037 ent->owner = abfd;
411e1bfb 5038 ent->tls_type = tls_type;
927be08e 5039 ent->is_indirect = FALSE;
411e1bfb
AM
5040 ent->got.refcount = 0;
5041 local_got_ents[r_symndx] = ent;
5042 }
5043 ent->got.refcount += 1;
5044 }
5045
e054468f 5046 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5047 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 5048 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
5049
5050 return local_plt + r_symndx;
65f38f15
AM
5051}
5052
411e1bfb 5053static bfd_boolean
e054468f 5054update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 5055{
411e1bfb 5056 struct plt_entry *ent;
1e2f5b6e 5057
e054468f 5058 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5059 if (ent->addend == addend)
5060 break;
5061 if (ent == NULL)
1e2f5b6e 5062 {
411e1bfb 5063 bfd_size_type amt = sizeof (*ent);
4ce794b7 5064 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
5065 if (ent == NULL)
5066 return FALSE;
e054468f 5067 ent->next = *plist;
411e1bfb
AM
5068 ent->addend = addend;
5069 ent->plt.refcount = 0;
e054468f 5070 *plist = ent;
1e2f5b6e 5071 }
411e1bfb 5072 ent->plt.refcount += 1;
b34976b6 5073 return TRUE;
1e2f5b6e
AM
5074}
5075
e054468f
AM
5076static bfd_boolean
5077is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5078{
5079 return (r_type == R_PPC64_REL24
5080 || r_type == R_PPC64_REL14
5081 || r_type == R_PPC64_REL14_BRTAKEN
5082 || r_type == R_PPC64_REL14_BRNTAKEN
5083 || r_type == R_PPC64_ADDR24
5084 || r_type == R_PPC64_ADDR14
5085 || r_type == R_PPC64_ADDR14_BRTAKEN
5086 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5087}
5088
5bd4f169 5089/* Look through the relocs for a section during the first phase, and
65f38f15 5090 calculate needed space in the global offset table, procedure
5d1634d7 5091 linkage table, and dynamic reloc sections. */
5bd4f169 5092
b34976b6 5093static bfd_boolean
4ce794b7
AM
5094ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5095 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5096{
65f38f15 5097 struct ppc_link_hash_table *htab;
5bd4f169 5098 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 5099 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
5100 const Elf_Internal_Rela *rel;
5101 const Elf_Internal_Rela *rel_end;
5bd4f169 5102 asection *sreloc;
1e2f5b6e 5103 asection **opd_sym_map;
3a71aa26 5104 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 5105
1049f94e 5106 if (info->relocatable)
b34976b6 5107 return TRUE;
5bd4f169 5108
680a3378
AM
5109 /* Don't do anything special with non-loaded, non-alloced sections.
5110 In particular, any relocs in such sections should not affect GOT
5111 and PLT reference counting (ie. we don't allow them to create GOT
5112 or PLT entries), there's no possibility or desire to optimize TLS
5113 relocs, and there's not much point in propagating relocs to shared
5114 libs that the dynamic linker won't relocate. */
5115 if ((sec->flags & SEC_ALLOC) == 0)
5116 return TRUE;
5117
0c8d6e5c 5118 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 5119
65f38f15 5120 htab = ppc_hash_table (info);
4dfe6ac6
NC
5121 if (htab == NULL)
5122 return FALSE;
5123
3a71aa26
AM
5124 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5125 FALSE, FALSE, TRUE);
5126 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5127 FALSE, FALSE, TRUE);
0ffa91dd 5128 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5129 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 5130 sreloc = NULL;
1e2f5b6e 5131 opd_sym_map = NULL;
70cc837d 5132 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
5133 {
5134 /* Garbage collection needs some extra help with .opd sections.
5135 We don't want to necessarily keep everything referenced by
5136 relocs in .opd, as that would keep all functions. Instead,
5137 if we reference an .opd symbol (a function descriptor), we
5138 want to keep the function code symbol's section. This is
5139 easy for global symbols, but for local syms we need to keep
74f0fb50 5140 information about the associated function section. */
1e2f5b6e
AM
5141 bfd_size_type amt;
5142
ee67d69a
AM
5143 if (abiversion (abfd) == 0)
5144 set_abiversion (abfd, 1);
5145 else if (abiversion (abfd) == 2)
5146 {
5147 info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5148 abiversion (abfd));
5149 bfd_set_error (bfd_error_bad_value);
5150 return FALSE;
5151 }
74f0fb50 5152 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 5153 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 5154 if (opd_sym_map == NULL)
b34976b6 5155 return FALSE;
74f0fb50 5156 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
5157 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5158 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 5159 }
5bd4f169
AM
5160
5161 rel_end = relocs + sec->reloc_count;
5162 for (rel = relocs; rel < rel_end; rel++)
5163 {
5164 unsigned long r_symndx;
5165 struct elf_link_hash_entry *h;
04c9666a 5166 enum elf_ppc64_reloc_type r_type;
727fc41e 5167 int tls_type;
7c8fe5c4 5168 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 5169 struct plt_entry **ifunc;
5bd4f169
AM
5170
5171 r_symndx = ELF64_R_SYM (rel->r_info);
5172 if (r_symndx < symtab_hdr->sh_info)
5173 h = NULL;
5174 else
973a3492
L
5175 {
5176 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5177 h = elf_follow_link (h);
81fbe831
AM
5178
5179 /* PR15323, ref flags aren't set for references in the same
5180 object. */
5181 h->root.non_ir_ref = 1;
1c865ab2
AM
5182
5183 if (h == htab->elf.hgot)
5184 sec->has_toc_reloc = 1;
973a3492 5185 }
5bd4f169 5186
727fc41e 5187 tls_type = 0;
e054468f 5188 ifunc = NULL;
25f23106
AM
5189 if (h != NULL)
5190 {
5191 if (h->type == STT_GNU_IFUNC)
5192 {
5193 h->needs_plt = 1;
5194 ifunc = &h->plt.plist;
5195 }
5196 }
5197 else
5198 {
5199 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5200 abfd, r_symndx);
5201 if (isym == NULL)
5202 return FALSE;
5203
5204 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5205 {
5206 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5207 rel->r_addend, PLT_IFUNC);
5208 if (ifunc == NULL)
5209 return FALSE;
5210 }
5211 }
4ce794b7 5212 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
5213 if (is_branch_reloc (r_type))
5214 {
5215 if (h != NULL && (h == tga || h == dottga))
5216 {
5217 if (rel != relocs
5218 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5219 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5220 /* We have a new-style __tls_get_addr call with a marker
5221 reloc. */
5222 ;
5223 else
5224 /* Mark this section as having an old-style call. */
5225 sec->has_tls_get_addr_call = 1;
5226 }
727fc41e 5227
e054468f 5228 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5229 if (ifunc != NULL
5230 && !update_plt_info (abfd, ifunc, rel->r_addend))
5231 return FALSE;
5232 }
727fc41e 5233
a33d1f77 5234 switch (r_type)
5bd4f169 5235 {
727fc41e
AM
5236 case R_PPC64_TLSGD:
5237 case R_PPC64_TLSLD:
5238 /* These special tls relocs tie a call to __tls_get_addr with
5239 its parameter symbol. */
5240 break;
5241
411e1bfb
AM
5242 case R_PPC64_GOT_TLSLD16:
5243 case R_PPC64_GOT_TLSLD16_LO:
5244 case R_PPC64_GOT_TLSLD16_HI:
5245 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5246 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5247 goto dogottls;
5248
5249 case R_PPC64_GOT_TLSGD16:
5250 case R_PPC64_GOT_TLSGD16_LO:
5251 case R_PPC64_GOT_TLSGD16_HI:
5252 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5253 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5254 goto dogottls;
5255
5256 case R_PPC64_GOT_TPREL16_DS:
5257 case R_PPC64_GOT_TPREL16_LO_DS:
5258 case R_PPC64_GOT_TPREL16_HI:
5259 case R_PPC64_GOT_TPREL16_HA:
1d483afe 5260 if (!info->executable)
411e1bfb
AM
5261 info->flags |= DF_STATIC_TLS;
5262 tls_type = TLS_TLS | TLS_TPREL;
5263 goto dogottls;
5264
5265 case R_PPC64_GOT_DTPREL16_DS:
5266 case R_PPC64_GOT_DTPREL16_LO_DS:
5267 case R_PPC64_GOT_DTPREL16_HI:
5268 case R_PPC64_GOT_DTPREL16_HA:
5269 tls_type = TLS_TLS | TLS_DTPREL;
5270 dogottls:
5271 sec->has_tls_reloc = 1;
5272 /* Fall thru */
5273
5bd4f169 5274 case R_PPC64_GOT16:
5bd4f169 5275 case R_PPC64_GOT16_DS:
65f38f15
AM
5276 case R_PPC64_GOT16_HA:
5277 case R_PPC64_GOT16_HI:
5278 case R_PPC64_GOT16_LO:
5bd4f169 5279 case R_PPC64_GOT16_LO_DS:
65f38f15 5280 /* This symbol requires a global offset table entry. */
4c52953f 5281 sec->has_toc_reloc = 1;
33c0ec9d
AM
5282 if (r_type == R_PPC64_GOT_TLSLD16
5283 || r_type == R_PPC64_GOT_TLSGD16
5284 || r_type == R_PPC64_GOT_TPREL16_DS
5285 || r_type == R_PPC64_GOT_DTPREL16_DS
5286 || r_type == R_PPC64_GOT16
5287 || r_type == R_PPC64_GOT16_DS)
5288 {
5289 htab->do_multi_toc = 1;
d77c8a4b 5290 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5291 }
5292
e717da7e
AM
5293 if (ppc64_elf_tdata (abfd)->got == NULL
5294 && !create_got_section (abfd, info))
b34976b6 5295 return FALSE;
5bd4f169
AM
5296
5297 if (h != NULL)
5298 {
411e1bfb
AM
5299 struct ppc_link_hash_entry *eh;
5300 struct got_entry *ent;
65f38f15 5301
411e1bfb
AM
5302 eh = (struct ppc_link_hash_entry *) h;
5303 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5304 if (ent->addend == rel->r_addend
e717da7e 5305 && ent->owner == abfd
411e1bfb
AM
5306 && ent->tls_type == tls_type)
5307 break;
5308 if (ent == NULL)
5bd4f169 5309 {
411e1bfb 5310 bfd_size_type amt = sizeof (*ent);
4ce794b7 5311 ent = bfd_alloc (abfd, amt);
411e1bfb 5312 if (ent == NULL)
b34976b6 5313 return FALSE;
411e1bfb
AM
5314 ent->next = eh->elf.got.glist;
5315 ent->addend = rel->r_addend;
e717da7e 5316 ent->owner = abfd;
411e1bfb 5317 ent->tls_type = tls_type;
927be08e 5318 ent->is_indirect = FALSE;
411e1bfb
AM
5319 ent->got.refcount = 0;
5320 eh->elf.got.glist = ent;
5bd4f169 5321 }
411e1bfb 5322 ent->got.refcount += 1;
e7b938ca 5323 eh->tls_mask |= tls_type;
5bd4f169 5324 }
411e1bfb
AM
5325 else
5326 /* This is a global offset table entry for a local symbol. */
5327 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5328 rel->r_addend, tls_type))
5329 return FALSE;
5bd4f169
AM
5330 break;
5331
5bd4f169 5332 case R_PPC64_PLT16_HA:
65f38f15
AM
5333 case R_PPC64_PLT16_HI:
5334 case R_PPC64_PLT16_LO:
5335 case R_PPC64_PLT32:
5336 case R_PPC64_PLT64:
5bd4f169 5337 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5338 actually build the entry in adjust_dynamic_symbol,
5339 because this might be a case of linking PIC code without
5340 linking in any dynamic objects, in which case we don't
5341 need to generate a procedure linkage table after all. */
5bd4f169
AM
5342 if (h == NULL)
5343 {
5344 /* It does not make sense to have a procedure linkage
3fad3c7c 5345 table entry for a local symbol. */
5bd4f169 5346 bfd_set_error (bfd_error_bad_value);
b34976b6 5347 return FALSE;
5bd4f169 5348 }
411e1bfb 5349 else
e054468f
AM
5350 {
5351 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5352 return FALSE;
5353 h->needs_plt = 1;
5354 if (h->root.root.string[0] == '.'
5355 && h->root.root.string[1] != '\0')
5356 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5357 }
5bd4f169
AM
5358 break;
5359
5360 /* The following relocations don't need to propagate the
5361 relocation if linking a shared object since they are
5362 section relative. */
5363 case R_PPC64_SECTOFF:
5364 case R_PPC64_SECTOFF_LO:
5365 case R_PPC64_SECTOFF_HI:
5366 case R_PPC64_SECTOFF_HA:
5367 case R_PPC64_SECTOFF_DS:
5368 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5369 case R_PPC64_DTPREL16:
5370 case R_PPC64_DTPREL16_LO:
5371 case R_PPC64_DTPREL16_HI:
5372 case R_PPC64_DTPREL16_HA:
5373 case R_PPC64_DTPREL16_DS:
5374 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
5375 case R_PPC64_DTPREL16_HIGH:
5376 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
5377 case R_PPC64_DTPREL16_HIGHER:
5378 case R_PPC64_DTPREL16_HIGHERA:
5379 case R_PPC64_DTPREL16_HIGHEST:
5380 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5381 break;
5382
ad8e1ba5 5383 /* Nor do these. */
25f23106
AM
5384 case R_PPC64_REL16:
5385 case R_PPC64_REL16_LO:
5386 case R_PPC64_REL16_HI:
5387 case R_PPC64_REL16_HA:
5388 break;
5389
ad8e1ba5 5390 case R_PPC64_TOC16:
33c0ec9d
AM
5391 case R_PPC64_TOC16_DS:
5392 htab->do_multi_toc = 1;
d77c8a4b 5393 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5394 case R_PPC64_TOC16_LO:
5395 case R_PPC64_TOC16_HI:
5396 case R_PPC64_TOC16_HA:
ad8e1ba5 5397 case R_PPC64_TOC16_LO_DS:
4c52953f 5398 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5399 break;
5400
5bd4f169
AM
5401 /* This relocation describes the C++ object vtable hierarchy.
5402 Reconstruct it for later use during GC. */
5403 case R_PPC64_GNU_VTINHERIT:
c152c796 5404 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5405 return FALSE;
5bd4f169
AM
5406 break;
5407
5408 /* This relocation describes which C++ vtable entries are actually
5409 used. Record for later use during GC. */
5410 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5411 BFD_ASSERT (h != NULL);
5412 if (h != NULL
5413 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5414 return FALSE;
5bd4f169
AM
5415 break;
5416
721956f4
AM
5417 case R_PPC64_REL14:
5418 case R_PPC64_REL14_BRTAKEN:
5419 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5420 {
5421 asection *dest = NULL;
5422
5423 /* Heuristic: If jumping outside our section, chances are
5424 we are going to need a stub. */
5425 if (h != NULL)
5426 {
5427 /* If the sym is weak it may be overridden later, so
5428 don't assume we know where a weak sym lives. */
5429 if (h->root.type == bfd_link_hash_defined)
5430 dest = h->root.u.def.section;
5431 }
5432 else
87d72d41
AM
5433 {
5434 Elf_Internal_Sym *isym;
5435
5436 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5437 abfd, r_symndx);
5438 if (isym == NULL)
5439 return FALSE;
5440
5441 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5442 }
5443
220c76dd 5444 if (dest != sec)
7c8fe5c4 5445 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5446 }
721956f4
AM
5447 /* Fall through. */
5448
5d1634d7 5449 case R_PPC64_REL24:
e054468f 5450 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5451 {
5452 /* We may need a .plt entry if the function this reloc
5453 refers to is in a shared lib. */
e054468f 5454 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5455 return FALSE;
e054468f
AM
5456 h->needs_plt = 1;
5457 if (h->root.root.string[0] == '.'
5458 && h->root.root.string[1] != '\0')
5459 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5460 if (h == tga || h == dottga)
411e1bfb 5461 sec->has_tls_reloc = 1;
411e1bfb
AM
5462 }
5463 break;
5464
5465 case R_PPC64_TPREL64:
5466 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5467 if (!info->executable)
411e1bfb
AM
5468 info->flags |= DF_STATIC_TLS;
5469 goto dotlstoc;
5470
5471 case R_PPC64_DTPMOD64:
5472 if (rel + 1 < rel_end
5473 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5474 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5475 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5476 else
951fd09b 5477 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5478 goto dotlstoc;
5479
5480 case R_PPC64_DTPREL64:
5481 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5482 if (rel != relocs
5483 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5484 && rel[-1].r_offset == rel->r_offset - 8)
5485 /* This is the second reloc of a dtpmod, dtprel pair.
5486 Don't mark with TLS_DTPREL. */
5487 goto dodyn;
5488
5489 dotlstoc:
5490 sec->has_tls_reloc = 1;
5491 if (h != NULL)
5492 {
5493 struct ppc_link_hash_entry *eh;
5494 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5495 eh->tls_mask |= tls_type;
411e1bfb
AM
5496 }
5497 else
5498 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5499 rel->r_addend, tls_type))
5500 return FALSE;
5501
7c8fe5c4
AM
5502 ppc64_sec = ppc64_elf_section_data (sec);
5503 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5504 {
3a71aa26
AM
5505 bfd_size_type amt;
5506
e7b938ca 5507 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5508 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5509 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5510 if (ppc64_sec->u.toc.symndx == NULL)
5511 return FALSE;
5512 amt = sec->size * sizeof (bfd_vma) / 8;
5513 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5514 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5515 return FALSE;
7c8fe5c4
AM
5516 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5517 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5518 }
5519 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5520 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5521 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5522
5523 /* Mark the second slot of a GD or LD entry.
5524 -1 to indicate GD and -2 to indicate LD. */
5525 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5526 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5527 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5528 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5529 goto dodyn;
5530
5531 case R_PPC64_TPREL16:
5532 case R_PPC64_TPREL16_LO:
5533 case R_PPC64_TPREL16_HI:
5534 case R_PPC64_TPREL16_HA:
5535 case R_PPC64_TPREL16_DS:
5536 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
5537 case R_PPC64_TPREL16_HIGH:
5538 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
5539 case R_PPC64_TPREL16_HIGHER:
5540 case R_PPC64_TPREL16_HIGHERA:
5541 case R_PPC64_TPREL16_HIGHEST:
5542 case R_PPC64_TPREL16_HIGHESTA:
5543 if (info->shared)
5544 {
1d483afe
AM
5545 if (!info->executable)
5546 info->flags |= DF_STATIC_TLS;
411e1bfb 5547 goto dodyn;
5d1634d7
AM
5548 }
5549 break;
5550
e86ce104 5551 case R_PPC64_ADDR64:
1e2f5b6e 5552 if (opd_sym_map != NULL
1e2f5b6e 5553 && rel + 1 < rel_end
4ce794b7 5554 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5555 {
8387904d
AM
5556 if (h != NULL)
5557 {
5558 if (h->root.root.string[0] == '.'
5559 && h->root.root.string[1] != 0
b31867b6 5560 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5561 ;
5562 else
5563 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5564 }
5565 else
5566 {
5567 asection *s;
87d72d41 5568 Elf_Internal_Sym *isym;
1e2f5b6e 5569
87d72d41
AM
5570 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5571 abfd, r_symndx);
5572 if (isym == NULL)
8387904d 5573 return FALSE;
87d72d41
AM
5574
5575 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5576 if (s != NULL && s != sec)
3f764659 5577 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5578 }
1e2f5b6e 5579 }
e86ce104
AM
5580 /* Fall through. */
5581
04c9666a 5582 case R_PPC64_REL30:
5bd4f169 5583 case R_PPC64_REL32:
04c9666a 5584 case R_PPC64_REL64:
65f38f15
AM
5585 case R_PPC64_ADDR14:
5586 case R_PPC64_ADDR14_BRNTAKEN:
5587 case R_PPC64_ADDR14_BRTAKEN:
5588 case R_PPC64_ADDR16:
5589 case R_PPC64_ADDR16_DS:
5590 case R_PPC64_ADDR16_HA:
5591 case R_PPC64_ADDR16_HI:
f9c6b907
AM
5592 case R_PPC64_ADDR16_HIGH:
5593 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
5594 case R_PPC64_ADDR16_HIGHER:
5595 case R_PPC64_ADDR16_HIGHERA:
5596 case R_PPC64_ADDR16_HIGHEST:
5597 case R_PPC64_ADDR16_HIGHESTA:
5598 case R_PPC64_ADDR16_LO:
5599 case R_PPC64_ADDR16_LO_DS:
5600 case R_PPC64_ADDR24:
65f38f15 5601 case R_PPC64_ADDR32:
65f38f15
AM
5602 case R_PPC64_UADDR16:
5603 case R_PPC64_UADDR32:
5604 case R_PPC64_UADDR64:
5bd4f169 5605 case R_PPC64_TOC:
81848ca0
AM
5606 if (h != NULL && !info->shared)
5607 /* We may need a copy reloc. */
f5385ebf 5608 h->non_got_ref = 1;
81848ca0 5609
41bd81ab 5610 /* Don't propagate .opd relocs. */
1e2f5b6e 5611 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5612 break;
e86ce104 5613
65f38f15
AM
5614 /* If we are creating a shared library, and this is a reloc
5615 against a global symbol, or a non PC relative reloc
5616 against a local symbol, then we need to copy the reloc
5617 into the shared library. However, if we are linking with
5618 -Bsymbolic, we do not need to copy a reloc against a
5619 global symbol which is defined in an object we are
5620 including in the link (i.e., DEF_REGULAR is set). At
5621 this point we have not seen all the input files, so it is
5622 possible that DEF_REGULAR is not set now but will be set
5623 later (it is never cleared). In case of a weak definition,
5624 DEF_REGULAR may be cleared later by a strong definition in
5625 a shared library. We account for that possibility below by
f4656909 5626 storing information in the dyn_relocs field of the hash
65f38f15
AM
5627 table entry. A similar situation occurs when creating
5628 shared libraries and symbol visibility changes render the
5629 symbol local.
5630
5631 If on the other hand, we are creating an executable, we
5632 may need to keep relocations for symbols satisfied by a
5633 dynamic library if we manage to avoid copy relocs for the
5634 symbol. */
411e1bfb 5635 dodyn:
65f38f15 5636 if ((info->shared
1d483afe 5637 && (must_be_dyn_reloc (info, r_type)
65f38f15 5638 || (h != NULL
198f1157 5639 && (!SYMBOLIC_BIND (info, h)
65f38f15 5640 || h->root.type == bfd_link_hash_defweak
f5385ebf 5641 || !h->def_regular))))
f4656909
AM
5642 || (ELIMINATE_COPY_RELOCS
5643 && !info->shared
65f38f15
AM
5644 && h != NULL
5645 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5646 || !h->def_regular))
5647 || (!info->shared
5648 && ifunc != NULL))
5bd4f169 5649 {
65f38f15
AM
5650 /* We must copy these reloc types into the output file.
5651 Create a reloc section in dynobj and make room for
5652 this reloc. */
5bd4f169
AM
5653 if (sreloc == NULL)
5654 {
83bac4b0
NC
5655 sreloc = _bfd_elf_make_dynamic_reloc_section
5656 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5657
5bd4f169 5658 if (sreloc == NULL)
83bac4b0 5659 return FALSE;
5bd4f169
AM
5660 }
5661
65f38f15
AM
5662 /* If this is a global symbol, we count the number of
5663 relocations we need for this symbol. */
5664 if (h != NULL)
5665 {
19e08130
AM
5666 struct elf_dyn_relocs *p;
5667 struct elf_dyn_relocs **head;
5668
ec338859 5669 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
19e08130
AM
5670 p = *head;
5671 if (p == NULL || p->sec != sec)
5672 {
5673 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5674 if (p == NULL)
5675 return FALSE;
5676 p->next = *head;
5677 *head = p;
5678 p->sec = sec;
5679 p->count = 0;
5680 p->pc_count = 0;
5681 }
5682 p->count += 1;
5683 if (!must_be_dyn_reloc (info, r_type))
5684 p->pc_count += 1;
65f38f15
AM
5685 }
5686 else
5687 {
ec338859
AM
5688 /* Track dynamic relocs needed for local syms too.
5689 We really need local syms available to do this
5690 easily. Oh well. */
19e08130
AM
5691 struct ppc_dyn_relocs *p;
5692 struct ppc_dyn_relocs **head;
5693 bfd_boolean is_ifunc;
ec338859 5694 asection *s;
6edfbbad 5695 void *vpp;
87d72d41 5696 Elf_Internal_Sym *isym;
6edfbbad 5697
87d72d41
AM
5698 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5699 abfd, r_symndx);
5700 if (isym == NULL)
b34976b6 5701 return FALSE;
ec338859 5702
87d72d41
AM
5703 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5704 if (s == NULL)
5705 s = sec;
5706
6edfbbad 5707 vpp = &elf_section_data (s)->local_dynrel;
19e08130
AM
5708 head = (struct ppc_dyn_relocs **) vpp;
5709 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5710 p = *head;
5711 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5712 p = p->next;
5713 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5714 {
5715 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5716 if (p == NULL)
5717 return FALSE;
5718 p->next = *head;
5719 *head = p;
5720 p->sec = sec;
5721 p->ifunc = is_ifunc;
5722 p->count = 0;
5723 }
5724 p->count += 1;
ec338859 5725 }
65f38f15 5726 }
5bd4f169 5727 break;
65f38f15
AM
5728
5729 default:
96e0dda4 5730 break;
5bd4f169
AM
5731 }
5732 }
5733
b34976b6 5734 return TRUE;
5bd4f169
AM
5735}
5736
ee67d69a
AM
5737/* Merge backend specific data from an object file to the output
5738 object file when linking. */
5739
5740static bfd_boolean
5741ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5742{
5743 unsigned long iflags, oflags;
5744
5745 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5746 return TRUE;
5747
5748 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5749 return TRUE;
5750
5751 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5752 return FALSE;
5753
5754 iflags = elf_elfheader (ibfd)->e_flags;
5755 oflags = elf_elfheader (obfd)->e_flags;
5756
5757 if (!elf_flags_init (obfd) || oflags == 0)
5758 {
5759 elf_flags_init (obfd) = TRUE;
5760 elf_elfheader (obfd)->e_flags = iflags;
5761 }
5762 else if (iflags == oflags || iflags == 0)
5763 ;
5764 else if (iflags & ~EF_PPC64_ABI)
5765 {
5766 (*_bfd_error_handler)
5767 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5768 bfd_set_error (bfd_error_bad_value);
5769 return FALSE;
5770 }
5771 else
5772 {
5773 (*_bfd_error_handler)
5774 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5775 ibfd, iflags, oflags);
5776 bfd_set_error (bfd_error_bad_value);
5777 return FALSE;
5778 }
5779
5780 /* Merge Tag_compatibility attributes and any common GNU ones. */
5781 _bfd_elf_merge_object_attributes (ibfd, obfd);
5782
5783 return TRUE;
5784}
5785
5786static bfd_boolean
5787ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5788{
5789 /* Print normal ELF private data. */
5790 _bfd_elf_print_private_bfd_data (abfd, ptr);
5791
5792 if (elf_elfheader (abfd)->e_flags != 0)
5793 {
5794 FILE *file = ptr;
5795
5796 /* xgettext:c-format */
5797 fprintf (file, _("private flags = 0x%lx:"),
5798 elf_elfheader (abfd)->e_flags);
5799
5800 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5801 fprintf (file, _(" [abiv%ld]"),
5802 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5803 fputc ('\n', file);
5804 }
5805
5806 return TRUE;
5807}
5808
8387904d
AM
5809/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5810 of the code entry point, and its section. */
5811
5812static bfd_vma
5813opd_entry_value (asection *opd_sec,
5814 bfd_vma offset,
5815 asection **code_sec,
aef36ac1
AM
5816 bfd_vma *code_off,
5817 bfd_boolean in_code_sec)
8387904d
AM
5818{
5819 bfd *opd_bfd = opd_sec->owner;
8860955f 5820 Elf_Internal_Rela *relocs;
8387904d 5821 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5822 bfd_vma val;
8387904d 5823
9f296da3
AM
5824 /* No relocs implies we are linking a --just-symbols object, or looking
5825 at a final linked executable with addr2line or somesuch. */
4b85d634
AM
5826 if (opd_sec->reloc_count == 0)
5827 {
729eabd5 5828 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
3b36f7e6 5829
729eabd5
AM
5830 if (contents == NULL)
5831 {
5832 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5833 return (bfd_vma) -1;
5834 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5835 }
ee1e4ede 5836
729eabd5 5837 val = bfd_get_64 (opd_bfd, contents + offset);
aef36ac1
AM
5838 if (code_sec != NULL)
5839 {
5840 asection *sec, *likely = NULL;
ee1e4ede 5841
aef36ac1 5842 if (in_code_sec)
4b85d634 5843 {
aef36ac1
AM
5844 sec = *code_sec;
5845 if (sec->vma <= val
5846 && val < sec->vma + sec->size)
5847 likely = sec;
5848 else
5849 val = -1;
5850 }
5851 else
5852 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5853 if (sec->vma <= val
5854 && (sec->flags & SEC_LOAD) != 0
5855 && (sec->flags & SEC_ALLOC) != 0)
5856 likely = sec;
5857 if (likely != NULL)
5858 {
5859 *code_sec = likely;
5860 if (code_off != NULL)
5861 *code_off = val - likely->vma;
4b85d634
AM
5862 }
5863 }
aef36ac1 5864 return val;
4b85d634
AM
5865 }
5866
0c8d6e5c 5867 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5868
729eabd5 5869 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
8860955f
AM
5870 if (relocs == NULL)
5871 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5872
8387904d 5873 /* Go find the opd reloc at the sym address. */
8860955f 5874 lo = relocs;
8387904d
AM
5875 BFD_ASSERT (lo != NULL);
5876 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5877 val = (bfd_vma) -1;
8387904d
AM
5878 while (lo < hi)
5879 {
5880 look = lo + (hi - lo) / 2;
5881 if (look->r_offset < offset)
5882 lo = look + 1;
5883 else if (look->r_offset > offset)
5884 hi = look;
5885 else
5886 {
0ffa91dd
NC
5887 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5888
8387904d
AM
5889 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5890 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5891 {
5892 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5893 asection *sec;
5894
62599110
AM
5895 if (symndx < symtab_hdr->sh_info
5896 || elf_sym_hashes (opd_bfd) == NULL)
8387904d
AM
5897 {
5898 Elf_Internal_Sym *sym;
5899
5900 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5901 if (sym == NULL)
5902 {
62599110
AM
5903 size_t symcnt = symtab_hdr->sh_info;
5904 if (elf_sym_hashes (opd_bfd) == NULL)
5905 symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5906 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
8387904d
AM
5907 0, NULL, NULL, NULL);
5908 if (sym == NULL)
645ea6a9 5909 break;
8387904d
AM
5910 symtab_hdr->contents = (bfd_byte *) sym;
5911 }
5912
5913 sym += symndx;
5914 val = sym->st_value;
cb33740c 5915 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5916 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5917 }
5918 else
5919 {
5920 struct elf_link_hash_entry **sym_hashes;
5921 struct elf_link_hash_entry *rh;
5922
5923 sym_hashes = elf_sym_hashes (opd_bfd);
5924 rh = sym_hashes[symndx - symtab_hdr->sh_info];
128205bb
AM
5925 if (rh != NULL)
5926 {
5927 rh = elf_follow_link (rh);
5928 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5929 || rh->root.type == bfd_link_hash_defweak);
5930 val = rh->root.u.def.value;
5931 sec = rh->root.u.def.section;
5932 }
5933 else
5934 {
5935 /* Handle the odd case where we can be called
5936 during bfd_elf_link_add_symbols before the
5937 symbol hashes have been fully populated. */
5938 Elf_Internal_Sym *sym;
5939
5940 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5941 symndx, NULL, NULL, NULL);
5942 if (sym == NULL)
5943 break;
5944
5945 val = sym->st_value;
5946 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5947 free (sym);
5948 }
8387904d
AM
5949 }
5950 val += look->r_addend;
5951 if (code_off != NULL)
5952 *code_off = val;
5953 if (code_sec != NULL)
aef36ac1
AM
5954 {
5955 if (in_code_sec && *code_sec != sec)
5956 return -1;
5957 else
5958 *code_sec = sec;
5959 }
8387904d
AM
5960 if (sec != NULL && sec->output_section != NULL)
5961 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5962 }
5963 break;
5964 }
5965 }
645ea6a9 5966
645ea6a9 5967 return val;
8387904d
AM
5968}
5969
aef36ac1
AM
5970/* If the ELF symbol SYM might be a function in SEC, return the
5971 function size and set *CODE_OFF to the function's entry point,
5972 otherwise return zero. */
9f296da3 5973
aef36ac1
AM
5974static bfd_size_type
5975ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5976 bfd_vma *code_off)
9f296da3 5977{
aef36ac1
AM
5978 bfd_size_type size;
5979
5980 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5981 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5982 return 0;
5983
5984 size = 0;
5985 if (!(sym->flags & BSF_SYNTHETIC))
5986 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5987
5988 if (strcmp (sym->section->name, ".opd") == 0)
9f296da3 5989 {
aef36ac1
AM
5990 if (opd_entry_value (sym->section, sym->value,
5991 &sec, code_off, TRUE) == (bfd_vma) -1)
5992 return 0;
5993 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5994 symbol. This size has nothing to do with the code size of the
5995 function, which is what we're supposed to return, but the
5996 code size isn't available without looking up the dot-sym.
5997 However, doing that would be a waste of time particularly
5998 since elf_find_function will look at the dot-sym anyway.
5999 Now, elf_find_function will keep the largest size of any
6000 function sym found at the code address of interest, so return
6001 1 here to avoid it incorrectly caching a larger function size
6002 for a small function. This does mean we return the wrong
6003 size for a new-ABI function of size 24, but all that does is
6004 disable caching for such functions. */
6005 if (size == 24)
6006 size = 1;
9f296da3 6007 }
aef36ac1
AM
6008 else
6009 {
6010 if (sym->section != sec)
6011 return 0;
6012 *code_off = sym->value;
6013 }
6014 if (size == 0)
6015 size = 1;
6016 return size;
9f296da3
AM
6017}
6018
854b41e7
AM
6019/* Return true if symbol is defined in a regular object file. */
6020
6021static bfd_boolean
6022is_static_defined (struct elf_link_hash_entry *h)
6023{
6024 return ((h->root.type == bfd_link_hash_defined
6025 || h->root.type == bfd_link_hash_defweak)
6026 && h->root.u.def.section != NULL
6027 && h->root.u.def.section->output_section != NULL);
6028}
6029
b31867b6
AM
6030/* If FDH is a function descriptor symbol, return the associated code
6031 entry symbol if it is defined. Return NULL otherwise. */
6032
6033static struct ppc_link_hash_entry *
6034defined_code_entry (struct ppc_link_hash_entry *fdh)
6035{
6036 if (fdh->is_func_descriptor)
6037 {
6038 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6039 if (fh->elf.root.type == bfd_link_hash_defined
6040 || fh->elf.root.type == bfd_link_hash_defweak)
6041 return fh;
6042 }
6043 return NULL;
6044}
6045
6046/* If FH is a function code entry symbol, return the associated
6047 function descriptor symbol if it is defined. Return NULL otherwise. */
6048
6049static struct ppc_link_hash_entry *
6050defined_func_desc (struct ppc_link_hash_entry *fh)
6051{
6052 if (fh->oh != NULL
6053 && fh->oh->is_func_descriptor)
6054 {
6055 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6056 if (fdh->elf.root.type == bfd_link_hash_defined
6057 || fdh->elf.root.type == bfd_link_hash_defweak)
6058 return fdh;
6059 }
6060 return NULL;
6061}
6062
74f0fb50
AM
6063/* Mark all our entry sym sections, both opd and code section. */
6064
6065static void
6066ppc64_elf_gc_keep (struct bfd_link_info *info)
6067{
6068 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6069 struct bfd_sym_chain *sym;
6070
4dfe6ac6
NC
6071 if (htab == NULL)
6072 return;
6073
74f0fb50
AM
6074 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6075 {
b31867b6 6076 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
6077 asection *sec;
6078
6079 eh = (struct ppc_link_hash_entry *)
b31867b6 6080 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
6081 if (eh == NULL)
6082 continue;
6083 if (eh->elf.root.type != bfd_link_hash_defined
6084 && eh->elf.root.type != bfd_link_hash_defweak)
6085 continue;
6086
b31867b6
AM
6087 fh = defined_code_entry (eh);
6088 if (fh != NULL)
74f0fb50 6089 {
b31867b6 6090 sec = fh->elf.root.u.def.section;
74f0fb50
AM
6091 sec->flags |= SEC_KEEP;
6092 }
6093 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6094 && opd_entry_value (eh->elf.root.u.def.section,
6095 eh->elf.root.u.def.value,
aef36ac1 6096 &sec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50
AM
6097 sec->flags |= SEC_KEEP;
6098
6099 sec = eh->elf.root.u.def.section;
6100 sec->flags |= SEC_KEEP;
6101 }
6102}
6103
64d03ab5
AM
6104/* Mark sections containing dynamically referenced symbols. When
6105 building shared libraries, we must assume that any visible symbol is
6106 referenced. */
6107
6108static bfd_boolean
6109ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6110{
6111 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6112 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 6113 struct ppc_link_hash_entry *fdh;
64d03ab5 6114
64d03ab5 6115 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
6116 fdh = defined_func_desc (eh);
6117 if (fdh != NULL)
6118 eh = fdh;
64d03ab5
AM
6119
6120 if ((eh->elf.root.type == bfd_link_hash_defined
6121 || eh->elf.root.type == bfd_link_hash_defweak)
6122 && (eh->elf.ref_dynamic
6123 || (!info->executable
6124 && eh->elf.def_regular
6125 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8
AM
6126 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6127 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6128 || !bfd_hide_sym_by_version (info->version_info,
6129 eh->elf.root.root.string)))))
64d03ab5
AM
6130 {
6131 asection *code_sec;
b31867b6 6132 struct ppc_link_hash_entry *fh;
64d03ab5
AM
6133
6134 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6135
6136 /* Function descriptor syms cause the associated
6137 function code sym section to be marked. */
b31867b6
AM
6138 fh = defined_code_entry (eh);
6139 if (fh != NULL)
6140 {
6141 code_sec = fh->elf.root.u.def.section;
6142 code_sec->flags |= SEC_KEEP;
6143 }
64d03ab5
AM
6144 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6145 && opd_entry_value (eh->elf.root.u.def.section,
6146 eh->elf.root.u.def.value,
aef36ac1 6147 &code_sec, NULL, FALSE) != (bfd_vma) -1)
64d03ab5
AM
6148 code_sec->flags |= SEC_KEEP;
6149 }
6150
6151 return TRUE;
6152}
6153
5bd4f169
AM
6154/* Return the section that should be marked against GC for a given
6155 relocation. */
6156
6157static asection *
4ce794b7 6158ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 6159 struct bfd_link_info *info,
4ce794b7
AM
6160 Elf_Internal_Rela *rel,
6161 struct elf_link_hash_entry *h,
6162 Elf_Internal_Sym *sym)
5bd4f169 6163{
ccfa59ea
AM
6164 asection *rsec;
6165
ccfa59ea
AM
6166 /* Syms return NULL if we're marking .opd, so we avoid marking all
6167 function sections, as all functions are referenced in .opd. */
6168 rsec = NULL;
6169 if (get_opd_info (sec) != NULL)
6170 return rsec;
1e2f5b6e 6171
5bd4f169
AM
6172 if (h != NULL)
6173 {
04c9666a 6174 enum elf_ppc64_reloc_type r_type;
b31867b6 6175 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 6176
4ce794b7 6177 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 6178 switch (r_type)
5bd4f169
AM
6179 {
6180 case R_PPC64_GNU_VTINHERIT:
6181 case R_PPC64_GNU_VTENTRY:
6182 break;
6183
6184 default:
6185 switch (h->root.type)
6186 {
6187 case bfd_link_hash_defined:
6188 case bfd_link_hash_defweak:
ccfa59ea 6189 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
6190 fdh = defined_func_desc (eh);
6191 if (fdh != NULL)
6192 eh = fdh;
1e2f5b6e
AM
6193
6194 /* Function descriptor syms cause the associated
6195 function code sym section to be marked. */
b31867b6
AM
6196 fh = defined_code_entry (eh);
6197 if (fh != NULL)
ccfa59ea
AM
6198 {
6199 /* They also mark their opd section. */
74f0fb50 6200 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6201
b31867b6 6202 rsec = fh->elf.root.u.def.section;
ccfa59ea 6203 }
8387904d
AM
6204 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6205 && opd_entry_value (eh->elf.root.u.def.section,
6206 eh->elf.root.u.def.value,
aef36ac1 6207 &rsec, NULL, FALSE) != (bfd_vma) -1)
74f0fb50 6208 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 6209 else
1e2f5b6e
AM
6210 rsec = h->root.u.def.section;
6211 break;
5bd4f169
AM
6212
6213 case bfd_link_hash_common:
1e2f5b6e
AM
6214 rsec = h->root.u.c.p->section;
6215 break;
5bd4f169
AM
6216
6217 default:
fb34365b 6218 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
6219 }
6220 }
6221 }
6222 else
6223 {
74f0fb50 6224 struct _opd_sec_data *opd;
1e2f5b6e
AM
6225
6226 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
6227 opd = get_opd_info (rsec);
6228 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 6229 {
74f0fb50 6230 rsec->gc_mark = 1;
ccfa59ea 6231
74f0fb50 6232 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 6233 }
5bd4f169
AM
6234 }
6235
1e2f5b6e 6236 return rsec;
5bd4f169
AM
6237}
6238
65f38f15
AM
6239/* Update the .got, .plt. and dynamic reloc reference counts for the
6240 section being removed. */
5bd4f169 6241
b34976b6 6242static bfd_boolean
4ce794b7
AM
6243ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6244 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 6245{
411e1bfb 6246 struct ppc_link_hash_table *htab;
5bd4f169
AM
6247 Elf_Internal_Shdr *symtab_hdr;
6248 struct elf_link_hash_entry **sym_hashes;
411e1bfb 6249 struct got_entry **local_got_ents;
5bd4f169 6250 const Elf_Internal_Rela *rel, *relend;
5bd4f169 6251
7dda2462
TG
6252 if (info->relocatable)
6253 return TRUE;
6254
680a3378
AM
6255 if ((sec->flags & SEC_ALLOC) == 0)
6256 return TRUE;
6257
ec338859
AM
6258 elf_section_data (sec)->local_dynrel = NULL;
6259
411e1bfb 6260 htab = ppc_hash_table (info);
4dfe6ac6
NC
6261 if (htab == NULL)
6262 return FALSE;
6263
0ffa91dd 6264 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 6265 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 6266 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
6267
6268 relend = relocs + sec->reloc_count;
6269 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
6270 {
6271 unsigned long r_symndx;
04c9666a 6272 enum elf_ppc64_reloc_type r_type;
58ac9f71 6273 struct elf_link_hash_entry *h = NULL;
f961d9dd 6274 unsigned char tls_type = 0;
5bd4f169 6275
a33d1f77 6276 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 6277 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
6278 if (r_symndx >= symtab_hdr->sh_info)
6279 {
6280 struct ppc_link_hash_entry *eh;
6061a67d
AM
6281 struct elf_dyn_relocs **pp;
6282 struct elf_dyn_relocs *p;
58ac9f71
AM
6283
6284 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6285 h = elf_follow_link (h);
58ac9f71
AM
6286 eh = (struct ppc_link_hash_entry *) h;
6287
6288 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6289 if (p->sec == sec)
6290 {
6291 /* Everything must go for SEC. */
6292 *pp = p->next;
6293 break;
6294 }
6295 }
6296
e054468f
AM
6297 if (is_branch_reloc (r_type))
6298 {
6299 struct plt_entry **ifunc = NULL;
6300 if (h != NULL)
6301 {
6302 if (h->type == STT_GNU_IFUNC)
6303 ifunc = &h->plt.plist;
6304 }
6305 else if (local_got_ents != NULL)
6306 {
6307 struct plt_entry **local_plt = (struct plt_entry **)
6308 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 6309 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
6310 (local_plt + symtab_hdr->sh_info);
6311 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6312 ifunc = local_plt + r_symndx;
6313 }
6314 if (ifunc != NULL)
6315 {
6316 struct plt_entry *ent;
6317
6318 for (ent = *ifunc; ent != NULL; ent = ent->next)
6319 if (ent->addend == rel->r_addend)
6320 break;
6321 if (ent == NULL)
6322 abort ();
6323 if (ent->plt.refcount > 0)
6324 ent->plt.refcount -= 1;
6325 continue;
6326 }
6327 }
6328
a33d1f77
AM
6329 switch (r_type)
6330 {
411e1bfb
AM
6331 case R_PPC64_GOT_TLSLD16:
6332 case R_PPC64_GOT_TLSLD16_LO:
6333 case R_PPC64_GOT_TLSLD16_HI:
6334 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 6335 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6336 goto dogot;
6337
6338 case R_PPC64_GOT_TLSGD16:
6339 case R_PPC64_GOT_TLSGD16_LO:
6340 case R_PPC64_GOT_TLSGD16_HI:
6341 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6342 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6343 goto dogot;
6344
6345 case R_PPC64_GOT_TPREL16_DS:
6346 case R_PPC64_GOT_TPREL16_LO_DS:
6347 case R_PPC64_GOT_TPREL16_HI:
6348 case R_PPC64_GOT_TPREL16_HA:
6349 tls_type = TLS_TLS | TLS_TPREL;
6350 goto dogot;
6351
6352 case R_PPC64_GOT_DTPREL16_DS:
6353 case R_PPC64_GOT_DTPREL16_LO_DS:
6354 case R_PPC64_GOT_DTPREL16_HI:
6355 case R_PPC64_GOT_DTPREL16_HA:
6356 tls_type = TLS_TLS | TLS_DTPREL;
6357 goto dogot;
6358
a33d1f77
AM
6359 case R_PPC64_GOT16:
6360 case R_PPC64_GOT16_DS:
6361 case R_PPC64_GOT16_HA:
6362 case R_PPC64_GOT16_HI:
6363 case R_PPC64_GOT16_LO:
6364 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
6365 dogot:
6366 {
6367 struct got_entry *ent;
6368
58ac9f71
AM
6369 if (h != NULL)
6370 ent = h->got.glist;
411e1bfb
AM
6371 else
6372 ent = local_got_ents[r_symndx];
6373
6374 for (; ent != NULL; ent = ent->next)
6375 if (ent->addend == rel->r_addend
e717da7e 6376 && ent->owner == abfd
411e1bfb
AM
6377 && ent->tls_type == tls_type)
6378 break;
6379 if (ent == NULL)
6380 abort ();
6381 if (ent->got.refcount > 0)
6382 ent->got.refcount -= 1;
6383 }
a33d1f77 6384 break;
65f38f15 6385
a33d1f77
AM
6386 case R_PPC64_PLT16_HA:
6387 case R_PPC64_PLT16_HI:
6388 case R_PPC64_PLT16_LO:
6389 case R_PPC64_PLT32:
6390 case R_PPC64_PLT64:
721956f4
AM
6391 case R_PPC64_REL14:
6392 case R_PPC64_REL14_BRNTAKEN:
6393 case R_PPC64_REL14_BRTAKEN:
5d1634d7 6394 case R_PPC64_REL24:
58ac9f71 6395 if (h != NULL)
5d1634d7 6396 {
411e1bfb
AM
6397 struct plt_entry *ent;
6398
411e1bfb
AM
6399 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6400 if (ent->addend == rel->r_addend)
6401 break;
7c6c1722 6402 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 6403 ent->plt.refcount -= 1;
5d1634d7 6404 }
e86ce104 6405 break;
5d1634d7 6406
a33d1f77
AM
6407 default:
6408 break;
6409 }
6410 }
b34976b6 6411 return TRUE;
5bd4f169
AM
6412}
6413
deb0e272
AM
6414/* The maximum size of .sfpr. */
6415#define SFPR_MAX (218*4)
6416
6417struct sfpr_def_parms
6418{
699733f6
AM
6419 const char name[12];
6420 unsigned char lo, hi;
deb0e272
AM
6421 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6422 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6423};
6424
6425/* Auto-generate _save*, _rest* functions in .sfpr. */
6426
4dfe6ac6 6427static bfd_boolean
deb0e272
AM
6428sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6429{
6430 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6431 unsigned int i;
6432 size_t len = strlen (parm->name);
6433 bfd_boolean writing = FALSE;
699733f6 6434 char sym[16];
deb0e272 6435
4dfe6ac6
NC
6436 if (htab == NULL)
6437 return FALSE;
6438
deb0e272
AM
6439 memcpy (sym, parm->name, len);
6440 sym[len + 2] = 0;
6441
6442 for (i = parm->lo; i <= parm->hi; i++)
6443 {
6444 struct elf_link_hash_entry *h;
6445
6446 sym[len + 0] = i / 10 + '0';
6447 sym[len + 1] = i % 10 + '0';
6448 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6449 if (h != NULL
f5385ebf 6450 && !h->def_regular)
deb0e272
AM
6451 {
6452 h->root.type = bfd_link_hash_defined;
6453 h->root.u.def.section = htab->sfpr;
6454 h->root.u.def.value = htab->sfpr->size;
6455 h->type = STT_FUNC;
f5385ebf 6456 h->def_regular = 1;
deb0e272
AM
6457 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6458 writing = TRUE;
6459 if (htab->sfpr->contents == NULL)
6460 {
6461 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6462 if (htab->sfpr->contents == NULL)
6463 return FALSE;
6464 }
6465 }
6466 if (writing)
6467 {
6468 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6469 if (i != parm->hi)
6470 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6471 else
6472 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6473 htab->sfpr->size = p - htab->sfpr->contents;
6474 }
6475 }
6476
6477 return TRUE;
6478}
6479
6480static bfd_byte *
6481savegpr0 (bfd *abfd, bfd_byte *p, int r)
6482{
6483 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6484 return p + 4;
6485}
6486
6487static bfd_byte *
6488savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6489{
6490 p = savegpr0 (abfd, p, r);
6491 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6492 p = p + 4;
6493 bfd_put_32 (abfd, BLR, p);
6494 return p + 4;
6495}
6496
6497static bfd_byte *
6498restgpr0 (bfd *abfd, bfd_byte *p, int r)
6499{
6500 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6501 return p + 4;
6502}
6503
6504static bfd_byte *
6505restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6506{
6507 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6508 p = p + 4;
6509 p = restgpr0 (abfd, p, r);
6510 bfd_put_32 (abfd, MTLR_R0, p);
6511 p = p + 4;
6512 if (r == 29)
6513 {
6514 p = restgpr0 (abfd, p, 30);
6515 p = restgpr0 (abfd, p, 31);
6516 }
6517 bfd_put_32 (abfd, BLR, p);
6518 return p + 4;
6519}
6520
6521static bfd_byte *
6522savegpr1 (bfd *abfd, bfd_byte *p, int r)
6523{
6524 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6525 return p + 4;
6526}
6527
6528static bfd_byte *
6529savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6530{
6531 p = savegpr1 (abfd, p, r);
6532 bfd_put_32 (abfd, BLR, p);
6533 return p + 4;
6534}
6535
6536static bfd_byte *
6537restgpr1 (bfd *abfd, bfd_byte *p, int r)
6538{
6539 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6540 return p + 4;
6541}
6542
6543static bfd_byte *
6544restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6545{
6546 p = restgpr1 (abfd, p, r);
6547 bfd_put_32 (abfd, BLR, p);
6548 return p + 4;
6549}
6550
6551static bfd_byte *
6552savefpr (bfd *abfd, bfd_byte *p, int r)
6553{
6554 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6555 return p + 4;
6556}
6557
6558static bfd_byte *
6559savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6560{
6561 p = savefpr (abfd, p, r);
6562 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6563 p = p + 4;
6564 bfd_put_32 (abfd, BLR, p);
6565 return p + 4;
6566}
6567
6568static bfd_byte *
6569restfpr (bfd *abfd, bfd_byte *p, int r)
6570{
6571 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6572 return p + 4;
6573}
6574
6575static bfd_byte *
6576restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6577{
6578 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6579 p = p + 4;
6580 p = restfpr (abfd, p, r);
6581 bfd_put_32 (abfd, MTLR_R0, p);
6582 p = p + 4;
6583 if (r == 29)
6584 {
6585 p = restfpr (abfd, p, 30);
6586 p = restfpr (abfd, p, 31);
6587 }
6588 bfd_put_32 (abfd, BLR, p);
6589 return p + 4;
6590}
6591
6592static bfd_byte *
6593savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6594{
6595 p = savefpr (abfd, p, r);
6596 bfd_put_32 (abfd, BLR, p);
6597 return p + 4;
6598}
6599
6600static bfd_byte *
6601restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6602{
6603 p = restfpr (abfd, p, r);
6604 bfd_put_32 (abfd, BLR, p);
6605 return p + 4;
6606}
6607
6608static bfd_byte *
6609savevr (bfd *abfd, bfd_byte *p, int r)
6610{
6611 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6612 p = p + 4;
6613 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6614 return p + 4;
6615}
6616
6617static bfd_byte *
6618savevr_tail (bfd *abfd, bfd_byte *p, int r)
6619{
6620 p = savevr (abfd, p, r);
6621 bfd_put_32 (abfd, BLR, p);
6622 return p + 4;
6623}
6624
6625static bfd_byte *
6626restvr (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, LVX_VR0_R12_R0 + (r << 21), p);
6631 return p + 4;
6632}
6633
6634static bfd_byte *
6635restvr_tail (bfd *abfd, bfd_byte *p, int r)
6636{
6637 p = restvr (abfd, p, r);
6638 bfd_put_32 (abfd, BLR, p);
6639 return p + 4;
6640}
6641
e86ce104
AM
6642/* Called via elf_link_hash_traverse to transfer dynamic linking
6643 information on function code symbol entries to their corresponding
6644 function descriptor symbol entries. */
deb0e272 6645
b34976b6 6646static bfd_boolean
4ce794b7 6647func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6648{
e86ce104 6649 struct bfd_link_info *info;
65f38f15 6650 struct ppc_link_hash_table *htab;
411e1bfb 6651 struct plt_entry *ent;
50bc7936
AM
6652 struct ppc_link_hash_entry *fh;
6653 struct ppc_link_hash_entry *fdh;
6654 bfd_boolean force_local;
5bd4f169 6655
50bc7936
AM
6656 fh = (struct ppc_link_hash_entry *) h;
6657 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6658 return TRUE;
e86ce104 6659
4ce794b7 6660 info = inf;
65f38f15 6661 htab = ppc_hash_table (info);
4dfe6ac6
NC
6662 if (htab == NULL)
6663 return FALSE;
5bd4f169 6664
c09bdfe5
AM
6665 /* Resolve undefined references to dot-symbols as the value
6666 in the function descriptor, if we have one in a regular object.
6667 This is to satisfy cases like ".quad .foo". Calls to functions
6668 in dynamic objects are handled elsewhere. */
6669 if (fh->elf.root.type == bfd_link_hash_undefweak
6670 && fh->was_undefined
b31867b6
AM
6671 && (fdh = defined_func_desc (fh)) != NULL
6672 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6673 && opd_entry_value (fdh->elf.root.u.def.section,
6674 fdh->elf.root.u.def.value,
c09bdfe5 6675 &fh->elf.root.u.def.section,
aef36ac1 6676 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
c09bdfe5 6677 {
b31867b6 6678 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6679 fh->elf.forced_local = 1;
b31867b6
AM
6680 fh->elf.def_regular = fdh->elf.def_regular;
6681 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6682 }
6683
e86ce104
AM
6684 /* If this is a function code symbol, transfer dynamic linking
6685 information to the function descriptor symbol. */
50bc7936 6686 if (!fh->is_func)
b34976b6 6687 return TRUE;
e86ce104 6688
50bc7936 6689 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6690 if (ent->plt.refcount > 0)
6691 break;
50bc7936
AM
6692 if (ent == NULL
6693 || fh->elf.root.root.string[0] != '.'
6694 || fh->elf.root.root.string[1] == '\0')
6695 return TRUE;
5bd4f169 6696
50bc7936
AM
6697 /* Find the corresponding function descriptor symbol. Create it
6698 as undefined if necessary. */
5bd4f169 6699
b31867b6 6700 fdh = lookup_fdh (fh, htab);
50bc7936 6701 if (fdh == NULL
df131623 6702 && !info->executable
50bc7936
AM
6703 && (fh->elf.root.type == bfd_link_hash_undefined
6704 || fh->elf.root.type == bfd_link_hash_undefweak))
6705 {
908b32fc 6706 fdh = make_fdh (info, fh);
bb700d78
AM
6707 if (fdh == NULL)
6708 return FALSE;
50bc7936 6709 }
648cca2c 6710
908b32fc 6711 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6712 code symbol is strong undefined, make the fake sym the same.
6713 If the function code symbol is defined, then force the fake
6714 descriptor local; We can't support overriding of symbols in a
6715 shared library on a fake descriptor. */
908b32fc
AM
6716
6717 if (fdh != NULL
6718 && fdh->fake
433817dd 6719 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6720 {
433817dd
AM
6721 if (fh->elf.root.type == bfd_link_hash_undefined)
6722 {
6723 fdh->elf.root.type = bfd_link_hash_undefined;
6724 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6725 }
6726 else if (fh->elf.root.type == bfd_link_hash_defined
6727 || fh->elf.root.type == bfd_link_hash_defweak)
6728 {
6729 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6730 }
908b32fc
AM
6731 }
6732
50bc7936 6733 if (fdh != NULL
f5385ebf 6734 && !fdh->elf.forced_local
df131623 6735 && (!info->executable
f5385ebf
AM
6736 || fdh->elf.def_dynamic
6737 || fdh->elf.ref_dynamic
50bc7936
AM
6738 || (fdh->elf.root.type == bfd_link_hash_undefweak
6739 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6740 {
6741 if (fdh->elf.dynindx == -1)
c152c796 6742 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6743 return FALSE;
f5385ebf
AM
6744 fdh->elf.ref_regular |= fh->elf.ref_regular;
6745 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6746 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6747 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6748 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6749 {
40d16e0b 6750 move_plt_plist (fh, fdh);
f5385ebf 6751 fdh->elf.needs_plt = 1;
e86ce104 6752 }
50bc7936 6753 fdh->is_func_descriptor = 1;
34814b9f
AM
6754 fdh->oh = fh;
6755 fh->oh = fdh;
e86ce104
AM
6756 }
6757
50bc7936
AM
6758 /* Now that the info is on the function descriptor, clear the
6759 function code sym info. Any function code syms for which we
6760 don't have a definition in a regular file, we force local.
6761 This prevents a shared library from exporting syms that have
6762 been imported from another library. Function code syms that
6763 are really in the library we must leave global to prevent the
6764 linker dragging in a definition from a static library. */
93f3fa99
AM
6765 force_local = (!fh->elf.def_regular
6766 || fdh == NULL
6767 || !fdh->elf.def_regular
6768 || fdh->elf.forced_local);
50bc7936
AM
6769 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6770
b34976b6 6771 return TRUE;
e86ce104 6772}
40b8271b 6773
e86ce104 6774/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6775 this hook to a) provide some gcc support functions, and b) transfer
6776 dynamic linking information gathered so far on function code symbol
6777 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6778
b34976b6 6779static bfd_boolean
4ce794b7
AM
6780ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6781 struct bfd_link_info *info)
e86ce104
AM
6782{
6783 struct ppc_link_hash_table *htab;
82bd7b59 6784 unsigned int i;
27fc25a1 6785 static const struct sfpr_def_parms funcs[] =
deb0e272
AM
6786 {
6787 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6788 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6789 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6790 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6791 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6792 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6793 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6794 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6795 { "._savef", 14, 31, savefpr, savefpr1_tail },
6796 { "._restf", 14, 31, restfpr, restfpr1_tail },
6797 { "_savevr_", 20, 31, savevr, savevr_tail },
6798 { "_restvr_", 20, 31, restvr, restvr_tail }
6799 };
e86ce104
AM
6800
6801 htab = ppc_hash_table (info);
4dfe6ac6
NC
6802 if (htab == NULL)
6803 return FALSE;
6804
5295321c
AM
6805 if (!info->relocatable
6806 && htab->elf.hgot != NULL)
6807 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
c66bb0ee 6808
82bd7b59
AM
6809 if (htab->sfpr == NULL)
6810 /* We don't have any relocs. */
b34976b6 6811 return TRUE;
82bd7b59 6812
deb0e272
AM
6813 /* Provide any missing _save* and _rest* functions. */
6814 htab->sfpr->size = 0;
27fc25a1
AM
6815 if (!info->relocatable)
6816 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6817 if (!sfpr_define (info, &funcs[i]))
6818 return FALSE;
82bd7b59 6819
4ce794b7 6820 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6821
eea6121a 6822 if (htab->sfpr->size == 0)
8423293d 6823 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6824
b34976b6 6825 return TRUE;
e86ce104
AM
6826}
6827
6828/* Adjust a symbol defined by a dynamic object and referenced by a
6829 regular object. The current definition is in some section of the
6830 dynamic object, but we're not including those sections. We have to
6831 change the definition to something the rest of the link can
6832 understand. */
6833
b34976b6 6834static bfd_boolean
4ce794b7
AM
6835ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6836 struct elf_link_hash_entry *h)
e86ce104
AM
6837{
6838 struct ppc_link_hash_table *htab;
e86ce104 6839 asection *s;
e86ce104
AM
6840
6841 htab = ppc_hash_table (info);
4dfe6ac6
NC
6842 if (htab == NULL)
6843 return FALSE;
e86ce104
AM
6844
6845 /* Deal with function syms. */
6846 if (h->type == STT_FUNC
e054468f 6847 || h->type == STT_GNU_IFUNC
f5385ebf 6848 || h->needs_plt)
e86ce104
AM
6849 {
6850 /* Clear procedure linkage table information for any symbol that
6851 won't need a .plt entry. */
411e1bfb
AM
6852 struct plt_entry *ent;
6853 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6854 if (ent->plt.refcount > 0)
6855 break;
8387904d 6856 if (ent == NULL
e054468f
AM
6857 || (h->type != STT_GNU_IFUNC
6858 && (SYMBOL_CALLS_LOCAL (info, h)
6859 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6860 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6861 {
411e1bfb 6862 h->plt.plist = NULL;
f5385ebf 6863 h->needs_plt = 0;
40b8271b 6864 }
5bd4f169 6865 }
bbd7ec4a 6866 else
411e1bfb 6867 h->plt.plist = NULL;
5bd4f169
AM
6868
6869 /* If this is a weak symbol, and there is a real definition, the
6870 processor independent code will have arranged for us to see the
6871 real definition first, and we can just use the same value. */
f6e332e6 6872 if (h->u.weakdef != NULL)
5bd4f169 6873 {
f6e332e6
AM
6874 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6875 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6876 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6877 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6878 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6879 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6880 return TRUE;
5bd4f169
AM
6881 }
6882
5bd4f169
AM
6883 /* If we are creating a shared library, we must presume that the
6884 only references to the symbol are via the global offset table.
6885 For such cases we need not do anything here; the relocations will
6886 be handled correctly by relocate_section. */
6887 if (info->shared)
b34976b6 6888 return TRUE;
5bd4f169 6889
65f38f15
AM
6890 /* If there are no references to this symbol that do not use the
6891 GOT, we don't need to generate a copy reloc. */
f5385ebf 6892 if (!h->non_got_ref)
b34976b6 6893 return TRUE;
65f38f15 6894
b186458a
JJ
6895 /* Don't generate a copy reloc for symbols defined in the executable. */
6896 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6897 return TRUE;
6898
f4656909 6899 if (ELIMINATE_COPY_RELOCS)
65f38f15 6900 {
f4656909 6901 struct ppc_link_hash_entry * eh;
6061a67d 6902 struct elf_dyn_relocs *p;
65f38f15 6903
f4656909
AM
6904 eh = (struct ppc_link_hash_entry *) h;
6905 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6906 {
6907 s = p->sec->output_section;
6908 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6909 break;
6910 }
6911
6912 /* If we didn't find any dynamic relocs in read-only sections, then
6913 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6914 if (p == NULL)
6915 {
f5385ebf 6916 h->non_got_ref = 0;
f4656909
AM
6917 return TRUE;
6918 }
65f38f15
AM
6919 }
6920
5d35169e 6921 if (h->plt.plist != NULL)
97b639ba
AM
6922 {
6923 /* We should never get here, but unfortunately there are versions
6924 of gcc out there that improperly (for this ABI) put initialized
6925 function pointers, vtable refs and suchlike in read-only
6926 sections. Allow them to proceed, but warn that this might
6927 break at runtime. */
25f53a85 6928 info->callbacks->einfo
bc30df16 6929 (_("%P: copy reloc against `%T' requires lazy plt linking; "
25f53a85 6930 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6931 h->root.root.string);
6932 }
5d35169e
AM
6933
6934 /* This is a reference to a symbol defined by a dynamic object which
6935 is not a function. */
6936
5bd4f169
AM
6937 /* We must allocate the symbol in our .dynbss section, which will
6938 become part of the .bss section of the executable. There will be
6939 an entry for this symbol in the .dynsym section. The dynamic
6940 object will contain position independent code, so all references
6941 from the dynamic object to this symbol will go through the global
6942 offset table. The dynamic linker will use the .dynsym entry to
6943 determine the address it must put in the global offset table, so
6944 both the dynamic object and the regular object will refer to the
6945 same memory location for the variable. */
5bd4f169 6946
04c9666a
AM
6947 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6948 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6949 runtime process image. We need to remember the offset into the
6950 .rela.bss section we are going to use. */
1d7e9d18 6951 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5bd4f169 6952 {
eea6121a 6953 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6954 h->needs_copy = 1;
5bd4f169
AM
6955 }
6956
4ce794b7 6957 s = htab->dynbss;
5bd4f169 6958
027297b7 6959 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6960}
6961
e86ce104
AM
6962/* If given a function descriptor symbol, hide both the function code
6963 sym and the descriptor. */
6964static void
4ce794b7
AM
6965ppc64_elf_hide_symbol (struct bfd_link_info *info,
6966 struct elf_link_hash_entry *h,
6967 bfd_boolean force_local)
e86ce104 6968{
34814b9f 6969 struct ppc_link_hash_entry *eh;
e86ce104
AM
6970 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6971
34814b9f
AM
6972 eh = (struct ppc_link_hash_entry *) h;
6973 if (eh->is_func_descriptor)
e86ce104 6974 {
34814b9f 6975 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6976
721956f4 6977 if (fh == NULL)
d1329ca3
AM
6978 {
6979 const char *p, *q;
6980 struct ppc_link_hash_table *htab;
6981 char save;
6982
6983 /* We aren't supposed to use alloca in BFD because on
6984 systems which do not have alloca the version in libiberty
6985 calls xmalloc, which might cause the program to crash
6986 when it runs out of memory. This function doesn't have a
6987 return status, so there's no way to gracefully return an
6988 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6989 accessed; It's either a string in an ELF string table,
6990 or allocated in an objalloc structure. */
d1329ca3 6991
34814b9f 6992 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6993 save = *p;
6994 *(char *) p = '.';
6995 htab = ppc_hash_table (info);
4dfe6ac6
NC
6996 if (htab == NULL)
6997 return;
6998
34814b9f
AM
6999 fh = (struct ppc_link_hash_entry *)
7000 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7001 *(char *) p = save;
7002
7003 /* Unfortunately, if it so happens that the string we were
7004 looking for was allocated immediately before this string,
7005 then we overwrote the string terminator. That's the only
7006 reason the lookup should fail. */
7007 if (fh == NULL)
7008 {
34814b9f
AM
7009 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7010 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 7011 --q, --p;
34814b9f
AM
7012 if (q < eh->elf.root.root.string && *p == '.')
7013 fh = (struct ppc_link_hash_entry *)
7014 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
7015 }
7016 if (fh != NULL)
7017 {
34814b9f
AM
7018 eh->oh = fh;
7019 fh->oh = eh;
d1329ca3
AM
7020 }
7021 }
e86ce104 7022 if (fh != NULL)
34814b9f 7023 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
7024 }
7025}
7026
411e1bfb 7027static bfd_boolean
8843416a
AM
7028get_sym_h (struct elf_link_hash_entry **hp,
7029 Elf_Internal_Sym **symp,
7030 asection **symsecp,
f961d9dd 7031 unsigned char **tls_maskp,
8843416a
AM
7032 Elf_Internal_Sym **locsymsp,
7033 unsigned long r_symndx,
7034 bfd *ibfd)
411e1bfb 7035{
0ffa91dd 7036 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
7037
7038 if (r_symndx >= symtab_hdr->sh_info)
7039 {
7040 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7041 struct elf_link_hash_entry *h;
7042
7043 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7044 h = elf_follow_link (h);
411e1bfb
AM
7045
7046 if (hp != NULL)
7047 *hp = h;
7048
7049 if (symp != NULL)
7050 *symp = NULL;
7051
7052 if (symsecp != NULL)
7053 {
7054 asection *symsec = NULL;
7055 if (h->root.type == bfd_link_hash_defined
7056 || h->root.type == bfd_link_hash_defweak)
7057 symsec = h->root.u.def.section;
7058 *symsecp = symsec;
7059 }
7060
e7b938ca 7061 if (tls_maskp != NULL)
411e1bfb
AM
7062 {
7063 struct ppc_link_hash_entry *eh;
7064
7065 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 7066 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
7067 }
7068 }
7069 else
7070 {
7071 Elf_Internal_Sym *sym;
7072 Elf_Internal_Sym *locsyms = *locsymsp;
7073
7074 if (locsyms == NULL)
7075 {
7076 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7077 if (locsyms == NULL)
7078 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7079 symtab_hdr->sh_info,
7080 0, NULL, NULL, NULL);
7081 if (locsyms == NULL)
7082 return FALSE;
7083 *locsymsp = locsyms;
7084 }
7085 sym = locsyms + r_symndx;
7086
7087 if (hp != NULL)
7088 *hp = NULL;
7089
7090 if (symp != NULL)
7091 *symp = sym;
7092
7093 if (symsecp != NULL)
cb33740c 7094 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 7095
e7b938ca 7096 if (tls_maskp != NULL)
411e1bfb
AM
7097 {
7098 struct got_entry **lgot_ents;
f961d9dd 7099 unsigned char *tls_mask;
411e1bfb 7100
e7b938ca 7101 tls_mask = NULL;
411e1bfb
AM
7102 lgot_ents = elf_local_got_ents (ibfd);
7103 if (lgot_ents != NULL)
7104 {
e054468f
AM
7105 struct plt_entry **local_plt = (struct plt_entry **)
7106 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 7107 unsigned char *lgot_masks = (unsigned char *)
e054468f 7108 (local_plt + symtab_hdr->sh_info);
e7b938ca 7109 tls_mask = &lgot_masks[r_symndx];
411e1bfb 7110 }
e7b938ca 7111 *tls_maskp = tls_mask;
411e1bfb
AM
7112 }
7113 }
7114 return TRUE;
7115}
7116
e7b938ca 7117/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 7118 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 7119 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
7120
7121static int
f961d9dd 7122get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
7123 unsigned long *toc_symndx,
7124 bfd_vma *toc_addend,
0d4792f7 7125 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
7126 const Elf_Internal_Rela *rel,
7127 bfd *ibfd)
411e1bfb
AM
7128{
7129 unsigned long r_symndx;
0d4792f7 7130 int next_r;
411e1bfb
AM
7131 struct elf_link_hash_entry *h;
7132 Elf_Internal_Sym *sym;
7133 asection *sec;
7134 bfd_vma off;
7135
7136 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 7137 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 7138 return 0;
411e1bfb 7139
e7b938ca 7140 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 7141 || sec == NULL
6bee8834 7142 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 7143 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 7144 return 1;
411e1bfb
AM
7145
7146 /* Look inside a TOC section too. */
7147 if (h != NULL)
7148 {
7149 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7150 off = h->root.u.def.value;
7151 }
7152 else
7153 off = sym->st_value;
7154 off += rel->r_addend;
7155 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
7156 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7157 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
7158 if (toc_symndx != NULL)
7159 *toc_symndx = r_symndx;
3a71aa26
AM
7160 if (toc_addend != NULL)
7161 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7162 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7163 return 0;
854b41e7 7164 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
7165 && (next_r == -1 || next_r == -2))
7166 return 1 - next_r;
951fd09b 7167 return 1;
411e1bfb
AM
7168}
7169
3b421ab3
AM
7170/* Find (or create) an entry in the tocsave hash table. */
7171
7172static struct tocsave_entry *
7173tocsave_find (struct ppc_link_hash_table *htab,
7174 enum insert_option insert,
7175 Elf_Internal_Sym **local_syms,
7176 const Elf_Internal_Rela *irela,
7177 bfd *ibfd)
7178{
7179 unsigned long r_indx;
7180 struct elf_link_hash_entry *h;
7181 Elf_Internal_Sym *sym;
7182 struct tocsave_entry ent, *p;
7183 hashval_t hash;
7184 struct tocsave_entry **slot;
7185
7186 r_indx = ELF64_R_SYM (irela->r_info);
7187 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7188 return NULL;
7189 if (ent.sec == NULL || ent.sec->output_section == NULL)
7190 {
7191 (*_bfd_error_handler)
7192 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7193 return NULL;
7194 }
7195
7196 if (h != NULL)
7197 ent.offset = h->root.u.def.value;
7198 else
7199 ent.offset = sym->st_value;
7200 ent.offset += irela->r_addend;
7201
7202 hash = tocsave_htab_hash (&ent);
7203 slot = ((struct tocsave_entry **)
7204 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7205 if (slot == NULL)
7206 return NULL;
7207
7208 if (*slot == NULL)
7209 {
7210 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7211 if (p == NULL)
7212 return NULL;
7213 *p = ent;
7214 *slot = p;
7215 }
7216 return *slot;
7217}
7218
754021d0 7219/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 7220 code for the old ABI, these will already have been done. */
754021d0
AM
7221
7222static bfd_boolean
7223adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7224{
7225 struct ppc_link_hash_entry *eh;
7226 asection *sym_sec;
74f0fb50 7227 struct _opd_sec_data *opd;
754021d0
AM
7228
7229 if (h->root.type == bfd_link_hash_indirect)
7230 return TRUE;
7231
754021d0
AM
7232 if (h->root.type != bfd_link_hash_defined
7233 && h->root.type != bfd_link_hash_defweak)
7234 return TRUE;
7235
7236 eh = (struct ppc_link_hash_entry *) h;
7237 if (eh->adjust_done)
7238 return TRUE;
7239
7240 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
7241 opd = get_opd_info (sym_sec);
7242 if (opd != NULL && opd->adjust != NULL)
754021d0 7243 {
74f0fb50 7244 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
7245 if (adjust == -1)
7246 {
7247 /* This entry has been deleted. */
b3fac117 7248 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
7249 if (dsec == NULL)
7250 {
7251 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
dbaa2011 7252 if (discarded_section (dsec))
81688140 7253 {
b3fac117 7254 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
7255 break;
7256 }
7257 }
4025353c 7258 eh->elf.root.u.def.value = 0;
81688140 7259 eh->elf.root.u.def.section = dsec;
4025353c
AM
7260 }
7261 else
7262 eh->elf.root.u.def.value += adjust;
754021d0
AM
7263 eh->adjust_done = 1;
7264 }
7265 return TRUE;
7266}
7267
8c1d1bb8 7268/* Handles decrementing dynamic reloc counts for the reloc specified by
19e08130 7269 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
8c1d1bb8
AM
7270 have already been determined. */
7271
7272static bfd_boolean
7273dec_dynrel_count (bfd_vma r_info,
7274 asection *sec,
7275 struct bfd_link_info *info,
7276 Elf_Internal_Sym **local_syms,
7277 struct elf_link_hash_entry *h,
19e08130 7278 Elf_Internal_Sym *sym)
8c1d1bb8
AM
7279{
7280 enum elf_ppc64_reloc_type r_type;
19e08130 7281 asection *sym_sec = NULL;
8c1d1bb8
AM
7282
7283 /* Can this reloc be dynamic? This switch, and later tests here
7284 should be kept in sync with the code in check_relocs. */
7285 r_type = ELF64_R_TYPE (r_info);
7286 switch (r_type)
7287 {
7288 default:
7289 return TRUE;
7290
7291 case R_PPC64_TPREL16:
7292 case R_PPC64_TPREL16_LO:
7293 case R_PPC64_TPREL16_HI:
7294 case R_PPC64_TPREL16_HA:
7295 case R_PPC64_TPREL16_DS:
7296 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
7297 case R_PPC64_TPREL16_HIGH:
7298 case R_PPC64_TPREL16_HIGHA:
8c1d1bb8
AM
7299 case R_PPC64_TPREL16_HIGHER:
7300 case R_PPC64_TPREL16_HIGHERA:
7301 case R_PPC64_TPREL16_HIGHEST:
7302 case R_PPC64_TPREL16_HIGHESTA:
7303 if (!info->shared)
7304 return TRUE;
7305
7306 case R_PPC64_TPREL64:
7307 case R_PPC64_DTPMOD64:
7308 case R_PPC64_DTPREL64:
7309 case R_PPC64_ADDR64:
7310 case R_PPC64_REL30:
7311 case R_PPC64_REL32:
7312 case R_PPC64_REL64:
7313 case R_PPC64_ADDR14:
7314 case R_PPC64_ADDR14_BRNTAKEN:
7315 case R_PPC64_ADDR14_BRTAKEN:
7316 case R_PPC64_ADDR16:
7317 case R_PPC64_ADDR16_DS:
7318 case R_PPC64_ADDR16_HA:
7319 case R_PPC64_ADDR16_HI:
f9c6b907
AM
7320 case R_PPC64_ADDR16_HIGH:
7321 case R_PPC64_ADDR16_HIGHA:
8c1d1bb8
AM
7322 case R_PPC64_ADDR16_HIGHER:
7323 case R_PPC64_ADDR16_HIGHERA:
7324 case R_PPC64_ADDR16_HIGHEST:
7325 case R_PPC64_ADDR16_HIGHESTA:
7326 case R_PPC64_ADDR16_LO:
7327 case R_PPC64_ADDR16_LO_DS:
7328 case R_PPC64_ADDR24:
7329 case R_PPC64_ADDR32:
7330 case R_PPC64_UADDR16:
7331 case R_PPC64_UADDR32:
7332 case R_PPC64_UADDR64:
7333 case R_PPC64_TOC:
7334 break;
7335 }
7336
7337 if (local_syms != NULL)
7338 {
7339 unsigned long r_symndx;
8c1d1bb8
AM
7340 bfd *ibfd = sec->owner;
7341
7342 r_symndx = ELF64_R_SYM (r_info);
7343 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7344 return FALSE;
7345 }
7346
7347 if ((info->shared
1d483afe 7348 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8 7349 || (h != NULL
198f1157 7350 && (!SYMBOLIC_BIND (info, h)
8c1d1bb8
AM
7351 || h->root.type == bfd_link_hash_defweak
7352 || !h->def_regular))))
7353 || (ELIMINATE_COPY_RELOCS
7354 && !info->shared
7355 && h != NULL
7356 && (h->root.type == bfd_link_hash_defweak
7357 || !h->def_regular)))
7358 ;
7359 else
7360 return TRUE;
7361
7362 if (h != NULL)
6edfbbad 7363 {
19e08130
AM
7364 struct elf_dyn_relocs *p;
7365 struct elf_dyn_relocs **pp;
7366 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7367
7368 /* elf_gc_sweep may have already removed all dyn relocs associated
7369 with local syms for a given section. Also, symbol flags are
7370 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7371 report a dynreloc miscount. */
7372 if (*pp == NULL && info->gc_sections)
7373 return TRUE;
7374
7375 while ((p = *pp) != NULL)
60124e18 7376 {
19e08130
AM
7377 if (p->sec == sec)
7378 {
7379 if (!must_be_dyn_reloc (info, r_type))
7380 p->pc_count -= 1;
7381 p->count -= 1;
7382 if (p->count == 0)
7383 *pp = p->next;
7384 return TRUE;
7385 }
7386 pp = &p->next;
60124e18 7387 }
6edfbbad 7388 }
19e08130
AM
7389 else
7390 {
7391 struct ppc_dyn_relocs *p;
7392 struct ppc_dyn_relocs **pp;
7393 void *vpp;
7394 bfd_boolean is_ifunc;
8c1d1bb8 7395
19e08130
AM
7396 if (local_syms == NULL)
7397 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7398 if (sym_sec == NULL)
7399 sym_sec = sec;
c57da1a7 7400
19e08130
AM
7401 vpp = &elf_section_data (sym_sec)->local_dynrel;
7402 pp = (struct ppc_dyn_relocs **) vpp;
7403
7404 if (*pp == NULL && info->gc_sections)
7405 return TRUE;
7406
7407 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7408 while ((p = *pp) != NULL)
8c1d1bb8 7409 {
19e08130
AM
7410 if (p->sec == sec && p->ifunc == is_ifunc)
7411 {
7412 p->count -= 1;
7413 if (p->count == 0)
7414 *pp = p->next;
7415 return TRUE;
7416 }
7417 pp = &p->next;
8c1d1bb8 7418 }
8c1d1bb8
AM
7419 }
7420
8de848d8 7421 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 7422 sec->owner, sec);
8c1d1bb8
AM
7423 bfd_set_error (bfd_error_bad_value);
7424 return FALSE;
7425}
7426
754021d0
AM
7427/* Remove unused Official Procedure Descriptor entries. Currently we
7428 only remove those associated with functions in discarded link-once
7429 sections, or weakly defined functions that have been overridden. It
7430 would be possible to remove many more entries for statically linked
7431 applications. */
7432
b34976b6 7433bfd_boolean
33c0ec9d 7434ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
7435{
7436 bfd *ibfd;
754021d0 7437 bfd_boolean some_edited = FALSE;
3f764659 7438 asection *need_pad = NULL;
1e2f5b6e 7439
411e1bfb 7440 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
7441 {
7442 asection *sec;
7443 Elf_Internal_Rela *relstart, *rel, *relend;
7444 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7445 Elf_Internal_Sym *local_syms;
1e2f5b6e 7446 bfd_vma offset;
74f0fb50 7447 struct _opd_sec_data *opd;
3f764659
JJ
7448 bfd_boolean need_edit, add_aux_fields;
7449 bfd_size_type cnt_16b = 0;
1e2f5b6e 7450
854b41e7
AM
7451 if (!is_ppc64_elf (ibfd))
7452 continue;
7453
1e2f5b6e 7454 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7455 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7456 continue;
7457
dbaa2011 7458 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
4b85d634
AM
7459 continue;
7460
1e2f5b6e
AM
7461 if (sec->output_section == bfd_abs_section_ptr)
7462 continue;
7463
7464 /* Look through the section relocs. */
7465 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7466 continue;
7467
6cdc0ccc 7468 local_syms = NULL;
0ffa91dd 7469 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7470
7471 /* Read the relocations. */
4ce794b7 7472 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7473 info->keep_memory);
1e2f5b6e 7474 if (relstart == NULL)
b34976b6 7475 return FALSE;
1e2f5b6e
AM
7476
7477 /* First run through the relocs to check they are sane, and to
7478 determine whether we need to edit this opd section. */
b34976b6 7479 need_edit = FALSE;
3f764659 7480 need_pad = sec;
1e2f5b6e
AM
7481 offset = 0;
7482 relend = relstart + sec->reloc_count;
50bc7936 7483 for (rel = relstart; rel < relend; )
1e2f5b6e 7484 {
04c9666a 7485 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7486 unsigned long r_symndx;
7487 asection *sym_sec;
7488 struct elf_link_hash_entry *h;
7489 Elf_Internal_Sym *sym;
7490
3f764659 7491 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7492 only interested in the reloc pointing to a function entry
7493 point. */
50bc7936
AM
7494 if (rel->r_offset != offset
7495 || rel + 1 >= relend
7496 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
7497 {
7498 /* If someone messes with .opd alignment then after a
7499 "ld -r" we might have padding in the middle of .opd.
7500 Also, there's nothing to prevent someone putting
7501 something silly in .opd with the assembler. No .opd
b34976b6 7502 optimization for them! */
3f764659 7503 broken_opd:
1e2f5b6e 7504 (*_bfd_error_handler)
d003868e 7505 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 7506 need_edit = FALSE;
1e2f5b6e
AM
7507 break;
7508 }
7509
50bc7936
AM
7510 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7511 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7512 {
7513 (*_bfd_error_handler)
d003868e
AM
7514 (_("%B: unexpected reloc type %u in .opd section"),
7515 ibfd, r_type);
50bc7936
AM
7516 need_edit = FALSE;
7517 break;
7518 }
7519
1e2f5b6e 7520 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7521 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7522 r_symndx, ibfd))
50bc7936 7523 goto error_ret;
1e2f5b6e
AM
7524
7525 if (sym_sec == NULL || sym_sec->owner == NULL)
7526 {
411e1bfb
AM
7527 const char *sym_name;
7528 if (h != NULL)
7529 sym_name = h->root.root.string;
7530 else
26c61ae5
L
7531 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7532 sym_sec);
411e1bfb 7533
1e2f5b6e 7534 (*_bfd_error_handler)
d003868e
AM
7535 (_("%B: undefined sym `%s' in .opd section"),
7536 ibfd, sym_name);
b34976b6 7537 need_edit = FALSE;
1e2f5b6e
AM
7538 break;
7539 }
7540
51020317
AM
7541 /* opd entries are always for functions defined in the
7542 current input bfd. If the symbol isn't defined in the
7543 input bfd, then we won't be using the function in this
7544 bfd; It must be defined in a linkonce section in another
7545 bfd, or is weak. It's also possible that we are
7546 discarding the function due to a linker script /DISCARD/,
7547 which we test for via the output_section. */
7548 if (sym_sec->owner != ibfd
7549 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7550 need_edit = TRUE;
1e2f5b6e 7551
50bc7936 7552 rel += 2;
3f764659
JJ
7553 if (rel == relend
7554 || (rel + 1 == relend && rel->r_offset == offset + 16))
7555 {
7556 if (sec->size == offset + 24)
7557 {
7558 need_pad = NULL;
7559 break;
7560 }
7561 if (rel == relend && sec->size == offset + 16)
7562 {
7563 cnt_16b++;
7564 break;
7565 }
7566 goto broken_opd;
7567 }
7568
7569 if (rel->r_offset == offset + 24)
7570 offset += 24;
7571 else if (rel->r_offset != offset + 16)
7572 goto broken_opd;
7573 else if (rel + 1 < relend
7574 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7575 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7576 {
7577 offset += 16;
7578 cnt_16b++;
7579 }
7580 else if (rel + 2 < relend
7581 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7582 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7583 {
7584 offset += 24;
7585 rel += 1;
7586 }
7587 else
7588 goto broken_opd;
1e2f5b6e
AM
7589 }
7590
3f764659
JJ
7591 add_aux_fields = non_overlapping && cnt_16b > 0;
7592
7593 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7594 {
7595 Elf_Internal_Rela *write_rel;
d4730f92 7596 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7597 bfd_byte *rptr, *wptr;
983bddc8 7598 bfd_byte *new_contents;
b34976b6 7599 bfd_boolean skip;
3f764659 7600 long opd_ent_size;
74f0fb50
AM
7601 bfd_size_type amt;
7602
983bddc8 7603 new_contents = NULL;
74f0fb50
AM
7604 amt = sec->size * sizeof (long) / 8;
7605 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7606 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7607 if (opd->adjust == NULL)
7608 return FALSE;
7609 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7610
7611 /* This seems a waste of time as input .opd sections are all
7612 zeros as generated by gcc, but I suppose there's no reason
7613 this will always be so. We might start putting something in
7614 the third word of .opd entries. */
7615 if ((sec->flags & SEC_IN_MEMORY) == 0)
7616 {
eea6121a
AM
7617 bfd_byte *loc;
7618 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7619 {
eea6121a
AM
7620 if (loc != NULL)
7621 free (loc);
50bc7936 7622 error_ret:
6cdc0ccc
AM
7623 if (local_syms != NULL
7624 && symtab_hdr->contents != (unsigned char *) local_syms)
7625 free (local_syms);
6cdc0ccc
AM
7626 if (elf_section_data (sec)->relocs != relstart)
7627 free (relstart);
b34976b6 7628 return FALSE;
6cdc0ccc 7629 }
1e2f5b6e
AM
7630 sec->contents = loc;
7631 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7632 }
7633
7634 elf_section_data (sec)->relocs = relstart;
7635
3f764659 7636 new_contents = sec->contents;
3f764659
JJ
7637 if (add_aux_fields)
7638 {
7639 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7640 if (new_contents == NULL)
7641 return FALSE;
7642 need_pad = FALSE;
3f764659 7643 }
b4f4e59f
AM
7644 wptr = new_contents;
7645 rptr = sec->contents;
3f764659 7646
1e2f5b6e 7647 write_rel = relstart;
b34976b6 7648 skip = FALSE;
1e2f5b6e 7649 offset = 0;
3f764659 7650 opd_ent_size = 0;
1e2f5b6e
AM
7651 for (rel = relstart; rel < relend; rel++)
7652 {
50bc7936
AM
7653 unsigned long r_symndx;
7654 asection *sym_sec;
7655 struct elf_link_hash_entry *h;
7656 Elf_Internal_Sym *sym;
7657
7658 r_symndx = ELF64_R_SYM (rel->r_info);
7659 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7660 r_symndx, ibfd))
50bc7936
AM
7661 goto error_ret;
7662
1e2f5b6e
AM
7663 if (rel->r_offset == offset)
7664 {
50bc7936 7665 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7666
7667 /* See if the .opd entry is full 24 byte or
7668 16 byte (with fd_aux entry overlapped with next
7669 fd_func). */
7670 opd_ent_size = 24;
7671 if ((rel + 2 == relend && sec->size == offset + 16)
7672 || (rel + 3 < relend
7673 && rel[2].r_offset == offset + 16
7674 && rel[3].r_offset == offset + 24
7675 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7676 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7677 opd_ent_size = 16;
7678
4025353c
AM
7679 if (h != NULL
7680 && h->root.root.string[0] == '.')
c4f68ce3 7681 {
4dfe6ac6
NC
7682 struct ppc_link_hash_table *htab;
7683
7684 htab = ppc_hash_table (info);
7685 if (htab != NULL)
7686 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7687 htab);
c4f68ce3
AM
7688 if (fdh != NULL
7689 && fdh->elf.root.type != bfd_link_hash_defined
7690 && fdh->elf.root.type != bfd_link_hash_defweak)
7691 fdh = NULL;
7692 }
1e2f5b6e 7693
51020317
AM
7694 skip = (sym_sec->owner != ibfd
7695 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7696 if (skip)
7697 {
4025353c 7698 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7699 {
7700 /* Arrange for the function descriptor sym
7701 to be dropped. */
d6fe2dc1
AM
7702 fdh->elf.root.u.def.value = 0;
7703 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7704 }
74f0fb50 7705 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7706 }
7707 else
1e2f5b6e
AM
7708 {
7709 /* We'll be keeping this opd entry. */
7710
4025353c 7711 if (fdh != NULL)
1e2f5b6e 7712 {
754021d0
AM
7713 /* Redefine the function descriptor symbol to
7714 this location in the opd section. It is
7715 necessary to update the value here rather
7716 than using an array of adjustments as we do
7717 for local symbols, because various places
7718 in the generic ELF code use the value
7719 stored in u.def.value. */
3f764659 7720 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7721 fdh->adjust_done = 1;
1e2f5b6e 7722 }
754021d0
AM
7723
7724 /* Local syms are a bit tricky. We could
7725 tweak them as they can be cached, but
7726 we'd need to look through the local syms
7727 for the function descriptor sym which we
7728 don't have at the moment. So keep an
7729 array of adjustments. */
74f0fb50 7730 opd->adjust[rel->r_offset / 8]
3f764659 7731 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7732
7733 if (wptr != rptr)
3f764659
JJ
7734 memcpy (wptr, rptr, opd_ent_size);
7735 wptr += opd_ent_size;
7736 if (add_aux_fields && opd_ent_size == 16)
7737 {
7738 memset (wptr, '\0', 8);
7739 wptr += 8;
7740 }
1e2f5b6e 7741 }
3f764659
JJ
7742 rptr += opd_ent_size;
7743 offset += opd_ent_size;
1e2f5b6e
AM
7744 }
7745
50bc7936
AM
7746 if (skip)
7747 {
60124e18
AM
7748 if (!NO_OPD_RELOCS
7749 && !info->relocatable
18d944df 7750 && !dec_dynrel_count (rel->r_info, sec, info,
19e08130 7751 NULL, h, sym))
8c1d1bb8 7752 goto error_ret;
50bc7936
AM
7753 }
7754 else
1e2f5b6e 7755 {
50bc7936
AM
7756 /* We need to adjust any reloc offsets to point to the
7757 new opd entries. While we're at it, we may as well
7758 remove redundant relocs. */
74f0fb50 7759 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7760 if (write_rel != rel)
7761 memcpy (write_rel, rel, sizeof (*rel));
7762 ++write_rel;
7763 }
7764 }
7765
3f764659 7766 sec->size = wptr - new_contents;
1e2f5b6e 7767 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7768 if (add_aux_fields)
7769 {
7770 free (sec->contents);
7771 sec->contents = new_contents;
7772 }
7773
05bf9422 7774 /* Fudge the header size too, as this is used later in
cdcf6e38 7775 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7776 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7777 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7778 some_edited = TRUE;
1e2f5b6e 7779 }
6cdc0ccc 7780 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7781 free (relstart);
6cdc0ccc 7782
411e1bfb
AM
7783 if (local_syms != NULL
7784 && symtab_hdr->contents != (unsigned char *) local_syms)
7785 {
7786 if (!info->keep_memory)
7787 free (local_syms);
7788 else
7789 symtab_hdr->contents = (unsigned char *) local_syms;
7790 }
7791 }
7792
754021d0
AM
7793 if (some_edited)
7794 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7795
3f764659
JJ
7796 /* If we are doing a final link and the last .opd entry is just 16 byte
7797 long, add a 8 byte padding after it. */
7798 if (need_pad != NULL && !info->relocatable)
7799 {
7800 bfd_byte *p;
7801
7802 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7803 {
7804 BFD_ASSERT (need_pad->size > 0);
7805
7806 p = bfd_malloc (need_pad->size + 8);
7807 if (p == NULL)
7808 return FALSE;
699733f6 7809
3f764659
JJ
7810 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7811 p, 0, need_pad->size))
7812 return FALSE;
7813
7814 need_pad->contents = p;
7815 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7816 }
7817 else
7818 {
7819 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7820 if (p == NULL)
7821 return FALSE;
7822
7823 need_pad->contents = p;
7824 }
7825
7826 memset (need_pad->contents + need_pad->size, 0, 8);
7827 need_pad->size += 8;
7828 }
7829
411e1bfb
AM
7830 return TRUE;
7831}
7832
e1918d23 7833/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7834
e1918d23 7835asection *
33c0ec9d
AM
7836ppc64_elf_tls_setup (struct bfd_link_info *info,
7837 int no_tls_get_addr_opt,
7838 int *no_multi_toc)
411e1bfb 7839{
411e1bfb
AM
7840 struct ppc_link_hash_table *htab;
7841
411e1bfb 7842 htab = ppc_hash_table (info);
4dfe6ac6
NC
7843 if (htab == NULL)
7844 return NULL;
7845
ee67d69a
AM
7846 if (abiversion (info->output_bfd) == 1)
7847 htab->opd_abi = 1;
7848
33c0ec9d
AM
7849 if (*no_multi_toc)
7850 htab->do_multi_toc = 0;
7851 else if (!htab->do_multi_toc)
7852 *no_multi_toc = 1;
7853
3a71aa26
AM
7854 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7855 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7856 FALSE, FALSE, TRUE));
a7f2871e
AM
7857 /* Move dynamic linking info to the function descriptor sym. */
7858 if (htab->tls_get_addr != NULL)
7859 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7860 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7861 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7862 FALSE, FALSE, TRUE));
a7f2871e
AM
7863 if (!no_tls_get_addr_opt)
7864 {
7865 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7866
7867 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7868 FALSE, FALSE, TRUE);
7869 if (opt != NULL)
7870 func_desc_adjust (opt, info);
7871 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7872 FALSE, FALSE, TRUE);
7873 if (opt_fd != NULL
7874 && (opt_fd->root.type == bfd_link_hash_defined
7875 || opt_fd->root.type == bfd_link_hash_defweak))
7876 {
7877 /* If glibc supports an optimized __tls_get_addr call stub,
7878 signalled by the presence of __tls_get_addr_opt, and we'll
7879 be calling __tls_get_addr via a plt call stub, then
7880 make __tls_get_addr point to __tls_get_addr_opt. */
7881 tga_fd = &htab->tls_get_addr_fd->elf;
7882 if (htab->elf.dynamic_sections_created
7883 && tga_fd != NULL
7884 && (tga_fd->type == STT_FUNC
7885 || tga_fd->needs_plt)
7886 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7887 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7888 && tga_fd->root.type == bfd_link_hash_undefweak)))
7889 {
7890 struct plt_entry *ent;
7891
7892 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7893 if (ent->plt.refcount > 0)
7894 break;
7895 if (ent != NULL)
7896 {
7897 tga_fd->root.type = bfd_link_hash_indirect;
7898 tga_fd->root.u.i.link = &opt_fd->root;
7899 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7900 if (opt_fd->dynindx != -1)
7901 {
7902 /* Use __tls_get_addr_opt in dynamic relocations. */
7903 opt_fd->dynindx = -1;
7904 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7905 opt_fd->dynstr_index);
7906 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7907 return NULL;
a7f2871e
AM
7908 }
7909 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7910 tga = &htab->tls_get_addr->elf;
7911 if (opt != NULL && tga != NULL)
7912 {
7913 tga->root.type = bfd_link_hash_indirect;
7914 tga->root.u.i.link = &opt->root;
7915 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7916 _bfd_elf_link_hash_hide_symbol (info, opt,
7917 tga->forced_local);
7918 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7919 }
7920 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7921 htab->tls_get_addr_fd->is_func_descriptor = 1;
7922 if (htab->tls_get_addr != NULL)
7923 {
7924 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7925 htab->tls_get_addr->is_func = 1;
7926 }
7927 }
7928 }
7929 }
7930 else
7931 no_tls_get_addr_opt = TRUE;
7932 }
7933 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7934 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7935}
8387904d 7936
3a71aa26
AM
7937/* Return TRUE iff REL is a branch reloc with a global symbol matching
7938 HASH1 or HASH2. */
8387904d 7939
3a71aa26
AM
7940static bfd_boolean
7941branch_reloc_hash_match (const bfd *ibfd,
7942 const Elf_Internal_Rela *rel,
7943 const struct ppc_link_hash_entry *hash1,
7944 const struct ppc_link_hash_entry *hash2)
7945{
7946 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7947 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7948 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7949
e054468f 7950 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7951 {
3a71aa26
AM
7952 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7953 struct elf_link_hash_entry *h;
8387904d 7954
3a71aa26 7955 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7956 h = elf_follow_link (h);
3a71aa26
AM
7957 if (h == &hash1->elf || h == &hash2->elf)
7958 return TRUE;
a48ebf4d 7959 }
3a71aa26 7960 return FALSE;
951fd09b 7961}
411e1bfb 7962
951fd09b
AM
7963/* Run through all the TLS relocs looking for optimization
7964 opportunities. The linker has been hacked (see ppc64elf.em) to do
7965 a preliminary section layout so that we know the TLS segment
7966 offsets. We can't optimize earlier because some optimizations need
7967 to know the tp offset, and we need to optimize before allocating
7968 dynamic relocations. */
7969
7970bfd_boolean
33c0ec9d 7971ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7972{
7973 bfd *ibfd;
7974 asection *sec;
7975 struct ppc_link_hash_table *htab;
663a1470 7976 unsigned char *toc_ref;
102890f0 7977 int pass;
951fd09b 7978
1d483afe 7979 if (info->relocatable || !info->executable)
411e1bfb
AM
7980 return TRUE;
7981
951fd09b 7982 htab = ppc_hash_table (info);
4dfe6ac6
NC
7983 if (htab == NULL)
7984 return FALSE;
7985
663a1470
AM
7986 /* Make two passes over the relocs. On the first pass, mark toc
7987 entries involved with tls relocs, and check that tls relocs
7988 involved in setting up a tls_get_addr call are indeed followed by
7989 such a call. If they are not, we can't do any tls optimization.
7990 On the second pass twiddle tls_mask flags to notify
7991 relocate_section that optimization can be done, and adjust got
7992 and plt refcounts. */
7993 toc_ref = NULL;
7994 for (pass = 0; pass < 2; ++pass)
7995 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7996 {
7997 Elf_Internal_Sym *locsyms = NULL;
7998 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7999
102890f0
AM
8000 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8001 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8002 {
8003 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 8004 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 8005
102890f0
AM
8006 /* Read the relocations. */
8007 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8008 info->keep_memory);
8009 if (relstart == NULL)
8010 return FALSE;
411e1bfb 8011
102890f0
AM
8012 relend = relstart + sec->reloc_count;
8013 for (rel = relstart; rel < relend; rel++)
8014 {
8015 enum elf_ppc64_reloc_type r_type;
8016 unsigned long r_symndx;
8017 struct elf_link_hash_entry *h;
8018 Elf_Internal_Sym *sym;
8019 asection *sym_sec;
f961d9dd
AM
8020 unsigned char *tls_mask;
8021 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
8022 bfd_vma value;
8023 bfd_boolean ok_tprel, is_local;
8024 long toc_ref_index = 0;
8025 int expecting_tls_get_addr = 0;
663a1470 8026 bfd_boolean ret = FALSE;
411e1bfb 8027
102890f0
AM
8028 r_symndx = ELF64_R_SYM (rel->r_info);
8029 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8030 r_symndx, ibfd))
8031 {
8032 err_free_rel:
8033 if (elf_section_data (sec)->relocs != relstart)
8034 free (relstart);
8035 if (toc_ref != NULL)
8036 free (toc_ref);
8037 if (locsyms != NULL
0ffa91dd 8038 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
8039 != (unsigned char *) locsyms))
8040 free (locsyms);
663a1470 8041 return ret;
102890f0 8042 }
411e1bfb 8043
102890f0
AM
8044 if (h != NULL)
8045 {
766bc656
AM
8046 if (h->root.type == bfd_link_hash_defined
8047 || h->root.type == bfd_link_hash_defweak)
8048 value = h->root.u.def.value;
8049 else if (h->root.type == bfd_link_hash_undefweak)
8050 value = 0;
8051 else
663a1470
AM
8052 {
8053 found_tls_get_addr_arg = 0;
8054 continue;
8055 }
102890f0
AM
8056 }
8057 else
8058 /* Symbols referenced by TLS relocs must be of type
8059 STT_TLS. So no need for .opd local sym adjust. */
8060 value = sym->st_value;
8061
8062 ok_tprel = FALSE;
8063 is_local = FALSE;
8064 if (h == NULL
8065 || !h->def_dynamic)
8066 {
8067 is_local = TRUE;
766bc656
AM
8068 if (h != NULL
8069 && h->root.type == bfd_link_hash_undefweak)
8070 ok_tprel = TRUE;
8071 else
8072 {
8073 value += sym_sec->output_offset;
8074 value += sym_sec->output_section->vma;
8075 value -= htab->elf.tls_sec->vma;
8076 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8077 < (bfd_vma) 1 << 32);
8078 }
102890f0 8079 }
951fd09b 8080
102890f0 8081 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
8082 /* If this section has old-style __tls_get_addr calls
8083 without marker relocs, then check that each
8084 __tls_get_addr call reloc is preceded by a reloc
8085 that conceivably belongs to the __tls_get_addr arg
8086 setup insn. If we don't find matching arg setup
8087 relocs, don't do any tls optimization. */
8088 if (pass == 0
8089 && sec->has_tls_get_addr_call
8090 && h != NULL
8091 && (h == &htab->tls_get_addr->elf
8092 || h == &htab->tls_get_addr_fd->elf)
8093 && !found_tls_get_addr_arg
8094 && is_branch_reloc (r_type))
8095 {
25f53a85 8096 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
8097 "TLS optimization disabled\n"),
8098 ibfd, sec, rel->r_offset);
8099 ret = TRUE;
8100 goto err_free_rel;
8101 }
8102
8103 found_tls_get_addr_arg = 0;
102890f0
AM
8104 switch (r_type)
8105 {
8106 case R_PPC64_GOT_TLSLD16:
8107 case R_PPC64_GOT_TLSLD16_LO:
8108 expecting_tls_get_addr = 1;
663a1470 8109 found_tls_get_addr_arg = 1;
102890f0
AM
8110 /* Fall thru */
8111
8112 case R_PPC64_GOT_TLSLD16_HI:
8113 case R_PPC64_GOT_TLSLD16_HA:
8114 /* These relocs should never be against a symbol
8115 defined in a shared lib. Leave them alone if
8116 that turns out to be the case. */
8117 if (!is_local)
8118 continue;
411e1bfb 8119
102890f0 8120 /* LD -> LE */
411e1bfb 8121 tls_set = 0;
102890f0
AM
8122 tls_clear = TLS_LD;
8123 tls_type = TLS_TLS | TLS_LD;
8124 break;
411e1bfb 8125
102890f0
AM
8126 case R_PPC64_GOT_TLSGD16:
8127 case R_PPC64_GOT_TLSGD16_LO:
8128 expecting_tls_get_addr = 1;
663a1470 8129 found_tls_get_addr_arg = 1;
102890f0
AM
8130 /* Fall thru */
8131
8132 case R_PPC64_GOT_TLSGD16_HI:
8133 case R_PPC64_GOT_TLSGD16_HA:
8134 if (ok_tprel)
8135 /* GD -> LE */
411e1bfb 8136 tls_set = 0;
102890f0
AM
8137 else
8138 /* GD -> IE */
8139 tls_set = TLS_TLS | TLS_TPRELGD;
8140 tls_clear = TLS_GD;
8141 tls_type = TLS_TLS | TLS_GD;
8142 break;
8143
8144 case R_PPC64_GOT_TPREL16_DS:
8145 case R_PPC64_GOT_TPREL16_LO_DS:
8146 case R_PPC64_GOT_TPREL16_HI:
8147 case R_PPC64_GOT_TPREL16_HA:
8148 if (ok_tprel)
8149 {
8150 /* IE -> LE */
8151 tls_set = 0;
8152 tls_clear = TLS_TPREL;
8153 tls_type = TLS_TLS | TLS_TPREL;
8154 break;
8155 }
411e1bfb
AM
8156 continue;
8157
727fc41e
AM
8158 case R_PPC64_TLSGD:
8159 case R_PPC64_TLSLD:
663a1470
AM
8160 found_tls_get_addr_arg = 1;
8161 /* Fall thru */
8162
8163 case R_PPC64_TLS:
8164 case R_PPC64_TOC16:
8165 case R_PPC64_TOC16_LO:
102890f0
AM
8166 if (sym_sec == NULL || sym_sec != toc)
8167 continue;
8168
8169 /* Mark this toc entry as referenced by a TLS
8170 code sequence. We can do that now in the
8171 case of R_PPC64_TLS, and after checking for
8172 tls_get_addr for the TOC16 relocs. */
8173 if (toc_ref == NULL)
663a1470
AM
8174 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8175 if (toc_ref == NULL)
8176 goto err_free_rel;
8177
102890f0
AM
8178 if (h != NULL)
8179 value = h->root.u.def.value;
8180 else
8181 value = sym->st_value;
8182 value += rel->r_addend;
8183 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 8184 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
8185 if (r_type == R_PPC64_TLS
8186 || r_type == R_PPC64_TLSGD
8187 || r_type == R_PPC64_TLSLD)
102890f0
AM
8188 {
8189 toc_ref[toc_ref_index] = 1;
8190 continue;
8191 }
8192
8193 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8194 continue;
8195
8196 tls_set = 0;
8197 tls_clear = 0;
8198 expecting_tls_get_addr = 2;
8199 break;
8200
8201 case R_PPC64_TPREL64:
8202 if (pass == 0
8203 || sec != toc
8204 || toc_ref == NULL
663a1470 8205 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8206 continue;
8207 if (ok_tprel)
8208 {
8209 /* IE -> LE */
8210 tls_set = TLS_EXPLICIT;
8211 tls_clear = TLS_TPREL;
8212 break;
8213 }
8214 continue;
8215
8216 case R_PPC64_DTPMOD64:
8217 if (pass == 0
8218 || sec != toc
8219 || toc_ref == NULL
663a1470 8220 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
8221 continue;
8222 if (rel + 1 < relend
8223 && (rel[1].r_info
8224 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8225 && rel[1].r_offset == rel->r_offset + 8)
8226 {
8227 if (ok_tprel)
8228 /* GD -> LE */
8229 tls_set = TLS_EXPLICIT | TLS_GD;
8230 else
8231 /* GD -> IE */
8232 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8233 tls_clear = TLS_GD;
8234 }
8235 else
8236 {
8237 if (!is_local)
8238 continue;
8239
8240 /* LD -> LE */
8241 tls_set = TLS_EXPLICIT;
8242 tls_clear = TLS_LD;
8243 }
8244 break;
8245
8246 default:
8247 continue;
8248 }
8249
8250 if (pass == 0)
8251 {
727fc41e
AM
8252 if (!expecting_tls_get_addr
8253 || !sec->has_tls_get_addr_call)
102890f0
AM
8254 continue;
8255
3a71aa26
AM
8256 if (rel + 1 < relend
8257 && branch_reloc_hash_match (ibfd, rel + 1,
8258 htab->tls_get_addr,
8259 htab->tls_get_addr_fd))
102890f0 8260 {
3a71aa26 8261 if (expecting_tls_get_addr == 2)
102890f0 8262 {
3a71aa26 8263 /* Check for toc tls entries. */
f961d9dd 8264 unsigned char *toc_tls;
3a71aa26
AM
8265 int retval;
8266
8267 retval = get_tls_mask (&toc_tls, NULL, NULL,
8268 &locsyms,
8269 rel, ibfd);
8270 if (retval == 0)
8271 goto err_free_rel;
663a1470
AM
8272 if (toc_tls != NULL)
8273 {
8274 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8275 found_tls_get_addr_arg = 1;
8276 if (retval > 1)
8277 toc_ref[toc_ref_index] = 1;
8278 }
102890f0 8279 }
3a71aa26 8280 continue;
102890f0
AM
8281 }
8282
8283 if (expecting_tls_get_addr != 1)
8284 continue;
8285
8286 /* Uh oh, we didn't find the expected call. We
8287 could just mark this symbol to exclude it
8288 from tls optimization but it's safer to skip
663a1470 8289 the entire optimization. */
25f53a85 8290 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
8291 "TLS optimization disabled\n"),
8292 ibfd, sec, rel->r_offset);
8293 ret = TRUE;
8294 goto err_free_rel;
102890f0
AM
8295 }
8296
85f7a9cb 8297 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
8298 {
8299 struct plt_entry *ent;
8300 for (ent = htab->tls_get_addr->elf.plt.plist;
8301 ent != NULL;
8302 ent = ent->next)
8303 if (ent->addend == 0)
411e1bfb 8304 {
102890f0 8305 if (ent->plt.refcount > 0)
30038c59 8306 {
102890f0
AM
8307 ent->plt.refcount -= 1;
8308 expecting_tls_get_addr = 0;
30038c59 8309 }
102890f0 8310 break;
411e1bfb 8311 }
102890f0 8312 }
411e1bfb 8313
85f7a9cb 8314 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
8315 {
8316 struct plt_entry *ent;
8317 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8318 ent != NULL;
8319 ent = ent->next)
8320 if (ent->addend == 0)
411e1bfb 8321 {
102890f0
AM
8322 if (ent->plt.refcount > 0)
8323 ent->plt.refcount -= 1;
8324 break;
411e1bfb 8325 }
102890f0 8326 }
411e1bfb 8327
102890f0 8328 if (tls_clear == 0)
30038c59
AM
8329 continue;
8330
102890f0
AM
8331 if ((tls_set & TLS_EXPLICIT) == 0)
8332 {
8333 struct got_entry *ent;
411e1bfb 8334
102890f0
AM
8335 /* Adjust got entry for this reloc. */
8336 if (h != NULL)
8337 ent = h->got.glist;
8338 else
8339 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 8340
102890f0
AM
8341 for (; ent != NULL; ent = ent->next)
8342 if (ent->addend == rel->r_addend
8343 && ent->owner == ibfd
8344 && ent->tls_type == tls_type)
8345 break;
8346 if (ent == NULL)
8347 abort ();
411e1bfb 8348
102890f0
AM
8349 if (tls_set == 0)
8350 {
8351 /* We managed to get rid of a got entry. */
8352 if (ent->got.refcount > 0)
8353 ent->got.refcount -= 1;
8354 }
8355 }
8356 else
8357 {
8358 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8359 we'll lose one or two dyn relocs. */
8360 if (!dec_dynrel_count (rel->r_info, sec, info,
19e08130 8361 NULL, h, sym))
102890f0 8362 return FALSE;
411e1bfb 8363
102890f0
AM
8364 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8365 {
8366 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
19e08130 8367 NULL, h, sym))
102890f0
AM
8368 return FALSE;
8369 }
8370 }
411e1bfb 8371
102890f0
AM
8372 *tls_mask |= tls_set;
8373 *tls_mask &= ~tls_clear;
8374 }
8c1d1bb8 8375
102890f0
AM
8376 if (elf_section_data (sec)->relocs != relstart)
8377 free (relstart);
8378 }
411e1bfb 8379
663a1470
AM
8380 if (locsyms != NULL
8381 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8382 {
8383 if (!info->keep_memory)
8384 free (locsyms);
8385 else
8386 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8387 }
8388 }
411e1bfb 8389
663a1470
AM
8390 if (toc_ref != NULL)
8391 free (toc_ref);
b34976b6 8392 return TRUE;
1e2f5b6e 8393}
b34976b6 8394
c5614fa4
AM
8395/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8396 the values of any global symbols in a toc section that has been
8397 edited. Globals in toc sections should be a rarity, so this function
8398 sets a flag if any are found in toc sections other than the one just
8399 edited, so that futher hash table traversals can be avoided. */
8400
8401struct adjust_toc_info
8402{
8403 asection *toc;
8404 unsigned long *skip;
8405 bfd_boolean global_toc_syms;
8406};
8407
ba761f19
AM
8408enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8409
c5614fa4
AM
8410static bfd_boolean
8411adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8412{
8413 struct ppc_link_hash_entry *eh;
8414 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 8415 unsigned long i;
c5614fa4 8416
c5614fa4
AM
8417 if (h->root.type != bfd_link_hash_defined
8418 && h->root.type != bfd_link_hash_defweak)
8419 return TRUE;
8420
8421 eh = (struct ppc_link_hash_entry *) h;
8422 if (eh->adjust_done)
8423 return TRUE;
8424
8425 if (eh->elf.root.u.def.section == toc_inf->toc)
8426 {
854b41e7
AM
8427 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8428 i = toc_inf->toc->rawsize >> 3;
c5614fa4 8429 else
854b41e7
AM
8430 i = eh->elf.root.u.def.value >> 3;
8431
ba761f19 8432 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8433 {
8434 (*_bfd_error_handler)
854b41e7
AM
8435 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8436 do
8437 ++i;
ba761f19 8438 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8439 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8440 }
854b41e7
AM
8441
8442 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8443 eh->adjust_done = 1;
8444 }
8445 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8446 toc_inf->global_toc_syms = TRUE;
8447
8448 return TRUE;
8449}
8450
560c8763
AM
8451/* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8452
8453static bfd_boolean
8454ok_lo_toc_insn (unsigned int insn)
8455{
8456 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8457 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8458 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8459 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8460 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8461 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8462 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8463 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8464 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8465 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8466 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8467 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8468 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8469 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8470 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8471 && (insn & 3) != 1)
8472 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8473 && ((insn & 3) == 0 || (insn & 3) == 3))
8474 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8475}
8476
c5614fa4
AM
8477/* Examine all relocs referencing .toc sections in order to remove
8478 unused .toc entries. */
8479
8480bfd_boolean
33c0ec9d 8481ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8482{
8483 bfd *ibfd;
8484 struct adjust_toc_info toc_inf;
67f0cbdb 8485 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8486
67f0cbdb 8487 htab->do_toc_opt = 1;
c5614fa4
AM
8488 toc_inf.global_toc_syms = TRUE;
8489 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8490 {
8491 asection *toc, *sec;
8492 Elf_Internal_Shdr *symtab_hdr;
8493 Elf_Internal_Sym *local_syms;
425b145b 8494 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8495 unsigned long *skip, *drop;
8496 unsigned char *used;
8497 unsigned char *keep, last, some_unused;
8498
854b41e7
AM
8499 if (!is_ppc64_elf (ibfd))
8500 continue;
8501
c5614fa4
AM
8502 toc = bfd_get_section_by_name (ibfd, ".toc");
8503 if (toc == NULL
92b7a70f 8504 || toc->size == 0
dbaa2011
AM
8505 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8506 || discarded_section (toc))
c5614fa4
AM
8507 continue;
8508
425b145b 8509 toc_relocs = NULL;
c5614fa4 8510 local_syms = NULL;
0ffa91dd 8511 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8512
8513 /* Look at sections dropped from the final link. */
8514 skip = NULL;
8515 relstart = NULL;
8516 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8517 {
8518 if (sec->reloc_count == 0
dbaa2011 8519 || !discarded_section (sec)
c5614fa4
AM
8520 || get_opd_info (sec)
8521 || (sec->flags & SEC_ALLOC) == 0
8522 || (sec->flags & SEC_DEBUGGING) != 0)
8523 continue;
8524
8525 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8526 if (relstart == NULL)
8527 goto error_ret;
8528
8529 /* Run through the relocs to see which toc entries might be
8530 unused. */
8531 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8532 {
8533 enum elf_ppc64_reloc_type r_type;
8534 unsigned long r_symndx;
8535 asection *sym_sec;
8536 struct elf_link_hash_entry *h;
8537 Elf_Internal_Sym *sym;
8538 bfd_vma val;
8539
8540 r_type = ELF64_R_TYPE (rel->r_info);
8541 switch (r_type)
8542 {
8543 default:
8544 continue;
8545
8546 case R_PPC64_TOC16:
8547 case R_PPC64_TOC16_LO:
8548 case R_PPC64_TOC16_HI:
8549 case R_PPC64_TOC16_HA:
8550 case R_PPC64_TOC16_DS:
8551 case R_PPC64_TOC16_LO_DS:
8552 break;
8553 }
8554
8555 r_symndx = ELF64_R_SYM (rel->r_info);
8556 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8557 r_symndx, ibfd))
8558 goto error_ret;
8559
8560 if (sym_sec != toc)
8561 continue;
8562
8563 if (h != NULL)
8564 val = h->root.u.def.value;
8565 else
8566 val = sym->st_value;
8567 val += rel->r_addend;
8568
8569 if (val >= toc->size)
8570 continue;
8571
8572 /* Anything in the toc ought to be aligned to 8 bytes.
8573 If not, don't mark as unused. */
8574 if (val & 7)
8575 continue;
8576
8577 if (skip == NULL)
8578 {
854b41e7 8579 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8580 if (skip == NULL)
8581 goto error_ret;
8582 }
8583
ba761f19 8584 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8585 }
8586
8587 if (elf_section_data (sec)->relocs != relstart)
8588 free (relstart);
8589 }
8590
ba761f19
AM
8591 /* For largetoc loads of address constants, we can convert
8592 . addis rx,2,addr@got@ha
8593 . ld ry,addr@got@l(rx)
8594 to
8595 . addis rx,2,addr@toc@ha
8596 . addi ry,rx,addr@toc@l
8597 when addr is within 2G of the toc pointer. This then means
8598 that the word storing "addr" in the toc is no longer needed. */
68ffbac6 8599
ba761f19
AM
8600 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8601 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8602 && toc->reloc_count != 0)
8603 {
8604 /* Read toc relocs. */
425b145b
AM
8605 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8606 info->keep_memory);
8607 if (toc_relocs == NULL)
ba761f19
AM
8608 goto error_ret;
8609
425b145b 8610 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8611 {
8612 enum elf_ppc64_reloc_type r_type;
8613 unsigned long r_symndx;
8614 asection *sym_sec;
8615 struct elf_link_hash_entry *h;
8616 Elf_Internal_Sym *sym;
8617 bfd_vma val, addr;
8618
8619 r_type = ELF64_R_TYPE (rel->r_info);
8620 if (r_type != R_PPC64_ADDR64)
8621 continue;
8622
8623 r_symndx = ELF64_R_SYM (rel->r_info);
8624 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8625 r_symndx, ibfd))
8626 goto error_ret;
8627
425b145b 8628 if (sym_sec == NULL
dbaa2011 8629 || discarded_section (sym_sec))
425b145b
AM
8630 continue;
8631
726d3ab0 8632 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8633 continue;
8634
8635 if (h != NULL)
bddc25c9
AM
8636 {
8637 if (h->type == STT_GNU_IFUNC)
8638 continue;
8639 val = h->root.u.def.value;
8640 }
ba761f19 8641 else
bddc25c9
AM
8642 {
8643 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8644 continue;
8645 val = sym->st_value;
8646 }
ba761f19
AM
8647 val += rel->r_addend;
8648 val += sym_sec->output_section->vma + sym_sec->output_offset;
8649
8650 /* We don't yet know the exact toc pointer value, but we
8651 know it will be somewhere in the toc section. Don't
8652 optimize if the difference from any possible toc
8653 pointer is outside [ff..f80008000, 7fff7fff]. */
8654 addr = toc->output_section->vma + TOC_BASE_OFF;
8655 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8656 continue;
8657
8658 addr = toc->output_section->vma + toc->output_section->rawsize;
8659 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8660 continue;
8661
8662 if (skip == NULL)
8663 {
8664 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8665 if (skip == NULL)
8666 goto error_ret;
8667 }
8668
8669 skip[rel->r_offset >> 3]
425b145b 8670 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8671 }
ba761f19
AM
8672 }
8673
c5614fa4
AM
8674 if (skip == NULL)
8675 continue;
8676
8677 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8678 if (used == NULL)
8679 {
8680 error_ret:
8681 if (local_syms != NULL
8682 && symtab_hdr->contents != (unsigned char *) local_syms)
8683 free (local_syms);
8684 if (sec != NULL
8685 && relstart != NULL
8686 && elf_section_data (sec)->relocs != relstart)
8687 free (relstart);
425b145b
AM
8688 if (toc_relocs != NULL
8689 && elf_section_data (toc)->relocs != toc_relocs)
8690 free (toc_relocs);
c5614fa4
AM
8691 if (skip != NULL)
8692 free (skip);
8693 return FALSE;
8694 }
8695
30038c59
AM
8696 /* Now check all kept sections that might reference the toc.
8697 Check the toc itself last. */
8698 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8699 : ibfd->sections);
c5614fa4 8700 sec != NULL;
c5614fa4 8701 sec = (sec == toc ? NULL
c5614fa4 8702 : sec->next == NULL ? toc
30038c59 8703 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8704 : sec->next))
8705 {
8706 int repeat;
8707
8708 if (sec->reloc_count == 0
dbaa2011 8709 || discarded_section (sec)
c5614fa4
AM
8710 || get_opd_info (sec)
8711 || (sec->flags & SEC_ALLOC) == 0
8712 || (sec->flags & SEC_DEBUGGING) != 0)
8713 continue;
8714
854b41e7
AM
8715 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8716 info->keep_memory);
c5614fa4
AM
8717 if (relstart == NULL)
8718 goto error_ret;
8719
8720 /* Mark toc entries referenced as used. */
c5614fa4 8721 do
d4f1ee75
AM
8722 {
8723 repeat = 0;
8724 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8725 {
8726 enum elf_ppc64_reloc_type r_type;
8727 unsigned long r_symndx;
8728 asection *sym_sec;
8729 struct elf_link_hash_entry *h;
8730 Elf_Internal_Sym *sym;
8731 bfd_vma val;
8732 enum {no_check, check_lo, check_ha} insn_check;
98528052 8733
d4f1ee75
AM
8734 r_type = ELF64_R_TYPE (rel->r_info);
8735 switch (r_type)
8736 {
8737 default:
8738 insn_check = no_check;
8739 break;
98528052 8740
d4f1ee75
AM
8741 case R_PPC64_GOT_TLSLD16_HA:
8742 case R_PPC64_GOT_TLSGD16_HA:
8743 case R_PPC64_GOT_TPREL16_HA:
8744 case R_PPC64_GOT_DTPREL16_HA:
8745 case R_PPC64_GOT16_HA:
8746 case R_PPC64_TOC16_HA:
8747 insn_check = check_ha;
8748 break;
98528052 8749
d4f1ee75
AM
8750 case R_PPC64_GOT_TLSLD16_LO:
8751 case R_PPC64_GOT_TLSGD16_LO:
8752 case R_PPC64_GOT_TPREL16_LO_DS:
8753 case R_PPC64_GOT_DTPREL16_LO_DS:
8754 case R_PPC64_GOT16_LO:
8755 case R_PPC64_GOT16_LO_DS:
8756 case R_PPC64_TOC16_LO:
8757 case R_PPC64_TOC16_LO_DS:
8758 insn_check = check_lo;
8759 break;
8760 }
560c8763 8761
d4f1ee75
AM
8762 if (insn_check != no_check)
8763 {
8764 bfd_vma off = rel->r_offset & ~3;
8765 unsigned char buf[4];
8766 unsigned int insn;
c5614fa4 8767
d4f1ee75
AM
8768 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8769 {
8770 free (used);
8771 goto error_ret;
8772 }
8773 insn = bfd_get_32 (ibfd, buf);
8774 if (insn_check == check_lo
8775 ? !ok_lo_toc_insn (insn)
8776 : ((insn & ((0x3f << 26) | 0x1f << 16))
8777 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8778 {
8779 char str[12];
8780
8781 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8782 sprintf (str, "%#08x", insn);
8783 info->callbacks->einfo
8784 (_("%P: %H: toc optimization is not supported for"
8785 " %s instruction.\n"),
8786 ibfd, sec, rel->r_offset & ~3, str);
8787 }
8788 }
c5614fa4 8789
d4f1ee75
AM
8790 switch (r_type)
8791 {
8792 case R_PPC64_TOC16:
8793 case R_PPC64_TOC16_LO:
8794 case R_PPC64_TOC16_HI:
8795 case R_PPC64_TOC16_HA:
8796 case R_PPC64_TOC16_DS:
8797 case R_PPC64_TOC16_LO_DS:
8798 /* In case we're taking addresses of toc entries. */
8799 case R_PPC64_ADDR64:
8800 break;
c5614fa4 8801
d4f1ee75
AM
8802 default:
8803 continue;
8804 }
c5614fa4 8805
d4f1ee75
AM
8806 r_symndx = ELF64_R_SYM (rel->r_info);
8807 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8808 r_symndx, ibfd))
8809 {
8810 free (used);
8811 goto error_ret;
8812 }
c5614fa4 8813
d4f1ee75
AM
8814 if (sym_sec != toc)
8815 continue;
c5614fa4 8816
d4f1ee75
AM
8817 if (h != NULL)
8818 val = h->root.u.def.value;
8819 else
8820 val = sym->st_value;
8821 val += rel->r_addend;
ba761f19 8822
d4f1ee75
AM
8823 if (val >= toc->size)
8824 continue;
ba761f19 8825
d4f1ee75
AM
8826 if ((skip[val >> 3] & can_optimize) != 0)
8827 {
8828 bfd_vma off;
8829 unsigned char opc;
8830
8831 switch (r_type)
8832 {
8833 case R_PPC64_TOC16_HA:
ba761f19 8834 break;
ba761f19 8835
d4f1ee75
AM
8836 case R_PPC64_TOC16_LO_DS:
8837 off = rel->r_offset;
8838 off += (bfd_big_endian (ibfd) ? -2 : 3);
8839 if (!bfd_get_section_contents (ibfd, sec, &opc,
8840 off, 1))
8841 {
8842 free (used);
8843 goto error_ret;
8844 }
8845 if ((opc & (0x3f << 2)) == (58u << 2))
8846 break;
8847 /* Fall thru */
ba761f19 8848
d4f1ee75
AM
8849 default:
8850 /* Wrong sort of reloc, or not a ld. We may
8851 as well clear ref_from_discarded too. */
8852 skip[val >> 3] = 0;
8853 }
8854 }
8855
8856 if (sec != toc)
8857 used[val >> 3] = 1;
8858 /* For the toc section, we only mark as used if this
8859 entry itself isn't unused. */
8860 else if ((used[rel->r_offset >> 3]
8861 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8862 && !used[val >> 3])
8863 {
8864 /* Do all the relocs again, to catch reference
8865 chains. */
8866 repeat = 1;
8867 used[val >> 3] = 1;
8868 }
8869 }
8870 }
c5614fa4 8871 while (repeat);
854b41e7
AM
8872
8873 if (elf_section_data (sec)->relocs != relstart)
8874 free (relstart);
c5614fa4
AM
8875 }
8876
8877 /* Merge the used and skip arrays. Assume that TOC
8878 doublewords not appearing as either used or unused belong
8879 to to an entry more than one doubleword in size. */
8880 for (drop = skip, keep = used, last = 0, some_unused = 0;
8881 drop < skip + (toc->size + 7) / 8;
8882 ++drop, ++keep)
8883 {
8884 if (*keep)
8885 {
ba761f19
AM
8886 *drop &= ~ref_from_discarded;
8887 if ((*drop & can_optimize) != 0)
8888 some_unused = 1;
c5614fa4
AM
8889 last = 0;
8890 }
b140b010 8891 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8892 {
8893 some_unused = 1;
ba761f19 8894 last = ref_from_discarded;
c5614fa4
AM
8895 }
8896 else
8897 *drop = last;
8898 }
8899
8900 free (used);
8901
8902 if (some_unused)
8903 {
8904 bfd_byte *contents, *src;
8905 unsigned long off;
d62b3684 8906 Elf_Internal_Sym *sym;
ba761f19 8907 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8908
8909 /* Shuffle the toc contents, and at the same time convert the
8910 skip array from booleans into offsets. */
8911 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8912 goto error_ret;
8913
8914 elf_section_data (toc)->this_hdr.contents = contents;
8915
8916 for (src = contents, off = 0, drop = skip;
8917 src < contents + toc->size;
8918 src += 8, ++drop)
8919 {
ba761f19
AM
8920 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8921 off += 8;
c5614fa4
AM
8922 else if (off != 0)
8923 {
8924 *drop = off;
8925 memcpy (src - off, src, 8);
8926 }
8927 }
854b41e7 8928 *drop = off;
c5614fa4
AM
8929 toc->rawsize = toc->size;
8930 toc->size = src - contents - off;
8931
ba761f19
AM
8932 /* Adjust addends for relocs against the toc section sym,
8933 and optimize any accesses we can. */
c5614fa4
AM
8934 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8935 {
8936 if (sec->reloc_count == 0
dbaa2011 8937 || discarded_section (sec))
c5614fa4
AM
8938 continue;
8939
8940 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8941 info->keep_memory);
c5614fa4
AM
8942 if (relstart == NULL)
8943 goto error_ret;
8944
8945 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8946 {
8947 enum elf_ppc64_reloc_type r_type;
8948 unsigned long r_symndx;
8949 asection *sym_sec;
8950 struct elf_link_hash_entry *h;
854b41e7 8951 bfd_vma val;
c5614fa4
AM
8952
8953 r_type = ELF64_R_TYPE (rel->r_info);
8954 switch (r_type)
8955 {
8956 default:
8957 continue;
8958
8959 case R_PPC64_TOC16:
8960 case R_PPC64_TOC16_LO:
8961 case R_PPC64_TOC16_HI:
8962 case R_PPC64_TOC16_HA:
8963 case R_PPC64_TOC16_DS:
8964 case R_PPC64_TOC16_LO_DS:
8965 case R_PPC64_ADDR64:
8966 break;
8967 }
8968
8969 r_symndx = ELF64_R_SYM (rel->r_info);
8970 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8971 r_symndx, ibfd))
8972 goto error_ret;
8973
ba761f19 8974 if (sym_sec != toc)
c5614fa4
AM
8975 continue;
8976
ba761f19
AM
8977 if (h != NULL)
8978 val = h->root.u.def.value;
8979 else
8980 {
8981 val = sym->st_value;
8982 if (val != 0)
8983 local_toc_syms = TRUE;
8984 }
8985
8986 val += rel->r_addend;
854b41e7
AM
8987
8988 if (val > toc->rawsize)
8989 val = toc->rawsize;
ba761f19
AM
8990 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8991 continue;
8992 else if ((skip[val >> 3] & can_optimize) != 0)
8993 {
8994 Elf_Internal_Rela *tocrel
425b145b 8995 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8996 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8997
8998 switch (r_type)
8999 {
9000 case R_PPC64_TOC16_HA:
9001 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9002 break;
9003
9004 case R_PPC64_TOC16_LO_DS:
9005 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9006 break;
9007
9008 default:
28942f62
AM
9009 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9010 ppc_howto_init ();
b140b010 9011 info->callbacks->einfo
bc30df16 9012 (_("%P: %H: %s references "
b140b010
AM
9013 "optimized away TOC entry\n"),
9014 ibfd, sec, rel->r_offset,
9015 ppc64_elf_howto_table[r_type]->name);
9016 bfd_set_error (bfd_error_bad_value);
9017 goto error_ret;
ba761f19
AM
9018 }
9019 rel->r_addend = tocrel->r_addend;
9020 elf_section_data (sec)->relocs = relstart;
9021 continue;
9022 }
9023
9024 if (h != NULL || sym->st_value != 0)
9025 continue;
854b41e7
AM
9026
9027 rel->r_addend -= skip[val >> 3];
9028 elf_section_data (sec)->relocs = relstart;
c5614fa4 9029 }
854b41e7
AM
9030
9031 if (elf_section_data (sec)->relocs != relstart)
9032 free (relstart);
c5614fa4
AM
9033 }
9034
9035 /* We shouldn't have local or global symbols defined in the TOC,
9036 but handle them anyway. */
df22d223
AM
9037 if (local_syms != NULL)
9038 for (sym = local_syms;
9039 sym < local_syms + symtab_hdr->sh_info;
9040 ++sym)
9041 if (sym->st_value != 0
9042 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9043 {
9044 unsigned long i;
854b41e7 9045
df22d223
AM
9046 if (sym->st_value > toc->rawsize)
9047 i = toc->rawsize >> 3;
9048 else
9049 i = sym->st_value >> 3;
854b41e7 9050
df22d223
AM
9051 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9052 {
9053 if (local_toc_syms)
9054 (*_bfd_error_handler)
9055 (_("%s defined on removed toc entry"),
9056 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9057 do
9058 ++i;
9059 while ((skip[i] & (ref_from_discarded | can_optimize)));
9060 sym->st_value = (bfd_vma) i << 3;
9061 }
d62b3684 9062
df22d223
AM
9063 sym->st_value -= skip[i];
9064 symtab_hdr->contents = (unsigned char *) local_syms;
9065 }
c5614fa4 9066
854b41e7 9067 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
9068 if (toc_inf.global_toc_syms)
9069 {
9070 toc_inf.toc = toc;
9071 toc_inf.skip = skip;
9072 toc_inf.global_toc_syms = FALSE;
9073 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9074 &toc_inf);
9075 }
854b41e7
AM
9076
9077 if (toc->reloc_count != 0)
9078 {
d4730f92 9079 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
9080 Elf_Internal_Rela *wrel;
9081 bfd_size_type sz;
9082
854b41e7 9083 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
9084 if (toc_relocs == NULL)
9085 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9086 info->keep_memory);
9087 if (toc_relocs == NULL)
9088 goto error_ret;
9089
425b145b
AM
9090 wrel = toc_relocs;
9091 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
9092 if ((skip[rel->r_offset >> 3]
9093 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
9094 {
9095 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9096 wrel->r_info = rel->r_info;
9097 wrel->r_addend = rel->r_addend;
9098 ++wrel;
9099 }
9100 else if (!dec_dynrel_count (rel->r_info, toc, info,
9101 &local_syms, NULL, NULL))
9102 goto error_ret;
9103
425b145b
AM
9104 elf_section_data (toc)->relocs = toc_relocs;
9105 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
9106 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9107 sz = rel_hdr->sh_entsize;
9108 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 9109 }
c5614fa4 9110 }
28be611c
AM
9111 else if (toc_relocs != NULL
9112 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 9113 free (toc_relocs);
c5614fa4
AM
9114
9115 if (local_syms != NULL
9116 && symtab_hdr->contents != (unsigned char *) local_syms)
9117 {
9118 if (!info->keep_memory)
9119 free (local_syms);
9120 else
9121 symtab_hdr->contents = (unsigned char *) local_syms;
9122 }
9123 free (skip);
9124 }
9125
9126 return TRUE;
9127}
9128
1bbe0902
AM
9129/* Return true iff input section I references the TOC using
9130 instructions limited to +/-32k offsets. */
9131
9132bfd_boolean
9133ppc64_elf_has_small_toc_reloc (asection *i)
9134{
9135 return (is_ppc64_elf (i->owner)
9136 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9137}
9138
927be08e
AM
9139/* Allocate space for one GOT entry. */
9140
9141static void
9142allocate_got (struct elf_link_hash_entry *h,
9143 struct bfd_link_info *info,
9144 struct got_entry *gent)
9145{
9146 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9147 bfd_boolean dyn;
9148 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9149 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9150 ? 16 : 8);
9151 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9152 ? 2 : 1) * sizeof (Elf64_External_Rela);
9153 asection *got = ppc64_elf_tdata (gent->owner)->got;
9154
9155 gent->got.offset = got->size;
9156 got->size += entsize;
9157
9158 dyn = htab->elf.dynamic_sections_created;
19e08130 9159 if (h->type == STT_GNU_IFUNC)
927be08e 9160 {
19e08130
AM
9161 htab->reliplt->size += rentsize;
9162 htab->got_reli_size += rentsize;
927be08e 9163 }
19e08130
AM
9164 else if ((info->shared
9165 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9166 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9167 || h->root.type != bfd_link_hash_undefweak))
927be08e 9168 {
19e08130 9169 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
927be08e 9170 relgot->size += rentsize;
927be08e
AM
9171 }
9172}
9173
7865406b
AM
9174/* This function merges got entries in the same toc group. */
9175
9176static void
9177merge_got_entries (struct got_entry **pent)
9178{
9179 struct got_entry *ent, *ent2;
9180
9181 for (ent = *pent; ent != NULL; ent = ent->next)
9182 if (!ent->is_indirect)
9183 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9184 if (!ent2->is_indirect
9185 && ent2->addend == ent->addend
9186 && ent2->tls_type == ent->tls_type
9187 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9188 {
9189 ent2->is_indirect = TRUE;
9190 ent2->got.ent = ent;
9191 }
9192}
9193
65f38f15
AM
9194/* Allocate space in .plt, .got and associated reloc sections for
9195 dynamic relocs. */
5bd4f169 9196
b34976b6 9197static bfd_boolean
4ce794b7 9198allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 9199{
65f38f15
AM
9200 struct bfd_link_info *info;
9201 struct ppc_link_hash_table *htab;
5bd4f169 9202 asection *s;
65f38f15 9203 struct ppc_link_hash_entry *eh;
6061a67d 9204 struct elf_dyn_relocs *p;
0b8bcf0d 9205 struct got_entry **pgent, *gent;
5bd4f169 9206
e92d460e 9207 if (h->root.type == bfd_link_hash_indirect)
b34976b6 9208 return TRUE;
5bd4f169 9209
65f38f15
AM
9210 info = (struct bfd_link_info *) inf;
9211 htab = ppc_hash_table (info);
4dfe6ac6
NC
9212 if (htab == NULL)
9213 return FALSE;
5bd4f169 9214
e054468f
AM
9215 if ((htab->elf.dynamic_sections_created
9216 && h->dynindx != -1
9217 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9218 || h->type == STT_GNU_IFUNC)
5bd4f169 9219 {
411e1bfb
AM
9220 struct plt_entry *pent;
9221 bfd_boolean doneone = FALSE;
9222 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9223 if (pent->plt.refcount > 0)
9224 {
25f23106
AM
9225 if (!htab->elf.dynamic_sections_created
9226 || h->dynindx == -1)
e054468f
AM
9227 {
9228 s = htab->iplt;
9229 pent->plt.offset = s->size;
9230 s->size += PLT_ENTRY_SIZE;
9231 s = htab->reliplt;
9232 }
9233 else
9234 {
9235 /* If this is the first .plt entry, make room for the special
9236 first entry. */
9237 s = htab->plt;
9238 if (s->size == 0)
9239 s->size += PLT_INITIAL_ENTRY_SIZE;
9240
9241 pent->plt.offset = s->size;
9242
9243 /* Make room for this entry. */
9244 s->size += PLT_ENTRY_SIZE;
9245
9246 /* Make room for the .glink code. */
9247 s = htab->glink;
9248 if (s->size == 0)
9249 s->size += GLINK_CALL_STUB_SIZE;
9250 /* We need bigger stubs past index 32767. */
9251 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9252 s->size += 4;
9253 s->size += 2*4;
9254
9255 /* We also need to make an entry in the .rela.plt section. */
9256 s = htab->relplt;
9257 }
eea6121a 9258 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
9259 doneone = TRUE;
9260 }
9261 else
9262 pent->plt.offset = (bfd_vma) -1;
9263 if (!doneone)
65f38f15 9264 {
411e1bfb 9265 h->plt.plist = NULL;
f5385ebf 9266 h->needs_plt = 0;
65f38f15
AM
9267 }
9268 }
9269 else
9270 {
411e1bfb 9271 h->plt.plist = NULL;
f5385ebf 9272 h->needs_plt = 0;
65f38f15
AM
9273 }
9274
951fd09b
AM
9275 eh = (struct ppc_link_hash_entry *) h;
9276 /* Run through the TLS GD got entries first if we're changing them
9277 to TPREL. */
e7b938ca 9278 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
9279 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9280 if (gent->got.refcount > 0
9281 && (gent->tls_type & TLS_GD) != 0)
9282 {
9283 /* This was a GD entry that has been converted to TPREL. If
9284 there happens to be a TPREL entry we can use that one. */
9285 struct got_entry *ent;
9286 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9287 if (ent->got.refcount > 0
9288 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
9289 && ent->addend == gent->addend
9290 && ent->owner == gent->owner)
951fd09b
AM
9291 {
9292 gent->got.refcount = 0;
9293 break;
9294 }
9295
9296 /* If not, then we'll be using our own TPREL entry. */
9297 if (gent->got.refcount != 0)
9298 gent->tls_type = TLS_TLS | TLS_TPREL;
9299 }
9300
7865406b
AM
9301 /* Remove any list entry that won't generate a word in the GOT before
9302 we call merge_got_entries. Otherwise we risk merging to empty
9303 entries. */
0b8bcf0d
AM
9304 pgent = &h->got.glist;
9305 while ((gent = *pgent) != NULL)
411e1bfb 9306 if (gent->got.refcount > 0)
7865406b
AM
9307 {
9308 if ((gent->tls_type & TLS_LD) != 0
9309 && !h->def_dynamic)
9310 {
9311 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9312 *pgent = gent->next;
9313 }
9314 else
9315 pgent = &gent->next;
9316 }
9317 else
9318 *pgent = gent->next;
9319
9320 if (!htab->do_multi_toc)
9321 merge_got_entries (&h->got.glist);
9322
9323 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9324 if (!gent->is_indirect)
411e1bfb
AM
9325 {
9326 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
9327 Undefined weak syms won't yet be marked as dynamic,
9328 nor will all TLS symbols. */
411e1bfb 9329 if (h->dynindx == -1
b099ab9f 9330 && !h->forced_local
25f23106 9331 && h->type != STT_GNU_IFUNC
b099ab9f 9332 && htab->elf.dynamic_sections_created)
411e1bfb 9333 {
c152c796 9334 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
9335 return FALSE;
9336 }
65f38f15 9337
0c8d6e5c 9338 if (!is_ppc64_elf (gent->owner))
927be08e 9339 abort ();
0ffa91dd 9340
927be08e 9341 allocate_got (h, info, gent);
411e1bfb 9342 }
65f38f15 9343
b099ab9f 9344 if (eh->dyn_relocs == NULL
25f23106
AM
9345 || (!htab->elf.dynamic_sections_created
9346 && h->type != STT_GNU_IFUNC))
b34976b6 9347 return TRUE;
65f38f15
AM
9348
9349 /* In the shared -Bsymbolic case, discard space allocated for
9350 dynamic pc-relative relocs against symbols which turn out to be
9351 defined in regular objects. For the normal shared case, discard
9352 space for relocs that have become local due to symbol visibility
9353 changes. */
9354
9355 if (info->shared)
9356 {
9c7a29a3 9357 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 9358 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
9359 generated via assembly. We want calls to protected symbols to
9360 resolve directly to the function rather than going via the plt.
9361 If people want function pointer comparisons to work as expected
9362 then they should avoid writing weird assembly. */
09695f56 9363 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 9364 {
6061a67d 9365 struct elf_dyn_relocs **pp;
65f38f15
AM
9366
9367 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 9368 {
65f38f15
AM
9369 p->count -= p->pc_count;
9370 p->pc_count = 0;
9371 if (p->count == 0)
9372 *pp = p->next;
9373 else
9374 pp = &p->next;
5bd4f169 9375 }
65f38f15 9376 }
4e795f50
AM
9377
9378 /* Also discard relocs on undefined weak syms with non-default
9379 visibility. */
cab87ef9
AM
9380 if (eh->dyn_relocs != NULL
9381 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
9382 {
9383 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9384 eh->dyn_relocs = NULL;
9385
9386 /* Make sure this symbol is output as a dynamic symbol.
9387 Undefined weak syms won't yet be marked as dynamic. */
9388 else if (h->dynindx == -1
9389 && !h->forced_local)
9390 {
9391 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9392 return FALSE;
9393 }
9394 }
65f38f15 9395 }
25f23106
AM
9396 else if (h->type == STT_GNU_IFUNC)
9397 {
9398 if (!h->non_got_ref)
9399 eh->dyn_relocs = NULL;
9400 }
f4656909 9401 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
9402 {
9403 /* For the non-shared case, discard space for relocs against
9404 symbols which turn out to need copy relocs or are not
9405 dynamic. */
9406
f5385ebf 9407 if (!h->non_got_ref
f5385ebf 9408 && !h->def_regular)
65f38f15
AM
9409 {
9410 /* Make sure this symbol is output as a dynamic symbol.
9411 Undefined weak syms won't yet be marked as dynamic. */
9412 if (h->dynindx == -1
f5385ebf 9413 && !h->forced_local)
65f38f15 9414 {
c152c796 9415 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 9416 return FALSE;
65f38f15
AM
9417 }
9418
9419 /* If that succeeded, we know we'll be keeping all the
9420 relocs. */
9421 if (h->dynindx != -1)
9422 goto keep;
9423 }
9424
9425 eh->dyn_relocs = NULL;
9426
ec338859 9427 keep: ;
65f38f15
AM
9428 }
9429
9430 /* Finally, allocate space. */
9431 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9432 {
9433 asection *sreloc = elf_section_data (p->sec)->sreloc;
19e08130 9434 if (eh->elf.type == STT_GNU_IFUNC)
25f23106 9435 sreloc = htab->reliplt;
eea6121a 9436 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
9437 }
9438
b34976b6 9439 return TRUE;
65f38f15
AM
9440}
9441
9442/* Find any dynamic relocs that apply to read-only sections. */
9443
b34976b6 9444static bfd_boolean
4ce794b7 9445readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
9446{
9447 struct ppc_link_hash_entry *eh;
6061a67d 9448 struct elf_dyn_relocs *p;
65f38f15
AM
9449
9450 eh = (struct ppc_link_hash_entry *) h;
9451 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9452 {
9453 asection *s = p->sec->output_section;
9454
9455 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9456 {
4ce794b7 9457 struct bfd_link_info *info = inf;
65f38f15
AM
9458
9459 info->flags |= DF_TEXTREL;
9460
9461 /* Not an error, just cut short the traversal. */
b34976b6 9462 return FALSE;
65f38f15
AM
9463 }
9464 }
b34976b6 9465 return TRUE;
65f38f15
AM
9466}
9467
9468/* Set the sizes of the dynamic sections. */
9469
b34976b6 9470static bfd_boolean
ee67d69a 9471ppc64_elf_size_dynamic_sections (bfd *output_bfd,
4ce794b7 9472 struct bfd_link_info *info)
65f38f15
AM
9473{
9474 struct ppc_link_hash_table *htab;
9475 bfd *dynobj;
9476 asection *s;
b34976b6 9477 bfd_boolean relocs;
65f38f15 9478 bfd *ibfd;
7865406b 9479 struct got_entry *first_tlsld;
65f38f15
AM
9480
9481 htab = ppc_hash_table (info);
4dfe6ac6
NC
9482 if (htab == NULL)
9483 return FALSE;
9484
65f38f15
AM
9485 dynobj = htab->elf.dynobj;
9486 if (dynobj == NULL)
9487 abort ();
9488
9489 if (htab->elf.dynamic_sections_created)
9490 {
9491 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 9492 if (info->executable)
65f38f15 9493 {
3d4d4302 9494 s = bfd_get_linker_section (dynobj, ".interp");
65f38f15
AM
9495 if (s == NULL)
9496 abort ();
eea6121a 9497 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9498 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9499 }
9500 }
9501
9502 /* Set up .got offsets for local syms, and space for local dynamic
9503 relocs. */
9504 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9505 {
411e1bfb
AM
9506 struct got_entry **lgot_ents;
9507 struct got_entry **end_lgot_ents;
e054468f
AM
9508 struct plt_entry **local_plt;
9509 struct plt_entry **end_local_plt;
f961d9dd 9510 unsigned char *lgot_masks;
65f38f15
AM
9511 bfd_size_type locsymcount;
9512 Elf_Internal_Shdr *symtab_hdr;
65f38f15 9513
0c8d6e5c 9514 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9515 continue;
9516
9517 for (s = ibfd->sections; s != NULL; s = s->next)
9518 {
19e08130 9519 struct ppc_dyn_relocs *p;
65f38f15 9520
6edfbbad 9521 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9522 {
ec338859
AM
9523 if (!bfd_is_abs_section (p->sec)
9524 && bfd_is_abs_section (p->sec->output_section))
9525 {
9526 /* Input section has been discarded, either because
9527 it is a copy of a linkonce section or due to
9528 linker script /DISCARD/, so we'll be discarding
9529 the relocs too. */
9530 }
248866a8 9531 else if (p->count != 0)
ec338859 9532 {
19e08130
AM
9533 asection *srel = elf_section_data (p->sec)->sreloc;
9534 if (p->ifunc)
25f23106 9535 srel = htab->reliplt;
eea6121a 9536 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9537 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9538 info->flags |= DF_TEXTREL;
ec338859 9539 }
65f38f15
AM
9540 }
9541 }
9542
411e1bfb
AM
9543 lgot_ents = elf_local_got_ents (ibfd);
9544 if (!lgot_ents)
65f38f15
AM
9545 continue;
9546
0ffa91dd 9547 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9548 locsymcount = symtab_hdr->sh_info;
411e1bfb 9549 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9550 local_plt = (struct plt_entry **) end_lgot_ents;
9551 end_local_plt = local_plt + locsymcount;
f961d9dd 9552 lgot_masks = (unsigned char *) end_local_plt;
e717da7e 9553 s = ppc64_elf_tdata (ibfd)->got;
e7b938ca 9554 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9555 {
0b8bcf0d 9556 struct got_entry **pent, *ent;
411e1bfb 9557
0b8bcf0d
AM
9558 pent = lgot_ents;
9559 while ((ent = *pent) != NULL)
411e1bfb
AM
9560 if (ent->got.refcount > 0)
9561 {
e7b938ca 9562 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9563 {
927be08e 9564 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9565 *pent = ent->next;
411e1bfb
AM
9566 }
9567 else
9568 {
19e08130
AM
9569 unsigned int ent_size = 8;
9570 unsigned int rel_size = sizeof (Elf64_External_Rela);
9571
eea6121a 9572 ent->got.offset = s->size;
e7b938ca 9573 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
927be08e 9574 {
19e08130
AM
9575 ent_size *= 2;
9576 rel_size *= 2;
9577 }
9578 s->size += ent_size;
9579 if ((*lgot_masks & PLT_IFUNC) != 0)
9580 {
9581 htab->reliplt->size += rel_size;
9582 htab->got_reli_size += rel_size;
9583 }
9584 else if (info->shared)
9585 {
9586 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9587 srel->size += rel_size;
927be08e 9588 }
0b8bcf0d 9589 pent = &ent->next;
411e1bfb
AM
9590 }
9591 }
9592 else
0b8bcf0d 9593 *pent = ent->next;
65f38f15 9594 }
e054468f
AM
9595
9596 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9597 for (; local_plt < end_local_plt; ++local_plt)
9598 {
9599 struct plt_entry *ent;
9600
9601 for (ent = *local_plt; ent != NULL; ent = ent->next)
9602 if (ent->plt.refcount > 0)
9603 {
91d6fa6a 9604 s = htab->iplt;
e054468f
AM
9605 ent->plt.offset = s->size;
9606 s->size += PLT_ENTRY_SIZE;
9607
25f23106 9608 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9609 }
9610 else
9611 ent->plt.offset = (bfd_vma) -1;
9612 }
65f38f15
AM
9613 }
9614
9615 /* Allocate global sym .plt and .got entries, and space for global
9616 sym dynamic relocs. */
4ce794b7 9617 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 9618
7865406b 9619 first_tlsld = NULL;
102890f0
AM
9620 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9621 {
7865406b
AM
9622 struct got_entry *ent;
9623
0c8d6e5c 9624 if (!is_ppc64_elf (ibfd))
102890f0
AM
9625 continue;
9626
7865406b
AM
9627 ent = ppc64_tlsld_got (ibfd);
9628 if (ent->got.refcount > 0)
102890f0 9629 {
7865406b 9630 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9631 {
7865406b
AM
9632 ent->is_indirect = TRUE;
9633 ent->got.ent = first_tlsld;
9634 }
9635 else
9636 {
9637 if (first_tlsld == NULL)
9638 first_tlsld = ent;
9639 s = ppc64_elf_tdata (ibfd)->got;
9640 ent->got.offset = s->size;
9641 ent->owner = ibfd;
9642 s->size += 16;
9643 if (info->shared)
9644 {
9645 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9646 srel->size += sizeof (Elf64_External_Rela);
9647 }
102890f0
AM
9648 }
9649 }
9650 else
7865406b 9651 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9652 }
9653
65f38f15
AM
9654 /* We now have determined the sizes of the various dynamic sections.
9655 Allocate memory for them. */
b34976b6 9656 relocs = FALSE;
65f38f15
AM
9657 for (s = dynobj->sections; s != NULL; s = s->next)
9658 {
9659 if ((s->flags & SEC_LINKER_CREATED) == 0)
9660 continue;
9661
4ce794b7 9662 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9663 /* These haven't been allocated yet; don't strip. */
9664 continue;
e717da7e
AM
9665 else if (s == htab->got
9666 || s == htab->plt
e054468f 9667 || s == htab->iplt
c456f082
AM
9668 || s == htab->glink
9669 || s == htab->dynbss)
65f38f15
AM
9670 {
9671 /* Strip this section if we don't need it; see the
9672 comment below. */
5bd4f169 9673 }
58d180e8
AM
9674 else if (s == htab->glink_eh_frame)
9675 {
9676 if (!bfd_is_abs_section (s->output_section))
9677 /* Not sized yet. */
9678 continue;
9679 }
70cc837d 9680 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9681 {
c456f082 9682 if (s->size != 0)
5bd4f169 9683 {
4ce794b7 9684 if (s != htab->relplt)
b34976b6 9685 relocs = TRUE;
5bd4f169
AM
9686
9687 /* We use the reloc_count field as a counter if we need
9688 to copy relocs into the output file. */
9689 s->reloc_count = 0;
9690 }
9691 }
65f38f15 9692 else
5bd4f169
AM
9693 {
9694 /* It's not one of our sections, so don't allocate space. */
9695 continue;
9696 }
9697
eea6121a 9698 if (s->size == 0)
5bd4f169 9699 {
c456f082
AM
9700 /* If we don't need this section, strip it from the
9701 output file. This is mostly to handle .rela.bss and
9702 .rela.plt. We must create both sections in
9703 create_dynamic_sections, because they must be created
9704 before the linker maps input sections to output
9705 sections. The linker does that before
9706 adjust_dynamic_symbol is called, and it is that
9707 function which decides whether anything needs to go
9708 into these sections. */
8423293d 9709 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9710 continue;
9711 }
9712
c456f082 9713 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9714 continue;
9715
65f38f15
AM
9716 /* Allocate memory for the section contents. We use bfd_zalloc
9717 here in case unused entries are not reclaimed before the
9718 section's contents are written out. This should not happen,
411e1bfb
AM
9719 but this way if it does we get a R_PPC64_NONE reloc in .rela
9720 sections instead of garbage.
9721 We also rely on the section contents being zero when writing
9722 the GOT. */
eea6121a 9723 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9724 if (s->contents == NULL)
b34976b6 9725 return FALSE;
5bd4f169
AM
9726 }
9727
e717da7e
AM
9728 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9729 {
0c8d6e5c 9730 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9731 continue;
9732
e717da7e
AM
9733 s = ppc64_elf_tdata (ibfd)->got;
9734 if (s != NULL && s != htab->got)
9735 {
eea6121a 9736 if (s->size == 0)
8423293d 9737 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9738 else
9739 {
eea6121a 9740 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9741 if (s->contents == NULL)
9742 return FALSE;
9743 }
9744 }
9745 s = ppc64_elf_tdata (ibfd)->relgot;
9746 if (s != NULL)
9747 {
eea6121a 9748 if (s->size == 0)
8423293d 9749 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9750 else
9751 {
eea6121a 9752 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9753 if (s->contents == NULL)
9754 return FALSE;
9755 relocs = TRUE;
9756 s->reloc_count = 0;
9757 }
9758 }
9759 }
9760
e86ce104 9761 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9762 {
9763 /* Add some entries to the .dynamic section. We fill in the
9764 values later, in ppc64_elf_finish_dynamic_sections, but we
9765 must add the entries now so that we get the correct size for
9766 the .dynamic section. The DT_DEBUG entry is filled in by the
9767 dynamic linker and used by the debugger. */
dc810e39 9768#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9769 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9770
36af4a4e 9771 if (info->executable)
5bd4f169 9772 {
dc810e39 9773 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9774 return FALSE;
5bd4f169
AM
9775 }
9776
eea6121a 9777 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9778 {
dc810e39
AM
9779 if (!add_dynamic_entry (DT_PLTGOT, 0)
9780 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9781 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9782 || !add_dynamic_entry (DT_JMPREL, 0)
9783 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9784 return FALSE;
5bd4f169
AM
9785 }
9786
ee67d69a 9787 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
19397422
AM
9788 {
9789 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9790 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9791 return FALSE;
19397422
AM
9792 }
9793
a7f2871e
AM
9794 if (!htab->no_tls_get_addr_opt
9795 && htab->tls_get_addr_fd != NULL
9796 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9797 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9798 return FALSE;
9799
5bd4f169
AM
9800 if (relocs)
9801 {
dc810e39
AM
9802 if (!add_dynamic_entry (DT_RELA, 0)
9803 || !add_dynamic_entry (DT_RELASZ, 0)
9804 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9805 return FALSE;
5bd4f169 9806
65f38f15
AM
9807 /* If any dynamic relocs apply to a read-only section,
9808 then we need a DT_TEXTREL entry. */
248866a8 9809 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9810 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9811
65f38f15 9812 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9813 {
65f38f15 9814 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9815 return FALSE;
5bd4f169 9816 }
5bd4f169 9817 }
5bd4f169 9818 }
65f38f15 9819#undef add_dynamic_entry
5bd4f169 9820
b34976b6 9821 return TRUE;
5bd4f169
AM
9822}
9823
721956f4 9824/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9825
4ce794b7
AM
9826static inline enum ppc_stub_type
9827ppc_type_of_stub (asection *input_sec,
9828 const Elf_Internal_Rela *rel,
9829 struct ppc_link_hash_entry **hash,
e054468f 9830 struct plt_entry **plt_ent,
6911b7dc
AM
9831 bfd_vma destination,
9832 unsigned long local_off)
5bd4f169 9833{
721956f4
AM
9834 struct ppc_link_hash_entry *h = *hash;
9835 bfd_vma location;
9836 bfd_vma branch_offset;
9837 bfd_vma max_branch_offset;
4ce794b7 9838 enum elf_ppc64_reloc_type r_type;
5bd4f169 9839
721956f4
AM
9840 if (h != NULL)
9841 {
e054468f 9842 struct plt_entry *ent;
7fe2b9a6 9843 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9844 if (h->oh != NULL
9845 && h->oh->is_func_descriptor)
7b8f6675
AM
9846 {
9847 fdh = ppc_follow_link (h->oh);
9848 *hash = fdh;
9849 }
8387904d 9850
e054468f
AM
9851 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9852 if (ent->addend == rel->r_addend
9853 && ent->plt.offset != (bfd_vma) -1)
9854 {
e054468f
AM
9855 *plt_ent = ent;
9856 return ppc_stub_plt_call;
9857 }
5bd4f169 9858
7fe2b9a6
AM
9859 /* Here, we know we don't have a plt entry. If we don't have a
9860 either a defined function descriptor or a defined entry symbol
9861 in a regular object file, then it is pointless trying to make
9862 any other type of stub. */
854b41e7
AM
9863 if (!is_static_defined (&fdh->elf)
9864 && !is_static_defined (&h->elf))
721956f4 9865 return ppc_stub_none;
5d1634d7 9866 }
e054468f
AM
9867 else if (elf_local_got_ents (input_sec->owner) != NULL)
9868 {
9869 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9870 struct plt_entry **local_plt = (struct plt_entry **)
9871 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9872 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9873
9874 if (local_plt[r_symndx] != NULL)
9875 {
9876 struct plt_entry *ent;
9877
9878 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9879 if (ent->addend == rel->r_addend
9880 && ent->plt.offset != (bfd_vma) -1)
9881 {
9882 *plt_ent = ent;
9883 return ppc_stub_plt_call;
9884 }
9885 }
9886 }
5d1634d7 9887
721956f4
AM
9888 /* Determine where the call point is. */
9889 location = (input_sec->output_offset
9890 + input_sec->output_section->vma
9891 + rel->r_offset);
5d1634d7 9892
721956f4
AM
9893 branch_offset = destination - location;
9894 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9895
721956f4
AM
9896 /* Determine if a long branch stub is needed. */
9897 max_branch_offset = 1 << 25;
4ce794b7 9898 if (r_type != R_PPC64_REL24)
721956f4 9899 max_branch_offset = 1 << 15;
5d1634d7 9900
6911b7dc 9901 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
721956f4
AM
9902 /* We need a stub. Figure out whether a long_branch or plt_branch
9903 is needed later. */
9904 return ppc_stub_long_branch;
5d1634d7 9905
721956f4 9906 return ppc_stub_none;
5d1634d7
AM
9907}
9908
794e51c0
AM
9909/* With power7 weakly ordered memory model, it is possible for ld.so
9910 to update a plt entry in one thread and have another thread see a
9911 stale zero toc entry. To avoid this we need some sort of acquire
9912 barrier in the call stub. One solution is to make the load of the
9913 toc word seem to appear to depend on the load of the function entry
9914 word. Another solution is to test for r2 being zero, and branch to
9915 the appropriate glink entry if so.
9916
9917 . fake dep barrier compare
71a39c98
AM
9918 . ld 12,xxx(2) ld 12,xxx(2)
9919 . mtctr 12 mtctr 12
9920 . xor 11,12,12 ld 2,xxx+8(2)
794e51c0
AM
9921 . add 2,2,11 cmpldi 2,0
9922 . ld 2,xxx+8(2) bnectr+
9923 . bctr b <glink_entry>
9924
9925 The solution involving the compare turns out to be faster, so
9926 that's what we use unless the branch won't reach. */
9927
9928#define ALWAYS_USE_FAKE_DEP 0
9929#define ALWAYS_EMIT_R2SAVE 0
5d1634d7 9930
5d1634d7
AM
9931#define PPC_LO(v) ((v) & 0xffff)
9932#define PPC_HI(v) (((v) >> 16) & 0xffff)
9933#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9934
794e51c0
AM
9935static inline unsigned int
9936plt_stub_size (struct ppc_link_hash_table *htab,
9937 struct ppc_stub_hash_entry *stub_entry,
9938 bfd_vma off)
9939{
9940 unsigned size = PLT_CALL_STUB_SIZE;
9941
9942 if (!(ALWAYS_EMIT_R2SAVE
9943 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
9944 size -= 4;
9945 if (!htab->plt_static_chain)
9946 size -= 4;
9947 if (htab->plt_thread_safe)
9948 size += 8;
9949 if (PPC_HA (off) == 0)
9950 size -= 4;
9951 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9952 size += 4;
9953 if (stub_entry->h != NULL
9954 && (stub_entry->h == htab->tls_get_addr_fd
9955 || stub_entry->h == htab->tls_get_addr)
9956 && !htab->no_tls_get_addr_opt)
9957 size += 13 * 4;
9958 return size;
9959}
9960
9961/* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9962 then return the padding needed to do so. */
9963static inline unsigned int
9964plt_stub_pad (struct ppc_link_hash_table *htab,
9965 struct ppc_stub_hash_entry *stub_entry,
9966 bfd_vma plt_off)
9967{
9968 int stub_align = 1 << htab->plt_stub_align;
9969 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9970 bfd_vma stub_off = stub_entry->stub_sec->size;
9971
9972 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9973 > (stub_size & -stub_align))
9974 return stub_align - (stub_off & (stub_align - 1));
9975 return 0;
9976}
9977
9978/* Build a .plt call stub. */
9979
9980static inline bfd_byte *
9981build_plt_stub (struct ppc_link_hash_table *htab,
9982 struct ppc_stub_hash_entry *stub_entry,
9983 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9984{
9985 bfd *obfd = htab->stub_bfd;
9986 bfd_boolean plt_static_chain = htab->plt_static_chain;
9987 bfd_boolean plt_thread_safe = htab->plt_thread_safe;
9988 bfd_boolean use_fake_dep = plt_thread_safe;
9989 bfd_vma cmp_branch_off = 0;
9990
9991 if (!ALWAYS_USE_FAKE_DEP
9992 && plt_thread_safe
9993 && !(stub_entry->h != NULL
9994 && (stub_entry->h == htab->tls_get_addr_fd
9995 || stub_entry->h == htab->tls_get_addr)
9996 && !htab->no_tls_get_addr_opt))
9997 {
9998 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9999 bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
10000 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10001 bfd_vma to, from;
10002
68d62958
AM
10003 if (pltindex > 32768)
10004 glinkoff += (pltindex - 32768) * 4;
794e51c0
AM
10005 to = (glinkoff
10006 + htab->glink->output_offset
10007 + htab->glink->output_section->vma);
10008 from = (p - stub_entry->stub_sec->contents
10009 + 4 * (ALWAYS_EMIT_R2SAVE
10010 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10011 + 4 * (PPC_HA (offset) != 0)
10012 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10013 != PPC_HA (offset))
10014 + 4 * (plt_static_chain != 0)
10015 + 20
10016 + stub_entry->stub_sec->output_offset
10017 + stub_entry->stub_sec->output_section->vma);
10018 cmp_branch_off = to - from;
10019 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10020 }
10021
ac2df442
AM
10022 if (PPC_HA (offset) != 0)
10023 {
176a0d42
AM
10024 if (r != NULL)
10025 {
794e51c0
AM
10026 if (ALWAYS_EMIT_R2SAVE
10027 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10028 r[0].r_offset += 4;
176a0d42 10029 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 10030 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
10031 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10032 r[1].r_addend = r[0].r_addend;
c7131b65 10033 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
10034 {
10035 r[2].r_offset = r[1].r_offset + 4;
10036 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10037 r[2].r_addend = r[0].r_addend;
10038 }
10039 else
10040 {
794e51c0 10041 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
176a0d42
AM
10042 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10043 r[2].r_addend = r[0].r_addend + 8;
c7131b65
AM
10044 if (plt_static_chain)
10045 {
10046 r[3].r_offset = r[2].r_offset + 4;
10047 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10048 r[3].r_addend = r[0].r_addend + 16;
10049 }
176a0d42
AM
10050 }
10051 }
794e51c0
AM
10052 if (ALWAYS_EMIT_R2SAVE
10053 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10054 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
71a39c98
AM
10055 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10056 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
9df0ef5f 10057 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442 10058 {
71a39c98 10059 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
ac2df442
AM
10060 offset = 0;
10061 }
71a39c98 10062 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
794e51c0
AM
10063 if (use_fake_dep)
10064 {
71a39c98
AM
10065 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10066 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
794e51c0 10067 }
71a39c98 10068 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
9df0ef5f 10069 if (plt_static_chain)
71a39c98 10070 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
10071 }
10072 else
10073 {
176a0d42
AM
10074 if (r != NULL)
10075 {
794e51c0
AM
10076 if (ALWAYS_EMIT_R2SAVE
10077 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10078 r[0].r_offset += 4;
176a0d42 10079 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65 10080 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
10081 {
10082 r[1].r_offset = r[0].r_offset + 4;
10083 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10084 r[1].r_addend = r[0].r_addend;
10085 }
10086 else
10087 {
794e51c0 10088 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
176a0d42 10089 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65
AM
10090 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10091 if (plt_static_chain)
10092 {
10093 r[2].r_offset = r[1].r_offset + 4;
10094 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10095 r[2].r_addend = r[0].r_addend + 8;
10096 }
176a0d42
AM
10097 }
10098 }
794e51c0
AM
10099 if (ALWAYS_EMIT_R2SAVE
10100 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10101 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
71a39c98 10102 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
9df0ef5f 10103 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
10104 {
10105 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10106 offset = 0;
10107 }
71a39c98 10108 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
794e51c0
AM
10109 if (use_fake_dep)
10110 {
71a39c98 10111 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
794e51c0
AM
10112 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10113 }
9df0ef5f
AM
10114 if (plt_static_chain)
10115 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
ac2df442 10116 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
ac2df442 10117 }
794e51c0
AM
10118 if (plt_thread_safe && !use_fake_dep)
10119 {
10120 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10121 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
22aa0c7e 10122 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
794e51c0
AM
10123 }
10124 else
10125 bfd_put_32 (obfd, BCTR, p), p += 4;
5d1634d7
AM
10126 return p;
10127}
10128
a7f2871e
AM
10129/* Build a special .plt call stub for __tls_get_addr. */
10130
10131#define LD_R11_0R3 0xe9630000
10132#define LD_R12_0R3 0xe9830000
10133#define MR_R0_R3 0x7c601b78
10134#define CMPDI_R11_0 0x2c2b0000
10135#define ADD_R3_R12_R13 0x7c6c6a14
10136#define BEQLR 0x4d820020
10137#define MR_R3_R0 0x7c030378
10138#define MFLR_R11 0x7d6802a6
10139#define STD_R11_0R1 0xf9610000
10140#define BCTRL 0x4e800421
10141#define LD_R11_0R1 0xe9610000
10142#define LD_R2_0R1 0xe8410000
10143#define MTLR_R11 0x7d6803a6
10144
10145static inline bfd_byte *
794e51c0
AM
10146build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10147 struct ppc_stub_hash_entry *stub_entry,
10148 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
a7f2871e 10149{
794e51c0
AM
10150 bfd *obfd = htab->stub_bfd;
10151
a7f2871e
AM
10152 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10153 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10154 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10155 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10156 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10157 bfd_put_32 (obfd, BEQLR, p), p += 4;
10158 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10159 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10160 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
10161
10162 if (r != NULL)
10163 r[0].r_offset += 9 * 4;
794e51c0 10164 p = build_plt_stub (htab, stub_entry, p, offset, r);
a7f2871e
AM
10165 bfd_put_32 (obfd, BCTRL, p - 4);
10166
10167 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
10168 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
10169 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10170 bfd_put_32 (obfd, BLR, p), p += 4;
10171
10172 return p;
10173}
10174
176a0d42
AM
10175static Elf_Internal_Rela *
10176get_relocs (asection *sec, int count)
10177{
10178 Elf_Internal_Rela *relocs;
10179 struct bfd_elf_section_data *elfsec_data;
10180
10181 elfsec_data = elf_section_data (sec);
10182 relocs = elfsec_data->relocs;
10183 if (relocs == NULL)
10184 {
10185 bfd_size_type relsize;
10186 relsize = sec->reloc_count * sizeof (*relocs);
10187 relocs = bfd_alloc (sec->owner, relsize);
10188 if (relocs == NULL)
10189 return NULL;
10190 elfsec_data->relocs = relocs;
d4730f92
BS
10191 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10192 sizeof (Elf_Internal_Shdr));
10193 if (elfsec_data->rela.hdr == NULL)
10194 return NULL;
10195 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10196 * sizeof (Elf64_External_Rela));
10197 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
10198 sec->reloc_count = 0;
10199 }
10200 relocs += sec->reloc_count;
10201 sec->reloc_count += count;
10202 return relocs;
10203}
10204
aa374f67 10205static bfd_vma
25f53a85 10206get_r2off (struct bfd_link_info *info,
aa374f67
AM
10207 struct ppc_stub_hash_entry *stub_entry)
10208{
25f53a85 10209 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
10210 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10211
10212 if (r2off == 0)
10213 {
10214 /* Support linking -R objects. Get the toc pointer from the
10215 opd entry. */
10216 char buf[8];
10217 asection *opd = stub_entry->h->elf.root.u.def.section;
10218 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10219
10220 if (strcmp (opd->name, ".opd") != 0
10221 || opd->reloc_count != 0)
10222 {
bc30df16 10223 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
25f53a85 10224 stub_entry->h->elf.root.root.string);
aa374f67
AM
10225 bfd_set_error (bfd_error_bad_value);
10226 return 0;
10227 }
10228 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10229 return 0;
10230 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 10231 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
10232 }
10233 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10234 return r2off;
10235}
10236
b34976b6 10237static bfd_boolean
4ce794b7 10238ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 10239{
721956f4
AM
10240 struct ppc_stub_hash_entry *stub_entry;
10241 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
10242 struct bfd_link_info *info;
10243 struct ppc_link_hash_table *htab;
721956f4
AM
10244 bfd_byte *loc;
10245 bfd_byte *p;
ee75fd95 10246 bfd_vma dest, off;
721956f4 10247 int size;
176a0d42 10248 Elf_Internal_Rela *r;
e054468f 10249 asection *plt;
5d1634d7 10250
721956f4
AM
10251 /* Massage our args to the form they really have. */
10252 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 10253 info = in_arg;
5d1634d7 10254
5d1634d7 10255 htab = ppc_hash_table (info);
4dfe6ac6
NC
10256 if (htab == NULL)
10257 return FALSE;
5d1634d7 10258
721956f4 10259 /* Make a note of the offset within the stubs for this entry. */
eea6121a 10260 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 10261 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 10262
4ce794b7 10263 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 10264 switch (stub_entry->stub_type)
5d1634d7 10265 {
721956f4 10266 case ppc_stub_long_branch:
ad8e1ba5 10267 case ppc_stub_long_branch_r2off:
721956f4 10268 /* Branches are relative. This is where we are going to. */
6911b7dc
AM
10269 dest = (stub_entry->target_value
10270 + stub_entry->target_section->output_offset
10271 + stub_entry->target_section->output_section->vma);
10272 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10273 off = dest;
5d1634d7 10274
721956f4
AM
10275 /* And this is where we are coming from. */
10276 off -= (stub_entry->stub_offset
97b639ba
AM
10277 + stub_entry->stub_sec->output_offset
10278 + stub_entry->stub_sec->output_section->vma);
e86ce104 10279
ac2df442
AM
10280 size = 4;
10281 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 10282 {
25f53a85 10283 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 10284
aa374f67
AM
10285 if (r2off == 0)
10286 {
10287 htab->stub_error = TRUE;
10288 return FALSE;
10289 }
97b639ba 10290 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 10291 loc += 4;
ac2df442
AM
10292 size = 12;
10293 if (PPC_HA (r2off) != 0)
10294 {
10295 size = 16;
10296 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10297 loc += 4;
10298 }
97b639ba 10299 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 10300 loc += 4;
ac2df442 10301 off -= size - 4;
ad8e1ba5 10302 }
97b639ba 10303 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 10304
5c3dead3
AM
10305 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10306 {
bc30df16
AM
10307 info->callbacks->einfo
10308 (_("%P: long branch stub `%s' offset overflow\n"),
10309 stub_entry->root.string);
5c3dead3
AM
10310 htab->stub_error = TRUE;
10311 return FALSE;
10312 }
ee75fd95
AM
10313
10314 if (info->emitrelocations)
10315 {
176a0d42
AM
10316 r = get_relocs (stub_entry->stub_sec, 1);
10317 if (r == NULL)
10318 return FALSE;
ee75fd95
AM
10319 r->r_offset = loc - stub_entry->stub_sec->contents;
10320 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10321 r->r_addend = dest;
10322 if (stub_entry->h != NULL)
10323 {
10324 struct elf_link_hash_entry **hashes;
10325 unsigned long symndx;
10326 struct ppc_link_hash_entry *h;
10327
10328 hashes = elf_sym_hashes (htab->stub_bfd);
10329 if (hashes == NULL)
10330 {
10331 bfd_size_type hsize;
10332
10333 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10334 hashes = bfd_zalloc (htab->stub_bfd, hsize);
10335 if (hashes == NULL)
10336 return FALSE;
10337 elf_sym_hashes (htab->stub_bfd) = hashes;
10338 htab->stub_globals = 1;
10339 }
10340 symndx = htab->stub_globals++;
10341 h = stub_entry->h;
10342 hashes[symndx] = &h->elf;
10343 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10344 if (h->oh != NULL && h->oh->is_func)
b31867b6 10345 h = ppc_follow_link (h->oh);
ee75fd95
AM
10346 if (h->elf.root.u.def.section != stub_entry->target_section)
10347 /* H is an opd symbol. The addend must be zero. */
10348 r->r_addend = 0;
10349 else
10350 {
10351 off = (h->elf.root.u.def.value
10352 + h->elf.root.u.def.section->output_offset
10353 + h->elf.root.u.def.section->output_section->vma);
10354 r->r_addend -= off;
10355 }
10356 }
10357 }
721956f4 10358 break;
e86ce104 10359
721956f4 10360 case ppc_stub_plt_branch:
ad8e1ba5 10361 case ppc_stub_plt_branch_r2off:
721956f4
AM
10362 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10363 stub_entry->root.string + 9,
b34976b6 10364 FALSE, FALSE);
721956f4
AM
10365 if (br_entry == NULL)
10366 {
8de848d8 10367 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 10368 stub_entry->root.string);
b34976b6
AM
10369 htab->stub_error = TRUE;
10370 return FALSE;
721956f4
AM
10371 }
10372
176a0d42
AM
10373 dest = (stub_entry->target_value
10374 + stub_entry->target_section->output_offset
10375 + stub_entry->target_section->output_section->vma);
6911b7dc
AM
10376 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10377 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
721956f4 10378
176a0d42 10379 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 10380 htab->brlt->contents + br_entry->offset);
721956f4 10381
f94498ff 10382 if (br_entry->iter == htab->stub_iteration)
721956f4 10383 {
f94498ff 10384 br_entry->iter = 0;
84f5d08e 10385
f94498ff 10386 if (htab->relbrlt != NULL)
84f5d08e 10387 {
f94498ff
AM
10388 /* Create a reloc for the branch lookup table entry. */
10389 Elf_Internal_Rela rela;
10390 bfd_byte *rl;
10391
10392 rela.r_offset = (br_entry->offset
10393 + htab->brlt->output_offset
10394 + htab->brlt->output_section->vma);
10395 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10396 rela.r_addend = dest;
f94498ff
AM
10397
10398 rl = htab->relbrlt->contents;
10399 rl += (htab->relbrlt->reloc_count++
10400 * sizeof (Elf64_External_Rela));
10401 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10402 }
10403 else if (info->emitrelocations)
10404 {
176a0d42
AM
10405 r = get_relocs (htab->brlt, 1);
10406 if (r == NULL)
10407 return FALSE;
10408 /* brlt, being SEC_LINKER_CREATED does not go through the
10409 normal reloc processing. Symbols and offsets are not
10410 translated from input file to output file form, so
10411 set up the offset per the output file. */
f94498ff
AM
10412 r->r_offset = (br_entry->offset
10413 + htab->brlt->output_offset
10414 + htab->brlt->output_section->vma);
10415 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 10416 r->r_addend = dest;
84f5d08e 10417 }
84f5d08e 10418 }
721956f4 10419
176a0d42
AM
10420 dest = (br_entry->offset
10421 + htab->brlt->output_offset
10422 + htab->brlt->output_section->vma);
10423
10424 off = (dest
4ce794b7 10425 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 10426 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10427
ad8e1ba5 10428 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 10429 {
25f53a85 10430 info->callbacks->einfo
bc30df16 10431 (_("%P: linkage table error against `%T'\n"),
721956f4 10432 stub_entry->root.string);
5d1634d7 10433 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10434 htab->stub_error = TRUE;
10435 return FALSE;
5d1634d7 10436 }
41bd81ab 10437
176a0d42
AM
10438 if (info->emitrelocations)
10439 {
10440 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10441 if (r == NULL)
10442 return FALSE;
10443 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10444 if (bfd_big_endian (info->output_bfd))
10445 r[0].r_offset += 2;
176a0d42
AM
10446 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10447 r[0].r_offset += 4;
10448 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10449 r[0].r_addend = dest;
10450 if (PPC_HA (off) != 0)
10451 {
10452 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10453 r[1].r_offset = r[0].r_offset + 4;
10454 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10455 r[1].r_addend = r[0].r_addend;
10456 }
10457 }
10458
ad8e1ba5
AM
10459 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10460 {
176a0d42 10461 if (PPC_HA (off) != 0)
ac2df442
AM
10462 {
10463 size = 16;
71a39c98 10464 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
ac2df442 10465 loc += 4;
71a39c98 10466 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
ac2df442
AM
10467 }
10468 else
10469 {
10470 size = 12;
71a39c98 10471 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442 10472 }
ad8e1ba5
AM
10473 }
10474 else
10475 {
25f53a85 10476 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
10477
10478 if (r2off == 0)
10479 {
10480 htab->stub_error = TRUE;
10481 return FALSE;
10482 }
ad8e1ba5 10483
97b639ba 10484 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 10485 loc += 4;
ac2df442 10486 size = 20;
176a0d42 10487 if (PPC_HA (off) != 0)
ac2df442
AM
10488 {
10489 size += 4;
71a39c98 10490 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
ac2df442 10491 loc += 4;
71a39c98 10492 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
ac2df442
AM
10493 loc += 4;
10494 }
10495 else
10496 {
71a39c98 10497 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
ac2df442
AM
10498 loc += 4;
10499 }
10500
10501 if (PPC_HA (r2off) != 0)
10502 {
10503 size += 4;
10504 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10505 loc += 4;
10506 }
97b639ba 10507 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
10508 }
10509 loc += 4;
71a39c98 10510 bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
ad8e1ba5 10511 loc += 4;
97b639ba 10512 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 10513 break;
5d1634d7 10514
721956f4 10515 case ppc_stub_plt_call:
794e51c0 10516 case ppc_stub_plt_call_r2save:
e054468f 10517 if (stub_entry->h != NULL
b31867b6
AM
10518 && stub_entry->h->is_func_descriptor
10519 && stub_entry->h->oh != NULL)
c862ae31 10520 {
b31867b6
AM
10521 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10522
10523 /* If the old-ABI "dot-symbol" is undefined make it weak so
10524 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10525 FIXME: We used to define the symbol on one of the call
10526 stubs instead, which is why we test symbol section id
10527 against htab->top_id in various places. Likely all
10528 these checks could now disappear. */
10529 if (fh->elf.root.type == bfd_link_hash_undefined)
10530 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
10531 /* Stop undo_symbol_twiddle changing it back to undefined. */
10532 fh->was_undefined = 0;
c862ae31
AM
10533 }
10534
721956f4 10535 /* Now build the stub. */
e054468f 10536 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 10537 if (dest >= (bfd_vma) -2)
721956f4
AM
10538 abort ();
10539
e054468f 10540 plt = htab->plt;
25f23106
AM
10541 if (!htab->elf.dynamic_sections_created
10542 || stub_entry->h == NULL
10543 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10544 plt = htab->iplt;
10545
10546 dest += plt->output_offset + plt->output_section->vma;
10547
10548 if (stub_entry->h == NULL
10549 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10550 {
10551 Elf_Internal_Rela rela;
10552 bfd_byte *rl;
10553
10554 rela.r_offset = dest;
ee67d69a
AM
10555 if (htab->opd_abi)
10556 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10557 else
10558 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
10559 rela.r_addend = (stub_entry->target_value
10560 + stub_entry->target_section->output_offset
10561 + stub_entry->target_section->output_section->vma);
10562
25f23106
AM
10563 rl = (htab->reliplt->contents
10564 + (htab->reliplt->reloc_count++
10565 * sizeof (Elf64_External_Rela)));
10566 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
10567 stub_entry->plt_ent->plt.offset |= 1;
10568 }
176a0d42
AM
10569
10570 off = (dest
e054468f 10571 - elf_gp (plt->output_section->owner)
176a0d42 10572 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10573
ad8e1ba5 10574 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 10575 {
25f53a85 10576 info->callbacks->einfo
bc30df16 10577 (_("%P: linkage table error against `%T'\n"),
e054468f
AM
10578 stub_entry->h != NULL
10579 ? stub_entry->h->elf.root.root.string
10580 : "<local sym>");
721956f4 10581 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
10582 htab->stub_error = TRUE;
10583 return FALSE;
721956f4
AM
10584 }
10585
794e51c0
AM
10586 if (htab->plt_stub_align != 0)
10587 {
10588 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10589
10590 stub_entry->stub_sec->size += pad;
10591 stub_entry->stub_offset = stub_entry->stub_sec->size;
10592 loc += pad;
10593 }
10594
176a0d42
AM
10595 r = NULL;
10596 if (info->emitrelocations)
10597 {
10598 r = get_relocs (stub_entry->stub_sec,
c7131b65
AM
10599 (2
10600 + (PPC_HA (off) != 0)
10601 + (htab->plt_static_chain
10602 && PPC_HA (off + 16) == PPC_HA (off))));
176a0d42
AM
10603 if (r == NULL)
10604 return FALSE;
10605 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
10606 if (bfd_big_endian (info->output_bfd))
10607 r[0].r_offset += 2;
176a0d42
AM
10608 r[0].r_addend = dest;
10609 }
a7f2871e
AM
10610 if (stub_entry->h != NULL
10611 && (stub_entry->h == htab->tls_get_addr_fd
10612 || stub_entry->h == htab->tls_get_addr)
10613 && !htab->no_tls_get_addr_opt)
794e51c0 10614 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
a7f2871e 10615 else
794e51c0 10616 p = build_plt_stub (htab, stub_entry, loc, off, r);
721956f4
AM
10617 size = p - loc;
10618 break;
10619
10620 default:
10621 BFD_FAIL ();
b34976b6 10622 return FALSE;
721956f4
AM
10623 }
10624
eea6121a 10625 stub_entry->stub_sec->size += size;
97b639ba 10626
ee75fd95 10627 if (htab->emit_stub_syms)
97b639ba
AM
10628 {
10629 struct elf_link_hash_entry *h;
ee75fd95
AM
10630 size_t len1, len2;
10631 char *name;
10632 const char *const stub_str[] = { "long_branch",
10633 "long_branch_r2off",
10634 "plt_branch",
10635 "plt_branch_r2off",
794e51c0 10636 "plt_call",
ee75fd95
AM
10637 "plt_call" };
10638
10639 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10640 len2 = strlen (stub_entry->root.string);
10641 name = bfd_malloc (len1 + len2 + 2);
10642 if (name == NULL)
10643 return FALSE;
10644 memcpy (name, stub_entry->root.string, 9);
10645 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10646 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10647 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10648 if (h == NULL)
10649 return FALSE;
10650 if (h->root.type == bfd_link_hash_new)
10651 {
10652 h->root.type = bfd_link_hash_defined;
10653 h->root.u.def.section = stub_entry->stub_sec;
10654 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10655 h->ref_regular = 1;
10656 h->def_regular = 1;
10657 h->ref_regular_nonweak = 1;
10658 h->forced_local = 1;
10659 h->non_elf = 0;
97b639ba
AM
10660 }
10661 }
10662
b34976b6 10663 return TRUE;
721956f4
AM
10664}
10665
10666/* As above, but don't actually build the stub. Just bump offset so
10667 we know stub section sizes, and select plt_branch stubs where
10668 long_branch stubs won't do. */
10669
b34976b6 10670static bfd_boolean
4ce794b7 10671ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10672{
10673 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10674 struct bfd_link_info *info;
721956f4
AM
10675 struct ppc_link_hash_table *htab;
10676 bfd_vma off;
10677 int size;
10678
10679 /* Massage our args to the form they really have. */
10680 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10681 info = in_arg;
10682
10683 htab = ppc_hash_table (info);
4dfe6ac6
NC
10684 if (htab == NULL)
10685 return FALSE;
721956f4 10686
794e51c0
AM
10687 if (stub_entry->stub_type == ppc_stub_plt_call
10688 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
721956f4 10689 {
e054468f
AM
10690 asection *plt;
10691 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 10692 if (off >= (bfd_vma) -2)
411e1bfb 10693 abort ();
e054468f 10694 plt = htab->plt;
25f23106
AM
10695 if (!htab->elf.dynamic_sections_created
10696 || stub_entry->h == NULL
10697 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10698 plt = htab->iplt;
10699 off += (plt->output_offset
10700 + plt->output_section->vma
10701 - elf_gp (plt->output_section->owner)
ad8e1ba5 10702 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10703
794e51c0
AM
10704 size = plt_stub_size (htab, stub_entry, off);
10705 if (htab->plt_stub_align)
10706 size += plt_stub_pad (htab, stub_entry, off);
176a0d42
AM
10707 if (info->emitrelocations)
10708 {
10709 stub_entry->stub_sec->reloc_count
c7131b65
AM
10710 += (2
10711 + (PPC_HA (off) != 0)
10712 + (htab->plt_static_chain
10713 && PPC_HA (off + 16) == PPC_HA (off)));
176a0d42
AM
10714 stub_entry->stub_sec->flags |= SEC_RELOC;
10715 }
721956f4
AM
10716 }
10717 else
10718 {
ad8e1ba5
AM
10719 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10720 variants. */
ac2df442 10721 bfd_vma r2off = 0;
6911b7dc 10722 bfd_vma local_off = 0;
ac2df442 10723
721956f4
AM
10724 off = (stub_entry->target_value
10725 + stub_entry->target_section->output_offset
10726 + stub_entry->target_section->output_section->vma);
eea6121a 10727 off -= (stub_entry->stub_sec->size
721956f4
AM
10728 + stub_entry->stub_sec->output_offset
10729 + stub_entry->stub_sec->output_section->vma);
10730
ad8e1ba5
AM
10731 /* Reset the stub type from the plt variant in case we now
10732 can reach with a shorter stub. */
10733 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10734 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10735
10736 size = 4;
10737 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10738 {
25f53a85 10739 r2off = get_r2off (info, stub_entry);
aa374f67
AM
10740 if (r2off == 0)
10741 {
10742 htab->stub_error = TRUE;
10743 return FALSE;
10744 }
ac2df442
AM
10745 size = 12;
10746 if (PPC_HA (r2off) != 0)
10747 size = 16;
10748 off -= size - 4;
ad8e1ba5
AM
10749 }
10750
6911b7dc
AM
10751 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10752
ad8e1ba5 10753 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
6911b7dc 10754 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
721956f4
AM
10755 {
10756 struct ppc_branch_hash_entry *br_entry;
10757
10758 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10759 stub_entry->root.string + 9,
b34976b6 10760 TRUE, FALSE);
721956f4
AM
10761 if (br_entry == NULL)
10762 {
8de848d8 10763 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 10764 stub_entry->root.string);
b34976b6
AM
10765 htab->stub_error = TRUE;
10766 return FALSE;
721956f4
AM
10767 }
10768
10769 if (br_entry->iter != htab->stub_iteration)
10770 {
10771 br_entry->iter = htab->stub_iteration;
eea6121a
AM
10772 br_entry->offset = htab->brlt->size;
10773 htab->brlt->size += 8;
63bc6f6c 10774
ee75fd95 10775 if (htab->relbrlt != NULL)
eea6121a 10776 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
10777 else if (info->emitrelocations)
10778 {
10779 htab->brlt->reloc_count += 1;
10780 htab->brlt->flags |= SEC_RELOC;
10781 }
721956f4 10782 }
ad8e1ba5
AM
10783
10784 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
10785 off = (br_entry->offset
10786 + htab->brlt->output_offset
10787 + htab->brlt->output_section->vma
10788 - elf_gp (htab->brlt->output_section->owner)
10789 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10790
176a0d42
AM
10791 if (info->emitrelocations)
10792 {
10793 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10794 stub_entry->stub_sec->flags |= SEC_RELOC;
10795 }
10796
ac2df442
AM
10797 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10798 {
10799 size = 12;
176a0d42 10800 if (PPC_HA (off) != 0)
ac2df442
AM
10801 size = 16;
10802 }
10803 else
10804 {
10805 size = 20;
176a0d42 10806 if (PPC_HA (off) != 0)
ac2df442
AM
10807 size += 4;
10808
10809 if (PPC_HA (r2off) != 0)
10810 size += 4;
10811 }
721956f4 10812 }
84f5d08e
AM
10813 else if (info->emitrelocations)
10814 {
10815 stub_entry->stub_sec->reloc_count += 1;
10816 stub_entry->stub_sec->flags |= SEC_RELOC;
10817 }
721956f4
AM
10818 }
10819
eea6121a 10820 stub_entry->stub_sec->size += size;
b34976b6 10821 return TRUE;
721956f4
AM
10822}
10823
10824/* Set up various things so that we can make a list of input sections
10825 for each output section included in the link. Returns -1 on error,
cedb70c5 10826 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10827
10828int
927be08e
AM
10829ppc64_elf_setup_section_lists
10830 (struct bfd_link_info *info,
10831 asection *(*add_stub_section) (const char *, asection *),
10832 void (*layout_sections_again) (void))
721956f4
AM
10833{
10834 bfd *input_bfd;
734b6cf9 10835 int top_id, top_index, id;
721956f4 10836 asection *section;
734b6cf9 10837 asection **input_list;
721956f4
AM
10838 bfd_size_type amt;
10839 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10840
4dfe6ac6
NC
10841 if (htab == NULL)
10842 return -1;
927be08e
AM
10843 /* Stash our params away. */
10844 htab->add_stub_section = add_stub_section;
10845 htab->layout_sections_again = layout_sections_again;
4c52953f 10846
1e2f5b6e 10847 /* Find the top input section id. */
3d6f9012 10848 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10849 input_bfd != NULL;
10850 input_bfd = input_bfd->link_next)
10851 {
721956f4
AM
10852 for (section = input_bfd->sections;
10853 section != NULL;
10854 section = section->next)
10855 {
10856 if (top_id < section->id)
10857 top_id = section->id;
10858 }
10859 }
721956f4 10860
8f3bab57 10861 htab->top_id = top_id;
721956f4 10862 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10863 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10864 if (htab->stub_group == NULL)
10865 return -1;
10866
3d6f9012
AM
10867 /* Set toc_off for com, und, abs and ind sections. */
10868 for (id = 0; id < 3; id++)
10869 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10870
734b6cf9
AM
10871 /* We can't use output_bfd->section_count here to find the top output
10872 section index as some sections may have been removed, and
8423293d 10873 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10874 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10875 section != NULL;
10876 section = section->next)
10877 {
10878 if (top_index < section->index)
10879 top_index = section->index;
10880 }
10881
10882 htab->top_index = top_index;
10883 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10884 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10885 htab->input_list = input_list;
10886 if (input_list == NULL)
10887 return -1;
10888
721956f4
AM
10889 return 1;
10890}
10891
927be08e
AM
10892/* Set up for first pass at multitoc partitioning. */
10893
10894void
10895ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10896{
10897 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10898
1c865ab2 10899 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
927be08e
AM
10900 htab->toc_bfd = NULL;
10901 htab->toc_first_sec = NULL;
10902}
10903
e717da7e
AM
10904/* The linker repeatedly calls this function for each TOC input section
10905 and linker generated GOT section. Group input bfds such that the toc
927be08e 10906 within a group is less than 64k in size. */
ad8e1ba5 10907
927be08e 10908bfd_boolean
4ce794b7 10909ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10910{
10911 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10912 bfd_vma addr, off, limit;
ad8e1ba5 10913
4dfe6ac6
NC
10914 if (htab == NULL)
10915 return FALSE;
10916
927be08e 10917 if (!htab->second_toc_pass)
4c52953f 10918 {
927be08e 10919 /* Keep track of the first .toc or .got section for this input bfd. */
a4fd3de5
AM
10920 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10921
10922 if (new_bfd)
bf102f86
AM
10923 {
10924 htab->toc_bfd = isec->owner;
10925 htab->toc_first_sec = isec;
10926 }
927be08e 10927
bf102f86
AM
10928 addr = isec->output_offset + isec->output_section->vma;
10929 off = addr - htab->toc_curr;
d77c8a4b
AM
10930 limit = 0x80008000;
10931 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10932 limit = 0x10000;
10933 if (off + isec->size > limit)
bf102f86
AM
10934 {
10935 addr = (htab->toc_first_sec->output_offset
10936 + htab->toc_first_sec->output_section->vma);
10937 htab->toc_curr = addr;
10938 }
99877b66 10939
927be08e
AM
10940 /* toc_curr is the base address of this toc group. Set elf_gp
10941 for the input section to be the offset relative to the
10942 output toc base plus 0x8000. Making the input elf_gp an
10943 offset allows us to move the toc as a whole without
10944 recalculating input elf_gp. */
10945 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10946 off += TOC_BASE_OFF;
10947
10948 /* Die if someone uses a linker script that doesn't keep input
10949 file .toc and .got together. */
a4fd3de5
AM
10950 if (new_bfd
10951 && elf_gp (isec->owner) != 0
927be08e
AM
10952 && elf_gp (isec->owner) != off)
10953 return FALSE;
10954
10955 elf_gp (isec->owner) = off;
10956 return TRUE;
4c52953f 10957 }
927be08e
AM
10958
10959 /* During the second pass toc_first_sec points to the start of
10960 a toc group, and toc_curr is used to track the old elf_gp.
10961 We use toc_bfd to ensure we only look at each bfd once. */
10962 if (htab->toc_bfd == isec->owner)
10963 return TRUE;
10964 htab->toc_bfd = isec->owner;
10965
10966 if (htab->toc_first_sec == NULL
10967 || htab->toc_curr != elf_gp (isec->owner))
10968 {
10969 htab->toc_curr = elf_gp (isec->owner);
10970 htab->toc_first_sec = isec;
10971 }
10972 addr = (htab->toc_first_sec->output_offset
10973 + htab->toc_first_sec->output_section->vma);
10974 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10975 elf_gp (isec->owner) = off;
10976
10977 return TRUE;
ad8e1ba5
AM
10978}
10979
927be08e
AM
10980/* Called via elf_link_hash_traverse to merge GOT entries for global
10981 symbol H. */
10982
10983static bfd_boolean
10984merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10985{
10986 if (h->root.type == bfd_link_hash_indirect)
10987 return TRUE;
10988
927be08e
AM
10989 merge_got_entries (&h->got.glist);
10990
10991 return TRUE;
10992}
10993
10994/* Called via elf_link_hash_traverse to allocate GOT entries for global
10995 symbol H. */
10996
10997static bfd_boolean
10998reallocate_got (struct elf_link_hash_entry *h, void *inf)
10999{
11000 struct got_entry *gent;
11001
11002 if (h->root.type == bfd_link_hash_indirect)
11003 return TRUE;
11004
927be08e
AM
11005 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11006 if (!gent->is_indirect)
11007 allocate_got (h, (struct bfd_link_info *) inf, gent);
11008 return TRUE;
11009}
11010
11011/* Called on the first multitoc pass after the last call to
11012 ppc64_elf_next_toc_section. This function removes duplicate GOT
11013 entries. */
11014
11015bfd_boolean
11016ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
11017{
11018 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
11019 struct bfd *ibfd, *ibfd2;
11020 bfd_boolean done_something;
11021
11022 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 11023
7865406b
AM
11024 if (!htab->do_multi_toc)
11025 return FALSE;
11026
d0fae19d 11027 /* Merge global sym got entries within a toc group. */
927be08e
AM
11028 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11029
11030 /* And tlsld_got. */
11031 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11032 {
11033 struct got_entry *ent, *ent2;
11034
11035 if (!is_ppc64_elf (ibfd))
11036 continue;
11037
11038 ent = ppc64_tlsld_got (ibfd);
11039 if (!ent->is_indirect
11040 && ent->got.offset != (bfd_vma) -1)
11041 {
11042 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11043 {
11044 if (!is_ppc64_elf (ibfd2))
11045 continue;
11046
11047 ent2 = ppc64_tlsld_got (ibfd2);
11048 if (!ent2->is_indirect
11049 && ent2->got.offset != (bfd_vma) -1
11050 && elf_gp (ibfd2) == elf_gp (ibfd))
11051 {
11052 ent2->is_indirect = TRUE;
11053 ent2->got.ent = ent;
11054 }
11055 }
11056 }
11057 }
11058
11059 /* Zap sizes of got sections. */
11060 htab->reliplt->rawsize = htab->reliplt->size;
11061 htab->reliplt->size -= htab->got_reli_size;
11062 htab->got_reli_size = 0;
11063
11064 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11065 {
11066 asection *got, *relgot;
11067
11068 if (!is_ppc64_elf (ibfd))
11069 continue;
11070
11071 got = ppc64_elf_tdata (ibfd)->got;
11072 if (got != NULL)
11073 {
11074 got->rawsize = got->size;
11075 got->size = 0;
11076 relgot = ppc64_elf_tdata (ibfd)->relgot;
11077 relgot->rawsize = relgot->size;
11078 relgot->size = 0;
11079 }
11080 }
11081
11082 /* Now reallocate the got, local syms first. We don't need to
11083 allocate section contents again since we never increase size. */
11084 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11085 {
11086 struct got_entry **lgot_ents;
11087 struct got_entry **end_lgot_ents;
11088 struct plt_entry **local_plt;
11089 struct plt_entry **end_local_plt;
f961d9dd 11090 unsigned char *lgot_masks;
927be08e
AM
11091 bfd_size_type locsymcount;
11092 Elf_Internal_Shdr *symtab_hdr;
19e08130 11093 asection *s;
927be08e
AM
11094
11095 if (!is_ppc64_elf (ibfd))
11096 continue;
11097
11098 lgot_ents = elf_local_got_ents (ibfd);
11099 if (!lgot_ents)
11100 continue;
11101
11102 symtab_hdr = &elf_symtab_hdr (ibfd);
11103 locsymcount = symtab_hdr->sh_info;
11104 end_lgot_ents = lgot_ents + locsymcount;
11105 local_plt = (struct plt_entry **) end_lgot_ents;
11106 end_local_plt = local_plt + locsymcount;
f961d9dd 11107 lgot_masks = (unsigned char *) end_local_plt;
927be08e 11108 s = ppc64_elf_tdata (ibfd)->got;
927be08e
AM
11109 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11110 {
11111 struct got_entry *ent;
11112
11113 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d 11114 {
19e08130
AM
11115 unsigned int ent_size = 8;
11116 unsigned int rel_size = sizeof (Elf64_External_Rela);
11117
d0fae19d
AM
11118 ent->got.offset = s->size;
11119 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
d0fae19d 11120 {
19e08130
AM
11121 ent_size *= 2;
11122 rel_size *= 2;
11123 }
11124 s->size += ent_size;
11125 if ((*lgot_masks & PLT_IFUNC) != 0)
11126 {
11127 htab->reliplt->size += rel_size;
11128 htab->got_reli_size += rel_size;
11129 }
11130 else if (info->shared)
11131 {
11132 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11133 srel->size += rel_size;
d0fae19d
AM
11134 }
11135 }
927be08e
AM
11136 }
11137 }
11138
11139 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11140
11141 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11142 {
11143 struct got_entry *ent;
11144
11145 if (!is_ppc64_elf (ibfd))
11146 continue;
11147
11148 ent = ppc64_tlsld_got (ibfd);
11149 if (!ent->is_indirect
11150 && ent->got.offset != (bfd_vma) -1)
11151 {
11152 asection *s = ppc64_elf_tdata (ibfd)->got;
11153 ent->got.offset = s->size;
11154 s->size += 16;
11155 if (info->shared)
11156 {
11157 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11158 srel->size += sizeof (Elf64_External_Rela);
11159 }
11160 }
11161 }
11162
11163 done_something = htab->reliplt->rawsize != htab->reliplt->size;
11164 if (!done_something)
11165 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11166 {
11167 asection *got;
11168
11169 if (!is_ppc64_elf (ibfd))
11170 continue;
11171
11172 got = ppc64_elf_tdata (ibfd)->got;
11173 if (got != NULL)
11174 {
11175 done_something = got->rawsize != got->size;
11176 if (done_something)
11177 break;
11178 }
11179 }
11180
11181 if (done_something)
11182 (*htab->layout_sections_again) ();
11183
11184 /* Set up for second pass over toc sections to recalculate elf_gp
11185 on input sections. */
11186 htab->toc_bfd = NULL;
11187 htab->toc_first_sec = NULL;
11188 htab->second_toc_pass = TRUE;
11189 return done_something;
11190}
11191
11192/* Called after second pass of multitoc partitioning. */
11193
11194void
11195ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11196{
11197 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11198
11199 /* After the second pass, toc_curr tracks the TOC offset used
11200 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 11201 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
11202}
11203
9b5ecbd0
AM
11204/* No toc references were found in ISEC. If the code in ISEC makes no
11205 calls, then there's no need to use toc adjusting stubs when branching
11206 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
11207 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11208 needed, and 2 if a cyclical call-graph was found but no other reason
11209 for a stub was detected. If called from the top level, a return of
11210 2 means the same as a return of 0. */
9b5ecbd0
AM
11211
11212static int
4ce794b7 11213toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 11214{
9b5ecbd0 11215 int ret;
70cc837d
AM
11216
11217 /* Mark this section as checked. */
11218 isec->call_check_done = 1;
9b5ecbd0 11219
772119ce
AM
11220 /* We know none of our code bearing sections will need toc stubs. */
11221 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11222 return 0;
11223
eea6121a 11224 if (isec->size == 0)
082c50f8
AM
11225 return 0;
11226
4c52953f
AM
11227 if (isec->output_section == NULL)
11228 return 0;
11229
4c52953f 11230 ret = 0;
70cc837d 11231 if (isec->reloc_count != 0)
9b5ecbd0 11232 {
70cc837d
AM
11233 Elf_Internal_Rela *relstart, *rel;
11234 Elf_Internal_Sym *local_syms;
11235 struct ppc_link_hash_table *htab;
2917689a 11236
70cc837d
AM
11237 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11238 info->keep_memory);
11239 if (relstart == NULL)
11240 return -1;
90aecf7a 11241
70cc837d
AM
11242 /* Look for branches to outside of this section. */
11243 local_syms = NULL;
11244 htab = ppc_hash_table (info);
11245 if (htab == NULL)
11246 return -1;
4c52953f 11247
70cc837d 11248 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 11249 {
70cc837d
AM
11250 enum elf_ppc64_reloc_type r_type;
11251 unsigned long r_symndx;
11252 struct elf_link_hash_entry *h;
11253 struct ppc_link_hash_entry *eh;
11254 Elf_Internal_Sym *sym;
11255 asection *sym_sec;
11256 struct _opd_sec_data *opd;
11257 bfd_vma sym_value;
11258 bfd_vma dest;
11259
11260 r_type = ELF64_R_TYPE (rel->r_info);
11261 if (r_type != R_PPC64_REL24
11262 && r_type != R_PPC64_REL14
11263 && r_type != R_PPC64_REL14_BRTAKEN
11264 && r_type != R_PPC64_REL14_BRNTAKEN)
11265 continue;
4c52953f 11266
70cc837d
AM
11267 r_symndx = ELF64_R_SYM (rel->r_info);
11268 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11269 isec->owner))
4c52953f 11270 {
70cc837d
AM
11271 ret = -1;
11272 break;
11273 }
4c52953f 11274
70cc837d
AM
11275 /* Calls to dynamic lib functions go through a plt call stub
11276 that uses r2. */
11277 eh = (struct ppc_link_hash_entry *) h;
11278 if (eh != NULL
11279 && (eh->elf.plt.plist != NULL
11280 || (eh->oh != NULL
11281 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11282 {
11283 ret = 1;
11284 break;
4c52953f
AM
11285 }
11286
70cc837d
AM
11287 if (sym_sec == NULL)
11288 /* Ignore other undefined symbols. */
4c52953f 11289 continue;
4c52953f 11290
70cc837d
AM
11291 /* Assume branches to other sections not included in the
11292 link need stubs too, to cover -R and absolute syms. */
11293 if (sym_sec->output_section == NULL)
11294 {
11295 ret = 1;
11296 break;
11297 }
4c52953f 11298
70cc837d
AM
11299 if (h == NULL)
11300 sym_value = sym->st_value;
11301 else
11302 {
11303 if (h->root.type != bfd_link_hash_defined
11304 && h->root.type != bfd_link_hash_defweak)
11305 abort ();
11306 sym_value = h->root.u.def.value;
11307 }
11308 sym_value += rel->r_addend;
4c52953f 11309
70cc837d
AM
11310 /* If this branch reloc uses an opd sym, find the code section. */
11311 opd = get_opd_info (sym_sec);
11312 if (opd != NULL)
11313 {
11314 if (h == NULL && opd->adjust != NULL)
11315 {
11316 long adjust;
4c52953f 11317
70cc837d
AM
11318 adjust = opd->adjust[sym->st_value / 8];
11319 if (adjust == -1)
11320 /* Assume deleted functions won't ever be called. */
11321 continue;
11322 sym_value += adjust;
11323 }
4c52953f 11324
aef36ac1
AM
11325 dest = opd_entry_value (sym_sec, sym_value,
11326 &sym_sec, NULL, FALSE);
70cc837d
AM
11327 if (dest == (bfd_vma) -1)
11328 continue;
11329 }
11330 else
11331 dest = (sym_value
11332 + sym_sec->output_offset
11333 + sym_sec->output_section->vma);
4c52953f 11334
70cc837d
AM
11335 /* Ignore branch to self. */
11336 if (sym_sec == isec)
11337 continue;
4c52953f 11338
70cc837d
AM
11339 /* If the called function uses the toc, we need a stub. */
11340 if (sym_sec->has_toc_reloc
11341 || sym_sec->makes_toc_func_call)
4c52953f 11342 {
70cc837d 11343 ret = 1;
4c52953f
AM
11344 break;
11345 }
70cc837d
AM
11346
11347 /* Assume any branch that needs a long branch stub might in fact
11348 need a plt_branch stub. A plt_branch stub uses r2. */
11349 else if (dest - (isec->output_offset
11350 + isec->output_section->vma
6911b7dc
AM
11351 + rel->r_offset) + (1 << 25)
11352 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11353 ? h->other
11354 : sym->st_other))
4c52953f 11355 {
70cc837d
AM
11356 ret = 1;
11357 break;
11358 }
11359
11360 /* If calling back to a section in the process of being
11361 tested, we can't say for sure that no toc adjusting stubs
11362 are needed, so don't return zero. */
11363 else if (sym_sec->call_check_in_progress)
11364 ret = 2;
11365
11366 /* Branches to another section that itself doesn't have any TOC
11367 references are OK. Recursively call ourselves to check. */
11368 else if (!sym_sec->call_check_done)
11369 {
11370 int recur;
11371
11372 /* Mark current section as indeterminate, so that other
11373 sections that call back to current won't be marked as
11374 known. */
11375 isec->call_check_in_progress = 1;
11376 recur = toc_adjusting_stub_needed (info, sym_sec);
11377 isec->call_check_in_progress = 0;
11378
4c52953f
AM
11379 if (recur != 0)
11380 {
70cc837d
AM
11381 ret = recur;
11382 if (recur != 2)
11383 break;
4c52953f
AM
11384 }
11385 }
4c52953f 11386 }
70cc837d
AM
11387
11388 if (local_syms != NULL
11389 && (elf_symtab_hdr (isec->owner).contents
11390 != (unsigned char *) local_syms))
11391 free (local_syms);
11392 if (elf_section_data (isec)->relocs != relstart)
11393 free (relstart);
9b5ecbd0
AM
11394 }
11395
70cc837d
AM
11396 if ((ret & 1) == 0
11397 && isec->map_head.s != NULL
11398 && (strcmp (isec->output_section->name, ".init") == 0
11399 || strcmp (isec->output_section->name, ".fini") == 0))
11400 {
11401 if (isec->map_head.s->has_toc_reloc
11402 || isec->map_head.s->makes_toc_func_call)
11403 ret = 1;
11404 else if (!isec->map_head.s->call_check_done)
11405 {
11406 int recur;
11407 isec->call_check_in_progress = 1;
11408 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11409 isec->call_check_in_progress = 0;
11410 if (recur != 0)
11411 ret = recur;
11412 }
11413 }
11414
11415 if (ret == 1)
11416 isec->makes_toc_func_call = 1;
4c52953f 11417
9b5ecbd0
AM
11418 return ret;
11419}
11420
721956f4
AM
11421/* The linker repeatedly calls this function for each input section,
11422 in the order that input sections are linked into output sections.
11423 Build lists of input sections to determine groupings between which
11424 we may insert linker stubs. */
11425
9b5ecbd0 11426bfd_boolean
4ce794b7 11427ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
11428{
11429 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11430
4dfe6ac6
NC
11431 if (htab == NULL)
11432 return FALSE;
11433
734b6cf9
AM
11434 if ((isec->output_section->flags & SEC_CODE) != 0
11435 && isec->output_section->index <= htab->top_index)
721956f4 11436 {
734b6cf9 11437 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 11438 /* Steal the link_sec pointer for our list. */
721956f4 11439#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
11440 /* This happens to make the list in reverse order,
11441 which is what we want. */
734b6cf9
AM
11442 PREV_SEC (isec) = *list;
11443 *list = isec;
721956f4 11444 }
ad8e1ba5 11445
4c52953f 11446 if (htab->multi_toc_needed)
9b5ecbd0 11447 {
4c52953f
AM
11448 /* If a code section has a function that uses the TOC then we need
11449 to use the right TOC (obviously). Also, make sure that .opd gets
11450 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
11451 can't find their function symbol (shouldn't ever happen now).
11452 Also specially treat .fixup for the linux kernel. .fixup
11453 contains branches, but only back to the function that hit an
11454 exception. */
11455 if (isec->has_toc_reloc
11456 || (isec->flags & SEC_CODE) == 0
11457 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
11458 {
11459 if (elf_gp (isec->owner) != 0)
11460 htab->toc_curr = elf_gp (isec->owner);
11461 }
6683a28d
AM
11462 else
11463 {
11464 if (!isec->call_check_done
11465 && toc_adjusting_stub_needed (info, isec) < 0)
11466 return FALSE;
11467 /* If we make a local call from this section, ie. a branch
11468 without a following nop, then we have no place to put a
11469 toc restoring insn. We must use the same toc group as
11470 the callee.
11471 Testing makes_toc_func_call actually tests for *any*
11472 calls to functions that need a good toc pointer. A more
11473 precise test would be better, as this one will set
11474 incorrect values for pasted .init/.fini fragments.
11475 (Fixed later in check_pasted_section.) */
11476 if (isec->makes_toc_func_call
11477 && elf_gp (isec->owner) != 0)
11478 htab->toc_curr = elf_gp (isec->owner);
11479 }
9b5ecbd0 11480 }
ad8e1ba5
AM
11481
11482 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 11483 Use the last TOC base. */
ad8e1ba5 11484 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 11485 return TRUE;
721956f4
AM
11486}
11487
70cc837d
AM
11488/* Check that all .init and .fini sections use the same toc, if they
11489 have toc relocs. */
11490
11491static bfd_boolean
11492check_pasted_section (struct bfd_link_info *info, const char *name)
11493{
11494 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11495
11496 if (o != NULL)
11497 {
11498 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11499 bfd_vma toc_off = 0;
11500 asection *i;
11501
11502 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11503 if (i->has_toc_reloc)
11504 {
11505 if (toc_off == 0)
11506 toc_off = htab->stub_group[i->id].toc_off;
11507 else if (toc_off != htab->stub_group[i->id].toc_off)
11508 return FALSE;
11509 }
6683a28d
AM
11510
11511 if (toc_off == 0)
11512 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11513 if (i->makes_toc_func_call)
11514 {
11515 toc_off = htab->stub_group[i->id].toc_off;
11516 break;
11517 }
11518
70cc837d
AM
11519 /* Make sure the whole pasted function uses the same toc offset. */
11520 if (toc_off != 0)
11521 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11522 htab->stub_group[i->id].toc_off = toc_off;
11523 }
11524 return TRUE;
11525}
11526
11527bfd_boolean
11528ppc64_elf_check_init_fini (struct bfd_link_info *info)
11529{
11530 return (check_pasted_section (info, ".init")
11531 & check_pasted_section (info, ".fini"));
11532}
11533
721956f4
AM
11534/* See whether we can group stub sections together. Grouping stub
11535 sections may result in fewer stubs. More importantly, we need to
11536 put all .init* and .fini* stubs at the beginning of the .init or
11537 .fini output sections respectively, because glibc splits the
11538 _init and _fini functions into multiple parts. Putting a stub in
11539 the middle of a function is not a good idea. */
11540
11541static void
4ce794b7
AM
11542group_sections (struct ppc_link_hash_table *htab,
11543 bfd_size_type stub_group_size,
11544 bfd_boolean stubs_always_before_branch)
721956f4 11545{
7c8fe5c4
AM
11546 asection **list;
11547 bfd_size_type stub14_group_size;
11548 bfd_boolean suppress_size_errors;
11549
11550 suppress_size_errors = FALSE;
11551 stub14_group_size = stub_group_size;
11552 if (stub_group_size == 1)
11553 {
11554 /* Default values. */
11555 if (stubs_always_before_branch)
11556 {
11557 stub_group_size = 0x1e00000;
11558 stub14_group_size = 0x7800;
11559 }
11560 else
11561 {
11562 stub_group_size = 0x1c00000;
11563 stub14_group_size = 0x7000;
11564 }
11565 suppress_size_errors = TRUE;
11566 }
11567
11568 list = htab->input_list + htab->top_index;
734b6cf9 11569 do
721956f4 11570 {
734b6cf9
AM
11571 asection *tail = *list;
11572 while (tail != NULL)
721956f4 11573 {
734b6cf9
AM
11574 asection *curr;
11575 asection *prev;
11576 bfd_size_type total;
11577 bfd_boolean big_sec;
11578 bfd_vma curr_toc;
11579
11580 curr = tail;
eea6121a 11581 total = tail->size;
6bee8834
AM
11582 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11583 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
11584 ? stub14_group_size : stub_group_size);
11585 if (big_sec && !suppress_size_errors)
5c3dead3
AM
11586 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11587 tail->owner, tail);
734b6cf9
AM
11588 curr_toc = htab->stub_group[tail->id].toc_off;
11589
11590 while ((prev = PREV_SEC (curr)) != NULL
11591 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
11592 < (ppc64_elf_section_data (prev) != NULL
11593 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11594 ? stub14_group_size : stub_group_size))
ad8e1ba5 11595 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
11596 curr = prev;
11597
11598 /* OK, the size from the start of CURR to the end is less
11599 than stub_group_size and thus can be handled by one stub
11600 section. (or the tail section is itself larger than
11601 stub_group_size, in which case we may be toast.) We
11602 should really be keeping track of the total size of stubs
11603 added here, as stubs contribute to the final output
11604 section size. That's a little tricky, and this way will
11605 only break if stubs added make the total size more than
11606 2^25, ie. for the default stub_group_size, if stubs total
11607 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11608 do
721956f4
AM
11609 {
11610 prev = PREV_SEC (tail);
734b6cf9 11611 /* Set up this stub group. */
721956f4
AM
11612 htab->stub_group[tail->id].link_sec = curr;
11613 }
734b6cf9
AM
11614 while (tail != curr && (tail = prev) != NULL);
11615
11616 /* But wait, there's more! Input sections up to stub_group_size
11617 bytes before the stub section can be handled by it too.
11618 Don't do this if we have a really large section after the
11619 stubs, as adding more stubs increases the chance that
11620 branches may not reach into the stub section. */
11621 if (!stubs_always_before_branch && !big_sec)
11622 {
11623 total = 0;
11624 while (prev != NULL
11625 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
11626 < (ppc64_elf_section_data (prev) != NULL
11627 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 11628 ? stub14_group_size : stub_group_size))
734b6cf9
AM
11629 && htab->stub_group[prev->id].toc_off == curr_toc)
11630 {
11631 tail = prev;
11632 prev = PREV_SEC (tail);
11633 htab->stub_group[tail->id].link_sec = curr;
11634 }
11635 }
11636 tail = prev;
721956f4
AM
11637 }
11638 }
734b6cf9
AM
11639 while (list-- != htab->input_list);
11640 free (htab->input_list);
721956f4
AM
11641#undef PREV_SEC
11642}
11643
58d180e8
AM
11644static const unsigned char glink_eh_frame_cie[] =
11645{
11646 0, 0, 0, 16, /* length. */
11647 0, 0, 0, 0, /* id. */
11648 1, /* CIE version. */
11649 'z', 'R', 0, /* Augmentation string. */
11650 4, /* Code alignment. */
11651 0x78, /* Data alignment. */
11652 65, /* RA reg. */
11653 1, /* Augmentation size. */
11654 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11655 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11656};
11657
d969d15f
AM
11658/* Stripping output sections is normally done before dynamic section
11659 symbols have been allocated. This function is called later, and
11660 handles cases like htab->brlt which is mapped to its own output
11661 section. */
11662
11663static void
11664maybe_strip_output (struct bfd_link_info *info, asection *isec)
11665{
11666 if (isec->size == 0
11667 && isec->output_section->size == 0
53d8967a 11668 && !(isec->output_section->flags & SEC_KEEP)
d969d15f
AM
11669 && !bfd_section_removed_from_list (info->output_bfd,
11670 isec->output_section)
11671 && elf_section_data (isec->output_section)->dynindx == 0)
11672 {
11673 isec->output_section->flags |= SEC_EXCLUDE;
11674 bfd_section_list_remove (info->output_bfd, isec->output_section);
11675 info->output_bfd->section_count--;
11676 }
11677}
11678
721956f4
AM
11679/* Determine and set the size of the stub section for a final link.
11680
11681 The basic idea here is to examine all the relocations looking for
11682 PC-relative calls to a target that is unreachable with a "bl"
11683 instruction. */
11684
b34976b6 11685bfd_boolean
9df0ef5f 11686ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
794e51c0
AM
11687 bfd_boolean plt_static_chain, int plt_thread_safe,
11688 int plt_stub_align)
721956f4
AM
11689{
11690 bfd_size_type stub_group_size;
b34976b6 11691 bfd_boolean stubs_always_before_branch;
721956f4
AM
11692 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11693
4dfe6ac6
NC
11694 if (htab == NULL)
11695 return FALSE;
11696
9df0ef5f 11697 htab->plt_static_chain = plt_static_chain;
794e51c0 11698 htab->plt_stub_align = plt_stub_align;
e2458743
AM
11699 if (plt_thread_safe == -1 && !info->executable)
11700 plt_thread_safe = 1;
794e51c0
AM
11701 if (plt_thread_safe == -1)
11702 {
e2458743 11703 static const char *const thread_starter[] =
794e51c0
AM
11704 {
11705 "pthread_create",
11706 /* libstdc++ */
11707 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11708 /* librt */
11709 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11710 "mq_notify", "create_timer",
11711 /* libanl */
11712 "getaddrinfo_a",
11713 /* libgomp */
11714 "GOMP_parallel_start",
11715 "GOMP_parallel_loop_static_start",
11716 "GOMP_parallel_loop_dynamic_start",
11717 "GOMP_parallel_loop_guided_start",
11718 "GOMP_parallel_loop_runtime_start",
68ffbac6 11719 "GOMP_parallel_sections_start",
794e51c0
AM
11720 };
11721 unsigned i;
11722
11723 for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11724 {
11725 struct elf_link_hash_entry *h;
11726 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11727 FALSE, FALSE, TRUE);
11728 plt_thread_safe = h != NULL && h->ref_regular;
11729 if (plt_thread_safe)
11730 break;
11731 }
11732 }
11733 htab->plt_thread_safe = plt_thread_safe;
721956f4
AM
11734 stubs_always_before_branch = group_size < 0;
11735 if (group_size < 0)
11736 stub_group_size = -group_size;
11737 else
11738 stub_group_size = group_size;
721956f4
AM
11739
11740 group_sections (htab, stub_group_size, stubs_always_before_branch);
11741
721956f4
AM
11742 while (1)
11743 {
11744 bfd *input_bfd;
11745 unsigned int bfd_indx;
11746 asection *stub_sec;
721956f4
AM
11747
11748 htab->stub_iteration += 1;
721956f4
AM
11749
11750 for (input_bfd = info->input_bfds, bfd_indx = 0;
11751 input_bfd != NULL;
11752 input_bfd = input_bfd->link_next, bfd_indx++)
11753 {
11754 Elf_Internal_Shdr *symtab_hdr;
11755 asection *section;
6cdc0ccc 11756 Elf_Internal_Sym *local_syms = NULL;
721956f4 11757
0c8d6e5c 11758 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11759 continue;
11760
721956f4 11761 /* We'll need the symbol table in a second. */
0ffa91dd 11762 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11763 if (symtab_hdr->sh_info == 0)
11764 continue;
11765
721956f4
AM
11766 /* Walk over each section attached to the input bfd. */
11767 for (section = input_bfd->sections;
11768 section != NULL;
11769 section = section->next)
11770 {
721956f4 11771 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11772
11773 /* If there aren't any relocs, then there's nothing more
11774 to do. */
11775 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11776 || (section->flags & SEC_ALLOC) == 0
11777 || (section->flags & SEC_LOAD) == 0
11778 || (section->flags & SEC_CODE) == 0
721956f4
AM
11779 || section->reloc_count == 0)
11780 continue;
11781
11782 /* If this section is a link-once section that will be
11783 discarded, then don't create any stubs. */
11784 if (section->output_section == NULL
927be08e 11785 || section->output_section->owner != info->output_bfd)
721956f4
AM
11786 continue;
11787
1e2f5b6e
AM
11788 /* Get the relocs. */
11789 internal_relocs
4ce794b7 11790 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 11791 info->keep_memory);
721956f4 11792 if (internal_relocs == NULL)
1e2f5b6e 11793 goto error_ret_free_local;
721956f4
AM
11794
11795 /* Now examine each relocation. */
11796 irela = internal_relocs;
11797 irelaend = irela + section->reloc_count;
11798 for (; irela < irelaend; irela++)
11799 {
4ce794b7
AM
11800 enum elf_ppc64_reloc_type r_type;
11801 unsigned int r_indx;
721956f4
AM
11802 enum ppc_stub_type stub_type;
11803 struct ppc_stub_hash_entry *stub_entry;
8387904d 11804 asection *sym_sec, *code_sec;
e054468f 11805 bfd_vma sym_value, code_value;
721956f4 11806 bfd_vma destination;
6911b7dc 11807 unsigned long local_off;
8843416a 11808 bfd_boolean ok_dest;
721956f4 11809 struct ppc_link_hash_entry *hash;
8387904d 11810 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
11811 struct elf_link_hash_entry *h;
11812 Elf_Internal_Sym *sym;
721956f4
AM
11813 char *stub_name;
11814 const asection *id_sec;
74f0fb50 11815 struct _opd_sec_data *opd;
e054468f 11816 struct plt_entry *plt_ent;
721956f4
AM
11817
11818 r_type = ELF64_R_TYPE (irela->r_info);
11819 r_indx = ELF64_R_SYM (irela->r_info);
11820
4ce794b7 11821 if (r_type >= R_PPC64_max)
721956f4
AM
11822 {
11823 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 11824 goto error_ret_free_internal;
721956f4
AM
11825 }
11826
11827 /* Only look for stubs on branch instructions. */
4ce794b7
AM
11828 if (r_type != R_PPC64_REL24
11829 && r_type != R_PPC64_REL14
11830 && r_type != R_PPC64_REL14_BRTAKEN
11831 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
11832 continue;
11833
11834 /* Now determine the call target, its name, value,
11835 section. */
411e1bfb
AM
11836 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11837 r_indx, input_bfd))
11838 goto error_ret_free_internal;
11839 hash = (struct ppc_link_hash_entry *) h;
11840
8843416a 11841 ok_dest = FALSE;
8387904d 11842 fdh = NULL;
7fe2b9a6 11843 sym_value = 0;
411e1bfb 11844 if (hash == NULL)
721956f4 11845 {
411e1bfb 11846 sym_value = sym->st_value;
8843416a 11847 ok_dest = TRUE;
721956f4 11848 }
7fe2b9a6
AM
11849 else if (hash->elf.root.type == bfd_link_hash_defined
11850 || hash->elf.root.type == bfd_link_hash_defweak)
11851 {
11852 sym_value = hash->elf.root.u.def.value;
11853 if (sym_sec->output_section != NULL)
11854 ok_dest = TRUE;
11855 }
11856 else if (hash->elf.root.type == bfd_link_hash_undefweak
11857 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11858 {
99877b66 11859 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11860 use the func descriptor sym instead if it is
11861 defined. */
ceb1f1ef 11862 if (hash->elf.root.root.string[0] == '.'
b31867b6 11863 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 11864 {
8387904d
AM
11865 if (fdh->elf.root.type == bfd_link_hash_defined
11866 || fdh->elf.root.type == bfd_link_hash_defweak)
11867 {
11868 sym_sec = fdh->elf.root.u.def.section;
11869 sym_value = fdh->elf.root.u.def.value;
11870 if (sym_sec->output_section != NULL)
11871 ok_dest = TRUE;
11872 }
99877b66
AM
11873 else
11874 fdh = NULL;
8387904d 11875 }
7fe2b9a6
AM
11876 }
11877 else
11878 {
11879 bfd_set_error (bfd_error_bad_value);
11880 goto error_ret_free_internal;
721956f4
AM
11881 }
11882
8843416a 11883 destination = 0;
6911b7dc 11884 local_off = 0;
8843416a
AM
11885 if (ok_dest)
11886 {
11887 sym_value += irela->r_addend;
11888 destination = (sym_value
11889 + sym_sec->output_offset
11890 + sym_sec->output_section->vma);
6911b7dc
AM
11891 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11892 ? hash->elf.other
11893 : sym->st_other);
8843416a
AM
11894 }
11895
8387904d 11896 code_sec = sym_sec;
e054468f 11897 code_value = sym_value;
74f0fb50
AM
11898 opd = get_opd_info (sym_sec);
11899 if (opd != NULL)
8387904d
AM
11900 {
11901 bfd_vma dest;
11902
74f0fb50 11903 if (hash == NULL && opd->adjust != NULL)
8387904d 11904 {
74f0fb50 11905 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11906 if (adjust == -1)
11907 continue;
e054468f 11908 code_value += adjust;
8387904d
AM
11909 sym_value += adjust;
11910 }
11911 dest = opd_entry_value (sym_sec, sym_value,
aef36ac1 11912 &code_sec, &code_value, FALSE);
8387904d
AM
11913 if (dest != (bfd_vma) -1)
11914 {
11915 destination = dest;
11916 if (fdh != NULL)
11917 {
11918 /* Fixup old ABI sym to point at code
11919 entry. */
99877b66 11920 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11921 hash->elf.root.u.def.section = code_sec;
e054468f 11922 hash->elf.root.u.def.value = code_value;
8387904d
AM
11923 }
11924 }
11925 }
11926
721956f4 11927 /* Determine what (if any) linker stub is needed. */
e054468f 11928 plt_ent = NULL;
721956f4 11929 stub_type = ppc_type_of_stub (section, irela, &hash,
6911b7dc
AM
11930 &plt_ent, destination,
11931 local_off);
ad8e1ba5
AM
11932
11933 if (stub_type != ppc_stub_plt_call)
11934 {
11935 /* Check whether we need a TOC adjusting stub.
11936 Since the linker pastes together pieces from
11937 different object files when creating the
11938 _init and _fini functions, it may be that a
11939 call to what looks like a local sym is in
11940 fact a call needing a TOC adjustment. */
8387904d
AM
11941 if (code_sec != NULL
11942 && code_sec->output_section != NULL
11943 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11944 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11945 && (code_sec->has_toc_reloc
11946 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
11947 stub_type = ppc_stub_long_branch_r2off;
11948 }
11949
721956f4
AM
11950 if (stub_type == ppc_stub_none)
11951 continue;
11952
411e1bfb
AM
11953 /* __tls_get_addr calls might be eliminated. */
11954 if (stub_type != ppc_stub_plt_call
11955 && hash != NULL
8387904d
AM
11956 && (hash == htab->tls_get_addr
11957 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11958 && section->has_tls_reloc
11959 && irela != internal_relocs)
11960 {
11961 /* Get tls info. */
f961d9dd 11962 unsigned char *tls_mask;
411e1bfb 11963
3a71aa26 11964 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11965 irela - 1, input_bfd))
11966 goto error_ret_free_internal;
e7b938ca 11967 if (*tls_mask != 0)
411e1bfb
AM
11968 continue;
11969 }
11970
3b421ab3
AM
11971 if (stub_type == ppc_stub_plt_call
11972 && irela + 1 < irelaend
11973 && irela[1].r_offset == irela->r_offset + 4
794e51c0
AM
11974 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
11975 {
11976 if (!tocsave_find (htab, INSERT,
11977 &local_syms, irela + 1, input_bfd))
11978 goto error_ret_free_internal;
11979 }
11980 else if (stub_type == ppc_stub_plt_call)
11981 stub_type = ppc_stub_plt_call_r2save;
3b421ab3 11982
721956f4
AM
11983 /* Support for grouping stub sections. */
11984 id_sec = htab->stub_group[section->id].link_sec;
11985
11986 /* Get the name of this stub. */
11987 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11988 if (!stub_name)
11989 goto error_ret_free_internal;
11990
11991 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11992 stub_name, FALSE, FALSE);
721956f4
AM
11993 if (stub_entry != NULL)
11994 {
11995 /* The proper stub has already been created. */
11996 free (stub_name);
794e51c0
AM
11997 if (stub_type == ppc_stub_plt_call_r2save)
11998 stub_entry->stub_type = stub_type;
721956f4
AM
11999 continue;
12000 }
12001
25f53a85 12002 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
12003 if (stub_entry == NULL)
12004 {
12005 free (stub_name);
6cdc0ccc
AM
12006 error_ret_free_internal:
12007 if (elf_section_data (section)->relocs == NULL)
12008 free (internal_relocs);
12009 error_ret_free_local:
12010 if (local_syms != NULL
12011 && (symtab_hdr->contents
12012 != (unsigned char *) local_syms))
12013 free (local_syms);
b34976b6 12014 return FALSE;
721956f4
AM
12015 }
12016
ad8e1ba5 12017 stub_entry->stub_type = stub_type;
794e51c0
AM
12018 if (stub_type != ppc_stub_plt_call
12019 && stub_type != ppc_stub_plt_call_r2save)
e054468f
AM
12020 {
12021 stub_entry->target_value = code_value;
12022 stub_entry->target_section = code_sec;
12023 }
12024 else
12025 {
12026 stub_entry->target_value = sym_value;
12027 stub_entry->target_section = sym_sec;
12028 }
721956f4 12029 stub_entry->h = hash;
e054468f 12030 stub_entry->plt_ent = plt_ent;
6911b7dc 12031 stub_entry->other = hash ? hash->elf.other : sym->st_other;
ee75fd95
AM
12032
12033 if (stub_entry->h != NULL)
12034 htab->stub_globals += 1;
721956f4
AM
12035 }
12036
12037 /* We're done with the internal relocs, free them. */
6cdc0ccc 12038 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 12039 free (internal_relocs);
721956f4 12040 }
6cdc0ccc
AM
12041
12042 if (local_syms != NULL
12043 && symtab_hdr->contents != (unsigned char *) local_syms)
12044 {
12045 if (!info->keep_memory)
12046 free (local_syms);
12047 else
12048 symtab_hdr->contents = (unsigned char *) local_syms;
12049 }
721956f4
AM
12050 }
12051
5c3dead3 12052 /* We may have added some stubs. Find out the new size of the
721956f4
AM
12053 stub sections. */
12054 for (stub_sec = htab->stub_bfd->sections;
12055 stub_sec != NULL;
12056 stub_sec = stub_sec->next)
e717da7e 12057 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 12058 {
5c3dead3 12059 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
12060 stub_sec->size = 0;
12061 stub_sec->reloc_count = 0;
84f5d08e 12062 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 12063 }
eea6121a
AM
12064
12065 htab->brlt->size = 0;
84f5d08e
AM
12066 htab->brlt->reloc_count = 0;
12067 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 12068 if (htab->relbrlt != NULL)
eea6121a 12069 htab->relbrlt->size = 0;
721956f4 12070
63bc6f6c 12071 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 12072
176a0d42
AM
12073 if (info->emitrelocations
12074 && htab->glink != NULL && htab->glink->size != 0)
12075 {
12076 htab->glink->reloc_count = 1;
12077 htab->glink->flags |= SEC_RELOC;
12078 }
12079
58d180e8
AM
12080 if (htab->glink_eh_frame != NULL
12081 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
9a2a56cc 12082 && htab->glink_eh_frame->output_section->size != 0)
58d180e8 12083 {
4bbe044a 12084 size_t size = 0, align;
58d180e8
AM
12085
12086 for (stub_sec = htab->stub_bfd->sections;
12087 stub_sec != NULL;
12088 stub_sec = stub_sec->next)
12089 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12090 size += 20;
12091 if (htab->glink != NULL && htab->glink->size != 0)
12092 size += 24;
12093 if (size != 0)
12094 size += sizeof (glink_eh_frame_cie);
4bbe044a
AM
12095 align = 1;
12096 align <<= htab->glink_eh_frame->output_section->alignment_power;
12097 align -= 1;
12098 size = (size + align) & ~align;
58d180e8
AM
12099 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12100 htab->glink_eh_frame->size = size;
12101 }
12102
794e51c0
AM
12103 if (htab->plt_stub_align != 0)
12104 for (stub_sec = htab->stub_bfd->sections;
12105 stub_sec != NULL;
12106 stub_sec = stub_sec->next)
12107 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12108 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12109 & (-1 << htab->plt_stub_align));
12110
5c3dead3
AM
12111 for (stub_sec = htab->stub_bfd->sections;
12112 stub_sec != NULL;
12113 stub_sec = stub_sec->next)
12114 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12115 && stub_sec->rawsize != stub_sec->size)
12116 break;
12117
12118 /* Exit from this loop when no stubs have been added, and no stubs
12119 have changed size. */
58d180e8
AM
12120 if (stub_sec == NULL
12121 && (htab->glink_eh_frame == NULL
12122 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
12123 break;
12124
721956f4
AM
12125 /* Ask the linker to do its stuff. */
12126 (*htab->layout_sections_again) ();
12127 }
12128
d969d15f
AM
12129 maybe_strip_output (info, htab->brlt);
12130 if (htab->glink_eh_frame != NULL)
12131 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 12132
b34976b6 12133 return TRUE;
721956f4
AM
12134}
12135
12136/* Called after we have determined section placement. If sections
805fc799 12137 move, we'll be called again. Provide a value for TOCstart. */
721956f4 12138
805fc799 12139bfd_vma
1c865ab2 12140ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
721956f4 12141{
805fc799
AM
12142 asection *s;
12143 bfd_vma TOCstart;
721956f4 12144
805fc799
AM
12145 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12146 order. The TOC starts where the first of these sections starts. */
12147 s = bfd_get_section_by_name (obfd, ".got");
e054468f 12148 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12149 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 12150 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12151 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 12152 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 12153 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 12154 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
12155 {
12156 /* This may happen for
12157 o references to TOC base (SYM@toc / TOC[tc0]) without a
12158 .toc directive
12159 o bad linker script
12160 o --gc-sections and empty TOC sections
12161
12162 FIXME: Warn user? */
12163
12164 /* Look for a likely section. We probably won't even be
12165 using TOCstart. */
12166 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12167 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12168 | SEC_EXCLUDE))
805fc799
AM
12169 == (SEC_ALLOC | SEC_SMALL_DATA))
12170 break;
721956f4 12171 if (s == NULL)
805fc799 12172 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12173 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
12174 == (SEC_ALLOC | SEC_SMALL_DATA))
12175 break;
721956f4 12176 if (s == NULL)
805fc799 12177 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
12178 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12179 == SEC_ALLOC)
805fc799 12180 break;
721956f4 12181 if (s == NULL)
805fc799 12182 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 12183 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
12184 break;
12185 }
721956f4 12186
805fc799
AM
12187 TOCstart = 0;
12188 if (s != NULL)
12189 TOCstart = s->output_section->vma + s->output_offset;
721956f4 12190
1c865ab2
AM
12191 _bfd_set_gp_value (obfd, TOCstart);
12192
12193 if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12194 {
12195 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12196
12197 if (htab != NULL
12198 && htab->elf.hgot != NULL)
12199 {
5295321c 12200 htab->elf.hgot->type = STT_OBJECT;
1c865ab2
AM
12201 htab->elf.hgot->root.type = bfd_link_hash_defined;
12202 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12203 htab->elf.hgot->root.u.def.section = s;
12204 }
12205 }
805fc799 12206 return TOCstart;
721956f4
AM
12207}
12208
12209/* Build all the stubs associated with the current output file.
12210 The stubs are kept in a hash table attached to the main linker
12211 hash table. This function is called via gldelf64ppc_finish. */
12212
b34976b6 12213bfd_boolean
4ce794b7
AM
12214ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12215 struct bfd_link_info *info,
12216 char **stats)
5d1634d7
AM
12217{
12218 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 12219 asection *stub_sec;
5d1634d7 12220 bfd_byte *p;
e717da7e 12221 int stub_sec_count = 0;
5d1634d7 12222
4dfe6ac6
NC
12223 if (htab == NULL)
12224 return FALSE;
12225
ad8e1ba5 12226 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
12227
12228 /* Allocate memory to hold the linker stubs. */
721956f4
AM
12229 for (stub_sec = htab->stub_bfd->sections;
12230 stub_sec != NULL;
12231 stub_sec = stub_sec->next)
eea6121a
AM
12232 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12233 && stub_sec->size != 0)
e717da7e 12234 {
eea6121a
AM
12235 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12236 if (stub_sec->contents == NULL)
12237 return FALSE;
12238 /* We want to check that built size is the same as calculated
12239 size. rawsize is a convenient location to use. */
12240 stub_sec->rawsize = stub_sec->size;
12241 stub_sec->size = 0;
e717da7e 12242 }
5d1634d7 12243
23eb7e01 12244 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 12245 {
9f951329 12246 unsigned int indx;
ad8e1ba5 12247 bfd_vma plt0;
9f951329 12248
721956f4 12249 /* Build the .glink plt call stub. */
97b639ba
AM
12250 if (htab->emit_stub_syms)
12251 {
12252 struct elf_link_hash_entry *h;
468392fb
AM
12253 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12254 TRUE, FALSE, FALSE);
97b639ba
AM
12255 if (h == NULL)
12256 return FALSE;
12257 if (h->root.type == bfd_link_hash_new)
12258 {
12259 h->root.type = bfd_link_hash_defined;
12260 h->root.u.def.section = htab->glink;
ee4bf8d2 12261 h->root.u.def.value = 8;
f5385ebf
AM
12262 h->ref_regular = 1;
12263 h->def_regular = 1;
12264 h->ref_regular_nonweak = 1;
12265 h->forced_local = 1;
12266 h->non_elf = 0;
97b639ba
AM
12267 }
12268 }
176a0d42
AM
12269 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12270 if (info->emitrelocations)
12271 {
12272 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12273 if (r == NULL)
12274 return FALSE;
12275 r->r_offset = (htab->glink->output_offset
12276 + htab->glink->output_section->vma);
12277 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12278 r->r_addend = plt0;
12279 }
4ce794b7 12280 p = htab->glink->contents;
176a0d42 12281 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
12282 bfd_put_64 (htab->glink->owner, plt0, p);
12283 p += 8;
12284 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 12285 p += 4;
ee4bf8d2 12286 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 12287 p += 4;
ee4bf8d2 12288 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 12289 p += 4;
71a39c98 12290 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
ad8e1ba5 12291 p += 4;
ee4bf8d2 12292 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 12293 p += 4;
71a39c98 12294 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
ad8e1ba5 12295 p += 4;
71a39c98 12296 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
ad8e1ba5 12297 p += 4;
71a39c98 12298 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
ad8e1ba5 12299 p += 4;
71a39c98 12300 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
ad8e1ba5 12301 p += 4;
71a39c98 12302 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
ad8e1ba5 12303 p += 4;
4ce794b7 12304 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 12305 p += 4;
ee4bf8d2
AM
12306 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12307 {
12308 bfd_put_32 (htab->glink->owner, NOP, p);
12309 p += 4;
12310 }
ad8e1ba5 12311
9f951329
AM
12312 /* Build the .glink lazy link call stubs. */
12313 indx = 0;
eea6121a 12314 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
12315 {
12316 if (indx < 0x8000)
12317 {
4ce794b7 12318 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
12319 p += 4;
12320 }
12321 else
12322 {
4ce794b7 12323 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 12324 p += 4;
4ce794b7 12325 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
12326 p += 4;
12327 }
4ce794b7 12328 bfd_put_32 (htab->glink->owner,
ee4bf8d2 12329 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 12330 indx++;
9f951329
AM
12331 p += 4;
12332 }
eea6121a 12333 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 12334 }
5d1634d7 12335
eea6121a 12336 if (htab->brlt->size != 0)
721956f4 12337 {
4ce794b7 12338 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 12339 htab->brlt->size);
4ce794b7 12340 if (htab->brlt->contents == NULL)
b34976b6 12341 return FALSE;
721956f4 12342 }
ee75fd95 12343 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
12344 {
12345 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 12346 htab->relbrlt->size);
63bc6f6c
AM
12347 if (htab->relbrlt->contents == NULL)
12348 return FALSE;
12349 }
5d1634d7 12350
58d180e8
AM
12351 if (htab->glink_eh_frame != NULL
12352 && htab->glink_eh_frame->size != 0)
12353 {
12354 bfd_vma val;
4bbe044a
AM
12355 bfd_byte *last_fde;
12356 size_t last_fde_len, size, align, pad;
58d180e8
AM
12357
12358 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12359 if (p == NULL)
12360 return FALSE;
12361 htab->glink_eh_frame->contents = p;
4bbe044a 12362 last_fde = p;
58d180e8
AM
12363
12364 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12365
12366 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12367 /* CIE length (rewrite in case little-endian). */
4bbe044a
AM
12368 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12369 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
58d180e8
AM
12370 p += sizeof (glink_eh_frame_cie);
12371
12372 for (stub_sec = htab->stub_bfd->sections;
12373 stub_sec != NULL;
12374 stub_sec = stub_sec->next)
12375 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12376 {
4bbe044a
AM
12377 last_fde = p;
12378 last_fde_len = 16;
58d180e8
AM
12379 /* FDE length. */
12380 bfd_put_32 (htab->elf.dynobj, 16, p);
12381 p += 4;
12382 /* CIE pointer. */
12383 val = p - htab->glink_eh_frame->contents;
12384 bfd_put_32 (htab->elf.dynobj, val, p);
12385 p += 4;
12386 /* Offset to stub section. */
12387 val = (stub_sec->output_section->vma
12388 + stub_sec->output_offset);
12389 val -= (htab->glink_eh_frame->output_section->vma
12390 + htab->glink_eh_frame->output_offset);
12391 val -= p - htab->glink_eh_frame->contents;
12392 if (val + 0x80000000 > 0xffffffff)
12393 {
12394 info->callbacks->einfo
8de848d8 12395 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
12396 stub_sec->name);
12397 return FALSE;
12398 }
12399 bfd_put_32 (htab->elf.dynobj, val, p);
12400 p += 4;
12401 /* stub section size. */
12402 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12403 p += 4;
12404 /* Augmentation. */
12405 p += 1;
12406 /* Pad. */
12407 p += 3;
12408 }
12409 if (htab->glink != NULL && htab->glink->size != 0)
12410 {
4bbe044a
AM
12411 last_fde = p;
12412 last_fde_len = 20;
58d180e8
AM
12413 /* FDE length. */
12414 bfd_put_32 (htab->elf.dynobj, 20, p);
12415 p += 4;
12416 /* CIE pointer. */
12417 val = p - htab->glink_eh_frame->contents;
12418 bfd_put_32 (htab->elf.dynobj, val, p);
12419 p += 4;
12420 /* Offset to .glink. */
12421 val = (htab->glink->output_section->vma
12422 + htab->glink->output_offset
12423 + 8);
12424 val -= (htab->glink_eh_frame->output_section->vma
12425 + htab->glink_eh_frame->output_offset);
12426 val -= p - htab->glink_eh_frame->contents;
12427 if (val + 0x80000000 > 0xffffffff)
12428 {
12429 info->callbacks->einfo
8de848d8 12430 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
12431 htab->glink->name);
12432 return FALSE;
12433 }
12434 bfd_put_32 (htab->elf.dynobj, val, p);
12435 p += 4;
12436 /* .glink size. */
12437 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12438 p += 4;
12439 /* Augmentation. */
12440 p += 1;
12441
12442 *p++ = DW_CFA_advance_loc + 1;
12443 *p++ = DW_CFA_register;
12444 *p++ = 65;
12445 *p++ = 12;
12446 *p++ = DW_CFA_advance_loc + 4;
12447 *p++ = DW_CFA_restore_extended;
12448 *p++ = 65;
12449 }
4bbe044a
AM
12450 /* Subsume any padding into the last FDE if user .eh_frame
12451 sections are aligned more than glink_eh_frame. Otherwise any
12452 zero padding will be seen as a terminator. */
12453 size = p - htab->glink_eh_frame->contents;
12454 align = 1;
12455 align <<= htab->glink_eh_frame->output_section->alignment_power;
12456 align -= 1;
12457 pad = ((size + align) & ~align) - size;
12458 htab->glink_eh_frame->size = size + pad;
12459 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
58d180e8
AM
12460 }
12461
721956f4
AM
12462 /* Build the stubs as directed by the stub hash table. */
12463 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 12464
aa8a7074
AM
12465 if (htab->relbrlt != NULL)
12466 htab->relbrlt->reloc_count = 0;
12467
794e51c0
AM
12468 if (htab->plt_stub_align != 0)
12469 for (stub_sec = htab->stub_bfd->sections;
12470 stub_sec != NULL;
12471 stub_sec = stub_sec->next)
12472 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12473 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12474 & (-1 << htab->plt_stub_align));
12475
721956f4
AM
12476 for (stub_sec = htab->stub_bfd->sections;
12477 stub_sec != NULL;
12478 stub_sec = stub_sec->next)
e717da7e
AM
12479 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12480 {
12481 stub_sec_count += 1;
eea6121a 12482 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
12483 break;
12484 }
5d1634d7 12485
721956f4 12486 if (stub_sec != NULL
58d180e8
AM
12487 || htab->glink->rawsize != htab->glink->size
12488 || (htab->glink_eh_frame != NULL
12489 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 12490 {
b34976b6 12491 htab->stub_error = TRUE;
8de848d8 12492 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 12493 }
721956f4 12494
d2a300cf
AM
12495 if (htab->stub_error)
12496 return FALSE;
12497
12498 if (stats != NULL)
12499 {
12500 *stats = bfd_malloc (500);
12501 if (*stats == NULL)
12502 return FALSE;
12503
ee75fd95 12504 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
12505 " branch %lu\n"
12506 " toc adjust %lu\n"
12507 " long branch %lu\n"
12508 " long toc adj %lu\n"
794e51c0
AM
12509 " plt call %lu\n"
12510 " plt call toc %lu"),
e717da7e 12511 stub_sec_count,
ee75fd95 12512 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
12513 htab->stub_count[ppc_stub_long_branch - 1],
12514 htab->stub_count[ppc_stub_long_branch_r2off - 1],
12515 htab->stub_count[ppc_stub_plt_branch - 1],
12516 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
794e51c0
AM
12517 htab->stub_count[ppc_stub_plt_call - 1],
12518 htab->stub_count[ppc_stub_plt_call_r2save - 1]);
d2a300cf
AM
12519 }
12520 return TRUE;
5bd4f169
AM
12521}
12522
99877b66
AM
12523/* This function undoes the changes made by add_symbol_adjust. */
12524
12525static bfd_boolean
12526undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12527{
12528 struct ppc_link_hash_entry *eh;
12529
12530 if (h->root.type == bfd_link_hash_indirect)
12531 return TRUE;
12532
99877b66
AM
12533 eh = (struct ppc_link_hash_entry *) h;
12534 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12535 return TRUE;
12536
12537 eh->elf.root.type = bfd_link_hash_undefined;
12538 return TRUE;
12539}
12540
12541void
12542ppc64_elf_restore_symbols (struct bfd_link_info *info)
12543{
12544 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
12545
12546 if (htab != NULL)
12547 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
12548}
12549
60124e18
AM
12550/* What to do when ld finds relocations against symbols defined in
12551 discarded sections. */
12552
12553static unsigned int
12554ppc64_elf_action_discarded (asection *sec)
12555{
12556 if (strcmp (".opd", sec->name) == 0)
12557 return 0;
12558
12559 if (strcmp (".toc", sec->name) == 0)
12560 return 0;
12561
bce50a28
JJ
12562 if (strcmp (".toc1", sec->name) == 0)
12563 return 0;
12564
60124e18
AM
12565 return _bfd_elf_default_action_discarded (sec);
12566}
12567
5bd4f169
AM
12568/* The RELOCATE_SECTION function is called by the ELF backend linker
12569 to handle the relocations for a section.
12570
12571 The relocs are always passed as Rela structures; if the section
12572 actually uses Rel structures, the r_addend field will always be
12573 zero.
12574
12575 This function is responsible for adjust the section contents as
12576 necessary, and (if using Rela relocs and generating a
1049f94e 12577 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
12578 necessary.
12579
12580 This function does not have to worry about setting the reloc
12581 address or the reloc symbol index.
12582
12583 LOCAL_SYMS is a pointer to the swapped in local symbols.
12584
12585 LOCAL_SECTIONS is an array giving the section in the input file
12586 corresponding to the st_shndx field of each local symbol.
12587
12588 The global hash table entry for the global symbols can be found
12589 via elf_sym_hashes (input_bfd).
12590
1049f94e 12591 When generating relocatable output, this function must handle
5bd4f169
AM
12592 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
12593 going to be the section symbol corresponding to the output
12594 section, which means that the addend must be adjusted
12595 accordingly. */
12596
b34976b6 12597static bfd_boolean
4ce794b7
AM
12598ppc64_elf_relocate_section (bfd *output_bfd,
12599 struct bfd_link_info *info,
12600 bfd *input_bfd,
12601 asection *input_section,
12602 bfd_byte *contents,
12603 Elf_Internal_Rela *relocs,
12604 Elf_Internal_Sym *local_syms,
12605 asection **local_sections)
5bd4f169 12606{
65f38f15 12607 struct ppc_link_hash_table *htab;
5bd4f169
AM
12608 Elf_Internal_Shdr *symtab_hdr;
12609 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
12610 Elf_Internal_Rela *rel;
12611 Elf_Internal_Rela *relend;
411e1bfb
AM
12612 Elf_Internal_Rela outrel;
12613 bfd_byte *loc;
411e1bfb 12614 struct got_entry **local_got_ents;
5bd4f169 12615 bfd_vma TOCstart;
b34976b6
AM
12616 bfd_boolean ret = TRUE;
12617 bfd_boolean is_opd;
794e51c0
AM
12618 /* Assume 'at' branch hints. */
12619 bfd_boolean is_isa_v2 = TRUE;
4fe5ca5b 12620 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 12621
65f38f15 12622 /* Initialize howto table if needed. */
5bd4f169 12623 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
12624 ppc_howto_init ();
12625
65f38f15 12626 htab = ppc_hash_table (info);
4dfe6ac6
NC
12627 if (htab == NULL)
12628 return FALSE;
ee75fd95
AM
12629
12630 /* Don't relocate stub sections. */
12631 if (input_section->owner == htab->stub_bfd)
12632 return TRUE;
12633
0c8d6e5c 12634 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 12635
411e1bfb 12636 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 12637 TOCstart = elf_gp (output_bfd);
0ffa91dd 12638 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 12639 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 12640 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 12641
5bd4f169
AM
12642 rel = relocs;
12643 relend = relocs + input_section->reloc_count;
12644 for (; rel < relend; rel++)
12645 {
04c9666a 12646 enum elf_ppc64_reloc_type r_type;
31c76678 12647 bfd_vma addend;
5bd4f169
AM
12648 bfd_reloc_status_type r;
12649 Elf_Internal_Sym *sym;
12650 asection *sec;
039b3fef
AM
12651 struct elf_link_hash_entry *h_elf;
12652 struct ppc_link_hash_entry *h;
12653 struct ppc_link_hash_entry *fdh;
5bd4f169 12654 const char *sym_name;
0d4792f7 12655 unsigned long r_symndx, toc_symndx;
3a71aa26 12656 bfd_vma toc_addend;
f961d9dd
AM
12657 unsigned char tls_mask, tls_gd, tls_type;
12658 unsigned char sym_type;
5bd4f169 12659 bfd_vma relocation;
b34976b6
AM
12660 bfd_boolean unresolved_reloc;
12661 bfd_boolean warned;
bc30df16 12662 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
67f0cbdb 12663 unsigned int insn;
e11840f9 12664 unsigned int mask;
721956f4
AM
12665 struct ppc_stub_hash_entry *stub_entry;
12666 bfd_vma max_br_offset;
12667 bfd_vma from;
31c76678 12668 const Elf_Internal_Rela orig_rel = *rel;
5bd4f169 12669
4ce794b7 12670 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 12671 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
12672
12673 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12674 symbol of the previous ADDR64 reloc. The symbol gives us the
12675 proper TOC base to use. */
12676 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12677 && rel != relocs
12678 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12679 && is_opd)
12680 r_symndx = ELF64_R_SYM (rel[-1].r_info);
12681
4ce794b7
AM
12682 sym = NULL;
12683 sec = NULL;
039b3fef 12684 h_elf = NULL;
4ce794b7 12685 sym_name = NULL;
b34976b6
AM
12686 unresolved_reloc = FALSE;
12687 warned = FALSE;
65f38f15 12688
0b13192e 12689 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
12690 {
12691 /* It's a local symbol. */
74f0fb50 12692 struct _opd_sec_data *opd;
4025353c 12693
5bd4f169
AM
12694 sym = local_syms + r_symndx;
12695 sec = local_sections[r_symndx];
26c61ae5 12696 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 12697 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 12698 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
12699 opd = get_opd_info (sec);
12700 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 12701 {
74f0fb50 12702 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
12703 if (adjust == -1)
12704 relocation = 0;
12705 else
4cc603a5
AM
12706 {
12707 /* If this is a relocation against the opd section sym
12708 and we have edited .opd, adjust the reloc addend so
12709 that ld -r and ld --emit-relocs output is correct.
12710 If it is a reloc against some other .opd symbol,
12711 then the symbol value will be adjusted later. */
12712 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12713 rel->r_addend += adjust;
12714 else
12715 relocation += adjust;
12716 }
1e2f5b6e 12717 }
5bd4f169
AM
12718 }
12719 else
12720 {
b2a8e766
AM
12721 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12722 r_symndx, symtab_hdr, sym_hashes,
039b3fef 12723 h_elf, sec, relocation,
b2a8e766 12724 unresolved_reloc, warned);
039b3fef
AM
12725 sym_name = h_elf->root.root.string;
12726 sym_type = h_elf->type;
b69fdb4e
AM
12727 if (sec != NULL
12728 && sec->owner == output_bfd
12729 && strcmp (sec->name, ".opd") == 0)
12730 {
12731 /* This is a symbol defined in a linker script. All
12732 such are defined in output sections, even those
12733 defined by simple assignment from a symbol defined in
12734 an input section. Transfer the symbol to an
12735 appropriate input .opd section, so that a branch to
12736 this symbol will be mapped to the location specified
12737 by the opd entry. */
12738 struct bfd_link_order *lo;
12739 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12740 if (lo->type == bfd_indirect_link_order)
12741 {
12742 asection *isec = lo->u.indirect.section;
12743 if (h_elf->root.u.def.value >= isec->output_offset
12744 && h_elf->root.u.def.value < (isec->output_offset
12745 + isec->size))
12746 {
12747 h_elf->root.u.def.value -= isec->output_offset;
12748 h_elf->root.u.def.section = isec;
12749 sec = isec;
12750 break;
12751 }
12752 }
12753 }
5bd4f169 12754 }
039b3fef 12755 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 12756
dbaa2011 12757 if (sec != NULL && discarded_section (sec))
e4067dbb 12758 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b
MR
12759 rel, 1, relend,
12760 ppc64_elf_howto_table[r_type], 0,
e4067dbb 12761 contents);
ab96bf03
AM
12762
12763 if (info->relocatable)
12764 continue;
12765
f40da81b
AM
12766 if (h != NULL && &h->elf == htab->elf.hgot)
12767 {
12768 relocation = (TOCstart
12769 + htab->stub_group[input_section->id].toc_off);
12770 sec = bfd_abs_section_ptr;
12771 unresolved_reloc = FALSE;
12772 }
12773
951fd09b
AM
12774 /* TLS optimizations. Replace instruction sequences and relocs
12775 based on information we collected in tls_optimize. We edit
12776 RELOCS so that --emit-relocs will output something sensible
12777 for the final instruction stream. */
12778 tls_mask = 0;
12779 tls_gd = 0;
0d4792f7 12780 toc_symndx = 0;
727fc41e
AM
12781 if (h != NULL)
12782 tls_mask = h->tls_mask;
12783 else if (local_got_ents != NULL)
411e1bfb 12784 {
e054468f
AM
12785 struct plt_entry **local_plt = (struct plt_entry **)
12786 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 12787 unsigned char *lgot_masks = (unsigned char *)
e054468f 12788 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
12789 tls_mask = lgot_masks[r_symndx];
12790 }
12791 if (tls_mask == 0
12792 && (r_type == R_PPC64_TLS
12793 || r_type == R_PPC64_TLSGD
12794 || r_type == R_PPC64_TLSLD))
12795 {
12796 /* Check for toc tls entries. */
f961d9dd 12797 unsigned char *toc_tls;
0d4792f7 12798
727fc41e
AM
12799 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12800 &local_syms, rel, input_bfd))
12801 return FALSE;
0d4792f7 12802
727fc41e
AM
12803 if (toc_tls)
12804 tls_mask = *toc_tls;
0d4792f7
AM
12805 }
12806
12807 /* Check that tls relocs are used with tls syms, and non-tls
12808 relocs are used with non-tls syms. */
cf35638d 12809 if (r_symndx != STN_UNDEF
0d4792f7
AM
12810 && r_type != R_PPC64_NONE
12811 && (h == NULL
039b3fef
AM
12812 || h->elf.root.type == bfd_link_hash_defined
12813 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
12814 && (IS_PPC64_TLS_RELOC (r_type)
12815 != (sym_type == STT_TLS
12816 || (sym_type == STT_SECTION
12817 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 12818 {
727fc41e
AM
12819 if (tls_mask != 0
12820 && (r_type == R_PPC64_TLS
12821 || r_type == R_PPC64_TLSGD
12822 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
12823 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12824 ;
12825 else
25f53a85 12826 info->callbacks->einfo
1d483afe 12827 (!IS_PPC64_TLS_RELOC (r_type)
bc30df16
AM
12828 ? _("%P: %H: %s used with TLS symbol `%T'\n")
12829 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
25f53a85 12830 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
12831 ppc64_elf_howto_table[r_type]->name,
12832 sym_name);
411e1bfb
AM
12833 }
12834
12835 /* Ensure reloc mapping code below stays sane. */
12836 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12837 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12838 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12839 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12840 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12841 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12842 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12843 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12844 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12845 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12846 abort ();
0d4792f7 12847
411e1bfb
AM
12848 switch (r_type)
12849 {
12850 default:
411e1bfb
AM
12851 break;
12852
ba761f19
AM
12853 case R_PPC64_LO_DS_OPT:
12854 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12855 if ((insn & (0x3f << 26)) != 58u << 26)
12856 abort ();
12857 insn += (14u << 26) - (58u << 26);
12858 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12859 r_type = R_PPC64_TOC16_LO;
12860 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12861 break;
12862
411e1bfb
AM
12863 case R_PPC64_TOC16:
12864 case R_PPC64_TOC16_LO:
12865 case R_PPC64_TOC16_DS:
12866 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
12867 {
12868 /* Check for toc tls entries. */
f961d9dd 12869 unsigned char *toc_tls;
951fd09b 12870 int retval;
411e1bfb 12871
3a71aa26
AM
12872 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12873 &local_syms, rel, input_bfd);
951fd09b 12874 if (retval == 0)
411e1bfb
AM
12875 return FALSE;
12876
12877 if (toc_tls)
12878 {
951fd09b 12879 tls_mask = *toc_tls;
411e1bfb
AM
12880 if (r_type == R_PPC64_TOC16_DS
12881 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
12882 {
12883 if (tls_mask != 0
12884 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12885 goto toctprel;
12886 }
411e1bfb 12887 else
951fd09b
AM
12888 {
12889 /* If we found a GD reloc pair, then we might be
12890 doing a GD->IE transition. */
12891 if (retval == 2)
12892 {
12893 tls_gd = TLS_TPRELGD;
12894 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12895 goto tls_ldgd_opt;
951fd09b
AM
12896 }
12897 else if (retval == 3)
12898 {
12899 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 12900 goto tls_ldgd_opt;
951fd09b
AM
12901 }
12902 }
411e1bfb
AM
12903 }
12904 }
12905 break;
12906
9d6ded02
AM
12907 case R_PPC64_GOT_TPREL16_HI:
12908 case R_PPC64_GOT_TPREL16_HA:
12909 if (tls_mask != 0
12910 && (tls_mask & TLS_TPREL) == 0)
12911 {
12912 rel->r_offset -= d_offset;
12913 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12914 r_type = R_PPC64_NONE;
12915 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12916 }
12917 break;
12918
411e1bfb
AM
12919 case R_PPC64_GOT_TPREL16_DS:
12920 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
12921 if (tls_mask != 0
12922 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12923 {
81407a69 12924 toctprel:
4fe5ca5b 12925 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
12926 insn &= 31 << 21;
12927 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 12928 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 12929 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
12930 if (toc_symndx != 0)
12931 {
12932 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12933 rel->r_addend = toc_addend;
0d4792f7
AM
12934 /* We changed the symbol. Start over in order to
12935 get h, sym, sec etc. right. */
12936 rel--;
12937 continue;
12938 }
12939 else
12940 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12941 }
12942 break;
12943
12944 case R_PPC64_TLS:
951fd09b
AM
12945 if (tls_mask != 0
12946 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12947 {
411e1bfb 12948 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
12949 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12950 if (insn == 0)
411e1bfb 12951 abort ();
411e1bfb 12952 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 12953 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
12954 PPC64_TPREL16_LO which is at low-order half-word. */
12955 rel->r_offset += d_offset;
0d4792f7
AM
12956 r_type = R_PPC64_TPREL16_LO;
12957 if (toc_symndx != 0)
12958 {
12959 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12960 rel->r_addend = toc_addend;
0d4792f7
AM
12961 /* We changed the symbol. Start over in order to
12962 get h, sym, sec etc. right. */
12963 rel--;
12964 continue;
12965 }
12966 else
12967 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12968 }
12969 break;
12970
411e1bfb
AM
12971 case R_PPC64_GOT_TLSGD16_HI:
12972 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
12973 tls_gd = TLS_TPRELGD;
12974 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12975 goto tls_gdld_hi;
12976 break;
12977
411e1bfb
AM
12978 case R_PPC64_GOT_TLSLD16_HI:
12979 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12980 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 12981 {
951fd09b
AM
12982 tls_gdld_hi:
12983 if ((tls_mask & tls_gd) != 0)
12984 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12985 + R_PPC64_GOT_TPREL16_DS);
12986 else
411e1bfb 12987 {
4fe5ca5b 12988 rel->r_offset -= d_offset;
727ac201 12989 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 12990 r_type = R_PPC64_NONE;
411e1bfb 12991 }
951fd09b 12992 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12993 }
12994 break;
12995
951fd09b
AM
12996 case R_PPC64_GOT_TLSGD16:
12997 case R_PPC64_GOT_TLSGD16_LO:
12998 tls_gd = TLS_TPRELGD;
12999 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 13000 goto tls_ldgd_opt;
951fd09b 13001 break;
411e1bfb 13002
951fd09b
AM
13003 case R_PPC64_GOT_TLSLD16:
13004 case R_PPC64_GOT_TLSLD16_LO:
13005 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13006 {
3a71aa26 13007 unsigned int insn1, insn2, insn3;
102890f0
AM
13008 bfd_vma offset;
13009
13010 tls_ldgd_opt:
727fc41e
AM
13011 offset = (bfd_vma) -1;
13012 /* If not using the newer R_PPC64_TLSGD/LD to mark
13013 __tls_get_addr calls, we must trust that the call
13014 stays with its arg setup insns, ie. that the next
13015 reloc is the __tls_get_addr call associated with
13016 the current reloc. Edit both insns. */
13017 if (input_section->has_tls_get_addr_call
13018 && rel + 1 < relend
13019 && branch_reloc_hash_match (input_bfd, rel + 1,
13020 htab->tls_get_addr,
13021 htab->tls_get_addr_fd))
13022 offset = rel[1].r_offset;
102890f0 13023 if ((tls_mask & tls_gd) != 0)
411e1bfb 13024 {
102890f0 13025 /* IE */
3a71aa26
AM
13026 insn1 = bfd_get_32 (output_bfd,
13027 contents + rel->r_offset - d_offset);
102890f0
AM
13028 insn1 &= (1 << 26) - (1 << 2);
13029 insn1 |= 58 << 26; /* ld */
13030 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 13031 if (offset != (bfd_vma) -1)
f58d5a2d 13032 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
13033 if ((tls_mask & TLS_EXPLICIT) == 0)
13034 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13035 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 13036 else
102890f0
AM
13037 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13038 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13039 }
13040 else
13041 {
13042 /* LE */
13043 insn1 = 0x3c6d0000; /* addis 3,13,0 */
13044 insn2 = 0x38630000; /* addi 3,3,0 */
13045 if (tls_gd == 0)
951fd09b 13046 {
102890f0 13047 /* Was an LD reloc. */
1d483afe
AM
13048 if (toc_symndx)
13049 sec = local_sections[toc_symndx];
13050 for (r_symndx = 0;
13051 r_symndx < symtab_hdr->sh_info;
13052 r_symndx++)
13053 if (local_sections[r_symndx] == sec)
13054 break;
13055 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13056 r_symndx = STN_UNDEF;
102890f0 13057 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13058 if (r_symndx != STN_UNDEF)
1d483afe
AM
13059 rel->r_addend -= (local_syms[r_symndx].st_value
13060 + sec->output_offset
13061 + sec->output_section->vma);
951fd09b 13062 }
102890f0 13063 else if (toc_symndx != 0)
3a71aa26
AM
13064 {
13065 r_symndx = toc_symndx;
13066 rel->r_addend = toc_addend;
13067 }
102890f0
AM
13068 r_type = R_PPC64_TPREL16_HA;
13069 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
13070 if (offset != (bfd_vma) -1)
13071 {
13072 rel[1].r_info = ELF64_R_INFO (r_symndx,
13073 R_PPC64_TPREL16_LO);
13074 rel[1].r_offset = offset + d_offset;
13075 rel[1].r_addend = rel->r_addend;
13076 }
102890f0 13077 }
3a71aa26
AM
13078 bfd_put_32 (output_bfd, insn1,
13079 contents + rel->r_offset - d_offset);
727fc41e
AM
13080 if (offset != (bfd_vma) -1)
13081 {
13082 insn3 = bfd_get_32 (output_bfd,
13083 contents + offset + 4);
13084 if (insn3 == NOP
13085 || insn3 == CROR_151515 || insn3 == CROR_313131)
13086 {
13087 rel[1].r_offset += 4;
13088 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13089 insn2 = NOP;
13090 }
13091 bfd_put_32 (output_bfd, insn2, contents + offset);
13092 }
13093 if ((tls_mask & tls_gd) == 0
13094 && (tls_gd == 0 || toc_symndx != 0))
13095 {
13096 /* We changed the symbol. Start over in order
13097 to get h, sym, sec etc. right. */
13098 rel--;
13099 continue;
13100 }
13101 }
13102 break;
13103
13104 case R_PPC64_TLSGD:
13105 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13106 {
13107 unsigned int insn2, insn3;
13108 bfd_vma offset = rel->r_offset;
13109
13110 if ((tls_mask & TLS_TPRELGD) != 0)
13111 {
13112 /* IE */
13113 r_type = R_PPC64_NONE;
13114 insn2 = 0x7c636a14; /* add 3,3,13 */
13115 }
13116 else
13117 {
13118 /* LE */
13119 if (toc_symndx != 0)
13120 {
13121 r_symndx = toc_symndx;
13122 rel->r_addend = toc_addend;
13123 }
13124 r_type = R_PPC64_TPREL16_LO;
13125 rel->r_offset = offset + d_offset;
13126 insn2 = 0x38630000; /* addi 3,3,0 */
13127 }
13128 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13129 /* Zap the reloc on the _tls_get_addr call too. */
13130 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13131 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
13132 insn3 = bfd_get_32 (output_bfd,
13133 contents + offset + 4);
102890f0
AM
13134 if (insn3 == NOP
13135 || insn3 == CROR_151515 || insn3 == CROR_313131)
13136 {
727fc41e 13137 rel->r_offset += 4;
3a71aa26
AM
13138 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13139 insn2 = NOP;
102890f0 13140 }
102890f0 13141 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 13142 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 13143 {
102890f0
AM
13144 rel--;
13145 continue;
411e1bfb 13146 }
411e1bfb 13147 }
411e1bfb
AM
13148 break;
13149
727fc41e
AM
13150 case R_PPC64_TLSLD:
13151 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13152 {
13153 unsigned int insn2, insn3;
13154 bfd_vma offset = rel->r_offset;
13155
13156 if (toc_symndx)
13157 sec = local_sections[toc_symndx];
13158 for (r_symndx = 0;
13159 r_symndx < symtab_hdr->sh_info;
13160 r_symndx++)
13161 if (local_sections[r_symndx] == sec)
13162 break;
13163 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 13164 r_symndx = STN_UNDEF;
727fc41e 13165 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 13166 if (r_symndx != STN_UNDEF)
727fc41e
AM
13167 rel->r_addend -= (local_syms[r_symndx].st_value
13168 + sec->output_offset
13169 + sec->output_section->vma);
13170
13171 r_type = R_PPC64_TPREL16_LO;
13172 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13173 rel->r_offset = offset + d_offset;
13174 /* Zap the reloc on the _tls_get_addr call too. */
13175 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 13176 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
13177 insn2 = 0x38630000; /* addi 3,3,0 */
13178 insn3 = bfd_get_32 (output_bfd,
13179 contents + offset + 4);
13180 if (insn3 == NOP
13181 || insn3 == CROR_151515 || insn3 == CROR_313131)
13182 {
13183 rel->r_offset += 4;
13184 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13185 insn2 = NOP;
13186 }
13187 bfd_put_32 (output_bfd, insn2, contents + offset);
13188 rel--;
13189 continue;
13190 }
13191 break;
13192
411e1bfb 13193 case R_PPC64_DTPMOD64:
951fd09b
AM
13194 if (rel + 1 < relend
13195 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13196 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 13197 {
951fd09b
AM
13198 if ((tls_mask & TLS_GD) == 0)
13199 {
13200 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13201 if ((tls_mask & TLS_TPRELGD) != 0)
13202 r_type = R_PPC64_TPREL64;
13203 else
13204 {
4ce794b7 13205 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
13206 r_type = R_PPC64_NONE;
13207 }
13208 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13209 }
13210 }
13211 else
13212 {
13213 if ((tls_mask & TLS_LD) == 0)
411e1bfb 13214 {
4ce794b7 13215 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 13216 r_type = R_PPC64_NONE;
951fd09b 13217 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 13218 }
411e1bfb
AM
13219 }
13220 break;
13221
13222 case R_PPC64_TPREL64:
951fd09b 13223 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
13224 {
13225 r_type = R_PPC64_NONE;
13226 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13227 }
13228 break;
13229 }
13230
13231 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 13232 insn = 0;
b25116a9
AM
13233 max_br_offset = 1 << 25;
13234 addend = rel->r_addend;
bc30df16 13235 reloc_dest = DEST_NORMAL;
65f38f15 13236 switch (r_type)
5bd4f169
AM
13237 {
13238 default:
65f38f15 13239 break;
5bd4f169 13240
3b421ab3
AM
13241 case R_PPC64_TOCSAVE:
13242 if (relocation + addend == (rel->r_offset
13243 + input_section->output_offset
13244 + input_section->output_section->vma)
13245 && tocsave_find (htab, NO_INSERT,
13246 &local_syms, rel, input_bfd))
13247 {
13248 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13249 if (insn == NOP
13250 || insn == CROR_151515 || insn == CROR_313131)
13251 bfd_put_32 (input_bfd, STD_R2_40R1,
13252 contents + rel->r_offset);
13253 }
13254 break;
13255
65f38f15
AM
13256 /* Branch taken prediction relocations. */
13257 case R_PPC64_ADDR14_BRTAKEN:
13258 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
13259 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13260 /* Fall thru. */
65f38f15 13261
86c76c7b 13262 /* Branch not taken prediction relocations. */
65f38f15
AM
13263 case R_PPC64_ADDR14_BRNTAKEN:
13264 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
13265 insn |= bfd_get_32 (output_bfd,
13266 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 13267 /* Fall thru. */
86c76c7b 13268
b25116a9
AM
13269 case R_PPC64_REL14:
13270 max_br_offset = 1 << 15;
13271 /* Fall thru. */
5bd4f169 13272
65f38f15 13273 case R_PPC64_REL24:
ad8e1ba5
AM
13274 /* Calls to functions with a different TOC, such as calls to
13275 shared objects, need to alter the TOC pointer. This is
13276 done using a linkage stub. A REL24 branching to these
13277 linkage stubs needs to be followed by a nop, as the nop
13278 will be replaced with an instruction to restore the TOC
13279 base pointer. */
8387904d 13280 fdh = h;
b31867b6
AM
13281 if (h != NULL
13282 && h->oh != NULL
13283 && h->oh->is_func_descriptor)
13284 fdh = ppc_follow_link (h->oh);
31c76678
DK
13285 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13286 htab);
6abec6bc 13287 if (stub_entry != NULL
ad8e1ba5 13288 && (stub_entry->stub_type == ppc_stub_plt_call
794e51c0 13289 || stub_entry->stub_type == ppc_stub_plt_call_r2save
ad8e1ba5
AM
13290 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13291 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 13292 {
b25116a9 13293 bfd_boolean can_plt_call = FALSE;
721956f4 13294
ba8ca3e7
AM
13295 /* All of these stubs will modify r2, so there must be a
13296 branch and link followed by a nop. The nop is
13297 replaced by an insn to restore r2. */
eea6121a 13298 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 13299 {
ba8ca3e7
AM
13300 unsigned long br;
13301
13302 br = bfd_get_32 (input_bfd,
13303 contents + rel->r_offset);
13304 if ((br & 1) != 0)
41bd81ab 13305 {
ba8ca3e7
AM
13306 unsigned long nop;
13307
13308 nop = bfd_get_32 (input_bfd,
13309 contents + rel->r_offset + 4);
13310 if (nop == NOP
13311 || nop == CROR_151515 || nop == CROR_313131)
a7f2871e 13312 {
ba8ca3e7
AM
13313 if (h != NULL
13314 && (h == htab->tls_get_addr_fd
13315 || h == htab->tls_get_addr)
13316 && !htab->no_tls_get_addr_opt)
13317 {
13318 /* Special stub used, leave nop alone. */
13319 }
13320 else
13321 bfd_put_32 (input_bfd, LD_R2_40R1,
13322 contents + rel->r_offset + 4);
13323 can_plt_call = TRUE;
a7f2871e 13324 }
41bd81ab 13325 }
5bd4f169 13326 }
721956f4 13327
ba8ca3e7 13328 if (!can_plt_call && h != NULL)
721956f4 13329 {
ba8ca3e7
AM
13330 const char *name = h->elf.root.root.string;
13331
13332 if (*name == '.')
13333 ++name;
13334
13335 if (strncmp (name, "__libc_start_main", 17) == 0
13336 && (name[17] == 0 || name[17] == '@'))
6ab189d5 13337 {
ba8ca3e7
AM
13338 /* Allow crt1 branch to go via a toc adjusting
13339 stub. Other calls that never return could do
13340 the same, if we could detect such. */
b25116a9 13341 can_plt_call = TRUE;
6ab189d5 13342 }
ba8ca3e7
AM
13343 }
13344
13345 if (!can_plt_call)
13346 {
13347 /* g++ as of 20130507 emits self-calls without a
13348 following nop. This is arguably wrong since we
13349 have conflicting information. On the one hand a
13350 global symbol and on the other a local call
13351 sequence, but don't error for this special case.
13352 It isn't possible to cheaply verify we have
13353 exactly such a call. Allow all calls to the same
13354 section. */
13355 asection *code_sec = sec;
13356
13357 if (get_opd_info (sec) != NULL)
ad8e1ba5 13358 {
ba8ca3e7
AM
13359 bfd_vma off = (relocation + addend
13360 - sec->output_section->vma
13361 - sec->output_offset);
bc30df16 13362
ba8ca3e7 13363 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
ad8e1ba5 13364 }
ba8ca3e7
AM
13365 if (code_sec == input_section)
13366 can_plt_call = TRUE;
13367 }
13368
13369 if (!can_plt_call)
13370 {
13371 info->callbacks->einfo
13372 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13373 "recompile with -fPIC"),
13374 input_bfd, input_section, rel->r_offset, sym_name);
13375
13376 bfd_set_error (bfd_error_bad_value);
13377 ret = FALSE;
721956f4
AM
13378 }
13379
b25116a9 13380 if (can_plt_call
794e51c0
AM
13381 && (stub_entry->stub_type == ppc_stub_plt_call
13382 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
b25116a9
AM
13383 unresolved_reloc = FALSE;
13384 }
13385
6abec6bc
AM
13386 if ((stub_entry == NULL
13387 || stub_entry->stub_type == ppc_stub_long_branch
13388 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
13389 && get_opd_info (sec) != NULL)
13390 {
13391 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
13392 bfd_vma off = (relocation + addend
13393 - sec->output_section->vma
13394 - sec->output_offset);
aef36ac1 13395 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
8387904d
AM
13396 if (dest != (bfd_vma) -1)
13397 {
13398 relocation = dest;
13399 addend = 0;
bc30df16 13400 reloc_dest = DEST_OPD;
8387904d
AM
13401 }
13402 }
13403
b25116a9
AM
13404 /* If the branch is out of reach we ought to have a long
13405 branch stub. */
13406 from = (rel->r_offset
13407 + input_section->output_offset
13408 + input_section->output_section->vma);
13409
6911b7dc
AM
13410 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13411 ? fdh->elf.other
13412 : sym->st_other);
13413
6abec6bc
AM
13414 if (stub_entry != NULL
13415 && (stub_entry->stub_type == ppc_stub_long_branch
13416 || stub_entry->stub_type == ppc_stub_plt_branch)
13417 && (r_type == R_PPC64_ADDR14_BRTAKEN
13418 || r_type == R_PPC64_ADDR14_BRNTAKEN
13419 || (relocation + addend - from + max_br_offset
13420 < 2 * max_br_offset)))
13421 /* Don't use the stub if this branch is in range. */
13422 stub_entry = NULL;
b25116a9
AM
13423
13424 if (stub_entry != NULL)
13425 {
13426 /* Munge up the value and addend so that we call the stub
13427 rather than the procedure directly. */
13428 relocation = (stub_entry->stub_offset
13429 + stub_entry->stub_sec->output_offset
13430 + stub_entry->stub_sec->output_section->vma);
13431 addend = 0;
bc30df16 13432 reloc_dest = DEST_STUB;
3b421ab3 13433
794e51c0
AM
13434 if ((stub_entry->stub_type == ppc_stub_plt_call
13435 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13436 && (ALWAYS_EMIT_R2SAVE
13437 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
3b421ab3
AM
13438 && rel + 1 < relend
13439 && rel[1].r_offset == rel->r_offset + 4
13440 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13441 relocation += 4;
b25116a9
AM
13442 }
13443
13444 if (insn != 0)
13445 {
794e51c0 13446 if (is_isa_v2)
721956f4 13447 {
b25116a9
AM
13448 /* Set 'a' bit. This is 0b00010 in BO field for branch
13449 on CR(BI) insns (BO == 001at or 011at), and 0b01000
13450 for branch on CTR insns (BO == 1a00t or 1a01t). */
13451 if ((insn & (0x14 << 21)) == (0x04 << 21))
13452 insn |= 0x02 << 21;
13453 else if ((insn & (0x14 << 21)) == (0x10 << 21))
13454 insn |= 0x08 << 21;
13455 else
13456 break;
13457 }
13458 else
13459 {
13460 /* Invert 'y' bit if not the default. */
4cc603a5 13461 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 13462 insn ^= 0x01 << 21;
721956f4 13463 }
b25116a9
AM
13464
13465 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 13466 }
e86ce104 13467
06da1e8e
AM
13468 /* NOP out calls to undefined weak functions.
13469 We can thus call a weak function without first
13470 checking whether the function is defined. */
b25116a9 13471 else if (h != NULL
039b3fef 13472 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 13473 && h->elf.dynindx == -1
b25116a9
AM
13474 && r_type == R_PPC64_REL24
13475 && relocation == 0
4cc603a5 13476 && addend == 0)
e86ce104 13477 {
06da1e8e
AM
13478 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13479 continue;
e86ce104 13480 }
65f38f15
AM
13481 break;
13482 }
5bd4f169 13483
65f38f15 13484 /* Set `addend'. */
411e1bfb 13485 tls_type = 0;
65f38f15
AM
13486 switch (r_type)
13487 {
13488 default:
25f53a85 13489 info->callbacks->einfo
bc30df16 13490 (_("%P: %B: unknown relocation type %d for `%T'\n"),
d003868e 13491 input_bfd, (int) r_type, sym_name);
5bd4f169 13492
65f38f15 13493 bfd_set_error (bfd_error_bad_value);
b34976b6 13494 ret = FALSE;
65f38f15 13495 continue;
5bd4f169 13496
65f38f15 13497 case R_PPC64_NONE:
411e1bfb 13498 case R_PPC64_TLS:
727fc41e
AM
13499 case R_PPC64_TLSGD:
13500 case R_PPC64_TLSLD:
3b421ab3 13501 case R_PPC64_TOCSAVE:
04c9666a
AM
13502 case R_PPC64_GNU_VTINHERIT:
13503 case R_PPC64_GNU_VTENTRY:
65f38f15 13504 continue;
5bd4f169
AM
13505
13506 /* GOT16 relocations. Like an ADDR16 using the symbol's
13507 address in the GOT as relocation value instead of the
411e1bfb 13508 symbol's value itself. Also, create a GOT entry for the
5bd4f169 13509 symbol and put the symbol value there. */
411e1bfb
AM
13510 case R_PPC64_GOT_TLSGD16:
13511 case R_PPC64_GOT_TLSGD16_LO:
13512 case R_PPC64_GOT_TLSGD16_HI:
13513 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 13514 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
13515 goto dogot;
13516
13517 case R_PPC64_GOT_TLSLD16:
13518 case R_PPC64_GOT_TLSLD16_LO:
13519 case R_PPC64_GOT_TLSLD16_HI:
13520 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 13521 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
13522 goto dogot;
13523
13524 case R_PPC64_GOT_TPREL16_DS:
13525 case R_PPC64_GOT_TPREL16_LO_DS:
13526 case R_PPC64_GOT_TPREL16_HI:
13527 case R_PPC64_GOT_TPREL16_HA:
13528 tls_type = TLS_TLS | TLS_TPREL;
13529 goto dogot;
13530
13531 case R_PPC64_GOT_DTPREL16_DS:
13532 case R_PPC64_GOT_DTPREL16_LO_DS:
13533 case R_PPC64_GOT_DTPREL16_HI:
13534 case R_PPC64_GOT_DTPREL16_HA:
13535 tls_type = TLS_TLS | TLS_DTPREL;
13536 goto dogot;
13537
65f38f15
AM
13538 case R_PPC64_GOT16:
13539 case R_PPC64_GOT16_LO:
13540 case R_PPC64_GOT16_HI:
13541 case R_PPC64_GOT16_HA:
13542 case R_PPC64_GOT16_DS:
13543 case R_PPC64_GOT16_LO_DS:
411e1bfb 13544 dogot:
5bd4f169
AM
13545 {
13546 /* Relocation is to the entry for this symbol in the global
13547 offset table. */
e717da7e 13548 asection *got;
d881513a 13549 bfd_vma *offp;
5bd4f169 13550 bfd_vma off;
d881513a 13551 unsigned long indx = 0;
927be08e 13552 struct got_entry *ent;
65f38f15 13553
d881513a
AM
13554 if (tls_type == (TLS_TLS | TLS_LD)
13555 && (h == NULL
f5385ebf 13556 || !h->elf.def_dynamic))
927be08e 13557 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 13558 else
5bd4f169 13559 {
5bd4f169 13560
d881513a
AM
13561 if (h != NULL)
13562 {
13563 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
13564 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13565 &h->elf)
d881513a 13566 || (info->shared
726d3ab0 13567 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
13568 /* This is actually a static link, or it is a
13569 -Bsymbolic link and the symbol is defined
13570 locally, or the symbol was forced to be local
13571 because of a version file. */
13572 ;
13573 else
13574 {
14acf4dc 13575 BFD_ASSERT (h->elf.dynindx != -1);
039b3fef 13576 indx = h->elf.dynindx;
d881513a
AM
13577 unresolved_reloc = FALSE;
13578 }
039b3fef 13579 ent = h->elf.got.glist;
d881513a 13580 }
411e1bfb 13581 else
5bd4f169 13582 {
d881513a
AM
13583 if (local_got_ents == NULL)
13584 abort ();
13585 ent = local_got_ents[r_symndx];
5bd4f169 13586 }
d881513a
AM
13587
13588 for (; ent != NULL; ent = ent->next)
31c76678 13589 if (ent->addend == orig_rel.r_addend
e717da7e 13590 && ent->owner == input_bfd
d881513a
AM
13591 && ent->tls_type == tls_type)
13592 break;
5bd4f169 13593 }
411e1bfb 13594
927be08e
AM
13595 if (ent == NULL)
13596 abort ();
13597 if (ent->is_indirect)
13598 ent = ent->got.ent;
13599 offp = &ent->got.offset;
13600 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
13601 if (got == NULL)
13602 abort ();
13603
411e1bfb
AM
13604 /* The offset must always be a multiple of 8. We use the
13605 least significant bit to record whether we have already
13606 processed this entry. */
d881513a 13607 off = *offp;
411e1bfb
AM
13608 if ((off & 1) != 0)
13609 off &= ~1;
5bd4f169
AM
13610 else
13611 {
411e1bfb
AM
13612 /* Generate relocs for the dynamic linker, except in
13613 the case of TLSLD where we'll use one entry per
13614 module. */
25f23106
AM
13615 asection *relgot;
13616 bfd_boolean ifunc;
e717da7e 13617
d881513a 13618 *offp = off | 1;
25f23106
AM
13619 relgot = NULL;
13620 ifunc = (h != NULL
13621 ? h->elf.type == STT_GNU_IFUNC
13622 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
19e08130 13623 if (ifunc)
25f23106 13624 relgot = htab->reliplt;
19e08130
AM
13625 else if ((info->shared || indx != 0)
13626 && (h == NULL
13627 || (tls_type == (TLS_TLS | TLS_LD)
13628 && !h->elf.def_dynamic)
13629 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13630 || h->elf.root.type != bfd_link_hash_undefweak))
13631 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106 13632 if (relgot != NULL)
5bd4f169 13633 {
e717da7e
AM
13634 outrel.r_offset = (got->output_section->vma
13635 + got->output_offset
411e1bfb 13636 + off);
4cc603a5 13637 outrel.r_addend = addend;
d881513a 13638 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 13639 {
411e1bfb 13640 outrel.r_addend = 0;
e515b051 13641 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
13642 if (tls_type == (TLS_TLS | TLS_GD))
13643 {
e717da7e
AM
13644 loc = relgot->contents;
13645 loc += (relgot->reloc_count++
d881513a
AM
13646 * sizeof (Elf64_External_Rela));
13647 bfd_elf64_swap_reloca_out (output_bfd,
13648 &outrel, loc);
e515b051 13649 outrel.r_offset += 8;
4cc603a5 13650 outrel.r_addend = addend;
d881513a
AM
13651 outrel.r_info
13652 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 13653 }
411e1bfb 13654 }
951fd09b 13655 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 13656 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 13657 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13658 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
13659 else if (indx != 0)
13660 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13661 else
81407a69 13662 {
25f23106
AM
13663 if (ifunc)
13664 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13665 else
13666 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
13667
13668 /* Write the .got section contents for the sake
13669 of prelink. */
e717da7e 13670 loc = got->contents + off;
23fbd6fa
JJ
13671 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13672 loc);
81407a69 13673 }
81407a69
AM
13674
13675 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
13676 {
13677 outrel.r_addend += relocation;
13678 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 13679 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 13680 }
e717da7e
AM
13681 loc = relgot->contents;
13682 loc += (relgot->reloc_count++
411e1bfb
AM
13683 * sizeof (Elf64_External_Rela));
13684 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13685 }
13686
ad8e1ba5 13687 /* Init the .got section contents here if we're not
81407a69 13688 emitting a reloc. */
d881513a 13689 else
411e1bfb 13690 {
4cc603a5 13691 relocation += addend;
7b609f53
AM
13692 if (tls_type == (TLS_TLS | TLS_LD))
13693 relocation = 1;
13694 else if (tls_type != 0)
411e1bfb 13695 {
e1918d23 13696 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 13697 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 13698 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 13699
7b609f53
AM
13700 if (tls_type == (TLS_TLS | TLS_GD))
13701 {
13702 bfd_put_64 (output_bfd, relocation,
e717da7e 13703 got->contents + off + 8);
7b609f53
AM
13704 relocation = 1;
13705 }
411e1bfb 13706 }
7b609f53 13707
411e1bfb 13708 bfd_put_64 (output_bfd, relocation,
e717da7e 13709 got->contents + off);
5bd4f169
AM
13710 }
13711 }
13712
65f38f15
AM
13713 if (off >= (bfd_vma) -2)
13714 abort ();
13715
bf102f86
AM
13716 relocation = got->output_section->vma + got->output_offset + off;
13717 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 13718 }
65f38f15
AM
13719 break;
13720
13721 case R_PPC64_PLT16_HA:
13722 case R_PPC64_PLT16_HI:
13723 case R_PPC64_PLT16_LO:
13724 case R_PPC64_PLT32:
13725 case R_PPC64_PLT64:
13726 /* Relocation is to the entry for this symbol in the
13727 procedure linkage table. */
13728
13729 /* Resolve a PLT reloc against a local symbol directly,
13730 without using the procedure linkage table. */
13731 if (h == NULL)
13732 break;
13733
411e1bfb
AM
13734 /* It's possible that we didn't make a PLT entry for this
13735 symbol. This happens when statically linking PIC code,
13736 or when using -Bsymbolic. Go find a match if there is a
13737 PLT entry. */
4ce794b7 13738 if (htab->plt != NULL)
65f38f15 13739 {
411e1bfb 13740 struct plt_entry *ent;
039b3fef 13741 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
31c76678 13742 if (ent->addend == orig_rel.r_addend
411e1bfb
AM
13743 && ent->plt.offset != (bfd_vma) -1)
13744 {
4ce794b7
AM
13745 relocation = (htab->plt->output_section->vma
13746 + htab->plt->output_offset
411e1bfb
AM
13747 + ent->plt.offset);
13748 unresolved_reloc = FALSE;
13749 }
65f38f15 13750 }
65f38f15 13751 break;
5bd4f169 13752
0b13192e
AM
13753 case R_PPC64_TOC:
13754 /* Relocation value is TOC base. */
13755 relocation = TOCstart;
cf35638d 13756 if (r_symndx == STN_UNDEF)
0b13192e 13757 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
13758 else if (unresolved_reloc)
13759 ;
13760 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
13761 relocation += htab->stub_group[sec->id].toc_off;
13762 else
13763 unresolved_reloc = TRUE;
ab96bf03 13764 goto dodyn;
0b13192e 13765
5bd4f169
AM
13766 /* TOC16 relocs. We want the offset relative to the TOC base,
13767 which is the address of the start of the TOC plus 0x8000.
13768 The TOC consists of sections .got, .toc, .tocbss, and .plt,
13769 in this order. */
65f38f15
AM
13770 case R_PPC64_TOC16:
13771 case R_PPC64_TOC16_LO:
13772 case R_PPC64_TOC16_HI:
13773 case R_PPC64_TOC16_DS:
13774 case R_PPC64_TOC16_LO_DS:
13775 case R_PPC64_TOC16_HA:
ad8e1ba5 13776 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
13777 break;
13778
13779 /* Relocate against the beginning of the section. */
65f38f15
AM
13780 case R_PPC64_SECTOFF:
13781 case R_PPC64_SECTOFF_LO:
13782 case R_PPC64_SECTOFF_HI:
13783 case R_PPC64_SECTOFF_DS:
13784 case R_PPC64_SECTOFF_LO_DS:
13785 case R_PPC64_SECTOFF_HA:
4ce794b7 13786 if (sec != NULL)
65f38f15 13787 addend -= sec->output_section->vma;
5bd4f169
AM
13788 break;
13789
25f23106
AM
13790 case R_PPC64_REL16:
13791 case R_PPC64_REL16_LO:
13792 case R_PPC64_REL16_HI:
13793 case R_PPC64_REL16_HA:
13794 break;
13795
721956f4
AM
13796 case R_PPC64_REL14:
13797 case R_PPC64_REL14_BRNTAKEN:
13798 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
13799 case R_PPC64_REL24:
13800 break;
13801
411e1bfb
AM
13802 case R_PPC64_TPREL16:
13803 case R_PPC64_TPREL16_LO:
13804 case R_PPC64_TPREL16_HI:
13805 case R_PPC64_TPREL16_HA:
13806 case R_PPC64_TPREL16_DS:
13807 case R_PPC64_TPREL16_LO_DS:
f9c6b907
AM
13808 case R_PPC64_TPREL16_HIGH:
13809 case R_PPC64_TPREL16_HIGHA:
411e1bfb
AM
13810 case R_PPC64_TPREL16_HIGHER:
13811 case R_PPC64_TPREL16_HIGHERA:
13812 case R_PPC64_TPREL16_HIGHEST:
13813 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
13814 if (h != NULL
13815 && h->elf.root.type == bfd_link_hash_undefweak
13816 && h->elf.dynindx == -1)
13817 {
13818 /* Make this relocation against an undefined weak symbol
13819 resolve to zero. This is really just a tweak, since
13820 code using weak externs ought to check that they are
13821 defined before using them. */
13822 bfd_byte *p = contents + rel->r_offset - d_offset;
13823
13824 insn = bfd_get_32 (output_bfd, p);
13825 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13826 if (insn != 0)
13827 bfd_put_32 (output_bfd, insn, p);
13828 break;
13829 }
e1918d23 13830 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13831 if (info->shared)
13832 /* The TPREL16 relocs shouldn't really be used in shared
13833 libs as they will result in DT_TEXTREL being set, but
13834 support them anyway. */
13835 goto dodyn;
13836 break;
13837
13838 case R_PPC64_DTPREL16:
13839 case R_PPC64_DTPREL16_LO:
13840 case R_PPC64_DTPREL16_HI:
13841 case R_PPC64_DTPREL16_HA:
13842 case R_PPC64_DTPREL16_DS:
13843 case R_PPC64_DTPREL16_LO_DS:
f9c6b907
AM
13844 case R_PPC64_DTPREL16_HIGH:
13845 case R_PPC64_DTPREL16_HIGHA:
411e1bfb
AM
13846 case R_PPC64_DTPREL16_HIGHER:
13847 case R_PPC64_DTPREL16_HIGHERA:
13848 case R_PPC64_DTPREL16_HIGHEST:
13849 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 13850 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13851 break;
13852
e515b051
AM
13853 case R_PPC64_DTPMOD64:
13854 relocation = 1;
13855 addend = 0;
13856 goto dodyn;
13857
411e1bfb 13858 case R_PPC64_TPREL64:
e1918d23 13859 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13860 goto dodyn;
13861
13862 case R_PPC64_DTPREL64:
e1918d23 13863 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13864 /* Fall thru */
13865
65f38f15
AM
13866 /* Relocations that may need to be propagated if this is a
13867 dynamic object. */
04c9666a 13868 case R_PPC64_REL30:
65f38f15
AM
13869 case R_PPC64_REL32:
13870 case R_PPC64_REL64:
13871 case R_PPC64_ADDR14:
13872 case R_PPC64_ADDR14_BRNTAKEN:
13873 case R_PPC64_ADDR14_BRTAKEN:
13874 case R_PPC64_ADDR16:
13875 case R_PPC64_ADDR16_DS:
13876 case R_PPC64_ADDR16_HA:
13877 case R_PPC64_ADDR16_HI:
f9c6b907
AM
13878 case R_PPC64_ADDR16_HIGH:
13879 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
13880 case R_PPC64_ADDR16_HIGHER:
13881 case R_PPC64_ADDR16_HIGHERA:
13882 case R_PPC64_ADDR16_HIGHEST:
13883 case R_PPC64_ADDR16_HIGHESTA:
13884 case R_PPC64_ADDR16_LO:
13885 case R_PPC64_ADDR16_LO_DS:
13886 case R_PPC64_ADDR24:
65f38f15
AM
13887 case R_PPC64_ADDR32:
13888 case R_PPC64_ADDR64:
13889 case R_PPC64_UADDR16:
13890 case R_PPC64_UADDR32:
13891 case R_PPC64_UADDR64:
411e1bfb 13892 dodyn:
5d1634d7 13893 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
13894 break;
13895
41bd81ab
AM
13896 if (NO_OPD_RELOCS && is_opd)
13897 break;
13898
65f38f15 13899 if ((info->shared
4e795f50 13900 && (h == NULL
039b3fef
AM
13901 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13902 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 13903 && (must_be_dyn_reloc (info, r_type)
039b3fef 13904 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
13905 || (ELIMINATE_COPY_RELOCS
13906 && !info->shared
65f38f15 13907 && h != NULL
039b3fef 13908 && h->elf.dynindx != -1
f5385ebf 13909 && !h->elf.non_got_ref
25f23106
AM
13910 && !h->elf.def_regular)
13911 || (!info->shared
13912 && (h != NULL
13913 ? h->elf.type == STT_GNU_IFUNC
13914 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 13915 {
b34976b6 13916 bfd_boolean skip, relocate;
65f38f15 13917 asection *sreloc;
1cf1f670 13918 bfd_vma out_off;
65f38f15
AM
13919
13920 /* When generating a dynamic object, these relocations
13921 are copied into the output file to be resolved at run
13922 time. */
13923
b34976b6
AM
13924 skip = FALSE;
13925 relocate = FALSE;
65f38f15 13926
1cf1f670
AM
13927 out_off = _bfd_elf_section_offset (output_bfd, info,
13928 input_section, rel->r_offset);
13929 if (out_off == (bfd_vma) -1)
b34976b6 13930 skip = TRUE;
1cf1f670 13931 else if (out_off == (bfd_vma) -2)
b34976b6 13932 skip = TRUE, relocate = TRUE;
1cf1f670
AM
13933 out_off += (input_section->output_section->vma
13934 + input_section->output_offset);
13935 outrel.r_offset = out_off;
411e1bfb 13936 outrel.r_addend = rel->r_addend;
65f38f15 13937
1cf1f670
AM
13938 /* Optimize unaligned reloc use. */
13939 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13940 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13941 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13942 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13943 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13944 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13945 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13946 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13947 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13948
65f38f15 13949 if (skip)
0bb2d96a 13950 memset (&outrel, 0, sizeof outrel);
726d3ab0 13951 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
13952 && !is_opd
13953 && r_type != R_PPC64_TOC)
14acf4dc
MR
13954 {
13955 BFD_ASSERT (h->elf.dynindx != -1);
13956 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13957 }
65f38f15
AM
13958 else
13959 {
41bd81ab
AM
13960 /* This symbol is local, or marked to become local,
13961 or this is an opd section reloc which must point
13962 at a local function. */
65f38f15 13963 outrel.r_addend += relocation;
e86ce104 13964 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 13965 {
3fad3c7c 13966 if (is_opd && h != NULL)
afbe61cf
AM
13967 {
13968 /* Lie about opd entries. This case occurs
13969 when building shared libraries and we
13970 reference a function in another shared
3fad3c7c
AM
13971 lib. The same thing happens for a weak
13972 definition in an application that's
13973 overridden by a strong definition in a
13974 shared lib. (I believe this is a generic
13975 bug in binutils handling of weak syms.)
13976 In these cases we won't use the opd
1e2f5b6e 13977 entry in this lib. */
b34976b6 13978 unresolved_reloc = FALSE;
afbe61cf 13979 }
25f23106
AM
13980 if (!is_opd
13981 && r_type == R_PPC64_ADDR64
13982 && (h != NULL
13983 ? h->elf.type == STT_GNU_IFUNC
13984 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13985 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13986 else
13987 {
13988 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 13989
25f23106
AM
13990 /* We need to relocate .opd contents for ld.so.
13991 Prelink also wants simple and consistent rules
13992 for relocs. This make all RELATIVE relocs have
13993 *r_offset equal to r_addend. */
13994 relocate = TRUE;
13995 }
65f38f15
AM
13996 }
13997 else
13998 {
13999 long indx = 0;
14000
25f23106
AM
14001 if (h != NULL
14002 ? h->elf.type == STT_GNU_IFUNC
14003 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14004 {
25f53a85 14005 info->callbacks->einfo
bc30df16
AM
14006 (_("%P: %H: %s for indirect "
14007 "function `%T' unsupported\n"),
25f53a85 14008 input_bfd, input_section, rel->r_offset,
25f23106
AM
14009 ppc64_elf_howto_table[r_type]->name,
14010 sym_name);
14011 ret = FALSE;
14012 }
cf35638d 14013 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
14014 ;
14015 else if (sec == NULL || sec->owner == NULL)
14016 {
14017 bfd_set_error (bfd_error_bad_value);
b34976b6 14018 return FALSE;
65f38f15
AM
14019 }
14020 else
14021 {
14022 asection *osec;
14023
14024 osec = sec->output_section;
14025 indx = elf_section_data (osec)->dynindx;
14026
74541ad4
AM
14027 if (indx == 0)
14028 {
14029 if ((osec->flags & SEC_READONLY) == 0
14030 && htab->elf.data_index_section != NULL)
14031 osec = htab->elf.data_index_section;
14032 else
14033 osec = htab->elf.text_index_section;
14034 indx = elf_section_data (osec)->dynindx;
14035 }
14036 BFD_ASSERT (indx != 0);
14037
65f38f15
AM
14038 /* We are turning this relocation into one
14039 against a section symbol, so subtract out
14040 the output section's address but not the
14041 offset of the input section in the output
14042 section. */
14043 outrel.r_addend -= osec->vma;
14044 }
14045
14046 outrel.r_info = ELF64_R_INFO (indx, r_type);
14047 }
14048 }
14049
14050 sreloc = elf_section_data (input_section)->sreloc;
19e08130
AM
14051 if (h != NULL
14052 ? h->elf.type == STT_GNU_IFUNC
14053 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
25f23106 14054 sreloc = htab->reliplt;
65f38f15
AM
14055 if (sreloc == NULL)
14056 abort ();
14057
dfbb6ac9
AM
14058 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14059 >= sreloc->size)
14060 abort ();
947216bf
AM
14061 loc = sreloc->contents;
14062 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
14063 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14064
14065 /* If this reloc is against an external symbol, it will
14066 be computed at runtime, so there's no need to do
81407a69
AM
14067 anything now. However, for the sake of prelink ensure
14068 that the section contents are a known value. */
65f38f15 14069 if (! relocate)
81407a69
AM
14070 {
14071 unresolved_reloc = FALSE;
14072 /* The value chosen here is quite arbitrary as ld.so
14073 ignores section contents except for the special
14074 case of .opd where the contents might be accessed
14075 before relocation. Choose zero, as that won't
14076 cause reloc overflow. */
14077 relocation = 0;
14078 addend = 0;
14079 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14080 to improve backward compatibility with older
14081 versions of ld. */
14082 if (r_type == R_PPC64_ADDR64)
14083 addend = outrel.r_addend;
14084 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 14085 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
14086 addend = (input_section->output_section->vma
14087 + input_section->output_offset
14088 + rel->r_offset);
81407a69 14089 }
65f38f15 14090 }
5bd4f169
AM
14091 break;
14092
65f38f15
AM
14093 case R_PPC64_COPY:
14094 case R_PPC64_GLOB_DAT:
14095 case R_PPC64_JMP_SLOT:
25f23106 14096 case R_PPC64_JMP_IREL:
65f38f15
AM
14097 case R_PPC64_RELATIVE:
14098 /* We shouldn't ever see these dynamic relocs in relocatable
14099 files. */
ae9a127f 14100 /* Fall through. */
65f38f15
AM
14101
14102 case R_PPC64_PLTGOT16:
14103 case R_PPC64_PLTGOT16_DS:
14104 case R_PPC64_PLTGOT16_HA:
14105 case R_PPC64_PLTGOT16_HI:
14106 case R_PPC64_PLTGOT16_LO:
14107 case R_PPC64_PLTGOT16_LO_DS:
14108 case R_PPC64_PLTREL32:
14109 case R_PPC64_PLTREL64:
14110 /* These ones haven't been implemented yet. */
14111
25f53a85 14112 info->callbacks->einfo
bc30df16 14113 (_("%P: %B: %s is not supported for `%T'\n"),
d003868e 14114 input_bfd,
4ce794b7 14115 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
14116
14117 bfd_set_error (bfd_error_invalid_operation);
b34976b6 14118 ret = FALSE;
5bd4f169 14119 continue;
65f38f15 14120 }
5bd4f169 14121
67f0cbdb
AM
14122 /* Multi-instruction sequences that access the TOC can be
14123 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14124 to nop; addi rb,r2,x; */
14125 switch (r_type)
14126 {
14127 default:
14128 break;
14129
14130 case R_PPC64_GOT_TLSLD16_HI:
14131 case R_PPC64_GOT_TLSGD16_HI:
14132 case R_PPC64_GOT_TPREL16_HI:
14133 case R_PPC64_GOT_DTPREL16_HI:
14134 case R_PPC64_GOT16_HI:
14135 case R_PPC64_TOC16_HI:
14136 /* These relocs would only be useful if building up an
14137 offset to later add to r2, perhaps in an indexed
14138 addressing mode instruction. Don't try to optimize.
14139 Unfortunately, the possibility of someone building up an
14140 offset like this or even with the HA relocs, means that
14141 we need to check the high insn when optimizing the low
14142 insn. */
14143 break;
14144
14145 case R_PPC64_GOT_TLSLD16_HA:
14146 case R_PPC64_GOT_TLSGD16_HA:
14147 case R_PPC64_GOT_TPREL16_HA:
14148 case R_PPC64_GOT_DTPREL16_HA:
14149 case R_PPC64_GOT16_HA:
14150 case R_PPC64_TOC16_HA:
98528052 14151 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14152 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
98528052
AM
14153 {
14154 bfd_byte *p = contents + (rel->r_offset & ~3);
14155 bfd_put_32 (input_bfd, NOP, p);
14156 }
67f0cbdb
AM
14157 break;
14158
14159 case R_PPC64_GOT_TLSLD16_LO:
14160 case R_PPC64_GOT_TLSGD16_LO:
14161 case R_PPC64_GOT_TPREL16_LO_DS:
14162 case R_PPC64_GOT_DTPREL16_LO_DS:
14163 case R_PPC64_GOT16_LO:
14164 case R_PPC64_GOT16_LO_DS:
14165 case R_PPC64_TOC16_LO:
14166 case R_PPC64_TOC16_LO_DS:
98528052 14167 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
560c8763 14168 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
67f0cbdb
AM
14169 {
14170 bfd_byte *p = contents + (rel->r_offset & ~3);
14171 insn = bfd_get_32 (input_bfd, p);
560c8763
AM
14172 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14173 {
14174 /* Transform addic to addi when we change reg. */
14175 insn &= ~((0x3f << 26) | (0x1f << 16));
14176 insn |= (14u << 26) | (2 << 16);
14177 }
14178 else
67f0cbdb 14179 {
98528052
AM
14180 insn &= ~(0x1f << 16);
14181 insn |= 2 << 16;
67f0cbdb 14182 }
560c8763 14183 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
14184 }
14185 break;
14186 }
14187
65f38f15
AM
14188 /* Do any further special processing. */
14189 switch (r_type)
14190 {
14191 default:
14192 break;
14193
25f23106 14194 case R_PPC64_REL16_HA:
f9c6b907
AM
14195 case R_PPC64_ADDR16_HA:
14196 case R_PPC64_ADDR16_HIGHA:
65f38f15
AM
14197 case R_PPC64_ADDR16_HIGHERA:
14198 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
14199 case R_PPC64_TOC16_HA:
14200 case R_PPC64_SECTOFF_HA:
411e1bfb 14201 case R_PPC64_TPREL16_HA:
f9c6b907 14202 case R_PPC64_TPREL16_HIGHA:
411e1bfb 14203 case R_PPC64_TPREL16_HIGHERA:
411e1bfb 14204 case R_PPC64_TPREL16_HIGHESTA:
f9c6b907
AM
14205 case R_PPC64_DTPREL16_HA:
14206 case R_PPC64_DTPREL16_HIGHA:
411e1bfb 14207 case R_PPC64_DTPREL16_HIGHERA:
411e1bfb 14208 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
14209 /* It's just possible that this symbol is a weak symbol
14210 that's not actually defined anywhere. In that case,
14211 'sec' would be NULL, and we should leave the symbol
14212 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
14213 if (sec == NULL)
14214 break;
14215 /* Fall thru */
14216
14217 case R_PPC64_GOT16_HA:
14218 case R_PPC64_PLTGOT16_HA:
14219 case R_PPC64_PLT16_HA:
14220 case R_PPC64_GOT_TLSGD16_HA:
14221 case R_PPC64_GOT_TLSLD16_HA:
14222 case R_PPC64_GOT_TPREL16_HA:
14223 case R_PPC64_GOT_DTPREL16_HA:
14224 /* Add 0x10000 if sign bit in 0:15 is set.
14225 Bits 0:15 are not used. */
14226 addend += 0x8000;
65f38f15
AM
14227 break;
14228
14229 case R_PPC64_ADDR16_DS:
14230 case R_PPC64_ADDR16_LO_DS:
14231 case R_PPC64_GOT16_DS:
14232 case R_PPC64_GOT16_LO_DS:
14233 case R_PPC64_PLT16_LO_DS:
14234 case R_PPC64_SECTOFF_DS:
14235 case R_PPC64_SECTOFF_LO_DS:
14236 case R_PPC64_TOC16_DS:
14237 case R_PPC64_TOC16_LO_DS:
14238 case R_PPC64_PLTGOT16_DS:
14239 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
14240 case R_PPC64_GOT_TPREL16_DS:
14241 case R_PPC64_GOT_TPREL16_LO_DS:
14242 case R_PPC64_GOT_DTPREL16_DS:
14243 case R_PPC64_GOT_DTPREL16_LO_DS:
14244 case R_PPC64_TPREL16_DS:
14245 case R_PPC64_TPREL16_LO_DS:
14246 case R_PPC64_DTPREL16_DS:
14247 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
14248 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14249 mask = 3;
14250 /* If this reloc is against an lq insn, then the value must be
14251 a multiple of 16. This is somewhat of a hack, but the
14252 "correct" way to do this by defining _DQ forms of all the
14253 _DS relocs bloats all reloc switches in this file. It
14254 doesn't seem to make much sense to use any of these relocs
14255 in data, so testing the insn should be safe. */
494dac0c 14256 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
14257 mask = 15;
14258 if (((relocation + addend) & mask) != 0)
65f38f15 14259 {
25f53a85 14260 info->callbacks->einfo
8de848d8 14261 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 14262 input_bfd, input_section, rel->r_offset,
4ce794b7 14263 ppc64_elf_howto_table[r_type]->name,
adadcc0c 14264 mask + 1);
65f38f15 14265 bfd_set_error (bfd_error_bad_value);
b34976b6 14266 ret = FALSE;
65f38f15
AM
14267 continue;
14268 }
14269 break;
5bd4f169
AM
14270 }
14271
239e1f3a
AM
14272 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14273 because such sections are not SEC_ALLOC and thus ld.so will
14274 not process them. */
65f38f15 14275 if (unresolved_reloc
239e1f3a 14276 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
14277 && h->elf.def_dynamic)
14278 && _bfd_elf_section_offset (output_bfd, info, input_section,
14279 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 14280 {
25f53a85 14281 info->callbacks->einfo
bc30df16 14282 (_("%P: %H: unresolvable %s against `%T'\n"),
25f53a85 14283 input_bfd, input_section, rel->r_offset,
7b609f53 14284 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 14285 h->elf.root.root.string);
b34976b6 14286 ret = FALSE;
9c07fe7c 14287 }
5bd4f169 14288
65f38f15 14289 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
14290 input_bfd,
14291 input_section,
14292 contents,
411e1bfb 14293 rel->r_offset,
5bd4f169
AM
14294 relocation,
14295 addend);
14296
ef60b7ff 14297 if (r != bfd_reloc_ok)
5bd4f169 14298 {
bc30df16
AM
14299 char *more_info = NULL;
14300 const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14301
14302 if (reloc_dest != DEST_NORMAL)
14303 {
14304 more_info = bfd_malloc (strlen (reloc_name) + 8);
14305 if (more_info != NULL)
14306 {
14307 strcpy (more_info, reloc_name);
14308 strcat (more_info, (reloc_dest == DEST_OPD
14309 ? " (OPD)" : " (stub)"));
14310 reloc_name = more_info;
14311 }
14312 }
14313
cd27b276 14314 if (r == bfd_reloc_overflow)
5bd4f169 14315 {
cd27b276
AM
14316 if (warned)
14317 continue;
14318 if (h != NULL
039b3fef 14319 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 14320 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
14321 {
14322 /* Assume this is a call protected by other code that
14323 detects the symbol is undefined. If this is the case,
14324 we can safely ignore the overflow. If not, the
14325 program is hosed anyway, and a little warning isn't
14326 going to help. */
14327
14328 continue;
14329 }
14330
ef60b7ff 14331 if (!((*info->callbacks->reloc_overflow)
bc30df16
AM
14332 (info, &h->elf.root, sym_name,
14333 reloc_name, orig_rel.r_addend,
14334 input_bfd, input_section, rel->r_offset)))
b34976b6 14335 return FALSE;
ef60b7ff
AM
14336 }
14337 else
14338 {
25f53a85 14339 info->callbacks->einfo
bc30df16 14340 (_("%P: %H: %s against `%T': error %d\n"),
25f53a85 14341 input_bfd, input_section, rel->r_offset,
bc30df16 14342 reloc_name, sym_name, (int) r);
b34976b6 14343 ret = FALSE;
ef60b7ff 14344 }
bc30df16
AM
14345 if (more_info != NULL)
14346 free (more_info);
5bd4f169 14347 }
5bd4f169
AM
14348 }
14349
645ea6a9
AM
14350 /* If we're emitting relocations, then shortly after this function
14351 returns, reloc offsets and addends for this section will be
14352 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
14353 file rather than the input. Save a copy of the relocs for
14354 opd_entry_value. */
4cc603a5 14355 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
14356 {
14357 bfd_size_type amt;
14358 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14359 rel = bfd_alloc (input_bfd, amt);
729eabd5
AM
14360 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14361 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
8860955f
AM
14362 if (rel == NULL)
14363 return FALSE;
14364 memcpy (rel, relocs, amt);
14365 }
5bd4f169
AM
14366 return ret;
14367}
14368
754021d0
AM
14369/* Adjust the value of any local symbols in opd sections. */
14370
6e0b88f1 14371static int
754021d0
AM
14372ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14373 const char *name ATTRIBUTE_UNUSED,
14374 Elf_Internal_Sym *elfsym,
14375 asection *input_sec,
14376 struct elf_link_hash_entry *h)
14377{
74f0fb50
AM
14378 struct _opd_sec_data *opd;
14379 long adjust;
754021d0
AM
14380 bfd_vma value;
14381
4025353c 14382 if (h != NULL)
6e0b88f1 14383 return 1;
4025353c 14384
74f0fb50
AM
14385 opd = get_opd_info (input_sec);
14386 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 14387 return 1;
754021d0
AM
14388
14389 value = elfsym->st_value - input_sec->output_offset;
14390 if (!info->relocatable)
14391 value -= input_sec->output_section->vma;
14392
74f0fb50 14393 adjust = opd->adjust[value / 8];
4025353c 14394 if (adjust == -1)
6e0b88f1
AM
14395 return 2;
14396
14397 elfsym->st_value += adjust;
14398 return 1;
754021d0
AM
14399}
14400
5bd4f169
AM
14401/* Finish up dynamic symbol handling. We set the contents of various
14402 dynamic sections here. */
14403
b34976b6 14404static bfd_boolean
4ce794b7
AM
14405ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14406 struct bfd_link_info *info,
14407 struct elf_link_hash_entry *h,
ab6dce23 14408 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5bd4f169 14409{
65f38f15 14410 struct ppc_link_hash_table *htab;
8387904d
AM
14411 struct plt_entry *ent;
14412 Elf_Internal_Rela rela;
14413 bfd_byte *loc;
5bd4f169 14414
65f38f15 14415 htab = ppc_hash_table (info);
4dfe6ac6
NC
14416 if (htab == NULL)
14417 return FALSE;
5bd4f169 14418
8387904d
AM
14419 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14420 if (ent->plt.offset != (bfd_vma) -1)
14421 {
14422 /* This symbol has an entry in the procedure linkage
14423 table. Set it up. */
e054468f
AM
14424 if (!htab->elf.dynamic_sections_created
14425 || h->dynindx == -1)
14426 {
14427 BFD_ASSERT (h->type == STT_GNU_IFUNC
14428 && h->def_regular
14429 && (h->root.type == bfd_link_hash_defined
14430 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
14431 rela.r_offset = (htab->iplt->output_section->vma
14432 + htab->iplt->output_offset
14433 + ent->plt.offset);
ee67d69a
AM
14434 if (htab->opd_abi)
14435 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14436 else
14437 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
e054468f
AM
14438 rela.r_addend = (h->root.u.def.value
14439 + h->root.u.def.section->output_offset
14440 + h->root.u.def.section->output_section->vma
14441 + ent->addend);
25f23106
AM
14442 loc = (htab->reliplt->contents
14443 + (htab->reliplt->reloc_count++
14444 * sizeof (Elf64_External_Rela)));
e054468f
AM
14445 }
14446 else
14447 {
25f23106
AM
14448 rela.r_offset = (htab->plt->output_section->vma
14449 + htab->plt->output_offset
14450 + ent->plt.offset);
e054468f
AM
14451 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14452 rela.r_addend = ent->addend;
25f23106
AM
14453 loc = (htab->relplt->contents
14454 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
14455 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 14456 }
8387904d
AM
14457 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14458 }
5bd4f169 14459
f5385ebf 14460 if (h->needs_copy)
5bd4f169 14461 {
65f38f15 14462 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 14463
65f38f15
AM
14464 if (h->dynindx == -1
14465 || (h->root.type != bfd_link_hash_defined
14466 && h->root.type != bfd_link_hash_defweak)
4ce794b7 14467 || htab->relbss == NULL)
65f38f15 14468 abort ();
5bd4f169
AM
14469
14470 rela.r_offset = (h->root.u.def.value
14471 + h->root.u.def.section->output_section->vma
14472 + h->root.u.def.section->output_offset);
14473 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14474 rela.r_addend = 0;
4ce794b7
AM
14475 loc = htab->relbss->contents;
14476 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 14477 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
14478 }
14479
b34976b6 14480 return TRUE;
5bd4f169
AM
14481}
14482
65f38f15
AM
14483/* Used to decide how to sort relocs in an optimal manner for the
14484 dynamic linker, before writing them out. */
14485
14486static enum elf_reloc_type_class
7e612e98
AM
14487ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14488 const asection *rel_sec,
14489 const Elf_Internal_Rela *rela)
65f38f15 14490{
04c9666a 14491 enum elf_ppc64_reloc_type r_type;
7e612e98
AM
14492 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14493
14494 if (rel_sec == htab->reliplt)
14495 return reloc_class_ifunc;
a33d1f77 14496
4ce794b7 14497 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 14498 switch (r_type)
65f38f15
AM
14499 {
14500 case R_PPC64_RELATIVE:
14501 return reloc_class_relative;
14502 case R_PPC64_JMP_SLOT:
14503 return reloc_class_plt;
14504 case R_PPC64_COPY:
14505 return reloc_class_copy;
14506 default:
14507 return reloc_class_normal;
14508 }
14509}
14510
5bd4f169
AM
14511/* Finish up the dynamic sections. */
14512
b34976b6 14513static bfd_boolean
4ce794b7
AM
14514ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14515 struct bfd_link_info *info)
5bd4f169 14516{
65f38f15
AM
14517 struct ppc_link_hash_table *htab;
14518 bfd *dynobj;
5bd4f169 14519 asection *sdyn;
5bd4f169 14520
65f38f15 14521 htab = ppc_hash_table (info);
4dfe6ac6
NC
14522 if (htab == NULL)
14523 return FALSE;
14524
65f38f15 14525 dynobj = htab->elf.dynobj;
3d4d4302 14526 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5bd4f169 14527
65f38f15 14528 if (htab->elf.dynamic_sections_created)
5bd4f169 14529 {
5bd4f169
AM
14530 Elf64_External_Dyn *dyncon, *dynconend;
14531
4ce794b7 14532 if (sdyn == NULL || htab->got == NULL)
65f38f15 14533 abort ();
5bd4f169
AM
14534
14535 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 14536 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
14537 for (; dyncon < dynconend; dyncon++)
14538 {
14539 Elf_Internal_Dyn dyn;
19397422 14540 asection *s;
5bd4f169
AM
14541
14542 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14543
14544 switch (dyn.d_tag)
14545 {
65f38f15
AM
14546 default:
14547 continue;
5bd4f169 14548
5d1634d7 14549 case DT_PPC64_GLINK:
4ce794b7 14550 s = htab->glink;
6348e046 14551 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
14552 /* We stupidly defined DT_PPC64_GLINK to be the start
14553 of glink rather than the first entry point, which is
14554 what ld.so needs, and now have a bigger stub to
14555 support automatic multiple TOCs. */
14556 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
14557 break;
14558
19397422
AM
14559 case DT_PPC64_OPD:
14560 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
14561 if (s == NULL)
14562 continue;
14563 dyn.d_un.d_ptr = s->vma;
19397422
AM
14564 break;
14565
14566 case DT_PPC64_OPDSZ:
14567 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
14568 if (s == NULL)
14569 continue;
eea6121a 14570 dyn.d_un.d_val = s->size;
19397422
AM
14571 break;
14572
65f38f15 14573 case DT_PLTGOT:
4ce794b7 14574 s = htab->plt;
6348e046 14575 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
14576 break;
14577
14578 case DT_JMPREL:
4ce794b7 14579 s = htab->relplt;
6348e046 14580 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 14581 break;
5bd4f169 14582
65f38f15 14583 case DT_PLTRELSZ:
eea6121a 14584 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
14585 break;
14586
14587 case DT_RELASZ:
14588 /* Don't count procedure linkage table relocs in the
14589 overall reloc count. */
4ce794b7 14590 s = htab->relplt;
6348e046
AM
14591 if (s == NULL)
14592 continue;
eea6121a 14593 dyn.d_un.d_val -= s->size;
6348e046
AM
14594 break;
14595
14596 case DT_RELA:
14597 /* We may not be using the standard ELF linker script.
14598 If .rela.plt is the first .rela section, we adjust
14599 DT_RELA to not include it. */
4ce794b7 14600 s = htab->relplt;
6348e046
AM
14601 if (s == NULL)
14602 continue;
14603 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14604 continue;
eea6121a 14605 dyn.d_un.d_ptr += s->size;
65f38f15 14606 break;
5bd4f169 14607 }
5bd4f169 14608
65f38f15 14609 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 14610 }
5bd4f169
AM
14611 }
14612
eea6121a 14613 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
14614 {
14615 /* Fill in the first entry in the global offset table.
14616 We use it to hold the link-time TOCbase. */
14617 bfd_put_64 (output_bfd,
60ee0d4a 14618 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 14619 htab->got->contents);
5d1634d7
AM
14620
14621 /* Set .got entry size. */
4ce794b7 14622 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
14623 }
14624
eea6121a 14625 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
14626 {
14627 /* Set .plt entry size. */
4ce794b7 14628 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
14629 = PLT_ENTRY_SIZE;
14630 }
14631
84f5d08e
AM
14632 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14633 brlt ourselves if emitrelocations. */
14634 if (htab->brlt != NULL
14635 && htab->brlt->reloc_count != 0
14636 && !_bfd_elf_link_output_relocs (output_bfd,
14637 htab->brlt,
d4730f92 14638 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
14639 elf_section_data (htab->brlt)->relocs,
14640 NULL))
14641 return FALSE;
14642
176a0d42
AM
14643 if (htab->glink != NULL
14644 && htab->glink->reloc_count != 0
14645 && !_bfd_elf_link_output_relocs (output_bfd,
14646 htab->glink,
d4730f92 14647 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
14648 elf_section_data (htab->glink)->relocs,
14649 NULL))
14650 return FALSE;
14651
58d180e8
AM
14652
14653 if (htab->glink_eh_frame != NULL
dbaa2011 14654 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
58d180e8
AM
14655 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14656 htab->glink_eh_frame,
14657 htab->glink_eh_frame->contents))
14658 return FALSE;
14659
e717da7e 14660 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
14661 since we didn't add them to DYNOBJ. We know dynobj is the first
14662 bfd. */
e717da7e
AM
14663 while ((dynobj = dynobj->link_next) != NULL)
14664 {
14665 asection *s;
7b53ace3 14666
0c8d6e5c 14667 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
14668 continue;
14669
e717da7e
AM
14670 s = ppc64_elf_tdata (dynobj)->got;
14671 if (s != NULL
eea6121a 14672 && s->size != 0
e717da7e
AM
14673 && s->output_section != bfd_abs_section_ptr
14674 && !bfd_set_section_contents (output_bfd, s->output_section,
14675 s->contents, s->output_offset,
eea6121a 14676 s->size))
e717da7e
AM
14677 return FALSE;
14678 s = ppc64_elf_tdata (dynobj)->relgot;
14679 if (s != NULL
eea6121a 14680 && s->size != 0
e717da7e
AM
14681 && s->output_section != bfd_abs_section_ptr
14682 && !bfd_set_section_contents (output_bfd, s->output_section,
14683 s->contents, s->output_offset,
eea6121a 14684 s->size))
e717da7e
AM
14685 return FALSE;
14686 }
f6c52c13 14687
b34976b6 14688 return TRUE;
5bd4f169
AM
14689}
14690
5bd4f169 14691#include "elf64-target.h"
7b8e7dad
AM
14692
14693/* FreeBSD support */
14694
14695#undef TARGET_LITTLE_SYM
14696#undef TARGET_LITTLE_NAME
14697
14698#undef TARGET_BIG_SYM
14699#define TARGET_BIG_SYM bfd_elf64_powerpc_freebsd_vec
14700#undef TARGET_BIG_NAME
14701#define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14702
14703#undef ELF_OSABI
14704#define ELF_OSABI ELFOSABI_FREEBSD
14705
14706#undef elf64_bed
14707#define elf64_bed elf64_powerpc_fbsd_bed
14708
14709#include "elf64-target.h"
14710
This page took 1.850074 seconds and 4 git commands to generate.