gdb/testsuite/
[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,
df22d223 3 2009, 2010, 2011 Free Software Foundation, Inc.
5bd4f169
AM
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 6 Largely rewritten by Alan Modra.
5bd4f169 7
ae9a127f 8 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 9
ae9a127f
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
ae9a127f 13 (at your option) any later version.
5bd4f169 14
ae9a127f
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
5bd4f169 19
4ce794b7
AM
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 23
cd123cb7 24
4ce794b7
AM
25/* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 28
3db64b00 29#include "sysdep.h"
183e98be 30#include <stdarg.h>
5bd4f169 31#include "bfd.h"
5bd4f169
AM
32#include "bfdlink.h"
33#include "libbfd.h"
34#include "elf-bfd.h"
04c9666a 35#include "elf/ppc64.h"
5d1634d7 36#include "elf64-ppc.h"
58d180e8 37#include "dwarf2.h"
5bd4f169 38
805fc799 39static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
41static bfd_reloc_status_type ppc64_elf_branch_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 43static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 45static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 47static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 49static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 51static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 53static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 55static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
57static bfd_vma opd_entry_value
58 (asection *, bfd_vma, asection **, bfd_vma *);
5bd4f169 59
ad8e1ba5
AM
60#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
61#define TARGET_LITTLE_NAME "elf64-powerpcle"
62#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
63#define TARGET_BIG_NAME "elf64-powerpc"
64#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 65#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
66#define ELF_MACHINE_CODE EM_PPC64
67#define ELF_MAXPAGESIZE 0x10000
24718e3b 68#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
69#define elf_info_to_howto ppc64_elf_info_to_howto
70
71#define elf_backend_want_got_sym 0
72#define elf_backend_want_plt_sym 0
73#define elf_backend_plt_alignment 3
74#define elf_backend_plt_not_loaded 1
ad8e1ba5 75#define elf_backend_got_header_size 8
ad8e1ba5
AM
76#define elf_backend_can_gc_sections 1
77#define elf_backend_can_refcount 1
78#define elf_backend_rela_normal 1
6bfdb61b 79#define elf_backend_default_execstack 0
ad8e1ba5 80
e717da7e 81#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 82#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 83#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
c79d6685 84#define bfd_elf64_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
ad8e1ba5
AM
85#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
86#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
87#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 88#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 89#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
90
91#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
92#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
93#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 94#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
95#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
96#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 97#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 98#define elf_backend_check_directives ppc64_elf_process_dot_syms
97fed1c9 99#define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
8387904d 100#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 101#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 102#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 103#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
104#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
105#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
106#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
107#define elf_backend_hide_symbol ppc64_elf_hide_symbol
108#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
109#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 110#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 111#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
112#define elf_backend_relocate_section ppc64_elf_relocate_section
113#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
114#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
115#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 116#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 117#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 118#define elf_backend_post_process_headers _bfd_elf_set_osabi
ad8e1ba5 119
5bd4f169
AM
120/* The name of the dynamic interpreter. This is put in the .interp
121 section. */
122#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
123
124/* The size in bytes of an entry in the procedure linkage table. */
125#define PLT_ENTRY_SIZE 24
126
127/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 128#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
129
130/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
131#define TOC_BASE_OFF 0x8000
132
133/* Offset of tp and dtp pointers from start of TLS block. */
134#define TP_OFFSET 0x7000
135#define DTP_OFFSET 0x8000
5bd4f169 136
ad8e1ba5
AM
137/* .plt call stub instructions. The normal stub is like this, but
138 sometimes the .plt entry crosses a 64k boundary and we need to
ac2df442 139 insert an addi to adjust r12. */
ad8e1ba5 140#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7
AM
141#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
142#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
143#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
5d1634d7 144#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
ac2df442 145#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
5d1634d7
AM
146 /* ld %r11,xxx+16@l(%r12) */
147#define BCTR 0x4e800420 /* bctr */
148
5d1634d7 149
ee4bf8d2 150#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
ac2df442 151#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
ad8e1ba5
AM
152#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
153#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
154
ac2df442
AM
155#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
156#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
157
ad8e1ba5
AM
158#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
159
ee4bf8d2 160/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 161#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
162 /* 0: */
163 /* .quad plt0-1f */
164 /* __glink: */
165#define MFLR_R12 0x7d8802a6 /* mflr %12 */
166#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
167 /* 1: */
168#define MFLR_R11 0x7d6802a6 /* mflr %11 */
169#define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
170#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
171#define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
172 /* ld %11,0(%12) */
173 /* ld %2,8(%12) */
174 /* mtctr %11 */
175 /* ld %11,16(%12) */
176 /* bctr */
5d1634d7
AM
177
178/* Pad with this. */
179#define NOP 0x60000000
180
721956f4
AM
181/* Some other nops. */
182#define CROR_151515 0x4def7b82
183#define CROR_313131 0x4ffffb82
184
cedb70c5 185/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
186#define LI_R0_0 0x38000000 /* li %r0,0 */
187#define B_DOT 0x48000000 /* b . */
188
189/* After that, we need two instructions to load the index, followed by
190 a branch. */
191#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 192#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 193
deb0e272
AM
194/* Instructions used by the save and restore reg functions. */
195#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
196#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
197#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
198#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
199#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
200#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
201#define LI_R12_0 0x39800000 /* li %r12,0 */
202#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
203#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
204#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
205#define BLR 0x4e800020 /* blr */
206
41bd81ab
AM
207/* Since .opd is an array of descriptors and each entry will end up
208 with identical R_PPC64_RELATIVE relocs, there is really no need to
209 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 210 relocate .opd without reloc entries. */
41bd81ab
AM
211#ifndef NO_OPD_RELOCS
212#define NO_OPD_RELOCS 0
213#endif
5bd4f169 214\f
f5e87a1d 215#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 216
5bd4f169 217/* Relocation HOWTO's. */
04c9666a 218static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
219
220static reloc_howto_type ppc64_elf_howto_raw[] = {
221 /* This reloc does nothing. */
222 HOWTO (R_PPC64_NONE, /* type */
223 0, /* rightshift */
411e1bfb
AM
224 2, /* size (0 = byte, 1 = short, 2 = long) */
225 32, /* bitsize */
b34976b6 226 FALSE, /* pc_relative */
5bd4f169 227 0, /* bitpos */
f5e87a1d 228 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
229 bfd_elf_generic_reloc, /* special_function */
230 "R_PPC64_NONE", /* name */
b34976b6 231 FALSE, /* partial_inplace */
d006db6c 232 0, /* src_mask */
5bd4f169 233 0, /* dst_mask */
b34976b6 234 FALSE), /* pcrel_offset */
5bd4f169
AM
235
236 /* A standard 32 bit relocation. */
237 HOWTO (R_PPC64_ADDR32, /* type */
238 0, /* rightshift */
239 2, /* size (0 = byte, 1 = short, 2 = long) */
240 32, /* bitsize */
b34976b6 241 FALSE, /* pc_relative */
5bd4f169
AM
242 0, /* bitpos */
243 complain_overflow_bitfield, /* complain_on_overflow */
244 bfd_elf_generic_reloc, /* special_function */
245 "R_PPC64_ADDR32", /* name */
b34976b6 246 FALSE, /* partial_inplace */
5bd4f169
AM
247 0, /* src_mask */
248 0xffffffff, /* dst_mask */
b34976b6 249 FALSE), /* pcrel_offset */
5bd4f169
AM
250
251 /* An absolute 26 bit branch; the lower two bits must be zero.
252 FIXME: we don't check that, we just clear them. */
253 HOWTO (R_PPC64_ADDR24, /* type */
254 0, /* rightshift */
255 2, /* size (0 = byte, 1 = short, 2 = long) */
256 26, /* bitsize */
b34976b6 257 FALSE, /* pc_relative */
5bd4f169
AM
258 0, /* bitpos */
259 complain_overflow_bitfield, /* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_PPC64_ADDR24", /* name */
b34976b6 262 FALSE, /* partial_inplace */
d006db6c 263 0, /* src_mask */
f5e87a1d 264 0x03fffffc, /* dst_mask */
b34976b6 265 FALSE), /* pcrel_offset */
5bd4f169
AM
266
267 /* A standard 16 bit relocation. */
268 HOWTO (R_PPC64_ADDR16, /* type */
269 0, /* rightshift */
270 1, /* size (0 = byte, 1 = short, 2 = long) */
271 16, /* bitsize */
b34976b6 272 FALSE, /* pc_relative */
5bd4f169
AM
273 0, /* bitpos */
274 complain_overflow_bitfield, /* complain_on_overflow */
275 bfd_elf_generic_reloc, /* special_function */
276 "R_PPC64_ADDR16", /* name */
b34976b6 277 FALSE, /* partial_inplace */
5bd4f169
AM
278 0, /* src_mask */
279 0xffff, /* dst_mask */
b34976b6 280 FALSE), /* pcrel_offset */
5bd4f169
AM
281
282 /* A 16 bit relocation without overflow. */
283 HOWTO (R_PPC64_ADDR16_LO, /* type */
284 0, /* rightshift */
285 1, /* size (0 = byte, 1 = short, 2 = long) */
286 16, /* bitsize */
b34976b6 287 FALSE, /* pc_relative */
5bd4f169
AM
288 0, /* bitpos */
289 complain_overflow_dont,/* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_PPC64_ADDR16_LO", /* name */
b34976b6 292 FALSE, /* partial_inplace */
5bd4f169
AM
293 0, /* src_mask */
294 0xffff, /* dst_mask */
b34976b6 295 FALSE), /* pcrel_offset */
5bd4f169
AM
296
297 /* Bits 16-31 of an address. */
298 HOWTO (R_PPC64_ADDR16_HI, /* type */
299 16, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 16, /* bitsize */
b34976b6 302 FALSE, /* pc_relative */
5bd4f169
AM
303 0, /* bitpos */
304 complain_overflow_dont, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_PPC64_ADDR16_HI", /* name */
b34976b6 307 FALSE, /* partial_inplace */
5bd4f169
AM
308 0, /* src_mask */
309 0xffff, /* dst_mask */
b34976b6 310 FALSE), /* pcrel_offset */
5bd4f169
AM
311
312 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
313 bits, treated as a signed number, is negative. */
314 HOWTO (R_PPC64_ADDR16_HA, /* type */
315 16, /* rightshift */
316 1, /* size (0 = byte, 1 = short, 2 = long) */
317 16, /* bitsize */
b34976b6 318 FALSE, /* pc_relative */
5bd4f169
AM
319 0, /* bitpos */
320 complain_overflow_dont, /* complain_on_overflow */
805fc799 321 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 322 "R_PPC64_ADDR16_HA", /* name */
b34976b6 323 FALSE, /* partial_inplace */
5bd4f169
AM
324 0, /* src_mask */
325 0xffff, /* dst_mask */
b34976b6 326 FALSE), /* pcrel_offset */
5bd4f169
AM
327
328 /* An absolute 16 bit branch; the lower two bits must be zero.
329 FIXME: we don't check that, we just clear them. */
330 HOWTO (R_PPC64_ADDR14, /* type */
331 0, /* rightshift */
332 2, /* size (0 = byte, 1 = short, 2 = long) */
333 16, /* bitsize */
b34976b6 334 FALSE, /* pc_relative */
5bd4f169
AM
335 0, /* bitpos */
336 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 337 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 338 "R_PPC64_ADDR14", /* name */
b34976b6 339 FALSE, /* partial_inplace */
d006db6c 340 0, /* src_mask */
f5e87a1d 341 0x0000fffc, /* dst_mask */
b34976b6 342 FALSE), /* pcrel_offset */
5bd4f169
AM
343
344 /* An absolute 16 bit branch, for which bit 10 should be set to
345 indicate that the branch is expected to be taken. The lower two
346 bits must be zero. */
347 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
348 0, /* rightshift */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
350 16, /* bitsize */
b34976b6 351 FALSE, /* pc_relative */
5bd4f169
AM
352 0, /* bitpos */
353 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 354 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 355 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 356 FALSE, /* partial_inplace */
d006db6c 357 0, /* src_mask */
f5e87a1d 358 0x0000fffc, /* dst_mask */
b34976b6 359 FALSE), /* pcrel_offset */
5bd4f169
AM
360
361 /* An absolute 16 bit branch, for which bit 10 should be set to
362 indicate that the branch is not expected to be taken. The lower
363 two bits must be zero. */
364 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
365 0, /* rightshift */
366 2, /* size (0 = byte, 1 = short, 2 = long) */
367 16, /* bitsize */
b34976b6 368 FALSE, /* pc_relative */
5bd4f169
AM
369 0, /* bitpos */
370 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 371 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 372 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 373 FALSE, /* partial_inplace */
d006db6c 374 0, /* src_mask */
f5e87a1d 375 0x0000fffc, /* dst_mask */
b34976b6 376 FALSE), /* pcrel_offset */
5bd4f169
AM
377
378 /* A relative 26 bit branch; the lower two bits must be zero. */
379 HOWTO (R_PPC64_REL24, /* type */
380 0, /* rightshift */
381 2, /* size (0 = byte, 1 = short, 2 = long) */
382 26, /* bitsize */
b34976b6 383 TRUE, /* pc_relative */
5bd4f169
AM
384 0, /* bitpos */
385 complain_overflow_signed, /* complain_on_overflow */
2441e016 386 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 387 "R_PPC64_REL24", /* name */
b34976b6 388 FALSE, /* partial_inplace */
d006db6c 389 0, /* src_mask */
f5e87a1d 390 0x03fffffc, /* dst_mask */
b34976b6 391 TRUE), /* pcrel_offset */
5bd4f169
AM
392
393 /* A relative 16 bit branch; the lower two bits must be zero. */
394 HOWTO (R_PPC64_REL14, /* type */
395 0, /* rightshift */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
397 16, /* bitsize */
b34976b6 398 TRUE, /* pc_relative */
5bd4f169
AM
399 0, /* bitpos */
400 complain_overflow_signed, /* complain_on_overflow */
2441e016 401 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 402 "R_PPC64_REL14", /* name */
b34976b6 403 FALSE, /* partial_inplace */
d006db6c 404 0, /* src_mask */
f5e87a1d 405 0x0000fffc, /* dst_mask */
b34976b6 406 TRUE), /* pcrel_offset */
5bd4f169
AM
407
408 /* A relative 16 bit branch. Bit 10 should be set to indicate that
409 the branch is expected to be taken. The lower two bits must be
410 zero. */
411 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
412 0, /* rightshift */
413 2, /* size (0 = byte, 1 = short, 2 = long) */
414 16, /* bitsize */
b34976b6 415 TRUE, /* pc_relative */
5bd4f169
AM
416 0, /* bitpos */
417 complain_overflow_signed, /* complain_on_overflow */
805fc799 418 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 419 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 420 FALSE, /* partial_inplace */
d006db6c 421 0, /* src_mask */
f5e87a1d 422 0x0000fffc, /* dst_mask */
b34976b6 423 TRUE), /* pcrel_offset */
5bd4f169
AM
424
425 /* A relative 16 bit branch. Bit 10 should be set to indicate that
426 the branch is not expected to be taken. The lower two bits must
427 be zero. */
428 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
429 0, /* rightshift */
430 2, /* size (0 = byte, 1 = short, 2 = long) */
431 16, /* bitsize */
b34976b6 432 TRUE, /* pc_relative */
5bd4f169
AM
433 0, /* bitpos */
434 complain_overflow_signed, /* complain_on_overflow */
805fc799 435 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 436 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 437 FALSE, /* partial_inplace */
d006db6c 438 0, /* src_mask */
f5e87a1d 439 0x0000fffc, /* dst_mask */
b34976b6 440 TRUE), /* pcrel_offset */
5bd4f169
AM
441
442 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
443 symbol. */
444 HOWTO (R_PPC64_GOT16, /* type */
445 0, /* rightshift */
446 1, /* size (0 = byte, 1 = short, 2 = long) */
447 16, /* bitsize */
b34976b6 448 FALSE, /* pc_relative */
5bd4f169
AM
449 0, /* bitpos */
450 complain_overflow_signed, /* complain_on_overflow */
805fc799 451 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 452 "R_PPC64_GOT16", /* name */
b34976b6 453 FALSE, /* partial_inplace */
5bd4f169
AM
454 0, /* src_mask */
455 0xffff, /* dst_mask */
b34976b6 456 FALSE), /* pcrel_offset */
5bd4f169
AM
457
458 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
459 the symbol. */
460 HOWTO (R_PPC64_GOT16_LO, /* type */
461 0, /* rightshift */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
463 16, /* bitsize */
b34976b6 464 FALSE, /* pc_relative */
5bd4f169
AM
465 0, /* bitpos */
466 complain_overflow_dont, /* complain_on_overflow */
805fc799 467 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 468 "R_PPC64_GOT16_LO", /* name */
b34976b6 469 FALSE, /* partial_inplace */
5bd4f169
AM
470 0, /* src_mask */
471 0xffff, /* dst_mask */
b34976b6 472 FALSE), /* pcrel_offset */
5bd4f169
AM
473
474 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
475 the symbol. */
476 HOWTO (R_PPC64_GOT16_HI, /* type */
477 16, /* rightshift */
478 1, /* size (0 = byte, 1 = short, 2 = long) */
479 16, /* bitsize */
b34976b6 480 FALSE, /* pc_relative */
5bd4f169
AM
481 0, /* bitpos */
482 complain_overflow_dont,/* complain_on_overflow */
805fc799 483 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 484 "R_PPC64_GOT16_HI", /* name */
b34976b6 485 FALSE, /* partial_inplace */
5bd4f169
AM
486 0, /* src_mask */
487 0xffff, /* dst_mask */
b34976b6 488 FALSE), /* pcrel_offset */
5bd4f169
AM
489
490 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
491 the symbol. */
492 HOWTO (R_PPC64_GOT16_HA, /* type */
493 16, /* rightshift */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
495 16, /* bitsize */
b34976b6 496 FALSE, /* pc_relative */
5bd4f169
AM
497 0, /* bitpos */
498 complain_overflow_dont,/* complain_on_overflow */
805fc799 499 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 500 "R_PPC64_GOT16_HA", /* name */
b34976b6 501 FALSE, /* partial_inplace */
5bd4f169
AM
502 0, /* src_mask */
503 0xffff, /* dst_mask */
b34976b6 504 FALSE), /* pcrel_offset */
5bd4f169
AM
505
506 /* This is used only by the dynamic linker. The symbol should exist
507 both in the object being run and in some shared library. The
508 dynamic linker copies the data addressed by the symbol from the
509 shared library into the object, because the object being
510 run has to have the data at some particular address. */
511 HOWTO (R_PPC64_COPY, /* type */
512 0, /* rightshift */
f5e87a1d
AM
513 0, /* this one is variable size */
514 0, /* bitsize */
b34976b6 515 FALSE, /* pc_relative */
5bd4f169 516 0, /* bitpos */
f5e87a1d
AM
517 complain_overflow_dont, /* complain_on_overflow */
518 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 519 "R_PPC64_COPY", /* name */
b34976b6 520 FALSE, /* partial_inplace */
5bd4f169
AM
521 0, /* src_mask */
522 0, /* dst_mask */
b34976b6 523 FALSE), /* pcrel_offset */
5bd4f169
AM
524
525 /* Like R_PPC64_ADDR64, but used when setting global offset table
526 entries. */
527 HOWTO (R_PPC64_GLOB_DAT, /* type */
528 0, /* rightshift */
529 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
530 64, /* bitsize */
b34976b6 531 FALSE, /* pc_relative */
5bd4f169
AM
532 0, /* bitpos */
533 complain_overflow_dont, /* complain_on_overflow */
805fc799 534 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 535 "R_PPC64_GLOB_DAT", /* name */
b34976b6 536 FALSE, /* partial_inplace */
5bd4f169 537 0, /* src_mask */
f5e87a1d 538 ONES (64), /* dst_mask */
b34976b6 539 FALSE), /* pcrel_offset */
5bd4f169
AM
540
541 /* Created by the link editor. Marks a procedure linkage table
542 entry for a symbol. */
543 HOWTO (R_PPC64_JMP_SLOT, /* type */
544 0, /* rightshift */
545 0, /* size (0 = byte, 1 = short, 2 = long) */
546 0, /* bitsize */
b34976b6 547 FALSE, /* pc_relative */
5bd4f169
AM
548 0, /* bitpos */
549 complain_overflow_dont, /* complain_on_overflow */
805fc799 550 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 551 "R_PPC64_JMP_SLOT", /* name */
b34976b6 552 FALSE, /* partial_inplace */
5bd4f169
AM
553 0, /* src_mask */
554 0, /* dst_mask */
b34976b6 555 FALSE), /* pcrel_offset */
5bd4f169
AM
556
557 /* Used only by the dynamic linker. When the object is run, this
558 doubleword64 is set to the load address of the object, plus the
559 addend. */
560 HOWTO (R_PPC64_RELATIVE, /* type */
561 0, /* rightshift */
562 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
563 64, /* bitsize */
b34976b6 564 FALSE, /* pc_relative */
5bd4f169
AM
565 0, /* bitpos */
566 complain_overflow_dont, /* complain_on_overflow */
567 bfd_elf_generic_reloc, /* special_function */
568 "R_PPC64_RELATIVE", /* name */
b34976b6 569 FALSE, /* partial_inplace */
5bd4f169 570 0, /* src_mask */
f5e87a1d 571 ONES (64), /* dst_mask */
b34976b6 572 FALSE), /* pcrel_offset */
5bd4f169
AM
573
574 /* Like R_PPC64_ADDR32, but may be unaligned. */
575 HOWTO (R_PPC64_UADDR32, /* type */
576 0, /* rightshift */
577 2, /* size (0 = byte, 1 = short, 2 = long) */
578 32, /* bitsize */
b34976b6 579 FALSE, /* pc_relative */
5bd4f169
AM
580 0, /* bitpos */
581 complain_overflow_bitfield, /* complain_on_overflow */
582 bfd_elf_generic_reloc, /* special_function */
583 "R_PPC64_UADDR32", /* name */
b34976b6 584 FALSE, /* partial_inplace */
5bd4f169
AM
585 0, /* src_mask */
586 0xffffffff, /* dst_mask */
b34976b6 587 FALSE), /* pcrel_offset */
5bd4f169
AM
588
589 /* Like R_PPC64_ADDR16, but may be unaligned. */
590 HOWTO (R_PPC64_UADDR16, /* type */
591 0, /* rightshift */
592 1, /* size (0 = byte, 1 = short, 2 = long) */
593 16, /* bitsize */
b34976b6 594 FALSE, /* pc_relative */
5bd4f169
AM
595 0, /* bitpos */
596 complain_overflow_bitfield, /* complain_on_overflow */
597 bfd_elf_generic_reloc, /* special_function */
598 "R_PPC64_UADDR16", /* name */
b34976b6 599 FALSE, /* partial_inplace */
5bd4f169
AM
600 0, /* src_mask */
601 0xffff, /* dst_mask */
b34976b6 602 FALSE), /* pcrel_offset */
5bd4f169
AM
603
604 /* 32-bit PC relative. */
605 HOWTO (R_PPC64_REL32, /* type */
606 0, /* rightshift */
607 2, /* size (0 = byte, 1 = short, 2 = long) */
608 32, /* bitsize */
b34976b6 609 TRUE, /* pc_relative */
5bd4f169 610 0, /* bitpos */
cedb70c5 611 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
612 complain_overflow_signed, /* complain_on_overflow */
613 bfd_elf_generic_reloc, /* special_function */
614 "R_PPC64_REL32", /* name */
b34976b6 615 FALSE, /* partial_inplace */
5bd4f169
AM
616 0, /* src_mask */
617 0xffffffff, /* dst_mask */
b34976b6 618 TRUE), /* pcrel_offset */
5bd4f169 619
10ed1bba 620 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
621 HOWTO (R_PPC64_PLT32, /* type */
622 0, /* rightshift */
623 2, /* size (0 = byte, 1 = short, 2 = long) */
624 32, /* bitsize */
b34976b6 625 FALSE, /* pc_relative */
5bd4f169
AM
626 0, /* bitpos */
627 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 628 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 629 "R_PPC64_PLT32", /* name */
b34976b6 630 FALSE, /* partial_inplace */
5bd4f169 631 0, /* src_mask */
f5e87a1d 632 0xffffffff, /* dst_mask */
b34976b6 633 FALSE), /* pcrel_offset */
5bd4f169
AM
634
635 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
636 FIXME: R_PPC64_PLTREL32 not supported. */
637 HOWTO (R_PPC64_PLTREL32, /* type */
638 0, /* rightshift */
639 2, /* size (0 = byte, 1 = short, 2 = long) */
640 32, /* bitsize */
b34976b6 641 TRUE, /* pc_relative */
5bd4f169
AM
642 0, /* bitpos */
643 complain_overflow_signed, /* complain_on_overflow */
644 bfd_elf_generic_reloc, /* special_function */
645 "R_PPC64_PLTREL32", /* name */
b34976b6 646 FALSE, /* partial_inplace */
5bd4f169 647 0, /* src_mask */
f5e87a1d 648 0xffffffff, /* dst_mask */
b34976b6 649 TRUE), /* pcrel_offset */
5bd4f169
AM
650
651 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
652 the symbol. */
653 HOWTO (R_PPC64_PLT16_LO, /* type */
654 0, /* rightshift */
655 1, /* size (0 = byte, 1 = short, 2 = long) */
656 16, /* bitsize */
b34976b6 657 FALSE, /* pc_relative */
5bd4f169
AM
658 0, /* bitpos */
659 complain_overflow_dont, /* complain_on_overflow */
805fc799 660 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 661 "R_PPC64_PLT16_LO", /* name */
b34976b6 662 FALSE, /* partial_inplace */
5bd4f169
AM
663 0, /* src_mask */
664 0xffff, /* dst_mask */
b34976b6 665 FALSE), /* pcrel_offset */
5bd4f169
AM
666
667 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
668 the symbol. */
669 HOWTO (R_PPC64_PLT16_HI, /* type */
670 16, /* rightshift */
671 1, /* size (0 = byte, 1 = short, 2 = long) */
672 16, /* bitsize */
b34976b6 673 FALSE, /* pc_relative */
5bd4f169
AM
674 0, /* bitpos */
675 complain_overflow_dont, /* complain_on_overflow */
805fc799 676 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 677 "R_PPC64_PLT16_HI", /* name */
b34976b6 678 FALSE, /* partial_inplace */
5bd4f169
AM
679 0, /* src_mask */
680 0xffff, /* dst_mask */
b34976b6 681 FALSE), /* pcrel_offset */
5bd4f169
AM
682
683 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
684 the symbol. */
685 HOWTO (R_PPC64_PLT16_HA, /* type */
686 16, /* rightshift */
687 1, /* size (0 = byte, 1 = short, 2 = long) */
688 16, /* bitsize */
b34976b6 689 FALSE, /* pc_relative */
5bd4f169
AM
690 0, /* bitpos */
691 complain_overflow_dont, /* complain_on_overflow */
805fc799 692 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 693 "R_PPC64_PLT16_HA", /* name */
b34976b6 694 FALSE, /* partial_inplace */
5bd4f169
AM
695 0, /* src_mask */
696 0xffff, /* dst_mask */
b34976b6 697 FALSE), /* pcrel_offset */
5bd4f169 698
c061c2d8 699 /* 16-bit section relative relocation. */
5bd4f169
AM
700 HOWTO (R_PPC64_SECTOFF, /* type */
701 0, /* rightshift */
c061c2d8
AM
702 1, /* size (0 = byte, 1 = short, 2 = long) */
703 16, /* bitsize */
b34976b6 704 FALSE, /* pc_relative */
5bd4f169
AM
705 0, /* bitpos */
706 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 707 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 708 "R_PPC64_SECTOFF", /* name */
b34976b6 709 FALSE, /* partial_inplace */
5bd4f169 710 0, /* src_mask */
c061c2d8 711 0xffff, /* dst_mask */
b34976b6 712 FALSE), /* pcrel_offset */
5bd4f169 713
c061c2d8 714 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
715 HOWTO (R_PPC64_SECTOFF_LO, /* type */
716 0, /* rightshift */
717 1, /* size (0 = byte, 1 = short, 2 = long) */
718 16, /* bitsize */
b34976b6 719 FALSE, /* pc_relative */
5bd4f169
AM
720 0, /* bitpos */
721 complain_overflow_dont, /* complain_on_overflow */
805fc799 722 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 723 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 724 FALSE, /* partial_inplace */
5bd4f169
AM
725 0, /* src_mask */
726 0xffff, /* dst_mask */
b34976b6 727 FALSE), /* pcrel_offset */
5bd4f169
AM
728
729 /* 16-bit upper half section relative relocation. */
730 HOWTO (R_PPC64_SECTOFF_HI, /* type */
731 16, /* rightshift */
732 1, /* size (0 = byte, 1 = short, 2 = long) */
733 16, /* bitsize */
b34976b6 734 FALSE, /* pc_relative */
5bd4f169
AM
735 0, /* bitpos */
736 complain_overflow_dont, /* complain_on_overflow */
805fc799 737 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 738 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 739 FALSE, /* partial_inplace */
5bd4f169
AM
740 0, /* src_mask */
741 0xffff, /* dst_mask */
b34976b6 742 FALSE), /* pcrel_offset */
5bd4f169
AM
743
744 /* 16-bit upper half adjusted section relative relocation. */
745 HOWTO (R_PPC64_SECTOFF_HA, /* type */
746 16, /* rightshift */
747 1, /* size (0 = byte, 1 = short, 2 = long) */
748 16, /* bitsize */
b34976b6 749 FALSE, /* pc_relative */
5bd4f169
AM
750 0, /* bitpos */
751 complain_overflow_dont, /* complain_on_overflow */
805fc799 752 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 753 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 754 FALSE, /* partial_inplace */
5bd4f169
AM
755 0, /* src_mask */
756 0xffff, /* dst_mask */
b34976b6 757 FALSE), /* pcrel_offset */
5bd4f169 758
04c9666a
AM
759 /* Like R_PPC64_REL24 without touching the two least significant bits. */
760 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
761 2, /* rightshift */
762 2, /* size (0 = byte, 1 = short, 2 = long) */
763 30, /* bitsize */
b34976b6 764 TRUE, /* pc_relative */
5bd4f169
AM
765 0, /* bitpos */
766 complain_overflow_dont, /* complain_on_overflow */
767 bfd_elf_generic_reloc, /* special_function */
04c9666a 768 "R_PPC64_REL30", /* name */
b34976b6 769 FALSE, /* partial_inplace */
d006db6c 770 0, /* src_mask */
5bd4f169 771 0xfffffffc, /* dst_mask */
b34976b6 772 TRUE), /* pcrel_offset */
5bd4f169
AM
773
774 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
775
776 /* A standard 64-bit relocation. */
777 HOWTO (R_PPC64_ADDR64, /* type */
778 0, /* rightshift */
779 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
780 64, /* bitsize */
b34976b6 781 FALSE, /* pc_relative */
5bd4f169
AM
782 0, /* bitpos */
783 complain_overflow_dont, /* complain_on_overflow */
784 bfd_elf_generic_reloc, /* special_function */
785 "R_PPC64_ADDR64", /* name */
b34976b6 786 FALSE, /* partial_inplace */
5bd4f169 787 0, /* src_mask */
f5e87a1d 788 ONES (64), /* dst_mask */
b34976b6 789 FALSE), /* pcrel_offset */
5bd4f169
AM
790
791 /* The bits 32-47 of an address. */
792 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
793 32, /* rightshift */
794 1, /* size (0 = byte, 1 = short, 2 = long) */
795 16, /* bitsize */
b34976b6 796 FALSE, /* pc_relative */
5bd4f169
AM
797 0, /* bitpos */
798 complain_overflow_dont, /* complain_on_overflow */
799 bfd_elf_generic_reloc, /* special_function */
800 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 801 FALSE, /* partial_inplace */
5bd4f169
AM
802 0, /* src_mask */
803 0xffff, /* dst_mask */
b34976b6 804 FALSE), /* pcrel_offset */
5bd4f169
AM
805
806 /* The bits 32-47 of an address, plus 1 if the contents of the low
807 16 bits, treated as a signed number, is negative. */
808 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
809 32, /* rightshift */
810 1, /* size (0 = byte, 1 = short, 2 = long) */
811 16, /* bitsize */
b34976b6 812 FALSE, /* pc_relative */
5bd4f169
AM
813 0, /* bitpos */
814 complain_overflow_dont, /* complain_on_overflow */
805fc799 815 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 816 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 817 FALSE, /* partial_inplace */
5bd4f169
AM
818 0, /* src_mask */
819 0xffff, /* dst_mask */
b34976b6 820 FALSE), /* pcrel_offset */
5bd4f169
AM
821
822 /* The bits 48-63 of an address. */
823 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
824 48, /* rightshift */
825 1, /* size (0 = byte, 1 = short, 2 = long) */
826 16, /* bitsize */
b34976b6 827 FALSE, /* pc_relative */
5bd4f169
AM
828 0, /* bitpos */
829 complain_overflow_dont, /* complain_on_overflow */
830 bfd_elf_generic_reloc, /* special_function */
831 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 832 FALSE, /* partial_inplace */
5bd4f169
AM
833 0, /* src_mask */
834 0xffff, /* dst_mask */
b34976b6 835 FALSE), /* pcrel_offset */
5bd4f169
AM
836
837 /* The bits 48-63 of an address, plus 1 if the contents of the low
838 16 bits, treated as a signed number, is negative. */
839 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
840 48, /* rightshift */
841 1, /* size (0 = byte, 1 = short, 2 = long) */
842 16, /* bitsize */
b34976b6 843 FALSE, /* pc_relative */
5bd4f169
AM
844 0, /* bitpos */
845 complain_overflow_dont, /* complain_on_overflow */
805fc799 846 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 847 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 848 FALSE, /* partial_inplace */
5bd4f169
AM
849 0, /* src_mask */
850 0xffff, /* dst_mask */
b34976b6 851 FALSE), /* pcrel_offset */
5bd4f169
AM
852
853 /* Like ADDR64, but may be unaligned. */
854 HOWTO (R_PPC64_UADDR64, /* type */
855 0, /* rightshift */
856 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
857 64, /* bitsize */
b34976b6 858 FALSE, /* pc_relative */
5bd4f169
AM
859 0, /* bitpos */
860 complain_overflow_dont, /* complain_on_overflow */
861 bfd_elf_generic_reloc, /* special_function */
862 "R_PPC64_UADDR64", /* name */
b34976b6 863 FALSE, /* partial_inplace */
5bd4f169 864 0, /* src_mask */
f5e87a1d 865 ONES (64), /* dst_mask */
b34976b6 866 FALSE), /* pcrel_offset */
5bd4f169
AM
867
868 /* 64-bit relative relocation. */
869 HOWTO (R_PPC64_REL64, /* type */
870 0, /* rightshift */
871 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
872 64, /* bitsize */
b34976b6 873 TRUE, /* pc_relative */
5bd4f169
AM
874 0, /* bitpos */
875 complain_overflow_dont, /* complain_on_overflow */
876 bfd_elf_generic_reloc, /* special_function */
877 "R_PPC64_REL64", /* name */
b34976b6 878 FALSE, /* partial_inplace */
5bd4f169 879 0, /* src_mask */
f5e87a1d 880 ONES (64), /* dst_mask */
b34976b6 881 TRUE), /* pcrel_offset */
5bd4f169 882
cedb70c5 883 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
884 HOWTO (R_PPC64_PLT64, /* type */
885 0, /* rightshift */
886 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
887 64, /* bitsize */
b34976b6 888 FALSE, /* pc_relative */
5bd4f169
AM
889 0, /* bitpos */
890 complain_overflow_dont, /* complain_on_overflow */
805fc799 891 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 892 "R_PPC64_PLT64", /* name */
b34976b6 893 FALSE, /* partial_inplace */
5bd4f169 894 0, /* src_mask */
f5e87a1d 895 ONES (64), /* dst_mask */
b34976b6 896 FALSE), /* pcrel_offset */
5bd4f169
AM
897
898 /* 64-bit PC relative relocation to the symbol's procedure linkage
899 table. */
900 /* FIXME: R_PPC64_PLTREL64 not supported. */
901 HOWTO (R_PPC64_PLTREL64, /* type */
902 0, /* rightshift */
903 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
904 64, /* bitsize */
b34976b6 905 TRUE, /* pc_relative */
5bd4f169
AM
906 0, /* bitpos */
907 complain_overflow_dont, /* complain_on_overflow */
805fc799 908 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 909 "R_PPC64_PLTREL64", /* name */
b34976b6 910 FALSE, /* partial_inplace */
5bd4f169 911 0, /* src_mask */
f5e87a1d 912 ONES (64), /* dst_mask */
b34976b6 913 TRUE), /* pcrel_offset */
5bd4f169
AM
914
915 /* 16 bit TOC-relative relocation. */
916
917 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
918 HOWTO (R_PPC64_TOC16, /* type */
919 0, /* rightshift */
920 1, /* size (0 = byte, 1 = short, 2 = long) */
921 16, /* bitsize */
b34976b6 922 FALSE, /* pc_relative */
5bd4f169
AM
923 0, /* bitpos */
924 complain_overflow_signed, /* complain_on_overflow */
805fc799 925 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 926 "R_PPC64_TOC16", /* name */
b34976b6 927 FALSE, /* partial_inplace */
5bd4f169
AM
928 0, /* src_mask */
929 0xffff, /* dst_mask */
b34976b6 930 FALSE), /* pcrel_offset */
5bd4f169
AM
931
932 /* 16 bit TOC-relative relocation without overflow. */
933
934 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
935 HOWTO (R_PPC64_TOC16_LO, /* type */
936 0, /* rightshift */
937 1, /* size (0 = byte, 1 = short, 2 = long) */
938 16, /* bitsize */
b34976b6 939 FALSE, /* pc_relative */
5bd4f169
AM
940 0, /* bitpos */
941 complain_overflow_dont, /* complain_on_overflow */
805fc799 942 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 943 "R_PPC64_TOC16_LO", /* name */
b34976b6 944 FALSE, /* partial_inplace */
5bd4f169
AM
945 0, /* src_mask */
946 0xffff, /* dst_mask */
b34976b6 947 FALSE), /* pcrel_offset */
5bd4f169
AM
948
949 /* 16 bit TOC-relative relocation, high 16 bits. */
950
951 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
952 HOWTO (R_PPC64_TOC16_HI, /* type */
953 16, /* rightshift */
954 1, /* size (0 = byte, 1 = short, 2 = long) */
955 16, /* bitsize */
b34976b6 956 FALSE, /* pc_relative */
5bd4f169
AM
957 0, /* bitpos */
958 complain_overflow_dont, /* complain_on_overflow */
805fc799 959 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 960 "R_PPC64_TOC16_HI", /* name */
b34976b6 961 FALSE, /* partial_inplace */
5bd4f169
AM
962 0, /* src_mask */
963 0xffff, /* dst_mask */
b34976b6 964 FALSE), /* pcrel_offset */
5bd4f169
AM
965
966 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
967 contents of the low 16 bits, treated as a signed number, is
968 negative. */
969
970 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
971 HOWTO (R_PPC64_TOC16_HA, /* type */
972 16, /* rightshift */
973 1, /* size (0 = byte, 1 = short, 2 = long) */
974 16, /* bitsize */
b34976b6 975 FALSE, /* pc_relative */
5bd4f169
AM
976 0, /* bitpos */
977 complain_overflow_dont, /* complain_on_overflow */
805fc799 978 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 979 "R_PPC64_TOC16_HA", /* name */
b34976b6 980 FALSE, /* partial_inplace */
5bd4f169
AM
981 0, /* src_mask */
982 0xffff, /* dst_mask */
b34976b6 983 FALSE), /* pcrel_offset */
5bd4f169
AM
984
985 /* 64-bit relocation; insert value of TOC base (.TOC.). */
986
987 /* R_PPC64_TOC 51 doubleword64 .TOC. */
988 HOWTO (R_PPC64_TOC, /* type */
989 0, /* rightshift */
990 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
991 64, /* bitsize */
b34976b6 992 FALSE, /* pc_relative */
5bd4f169
AM
993 0, /* bitpos */
994 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 995 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 996 "R_PPC64_TOC", /* name */
b34976b6 997 FALSE, /* partial_inplace */
5bd4f169 998 0, /* src_mask */
f5e87a1d 999 ONES (64), /* dst_mask */
b34976b6 1000 FALSE), /* pcrel_offset */
5bd4f169
AM
1001
1002 /* Like R_PPC64_GOT16, but also informs the link editor that the
1003 value to relocate may (!) refer to a PLT entry which the link
1004 editor (a) may replace with the symbol value. If the link editor
1005 is unable to fully resolve the symbol, it may (b) create a PLT
1006 entry and store the address to the new PLT entry in the GOT.
1007 This permits lazy resolution of function symbols at run time.
1008 The link editor may also skip all of this and just (c) emit a
1009 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1010 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1011 HOWTO (R_PPC64_PLTGOT16, /* type */
1012 0, /* rightshift */
1013 1, /* size (0 = byte, 1 = short, 2 = long) */
1014 16, /* bitsize */
b34976b6 1015 FALSE, /* pc_relative */
5bd4f169
AM
1016 0, /* bitpos */
1017 complain_overflow_signed, /* complain_on_overflow */
805fc799 1018 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1019 "R_PPC64_PLTGOT16", /* name */
1020 FALSE, /* partial_inplace */
1021 0, /* src_mask */
1022 0xffff, /* dst_mask */
1023 FALSE), /* pcrel_offset */
1024
1025 /* Like R_PPC64_PLTGOT16, but without overflow. */
1026 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1027 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1028 0, /* rightshift */
1029 1, /* size (0 = byte, 1 = short, 2 = long) */
1030 16, /* bitsize */
1031 FALSE, /* pc_relative */
1032 0, /* bitpos */
1033 complain_overflow_dont, /* complain_on_overflow */
1034 ppc64_elf_unhandled_reloc, /* special_function */
1035 "R_PPC64_PLTGOT16_LO", /* name */
1036 FALSE, /* partial_inplace */
1037 0, /* src_mask */
1038 0xffff, /* dst_mask */
1039 FALSE), /* pcrel_offset */
1040
1041 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1042 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1043 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1044 16, /* rightshift */
1045 1, /* size (0 = byte, 1 = short, 2 = long) */
1046 16, /* bitsize */
1047 FALSE, /* pc_relative */
1048 0, /* bitpos */
1049 complain_overflow_dont, /* complain_on_overflow */
1050 ppc64_elf_unhandled_reloc, /* special_function */
1051 "R_PPC64_PLTGOT16_HI", /* name */
1052 FALSE, /* partial_inplace */
1053 0, /* src_mask */
1054 0xffff, /* dst_mask */
1055 FALSE), /* pcrel_offset */
1056
1057 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1058 1 if the contents of the low 16 bits, treated as a signed number,
1059 is negative. */
1060 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1061 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1062 16, /* rightshift */
1063 1, /* size (0 = byte, 1 = short, 2 = long) */
1064 16, /* bitsize */
1065 FALSE, /* pc_relative */
1066 0, /* bitpos */
1067 complain_overflow_dont,/* complain_on_overflow */
1068 ppc64_elf_unhandled_reloc, /* special_function */
1069 "R_PPC64_PLTGOT16_HA", /* name */
1070 FALSE, /* partial_inplace */
1071 0, /* src_mask */
1072 0xffff, /* dst_mask */
1073 FALSE), /* pcrel_offset */
1074
1075 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1076 HOWTO (R_PPC64_ADDR16_DS, /* type */
1077 0, /* rightshift */
1078 1, /* size (0 = byte, 1 = short, 2 = long) */
1079 16, /* bitsize */
1080 FALSE, /* pc_relative */
1081 0, /* bitpos */
1082 complain_overflow_bitfield, /* complain_on_overflow */
1083 bfd_elf_generic_reloc, /* special_function */
1084 "R_PPC64_ADDR16_DS", /* name */
1085 FALSE, /* partial_inplace */
1086 0, /* src_mask */
1087 0xfffc, /* dst_mask */
1088 FALSE), /* pcrel_offset */
1089
1090 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1091 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1092 0, /* rightshift */
1093 1, /* size (0 = byte, 1 = short, 2 = long) */
1094 16, /* bitsize */
1095 FALSE, /* pc_relative */
1096 0, /* bitpos */
1097 complain_overflow_dont,/* complain_on_overflow */
1098 bfd_elf_generic_reloc, /* special_function */
1099 "R_PPC64_ADDR16_LO_DS",/* name */
1100 FALSE, /* partial_inplace */
1101 0, /* src_mask */
1102 0xfffc, /* dst_mask */
1103 FALSE), /* pcrel_offset */
1104
1105 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1106 HOWTO (R_PPC64_GOT16_DS, /* type */
1107 0, /* rightshift */
1108 1, /* size (0 = byte, 1 = short, 2 = long) */
1109 16, /* bitsize */
1110 FALSE, /* pc_relative */
1111 0, /* bitpos */
1112 complain_overflow_signed, /* complain_on_overflow */
1113 ppc64_elf_unhandled_reloc, /* special_function */
1114 "R_PPC64_GOT16_DS", /* name */
1115 FALSE, /* partial_inplace */
1116 0, /* src_mask */
1117 0xfffc, /* dst_mask */
1118 FALSE), /* pcrel_offset */
1119
1120 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1121 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1122 0, /* rightshift */
1123 1, /* size (0 = byte, 1 = short, 2 = long) */
1124 16, /* bitsize */
1125 FALSE, /* pc_relative */
1126 0, /* bitpos */
1127 complain_overflow_dont, /* complain_on_overflow */
1128 ppc64_elf_unhandled_reloc, /* special_function */
1129 "R_PPC64_GOT16_LO_DS", /* name */
1130 FALSE, /* partial_inplace */
1131 0, /* src_mask */
1132 0xfffc, /* dst_mask */
1133 FALSE), /* pcrel_offset */
1134
1135 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1136 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1137 0, /* rightshift */
1138 1, /* size (0 = byte, 1 = short, 2 = long) */
1139 16, /* bitsize */
1140 FALSE, /* pc_relative */
1141 0, /* bitpos */
1142 complain_overflow_dont, /* complain_on_overflow */
1143 ppc64_elf_unhandled_reloc, /* special_function */
1144 "R_PPC64_PLT16_LO_DS", /* name */
1145 FALSE, /* partial_inplace */
1146 0, /* src_mask */
1147 0xfffc, /* dst_mask */
1148 FALSE), /* pcrel_offset */
1149
1150 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1151 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1152 0, /* rightshift */
1153 1, /* size (0 = byte, 1 = short, 2 = long) */
1154 16, /* bitsize */
1155 FALSE, /* pc_relative */
1156 0, /* bitpos */
1157 complain_overflow_bitfield, /* complain_on_overflow */
1158 ppc64_elf_sectoff_reloc, /* special_function */
1159 "R_PPC64_SECTOFF_DS", /* name */
1160 FALSE, /* partial_inplace */
1161 0, /* src_mask */
1162 0xfffc, /* dst_mask */
1163 FALSE), /* pcrel_offset */
1164
1165 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1166 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1167 0, /* rightshift */
1168 1, /* size (0 = byte, 1 = short, 2 = long) */
1169 16, /* bitsize */
1170 FALSE, /* pc_relative */
1171 0, /* bitpos */
1172 complain_overflow_dont, /* complain_on_overflow */
1173 ppc64_elf_sectoff_reloc, /* special_function */
1174 "R_PPC64_SECTOFF_LO_DS",/* name */
1175 FALSE, /* partial_inplace */
1176 0, /* src_mask */
1177 0xfffc, /* dst_mask */
1178 FALSE), /* pcrel_offset */
1179
1180 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1181 HOWTO (R_PPC64_TOC16_DS, /* type */
1182 0, /* rightshift */
1183 1, /* size (0 = byte, 1 = short, 2 = long) */
1184 16, /* bitsize */
1185 FALSE, /* pc_relative */
1186 0, /* bitpos */
1187 complain_overflow_signed, /* complain_on_overflow */
1188 ppc64_elf_toc_reloc, /* special_function */
1189 "R_PPC64_TOC16_DS", /* name */
1190 FALSE, /* partial_inplace */
1191 0, /* src_mask */
1192 0xfffc, /* dst_mask */
1193 FALSE), /* pcrel_offset */
1194
1195 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1196 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1197 0, /* rightshift */
1198 1, /* size (0 = byte, 1 = short, 2 = long) */
1199 16, /* bitsize */
1200 FALSE, /* pc_relative */
1201 0, /* bitpos */
1202 complain_overflow_dont, /* complain_on_overflow */
1203 ppc64_elf_toc_reloc, /* special_function */
1204 "R_PPC64_TOC16_LO_DS", /* name */
1205 FALSE, /* partial_inplace */
1206 0, /* src_mask */
1207 0xfffc, /* dst_mask */
1208 FALSE), /* pcrel_offset */
1209
1210 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1211 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1212 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1213 0, /* rightshift */
1214 1, /* size (0 = byte, 1 = short, 2 = long) */
1215 16, /* bitsize */
1216 FALSE, /* pc_relative */
1217 0, /* bitpos */
1218 complain_overflow_signed, /* complain_on_overflow */
1219 ppc64_elf_unhandled_reloc, /* special_function */
1220 "R_PPC64_PLTGOT16_DS", /* name */
1221 FALSE, /* partial_inplace */
1222 0, /* src_mask */
1223 0xfffc, /* dst_mask */
1224 FALSE), /* pcrel_offset */
1225
1226 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1227 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1228 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1229 0, /* rightshift */
1230 1, /* size (0 = byte, 1 = short, 2 = long) */
1231 16, /* bitsize */
1232 FALSE, /* pc_relative */
1233 0, /* bitpos */
1234 complain_overflow_dont, /* complain_on_overflow */
1235 ppc64_elf_unhandled_reloc, /* special_function */
1236 "R_PPC64_PLTGOT16_LO_DS",/* name */
1237 FALSE, /* partial_inplace */
1238 0, /* src_mask */
1239 0xfffc, /* dst_mask */
1240 FALSE), /* pcrel_offset */
1241
727fc41e 1242 /* Marker relocs for TLS. */
411e1bfb
AM
1243 HOWTO (R_PPC64_TLS,
1244 0, /* rightshift */
1245 2, /* size (0 = byte, 1 = short, 2 = long) */
1246 32, /* bitsize */
1247 FALSE, /* pc_relative */
1248 0, /* bitpos */
1249 complain_overflow_dont, /* complain_on_overflow */
1250 bfd_elf_generic_reloc, /* special_function */
1251 "R_PPC64_TLS", /* name */
1252 FALSE, /* partial_inplace */
1253 0, /* src_mask */
1254 0, /* dst_mask */
1255 FALSE), /* pcrel_offset */
1256
727fc41e
AM
1257 HOWTO (R_PPC64_TLSGD,
1258 0, /* rightshift */
1259 2, /* size (0 = byte, 1 = short, 2 = long) */
1260 32, /* bitsize */
1261 FALSE, /* pc_relative */
1262 0, /* bitpos */
1263 complain_overflow_dont, /* complain_on_overflow */
1264 bfd_elf_generic_reloc, /* special_function */
1265 "R_PPC64_TLSGD", /* name */
1266 FALSE, /* partial_inplace */
1267 0, /* src_mask */
1268 0, /* dst_mask */
1269 FALSE), /* pcrel_offset */
1270
1271 HOWTO (R_PPC64_TLSLD,
1272 0, /* rightshift */
1273 2, /* size (0 = byte, 1 = short, 2 = long) */
1274 32, /* bitsize */
1275 FALSE, /* pc_relative */
1276 0, /* bitpos */
1277 complain_overflow_dont, /* complain_on_overflow */
1278 bfd_elf_generic_reloc, /* special_function */
1279 "R_PPC64_TLSLD", /* name */
1280 FALSE, /* partial_inplace */
1281 0, /* src_mask */
1282 0, /* dst_mask */
1283 FALSE), /* pcrel_offset */
1284
3b421ab3
AM
1285 HOWTO (R_PPC64_TOCSAVE,
1286 0, /* rightshift */
1287 2, /* size (0 = byte, 1 = short, 2 = long) */
1288 32, /* bitsize */
1289 FALSE, /* pc_relative */
1290 0, /* bitpos */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 bfd_elf_generic_reloc, /* special_function */
1293 "R_PPC64_TOCSAVE", /* name */
1294 FALSE, /* partial_inplace */
1295 0, /* src_mask */
1296 0, /* dst_mask */
1297 FALSE), /* pcrel_offset */
1298
411e1bfb
AM
1299 /* Computes the load module index of the load module that contains the
1300 definition of its TLS sym. */
1301 HOWTO (R_PPC64_DTPMOD64,
1302 0, /* rightshift */
1303 4, /* size (0 = byte, 1 = short, 2 = long) */
1304 64, /* bitsize */
1305 FALSE, /* pc_relative */
1306 0, /* bitpos */
1307 complain_overflow_dont, /* complain_on_overflow */
1308 ppc64_elf_unhandled_reloc, /* special_function */
1309 "R_PPC64_DTPMOD64", /* name */
1310 FALSE, /* partial_inplace */
1311 0, /* src_mask */
1312 ONES (64), /* dst_mask */
1313 FALSE), /* pcrel_offset */
1314
1315 /* Computes a dtv-relative displacement, the difference between the value
1316 of sym+add and the base address of the thread-local storage block that
1317 contains the definition of sym, minus 0x8000. */
1318 HOWTO (R_PPC64_DTPREL64,
1319 0, /* rightshift */
1320 4, /* size (0 = byte, 1 = short, 2 = long) */
1321 64, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_dont, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL64", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 ONES (64), /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 /* A 16 bit dtprel reloc. */
1333 HOWTO (R_PPC64_DTPREL16,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_signed, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 /* Like DTPREL16, but no overflow. */
1348 HOWTO (R_PPC64_DTPREL16_LO,
1349 0, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPREL16_LO", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HI,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc64_elf_unhandled_reloc, /* special_function */
1371 "R_PPC64_DTPREL16_HI", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1376
1377 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HA,
1379 16, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc64_elf_unhandled_reloc, /* special_function */
1386 "R_PPC64_DTPREL16_HA", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1391
1392 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHER,
1394 32, /* rightshift */
1395 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 16, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_dont, /* complain_on_overflow */
1400 ppc64_elf_unhandled_reloc, /* special_function */
1401 "R_PPC64_DTPREL16_HIGHER", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1409 32, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_dont, /* complain_on_overflow */
1415 ppc64_elf_unhandled_reloc, /* special_function */
1416 "R_PPC64_DTPREL16_HIGHERA", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1424 48, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 FALSE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont, /* complain_on_overflow */
1430 ppc64_elf_unhandled_reloc, /* special_function */
1431 "R_PPC64_DTPREL16_HIGHEST", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1436
1437 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1438 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1439 48, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_dont, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xffff, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451
1452 /* Like DTPREL16, but for insns with a DS field. */
1453 HOWTO (R_PPC64_DTPREL16_DS,
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_signed, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466
1467 /* Like DTPREL16_DS, but no overflow. */
1468 HOWTO (R_PPC64_DTPREL16_LO_DS,
1469 0, /* rightshift */
1470 1, /* size (0 = byte, 1 = short, 2 = long) */
1471 16, /* bitsize */
1472 FALSE, /* pc_relative */
1473 0, /* bitpos */
1474 complain_overflow_dont, /* complain_on_overflow */
1475 ppc64_elf_unhandled_reloc, /* special_function */
1476 "R_PPC64_DTPREL16_LO_DS", /* name */
1477 FALSE, /* partial_inplace */
1478 0, /* src_mask */
1479 0xfffc, /* dst_mask */
1480 FALSE), /* pcrel_offset */
1481
1482 /* Computes a tp-relative displacement, the difference between the value of
1483 sym+add and the value of the thread pointer (r13). */
1484 HOWTO (R_PPC64_TPREL64,
1485 0, /* rightshift */
1486 4, /* size (0 = byte, 1 = short, 2 = long) */
1487 64, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_dont, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL64", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 ONES (64), /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* A 16 bit tprel reloc. */
1499 HOWTO (R_PPC64_TPREL16,
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_signed, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512
1513 /* Like TPREL16, but no overflow. */
1514 HOWTO (R_PPC64_TPREL16_LO,
1515 0, /* rightshift */
1516 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 16, /* bitsize */
1518 FALSE, /* pc_relative */
1519 0, /* bitpos */
1520 complain_overflow_dont, /* complain_on_overflow */
1521 ppc64_elf_unhandled_reloc, /* special_function */
1522 "R_PPC64_TPREL16_LO", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527
1528 /* Like TPREL16_LO, but next higher group of 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HI,
1530 16, /* rightshift */
1531 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 16, /* bitsize */
1533 FALSE, /* pc_relative */
1534 0, /* bitpos */
1535 complain_overflow_dont, /* complain_on_overflow */
1536 ppc64_elf_unhandled_reloc, /* special_function */
1537 "R_PPC64_TPREL16_HI", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1542
1543 /* Like TPREL16_HI, but adjust for low 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HA,
1545 16, /* rightshift */
1546 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 16, /* bitsize */
1548 FALSE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 ppc64_elf_unhandled_reloc, /* special_function */
1552 "R_PPC64_TPREL16_HA", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557
1558 /* Like TPREL16_HI, but next higher group of 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHER,
1560 32, /* rightshift */
1561 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 16, /* bitsize */
1563 FALSE, /* pc_relative */
1564 0, /* bitpos */
1565 complain_overflow_dont, /* complain_on_overflow */
1566 ppc64_elf_unhandled_reloc, /* special_function */
1567 "R_PPC64_TPREL16_HIGHER", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572
1573 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHERA,
1575 32, /* rightshift */
1576 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 16, /* bitsize */
1578 FALSE, /* pc_relative */
1579 0, /* bitpos */
1580 complain_overflow_dont, /* complain_on_overflow */
1581 ppc64_elf_unhandled_reloc, /* special_function */
1582 "R_PPC64_TPREL16_HIGHERA", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1587
1588 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHEST,
1590 48, /* rightshift */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont, /* complain_on_overflow */
1596 ppc64_elf_unhandled_reloc, /* special_function */
1597 "R_PPC64_TPREL16_HIGHEST", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1602
1603 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1604 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1605 48, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_dont, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_HIGHESTA", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xffff, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1617
1618 /* Like TPREL16, but for insns with a DS field. */
1619 HOWTO (R_PPC64_TPREL16_DS,
1620 0, /* rightshift */
1621 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 16, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_signed, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1632
1633 /* Like TPREL16_DS, but no overflow. */
1634 HOWTO (R_PPC64_TPREL16_LO_DS,
1635 0, /* rightshift */
1636 1, /* size (0 = byte, 1 = short, 2 = long) */
1637 16, /* bitsize */
1638 FALSE, /* pc_relative */
1639 0, /* bitpos */
1640 complain_overflow_dont, /* complain_on_overflow */
1641 ppc64_elf_unhandled_reloc, /* special_function */
1642 "R_PPC64_TPREL16_LO_DS", /* name */
1643 FALSE, /* partial_inplace */
1644 0, /* src_mask */
1645 0xfffc, /* dst_mask */
1646 FALSE), /* pcrel_offset */
1647
1648 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1649 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1650 to the first entry relative to the TOC base (r2). */
1651 HOWTO (R_PPC64_GOT_TLSGD16,
1652 0, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
1655 FALSE, /* pc_relative */
1656 0, /* bitpos */
1657 complain_overflow_signed, /* complain_on_overflow */
1658 ppc64_elf_unhandled_reloc, /* special_function */
1659 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1660 FALSE, /* partial_inplace */
5bd4f169
AM
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
b34976b6 1663 FALSE), /* pcrel_offset */
5bd4f169 1664
411e1bfb
AM
1665 /* Like GOT_TLSGD16, but no overflow. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1667 0, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
b34976b6 1670 FALSE, /* pc_relative */
5bd4f169
AM
1671 0, /* bitpos */
1672 complain_overflow_dont, /* complain_on_overflow */
805fc799 1673 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1674 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1675 FALSE, /* partial_inplace */
5bd4f169
AM
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
b34976b6 1678 FALSE), /* pcrel_offset */
5bd4f169 1679
411e1bfb
AM
1680 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1682 16, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
b34976b6 1685 FALSE, /* pc_relative */
5bd4f169
AM
1686 0, /* bitpos */
1687 complain_overflow_dont, /* complain_on_overflow */
805fc799 1688 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1689 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1690 FALSE, /* partial_inplace */
5bd4f169
AM
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
b34976b6 1693 FALSE), /* pcrel_offset */
5bd4f169 1694
411e1bfb
AM
1695 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1696 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1697 16, /* rightshift */
1698 1, /* size (0 = byte, 1 = short, 2 = long) */
1699 16, /* bitsize */
b34976b6 1700 FALSE, /* pc_relative */
5bd4f169 1701 0, /* bitpos */
411e1bfb 1702 complain_overflow_dont, /* complain_on_overflow */
805fc799 1703 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1704 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1705 FALSE, /* partial_inplace */
5bd4f169
AM
1706 0, /* src_mask */
1707 0xffff, /* dst_mask */
b34976b6 1708 FALSE), /* pcrel_offset */
5bd4f169 1709
411e1bfb
AM
1710 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1711 with values (sym+add)@dtpmod and zero, and computes the offset to the
1712 first entry relative to the TOC base (r2). */
1713 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
b34976b6 1717 FALSE, /* pc_relative */
5bd4f169 1718 0, /* bitpos */
411e1bfb
AM
1719 complain_overflow_signed, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1722 FALSE, /* partial_inplace */
d006db6c 1723 0, /* src_mask */
411e1bfb 1724 0xffff, /* dst_mask */
b34976b6 1725 FALSE), /* pcrel_offset */
5bd4f169 1726
411e1bfb
AM
1727 /* Like GOT_TLSLD16, but no overflow. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1729 0, /* rightshift */
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
b34976b6 1732 FALSE, /* pc_relative */
5bd4f169 1733 0, /* bitpos */
411e1bfb
AM
1734 complain_overflow_dont, /* complain_on_overflow */
1735 ppc64_elf_unhandled_reloc, /* special_function */
1736 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1737 FALSE, /* partial_inplace */
d006db6c 1738 0, /* src_mask */
411e1bfb 1739 0xffff, /* dst_mask */
b34976b6 1740 FALSE), /* pcrel_offset */
5bd4f169 1741
411e1bfb
AM
1742 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1744 16, /* rightshift */
5bd4f169
AM
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
b34976b6 1747 FALSE, /* pc_relative */
5bd4f169 1748 0, /* bitpos */
411e1bfb 1749 complain_overflow_dont, /* complain_on_overflow */
805fc799 1750 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1751 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1752 FALSE, /* partial_inplace */
d006db6c 1753 0, /* src_mask */
411e1bfb 1754 0xffff, /* dst_mask */
b34976b6 1755 FALSE), /* pcrel_offset */
5bd4f169 1756
411e1bfb
AM
1757 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1758 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1759 16, /* rightshift */
5bd4f169
AM
1760 1, /* size (0 = byte, 1 = short, 2 = long) */
1761 16, /* bitsize */
b34976b6 1762 FALSE, /* pc_relative */
5bd4f169
AM
1763 0, /* bitpos */
1764 complain_overflow_dont, /* complain_on_overflow */
805fc799 1765 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1766 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1767 FALSE, /* partial_inplace */
d006db6c 1768 0, /* src_mask */
411e1bfb 1769 0xffff, /* dst_mask */
b34976b6 1770 FALSE), /* pcrel_offset */
5bd4f169 1771
411e1bfb
AM
1772 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1773 the offset to the entry relative to the TOC base (r2). */
1774 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1775 0, /* rightshift */
1776 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 16, /* bitsize */
b34976b6 1778 FALSE, /* pc_relative */
5bd4f169 1779 0, /* bitpos */
411e1bfb 1780 complain_overflow_signed, /* complain_on_overflow */
805fc799 1781 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1782 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1783 FALSE, /* partial_inplace */
d006db6c 1784 0, /* src_mask */
5bd4f169 1785 0xfffc, /* dst_mask */
b34976b6 1786 FALSE), /* pcrel_offset */
5bd4f169 1787
411e1bfb
AM
1788 /* Like GOT_DTPREL16_DS, but no overflow. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1790 0, /* rightshift */
c061c2d8
AM
1791 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 16, /* bitsize */
b34976b6 1793 FALSE, /* pc_relative */
5bd4f169 1794 0, /* bitpos */
411e1bfb
AM
1795 complain_overflow_dont, /* complain_on_overflow */
1796 ppc64_elf_unhandled_reloc, /* special_function */
1797 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1798 FALSE, /* partial_inplace */
d006db6c 1799 0, /* src_mask */
c061c2d8 1800 0xfffc, /* dst_mask */
b34976b6 1801 FALSE), /* pcrel_offset */
5bd4f169 1802
411e1bfb
AM
1803 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1805 16, /* rightshift */
5bd4f169
AM
1806 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 16, /* bitsize */
b34976b6 1808 FALSE, /* pc_relative */
5bd4f169
AM
1809 0, /* bitpos */
1810 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1811 ppc64_elf_unhandled_reloc, /* special_function */
1812 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1813 FALSE, /* partial_inplace */
d006db6c 1814 0, /* src_mask */
411e1bfb 1815 0xffff, /* dst_mask */
b34976b6 1816 FALSE), /* pcrel_offset */
5bd4f169 1817
411e1bfb
AM
1818 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1819 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1820 16, /* rightshift */
1821 1, /* size (0 = byte, 1 = short, 2 = long) */
1822 16, /* bitsize */
1823 FALSE, /* pc_relative */
1824 0, /* bitpos */
1825 complain_overflow_dont, /* complain_on_overflow */
1826 ppc64_elf_unhandled_reloc, /* special_function */
1827 "R_PPC64_GOT_DTPREL16_HA", /* name */
1828 FALSE, /* partial_inplace */
1829 0, /* src_mask */
1830 0xffff, /* dst_mask */
1831 FALSE), /* pcrel_offset */
1832
1833 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1834 offset to the entry relative to the TOC base (r2). */
1835 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1836 0, /* rightshift */
1837 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 16, /* bitsize */
b34976b6 1839 FALSE, /* pc_relative */
5bd4f169
AM
1840 0, /* bitpos */
1841 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1844 FALSE, /* partial_inplace */
d006db6c 1845 0, /* src_mask */
ad8e1ba5 1846 0xfffc, /* dst_mask */
b34976b6 1847 FALSE), /* pcrel_offset */
5bd4f169 1848
411e1bfb
AM
1849 /* Like GOT_TPREL16_DS, but no overflow. */
1850 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1851 0, /* rightshift */
1852 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 16, /* bitsize */
b34976b6 1854 FALSE, /* pc_relative */
5bd4f169
AM
1855 0, /* bitpos */
1856 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1857 ppc64_elf_unhandled_reloc, /* special_function */
1858 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1859 FALSE, /* partial_inplace */
d006db6c 1860 0, /* src_mask */
ad8e1ba5 1861 0xfffc, /* dst_mask */
b34976b6 1862 FALSE), /* pcrel_offset */
5bd4f169 1863
411e1bfb
AM
1864 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HI,
1866 16, /* rightshift */
5bd4f169
AM
1867 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 16, /* bitsize */
b34976b6 1869 FALSE, /* pc_relative */
5bd4f169 1870 0, /* bitpos */
411e1bfb 1871 complain_overflow_dont, /* complain_on_overflow */
805fc799 1872 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1873 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1874 FALSE, /* partial_inplace */
d006db6c 1875 0, /* src_mask */
411e1bfb 1876 0xffff, /* dst_mask */
b34976b6 1877 FALSE), /* pcrel_offset */
5bd4f169 1878
411e1bfb
AM
1879 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1880 HOWTO (R_PPC64_GOT_TPREL16_HA,
1881 16, /* rightshift */
5bd4f169
AM
1882 1, /* size (0 = byte, 1 = short, 2 = long) */
1883 16, /* bitsize */
b34976b6 1884 FALSE, /* pc_relative */
5bd4f169
AM
1885 0, /* bitpos */
1886 complain_overflow_dont, /* complain_on_overflow */
805fc799 1887 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1888 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1889 FALSE, /* partial_inplace */
d006db6c 1890 0, /* src_mask */
411e1bfb 1891 0xffff, /* dst_mask */
b34976b6 1892 FALSE), /* pcrel_offset */
5bd4f169 1893
25f23106
AM
1894 HOWTO (R_PPC64_JMP_IREL, /* type */
1895 0, /* rightshift */
1896 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1897 0, /* bitsize */
1898 FALSE, /* pc_relative */
1899 0, /* bitpos */
1900 complain_overflow_dont, /* complain_on_overflow */
1901 ppc64_elf_unhandled_reloc, /* special_function */
1902 "R_PPC64_JMP_IREL", /* name */
1903 FALSE, /* partial_inplace */
1904 0, /* src_mask */
1905 0, /* dst_mask */
1906 FALSE), /* pcrel_offset */
1907
e054468f
AM
1908 HOWTO (R_PPC64_IRELATIVE, /* type */
1909 0, /* rightshift */
1910 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1911 64, /* bitsize */
1912 FALSE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_dont, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_IRELATIVE", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 ONES (64), /* dst_mask */
1920 FALSE), /* pcrel_offset */
1921
25f23106
AM
1922 /* A 16 bit relative relocation. */
1923 HOWTO (R_PPC64_REL16, /* type */
1924 0, /* rightshift */
1925 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 TRUE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_bitfield, /* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1936
1937 /* A 16 bit relative relocation without overflow. */
1938 HOWTO (R_PPC64_REL16_LO, /* type */
1939 0, /* rightshift */
1940 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 TRUE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont,/* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 "R_PPC64_REL16_LO", /* name */
1947 FALSE, /* partial_inplace */
1948 0, /* src_mask */
1949 0xffff, /* dst_mask */
1950 TRUE), /* pcrel_offset */
1951
1952 /* The high order 16 bits of a relative address. */
1953 HOWTO (R_PPC64_REL16_HI, /* type */
1954 16, /* rightshift */
1955 1, /* size (0 = byte, 1 = short, 2 = long) */
1956 16, /* bitsize */
1957 TRUE, /* pc_relative */
1958 0, /* bitpos */
1959 complain_overflow_dont, /* complain_on_overflow */
1960 bfd_elf_generic_reloc, /* special_function */
1961 "R_PPC64_REL16_HI", /* name */
1962 FALSE, /* partial_inplace */
1963 0, /* src_mask */
1964 0xffff, /* dst_mask */
1965 TRUE), /* pcrel_offset */
1966
1967 /* The high order 16 bits of a relative address, plus 1 if the contents of
1968 the low 16 bits, treated as a signed number, is negative. */
1969 HOWTO (R_PPC64_REL16_HA, /* type */
1970 16, /* rightshift */
1971 1, /* size (0 = byte, 1 = short, 2 = long) */
1972 16, /* bitsize */
1973 TRUE, /* pc_relative */
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 ppc64_elf_ha_reloc, /* special_function */
1977 "R_PPC64_REL16_HA", /* name */
1978 FALSE, /* partial_inplace */
1979 0, /* src_mask */
1980 0xffff, /* dst_mask */
1981 TRUE), /* pcrel_offset */
1982
5bd4f169
AM
1983 /* GNU extension to record C++ vtable hierarchy. */
1984 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1985 0, /* rightshift */
1986 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 0, /* bitsize */
b34976b6 1988 FALSE, /* pc_relative */
5bd4f169
AM
1989 0, /* bitpos */
1990 complain_overflow_dont, /* complain_on_overflow */
1991 NULL, /* special_function */
1992 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 1993 FALSE, /* partial_inplace */
5bd4f169
AM
1994 0, /* src_mask */
1995 0, /* dst_mask */
b34976b6 1996 FALSE), /* pcrel_offset */
5bd4f169
AM
1997
1998 /* GNU extension to record C++ vtable member usage. */
1999 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2000 0, /* rightshift */
2001 0, /* size (0 = byte, 1 = short, 2 = long) */
2002 0, /* bitsize */
b34976b6 2003 FALSE, /* pc_relative */
5bd4f169
AM
2004 0, /* bitpos */
2005 complain_overflow_dont, /* complain_on_overflow */
2006 NULL, /* special_function */
2007 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 2008 FALSE, /* partial_inplace */
5bd4f169
AM
2009 0, /* src_mask */
2010 0, /* dst_mask */
b34976b6 2011 FALSE), /* pcrel_offset */
5bd4f169
AM
2012};
2013
2014\f
2015/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2016 be done. */
2017
2018static void
4ce794b7 2019ppc_howto_init (void)
5bd4f169
AM
2020{
2021 unsigned int i, type;
2022
2023 for (i = 0;
2024 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2025 i++)
2026 {
2027 type = ppc64_elf_howto_raw[i].type;
2028 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2029 / sizeof (ppc64_elf_howto_table[0])));
2030 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2031 }
2032}
2033
2034static reloc_howto_type *
4ce794b7
AM
2035ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2036 bfd_reloc_code_real_type code)
5bd4f169 2037{
411e1bfb 2038 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2039
2040 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2041 /* Initialize howto table if needed. */
2042 ppc_howto_init ();
2043
4ce794b7 2044 switch (code)
5bd4f169
AM
2045 {
2046 default:
4ce794b7 2047 return NULL;
5bd4f169 2048
411e1bfb
AM
2049 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2050 break;
2051 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2052 break;
2053 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2054 break;
2055 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2056 break;
2057 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2058 break;
2059 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2060 break;
2061 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2062 break;
411e1bfb 2063 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2064 break;
411e1bfb 2065 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2066 break;
411e1bfb 2067 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2068 break;
411e1bfb 2069 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2070 break;
411e1bfb 2071 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2072 break;
411e1bfb 2073 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2074 break;
411e1bfb 2075 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2076 break;
411e1bfb 2077 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2078 break;
411e1bfb 2079 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2080 break;
411e1bfb 2081 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2082 break;
411e1bfb 2083 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2084 break;
411e1bfb 2085 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2086 break;
411e1bfb 2087 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2088 break;
411e1bfb 2089 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2090 break;
411e1bfb 2091 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2092 break;
411e1bfb 2093 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2094 break;
411e1bfb 2095 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2096 break;
411e1bfb 2097 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2098 break;
411e1bfb 2099 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2100 break;
411e1bfb 2101 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2102 break;
411e1bfb 2103 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2104 break;
411e1bfb 2105 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2106 break;
411e1bfb 2107 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2108 break;
411e1bfb 2109 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2110 break;
411e1bfb 2111 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2112 break;
411e1bfb 2113 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2114 break;
411e1bfb 2115 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2116 break;
411e1bfb 2117 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2118 break;
411e1bfb 2119 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2120 break;
411e1bfb 2121 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2122 break;
411e1bfb 2123 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2124 break;
411e1bfb 2125 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2126 break;
411e1bfb 2127 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2128 break;
411e1bfb 2129 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2130 break;
411e1bfb 2131 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2132 break;
411e1bfb 2133 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2134 break;
411e1bfb 2135 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2136 break;
411e1bfb 2137 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2138 break;
411e1bfb 2139 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2140 break;
411e1bfb 2141 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2142 break;
411e1bfb 2143 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2144 break;
411e1bfb 2145 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2146 break;
411e1bfb 2147 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2148 break;
411e1bfb 2149 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2150 break;
411e1bfb 2151 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2152 break;
411e1bfb 2153 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2154 break;
411e1bfb 2155 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2156 break;
411e1bfb 2157 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2158 break;
411e1bfb 2159 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2160 break;
411e1bfb 2161 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2162 break;
411e1bfb 2163 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2164 break;
411e1bfb 2165 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2166 break;
411e1bfb 2167 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2168 break;
727fc41e
AM
2169 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2170 break;
2171 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2172 break;
411e1bfb 2173 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2174 break;
411e1bfb 2175 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2176 break;
411e1bfb 2177 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2178 break;
411e1bfb 2179 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2180 break;
411e1bfb 2181 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2182 break;
411e1bfb 2183 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2184 break;
411e1bfb
AM
2185 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2186 break;
2187 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2188 break;
2189 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2190 break;
2191 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2192 break;
2193 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2194 break;
2195 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2196 break;
2197 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2198 break;
2199 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2200 break;
2201 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2202 break;
2203 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2204 break;
2205 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2206 break;
2207 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2208 break;
2209 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2210 break;
2211 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2212 break;
2213 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2214 break;
2215 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2216 break;
2217 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2218 break;
2219 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2220 break;
2221 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2222 break;
2223 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2224 break;
2225 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2226 break;
2227 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2228 break;
2229 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2230 break;
2231 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2232 break;
2233 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2234 break;
2235 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2236 break;
2237 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2238 break;
2239 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2240 break;
2241 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2242 break;
2243 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2244 break;
2245 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2246 break;
2247 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2248 break;
2249 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2250 break;
25f23106
AM
2251 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2252 break;
2253 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2254 break;
2255 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2256 break;
2257 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2258 break;
411e1bfb
AM
2259 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2260 break;
2261 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2262 break;
2263 }
2264
4ce794b7 2265 return ppc64_elf_howto_table[r];
5bd4f169
AM
2266};
2267
157090f7
AM
2268static reloc_howto_type *
2269ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2270 const char *r_name)
2271{
2272 unsigned int i;
2273
2274 for (i = 0;
2275 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2276 i++)
2277 if (ppc64_elf_howto_raw[i].name != NULL
2278 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2279 return &ppc64_elf_howto_raw[i];
2280
2281 return NULL;
2282}
2283
5bd4f169
AM
2284/* Set the howto pointer for a PowerPC ELF reloc. */
2285
2286static void
4ce794b7
AM
2287ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2288 Elf_Internal_Rela *dst)
5bd4f169 2289{
65f38f15
AM
2290 unsigned int type;
2291
ef60b7ff 2292 /* Initialize howto table if needed. */
5bd4f169 2293 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2294 ppc_howto_init ();
2295
65f38f15 2296 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2297 if (type >= (sizeof (ppc64_elf_howto_table)
2298 / sizeof (ppc64_elf_howto_table[0])))
2299 {
2300 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2301 abfd, (int) type);
2302 type = R_PPC64_NONE;
d0fb9a8d 2303 }
65f38f15 2304 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2305}
2306
04c9666a 2307/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2308
2309static bfd_reloc_status_type
4ce794b7
AM
2310ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2311 void *data, asection *input_section,
2312 bfd *output_bfd, char **error_message)
5bd4f169 2313{
805fc799
AM
2314 /* If this is a relocatable link (output_bfd test tells us), just
2315 call the generic function. Any adjustment will be done at final
2316 link time. */
2317 if (output_bfd != NULL)
cedb70c5 2318 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2319 input_section, output_bfd, error_message);
2320
2321 /* Adjust the addend for sign extension of the low 16 bits.
2322 We won't actually be using the low 16 bits, so trashing them
2323 doesn't matter. */
2324 reloc_entry->addend += 0x8000;
2325 return bfd_reloc_continue;
2326}
5bd4f169 2327
2441e016
AM
2328static bfd_reloc_status_type
2329ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2330 void *data, asection *input_section,
2331 bfd *output_bfd, char **error_message)
2332{
2333 if (output_bfd != NULL)
2334 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2335 input_section, output_bfd, error_message);
2336
699733f6
AM
2337 if (strcmp (symbol->section->name, ".opd") == 0
2338 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2339 {
2340 bfd_vma dest = opd_entry_value (symbol->section,
2341 symbol->value + reloc_entry->addend,
2342 NULL, NULL);
2343 if (dest != (bfd_vma) -1)
2344 reloc_entry->addend = dest - (symbol->value
2345 + symbol->section->output_section->vma
2346 + symbol->section->output_offset);
2347 }
2348 return bfd_reloc_continue;
2349}
2350
805fc799 2351static bfd_reloc_status_type
4ce794b7
AM
2352ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2353 void *data, asection *input_section,
2354 bfd *output_bfd, char **error_message)
805fc799
AM
2355{
2356 long insn;
04c9666a 2357 enum elf_ppc64_reloc_type r_type;
805fc799
AM
2358 bfd_size_type octets;
2359 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 2360 bfd_boolean is_power4 = FALSE;
805fc799
AM
2361
2362 /* If this is a relocatable link (output_bfd test tells us), just
2363 call the generic function. Any adjustment will be done at final
2364 link time. */
5bd4f169 2365 if (output_bfd != NULL)
cedb70c5 2366 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2367 input_section, output_bfd, error_message);
2368
2369 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2370 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2371 insn &= ~(0x01 << 21);
4ce794b7 2372 r_type = reloc_entry->howto->type;
805fc799
AM
2373 if (r_type == R_PPC64_ADDR14_BRTAKEN
2374 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2375 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799
AM
2376
2377 if (is_power4)
5bd4f169 2378 {
805fc799
AM
2379 /* Set 'a' bit. This is 0b00010 in BO field for branch
2380 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2381 for branch on CTR insns (BO == 1a00t or 1a01t). */
2382 if ((insn & (0x14 << 21)) == (0x04 << 21))
2383 insn |= 0x02 << 21;
2384 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2385 insn |= 0x08 << 21;
2386 else
2441e016 2387 goto out;
5bd4f169 2388 }
805fc799
AM
2389 else
2390 {
2391 bfd_vma target = 0;
2392 bfd_vma from;
5bd4f169 2393
805fc799
AM
2394 if (!bfd_is_com_section (symbol->section))
2395 target = symbol->value;
2396 target += symbol->section->output_section->vma;
2397 target += symbol->section->output_offset;
2398 target += reloc_entry->addend;
5bd4f169 2399
805fc799
AM
2400 from = (reloc_entry->address
2401 + input_section->output_offset
2402 + input_section->output_section->vma);
5bd4f169 2403
805fc799
AM
2404 /* Invert 'y' bit if not the default. */
2405 if ((bfd_signed_vma) (target - from) < 0)
2406 insn ^= 0x01 << 21;
2407 }
4ce794b7 2408 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2409 out:
2410 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2411 input_section, output_bfd, error_message);
805fc799 2412}
5bd4f169 2413
805fc799 2414static bfd_reloc_status_type
4ce794b7
AM
2415ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2416 void *data, asection *input_section,
2417 bfd *output_bfd, char **error_message)
805fc799
AM
2418{
2419 /* If this is a relocatable link (output_bfd test tells us), just
2420 call the generic function. Any adjustment will be done at final
2421 link time. */
2422 if (output_bfd != NULL)
cedb70c5 2423 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2424 input_section, output_bfd, error_message);
5bd4f169 2425
805fc799
AM
2426 /* Subtract the symbol section base address. */
2427 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2428 return bfd_reloc_continue;
2429}
2430
805fc799 2431static bfd_reloc_status_type
4ce794b7
AM
2432ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2433 void *data, asection *input_section,
2434 bfd *output_bfd, char **error_message)
805fc799
AM
2435{
2436 /* If this is a relocatable link (output_bfd test tells us), just
2437 call the generic function. Any adjustment will be done at final
2438 link time. */
2439 if (output_bfd != NULL)
cedb70c5 2440 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2441 input_section, output_bfd, error_message);
2442
2443 /* Subtract the symbol section base address. */
2444 reloc_entry->addend -= symbol->section->output_section->vma;
2445
2446 /* Adjust the addend for sign extension of the low 16 bits. */
2447 reloc_entry->addend += 0x8000;
2448 return bfd_reloc_continue;
2449}
2450
2451static bfd_reloc_status_type
4ce794b7
AM
2452ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2453 void *data, asection *input_section,
2454 bfd *output_bfd, char **error_message)
805fc799
AM
2455{
2456 bfd_vma TOCstart;
2457
2458 /* If this is a relocatable link (output_bfd test tells us), just
2459 call the generic function. Any adjustment will be done at final
2460 link time. */
2461 if (output_bfd != NULL)
cedb70c5 2462 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2463 input_section, output_bfd, error_message);
2464
2465 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2466 if (TOCstart == 0)
2467 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2468
2469 /* Subtract the TOC base address. */
2470 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2471 return bfd_reloc_continue;
2472}
2473
2474static bfd_reloc_status_type
4ce794b7
AM
2475ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2476 void *data, asection *input_section,
2477 bfd *output_bfd, char **error_message)
805fc799
AM
2478{
2479 bfd_vma TOCstart;
2480
2481 /* If this is a relocatable link (output_bfd test tells us), just
2482 call the generic function. Any adjustment will be done at final
2483 link time. */
2484 if (output_bfd != NULL)
cedb70c5 2485 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2486 input_section, output_bfd, error_message);
2487
2488 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2489 if (TOCstart == 0)
2490 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2491
2492 /* Subtract the TOC base address. */
2493 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2494
2495 /* Adjust the addend for sign extension of the low 16 bits. */
2496 reloc_entry->addend += 0x8000;
2497 return bfd_reloc_continue;
2498}
2499
2500static bfd_reloc_status_type
4ce794b7
AM
2501ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2502 void *data, asection *input_section,
2503 bfd *output_bfd, char **error_message)
805fc799
AM
2504{
2505 bfd_vma TOCstart;
2506 bfd_size_type octets;
2507
2508 /* If this is a relocatable link (output_bfd test tells us), just
2509 call the generic function. Any adjustment will be done at final
2510 link time. */
2511 if (output_bfd != NULL)
cedb70c5 2512 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2513 input_section, output_bfd, error_message);
2514
2515 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2516 if (TOCstart == 0)
2517 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2518
2519 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2520 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2521 return bfd_reloc_ok;
2522}
2523
2524static bfd_reloc_status_type
4ce794b7
AM
2525ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2526 void *data, asection *input_section,
2527 bfd *output_bfd, char **error_message)
805fc799
AM
2528{
2529 /* If this is a relocatable link (output_bfd test tells us), just
2530 call the generic function. Any adjustment will be done at final
2531 link time. */
2532 if (output_bfd != NULL)
cedb70c5 2533 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2534 input_section, output_bfd, error_message);
2535
2536 if (error_message != NULL)
2537 {
2538 static char buf[60];
2539 sprintf (buf, "generic linker can't handle %s",
2540 reloc_entry->howto->name);
2541 *error_message = buf;
2542 }
2543 return bfd_reloc_dangerous;
2544}
2545
927be08e
AM
2546/* Track GOT entries needed for a given symbol. We might need more
2547 than one got entry per symbol. */
2548struct got_entry
2549{
2550 struct got_entry *next;
2551
2552 /* The symbol addend that we'll be placing in the GOT. */
2553 bfd_vma addend;
2554
2555 /* Unlike other ELF targets, we use separate GOT entries for the same
2556 symbol referenced from different input files. This is to support
2557 automatic multiple TOC/GOT sections, where the TOC base can vary
2558 from one input file to another. After partitioning into TOC groups
2559 we merge entries within the group.
2560
2561 Point to the BFD owning this GOT entry. */
2562 bfd *owner;
2563
2564 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2565 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2566 unsigned char tls_type;
927be08e
AM
2567
2568 /* Non-zero if got.ent points to real entry. */
f961d9dd 2569 unsigned char is_indirect;
927be08e
AM
2570
2571 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2572 union
2573 {
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 struct got_entry *ent;
2577 } got;
2578};
2579
2580/* The same for PLT. */
2581struct plt_entry
2582{
2583 struct plt_entry *next;
2584
2585 bfd_vma addend;
2586
2587 union
2588 {
2589 bfd_signed_vma refcount;
2590 bfd_vma offset;
2591 } plt;
2592};
2593
e717da7e
AM
2594struct ppc64_elf_obj_tdata
2595{
2596 struct elf_obj_tdata elf;
2597
2598 /* Shortcuts to dynamic linker sections. */
2599 asection *got;
2600 asection *relgot;
2601
b3fac117
AM
2602 /* Used during garbage collection. We attach global symbols defined
2603 on removed .opd entries to this section so that the sym is removed. */
2604 asection *deleted_section;
81688140 2605
927be08e 2606 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2607 sections means we potentially need one of these for each input bfd. */
927be08e 2608 struct got_entry tlsld_got;
8860955f
AM
2609
2610 /* A copy of relocs before they are modified for --emit-relocs. */
2611 Elf_Internal_Rela *opd_relocs;
d77c8a4b
AM
2612
2613 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2614 the reloc to be in the range -32768 to 32767. */
98528052
AM
2615 unsigned int has_small_toc_reloc : 1;
2616
2617 /* Set if toc/got ha relocs detected not using r2. */
2618 unsigned int ha_relocs_not_using_r2 : 1;
e717da7e
AM
2619};
2620
2621#define ppc64_elf_tdata(bfd) \
2622 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2623
2624#define ppc64_tlsld_got(bfd) \
2625 (&ppc64_elf_tdata (bfd)->tlsld_got)
2626
0c8d6e5c
AM
2627#define is_ppc64_elf(bfd) \
2628 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2629 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2630
e717da7e
AM
2631/* Override the generic function because we store some extras. */
2632
2633static bfd_boolean
2634ppc64_elf_mkobject (bfd *abfd)
2635{
0ffa91dd 2636 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2637 PPC64_ELF_DATA);
e717da7e
AM
2638}
2639
feee612b
AM
2640/* Fix bad default arch selected for a 64 bit input bfd when the
2641 default is 32 bit. */
2642
b34976b6 2643static bfd_boolean
4ce794b7 2644ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2645{
2646 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2647 {
2648 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2649
2650 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2651 {
2652 /* Relies on arch after 32 bit default being 64 bit default. */
2653 abfd->arch_info = abfd->arch_info->next;
2654 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2655 }
2656 }
b34976b6 2657 return TRUE;
feee612b
AM
2658}
2659
d37c89e5
AM
2660/* Support for core dump NOTE sections. */
2661
2662static bfd_boolean
2663ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2664{
eea6121a 2665 size_t offset, size;
d37c89e5
AM
2666
2667 if (note->descsz != 504)
2668 return FALSE;
2669
2670 /* pr_cursig */
2671 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2672
2673 /* pr_pid */
261b8d08 2674 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2675
2676 /* pr_reg */
2677 offset = 112;
eea6121a 2678 size = 384;
d37c89e5
AM
2679
2680 /* Make a ".reg/999" section. */
2681 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2682 size, note->descpos + offset);
d37c89e5
AM
2683}
2684
2685static bfd_boolean
2686ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2687{
2688 if (note->descsz != 136)
2689 return FALSE;
2690
bc989cdc
JK
2691 elf_tdata (abfd)->core_pid
2692 = bfd_get_32 (abfd, note->descdata + 24);
d37c89e5
AM
2693 elf_tdata (abfd)->core_program
2694 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2695 elf_tdata (abfd)->core_command
2696 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2697
2698 return TRUE;
2699}
2700
183e98be
AM
2701static char *
2702ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2703 ...)
2704{
2705 switch (note_type)
2706 {
2707 default:
2708 return NULL;
2709
2710 case NT_PRPSINFO:
2711 {
2712 char data[136];
2713 va_list ap;
2714
2715 va_start (ap, note_type);
2716 memset (data, 0, 40);
2717 strncpy (data + 40, va_arg (ap, const char *), 16);
2718 strncpy (data + 56, va_arg (ap, const char *), 80);
2719 va_end (ap);
2720 return elfcore_write_note (abfd, buf, bufsiz,
2721 "CORE", note_type, data, sizeof (data));
2722 }
2723
2724 case NT_PRSTATUS:
2725 {
2726 char data[504];
2727 va_list ap;
2728 long pid;
2729 int cursig;
2730 const void *greg;
2731
2732 va_start (ap, note_type);
2733 memset (data, 0, 112);
2734 pid = va_arg (ap, long);
2735 bfd_put_32 (abfd, pid, data + 32);
2736 cursig = va_arg (ap, int);
2737 bfd_put_16 (abfd, cursig, data + 12);
2738 greg = va_arg (ap, const void *);
2739 memcpy (data + 112, greg, 384);
2740 memset (data + 496, 0, 8);
2741 va_end (ap);
2742 return elfcore_write_note (abfd, buf, bufsiz,
2743 "CORE", note_type, data, sizeof (data));
2744 }
2745 }
2746}
2747
5d35169e
AM
2748/* Add extra PPC sections. */
2749
b35d266b 2750static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2751{
0112cd26
NC
2752 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2753 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2754 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2755 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2756 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2757 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2758 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2759};
2760
7c8fe5c4
AM
2761enum _ppc64_sec_type {
2762 sec_normal = 0,
2763 sec_opd = 1,
2764 sec_toc = 2
2765};
2766
f0abc2a1
AM
2767struct _ppc64_elf_section_data
2768{
2769 struct bfd_elf_section_data elf;
411e1bfb 2770
f0abc2a1
AM
2771 union
2772 {
74f0fb50
AM
2773 /* An array with one entry for each opd function descriptor. */
2774 struct _opd_sec_data
2775 {
2776 /* Points to the function code section for local opd entries. */
2777 asection **func_sec;
2778
2779 /* After editing .opd, adjust references to opd local syms. */
2780 long *adjust;
2781 } opd;
7c8fe5c4 2782
3a71aa26
AM
2783 /* An array for toc sections, indexed by offset/8. */
2784 struct _toc_sec_data
2785 {
2786 /* Specifies the relocation symbol index used at a given toc offset. */
2787 unsigned *symndx;
2788
2789 /* And the relocation addend. */
2790 bfd_vma *add;
2791 } toc;
7c8fe5c4
AM
2792 } u;
2793
2794 enum _ppc64_sec_type sec_type:2;
411e1bfb 2795
7c8fe5c4
AM
2796 /* Flag set when small branches are detected. Used to
2797 select suitable defaults for the stub group size. */
2798 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2799};
2800
2801#define ppc64_elf_section_data(sec) \
411e1bfb 2802 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2803
2804static bfd_boolean
4ce794b7 2805ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2806{
f592407e
AM
2807 if (!sec->used_by_bfd)
2808 {
2809 struct _ppc64_elf_section_data *sdata;
2810 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2811
f592407e
AM
2812 sdata = bfd_zalloc (abfd, amt);
2813 if (sdata == NULL)
2814 return FALSE;
2815 sec->used_by_bfd = sdata;
2816 }
f0abc2a1
AM
2817
2818 return _bfd_elf_new_section_hook (abfd, sec);
2819}
4025353c 2820
74f0fb50 2821static struct _opd_sec_data *
4025353c
AM
2822get_opd_info (asection * sec)
2823{
2824 if (sec != NULL
2825 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2826 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2827 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2828 return NULL;
2829}
90e3cdf2
JJ
2830\f
2831/* Parameters for the qsort hook. */
90e3cdf2
JJ
2832static bfd_boolean synthetic_relocatable;
2833
699733f6 2834/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2835
2836static int
2837compare_symbols (const void *ap, const void *bp)
2838{
2839 const asymbol *a = * (const asymbol **) ap;
2840 const asymbol *b = * (const asymbol **) bp;
2841
699733f6
AM
2842 /* Section symbols first. */
2843 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2844 return -1;
699733f6 2845 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2846 return 1;
2847
699733f6 2848 /* then .opd symbols. */
ffcfec52
AM
2849 if (strcmp (a->section->name, ".opd") == 0
2850 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2851 return -1;
ffcfec52
AM
2852 if (strcmp (a->section->name, ".opd") != 0
2853 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2854 return 1;
2855
699733f6 2856 /* then other code symbols. */
90e3cdf2
JJ
2857 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2858 == (SEC_CODE | SEC_ALLOC)
2859 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2860 != (SEC_CODE | SEC_ALLOC))
2861 return -1;
2862
2863 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2864 != (SEC_CODE | SEC_ALLOC)
2865 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2866 == (SEC_CODE | SEC_ALLOC))
2867 return 1;
2868
2869 if (synthetic_relocatable)
2870 {
2871 if (a->section->id < b->section->id)
2872 return -1;
2873
2874 if (a->section->id > b->section->id)
2875 return 1;
2876 }
2877
2878 if (a->value + a->section->vma < b->value + b->section->vma)
2879 return -1;
2880
2881 if (a->value + a->section->vma > b->value + b->section->vma)
2882 return 1;
2883
4d35a0aa
AM
2884 /* For syms with the same value, prefer strong dynamic global function
2885 syms over other syms. */
2886 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2887 return -1;
2888
2889 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2890 return 1;
2891
2892 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2893 return -1;
2894
2895 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2896 return 1;
2897
2898 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2899 return -1;
2900
2901 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2902 return 1;
2903
2904 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2905 return -1;
2906
2907 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2908 return 1;
2909
90e3cdf2
JJ
2910 return 0;
2911}
2912
699733f6 2913/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2914
699733f6
AM
2915static asymbol *
2916sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2917{
699733f6 2918 long mid;
90e3cdf2 2919
699733f6
AM
2920 if (id == -1)
2921 {
2922 while (lo < hi)
2923 {
2924 mid = (lo + hi) >> 1;
2925 if (syms[mid]->value + syms[mid]->section->vma < value)
2926 lo = mid + 1;
2927 else if (syms[mid]->value + syms[mid]->section->vma > value)
2928 hi = mid;
2929 else
2930 return syms[mid];
2931 }
2932 }
2933 else
2934 {
2935 while (lo < hi)
2936 {
2937 mid = (lo + hi) >> 1;
2938 if (syms[mid]->section->id < id)
2939 lo = mid + 1;
2940 else if (syms[mid]->section->id > id)
2941 hi = mid;
2942 else if (syms[mid]->value < value)
2943 lo = mid + 1;
2944 else if (syms[mid]->value > value)
2945 hi = mid;
2946 else
2947 return syms[mid];
2948 }
2949 }
2950 return NULL;
90e3cdf2
JJ
2951}
2952
468392fb
AM
2953static bfd_boolean
2954section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2955{
2956 bfd_vma vma = *(bfd_vma *) ptr;
2957 return ((section->flags & SEC_ALLOC) != 0
2958 && section->vma <= vma
2959 && vma < section->vma + section->size);
2960}
2961
699733f6 2962/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 2963 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
2964
2965static long
a7535cf3
AM
2966ppc64_elf_get_synthetic_symtab (bfd *abfd,
2967 long static_count, asymbol **static_syms,
2968 long dyn_count, asymbol **dyn_syms,
c9727e01 2969 asymbol **ret)
90e3cdf2
JJ
2970{
2971 asymbol *s;
699733f6
AM
2972 long i;
2973 long count;
90e3cdf2 2974 char *names;
a7535cf3 2975 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2976 asection *opd;
90e3cdf2 2977 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2978 asymbol **syms;
90e3cdf2
JJ
2979
2980 *ret = NULL;
2981
2982 opd = bfd_get_section_by_name (abfd, ".opd");
2983 if (opd == NULL)
2984 return 0;
2985
a7535cf3 2986 symcount = static_count;
c9727e01 2987 if (!relocatable)
a7535cf3 2988 symcount += dyn_count;
90e3cdf2 2989 if (symcount == 0)
c9727e01 2990 return 0;
90e3cdf2 2991
a7535cf3
AM
2992 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2993 if (syms == NULL)
7356fed5 2994 return -1;
a7535cf3
AM
2995
2996 if (!relocatable && static_count != 0 && dyn_count != 0)
2997 {
2998 /* Use both symbol tables. */
2999 memcpy (syms, static_syms, static_count * sizeof (*syms));
3000 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3001 }
3002 else if (!relocatable && static_count == 0)
3003 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3004 else
3005 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3006
90e3cdf2 3007 synthetic_relocatable = relocatable;
595da8c5 3008 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3009
c9727e01
AM
3010 if (!relocatable && symcount > 1)
3011 {
3012 long j;
3013 /* Trim duplicate syms, since we may have merged the normal and
3014 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3015 different values, so trim any with the same value. */
c9727e01
AM
3016 for (i = 1, j = 1; i < symcount; ++i)
3017 if (syms[i - 1]->value + syms[i - 1]->section->vma
3018 != syms[i]->value + syms[i]->section->vma)
3019 syms[j++] = syms[i];
3020 symcount = j;
3021 }
3022
699733f6 3023 i = 0;
ffcfec52 3024 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3025 ++i;
3026 codesecsym = i;
90e3cdf2 3027
699733f6
AM
3028 for (; i < symcount; ++i)
3029 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3030 != (SEC_CODE | SEC_ALLOC))
3031 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3032 break;
3033 codesecsymend = i;
90e3cdf2 3034
699733f6
AM
3035 for (; i < symcount; ++i)
3036 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3037 break;
3038 secsymend = i;
90e3cdf2 3039
699733f6 3040 for (; i < symcount; ++i)
ffcfec52 3041 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3042 break;
3043 opdsymend = i;
90e3cdf2 3044
699733f6
AM
3045 for (; i < symcount; ++i)
3046 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3047 != (SEC_CODE | SEC_ALLOC))
3048 break;
3049 symcount = i;
3050
c9727e01 3051 count = 0;
90e3cdf2 3052
699733f6 3053 if (relocatable)
90e3cdf2 3054 {
699733f6
AM
3055 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3056 arelent *r;
3057 size_t size;
3058 long relcount;
90e3cdf2 3059
468392fb
AM
3060 if (opdsymend == secsymend)
3061 goto done;
3062
699733f6 3063 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3064 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3065 if (relcount == 0)
c9727e01 3066 goto done;
90e3cdf2 3067
7356fed5
AM
3068 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3069 {
3070 count = -1;
3071 goto done;
3072 }
3073
699733f6 3074 size = 0;
595da8c5 3075 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3076 {
3077 asymbol *sym;
90e3cdf2 3078
595da8c5 3079 while (r < opd->relocation + relcount
699733f6
AM
3080 && r->address < syms[i]->value + opd->vma)
3081 ++r;
90e3cdf2 3082
595da8c5 3083 if (r == opd->relocation + relcount)
699733f6 3084 break;
90e3cdf2 3085
699733f6
AM
3086 if (r->address != syms[i]->value + opd->vma)
3087 continue;
90e3cdf2 3088
699733f6
AM
3089 if (r->howto->type != R_PPC64_ADDR64)
3090 continue;
90e3cdf2 3091
699733f6
AM
3092 sym = *r->sym_ptr_ptr;
3093 if (!sym_exists_at (syms, opdsymend, symcount,
3094 sym->section->id, sym->value + r->addend))
3095 {
3096 ++count;
3097 size += sizeof (asymbol);
3098 size += strlen (syms[i]->name) + 2;
3099 }
3100 }
90e3cdf2 3101
699733f6
AM
3102 s = *ret = bfd_malloc (size);
3103 if (s == NULL)
3104 {
7356fed5 3105 count = -1;
c9727e01 3106 goto done;
699733f6 3107 }
90e3cdf2 3108
699733f6 3109 names = (char *) (s + count);
90e3cdf2 3110
595da8c5 3111 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3112 {
699733f6 3113 asymbol *sym;
90e3cdf2 3114
595da8c5 3115 while (r < opd->relocation + relcount
699733f6
AM
3116 && r->address < syms[i]->value + opd->vma)
3117 ++r;
90e3cdf2 3118
595da8c5 3119 if (r == opd->relocation + relcount)
699733f6
AM
3120 break;
3121
3122 if (r->address != syms[i]->value + opd->vma)
3123 continue;
3124
3125 if (r->howto->type != R_PPC64_ADDR64)
3126 continue;
90e3cdf2 3127
699733f6
AM
3128 sym = *r->sym_ptr_ptr;
3129 if (!sym_exists_at (syms, opdsymend, symcount,
3130 sym->section->id, sym->value + r->addend))
3131 {
3132 size_t len;
3133
3134 *s = *syms[i];
6ba2a415 3135 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3136 s->section = sym->section;
3137 s->value = sym->value + r->addend;
3138 s->name = names;
3139 *names++ = '.';
3140 len = strlen (syms[i]->name);
3141 memcpy (names, syms[i]->name, len + 1);
3142 names += len + 1;
6f610d07
UW
3143 /* Have udata.p point back to the original symbol this
3144 synthetic symbol was derived from. */
3145 s->udata.p = syms[i];
699733f6
AM
3146 s++;
3147 }
3148 }
3149 }
3150 else
90e3cdf2 3151 {
468392fb 3152 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
699733f6
AM
3153 bfd_byte *contents;
3154 size_t size;
468392fb
AM
3155 long plt_count = 0;
3156 bfd_vma glink_vma = 0, resolv_vma = 0;
3157 asection *dynamic, *glink = NULL, *relplt = NULL;
3158 arelent *p;
90e3cdf2 3159
699733f6
AM
3160 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3161 {
3162 if (contents)
c9727e01
AM
3163 {
3164 free_contents_and_exit:
3165 free (contents);
3166 }
7356fed5 3167 count = -1;
c9727e01 3168 goto done;
699733f6 3169 }
90e3cdf2 3170
699733f6
AM
3171 size = 0;
3172 for (i = secsymend; i < opdsymend; ++i)
3173 {
3174 bfd_vma ent;
90e3cdf2 3175
5ef11c02
AM
3176 /* Ignore bogus symbols. */
3177 if (syms[i]->value > opd->size - 8)
3178 continue;
3179
699733f6
AM
3180 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3181 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3182 {
3183 ++count;
3184 size += sizeof (asymbol);
3185 size += strlen (syms[i]->name) + 2;
3186 }
3187 }
90e3cdf2 3188
468392fb 3189 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3190 if (dyn_count != 0
3191 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3192 {
3193 bfd_byte *dynbuf, *extdyn, *extdynend;
3194 size_t extdynsize;
3195 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3196
3197 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3198 goto free_contents_and_exit;
3199
3200 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3201 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3202
3203 extdyn = dynbuf;
3204 extdynend = extdyn + dynamic->size;
3205 for (; extdyn < extdynend; extdyn += extdynsize)
3206 {
3207 Elf_Internal_Dyn dyn;
3208 (*swap_dyn_in) (abfd, extdyn, &dyn);
3209
3210 if (dyn.d_tag == DT_NULL)
3211 break;
3212
3213 if (dyn.d_tag == DT_PPC64_GLINK)
3214 {
3215 /* The first glink stub starts at offset 32; see comment in
3216 ppc64_elf_finish_dynamic_sections. */
3217 glink_vma = dyn.d_un.d_val + 32;
3218 /* The .glink section usually does not survive the final
3219 link; search for the section (usually .text) where the
3220 glink stubs now reside. */
3221 glink = bfd_sections_find_if (abfd, section_covers_vma,
3222 &glink_vma);
3223 break;
3224 }
3225 }
3226
3227 free (dynbuf);
3228 }
3229
3230 if (glink != NULL)
3231 {
3232 /* Determine __glink trampoline by reading the relative branch
3233 from the first glink stub. */
3234 bfd_byte buf[4];
3235 if (bfd_get_section_contents (abfd, glink, buf,
3236 glink_vma + 4 - glink->vma, 4))
3237 {
3238 unsigned int insn = bfd_get_32 (abfd, buf);
3239 insn ^= B_DOT;
3240 if ((insn & ~0x3fffffc) == 0)
3241 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3242 }
3243
3244 if (resolv_vma)
3245 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3246
066ee829
AM
3247 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3248 if (relplt != NULL)
3249 {
3250 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3251 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3252 goto free_contents_and_exit;
468392fb 3253
066ee829
AM
3254 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3255 size += plt_count * sizeof (asymbol);
468392fb 3256
066ee829
AM
3257 p = relplt->relocation;
3258 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3259 {
3260 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3261 if (p->addend != 0)
3262 size += sizeof ("+0x") - 1 + 16;
3263 }
066ee829 3264 }
468392fb
AM
3265 }
3266
699733f6
AM
3267 s = *ret = bfd_malloc (size);
3268 if (s == NULL)
7356fed5 3269 goto free_contents_and_exit;
90e3cdf2 3270
468392fb 3271 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3272
699733f6 3273 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3274 {
699733f6 3275 bfd_vma ent;
90e3cdf2 3276
5ef11c02
AM
3277 if (syms[i]->value > opd->size - 8)
3278 continue;
3279
699733f6
AM
3280 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3281 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3282 {
c9727e01 3283 long lo, hi;
699733f6 3284 size_t len;
c9727e01 3285 asection *sec = abfd->sections;
90e3cdf2 3286
699733f6
AM
3287 *s = *syms[i];
3288 lo = codesecsym;
3289 hi = codesecsymend;
3290 while (lo < hi)
3291 {
c9727e01 3292 long mid = (lo + hi) >> 1;
699733f6
AM
3293 if (syms[mid]->section->vma < ent)
3294 lo = mid + 1;
3295 else if (syms[mid]->section->vma > ent)
3296 hi = mid;
3297 else
c9727e01
AM
3298 {
3299 sec = syms[mid]->section;
3300 break;
3301 }
699733f6
AM
3302 }
3303
c9727e01 3304 if (lo >= hi && lo > codesecsym)
699733f6 3305 sec = syms[lo - 1]->section;
699733f6
AM
3306
3307 for (; sec != NULL; sec = sec->next)
3308 {
3309 if (sec->vma > ent)
3310 break;
63524580
JK
3311 /* SEC_LOAD may not be set if SEC is from a separate debug
3312 info file. */
3313 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3314 break;
3315 if ((sec->flags & SEC_CODE) != 0)
3316 s->section = sec;
3317 }
6ba2a415 3318 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3319 s->value = ent - s->section->vma;
3320 s->name = names;
3321 *names++ = '.';
3322 len = strlen (syms[i]->name);
3323 memcpy (names, syms[i]->name, len + 1);
3324 names += len + 1;
6f610d07
UW
3325 /* Have udata.p point back to the original symbol this
3326 synthetic symbol was derived from. */
3327 s->udata.p = syms[i];
699733f6 3328 s++;
90e3cdf2 3329 }
90e3cdf2 3330 }
699733f6 3331 free (contents);
468392fb
AM
3332
3333 if (glink != NULL && relplt != NULL)
3334 {
3335 if (resolv_vma)
3336 {
3337 /* Add a symbol for the main glink trampoline. */
86a4952b 3338 memset (s, 0, sizeof *s);
468392fb 3339 s->the_bfd = abfd;
6ba2a415 3340 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3341 s->section = glink;
3342 s->value = resolv_vma - glink->vma;
3343 s->name = names;
3344 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3345 names += sizeof ("__glink_PLTresolve");
3346 s++;
3347 count++;
3348 }
3349
3350 /* FIXME: It would be very much nicer to put sym@plt on the
3351 stub rather than on the glink branch table entry. The
3352 objdump disassembler would then use a sensible symbol
3353 name on plt calls. The difficulty in doing so is
3354 a) finding the stubs, and,
3355 b) matching stubs against plt entries, and,
3356 c) there can be multiple stubs for a given plt entry.
3357
3358 Solving (a) could be done by code scanning, but older
3359 ppc64 binaries used different stubs to current code.
3360 (b) is the tricky one since you need to known the toc
3361 pointer for at least one function that uses a pic stub to
3362 be able to calculate the plt address referenced.
3363 (c) means gdb would need to set multiple breakpoints (or
3364 find the glink branch itself) when setting breakpoints
3365 for pending shared library loads. */
3366 p = relplt->relocation;
3367 for (i = 0; i < plt_count; i++, p++)
3368 {
3369 size_t len;
3370
3371 *s = **p->sym_ptr_ptr;
3372 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3373 we are defining a symbol, ensure one of them is set. */
3374 if ((s->flags & BSF_LOCAL) == 0)
3375 s->flags |= BSF_GLOBAL;
6ba2a415 3376 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3377 s->section = glink;
3378 s->value = glink_vma - glink->vma;
3379 s->name = names;
3380 s->udata.p = NULL;
3381 len = strlen ((*p->sym_ptr_ptr)->name);
3382 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3383 names += len;
e054468f
AM
3384 if (p->addend != 0)
3385 {
3386 memcpy (names, "+0x", sizeof ("+0x") - 1);
3387 names += sizeof ("+0x") - 1;
3388 bfd_sprintf_vma (abfd, names, p->addend);
3389 names += strlen (names);
3390 }
468392fb
AM
3391 memcpy (names, "@plt", sizeof ("@plt"));
3392 names += sizeof ("@plt");
3393 s++;
3394 glink_vma += 8;
3395 if (i >= 0x8000)
3396 glink_vma += 4;
3397 }
3398 count += plt_count;
3399 }
90e3cdf2
JJ
3400 }
3401
c9727e01 3402 done:
a7535cf3 3403 free (syms);
90e3cdf2
JJ
3404 return count;
3405}
5bd4f169 3406\f
65f38f15
AM
3407/* The following functions are specific to the ELF linker, while
3408 functions above are used generally. Those named ppc64_elf_* are
3409 called by the main ELF linker code. They appear in this file more
3410 or less in the order in which they are called. eg.
3411 ppc64_elf_check_relocs is called early in the link process,
3412 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3413 called.
3414
3415 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3416 functions have both a function code symbol and a function descriptor
3417 symbol. A call to foo in a relocatable object file looks like:
3418
3419 . .text
3420 . x:
3421 . bl .foo
3422 . nop
3423
3424 The function definition in another object file might be:
3425
3426 . .section .opd
3427 . foo: .quad .foo
3428 . .quad .TOC.@tocbase
3429 . .quad 0
3430 .
3431 . .text
3432 . .foo: blr
3433
3434 When the linker resolves the call during a static link, the branch
3435 unsurprisingly just goes to .foo and the .opd information is unused.
3436 If the function definition is in a shared library, things are a little
3437 different: The call goes via a plt call stub, the opd information gets
3438 copied to the plt, and the linker patches the nop.
3439
3440 . x:
3441 . bl .foo_stub
3442 . ld 2,40(1)
3443 .
3444 .
3445 . .foo_stub:
3446 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 3447 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
3448 . std 2,40(1) # this is the general idea
3449 . ld 11,0(12)
3450 . ld 2,8(12)
3451 . mtctr 11
3452 . ld 11,16(12)
3453 . bctr
3454 .
3455 . .section .plt
3456 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3457
3458 The "reloc ()" notation is supposed to indicate that the linker emits
3459 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3460 copying.
3461
3462 What are the difficulties here? Well, firstly, the relocations
3463 examined by the linker in check_relocs are against the function code
3464 sym .foo, while the dynamic relocation in the plt is emitted against
3465 the function descriptor symbol, foo. Somewhere along the line, we need
3466 to carefully copy dynamic link information from one symbol to the other.
3467 Secondly, the generic part of the elf linker will make .foo a dynamic
3468 symbol as is normal for most other backends. We need foo dynamic
3469 instead, at least for an application final link. However, when
3470 creating a shared library containing foo, we need to have both symbols
3471 dynamic so that references to .foo are satisfied during the early
3472 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3473 definition from some other object, eg. a static library.
3474
3475 Update: As of August 2004, we support a new convention. Function
3476 calls may use the function descriptor symbol, ie. "bl foo". This
3477 behaves exactly as "bl .foo". */
65f38f15 3478
1d483afe 3479/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3480 selects those that must be copied when linking a shared library,
3481 even when the symbol is local. */
65f38f15 3482
1d483afe
AM
3483static int
3484must_be_dyn_reloc (struct bfd_link_info *info,
3485 enum elf_ppc64_reloc_type r_type)
3486{
3487 switch (r_type)
3488 {
3489 default:
3490 return 1;
3491
3492 case R_PPC64_REL32:
3493 case R_PPC64_REL64:
3494 case R_PPC64_REL30:
3495 return 0;
3496
3497 case R_PPC64_TPREL16:
3498 case R_PPC64_TPREL16_LO:
3499 case R_PPC64_TPREL16_HI:
3500 case R_PPC64_TPREL16_HA:
3501 case R_PPC64_TPREL16_DS:
3502 case R_PPC64_TPREL16_LO_DS:
3503 case R_PPC64_TPREL16_HIGHER:
3504 case R_PPC64_TPREL16_HIGHERA:
3505 case R_PPC64_TPREL16_HIGHEST:
3506 case R_PPC64_TPREL16_HIGHESTA:
3507 case R_PPC64_TPREL64:
3508 return !info->executable;
3509 }
3510}
65f38f15 3511
f4656909
AM
3512/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3513 copying dynamic variables from a shared lib into an app's dynbss
3514 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3515 shared lib. With code that gcc generates, it's vital that this be
3516 enabled; In the PowerPC64 ABI, the address of a function is actually
3517 the address of a function descriptor, which resides in the .opd
3518 section. gcc uses the descriptor directly rather than going via the
3519 GOT as some other ABI's do, which means that initialized function
3520 pointers must reference the descriptor. Thus, a function pointer
3521 initialized to the address of a function in a shared library will
3522 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3523 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3524 presents a problem as a plt entry for that function is also
3525 initialized from the function descriptor symbol and the copy reloc
3526 may not be initialized first. */
a23b6845 3527#define ELIMINATE_COPY_RELOCS 1
f4656909 3528
721956f4 3529/* Section name for stubs is the associated section name plus this
29942be8
NC
3530 string. */
3531#define STUB_SUFFIX ".stub"
721956f4
AM
3532
3533/* Linker stubs.
3534 ppc_stub_long_branch:
3535 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3536 destination, but a 24 bit branch in a stub section will reach.
3537 . b dest
3538
3539 ppc_stub_plt_branch:
3540 Similar to the above, but a 24 bit branch in the stub section won't
3541 reach its destination.
87e226ce
AM
3542 . addis %r12,%r2,xxx@toc@ha
3543 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3544 . mtctr %r11
3545 . bctr
3546
3547 ppc_stub_plt_call:
2c66dc6c
AM
3548 Used to call a function in a shared library. If it so happens that
3549 the plt entry referenced crosses a 64k boundary, then an extra
ac2df442 3550 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
87e226ce 3551 . addis %r12,%r2,xxx@toc@ha
721956f4 3552 . std %r2,40(%r1)
87e226ce 3553 . ld %r11,xxx+0@toc@l(%r12)
721956f4 3554 . mtctr %r11
ac2df442 3555 . ld %r2,xxx+8@toc@l(%r12)
87e226ce 3556 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3557 . bctr
ad8e1ba5
AM
3558
3559 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3560 code to adjust the value and save r2 to support multiple toc sections.
3561 A ppc_stub_long_branch with an r2 offset looks like:
3562 . std %r2,40(%r1)
3563 . addis %r2,%r2,off@ha
3564 . addi %r2,%r2,off@l
3565 . b dest
3566
3567 A ppc_stub_plt_branch with an r2 offset looks like:
3568 . std %r2,40(%r1)
3569 . addis %r12,%r2,xxx@toc@ha
3570 . ld %r11,xxx@toc@l(%r12)
3571 . addis %r2,%r2,off@ha
3572 . addi %r2,%r2,off@l
3573 . mtctr %r11
3574 . bctr
ac2df442
AM
3575
3576 In cases where the "addis" instruction would add zero, the "addis" is
3577 omitted and following instructions modified slightly in some cases.
721956f4
AM
3578*/
3579
3580enum ppc_stub_type {
3581 ppc_stub_none,
3582 ppc_stub_long_branch,
ad8e1ba5 3583 ppc_stub_long_branch_r2off,
721956f4 3584 ppc_stub_plt_branch,
ad8e1ba5 3585 ppc_stub_plt_branch_r2off,
721956f4
AM
3586 ppc_stub_plt_call
3587};
3588
3589struct ppc_stub_hash_entry {
3590
3591 /* Base hash table entry structure. */
3592 struct bfd_hash_entry root;
3593
ad8e1ba5
AM
3594 enum ppc_stub_type stub_type;
3595
721956f4
AM
3596 /* The stub section. */
3597 asection *stub_sec;
3598
3599 /* Offset within stub_sec of the beginning of this stub. */
3600 bfd_vma stub_offset;
3601
3602 /* Given the symbol's value and its section we can determine its final
3603 value when building the stubs (so the stub knows where to jump. */
3604 bfd_vma target_value;
3605 asection *target_section;
3606
721956f4
AM
3607 /* The symbol table entry, if any, that this was derived from. */
3608 struct ppc_link_hash_entry *h;
e054468f 3609 struct plt_entry *plt_ent;
721956f4 3610
411e1bfb
AM
3611 /* And the reloc addend that this was derived from. */
3612 bfd_vma addend;
3613
721956f4
AM
3614 /* Where this stub is being called from, or, in the case of combined
3615 stub sections, the first input section in the group. */
3616 asection *id_sec;
3617};
3618
3619struct ppc_branch_hash_entry {
3620
3621 /* Base hash table entry structure. */
3622 struct bfd_hash_entry root;
3623
c456f082 3624 /* Offset within branch lookup table. */
721956f4
AM
3625 unsigned int offset;
3626
3627 /* Generation marker. */
3628 unsigned int iter;
3629};
65f38f15
AM
3630
3631struct ppc_link_hash_entry
3632{
3633 struct elf_link_hash_entry elf;
3634
b3fac117
AM
3635 union {
3636 /* A pointer to the most recently used stub hash entry against this
3637 symbol. */
3638 struct ppc_stub_hash_entry *stub_cache;
3639
3640 /* A pointer to the next symbol starting with a '.' */
3641 struct ppc_link_hash_entry *next_dot_sym;
3642 } u;
721956f4 3643
65f38f15 3644 /* Track dynamic relocs copied for this symbol. */
6061a67d 3645 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3646
721956f4 3647 /* Link between function code and descriptor symbols. */
34814b9f 3648 struct ppc_link_hash_entry *oh;
721956f4 3649
e86ce104
AM
3650 /* Flag function code and descriptor symbols. */
3651 unsigned int is_func:1;
3652 unsigned int is_func_descriptor:1;
908b32fc 3653 unsigned int fake:1;
411e1bfb 3654
c5614fa4
AM
3655 /* Whether global opd/toc sym has been adjusted or not.
3656 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3657 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3658 unsigned int adjust_done:1;
3659
99877b66
AM
3660 /* Set if we twiddled this symbol to weak at some stage. */
3661 unsigned int was_undefined:1;
3662
411e1bfb 3663 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3664 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3665 corresponding relocs are encountered during check_relocs.
3666 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3667 indicate the corresponding GOT entry type is not needed.
3668 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3669 a TPREL one. We use a separate flag rather than setting TPREL
3670 just for convenience in distinguishing the two cases. */
3671#define TLS_GD 1 /* GD reloc. */
3672#define TLS_LD 2 /* LD reloc. */
3673#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3674#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3675#define TLS_TLS 16 /* Any TLS reloc. */
3676#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3677#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3678#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3679 unsigned char tls_mask;
65f38f15
AM
3680};
3681
3682/* ppc64 ELF linker hash table. */
3683
3684struct ppc_link_hash_table
3685{
3686 struct elf_link_hash_table elf;
3687
721956f4
AM
3688 /* The stub hash table. */
3689 struct bfd_hash_table stub_hash_table;
3690
3691 /* Another hash table for plt_branch stubs. */
3692 struct bfd_hash_table branch_hash_table;
3693
3b421ab3
AM
3694 /* Hash table for function prologue tocsave. */
3695 htab_t tocsave_htab;
3696
721956f4
AM
3697 /* Linker stub bfd. */
3698 bfd *stub_bfd;
3699
3700 /* Linker call-backs. */
4ce794b7
AM
3701 asection * (*add_stub_section) (const char *, asection *);
3702 void (*layout_sections_again) (void);
721956f4
AM
3703
3704 /* Array to keep track of which stub sections have been created, and
3705 information on stub grouping. */
3706 struct map_stub {
3707 /* This is the section to which stubs in the group will be attached. */
3708 asection *link_sec;
3709 /* The stub section. */
3710 asection *stub_sec;
ad8e1ba5
AM
3711 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3712 bfd_vma toc_off;
721956f4
AM
3713 } *stub_group;
3714
ad8e1ba5
AM
3715 /* Temp used when calculating TOC pointers. */
3716 bfd_vma toc_curr;
bf102f86
AM
3717 bfd *toc_bfd;
3718 asection *toc_first_sec;
ad8e1ba5 3719
8f3bab57
AM
3720 /* Highest input section id. */
3721 int top_id;
3722
734b6cf9
AM
3723 /* Highest output section index. */
3724 int top_index;
3725
b3fac117
AM
3726 /* Used when adding symbols. */
3727 struct ppc_link_hash_entry *dot_syms;
3728
734b6cf9
AM
3729 /* List of input sections for each output section. */
3730 asection **input_list;
721956f4 3731
65f38f15 3732 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3733 asection *got;
4ce794b7
AM
3734 asection *plt;
3735 asection *relplt;
e054468f
AM
3736 asection *iplt;
3737 asection *reliplt;
4ce794b7
AM
3738 asection *dynbss;
3739 asection *relbss;
3740 asection *glink;
82bd7b59 3741 asection *sfpr;
4ce794b7
AM
3742 asection *brlt;
3743 asection *relbrlt;
58d180e8 3744 asection *glink_eh_frame;
ec338859 3745
8387904d
AM
3746 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3747 struct ppc_link_hash_entry *tls_get_addr;
3748 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3749
927be08e
AM
3750 /* The size of reliplt used by got entry relocs. */
3751 bfd_size_type got_reli_size;
3752
9b5ecbd0
AM
3753 /* Statistics. */
3754 unsigned long stub_count[ppc_stub_plt_call];
3755
ee75fd95
AM
3756 /* Number of stubs against global syms. */
3757 unsigned long stub_globals;
3758
9df0ef5f
AM
3759 /* Set if PLT call stubs should load r11. */
3760 unsigned int plt_static_chain:1;
3761
ad8e1ba5 3762 /* Set if we should emit symbols for stubs. */
99877b66 3763 unsigned int emit_stub_syms:1;
ad8e1ba5 3764
a7f2871e
AM
3765 /* Set if __tls_get_addr optimization should not be done. */
3766 unsigned int no_tls_get_addr_opt:1;
3767
4c52953f 3768 /* Support for multiple toc sections. */
33c0ec9d 3769 unsigned int do_multi_toc:1;
4c52953f 3770 unsigned int multi_toc_needed:1;
927be08e 3771 unsigned int second_toc_pass:1;
67f0cbdb 3772 unsigned int do_toc_opt:1;
4c52953f 3773
5d1634d7 3774 /* Set on error. */
99877b66 3775 unsigned int stub_error:1;
721956f4 3776
7d9616d7 3777 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3778 unsigned int twiddled_syms:1;
721956f4
AM
3779
3780 /* Incremented every time we size stubs. */
3781 unsigned int stub_iteration;
5d1634d7 3782
87d72d41
AM
3783 /* Small local sym cache. */
3784 struct sym_cache sym_cache;
65f38f15
AM
3785};
3786
4c52953f
AM
3787/* Rename some of the generic section flags to better document how they
3788 are used here. */
b0dddeec
AM
3789
3790/* Nonzero if this section has TLS related relocations. */
3791#define has_tls_reloc sec_flg0
3792
3793/* Nonzero if this section has a call to __tls_get_addr. */
3794#define has_tls_get_addr_call sec_flg1
3795
3796/* Nonzero if this section has any toc or got relocs. */
3797#define has_toc_reloc sec_flg2
3798
3799/* Nonzero if this section has a call to another section that uses
3800 the toc or got. */
d77c8a4b 3801#define makes_toc_func_call sec_flg3
b0dddeec
AM
3802
3803/* Recursion protection when determining above flag. */
d77c8a4b 3804#define call_check_in_progress sec_flg4
70cc837d 3805#define call_check_done sec_flg5
4c52953f 3806
65f38f15
AM
3807/* Get the ppc64 ELF linker hash table from a link_info structure. */
3808
3809#define ppc_hash_table(p) \
4dfe6ac6
NC
3810 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3811 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3812
721956f4
AM
3813#define ppc_stub_hash_lookup(table, string, create, copy) \
3814 ((struct ppc_stub_hash_entry *) \
3815 bfd_hash_lookup ((table), (string), (create), (copy)))
3816
3817#define ppc_branch_hash_lookup(table, string, create, copy) \
3818 ((struct ppc_branch_hash_entry *) \
3819 bfd_hash_lookup ((table), (string), (create), (copy)))
3820
3821/* Create an entry in the stub hash table. */
3822
3823static struct bfd_hash_entry *
4ce794b7
AM
3824stub_hash_newfunc (struct bfd_hash_entry *entry,
3825 struct bfd_hash_table *table,
3826 const char *string)
721956f4
AM
3827{
3828 /* Allocate the structure if it has not already been allocated by a
3829 subclass. */
3830 if (entry == NULL)
3831 {
3832 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3833 if (entry == NULL)
3834 return entry;
3835 }
3836
3837 /* Call the allocation method of the superclass. */
3838 entry = bfd_hash_newfunc (entry, table, string);
3839 if (entry != NULL)
3840 {
3841 struct ppc_stub_hash_entry *eh;
3842
3843 /* Initialize the local fields. */
3844 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3845 eh->stub_type = ppc_stub_none;
721956f4
AM
3846 eh->stub_sec = NULL;
3847 eh->stub_offset = 0;
3848 eh->target_value = 0;
3849 eh->target_section = NULL;
721956f4
AM
3850 eh->h = NULL;
3851 eh->id_sec = NULL;
3852 }
3853
3854 return entry;
3855}
3856
3857/* Create an entry in the branch hash table. */
3858
3859static struct bfd_hash_entry *
4ce794b7
AM
3860branch_hash_newfunc (struct bfd_hash_entry *entry,
3861 struct bfd_hash_table *table,
3862 const char *string)
721956f4
AM
3863{
3864 /* Allocate the structure if it has not already been allocated by a
3865 subclass. */
3866 if (entry == NULL)
3867 {
3868 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3869 if (entry == NULL)
3870 return entry;
3871 }
3872
3873 /* Call the allocation method of the superclass. */
3874 entry = bfd_hash_newfunc (entry, table, string);
3875 if (entry != NULL)
3876 {
3877 struct ppc_branch_hash_entry *eh;
3878
3879 /* Initialize the local fields. */
3880 eh = (struct ppc_branch_hash_entry *) entry;
3881 eh->offset = 0;
3882 eh->iter = 0;
3883 }
3884
3885 return entry;
3886}
3887
65f38f15
AM
3888/* Create an entry in a ppc64 ELF linker hash table. */
3889
3890static struct bfd_hash_entry *
4ce794b7
AM
3891link_hash_newfunc (struct bfd_hash_entry *entry,
3892 struct bfd_hash_table *table,
3893 const char *string)
65f38f15
AM
3894{
3895 /* Allocate the structure if it has not already been allocated by a
3896 subclass. */
3897 if (entry == NULL)
3898 {
3899 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3900 if (entry == NULL)
3901 return entry;
3902 }
3903
3904 /* Call the allocation method of the superclass. */
3905 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3906 if (entry != NULL)
3907 {
3908 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3909
b3fac117 3910 memset (&eh->u.stub_cache, 0,
908b32fc 3911 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3912 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3913
3914 /* When making function calls, old ABI code references function entry
3915 points (dot symbols), while new ABI code references the function
3916 descriptor symbol. We need to make any combination of reference and
3917 definition work together, without breaking archive linking.
3918
3919 For a defined function "foo" and an undefined call to "bar":
3920 An old object defines "foo" and ".foo", references ".bar" (possibly
3921 "bar" too).
3922 A new object defines "foo" and references "bar".
3923
3924 A new object thus has no problem with its undefined symbols being
3925 satisfied by definitions in an old object. On the other hand, the
3926 old object won't have ".bar" satisfied by a new object.
3927
3928 Keep a list of newly added dot-symbols. */
3929
3930 if (string[0] == '.')
3931 {
3932 struct ppc_link_hash_table *htab;
3933
3934 htab = (struct ppc_link_hash_table *) table;
3935 eh->u.next_dot_sym = htab->dot_syms;
3936 htab->dot_syms = eh;
3937 }
65f38f15
AM
3938 }
3939
3940 return entry;
3941}
3942
3b421ab3
AM
3943struct tocsave_entry {
3944 asection *sec;
3945 bfd_vma offset;
3946};
3947
3948static hashval_t
3949tocsave_htab_hash (const void *p)
3950{
3951 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3952 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
3953}
3954
3955static int
3956tocsave_htab_eq (const void *p1, const void *p2)
3957{
3958 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3959 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3960 return e1->sec == e2->sec && e1->offset == e2->offset;
3961}
3962
65f38f15
AM
3963/* Create a ppc64 ELF linker hash table. */
3964
3965static struct bfd_link_hash_table *
4ce794b7 3966ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3967{
3968 struct ppc_link_hash_table *htab;
3969 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3970
4ce794b7 3971 htab = bfd_zmalloc (amt);
65f38f15
AM
3972 if (htab == NULL)
3973 return NULL;
3974
66eb6687 3975 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3976 sizeof (struct ppc_link_hash_entry),
3977 PPC64_ELF_DATA))
65f38f15 3978 {
e2d34d7d 3979 free (htab);
65f38f15
AM
3980 return NULL;
3981 }
3982
721956f4 3983 /* Init the stub hash table too. */
66eb6687
AM
3984 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3985 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
3986 return NULL;
3987
3988 /* And the branch hash table. */
66eb6687
AM
3989 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3990 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
3991 return NULL;
3992
3b421ab3
AM
3993 htab->tocsave_htab = htab_try_create (1024,
3994 tocsave_htab_hash,
3995 tocsave_htab_eq,
3996 NULL);
3997 if (htab->tocsave_htab == NULL)
3998 return NULL;
3999
3254fd24
AM
4000 /* Initializing two fields of the union is just cosmetic. We really
4001 only care about glist, but when compiled on a 32-bit host the
4002 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4003 debugger inspection of these fields look nicer. */
a6aa5195
AM
4004 htab->elf.init_got_refcount.refcount = 0;
4005 htab->elf.init_got_refcount.glist = NULL;
4006 htab->elf.init_plt_refcount.refcount = 0;
4007 htab->elf.init_plt_refcount.glist = NULL;
4008 htab->elf.init_got_offset.offset = 0;
4009 htab->elf.init_got_offset.glist = NULL;
4010 htab->elf.init_plt_offset.offset = 0;
4011 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4012
65f38f15
AM
4013 return &htab->elf.root;
4014}
4015
721956f4
AM
4016/* Free the derived linker hash table. */
4017
4018static void
4ce794b7 4019ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4 4020{
3b421ab3 4021 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
721956f4 4022
3b421ab3
AM
4023 bfd_hash_table_free (&htab->stub_hash_table);
4024 bfd_hash_table_free (&htab->branch_hash_table);
4025 if (htab->tocsave_htab)
4026 htab_delete (htab->tocsave_htab);
721956f4
AM
4027 _bfd_generic_link_hash_table_free (hash);
4028}
4029
e717da7e
AM
4030/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4031
4032void
4033ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4034{
4035 struct ppc_link_hash_table *htab;
4036
4037 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4038
4039/* Always hook our dynamic sections into the first bfd, which is the
4040 linker created stub bfd. This ensures that the GOT header is at
4041 the start of the output TOC section. */
4042 htab = ppc_hash_table (info);
4dfe6ac6
NC
4043 if (htab == NULL)
4044 return;
e717da7e
AM
4045 htab->stub_bfd = abfd;
4046 htab->elf.dynobj = abfd;
4047}
4048
721956f4
AM
4049/* Build a name for an entry in the stub hash table. */
4050
4051static char *
4ce794b7
AM
4052ppc_stub_name (const asection *input_section,
4053 const asection *sym_sec,
4054 const struct ppc_link_hash_entry *h,
4055 const Elf_Internal_Rela *rel)
721956f4
AM
4056{
4057 char *stub_name;
4058 bfd_size_type len;
4059
4060 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4061 offsets from a sym as a branch target? In fact, we could
4062 probably assume the addend is always zero. */
4063 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4064
4065 if (h)
4066 {
4067 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4068 stub_name = bfd_malloc (len);
46de2a7c
AM
4069 if (stub_name == NULL)
4070 return stub_name;
4071
4072 sprintf (stub_name, "%08x.%s+%x",
4073 input_section->id & 0xffffffff,
4074 h->elf.root.root.string,
4075 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4076 }
4077 else
4078 {
ad8e1ba5 4079 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4080 stub_name = bfd_malloc (len);
46de2a7c
AM
4081 if (stub_name == NULL)
4082 return stub_name;
4083
4084 sprintf (stub_name, "%08x.%x:%x+%x",
4085 input_section->id & 0xffffffff,
4086 sym_sec->id & 0xffffffff,
4087 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4088 (int) rel->r_addend & 0xffffffff);
721956f4 4089 }
ee75fd95
AM
4090 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4091 stub_name[len - 2] = 0;
721956f4
AM
4092 return stub_name;
4093}
4094
4095/* Look up an entry in the stub hash. Stub entries are cached because
4096 creating the stub name takes a bit of time. */
4097
4098static struct ppc_stub_hash_entry *
4ce794b7
AM
4099ppc_get_stub_entry (const asection *input_section,
4100 const asection *sym_sec,
039b3fef 4101 struct ppc_link_hash_entry *h,
4ce794b7
AM
4102 const Elf_Internal_Rela *rel,
4103 struct ppc_link_hash_table *htab)
721956f4
AM
4104{
4105 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4106 const asection *id_sec;
4107
4108 /* If this input section is part of a group of sections sharing one
4109 stub section, then use the id of the first section in the group.
4110 Stub names need to include a section id, as there may well be
4111 more than one stub used to reach say, printf, and we need to
4112 distinguish between them. */
4113 id_sec = htab->stub_group[input_section->id].link_sec;
4114
b3fac117
AM
4115 if (h != NULL && h->u.stub_cache != NULL
4116 && h->u.stub_cache->h == h
4117 && h->u.stub_cache->id_sec == id_sec)
721956f4 4118 {
b3fac117 4119 stub_entry = h->u.stub_cache;
721956f4
AM
4120 }
4121 else
4122 {
4123 char *stub_name;
4124
4125 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4126 if (stub_name == NULL)
4127 return NULL;
4128
4129 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4130 stub_name, FALSE, FALSE);
721956f4 4131 if (h != NULL)
b3fac117 4132 h->u.stub_cache = stub_entry;
721956f4
AM
4133
4134 free (stub_name);
4135 }
4136
4137 return stub_entry;
4138}
4139
4140/* Add a new stub entry to the stub hash. Not all fields of the new
4141 stub entry are initialised. */
4142
4143static struct ppc_stub_hash_entry *
4ce794b7
AM
4144ppc_add_stub (const char *stub_name,
4145 asection *section,
25f53a85 4146 struct bfd_link_info *info)
721956f4 4147{
25f53a85 4148 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4149 asection *link_sec;
4150 asection *stub_sec;
4151 struct ppc_stub_hash_entry *stub_entry;
4152
4153 link_sec = htab->stub_group[section->id].link_sec;
4154 stub_sec = htab->stub_group[section->id].stub_sec;
4155 if (stub_sec == NULL)
4156 {
4157 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4158 if (stub_sec == NULL)
4159 {
d4c88bbb 4160 size_t namelen;
721956f4
AM
4161 bfd_size_type len;
4162 char *s_name;
4163
d4c88bbb
AM
4164 namelen = strlen (link_sec->name);
4165 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4166 s_name = bfd_alloc (htab->stub_bfd, len);
4167 if (s_name == NULL)
4168 return NULL;
4169
d4c88bbb
AM
4170 memcpy (s_name, link_sec->name, namelen);
4171 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4172 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4173 if (stub_sec == NULL)
4174 return NULL;
4175 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4176 }
4177 htab->stub_group[section->id].stub_sec = stub_sec;
4178 }
4179
4180 /* Enter this entry into the linker stub hash table. */
4181 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4182 TRUE, FALSE);
721956f4
AM
4183 if (stub_entry == NULL)
4184 {
8de848d8 4185 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
25f53a85 4186 section->owner, stub_name);
721956f4
AM
4187 return NULL;
4188 }
4189
4190 stub_entry->stub_sec = stub_sec;
4191 stub_entry->stub_offset = 0;
4192 stub_entry->id_sec = link_sec;
4193 return stub_entry;
4194}
4195
82bd7b59
AM
4196/* Create sections for linker generated code. */
4197
b34976b6 4198static bfd_boolean
4ce794b7 4199create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
4200{
4201 struct ppc_link_hash_table *htab;
4202 flagword flags;
4203
4204 htab = ppc_hash_table (info);
4dfe6ac6
NC
4205 if (htab == NULL)
4206 return FALSE;
82bd7b59
AM
4207
4208 /* Create .sfpr for code to save and restore fp regs. */
4209 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4210 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
4211 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4212 flags);
82bd7b59 4213 if (htab->sfpr == NULL
82bd7b59 4214 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 4215 return FALSE;
82bd7b59 4216
721956f4 4217 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
4218 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4219 flags);
4ce794b7 4220 if (htab->glink == NULL
ee4bf8d2 4221 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 4222 return FALSE;
82bd7b59 4223
58d180e8
AM
4224 if (!info->no_ld_generated_unwind_info)
4225 {
4226 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4227 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4228 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4229 ".eh_frame",
4230 flags);
4231 if (htab->glink_eh_frame == NULL
4232 || !bfd_set_section_alignment (abfd, htab->glink_eh_frame, 2))
4233 return FALSE;
4234 }
4235
e054468f
AM
4236 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4237 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4238 if (htab->iplt == NULL
4239 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4240 return FALSE;
4241
4242 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4243 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4244 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4245 ".rela.iplt",
4246 flags);
4247 if (htab->reliplt == NULL
4248 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4249 return FALSE;
4250
c456f082 4251 /* Create branch lookup table for plt_branch stubs. */
e4e0193e
AM
4252 flags = (SEC_ALLOC | SEC_LOAD
4253 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4254 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4255 flags);
4ce794b7 4256 if (htab->brlt == NULL
4ce794b7 4257 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 4258 return FALSE;
721956f4 4259
e4e0193e 4260 if (!info->shared)
c456f082
AM
4261 return TRUE;
4262
e4e0193e
AM
4263 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4264 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4265 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4266 ".rela.branch_lt",
4267 flags);
e054468f 4268 if (htab->relbrlt == NULL
c456f082
AM
4269 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4270 return FALSE;
4271
b34976b6 4272 return TRUE;
82bd7b59
AM
4273}
4274
e717da7e
AM
4275/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4276 not already done. */
65f38f15 4277
b34976b6 4278static bfd_boolean
e717da7e 4279create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4280{
e717da7e
AM
4281 asection *got, *relgot;
4282 flagword flags;
4283 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4284
0c8d6e5c 4285 if (!is_ppc64_elf (abfd))
0ffa91dd 4286 return FALSE;
4dfe6ac6
NC
4287 if (htab == NULL)
4288 return FALSE;
0ffa91dd 4289
4ce794b7 4290 if (!htab->got)
e717da7e
AM
4291 {
4292 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4293 return FALSE;
4294
4295 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4296 if (!htab->got)
4297 abort ();
4298 }
4299
4300 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4301 | SEC_LINKER_CREATED);
4302
c456f082 4303 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4304 if (!got
e717da7e
AM
4305 || !bfd_set_section_alignment (abfd, got, 3))
4306 return FALSE;
65f38f15 4307
c456f082
AM
4308 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4309 flags | SEC_READONLY);
e717da7e 4310 if (!relgot
e717da7e 4311 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4312 return FALSE;
e717da7e
AM
4313
4314 ppc64_elf_tdata (abfd)->got = got;
4315 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4316 return TRUE;
65f38f15 4317}
5bd4f169 4318
82bd7b59 4319/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4320
b34976b6 4321static bfd_boolean
4ce794b7 4322ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4323{
65f38f15 4324 struct ppc_link_hash_table *htab;
5bd4f169 4325
65f38f15 4326 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4327 return FALSE;
65f38f15 4328
e717da7e 4329 htab = ppc_hash_table (info);
4dfe6ac6
NC
4330 if (htab == NULL)
4331 return FALSE;
4332
e717da7e
AM
4333 if (!htab->got)
4334 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
4335 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4336 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4337 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 4338 if (!info->shared)
4ce794b7 4339 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 4340
e717da7e 4341 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4342 || (!info->shared && !htab->relbss))
65f38f15
AM
4343 abort ();
4344
b34976b6 4345 return TRUE;
5bd4f169
AM
4346}
4347
b31867b6
AM
4348/* Follow indirect and warning symbol links. */
4349
4350static inline struct bfd_link_hash_entry *
4351follow_link (struct bfd_link_hash_entry *h)
4352{
4353 while (h->type == bfd_link_hash_indirect
4354 || h->type == bfd_link_hash_warning)
4355 h = h->u.i.link;
4356 return h;
4357}
4358
4359static inline struct elf_link_hash_entry *
4360elf_follow_link (struct elf_link_hash_entry *h)
4361{
4362 return (struct elf_link_hash_entry *) follow_link (&h->root);
4363}
4364
4365static inline struct ppc_link_hash_entry *
4366ppc_follow_link (struct ppc_link_hash_entry *h)
4367{
4368 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4369}
4370
40d16e0b
AM
4371/* Merge PLT info on FROM with that on TO. */
4372
4373static void
4374move_plt_plist (struct ppc_link_hash_entry *from,
4375 struct ppc_link_hash_entry *to)
4376{
4377 if (from->elf.plt.plist != NULL)
4378 {
4379 if (to->elf.plt.plist != NULL)
4380 {
4381 struct plt_entry **entp;
4382 struct plt_entry *ent;
4383
4384 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4385 {
4386 struct plt_entry *dent;
4387
4388 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4389 if (dent->addend == ent->addend)
4390 {
4391 dent->plt.refcount += ent->plt.refcount;
4392 *entp = ent->next;
4393 break;
4394 }
4395 if (dent == NULL)
4396 entp = &ent->next;
4397 }
4398 *entp = to->elf.plt.plist;
4399 }
4400
4401 to->elf.plt.plist = from->elf.plt.plist;
4402 from->elf.plt.plist = NULL;
4403 }
4404}
4405
65f38f15
AM
4406/* Copy the extra info we tack onto an elf_link_hash_entry. */
4407
4408static void
fcfa13d2
AM
4409ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4410 struct elf_link_hash_entry *dir,
4411 struct elf_link_hash_entry *ind)
65f38f15
AM
4412{
4413 struct ppc_link_hash_entry *edir, *eind;
4414
4415 edir = (struct ppc_link_hash_entry *) dir;
4416 eind = (struct ppc_link_hash_entry *) ind;
4417
c79d6685
AM
4418 edir->is_func |= eind->is_func;
4419 edir->is_func_descriptor |= eind->is_func_descriptor;
4420 edir->tls_mask |= eind->tls_mask;
4421 if (eind->oh != NULL)
4422 edir->oh = ppc_follow_link (eind->oh);
4423
4424 /* If called to transfer flags for a weakdef during processing
4425 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4426 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4427 if (!(ELIMINATE_COPY_RELOCS
4428 && eind->elf.root.type != bfd_link_hash_indirect
4429 && edir->elf.dynamic_adjusted))
4430 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4431
4432 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4433 edir->elf.ref_regular |= eind->elf.ref_regular;
4434 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4435 edir->elf.needs_plt |= eind->elf.needs_plt;
4436
4437 /* If we were called to copy over info for a weak sym, that's all. */
4438 if (eind->elf.root.type != bfd_link_hash_indirect)
4439 return;
4440
411e1bfb 4441 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4442 if (eind->dyn_relocs != NULL)
65f38f15 4443 {
bbd7ec4a
AM
4444 if (edir->dyn_relocs != NULL)
4445 {
6061a67d
AM
4446 struct elf_dyn_relocs **pp;
4447 struct elf_dyn_relocs *p;
bbd7ec4a 4448
fcfa13d2 4449 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4450 list. Merge any entries against the same section. */
4451 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4452 {
6061a67d 4453 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4454
4455 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4456 if (q->sec == p->sec)
4457 {
4458 q->pc_count += p->pc_count;
4459 q->count += p->count;
4460 *pp = p->next;
4461 break;
4462 }
4463 if (q == NULL)
4464 pp = &p->next;
4465 }
4466 *pp = edir->dyn_relocs;
4467 }
4468
65f38f15
AM
4469 edir->dyn_relocs = eind->dyn_relocs;
4470 eind->dyn_relocs = NULL;
4471 }
65f38f15 4472
81848ca0
AM
4473 /* Copy over got entries that we may have already seen to the
4474 symbol which just became indirect. */
411e1bfb
AM
4475 if (eind->elf.got.glist != NULL)
4476 {
4477 if (edir->elf.got.glist != NULL)
4478 {
4479 struct got_entry **entp;
4480 struct got_entry *ent;
4481
4482 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4483 {
4484 struct got_entry *dent;
4485
4486 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4487 if (dent->addend == ent->addend
e717da7e 4488 && dent->owner == ent->owner
411e1bfb
AM
4489 && dent->tls_type == ent->tls_type)
4490 {
4491 dent->got.refcount += ent->got.refcount;
4492 *entp = ent->next;
4493 break;
4494 }
4495 if (dent == NULL)
4496 entp = &ent->next;
4497 }
4498 *entp = edir->elf.got.glist;
4499 }
4500
4501 edir->elf.got.glist = eind->elf.got.glist;
4502 eind->elf.got.glist = NULL;
4503 }
4504
4505 /* And plt entries. */
40d16e0b 4506 move_plt_plist (eind, edir);
411e1bfb 4507
fcfa13d2 4508 if (eind->elf.dynindx != -1)
411e1bfb 4509 {
fcfa13d2
AM
4510 if (edir->elf.dynindx != -1)
4511 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4512 edir->elf.dynstr_index);
411e1bfb
AM
4513 edir->elf.dynindx = eind->elf.dynindx;
4514 edir->elf.dynstr_index = eind->elf.dynstr_index;
4515 eind->elf.dynindx = -1;
4516 eind->elf.dynstr_index = 0;
4517 }
411e1bfb
AM
4518}
4519
8387904d
AM
4520/* Find the function descriptor hash entry from the given function code
4521 hash entry FH. Link the entries via their OH fields. */
4522
4523static struct ppc_link_hash_entry *
b31867b6 4524lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4525{
4526 struct ppc_link_hash_entry *fdh = fh->oh;
4527
4528 if (fdh == NULL)
4529 {
4530 const char *fd_name = fh->elf.root.root.string + 1;
4531
4532 fdh = (struct ppc_link_hash_entry *)
4533 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4534 if (fdh == NULL)
4535 return fdh;
4536
4537 fdh->is_func_descriptor = 1;
4538 fdh->oh = fh;
4539 fh->is_func = 1;
4540 fh->oh = fdh;
8387904d
AM
4541 }
4542
b31867b6 4543 return ppc_follow_link (fdh);
8387904d
AM
4544}
4545
bb700d78
AM
4546/* Make a fake function descriptor sym for the code sym FH. */
4547
4548static struct ppc_link_hash_entry *
4549make_fdh (struct bfd_link_info *info,
908b32fc 4550 struct ppc_link_hash_entry *fh)
bb700d78
AM
4551{
4552 bfd *abfd;
4553 asymbol *newsym;
4554 struct bfd_link_hash_entry *bh;
4555 struct ppc_link_hash_entry *fdh;
4556
4557 abfd = fh->elf.root.u.undef.abfd;
4558 newsym = bfd_make_empty_symbol (abfd);
4559 newsym->name = fh->elf.root.root.string + 1;
4560 newsym->section = bfd_und_section_ptr;
4561 newsym->value = 0;
908b32fc 4562 newsym->flags = BSF_WEAK;
bb700d78
AM
4563
4564 bh = NULL;
4565 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4566 newsym->flags, newsym->section,
4567 newsym->value, NULL, FALSE, FALSE,
4568 &bh))
4569 return NULL;
4570
4571 fdh = (struct ppc_link_hash_entry *) bh;
4572 fdh->elf.non_elf = 0;
908b32fc
AM
4573 fdh->fake = 1;
4574 fdh->is_func_descriptor = 1;
4575 fdh->oh = fh;
4576 fh->is_func = 1;
4577 fh->oh = fdh;
bb700d78
AM
4578 return fdh;
4579}
4580
8387904d
AM
4581/* Fix function descriptor symbols defined in .opd sections to be
4582 function type. */
555cd476
AM
4583
4584static bfd_boolean
c16153ae 4585ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4586 struct bfd_link_info *info,
555cd476 4587 Elf_Internal_Sym *isym,
b3fac117 4588 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4589 flagword *flags ATTRIBUTE_UNUSED,
4590 asection **sec,
4591 bfd_vma *value ATTRIBUTE_UNUSED)
4592{
f64b2e8d
NC
4593 if ((ibfd->flags & DYNAMIC) == 0
4594 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4595 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4596
e054468f 4597 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4598 {
4599 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4600 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4601 }
e054468f
AM
4602 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4603 ;
4604 else if (*sec != NULL
70cc837d 4605 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4606 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4607
555cd476
AM
4608 return TRUE;
4609}
4610
8387904d 4611/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4612 inclusion of a new ABI object archive that defines "bar".
4613 NAME is a symbol defined in an archive. Return a symbol in the hash
4614 table that might be satisfied by the archive symbols. */
8387904d
AM
4615
4616static struct elf_link_hash_entry *
4617ppc64_elf_archive_symbol_lookup (bfd *abfd,
4618 struct bfd_link_info *info,
4619 const char *name)
4620{
4621 struct elf_link_hash_entry *h;
4622 char *dot_name;
4623 size_t len;
4624
4625 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4626 if (h != NULL
4627 /* Don't return this sym if it is a fake function descriptor
4628 created by add_symbol_adjust. */
4629 && !(h->root.type == bfd_link_hash_undefweak
4630 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4631 return h;
4632
4633 if (name[0] == '.')
4634 return h;
4635
4636 len = strlen (name);
4637 dot_name = bfd_alloc (abfd, len + 2);
4638 if (dot_name == NULL)
4639 return (struct elf_link_hash_entry *) 0 - 1;
4640 dot_name[0] = '.';
4641 memcpy (dot_name + 1, name, len + 1);
4642 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4643 bfd_release (abfd, dot_name);
4644 return h;
4645}
4646
4647/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4648 new ABI object defines "bar". Well, at least, undefined dot symbols
4649 are made weak. This stops later archive searches from including an
4650 object if we already have a function descriptor definition. It also
35b0ce59
AM
4651 prevents the linker complaining about undefined symbols.
4652 We also check and correct mismatched symbol visibility here. The
4653 most restrictive visibility of the function descriptor and the
4654 function entry symbol is used. */
8387904d
AM
4655
4656static bfd_boolean
b3fac117 4657add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4658{
8387904d
AM
4659 struct ppc_link_hash_table *htab;
4660 struct ppc_link_hash_entry *fdh;
4661
b3fac117 4662 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4663 return TRUE;
4664
b3fac117
AM
4665 if (eh->elf.root.type == bfd_link_hash_warning)
4666 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4667
b3fac117
AM
4668 if (eh->elf.root.root.string[0] != '.')
4669 abort ();
8387904d 4670
b3fac117 4671 htab = ppc_hash_table (info);
4dfe6ac6
NC
4672 if (htab == NULL)
4673 return FALSE;
4674
b31867b6
AM
4675 fdh = lookup_fdh (eh, htab);
4676 if (fdh == NULL)
4677 {
4678 if (!info->relocatable
4679 && (eh->elf.root.type == bfd_link_hash_undefined
4680 || eh->elf.root.type == bfd_link_hash_undefweak)
4681 && eh->elf.ref_regular)
4682 {
4683 /* Make an undefweak function descriptor sym, which is enough to
4684 pull in an --as-needed shared lib, but won't cause link
4685 errors. Archives are handled elsewhere. */
4686 fdh = make_fdh (info, eh);
4687 if (fdh == NULL)
4688 return FALSE;
4689 fdh->elf.ref_regular = 1;
4690 }
bb700d78 4691 }
b31867b6 4692 else
8387904d 4693 {
35b0ce59
AM
4694 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4695 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4696 if (entry_vis < descr_vis)
4697 fdh->elf.other += entry_vis - descr_vis;
4698 else if (entry_vis > descr_vis)
4699 eh->elf.other += descr_vis - entry_vis;
4700
e87d886e
AM
4701 if ((fdh->elf.root.type == bfd_link_hash_defined
4702 || fdh->elf.root.type == bfd_link_hash_defweak)
4703 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4704 {
4705 eh->elf.root.type = bfd_link_hash_undefweak;
4706 eh->was_undefined = 1;
4707 htab->twiddled_syms = 1;
4708 }
8387904d 4709 }
99877b66 4710
8387904d
AM
4711 return TRUE;
4712}
4713
b3fac117
AM
4714/* Process list of dot-symbols we made in link_hash_newfunc. */
4715
8387904d 4716static bfd_boolean
7d9616d7 4717ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4718{
99877b66 4719 struct ppc_link_hash_table *htab;
b3fac117 4720 struct ppc_link_hash_entry **p, *eh;
433817dd 4721
0c8d6e5c 4722 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4723 return TRUE;
4dfe6ac6
NC
4724 htab = ppc_hash_table (info);
4725 if (htab == NULL)
4726 return FALSE;
35b0ce59 4727
0c8d6e5c 4728 if (is_ppc64_elf (ibfd))
b3fac117
AM
4729 {
4730 p = &htab->dot_syms;
4731 while ((eh = *p) != NULL)
4732 {
4733 *p = NULL;
4734 if (!add_symbol_adjust (eh, info))
4735 return FALSE;
4736 p = &eh->u.next_dot_sym;
4737 }
4738 }
4739
4740 /* Clear the list for non-ppc64 input files. */
4741 p = &htab->dot_syms;
4742 while ((eh = *p) != NULL)
4743 {
4744 *p = NULL;
4745 p = &eh->u.next_dot_sym;
4746 }
99877b66
AM
4747
4748 /* We need to fix the undefs list for any syms we have twiddled to
4749 undef_weak. */
4750 if (htab->twiddled_syms)
4751 {
77cfaee6 4752 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4753 htab->twiddled_syms = 0;
4754 }
b3fac117 4755 return TRUE;
8387904d
AM
4756}
4757
97fed1c9
JJ
4758/* Undo hash table changes when an --as-needed input file is determined
4759 not to be needed. */
4760
4761static bfd_boolean
4762ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4763 struct bfd_link_info *info)
4764{
4dfe6ac6
NC
4765 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4766
4767 if (htab == NULL)
4768 return FALSE;
4769
4770 htab->dot_syms = NULL;
97fed1c9
JJ
4771 return TRUE;
4772}
4773
aa374f67
AM
4774/* If --just-symbols against a final linked binary, then assume we need
4775 toc adjusting stubs when calling functions defined there. */
4776
4777static void
4778ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4779{
4780 if ((sec->flags & SEC_CODE) != 0
4781 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4782 && is_ppc64_elf (sec->owner))
4783 {
4784 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4785 if (got != NULL
4786 && got->size >= elf_backend_got_header_size
4787 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4788 sec->has_toc_reloc = 1;
4789 }
4790 _bfd_elf_link_just_syms (sec, info);
4791}
4792
e054468f 4793static struct plt_entry **
4ce794b7
AM
4794update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4795 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4796{
4797 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4798 struct plt_entry **local_plt;
f961d9dd 4799 unsigned char *local_got_tls_masks;
411e1bfb
AM
4800
4801 if (local_got_ents == NULL)
4802 {
4803 bfd_size_type size = symtab_hdr->sh_info;
4804
e054468f
AM
4805 size *= (sizeof (*local_got_ents)
4806 + sizeof (*local_plt)
4807 + sizeof (*local_got_tls_masks));
4ce794b7 4808 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4809 if (local_got_ents == NULL)
e054468f 4810 return NULL;
411e1bfb
AM
4811 elf_local_got_ents (abfd) = local_got_ents;
4812 }
4813
e054468f 4814 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4815 {
4816 struct got_entry *ent;
4817
4818 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4819 if (ent->addend == r_addend
4820 && ent->owner == abfd
4821 && ent->tls_type == tls_type)
411e1bfb
AM
4822 break;
4823 if (ent == NULL)
4824 {
4825 bfd_size_type amt = sizeof (*ent);
4ce794b7 4826 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4827 if (ent == NULL)
4828 return FALSE;
4829 ent->next = local_got_ents[r_symndx];
4830 ent->addend = r_addend;
e717da7e 4831 ent->owner = abfd;
411e1bfb 4832 ent->tls_type = tls_type;
927be08e 4833 ent->is_indirect = FALSE;
411e1bfb
AM
4834 ent->got.refcount = 0;
4835 local_got_ents[r_symndx] = ent;
4836 }
4837 ent->got.refcount += 1;
4838 }
4839
e054468f 4840 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4841 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 4842 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
4843
4844 return local_plt + r_symndx;
65f38f15
AM
4845}
4846
411e1bfb 4847static bfd_boolean
e054468f 4848update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4849{
411e1bfb 4850 struct plt_entry *ent;
1e2f5b6e 4851
e054468f 4852 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4853 if (ent->addend == addend)
4854 break;
4855 if (ent == NULL)
1e2f5b6e 4856 {
411e1bfb 4857 bfd_size_type amt = sizeof (*ent);
4ce794b7 4858 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4859 if (ent == NULL)
4860 return FALSE;
e054468f 4861 ent->next = *plist;
411e1bfb
AM
4862 ent->addend = addend;
4863 ent->plt.refcount = 0;
e054468f 4864 *plist = ent;
1e2f5b6e 4865 }
411e1bfb 4866 ent->plt.refcount += 1;
b34976b6 4867 return TRUE;
1e2f5b6e
AM
4868}
4869
e054468f
AM
4870static bfd_boolean
4871is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4872{
4873 return (r_type == R_PPC64_REL24
4874 || r_type == R_PPC64_REL14
4875 || r_type == R_PPC64_REL14_BRTAKEN
4876 || r_type == R_PPC64_REL14_BRNTAKEN
4877 || r_type == R_PPC64_ADDR24
4878 || r_type == R_PPC64_ADDR14
4879 || r_type == R_PPC64_ADDR14_BRTAKEN
4880 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4881}
4882
5bd4f169 4883/* Look through the relocs for a section during the first phase, and
65f38f15 4884 calculate needed space in the global offset table, procedure
5d1634d7 4885 linkage table, and dynamic reloc sections. */
5bd4f169 4886
b34976b6 4887static bfd_boolean
4ce794b7
AM
4888ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4889 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4890{
65f38f15 4891 struct ppc_link_hash_table *htab;
5bd4f169 4892 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4893 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4894 const Elf_Internal_Rela *rel;
4895 const Elf_Internal_Rela *rel_end;
5bd4f169 4896 asection *sreloc;
1e2f5b6e 4897 asection **opd_sym_map;
3a71aa26 4898 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 4899
1049f94e 4900 if (info->relocatable)
b34976b6 4901 return TRUE;
5bd4f169 4902
680a3378
AM
4903 /* Don't do anything special with non-loaded, non-alloced sections.
4904 In particular, any relocs in such sections should not affect GOT
4905 and PLT reference counting (ie. we don't allow them to create GOT
4906 or PLT entries), there's no possibility or desire to optimize TLS
4907 relocs, and there's not much point in propagating relocs to shared
4908 libs that the dynamic linker won't relocate. */
4909 if ((sec->flags & SEC_ALLOC) == 0)
4910 return TRUE;
4911
0c8d6e5c 4912 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4913
65f38f15 4914 htab = ppc_hash_table (info);
4dfe6ac6
NC
4915 if (htab == NULL)
4916 return FALSE;
4917
3a71aa26
AM
4918 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4919 FALSE, FALSE, TRUE);
4920 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4921 FALSE, FALSE, TRUE);
0ffa91dd 4922 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4923 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4924 sreloc = NULL;
1e2f5b6e 4925 opd_sym_map = NULL;
70cc837d 4926 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
4927 {
4928 /* Garbage collection needs some extra help with .opd sections.
4929 We don't want to necessarily keep everything referenced by
4930 relocs in .opd, as that would keep all functions. Instead,
4931 if we reference an .opd symbol (a function descriptor), we
4932 want to keep the function code symbol's section. This is
4933 easy for global symbols, but for local syms we need to keep
74f0fb50 4934 information about the associated function section. */
1e2f5b6e
AM
4935 bfd_size_type amt;
4936
74f0fb50 4937 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 4938 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4939 if (opd_sym_map == NULL)
b34976b6 4940 return FALSE;
74f0fb50 4941 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
4942 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4943 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4944 }
5bd4f169 4945
82bd7b59
AM
4946 if (htab->sfpr == NULL
4947 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4948 return FALSE;
82bd7b59 4949
5bd4f169
AM
4950 rel_end = relocs + sec->reloc_count;
4951 for (rel = relocs; rel < rel_end; rel++)
4952 {
4953 unsigned long r_symndx;
4954 struct elf_link_hash_entry *h;
04c9666a 4955 enum elf_ppc64_reloc_type r_type;
727fc41e 4956 int tls_type;
7c8fe5c4 4957 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 4958 struct plt_entry **ifunc;
5bd4f169
AM
4959
4960 r_symndx = ELF64_R_SYM (rel->r_info);
4961 if (r_symndx < symtab_hdr->sh_info)
4962 h = NULL;
4963 else
973a3492
L
4964 {
4965 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4966 h = elf_follow_link (h);
973a3492 4967 }
5bd4f169 4968
727fc41e 4969 tls_type = 0;
e054468f 4970 ifunc = NULL;
25f23106
AM
4971 if (h != NULL)
4972 {
4973 if (h->type == STT_GNU_IFUNC)
4974 {
4975 h->needs_plt = 1;
4976 ifunc = &h->plt.plist;
4977 }
4978 }
4979 else
4980 {
4981 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4982 abfd, r_symndx);
4983 if (isym == NULL)
4984 return FALSE;
4985
4986 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4987 {
4988 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4989 rel->r_addend, PLT_IFUNC);
4990 if (ifunc == NULL)
4991 return FALSE;
4992 }
4993 }
4ce794b7 4994 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
4995 if (is_branch_reloc (r_type))
4996 {
4997 if (h != NULL && (h == tga || h == dottga))
4998 {
4999 if (rel != relocs
5000 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5001 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5002 /* We have a new-style __tls_get_addr call with a marker
5003 reloc. */
5004 ;
5005 else
5006 /* Mark this section as having an old-style call. */
5007 sec->has_tls_get_addr_call = 1;
5008 }
727fc41e 5009
e054468f 5010 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
5011 if (ifunc != NULL
5012 && !update_plt_info (abfd, ifunc, rel->r_addend))
5013 return FALSE;
5014 }
727fc41e 5015
a33d1f77 5016 switch (r_type)
5bd4f169 5017 {
727fc41e
AM
5018 case R_PPC64_TLSGD:
5019 case R_PPC64_TLSLD:
5020 /* These special tls relocs tie a call to __tls_get_addr with
5021 its parameter symbol. */
5022 break;
5023
411e1bfb
AM
5024 case R_PPC64_GOT_TLSLD16:
5025 case R_PPC64_GOT_TLSLD16_LO:
5026 case R_PPC64_GOT_TLSLD16_HI:
5027 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5028 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5029 goto dogottls;
5030
5031 case R_PPC64_GOT_TLSGD16:
5032 case R_PPC64_GOT_TLSGD16_LO:
5033 case R_PPC64_GOT_TLSGD16_HI:
5034 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5035 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5036 goto dogottls;
5037
5038 case R_PPC64_GOT_TPREL16_DS:
5039 case R_PPC64_GOT_TPREL16_LO_DS:
5040 case R_PPC64_GOT_TPREL16_HI:
5041 case R_PPC64_GOT_TPREL16_HA:
1d483afe 5042 if (!info->executable)
411e1bfb
AM
5043 info->flags |= DF_STATIC_TLS;
5044 tls_type = TLS_TLS | TLS_TPREL;
5045 goto dogottls;
5046
5047 case R_PPC64_GOT_DTPREL16_DS:
5048 case R_PPC64_GOT_DTPREL16_LO_DS:
5049 case R_PPC64_GOT_DTPREL16_HI:
5050 case R_PPC64_GOT_DTPREL16_HA:
5051 tls_type = TLS_TLS | TLS_DTPREL;
5052 dogottls:
5053 sec->has_tls_reloc = 1;
5054 /* Fall thru */
5055
5bd4f169 5056 case R_PPC64_GOT16:
5bd4f169 5057 case R_PPC64_GOT16_DS:
65f38f15
AM
5058 case R_PPC64_GOT16_HA:
5059 case R_PPC64_GOT16_HI:
5060 case R_PPC64_GOT16_LO:
5bd4f169 5061 case R_PPC64_GOT16_LO_DS:
65f38f15 5062 /* This symbol requires a global offset table entry. */
4c52953f 5063 sec->has_toc_reloc = 1;
33c0ec9d
AM
5064 if (r_type == R_PPC64_GOT_TLSLD16
5065 || r_type == R_PPC64_GOT_TLSGD16
5066 || r_type == R_PPC64_GOT_TPREL16_DS
5067 || r_type == R_PPC64_GOT_DTPREL16_DS
5068 || r_type == R_PPC64_GOT16
5069 || r_type == R_PPC64_GOT16_DS)
5070 {
5071 htab->do_multi_toc = 1;
d77c8a4b 5072 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5073 }
5074
e717da7e
AM
5075 if (ppc64_elf_tdata (abfd)->got == NULL
5076 && !create_got_section (abfd, info))
b34976b6 5077 return FALSE;
5bd4f169
AM
5078
5079 if (h != NULL)
5080 {
411e1bfb
AM
5081 struct ppc_link_hash_entry *eh;
5082 struct got_entry *ent;
65f38f15 5083
411e1bfb
AM
5084 eh = (struct ppc_link_hash_entry *) h;
5085 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5086 if (ent->addend == rel->r_addend
e717da7e 5087 && ent->owner == abfd
411e1bfb
AM
5088 && ent->tls_type == tls_type)
5089 break;
5090 if (ent == NULL)
5bd4f169 5091 {
411e1bfb 5092 bfd_size_type amt = sizeof (*ent);
4ce794b7 5093 ent = bfd_alloc (abfd, amt);
411e1bfb 5094 if (ent == NULL)
b34976b6 5095 return FALSE;
411e1bfb
AM
5096 ent->next = eh->elf.got.glist;
5097 ent->addend = rel->r_addend;
e717da7e 5098 ent->owner = abfd;
411e1bfb 5099 ent->tls_type = tls_type;
927be08e 5100 ent->is_indirect = FALSE;
411e1bfb
AM
5101 ent->got.refcount = 0;
5102 eh->elf.got.glist = ent;
5bd4f169 5103 }
411e1bfb 5104 ent->got.refcount += 1;
e7b938ca 5105 eh->tls_mask |= tls_type;
5bd4f169 5106 }
411e1bfb
AM
5107 else
5108 /* This is a global offset table entry for a local symbol. */
5109 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5110 rel->r_addend, tls_type))
5111 return FALSE;
5bd4f169
AM
5112 break;
5113
5bd4f169 5114 case R_PPC64_PLT16_HA:
65f38f15
AM
5115 case R_PPC64_PLT16_HI:
5116 case R_PPC64_PLT16_LO:
5117 case R_PPC64_PLT32:
5118 case R_PPC64_PLT64:
5bd4f169 5119 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5120 actually build the entry in adjust_dynamic_symbol,
5121 because this might be a case of linking PIC code without
5122 linking in any dynamic objects, in which case we don't
5123 need to generate a procedure linkage table after all. */
5bd4f169
AM
5124 if (h == NULL)
5125 {
5126 /* It does not make sense to have a procedure linkage
3fad3c7c 5127 table entry for a local symbol. */
5bd4f169 5128 bfd_set_error (bfd_error_bad_value);
b34976b6 5129 return FALSE;
5bd4f169 5130 }
411e1bfb 5131 else
e054468f
AM
5132 {
5133 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5134 return FALSE;
5135 h->needs_plt = 1;
5136 if (h->root.root.string[0] == '.'
5137 && h->root.root.string[1] != '\0')
5138 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5139 }
5bd4f169
AM
5140 break;
5141
5142 /* The following relocations don't need to propagate the
5143 relocation if linking a shared object since they are
5144 section relative. */
5145 case R_PPC64_SECTOFF:
5146 case R_PPC64_SECTOFF_LO:
5147 case R_PPC64_SECTOFF_HI:
5148 case R_PPC64_SECTOFF_HA:
5149 case R_PPC64_SECTOFF_DS:
5150 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5151 case R_PPC64_DTPREL16:
5152 case R_PPC64_DTPREL16_LO:
5153 case R_PPC64_DTPREL16_HI:
5154 case R_PPC64_DTPREL16_HA:
5155 case R_PPC64_DTPREL16_DS:
5156 case R_PPC64_DTPREL16_LO_DS:
5157 case R_PPC64_DTPREL16_HIGHER:
5158 case R_PPC64_DTPREL16_HIGHERA:
5159 case R_PPC64_DTPREL16_HIGHEST:
5160 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5161 break;
5162
ad8e1ba5 5163 /* Nor do these. */
25f23106
AM
5164 case R_PPC64_REL16:
5165 case R_PPC64_REL16_LO:
5166 case R_PPC64_REL16_HI:
5167 case R_PPC64_REL16_HA:
5168 break;
5169
ad8e1ba5 5170 case R_PPC64_TOC16:
33c0ec9d
AM
5171 case R_PPC64_TOC16_DS:
5172 htab->do_multi_toc = 1;
d77c8a4b 5173 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5174 case R_PPC64_TOC16_LO:
5175 case R_PPC64_TOC16_HI:
5176 case R_PPC64_TOC16_HA:
ad8e1ba5 5177 case R_PPC64_TOC16_LO_DS:
4c52953f 5178 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5179 break;
5180
5bd4f169
AM
5181 /* This relocation describes the C++ object vtable hierarchy.
5182 Reconstruct it for later use during GC. */
5183 case R_PPC64_GNU_VTINHERIT:
c152c796 5184 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5185 return FALSE;
5bd4f169
AM
5186 break;
5187
5188 /* This relocation describes which C++ vtable entries are actually
5189 used. Record for later use during GC. */
5190 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5191 BFD_ASSERT (h != NULL);
5192 if (h != NULL
5193 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5194 return FALSE;
5bd4f169
AM
5195 break;
5196
721956f4
AM
5197 case R_PPC64_REL14:
5198 case R_PPC64_REL14_BRTAKEN:
5199 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5200 {
5201 asection *dest = NULL;
5202
5203 /* Heuristic: If jumping outside our section, chances are
5204 we are going to need a stub. */
5205 if (h != NULL)
5206 {
5207 /* If the sym is weak it may be overridden later, so
5208 don't assume we know where a weak sym lives. */
5209 if (h->root.type == bfd_link_hash_defined)
5210 dest = h->root.u.def.section;
5211 }
5212 else
87d72d41
AM
5213 {
5214 Elf_Internal_Sym *isym;
5215
5216 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5217 abfd, r_symndx);
5218 if (isym == NULL)
5219 return FALSE;
5220
5221 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5222 }
5223
220c76dd 5224 if (dest != sec)
7c8fe5c4 5225 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5226 }
721956f4
AM
5227 /* Fall through. */
5228
5d1634d7 5229 case R_PPC64_REL24:
e054468f 5230 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5231 {
5232 /* We may need a .plt entry if the function this reloc
5233 refers to is in a shared lib. */
e054468f 5234 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5235 return FALSE;
e054468f
AM
5236 h->needs_plt = 1;
5237 if (h->root.root.string[0] == '.'
5238 && h->root.root.string[1] != '\0')
5239 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5240 if (h == tga || h == dottga)
411e1bfb 5241 sec->has_tls_reloc = 1;
411e1bfb
AM
5242 }
5243 break;
5244
5245 case R_PPC64_TPREL64:
5246 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5247 if (!info->executable)
411e1bfb
AM
5248 info->flags |= DF_STATIC_TLS;
5249 goto dotlstoc;
5250
5251 case R_PPC64_DTPMOD64:
5252 if (rel + 1 < rel_end
5253 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5254 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5255 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5256 else
951fd09b 5257 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5258 goto dotlstoc;
5259
5260 case R_PPC64_DTPREL64:
5261 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5262 if (rel != relocs
5263 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5264 && rel[-1].r_offset == rel->r_offset - 8)
5265 /* This is the second reloc of a dtpmod, dtprel pair.
5266 Don't mark with TLS_DTPREL. */
5267 goto dodyn;
5268
5269 dotlstoc:
5270 sec->has_tls_reloc = 1;
5271 if (h != NULL)
5272 {
5273 struct ppc_link_hash_entry *eh;
5274 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5275 eh->tls_mask |= tls_type;
411e1bfb
AM
5276 }
5277 else
5278 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5279 rel->r_addend, tls_type))
5280 return FALSE;
5281
7c8fe5c4
AM
5282 ppc64_sec = ppc64_elf_section_data (sec);
5283 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5284 {
3a71aa26
AM
5285 bfd_size_type amt;
5286
e7b938ca 5287 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5288 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5289 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5290 if (ppc64_sec->u.toc.symndx == NULL)
5291 return FALSE;
5292 amt = sec->size * sizeof (bfd_vma) / 8;
5293 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5294 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5295 return FALSE;
7c8fe5c4
AM
5296 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5297 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5298 }
5299 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5300 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5301 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5302
5303 /* Mark the second slot of a GD or LD entry.
5304 -1 to indicate GD and -2 to indicate LD. */
5305 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5306 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5307 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5308 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5309 goto dodyn;
5310
5311 case R_PPC64_TPREL16:
5312 case R_PPC64_TPREL16_LO:
5313 case R_PPC64_TPREL16_HI:
5314 case R_PPC64_TPREL16_HA:
5315 case R_PPC64_TPREL16_DS:
5316 case R_PPC64_TPREL16_LO_DS:
5317 case R_PPC64_TPREL16_HIGHER:
5318 case R_PPC64_TPREL16_HIGHERA:
5319 case R_PPC64_TPREL16_HIGHEST:
5320 case R_PPC64_TPREL16_HIGHESTA:
5321 if (info->shared)
5322 {
1d483afe
AM
5323 if (!info->executable)
5324 info->flags |= DF_STATIC_TLS;
411e1bfb 5325 goto dodyn;
5d1634d7
AM
5326 }
5327 break;
5328
e86ce104 5329 case R_PPC64_ADDR64:
1e2f5b6e 5330 if (opd_sym_map != NULL
1e2f5b6e 5331 && rel + 1 < rel_end
4ce794b7 5332 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5333 {
8387904d
AM
5334 if (h != NULL)
5335 {
5336 if (h->root.root.string[0] == '.'
5337 && h->root.root.string[1] != 0
b31867b6 5338 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5339 ;
5340 else
5341 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5342 }
5343 else
5344 {
5345 asection *s;
87d72d41 5346 Elf_Internal_Sym *isym;
1e2f5b6e 5347
87d72d41
AM
5348 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5349 abfd, r_symndx);
5350 if (isym == NULL)
8387904d 5351 return FALSE;
87d72d41
AM
5352
5353 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5354 if (s != NULL && s != sec)
3f764659 5355 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5356 }
1e2f5b6e 5357 }
e86ce104
AM
5358 /* Fall through. */
5359
04c9666a 5360 case R_PPC64_REL30:
5bd4f169 5361 case R_PPC64_REL32:
04c9666a 5362 case R_PPC64_REL64:
65f38f15
AM
5363 case R_PPC64_ADDR14:
5364 case R_PPC64_ADDR14_BRNTAKEN:
5365 case R_PPC64_ADDR14_BRTAKEN:
5366 case R_PPC64_ADDR16:
5367 case R_PPC64_ADDR16_DS:
5368 case R_PPC64_ADDR16_HA:
5369 case R_PPC64_ADDR16_HI:
5370 case R_PPC64_ADDR16_HIGHER:
5371 case R_PPC64_ADDR16_HIGHERA:
5372 case R_PPC64_ADDR16_HIGHEST:
5373 case R_PPC64_ADDR16_HIGHESTA:
5374 case R_PPC64_ADDR16_LO:
5375 case R_PPC64_ADDR16_LO_DS:
5376 case R_PPC64_ADDR24:
65f38f15 5377 case R_PPC64_ADDR32:
65f38f15
AM
5378 case R_PPC64_UADDR16:
5379 case R_PPC64_UADDR32:
5380 case R_PPC64_UADDR64:
5bd4f169 5381 case R_PPC64_TOC:
81848ca0
AM
5382 if (h != NULL && !info->shared)
5383 /* We may need a copy reloc. */
f5385ebf 5384 h->non_got_ref = 1;
81848ca0 5385
41bd81ab 5386 /* Don't propagate .opd relocs. */
1e2f5b6e 5387 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5388 break;
e86ce104 5389
65f38f15
AM
5390 /* If we are creating a shared library, and this is a reloc
5391 against a global symbol, or a non PC relative reloc
5392 against a local symbol, then we need to copy the reloc
5393 into the shared library. However, if we are linking with
5394 -Bsymbolic, we do not need to copy a reloc against a
5395 global symbol which is defined in an object we are
5396 including in the link (i.e., DEF_REGULAR is set). At
5397 this point we have not seen all the input files, so it is
5398 possible that DEF_REGULAR is not set now but will be set
5399 later (it is never cleared). In case of a weak definition,
5400 DEF_REGULAR may be cleared later by a strong definition in
5401 a shared library. We account for that possibility below by
f4656909 5402 storing information in the dyn_relocs field of the hash
65f38f15
AM
5403 table entry. A similar situation occurs when creating
5404 shared libraries and symbol visibility changes render the
5405 symbol local.
5406
5407 If on the other hand, we are creating an executable, we
5408 may need to keep relocations for symbols satisfied by a
5409 dynamic library if we manage to avoid copy relocs for the
5410 symbol. */
411e1bfb 5411 dodyn:
65f38f15 5412 if ((info->shared
1d483afe 5413 && (must_be_dyn_reloc (info, r_type)
65f38f15
AM
5414 || (h != NULL
5415 && (! info->symbolic
5416 || h->root.type == bfd_link_hash_defweak
f5385ebf 5417 || !h->def_regular))))
f4656909
AM
5418 || (ELIMINATE_COPY_RELOCS
5419 && !info->shared
65f38f15
AM
5420 && h != NULL
5421 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5422 || !h->def_regular))
5423 || (!info->shared
5424 && ifunc != NULL))
5bd4f169 5425 {
6061a67d
AM
5426 struct elf_dyn_relocs *p;
5427 struct elf_dyn_relocs **head;
ec338859 5428
65f38f15
AM
5429 /* We must copy these reloc types into the output file.
5430 Create a reloc section in dynobj and make room for
5431 this reloc. */
5bd4f169
AM
5432 if (sreloc == NULL)
5433 {
83bac4b0
NC
5434 sreloc = _bfd_elf_make_dynamic_reloc_section
5435 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5436
5bd4f169 5437 if (sreloc == NULL)
83bac4b0 5438 return FALSE;
5bd4f169
AM
5439 }
5440
65f38f15
AM
5441 /* If this is a global symbol, we count the number of
5442 relocations we need for this symbol. */
5443 if (h != NULL)
5444 {
ec338859 5445 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
5446 }
5447 else
5448 {
ec338859
AM
5449 /* Track dynamic relocs needed for local syms too.
5450 We really need local syms available to do this
5451 easily. Oh well. */
ec338859 5452 asection *s;
6edfbbad 5453 void *vpp;
87d72d41 5454 Elf_Internal_Sym *isym;
6edfbbad 5455
87d72d41
AM
5456 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5457 abfd, r_symndx);
5458 if (isym == NULL)
b34976b6 5459 return FALSE;
ec338859 5460
87d72d41
AM
5461 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5462 if (s == NULL)
5463 s = sec;
5464
6edfbbad 5465 vpp = &elf_section_data (s)->local_dynrel;
6061a67d 5466 head = (struct elf_dyn_relocs **) vpp;
65f38f15 5467 }
ec338859
AM
5468
5469 p = *head;
5470 if (p == NULL || p->sec != sec)
5471 {
4ce794b7 5472 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 5473 if (p == NULL)
b34976b6 5474 return FALSE;
ec338859
AM
5475 p->next = *head;
5476 *head = p;
5477 p->sec = sec;
5478 p->count = 0;
5479 p->pc_count = 0;
5480 }
5481
5482 p->count += 1;
1d483afe 5483 if (!must_be_dyn_reloc (info, r_type))
ec338859 5484 p->pc_count += 1;
65f38f15 5485 }
5bd4f169 5486 break;
65f38f15
AM
5487
5488 default:
96e0dda4 5489 break;
5bd4f169
AM
5490 }
5491 }
5492
b34976b6 5493 return TRUE;
5bd4f169
AM
5494}
5495
8387904d
AM
5496/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5497 of the code entry point, and its section. */
5498
5499static bfd_vma
5500opd_entry_value (asection *opd_sec,
5501 bfd_vma offset,
5502 asection **code_sec,
5503 bfd_vma *code_off)
5504{
5505 bfd *opd_bfd = opd_sec->owner;
8860955f 5506 Elf_Internal_Rela *relocs;
8387904d 5507 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5508 bfd_vma val;
8387904d 5509
4b85d634
AM
5510 /* No relocs implies we are linking a --just-symbols object. */
5511 if (opd_sec->reloc_count == 0)
5512 {
aa374f67
AM
5513 char buf[8];
5514
5515 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
4b85d634 5516 return (bfd_vma) -1;
3b36f7e6 5517
aa374f67 5518 val = bfd_get_64 (opd_bfd, buf);
4b85d634
AM
5519 if (code_sec != NULL)
5520 {
5521 asection *sec, *likely = NULL;
5522 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5523 if (sec->vma <= val
5524 && (sec->flags & SEC_LOAD) != 0
5525 && (sec->flags & SEC_ALLOC) != 0)
5526 likely = sec;
5527 if (likely != NULL)
5528 {
5529 *code_sec = likely;
5530 if (code_off != NULL)
5531 *code_off = val - likely->vma;
5532 }
5533 }
5534 return val;
5535 }
5536
0c8d6e5c 5537 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5538
8860955f
AM
5539 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5540 if (relocs == NULL)
5541 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5542
8387904d 5543 /* Go find the opd reloc at the sym address. */
8860955f 5544 lo = relocs;
8387904d
AM
5545 BFD_ASSERT (lo != NULL);
5546 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5547 val = (bfd_vma) -1;
8387904d
AM
5548 while (lo < hi)
5549 {
5550 look = lo + (hi - lo) / 2;
5551 if (look->r_offset < offset)
5552 lo = look + 1;
5553 else if (look->r_offset > offset)
5554 hi = look;
5555 else
5556 {
0ffa91dd
NC
5557 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5558
8387904d
AM
5559 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5560 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5561 {
5562 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5563 asection *sec;
5564
5565 if (symndx < symtab_hdr->sh_info)
5566 {
5567 Elf_Internal_Sym *sym;
5568
5569 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5570 if (sym == NULL)
5571 {
5572 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5573 symtab_hdr->sh_info,
5574 0, NULL, NULL, NULL);
5575 if (sym == NULL)
645ea6a9 5576 break;
8387904d
AM
5577 symtab_hdr->contents = (bfd_byte *) sym;
5578 }
5579
5580 sym += symndx;
5581 val = sym->st_value;
cb33740c 5582 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5583 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5584 }
5585 else
5586 {
5587 struct elf_link_hash_entry **sym_hashes;
5588 struct elf_link_hash_entry *rh;
5589
5590 sym_hashes = elf_sym_hashes (opd_bfd);
5591 rh = sym_hashes[symndx - symtab_hdr->sh_info];
b31867b6 5592 rh = elf_follow_link (rh);
8387904d
AM
5593 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5594 || rh->root.type == bfd_link_hash_defweak);
5595 val = rh->root.u.def.value;
5596 sec = rh->root.u.def.section;
5597 }
5598 val += look->r_addend;
5599 if (code_off != NULL)
5600 *code_off = val;
5601 if (code_sec != NULL)
5602 *code_sec = sec;
5603 if (sec != NULL && sec->output_section != NULL)
5604 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5605 }
5606 break;
5607 }
5608 }
645ea6a9 5609
645ea6a9 5610 return val;
8387904d
AM
5611}
5612
854b41e7
AM
5613/* Return true if symbol is defined in a regular object file. */
5614
5615static bfd_boolean
5616is_static_defined (struct elf_link_hash_entry *h)
5617{
5618 return ((h->root.type == bfd_link_hash_defined
5619 || h->root.type == bfd_link_hash_defweak)
5620 && h->root.u.def.section != NULL
5621 && h->root.u.def.section->output_section != NULL);
5622}
5623
b31867b6
AM
5624/* If FDH is a function descriptor symbol, return the associated code
5625 entry symbol if it is defined. Return NULL otherwise. */
5626
5627static struct ppc_link_hash_entry *
5628defined_code_entry (struct ppc_link_hash_entry *fdh)
5629{
5630 if (fdh->is_func_descriptor)
5631 {
5632 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5633 if (fh->elf.root.type == bfd_link_hash_defined
5634 || fh->elf.root.type == bfd_link_hash_defweak)
5635 return fh;
5636 }
5637 return NULL;
5638}
5639
5640/* If FH is a function code entry symbol, return the associated
5641 function descriptor symbol if it is defined. Return NULL otherwise. */
5642
5643static struct ppc_link_hash_entry *
5644defined_func_desc (struct ppc_link_hash_entry *fh)
5645{
5646 if (fh->oh != NULL
5647 && fh->oh->is_func_descriptor)
5648 {
5649 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5650 if (fdh->elf.root.type == bfd_link_hash_defined
5651 || fdh->elf.root.type == bfd_link_hash_defweak)
5652 return fdh;
5653 }
5654 return NULL;
5655}
5656
74f0fb50
AM
5657/* Mark all our entry sym sections, both opd and code section. */
5658
5659static void
5660ppc64_elf_gc_keep (struct bfd_link_info *info)
5661{
5662 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5663 struct bfd_sym_chain *sym;
5664
4dfe6ac6
NC
5665 if (htab == NULL)
5666 return;
5667
74f0fb50
AM
5668 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5669 {
b31867b6 5670 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5671 asection *sec;
5672
5673 eh = (struct ppc_link_hash_entry *)
b31867b6 5674 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5675 if (eh == NULL)
5676 continue;
5677 if (eh->elf.root.type != bfd_link_hash_defined
5678 && eh->elf.root.type != bfd_link_hash_defweak)
5679 continue;
5680
b31867b6
AM
5681 fh = defined_code_entry (eh);
5682 if (fh != NULL)
74f0fb50 5683 {
b31867b6 5684 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5685 sec->flags |= SEC_KEEP;
5686 }
5687 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5688 && opd_entry_value (eh->elf.root.u.def.section,
5689 eh->elf.root.u.def.value,
5690 &sec, NULL) != (bfd_vma) -1)
5691 sec->flags |= SEC_KEEP;
5692
5693 sec = eh->elf.root.u.def.section;
5694 sec->flags |= SEC_KEEP;
5695 }
5696}
5697
64d03ab5
AM
5698/* Mark sections containing dynamically referenced symbols. When
5699 building shared libraries, we must assume that any visible symbol is
5700 referenced. */
5701
5702static bfd_boolean
5703ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5704{
5705 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5706 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5707 struct ppc_link_hash_entry *fdh;
64d03ab5 5708
64d03ab5 5709 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5710 fdh = defined_func_desc (eh);
5711 if (fdh != NULL)
5712 eh = fdh;
64d03ab5
AM
5713
5714 if ((eh->elf.root.type == bfd_link_hash_defined
5715 || eh->elf.root.type == bfd_link_hash_defweak)
5716 && (eh->elf.ref_dynamic
5717 || (!info->executable
5718 && eh->elf.def_regular
5719 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
4c58e0d8
AM
5720 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5721 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
5722 || !bfd_hide_sym_by_version (info->version_info,
5723 eh->elf.root.root.string)))))
64d03ab5
AM
5724 {
5725 asection *code_sec;
b31867b6 5726 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5727
5728 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5729
5730 /* Function descriptor syms cause the associated
5731 function code sym section to be marked. */
b31867b6
AM
5732 fh = defined_code_entry (eh);
5733 if (fh != NULL)
5734 {
5735 code_sec = fh->elf.root.u.def.section;
5736 code_sec->flags |= SEC_KEEP;
5737 }
64d03ab5
AM
5738 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5739 && opd_entry_value (eh->elf.root.u.def.section,
5740 eh->elf.root.u.def.value,
5741 &code_sec, NULL) != (bfd_vma) -1)
5742 code_sec->flags |= SEC_KEEP;
5743 }
5744
5745 return TRUE;
5746}
5747
5bd4f169
AM
5748/* Return the section that should be marked against GC for a given
5749 relocation. */
5750
5751static asection *
4ce794b7 5752ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5753 struct bfd_link_info *info,
4ce794b7
AM
5754 Elf_Internal_Rela *rel,
5755 struct elf_link_hash_entry *h,
5756 Elf_Internal_Sym *sym)
5bd4f169 5757{
ccfa59ea
AM
5758 asection *rsec;
5759
ccfa59ea
AM
5760 /* Syms return NULL if we're marking .opd, so we avoid marking all
5761 function sections, as all functions are referenced in .opd. */
5762 rsec = NULL;
5763 if (get_opd_info (sec) != NULL)
5764 return rsec;
1e2f5b6e 5765
5bd4f169
AM
5766 if (h != NULL)
5767 {
04c9666a 5768 enum elf_ppc64_reloc_type r_type;
b31867b6 5769 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5770
4ce794b7 5771 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5772 switch (r_type)
5bd4f169
AM
5773 {
5774 case R_PPC64_GNU_VTINHERIT:
5775 case R_PPC64_GNU_VTENTRY:
5776 break;
5777
5778 default:
5779 switch (h->root.type)
5780 {
5781 case bfd_link_hash_defined:
5782 case bfd_link_hash_defweak:
ccfa59ea 5783 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5784 fdh = defined_func_desc (eh);
5785 if (fdh != NULL)
5786 eh = fdh;
1e2f5b6e
AM
5787
5788 /* Function descriptor syms cause the associated
5789 function code sym section to be marked. */
b31867b6
AM
5790 fh = defined_code_entry (eh);
5791 if (fh != NULL)
ccfa59ea
AM
5792 {
5793 /* They also mark their opd section. */
74f0fb50 5794 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5795
b31867b6 5796 rsec = fh->elf.root.u.def.section;
ccfa59ea 5797 }
8387904d
AM
5798 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5799 && opd_entry_value (eh->elf.root.u.def.section,
5800 eh->elf.root.u.def.value,
5801 &rsec, NULL) != (bfd_vma) -1)
74f0fb50 5802 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5803 else
1e2f5b6e
AM
5804 rsec = h->root.u.def.section;
5805 break;
5bd4f169
AM
5806
5807 case bfd_link_hash_common:
1e2f5b6e
AM
5808 rsec = h->root.u.c.p->section;
5809 break;
5bd4f169
AM
5810
5811 default:
fb34365b 5812 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5813 }
5814 }
5815 }
5816 else
5817 {
74f0fb50 5818 struct _opd_sec_data *opd;
1e2f5b6e
AM
5819
5820 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5821 opd = get_opd_info (rsec);
5822 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5823 {
74f0fb50 5824 rsec->gc_mark = 1;
ccfa59ea 5825
74f0fb50 5826 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5827 }
5bd4f169
AM
5828 }
5829
1e2f5b6e 5830 return rsec;
5bd4f169
AM
5831}
5832
65f38f15
AM
5833/* Update the .got, .plt. and dynamic reloc reference counts for the
5834 section being removed. */
5bd4f169 5835
b34976b6 5836static bfd_boolean
4ce794b7
AM
5837ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5838 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5839{
411e1bfb 5840 struct ppc_link_hash_table *htab;
5bd4f169
AM
5841 Elf_Internal_Shdr *symtab_hdr;
5842 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5843 struct got_entry **local_got_ents;
5bd4f169 5844 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5845
7dda2462
TG
5846 if (info->relocatable)
5847 return TRUE;
5848
680a3378
AM
5849 if ((sec->flags & SEC_ALLOC) == 0)
5850 return TRUE;
5851
ec338859
AM
5852 elf_section_data (sec)->local_dynrel = NULL;
5853
411e1bfb 5854 htab = ppc_hash_table (info);
4dfe6ac6
NC
5855 if (htab == NULL)
5856 return FALSE;
5857
0ffa91dd 5858 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5859 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5860 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5861
5862 relend = relocs + sec->reloc_count;
5863 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5864 {
5865 unsigned long r_symndx;
04c9666a 5866 enum elf_ppc64_reloc_type r_type;
58ac9f71 5867 struct elf_link_hash_entry *h = NULL;
f961d9dd 5868 unsigned char tls_type = 0;
5bd4f169 5869
a33d1f77 5870 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5871 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5872 if (r_symndx >= symtab_hdr->sh_info)
5873 {
5874 struct ppc_link_hash_entry *eh;
6061a67d
AM
5875 struct elf_dyn_relocs **pp;
5876 struct elf_dyn_relocs *p;
58ac9f71
AM
5877
5878 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5879 h = elf_follow_link (h);
58ac9f71
AM
5880 eh = (struct ppc_link_hash_entry *) h;
5881
5882 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5883 if (p->sec == sec)
5884 {
5885 /* Everything must go for SEC. */
5886 *pp = p->next;
5887 break;
5888 }
5889 }
5890
e054468f
AM
5891 if (is_branch_reloc (r_type))
5892 {
5893 struct plt_entry **ifunc = NULL;
5894 if (h != NULL)
5895 {
5896 if (h->type == STT_GNU_IFUNC)
5897 ifunc = &h->plt.plist;
5898 }
5899 else if (local_got_ents != NULL)
5900 {
5901 struct plt_entry **local_plt = (struct plt_entry **)
5902 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5903 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
5904 (local_plt + symtab_hdr->sh_info);
5905 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5906 ifunc = local_plt + r_symndx;
5907 }
5908 if (ifunc != NULL)
5909 {
5910 struct plt_entry *ent;
5911
5912 for (ent = *ifunc; ent != NULL; ent = ent->next)
5913 if (ent->addend == rel->r_addend)
5914 break;
5915 if (ent == NULL)
5916 abort ();
5917 if (ent->plt.refcount > 0)
5918 ent->plt.refcount -= 1;
5919 continue;
5920 }
5921 }
5922
a33d1f77
AM
5923 switch (r_type)
5924 {
411e1bfb
AM
5925 case R_PPC64_GOT_TLSLD16:
5926 case R_PPC64_GOT_TLSLD16_LO:
5927 case R_PPC64_GOT_TLSLD16_HI:
5928 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5929 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5930 goto dogot;
5931
5932 case R_PPC64_GOT_TLSGD16:
5933 case R_PPC64_GOT_TLSGD16_LO:
5934 case R_PPC64_GOT_TLSGD16_HI:
5935 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5936 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5937 goto dogot;
5938
5939 case R_PPC64_GOT_TPREL16_DS:
5940 case R_PPC64_GOT_TPREL16_LO_DS:
5941 case R_PPC64_GOT_TPREL16_HI:
5942 case R_PPC64_GOT_TPREL16_HA:
5943 tls_type = TLS_TLS | TLS_TPREL;
5944 goto dogot;
5945
5946 case R_PPC64_GOT_DTPREL16_DS:
5947 case R_PPC64_GOT_DTPREL16_LO_DS:
5948 case R_PPC64_GOT_DTPREL16_HI:
5949 case R_PPC64_GOT_DTPREL16_HA:
5950 tls_type = TLS_TLS | TLS_DTPREL;
5951 goto dogot;
5952
a33d1f77
AM
5953 case R_PPC64_GOT16:
5954 case R_PPC64_GOT16_DS:
5955 case R_PPC64_GOT16_HA:
5956 case R_PPC64_GOT16_HI:
5957 case R_PPC64_GOT16_LO:
5958 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5959 dogot:
5960 {
5961 struct got_entry *ent;
5962
58ac9f71
AM
5963 if (h != NULL)
5964 ent = h->got.glist;
411e1bfb
AM
5965 else
5966 ent = local_got_ents[r_symndx];
5967
5968 for (; ent != NULL; ent = ent->next)
5969 if (ent->addend == rel->r_addend
e717da7e 5970 && ent->owner == abfd
411e1bfb
AM
5971 && ent->tls_type == tls_type)
5972 break;
5973 if (ent == NULL)
5974 abort ();
5975 if (ent->got.refcount > 0)
5976 ent->got.refcount -= 1;
5977 }
a33d1f77 5978 break;
65f38f15 5979
a33d1f77
AM
5980 case R_PPC64_PLT16_HA:
5981 case R_PPC64_PLT16_HI:
5982 case R_PPC64_PLT16_LO:
5983 case R_PPC64_PLT32:
5984 case R_PPC64_PLT64:
721956f4
AM
5985 case R_PPC64_REL14:
5986 case R_PPC64_REL14_BRNTAKEN:
5987 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5988 case R_PPC64_REL24:
58ac9f71 5989 if (h != NULL)
5d1634d7 5990 {
411e1bfb
AM
5991 struct plt_entry *ent;
5992
411e1bfb
AM
5993 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5994 if (ent->addend == rel->r_addend)
5995 break;
7c6c1722 5996 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 5997 ent->plt.refcount -= 1;
5d1634d7 5998 }
e86ce104 5999 break;
5d1634d7 6000
a33d1f77
AM
6001 default:
6002 break;
6003 }
6004 }
b34976b6 6005 return TRUE;
5bd4f169
AM
6006}
6007
deb0e272
AM
6008/* The maximum size of .sfpr. */
6009#define SFPR_MAX (218*4)
6010
6011struct sfpr_def_parms
6012{
699733f6
AM
6013 const char name[12];
6014 unsigned char lo, hi;
deb0e272
AM
6015 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6016 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6017};
6018
6019/* Auto-generate _save*, _rest* functions in .sfpr. */
6020
4dfe6ac6 6021static bfd_boolean
deb0e272
AM
6022sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6023{
6024 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6025 unsigned int i;
6026 size_t len = strlen (parm->name);
6027 bfd_boolean writing = FALSE;
699733f6 6028 char sym[16];
deb0e272 6029
4dfe6ac6
NC
6030 if (htab == NULL)
6031 return FALSE;
6032
deb0e272
AM
6033 memcpy (sym, parm->name, len);
6034 sym[len + 2] = 0;
6035
6036 for (i = parm->lo; i <= parm->hi; i++)
6037 {
6038 struct elf_link_hash_entry *h;
6039
6040 sym[len + 0] = i / 10 + '0';
6041 sym[len + 1] = i % 10 + '0';
6042 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6043 if (h != NULL
f5385ebf 6044 && !h->def_regular)
deb0e272
AM
6045 {
6046 h->root.type = bfd_link_hash_defined;
6047 h->root.u.def.section = htab->sfpr;
6048 h->root.u.def.value = htab->sfpr->size;
6049 h->type = STT_FUNC;
f5385ebf 6050 h->def_regular = 1;
deb0e272
AM
6051 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6052 writing = TRUE;
6053 if (htab->sfpr->contents == NULL)
6054 {
6055 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6056 if (htab->sfpr->contents == NULL)
6057 return FALSE;
6058 }
6059 }
6060 if (writing)
6061 {
6062 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6063 if (i != parm->hi)
6064 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6065 else
6066 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6067 htab->sfpr->size = p - htab->sfpr->contents;
6068 }
6069 }
6070
6071 return TRUE;
6072}
6073
6074static bfd_byte *
6075savegpr0 (bfd *abfd, bfd_byte *p, int r)
6076{
6077 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6078 return p + 4;
6079}
6080
6081static bfd_byte *
6082savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6083{
6084 p = savegpr0 (abfd, p, r);
6085 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6086 p = p + 4;
6087 bfd_put_32 (abfd, BLR, p);
6088 return p + 4;
6089}
6090
6091static bfd_byte *
6092restgpr0 (bfd *abfd, bfd_byte *p, int r)
6093{
6094 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6095 return p + 4;
6096}
6097
6098static bfd_byte *
6099restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6100{
6101 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6102 p = p + 4;
6103 p = restgpr0 (abfd, p, r);
6104 bfd_put_32 (abfd, MTLR_R0, p);
6105 p = p + 4;
6106 if (r == 29)
6107 {
6108 p = restgpr0 (abfd, p, 30);
6109 p = restgpr0 (abfd, p, 31);
6110 }
6111 bfd_put_32 (abfd, BLR, p);
6112 return p + 4;
6113}
6114
6115static bfd_byte *
6116savegpr1 (bfd *abfd, bfd_byte *p, int r)
6117{
6118 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6119 return p + 4;
6120}
6121
6122static bfd_byte *
6123savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6124{
6125 p = savegpr1 (abfd, p, r);
6126 bfd_put_32 (abfd, BLR, p);
6127 return p + 4;
6128}
6129
6130static bfd_byte *
6131restgpr1 (bfd *abfd, bfd_byte *p, int r)
6132{
6133 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6134 return p + 4;
6135}
6136
6137static bfd_byte *
6138restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6139{
6140 p = restgpr1 (abfd, p, r);
6141 bfd_put_32 (abfd, BLR, p);
6142 return p + 4;
6143}
6144
6145static bfd_byte *
6146savefpr (bfd *abfd, bfd_byte *p, int r)
6147{
6148 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6149 return p + 4;
6150}
6151
6152static bfd_byte *
6153savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6154{
6155 p = savefpr (abfd, p, r);
6156 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6157 p = p + 4;
6158 bfd_put_32 (abfd, BLR, p);
6159 return p + 4;
6160}
6161
6162static bfd_byte *
6163restfpr (bfd *abfd, bfd_byte *p, int r)
6164{
6165 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6166 return p + 4;
6167}
6168
6169static bfd_byte *
6170restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6171{
6172 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6173 p = p + 4;
6174 p = restfpr (abfd, p, r);
6175 bfd_put_32 (abfd, MTLR_R0, p);
6176 p = p + 4;
6177 if (r == 29)
6178 {
6179 p = restfpr (abfd, p, 30);
6180 p = restfpr (abfd, p, 31);
6181 }
6182 bfd_put_32 (abfd, BLR, p);
6183 return p + 4;
6184}
6185
6186static bfd_byte *
6187savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6188{
6189 p = savefpr (abfd, p, r);
6190 bfd_put_32 (abfd, BLR, p);
6191 return p + 4;
6192}
6193
6194static bfd_byte *
6195restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6196{
6197 p = restfpr (abfd, p, r);
6198 bfd_put_32 (abfd, BLR, p);
6199 return p + 4;
6200}
6201
6202static bfd_byte *
6203savevr (bfd *abfd, bfd_byte *p, int r)
6204{
6205 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6206 p = p + 4;
6207 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6208 return p + 4;
6209}
6210
6211static bfd_byte *
6212savevr_tail (bfd *abfd, bfd_byte *p, int r)
6213{
6214 p = savevr (abfd, p, r);
6215 bfd_put_32 (abfd, BLR, p);
6216 return p + 4;
6217}
6218
6219static bfd_byte *
6220restvr (bfd *abfd, bfd_byte *p, int r)
6221{
6222 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6223 p = p + 4;
6224 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6225 return p + 4;
6226}
6227
6228static bfd_byte *
6229restvr_tail (bfd *abfd, bfd_byte *p, int r)
6230{
6231 p = restvr (abfd, p, r);
6232 bfd_put_32 (abfd, BLR, p);
6233 return p + 4;
6234}
6235
e86ce104
AM
6236/* Called via elf_link_hash_traverse to transfer dynamic linking
6237 information on function code symbol entries to their corresponding
6238 function descriptor symbol entries. */
deb0e272 6239
b34976b6 6240static bfd_boolean
4ce794b7 6241func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6242{
e86ce104 6243 struct bfd_link_info *info;
65f38f15 6244 struct ppc_link_hash_table *htab;
411e1bfb 6245 struct plt_entry *ent;
50bc7936
AM
6246 struct ppc_link_hash_entry *fh;
6247 struct ppc_link_hash_entry *fdh;
6248 bfd_boolean force_local;
5bd4f169 6249
50bc7936
AM
6250 fh = (struct ppc_link_hash_entry *) h;
6251 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6252 return TRUE;
e86ce104 6253
4ce794b7 6254 info = inf;
65f38f15 6255 htab = ppc_hash_table (info);
4dfe6ac6
NC
6256 if (htab == NULL)
6257 return FALSE;
5bd4f169 6258
c09bdfe5
AM
6259 /* Resolve undefined references to dot-symbols as the value
6260 in the function descriptor, if we have one in a regular object.
6261 This is to satisfy cases like ".quad .foo". Calls to functions
6262 in dynamic objects are handled elsewhere. */
6263 if (fh->elf.root.type == bfd_link_hash_undefweak
6264 && fh->was_undefined
b31867b6
AM
6265 && (fdh = defined_func_desc (fh)) != NULL
6266 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6267 && opd_entry_value (fdh->elf.root.u.def.section,
6268 fdh->elf.root.u.def.value,
c09bdfe5
AM
6269 &fh->elf.root.u.def.section,
6270 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6271 {
b31867b6 6272 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6273 fh->elf.forced_local = 1;
b31867b6
AM
6274 fh->elf.def_regular = fdh->elf.def_regular;
6275 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6276 }
6277
e86ce104
AM
6278 /* If this is a function code symbol, transfer dynamic linking
6279 information to the function descriptor symbol. */
50bc7936 6280 if (!fh->is_func)
b34976b6 6281 return TRUE;
e86ce104 6282
50bc7936 6283 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6284 if (ent->plt.refcount > 0)
6285 break;
50bc7936
AM
6286 if (ent == NULL
6287 || fh->elf.root.root.string[0] != '.'
6288 || fh->elf.root.root.string[1] == '\0')
6289 return TRUE;
5bd4f169 6290
50bc7936
AM
6291 /* Find the corresponding function descriptor symbol. Create it
6292 as undefined if necessary. */
5bd4f169 6293
b31867b6 6294 fdh = lookup_fdh (fh, htab);
50bc7936 6295 if (fdh == NULL
df131623 6296 && !info->executable
50bc7936
AM
6297 && (fh->elf.root.type == bfd_link_hash_undefined
6298 || fh->elf.root.type == bfd_link_hash_undefweak))
6299 {
908b32fc 6300 fdh = make_fdh (info, fh);
bb700d78
AM
6301 if (fdh == NULL)
6302 return FALSE;
50bc7936 6303 }
648cca2c 6304
908b32fc 6305 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6306 code symbol is strong undefined, make the fake sym the same.
6307 If the function code symbol is defined, then force the fake
6308 descriptor local; We can't support overriding of symbols in a
6309 shared library on a fake descriptor. */
908b32fc
AM
6310
6311 if (fdh != NULL
6312 && fdh->fake
433817dd 6313 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6314 {
433817dd
AM
6315 if (fh->elf.root.type == bfd_link_hash_undefined)
6316 {
6317 fdh->elf.root.type = bfd_link_hash_undefined;
6318 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6319 }
6320 else if (fh->elf.root.type == bfd_link_hash_defined
6321 || fh->elf.root.type == bfd_link_hash_defweak)
6322 {
6323 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6324 }
908b32fc
AM
6325 }
6326
50bc7936 6327 if (fdh != NULL
f5385ebf 6328 && !fdh->elf.forced_local
df131623 6329 && (!info->executable
f5385ebf
AM
6330 || fdh->elf.def_dynamic
6331 || fdh->elf.ref_dynamic
50bc7936
AM
6332 || (fdh->elf.root.type == bfd_link_hash_undefweak
6333 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6334 {
6335 if (fdh->elf.dynindx == -1)
c152c796 6336 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6337 return FALSE;
f5385ebf
AM
6338 fdh->elf.ref_regular |= fh->elf.ref_regular;
6339 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6340 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6341 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6342 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6343 {
40d16e0b 6344 move_plt_plist (fh, fdh);
f5385ebf 6345 fdh->elf.needs_plt = 1;
e86ce104 6346 }
50bc7936 6347 fdh->is_func_descriptor = 1;
34814b9f
AM
6348 fdh->oh = fh;
6349 fh->oh = fdh;
e86ce104
AM
6350 }
6351
50bc7936
AM
6352 /* Now that the info is on the function descriptor, clear the
6353 function code sym info. Any function code syms for which we
6354 don't have a definition in a regular file, we force local.
6355 This prevents a shared library from exporting syms that have
6356 been imported from another library. Function code syms that
6357 are really in the library we must leave global to prevent the
6358 linker dragging in a definition from a static library. */
93f3fa99
AM
6359 force_local = (!fh->elf.def_regular
6360 || fdh == NULL
6361 || !fdh->elf.def_regular
6362 || fdh->elf.forced_local);
50bc7936
AM
6363 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6364
b34976b6 6365 return TRUE;
e86ce104 6366}
40b8271b 6367
e86ce104 6368/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6369 this hook to a) provide some gcc support functions, and b) transfer
6370 dynamic linking information gathered so far on function code symbol
6371 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6372
b34976b6 6373static bfd_boolean
4ce794b7
AM
6374ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6375 struct bfd_link_info *info)
e86ce104
AM
6376{
6377 struct ppc_link_hash_table *htab;
82bd7b59 6378 unsigned int i;
deb0e272
AM
6379 const struct sfpr_def_parms funcs[] =
6380 {
6381 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6382 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6383 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6384 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6385 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6386 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6387 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6388 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6389 { "._savef", 14, 31, savefpr, savefpr1_tail },
6390 { "._restf", 14, 31, restfpr, restfpr1_tail },
6391 { "_savevr_", 20, 31, savevr, savevr_tail },
6392 { "_restvr_", 20, 31, restvr, restvr_tail }
6393 };
e86ce104
AM
6394
6395 htab = ppc_hash_table (info);
4dfe6ac6
NC
6396 if (htab == NULL)
6397 return FALSE;
6398
82bd7b59
AM
6399 if (htab->sfpr == NULL)
6400 /* We don't have any relocs. */
b34976b6 6401 return TRUE;
82bd7b59 6402
deb0e272
AM
6403 /* Provide any missing _save* and _rest* functions. */
6404 htab->sfpr->size = 0;
6405 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6406 if (!sfpr_define (info, &funcs[i]))
6407 return FALSE;
82bd7b59 6408
4ce794b7 6409 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6410
eea6121a 6411 if (htab->sfpr->size == 0)
8423293d 6412 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6413
b34976b6 6414 return TRUE;
e86ce104
AM
6415}
6416
6417/* Adjust a symbol defined by a dynamic object and referenced by a
6418 regular object. The current definition is in some section of the
6419 dynamic object, but we're not including those sections. We have to
6420 change the definition to something the rest of the link can
6421 understand. */
6422
b34976b6 6423static bfd_boolean
4ce794b7
AM
6424ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6425 struct elf_link_hash_entry *h)
e86ce104
AM
6426{
6427 struct ppc_link_hash_table *htab;
e86ce104 6428 asection *s;
e86ce104
AM
6429
6430 htab = ppc_hash_table (info);
4dfe6ac6
NC
6431 if (htab == NULL)
6432 return FALSE;
e86ce104
AM
6433
6434 /* Deal with function syms. */
6435 if (h->type == STT_FUNC
e054468f 6436 || h->type == STT_GNU_IFUNC
f5385ebf 6437 || h->needs_plt)
e86ce104
AM
6438 {
6439 /* Clear procedure linkage table information for any symbol that
6440 won't need a .plt entry. */
411e1bfb
AM
6441 struct plt_entry *ent;
6442 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6443 if (ent->plt.refcount > 0)
6444 break;
8387904d 6445 if (ent == NULL
e054468f
AM
6446 || (h->type != STT_GNU_IFUNC
6447 && (SYMBOL_CALLS_LOCAL (info, h)
6448 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6449 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6450 {
411e1bfb 6451 h->plt.plist = NULL;
f5385ebf 6452 h->needs_plt = 0;
40b8271b 6453 }
5bd4f169 6454 }
bbd7ec4a 6455 else
411e1bfb 6456 h->plt.plist = NULL;
5bd4f169
AM
6457
6458 /* If this is a weak symbol, and there is a real definition, the
6459 processor independent code will have arranged for us to see the
6460 real definition first, and we can just use the same value. */
f6e332e6 6461 if (h->u.weakdef != NULL)
5bd4f169 6462 {
f6e332e6
AM
6463 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6464 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6465 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6466 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6467 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6468 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6469 return TRUE;
5bd4f169
AM
6470 }
6471
5bd4f169
AM
6472 /* If we are creating a shared library, we must presume that the
6473 only references to the symbol are via the global offset table.
6474 For such cases we need not do anything here; the relocations will
6475 be handled correctly by relocate_section. */
6476 if (info->shared)
b34976b6 6477 return TRUE;
5bd4f169 6478
65f38f15
AM
6479 /* If there are no references to this symbol that do not use the
6480 GOT, we don't need to generate a copy reloc. */
f5385ebf 6481 if (!h->non_got_ref)
b34976b6 6482 return TRUE;
65f38f15 6483
b186458a
JJ
6484 /* Don't generate a copy reloc for symbols defined in the executable. */
6485 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6486 return TRUE;
6487
f4656909 6488 if (ELIMINATE_COPY_RELOCS)
65f38f15 6489 {
f4656909 6490 struct ppc_link_hash_entry * eh;
6061a67d 6491 struct elf_dyn_relocs *p;
65f38f15 6492
f4656909
AM
6493 eh = (struct ppc_link_hash_entry *) h;
6494 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6495 {
6496 s = p->sec->output_section;
6497 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6498 break;
6499 }
6500
6501 /* If we didn't find any dynamic relocs in read-only sections, then
6502 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6503 if (p == NULL)
6504 {
f5385ebf 6505 h->non_got_ref = 0;
f4656909
AM
6506 return TRUE;
6507 }
65f38f15
AM
6508 }
6509
5d35169e 6510 if (h->plt.plist != NULL)
97b639ba
AM
6511 {
6512 /* We should never get here, but unfortunately there are versions
6513 of gcc out there that improperly (for this ABI) put initialized
6514 function pointers, vtable refs and suchlike in read-only
6515 sections. Allow them to proceed, but warn that this might
6516 break at runtime. */
25f53a85 6517 info->callbacks->einfo
8de848d8 6518 (_("%P: copy reloc against `%s' requires lazy plt linking; "
25f53a85 6519 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6520 h->root.root.string);
6521 }
5d35169e
AM
6522
6523 /* This is a reference to a symbol defined by a dynamic object which
6524 is not a function. */
6525
909272ee
AM
6526 if (h->size == 0)
6527 {
8de848d8 6528 info->callbacks->einfo (_("%P: dynamic variable `%s' is zero size\n"),
25f53a85 6529 h->root.root.string);
909272ee
AM
6530 return TRUE;
6531 }
6532
5bd4f169
AM
6533 /* We must allocate the symbol in our .dynbss section, which will
6534 become part of the .bss section of the executable. There will be
6535 an entry for this symbol in the .dynsym section. The dynamic
6536 object will contain position independent code, so all references
6537 from the dynamic object to this symbol will go through the global
6538 offset table. The dynamic linker will use the .dynsym entry to
6539 determine the address it must put in the global offset table, so
6540 both the dynamic object and the regular object will refer to the
6541 same memory location for the variable. */
5bd4f169 6542
04c9666a
AM
6543 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6544 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6545 runtime process image. We need to remember the offset into the
6546 .rela.bss section we are going to use. */
6547 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6548 {
eea6121a 6549 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6550 h->needs_copy = 1;
5bd4f169
AM
6551 }
6552
4ce794b7 6553 s = htab->dynbss;
5bd4f169 6554
027297b7 6555 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6556}
6557
e86ce104
AM
6558/* If given a function descriptor symbol, hide both the function code
6559 sym and the descriptor. */
6560static void
4ce794b7
AM
6561ppc64_elf_hide_symbol (struct bfd_link_info *info,
6562 struct elf_link_hash_entry *h,
6563 bfd_boolean force_local)
e86ce104 6564{
34814b9f 6565 struct ppc_link_hash_entry *eh;
e86ce104
AM
6566 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6567
34814b9f
AM
6568 eh = (struct ppc_link_hash_entry *) h;
6569 if (eh->is_func_descriptor)
e86ce104 6570 {
34814b9f 6571 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6572
721956f4 6573 if (fh == NULL)
d1329ca3
AM
6574 {
6575 const char *p, *q;
6576 struct ppc_link_hash_table *htab;
6577 char save;
6578
6579 /* We aren't supposed to use alloca in BFD because on
6580 systems which do not have alloca the version in libiberty
6581 calls xmalloc, which might cause the program to crash
6582 when it runs out of memory. This function doesn't have a
6583 return status, so there's no way to gracefully return an
6584 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6585 accessed; It's either a string in an ELF string table,
6586 or allocated in an objalloc structure. */
d1329ca3 6587
34814b9f 6588 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6589 save = *p;
6590 *(char *) p = '.';
6591 htab = ppc_hash_table (info);
4dfe6ac6
NC
6592 if (htab == NULL)
6593 return;
6594
34814b9f
AM
6595 fh = (struct ppc_link_hash_entry *)
6596 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6597 *(char *) p = save;
6598
6599 /* Unfortunately, if it so happens that the string we were
6600 looking for was allocated immediately before this string,
6601 then we overwrote the string terminator. That's the only
6602 reason the lookup should fail. */
6603 if (fh == NULL)
6604 {
34814b9f
AM
6605 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6606 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6607 --q, --p;
34814b9f
AM
6608 if (q < eh->elf.root.root.string && *p == '.')
6609 fh = (struct ppc_link_hash_entry *)
6610 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6611 }
6612 if (fh != NULL)
6613 {
34814b9f
AM
6614 eh->oh = fh;
6615 fh->oh = eh;
d1329ca3
AM
6616 }
6617 }
e86ce104 6618 if (fh != NULL)
34814b9f 6619 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6620 }
6621}
6622
411e1bfb 6623static bfd_boolean
8843416a
AM
6624get_sym_h (struct elf_link_hash_entry **hp,
6625 Elf_Internal_Sym **symp,
6626 asection **symsecp,
f961d9dd 6627 unsigned char **tls_maskp,
8843416a
AM
6628 Elf_Internal_Sym **locsymsp,
6629 unsigned long r_symndx,
6630 bfd *ibfd)
411e1bfb 6631{
0ffa91dd 6632 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6633
6634 if (r_symndx >= symtab_hdr->sh_info)
6635 {
6636 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6637 struct elf_link_hash_entry *h;
6638
6639 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6640 h = elf_follow_link (h);
411e1bfb
AM
6641
6642 if (hp != NULL)
6643 *hp = h;
6644
6645 if (symp != NULL)
6646 *symp = NULL;
6647
6648 if (symsecp != NULL)
6649 {
6650 asection *symsec = NULL;
6651 if (h->root.type == bfd_link_hash_defined
6652 || h->root.type == bfd_link_hash_defweak)
6653 symsec = h->root.u.def.section;
6654 *symsecp = symsec;
6655 }
6656
e7b938ca 6657 if (tls_maskp != NULL)
411e1bfb
AM
6658 {
6659 struct ppc_link_hash_entry *eh;
6660
6661 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6662 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6663 }
6664 }
6665 else
6666 {
6667 Elf_Internal_Sym *sym;
6668 Elf_Internal_Sym *locsyms = *locsymsp;
6669
6670 if (locsyms == NULL)
6671 {
6672 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6673 if (locsyms == NULL)
6674 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6675 symtab_hdr->sh_info,
6676 0, NULL, NULL, NULL);
6677 if (locsyms == NULL)
6678 return FALSE;
6679 *locsymsp = locsyms;
6680 }
6681 sym = locsyms + r_symndx;
6682
6683 if (hp != NULL)
6684 *hp = NULL;
6685
6686 if (symp != NULL)
6687 *symp = sym;
6688
6689 if (symsecp != NULL)
cb33740c 6690 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6691
e7b938ca 6692 if (tls_maskp != NULL)
411e1bfb
AM
6693 {
6694 struct got_entry **lgot_ents;
f961d9dd 6695 unsigned char *tls_mask;
411e1bfb 6696
e7b938ca 6697 tls_mask = NULL;
411e1bfb
AM
6698 lgot_ents = elf_local_got_ents (ibfd);
6699 if (lgot_ents != NULL)
6700 {
e054468f
AM
6701 struct plt_entry **local_plt = (struct plt_entry **)
6702 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6703 unsigned char *lgot_masks = (unsigned char *)
e054468f 6704 (local_plt + symtab_hdr->sh_info);
e7b938ca 6705 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6706 }
e7b938ca 6707 *tls_maskp = tls_mask;
411e1bfb
AM
6708 }
6709 }
6710 return TRUE;
6711}
6712
e7b938ca 6713/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6714 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6715 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6716
6717static int
f961d9dd 6718get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6719 unsigned long *toc_symndx,
6720 bfd_vma *toc_addend,
0d4792f7 6721 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6722 const Elf_Internal_Rela *rel,
6723 bfd *ibfd)
411e1bfb
AM
6724{
6725 unsigned long r_symndx;
0d4792f7 6726 int next_r;
411e1bfb
AM
6727 struct elf_link_hash_entry *h;
6728 Elf_Internal_Sym *sym;
6729 asection *sec;
6730 bfd_vma off;
6731
6732 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6733 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6734 return 0;
411e1bfb 6735
e7b938ca 6736 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6737 || sec == NULL
6bee8834 6738 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6739 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6740 return 1;
411e1bfb
AM
6741
6742 /* Look inside a TOC section too. */
6743 if (h != NULL)
6744 {
6745 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6746 off = h->root.u.def.value;
6747 }
6748 else
6749 off = sym->st_value;
6750 off += rel->r_addend;
6751 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6752 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6753 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6754 if (toc_symndx != NULL)
6755 *toc_symndx = r_symndx;
3a71aa26
AM
6756 if (toc_addend != NULL)
6757 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6758 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6759 return 0;
854b41e7 6760 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6761 && (next_r == -1 || next_r == -2))
6762 return 1 - next_r;
951fd09b 6763 return 1;
411e1bfb
AM
6764}
6765
3b421ab3
AM
6766/* Find (or create) an entry in the tocsave hash table. */
6767
6768static struct tocsave_entry *
6769tocsave_find (struct ppc_link_hash_table *htab,
6770 enum insert_option insert,
6771 Elf_Internal_Sym **local_syms,
6772 const Elf_Internal_Rela *irela,
6773 bfd *ibfd)
6774{
6775 unsigned long r_indx;
6776 struct elf_link_hash_entry *h;
6777 Elf_Internal_Sym *sym;
6778 struct tocsave_entry ent, *p;
6779 hashval_t hash;
6780 struct tocsave_entry **slot;
6781
6782 r_indx = ELF64_R_SYM (irela->r_info);
6783 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6784 return NULL;
6785 if (ent.sec == NULL || ent.sec->output_section == NULL)
6786 {
6787 (*_bfd_error_handler)
6788 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
6789 return NULL;
6790 }
6791
6792 if (h != NULL)
6793 ent.offset = h->root.u.def.value;
6794 else
6795 ent.offset = sym->st_value;
6796 ent.offset += irela->r_addend;
6797
6798 hash = tocsave_htab_hash (&ent);
6799 slot = ((struct tocsave_entry **)
6800 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6801 if (slot == NULL)
6802 return NULL;
6803
6804 if (*slot == NULL)
6805 {
6806 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6807 if (p == NULL)
6808 return NULL;
6809 *p = ent;
6810 *slot = p;
6811 }
6812 return *slot;
6813}
6814
754021d0 6815/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6816 code for the old ABI, these will already have been done. */
754021d0
AM
6817
6818static bfd_boolean
6819adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6820{
6821 struct ppc_link_hash_entry *eh;
6822 asection *sym_sec;
74f0fb50 6823 struct _opd_sec_data *opd;
754021d0
AM
6824
6825 if (h->root.type == bfd_link_hash_indirect)
6826 return TRUE;
6827
754021d0
AM
6828 if (h->root.type != bfd_link_hash_defined
6829 && h->root.type != bfd_link_hash_defweak)
6830 return TRUE;
6831
6832 eh = (struct ppc_link_hash_entry *) h;
6833 if (eh->adjust_done)
6834 return TRUE;
6835
6836 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6837 opd = get_opd_info (sym_sec);
6838 if (opd != NULL && opd->adjust != NULL)
754021d0 6839 {
74f0fb50 6840 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6841 if (adjust == -1)
6842 {
6843 /* This entry has been deleted. */
b3fac117 6844 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6845 if (dsec == NULL)
6846 {
6847 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6848 if (elf_discarded_section (dsec))
6849 {
b3fac117 6850 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6851 break;
6852 }
6853 }
4025353c 6854 eh->elf.root.u.def.value = 0;
81688140 6855 eh->elf.root.u.def.section = dsec;
4025353c
AM
6856 }
6857 else
6858 eh->elf.root.u.def.value += adjust;
754021d0
AM
6859 eh->adjust_done = 1;
6860 }
6861 return TRUE;
6862}
6863
8c1d1bb8
AM
6864/* Handles decrementing dynamic reloc counts for the reloc specified by
6865 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6866 have already been determined. */
6867
6868static bfd_boolean
6869dec_dynrel_count (bfd_vma r_info,
6870 asection *sec,
6871 struct bfd_link_info *info,
6872 Elf_Internal_Sym **local_syms,
6873 struct elf_link_hash_entry *h,
6874 asection *sym_sec)
6875{
6876 enum elf_ppc64_reloc_type r_type;
6061a67d
AM
6877 struct elf_dyn_relocs *p;
6878 struct elf_dyn_relocs **pp;
8c1d1bb8
AM
6879
6880 /* Can this reloc be dynamic? This switch, and later tests here
6881 should be kept in sync with the code in check_relocs. */
6882 r_type = ELF64_R_TYPE (r_info);
6883 switch (r_type)
6884 {
6885 default:
6886 return TRUE;
6887
6888 case R_PPC64_TPREL16:
6889 case R_PPC64_TPREL16_LO:
6890 case R_PPC64_TPREL16_HI:
6891 case R_PPC64_TPREL16_HA:
6892 case R_PPC64_TPREL16_DS:
6893 case R_PPC64_TPREL16_LO_DS:
6894 case R_PPC64_TPREL16_HIGHER:
6895 case R_PPC64_TPREL16_HIGHERA:
6896 case R_PPC64_TPREL16_HIGHEST:
6897 case R_PPC64_TPREL16_HIGHESTA:
6898 if (!info->shared)
6899 return TRUE;
6900
6901 case R_PPC64_TPREL64:
6902 case R_PPC64_DTPMOD64:
6903 case R_PPC64_DTPREL64:
6904 case R_PPC64_ADDR64:
6905 case R_PPC64_REL30:
6906 case R_PPC64_REL32:
6907 case R_PPC64_REL64:
6908 case R_PPC64_ADDR14:
6909 case R_PPC64_ADDR14_BRNTAKEN:
6910 case R_PPC64_ADDR14_BRTAKEN:
6911 case R_PPC64_ADDR16:
6912 case R_PPC64_ADDR16_DS:
6913 case R_PPC64_ADDR16_HA:
6914 case R_PPC64_ADDR16_HI:
6915 case R_PPC64_ADDR16_HIGHER:
6916 case R_PPC64_ADDR16_HIGHERA:
6917 case R_PPC64_ADDR16_HIGHEST:
6918 case R_PPC64_ADDR16_HIGHESTA:
6919 case R_PPC64_ADDR16_LO:
6920 case R_PPC64_ADDR16_LO_DS:
6921 case R_PPC64_ADDR24:
6922 case R_PPC64_ADDR32:
6923 case R_PPC64_UADDR16:
6924 case R_PPC64_UADDR32:
6925 case R_PPC64_UADDR64:
6926 case R_PPC64_TOC:
6927 break;
6928 }
6929
6930 if (local_syms != NULL)
6931 {
6932 unsigned long r_symndx;
6933 Elf_Internal_Sym *sym;
6934 bfd *ibfd = sec->owner;
6935
6936 r_symndx = ELF64_R_SYM (r_info);
6937 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6938 return FALSE;
6939 }
6940
6941 if ((info->shared
1d483afe 6942 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8
AM
6943 || (h != NULL
6944 && (!info->symbolic
6945 || h->root.type == bfd_link_hash_defweak
6946 || !h->def_regular))))
6947 || (ELIMINATE_COPY_RELOCS
6948 && !info->shared
6949 && h != NULL
6950 && (h->root.type == bfd_link_hash_defweak
6951 || !h->def_regular)))
6952 ;
6953 else
6954 return TRUE;
6955
6956 if (h != NULL)
6957 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6958 else
6edfbbad 6959 {
60124e18
AM
6960 if (sym_sec != NULL)
6961 {
6962 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6061a67d 6963 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6964 }
6965 else
6966 {
6967 void *vpp = &elf_section_data (sec)->local_dynrel;
6061a67d 6968 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6969 }
6970
6971 /* elf_gc_sweep may have already removed all dyn relocs associated
6972 with local syms for a given section. Don't report a dynreloc
6973 miscount. */
6974 if (*pp == NULL)
6975 return TRUE;
6edfbbad 6976 }
8c1d1bb8
AM
6977
6978 while ((p = *pp) != NULL)
6979 {
6980 if (p->sec == sec)
6981 {
1d483afe 6982 if (!must_be_dyn_reloc (info, r_type))
8c1d1bb8
AM
6983 p->pc_count -= 1;
6984 p->count -= 1;
6985 if (p->count == 0)
6986 *pp = p->next;
6987 return TRUE;
6988 }
6989 pp = &p->next;
6990 }
6991
8de848d8 6992 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
25f53a85 6993 sec->owner, sec);
8c1d1bb8
AM
6994 bfd_set_error (bfd_error_bad_value);
6995 return FALSE;
6996}
6997
754021d0
AM
6998/* Remove unused Official Procedure Descriptor entries. Currently we
6999 only remove those associated with functions in discarded link-once
7000 sections, or weakly defined functions that have been overridden. It
7001 would be possible to remove many more entries for statically linked
7002 applications. */
7003
b34976b6 7004bfd_boolean
33c0ec9d 7005ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
7006{
7007 bfd *ibfd;
754021d0 7008 bfd_boolean some_edited = FALSE;
3f764659 7009 asection *need_pad = NULL;
1e2f5b6e 7010
411e1bfb 7011 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
7012 {
7013 asection *sec;
7014 Elf_Internal_Rela *relstart, *rel, *relend;
7015 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 7016 Elf_Internal_Sym *local_syms;
1e2f5b6e 7017 bfd_vma offset;
74f0fb50 7018 struct _opd_sec_data *opd;
3f764659
JJ
7019 bfd_boolean need_edit, add_aux_fields;
7020 bfd_size_type cnt_16b = 0;
1e2f5b6e 7021
854b41e7
AM
7022 if (!is_ppc64_elf (ibfd))
7023 continue;
7024
1e2f5b6e 7025 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 7026 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
7027 continue;
7028
4b85d634
AM
7029 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
7030 continue;
7031
1e2f5b6e
AM
7032 if (sec->output_section == bfd_abs_section_ptr)
7033 continue;
7034
7035 /* Look through the section relocs. */
7036 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7037 continue;
7038
6cdc0ccc 7039 local_syms = NULL;
0ffa91dd 7040 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
7041
7042 /* Read the relocations. */
4ce794b7 7043 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 7044 info->keep_memory);
1e2f5b6e 7045 if (relstart == NULL)
b34976b6 7046 return FALSE;
1e2f5b6e
AM
7047
7048 /* First run through the relocs to check they are sane, and to
7049 determine whether we need to edit this opd section. */
b34976b6 7050 need_edit = FALSE;
3f764659 7051 need_pad = sec;
1e2f5b6e
AM
7052 offset = 0;
7053 relend = relstart + sec->reloc_count;
50bc7936 7054 for (rel = relstart; rel < relend; )
1e2f5b6e 7055 {
04c9666a 7056 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
7057 unsigned long r_symndx;
7058 asection *sym_sec;
7059 struct elf_link_hash_entry *h;
7060 Elf_Internal_Sym *sym;
7061
3f764659 7062 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
7063 only interested in the reloc pointing to a function entry
7064 point. */
50bc7936
AM
7065 if (rel->r_offset != offset
7066 || rel + 1 >= relend
7067 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
7068 {
7069 /* If someone messes with .opd alignment then after a
7070 "ld -r" we might have padding in the middle of .opd.
7071 Also, there's nothing to prevent someone putting
7072 something silly in .opd with the assembler. No .opd
b34976b6 7073 optimization for them! */
3f764659 7074 broken_opd:
1e2f5b6e 7075 (*_bfd_error_handler)
d003868e 7076 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 7077 need_edit = FALSE;
1e2f5b6e
AM
7078 break;
7079 }
7080
50bc7936
AM
7081 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7082 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7083 {
7084 (*_bfd_error_handler)
d003868e
AM
7085 (_("%B: unexpected reloc type %u in .opd section"),
7086 ibfd, r_type);
50bc7936
AM
7087 need_edit = FALSE;
7088 break;
7089 }
7090
1e2f5b6e 7091 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7092 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7093 r_symndx, ibfd))
50bc7936 7094 goto error_ret;
1e2f5b6e
AM
7095
7096 if (sym_sec == NULL || sym_sec->owner == NULL)
7097 {
411e1bfb
AM
7098 const char *sym_name;
7099 if (h != NULL)
7100 sym_name = h->root.root.string;
7101 else
26c61ae5
L
7102 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7103 sym_sec);
411e1bfb 7104
1e2f5b6e 7105 (*_bfd_error_handler)
d003868e
AM
7106 (_("%B: undefined sym `%s' in .opd section"),
7107 ibfd, sym_name);
b34976b6 7108 need_edit = FALSE;
1e2f5b6e
AM
7109 break;
7110 }
7111
51020317
AM
7112 /* opd entries are always for functions defined in the
7113 current input bfd. If the symbol isn't defined in the
7114 input bfd, then we won't be using the function in this
7115 bfd; It must be defined in a linkonce section in another
7116 bfd, or is weak. It's also possible that we are
7117 discarding the function due to a linker script /DISCARD/,
7118 which we test for via the output_section. */
7119 if (sym_sec->owner != ibfd
7120 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7121 need_edit = TRUE;
1e2f5b6e 7122
50bc7936 7123 rel += 2;
3f764659
JJ
7124 if (rel == relend
7125 || (rel + 1 == relend && rel->r_offset == offset + 16))
7126 {
7127 if (sec->size == offset + 24)
7128 {
7129 need_pad = NULL;
7130 break;
7131 }
7132 if (rel == relend && sec->size == offset + 16)
7133 {
7134 cnt_16b++;
7135 break;
7136 }
7137 goto broken_opd;
7138 }
7139
7140 if (rel->r_offset == offset + 24)
7141 offset += 24;
7142 else if (rel->r_offset != offset + 16)
7143 goto broken_opd;
7144 else if (rel + 1 < relend
7145 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7146 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7147 {
7148 offset += 16;
7149 cnt_16b++;
7150 }
7151 else if (rel + 2 < relend
7152 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7153 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7154 {
7155 offset += 24;
7156 rel += 1;
7157 }
7158 else
7159 goto broken_opd;
1e2f5b6e
AM
7160 }
7161
3f764659
JJ
7162 add_aux_fields = non_overlapping && cnt_16b > 0;
7163
7164 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7165 {
7166 Elf_Internal_Rela *write_rel;
d4730f92 7167 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7168 bfd_byte *rptr, *wptr;
983bddc8 7169 bfd_byte *new_contents;
b34976b6 7170 bfd_boolean skip;
3f764659 7171 long opd_ent_size;
74f0fb50
AM
7172 bfd_size_type amt;
7173
983bddc8 7174 new_contents = NULL;
74f0fb50
AM
7175 amt = sec->size * sizeof (long) / 8;
7176 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7177 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7178 if (opd->adjust == NULL)
7179 return FALSE;
7180 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7181
7182 /* This seems a waste of time as input .opd sections are all
7183 zeros as generated by gcc, but I suppose there's no reason
7184 this will always be so. We might start putting something in
7185 the third word of .opd entries. */
7186 if ((sec->flags & SEC_IN_MEMORY) == 0)
7187 {
eea6121a
AM
7188 bfd_byte *loc;
7189 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7190 {
eea6121a
AM
7191 if (loc != NULL)
7192 free (loc);
50bc7936 7193 error_ret:
6cdc0ccc
AM
7194 if (local_syms != NULL
7195 && symtab_hdr->contents != (unsigned char *) local_syms)
7196 free (local_syms);
6cdc0ccc
AM
7197 if (elf_section_data (sec)->relocs != relstart)
7198 free (relstart);
b34976b6 7199 return FALSE;
6cdc0ccc 7200 }
1e2f5b6e
AM
7201 sec->contents = loc;
7202 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7203 }
7204
7205 elf_section_data (sec)->relocs = relstart;
7206
3f764659 7207 new_contents = sec->contents;
3f764659
JJ
7208 if (add_aux_fields)
7209 {
7210 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7211 if (new_contents == NULL)
7212 return FALSE;
7213 need_pad = FALSE;
3f764659 7214 }
b4f4e59f
AM
7215 wptr = new_contents;
7216 rptr = sec->contents;
3f764659 7217
1e2f5b6e 7218 write_rel = relstart;
b34976b6 7219 skip = FALSE;
1e2f5b6e 7220 offset = 0;
3f764659 7221 opd_ent_size = 0;
1e2f5b6e
AM
7222 for (rel = relstart; rel < relend; rel++)
7223 {
50bc7936
AM
7224 unsigned long r_symndx;
7225 asection *sym_sec;
7226 struct elf_link_hash_entry *h;
7227 Elf_Internal_Sym *sym;
7228
7229 r_symndx = ELF64_R_SYM (rel->r_info);
7230 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7231 r_symndx, ibfd))
50bc7936
AM
7232 goto error_ret;
7233
1e2f5b6e
AM
7234 if (rel->r_offset == offset)
7235 {
50bc7936 7236 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7237
7238 /* See if the .opd entry is full 24 byte or
7239 16 byte (with fd_aux entry overlapped with next
7240 fd_func). */
7241 opd_ent_size = 24;
7242 if ((rel + 2 == relend && sec->size == offset + 16)
7243 || (rel + 3 < relend
7244 && rel[2].r_offset == offset + 16
7245 && rel[3].r_offset == offset + 24
7246 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7247 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7248 opd_ent_size = 16;
7249
4025353c
AM
7250 if (h != NULL
7251 && h->root.root.string[0] == '.')
c4f68ce3 7252 {
4dfe6ac6
NC
7253 struct ppc_link_hash_table *htab;
7254
7255 htab = ppc_hash_table (info);
7256 if (htab != NULL)
7257 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7258 htab);
c4f68ce3
AM
7259 if (fdh != NULL
7260 && fdh->elf.root.type != bfd_link_hash_defined
7261 && fdh->elf.root.type != bfd_link_hash_defweak)
7262 fdh = NULL;
7263 }
1e2f5b6e 7264
51020317
AM
7265 skip = (sym_sec->owner != ibfd
7266 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7267 if (skip)
7268 {
4025353c 7269 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7270 {
7271 /* Arrange for the function descriptor sym
7272 to be dropped. */
d6fe2dc1
AM
7273 fdh->elf.root.u.def.value = 0;
7274 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7275 }
74f0fb50 7276 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7277 }
7278 else
1e2f5b6e
AM
7279 {
7280 /* We'll be keeping this opd entry. */
7281
4025353c 7282 if (fdh != NULL)
1e2f5b6e 7283 {
754021d0
AM
7284 /* Redefine the function descriptor symbol to
7285 this location in the opd section. It is
7286 necessary to update the value here rather
7287 than using an array of adjustments as we do
7288 for local symbols, because various places
7289 in the generic ELF code use the value
7290 stored in u.def.value. */
3f764659 7291 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7292 fdh->adjust_done = 1;
1e2f5b6e 7293 }
754021d0
AM
7294
7295 /* Local syms are a bit tricky. We could
7296 tweak them as they can be cached, but
7297 we'd need to look through the local syms
7298 for the function descriptor sym which we
7299 don't have at the moment. So keep an
7300 array of adjustments. */
74f0fb50 7301 opd->adjust[rel->r_offset / 8]
3f764659 7302 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7303
7304 if (wptr != rptr)
3f764659
JJ
7305 memcpy (wptr, rptr, opd_ent_size);
7306 wptr += opd_ent_size;
7307 if (add_aux_fields && opd_ent_size == 16)
7308 {
7309 memset (wptr, '\0', 8);
7310 wptr += 8;
7311 }
1e2f5b6e 7312 }
3f764659
JJ
7313 rptr += opd_ent_size;
7314 offset += opd_ent_size;
1e2f5b6e
AM
7315 }
7316
50bc7936
AM
7317 if (skip)
7318 {
60124e18
AM
7319 if (!NO_OPD_RELOCS
7320 && !info->relocatable
18d944df
AM
7321 && !dec_dynrel_count (rel->r_info, sec, info,
7322 NULL, h, sym_sec))
8c1d1bb8 7323 goto error_ret;
50bc7936
AM
7324 }
7325 else
1e2f5b6e 7326 {
50bc7936
AM
7327 /* We need to adjust any reloc offsets to point to the
7328 new opd entries. While we're at it, we may as well
7329 remove redundant relocs. */
74f0fb50 7330 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7331 if (write_rel != rel)
7332 memcpy (write_rel, rel, sizeof (*rel));
7333 ++write_rel;
7334 }
7335 }
7336
3f764659 7337 sec->size = wptr - new_contents;
1e2f5b6e 7338 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7339 if (add_aux_fields)
7340 {
7341 free (sec->contents);
7342 sec->contents = new_contents;
7343 }
7344
05bf9422 7345 /* Fudge the header size too, as this is used later in
cdcf6e38 7346 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7347 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7348 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7349 some_edited = TRUE;
1e2f5b6e 7350 }
6cdc0ccc 7351 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7352 free (relstart);
6cdc0ccc 7353
411e1bfb
AM
7354 if (local_syms != NULL
7355 && symtab_hdr->contents != (unsigned char *) local_syms)
7356 {
7357 if (!info->keep_memory)
7358 free (local_syms);
7359 else
7360 symtab_hdr->contents = (unsigned char *) local_syms;
7361 }
7362 }
7363
754021d0
AM
7364 if (some_edited)
7365 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7366
3f764659
JJ
7367 /* If we are doing a final link and the last .opd entry is just 16 byte
7368 long, add a 8 byte padding after it. */
7369 if (need_pad != NULL && !info->relocatable)
7370 {
7371 bfd_byte *p;
7372
7373 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7374 {
7375 BFD_ASSERT (need_pad->size > 0);
7376
7377 p = bfd_malloc (need_pad->size + 8);
7378 if (p == NULL)
7379 return FALSE;
699733f6 7380
3f764659
JJ
7381 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7382 p, 0, need_pad->size))
7383 return FALSE;
7384
7385 need_pad->contents = p;
7386 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7387 }
7388 else
7389 {
7390 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7391 if (p == NULL)
7392 return FALSE;
7393
7394 need_pad->contents = p;
7395 }
7396
7397 memset (need_pad->contents + need_pad->size, 0, 8);
7398 need_pad->size += 8;
7399 }
7400
411e1bfb
AM
7401 return TRUE;
7402}
7403
e1918d23 7404/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7405
e1918d23 7406asection *
33c0ec9d
AM
7407ppc64_elf_tls_setup (struct bfd_link_info *info,
7408 int no_tls_get_addr_opt,
7409 int *no_multi_toc)
411e1bfb 7410{
411e1bfb
AM
7411 struct ppc_link_hash_table *htab;
7412
411e1bfb 7413 htab = ppc_hash_table (info);
4dfe6ac6
NC
7414 if (htab == NULL)
7415 return NULL;
7416
33c0ec9d
AM
7417 if (*no_multi_toc)
7418 htab->do_multi_toc = 0;
7419 else if (!htab->do_multi_toc)
7420 *no_multi_toc = 1;
7421
3a71aa26
AM
7422 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7423 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7424 FALSE, FALSE, TRUE));
a7f2871e
AM
7425 /* Move dynamic linking info to the function descriptor sym. */
7426 if (htab->tls_get_addr != NULL)
7427 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7428 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7429 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7430 FALSE, FALSE, TRUE));
a7f2871e
AM
7431 if (!no_tls_get_addr_opt)
7432 {
7433 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7434
7435 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7436 FALSE, FALSE, TRUE);
7437 if (opt != NULL)
7438 func_desc_adjust (opt, info);
7439 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7440 FALSE, FALSE, TRUE);
7441 if (opt_fd != NULL
7442 && (opt_fd->root.type == bfd_link_hash_defined
7443 || opt_fd->root.type == bfd_link_hash_defweak))
7444 {
7445 /* If glibc supports an optimized __tls_get_addr call stub,
7446 signalled by the presence of __tls_get_addr_opt, and we'll
7447 be calling __tls_get_addr via a plt call stub, then
7448 make __tls_get_addr point to __tls_get_addr_opt. */
7449 tga_fd = &htab->tls_get_addr_fd->elf;
7450 if (htab->elf.dynamic_sections_created
7451 && tga_fd != NULL
7452 && (tga_fd->type == STT_FUNC
7453 || tga_fd->needs_plt)
7454 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7455 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7456 && tga_fd->root.type == bfd_link_hash_undefweak)))
7457 {
7458 struct plt_entry *ent;
7459
7460 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7461 if (ent->plt.refcount > 0)
7462 break;
7463 if (ent != NULL)
7464 {
7465 tga_fd->root.type = bfd_link_hash_indirect;
7466 tga_fd->root.u.i.link = &opt_fd->root;
7467 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7468 if (opt_fd->dynindx != -1)
7469 {
7470 /* Use __tls_get_addr_opt in dynamic relocations. */
7471 opt_fd->dynindx = -1;
7472 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7473 opt_fd->dynstr_index);
7474 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7475 return NULL;
a7f2871e
AM
7476 }
7477 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7478 tga = &htab->tls_get_addr->elf;
7479 if (opt != NULL && tga != NULL)
7480 {
7481 tga->root.type = bfd_link_hash_indirect;
7482 tga->root.u.i.link = &opt->root;
7483 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7484 _bfd_elf_link_hash_hide_symbol (info, opt,
7485 tga->forced_local);
7486 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7487 }
7488 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7489 htab->tls_get_addr_fd->is_func_descriptor = 1;
7490 if (htab->tls_get_addr != NULL)
7491 {
7492 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7493 htab->tls_get_addr->is_func = 1;
7494 }
7495 }
7496 }
7497 }
7498 else
7499 no_tls_get_addr_opt = TRUE;
7500 }
7501 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7502 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7503}
8387904d 7504
3a71aa26
AM
7505/* Return TRUE iff REL is a branch reloc with a global symbol matching
7506 HASH1 or HASH2. */
8387904d 7507
3a71aa26
AM
7508static bfd_boolean
7509branch_reloc_hash_match (const bfd *ibfd,
7510 const Elf_Internal_Rela *rel,
7511 const struct ppc_link_hash_entry *hash1,
7512 const struct ppc_link_hash_entry *hash2)
7513{
7514 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7515 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7516 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7517
e054468f 7518 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7519 {
3a71aa26
AM
7520 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7521 struct elf_link_hash_entry *h;
8387904d 7522
3a71aa26 7523 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7524 h = elf_follow_link (h);
3a71aa26
AM
7525 if (h == &hash1->elf || h == &hash2->elf)
7526 return TRUE;
a48ebf4d 7527 }
3a71aa26 7528 return FALSE;
951fd09b 7529}
411e1bfb 7530
951fd09b
AM
7531/* Run through all the TLS relocs looking for optimization
7532 opportunities. The linker has been hacked (see ppc64elf.em) to do
7533 a preliminary section layout so that we know the TLS segment
7534 offsets. We can't optimize earlier because some optimizations need
7535 to know the tp offset, and we need to optimize before allocating
7536 dynamic relocations. */
7537
7538bfd_boolean
33c0ec9d 7539ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7540{
7541 bfd *ibfd;
7542 asection *sec;
7543 struct ppc_link_hash_table *htab;
663a1470 7544 unsigned char *toc_ref;
102890f0 7545 int pass;
951fd09b 7546
1d483afe 7547 if (info->relocatable || !info->executable)
411e1bfb
AM
7548 return TRUE;
7549
951fd09b 7550 htab = ppc_hash_table (info);
4dfe6ac6
NC
7551 if (htab == NULL)
7552 return FALSE;
7553
663a1470
AM
7554 /* Make two passes over the relocs. On the first pass, mark toc
7555 entries involved with tls relocs, and check that tls relocs
7556 involved in setting up a tls_get_addr call are indeed followed by
7557 such a call. If they are not, we can't do any tls optimization.
7558 On the second pass twiddle tls_mask flags to notify
7559 relocate_section that optimization can be done, and adjust got
7560 and plt refcounts. */
7561 toc_ref = NULL;
7562 for (pass = 0; pass < 2; ++pass)
7563 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7564 {
7565 Elf_Internal_Sym *locsyms = NULL;
7566 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7567
102890f0
AM
7568 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7569 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7570 {
7571 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7572 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7573
102890f0
AM
7574 /* Read the relocations. */
7575 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7576 info->keep_memory);
7577 if (relstart == NULL)
7578 return FALSE;
411e1bfb 7579
102890f0
AM
7580 relend = relstart + sec->reloc_count;
7581 for (rel = relstart; rel < relend; rel++)
7582 {
7583 enum elf_ppc64_reloc_type r_type;
7584 unsigned long r_symndx;
7585 struct elf_link_hash_entry *h;
7586 Elf_Internal_Sym *sym;
7587 asection *sym_sec;
f961d9dd
AM
7588 unsigned char *tls_mask;
7589 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7590 bfd_vma value;
7591 bfd_boolean ok_tprel, is_local;
7592 long toc_ref_index = 0;
7593 int expecting_tls_get_addr = 0;
663a1470 7594 bfd_boolean ret = FALSE;
411e1bfb 7595
102890f0
AM
7596 r_symndx = ELF64_R_SYM (rel->r_info);
7597 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7598 r_symndx, ibfd))
7599 {
7600 err_free_rel:
7601 if (elf_section_data (sec)->relocs != relstart)
7602 free (relstart);
7603 if (toc_ref != NULL)
7604 free (toc_ref);
7605 if (locsyms != NULL
0ffa91dd 7606 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7607 != (unsigned char *) locsyms))
7608 free (locsyms);
663a1470 7609 return ret;
102890f0 7610 }
411e1bfb 7611
102890f0
AM
7612 if (h != NULL)
7613 {
766bc656
AM
7614 if (h->root.type == bfd_link_hash_defined
7615 || h->root.type == bfd_link_hash_defweak)
7616 value = h->root.u.def.value;
7617 else if (h->root.type == bfd_link_hash_undefweak)
7618 value = 0;
7619 else
663a1470
AM
7620 {
7621 found_tls_get_addr_arg = 0;
7622 continue;
7623 }
102890f0
AM
7624 }
7625 else
7626 /* Symbols referenced by TLS relocs must be of type
7627 STT_TLS. So no need for .opd local sym adjust. */
7628 value = sym->st_value;
7629
7630 ok_tprel = FALSE;
7631 is_local = FALSE;
7632 if (h == NULL
7633 || !h->def_dynamic)
7634 {
7635 is_local = TRUE;
766bc656
AM
7636 if (h != NULL
7637 && h->root.type == bfd_link_hash_undefweak)
7638 ok_tprel = TRUE;
7639 else
7640 {
7641 value += sym_sec->output_offset;
7642 value += sym_sec->output_section->vma;
7643 value -= htab->elf.tls_sec->vma;
7644 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7645 < (bfd_vma) 1 << 32);
7646 }
102890f0 7647 }
951fd09b 7648
102890f0 7649 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7650 /* If this section has old-style __tls_get_addr calls
7651 without marker relocs, then check that each
7652 __tls_get_addr call reloc is preceded by a reloc
7653 that conceivably belongs to the __tls_get_addr arg
7654 setup insn. If we don't find matching arg setup
7655 relocs, don't do any tls optimization. */
7656 if (pass == 0
7657 && sec->has_tls_get_addr_call
7658 && h != NULL
7659 && (h == &htab->tls_get_addr->elf
7660 || h == &htab->tls_get_addr_fd->elf)
7661 && !found_tls_get_addr_arg
7662 && is_branch_reloc (r_type))
7663 {
25f53a85 7664 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7665 "TLS optimization disabled\n"),
7666 ibfd, sec, rel->r_offset);
7667 ret = TRUE;
7668 goto err_free_rel;
7669 }
7670
7671 found_tls_get_addr_arg = 0;
102890f0
AM
7672 switch (r_type)
7673 {
7674 case R_PPC64_GOT_TLSLD16:
7675 case R_PPC64_GOT_TLSLD16_LO:
7676 expecting_tls_get_addr = 1;
663a1470 7677 found_tls_get_addr_arg = 1;
102890f0
AM
7678 /* Fall thru */
7679
7680 case R_PPC64_GOT_TLSLD16_HI:
7681 case R_PPC64_GOT_TLSLD16_HA:
7682 /* These relocs should never be against a symbol
7683 defined in a shared lib. Leave them alone if
7684 that turns out to be the case. */
7685 if (!is_local)
7686 continue;
411e1bfb 7687
102890f0 7688 /* LD -> LE */
411e1bfb 7689 tls_set = 0;
102890f0
AM
7690 tls_clear = TLS_LD;
7691 tls_type = TLS_TLS | TLS_LD;
7692 break;
411e1bfb 7693
102890f0
AM
7694 case R_PPC64_GOT_TLSGD16:
7695 case R_PPC64_GOT_TLSGD16_LO:
7696 expecting_tls_get_addr = 1;
663a1470 7697 found_tls_get_addr_arg = 1;
102890f0
AM
7698 /* Fall thru */
7699
7700 case R_PPC64_GOT_TLSGD16_HI:
7701 case R_PPC64_GOT_TLSGD16_HA:
7702 if (ok_tprel)
7703 /* GD -> LE */
411e1bfb 7704 tls_set = 0;
102890f0
AM
7705 else
7706 /* GD -> IE */
7707 tls_set = TLS_TLS | TLS_TPRELGD;
7708 tls_clear = TLS_GD;
7709 tls_type = TLS_TLS | TLS_GD;
7710 break;
7711
7712 case R_PPC64_GOT_TPREL16_DS:
7713 case R_PPC64_GOT_TPREL16_LO_DS:
7714 case R_PPC64_GOT_TPREL16_HI:
7715 case R_PPC64_GOT_TPREL16_HA:
7716 if (ok_tprel)
7717 {
7718 /* IE -> LE */
7719 tls_set = 0;
7720 tls_clear = TLS_TPREL;
7721 tls_type = TLS_TLS | TLS_TPREL;
7722 break;
7723 }
411e1bfb
AM
7724 continue;
7725
727fc41e
AM
7726 case R_PPC64_TLSGD:
7727 case R_PPC64_TLSLD:
663a1470
AM
7728 found_tls_get_addr_arg = 1;
7729 /* Fall thru */
7730
7731 case R_PPC64_TLS:
7732 case R_PPC64_TOC16:
7733 case R_PPC64_TOC16_LO:
102890f0
AM
7734 if (sym_sec == NULL || sym_sec != toc)
7735 continue;
7736
7737 /* Mark this toc entry as referenced by a TLS
7738 code sequence. We can do that now in the
7739 case of R_PPC64_TLS, and after checking for
7740 tls_get_addr for the TOC16 relocs. */
7741 if (toc_ref == NULL)
663a1470
AM
7742 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7743 if (toc_ref == NULL)
7744 goto err_free_rel;
7745
102890f0
AM
7746 if (h != NULL)
7747 value = h->root.u.def.value;
7748 else
7749 value = sym->st_value;
7750 value += rel->r_addend;
7751 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 7752 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7753 if (r_type == R_PPC64_TLS
7754 || r_type == R_PPC64_TLSGD
7755 || r_type == R_PPC64_TLSLD)
102890f0
AM
7756 {
7757 toc_ref[toc_ref_index] = 1;
7758 continue;
7759 }
7760
7761 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7762 continue;
7763
7764 tls_set = 0;
7765 tls_clear = 0;
7766 expecting_tls_get_addr = 2;
7767 break;
7768
7769 case R_PPC64_TPREL64:
7770 if (pass == 0
7771 || sec != toc
7772 || toc_ref == NULL
663a1470 7773 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7774 continue;
7775 if (ok_tprel)
7776 {
7777 /* IE -> LE */
7778 tls_set = TLS_EXPLICIT;
7779 tls_clear = TLS_TPREL;
7780 break;
7781 }
7782 continue;
7783
7784 case R_PPC64_DTPMOD64:
7785 if (pass == 0
7786 || sec != toc
7787 || toc_ref == NULL
663a1470 7788 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7789 continue;
7790 if (rel + 1 < relend
7791 && (rel[1].r_info
7792 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7793 && rel[1].r_offset == rel->r_offset + 8)
7794 {
7795 if (ok_tprel)
7796 /* GD -> LE */
7797 tls_set = TLS_EXPLICIT | TLS_GD;
7798 else
7799 /* GD -> IE */
7800 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7801 tls_clear = TLS_GD;
7802 }
7803 else
7804 {
7805 if (!is_local)
7806 continue;
7807
7808 /* LD -> LE */
7809 tls_set = TLS_EXPLICIT;
7810 tls_clear = TLS_LD;
7811 }
7812 break;
7813
7814 default:
7815 continue;
7816 }
7817
7818 if (pass == 0)
7819 {
727fc41e
AM
7820 if (!expecting_tls_get_addr
7821 || !sec->has_tls_get_addr_call)
102890f0
AM
7822 continue;
7823
3a71aa26
AM
7824 if (rel + 1 < relend
7825 && branch_reloc_hash_match (ibfd, rel + 1,
7826 htab->tls_get_addr,
7827 htab->tls_get_addr_fd))
102890f0 7828 {
3a71aa26 7829 if (expecting_tls_get_addr == 2)
102890f0 7830 {
3a71aa26 7831 /* Check for toc tls entries. */
f961d9dd 7832 unsigned char *toc_tls;
3a71aa26
AM
7833 int retval;
7834
7835 retval = get_tls_mask (&toc_tls, NULL, NULL,
7836 &locsyms,
7837 rel, ibfd);
7838 if (retval == 0)
7839 goto err_free_rel;
663a1470
AM
7840 if (toc_tls != NULL)
7841 {
7842 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7843 found_tls_get_addr_arg = 1;
7844 if (retval > 1)
7845 toc_ref[toc_ref_index] = 1;
7846 }
102890f0 7847 }
3a71aa26 7848 continue;
102890f0
AM
7849 }
7850
7851 if (expecting_tls_get_addr != 1)
7852 continue;
7853
7854 /* Uh oh, we didn't find the expected call. We
7855 could just mark this symbol to exclude it
7856 from tls optimization but it's safer to skip
663a1470 7857 the entire optimization. */
25f53a85 7858 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7859 "TLS optimization disabled\n"),
7860 ibfd, sec, rel->r_offset);
7861 ret = TRUE;
7862 goto err_free_rel;
102890f0
AM
7863 }
7864
85f7a9cb 7865 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
7866 {
7867 struct plt_entry *ent;
7868 for (ent = htab->tls_get_addr->elf.plt.plist;
7869 ent != NULL;
7870 ent = ent->next)
7871 if (ent->addend == 0)
411e1bfb 7872 {
102890f0 7873 if (ent->plt.refcount > 0)
30038c59 7874 {
102890f0
AM
7875 ent->plt.refcount -= 1;
7876 expecting_tls_get_addr = 0;
30038c59 7877 }
102890f0 7878 break;
411e1bfb 7879 }
102890f0 7880 }
411e1bfb 7881
85f7a9cb 7882 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
7883 {
7884 struct plt_entry *ent;
7885 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7886 ent != NULL;
7887 ent = ent->next)
7888 if (ent->addend == 0)
411e1bfb 7889 {
102890f0
AM
7890 if (ent->plt.refcount > 0)
7891 ent->plt.refcount -= 1;
7892 break;
411e1bfb 7893 }
102890f0 7894 }
411e1bfb 7895
102890f0 7896 if (tls_clear == 0)
30038c59
AM
7897 continue;
7898
102890f0
AM
7899 if ((tls_set & TLS_EXPLICIT) == 0)
7900 {
7901 struct got_entry *ent;
411e1bfb 7902
102890f0
AM
7903 /* Adjust got entry for this reloc. */
7904 if (h != NULL)
7905 ent = h->got.glist;
7906 else
7907 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7908
102890f0
AM
7909 for (; ent != NULL; ent = ent->next)
7910 if (ent->addend == rel->r_addend
7911 && ent->owner == ibfd
7912 && ent->tls_type == tls_type)
7913 break;
7914 if (ent == NULL)
7915 abort ();
411e1bfb 7916
102890f0
AM
7917 if (tls_set == 0)
7918 {
7919 /* We managed to get rid of a got entry. */
7920 if (ent->got.refcount > 0)
7921 ent->got.refcount -= 1;
7922 }
7923 }
7924 else
7925 {
7926 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7927 we'll lose one or two dyn relocs. */
7928 if (!dec_dynrel_count (rel->r_info, sec, info,
7929 NULL, h, sym_sec))
7930 return FALSE;
411e1bfb 7931
102890f0
AM
7932 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7933 {
7934 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7935 NULL, h, sym_sec))
7936 return FALSE;
7937 }
7938 }
411e1bfb 7939
102890f0
AM
7940 *tls_mask |= tls_set;
7941 *tls_mask &= ~tls_clear;
7942 }
8c1d1bb8 7943
102890f0
AM
7944 if (elf_section_data (sec)->relocs != relstart)
7945 free (relstart);
7946 }
411e1bfb 7947
663a1470
AM
7948 if (locsyms != NULL
7949 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7950 {
7951 if (!info->keep_memory)
7952 free (locsyms);
7953 else
7954 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7955 }
7956 }
411e1bfb 7957
663a1470
AM
7958 if (toc_ref != NULL)
7959 free (toc_ref);
b34976b6 7960 return TRUE;
1e2f5b6e 7961}
b34976b6 7962
c5614fa4
AM
7963/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7964 the values of any global symbols in a toc section that has been
7965 edited. Globals in toc sections should be a rarity, so this function
7966 sets a flag if any are found in toc sections other than the one just
7967 edited, so that futher hash table traversals can be avoided. */
7968
7969struct adjust_toc_info
7970{
7971 asection *toc;
7972 unsigned long *skip;
7973 bfd_boolean global_toc_syms;
7974};
7975
ba761f19
AM
7976enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7977
c5614fa4
AM
7978static bfd_boolean
7979adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7980{
7981 struct ppc_link_hash_entry *eh;
7982 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7983 unsigned long i;
c5614fa4 7984
c5614fa4
AM
7985 if (h->root.type != bfd_link_hash_defined
7986 && h->root.type != bfd_link_hash_defweak)
7987 return TRUE;
7988
7989 eh = (struct ppc_link_hash_entry *) h;
7990 if (eh->adjust_done)
7991 return TRUE;
7992
7993 if (eh->elf.root.u.def.section == toc_inf->toc)
7994 {
854b41e7
AM
7995 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7996 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7997 else
854b41e7
AM
7998 i = eh->elf.root.u.def.value >> 3;
7999
ba761f19 8000 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
8001 {
8002 (*_bfd_error_handler)
854b41e7
AM
8003 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8004 do
8005 ++i;
ba761f19 8006 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 8007 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 8008 }
854b41e7
AM
8009
8010 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
8011 eh->adjust_done = 1;
8012 }
8013 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8014 toc_inf->global_toc_syms = TRUE;
8015
8016 return TRUE;
8017}
8018
8019/* Examine all relocs referencing .toc sections in order to remove
8020 unused .toc entries. */
8021
8022bfd_boolean
33c0ec9d 8023ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
8024{
8025 bfd *ibfd;
8026 struct adjust_toc_info toc_inf;
67f0cbdb 8027 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 8028
67f0cbdb 8029 htab->do_toc_opt = 1;
c5614fa4
AM
8030 toc_inf.global_toc_syms = TRUE;
8031 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8032 {
8033 asection *toc, *sec;
8034 Elf_Internal_Shdr *symtab_hdr;
8035 Elf_Internal_Sym *local_syms;
425b145b 8036 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
8037 unsigned long *skip, *drop;
8038 unsigned char *used;
8039 unsigned char *keep, last, some_unused;
8040
854b41e7
AM
8041 if (!is_ppc64_elf (ibfd))
8042 continue;
8043
c5614fa4
AM
8044 toc = bfd_get_section_by_name (ibfd, ".toc");
8045 if (toc == NULL
92b7a70f 8046 || toc->size == 0
c5614fa4
AM
8047 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
8048 || elf_discarded_section (toc))
8049 continue;
8050
425b145b 8051 toc_relocs = NULL;
c5614fa4 8052 local_syms = NULL;
0ffa91dd 8053 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
8054
8055 /* Look at sections dropped from the final link. */
8056 skip = NULL;
8057 relstart = NULL;
8058 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8059 {
8060 if (sec->reloc_count == 0
8061 || !elf_discarded_section (sec)
8062 || get_opd_info (sec)
8063 || (sec->flags & SEC_ALLOC) == 0
8064 || (sec->flags & SEC_DEBUGGING) != 0)
8065 continue;
8066
8067 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8068 if (relstart == NULL)
8069 goto error_ret;
8070
8071 /* Run through the relocs to see which toc entries might be
8072 unused. */
8073 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8074 {
8075 enum elf_ppc64_reloc_type r_type;
8076 unsigned long r_symndx;
8077 asection *sym_sec;
8078 struct elf_link_hash_entry *h;
8079 Elf_Internal_Sym *sym;
8080 bfd_vma val;
8081
8082 r_type = ELF64_R_TYPE (rel->r_info);
8083 switch (r_type)
8084 {
8085 default:
8086 continue;
8087
8088 case R_PPC64_TOC16:
8089 case R_PPC64_TOC16_LO:
8090 case R_PPC64_TOC16_HI:
8091 case R_PPC64_TOC16_HA:
8092 case R_PPC64_TOC16_DS:
8093 case R_PPC64_TOC16_LO_DS:
8094 break;
8095 }
8096
8097 r_symndx = ELF64_R_SYM (rel->r_info);
8098 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8099 r_symndx, ibfd))
8100 goto error_ret;
8101
8102 if (sym_sec != toc)
8103 continue;
8104
8105 if (h != NULL)
8106 val = h->root.u.def.value;
8107 else
8108 val = sym->st_value;
8109 val += rel->r_addend;
8110
8111 if (val >= toc->size)
8112 continue;
8113
8114 /* Anything in the toc ought to be aligned to 8 bytes.
8115 If not, don't mark as unused. */
8116 if (val & 7)
8117 continue;
8118
8119 if (skip == NULL)
8120 {
854b41e7 8121 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8122 if (skip == NULL)
8123 goto error_ret;
8124 }
8125
ba761f19 8126 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8127 }
8128
8129 if (elf_section_data (sec)->relocs != relstart)
8130 free (relstart);
8131 }
8132
ba761f19
AM
8133 /* For largetoc loads of address constants, we can convert
8134 . addis rx,2,addr@got@ha
8135 . ld ry,addr@got@l(rx)
8136 to
8137 . addis rx,2,addr@toc@ha
8138 . addi ry,rx,addr@toc@l
8139 when addr is within 2G of the toc pointer. This then means
8140 that the word storing "addr" in the toc is no longer needed. */
8141
8142 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8143 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8144 && toc->reloc_count != 0)
8145 {
8146 /* Read toc relocs. */
425b145b
AM
8147 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8148 info->keep_memory);
8149 if (toc_relocs == NULL)
ba761f19
AM
8150 goto error_ret;
8151
425b145b 8152 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8153 {
8154 enum elf_ppc64_reloc_type r_type;
8155 unsigned long r_symndx;
8156 asection *sym_sec;
8157 struct elf_link_hash_entry *h;
8158 Elf_Internal_Sym *sym;
8159 bfd_vma val, addr;
8160
8161 r_type = ELF64_R_TYPE (rel->r_info);
8162 if (r_type != R_PPC64_ADDR64)
8163 continue;
8164
8165 r_symndx = ELF64_R_SYM (rel->r_info);
8166 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8167 r_symndx, ibfd))
8168 goto error_ret;
8169
425b145b
AM
8170 if (sym_sec == NULL
8171 || elf_discarded_section (sym_sec))
8172 continue;
8173
726d3ab0 8174 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8175 continue;
8176
8177 if (h != NULL)
bddc25c9
AM
8178 {
8179 if (h->type == STT_GNU_IFUNC)
8180 continue;
8181 val = h->root.u.def.value;
8182 }
ba761f19 8183 else
bddc25c9
AM
8184 {
8185 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8186 continue;
8187 val = sym->st_value;
8188 }
ba761f19
AM
8189 val += rel->r_addend;
8190 val += sym_sec->output_section->vma + sym_sec->output_offset;
8191
8192 /* We don't yet know the exact toc pointer value, but we
8193 know it will be somewhere in the toc section. Don't
8194 optimize if the difference from any possible toc
8195 pointer is outside [ff..f80008000, 7fff7fff]. */
8196 addr = toc->output_section->vma + TOC_BASE_OFF;
8197 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8198 continue;
8199
8200 addr = toc->output_section->vma + toc->output_section->rawsize;
8201 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8202 continue;
8203
8204 if (skip == NULL)
8205 {
8206 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8207 if (skip == NULL)
8208 goto error_ret;
8209 }
8210
8211 skip[rel->r_offset >> 3]
425b145b 8212 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8213 }
ba761f19
AM
8214 }
8215
c5614fa4
AM
8216 if (skip == NULL)
8217 continue;
8218
8219 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8220 if (used == NULL)
8221 {
8222 error_ret:
8223 if (local_syms != NULL
8224 && symtab_hdr->contents != (unsigned char *) local_syms)
8225 free (local_syms);
8226 if (sec != NULL
8227 && relstart != NULL
8228 && elf_section_data (sec)->relocs != relstart)
8229 free (relstart);
425b145b
AM
8230 if (toc_relocs != NULL
8231 && elf_section_data (toc)->relocs != toc_relocs)
8232 free (toc_relocs);
c5614fa4
AM
8233 if (skip != NULL)
8234 free (skip);
8235 return FALSE;
8236 }
8237
30038c59
AM
8238 /* Now check all kept sections that might reference the toc.
8239 Check the toc itself last. */
8240 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8241 : ibfd->sections);
c5614fa4 8242 sec != NULL;
c5614fa4 8243 sec = (sec == toc ? NULL
c5614fa4 8244 : sec->next == NULL ? toc
30038c59 8245 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8246 : sec->next))
8247 {
8248 int repeat;
8249
8250 if (sec->reloc_count == 0
8251 || elf_discarded_section (sec)
8252 || get_opd_info (sec)
8253 || (sec->flags & SEC_ALLOC) == 0
8254 || (sec->flags & SEC_DEBUGGING) != 0)
8255 continue;
8256
854b41e7
AM
8257 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8258 info->keep_memory);
c5614fa4
AM
8259 if (relstart == NULL)
8260 goto error_ret;
8261
8262 /* Mark toc entries referenced as used. */
8263 repeat = 0;
8264 do
8265 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8266 {
8267 enum elf_ppc64_reloc_type r_type;
8268 unsigned long r_symndx;
8269 asection *sym_sec;
8270 struct elf_link_hash_entry *h;
8271 Elf_Internal_Sym *sym;
8272 bfd_vma val;
8273
8274 r_type = ELF64_R_TYPE (rel->r_info);
98528052
AM
8275 switch (r_type)
8276 {
8277 default:
8278 break;
8279
8280 case R_PPC64_GOT_TLSLD16_HA:
8281 case R_PPC64_GOT_TLSGD16_HA:
8282 case R_PPC64_GOT_TPREL16_HA:
8283 case R_PPC64_GOT_DTPREL16_HA:
8284 case R_PPC64_GOT16_HA:
8285 case R_PPC64_TOC16_HA:
8286 {
8287 bfd_vma off = rel->r_offset & ~3;
8288 unsigned char buf[4];
8289 unsigned int insn;
8290
8291 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8292 {
8293 free (used);
8294 goto error_ret;
8295 }
8296 insn = bfd_get_32 (ibfd, buf);
8297 if ((insn & ((0x3f << 26) | 0x1f << 16))
8298 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */)
8299 ppc64_elf_tdata (ibfd)->ha_relocs_not_using_r2 = 1;
8300 }
8301 break;
8302 }
8303
c5614fa4
AM
8304 switch (r_type)
8305 {
8306 case R_PPC64_TOC16:
8307 case R_PPC64_TOC16_LO:
8308 case R_PPC64_TOC16_HI:
8309 case R_PPC64_TOC16_HA:
8310 case R_PPC64_TOC16_DS:
8311 case R_PPC64_TOC16_LO_DS:
8312 /* In case we're taking addresses of toc entries. */
8313 case R_PPC64_ADDR64:
8314 break;
8315
8316 default:
8317 continue;
8318 }
8319
8320 r_symndx = ELF64_R_SYM (rel->r_info);
8321 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8322 r_symndx, ibfd))
8323 {
8324 free (used);
8325 goto error_ret;
8326 }
8327
8328 if (sym_sec != toc)
8329 continue;
8330
8331 if (h != NULL)
8332 val = h->root.u.def.value;
8333 else
8334 val = sym->st_value;
8335 val += rel->r_addend;
8336
8337 if (val >= toc->size)
8338 continue;
8339
ba761f19
AM
8340 if ((skip[val >> 3] & can_optimize) != 0)
8341 {
8342 bfd_vma off;
8343 unsigned char opc;
8344
8345 switch (r_type)
8346 {
8347 case R_PPC64_TOC16_HA:
8348 break;
8349
8350 case R_PPC64_TOC16_LO_DS:
8351 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8352 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
98528052
AM
8353 {
8354 free (used);
8355 goto error_ret;
8356 }
ba761f19
AM
8357 if ((opc & (0x3f << 2)) == (58u << 2))
8358 break;
8359 /* Fall thru */
8360
8361 default:
8362 /* Wrong sort of reloc, or not a ld. We may
8363 as well clear ref_from_discarded too. */
8364 skip[val >> 3] = 0;
8365 }
8366 }
8367
c5614fa4
AM
8368 /* For the toc section, we only mark as used if
8369 this entry itself isn't unused. */
8370 if (sec == toc
8371 && !used[val >> 3]
8372 && (used[rel->r_offset >> 3]
ba761f19 8373 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
c5614fa4
AM
8374 /* Do all the relocs again, to catch reference
8375 chains. */
8376 repeat = 1;
8377
8378 used[val >> 3] = 1;
8379 }
8380 while (repeat);
854b41e7
AM
8381
8382 if (elf_section_data (sec)->relocs != relstart)
8383 free (relstart);
c5614fa4
AM
8384 }
8385
8386 /* Merge the used and skip arrays. Assume that TOC
8387 doublewords not appearing as either used or unused belong
8388 to to an entry more than one doubleword in size. */
8389 for (drop = skip, keep = used, last = 0, some_unused = 0;
8390 drop < skip + (toc->size + 7) / 8;
8391 ++drop, ++keep)
8392 {
8393 if (*keep)
8394 {
ba761f19
AM
8395 *drop &= ~ref_from_discarded;
8396 if ((*drop & can_optimize) != 0)
8397 some_unused = 1;
c5614fa4
AM
8398 last = 0;
8399 }
b140b010 8400 else if ((*drop & ref_from_discarded) != 0)
c5614fa4
AM
8401 {
8402 some_unused = 1;
ba761f19 8403 last = ref_from_discarded;
c5614fa4
AM
8404 }
8405 else
8406 *drop = last;
8407 }
8408
8409 free (used);
8410
8411 if (some_unused)
8412 {
8413 bfd_byte *contents, *src;
8414 unsigned long off;
d62b3684 8415 Elf_Internal_Sym *sym;
ba761f19 8416 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8417
8418 /* Shuffle the toc contents, and at the same time convert the
8419 skip array from booleans into offsets. */
8420 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8421 goto error_ret;
8422
8423 elf_section_data (toc)->this_hdr.contents = contents;
8424
8425 for (src = contents, off = 0, drop = skip;
8426 src < contents + toc->size;
8427 src += 8, ++drop)
8428 {
ba761f19
AM
8429 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8430 off += 8;
c5614fa4
AM
8431 else if (off != 0)
8432 {
8433 *drop = off;
8434 memcpy (src - off, src, 8);
8435 }
8436 }
854b41e7 8437 *drop = off;
c5614fa4
AM
8438 toc->rawsize = toc->size;
8439 toc->size = src - contents - off;
8440
ba761f19
AM
8441 /* Adjust addends for relocs against the toc section sym,
8442 and optimize any accesses we can. */
c5614fa4
AM
8443 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8444 {
8445 if (sec->reloc_count == 0
8446 || elf_discarded_section (sec))
8447 continue;
8448
8449 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8450 info->keep_memory);
c5614fa4
AM
8451 if (relstart == NULL)
8452 goto error_ret;
8453
8454 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8455 {
8456 enum elf_ppc64_reloc_type r_type;
8457 unsigned long r_symndx;
8458 asection *sym_sec;
8459 struct elf_link_hash_entry *h;
854b41e7 8460 bfd_vma val;
c5614fa4
AM
8461
8462 r_type = ELF64_R_TYPE (rel->r_info);
8463 switch (r_type)
8464 {
8465 default:
8466 continue;
8467
8468 case R_PPC64_TOC16:
8469 case R_PPC64_TOC16_LO:
8470 case R_PPC64_TOC16_HI:
8471 case R_PPC64_TOC16_HA:
8472 case R_PPC64_TOC16_DS:
8473 case R_PPC64_TOC16_LO_DS:
8474 case R_PPC64_ADDR64:
8475 break;
8476 }
8477
8478 r_symndx = ELF64_R_SYM (rel->r_info);
8479 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8480 r_symndx, ibfd))
8481 goto error_ret;
8482
ba761f19 8483 if (sym_sec != toc)
c5614fa4
AM
8484 continue;
8485
ba761f19
AM
8486 if (h != NULL)
8487 val = h->root.u.def.value;
8488 else
8489 {
8490 val = sym->st_value;
8491 if (val != 0)
8492 local_toc_syms = TRUE;
8493 }
8494
8495 val += rel->r_addend;
854b41e7
AM
8496
8497 if (val > toc->rawsize)
8498 val = toc->rawsize;
ba761f19
AM
8499 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8500 continue;
8501 else if ((skip[val >> 3] & can_optimize) != 0)
8502 {
8503 Elf_Internal_Rela *tocrel
425b145b 8504 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8505 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8506
8507 switch (r_type)
8508 {
8509 case R_PPC64_TOC16_HA:
8510 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8511 break;
8512
8513 case R_PPC64_TOC16_LO_DS:
8514 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8515 break;
8516
8517 default:
28942f62
AM
8518 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
8519 ppc_howto_init ();
b140b010
AM
8520 info->callbacks->einfo
8521 (_("%P: %H: %s relocation references "
8522 "optimized away TOC entry\n"),
8523 ibfd, sec, rel->r_offset,
8524 ppc64_elf_howto_table[r_type]->name);
8525 bfd_set_error (bfd_error_bad_value);
8526 goto error_ret;
ba761f19
AM
8527 }
8528 rel->r_addend = tocrel->r_addend;
8529 elf_section_data (sec)->relocs = relstart;
8530 continue;
8531 }
8532
8533 if (h != NULL || sym->st_value != 0)
8534 continue;
854b41e7
AM
8535
8536 rel->r_addend -= skip[val >> 3];
8537 elf_section_data (sec)->relocs = relstart;
c5614fa4 8538 }
854b41e7
AM
8539
8540 if (elf_section_data (sec)->relocs != relstart)
8541 free (relstart);
c5614fa4
AM
8542 }
8543
8544 /* We shouldn't have local or global symbols defined in the TOC,
8545 but handle them anyway. */
df22d223
AM
8546 if (local_syms != NULL)
8547 for (sym = local_syms;
8548 sym < local_syms + symtab_hdr->sh_info;
8549 ++sym)
8550 if (sym->st_value != 0
8551 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8552 {
8553 unsigned long i;
854b41e7 8554
df22d223
AM
8555 if (sym->st_value > toc->rawsize)
8556 i = toc->rawsize >> 3;
8557 else
8558 i = sym->st_value >> 3;
854b41e7 8559
df22d223
AM
8560 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8561 {
8562 if (local_toc_syms)
8563 (*_bfd_error_handler)
8564 (_("%s defined on removed toc entry"),
8565 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8566 do
8567 ++i;
8568 while ((skip[i] & (ref_from_discarded | can_optimize)));
8569 sym->st_value = (bfd_vma) i << 3;
8570 }
d62b3684 8571
df22d223
AM
8572 sym->st_value -= skip[i];
8573 symtab_hdr->contents = (unsigned char *) local_syms;
8574 }
c5614fa4 8575
854b41e7 8576 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8577 if (toc_inf.global_toc_syms)
8578 {
8579 toc_inf.toc = toc;
8580 toc_inf.skip = skip;
8581 toc_inf.global_toc_syms = FALSE;
8582 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8583 &toc_inf);
8584 }
854b41e7
AM
8585
8586 if (toc->reloc_count != 0)
8587 {
d4730f92 8588 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8589 Elf_Internal_Rela *wrel;
8590 bfd_size_type sz;
8591
854b41e7 8592 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8593 if (toc_relocs == NULL)
8594 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8595 info->keep_memory);
8596 if (toc_relocs == NULL)
8597 goto error_ret;
8598
425b145b
AM
8599 wrel = toc_relocs;
8600 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8601 if ((skip[rel->r_offset >> 3]
8602 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8603 {
8604 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8605 wrel->r_info = rel->r_info;
8606 wrel->r_addend = rel->r_addend;
8607 ++wrel;
8608 }
8609 else if (!dec_dynrel_count (rel->r_info, toc, info,
8610 &local_syms, NULL, NULL))
8611 goto error_ret;
8612
425b145b
AM
8613 elf_section_data (toc)->relocs = toc_relocs;
8614 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8615 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8616 sz = rel_hdr->sh_entsize;
8617 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8618 }
c5614fa4 8619 }
28be611c
AM
8620 else if (toc_relocs != NULL
8621 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8622 free (toc_relocs);
c5614fa4
AM
8623
8624 if (local_syms != NULL
8625 && symtab_hdr->contents != (unsigned char *) local_syms)
8626 {
8627 if (!info->keep_memory)
8628 free (local_syms);
8629 else
8630 symtab_hdr->contents = (unsigned char *) local_syms;
8631 }
8632 free (skip);
8633 }
8634
8635 return TRUE;
8636}
8637
1bbe0902
AM
8638/* Return true iff input section I references the TOC using
8639 instructions limited to +/-32k offsets. */
8640
8641bfd_boolean
8642ppc64_elf_has_small_toc_reloc (asection *i)
8643{
8644 return (is_ppc64_elf (i->owner)
8645 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8646}
8647
927be08e
AM
8648/* Allocate space for one GOT entry. */
8649
8650static void
8651allocate_got (struct elf_link_hash_entry *h,
8652 struct bfd_link_info *info,
8653 struct got_entry *gent)
8654{
8655 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8656 bfd_boolean dyn;
8657 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8658 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8659 ? 16 : 8);
8660 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8661 ? 2 : 1) * sizeof (Elf64_External_Rela);
8662 asection *got = ppc64_elf_tdata (gent->owner)->got;
8663
8664 gent->got.offset = got->size;
8665 got->size += entsize;
8666
8667 dyn = htab->elf.dynamic_sections_created;
8668 if ((info->shared
8669 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8670 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8671 || h->root.type != bfd_link_hash_undefweak))
8672 {
8673 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8674 relgot->size += rentsize;
8675 }
8676 else if (h->type == STT_GNU_IFUNC)
8677 {
8678 asection *relgot = htab->reliplt;
8679 relgot->size += rentsize;
8680 htab->got_reli_size += rentsize;
8681 }
8682}
8683
7865406b
AM
8684/* This function merges got entries in the same toc group. */
8685
8686static void
8687merge_got_entries (struct got_entry **pent)
8688{
8689 struct got_entry *ent, *ent2;
8690
8691 for (ent = *pent; ent != NULL; ent = ent->next)
8692 if (!ent->is_indirect)
8693 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8694 if (!ent2->is_indirect
8695 && ent2->addend == ent->addend
8696 && ent2->tls_type == ent->tls_type
8697 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8698 {
8699 ent2->is_indirect = TRUE;
8700 ent2->got.ent = ent;
8701 }
8702}
8703
65f38f15
AM
8704/* Allocate space in .plt, .got and associated reloc sections for
8705 dynamic relocs. */
5bd4f169 8706
b34976b6 8707static bfd_boolean
4ce794b7 8708allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8709{
65f38f15
AM
8710 struct bfd_link_info *info;
8711 struct ppc_link_hash_table *htab;
5bd4f169 8712 asection *s;
65f38f15 8713 struct ppc_link_hash_entry *eh;
6061a67d 8714 struct elf_dyn_relocs *p;
0b8bcf0d 8715 struct got_entry **pgent, *gent;
5bd4f169 8716
e92d460e 8717 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8718 return TRUE;
5bd4f169 8719
65f38f15
AM
8720 info = (struct bfd_link_info *) inf;
8721 htab = ppc_hash_table (info);
4dfe6ac6
NC
8722 if (htab == NULL)
8723 return FALSE;
5bd4f169 8724
e054468f
AM
8725 if ((htab->elf.dynamic_sections_created
8726 && h->dynindx != -1
8727 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8728 || h->type == STT_GNU_IFUNC)
5bd4f169 8729 {
411e1bfb
AM
8730 struct plt_entry *pent;
8731 bfd_boolean doneone = FALSE;
8732 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8733 if (pent->plt.refcount > 0)
8734 {
25f23106
AM
8735 if (!htab->elf.dynamic_sections_created
8736 || h->dynindx == -1)
e054468f
AM
8737 {
8738 s = htab->iplt;
8739 pent->plt.offset = s->size;
8740 s->size += PLT_ENTRY_SIZE;
8741 s = htab->reliplt;
8742 }
8743 else
8744 {
8745 /* If this is the first .plt entry, make room for the special
8746 first entry. */
8747 s = htab->plt;
8748 if (s->size == 0)
8749 s->size += PLT_INITIAL_ENTRY_SIZE;
8750
8751 pent->plt.offset = s->size;
8752
8753 /* Make room for this entry. */
8754 s->size += PLT_ENTRY_SIZE;
8755
8756 /* Make room for the .glink code. */
8757 s = htab->glink;
8758 if (s->size == 0)
8759 s->size += GLINK_CALL_STUB_SIZE;
8760 /* We need bigger stubs past index 32767. */
8761 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8762 s->size += 4;
8763 s->size += 2*4;
8764
8765 /* We also need to make an entry in the .rela.plt section. */
8766 s = htab->relplt;
8767 }
eea6121a 8768 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
8769 doneone = TRUE;
8770 }
8771 else
8772 pent->plt.offset = (bfd_vma) -1;
8773 if (!doneone)
65f38f15 8774 {
411e1bfb 8775 h->plt.plist = NULL;
f5385ebf 8776 h->needs_plt = 0;
65f38f15
AM
8777 }
8778 }
8779 else
8780 {
411e1bfb 8781 h->plt.plist = NULL;
f5385ebf 8782 h->needs_plt = 0;
65f38f15
AM
8783 }
8784
951fd09b
AM
8785 eh = (struct ppc_link_hash_entry *) h;
8786 /* Run through the TLS GD got entries first if we're changing them
8787 to TPREL. */
e7b938ca 8788 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
8789 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8790 if (gent->got.refcount > 0
8791 && (gent->tls_type & TLS_GD) != 0)
8792 {
8793 /* This was a GD entry that has been converted to TPREL. If
8794 there happens to be a TPREL entry we can use that one. */
8795 struct got_entry *ent;
8796 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8797 if (ent->got.refcount > 0
8798 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8799 && ent->addend == gent->addend
8800 && ent->owner == gent->owner)
951fd09b
AM
8801 {
8802 gent->got.refcount = 0;
8803 break;
8804 }
8805
8806 /* If not, then we'll be using our own TPREL entry. */
8807 if (gent->got.refcount != 0)
8808 gent->tls_type = TLS_TLS | TLS_TPREL;
8809 }
8810
7865406b
AM
8811 /* Remove any list entry that won't generate a word in the GOT before
8812 we call merge_got_entries. Otherwise we risk merging to empty
8813 entries. */
0b8bcf0d
AM
8814 pgent = &h->got.glist;
8815 while ((gent = *pgent) != NULL)
411e1bfb 8816 if (gent->got.refcount > 0)
7865406b
AM
8817 {
8818 if ((gent->tls_type & TLS_LD) != 0
8819 && !h->def_dynamic)
8820 {
8821 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8822 *pgent = gent->next;
8823 }
8824 else
8825 pgent = &gent->next;
8826 }
8827 else
8828 *pgent = gent->next;
8829
8830 if (!htab->do_multi_toc)
8831 merge_got_entries (&h->got.glist);
8832
8833 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8834 if (!gent->is_indirect)
411e1bfb
AM
8835 {
8836 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
8837 Undefined weak syms won't yet be marked as dynamic,
8838 nor will all TLS symbols. */
411e1bfb 8839 if (h->dynindx == -1
b099ab9f 8840 && !h->forced_local
25f23106 8841 && h->type != STT_GNU_IFUNC
b099ab9f 8842 && htab->elf.dynamic_sections_created)
411e1bfb 8843 {
c152c796 8844 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
8845 return FALSE;
8846 }
65f38f15 8847
0c8d6e5c 8848 if (!is_ppc64_elf (gent->owner))
927be08e 8849 abort ();
0ffa91dd 8850
927be08e 8851 allocate_got (h, info, gent);
411e1bfb 8852 }
65f38f15 8853
b099ab9f 8854 if (eh->dyn_relocs == NULL
25f23106
AM
8855 || (!htab->elf.dynamic_sections_created
8856 && h->type != STT_GNU_IFUNC))
b34976b6 8857 return TRUE;
65f38f15
AM
8858
8859 /* In the shared -Bsymbolic case, discard space allocated for
8860 dynamic pc-relative relocs against symbols which turn out to be
8861 defined in regular objects. For the normal shared case, discard
8862 space for relocs that have become local due to symbol visibility
8863 changes. */
8864
8865 if (info->shared)
8866 {
9c7a29a3 8867 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 8868 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
8869 generated via assembly. We want calls to protected symbols to
8870 resolve directly to the function rather than going via the plt.
8871 If people want function pointer comparisons to work as expected
8872 then they should avoid writing weird assembly. */
09695f56 8873 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 8874 {
6061a67d 8875 struct elf_dyn_relocs **pp;
65f38f15
AM
8876
8877 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 8878 {
65f38f15
AM
8879 p->count -= p->pc_count;
8880 p->pc_count = 0;
8881 if (p->count == 0)
8882 *pp = p->next;
8883 else
8884 pp = &p->next;
5bd4f169 8885 }
65f38f15 8886 }
4e795f50
AM
8887
8888 /* Also discard relocs on undefined weak syms with non-default
8889 visibility. */
cab87ef9
AM
8890 if (eh->dyn_relocs != NULL
8891 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
8892 {
8893 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8894 eh->dyn_relocs = NULL;
8895
8896 /* Make sure this symbol is output as a dynamic symbol.
8897 Undefined weak syms won't yet be marked as dynamic. */
8898 else if (h->dynindx == -1
8899 && !h->forced_local)
8900 {
8901 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8902 return FALSE;
8903 }
8904 }
65f38f15 8905 }
25f23106
AM
8906 else if (h->type == STT_GNU_IFUNC)
8907 {
8908 if (!h->non_got_ref)
8909 eh->dyn_relocs = NULL;
8910 }
f4656909 8911 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
8912 {
8913 /* For the non-shared case, discard space for relocs against
8914 symbols which turn out to need copy relocs or are not
8915 dynamic. */
8916
f5385ebf 8917 if (!h->non_got_ref
f5385ebf 8918 && !h->def_regular)
65f38f15
AM
8919 {
8920 /* Make sure this symbol is output as a dynamic symbol.
8921 Undefined weak syms won't yet be marked as dynamic. */
8922 if (h->dynindx == -1
f5385ebf 8923 && !h->forced_local)
65f38f15 8924 {
c152c796 8925 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8926 return FALSE;
65f38f15
AM
8927 }
8928
8929 /* If that succeeded, we know we'll be keeping all the
8930 relocs. */
8931 if (h->dynindx != -1)
8932 goto keep;
8933 }
8934
8935 eh->dyn_relocs = NULL;
8936
ec338859 8937 keep: ;
65f38f15
AM
8938 }
8939
8940 /* Finally, allocate space. */
8941 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8942 {
8943 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
8944 if (!htab->elf.dynamic_sections_created)
8945 sreloc = htab->reliplt;
eea6121a 8946 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
8947 }
8948
b34976b6 8949 return TRUE;
65f38f15
AM
8950}
8951
8952/* Find any dynamic relocs that apply to read-only sections. */
8953
b34976b6 8954static bfd_boolean
4ce794b7 8955readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
8956{
8957 struct ppc_link_hash_entry *eh;
6061a67d 8958 struct elf_dyn_relocs *p;
65f38f15
AM
8959
8960 eh = (struct ppc_link_hash_entry *) h;
8961 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8962 {
8963 asection *s = p->sec->output_section;
8964
8965 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8966 {
4ce794b7 8967 struct bfd_link_info *info = inf;
65f38f15
AM
8968
8969 info->flags |= DF_TEXTREL;
8970
8971 /* Not an error, just cut short the traversal. */
b34976b6 8972 return FALSE;
65f38f15
AM
8973 }
8974 }
b34976b6 8975 return TRUE;
65f38f15
AM
8976}
8977
8978/* Set the sizes of the dynamic sections. */
8979
b34976b6 8980static bfd_boolean
4ce794b7
AM
8981ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8982 struct bfd_link_info *info)
65f38f15
AM
8983{
8984 struct ppc_link_hash_table *htab;
8985 bfd *dynobj;
8986 asection *s;
b34976b6 8987 bfd_boolean relocs;
65f38f15 8988 bfd *ibfd;
7865406b 8989 struct got_entry *first_tlsld;
65f38f15
AM
8990
8991 htab = ppc_hash_table (info);
4dfe6ac6
NC
8992 if (htab == NULL)
8993 return FALSE;
8994
65f38f15
AM
8995 dynobj = htab->elf.dynobj;
8996 if (dynobj == NULL)
8997 abort ();
8998
8999 if (htab->elf.dynamic_sections_created)
9000 {
9001 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 9002 if (info->executable)
65f38f15
AM
9003 {
9004 s = bfd_get_section_by_name (dynobj, ".interp");
9005 if (s == NULL)
9006 abort ();
eea6121a 9007 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
9008 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9009 }
9010 }
9011
9012 /* Set up .got offsets for local syms, and space for local dynamic
9013 relocs. */
9014 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9015 {
411e1bfb
AM
9016 struct got_entry **lgot_ents;
9017 struct got_entry **end_lgot_ents;
e054468f
AM
9018 struct plt_entry **local_plt;
9019 struct plt_entry **end_local_plt;
f961d9dd 9020 unsigned char *lgot_masks;
65f38f15
AM
9021 bfd_size_type locsymcount;
9022 Elf_Internal_Shdr *symtab_hdr;
9023 asection *srel;
9024
0c8d6e5c 9025 if (!is_ppc64_elf (ibfd))
65f38f15
AM
9026 continue;
9027
9028 for (s = ibfd->sections; s != NULL; s = s->next)
9029 {
6061a67d 9030 struct elf_dyn_relocs *p;
65f38f15 9031
6edfbbad 9032 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 9033 {
ec338859
AM
9034 if (!bfd_is_abs_section (p->sec)
9035 && bfd_is_abs_section (p->sec->output_section))
9036 {
9037 /* Input section has been discarded, either because
9038 it is a copy of a linkonce section or due to
9039 linker script /DISCARD/, so we'll be discarding
9040 the relocs too. */
9041 }
248866a8 9042 else if (p->count != 0)
ec338859
AM
9043 {
9044 srel = elf_section_data (p->sec)->sreloc;
25f23106
AM
9045 if (!htab->elf.dynamic_sections_created)
9046 srel = htab->reliplt;
eea6121a 9047 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
9048 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9049 info->flags |= DF_TEXTREL;
ec338859 9050 }
65f38f15
AM
9051 }
9052 }
9053
411e1bfb
AM
9054 lgot_ents = elf_local_got_ents (ibfd);
9055 if (!lgot_ents)
65f38f15
AM
9056 continue;
9057
0ffa91dd 9058 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 9059 locsymcount = symtab_hdr->sh_info;
411e1bfb 9060 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
9061 local_plt = (struct plt_entry **) end_lgot_ents;
9062 end_local_plt = local_plt + locsymcount;
f961d9dd 9063 lgot_masks = (unsigned char *) end_local_plt;
e717da7e
AM
9064 s = ppc64_elf_tdata (ibfd)->got;
9065 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 9066 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 9067 {
0b8bcf0d 9068 struct got_entry **pent, *ent;
411e1bfb 9069
0b8bcf0d
AM
9070 pent = lgot_ents;
9071 while ((ent = *pent) != NULL)
411e1bfb
AM
9072 if (ent->got.refcount > 0)
9073 {
e7b938ca 9074 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 9075 {
927be08e 9076 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 9077 *pent = ent->next;
411e1bfb
AM
9078 }
9079 else
9080 {
25f23106 9081 unsigned int num = 1;
eea6121a 9082 ent->got.offset = s->size;
e7b938ca 9083 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
25f23106
AM
9084 num = 2;
9085 s->size += num * 8;
9086 if (info->shared)
9087 srel->size += num * sizeof (Elf64_External_Rela);
9088 else if ((*lgot_masks & PLT_IFUNC) != 0)
927be08e
AM
9089 {
9090 htab->reliplt->size
9091 += num * sizeof (Elf64_External_Rela);
9092 htab->got_reli_size
9093 += num * sizeof (Elf64_External_Rela);
9094 }
0b8bcf0d 9095 pent = &ent->next;
411e1bfb
AM
9096 }
9097 }
9098 else
0b8bcf0d 9099 *pent = ent->next;
65f38f15 9100 }
e054468f
AM
9101
9102 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9103 for (; local_plt < end_local_plt; ++local_plt)
9104 {
9105 struct plt_entry *ent;
9106
9107 for (ent = *local_plt; ent != NULL; ent = ent->next)
9108 if (ent->plt.refcount > 0)
9109 {
91d6fa6a 9110 s = htab->iplt;
e054468f
AM
9111 ent->plt.offset = s->size;
9112 s->size += PLT_ENTRY_SIZE;
9113
25f23106 9114 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9115 }
9116 else
9117 ent->plt.offset = (bfd_vma) -1;
9118 }
65f38f15
AM
9119 }
9120
9121 /* Allocate global sym .plt and .got entries, and space for global
9122 sym dynamic relocs. */
4ce794b7 9123 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 9124
7865406b 9125 first_tlsld = NULL;
102890f0
AM
9126 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9127 {
7865406b
AM
9128 struct got_entry *ent;
9129
0c8d6e5c 9130 if (!is_ppc64_elf (ibfd))
102890f0
AM
9131 continue;
9132
7865406b
AM
9133 ent = ppc64_tlsld_got (ibfd);
9134 if (ent->got.refcount > 0)
102890f0 9135 {
7865406b 9136 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9137 {
7865406b
AM
9138 ent->is_indirect = TRUE;
9139 ent->got.ent = first_tlsld;
9140 }
9141 else
9142 {
9143 if (first_tlsld == NULL)
9144 first_tlsld = ent;
9145 s = ppc64_elf_tdata (ibfd)->got;
9146 ent->got.offset = s->size;
9147 ent->owner = ibfd;
9148 s->size += 16;
9149 if (info->shared)
9150 {
9151 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9152 srel->size += sizeof (Elf64_External_Rela);
9153 }
102890f0
AM
9154 }
9155 }
9156 else
7865406b 9157 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9158 }
9159
65f38f15
AM
9160 /* We now have determined the sizes of the various dynamic sections.
9161 Allocate memory for them. */
b34976b6 9162 relocs = FALSE;
65f38f15
AM
9163 for (s = dynobj->sections; s != NULL; s = s->next)
9164 {
9165 if ((s->flags & SEC_LINKER_CREATED) == 0)
9166 continue;
9167
4ce794b7 9168 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9169 /* These haven't been allocated yet; don't strip. */
9170 continue;
e717da7e
AM
9171 else if (s == htab->got
9172 || s == htab->plt
e054468f 9173 || s == htab->iplt
c456f082
AM
9174 || s == htab->glink
9175 || s == htab->dynbss)
65f38f15
AM
9176 {
9177 /* Strip this section if we don't need it; see the
9178 comment below. */
5bd4f169 9179 }
58d180e8
AM
9180 else if (s == htab->glink_eh_frame)
9181 {
9182 if (!bfd_is_abs_section (s->output_section))
9183 /* Not sized yet. */
9184 continue;
9185 }
70cc837d 9186 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9187 {
c456f082 9188 if (s->size != 0)
5bd4f169 9189 {
4ce794b7 9190 if (s != htab->relplt)
b34976b6 9191 relocs = TRUE;
5bd4f169
AM
9192
9193 /* We use the reloc_count field as a counter if we need
9194 to copy relocs into the output file. */
9195 s->reloc_count = 0;
9196 }
9197 }
65f38f15 9198 else
5bd4f169
AM
9199 {
9200 /* It's not one of our sections, so don't allocate space. */
9201 continue;
9202 }
9203
eea6121a 9204 if (s->size == 0)
5bd4f169 9205 {
c456f082
AM
9206 /* If we don't need this section, strip it from the
9207 output file. This is mostly to handle .rela.bss and
9208 .rela.plt. We must create both sections in
9209 create_dynamic_sections, because they must be created
9210 before the linker maps input sections to output
9211 sections. The linker does that before
9212 adjust_dynamic_symbol is called, and it is that
9213 function which decides whether anything needs to go
9214 into these sections. */
8423293d 9215 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9216 continue;
9217 }
9218
c456f082 9219 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9220 continue;
9221
65f38f15
AM
9222 /* Allocate memory for the section contents. We use bfd_zalloc
9223 here in case unused entries are not reclaimed before the
9224 section's contents are written out. This should not happen,
411e1bfb
AM
9225 but this way if it does we get a R_PPC64_NONE reloc in .rela
9226 sections instead of garbage.
9227 We also rely on the section contents being zero when writing
9228 the GOT. */
eea6121a 9229 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9230 if (s->contents == NULL)
b34976b6 9231 return FALSE;
5bd4f169
AM
9232 }
9233
e717da7e
AM
9234 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9235 {
0c8d6e5c 9236 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9237 continue;
9238
e717da7e
AM
9239 s = ppc64_elf_tdata (ibfd)->got;
9240 if (s != NULL && s != htab->got)
9241 {
eea6121a 9242 if (s->size == 0)
8423293d 9243 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9244 else
9245 {
eea6121a 9246 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9247 if (s->contents == NULL)
9248 return FALSE;
9249 }
9250 }
9251 s = ppc64_elf_tdata (ibfd)->relgot;
9252 if (s != NULL)
9253 {
eea6121a 9254 if (s->size == 0)
8423293d 9255 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9256 else
9257 {
eea6121a 9258 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9259 if (s->contents == NULL)
9260 return FALSE;
9261 relocs = TRUE;
9262 s->reloc_count = 0;
9263 }
9264 }
9265 }
9266
e86ce104 9267 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9268 {
9269 /* Add some entries to the .dynamic section. We fill in the
9270 values later, in ppc64_elf_finish_dynamic_sections, but we
9271 must add the entries now so that we get the correct size for
9272 the .dynamic section. The DT_DEBUG entry is filled in by the
9273 dynamic linker and used by the debugger. */
dc810e39 9274#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9275 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9276
36af4a4e 9277 if (info->executable)
5bd4f169 9278 {
dc810e39 9279 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9280 return FALSE;
5bd4f169
AM
9281 }
9282
eea6121a 9283 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9284 {
dc810e39
AM
9285 if (!add_dynamic_entry (DT_PLTGOT, 0)
9286 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9287 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9288 || !add_dynamic_entry (DT_JMPREL, 0)
9289 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9290 return FALSE;
5bd4f169
AM
9291 }
9292
19397422
AM
9293 if (NO_OPD_RELOCS)
9294 {
9295 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9296 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9297 return FALSE;
19397422
AM
9298 }
9299
a7f2871e
AM
9300 if (!htab->no_tls_get_addr_opt
9301 && htab->tls_get_addr_fd != NULL
9302 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9303 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9304 return FALSE;
9305
5bd4f169
AM
9306 if (relocs)
9307 {
dc810e39
AM
9308 if (!add_dynamic_entry (DT_RELA, 0)
9309 || !add_dynamic_entry (DT_RELASZ, 0)
9310 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9311 return FALSE;
5bd4f169 9312
65f38f15
AM
9313 /* If any dynamic relocs apply to a read-only section,
9314 then we need a DT_TEXTREL entry. */
248866a8 9315 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9316 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9317
65f38f15 9318 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9319 {
65f38f15 9320 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9321 return FALSE;
5bd4f169 9322 }
5bd4f169 9323 }
5bd4f169 9324 }
65f38f15 9325#undef add_dynamic_entry
5bd4f169 9326
b34976b6 9327 return TRUE;
5bd4f169
AM
9328}
9329
721956f4 9330/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9331
4ce794b7
AM
9332static inline enum ppc_stub_type
9333ppc_type_of_stub (asection *input_sec,
9334 const Elf_Internal_Rela *rel,
9335 struct ppc_link_hash_entry **hash,
e054468f 9336 struct plt_entry **plt_ent,
4ce794b7 9337 bfd_vma destination)
5bd4f169 9338{
721956f4
AM
9339 struct ppc_link_hash_entry *h = *hash;
9340 bfd_vma location;
9341 bfd_vma branch_offset;
9342 bfd_vma max_branch_offset;
4ce794b7 9343 enum elf_ppc64_reloc_type r_type;
5bd4f169 9344
721956f4
AM
9345 if (h != NULL)
9346 {
e054468f 9347 struct plt_entry *ent;
7fe2b9a6 9348 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9349 if (h->oh != NULL
9350 && h->oh->is_func_descriptor)
7b8f6675
AM
9351 {
9352 fdh = ppc_follow_link (h->oh);
9353 *hash = fdh;
9354 }
8387904d 9355
e054468f
AM
9356 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9357 if (ent->addend == rel->r_addend
9358 && ent->plt.offset != (bfd_vma) -1)
9359 {
e054468f
AM
9360 *plt_ent = ent;
9361 return ppc_stub_plt_call;
9362 }
5bd4f169 9363
7fe2b9a6
AM
9364 /* Here, we know we don't have a plt entry. If we don't have a
9365 either a defined function descriptor or a defined entry symbol
9366 in a regular object file, then it is pointless trying to make
9367 any other type of stub. */
854b41e7
AM
9368 if (!is_static_defined (&fdh->elf)
9369 && !is_static_defined (&h->elf))
721956f4 9370 return ppc_stub_none;
5d1634d7 9371 }
e054468f
AM
9372 else if (elf_local_got_ents (input_sec->owner) != NULL)
9373 {
9374 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9375 struct plt_entry **local_plt = (struct plt_entry **)
9376 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9377 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9378
9379 if (local_plt[r_symndx] != NULL)
9380 {
9381 struct plt_entry *ent;
9382
9383 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9384 if (ent->addend == rel->r_addend
9385 && ent->plt.offset != (bfd_vma) -1)
9386 {
9387 *plt_ent = ent;
9388 return ppc_stub_plt_call;
9389 }
9390 }
9391 }
5d1634d7 9392
721956f4
AM
9393 /* Determine where the call point is. */
9394 location = (input_sec->output_offset
9395 + input_sec->output_section->vma
9396 + rel->r_offset);
5d1634d7 9397
721956f4
AM
9398 branch_offset = destination - location;
9399 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9400
721956f4
AM
9401 /* Determine if a long branch stub is needed. */
9402 max_branch_offset = 1 << 25;
4ce794b7 9403 if (r_type != R_PPC64_REL24)
721956f4 9404 max_branch_offset = 1 << 15;
5d1634d7 9405
721956f4
AM
9406 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9407 /* We need a stub. Figure out whether a long_branch or plt_branch
9408 is needed later. */
9409 return ppc_stub_long_branch;
5d1634d7 9410
721956f4 9411 return ppc_stub_none;
5d1634d7
AM
9412}
9413
9414/* Build a .plt call stub. */
9415
4ce794b7 9416static inline bfd_byte *
9df0ef5f
AM
9417build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r,
9418 bfd_boolean plt_static_chain)
5d1634d7
AM
9419{
9420#define PPC_LO(v) ((v) & 0xffff)
9421#define PPC_HI(v) (((v) >> 16) & 0xffff)
9422#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9423
ac2df442
AM
9424 if (PPC_HA (offset) != 0)
9425 {
176a0d42
AM
9426 if (r != NULL)
9427 {
3b421ab3 9428 r[0].r_offset += 4;
176a0d42 9429 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
3b421ab3 9430 r[1].r_offset = r[0].r_offset + 4;
176a0d42
AM
9431 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9432 r[1].r_addend = r[0].r_addend;
c7131b65 9433 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
9434 {
9435 r[2].r_offset = r[1].r_offset + 4;
9436 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9437 r[2].r_addend = r[0].r_addend;
9438 }
9439 else
9440 {
9441 r[2].r_offset = r[1].r_offset + 8;
9442 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9443 r[2].r_addend = r[0].r_addend + 8;
c7131b65
AM
9444 if (plt_static_chain)
9445 {
9446 r[3].r_offset = r[2].r_offset + 4;
9447 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9448 r[3].r_addend = r[0].r_addend + 16;
9449 }
176a0d42
AM
9450 }
9451 }
ac2df442 9452 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
3b421ab3 9453 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
ac2df442 9454 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9df0ef5f 9455 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9456 {
9457 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9458 offset = 0;
9459 }
9460 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9461 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9df0ef5f
AM
9462 if (plt_static_chain)
9463 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
9464 bfd_put_32 (obfd, BCTR, p), p += 4;
9465 }
9466 else
9467 {
176a0d42
AM
9468 if (r != NULL)
9469 {
9470 r[0].r_offset += 4;
9471 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65 9472 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
176a0d42
AM
9473 {
9474 r[1].r_offset = r[0].r_offset + 4;
9475 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9476 r[1].r_addend = r[0].r_addend;
9477 }
9478 else
9479 {
9480 r[1].r_offset = r[0].r_offset + 8;
9481 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
c7131b65
AM
9482 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
9483 if (plt_static_chain)
9484 {
9485 r[2].r_offset = r[1].r_offset + 4;
9486 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9487 r[2].r_addend = r[0].r_addend + 8;
9488 }
176a0d42
AM
9489 }
9490 }
ac2df442
AM
9491 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9492 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9df0ef5f 9493 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9494 {
9495 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9496 offset = 0;
9497 }
9498 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9df0ef5f
AM
9499 if (plt_static_chain)
9500 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
9501 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9502 bfd_put_32 (obfd, BCTR, p), p += 4;
9503 }
5d1634d7
AM
9504 return p;
9505}
9506
a7f2871e
AM
9507/* Build a special .plt call stub for __tls_get_addr. */
9508
9509#define LD_R11_0R3 0xe9630000
9510#define LD_R12_0R3 0xe9830000
9511#define MR_R0_R3 0x7c601b78
9512#define CMPDI_R11_0 0x2c2b0000
9513#define ADD_R3_R12_R13 0x7c6c6a14
9514#define BEQLR 0x4d820020
9515#define MR_R3_R0 0x7c030378
9516#define MFLR_R11 0x7d6802a6
9517#define STD_R11_0R1 0xf9610000
9518#define BCTRL 0x4e800421
9519#define LD_R11_0R1 0xe9610000
9520#define LD_R2_0R1 0xe8410000
9521#define MTLR_R11 0x7d6803a6
9522
9523static inline bfd_byte *
9524build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9df0ef5f 9525 Elf_Internal_Rela *r, bfd_boolean plt_static_chain)
a7f2871e
AM
9526{
9527 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9528 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9529 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9530 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9531 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9532 bfd_put_32 (obfd, BEQLR, p), p += 4;
9533 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9534 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9535 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9536
9537 if (r != NULL)
9538 r[0].r_offset += 9 * 4;
9df0ef5f 9539 p = build_plt_stub (obfd, p, offset, r, plt_static_chain);
a7f2871e
AM
9540 bfd_put_32 (obfd, BCTRL, p - 4);
9541
9542 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9543 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9544 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9545 bfd_put_32 (obfd, BLR, p), p += 4;
9546
9547 return p;
9548}
9549
176a0d42
AM
9550static Elf_Internal_Rela *
9551get_relocs (asection *sec, int count)
9552{
9553 Elf_Internal_Rela *relocs;
9554 struct bfd_elf_section_data *elfsec_data;
9555
9556 elfsec_data = elf_section_data (sec);
9557 relocs = elfsec_data->relocs;
9558 if (relocs == NULL)
9559 {
9560 bfd_size_type relsize;
9561 relsize = sec->reloc_count * sizeof (*relocs);
9562 relocs = bfd_alloc (sec->owner, relsize);
9563 if (relocs == NULL)
9564 return NULL;
9565 elfsec_data->relocs = relocs;
d4730f92
BS
9566 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9567 sizeof (Elf_Internal_Shdr));
9568 if (elfsec_data->rela.hdr == NULL)
9569 return NULL;
9570 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9571 * sizeof (Elf64_External_Rela));
9572 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9573 sec->reloc_count = 0;
9574 }
9575 relocs += sec->reloc_count;
9576 sec->reloc_count += count;
9577 return relocs;
9578}
9579
aa374f67 9580static bfd_vma
25f53a85 9581get_r2off (struct bfd_link_info *info,
aa374f67
AM
9582 struct ppc_stub_hash_entry *stub_entry)
9583{
25f53a85 9584 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
9585 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9586
9587 if (r2off == 0)
9588 {
9589 /* Support linking -R objects. Get the toc pointer from the
9590 opd entry. */
9591 char buf[8];
9592 asection *opd = stub_entry->h->elf.root.u.def.section;
9593 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9594
9595 if (strcmp (opd->name, ".opd") != 0
9596 || opd->reloc_count != 0)
9597 {
8de848d8 9598 info->callbacks->einfo (_("%P: cannot find opd entry toc for %s\n"),
25f53a85 9599 stub_entry->h->elf.root.root.string);
aa374f67
AM
9600 bfd_set_error (bfd_error_bad_value);
9601 return 0;
9602 }
9603 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9604 return 0;
9605 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 9606 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
9607 }
9608 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9609 return r2off;
9610}
9611
b34976b6 9612static bfd_boolean
4ce794b7 9613ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 9614{
721956f4
AM
9615 struct ppc_stub_hash_entry *stub_entry;
9616 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
9617 struct bfd_link_info *info;
9618 struct ppc_link_hash_table *htab;
721956f4
AM
9619 bfd_byte *loc;
9620 bfd_byte *p;
ee75fd95 9621 bfd_vma dest, off;
721956f4 9622 int size;
176a0d42 9623 Elf_Internal_Rela *r;
e054468f 9624 asection *plt;
5d1634d7 9625
721956f4
AM
9626 /* Massage our args to the form they really have. */
9627 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 9628 info = in_arg;
5d1634d7 9629
5d1634d7 9630 htab = ppc_hash_table (info);
4dfe6ac6
NC
9631 if (htab == NULL)
9632 return FALSE;
5d1634d7 9633
721956f4 9634 /* Make a note of the offset within the stubs for this entry. */
eea6121a 9635 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 9636 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 9637
4ce794b7 9638 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 9639 switch (stub_entry->stub_type)
5d1634d7 9640 {
721956f4 9641 case ppc_stub_long_branch:
ad8e1ba5 9642 case ppc_stub_long_branch_r2off:
721956f4 9643 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
9644 off = dest = (stub_entry->target_value
9645 + stub_entry->target_section->output_offset
9646 + stub_entry->target_section->output_section->vma);
5d1634d7 9647
721956f4
AM
9648 /* And this is where we are coming from. */
9649 off -= (stub_entry->stub_offset
97b639ba
AM
9650 + stub_entry->stub_sec->output_offset
9651 + stub_entry->stub_sec->output_section->vma);
e86ce104 9652
ac2df442
AM
9653 size = 4;
9654 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 9655 {
25f53a85 9656 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 9657
aa374f67
AM
9658 if (r2off == 0)
9659 {
9660 htab->stub_error = TRUE;
9661 return FALSE;
9662 }
97b639ba 9663 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9664 loc += 4;
ac2df442
AM
9665 size = 12;
9666 if (PPC_HA (r2off) != 0)
9667 {
9668 size = 16;
9669 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9670 loc += 4;
9671 }
97b639ba 9672 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 9673 loc += 4;
ac2df442 9674 off -= size - 4;
ad8e1ba5 9675 }
97b639ba 9676 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 9677
5c3dead3
AM
9678 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9679 {
8de848d8 9680 info->callbacks->einfo (_("%P: long branch stub `%s' offset overflow\n"),
25f53a85 9681 stub_entry->root.string);
5c3dead3
AM
9682 htab->stub_error = TRUE;
9683 return FALSE;
9684 }
ee75fd95
AM
9685
9686 if (info->emitrelocations)
9687 {
176a0d42
AM
9688 r = get_relocs (stub_entry->stub_sec, 1);
9689 if (r == NULL)
9690 return FALSE;
ee75fd95
AM
9691 r->r_offset = loc - stub_entry->stub_sec->contents;
9692 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9693 r->r_addend = dest;
9694 if (stub_entry->h != NULL)
9695 {
9696 struct elf_link_hash_entry **hashes;
9697 unsigned long symndx;
9698 struct ppc_link_hash_entry *h;
9699
9700 hashes = elf_sym_hashes (htab->stub_bfd);
9701 if (hashes == NULL)
9702 {
9703 bfd_size_type hsize;
9704
9705 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9706 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9707 if (hashes == NULL)
9708 return FALSE;
9709 elf_sym_hashes (htab->stub_bfd) = hashes;
9710 htab->stub_globals = 1;
9711 }
9712 symndx = htab->stub_globals++;
9713 h = stub_entry->h;
9714 hashes[symndx] = &h->elf;
9715 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9716 if (h->oh != NULL && h->oh->is_func)
b31867b6 9717 h = ppc_follow_link (h->oh);
ee75fd95
AM
9718 if (h->elf.root.u.def.section != stub_entry->target_section)
9719 /* H is an opd symbol. The addend must be zero. */
9720 r->r_addend = 0;
9721 else
9722 {
9723 off = (h->elf.root.u.def.value
9724 + h->elf.root.u.def.section->output_offset
9725 + h->elf.root.u.def.section->output_section->vma);
9726 r->r_addend -= off;
9727 }
9728 }
9729 }
721956f4 9730 break;
e86ce104 9731
721956f4 9732 case ppc_stub_plt_branch:
ad8e1ba5 9733 case ppc_stub_plt_branch_r2off:
721956f4
AM
9734 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9735 stub_entry->root.string + 9,
b34976b6 9736 FALSE, FALSE);
721956f4
AM
9737 if (br_entry == NULL)
9738 {
8de848d8 9739 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
25f53a85 9740 stub_entry->root.string);
b34976b6
AM
9741 htab->stub_error = TRUE;
9742 return FALSE;
721956f4
AM
9743 }
9744
176a0d42
AM
9745 dest = (stub_entry->target_value
9746 + stub_entry->target_section->output_offset
9747 + stub_entry->target_section->output_section->vma);
721956f4 9748
176a0d42 9749 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 9750 htab->brlt->contents + br_entry->offset);
721956f4 9751
f94498ff 9752 if (br_entry->iter == htab->stub_iteration)
721956f4 9753 {
f94498ff 9754 br_entry->iter = 0;
84f5d08e 9755
f94498ff 9756 if (htab->relbrlt != NULL)
84f5d08e 9757 {
f94498ff
AM
9758 /* Create a reloc for the branch lookup table entry. */
9759 Elf_Internal_Rela rela;
9760 bfd_byte *rl;
9761
9762 rela.r_offset = (br_entry->offset
9763 + htab->brlt->output_offset
9764 + htab->brlt->output_section->vma);
9765 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9766 rela.r_addend = dest;
f94498ff
AM
9767
9768 rl = htab->relbrlt->contents;
9769 rl += (htab->relbrlt->reloc_count++
9770 * sizeof (Elf64_External_Rela));
9771 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9772 }
9773 else if (info->emitrelocations)
9774 {
176a0d42
AM
9775 r = get_relocs (htab->brlt, 1);
9776 if (r == NULL)
9777 return FALSE;
9778 /* brlt, being SEC_LINKER_CREATED does not go through the
9779 normal reloc processing. Symbols and offsets are not
9780 translated from input file to output file form, so
9781 set up the offset per the output file. */
f94498ff
AM
9782 r->r_offset = (br_entry->offset
9783 + htab->brlt->output_offset
9784 + htab->brlt->output_section->vma);
9785 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9786 r->r_addend = dest;
84f5d08e 9787 }
84f5d08e 9788 }
721956f4 9789
176a0d42
AM
9790 dest = (br_entry->offset
9791 + htab->brlt->output_offset
9792 + htab->brlt->output_section->vma);
9793
9794 off = (dest
4ce794b7 9795 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 9796 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9797
ad8e1ba5 9798 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 9799 {
25f53a85 9800 info->callbacks->einfo
8de848d8 9801 (_("%P: linkage table error against `%s'\n"),
721956f4 9802 stub_entry->root.string);
5d1634d7 9803 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9804 htab->stub_error = TRUE;
9805 return FALSE;
5d1634d7 9806 }
41bd81ab 9807
176a0d42
AM
9808 if (info->emitrelocations)
9809 {
9810 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9811 if (r == NULL)
9812 return FALSE;
9813 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9814 if (bfd_big_endian (info->output_bfd))
9815 r[0].r_offset += 2;
176a0d42
AM
9816 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9817 r[0].r_offset += 4;
9818 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9819 r[0].r_addend = dest;
9820 if (PPC_HA (off) != 0)
9821 {
9822 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9823 r[1].r_offset = r[0].r_offset + 4;
9824 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9825 r[1].r_addend = r[0].r_addend;
9826 }
9827 }
9828
ad8e1ba5
AM
9829 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9830 {
176a0d42 9831 if (PPC_HA (off) != 0)
ac2df442
AM
9832 {
9833 size = 16;
176a0d42 9834 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9835 loc += 4;
176a0d42 9836 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9837 }
9838 else
9839 {
9840 size = 12;
176a0d42 9841 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442 9842 }
ad8e1ba5
AM
9843 }
9844 else
9845 {
25f53a85 9846 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
9847
9848 if (r2off == 0)
9849 {
9850 htab->stub_error = TRUE;
9851 return FALSE;
9852 }
ad8e1ba5 9853
97b639ba 9854 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9855 loc += 4;
ac2df442 9856 size = 20;
176a0d42 9857 if (PPC_HA (off) != 0)
ac2df442
AM
9858 {
9859 size += 4;
176a0d42 9860 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9861 loc += 4;
176a0d42 9862 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9863 loc += 4;
9864 }
9865 else
9866 {
176a0d42 9867 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442
AM
9868 loc += 4;
9869 }
9870
9871 if (PPC_HA (r2off) != 0)
9872 {
9873 size += 4;
9874 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9875 loc += 4;
9876 }
97b639ba 9877 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
9878 }
9879 loc += 4;
97b639ba 9880 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 9881 loc += 4;
97b639ba 9882 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 9883 break;
5d1634d7 9884
721956f4 9885 case ppc_stub_plt_call:
e054468f 9886 if (stub_entry->h != NULL
b31867b6
AM
9887 && stub_entry->h->is_func_descriptor
9888 && stub_entry->h->oh != NULL)
c862ae31 9889 {
b31867b6
AM
9890 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9891
9892 /* If the old-ABI "dot-symbol" is undefined make it weak so
9893 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9894 FIXME: We used to define the symbol on one of the call
9895 stubs instead, which is why we test symbol section id
9896 against htab->top_id in various places. Likely all
9897 these checks could now disappear. */
9898 if (fh->elf.root.type == bfd_link_hash_undefined)
9899 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
9900 /* Stop undo_symbol_twiddle changing it back to undefined. */
9901 fh->was_undefined = 0;
c862ae31
AM
9902 }
9903
721956f4 9904 /* Now build the stub. */
e054468f 9905 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 9906 if (dest >= (bfd_vma) -2)
721956f4
AM
9907 abort ();
9908
e054468f 9909 plt = htab->plt;
25f23106
AM
9910 if (!htab->elf.dynamic_sections_created
9911 || stub_entry->h == NULL
9912 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9913 plt = htab->iplt;
9914
9915 dest += plt->output_offset + plt->output_section->vma;
9916
9917 if (stub_entry->h == NULL
9918 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9919 {
9920 Elf_Internal_Rela rela;
9921 bfd_byte *rl;
9922
9923 rela.r_offset = dest;
25f23106 9924 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
9925 rela.r_addend = (stub_entry->target_value
9926 + stub_entry->target_section->output_offset
9927 + stub_entry->target_section->output_section->vma);
9928
25f23106
AM
9929 rl = (htab->reliplt->contents
9930 + (htab->reliplt->reloc_count++
9931 * sizeof (Elf64_External_Rela)));
9932 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
9933 stub_entry->plt_ent->plt.offset |= 1;
9934 }
176a0d42
AM
9935
9936 off = (dest
e054468f 9937 - elf_gp (plt->output_section->owner)
176a0d42 9938 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9939
ad8e1ba5 9940 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 9941 {
25f53a85 9942 info->callbacks->einfo
8de848d8 9943 (_("%P: linkage table error against `%s'\n"),
e054468f
AM
9944 stub_entry->h != NULL
9945 ? stub_entry->h->elf.root.root.string
9946 : "<local sym>");
721956f4 9947 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9948 htab->stub_error = TRUE;
9949 return FALSE;
721956f4
AM
9950 }
9951
176a0d42
AM
9952 r = NULL;
9953 if (info->emitrelocations)
9954 {
9955 r = get_relocs (stub_entry->stub_sec,
c7131b65
AM
9956 (2
9957 + (PPC_HA (off) != 0)
9958 + (htab->plt_static_chain
9959 && PPC_HA (off + 16) == PPC_HA (off))));
176a0d42
AM
9960 if (r == NULL)
9961 return FALSE;
9962 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9963 if (bfd_big_endian (info->output_bfd))
9964 r[0].r_offset += 2;
176a0d42
AM
9965 r[0].r_addend = dest;
9966 }
a7f2871e
AM
9967 if (stub_entry->h != NULL
9968 && (stub_entry->h == htab->tls_get_addr_fd
9969 || stub_entry->h == htab->tls_get_addr)
9970 && !htab->no_tls_get_addr_opt)
9df0ef5f
AM
9971 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r,
9972 htab->plt_static_chain);
a7f2871e 9973 else
9df0ef5f
AM
9974 p = build_plt_stub (htab->stub_bfd, loc, off, r,
9975 htab->plt_static_chain);
721956f4
AM
9976 size = p - loc;
9977 break;
9978
9979 default:
9980 BFD_FAIL ();
b34976b6 9981 return FALSE;
721956f4
AM
9982 }
9983
eea6121a 9984 stub_entry->stub_sec->size += size;
97b639ba 9985
ee75fd95 9986 if (htab->emit_stub_syms)
97b639ba
AM
9987 {
9988 struct elf_link_hash_entry *h;
ee75fd95
AM
9989 size_t len1, len2;
9990 char *name;
9991 const char *const stub_str[] = { "long_branch",
9992 "long_branch_r2off",
9993 "plt_branch",
9994 "plt_branch_r2off",
9995 "plt_call" };
9996
9997 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9998 len2 = strlen (stub_entry->root.string);
9999 name = bfd_malloc (len1 + len2 + 2);
10000 if (name == NULL)
10001 return FALSE;
10002 memcpy (name, stub_entry->root.string, 9);
10003 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10004 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10005 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
10006 if (h == NULL)
10007 return FALSE;
10008 if (h->root.type == bfd_link_hash_new)
10009 {
10010 h->root.type = bfd_link_hash_defined;
10011 h->root.u.def.section = stub_entry->stub_sec;
10012 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
10013 h->ref_regular = 1;
10014 h->def_regular = 1;
10015 h->ref_regular_nonweak = 1;
10016 h->forced_local = 1;
10017 h->non_elf = 0;
97b639ba
AM
10018 }
10019 }
10020
b34976b6 10021 return TRUE;
721956f4
AM
10022}
10023
10024/* As above, but don't actually build the stub. Just bump offset so
10025 we know stub section sizes, and select plt_branch stubs where
10026 long_branch stubs won't do. */
10027
b34976b6 10028static bfd_boolean
4ce794b7 10029ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
10030{
10031 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 10032 struct bfd_link_info *info;
721956f4
AM
10033 struct ppc_link_hash_table *htab;
10034 bfd_vma off;
10035 int size;
10036
10037 /* Massage our args to the form they really have. */
10038 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
10039 info = in_arg;
10040
10041 htab = ppc_hash_table (info);
4dfe6ac6
NC
10042 if (htab == NULL)
10043 return FALSE;
721956f4
AM
10044
10045 if (stub_entry->stub_type == ppc_stub_plt_call)
10046 {
e054468f
AM
10047 asection *plt;
10048 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 10049 if (off >= (bfd_vma) -2)
411e1bfb 10050 abort ();
e054468f 10051 plt = htab->plt;
25f23106
AM
10052 if (!htab->elf.dynamic_sections_created
10053 || stub_entry->h == NULL
10054 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
10055 plt = htab->iplt;
10056 off += (plt->output_offset
10057 + plt->output_section->vma
10058 - elf_gp (plt->output_section->owner)
ad8e1ba5 10059 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 10060
ad8e1ba5 10061 size = PLT_CALL_STUB_SIZE;
9df0ef5f
AM
10062 if (!htab->plt_static_chain)
10063 size -= 4;
ac2df442
AM
10064 if (PPC_HA (off) == 0)
10065 size -= 4;
c7131b65 10066 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
721956f4 10067 size += 4;
a7f2871e
AM
10068 if (stub_entry->h != NULL
10069 && (stub_entry->h == htab->tls_get_addr_fd
10070 || stub_entry->h == htab->tls_get_addr)
10071 && !htab->no_tls_get_addr_opt)
10072 size += 13 * 4;
176a0d42
AM
10073 if (info->emitrelocations)
10074 {
10075 stub_entry->stub_sec->reloc_count
c7131b65
AM
10076 += (2
10077 + (PPC_HA (off) != 0)
10078 + (htab->plt_static_chain
10079 && PPC_HA (off + 16) == PPC_HA (off)));
176a0d42
AM
10080 stub_entry->stub_sec->flags |= SEC_RELOC;
10081 }
721956f4
AM
10082 }
10083 else
10084 {
ad8e1ba5
AM
10085 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10086 variants. */
ac2df442
AM
10087 bfd_vma r2off = 0;
10088
721956f4
AM
10089 off = (stub_entry->target_value
10090 + stub_entry->target_section->output_offset
10091 + stub_entry->target_section->output_section->vma);
eea6121a 10092 off -= (stub_entry->stub_sec->size
721956f4
AM
10093 + stub_entry->stub_sec->output_offset
10094 + stub_entry->stub_sec->output_section->vma);
10095
ad8e1ba5
AM
10096 /* Reset the stub type from the plt variant in case we now
10097 can reach with a shorter stub. */
10098 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10099 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10100
10101 size = 4;
10102 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10103 {
25f53a85 10104 r2off = get_r2off (info, stub_entry);
aa374f67
AM
10105 if (r2off == 0)
10106 {
10107 htab->stub_error = TRUE;
10108 return FALSE;
10109 }
ac2df442
AM
10110 size = 12;
10111 if (PPC_HA (r2off) != 0)
10112 size = 16;
10113 off -= size - 4;
ad8e1ba5
AM
10114 }
10115
10116 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
10117 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10118 {
10119 struct ppc_branch_hash_entry *br_entry;
10120
10121 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10122 stub_entry->root.string + 9,
b34976b6 10123 TRUE, FALSE);
721956f4
AM
10124 if (br_entry == NULL)
10125 {
8de848d8 10126 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
25f53a85 10127 stub_entry->root.string);
b34976b6
AM
10128 htab->stub_error = TRUE;
10129 return FALSE;
721956f4
AM
10130 }
10131
10132 if (br_entry->iter != htab->stub_iteration)
10133 {
10134 br_entry->iter = htab->stub_iteration;
eea6121a
AM
10135 br_entry->offset = htab->brlt->size;
10136 htab->brlt->size += 8;
63bc6f6c 10137
ee75fd95 10138 if (htab->relbrlt != NULL)
eea6121a 10139 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
10140 else if (info->emitrelocations)
10141 {
10142 htab->brlt->reloc_count += 1;
10143 htab->brlt->flags |= SEC_RELOC;
10144 }
721956f4 10145 }
ad8e1ba5
AM
10146
10147 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
10148 off = (br_entry->offset
10149 + htab->brlt->output_offset
10150 + htab->brlt->output_section->vma
10151 - elf_gp (htab->brlt->output_section->owner)
10152 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10153
176a0d42
AM
10154 if (info->emitrelocations)
10155 {
10156 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10157 stub_entry->stub_sec->flags |= SEC_RELOC;
10158 }
10159
ac2df442
AM
10160 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10161 {
10162 size = 12;
176a0d42 10163 if (PPC_HA (off) != 0)
ac2df442
AM
10164 size = 16;
10165 }
10166 else
10167 {
10168 size = 20;
176a0d42 10169 if (PPC_HA (off) != 0)
ac2df442
AM
10170 size += 4;
10171
10172 if (PPC_HA (r2off) != 0)
10173 size += 4;
10174 }
721956f4 10175 }
84f5d08e
AM
10176 else if (info->emitrelocations)
10177 {
10178 stub_entry->stub_sec->reloc_count += 1;
10179 stub_entry->stub_sec->flags |= SEC_RELOC;
10180 }
721956f4
AM
10181 }
10182
eea6121a 10183 stub_entry->stub_sec->size += size;
b34976b6 10184 return TRUE;
721956f4
AM
10185}
10186
10187/* Set up various things so that we can make a list of input sections
10188 for each output section included in the link. Returns -1 on error,
cedb70c5 10189 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10190
10191int
927be08e
AM
10192ppc64_elf_setup_section_lists
10193 (struct bfd_link_info *info,
10194 asection *(*add_stub_section) (const char *, asection *),
10195 void (*layout_sections_again) (void))
721956f4
AM
10196{
10197 bfd *input_bfd;
734b6cf9 10198 int top_id, top_index, id;
721956f4 10199 asection *section;
734b6cf9 10200 asection **input_list;
721956f4
AM
10201 bfd_size_type amt;
10202 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10203
4dfe6ac6
NC
10204 if (htab == NULL)
10205 return -1;
927be08e
AM
10206 /* Stash our params away. */
10207 htab->add_stub_section = add_stub_section;
10208 htab->layout_sections_again = layout_sections_again;
4c52953f 10209
4ce794b7 10210 if (htab->brlt == NULL)
721956f4
AM
10211 return 0;
10212
1e2f5b6e 10213 /* Find the top input section id. */
3d6f9012 10214 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10215 input_bfd != NULL;
10216 input_bfd = input_bfd->link_next)
10217 {
721956f4
AM
10218 for (section = input_bfd->sections;
10219 section != NULL;
10220 section = section->next)
10221 {
10222 if (top_id < section->id)
10223 top_id = section->id;
10224 }
10225 }
721956f4 10226
8f3bab57 10227 htab->top_id = top_id;
721956f4 10228 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10229 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10230 if (htab->stub_group == NULL)
10231 return -1;
10232
3d6f9012
AM
10233 /* Set toc_off for com, und, abs and ind sections. */
10234 for (id = 0; id < 3; id++)
10235 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10236
734b6cf9
AM
10237 /* We can't use output_bfd->section_count here to find the top output
10238 section index as some sections may have been removed, and
8423293d 10239 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10240 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10241 section != NULL;
10242 section = section->next)
10243 {
10244 if (top_index < section->index)
10245 top_index = section->index;
10246 }
10247
10248 htab->top_index = top_index;
10249 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10250 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10251 htab->input_list = input_list;
10252 if (input_list == NULL)
10253 return -1;
10254
721956f4
AM
10255 return 1;
10256}
10257
927be08e
AM
10258/* Set up for first pass at multitoc partitioning. */
10259
10260void
10261ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10262{
10263 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10264
10265 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10266 htab->toc_curr = elf_gp (info->output_bfd);
10267 htab->toc_bfd = NULL;
10268 htab->toc_first_sec = NULL;
10269}
10270
e717da7e
AM
10271/* The linker repeatedly calls this function for each TOC input section
10272 and linker generated GOT section. Group input bfds such that the toc
927be08e 10273 within a group is less than 64k in size. */
ad8e1ba5 10274
927be08e 10275bfd_boolean
4ce794b7 10276ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10277{
10278 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10279 bfd_vma addr, off, limit;
ad8e1ba5 10280
4dfe6ac6
NC
10281 if (htab == NULL)
10282 return FALSE;
10283
927be08e 10284 if (!htab->second_toc_pass)
4c52953f 10285 {
927be08e 10286 /* Keep track of the first .toc or .got section for this input bfd. */
bf102f86
AM
10287 if (htab->toc_bfd != isec->owner)
10288 {
10289 htab->toc_bfd = isec->owner;
10290 htab->toc_first_sec = isec;
10291 }
927be08e 10292
bf102f86
AM
10293 addr = isec->output_offset + isec->output_section->vma;
10294 off = addr - htab->toc_curr;
d77c8a4b
AM
10295 limit = 0x80008000;
10296 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10297 limit = 0x10000;
10298 if (off + isec->size > limit)
bf102f86
AM
10299 {
10300 addr = (htab->toc_first_sec->output_offset
10301 + htab->toc_first_sec->output_section->vma);
10302 htab->toc_curr = addr;
10303 }
99877b66 10304
927be08e
AM
10305 /* toc_curr is the base address of this toc group. Set elf_gp
10306 for the input section to be the offset relative to the
10307 output toc base plus 0x8000. Making the input elf_gp an
10308 offset allows us to move the toc as a whole without
10309 recalculating input elf_gp. */
10310 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10311 off += TOC_BASE_OFF;
10312
10313 /* Die if someone uses a linker script that doesn't keep input
10314 file .toc and .got together. */
10315 if (elf_gp (isec->owner) != 0
10316 && elf_gp (isec->owner) != off)
10317 return FALSE;
10318
10319 elf_gp (isec->owner) = off;
10320 return TRUE;
4c52953f 10321 }
927be08e
AM
10322
10323 /* During the second pass toc_first_sec points to the start of
10324 a toc group, and toc_curr is used to track the old elf_gp.
10325 We use toc_bfd to ensure we only look at each bfd once. */
10326 if (htab->toc_bfd == isec->owner)
10327 return TRUE;
10328 htab->toc_bfd = isec->owner;
10329
10330 if (htab->toc_first_sec == NULL
10331 || htab->toc_curr != elf_gp (isec->owner))
10332 {
10333 htab->toc_curr = elf_gp (isec->owner);
10334 htab->toc_first_sec = isec;
10335 }
10336 addr = (htab->toc_first_sec->output_offset
10337 + htab->toc_first_sec->output_section->vma);
10338 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10339 elf_gp (isec->owner) = off;
10340
10341 return TRUE;
ad8e1ba5
AM
10342}
10343
927be08e
AM
10344/* Called via elf_link_hash_traverse to merge GOT entries for global
10345 symbol H. */
10346
10347static bfd_boolean
10348merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10349{
10350 if (h->root.type == bfd_link_hash_indirect)
10351 return TRUE;
10352
927be08e
AM
10353 merge_got_entries (&h->got.glist);
10354
10355 return TRUE;
10356}
10357
10358/* Called via elf_link_hash_traverse to allocate GOT entries for global
10359 symbol H. */
10360
10361static bfd_boolean
10362reallocate_got (struct elf_link_hash_entry *h, void *inf)
10363{
10364 struct got_entry *gent;
10365
10366 if (h->root.type == bfd_link_hash_indirect)
10367 return TRUE;
10368
927be08e
AM
10369 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10370 if (!gent->is_indirect)
10371 allocate_got (h, (struct bfd_link_info *) inf, gent);
10372 return TRUE;
10373}
10374
10375/* Called on the first multitoc pass after the last call to
10376 ppc64_elf_next_toc_section. This function removes duplicate GOT
10377 entries. */
10378
10379bfd_boolean
10380ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10381{
10382 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10383 struct bfd *ibfd, *ibfd2;
10384 bfd_boolean done_something;
10385
10386 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10387
7865406b
AM
10388 if (!htab->do_multi_toc)
10389 return FALSE;
10390
d0fae19d 10391 /* Merge global sym got entries within a toc group. */
927be08e
AM
10392 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10393
10394 /* And tlsld_got. */
10395 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10396 {
10397 struct got_entry *ent, *ent2;
10398
10399 if (!is_ppc64_elf (ibfd))
10400 continue;
10401
10402 ent = ppc64_tlsld_got (ibfd);
10403 if (!ent->is_indirect
10404 && ent->got.offset != (bfd_vma) -1)
10405 {
10406 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10407 {
10408 if (!is_ppc64_elf (ibfd2))
10409 continue;
10410
10411 ent2 = ppc64_tlsld_got (ibfd2);
10412 if (!ent2->is_indirect
10413 && ent2->got.offset != (bfd_vma) -1
10414 && elf_gp (ibfd2) == elf_gp (ibfd))
10415 {
10416 ent2->is_indirect = TRUE;
10417 ent2->got.ent = ent;
10418 }
10419 }
10420 }
10421 }
10422
10423 /* Zap sizes of got sections. */
10424 htab->reliplt->rawsize = htab->reliplt->size;
10425 htab->reliplt->size -= htab->got_reli_size;
10426 htab->got_reli_size = 0;
10427
10428 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10429 {
10430 asection *got, *relgot;
10431
10432 if (!is_ppc64_elf (ibfd))
10433 continue;
10434
10435 got = ppc64_elf_tdata (ibfd)->got;
10436 if (got != NULL)
10437 {
10438 got->rawsize = got->size;
10439 got->size = 0;
10440 relgot = ppc64_elf_tdata (ibfd)->relgot;
10441 relgot->rawsize = relgot->size;
10442 relgot->size = 0;
10443 }
10444 }
10445
10446 /* Now reallocate the got, local syms first. We don't need to
10447 allocate section contents again since we never increase size. */
10448 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10449 {
10450 struct got_entry **lgot_ents;
10451 struct got_entry **end_lgot_ents;
10452 struct plt_entry **local_plt;
10453 struct plt_entry **end_local_plt;
f961d9dd 10454 unsigned char *lgot_masks;
927be08e
AM
10455 bfd_size_type locsymcount;
10456 Elf_Internal_Shdr *symtab_hdr;
10457 asection *s, *srel;
10458
10459 if (!is_ppc64_elf (ibfd))
10460 continue;
10461
10462 lgot_ents = elf_local_got_ents (ibfd);
10463 if (!lgot_ents)
10464 continue;
10465
10466 symtab_hdr = &elf_symtab_hdr (ibfd);
10467 locsymcount = symtab_hdr->sh_info;
10468 end_lgot_ents = lgot_ents + locsymcount;
10469 local_plt = (struct plt_entry **) end_lgot_ents;
10470 end_local_plt = local_plt + locsymcount;
f961d9dd 10471 lgot_masks = (unsigned char *) end_local_plt;
927be08e
AM
10472 s = ppc64_elf_tdata (ibfd)->got;
10473 srel = ppc64_elf_tdata (ibfd)->relgot;
10474 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10475 {
10476 struct got_entry *ent;
10477
10478 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d
AM
10479 {
10480 unsigned int num = 1;
10481 ent->got.offset = s->size;
10482 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10483 num = 2;
10484 s->size += num * 8;
10485 if (info->shared)
10486 srel->size += num * sizeof (Elf64_External_Rela);
10487 else if ((*lgot_masks & PLT_IFUNC) != 0)
10488 {
10489 htab->reliplt->size
10490 += num * sizeof (Elf64_External_Rela);
10491 htab->got_reli_size
10492 += num * sizeof (Elf64_External_Rela);
10493 }
10494 }
927be08e
AM
10495 }
10496 }
10497
10498 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10499
10500 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10501 {
10502 struct got_entry *ent;
10503
10504 if (!is_ppc64_elf (ibfd))
10505 continue;
10506
10507 ent = ppc64_tlsld_got (ibfd);
10508 if (!ent->is_indirect
10509 && ent->got.offset != (bfd_vma) -1)
10510 {
10511 asection *s = ppc64_elf_tdata (ibfd)->got;
10512 ent->got.offset = s->size;
10513 s->size += 16;
10514 if (info->shared)
10515 {
10516 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10517 srel->size += sizeof (Elf64_External_Rela);
10518 }
10519 }
10520 }
10521
10522 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10523 if (!done_something)
10524 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10525 {
10526 asection *got;
10527
10528 if (!is_ppc64_elf (ibfd))
10529 continue;
10530
10531 got = ppc64_elf_tdata (ibfd)->got;
10532 if (got != NULL)
10533 {
10534 done_something = got->rawsize != got->size;
10535 if (done_something)
10536 break;
10537 }
10538 }
10539
10540 if (done_something)
10541 (*htab->layout_sections_again) ();
10542
10543 /* Set up for second pass over toc sections to recalculate elf_gp
10544 on input sections. */
10545 htab->toc_bfd = NULL;
10546 htab->toc_first_sec = NULL;
10547 htab->second_toc_pass = TRUE;
10548 return done_something;
10549}
10550
10551/* Called after second pass of multitoc partitioning. */
10552
10553void
10554ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10555{
10556 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10557
10558 /* After the second pass, toc_curr tracks the TOC offset used
10559 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 10560 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
10561}
10562
9b5ecbd0
AM
10563/* No toc references were found in ISEC. If the code in ISEC makes no
10564 calls, then there's no need to use toc adjusting stubs when branching
10565 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
10566 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10567 needed, and 2 if a cyclical call-graph was found but no other reason
10568 for a stub was detected. If called from the top level, a return of
10569 2 means the same as a return of 0. */
9b5ecbd0
AM
10570
10571static int
4ce794b7 10572toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 10573{
9b5ecbd0 10574 int ret;
70cc837d
AM
10575
10576 /* Mark this section as checked. */
10577 isec->call_check_done = 1;
9b5ecbd0 10578
772119ce
AM
10579 /* We know none of our code bearing sections will need toc stubs. */
10580 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10581 return 0;
10582
eea6121a 10583 if (isec->size == 0)
082c50f8
AM
10584 return 0;
10585
4c52953f
AM
10586 if (isec->output_section == NULL)
10587 return 0;
10588
4c52953f 10589 ret = 0;
70cc837d 10590 if (isec->reloc_count != 0)
9b5ecbd0 10591 {
70cc837d
AM
10592 Elf_Internal_Rela *relstart, *rel;
10593 Elf_Internal_Sym *local_syms;
10594 struct ppc_link_hash_table *htab;
2917689a 10595
70cc837d
AM
10596 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10597 info->keep_memory);
10598 if (relstart == NULL)
10599 return -1;
90aecf7a 10600
70cc837d
AM
10601 /* Look for branches to outside of this section. */
10602 local_syms = NULL;
10603 htab = ppc_hash_table (info);
10604 if (htab == NULL)
10605 return -1;
4c52953f 10606
70cc837d 10607 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 10608 {
70cc837d
AM
10609 enum elf_ppc64_reloc_type r_type;
10610 unsigned long r_symndx;
10611 struct elf_link_hash_entry *h;
10612 struct ppc_link_hash_entry *eh;
10613 Elf_Internal_Sym *sym;
10614 asection *sym_sec;
10615 struct _opd_sec_data *opd;
10616 bfd_vma sym_value;
10617 bfd_vma dest;
10618
10619 r_type = ELF64_R_TYPE (rel->r_info);
10620 if (r_type != R_PPC64_REL24
10621 && r_type != R_PPC64_REL14
10622 && r_type != R_PPC64_REL14_BRTAKEN
10623 && r_type != R_PPC64_REL14_BRNTAKEN)
10624 continue;
4c52953f 10625
70cc837d
AM
10626 r_symndx = ELF64_R_SYM (rel->r_info);
10627 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10628 isec->owner))
4c52953f 10629 {
70cc837d
AM
10630 ret = -1;
10631 break;
10632 }
4c52953f 10633
70cc837d
AM
10634 /* Calls to dynamic lib functions go through a plt call stub
10635 that uses r2. */
10636 eh = (struct ppc_link_hash_entry *) h;
10637 if (eh != NULL
10638 && (eh->elf.plt.plist != NULL
10639 || (eh->oh != NULL
10640 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10641 {
10642 ret = 1;
10643 break;
4c52953f
AM
10644 }
10645
70cc837d
AM
10646 if (sym_sec == NULL)
10647 /* Ignore other undefined symbols. */
4c52953f 10648 continue;
4c52953f 10649
70cc837d
AM
10650 /* Assume branches to other sections not included in the
10651 link need stubs too, to cover -R and absolute syms. */
10652 if (sym_sec->output_section == NULL)
10653 {
10654 ret = 1;
10655 break;
10656 }
4c52953f 10657
70cc837d
AM
10658 if (h == NULL)
10659 sym_value = sym->st_value;
10660 else
10661 {
10662 if (h->root.type != bfd_link_hash_defined
10663 && h->root.type != bfd_link_hash_defweak)
10664 abort ();
10665 sym_value = h->root.u.def.value;
10666 }
10667 sym_value += rel->r_addend;
4c52953f 10668
70cc837d
AM
10669 /* If this branch reloc uses an opd sym, find the code section. */
10670 opd = get_opd_info (sym_sec);
10671 if (opd != NULL)
10672 {
10673 if (h == NULL && opd->adjust != NULL)
10674 {
10675 long adjust;
4c52953f 10676
70cc837d
AM
10677 adjust = opd->adjust[sym->st_value / 8];
10678 if (adjust == -1)
10679 /* Assume deleted functions won't ever be called. */
10680 continue;
10681 sym_value += adjust;
10682 }
4c52953f 10683
70cc837d
AM
10684 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10685 if (dest == (bfd_vma) -1)
10686 continue;
10687 }
10688 else
10689 dest = (sym_value
10690 + sym_sec->output_offset
10691 + sym_sec->output_section->vma);
4c52953f 10692
70cc837d
AM
10693 /* Ignore branch to self. */
10694 if (sym_sec == isec)
10695 continue;
4c52953f 10696
70cc837d
AM
10697 /* If the called function uses the toc, we need a stub. */
10698 if (sym_sec->has_toc_reloc
10699 || sym_sec->makes_toc_func_call)
4c52953f 10700 {
70cc837d 10701 ret = 1;
4c52953f
AM
10702 break;
10703 }
70cc837d
AM
10704
10705 /* Assume any branch that needs a long branch stub might in fact
10706 need a plt_branch stub. A plt_branch stub uses r2. */
10707 else if (dest - (isec->output_offset
10708 + isec->output_section->vma
10709 + rel->r_offset) + (1 << 25) >= (2 << 25))
4c52953f 10710 {
70cc837d
AM
10711 ret = 1;
10712 break;
10713 }
10714
10715 /* If calling back to a section in the process of being
10716 tested, we can't say for sure that no toc adjusting stubs
10717 are needed, so don't return zero. */
10718 else if (sym_sec->call_check_in_progress)
10719 ret = 2;
10720
10721 /* Branches to another section that itself doesn't have any TOC
10722 references are OK. Recursively call ourselves to check. */
10723 else if (!sym_sec->call_check_done)
10724 {
10725 int recur;
10726
10727 /* Mark current section as indeterminate, so that other
10728 sections that call back to current won't be marked as
10729 known. */
10730 isec->call_check_in_progress = 1;
10731 recur = toc_adjusting_stub_needed (info, sym_sec);
10732 isec->call_check_in_progress = 0;
10733
4c52953f
AM
10734 if (recur != 0)
10735 {
70cc837d
AM
10736 ret = recur;
10737 if (recur != 2)
10738 break;
4c52953f
AM
10739 }
10740 }
4c52953f 10741 }
70cc837d
AM
10742
10743 if (local_syms != NULL
10744 && (elf_symtab_hdr (isec->owner).contents
10745 != (unsigned char *) local_syms))
10746 free (local_syms);
10747 if (elf_section_data (isec)->relocs != relstart)
10748 free (relstart);
9b5ecbd0
AM
10749 }
10750
70cc837d
AM
10751 if ((ret & 1) == 0
10752 && isec->map_head.s != NULL
10753 && (strcmp (isec->output_section->name, ".init") == 0
10754 || strcmp (isec->output_section->name, ".fini") == 0))
10755 {
10756 if (isec->map_head.s->has_toc_reloc
10757 || isec->map_head.s->makes_toc_func_call)
10758 ret = 1;
10759 else if (!isec->map_head.s->call_check_done)
10760 {
10761 int recur;
10762 isec->call_check_in_progress = 1;
10763 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10764 isec->call_check_in_progress = 0;
10765 if (recur != 0)
10766 ret = recur;
10767 }
10768 }
10769
10770 if (ret == 1)
10771 isec->makes_toc_func_call = 1;
4c52953f 10772
9b5ecbd0
AM
10773 return ret;
10774}
10775
721956f4
AM
10776/* The linker repeatedly calls this function for each input section,
10777 in the order that input sections are linked into output sections.
10778 Build lists of input sections to determine groupings between which
10779 we may insert linker stubs. */
10780
9b5ecbd0 10781bfd_boolean
4ce794b7 10782ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
10783{
10784 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10785
4dfe6ac6
NC
10786 if (htab == NULL)
10787 return FALSE;
10788
734b6cf9
AM
10789 if ((isec->output_section->flags & SEC_CODE) != 0
10790 && isec->output_section->index <= htab->top_index)
721956f4 10791 {
734b6cf9 10792 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 10793 /* Steal the link_sec pointer for our list. */
721956f4 10794#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
10795 /* This happens to make the list in reverse order,
10796 which is what we want. */
734b6cf9
AM
10797 PREV_SEC (isec) = *list;
10798 *list = isec;
721956f4 10799 }
ad8e1ba5 10800
4c52953f 10801 if (htab->multi_toc_needed)
9b5ecbd0 10802 {
4c52953f
AM
10803 /* If a code section has a function that uses the TOC then we need
10804 to use the right TOC (obviously). Also, make sure that .opd gets
10805 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
10806 can't find their function symbol (shouldn't ever happen now).
10807 Also specially treat .fixup for the linux kernel. .fixup
10808 contains branches, but only back to the function that hit an
10809 exception. */
10810 if (isec->has_toc_reloc
10811 || (isec->flags & SEC_CODE) == 0
10812 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
10813 {
10814 if (elf_gp (isec->owner) != 0)
10815 htab->toc_curr = elf_gp (isec->owner);
10816 }
6683a28d
AM
10817 else
10818 {
10819 if (!isec->call_check_done
10820 && toc_adjusting_stub_needed (info, isec) < 0)
10821 return FALSE;
10822 /* If we make a local call from this section, ie. a branch
10823 without a following nop, then we have no place to put a
10824 toc restoring insn. We must use the same toc group as
10825 the callee.
10826 Testing makes_toc_func_call actually tests for *any*
10827 calls to functions that need a good toc pointer. A more
10828 precise test would be better, as this one will set
10829 incorrect values for pasted .init/.fini fragments.
10830 (Fixed later in check_pasted_section.) */
10831 if (isec->makes_toc_func_call
10832 && elf_gp (isec->owner) != 0)
10833 htab->toc_curr = elf_gp (isec->owner);
10834 }
9b5ecbd0 10835 }
ad8e1ba5
AM
10836
10837 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 10838 Use the last TOC base. */
ad8e1ba5 10839 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 10840 return TRUE;
721956f4
AM
10841}
10842
70cc837d
AM
10843/* Check that all .init and .fini sections use the same toc, if they
10844 have toc relocs. */
10845
10846static bfd_boolean
10847check_pasted_section (struct bfd_link_info *info, const char *name)
10848{
10849 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10850
10851 if (o != NULL)
10852 {
10853 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10854 bfd_vma toc_off = 0;
10855 asection *i;
10856
10857 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10858 if (i->has_toc_reloc)
10859 {
10860 if (toc_off == 0)
10861 toc_off = htab->stub_group[i->id].toc_off;
10862 else if (toc_off != htab->stub_group[i->id].toc_off)
10863 return FALSE;
10864 }
6683a28d
AM
10865
10866 if (toc_off == 0)
10867 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10868 if (i->makes_toc_func_call)
10869 {
10870 toc_off = htab->stub_group[i->id].toc_off;
10871 break;
10872 }
10873
70cc837d
AM
10874 /* Make sure the whole pasted function uses the same toc offset. */
10875 if (toc_off != 0)
10876 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10877 htab->stub_group[i->id].toc_off = toc_off;
10878 }
10879 return TRUE;
10880}
10881
10882bfd_boolean
10883ppc64_elf_check_init_fini (struct bfd_link_info *info)
10884{
10885 return (check_pasted_section (info, ".init")
10886 & check_pasted_section (info, ".fini"));
10887}
10888
721956f4
AM
10889/* See whether we can group stub sections together. Grouping stub
10890 sections may result in fewer stubs. More importantly, we need to
10891 put all .init* and .fini* stubs at the beginning of the .init or
10892 .fini output sections respectively, because glibc splits the
10893 _init and _fini functions into multiple parts. Putting a stub in
10894 the middle of a function is not a good idea. */
10895
10896static void
4ce794b7
AM
10897group_sections (struct ppc_link_hash_table *htab,
10898 bfd_size_type stub_group_size,
10899 bfd_boolean stubs_always_before_branch)
721956f4 10900{
7c8fe5c4
AM
10901 asection **list;
10902 bfd_size_type stub14_group_size;
10903 bfd_boolean suppress_size_errors;
10904
10905 suppress_size_errors = FALSE;
10906 stub14_group_size = stub_group_size;
10907 if (stub_group_size == 1)
10908 {
10909 /* Default values. */
10910 if (stubs_always_before_branch)
10911 {
10912 stub_group_size = 0x1e00000;
10913 stub14_group_size = 0x7800;
10914 }
10915 else
10916 {
10917 stub_group_size = 0x1c00000;
10918 stub14_group_size = 0x7000;
10919 }
10920 suppress_size_errors = TRUE;
10921 }
10922
10923 list = htab->input_list + htab->top_index;
734b6cf9 10924 do
721956f4 10925 {
734b6cf9
AM
10926 asection *tail = *list;
10927 while (tail != NULL)
721956f4 10928 {
734b6cf9
AM
10929 asection *curr;
10930 asection *prev;
10931 bfd_size_type total;
10932 bfd_boolean big_sec;
10933 bfd_vma curr_toc;
10934
10935 curr = tail;
eea6121a 10936 total = tail->size;
6bee8834
AM
10937 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10938 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
10939 ? stub14_group_size : stub_group_size);
10940 if (big_sec && !suppress_size_errors)
5c3dead3
AM
10941 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10942 tail->owner, tail);
734b6cf9
AM
10943 curr_toc = htab->stub_group[tail->id].toc_off;
10944
10945 while ((prev = PREV_SEC (curr)) != NULL
10946 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
10947 < (ppc64_elf_section_data (prev) != NULL
10948 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10949 ? stub14_group_size : stub_group_size))
ad8e1ba5 10950 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
10951 curr = prev;
10952
10953 /* OK, the size from the start of CURR to the end is less
10954 than stub_group_size and thus can be handled by one stub
10955 section. (or the tail section is itself larger than
10956 stub_group_size, in which case we may be toast.) We
10957 should really be keeping track of the total size of stubs
10958 added here, as stubs contribute to the final output
10959 section size. That's a little tricky, and this way will
10960 only break if stubs added make the total size more than
10961 2^25, ie. for the default stub_group_size, if stubs total
10962 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10963 do
721956f4
AM
10964 {
10965 prev = PREV_SEC (tail);
734b6cf9 10966 /* Set up this stub group. */
721956f4
AM
10967 htab->stub_group[tail->id].link_sec = curr;
10968 }
734b6cf9
AM
10969 while (tail != curr && (tail = prev) != NULL);
10970
10971 /* But wait, there's more! Input sections up to stub_group_size
10972 bytes before the stub section can be handled by it too.
10973 Don't do this if we have a really large section after the
10974 stubs, as adding more stubs increases the chance that
10975 branches may not reach into the stub section. */
10976 if (!stubs_always_before_branch && !big_sec)
10977 {
10978 total = 0;
10979 while (prev != NULL
10980 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
10981 < (ppc64_elf_section_data (prev) != NULL
10982 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10983 ? stub14_group_size : stub_group_size))
734b6cf9
AM
10984 && htab->stub_group[prev->id].toc_off == curr_toc)
10985 {
10986 tail = prev;
10987 prev = PREV_SEC (tail);
10988 htab->stub_group[tail->id].link_sec = curr;
10989 }
10990 }
10991 tail = prev;
721956f4
AM
10992 }
10993 }
734b6cf9
AM
10994 while (list-- != htab->input_list);
10995 free (htab->input_list);
721956f4
AM
10996#undef PREV_SEC
10997}
10998
58d180e8
AM
10999static const unsigned char glink_eh_frame_cie[] =
11000{
11001 0, 0, 0, 16, /* length. */
11002 0, 0, 0, 0, /* id. */
11003 1, /* CIE version. */
11004 'z', 'R', 0, /* Augmentation string. */
11005 4, /* Code alignment. */
11006 0x78, /* Data alignment. */
11007 65, /* RA reg. */
11008 1, /* Augmentation size. */
11009 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11010 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11011};
11012
d969d15f
AM
11013/* Stripping output sections is normally done before dynamic section
11014 symbols have been allocated. This function is called later, and
11015 handles cases like htab->brlt which is mapped to its own output
11016 section. */
11017
11018static void
11019maybe_strip_output (struct bfd_link_info *info, asection *isec)
11020{
11021 if (isec->size == 0
11022 && isec->output_section->size == 0
11023 && !bfd_section_removed_from_list (info->output_bfd,
11024 isec->output_section)
11025 && elf_section_data (isec->output_section)->dynindx == 0)
11026 {
11027 isec->output_section->flags |= SEC_EXCLUDE;
11028 bfd_section_list_remove (info->output_bfd, isec->output_section);
11029 info->output_bfd->section_count--;
11030 }
11031}
11032
721956f4
AM
11033/* Determine and set the size of the stub section for a final link.
11034
11035 The basic idea here is to examine all the relocations looking for
11036 PC-relative calls to a target that is unreachable with a "bl"
11037 instruction. */
11038
b34976b6 11039bfd_boolean
9df0ef5f
AM
11040ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11041 bfd_boolean plt_static_chain)
721956f4
AM
11042{
11043 bfd_size_type stub_group_size;
b34976b6 11044 bfd_boolean stubs_always_before_branch;
721956f4
AM
11045 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11046
4dfe6ac6
NC
11047 if (htab == NULL)
11048 return FALSE;
11049
9df0ef5f 11050 htab->plt_static_chain = plt_static_chain;
721956f4
AM
11051 stubs_always_before_branch = group_size < 0;
11052 if (group_size < 0)
11053 stub_group_size = -group_size;
11054 else
11055 stub_group_size = group_size;
721956f4
AM
11056
11057 group_sections (htab, stub_group_size, stubs_always_before_branch);
11058
721956f4
AM
11059 while (1)
11060 {
11061 bfd *input_bfd;
11062 unsigned int bfd_indx;
11063 asection *stub_sec;
721956f4
AM
11064
11065 htab->stub_iteration += 1;
721956f4
AM
11066
11067 for (input_bfd = info->input_bfds, bfd_indx = 0;
11068 input_bfd != NULL;
11069 input_bfd = input_bfd->link_next, bfd_indx++)
11070 {
11071 Elf_Internal_Shdr *symtab_hdr;
11072 asection *section;
6cdc0ccc 11073 Elf_Internal_Sym *local_syms = NULL;
721956f4 11074
0c8d6e5c 11075 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
11076 continue;
11077
721956f4 11078 /* We'll need the symbol table in a second. */
0ffa91dd 11079 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
11080 if (symtab_hdr->sh_info == 0)
11081 continue;
11082
721956f4
AM
11083 /* Walk over each section attached to the input bfd. */
11084 for (section = input_bfd->sections;
11085 section != NULL;
11086 section = section->next)
11087 {
721956f4 11088 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
11089
11090 /* If there aren't any relocs, then there's nothing more
11091 to do. */
11092 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
11093 || (section->flags & SEC_ALLOC) == 0
11094 || (section->flags & SEC_LOAD) == 0
11095 || (section->flags & SEC_CODE) == 0
721956f4
AM
11096 || section->reloc_count == 0)
11097 continue;
11098
11099 /* If this section is a link-once section that will be
11100 discarded, then don't create any stubs. */
11101 if (section->output_section == NULL
927be08e 11102 || section->output_section->owner != info->output_bfd)
721956f4
AM
11103 continue;
11104
1e2f5b6e
AM
11105 /* Get the relocs. */
11106 internal_relocs
4ce794b7 11107 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 11108 info->keep_memory);
721956f4 11109 if (internal_relocs == NULL)
1e2f5b6e 11110 goto error_ret_free_local;
721956f4
AM
11111
11112 /* Now examine each relocation. */
11113 irela = internal_relocs;
11114 irelaend = irela + section->reloc_count;
11115 for (; irela < irelaend; irela++)
11116 {
4ce794b7
AM
11117 enum elf_ppc64_reloc_type r_type;
11118 unsigned int r_indx;
721956f4
AM
11119 enum ppc_stub_type stub_type;
11120 struct ppc_stub_hash_entry *stub_entry;
8387904d 11121 asection *sym_sec, *code_sec;
e054468f 11122 bfd_vma sym_value, code_value;
721956f4 11123 bfd_vma destination;
8843416a 11124 bfd_boolean ok_dest;
721956f4 11125 struct ppc_link_hash_entry *hash;
8387904d 11126 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
11127 struct elf_link_hash_entry *h;
11128 Elf_Internal_Sym *sym;
721956f4
AM
11129 char *stub_name;
11130 const asection *id_sec;
74f0fb50 11131 struct _opd_sec_data *opd;
e054468f 11132 struct plt_entry *plt_ent;
721956f4
AM
11133
11134 r_type = ELF64_R_TYPE (irela->r_info);
11135 r_indx = ELF64_R_SYM (irela->r_info);
11136
4ce794b7 11137 if (r_type >= R_PPC64_max)
721956f4
AM
11138 {
11139 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 11140 goto error_ret_free_internal;
721956f4
AM
11141 }
11142
11143 /* Only look for stubs on branch instructions. */
4ce794b7
AM
11144 if (r_type != R_PPC64_REL24
11145 && r_type != R_PPC64_REL14
11146 && r_type != R_PPC64_REL14_BRTAKEN
11147 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
11148 continue;
11149
11150 /* Now determine the call target, its name, value,
11151 section. */
411e1bfb
AM
11152 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11153 r_indx, input_bfd))
11154 goto error_ret_free_internal;
11155 hash = (struct ppc_link_hash_entry *) h;
11156
8843416a 11157 ok_dest = FALSE;
8387904d 11158 fdh = NULL;
7fe2b9a6 11159 sym_value = 0;
411e1bfb 11160 if (hash == NULL)
721956f4 11161 {
411e1bfb 11162 sym_value = sym->st_value;
8843416a 11163 ok_dest = TRUE;
721956f4 11164 }
7fe2b9a6
AM
11165 else if (hash->elf.root.type == bfd_link_hash_defined
11166 || hash->elf.root.type == bfd_link_hash_defweak)
11167 {
11168 sym_value = hash->elf.root.u.def.value;
11169 if (sym_sec->output_section != NULL)
11170 ok_dest = TRUE;
11171 }
11172 else if (hash->elf.root.type == bfd_link_hash_undefweak
11173 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11174 {
99877b66 11175 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11176 use the func descriptor sym instead if it is
11177 defined. */
ceb1f1ef 11178 if (hash->elf.root.root.string[0] == '.'
b31867b6 11179 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 11180 {
8387904d
AM
11181 if (fdh->elf.root.type == bfd_link_hash_defined
11182 || fdh->elf.root.type == bfd_link_hash_defweak)
11183 {
11184 sym_sec = fdh->elf.root.u.def.section;
11185 sym_value = fdh->elf.root.u.def.value;
11186 if (sym_sec->output_section != NULL)
11187 ok_dest = TRUE;
11188 }
99877b66
AM
11189 else
11190 fdh = NULL;
8387904d 11191 }
7fe2b9a6
AM
11192 }
11193 else
11194 {
11195 bfd_set_error (bfd_error_bad_value);
11196 goto error_ret_free_internal;
721956f4
AM
11197 }
11198
8843416a
AM
11199 destination = 0;
11200 if (ok_dest)
11201 {
11202 sym_value += irela->r_addend;
11203 destination = (sym_value
11204 + sym_sec->output_offset
11205 + sym_sec->output_section->vma);
11206 }
11207
8387904d 11208 code_sec = sym_sec;
e054468f 11209 code_value = sym_value;
74f0fb50
AM
11210 opd = get_opd_info (sym_sec);
11211 if (opd != NULL)
8387904d
AM
11212 {
11213 bfd_vma dest;
11214
74f0fb50 11215 if (hash == NULL && opd->adjust != NULL)
8387904d 11216 {
74f0fb50 11217 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11218 if (adjust == -1)
11219 continue;
e054468f 11220 code_value += adjust;
8387904d
AM
11221 sym_value += adjust;
11222 }
11223 dest = opd_entry_value (sym_sec, sym_value,
e054468f 11224 &code_sec, &code_value);
8387904d
AM
11225 if (dest != (bfd_vma) -1)
11226 {
11227 destination = dest;
11228 if (fdh != NULL)
11229 {
11230 /* Fixup old ABI sym to point at code
11231 entry. */
99877b66 11232 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11233 hash->elf.root.u.def.section = code_sec;
e054468f 11234 hash->elf.root.u.def.value = code_value;
8387904d
AM
11235 }
11236 }
11237 }
11238
721956f4 11239 /* Determine what (if any) linker stub is needed. */
e054468f 11240 plt_ent = NULL;
721956f4 11241 stub_type = ppc_type_of_stub (section, irela, &hash,
e054468f 11242 &plt_ent, destination);
ad8e1ba5
AM
11243
11244 if (stub_type != ppc_stub_plt_call)
11245 {
11246 /* Check whether we need a TOC adjusting stub.
11247 Since the linker pastes together pieces from
11248 different object files when creating the
11249 _init and _fini functions, it may be that a
11250 call to what looks like a local sym is in
11251 fact a call needing a TOC adjustment. */
8387904d
AM
11252 if (code_sec != NULL
11253 && code_sec->output_section != NULL
11254 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11255 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11256 && (code_sec->has_toc_reloc
11257 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
11258 stub_type = ppc_stub_long_branch_r2off;
11259 }
11260
721956f4
AM
11261 if (stub_type == ppc_stub_none)
11262 continue;
11263
411e1bfb
AM
11264 /* __tls_get_addr calls might be eliminated. */
11265 if (stub_type != ppc_stub_plt_call
11266 && hash != NULL
8387904d
AM
11267 && (hash == htab->tls_get_addr
11268 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11269 && section->has_tls_reloc
11270 && irela != internal_relocs)
11271 {
11272 /* Get tls info. */
f961d9dd 11273 unsigned char *tls_mask;
411e1bfb 11274
3a71aa26 11275 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11276 irela - 1, input_bfd))
11277 goto error_ret_free_internal;
e7b938ca 11278 if (*tls_mask != 0)
411e1bfb
AM
11279 continue;
11280 }
11281
3b421ab3
AM
11282 if (stub_type == ppc_stub_plt_call
11283 && irela + 1 < irelaend
11284 && irela[1].r_offset == irela->r_offset + 4
11285 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE
11286 && !tocsave_find (htab, INSERT,
11287 &local_syms, irela + 1, input_bfd))
11288 goto error_ret_free_internal;
11289
721956f4
AM
11290 /* Support for grouping stub sections. */
11291 id_sec = htab->stub_group[section->id].link_sec;
11292
11293 /* Get the name of this stub. */
11294 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11295 if (!stub_name)
11296 goto error_ret_free_internal;
11297
11298 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11299 stub_name, FALSE, FALSE);
721956f4
AM
11300 if (stub_entry != NULL)
11301 {
11302 /* The proper stub has already been created. */
11303 free (stub_name);
11304 continue;
11305 }
11306
25f53a85 11307 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
11308 if (stub_entry == NULL)
11309 {
11310 free (stub_name);
6cdc0ccc
AM
11311 error_ret_free_internal:
11312 if (elf_section_data (section)->relocs == NULL)
11313 free (internal_relocs);
11314 error_ret_free_local:
11315 if (local_syms != NULL
11316 && (symtab_hdr->contents
11317 != (unsigned char *) local_syms))
11318 free (local_syms);
b34976b6 11319 return FALSE;
721956f4
AM
11320 }
11321
ad8e1ba5 11322 stub_entry->stub_type = stub_type;
e054468f
AM
11323 if (stub_type != ppc_stub_plt_call)
11324 {
11325 stub_entry->target_value = code_value;
11326 stub_entry->target_section = code_sec;
11327 }
11328 else
11329 {
11330 stub_entry->target_value = sym_value;
11331 stub_entry->target_section = sym_sec;
11332 }
721956f4 11333 stub_entry->h = hash;
e054468f 11334 stub_entry->plt_ent = plt_ent;
411e1bfb 11335 stub_entry->addend = irela->r_addend;
ee75fd95
AM
11336
11337 if (stub_entry->h != NULL)
11338 htab->stub_globals += 1;
721956f4
AM
11339 }
11340
11341 /* We're done with the internal relocs, free them. */
6cdc0ccc 11342 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 11343 free (internal_relocs);
721956f4 11344 }
6cdc0ccc
AM
11345
11346 if (local_syms != NULL
11347 && symtab_hdr->contents != (unsigned char *) local_syms)
11348 {
11349 if (!info->keep_memory)
11350 free (local_syms);
11351 else
11352 symtab_hdr->contents = (unsigned char *) local_syms;
11353 }
721956f4
AM
11354 }
11355
5c3dead3 11356 /* We may have added some stubs. Find out the new size of the
721956f4
AM
11357 stub sections. */
11358 for (stub_sec = htab->stub_bfd->sections;
11359 stub_sec != NULL;
11360 stub_sec = stub_sec->next)
e717da7e 11361 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 11362 {
5c3dead3 11363 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
11364 stub_sec->size = 0;
11365 stub_sec->reloc_count = 0;
84f5d08e 11366 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 11367 }
eea6121a
AM
11368
11369 htab->brlt->size = 0;
84f5d08e
AM
11370 htab->brlt->reloc_count = 0;
11371 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 11372 if (htab->relbrlt != NULL)
eea6121a 11373 htab->relbrlt->size = 0;
721956f4 11374
63bc6f6c 11375 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 11376
176a0d42
AM
11377 if (info->emitrelocations
11378 && htab->glink != NULL && htab->glink->size != 0)
11379 {
11380 htab->glink->reloc_count = 1;
11381 htab->glink->flags |= SEC_RELOC;
11382 }
11383
58d180e8
AM
11384 if (htab->glink_eh_frame != NULL
11385 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
11386 && (htab->glink_eh_frame->flags & SEC_EXCLUDE) == 0)
11387 {
11388 bfd_size_type size = 0;
11389
11390 for (stub_sec = htab->stub_bfd->sections;
11391 stub_sec != NULL;
11392 stub_sec = stub_sec->next)
11393 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11394 size += 20;
11395 if (htab->glink != NULL && htab->glink->size != 0)
11396 size += 24;
11397 if (size != 0)
11398 size += sizeof (glink_eh_frame_cie);
11399 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11400 htab->glink_eh_frame->size = size;
11401 }
11402
5c3dead3
AM
11403 for (stub_sec = htab->stub_bfd->sections;
11404 stub_sec != NULL;
11405 stub_sec = stub_sec->next)
11406 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11407 && stub_sec->rawsize != stub_sec->size)
11408 break;
11409
11410 /* Exit from this loop when no stubs have been added, and no stubs
11411 have changed size. */
58d180e8
AM
11412 if (stub_sec == NULL
11413 && (htab->glink_eh_frame == NULL
11414 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
5c3dead3
AM
11415 break;
11416
721956f4
AM
11417 /* Ask the linker to do its stuff. */
11418 (*htab->layout_sections_again) ();
11419 }
11420
d969d15f
AM
11421 maybe_strip_output (info, htab->brlt);
11422 if (htab->glink_eh_frame != NULL)
11423 maybe_strip_output (info, htab->glink_eh_frame);
721956f4 11424
b34976b6 11425 return TRUE;
721956f4
AM
11426}
11427
11428/* Called after we have determined section placement. If sections
805fc799 11429 move, we'll be called again. Provide a value for TOCstart. */
721956f4 11430
805fc799 11431bfd_vma
4ce794b7 11432ppc64_elf_toc (bfd *obfd)
721956f4 11433{
805fc799
AM
11434 asection *s;
11435 bfd_vma TOCstart;
721956f4 11436
805fc799
AM
11437 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11438 order. The TOC starts where the first of these sections starts. */
11439 s = bfd_get_section_by_name (obfd, ".got");
e054468f 11440 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11441 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 11442 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11443 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 11444 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11445 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 11446 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
11447 {
11448 /* This may happen for
11449 o references to TOC base (SYM@toc / TOC[tc0]) without a
11450 .toc directive
11451 o bad linker script
11452 o --gc-sections and empty TOC sections
11453
11454 FIXME: Warn user? */
11455
11456 /* Look for a likely section. We probably won't even be
11457 using TOCstart. */
11458 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11459 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11460 | SEC_EXCLUDE))
805fc799
AM
11461 == (SEC_ALLOC | SEC_SMALL_DATA))
11462 break;
721956f4 11463 if (s == NULL)
805fc799 11464 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11465 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
11466 == (SEC_ALLOC | SEC_SMALL_DATA))
11467 break;
721956f4 11468 if (s == NULL)
805fc799 11469 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11470 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11471 == SEC_ALLOC)
805fc799 11472 break;
721956f4 11473 if (s == NULL)
805fc799 11474 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11475 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
11476 break;
11477 }
721956f4 11478
805fc799
AM
11479 TOCstart = 0;
11480 if (s != NULL)
11481 TOCstart = s->output_section->vma + s->output_offset;
721956f4 11482
805fc799 11483 return TOCstart;
721956f4
AM
11484}
11485
11486/* Build all the stubs associated with the current output file.
11487 The stubs are kept in a hash table attached to the main linker
11488 hash table. This function is called via gldelf64ppc_finish. */
11489
b34976b6 11490bfd_boolean
4ce794b7
AM
11491ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11492 struct bfd_link_info *info,
11493 char **stats)
5d1634d7
AM
11494{
11495 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 11496 asection *stub_sec;
5d1634d7 11497 bfd_byte *p;
e717da7e 11498 int stub_sec_count = 0;
5d1634d7 11499
4dfe6ac6
NC
11500 if (htab == NULL)
11501 return FALSE;
11502
ad8e1ba5 11503 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
11504
11505 /* Allocate memory to hold the linker stubs. */
721956f4
AM
11506 for (stub_sec = htab->stub_bfd->sections;
11507 stub_sec != NULL;
11508 stub_sec = stub_sec->next)
eea6121a
AM
11509 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11510 && stub_sec->size != 0)
e717da7e 11511 {
eea6121a
AM
11512 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11513 if (stub_sec->contents == NULL)
11514 return FALSE;
11515 /* We want to check that built size is the same as calculated
11516 size. rawsize is a convenient location to use. */
11517 stub_sec->rawsize = stub_sec->size;
11518 stub_sec->size = 0;
e717da7e 11519 }
5d1634d7 11520
23eb7e01 11521 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 11522 {
9f951329 11523 unsigned int indx;
ad8e1ba5 11524 bfd_vma plt0;
9f951329 11525
721956f4 11526 /* Build the .glink plt call stub. */
97b639ba
AM
11527 if (htab->emit_stub_syms)
11528 {
11529 struct elf_link_hash_entry *h;
468392fb
AM
11530 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11531 TRUE, FALSE, FALSE);
97b639ba
AM
11532 if (h == NULL)
11533 return FALSE;
11534 if (h->root.type == bfd_link_hash_new)
11535 {
11536 h->root.type = bfd_link_hash_defined;
11537 h->root.u.def.section = htab->glink;
ee4bf8d2 11538 h->root.u.def.value = 8;
f5385ebf
AM
11539 h->ref_regular = 1;
11540 h->def_regular = 1;
11541 h->ref_regular_nonweak = 1;
11542 h->forced_local = 1;
11543 h->non_elf = 0;
97b639ba
AM
11544 }
11545 }
176a0d42
AM
11546 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11547 if (info->emitrelocations)
11548 {
11549 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11550 if (r == NULL)
11551 return FALSE;
11552 r->r_offset = (htab->glink->output_offset
11553 + htab->glink->output_section->vma);
11554 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11555 r->r_addend = plt0;
11556 }
4ce794b7 11557 p = htab->glink->contents;
176a0d42 11558 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
11559 bfd_put_64 (htab->glink->owner, plt0, p);
11560 p += 8;
11561 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 11562 p += 4;
ee4bf8d2 11563 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 11564 p += 4;
ee4bf8d2 11565 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 11566 p += 4;
ee4bf8d2 11567 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 11568 p += 4;
ee4bf8d2 11569 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 11570 p += 4;
ee4bf8d2 11571 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 11572 p += 4;
ee4bf8d2 11573 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 11574 p += 4;
4ce794b7 11575 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 11576 p += 4;
4ce794b7 11577 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 11578 p += 4;
4ce794b7 11579 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 11580 p += 4;
4ce794b7 11581 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 11582 p += 4;
ee4bf8d2
AM
11583 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11584 {
11585 bfd_put_32 (htab->glink->owner, NOP, p);
11586 p += 4;
11587 }
ad8e1ba5 11588
9f951329
AM
11589 /* Build the .glink lazy link call stubs. */
11590 indx = 0;
eea6121a 11591 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
11592 {
11593 if (indx < 0x8000)
11594 {
4ce794b7 11595 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
11596 p += 4;
11597 }
11598 else
11599 {
4ce794b7 11600 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 11601 p += 4;
4ce794b7 11602 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
11603 p += 4;
11604 }
4ce794b7 11605 bfd_put_32 (htab->glink->owner,
ee4bf8d2 11606 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 11607 indx++;
9f951329
AM
11608 p += 4;
11609 }
eea6121a 11610 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 11611 }
5d1634d7 11612
eea6121a 11613 if (htab->brlt->size != 0)
721956f4 11614 {
4ce794b7 11615 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 11616 htab->brlt->size);
4ce794b7 11617 if (htab->brlt->contents == NULL)
b34976b6 11618 return FALSE;
721956f4 11619 }
ee75fd95 11620 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
11621 {
11622 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 11623 htab->relbrlt->size);
63bc6f6c
AM
11624 if (htab->relbrlt->contents == NULL)
11625 return FALSE;
11626 }
5d1634d7 11627
58d180e8
AM
11628 if (htab->glink_eh_frame != NULL
11629 && htab->glink_eh_frame->size != 0)
11630 {
11631 bfd_vma val;
11632
11633 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
11634 if (p == NULL)
11635 return FALSE;
11636 htab->glink_eh_frame->contents = p;
11637
11638 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
11639
11640 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
11641 /* CIE length (rewrite in case little-endian). */
11642 bfd_put_32 (htab->elf.dynobj, sizeof (glink_eh_frame_cie) - 4, p);
11643 p += sizeof (glink_eh_frame_cie);
11644
11645 for (stub_sec = htab->stub_bfd->sections;
11646 stub_sec != NULL;
11647 stub_sec = stub_sec->next)
11648 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11649 {
11650 /* FDE length. */
11651 bfd_put_32 (htab->elf.dynobj, 16, p);
11652 p += 4;
11653 /* CIE pointer. */
11654 val = p - htab->glink_eh_frame->contents;
11655 bfd_put_32 (htab->elf.dynobj, val, p);
11656 p += 4;
11657 /* Offset to stub section. */
11658 val = (stub_sec->output_section->vma
11659 + stub_sec->output_offset);
11660 val -= (htab->glink_eh_frame->output_section->vma
11661 + htab->glink_eh_frame->output_offset);
11662 val -= p - htab->glink_eh_frame->contents;
11663 if (val + 0x80000000 > 0xffffffff)
11664 {
11665 info->callbacks->einfo
8de848d8 11666 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
11667 stub_sec->name);
11668 return FALSE;
11669 }
11670 bfd_put_32 (htab->elf.dynobj, val, p);
11671 p += 4;
11672 /* stub section size. */
11673 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
11674 p += 4;
11675 /* Augmentation. */
11676 p += 1;
11677 /* Pad. */
11678 p += 3;
11679 }
11680 if (htab->glink != NULL && htab->glink->size != 0)
11681 {
11682 /* FDE length. */
11683 bfd_put_32 (htab->elf.dynobj, 20, p);
11684 p += 4;
11685 /* CIE pointer. */
11686 val = p - htab->glink_eh_frame->contents;
11687 bfd_put_32 (htab->elf.dynobj, val, p);
11688 p += 4;
11689 /* Offset to .glink. */
11690 val = (htab->glink->output_section->vma
11691 + htab->glink->output_offset
11692 + 8);
11693 val -= (htab->glink_eh_frame->output_section->vma
11694 + htab->glink_eh_frame->output_offset);
11695 val -= p - htab->glink_eh_frame->contents;
11696 if (val + 0x80000000 > 0xffffffff)
11697 {
11698 info->callbacks->einfo
8de848d8 11699 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
58d180e8
AM
11700 htab->glink->name);
11701 return FALSE;
11702 }
11703 bfd_put_32 (htab->elf.dynobj, val, p);
11704 p += 4;
11705 /* .glink size. */
11706 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
11707 p += 4;
11708 /* Augmentation. */
11709 p += 1;
11710
11711 *p++ = DW_CFA_advance_loc + 1;
11712 *p++ = DW_CFA_register;
11713 *p++ = 65;
11714 *p++ = 12;
11715 *p++ = DW_CFA_advance_loc + 4;
11716 *p++ = DW_CFA_restore_extended;
11717 *p++ = 65;
11718 }
11719 htab->glink_eh_frame->size = p - htab->glink_eh_frame->contents;
11720 }
11721
721956f4
AM
11722 /* Build the stubs as directed by the stub hash table. */
11723 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 11724
aa8a7074
AM
11725 if (htab->relbrlt != NULL)
11726 htab->relbrlt->reloc_count = 0;
11727
721956f4
AM
11728 for (stub_sec = htab->stub_bfd->sections;
11729 stub_sec != NULL;
11730 stub_sec = stub_sec->next)
e717da7e
AM
11731 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11732 {
11733 stub_sec_count += 1;
eea6121a 11734 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
11735 break;
11736 }
5d1634d7 11737
721956f4 11738 if (stub_sec != NULL
58d180e8
AM
11739 || htab->glink->rawsize != htab->glink->size
11740 || (htab->glink_eh_frame != NULL
11741 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
5d1634d7 11742 {
b34976b6 11743 htab->stub_error = TRUE;
8de848d8 11744 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
5d1634d7 11745 }
721956f4 11746
d2a300cf
AM
11747 if (htab->stub_error)
11748 return FALSE;
11749
11750 if (stats != NULL)
11751 {
11752 *stats = bfd_malloc (500);
11753 if (*stats == NULL)
11754 return FALSE;
11755
ee75fd95 11756 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
11757 " branch %lu\n"
11758 " toc adjust %lu\n"
11759 " long branch %lu\n"
11760 " long toc adj %lu\n"
11761 " plt call %lu"),
e717da7e 11762 stub_sec_count,
ee75fd95 11763 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
11764 htab->stub_count[ppc_stub_long_branch - 1],
11765 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11766 htab->stub_count[ppc_stub_plt_branch - 1],
11767 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11768 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
11769 }
11770 return TRUE;
5bd4f169
AM
11771}
11772
99877b66
AM
11773/* This function undoes the changes made by add_symbol_adjust. */
11774
11775static bfd_boolean
11776undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11777{
11778 struct ppc_link_hash_entry *eh;
11779
11780 if (h->root.type == bfd_link_hash_indirect)
11781 return TRUE;
11782
99877b66
AM
11783 eh = (struct ppc_link_hash_entry *) h;
11784 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11785 return TRUE;
11786
11787 eh->elf.root.type = bfd_link_hash_undefined;
11788 return TRUE;
11789}
11790
11791void
11792ppc64_elf_restore_symbols (struct bfd_link_info *info)
11793{
11794 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
11795
11796 if (htab != NULL)
11797 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
11798}
11799
60124e18
AM
11800/* What to do when ld finds relocations against symbols defined in
11801 discarded sections. */
11802
11803static unsigned int
11804ppc64_elf_action_discarded (asection *sec)
11805{
11806 if (strcmp (".opd", sec->name) == 0)
11807 return 0;
11808
11809 if (strcmp (".toc", sec->name) == 0)
11810 return 0;
11811
bce50a28
JJ
11812 if (strcmp (".toc1", sec->name) == 0)
11813 return 0;
11814
60124e18
AM
11815 return _bfd_elf_default_action_discarded (sec);
11816}
11817
5bd4f169
AM
11818/* The RELOCATE_SECTION function is called by the ELF backend linker
11819 to handle the relocations for a section.
11820
11821 The relocs are always passed as Rela structures; if the section
11822 actually uses Rel structures, the r_addend field will always be
11823 zero.
11824
11825 This function is responsible for adjust the section contents as
11826 necessary, and (if using Rela relocs and generating a
1049f94e 11827 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
11828 necessary.
11829
11830 This function does not have to worry about setting the reloc
11831 address or the reloc symbol index.
11832
11833 LOCAL_SYMS is a pointer to the swapped in local symbols.
11834
11835 LOCAL_SECTIONS is an array giving the section in the input file
11836 corresponding to the st_shndx field of each local symbol.
11837
11838 The global hash table entry for the global symbols can be found
11839 via elf_sym_hashes (input_bfd).
11840
1049f94e 11841 When generating relocatable output, this function must handle
5bd4f169
AM
11842 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11843 going to be the section symbol corresponding to the output
11844 section, which means that the addend must be adjusted
11845 accordingly. */
11846
b34976b6 11847static bfd_boolean
4ce794b7
AM
11848ppc64_elf_relocate_section (bfd *output_bfd,
11849 struct bfd_link_info *info,
11850 bfd *input_bfd,
11851 asection *input_section,
11852 bfd_byte *contents,
11853 Elf_Internal_Rela *relocs,
11854 Elf_Internal_Sym *local_syms,
11855 asection **local_sections)
5bd4f169 11856{
65f38f15 11857 struct ppc_link_hash_table *htab;
5bd4f169
AM
11858 Elf_Internal_Shdr *symtab_hdr;
11859 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
11860 Elf_Internal_Rela *rel;
11861 Elf_Internal_Rela *relend;
411e1bfb
AM
11862 Elf_Internal_Rela outrel;
11863 bfd_byte *loc;
411e1bfb 11864 struct got_entry **local_got_ents;
5bd4f169 11865 bfd_vma TOCstart;
b34976b6
AM
11866 bfd_boolean ret = TRUE;
11867 bfd_boolean is_opd;
96e0dda4 11868 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 11869 bfd_boolean is_power4 = FALSE;
4fe5ca5b 11870 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 11871
65f38f15 11872 /* Initialize howto table if needed. */
5bd4f169 11873 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
11874 ppc_howto_init ();
11875
65f38f15 11876 htab = ppc_hash_table (info);
4dfe6ac6
NC
11877 if (htab == NULL)
11878 return FALSE;
ee75fd95
AM
11879
11880 /* Don't relocate stub sections. */
11881 if (input_section->owner == htab->stub_bfd)
11882 return TRUE;
11883
0c8d6e5c 11884 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 11885
411e1bfb 11886 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 11887 TOCstart = elf_gp (output_bfd);
0ffa91dd 11888 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 11889 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 11890 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 11891
5bd4f169
AM
11892 rel = relocs;
11893 relend = relocs + input_section->reloc_count;
11894 for (; rel < relend; rel++)
11895 {
04c9666a 11896 enum elf_ppc64_reloc_type r_type;
4cc603a5 11897 bfd_vma addend, orig_addend;
5bd4f169
AM
11898 bfd_reloc_status_type r;
11899 Elf_Internal_Sym *sym;
11900 asection *sec;
039b3fef
AM
11901 struct elf_link_hash_entry *h_elf;
11902 struct ppc_link_hash_entry *h;
11903 struct ppc_link_hash_entry *fdh;
5bd4f169 11904 const char *sym_name;
0d4792f7 11905 unsigned long r_symndx, toc_symndx;
3a71aa26 11906 bfd_vma toc_addend;
f961d9dd
AM
11907 unsigned char tls_mask, tls_gd, tls_type;
11908 unsigned char sym_type;
5bd4f169 11909 bfd_vma relocation;
b34976b6
AM
11910 bfd_boolean unresolved_reloc;
11911 bfd_boolean warned;
67f0cbdb 11912 unsigned int insn;
e11840f9 11913 unsigned int mask;
721956f4
AM
11914 struct ppc_stub_hash_entry *stub_entry;
11915 bfd_vma max_br_offset;
11916 bfd_vma from;
5bd4f169 11917
4ce794b7 11918 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 11919 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
11920
11921 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11922 symbol of the previous ADDR64 reloc. The symbol gives us the
11923 proper TOC base to use. */
11924 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11925 && rel != relocs
11926 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11927 && is_opd)
11928 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11929
4ce794b7
AM
11930 sym = NULL;
11931 sec = NULL;
039b3fef 11932 h_elf = NULL;
4ce794b7 11933 sym_name = NULL;
b34976b6
AM
11934 unresolved_reloc = FALSE;
11935 warned = FALSE;
4cc603a5 11936 orig_addend = rel->r_addend;
65f38f15 11937
0b13192e 11938 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
11939 {
11940 /* It's a local symbol. */
74f0fb50 11941 struct _opd_sec_data *opd;
4025353c 11942
5bd4f169
AM
11943 sym = local_syms + r_symndx;
11944 sec = local_sections[r_symndx];
26c61ae5 11945 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 11946 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 11947 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
11948 opd = get_opd_info (sec);
11949 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 11950 {
74f0fb50 11951 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
11952 if (adjust == -1)
11953 relocation = 0;
11954 else
4cc603a5
AM
11955 {
11956 /* If this is a relocation against the opd section sym
11957 and we have edited .opd, adjust the reloc addend so
11958 that ld -r and ld --emit-relocs output is correct.
11959 If it is a reloc against some other .opd symbol,
11960 then the symbol value will be adjusted later. */
11961 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11962 rel->r_addend += adjust;
11963 else
11964 relocation += adjust;
11965 }
1e2f5b6e 11966 }
5bd4f169
AM
11967 }
11968 else
11969 {
b2a8e766
AM
11970 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11971 r_symndx, symtab_hdr, sym_hashes,
039b3fef 11972 h_elf, sec, relocation,
b2a8e766 11973 unresolved_reloc, warned);
039b3fef
AM
11974 sym_name = h_elf->root.root.string;
11975 sym_type = h_elf->type;
5bd4f169 11976 }
039b3fef 11977 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 11978
ab96bf03 11979 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
11980 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11981 rel, relend,
11982 ppc64_elf_howto_table[r_type],
11983 contents);
ab96bf03
AM
11984
11985 if (info->relocatable)
11986 continue;
11987
951fd09b
AM
11988 /* TLS optimizations. Replace instruction sequences and relocs
11989 based on information we collected in tls_optimize. We edit
11990 RELOCS so that --emit-relocs will output something sensible
11991 for the final instruction stream. */
11992 tls_mask = 0;
11993 tls_gd = 0;
0d4792f7 11994 toc_symndx = 0;
727fc41e
AM
11995 if (h != NULL)
11996 tls_mask = h->tls_mask;
11997 else if (local_got_ents != NULL)
411e1bfb 11998 {
e054468f
AM
11999 struct plt_entry **local_plt = (struct plt_entry **)
12000 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 12001 unsigned char *lgot_masks = (unsigned char *)
e054468f 12002 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
12003 tls_mask = lgot_masks[r_symndx];
12004 }
12005 if (tls_mask == 0
12006 && (r_type == R_PPC64_TLS
12007 || r_type == R_PPC64_TLSGD
12008 || r_type == R_PPC64_TLSLD))
12009 {
12010 /* Check for toc tls entries. */
f961d9dd 12011 unsigned char *toc_tls;
0d4792f7 12012
727fc41e
AM
12013 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12014 &local_syms, rel, input_bfd))
12015 return FALSE;
0d4792f7 12016
727fc41e
AM
12017 if (toc_tls)
12018 tls_mask = *toc_tls;
0d4792f7
AM
12019 }
12020
12021 /* Check that tls relocs are used with tls syms, and non-tls
12022 relocs are used with non-tls syms. */
cf35638d 12023 if (r_symndx != STN_UNDEF
0d4792f7
AM
12024 && r_type != R_PPC64_NONE
12025 && (h == NULL
039b3fef
AM
12026 || h->elf.root.type == bfd_link_hash_defined
12027 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
12028 && (IS_PPC64_TLS_RELOC (r_type)
12029 != (sym_type == STT_TLS
12030 || (sym_type == STT_SECTION
12031 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 12032 {
727fc41e
AM
12033 if (tls_mask != 0
12034 && (r_type == R_PPC64_TLS
12035 || r_type == R_PPC64_TLSGD
12036 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
12037 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12038 ;
12039 else
25f53a85 12040 info->callbacks->einfo
1d483afe 12041 (!IS_PPC64_TLS_RELOC (r_type)
8de848d8
AM
12042 ? _("%P: %H: %s used with TLS symbol %s\n")
12043 : _("%P: %H: %s used with non-TLS symbol %s\n"),
25f53a85 12044 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
12045 ppc64_elf_howto_table[r_type]->name,
12046 sym_name);
411e1bfb
AM
12047 }
12048
12049 /* Ensure reloc mapping code below stays sane. */
12050 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12051 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12052 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12053 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12054 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12055 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12056 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12057 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12058 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12059 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12060 abort ();
0d4792f7 12061
411e1bfb
AM
12062 switch (r_type)
12063 {
12064 default:
411e1bfb
AM
12065 break;
12066
ba761f19
AM
12067 case R_PPC64_LO_DS_OPT:
12068 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12069 if ((insn & (0x3f << 26)) != 58u << 26)
12070 abort ();
12071 insn += (14u << 26) - (58u << 26);
12072 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12073 r_type = R_PPC64_TOC16_LO;
12074 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12075 break;
12076
411e1bfb
AM
12077 case R_PPC64_TOC16:
12078 case R_PPC64_TOC16_LO:
12079 case R_PPC64_TOC16_DS:
12080 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
12081 {
12082 /* Check for toc tls entries. */
f961d9dd 12083 unsigned char *toc_tls;
951fd09b 12084 int retval;
411e1bfb 12085
3a71aa26
AM
12086 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12087 &local_syms, rel, input_bfd);
951fd09b 12088 if (retval == 0)
411e1bfb
AM
12089 return FALSE;
12090
12091 if (toc_tls)
12092 {
951fd09b 12093 tls_mask = *toc_tls;
411e1bfb
AM
12094 if (r_type == R_PPC64_TOC16_DS
12095 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
12096 {
12097 if (tls_mask != 0
12098 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12099 goto toctprel;
12100 }
411e1bfb 12101 else
951fd09b
AM
12102 {
12103 /* If we found a GD reloc pair, then we might be
12104 doing a GD->IE transition. */
12105 if (retval == 2)
12106 {
12107 tls_gd = TLS_TPRELGD;
12108 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12109 goto tls_ldgd_opt;
951fd09b
AM
12110 }
12111 else if (retval == 3)
12112 {
12113 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 12114 goto tls_ldgd_opt;
951fd09b
AM
12115 }
12116 }
411e1bfb
AM
12117 }
12118 }
12119 break;
12120
9d6ded02
AM
12121 case R_PPC64_GOT_TPREL16_HI:
12122 case R_PPC64_GOT_TPREL16_HA:
12123 if (tls_mask != 0
12124 && (tls_mask & TLS_TPREL) == 0)
12125 {
12126 rel->r_offset -= d_offset;
12127 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12128 r_type = R_PPC64_NONE;
12129 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12130 }
12131 break;
12132
411e1bfb
AM
12133 case R_PPC64_GOT_TPREL16_DS:
12134 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
12135 if (tls_mask != 0
12136 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12137 {
81407a69 12138 toctprel:
4fe5ca5b 12139 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
12140 insn &= 31 << 21;
12141 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 12142 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 12143 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
12144 if (toc_symndx != 0)
12145 {
12146 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12147 rel->r_addend = toc_addend;
0d4792f7
AM
12148 /* We changed the symbol. Start over in order to
12149 get h, sym, sec etc. right. */
12150 rel--;
12151 continue;
12152 }
12153 else
12154 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12155 }
12156 break;
12157
12158 case R_PPC64_TLS:
951fd09b
AM
12159 if (tls_mask != 0
12160 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 12161 {
411e1bfb 12162 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
12163 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12164 if (insn == 0)
411e1bfb 12165 abort ();
411e1bfb 12166 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 12167 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
12168 PPC64_TPREL16_LO which is at low-order half-word. */
12169 rel->r_offset += d_offset;
0d4792f7
AM
12170 r_type = R_PPC64_TPREL16_LO;
12171 if (toc_symndx != 0)
12172 {
12173 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 12174 rel->r_addend = toc_addend;
0d4792f7
AM
12175 /* We changed the symbol. Start over in order to
12176 get h, sym, sec etc. right. */
12177 rel--;
12178 continue;
12179 }
12180 else
12181 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12182 }
12183 break;
12184
411e1bfb
AM
12185 case R_PPC64_GOT_TLSGD16_HI:
12186 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
12187 tls_gd = TLS_TPRELGD;
12188 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12189 goto tls_gdld_hi;
12190 break;
12191
411e1bfb
AM
12192 case R_PPC64_GOT_TLSLD16_HI:
12193 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12194 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 12195 {
951fd09b
AM
12196 tls_gdld_hi:
12197 if ((tls_mask & tls_gd) != 0)
12198 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12199 + R_PPC64_GOT_TPREL16_DS);
12200 else
411e1bfb 12201 {
4fe5ca5b 12202 rel->r_offset -= d_offset;
727ac201 12203 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 12204 r_type = R_PPC64_NONE;
411e1bfb 12205 }
951fd09b 12206 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
12207 }
12208 break;
12209
951fd09b
AM
12210 case R_PPC64_GOT_TLSGD16:
12211 case R_PPC64_GOT_TLSGD16_LO:
12212 tls_gd = TLS_TPRELGD;
12213 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12214 goto tls_ldgd_opt;
951fd09b 12215 break;
411e1bfb 12216
951fd09b
AM
12217 case R_PPC64_GOT_TLSLD16:
12218 case R_PPC64_GOT_TLSLD16_LO:
12219 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12220 {
3a71aa26 12221 unsigned int insn1, insn2, insn3;
102890f0
AM
12222 bfd_vma offset;
12223
12224 tls_ldgd_opt:
727fc41e
AM
12225 offset = (bfd_vma) -1;
12226 /* If not using the newer R_PPC64_TLSGD/LD to mark
12227 __tls_get_addr calls, we must trust that the call
12228 stays with its arg setup insns, ie. that the next
12229 reloc is the __tls_get_addr call associated with
12230 the current reloc. Edit both insns. */
12231 if (input_section->has_tls_get_addr_call
12232 && rel + 1 < relend
12233 && branch_reloc_hash_match (input_bfd, rel + 1,
12234 htab->tls_get_addr,
12235 htab->tls_get_addr_fd))
12236 offset = rel[1].r_offset;
102890f0 12237 if ((tls_mask & tls_gd) != 0)
411e1bfb 12238 {
102890f0 12239 /* IE */
3a71aa26
AM
12240 insn1 = bfd_get_32 (output_bfd,
12241 contents + rel->r_offset - d_offset);
102890f0
AM
12242 insn1 &= (1 << 26) - (1 << 2);
12243 insn1 |= 58 << 26; /* ld */
12244 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 12245 if (offset != (bfd_vma) -1)
f58d5a2d 12246 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
12247 if ((tls_mask & TLS_EXPLICIT) == 0)
12248 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12249 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 12250 else
102890f0
AM
12251 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12252 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12253 }
12254 else
12255 {
12256 /* LE */
12257 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12258 insn2 = 0x38630000; /* addi 3,3,0 */
12259 if (tls_gd == 0)
951fd09b 12260 {
102890f0 12261 /* Was an LD reloc. */
1d483afe
AM
12262 if (toc_symndx)
12263 sec = local_sections[toc_symndx];
12264 for (r_symndx = 0;
12265 r_symndx < symtab_hdr->sh_info;
12266 r_symndx++)
12267 if (local_sections[r_symndx] == sec)
12268 break;
12269 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12270 r_symndx = STN_UNDEF;
102890f0 12271 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12272 if (r_symndx != STN_UNDEF)
1d483afe
AM
12273 rel->r_addend -= (local_syms[r_symndx].st_value
12274 + sec->output_offset
12275 + sec->output_section->vma);
951fd09b 12276 }
102890f0 12277 else if (toc_symndx != 0)
3a71aa26
AM
12278 {
12279 r_symndx = toc_symndx;
12280 rel->r_addend = toc_addend;
12281 }
102890f0
AM
12282 r_type = R_PPC64_TPREL16_HA;
12283 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
12284 if (offset != (bfd_vma) -1)
12285 {
12286 rel[1].r_info = ELF64_R_INFO (r_symndx,
12287 R_PPC64_TPREL16_LO);
12288 rel[1].r_offset = offset + d_offset;
12289 rel[1].r_addend = rel->r_addend;
12290 }
102890f0 12291 }
3a71aa26
AM
12292 bfd_put_32 (output_bfd, insn1,
12293 contents + rel->r_offset - d_offset);
727fc41e
AM
12294 if (offset != (bfd_vma) -1)
12295 {
12296 insn3 = bfd_get_32 (output_bfd,
12297 contents + offset + 4);
12298 if (insn3 == NOP
12299 || insn3 == CROR_151515 || insn3 == CROR_313131)
12300 {
12301 rel[1].r_offset += 4;
12302 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12303 insn2 = NOP;
12304 }
12305 bfd_put_32 (output_bfd, insn2, contents + offset);
12306 }
12307 if ((tls_mask & tls_gd) == 0
12308 && (tls_gd == 0 || toc_symndx != 0))
12309 {
12310 /* We changed the symbol. Start over in order
12311 to get h, sym, sec etc. right. */
12312 rel--;
12313 continue;
12314 }
12315 }
12316 break;
12317
12318 case R_PPC64_TLSGD:
12319 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12320 {
12321 unsigned int insn2, insn3;
12322 bfd_vma offset = rel->r_offset;
12323
12324 if ((tls_mask & TLS_TPRELGD) != 0)
12325 {
12326 /* IE */
12327 r_type = R_PPC64_NONE;
12328 insn2 = 0x7c636a14; /* add 3,3,13 */
12329 }
12330 else
12331 {
12332 /* LE */
12333 if (toc_symndx != 0)
12334 {
12335 r_symndx = toc_symndx;
12336 rel->r_addend = toc_addend;
12337 }
12338 r_type = R_PPC64_TPREL16_LO;
12339 rel->r_offset = offset + d_offset;
12340 insn2 = 0x38630000; /* addi 3,3,0 */
12341 }
12342 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12343 /* Zap the reloc on the _tls_get_addr call too. */
12344 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12345 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
12346 insn3 = bfd_get_32 (output_bfd,
12347 contents + offset + 4);
102890f0
AM
12348 if (insn3 == NOP
12349 || insn3 == CROR_151515 || insn3 == CROR_313131)
12350 {
727fc41e 12351 rel->r_offset += 4;
3a71aa26
AM
12352 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12353 insn2 = NOP;
102890f0 12354 }
102890f0 12355 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 12356 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 12357 {
102890f0
AM
12358 rel--;
12359 continue;
411e1bfb 12360 }
411e1bfb 12361 }
411e1bfb
AM
12362 break;
12363
727fc41e
AM
12364 case R_PPC64_TLSLD:
12365 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12366 {
12367 unsigned int insn2, insn3;
12368 bfd_vma offset = rel->r_offset;
12369
12370 if (toc_symndx)
12371 sec = local_sections[toc_symndx];
12372 for (r_symndx = 0;
12373 r_symndx < symtab_hdr->sh_info;
12374 r_symndx++)
12375 if (local_sections[r_symndx] == sec)
12376 break;
12377 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12378 r_symndx = STN_UNDEF;
727fc41e 12379 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12380 if (r_symndx != STN_UNDEF)
727fc41e
AM
12381 rel->r_addend -= (local_syms[r_symndx].st_value
12382 + sec->output_offset
12383 + sec->output_section->vma);
12384
12385 r_type = R_PPC64_TPREL16_LO;
12386 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12387 rel->r_offset = offset + d_offset;
12388 /* Zap the reloc on the _tls_get_addr call too. */
12389 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12390 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
12391 insn2 = 0x38630000; /* addi 3,3,0 */
12392 insn3 = bfd_get_32 (output_bfd,
12393 contents + offset + 4);
12394 if (insn3 == NOP
12395 || insn3 == CROR_151515 || insn3 == CROR_313131)
12396 {
12397 rel->r_offset += 4;
12398 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12399 insn2 = NOP;
12400 }
12401 bfd_put_32 (output_bfd, insn2, contents + offset);
12402 rel--;
12403 continue;
12404 }
12405 break;
12406
411e1bfb 12407 case R_PPC64_DTPMOD64:
951fd09b
AM
12408 if (rel + 1 < relend
12409 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12410 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 12411 {
951fd09b
AM
12412 if ((tls_mask & TLS_GD) == 0)
12413 {
12414 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12415 if ((tls_mask & TLS_TPRELGD) != 0)
12416 r_type = R_PPC64_TPREL64;
12417 else
12418 {
4ce794b7 12419 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
12420 r_type = R_PPC64_NONE;
12421 }
12422 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12423 }
12424 }
12425 else
12426 {
12427 if ((tls_mask & TLS_LD) == 0)
411e1bfb 12428 {
4ce794b7 12429 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 12430 r_type = R_PPC64_NONE;
951fd09b 12431 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 12432 }
411e1bfb
AM
12433 }
12434 break;
12435
12436 case R_PPC64_TPREL64:
951fd09b 12437 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
12438 {
12439 r_type = R_PPC64_NONE;
12440 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12441 }
12442 break;
12443 }
12444
12445 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 12446 insn = 0;
b25116a9
AM
12447 max_br_offset = 1 << 25;
12448 addend = rel->r_addend;
65f38f15 12449 switch (r_type)
5bd4f169
AM
12450 {
12451 default:
65f38f15 12452 break;
5bd4f169 12453
3b421ab3
AM
12454 case R_PPC64_TOCSAVE:
12455 if (relocation + addend == (rel->r_offset
12456 + input_section->output_offset
12457 + input_section->output_section->vma)
12458 && tocsave_find (htab, NO_INSERT,
12459 &local_syms, rel, input_bfd))
12460 {
12461 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
12462 if (insn == NOP
12463 || insn == CROR_151515 || insn == CROR_313131)
12464 bfd_put_32 (input_bfd, STD_R2_40R1,
12465 contents + rel->r_offset);
12466 }
12467 break;
12468
65f38f15
AM
12469 /* Branch taken prediction relocations. */
12470 case R_PPC64_ADDR14_BRTAKEN:
12471 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
12472 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12473 /* Fall thru. */
65f38f15 12474
86c76c7b 12475 /* Branch not taken prediction relocations. */
65f38f15
AM
12476 case R_PPC64_ADDR14_BRNTAKEN:
12477 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
12478 insn |= bfd_get_32 (output_bfd,
12479 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 12480 /* Fall thru. */
86c76c7b 12481
b25116a9
AM
12482 case R_PPC64_REL14:
12483 max_br_offset = 1 << 15;
12484 /* Fall thru. */
5bd4f169 12485
65f38f15 12486 case R_PPC64_REL24:
ad8e1ba5
AM
12487 /* Calls to functions with a different TOC, such as calls to
12488 shared objects, need to alter the TOC pointer. This is
12489 done using a linkage stub. A REL24 branching to these
12490 linkage stubs needs to be followed by a nop, as the nop
12491 will be replaced with an instruction to restore the TOC
12492 base pointer. */
8387904d 12493 fdh = h;
b31867b6
AM
12494 if (h != NULL
12495 && h->oh != NULL
12496 && h->oh->is_func_descriptor)
12497 fdh = ppc_follow_link (h->oh);
6abec6bc
AM
12498 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12499 if (stub_entry != NULL
ad8e1ba5
AM
12500 && (stub_entry->stub_type == ppc_stub_plt_call
12501 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12502 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 12503 {
b25116a9 12504 bfd_boolean can_plt_call = FALSE;
721956f4 12505
eea6121a 12506 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 12507 {
b25116a9
AM
12508 unsigned long nop;
12509 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12510 if (nop == NOP
12511 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 12512 {
a7f2871e
AM
12513 if (h != NULL
12514 && (h == htab->tls_get_addr_fd
12515 || h == htab->tls_get_addr)
12516 && !htab->no_tls_get_addr_opt)
12517 {
12518 /* Special stub used, leave nop alone. */
12519 }
12520 else
12521 bfd_put_32 (input_bfd, LD_R2_40R1,
12522 contents + rel->r_offset + 4);
b25116a9 12523 can_plt_call = TRUE;
41bd81ab 12524 }
5bd4f169 12525 }
721956f4
AM
12526
12527 if (!can_plt_call)
12528 {
ad8e1ba5
AM
12529 if (stub_entry->stub_type == ppc_stub_plt_call)
12530 {
12531 /* If this is a plain branch rather than a branch
4fa1c237
AM
12532 and link, don't require a nop. However, don't
12533 allow tail calls in a shared library as they
12534 will result in r2 being corrupted. */
b25116a9
AM
12535 unsigned long br;
12536 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 12537 if (info->executable && (br & 1) == 0)
b25116a9 12538 can_plt_call = TRUE;
15b552e9
AM
12539 else
12540 stub_entry = NULL;
ad8e1ba5 12541 }
6ab189d5 12542 else if (h != NULL
039b3fef 12543 && strcmp (h->elf.root.root.string,
6ab189d5
AM
12544 ".__libc_start_main") == 0)
12545 {
12546 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 12547 can_plt_call = TRUE;
6ab189d5 12548 }
ad8e1ba5
AM
12549 else
12550 {
12551 if (strcmp (input_section->output_section->name,
12552 ".init") == 0
12553 || strcmp (input_section->output_section->name,
12554 ".fini") == 0)
25f53a85 12555 info->callbacks->einfo
8de848d8 12556 (_("%P: %H: automatic multiple TOCs "
ad8e1ba5 12557 "not supported using your crt files; "
25f53a85
AM
12558 "recompile with -mminimal-toc or upgrade gcc\n"),
12559 input_bfd, input_section, rel->r_offset);
ad8e1ba5 12560 else
25f53a85 12561 info->callbacks->einfo
8de848d8 12562 (_("%P: %H: sibling call optimization to `%s' "
ad8e1ba5
AM
12563 "does not allow automatic multiple TOCs; "
12564 "recompile with -mminimal-toc or "
12565 "-fno-optimize-sibling-calls, "
25f53a85
AM
12566 "or make `%s' extern\n"),
12567 input_bfd, input_section, rel->r_offset,
ad8e1ba5
AM
12568 sym_name,
12569 sym_name);
12570 bfd_set_error (bfd_error_bad_value);
12571 ret = FALSE;
12572 }
721956f4
AM
12573 }
12574
b25116a9
AM
12575 if (can_plt_call
12576 && stub_entry->stub_type == ppc_stub_plt_call)
12577 unresolved_reloc = FALSE;
12578 }
12579
6abec6bc
AM
12580 if ((stub_entry == NULL
12581 || stub_entry->stub_type == ppc_stub_long_branch
12582 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
12583 && get_opd_info (sec) != NULL)
12584 {
12585 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
12586 bfd_vma off = (relocation + addend
12587 - sec->output_section->vma
12588 - sec->output_offset);
8387904d
AM
12589 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12590 if (dest != (bfd_vma) -1)
12591 {
12592 relocation = dest;
12593 addend = 0;
12594 }
12595 }
12596
b25116a9
AM
12597 /* If the branch is out of reach we ought to have a long
12598 branch stub. */
12599 from = (rel->r_offset
12600 + input_section->output_offset
12601 + input_section->output_section->vma);
12602
6abec6bc
AM
12603 if (stub_entry != NULL
12604 && (stub_entry->stub_type == ppc_stub_long_branch
12605 || stub_entry->stub_type == ppc_stub_plt_branch)
12606 && (r_type == R_PPC64_ADDR14_BRTAKEN
12607 || r_type == R_PPC64_ADDR14_BRNTAKEN
12608 || (relocation + addend - from + max_br_offset
12609 < 2 * max_br_offset)))
12610 /* Don't use the stub if this branch is in range. */
12611 stub_entry = NULL;
b25116a9
AM
12612
12613 if (stub_entry != NULL)
12614 {
12615 /* Munge up the value and addend so that we call the stub
12616 rather than the procedure directly. */
12617 relocation = (stub_entry->stub_offset
12618 + stub_entry->stub_sec->output_offset
12619 + stub_entry->stub_sec->output_section->vma);
12620 addend = 0;
3b421ab3
AM
12621
12622 if (stub_entry->stub_type == ppc_stub_plt_call
12623 && rel + 1 < relend
12624 && rel[1].r_offset == rel->r_offset + 4
12625 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
12626 relocation += 4;
b25116a9
AM
12627 }
12628
12629 if (insn != 0)
12630 {
12631 if (is_power4)
721956f4 12632 {
b25116a9
AM
12633 /* Set 'a' bit. This is 0b00010 in BO field for branch
12634 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12635 for branch on CTR insns (BO == 1a00t or 1a01t). */
12636 if ((insn & (0x14 << 21)) == (0x04 << 21))
12637 insn |= 0x02 << 21;
12638 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12639 insn |= 0x08 << 21;
12640 else
12641 break;
12642 }
12643 else
12644 {
12645 /* Invert 'y' bit if not the default. */
4cc603a5 12646 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 12647 insn ^= 0x01 << 21;
721956f4 12648 }
b25116a9
AM
12649
12650 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 12651 }
e86ce104 12652
06da1e8e
AM
12653 /* NOP out calls to undefined weak functions.
12654 We can thus call a weak function without first
12655 checking whether the function is defined. */
b25116a9 12656 else if (h != NULL
039b3fef 12657 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 12658 && h->elf.dynindx == -1
b25116a9
AM
12659 && r_type == R_PPC64_REL24
12660 && relocation == 0
4cc603a5 12661 && addend == 0)
e86ce104 12662 {
06da1e8e
AM
12663 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12664 continue;
e86ce104 12665 }
65f38f15
AM
12666 break;
12667 }
5bd4f169 12668
65f38f15 12669 /* Set `addend'. */
411e1bfb 12670 tls_type = 0;
65f38f15
AM
12671 switch (r_type)
12672 {
12673 default:
25f53a85 12674 info->callbacks->einfo
8de848d8 12675 (_("%P: %B: unknown relocation type %d for symbol %s\n"),
d003868e 12676 input_bfd, (int) r_type, sym_name);
5bd4f169 12677
65f38f15 12678 bfd_set_error (bfd_error_bad_value);
b34976b6 12679 ret = FALSE;
65f38f15 12680 continue;
5bd4f169 12681
65f38f15 12682 case R_PPC64_NONE:
411e1bfb 12683 case R_PPC64_TLS:
727fc41e
AM
12684 case R_PPC64_TLSGD:
12685 case R_PPC64_TLSLD:
3b421ab3 12686 case R_PPC64_TOCSAVE:
04c9666a
AM
12687 case R_PPC64_GNU_VTINHERIT:
12688 case R_PPC64_GNU_VTENTRY:
65f38f15 12689 continue;
5bd4f169
AM
12690
12691 /* GOT16 relocations. Like an ADDR16 using the symbol's
12692 address in the GOT as relocation value instead of the
411e1bfb 12693 symbol's value itself. Also, create a GOT entry for the
5bd4f169 12694 symbol and put the symbol value there. */
411e1bfb
AM
12695 case R_PPC64_GOT_TLSGD16:
12696 case R_PPC64_GOT_TLSGD16_LO:
12697 case R_PPC64_GOT_TLSGD16_HI:
12698 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 12699 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
12700 goto dogot;
12701
12702 case R_PPC64_GOT_TLSLD16:
12703 case R_PPC64_GOT_TLSLD16_LO:
12704 case R_PPC64_GOT_TLSLD16_HI:
12705 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12706 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
12707 goto dogot;
12708
12709 case R_PPC64_GOT_TPREL16_DS:
12710 case R_PPC64_GOT_TPREL16_LO_DS:
12711 case R_PPC64_GOT_TPREL16_HI:
12712 case R_PPC64_GOT_TPREL16_HA:
12713 tls_type = TLS_TLS | TLS_TPREL;
12714 goto dogot;
12715
12716 case R_PPC64_GOT_DTPREL16_DS:
12717 case R_PPC64_GOT_DTPREL16_LO_DS:
12718 case R_PPC64_GOT_DTPREL16_HI:
12719 case R_PPC64_GOT_DTPREL16_HA:
12720 tls_type = TLS_TLS | TLS_DTPREL;
12721 goto dogot;
12722
65f38f15
AM
12723 case R_PPC64_GOT16:
12724 case R_PPC64_GOT16_LO:
12725 case R_PPC64_GOT16_HI:
12726 case R_PPC64_GOT16_HA:
12727 case R_PPC64_GOT16_DS:
12728 case R_PPC64_GOT16_LO_DS:
411e1bfb 12729 dogot:
5bd4f169
AM
12730 {
12731 /* Relocation is to the entry for this symbol in the global
12732 offset table. */
e717da7e 12733 asection *got;
d881513a 12734 bfd_vma *offp;
5bd4f169 12735 bfd_vma off;
d881513a 12736 unsigned long indx = 0;
927be08e 12737 struct got_entry *ent;
65f38f15 12738
d881513a
AM
12739 if (tls_type == (TLS_TLS | TLS_LD)
12740 && (h == NULL
f5385ebf 12741 || !h->elf.def_dynamic))
927be08e 12742 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 12743 else
5bd4f169 12744 {
5bd4f169 12745
d881513a
AM
12746 if (h != NULL)
12747 {
12748 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
12749 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12750 &h->elf)
d881513a 12751 || (info->shared
726d3ab0 12752 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
12753 /* This is actually a static link, or it is a
12754 -Bsymbolic link and the symbol is defined
12755 locally, or the symbol was forced to be local
12756 because of a version file. */
12757 ;
12758 else
12759 {
039b3fef 12760 indx = h->elf.dynindx;
d881513a
AM
12761 unresolved_reloc = FALSE;
12762 }
039b3fef 12763 ent = h->elf.got.glist;
d881513a 12764 }
411e1bfb 12765 else
5bd4f169 12766 {
d881513a
AM
12767 if (local_got_ents == NULL)
12768 abort ();
12769 ent = local_got_ents[r_symndx];
5bd4f169 12770 }
d881513a
AM
12771
12772 for (; ent != NULL; ent = ent->next)
4cc603a5 12773 if (ent->addend == orig_addend
e717da7e 12774 && ent->owner == input_bfd
d881513a
AM
12775 && ent->tls_type == tls_type)
12776 break;
5bd4f169 12777 }
411e1bfb 12778
927be08e
AM
12779 if (ent == NULL)
12780 abort ();
12781 if (ent->is_indirect)
12782 ent = ent->got.ent;
12783 offp = &ent->got.offset;
12784 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
12785 if (got == NULL)
12786 abort ();
12787
411e1bfb
AM
12788 /* The offset must always be a multiple of 8. We use the
12789 least significant bit to record whether we have already
12790 processed this entry. */
d881513a 12791 off = *offp;
411e1bfb
AM
12792 if ((off & 1) != 0)
12793 off &= ~1;
5bd4f169
AM
12794 else
12795 {
411e1bfb
AM
12796 /* Generate relocs for the dynamic linker, except in
12797 the case of TLSLD where we'll use one entry per
12798 module. */
25f23106
AM
12799 asection *relgot;
12800 bfd_boolean ifunc;
e717da7e 12801
d881513a 12802 *offp = off | 1;
25f23106
AM
12803 relgot = NULL;
12804 ifunc = (h != NULL
12805 ? h->elf.type == STT_GNU_IFUNC
12806 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
4e795f50 12807 if ((info->shared || indx != 0)
927be08e
AM
12808 && (h == NULL
12809 || (tls_type == (TLS_TLS | TLS_LD)
12810 && !h->elf.def_dynamic)
039b3fef
AM
12811 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12812 || h->elf.root.type != bfd_link_hash_undefweak))
927be08e 12813 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106
AM
12814 else if (ifunc)
12815 relgot = htab->reliplt;
12816 if (relgot != NULL)
5bd4f169 12817 {
e717da7e
AM
12818 outrel.r_offset = (got->output_section->vma
12819 + got->output_offset
411e1bfb 12820 + off);
4cc603a5 12821 outrel.r_addend = addend;
d881513a 12822 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 12823 {
411e1bfb 12824 outrel.r_addend = 0;
e515b051 12825 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
12826 if (tls_type == (TLS_TLS | TLS_GD))
12827 {
e717da7e
AM
12828 loc = relgot->contents;
12829 loc += (relgot->reloc_count++
d881513a
AM
12830 * sizeof (Elf64_External_Rela));
12831 bfd_elf64_swap_reloca_out (output_bfd,
12832 &outrel, loc);
e515b051 12833 outrel.r_offset += 8;
4cc603a5 12834 outrel.r_addend = addend;
d881513a
AM
12835 outrel.r_info
12836 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 12837 }
411e1bfb 12838 }
951fd09b 12839 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 12840 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 12841 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12842 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
12843 else if (indx != 0)
12844 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12845 else
81407a69 12846 {
25f23106
AM
12847 if (ifunc)
12848 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12849 else
12850 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
12851
12852 /* Write the .got section contents for the sake
12853 of prelink. */
e717da7e 12854 loc = got->contents + off;
23fbd6fa
JJ
12855 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12856 loc);
81407a69 12857 }
81407a69
AM
12858
12859 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
12860 {
12861 outrel.r_addend += relocation;
12862 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 12863 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 12864 }
e717da7e
AM
12865 loc = relgot->contents;
12866 loc += (relgot->reloc_count++
411e1bfb
AM
12867 * sizeof (Elf64_External_Rela));
12868 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12869 }
12870
ad8e1ba5 12871 /* Init the .got section contents here if we're not
81407a69 12872 emitting a reloc. */
d881513a 12873 else
411e1bfb 12874 {
4cc603a5 12875 relocation += addend;
7b609f53
AM
12876 if (tls_type == (TLS_TLS | TLS_LD))
12877 relocation = 1;
12878 else if (tls_type != 0)
411e1bfb 12879 {
e1918d23 12880 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 12881 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12882 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 12883
7b609f53
AM
12884 if (tls_type == (TLS_TLS | TLS_GD))
12885 {
12886 bfd_put_64 (output_bfd, relocation,
e717da7e 12887 got->contents + off + 8);
7b609f53
AM
12888 relocation = 1;
12889 }
411e1bfb 12890 }
7b609f53 12891
411e1bfb 12892 bfd_put_64 (output_bfd, relocation,
e717da7e 12893 got->contents + off);
5bd4f169
AM
12894 }
12895 }
12896
65f38f15
AM
12897 if (off >= (bfd_vma) -2)
12898 abort ();
12899
bf102f86
AM
12900 relocation = got->output_section->vma + got->output_offset + off;
12901 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 12902 }
65f38f15
AM
12903 break;
12904
12905 case R_PPC64_PLT16_HA:
12906 case R_PPC64_PLT16_HI:
12907 case R_PPC64_PLT16_LO:
12908 case R_PPC64_PLT32:
12909 case R_PPC64_PLT64:
12910 /* Relocation is to the entry for this symbol in the
12911 procedure linkage table. */
12912
12913 /* Resolve a PLT reloc against a local symbol directly,
12914 without using the procedure linkage table. */
12915 if (h == NULL)
12916 break;
12917
411e1bfb
AM
12918 /* It's possible that we didn't make a PLT entry for this
12919 symbol. This happens when statically linking PIC code,
12920 or when using -Bsymbolic. Go find a match if there is a
12921 PLT entry. */
4ce794b7 12922 if (htab->plt != NULL)
65f38f15 12923 {
411e1bfb 12924 struct plt_entry *ent;
039b3fef 12925 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 12926 if (ent->addend == orig_addend
411e1bfb
AM
12927 && ent->plt.offset != (bfd_vma) -1)
12928 {
4ce794b7
AM
12929 relocation = (htab->plt->output_section->vma
12930 + htab->plt->output_offset
411e1bfb
AM
12931 + ent->plt.offset);
12932 unresolved_reloc = FALSE;
12933 }
65f38f15 12934 }
65f38f15 12935 break;
5bd4f169 12936
0b13192e
AM
12937 case R_PPC64_TOC:
12938 /* Relocation value is TOC base. */
12939 relocation = TOCstart;
cf35638d 12940 if (r_symndx == STN_UNDEF)
0b13192e 12941 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
12942 else if (unresolved_reloc)
12943 ;
12944 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
12945 relocation += htab->stub_group[sec->id].toc_off;
12946 else
12947 unresolved_reloc = TRUE;
ab96bf03 12948 goto dodyn;
0b13192e 12949
5bd4f169
AM
12950 /* TOC16 relocs. We want the offset relative to the TOC base,
12951 which is the address of the start of the TOC plus 0x8000.
12952 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12953 in this order. */
65f38f15
AM
12954 case R_PPC64_TOC16:
12955 case R_PPC64_TOC16_LO:
12956 case R_PPC64_TOC16_HI:
12957 case R_PPC64_TOC16_DS:
12958 case R_PPC64_TOC16_LO_DS:
12959 case R_PPC64_TOC16_HA:
ad8e1ba5 12960 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
12961 break;
12962
12963 /* Relocate against the beginning of the section. */
65f38f15
AM
12964 case R_PPC64_SECTOFF:
12965 case R_PPC64_SECTOFF_LO:
12966 case R_PPC64_SECTOFF_HI:
12967 case R_PPC64_SECTOFF_DS:
12968 case R_PPC64_SECTOFF_LO_DS:
12969 case R_PPC64_SECTOFF_HA:
4ce794b7 12970 if (sec != NULL)
65f38f15 12971 addend -= sec->output_section->vma;
5bd4f169
AM
12972 break;
12973
25f23106
AM
12974 case R_PPC64_REL16:
12975 case R_PPC64_REL16_LO:
12976 case R_PPC64_REL16_HI:
12977 case R_PPC64_REL16_HA:
12978 break;
12979
721956f4
AM
12980 case R_PPC64_REL14:
12981 case R_PPC64_REL14_BRNTAKEN:
12982 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
12983 case R_PPC64_REL24:
12984 break;
12985
411e1bfb
AM
12986 case R_PPC64_TPREL16:
12987 case R_PPC64_TPREL16_LO:
12988 case R_PPC64_TPREL16_HI:
12989 case R_PPC64_TPREL16_HA:
12990 case R_PPC64_TPREL16_DS:
12991 case R_PPC64_TPREL16_LO_DS:
12992 case R_PPC64_TPREL16_HIGHER:
12993 case R_PPC64_TPREL16_HIGHERA:
12994 case R_PPC64_TPREL16_HIGHEST:
12995 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
12996 if (h != NULL
12997 && h->elf.root.type == bfd_link_hash_undefweak
12998 && h->elf.dynindx == -1)
12999 {
13000 /* Make this relocation against an undefined weak symbol
13001 resolve to zero. This is really just a tweak, since
13002 code using weak externs ought to check that they are
13003 defined before using them. */
13004 bfd_byte *p = contents + rel->r_offset - d_offset;
13005
13006 insn = bfd_get_32 (output_bfd, p);
13007 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13008 if (insn != 0)
13009 bfd_put_32 (output_bfd, insn, p);
13010 break;
13011 }
e1918d23 13012 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13013 if (info->shared)
13014 /* The TPREL16 relocs shouldn't really be used in shared
13015 libs as they will result in DT_TEXTREL being set, but
13016 support them anyway. */
13017 goto dodyn;
13018 break;
13019
13020 case R_PPC64_DTPREL16:
13021 case R_PPC64_DTPREL16_LO:
13022 case R_PPC64_DTPREL16_HI:
13023 case R_PPC64_DTPREL16_HA:
13024 case R_PPC64_DTPREL16_DS:
13025 case R_PPC64_DTPREL16_LO_DS:
13026 case R_PPC64_DTPREL16_HIGHER:
13027 case R_PPC64_DTPREL16_HIGHERA:
13028 case R_PPC64_DTPREL16_HIGHEST:
13029 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 13030 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13031 break;
13032
e515b051
AM
13033 case R_PPC64_DTPMOD64:
13034 relocation = 1;
13035 addend = 0;
13036 goto dodyn;
13037
411e1bfb 13038 case R_PPC64_TPREL64:
e1918d23 13039 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
13040 goto dodyn;
13041
13042 case R_PPC64_DTPREL64:
e1918d23 13043 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
13044 /* Fall thru */
13045
65f38f15
AM
13046 /* Relocations that may need to be propagated if this is a
13047 dynamic object. */
04c9666a 13048 case R_PPC64_REL30:
65f38f15
AM
13049 case R_PPC64_REL32:
13050 case R_PPC64_REL64:
13051 case R_PPC64_ADDR14:
13052 case R_PPC64_ADDR14_BRNTAKEN:
13053 case R_PPC64_ADDR14_BRTAKEN:
13054 case R_PPC64_ADDR16:
13055 case R_PPC64_ADDR16_DS:
13056 case R_PPC64_ADDR16_HA:
13057 case R_PPC64_ADDR16_HI:
13058 case R_PPC64_ADDR16_HIGHER:
13059 case R_PPC64_ADDR16_HIGHERA:
13060 case R_PPC64_ADDR16_HIGHEST:
13061 case R_PPC64_ADDR16_HIGHESTA:
13062 case R_PPC64_ADDR16_LO:
13063 case R_PPC64_ADDR16_LO_DS:
13064 case R_PPC64_ADDR24:
65f38f15
AM
13065 case R_PPC64_ADDR32:
13066 case R_PPC64_ADDR64:
13067 case R_PPC64_UADDR16:
13068 case R_PPC64_UADDR32:
13069 case R_PPC64_UADDR64:
411e1bfb 13070 dodyn:
5d1634d7 13071 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
13072 break;
13073
41bd81ab
AM
13074 if (NO_OPD_RELOCS && is_opd)
13075 break;
13076
65f38f15 13077 if ((info->shared
4e795f50 13078 && (h == NULL
039b3fef
AM
13079 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13080 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 13081 && (must_be_dyn_reloc (info, r_type)
039b3fef 13082 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
13083 || (ELIMINATE_COPY_RELOCS
13084 && !info->shared
65f38f15 13085 && h != NULL
039b3fef 13086 && h->elf.dynindx != -1
f5385ebf 13087 && !h->elf.non_got_ref
25f23106
AM
13088 && !h->elf.def_regular)
13089 || (!info->shared
13090 && (h != NULL
13091 ? h->elf.type == STT_GNU_IFUNC
13092 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 13093 {
b34976b6 13094 bfd_boolean skip, relocate;
65f38f15 13095 asection *sreloc;
1cf1f670 13096 bfd_vma out_off;
65f38f15
AM
13097
13098 /* When generating a dynamic object, these relocations
13099 are copied into the output file to be resolved at run
13100 time. */
13101
b34976b6
AM
13102 skip = FALSE;
13103 relocate = FALSE;
65f38f15 13104
1cf1f670
AM
13105 out_off = _bfd_elf_section_offset (output_bfd, info,
13106 input_section, rel->r_offset);
13107 if (out_off == (bfd_vma) -1)
b34976b6 13108 skip = TRUE;
1cf1f670 13109 else if (out_off == (bfd_vma) -2)
b34976b6 13110 skip = TRUE, relocate = TRUE;
1cf1f670
AM
13111 out_off += (input_section->output_section->vma
13112 + input_section->output_offset);
13113 outrel.r_offset = out_off;
411e1bfb 13114 outrel.r_addend = rel->r_addend;
65f38f15 13115
1cf1f670
AM
13116 /* Optimize unaligned reloc use. */
13117 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13118 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13119 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13120 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13121 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13122 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13123 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13124 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13125 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13126
65f38f15 13127 if (skip)
0bb2d96a 13128 memset (&outrel, 0, sizeof outrel);
726d3ab0 13129 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
13130 && !is_opd
13131 && r_type != R_PPC64_TOC)
039b3fef 13132 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
13133 else
13134 {
41bd81ab
AM
13135 /* This symbol is local, or marked to become local,
13136 or this is an opd section reloc which must point
13137 at a local function. */
65f38f15 13138 outrel.r_addend += relocation;
e86ce104 13139 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 13140 {
3fad3c7c 13141 if (is_opd && h != NULL)
afbe61cf
AM
13142 {
13143 /* Lie about opd entries. This case occurs
13144 when building shared libraries and we
13145 reference a function in another shared
3fad3c7c
AM
13146 lib. The same thing happens for a weak
13147 definition in an application that's
13148 overridden by a strong definition in a
13149 shared lib. (I believe this is a generic
13150 bug in binutils handling of weak syms.)
13151 In these cases we won't use the opd
1e2f5b6e 13152 entry in this lib. */
b34976b6 13153 unresolved_reloc = FALSE;
afbe61cf 13154 }
25f23106
AM
13155 if (!is_opd
13156 && r_type == R_PPC64_ADDR64
13157 && (h != NULL
13158 ? h->elf.type == STT_GNU_IFUNC
13159 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13160 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13161 else
13162 {
13163 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 13164
25f23106
AM
13165 /* We need to relocate .opd contents for ld.so.
13166 Prelink also wants simple and consistent rules
13167 for relocs. This make all RELATIVE relocs have
13168 *r_offset equal to r_addend. */
13169 relocate = TRUE;
13170 }
65f38f15
AM
13171 }
13172 else
13173 {
13174 long indx = 0;
13175
25f23106
AM
13176 if (h != NULL
13177 ? h->elf.type == STT_GNU_IFUNC
13178 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13179 {
25f53a85 13180 info->callbacks->einfo
8de848d8 13181 (_("%P: %H: relocation %s for indirect "
25f53a85
AM
13182 "function %s unsupported\n"),
13183 input_bfd, input_section, rel->r_offset,
25f23106
AM
13184 ppc64_elf_howto_table[r_type]->name,
13185 sym_name);
13186 ret = FALSE;
13187 }
cf35638d 13188 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
13189 ;
13190 else if (sec == NULL || sec->owner == NULL)
13191 {
13192 bfd_set_error (bfd_error_bad_value);
b34976b6 13193 return FALSE;
65f38f15
AM
13194 }
13195 else
13196 {
13197 asection *osec;
13198
13199 osec = sec->output_section;
13200 indx = elf_section_data (osec)->dynindx;
13201
74541ad4
AM
13202 if (indx == 0)
13203 {
13204 if ((osec->flags & SEC_READONLY) == 0
13205 && htab->elf.data_index_section != NULL)
13206 osec = htab->elf.data_index_section;
13207 else
13208 osec = htab->elf.text_index_section;
13209 indx = elf_section_data (osec)->dynindx;
13210 }
13211 BFD_ASSERT (indx != 0);
13212
65f38f15
AM
13213 /* We are turning this relocation into one
13214 against a section symbol, so subtract out
13215 the output section's address but not the
13216 offset of the input section in the output
13217 section. */
13218 outrel.r_addend -= osec->vma;
13219 }
13220
13221 outrel.r_info = ELF64_R_INFO (indx, r_type);
13222 }
13223 }
13224
13225 sreloc = elf_section_data (input_section)->sreloc;
25f23106
AM
13226 if (!htab->elf.dynamic_sections_created)
13227 sreloc = htab->reliplt;
65f38f15
AM
13228 if (sreloc == NULL)
13229 abort ();
13230
dfbb6ac9
AM
13231 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13232 >= sreloc->size)
13233 abort ();
947216bf
AM
13234 loc = sreloc->contents;
13235 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
13236 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13237
13238 /* If this reloc is against an external symbol, it will
13239 be computed at runtime, so there's no need to do
81407a69
AM
13240 anything now. However, for the sake of prelink ensure
13241 that the section contents are a known value. */
65f38f15 13242 if (! relocate)
81407a69
AM
13243 {
13244 unresolved_reloc = FALSE;
13245 /* The value chosen here is quite arbitrary as ld.so
13246 ignores section contents except for the special
13247 case of .opd where the contents might be accessed
13248 before relocation. Choose zero, as that won't
13249 cause reloc overflow. */
13250 relocation = 0;
13251 addend = 0;
13252 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13253 to improve backward compatibility with older
13254 versions of ld. */
13255 if (r_type == R_PPC64_ADDR64)
13256 addend = outrel.r_addend;
13257 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 13258 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
13259 addend = (input_section->output_section->vma
13260 + input_section->output_offset
13261 + rel->r_offset);
81407a69 13262 }
65f38f15 13263 }
5bd4f169
AM
13264 break;
13265
65f38f15
AM
13266 case R_PPC64_COPY:
13267 case R_PPC64_GLOB_DAT:
13268 case R_PPC64_JMP_SLOT:
25f23106 13269 case R_PPC64_JMP_IREL:
65f38f15
AM
13270 case R_PPC64_RELATIVE:
13271 /* We shouldn't ever see these dynamic relocs in relocatable
13272 files. */
ae9a127f 13273 /* Fall through. */
65f38f15
AM
13274
13275 case R_PPC64_PLTGOT16:
13276 case R_PPC64_PLTGOT16_DS:
13277 case R_PPC64_PLTGOT16_HA:
13278 case R_PPC64_PLTGOT16_HI:
13279 case R_PPC64_PLTGOT16_LO:
13280 case R_PPC64_PLTGOT16_LO_DS:
13281 case R_PPC64_PLTREL32:
13282 case R_PPC64_PLTREL64:
13283 /* These ones haven't been implemented yet. */
13284
25f53a85 13285 info->callbacks->einfo
8de848d8 13286 (_("%P: %B: relocation %s is not supported for symbol %s\n"),
d003868e 13287 input_bfd,
4ce794b7 13288 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
13289
13290 bfd_set_error (bfd_error_invalid_operation);
b34976b6 13291 ret = FALSE;
5bd4f169 13292 continue;
65f38f15 13293 }
5bd4f169 13294
67f0cbdb
AM
13295 /* Multi-instruction sequences that access the TOC can be
13296 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13297 to nop; addi rb,r2,x; */
13298 switch (r_type)
13299 {
13300 default:
13301 break;
13302
13303 case R_PPC64_GOT_TLSLD16_HI:
13304 case R_PPC64_GOT_TLSGD16_HI:
13305 case R_PPC64_GOT_TPREL16_HI:
13306 case R_PPC64_GOT_DTPREL16_HI:
13307 case R_PPC64_GOT16_HI:
13308 case R_PPC64_TOC16_HI:
13309 /* These relocs would only be useful if building up an
13310 offset to later add to r2, perhaps in an indexed
13311 addressing mode instruction. Don't try to optimize.
13312 Unfortunately, the possibility of someone building up an
13313 offset like this or even with the HA relocs, means that
13314 we need to check the high insn when optimizing the low
13315 insn. */
13316 break;
13317
13318 case R_PPC64_GOT_TLSLD16_HA:
13319 case R_PPC64_GOT_TLSGD16_HA:
13320 case R_PPC64_GOT_TPREL16_HA:
13321 case R_PPC64_GOT_DTPREL16_HA:
13322 case R_PPC64_GOT16_HA:
13323 case R_PPC64_TOC16_HA:
98528052
AM
13324 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13325 && !ppc64_elf_tdata (input_bfd)->ha_relocs_not_using_r2)
13326 {
13327 bfd_byte *p = contents + (rel->r_offset & ~3);
13328 bfd_put_32 (input_bfd, NOP, p);
13329 }
67f0cbdb
AM
13330 break;
13331
13332 case R_PPC64_GOT_TLSLD16_LO:
13333 case R_PPC64_GOT_TLSGD16_LO:
13334 case R_PPC64_GOT_TPREL16_LO_DS:
13335 case R_PPC64_GOT_DTPREL16_LO_DS:
13336 case R_PPC64_GOT16_LO:
13337 case R_PPC64_GOT16_LO_DS:
13338 case R_PPC64_TOC16_LO:
13339 case R_PPC64_TOC16_LO_DS:
98528052
AM
13340 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
13341 && !ppc64_elf_tdata (input_bfd)->ha_relocs_not_using_r2)
67f0cbdb
AM
13342 {
13343 bfd_byte *p = contents + (rel->r_offset & ~3);
13344 insn = bfd_get_32 (input_bfd, p);
13345 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13346 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13347 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13348 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13349 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13350 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13351 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13352 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13353 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13354 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13355 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13356 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13357 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13358 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13359 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13360 && (insn & 3) != 1)
13361 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13362 && ((insn & 3) == 0 || (insn & 3) == 3)))
13363 {
98528052
AM
13364 insn &= ~(0x1f << 16);
13365 insn |= 2 << 16;
13366 bfd_put_32 (input_bfd, insn, p);
67f0cbdb
AM
13367 }
13368 }
13369 break;
13370 }
13371
65f38f15
AM
13372 /* Do any further special processing. */
13373 switch (r_type)
13374 {
13375 default:
13376 break;
13377
13378 case R_PPC64_ADDR16_HA:
25f23106 13379 case R_PPC64_REL16_HA:
65f38f15
AM
13380 case R_PPC64_ADDR16_HIGHERA:
13381 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
13382 case R_PPC64_TOC16_HA:
13383 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
13384 case R_PPC64_TPREL16_HA:
13385 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
13386 case R_PPC64_TPREL16_HIGHER:
13387 case R_PPC64_TPREL16_HIGHERA:
13388 case R_PPC64_TPREL16_HIGHEST:
13389 case R_PPC64_TPREL16_HIGHESTA:
13390 case R_PPC64_DTPREL16_HIGHER:
13391 case R_PPC64_DTPREL16_HIGHERA:
13392 case R_PPC64_DTPREL16_HIGHEST:
13393 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
13394 /* It's just possible that this symbol is a weak symbol
13395 that's not actually defined anywhere. In that case,
13396 'sec' would be NULL, and we should leave the symbol
13397 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
13398 if (sec == NULL)
13399 break;
13400 /* Fall thru */
13401
13402 case R_PPC64_GOT16_HA:
13403 case R_PPC64_PLTGOT16_HA:
13404 case R_PPC64_PLT16_HA:
13405 case R_PPC64_GOT_TLSGD16_HA:
13406 case R_PPC64_GOT_TLSLD16_HA:
13407 case R_PPC64_GOT_TPREL16_HA:
13408 case R_PPC64_GOT_DTPREL16_HA:
13409 /* Add 0x10000 if sign bit in 0:15 is set.
13410 Bits 0:15 are not used. */
13411 addend += 0x8000;
65f38f15
AM
13412 break;
13413
13414 case R_PPC64_ADDR16_DS:
13415 case R_PPC64_ADDR16_LO_DS:
13416 case R_PPC64_GOT16_DS:
13417 case R_PPC64_GOT16_LO_DS:
13418 case R_PPC64_PLT16_LO_DS:
13419 case R_PPC64_SECTOFF_DS:
13420 case R_PPC64_SECTOFF_LO_DS:
13421 case R_PPC64_TOC16_DS:
13422 case R_PPC64_TOC16_LO_DS:
13423 case R_PPC64_PLTGOT16_DS:
13424 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
13425 case R_PPC64_GOT_TPREL16_DS:
13426 case R_PPC64_GOT_TPREL16_LO_DS:
13427 case R_PPC64_GOT_DTPREL16_DS:
13428 case R_PPC64_GOT_DTPREL16_LO_DS:
13429 case R_PPC64_TPREL16_DS:
13430 case R_PPC64_TPREL16_LO_DS:
13431 case R_PPC64_DTPREL16_DS:
13432 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
13433 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13434 mask = 3;
13435 /* If this reloc is against an lq insn, then the value must be
13436 a multiple of 16. This is somewhat of a hack, but the
13437 "correct" way to do this by defining _DQ forms of all the
13438 _DS relocs bloats all reloc switches in this file. It
13439 doesn't seem to make much sense to use any of these relocs
13440 in data, so testing the insn should be safe. */
494dac0c 13441 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
13442 mask = 15;
13443 if (((relocation + addend) & mask) != 0)
65f38f15 13444 {
25f53a85 13445 info->callbacks->einfo
8de848d8 13446 (_("%P: %H: error: %s not a multiple of %u\n"),
25f53a85 13447 input_bfd, input_section, rel->r_offset,
4ce794b7 13448 ppc64_elf_howto_table[r_type]->name,
adadcc0c 13449 mask + 1);
65f38f15 13450 bfd_set_error (bfd_error_bad_value);
b34976b6 13451 ret = FALSE;
65f38f15
AM
13452 continue;
13453 }
13454 break;
5bd4f169
AM
13455 }
13456
239e1f3a
AM
13457 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13458 because such sections are not SEC_ALLOC and thus ld.so will
13459 not process them. */
65f38f15 13460 if (unresolved_reloc
239e1f3a 13461 && !((input_section->flags & SEC_DEBUGGING) != 0
1d5316ab
AM
13462 && h->elf.def_dynamic)
13463 && _bfd_elf_section_offset (output_bfd, info, input_section,
13464 rel->r_offset) != (bfd_vma) -1)
9c07fe7c 13465 {
25f53a85 13466 info->callbacks->einfo
8de848d8 13467 (_("%P: %H: unresolvable %s relocation against symbol `%s'\n"),
25f53a85 13468 input_bfd, input_section, rel->r_offset,
7b609f53 13469 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 13470 h->elf.root.root.string);
b34976b6 13471 ret = FALSE;
9c07fe7c 13472 }
5bd4f169 13473
65f38f15 13474 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
13475 input_bfd,
13476 input_section,
13477 contents,
411e1bfb 13478 rel->r_offset,
5bd4f169
AM
13479 relocation,
13480 addend);
13481
ef60b7ff 13482 if (r != bfd_reloc_ok)
5bd4f169 13483 {
cd27b276
AM
13484 if (sym_name == NULL)
13485 sym_name = "(null)";
13486 if (r == bfd_reloc_overflow)
5bd4f169 13487 {
cd27b276
AM
13488 if (warned)
13489 continue;
13490 if (h != NULL
039b3fef 13491 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 13492 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
13493 {
13494 /* Assume this is a call protected by other code that
13495 detects the symbol is undefined. If this is the case,
13496 we can safely ignore the overflow. If not, the
13497 program is hosed anyway, and a little warning isn't
13498 going to help. */
13499
13500 continue;
13501 }
13502
ef60b7ff 13503 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
13504 (info, (h ? &h->elf.root : NULL), sym_name,
13505 ppc64_elf_howto_table[r_type]->name,
4cc603a5 13506 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 13507 return FALSE;
ef60b7ff
AM
13508 }
13509 else
13510 {
25f53a85 13511 info->callbacks->einfo
8de848d8 13512 (_("%P: %H: %s reloc against `%s': error %d\n"),
25f53a85 13513 input_bfd, input_section, rel->r_offset,
4ce794b7 13514 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
13515 sym_name,
13516 (int) r);
b34976b6 13517 ret = FALSE;
ef60b7ff 13518 }
5bd4f169 13519 }
5bd4f169
AM
13520 }
13521
645ea6a9
AM
13522 /* If we're emitting relocations, then shortly after this function
13523 returns, reloc offsets and addends for this section will be
13524 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
13525 file rather than the input. Save a copy of the relocs for
13526 opd_entry_value. */
4cc603a5 13527 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
13528 {
13529 bfd_size_type amt;
13530 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13531 rel = bfd_alloc (input_bfd, amt);
13532 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13533 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13534 if (rel == NULL)
13535 return FALSE;
13536 memcpy (rel, relocs, amt);
13537 }
5bd4f169
AM
13538 return ret;
13539}
13540
754021d0
AM
13541/* Adjust the value of any local symbols in opd sections. */
13542
6e0b88f1 13543static int
754021d0
AM
13544ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13545 const char *name ATTRIBUTE_UNUSED,
13546 Elf_Internal_Sym *elfsym,
13547 asection *input_sec,
13548 struct elf_link_hash_entry *h)
13549{
74f0fb50
AM
13550 struct _opd_sec_data *opd;
13551 long adjust;
754021d0
AM
13552 bfd_vma value;
13553
4025353c 13554 if (h != NULL)
6e0b88f1 13555 return 1;
4025353c 13556
74f0fb50
AM
13557 opd = get_opd_info (input_sec);
13558 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 13559 return 1;
754021d0
AM
13560
13561 value = elfsym->st_value - input_sec->output_offset;
13562 if (!info->relocatable)
13563 value -= input_sec->output_section->vma;
13564
74f0fb50 13565 adjust = opd->adjust[value / 8];
4025353c 13566 if (adjust == -1)
6e0b88f1
AM
13567 return 2;
13568
13569 elfsym->st_value += adjust;
13570 return 1;
754021d0
AM
13571}
13572
5bd4f169
AM
13573/* Finish up dynamic symbol handling. We set the contents of various
13574 dynamic sections here. */
13575
b34976b6 13576static bfd_boolean
4ce794b7
AM
13577ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13578 struct bfd_link_info *info,
13579 struct elf_link_hash_entry *h,
13580 Elf_Internal_Sym *sym)
5bd4f169 13581{
65f38f15 13582 struct ppc_link_hash_table *htab;
8387904d
AM
13583 struct plt_entry *ent;
13584 Elf_Internal_Rela rela;
13585 bfd_byte *loc;
5bd4f169 13586
65f38f15 13587 htab = ppc_hash_table (info);
4dfe6ac6
NC
13588 if (htab == NULL)
13589 return FALSE;
5bd4f169 13590
8387904d
AM
13591 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13592 if (ent->plt.offset != (bfd_vma) -1)
13593 {
13594 /* This symbol has an entry in the procedure linkage
13595 table. Set it up. */
e054468f
AM
13596 if (!htab->elf.dynamic_sections_created
13597 || h->dynindx == -1)
13598 {
13599 BFD_ASSERT (h->type == STT_GNU_IFUNC
13600 && h->def_regular
13601 && (h->root.type == bfd_link_hash_defined
13602 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
13603 rela.r_offset = (htab->iplt->output_section->vma
13604 + htab->iplt->output_offset
13605 + ent->plt.offset);
13606 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
13607 rela.r_addend = (h->root.u.def.value
13608 + h->root.u.def.section->output_offset
13609 + h->root.u.def.section->output_section->vma
13610 + ent->addend);
25f23106
AM
13611 loc = (htab->reliplt->contents
13612 + (htab->reliplt->reloc_count++
13613 * sizeof (Elf64_External_Rela)));
e054468f
AM
13614 }
13615 else
13616 {
25f23106
AM
13617 rela.r_offset = (htab->plt->output_section->vma
13618 + htab->plt->output_offset
13619 + ent->plt.offset);
e054468f
AM
13620 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13621 rela.r_addend = ent->addend;
25f23106
AM
13622 loc = (htab->relplt->contents
13623 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13624 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 13625 }
8387904d
AM
13626 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13627 }
5bd4f169 13628
f5385ebf 13629 if (h->needs_copy)
5bd4f169 13630 {
65f38f15 13631 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 13632
65f38f15
AM
13633 if (h->dynindx == -1
13634 || (h->root.type != bfd_link_hash_defined
13635 && h->root.type != bfd_link_hash_defweak)
4ce794b7 13636 || htab->relbss == NULL)
65f38f15 13637 abort ();
5bd4f169
AM
13638
13639 rela.r_offset = (h->root.u.def.value
13640 + h->root.u.def.section->output_section->vma
13641 + h->root.u.def.section->output_offset);
13642 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13643 rela.r_addend = 0;
4ce794b7
AM
13644 loc = htab->relbss->contents;
13645 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 13646 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
13647 }
13648
5bd4f169
AM
13649 /* Mark some specially defined symbols as absolute. */
13650 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13651 sym->st_shndx = SHN_ABS;
13652
b34976b6 13653 return TRUE;
5bd4f169
AM
13654}
13655
65f38f15
AM
13656/* Used to decide how to sort relocs in an optimal manner for the
13657 dynamic linker, before writing them out. */
13658
13659static enum elf_reloc_type_class
4ce794b7 13660ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 13661{
04c9666a 13662 enum elf_ppc64_reloc_type r_type;
a33d1f77 13663
4ce794b7 13664 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 13665 switch (r_type)
65f38f15
AM
13666 {
13667 case R_PPC64_RELATIVE:
13668 return reloc_class_relative;
13669 case R_PPC64_JMP_SLOT:
13670 return reloc_class_plt;
13671 case R_PPC64_COPY:
13672 return reloc_class_copy;
13673 default:
13674 return reloc_class_normal;
13675 }
13676}
13677
5bd4f169
AM
13678/* Finish up the dynamic sections. */
13679
b34976b6 13680static bfd_boolean
4ce794b7
AM
13681ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13682 struct bfd_link_info *info)
5bd4f169 13683{
65f38f15
AM
13684 struct ppc_link_hash_table *htab;
13685 bfd *dynobj;
5bd4f169 13686 asection *sdyn;
5bd4f169 13687
65f38f15 13688 htab = ppc_hash_table (info);
4dfe6ac6
NC
13689 if (htab == NULL)
13690 return FALSE;
13691
65f38f15 13692 dynobj = htab->elf.dynobj;
5bd4f169
AM
13693 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13694
65f38f15 13695 if (htab->elf.dynamic_sections_created)
5bd4f169 13696 {
5bd4f169
AM
13697 Elf64_External_Dyn *dyncon, *dynconend;
13698
4ce794b7 13699 if (sdyn == NULL || htab->got == NULL)
65f38f15 13700 abort ();
5bd4f169
AM
13701
13702 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 13703 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
13704 for (; dyncon < dynconend; dyncon++)
13705 {
13706 Elf_Internal_Dyn dyn;
19397422 13707 asection *s;
5bd4f169
AM
13708
13709 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13710
13711 switch (dyn.d_tag)
13712 {
65f38f15
AM
13713 default:
13714 continue;
5bd4f169 13715
5d1634d7 13716 case DT_PPC64_GLINK:
4ce794b7 13717 s = htab->glink;
6348e046 13718 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
13719 /* We stupidly defined DT_PPC64_GLINK to be the start
13720 of glink rather than the first entry point, which is
13721 what ld.so needs, and now have a bigger stub to
13722 support automatic multiple TOCs. */
13723 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
13724 break;
13725
19397422
AM
13726 case DT_PPC64_OPD:
13727 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13728 if (s == NULL)
13729 continue;
13730 dyn.d_un.d_ptr = s->vma;
19397422
AM
13731 break;
13732
13733 case DT_PPC64_OPDSZ:
13734 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13735 if (s == NULL)
13736 continue;
eea6121a 13737 dyn.d_un.d_val = s->size;
19397422
AM
13738 break;
13739
65f38f15 13740 case DT_PLTGOT:
4ce794b7 13741 s = htab->plt;
6348e046 13742 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
13743 break;
13744
13745 case DT_JMPREL:
4ce794b7 13746 s = htab->relplt;
6348e046 13747 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 13748 break;
5bd4f169 13749
65f38f15 13750 case DT_PLTRELSZ:
eea6121a 13751 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
13752 break;
13753
13754 case DT_RELASZ:
13755 /* Don't count procedure linkage table relocs in the
13756 overall reloc count. */
4ce794b7 13757 s = htab->relplt;
6348e046
AM
13758 if (s == NULL)
13759 continue;
eea6121a 13760 dyn.d_un.d_val -= s->size;
6348e046
AM
13761 break;
13762
13763 case DT_RELA:
13764 /* We may not be using the standard ELF linker script.
13765 If .rela.plt is the first .rela section, we adjust
13766 DT_RELA to not include it. */
4ce794b7 13767 s = htab->relplt;
6348e046
AM
13768 if (s == NULL)
13769 continue;
13770 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13771 continue;
eea6121a 13772 dyn.d_un.d_ptr += s->size;
65f38f15 13773 break;
5bd4f169 13774 }
5bd4f169 13775
65f38f15 13776 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 13777 }
5bd4f169
AM
13778 }
13779
eea6121a 13780 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
13781 {
13782 /* Fill in the first entry in the global offset table.
13783 We use it to hold the link-time TOCbase. */
13784 bfd_put_64 (output_bfd,
60ee0d4a 13785 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 13786 htab->got->contents);
5d1634d7
AM
13787
13788 /* Set .got entry size. */
4ce794b7 13789 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
13790 }
13791
eea6121a 13792 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
13793 {
13794 /* Set .plt entry size. */
4ce794b7 13795 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
13796 = PLT_ENTRY_SIZE;
13797 }
13798
84f5d08e
AM
13799 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13800 brlt ourselves if emitrelocations. */
13801 if (htab->brlt != NULL
13802 && htab->brlt->reloc_count != 0
13803 && !_bfd_elf_link_output_relocs (output_bfd,
13804 htab->brlt,
d4730f92 13805 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
13806 elf_section_data (htab->brlt)->relocs,
13807 NULL))
13808 return FALSE;
13809
176a0d42
AM
13810 if (htab->glink != NULL
13811 && htab->glink->reloc_count != 0
13812 && !_bfd_elf_link_output_relocs (output_bfd,
13813 htab->glink,
d4730f92 13814 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
13815 elf_section_data (htab->glink)->relocs,
13816 NULL))
13817 return FALSE;
13818
58d180e8
AM
13819
13820 if (htab->glink_eh_frame != NULL
13821 && htab->glink_eh_frame->sec_info_type == ELF_INFO_TYPE_EH_FRAME
13822 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
13823 htab->glink_eh_frame,
13824 htab->glink_eh_frame->contents))
13825 return FALSE;
13826
e717da7e 13827 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
13828 since we didn't add them to DYNOBJ. We know dynobj is the first
13829 bfd. */
e717da7e
AM
13830 while ((dynobj = dynobj->link_next) != NULL)
13831 {
13832 asection *s;
7b53ace3 13833
0c8d6e5c 13834 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
13835 continue;
13836
e717da7e
AM
13837 s = ppc64_elf_tdata (dynobj)->got;
13838 if (s != NULL
eea6121a 13839 && s->size != 0
e717da7e
AM
13840 && s->output_section != bfd_abs_section_ptr
13841 && !bfd_set_section_contents (output_bfd, s->output_section,
13842 s->contents, s->output_offset,
eea6121a 13843 s->size))
e717da7e
AM
13844 return FALSE;
13845 s = ppc64_elf_tdata (dynobj)->relgot;
13846 if (s != NULL
eea6121a 13847 && s->size != 0
e717da7e
AM
13848 && s->output_section != bfd_abs_section_ptr
13849 && !bfd_set_section_contents (output_bfd, s->output_section,
13850 s->contents, s->output_offset,
eea6121a 13851 s->size))
e717da7e
AM
13852 return FALSE;
13853 }
f6c52c13 13854
b34976b6 13855 return TRUE;
5bd4f169
AM
13856}
13857
5bd4f169 13858#include "elf64-target.h"
This page took 2.035526 seconds and 4 git commands to generate.