]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
remove subdir_do recursive make command in gdbserver/Makefile.in
[thirdparty/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"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
56static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *);
5bd4f169 58
ad8e1ba5
AM
59#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
60#define TARGET_LITTLE_NAME "elf64-powerpcle"
61#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
24718e3b 67#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
ad8e1ba5 74#define elf_backend_got_header_size 8
ad8e1ba5
AM
75#define elf_backend_can_gc_sections 1
76#define elf_backend_can_refcount 1
77#define elf_backend_rela_normal 1
6bfdb61b 78#define elf_backend_default_execstack 0
ad8e1ba5 79
e717da7e 80#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 81#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
157090f7 82#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ad8e1ba5
AM
83#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
84#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
85#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
86#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 87#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
ad8e1ba5
AM
88
89#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
90#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
91#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 92#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
93#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
94#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 95#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 96#define elf_backend_check_directives ppc64_elf_process_dot_syms
97fed1c9 97#define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
8387904d 98#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 99#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 100#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 101#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
102#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
103#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
104#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
105#define elf_backend_hide_symbol ppc64_elf_hide_symbol
106#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
107#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 108#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 109#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
110#define elf_backend_relocate_section ppc64_elf_relocate_section
111#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
112#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
113#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 114#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 115#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 116#define elf_backend_post_process_headers _bfd_elf_set_osabi
ad8e1ba5 117
5bd4f169
AM
118/* The name of the dynamic interpreter. This is put in the .interp
119 section. */
120#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
121
122/* The size in bytes of an entry in the procedure linkage table. */
123#define PLT_ENTRY_SIZE 24
124
125/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 126#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
127
128/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
129#define TOC_BASE_OFF 0x8000
130
131/* Offset of tp and dtp pointers from start of TLS block. */
132#define TP_OFFSET 0x7000
133#define DTP_OFFSET 0x8000
5bd4f169 134
ad8e1ba5
AM
135/* .plt call stub instructions. The normal stub is like this, but
136 sometimes the .plt entry crosses a 64k boundary and we need to
ac2df442 137 insert an addi to adjust r12. */
ad8e1ba5 138#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7
AM
139#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
140#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
141#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
5d1634d7 142#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
ac2df442 143#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
5d1634d7
AM
144 /* ld %r11,xxx+16@l(%r12) */
145#define BCTR 0x4e800420 /* bctr */
146
5d1634d7 147
ee4bf8d2 148#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
ac2df442 149#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
ad8e1ba5
AM
150#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
151#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
152
ac2df442
AM
153#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
154#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
155
ad8e1ba5
AM
156#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
157
ee4bf8d2 158/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 159#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
160 /* 0: */
161 /* .quad plt0-1f */
162 /* __glink: */
163#define MFLR_R12 0x7d8802a6 /* mflr %12 */
164#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
165 /* 1: */
166#define MFLR_R11 0x7d6802a6 /* mflr %11 */
167#define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
168#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
169#define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
170 /* ld %11,0(%12) */
171 /* ld %2,8(%12) */
172 /* mtctr %11 */
173 /* ld %11,16(%12) */
174 /* bctr */
5d1634d7
AM
175
176/* Pad with this. */
177#define NOP 0x60000000
178
721956f4
AM
179/* Some other nops. */
180#define CROR_151515 0x4def7b82
181#define CROR_313131 0x4ffffb82
182
cedb70c5 183/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
184#define LI_R0_0 0x38000000 /* li %r0,0 */
185#define B_DOT 0x48000000 /* b . */
186
187/* After that, we need two instructions to load the index, followed by
188 a branch. */
189#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 190#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 191
deb0e272
AM
192/* Instructions used by the save and restore reg functions. */
193#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
194#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
195#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
196#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
197#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
198#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
199#define LI_R12_0 0x39800000 /* li %r12,0 */
200#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
201#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
202#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
203#define BLR 0x4e800020 /* blr */
204
41bd81ab
AM
205/* Since .opd is an array of descriptors and each entry will end up
206 with identical R_PPC64_RELATIVE relocs, there is really no need to
207 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 208 relocate .opd without reloc entries. */
41bd81ab
AM
209#ifndef NO_OPD_RELOCS
210#define NO_OPD_RELOCS 0
211#endif
5bd4f169 212\f
f5e87a1d 213#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 214
5bd4f169 215/* Relocation HOWTO's. */
04c9666a 216static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
217
218static reloc_howto_type ppc64_elf_howto_raw[] = {
219 /* This reloc does nothing. */
220 HOWTO (R_PPC64_NONE, /* type */
221 0, /* rightshift */
411e1bfb
AM
222 2, /* size (0 = byte, 1 = short, 2 = long) */
223 32, /* bitsize */
b34976b6 224 FALSE, /* pc_relative */
5bd4f169 225 0, /* bitpos */
f5e87a1d 226 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
227 bfd_elf_generic_reloc, /* special_function */
228 "R_PPC64_NONE", /* name */
b34976b6 229 FALSE, /* partial_inplace */
d006db6c 230 0, /* src_mask */
5bd4f169 231 0, /* dst_mask */
b34976b6 232 FALSE), /* pcrel_offset */
5bd4f169
AM
233
234 /* A standard 32 bit relocation. */
235 HOWTO (R_PPC64_ADDR32, /* type */
236 0, /* rightshift */
237 2, /* size (0 = byte, 1 = short, 2 = long) */
238 32, /* bitsize */
b34976b6 239 FALSE, /* pc_relative */
5bd4f169
AM
240 0, /* bitpos */
241 complain_overflow_bitfield, /* complain_on_overflow */
242 bfd_elf_generic_reloc, /* special_function */
243 "R_PPC64_ADDR32", /* name */
b34976b6 244 FALSE, /* partial_inplace */
5bd4f169
AM
245 0, /* src_mask */
246 0xffffffff, /* dst_mask */
b34976b6 247 FALSE), /* pcrel_offset */
5bd4f169
AM
248
249 /* An absolute 26 bit branch; the lower two bits must be zero.
250 FIXME: we don't check that, we just clear them. */
251 HOWTO (R_PPC64_ADDR24, /* type */
252 0, /* rightshift */
253 2, /* size (0 = byte, 1 = short, 2 = long) */
254 26, /* bitsize */
b34976b6 255 FALSE, /* pc_relative */
5bd4f169
AM
256 0, /* bitpos */
257 complain_overflow_bitfield, /* complain_on_overflow */
258 bfd_elf_generic_reloc, /* special_function */
259 "R_PPC64_ADDR24", /* name */
b34976b6 260 FALSE, /* partial_inplace */
d006db6c 261 0, /* src_mask */
f5e87a1d 262 0x03fffffc, /* dst_mask */
b34976b6 263 FALSE), /* pcrel_offset */
5bd4f169
AM
264
265 /* A standard 16 bit relocation. */
266 HOWTO (R_PPC64_ADDR16, /* type */
267 0, /* rightshift */
268 1, /* size (0 = byte, 1 = short, 2 = long) */
269 16, /* bitsize */
b34976b6 270 FALSE, /* pc_relative */
5bd4f169
AM
271 0, /* bitpos */
272 complain_overflow_bitfield, /* complain_on_overflow */
273 bfd_elf_generic_reloc, /* special_function */
274 "R_PPC64_ADDR16", /* name */
b34976b6 275 FALSE, /* partial_inplace */
5bd4f169
AM
276 0, /* src_mask */
277 0xffff, /* dst_mask */
b34976b6 278 FALSE), /* pcrel_offset */
5bd4f169
AM
279
280 /* A 16 bit relocation without overflow. */
281 HOWTO (R_PPC64_ADDR16_LO, /* type */
282 0, /* rightshift */
283 1, /* size (0 = byte, 1 = short, 2 = long) */
284 16, /* bitsize */
b34976b6 285 FALSE, /* pc_relative */
5bd4f169
AM
286 0, /* bitpos */
287 complain_overflow_dont,/* complain_on_overflow */
288 bfd_elf_generic_reloc, /* special_function */
289 "R_PPC64_ADDR16_LO", /* name */
b34976b6 290 FALSE, /* partial_inplace */
5bd4f169
AM
291 0, /* src_mask */
292 0xffff, /* dst_mask */
b34976b6 293 FALSE), /* pcrel_offset */
5bd4f169
AM
294
295 /* Bits 16-31 of an address. */
296 HOWTO (R_PPC64_ADDR16_HI, /* type */
297 16, /* rightshift */
298 1, /* size (0 = byte, 1 = short, 2 = long) */
299 16, /* bitsize */
b34976b6 300 FALSE, /* pc_relative */
5bd4f169
AM
301 0, /* bitpos */
302 complain_overflow_dont, /* complain_on_overflow */
303 bfd_elf_generic_reloc, /* special_function */
304 "R_PPC64_ADDR16_HI", /* name */
b34976b6 305 FALSE, /* partial_inplace */
5bd4f169
AM
306 0, /* src_mask */
307 0xffff, /* dst_mask */
b34976b6 308 FALSE), /* pcrel_offset */
5bd4f169
AM
309
310 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
311 bits, treated as a signed number, is negative. */
312 HOWTO (R_PPC64_ADDR16_HA, /* type */
313 16, /* rightshift */
314 1, /* size (0 = byte, 1 = short, 2 = long) */
315 16, /* bitsize */
b34976b6 316 FALSE, /* pc_relative */
5bd4f169
AM
317 0, /* bitpos */
318 complain_overflow_dont, /* complain_on_overflow */
805fc799 319 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 320 "R_PPC64_ADDR16_HA", /* name */
b34976b6 321 FALSE, /* partial_inplace */
5bd4f169
AM
322 0, /* src_mask */
323 0xffff, /* dst_mask */
b34976b6 324 FALSE), /* pcrel_offset */
5bd4f169
AM
325
326 /* An absolute 16 bit branch; the lower two bits must be zero.
327 FIXME: we don't check that, we just clear them. */
328 HOWTO (R_PPC64_ADDR14, /* type */
329 0, /* rightshift */
330 2, /* size (0 = byte, 1 = short, 2 = long) */
331 16, /* bitsize */
b34976b6 332 FALSE, /* pc_relative */
5bd4f169
AM
333 0, /* bitpos */
334 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 335 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 336 "R_PPC64_ADDR14", /* name */
b34976b6 337 FALSE, /* partial_inplace */
d006db6c 338 0, /* src_mask */
f5e87a1d 339 0x0000fffc, /* dst_mask */
b34976b6 340 FALSE), /* pcrel_offset */
5bd4f169
AM
341
342 /* An absolute 16 bit branch, for which bit 10 should be set to
343 indicate that the branch is expected to be taken. The lower two
344 bits must be zero. */
345 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
346 0, /* rightshift */
347 2, /* size (0 = byte, 1 = short, 2 = long) */
348 16, /* bitsize */
b34976b6 349 FALSE, /* pc_relative */
5bd4f169
AM
350 0, /* bitpos */
351 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 352 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 353 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 354 FALSE, /* partial_inplace */
d006db6c 355 0, /* src_mask */
f5e87a1d 356 0x0000fffc, /* dst_mask */
b34976b6 357 FALSE), /* pcrel_offset */
5bd4f169
AM
358
359 /* An absolute 16 bit branch, for which bit 10 should be set to
360 indicate that the branch is not expected to be taken. The lower
361 two bits must be zero. */
362 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
363 0, /* rightshift */
364 2, /* size (0 = byte, 1 = short, 2 = long) */
365 16, /* bitsize */
b34976b6 366 FALSE, /* pc_relative */
5bd4f169
AM
367 0, /* bitpos */
368 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 369 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 370 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 371 FALSE, /* partial_inplace */
d006db6c 372 0, /* src_mask */
f5e87a1d 373 0x0000fffc, /* dst_mask */
b34976b6 374 FALSE), /* pcrel_offset */
5bd4f169
AM
375
376 /* A relative 26 bit branch; the lower two bits must be zero. */
377 HOWTO (R_PPC64_REL24, /* type */
378 0, /* rightshift */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
380 26, /* bitsize */
b34976b6 381 TRUE, /* pc_relative */
5bd4f169
AM
382 0, /* bitpos */
383 complain_overflow_signed, /* complain_on_overflow */
2441e016 384 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 385 "R_PPC64_REL24", /* name */
b34976b6 386 FALSE, /* partial_inplace */
d006db6c 387 0, /* src_mask */
f5e87a1d 388 0x03fffffc, /* dst_mask */
b34976b6 389 TRUE), /* pcrel_offset */
5bd4f169
AM
390
391 /* A relative 16 bit branch; the lower two bits must be zero. */
392 HOWTO (R_PPC64_REL14, /* type */
393 0, /* rightshift */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
395 16, /* bitsize */
b34976b6 396 TRUE, /* pc_relative */
5bd4f169
AM
397 0, /* bitpos */
398 complain_overflow_signed, /* complain_on_overflow */
2441e016 399 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 400 "R_PPC64_REL14", /* name */
b34976b6 401 FALSE, /* partial_inplace */
d006db6c 402 0, /* src_mask */
f5e87a1d 403 0x0000fffc, /* dst_mask */
b34976b6 404 TRUE), /* pcrel_offset */
5bd4f169
AM
405
406 /* A relative 16 bit branch. Bit 10 should be set to indicate that
407 the branch is expected to be taken. The lower two bits must be
408 zero. */
409 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
410 0, /* rightshift */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
412 16, /* bitsize */
b34976b6 413 TRUE, /* pc_relative */
5bd4f169
AM
414 0, /* bitpos */
415 complain_overflow_signed, /* complain_on_overflow */
805fc799 416 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 417 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 418 FALSE, /* partial_inplace */
d006db6c 419 0, /* src_mask */
f5e87a1d 420 0x0000fffc, /* dst_mask */
b34976b6 421 TRUE), /* pcrel_offset */
5bd4f169
AM
422
423 /* A relative 16 bit branch. Bit 10 should be set to indicate that
424 the branch is not expected to be taken. The lower two bits must
425 be zero. */
426 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
427 0, /* rightshift */
428 2, /* size (0 = byte, 1 = short, 2 = long) */
429 16, /* bitsize */
b34976b6 430 TRUE, /* pc_relative */
5bd4f169
AM
431 0, /* bitpos */
432 complain_overflow_signed, /* complain_on_overflow */
805fc799 433 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 434 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 435 FALSE, /* partial_inplace */
d006db6c 436 0, /* src_mask */
f5e87a1d 437 0x0000fffc, /* dst_mask */
b34976b6 438 TRUE), /* pcrel_offset */
5bd4f169
AM
439
440 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
441 symbol. */
442 HOWTO (R_PPC64_GOT16, /* type */
443 0, /* rightshift */
444 1, /* size (0 = byte, 1 = short, 2 = long) */
445 16, /* bitsize */
b34976b6 446 FALSE, /* pc_relative */
5bd4f169
AM
447 0, /* bitpos */
448 complain_overflow_signed, /* complain_on_overflow */
805fc799 449 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 450 "R_PPC64_GOT16", /* name */
b34976b6 451 FALSE, /* partial_inplace */
5bd4f169
AM
452 0, /* src_mask */
453 0xffff, /* dst_mask */
b34976b6 454 FALSE), /* pcrel_offset */
5bd4f169
AM
455
456 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
457 the symbol. */
458 HOWTO (R_PPC64_GOT16_LO, /* type */
459 0, /* rightshift */
460 1, /* size (0 = byte, 1 = short, 2 = long) */
461 16, /* bitsize */
b34976b6 462 FALSE, /* pc_relative */
5bd4f169
AM
463 0, /* bitpos */
464 complain_overflow_dont, /* complain_on_overflow */
805fc799 465 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 466 "R_PPC64_GOT16_LO", /* name */
b34976b6 467 FALSE, /* partial_inplace */
5bd4f169
AM
468 0, /* src_mask */
469 0xffff, /* dst_mask */
b34976b6 470 FALSE), /* pcrel_offset */
5bd4f169
AM
471
472 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
473 the symbol. */
474 HOWTO (R_PPC64_GOT16_HI, /* type */
475 16, /* rightshift */
476 1, /* size (0 = byte, 1 = short, 2 = long) */
477 16, /* bitsize */
b34976b6 478 FALSE, /* pc_relative */
5bd4f169
AM
479 0, /* bitpos */
480 complain_overflow_dont,/* complain_on_overflow */
805fc799 481 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 482 "R_PPC64_GOT16_HI", /* name */
b34976b6 483 FALSE, /* partial_inplace */
5bd4f169
AM
484 0, /* src_mask */
485 0xffff, /* dst_mask */
b34976b6 486 FALSE), /* pcrel_offset */
5bd4f169
AM
487
488 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
489 the symbol. */
490 HOWTO (R_PPC64_GOT16_HA, /* type */
491 16, /* rightshift */
492 1, /* size (0 = byte, 1 = short, 2 = long) */
493 16, /* bitsize */
b34976b6 494 FALSE, /* pc_relative */
5bd4f169
AM
495 0, /* bitpos */
496 complain_overflow_dont,/* complain_on_overflow */
805fc799 497 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 498 "R_PPC64_GOT16_HA", /* name */
b34976b6 499 FALSE, /* partial_inplace */
5bd4f169
AM
500 0, /* src_mask */
501 0xffff, /* dst_mask */
b34976b6 502 FALSE), /* pcrel_offset */
5bd4f169
AM
503
504 /* This is used only by the dynamic linker. The symbol should exist
505 both in the object being run and in some shared library. The
506 dynamic linker copies the data addressed by the symbol from the
507 shared library into the object, because the object being
508 run has to have the data at some particular address. */
509 HOWTO (R_PPC64_COPY, /* type */
510 0, /* rightshift */
f5e87a1d
AM
511 0, /* this one is variable size */
512 0, /* bitsize */
b34976b6 513 FALSE, /* pc_relative */
5bd4f169 514 0, /* bitpos */
f5e87a1d
AM
515 complain_overflow_dont, /* complain_on_overflow */
516 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 517 "R_PPC64_COPY", /* name */
b34976b6 518 FALSE, /* partial_inplace */
5bd4f169
AM
519 0, /* src_mask */
520 0, /* dst_mask */
b34976b6 521 FALSE), /* pcrel_offset */
5bd4f169
AM
522
523 /* Like R_PPC64_ADDR64, but used when setting global offset table
524 entries. */
525 HOWTO (R_PPC64_GLOB_DAT, /* type */
526 0, /* rightshift */
527 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
528 64, /* bitsize */
b34976b6 529 FALSE, /* pc_relative */
5bd4f169
AM
530 0, /* bitpos */
531 complain_overflow_dont, /* complain_on_overflow */
805fc799 532 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 533 "R_PPC64_GLOB_DAT", /* name */
b34976b6 534 FALSE, /* partial_inplace */
5bd4f169 535 0, /* src_mask */
f5e87a1d 536 ONES (64), /* dst_mask */
b34976b6 537 FALSE), /* pcrel_offset */
5bd4f169
AM
538
539 /* Created by the link editor. Marks a procedure linkage table
540 entry for a symbol. */
541 HOWTO (R_PPC64_JMP_SLOT, /* type */
542 0, /* rightshift */
543 0, /* size (0 = byte, 1 = short, 2 = long) */
544 0, /* bitsize */
b34976b6 545 FALSE, /* pc_relative */
5bd4f169
AM
546 0, /* bitpos */
547 complain_overflow_dont, /* complain_on_overflow */
805fc799 548 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 549 "R_PPC64_JMP_SLOT", /* name */
b34976b6 550 FALSE, /* partial_inplace */
5bd4f169
AM
551 0, /* src_mask */
552 0, /* dst_mask */
b34976b6 553 FALSE), /* pcrel_offset */
5bd4f169
AM
554
555 /* Used only by the dynamic linker. When the object is run, this
556 doubleword64 is set to the load address of the object, plus the
557 addend. */
558 HOWTO (R_PPC64_RELATIVE, /* type */
559 0, /* rightshift */
560 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
561 64, /* bitsize */
b34976b6 562 FALSE, /* pc_relative */
5bd4f169
AM
563 0, /* bitpos */
564 complain_overflow_dont, /* complain_on_overflow */
565 bfd_elf_generic_reloc, /* special_function */
566 "R_PPC64_RELATIVE", /* name */
b34976b6 567 FALSE, /* partial_inplace */
5bd4f169 568 0, /* src_mask */
f5e87a1d 569 ONES (64), /* dst_mask */
b34976b6 570 FALSE), /* pcrel_offset */
5bd4f169
AM
571
572 /* Like R_PPC64_ADDR32, but may be unaligned. */
573 HOWTO (R_PPC64_UADDR32, /* type */
574 0, /* rightshift */
575 2, /* size (0 = byte, 1 = short, 2 = long) */
576 32, /* bitsize */
b34976b6 577 FALSE, /* pc_relative */
5bd4f169
AM
578 0, /* bitpos */
579 complain_overflow_bitfield, /* complain_on_overflow */
580 bfd_elf_generic_reloc, /* special_function */
581 "R_PPC64_UADDR32", /* name */
b34976b6 582 FALSE, /* partial_inplace */
5bd4f169
AM
583 0, /* src_mask */
584 0xffffffff, /* dst_mask */
b34976b6 585 FALSE), /* pcrel_offset */
5bd4f169
AM
586
587 /* Like R_PPC64_ADDR16, but may be unaligned. */
588 HOWTO (R_PPC64_UADDR16, /* type */
589 0, /* rightshift */
590 1, /* size (0 = byte, 1 = short, 2 = long) */
591 16, /* bitsize */
b34976b6 592 FALSE, /* pc_relative */
5bd4f169
AM
593 0, /* bitpos */
594 complain_overflow_bitfield, /* complain_on_overflow */
595 bfd_elf_generic_reloc, /* special_function */
596 "R_PPC64_UADDR16", /* name */
b34976b6 597 FALSE, /* partial_inplace */
5bd4f169
AM
598 0, /* src_mask */
599 0xffff, /* dst_mask */
b34976b6 600 FALSE), /* pcrel_offset */
5bd4f169
AM
601
602 /* 32-bit PC relative. */
603 HOWTO (R_PPC64_REL32, /* type */
604 0, /* rightshift */
605 2, /* size (0 = byte, 1 = short, 2 = long) */
606 32, /* bitsize */
b34976b6 607 TRUE, /* pc_relative */
5bd4f169 608 0, /* bitpos */
cedb70c5 609 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
610 complain_overflow_signed, /* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_PPC64_REL32", /* name */
b34976b6 613 FALSE, /* partial_inplace */
5bd4f169
AM
614 0, /* src_mask */
615 0xffffffff, /* dst_mask */
b34976b6 616 TRUE), /* pcrel_offset */
5bd4f169 617
10ed1bba 618 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
619 HOWTO (R_PPC64_PLT32, /* type */
620 0, /* rightshift */
621 2, /* size (0 = byte, 1 = short, 2 = long) */
622 32, /* bitsize */
b34976b6 623 FALSE, /* pc_relative */
5bd4f169
AM
624 0, /* bitpos */
625 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 626 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 627 "R_PPC64_PLT32", /* name */
b34976b6 628 FALSE, /* partial_inplace */
5bd4f169 629 0, /* src_mask */
f5e87a1d 630 0xffffffff, /* dst_mask */
b34976b6 631 FALSE), /* pcrel_offset */
5bd4f169
AM
632
633 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
634 FIXME: R_PPC64_PLTREL32 not supported. */
635 HOWTO (R_PPC64_PLTREL32, /* type */
636 0, /* rightshift */
637 2, /* size (0 = byte, 1 = short, 2 = long) */
638 32, /* bitsize */
b34976b6 639 TRUE, /* pc_relative */
5bd4f169
AM
640 0, /* bitpos */
641 complain_overflow_signed, /* complain_on_overflow */
642 bfd_elf_generic_reloc, /* special_function */
643 "R_PPC64_PLTREL32", /* name */
b34976b6 644 FALSE, /* partial_inplace */
5bd4f169 645 0, /* src_mask */
f5e87a1d 646 0xffffffff, /* dst_mask */
b34976b6 647 TRUE), /* pcrel_offset */
5bd4f169
AM
648
649 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
650 the symbol. */
651 HOWTO (R_PPC64_PLT16_LO, /* type */
652 0, /* rightshift */
653 1, /* size (0 = byte, 1 = short, 2 = long) */
654 16, /* bitsize */
b34976b6 655 FALSE, /* pc_relative */
5bd4f169
AM
656 0, /* bitpos */
657 complain_overflow_dont, /* complain_on_overflow */
805fc799 658 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 659 "R_PPC64_PLT16_LO", /* name */
b34976b6 660 FALSE, /* partial_inplace */
5bd4f169
AM
661 0, /* src_mask */
662 0xffff, /* dst_mask */
b34976b6 663 FALSE), /* pcrel_offset */
5bd4f169
AM
664
665 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
666 the symbol. */
667 HOWTO (R_PPC64_PLT16_HI, /* type */
668 16, /* rightshift */
669 1, /* size (0 = byte, 1 = short, 2 = long) */
670 16, /* bitsize */
b34976b6 671 FALSE, /* pc_relative */
5bd4f169
AM
672 0, /* bitpos */
673 complain_overflow_dont, /* complain_on_overflow */
805fc799 674 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 675 "R_PPC64_PLT16_HI", /* name */
b34976b6 676 FALSE, /* partial_inplace */
5bd4f169
AM
677 0, /* src_mask */
678 0xffff, /* dst_mask */
b34976b6 679 FALSE), /* pcrel_offset */
5bd4f169
AM
680
681 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
682 the symbol. */
683 HOWTO (R_PPC64_PLT16_HA, /* type */
684 16, /* rightshift */
685 1, /* size (0 = byte, 1 = short, 2 = long) */
686 16, /* bitsize */
b34976b6 687 FALSE, /* pc_relative */
5bd4f169
AM
688 0, /* bitpos */
689 complain_overflow_dont, /* complain_on_overflow */
805fc799 690 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 691 "R_PPC64_PLT16_HA", /* name */
b34976b6 692 FALSE, /* partial_inplace */
5bd4f169
AM
693 0, /* src_mask */
694 0xffff, /* dst_mask */
b34976b6 695 FALSE), /* pcrel_offset */
5bd4f169 696
c061c2d8 697 /* 16-bit section relative relocation. */
5bd4f169
AM
698 HOWTO (R_PPC64_SECTOFF, /* type */
699 0, /* rightshift */
c061c2d8
AM
700 1, /* size (0 = byte, 1 = short, 2 = long) */
701 16, /* bitsize */
b34976b6 702 FALSE, /* pc_relative */
5bd4f169
AM
703 0, /* bitpos */
704 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 705 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 706 "R_PPC64_SECTOFF", /* name */
b34976b6 707 FALSE, /* partial_inplace */
5bd4f169 708 0, /* src_mask */
c061c2d8 709 0xffff, /* dst_mask */
b34976b6 710 FALSE), /* pcrel_offset */
5bd4f169 711
c061c2d8 712 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
713 HOWTO (R_PPC64_SECTOFF_LO, /* type */
714 0, /* rightshift */
715 1, /* size (0 = byte, 1 = short, 2 = long) */
716 16, /* bitsize */
b34976b6 717 FALSE, /* pc_relative */
5bd4f169
AM
718 0, /* bitpos */
719 complain_overflow_dont, /* complain_on_overflow */
805fc799 720 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 721 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 722 FALSE, /* partial_inplace */
5bd4f169
AM
723 0, /* src_mask */
724 0xffff, /* dst_mask */
b34976b6 725 FALSE), /* pcrel_offset */
5bd4f169
AM
726
727 /* 16-bit upper half section relative relocation. */
728 HOWTO (R_PPC64_SECTOFF_HI, /* type */
729 16, /* rightshift */
730 1, /* size (0 = byte, 1 = short, 2 = long) */
731 16, /* bitsize */
b34976b6 732 FALSE, /* pc_relative */
5bd4f169
AM
733 0, /* bitpos */
734 complain_overflow_dont, /* complain_on_overflow */
805fc799 735 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 736 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 737 FALSE, /* partial_inplace */
5bd4f169
AM
738 0, /* src_mask */
739 0xffff, /* dst_mask */
b34976b6 740 FALSE), /* pcrel_offset */
5bd4f169
AM
741
742 /* 16-bit upper half adjusted section relative relocation. */
743 HOWTO (R_PPC64_SECTOFF_HA, /* type */
744 16, /* rightshift */
745 1, /* size (0 = byte, 1 = short, 2 = long) */
746 16, /* bitsize */
b34976b6 747 FALSE, /* pc_relative */
5bd4f169
AM
748 0, /* bitpos */
749 complain_overflow_dont, /* complain_on_overflow */
805fc799 750 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 751 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 752 FALSE, /* partial_inplace */
5bd4f169
AM
753 0, /* src_mask */
754 0xffff, /* dst_mask */
b34976b6 755 FALSE), /* pcrel_offset */
5bd4f169 756
04c9666a
AM
757 /* Like R_PPC64_REL24 without touching the two least significant bits. */
758 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
759 2, /* rightshift */
760 2, /* size (0 = byte, 1 = short, 2 = long) */
761 30, /* bitsize */
b34976b6 762 TRUE, /* pc_relative */
5bd4f169
AM
763 0, /* bitpos */
764 complain_overflow_dont, /* complain_on_overflow */
765 bfd_elf_generic_reloc, /* special_function */
04c9666a 766 "R_PPC64_REL30", /* name */
b34976b6 767 FALSE, /* partial_inplace */
d006db6c 768 0, /* src_mask */
5bd4f169 769 0xfffffffc, /* dst_mask */
b34976b6 770 TRUE), /* pcrel_offset */
5bd4f169
AM
771
772 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
773
774 /* A standard 64-bit relocation. */
775 HOWTO (R_PPC64_ADDR64, /* type */
776 0, /* rightshift */
777 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
778 64, /* bitsize */
b34976b6 779 FALSE, /* pc_relative */
5bd4f169
AM
780 0, /* bitpos */
781 complain_overflow_dont, /* complain_on_overflow */
782 bfd_elf_generic_reloc, /* special_function */
783 "R_PPC64_ADDR64", /* name */
b34976b6 784 FALSE, /* partial_inplace */
5bd4f169 785 0, /* src_mask */
f5e87a1d 786 ONES (64), /* dst_mask */
b34976b6 787 FALSE), /* pcrel_offset */
5bd4f169
AM
788
789 /* The bits 32-47 of an address. */
790 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
791 32, /* rightshift */
792 1, /* size (0 = byte, 1 = short, 2 = long) */
793 16, /* bitsize */
b34976b6 794 FALSE, /* pc_relative */
5bd4f169
AM
795 0, /* bitpos */
796 complain_overflow_dont, /* complain_on_overflow */
797 bfd_elf_generic_reloc, /* special_function */
798 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 799 FALSE, /* partial_inplace */
5bd4f169
AM
800 0, /* src_mask */
801 0xffff, /* dst_mask */
b34976b6 802 FALSE), /* pcrel_offset */
5bd4f169
AM
803
804 /* The bits 32-47 of an address, plus 1 if the contents of the low
805 16 bits, treated as a signed number, is negative. */
806 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
807 32, /* rightshift */
808 1, /* size (0 = byte, 1 = short, 2 = long) */
809 16, /* bitsize */
b34976b6 810 FALSE, /* pc_relative */
5bd4f169
AM
811 0, /* bitpos */
812 complain_overflow_dont, /* complain_on_overflow */
805fc799 813 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 814 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 815 FALSE, /* partial_inplace */
5bd4f169
AM
816 0, /* src_mask */
817 0xffff, /* dst_mask */
b34976b6 818 FALSE), /* pcrel_offset */
5bd4f169
AM
819
820 /* The bits 48-63 of an address. */
821 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
822 48, /* rightshift */
823 1, /* size (0 = byte, 1 = short, 2 = long) */
824 16, /* bitsize */
b34976b6 825 FALSE, /* pc_relative */
5bd4f169
AM
826 0, /* bitpos */
827 complain_overflow_dont, /* complain_on_overflow */
828 bfd_elf_generic_reloc, /* special_function */
829 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 830 FALSE, /* partial_inplace */
5bd4f169
AM
831 0, /* src_mask */
832 0xffff, /* dst_mask */
b34976b6 833 FALSE), /* pcrel_offset */
5bd4f169
AM
834
835 /* The bits 48-63 of an address, plus 1 if the contents of the low
836 16 bits, treated as a signed number, is negative. */
837 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
838 48, /* rightshift */
839 1, /* size (0 = byte, 1 = short, 2 = long) */
840 16, /* bitsize */
b34976b6 841 FALSE, /* pc_relative */
5bd4f169
AM
842 0, /* bitpos */
843 complain_overflow_dont, /* complain_on_overflow */
805fc799 844 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 845 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 846 FALSE, /* partial_inplace */
5bd4f169
AM
847 0, /* src_mask */
848 0xffff, /* dst_mask */
b34976b6 849 FALSE), /* pcrel_offset */
5bd4f169
AM
850
851 /* Like ADDR64, but may be unaligned. */
852 HOWTO (R_PPC64_UADDR64, /* type */
853 0, /* rightshift */
854 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
855 64, /* bitsize */
b34976b6 856 FALSE, /* pc_relative */
5bd4f169
AM
857 0, /* bitpos */
858 complain_overflow_dont, /* complain_on_overflow */
859 bfd_elf_generic_reloc, /* special_function */
860 "R_PPC64_UADDR64", /* name */
b34976b6 861 FALSE, /* partial_inplace */
5bd4f169 862 0, /* src_mask */
f5e87a1d 863 ONES (64), /* dst_mask */
b34976b6 864 FALSE), /* pcrel_offset */
5bd4f169
AM
865
866 /* 64-bit relative relocation. */
867 HOWTO (R_PPC64_REL64, /* type */
868 0, /* rightshift */
869 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
870 64, /* bitsize */
b34976b6 871 TRUE, /* pc_relative */
5bd4f169
AM
872 0, /* bitpos */
873 complain_overflow_dont, /* complain_on_overflow */
874 bfd_elf_generic_reloc, /* special_function */
875 "R_PPC64_REL64", /* name */
b34976b6 876 FALSE, /* partial_inplace */
5bd4f169 877 0, /* src_mask */
f5e87a1d 878 ONES (64), /* dst_mask */
b34976b6 879 TRUE), /* pcrel_offset */
5bd4f169 880
cedb70c5 881 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
882 HOWTO (R_PPC64_PLT64, /* type */
883 0, /* rightshift */
884 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
885 64, /* bitsize */
b34976b6 886 FALSE, /* pc_relative */
5bd4f169
AM
887 0, /* bitpos */
888 complain_overflow_dont, /* complain_on_overflow */
805fc799 889 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 890 "R_PPC64_PLT64", /* name */
b34976b6 891 FALSE, /* partial_inplace */
5bd4f169 892 0, /* src_mask */
f5e87a1d 893 ONES (64), /* dst_mask */
b34976b6 894 FALSE), /* pcrel_offset */
5bd4f169
AM
895
896 /* 64-bit PC relative relocation to the symbol's procedure linkage
897 table. */
898 /* FIXME: R_PPC64_PLTREL64 not supported. */
899 HOWTO (R_PPC64_PLTREL64, /* type */
900 0, /* rightshift */
901 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
902 64, /* bitsize */
b34976b6 903 TRUE, /* pc_relative */
5bd4f169
AM
904 0, /* bitpos */
905 complain_overflow_dont, /* complain_on_overflow */
805fc799 906 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 907 "R_PPC64_PLTREL64", /* name */
b34976b6 908 FALSE, /* partial_inplace */
5bd4f169 909 0, /* src_mask */
f5e87a1d 910 ONES (64), /* dst_mask */
b34976b6 911 TRUE), /* pcrel_offset */
5bd4f169
AM
912
913 /* 16 bit TOC-relative relocation. */
914
915 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
916 HOWTO (R_PPC64_TOC16, /* type */
917 0, /* rightshift */
918 1, /* size (0 = byte, 1 = short, 2 = long) */
919 16, /* bitsize */
b34976b6 920 FALSE, /* pc_relative */
5bd4f169
AM
921 0, /* bitpos */
922 complain_overflow_signed, /* complain_on_overflow */
805fc799 923 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 924 "R_PPC64_TOC16", /* name */
b34976b6 925 FALSE, /* partial_inplace */
5bd4f169
AM
926 0, /* src_mask */
927 0xffff, /* dst_mask */
b34976b6 928 FALSE), /* pcrel_offset */
5bd4f169
AM
929
930 /* 16 bit TOC-relative relocation without overflow. */
931
932 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
933 HOWTO (R_PPC64_TOC16_LO, /* type */
934 0, /* rightshift */
935 1, /* size (0 = byte, 1 = short, 2 = long) */
936 16, /* bitsize */
b34976b6 937 FALSE, /* pc_relative */
5bd4f169
AM
938 0, /* bitpos */
939 complain_overflow_dont, /* complain_on_overflow */
805fc799 940 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 941 "R_PPC64_TOC16_LO", /* name */
b34976b6 942 FALSE, /* partial_inplace */
5bd4f169
AM
943 0, /* src_mask */
944 0xffff, /* dst_mask */
b34976b6 945 FALSE), /* pcrel_offset */
5bd4f169
AM
946
947 /* 16 bit TOC-relative relocation, high 16 bits. */
948
949 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
950 HOWTO (R_PPC64_TOC16_HI, /* type */
951 16, /* rightshift */
952 1, /* size (0 = byte, 1 = short, 2 = long) */
953 16, /* bitsize */
b34976b6 954 FALSE, /* pc_relative */
5bd4f169
AM
955 0, /* bitpos */
956 complain_overflow_dont, /* complain_on_overflow */
805fc799 957 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 958 "R_PPC64_TOC16_HI", /* name */
b34976b6 959 FALSE, /* partial_inplace */
5bd4f169
AM
960 0, /* src_mask */
961 0xffff, /* dst_mask */
b34976b6 962 FALSE), /* pcrel_offset */
5bd4f169
AM
963
964 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
965 contents of the low 16 bits, treated as a signed number, is
966 negative. */
967
968 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
969 HOWTO (R_PPC64_TOC16_HA, /* type */
970 16, /* rightshift */
971 1, /* size (0 = byte, 1 = short, 2 = long) */
972 16, /* bitsize */
b34976b6 973 FALSE, /* pc_relative */
5bd4f169
AM
974 0, /* bitpos */
975 complain_overflow_dont, /* complain_on_overflow */
805fc799 976 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 977 "R_PPC64_TOC16_HA", /* name */
b34976b6 978 FALSE, /* partial_inplace */
5bd4f169
AM
979 0, /* src_mask */
980 0xffff, /* dst_mask */
b34976b6 981 FALSE), /* pcrel_offset */
5bd4f169
AM
982
983 /* 64-bit relocation; insert value of TOC base (.TOC.). */
984
985 /* R_PPC64_TOC 51 doubleword64 .TOC. */
986 HOWTO (R_PPC64_TOC, /* type */
987 0, /* rightshift */
988 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
989 64, /* bitsize */
b34976b6 990 FALSE, /* pc_relative */
5bd4f169
AM
991 0, /* bitpos */
992 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 993 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 994 "R_PPC64_TOC", /* name */
b34976b6 995 FALSE, /* partial_inplace */
5bd4f169 996 0, /* src_mask */
f5e87a1d 997 ONES (64), /* dst_mask */
b34976b6 998 FALSE), /* pcrel_offset */
5bd4f169
AM
999
1000 /* Like R_PPC64_GOT16, but also informs the link editor that the
1001 value to relocate may (!) refer to a PLT entry which the link
1002 editor (a) may replace with the symbol value. If the link editor
1003 is unable to fully resolve the symbol, it may (b) create a PLT
1004 entry and store the address to the new PLT entry in the GOT.
1005 This permits lazy resolution of function symbols at run time.
1006 The link editor may also skip all of this and just (c) emit a
1007 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1008 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1009 HOWTO (R_PPC64_PLTGOT16, /* type */
1010 0, /* rightshift */
1011 1, /* size (0 = byte, 1 = short, 2 = long) */
1012 16, /* bitsize */
b34976b6 1013 FALSE, /* pc_relative */
5bd4f169
AM
1014 0, /* bitpos */
1015 complain_overflow_signed, /* complain_on_overflow */
805fc799 1016 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1017 "R_PPC64_PLTGOT16", /* name */
1018 FALSE, /* partial_inplace */
1019 0, /* src_mask */
1020 0xffff, /* dst_mask */
1021 FALSE), /* pcrel_offset */
1022
1023 /* Like R_PPC64_PLTGOT16, but without overflow. */
1024 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1025 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1026 0, /* rightshift */
1027 1, /* size (0 = byte, 1 = short, 2 = long) */
1028 16, /* bitsize */
1029 FALSE, /* pc_relative */
1030 0, /* bitpos */
1031 complain_overflow_dont, /* complain_on_overflow */
1032 ppc64_elf_unhandled_reloc, /* special_function */
1033 "R_PPC64_PLTGOT16_LO", /* name */
1034 FALSE, /* partial_inplace */
1035 0, /* src_mask */
1036 0xffff, /* dst_mask */
1037 FALSE), /* pcrel_offset */
1038
1039 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1040 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1041 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1042 16, /* rightshift */
1043 1, /* size (0 = byte, 1 = short, 2 = long) */
1044 16, /* bitsize */
1045 FALSE, /* pc_relative */
1046 0, /* bitpos */
1047 complain_overflow_dont, /* complain_on_overflow */
1048 ppc64_elf_unhandled_reloc, /* special_function */
1049 "R_PPC64_PLTGOT16_HI", /* name */
1050 FALSE, /* partial_inplace */
1051 0, /* src_mask */
1052 0xffff, /* dst_mask */
1053 FALSE), /* pcrel_offset */
1054
1055 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1056 1 if the contents of the low 16 bits, treated as a signed number,
1057 is negative. */
1058 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1059 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1060 16, /* rightshift */
1061 1, /* size (0 = byte, 1 = short, 2 = long) */
1062 16, /* bitsize */
1063 FALSE, /* pc_relative */
1064 0, /* bitpos */
1065 complain_overflow_dont,/* complain_on_overflow */
1066 ppc64_elf_unhandled_reloc, /* special_function */
1067 "R_PPC64_PLTGOT16_HA", /* name */
1068 FALSE, /* partial_inplace */
1069 0, /* src_mask */
1070 0xffff, /* dst_mask */
1071 FALSE), /* pcrel_offset */
1072
1073 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1074 HOWTO (R_PPC64_ADDR16_DS, /* type */
1075 0, /* rightshift */
1076 1, /* size (0 = byte, 1 = short, 2 = long) */
1077 16, /* bitsize */
1078 FALSE, /* pc_relative */
1079 0, /* bitpos */
1080 complain_overflow_bitfield, /* complain_on_overflow */
1081 bfd_elf_generic_reloc, /* special_function */
1082 "R_PPC64_ADDR16_DS", /* name */
1083 FALSE, /* partial_inplace */
1084 0, /* src_mask */
1085 0xfffc, /* dst_mask */
1086 FALSE), /* pcrel_offset */
1087
1088 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1089 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1090 0, /* rightshift */
1091 1, /* size (0 = byte, 1 = short, 2 = long) */
1092 16, /* bitsize */
1093 FALSE, /* pc_relative */
1094 0, /* bitpos */
1095 complain_overflow_dont,/* complain_on_overflow */
1096 bfd_elf_generic_reloc, /* special_function */
1097 "R_PPC64_ADDR16_LO_DS",/* name */
1098 FALSE, /* partial_inplace */
1099 0, /* src_mask */
1100 0xfffc, /* dst_mask */
1101 FALSE), /* pcrel_offset */
1102
1103 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1104 HOWTO (R_PPC64_GOT16_DS, /* type */
1105 0, /* rightshift */
1106 1, /* size (0 = byte, 1 = short, 2 = long) */
1107 16, /* bitsize */
1108 FALSE, /* pc_relative */
1109 0, /* bitpos */
1110 complain_overflow_signed, /* complain_on_overflow */
1111 ppc64_elf_unhandled_reloc, /* special_function */
1112 "R_PPC64_GOT16_DS", /* name */
1113 FALSE, /* partial_inplace */
1114 0, /* src_mask */
1115 0xfffc, /* dst_mask */
1116 FALSE), /* pcrel_offset */
1117
1118 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1119 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1120 0, /* rightshift */
1121 1, /* size (0 = byte, 1 = short, 2 = long) */
1122 16, /* bitsize */
1123 FALSE, /* pc_relative */
1124 0, /* bitpos */
1125 complain_overflow_dont, /* complain_on_overflow */
1126 ppc64_elf_unhandled_reloc, /* special_function */
1127 "R_PPC64_GOT16_LO_DS", /* name */
1128 FALSE, /* partial_inplace */
1129 0, /* src_mask */
1130 0xfffc, /* dst_mask */
1131 FALSE), /* pcrel_offset */
1132
1133 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1134 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1135 0, /* rightshift */
1136 1, /* size (0 = byte, 1 = short, 2 = long) */
1137 16, /* bitsize */
1138 FALSE, /* pc_relative */
1139 0, /* bitpos */
1140 complain_overflow_dont, /* complain_on_overflow */
1141 ppc64_elf_unhandled_reloc, /* special_function */
1142 "R_PPC64_PLT16_LO_DS", /* name */
1143 FALSE, /* partial_inplace */
1144 0, /* src_mask */
1145 0xfffc, /* dst_mask */
1146 FALSE), /* pcrel_offset */
1147
1148 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1149 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1150 0, /* rightshift */
1151 1, /* size (0 = byte, 1 = short, 2 = long) */
1152 16, /* bitsize */
1153 FALSE, /* pc_relative */
1154 0, /* bitpos */
1155 complain_overflow_bitfield, /* complain_on_overflow */
1156 ppc64_elf_sectoff_reloc, /* special_function */
1157 "R_PPC64_SECTOFF_DS", /* name */
1158 FALSE, /* partial_inplace */
1159 0, /* src_mask */
1160 0xfffc, /* dst_mask */
1161 FALSE), /* pcrel_offset */
1162
1163 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1164 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1165 0, /* rightshift */
1166 1, /* size (0 = byte, 1 = short, 2 = long) */
1167 16, /* bitsize */
1168 FALSE, /* pc_relative */
1169 0, /* bitpos */
1170 complain_overflow_dont, /* complain_on_overflow */
1171 ppc64_elf_sectoff_reloc, /* special_function */
1172 "R_PPC64_SECTOFF_LO_DS",/* name */
1173 FALSE, /* partial_inplace */
1174 0, /* src_mask */
1175 0xfffc, /* dst_mask */
1176 FALSE), /* pcrel_offset */
1177
1178 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1179 HOWTO (R_PPC64_TOC16_DS, /* type */
1180 0, /* rightshift */
1181 1, /* size (0 = byte, 1 = short, 2 = long) */
1182 16, /* bitsize */
1183 FALSE, /* pc_relative */
1184 0, /* bitpos */
1185 complain_overflow_signed, /* complain_on_overflow */
1186 ppc64_elf_toc_reloc, /* special_function */
1187 "R_PPC64_TOC16_DS", /* name */
1188 FALSE, /* partial_inplace */
1189 0, /* src_mask */
1190 0xfffc, /* dst_mask */
1191 FALSE), /* pcrel_offset */
1192
1193 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1194 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1195 0, /* rightshift */
1196 1, /* size (0 = byte, 1 = short, 2 = long) */
1197 16, /* bitsize */
1198 FALSE, /* pc_relative */
1199 0, /* bitpos */
1200 complain_overflow_dont, /* complain_on_overflow */
1201 ppc64_elf_toc_reloc, /* special_function */
1202 "R_PPC64_TOC16_LO_DS", /* name */
1203 FALSE, /* partial_inplace */
1204 0, /* src_mask */
1205 0xfffc, /* dst_mask */
1206 FALSE), /* pcrel_offset */
1207
1208 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1209 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1210 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1211 0, /* rightshift */
1212 1, /* size (0 = byte, 1 = short, 2 = long) */
1213 16, /* bitsize */
1214 FALSE, /* pc_relative */
1215 0, /* bitpos */
1216 complain_overflow_signed, /* complain_on_overflow */
1217 ppc64_elf_unhandled_reloc, /* special_function */
1218 "R_PPC64_PLTGOT16_DS", /* name */
1219 FALSE, /* partial_inplace */
1220 0, /* src_mask */
1221 0xfffc, /* dst_mask */
1222 FALSE), /* pcrel_offset */
1223
1224 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1225 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1226 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1227 0, /* rightshift */
1228 1, /* size (0 = byte, 1 = short, 2 = long) */
1229 16, /* bitsize */
1230 FALSE, /* pc_relative */
1231 0, /* bitpos */
1232 complain_overflow_dont, /* complain_on_overflow */
1233 ppc64_elf_unhandled_reloc, /* special_function */
1234 "R_PPC64_PLTGOT16_LO_DS",/* name */
1235 FALSE, /* partial_inplace */
1236 0, /* src_mask */
1237 0xfffc, /* dst_mask */
1238 FALSE), /* pcrel_offset */
1239
727fc41e 1240 /* Marker relocs for TLS. */
411e1bfb
AM
1241 HOWTO (R_PPC64_TLS,
1242 0, /* rightshift */
1243 2, /* size (0 = byte, 1 = short, 2 = long) */
1244 32, /* bitsize */
1245 FALSE, /* pc_relative */
1246 0, /* bitpos */
1247 complain_overflow_dont, /* complain_on_overflow */
1248 bfd_elf_generic_reloc, /* special_function */
1249 "R_PPC64_TLS", /* name */
1250 FALSE, /* partial_inplace */
1251 0, /* src_mask */
1252 0, /* dst_mask */
1253 FALSE), /* pcrel_offset */
1254
727fc41e
AM
1255 HOWTO (R_PPC64_TLSGD,
1256 0, /* rightshift */
1257 2, /* size (0 = byte, 1 = short, 2 = long) */
1258 32, /* bitsize */
1259 FALSE, /* pc_relative */
1260 0, /* bitpos */
1261 complain_overflow_dont, /* complain_on_overflow */
1262 bfd_elf_generic_reloc, /* special_function */
1263 "R_PPC64_TLSGD", /* name */
1264 FALSE, /* partial_inplace */
1265 0, /* src_mask */
1266 0, /* dst_mask */
1267 FALSE), /* pcrel_offset */
1268
1269 HOWTO (R_PPC64_TLSLD,
1270 0, /* rightshift */
1271 2, /* size (0 = byte, 1 = short, 2 = long) */
1272 32, /* bitsize */
1273 FALSE, /* pc_relative */
1274 0, /* bitpos */
1275 complain_overflow_dont, /* complain_on_overflow */
1276 bfd_elf_generic_reloc, /* special_function */
1277 "R_PPC64_TLSLD", /* name */
1278 FALSE, /* partial_inplace */
1279 0, /* src_mask */
1280 0, /* dst_mask */
1281 FALSE), /* pcrel_offset */
1282
411e1bfb
AM
1283 /* Computes the load module index of the load module that contains the
1284 definition of its TLS sym. */
1285 HOWTO (R_PPC64_DTPMOD64,
1286 0, /* rightshift */
1287 4, /* size (0 = byte, 1 = short, 2 = long) */
1288 64, /* bitsize */
1289 FALSE, /* pc_relative */
1290 0, /* bitpos */
1291 complain_overflow_dont, /* complain_on_overflow */
1292 ppc64_elf_unhandled_reloc, /* special_function */
1293 "R_PPC64_DTPMOD64", /* name */
1294 FALSE, /* partial_inplace */
1295 0, /* src_mask */
1296 ONES (64), /* dst_mask */
1297 FALSE), /* pcrel_offset */
1298
1299 /* Computes a dtv-relative displacement, the difference between the value
1300 of sym+add and the base address of the thread-local storage block that
1301 contains the definition of sym, minus 0x8000. */
1302 HOWTO (R_PPC64_DTPREL64,
1303 0, /* rightshift */
1304 4, /* size (0 = byte, 1 = short, 2 = long) */
1305 64, /* bitsize */
1306 FALSE, /* pc_relative */
1307 0, /* bitpos */
1308 complain_overflow_dont, /* complain_on_overflow */
1309 ppc64_elf_unhandled_reloc, /* special_function */
1310 "R_PPC64_DTPREL64", /* name */
1311 FALSE, /* partial_inplace */
1312 0, /* src_mask */
1313 ONES (64), /* dst_mask */
1314 FALSE), /* pcrel_offset */
1315
1316 /* A 16 bit dtprel reloc. */
1317 HOWTO (R_PPC64_DTPREL16,
1318 0, /* rightshift */
1319 1, /* size (0 = byte, 1 = short, 2 = long) */
1320 16, /* bitsize */
1321 FALSE, /* pc_relative */
1322 0, /* bitpos */
1323 complain_overflow_signed, /* complain_on_overflow */
1324 ppc64_elf_unhandled_reloc, /* special_function */
1325 "R_PPC64_DTPREL16", /* name */
1326 FALSE, /* partial_inplace */
1327 0, /* src_mask */
1328 0xffff, /* dst_mask */
1329 FALSE), /* pcrel_offset */
1330
1331 /* Like DTPREL16, but no overflow. */
1332 HOWTO (R_PPC64_DTPREL16_LO,
1333 0, /* rightshift */
1334 1, /* size (0 = byte, 1 = short, 2 = long) */
1335 16, /* bitsize */
1336 FALSE, /* pc_relative */
1337 0, /* bitpos */
1338 complain_overflow_dont, /* complain_on_overflow */
1339 ppc64_elf_unhandled_reloc, /* special_function */
1340 "R_PPC64_DTPREL16_LO", /* name */
1341 FALSE, /* partial_inplace */
1342 0, /* src_mask */
1343 0xffff, /* dst_mask */
1344 FALSE), /* pcrel_offset */
1345
1346 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1347 HOWTO (R_PPC64_DTPREL16_HI,
1348 16, /* rightshift */
1349 1, /* size (0 = byte, 1 = short, 2 = long) */
1350 16, /* bitsize */
1351 FALSE, /* pc_relative */
1352 0, /* bitpos */
1353 complain_overflow_dont, /* complain_on_overflow */
1354 ppc64_elf_unhandled_reloc, /* special_function */
1355 "R_PPC64_DTPREL16_HI", /* name */
1356 FALSE, /* partial_inplace */
1357 0, /* src_mask */
1358 0xffff, /* dst_mask */
1359 FALSE), /* pcrel_offset */
1360
1361 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1362 HOWTO (R_PPC64_DTPREL16_HA,
1363 16, /* rightshift */
1364 1, /* size (0 = byte, 1 = short, 2 = long) */
1365 16, /* bitsize */
1366 FALSE, /* pc_relative */
1367 0, /* bitpos */
1368 complain_overflow_dont, /* complain_on_overflow */
1369 ppc64_elf_unhandled_reloc, /* special_function */
1370 "R_PPC64_DTPREL16_HA", /* name */
1371 FALSE, /* partial_inplace */
1372 0, /* src_mask */
1373 0xffff, /* dst_mask */
1374 FALSE), /* pcrel_offset */
1375
1376 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1377 HOWTO (R_PPC64_DTPREL16_HIGHER,
1378 32, /* rightshift */
1379 1, /* size (0 = byte, 1 = short, 2 = long) */
1380 16, /* bitsize */
1381 FALSE, /* pc_relative */
1382 0, /* bitpos */
1383 complain_overflow_dont, /* complain_on_overflow */
1384 ppc64_elf_unhandled_reloc, /* special_function */
1385 "R_PPC64_DTPREL16_HIGHER", /* name */
1386 FALSE, /* partial_inplace */
1387 0, /* src_mask */
1388 0xffff, /* dst_mask */
1389 FALSE), /* pcrel_offset */
1390
1391 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1392 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1393 32, /* rightshift */
1394 1, /* size (0 = byte, 1 = short, 2 = long) */
1395 16, /* bitsize */
1396 FALSE, /* pc_relative */
1397 0, /* bitpos */
1398 complain_overflow_dont, /* complain_on_overflow */
1399 ppc64_elf_unhandled_reloc, /* special_function */
1400 "R_PPC64_DTPREL16_HIGHERA", /* name */
1401 FALSE, /* partial_inplace */
1402 0, /* src_mask */
1403 0xffff, /* dst_mask */
1404 FALSE), /* pcrel_offset */
1405
1406 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1407 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1408 48, /* rightshift */
1409 1, /* size (0 = byte, 1 = short, 2 = long) */
1410 16, /* bitsize */
1411 FALSE, /* pc_relative */
1412 0, /* bitpos */
1413 complain_overflow_dont, /* complain_on_overflow */
1414 ppc64_elf_unhandled_reloc, /* special_function */
1415 "R_PPC64_DTPREL16_HIGHEST", /* name */
1416 FALSE, /* partial_inplace */
1417 0, /* src_mask */
1418 0xffff, /* dst_mask */
1419 FALSE), /* pcrel_offset */
1420
1421 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1422 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1423 48, /* rightshift */
1424 1, /* size (0 = byte, 1 = short, 2 = long) */
1425 16, /* bitsize */
1426 FALSE, /* pc_relative */
1427 0, /* bitpos */
1428 complain_overflow_dont, /* complain_on_overflow */
1429 ppc64_elf_unhandled_reloc, /* special_function */
1430 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1431 FALSE, /* partial_inplace */
1432 0, /* src_mask */
1433 0xffff, /* dst_mask */
1434 FALSE), /* pcrel_offset */
1435
1436 /* Like DTPREL16, but for insns with a DS field. */
1437 HOWTO (R_PPC64_DTPREL16_DS,
1438 0, /* rightshift */
1439 1, /* size (0 = byte, 1 = short, 2 = long) */
1440 16, /* bitsize */
1441 FALSE, /* pc_relative */
1442 0, /* bitpos */
1443 complain_overflow_signed, /* complain_on_overflow */
1444 ppc64_elf_unhandled_reloc, /* special_function */
1445 "R_PPC64_DTPREL16_DS", /* name */
1446 FALSE, /* partial_inplace */
1447 0, /* src_mask */
1448 0xfffc, /* dst_mask */
1449 FALSE), /* pcrel_offset */
1450
1451 /* Like DTPREL16_DS, but no overflow. */
1452 HOWTO (R_PPC64_DTPREL16_LO_DS,
1453 0, /* rightshift */
1454 1, /* size (0 = byte, 1 = short, 2 = long) */
1455 16, /* bitsize */
1456 FALSE, /* pc_relative */
1457 0, /* bitpos */
1458 complain_overflow_dont, /* complain_on_overflow */
1459 ppc64_elf_unhandled_reloc, /* special_function */
1460 "R_PPC64_DTPREL16_LO_DS", /* name */
1461 FALSE, /* partial_inplace */
1462 0, /* src_mask */
1463 0xfffc, /* dst_mask */
1464 FALSE), /* pcrel_offset */
1465
1466 /* Computes a tp-relative displacement, the difference between the value of
1467 sym+add and the value of the thread pointer (r13). */
1468 HOWTO (R_PPC64_TPREL64,
1469 0, /* rightshift */
1470 4, /* size (0 = byte, 1 = short, 2 = long) */
1471 64, /* 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_TPREL64", /* name */
1477 FALSE, /* partial_inplace */
1478 0, /* src_mask */
1479 ONES (64), /* dst_mask */
1480 FALSE), /* pcrel_offset */
1481
1482 /* A 16 bit tprel reloc. */
1483 HOWTO (R_PPC64_TPREL16,
1484 0, /* rightshift */
1485 1, /* size (0 = byte, 1 = short, 2 = long) */
1486 16, /* bitsize */
1487 FALSE, /* pc_relative */
1488 0, /* bitpos */
1489 complain_overflow_signed, /* complain_on_overflow */
1490 ppc64_elf_unhandled_reloc, /* special_function */
1491 "R_PPC64_TPREL16", /* name */
1492 FALSE, /* partial_inplace */
1493 0, /* src_mask */
1494 0xffff, /* dst_mask */
1495 FALSE), /* pcrel_offset */
1496
1497 /* Like TPREL16, but no overflow. */
1498 HOWTO (R_PPC64_TPREL16_LO,
1499 0, /* rightshift */
1500 1, /* size (0 = byte, 1 = short, 2 = long) */
1501 16, /* bitsize */
1502 FALSE, /* pc_relative */
1503 0, /* bitpos */
1504 complain_overflow_dont, /* complain_on_overflow */
1505 ppc64_elf_unhandled_reloc, /* special_function */
1506 "R_PPC64_TPREL16_LO", /* name */
1507 FALSE, /* partial_inplace */
1508 0, /* src_mask */
1509 0xffff, /* dst_mask */
1510 FALSE), /* pcrel_offset */
1511
1512 /* Like TPREL16_LO, but next higher group of 16 bits. */
1513 HOWTO (R_PPC64_TPREL16_HI,
1514 16, /* rightshift */
1515 1, /* size (0 = byte, 1 = short, 2 = long) */
1516 16, /* bitsize */
1517 FALSE, /* pc_relative */
1518 0, /* bitpos */
1519 complain_overflow_dont, /* complain_on_overflow */
1520 ppc64_elf_unhandled_reloc, /* special_function */
1521 "R_PPC64_TPREL16_HI", /* name */
1522 FALSE, /* partial_inplace */
1523 0, /* src_mask */
1524 0xffff, /* dst_mask */
1525 FALSE), /* pcrel_offset */
1526
1527 /* Like TPREL16_HI, but adjust for low 16 bits. */
1528 HOWTO (R_PPC64_TPREL16_HA,
1529 16, /* rightshift */
1530 1, /* size (0 = byte, 1 = short, 2 = long) */
1531 16, /* bitsize */
1532 FALSE, /* pc_relative */
1533 0, /* bitpos */
1534 complain_overflow_dont, /* complain_on_overflow */
1535 ppc64_elf_unhandled_reloc, /* special_function */
1536 "R_PPC64_TPREL16_HA", /* name */
1537 FALSE, /* partial_inplace */
1538 0, /* src_mask */
1539 0xffff, /* dst_mask */
1540 FALSE), /* pcrel_offset */
1541
1542 /* Like TPREL16_HI, but next higher group of 16 bits. */
1543 HOWTO (R_PPC64_TPREL16_HIGHER,
1544 32, /* rightshift */
1545 1, /* size (0 = byte, 1 = short, 2 = long) */
1546 16, /* bitsize */
1547 FALSE, /* pc_relative */
1548 0, /* bitpos */
1549 complain_overflow_dont, /* complain_on_overflow */
1550 ppc64_elf_unhandled_reloc, /* special_function */
1551 "R_PPC64_TPREL16_HIGHER", /* name */
1552 FALSE, /* partial_inplace */
1553 0, /* src_mask */
1554 0xffff, /* dst_mask */
1555 FALSE), /* pcrel_offset */
1556
1557 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1558 HOWTO (R_PPC64_TPREL16_HIGHERA,
1559 32, /* rightshift */
1560 1, /* size (0 = byte, 1 = short, 2 = long) */
1561 16, /* bitsize */
1562 FALSE, /* pc_relative */
1563 0, /* bitpos */
1564 complain_overflow_dont, /* complain_on_overflow */
1565 ppc64_elf_unhandled_reloc, /* special_function */
1566 "R_PPC64_TPREL16_HIGHERA", /* name */
1567 FALSE, /* partial_inplace */
1568 0, /* src_mask */
1569 0xffff, /* dst_mask */
1570 FALSE), /* pcrel_offset */
1571
1572 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1573 HOWTO (R_PPC64_TPREL16_HIGHEST,
1574 48, /* rightshift */
1575 1, /* size (0 = byte, 1 = short, 2 = long) */
1576 16, /* bitsize */
1577 FALSE, /* pc_relative */
1578 0, /* bitpos */
1579 complain_overflow_dont, /* complain_on_overflow */
1580 ppc64_elf_unhandled_reloc, /* special_function */
1581 "R_PPC64_TPREL16_HIGHEST", /* name */
1582 FALSE, /* partial_inplace */
1583 0, /* src_mask */
1584 0xffff, /* dst_mask */
1585 FALSE), /* pcrel_offset */
1586
1587 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1588 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1589 48, /* rightshift */
1590 1, /* size (0 = byte, 1 = short, 2 = long) */
1591 16, /* bitsize */
1592 FALSE, /* pc_relative */
1593 0, /* bitpos */
1594 complain_overflow_dont, /* complain_on_overflow */
1595 ppc64_elf_unhandled_reloc, /* special_function */
1596 "R_PPC64_TPREL16_HIGHESTA", /* name */
1597 FALSE, /* partial_inplace */
1598 0, /* src_mask */
1599 0xffff, /* dst_mask */
1600 FALSE), /* pcrel_offset */
1601
1602 /* Like TPREL16, but for insns with a DS field. */
1603 HOWTO (R_PPC64_TPREL16_DS,
1604 0, /* rightshift */
1605 1, /* size (0 = byte, 1 = short, 2 = long) */
1606 16, /* bitsize */
1607 FALSE, /* pc_relative */
1608 0, /* bitpos */
1609 complain_overflow_signed, /* complain_on_overflow */
1610 ppc64_elf_unhandled_reloc, /* special_function */
1611 "R_PPC64_TPREL16_DS", /* name */
1612 FALSE, /* partial_inplace */
1613 0, /* src_mask */
1614 0xfffc, /* dst_mask */
1615 FALSE), /* pcrel_offset */
1616
1617 /* Like TPREL16_DS, but no overflow. */
1618 HOWTO (R_PPC64_TPREL16_LO_DS,
1619 0, /* rightshift */
1620 1, /* size (0 = byte, 1 = short, 2 = long) */
1621 16, /* bitsize */
1622 FALSE, /* pc_relative */
1623 0, /* bitpos */
1624 complain_overflow_dont, /* complain_on_overflow */
1625 ppc64_elf_unhandled_reloc, /* special_function */
1626 "R_PPC64_TPREL16_LO_DS", /* name */
1627 FALSE, /* partial_inplace */
1628 0, /* src_mask */
1629 0xfffc, /* dst_mask */
1630 FALSE), /* pcrel_offset */
1631
1632 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1633 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1634 to the first entry relative to the TOC base (r2). */
1635 HOWTO (R_PPC64_GOT_TLSGD16,
1636 0, /* rightshift */
1637 1, /* size (0 = byte, 1 = short, 2 = long) */
1638 16, /* bitsize */
1639 FALSE, /* pc_relative */
1640 0, /* bitpos */
1641 complain_overflow_signed, /* complain_on_overflow */
1642 ppc64_elf_unhandled_reloc, /* special_function */
1643 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1644 FALSE, /* partial_inplace */
5bd4f169
AM
1645 0, /* src_mask */
1646 0xffff, /* dst_mask */
b34976b6 1647 FALSE), /* pcrel_offset */
5bd4f169 1648
411e1bfb
AM
1649 /* Like GOT_TLSGD16, but no overflow. */
1650 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1651 0, /* rightshift */
1652 1, /* size (0 = byte, 1 = short, 2 = long) */
1653 16, /* bitsize */
b34976b6 1654 FALSE, /* pc_relative */
5bd4f169
AM
1655 0, /* bitpos */
1656 complain_overflow_dont, /* complain_on_overflow */
805fc799 1657 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1658 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1659 FALSE, /* partial_inplace */
5bd4f169
AM
1660 0, /* src_mask */
1661 0xffff, /* dst_mask */
b34976b6 1662 FALSE), /* pcrel_offset */
5bd4f169 1663
411e1bfb
AM
1664 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1665 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1666 16, /* rightshift */
1667 1, /* size (0 = byte, 1 = short, 2 = long) */
1668 16, /* bitsize */
b34976b6 1669 FALSE, /* pc_relative */
5bd4f169
AM
1670 0, /* bitpos */
1671 complain_overflow_dont, /* complain_on_overflow */
805fc799 1672 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1673 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1674 FALSE, /* partial_inplace */
5bd4f169
AM
1675 0, /* src_mask */
1676 0xffff, /* dst_mask */
b34976b6 1677 FALSE), /* pcrel_offset */
5bd4f169 1678
411e1bfb
AM
1679 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1680 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1681 16, /* rightshift */
1682 1, /* size (0 = byte, 1 = short, 2 = long) */
1683 16, /* bitsize */
b34976b6 1684 FALSE, /* pc_relative */
5bd4f169 1685 0, /* bitpos */
411e1bfb 1686 complain_overflow_dont, /* complain_on_overflow */
805fc799 1687 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1688 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1689 FALSE, /* partial_inplace */
5bd4f169
AM
1690 0, /* src_mask */
1691 0xffff, /* dst_mask */
b34976b6 1692 FALSE), /* pcrel_offset */
5bd4f169 1693
411e1bfb
AM
1694 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1695 with values (sym+add)@dtpmod and zero, and computes the offset to the
1696 first entry relative to the TOC base (r2). */
1697 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1698 0, /* rightshift */
1699 1, /* size (0 = byte, 1 = short, 2 = long) */
1700 16, /* bitsize */
b34976b6 1701 FALSE, /* pc_relative */
5bd4f169 1702 0, /* bitpos */
411e1bfb
AM
1703 complain_overflow_signed, /* complain_on_overflow */
1704 ppc64_elf_unhandled_reloc, /* special_function */
1705 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1706 FALSE, /* partial_inplace */
d006db6c 1707 0, /* src_mask */
411e1bfb 1708 0xffff, /* dst_mask */
b34976b6 1709 FALSE), /* pcrel_offset */
5bd4f169 1710
411e1bfb
AM
1711 /* Like GOT_TLSLD16, but no overflow. */
1712 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1713 0, /* rightshift */
1714 1, /* size (0 = byte, 1 = short, 2 = long) */
1715 16, /* bitsize */
b34976b6 1716 FALSE, /* pc_relative */
5bd4f169 1717 0, /* bitpos */
411e1bfb
AM
1718 complain_overflow_dont, /* complain_on_overflow */
1719 ppc64_elf_unhandled_reloc, /* special_function */
1720 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1721 FALSE, /* partial_inplace */
d006db6c 1722 0, /* src_mask */
411e1bfb 1723 0xffff, /* dst_mask */
b34976b6 1724 FALSE), /* pcrel_offset */
5bd4f169 1725
411e1bfb
AM
1726 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1727 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1728 16, /* rightshift */
5bd4f169
AM
1729 1, /* size (0 = byte, 1 = short, 2 = long) */
1730 16, /* bitsize */
b34976b6 1731 FALSE, /* pc_relative */
5bd4f169 1732 0, /* bitpos */
411e1bfb 1733 complain_overflow_dont, /* complain_on_overflow */
805fc799 1734 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1735 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1736 FALSE, /* partial_inplace */
d006db6c 1737 0, /* src_mask */
411e1bfb 1738 0xffff, /* dst_mask */
b34976b6 1739 FALSE), /* pcrel_offset */
5bd4f169 1740
411e1bfb
AM
1741 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1742 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1743 16, /* rightshift */
5bd4f169
AM
1744 1, /* size (0 = byte, 1 = short, 2 = long) */
1745 16, /* bitsize */
b34976b6 1746 FALSE, /* pc_relative */
5bd4f169
AM
1747 0, /* bitpos */
1748 complain_overflow_dont, /* complain_on_overflow */
805fc799 1749 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1750 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1751 FALSE, /* partial_inplace */
d006db6c 1752 0, /* src_mask */
411e1bfb 1753 0xffff, /* dst_mask */
b34976b6 1754 FALSE), /* pcrel_offset */
5bd4f169 1755
411e1bfb
AM
1756 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1757 the offset to the entry relative to the TOC base (r2). */
1758 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1759 0, /* rightshift */
1760 1, /* size (0 = byte, 1 = short, 2 = long) */
1761 16, /* bitsize */
b34976b6 1762 FALSE, /* pc_relative */
5bd4f169 1763 0, /* bitpos */
411e1bfb 1764 complain_overflow_signed, /* complain_on_overflow */
805fc799 1765 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1766 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1767 FALSE, /* partial_inplace */
d006db6c 1768 0, /* src_mask */
5bd4f169 1769 0xfffc, /* dst_mask */
b34976b6 1770 FALSE), /* pcrel_offset */
5bd4f169 1771
411e1bfb
AM
1772 /* Like GOT_DTPREL16_DS, but no overflow. */
1773 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1774 0, /* rightshift */
c061c2d8
AM
1775 1, /* size (0 = byte, 1 = short, 2 = long) */
1776 16, /* bitsize */
b34976b6 1777 FALSE, /* pc_relative */
5bd4f169 1778 0, /* bitpos */
411e1bfb
AM
1779 complain_overflow_dont, /* complain_on_overflow */
1780 ppc64_elf_unhandled_reloc, /* special_function */
1781 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1782 FALSE, /* partial_inplace */
d006db6c 1783 0, /* src_mask */
c061c2d8 1784 0xfffc, /* dst_mask */
b34976b6 1785 FALSE), /* pcrel_offset */
5bd4f169 1786
411e1bfb
AM
1787 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1788 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1789 16, /* rightshift */
5bd4f169
AM
1790 1, /* size (0 = byte, 1 = short, 2 = long) */
1791 16, /* bitsize */
b34976b6 1792 FALSE, /* pc_relative */
5bd4f169
AM
1793 0, /* bitpos */
1794 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1795 ppc64_elf_unhandled_reloc, /* special_function */
1796 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1797 FALSE, /* partial_inplace */
d006db6c 1798 0, /* src_mask */
411e1bfb 1799 0xffff, /* dst_mask */
b34976b6 1800 FALSE), /* pcrel_offset */
5bd4f169 1801
411e1bfb
AM
1802 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1803 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1804 16, /* rightshift */
1805 1, /* size (0 = byte, 1 = short, 2 = long) */
1806 16, /* bitsize */
1807 FALSE, /* pc_relative */
1808 0, /* bitpos */
1809 complain_overflow_dont, /* complain_on_overflow */
1810 ppc64_elf_unhandled_reloc, /* special_function */
1811 "R_PPC64_GOT_DTPREL16_HA", /* name */
1812 FALSE, /* partial_inplace */
1813 0, /* src_mask */
1814 0xffff, /* dst_mask */
1815 FALSE), /* pcrel_offset */
1816
1817 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1818 offset to the entry relative to the TOC base (r2). */
1819 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1820 0, /* rightshift */
1821 1, /* size (0 = byte, 1 = short, 2 = long) */
1822 16, /* bitsize */
b34976b6 1823 FALSE, /* pc_relative */
5bd4f169
AM
1824 0, /* bitpos */
1825 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1826 ppc64_elf_unhandled_reloc, /* special_function */
1827 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1828 FALSE, /* partial_inplace */
d006db6c 1829 0, /* src_mask */
ad8e1ba5 1830 0xfffc, /* dst_mask */
b34976b6 1831 FALSE), /* pcrel_offset */
5bd4f169 1832
411e1bfb
AM
1833 /* Like GOT_TPREL16_DS, but no overflow. */
1834 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1835 0, /* rightshift */
1836 1, /* size (0 = byte, 1 = short, 2 = long) */
1837 16, /* bitsize */
b34976b6 1838 FALSE, /* pc_relative */
5bd4f169
AM
1839 0, /* bitpos */
1840 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1841 ppc64_elf_unhandled_reloc, /* special_function */
1842 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1843 FALSE, /* partial_inplace */
d006db6c 1844 0, /* src_mask */
ad8e1ba5 1845 0xfffc, /* dst_mask */
b34976b6 1846 FALSE), /* pcrel_offset */
5bd4f169 1847
411e1bfb
AM
1848 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1849 HOWTO (R_PPC64_GOT_TPREL16_HI,
1850 16, /* rightshift */
5bd4f169
AM
1851 1, /* size (0 = byte, 1 = short, 2 = long) */
1852 16, /* bitsize */
b34976b6 1853 FALSE, /* pc_relative */
5bd4f169 1854 0, /* bitpos */
411e1bfb 1855 complain_overflow_dont, /* complain_on_overflow */
805fc799 1856 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1857 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1858 FALSE, /* partial_inplace */
d006db6c 1859 0, /* src_mask */
411e1bfb 1860 0xffff, /* dst_mask */
b34976b6 1861 FALSE), /* pcrel_offset */
5bd4f169 1862
411e1bfb
AM
1863 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1864 HOWTO (R_PPC64_GOT_TPREL16_HA,
1865 16, /* rightshift */
5bd4f169
AM
1866 1, /* size (0 = byte, 1 = short, 2 = long) */
1867 16, /* bitsize */
b34976b6 1868 FALSE, /* pc_relative */
5bd4f169
AM
1869 0, /* bitpos */
1870 complain_overflow_dont, /* complain_on_overflow */
805fc799 1871 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1872 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1873 FALSE, /* partial_inplace */
d006db6c 1874 0, /* src_mask */
411e1bfb 1875 0xffff, /* dst_mask */
b34976b6 1876 FALSE), /* pcrel_offset */
5bd4f169 1877
25f23106
AM
1878 HOWTO (R_PPC64_JMP_IREL, /* type */
1879 0, /* rightshift */
1880 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1881 0, /* bitsize */
1882 FALSE, /* pc_relative */
1883 0, /* bitpos */
1884 complain_overflow_dont, /* complain_on_overflow */
1885 ppc64_elf_unhandled_reloc, /* special_function */
1886 "R_PPC64_JMP_IREL", /* name */
1887 FALSE, /* partial_inplace */
1888 0, /* src_mask */
1889 0, /* dst_mask */
1890 FALSE), /* pcrel_offset */
1891
e054468f
AM
1892 HOWTO (R_PPC64_IRELATIVE, /* type */
1893 0, /* rightshift */
1894 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1895 64, /* bitsize */
1896 FALSE, /* pc_relative */
1897 0, /* bitpos */
1898 complain_overflow_dont, /* complain_on_overflow */
1899 bfd_elf_generic_reloc, /* special_function */
1900 "R_PPC64_IRELATIVE", /* name */
1901 FALSE, /* partial_inplace */
1902 0, /* src_mask */
1903 ONES (64), /* dst_mask */
1904 FALSE), /* pcrel_offset */
1905
25f23106
AM
1906 /* A 16 bit relative relocation. */
1907 HOWTO (R_PPC64_REL16, /* type */
1908 0, /* rightshift */
1909 1, /* size (0 = byte, 1 = short, 2 = long) */
1910 16, /* bitsize */
1911 TRUE, /* pc_relative */
1912 0, /* bitpos */
1913 complain_overflow_bitfield, /* complain_on_overflow */
1914 bfd_elf_generic_reloc, /* special_function */
1915 "R_PPC64_REL16", /* name */
1916 FALSE, /* partial_inplace */
1917 0, /* src_mask */
1918 0xffff, /* dst_mask */
1919 TRUE), /* pcrel_offset */
1920
1921 /* A 16 bit relative relocation without overflow. */
1922 HOWTO (R_PPC64_REL16_LO, /* type */
1923 0, /* rightshift */
1924 1, /* size (0 = byte, 1 = short, 2 = long) */
1925 16, /* bitsize */
1926 TRUE, /* pc_relative */
1927 0, /* bitpos */
1928 complain_overflow_dont,/* complain_on_overflow */
1929 bfd_elf_generic_reloc, /* special_function */
1930 "R_PPC64_REL16_LO", /* name */
1931 FALSE, /* partial_inplace */
1932 0, /* src_mask */
1933 0xffff, /* dst_mask */
1934 TRUE), /* pcrel_offset */
1935
1936 /* The high order 16 bits of a relative address. */
1937 HOWTO (R_PPC64_REL16_HI, /* type */
1938 16, /* rightshift */
1939 1, /* size (0 = byte, 1 = short, 2 = long) */
1940 16, /* bitsize */
1941 TRUE, /* pc_relative */
1942 0, /* bitpos */
1943 complain_overflow_dont, /* complain_on_overflow */
1944 bfd_elf_generic_reloc, /* special_function */
1945 "R_PPC64_REL16_HI", /* name */
1946 FALSE, /* partial_inplace */
1947 0, /* src_mask */
1948 0xffff, /* dst_mask */
1949 TRUE), /* pcrel_offset */
1950
1951 /* The high order 16 bits of a relative address, plus 1 if the contents of
1952 the low 16 bits, treated as a signed number, is negative. */
1953 HOWTO (R_PPC64_REL16_HA, /* 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 ppc64_elf_ha_reloc, /* special_function */
1961 "R_PPC64_REL16_HA", /* name */
1962 FALSE, /* partial_inplace */
1963 0, /* src_mask */
1964 0xffff, /* dst_mask */
1965 TRUE), /* pcrel_offset */
1966
5bd4f169
AM
1967 /* GNU extension to record C++ vtable hierarchy. */
1968 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1969 0, /* rightshift */
1970 0, /* size (0 = byte, 1 = short, 2 = long) */
1971 0, /* bitsize */
b34976b6 1972 FALSE, /* pc_relative */
5bd4f169
AM
1973 0, /* bitpos */
1974 complain_overflow_dont, /* complain_on_overflow */
1975 NULL, /* special_function */
1976 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 1977 FALSE, /* partial_inplace */
5bd4f169
AM
1978 0, /* src_mask */
1979 0, /* dst_mask */
b34976b6 1980 FALSE), /* pcrel_offset */
5bd4f169
AM
1981
1982 /* GNU extension to record C++ vtable member usage. */
1983 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1984 0, /* rightshift */
1985 0, /* size (0 = byte, 1 = short, 2 = long) */
1986 0, /* bitsize */
b34976b6 1987 FALSE, /* pc_relative */
5bd4f169
AM
1988 0, /* bitpos */
1989 complain_overflow_dont, /* complain_on_overflow */
1990 NULL, /* special_function */
1991 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 1992 FALSE, /* partial_inplace */
5bd4f169
AM
1993 0, /* src_mask */
1994 0, /* dst_mask */
b34976b6 1995 FALSE), /* pcrel_offset */
5bd4f169
AM
1996};
1997
1998\f
1999/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2000 be done. */
2001
2002static void
4ce794b7 2003ppc_howto_init (void)
5bd4f169
AM
2004{
2005 unsigned int i, type;
2006
2007 for (i = 0;
2008 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2009 i++)
2010 {
2011 type = ppc64_elf_howto_raw[i].type;
2012 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2013 / sizeof (ppc64_elf_howto_table[0])));
2014 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2015 }
2016}
2017
2018static reloc_howto_type *
4ce794b7
AM
2019ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2020 bfd_reloc_code_real_type code)
5bd4f169 2021{
411e1bfb 2022 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2023
2024 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2025 /* Initialize howto table if needed. */
2026 ppc_howto_init ();
2027
4ce794b7 2028 switch (code)
5bd4f169
AM
2029 {
2030 default:
4ce794b7 2031 return NULL;
5bd4f169 2032
411e1bfb
AM
2033 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2034 break;
2035 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2036 break;
2037 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2038 break;
2039 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2040 break;
2041 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2042 break;
2043 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2044 break;
2045 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2046 break;
411e1bfb 2047 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2048 break;
411e1bfb 2049 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2050 break;
411e1bfb 2051 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2052 break;
411e1bfb 2053 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2054 break;
411e1bfb 2055 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2056 break;
411e1bfb 2057 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2058 break;
411e1bfb 2059 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2060 break;
411e1bfb 2061 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2062 break;
411e1bfb 2063 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2064 break;
411e1bfb 2065 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2066 break;
411e1bfb 2067 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2068 break;
411e1bfb 2069 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2070 break;
411e1bfb 2071 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2072 break;
411e1bfb 2073 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2074 break;
411e1bfb 2075 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2076 break;
411e1bfb 2077 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2078 break;
411e1bfb 2079 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2080 break;
411e1bfb 2081 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2082 break;
411e1bfb 2083 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2084 break;
411e1bfb 2085 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2086 break;
411e1bfb 2087 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2088 break;
411e1bfb 2089 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2090 break;
411e1bfb 2091 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2092 break;
411e1bfb 2093 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2094 break;
411e1bfb 2095 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2096 break;
411e1bfb 2097 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2098 break;
411e1bfb 2099 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2100 break;
411e1bfb 2101 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2102 break;
411e1bfb 2103 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2104 break;
411e1bfb 2105 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2106 break;
411e1bfb 2107 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2108 break;
411e1bfb 2109 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2110 break;
411e1bfb 2111 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2112 break;
411e1bfb 2113 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2114 break;
411e1bfb 2115 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2116 break;
411e1bfb 2117 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2118 break;
411e1bfb 2119 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2120 break;
411e1bfb 2121 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2122 break;
411e1bfb 2123 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2124 break;
411e1bfb 2125 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2126 break;
411e1bfb 2127 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2128 break;
411e1bfb 2129 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2130 break;
411e1bfb 2131 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2132 break;
411e1bfb 2133 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2134 break;
411e1bfb 2135 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2136 break;
411e1bfb 2137 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2138 break;
411e1bfb 2139 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2140 break;
411e1bfb 2141 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2142 break;
411e1bfb 2143 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2144 break;
411e1bfb 2145 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2146 break;
411e1bfb 2147 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2148 break;
411e1bfb 2149 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2150 break;
411e1bfb 2151 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2152 break;
727fc41e
AM
2153 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2154 break;
2155 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2156 break;
411e1bfb 2157 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2158 break;
411e1bfb 2159 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2160 break;
411e1bfb 2161 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2162 break;
411e1bfb 2163 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2164 break;
411e1bfb 2165 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2166 break;
411e1bfb 2167 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2168 break;
411e1bfb
AM
2169 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2170 break;
2171 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2172 break;
2173 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2174 break;
2175 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2176 break;
2177 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2178 break;
2179 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2180 break;
2181 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2182 break;
2183 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2184 break;
2185 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2186 break;
2187 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2188 break;
2189 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2190 break;
2191 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2192 break;
2193 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2194 break;
2195 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2196 break;
2197 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2198 break;
2199 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2200 break;
2201 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2202 break;
2203 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2204 break;
2205 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2206 break;
2207 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2208 break;
2209 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2210 break;
2211 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2212 break;
2213 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2214 break;
2215 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2216 break;
2217 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2218 break;
2219 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2220 break;
2221 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2222 break;
2223 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2224 break;
2225 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2226 break;
2227 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2228 break;
2229 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2230 break;
2231 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2232 break;
2233 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2234 break;
25f23106
AM
2235 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2236 break;
2237 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2238 break;
2239 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2240 break;
2241 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2242 break;
411e1bfb
AM
2243 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2244 break;
2245 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2246 break;
2247 }
2248
4ce794b7 2249 return ppc64_elf_howto_table[r];
5bd4f169
AM
2250};
2251
157090f7
AM
2252static reloc_howto_type *
2253ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2254 const char *r_name)
2255{
2256 unsigned int i;
2257
2258 for (i = 0;
2259 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2260 i++)
2261 if (ppc64_elf_howto_raw[i].name != NULL
2262 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2263 return &ppc64_elf_howto_raw[i];
2264
2265 return NULL;
2266}
2267
5bd4f169
AM
2268/* Set the howto pointer for a PowerPC ELF reloc. */
2269
2270static void
4ce794b7
AM
2271ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2272 Elf_Internal_Rela *dst)
5bd4f169 2273{
65f38f15
AM
2274 unsigned int type;
2275
ef60b7ff 2276 /* Initialize howto table if needed. */
5bd4f169 2277 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2278 ppc_howto_init ();
2279
65f38f15 2280 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2281 if (type >= (sizeof (ppc64_elf_howto_table)
2282 / sizeof (ppc64_elf_howto_table[0])))
2283 {
2284 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2285 abfd, (int) type);
2286 type = R_PPC64_NONE;
d0fb9a8d 2287 }
65f38f15 2288 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2289}
2290
04c9666a 2291/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2292
2293static bfd_reloc_status_type
4ce794b7
AM
2294ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2295 void *data, asection *input_section,
2296 bfd *output_bfd, char **error_message)
5bd4f169 2297{
805fc799
AM
2298 /* If this is a relocatable link (output_bfd test tells us), just
2299 call the generic function. Any adjustment will be done at final
2300 link time. */
2301 if (output_bfd != NULL)
cedb70c5 2302 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2303 input_section, output_bfd, error_message);
2304
2305 /* Adjust the addend for sign extension of the low 16 bits.
2306 We won't actually be using the low 16 bits, so trashing them
2307 doesn't matter. */
2308 reloc_entry->addend += 0x8000;
2309 return bfd_reloc_continue;
2310}
5bd4f169 2311
2441e016
AM
2312static bfd_reloc_status_type
2313ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2314 void *data, asection *input_section,
2315 bfd *output_bfd, char **error_message)
2316{
2317 if (output_bfd != NULL)
2318 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2319 input_section, output_bfd, error_message);
2320
699733f6
AM
2321 if (strcmp (symbol->section->name, ".opd") == 0
2322 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2323 {
2324 bfd_vma dest = opd_entry_value (symbol->section,
2325 symbol->value + reloc_entry->addend,
2326 NULL, NULL);
2327 if (dest != (bfd_vma) -1)
2328 reloc_entry->addend = dest - (symbol->value
2329 + symbol->section->output_section->vma
2330 + symbol->section->output_offset);
2331 }
2332 return bfd_reloc_continue;
2333}
2334
805fc799 2335static bfd_reloc_status_type
4ce794b7
AM
2336ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2337 void *data, asection *input_section,
2338 bfd *output_bfd, char **error_message)
805fc799
AM
2339{
2340 long insn;
04c9666a 2341 enum elf_ppc64_reloc_type r_type;
805fc799
AM
2342 bfd_size_type octets;
2343 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 2344 bfd_boolean is_power4 = FALSE;
805fc799
AM
2345
2346 /* If this is a relocatable link (output_bfd test tells us), just
2347 call the generic function. Any adjustment will be done at final
2348 link time. */
5bd4f169 2349 if (output_bfd != NULL)
cedb70c5 2350 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2351 input_section, output_bfd, error_message);
2352
2353 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2354 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2355 insn &= ~(0x01 << 21);
4ce794b7 2356 r_type = reloc_entry->howto->type;
805fc799
AM
2357 if (r_type == R_PPC64_ADDR14_BRTAKEN
2358 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2359 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799
AM
2360
2361 if (is_power4)
5bd4f169 2362 {
805fc799
AM
2363 /* Set 'a' bit. This is 0b00010 in BO field for branch
2364 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2365 for branch on CTR insns (BO == 1a00t or 1a01t). */
2366 if ((insn & (0x14 << 21)) == (0x04 << 21))
2367 insn |= 0x02 << 21;
2368 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2369 insn |= 0x08 << 21;
2370 else
2441e016 2371 goto out;
5bd4f169 2372 }
805fc799
AM
2373 else
2374 {
2375 bfd_vma target = 0;
2376 bfd_vma from;
5bd4f169 2377
805fc799
AM
2378 if (!bfd_is_com_section (symbol->section))
2379 target = symbol->value;
2380 target += symbol->section->output_section->vma;
2381 target += symbol->section->output_offset;
2382 target += reloc_entry->addend;
5bd4f169 2383
805fc799
AM
2384 from = (reloc_entry->address
2385 + input_section->output_offset
2386 + input_section->output_section->vma);
5bd4f169 2387
805fc799
AM
2388 /* Invert 'y' bit if not the default. */
2389 if ((bfd_signed_vma) (target - from) < 0)
2390 insn ^= 0x01 << 21;
2391 }
4ce794b7 2392 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2393 out:
2394 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2395 input_section, output_bfd, error_message);
805fc799 2396}
5bd4f169 2397
805fc799 2398static bfd_reloc_status_type
4ce794b7
AM
2399ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2400 void *data, asection *input_section,
2401 bfd *output_bfd, char **error_message)
805fc799
AM
2402{
2403 /* If this is a relocatable link (output_bfd test tells us), just
2404 call the generic function. Any adjustment will be done at final
2405 link time. */
2406 if (output_bfd != NULL)
cedb70c5 2407 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2408 input_section, output_bfd, error_message);
5bd4f169 2409
805fc799
AM
2410 /* Subtract the symbol section base address. */
2411 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2412 return bfd_reloc_continue;
2413}
2414
805fc799 2415static bfd_reloc_status_type
4ce794b7
AM
2416ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2417 void *data, asection *input_section,
2418 bfd *output_bfd, char **error_message)
805fc799
AM
2419{
2420 /* If this is a relocatable link (output_bfd test tells us), just
2421 call the generic function. Any adjustment will be done at final
2422 link time. */
2423 if (output_bfd != NULL)
cedb70c5 2424 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2425 input_section, output_bfd, error_message);
2426
2427 /* Subtract the symbol section base address. */
2428 reloc_entry->addend -= symbol->section->output_section->vma;
2429
2430 /* Adjust the addend for sign extension of the low 16 bits. */
2431 reloc_entry->addend += 0x8000;
2432 return bfd_reloc_continue;
2433}
2434
2435static bfd_reloc_status_type
4ce794b7
AM
2436ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2437 void *data, asection *input_section,
2438 bfd *output_bfd, char **error_message)
805fc799
AM
2439{
2440 bfd_vma TOCstart;
2441
2442 /* If this is a relocatable link (output_bfd test tells us), just
2443 call the generic function. Any adjustment will be done at final
2444 link time. */
2445 if (output_bfd != NULL)
cedb70c5 2446 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2447 input_section, output_bfd, error_message);
2448
2449 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2450 if (TOCstart == 0)
2451 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2452
2453 /* Subtract the TOC base address. */
2454 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2455 return bfd_reloc_continue;
2456}
2457
2458static bfd_reloc_status_type
4ce794b7
AM
2459ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2460 void *data, asection *input_section,
2461 bfd *output_bfd, char **error_message)
805fc799
AM
2462{
2463 bfd_vma TOCstart;
2464
2465 /* If this is a relocatable link (output_bfd test tells us), just
2466 call the generic function. Any adjustment will be done at final
2467 link time. */
2468 if (output_bfd != NULL)
cedb70c5 2469 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2470 input_section, output_bfd, error_message);
2471
2472 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2473 if (TOCstart == 0)
2474 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2475
2476 /* Subtract the TOC base address. */
2477 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2478
2479 /* Adjust the addend for sign extension of the low 16 bits. */
2480 reloc_entry->addend += 0x8000;
2481 return bfd_reloc_continue;
2482}
2483
2484static bfd_reloc_status_type
4ce794b7
AM
2485ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2486 void *data, asection *input_section,
2487 bfd *output_bfd, char **error_message)
805fc799
AM
2488{
2489 bfd_vma TOCstart;
2490 bfd_size_type octets;
2491
2492 /* If this is a relocatable link (output_bfd test tells us), just
2493 call the generic function. Any adjustment will be done at final
2494 link time. */
2495 if (output_bfd != NULL)
cedb70c5 2496 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2497 input_section, output_bfd, error_message);
2498
2499 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2500 if (TOCstart == 0)
2501 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2502
2503 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2504 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2505 return bfd_reloc_ok;
2506}
2507
2508static bfd_reloc_status_type
4ce794b7
AM
2509ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2510 void *data, asection *input_section,
2511 bfd *output_bfd, char **error_message)
805fc799
AM
2512{
2513 /* If this is a relocatable link (output_bfd test tells us), just
2514 call the generic function. Any adjustment will be done at final
2515 link time. */
2516 if (output_bfd != NULL)
cedb70c5 2517 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2518 input_section, output_bfd, error_message);
2519
2520 if (error_message != NULL)
2521 {
2522 static char buf[60];
2523 sprintf (buf, "generic linker can't handle %s",
2524 reloc_entry->howto->name);
2525 *error_message = buf;
2526 }
2527 return bfd_reloc_dangerous;
2528}
2529
927be08e
AM
2530/* Track GOT entries needed for a given symbol. We might need more
2531 than one got entry per symbol. */
2532struct got_entry
2533{
2534 struct got_entry *next;
2535
2536 /* The symbol addend that we'll be placing in the GOT. */
2537 bfd_vma addend;
2538
2539 /* Unlike other ELF targets, we use separate GOT entries for the same
2540 symbol referenced from different input files. This is to support
2541 automatic multiple TOC/GOT sections, where the TOC base can vary
2542 from one input file to another. After partitioning into TOC groups
2543 we merge entries within the group.
2544
2545 Point to the BFD owning this GOT entry. */
2546 bfd *owner;
2547
2548 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2549 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2550 unsigned char tls_type;
927be08e
AM
2551
2552 /* Non-zero if got.ent points to real entry. */
f961d9dd 2553 unsigned char is_indirect;
927be08e
AM
2554
2555 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2556 union
2557 {
2558 bfd_signed_vma refcount;
2559 bfd_vma offset;
2560 struct got_entry *ent;
2561 } got;
2562};
2563
2564/* The same for PLT. */
2565struct plt_entry
2566{
2567 struct plt_entry *next;
2568
2569 bfd_vma addend;
2570
2571 union
2572 {
2573 bfd_signed_vma refcount;
2574 bfd_vma offset;
2575 } plt;
2576};
2577
e717da7e
AM
2578struct ppc64_elf_obj_tdata
2579{
2580 struct elf_obj_tdata elf;
2581
2582 /* Shortcuts to dynamic linker sections. */
2583 asection *got;
2584 asection *relgot;
2585
b3fac117
AM
2586 /* Used during garbage collection. We attach global symbols defined
2587 on removed .opd entries to this section so that the sym is removed. */
2588 asection *deleted_section;
81688140 2589
927be08e 2590 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2591 sections means we potentially need one of these for each input bfd. */
927be08e 2592 struct got_entry tlsld_got;
8860955f
AM
2593
2594 /* A copy of relocs before they are modified for --emit-relocs. */
2595 Elf_Internal_Rela *opd_relocs;
d77c8a4b
AM
2596
2597 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2598 the reloc to be in the range -32768 to 32767. */
2599 unsigned int has_small_toc_reloc;
e717da7e
AM
2600};
2601
2602#define ppc64_elf_tdata(bfd) \
2603 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2604
2605#define ppc64_tlsld_got(bfd) \
2606 (&ppc64_elf_tdata (bfd)->tlsld_got)
2607
0c8d6e5c
AM
2608#define is_ppc64_elf(bfd) \
2609 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2610 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2611
e717da7e
AM
2612/* Override the generic function because we store some extras. */
2613
2614static bfd_boolean
2615ppc64_elf_mkobject (bfd *abfd)
2616{
0ffa91dd 2617 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2618 PPC64_ELF_DATA);
e717da7e
AM
2619}
2620
feee612b
AM
2621/* Fix bad default arch selected for a 64 bit input bfd when the
2622 default is 32 bit. */
2623
b34976b6 2624static bfd_boolean
4ce794b7 2625ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2626{
2627 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2628 {
2629 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2630
2631 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2632 {
2633 /* Relies on arch after 32 bit default being 64 bit default. */
2634 abfd->arch_info = abfd->arch_info->next;
2635 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2636 }
2637 }
b34976b6 2638 return TRUE;
feee612b
AM
2639}
2640
d37c89e5
AM
2641/* Support for core dump NOTE sections. */
2642
2643static bfd_boolean
2644ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2645{
eea6121a 2646 size_t offset, size;
d37c89e5
AM
2647
2648 if (note->descsz != 504)
2649 return FALSE;
2650
2651 /* pr_cursig */
2652 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2653
2654 /* pr_pid */
261b8d08 2655 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2656
2657 /* pr_reg */
2658 offset = 112;
eea6121a 2659 size = 384;
d37c89e5
AM
2660
2661 /* Make a ".reg/999" section. */
2662 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2663 size, note->descpos + offset);
d37c89e5
AM
2664}
2665
2666static bfd_boolean
2667ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2668{
2669 if (note->descsz != 136)
2670 return FALSE;
2671
2672 elf_tdata (abfd)->core_program
2673 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2674 elf_tdata (abfd)->core_command
2675 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2676
2677 return TRUE;
2678}
2679
183e98be
AM
2680static char *
2681ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2682 ...)
2683{
2684 switch (note_type)
2685 {
2686 default:
2687 return NULL;
2688
2689 case NT_PRPSINFO:
2690 {
2691 char data[136];
2692 va_list ap;
2693
2694 va_start (ap, note_type);
2695 memset (data, 0, 40);
2696 strncpy (data + 40, va_arg (ap, const char *), 16);
2697 strncpy (data + 56, va_arg (ap, const char *), 80);
2698 va_end (ap);
2699 return elfcore_write_note (abfd, buf, bufsiz,
2700 "CORE", note_type, data, sizeof (data));
2701 }
2702
2703 case NT_PRSTATUS:
2704 {
2705 char data[504];
2706 va_list ap;
2707 long pid;
2708 int cursig;
2709 const void *greg;
2710
2711 va_start (ap, note_type);
2712 memset (data, 0, 112);
2713 pid = va_arg (ap, long);
2714 bfd_put_32 (abfd, pid, data + 32);
2715 cursig = va_arg (ap, int);
2716 bfd_put_16 (abfd, cursig, data + 12);
2717 greg = va_arg (ap, const void *);
2718 memcpy (data + 112, greg, 384);
2719 memset (data + 496, 0, 8);
2720 va_end (ap);
2721 return elfcore_write_note (abfd, buf, bufsiz,
2722 "CORE", note_type, data, sizeof (data));
2723 }
2724 }
2725}
2726
5bd4f169
AM
2727/* Merge backend specific data from an object file to the output
2728 object file when linking. */
2f6d9989 2729
b34976b6 2730static bfd_boolean
4ce794b7 2731ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5bd4f169 2732{
5bd4f169
AM
2733 /* Check if we have the same endianess. */
2734 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
87e226ce 2735 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
5bd4f169
AM
2736 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2737 {
2738 const char *msg;
2739
2740 if (bfd_big_endian (ibfd))
d003868e 2741 msg = _("%B: compiled for a big endian system "
4ce794b7 2742 "and target is little endian");
5bd4f169 2743 else
d003868e 2744 msg = _("%B: compiled for a little endian system "
4ce794b7 2745 "and target is big endian");
5bd4f169 2746
d003868e 2747 (*_bfd_error_handler) (msg, ibfd);
5bd4f169
AM
2748
2749 bfd_set_error (bfd_error_wrong_format);
b34976b6 2750 return FALSE;
5bd4f169
AM
2751 }
2752
b34976b6 2753 return TRUE;
5bd4f169 2754}
f0abc2a1 2755
5d35169e
AM
2756/* Add extra PPC sections. */
2757
b35d266b 2758static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2759{
0112cd26
NC
2760 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2761 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2762 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2763 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2766 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2767};
2768
7c8fe5c4
AM
2769enum _ppc64_sec_type {
2770 sec_normal = 0,
2771 sec_opd = 1,
2772 sec_toc = 2
2773};
2774
f0abc2a1
AM
2775struct _ppc64_elf_section_data
2776{
2777 struct bfd_elf_section_data elf;
411e1bfb 2778
f0abc2a1
AM
2779 union
2780 {
74f0fb50
AM
2781 /* An array with one entry for each opd function descriptor. */
2782 struct _opd_sec_data
2783 {
2784 /* Points to the function code section for local opd entries. */
2785 asection **func_sec;
2786
2787 /* After editing .opd, adjust references to opd local syms. */
2788 long *adjust;
2789 } opd;
7c8fe5c4 2790
3a71aa26
AM
2791 /* An array for toc sections, indexed by offset/8. */
2792 struct _toc_sec_data
2793 {
2794 /* Specifies the relocation symbol index used at a given toc offset. */
2795 unsigned *symndx;
2796
2797 /* And the relocation addend. */
2798 bfd_vma *add;
2799 } toc;
7c8fe5c4
AM
2800 } u;
2801
2802 enum _ppc64_sec_type sec_type:2;
411e1bfb 2803
7c8fe5c4
AM
2804 /* Flag set when small branches are detected. Used to
2805 select suitable defaults for the stub group size. */
2806 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2807};
2808
2809#define ppc64_elf_section_data(sec) \
411e1bfb 2810 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2811
2812static bfd_boolean
4ce794b7 2813ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2814{
f592407e
AM
2815 if (!sec->used_by_bfd)
2816 {
2817 struct _ppc64_elf_section_data *sdata;
2818 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2819
f592407e
AM
2820 sdata = bfd_zalloc (abfd, amt);
2821 if (sdata == NULL)
2822 return FALSE;
2823 sec->used_by_bfd = sdata;
2824 }
f0abc2a1
AM
2825
2826 return _bfd_elf_new_section_hook (abfd, sec);
2827}
4025353c 2828
74f0fb50 2829static struct _opd_sec_data *
4025353c
AM
2830get_opd_info (asection * sec)
2831{
2832 if (sec != NULL
2833 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2834 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2835 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2836 return NULL;
2837}
90e3cdf2
JJ
2838\f
2839/* Parameters for the qsort hook. */
90e3cdf2
JJ
2840static bfd_boolean synthetic_relocatable;
2841
699733f6 2842/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2843
2844static int
2845compare_symbols (const void *ap, const void *bp)
2846{
2847 const asymbol *a = * (const asymbol **) ap;
2848 const asymbol *b = * (const asymbol **) bp;
2849
699733f6
AM
2850 /* Section symbols first. */
2851 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2852 return -1;
699733f6 2853 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2854 return 1;
2855
699733f6 2856 /* then .opd symbols. */
ffcfec52
AM
2857 if (strcmp (a->section->name, ".opd") == 0
2858 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2859 return -1;
ffcfec52
AM
2860 if (strcmp (a->section->name, ".opd") != 0
2861 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2862 return 1;
2863
699733f6 2864 /* then other code symbols. */
90e3cdf2
JJ
2865 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2866 == (SEC_CODE | SEC_ALLOC)
2867 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2868 != (SEC_CODE | SEC_ALLOC))
2869 return -1;
2870
2871 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2872 != (SEC_CODE | SEC_ALLOC)
2873 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2874 == (SEC_CODE | SEC_ALLOC))
2875 return 1;
2876
2877 if (synthetic_relocatable)
2878 {
2879 if (a->section->id < b->section->id)
2880 return -1;
2881
2882 if (a->section->id > b->section->id)
2883 return 1;
2884 }
2885
2886 if (a->value + a->section->vma < b->value + b->section->vma)
2887 return -1;
2888
2889 if (a->value + a->section->vma > b->value + b->section->vma)
2890 return 1;
2891
4d35a0aa
AM
2892 /* For syms with the same value, prefer strong dynamic global function
2893 syms over other syms. */
2894 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2895 return -1;
2896
2897 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2898 return 1;
2899
2900 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2901 return -1;
2902
2903 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2904 return 1;
2905
2906 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2907 return -1;
2908
2909 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2910 return 1;
2911
2912 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2913 return -1;
2914
2915 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2916 return 1;
2917
90e3cdf2
JJ
2918 return 0;
2919}
2920
699733f6 2921/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2922
699733f6
AM
2923static asymbol *
2924sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2925{
699733f6 2926 long mid;
90e3cdf2 2927
699733f6
AM
2928 if (id == -1)
2929 {
2930 while (lo < hi)
2931 {
2932 mid = (lo + hi) >> 1;
2933 if (syms[mid]->value + syms[mid]->section->vma < value)
2934 lo = mid + 1;
2935 else if (syms[mid]->value + syms[mid]->section->vma > value)
2936 hi = mid;
2937 else
2938 return syms[mid];
2939 }
2940 }
2941 else
2942 {
2943 while (lo < hi)
2944 {
2945 mid = (lo + hi) >> 1;
2946 if (syms[mid]->section->id < id)
2947 lo = mid + 1;
2948 else if (syms[mid]->section->id > id)
2949 hi = mid;
2950 else if (syms[mid]->value < value)
2951 lo = mid + 1;
2952 else if (syms[mid]->value > value)
2953 hi = mid;
2954 else
2955 return syms[mid];
2956 }
2957 }
2958 return NULL;
90e3cdf2
JJ
2959}
2960
468392fb
AM
2961static bfd_boolean
2962section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2963{
2964 bfd_vma vma = *(bfd_vma *) ptr;
2965 return ((section->flags & SEC_ALLOC) != 0
2966 && section->vma <= vma
2967 && vma < section->vma + section->size);
2968}
2969
699733f6 2970/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 2971 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
2972
2973static long
a7535cf3
AM
2974ppc64_elf_get_synthetic_symtab (bfd *abfd,
2975 long static_count, asymbol **static_syms,
2976 long dyn_count, asymbol **dyn_syms,
c9727e01 2977 asymbol **ret)
90e3cdf2
JJ
2978{
2979 asymbol *s;
699733f6
AM
2980 long i;
2981 long count;
90e3cdf2 2982 char *names;
a7535cf3 2983 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2984 asection *opd;
90e3cdf2 2985 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2986 asymbol **syms;
90e3cdf2
JJ
2987
2988 *ret = NULL;
2989
2990 opd = bfd_get_section_by_name (abfd, ".opd");
2991 if (opd == NULL)
2992 return 0;
2993
a7535cf3 2994 symcount = static_count;
c9727e01 2995 if (!relocatable)
a7535cf3 2996 symcount += dyn_count;
90e3cdf2 2997 if (symcount == 0)
c9727e01 2998 return 0;
90e3cdf2 2999
a7535cf3
AM
3000 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3001 if (syms == NULL)
7356fed5 3002 return -1;
a7535cf3
AM
3003
3004 if (!relocatable && static_count != 0 && dyn_count != 0)
3005 {
3006 /* Use both symbol tables. */
3007 memcpy (syms, static_syms, static_count * sizeof (*syms));
3008 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3009 }
3010 else if (!relocatable && static_count == 0)
3011 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3012 else
3013 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3014
90e3cdf2 3015 synthetic_relocatable = relocatable;
595da8c5 3016 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3017
c9727e01
AM
3018 if (!relocatable && symcount > 1)
3019 {
3020 long j;
3021 /* Trim duplicate syms, since we may have merged the normal and
3022 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3023 different values, so trim any with the same value. */
c9727e01
AM
3024 for (i = 1, j = 1; i < symcount; ++i)
3025 if (syms[i - 1]->value + syms[i - 1]->section->vma
3026 != syms[i]->value + syms[i]->section->vma)
3027 syms[j++] = syms[i];
3028 symcount = j;
3029 }
3030
699733f6 3031 i = 0;
ffcfec52 3032 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3033 ++i;
3034 codesecsym = i;
90e3cdf2 3035
699733f6
AM
3036 for (; i < symcount; ++i)
3037 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3038 != (SEC_CODE | SEC_ALLOC))
3039 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3040 break;
3041 codesecsymend = i;
90e3cdf2 3042
699733f6
AM
3043 for (; i < symcount; ++i)
3044 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3045 break;
3046 secsymend = i;
90e3cdf2 3047
699733f6 3048 for (; i < symcount; ++i)
ffcfec52 3049 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3050 break;
3051 opdsymend = i;
90e3cdf2 3052
699733f6
AM
3053 for (; i < symcount; ++i)
3054 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3055 != (SEC_CODE | SEC_ALLOC))
3056 break;
3057 symcount = i;
3058
c9727e01 3059 count = 0;
90e3cdf2 3060
699733f6 3061 if (relocatable)
90e3cdf2 3062 {
699733f6
AM
3063 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3064 arelent *r;
3065 size_t size;
3066 long relcount;
90e3cdf2 3067
468392fb
AM
3068 if (opdsymend == secsymend)
3069 goto done;
3070
699733f6 3071 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3072 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3073 if (relcount == 0)
c9727e01 3074 goto done;
90e3cdf2 3075
7356fed5
AM
3076 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3077 {
3078 count = -1;
3079 goto done;
3080 }
3081
699733f6 3082 size = 0;
595da8c5 3083 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3084 {
3085 asymbol *sym;
90e3cdf2 3086
595da8c5 3087 while (r < opd->relocation + relcount
699733f6
AM
3088 && r->address < syms[i]->value + opd->vma)
3089 ++r;
90e3cdf2 3090
595da8c5 3091 if (r == opd->relocation + relcount)
699733f6 3092 break;
90e3cdf2 3093
699733f6
AM
3094 if (r->address != syms[i]->value + opd->vma)
3095 continue;
90e3cdf2 3096
699733f6
AM
3097 if (r->howto->type != R_PPC64_ADDR64)
3098 continue;
90e3cdf2 3099
699733f6
AM
3100 sym = *r->sym_ptr_ptr;
3101 if (!sym_exists_at (syms, opdsymend, symcount,
3102 sym->section->id, sym->value + r->addend))
3103 {
3104 ++count;
3105 size += sizeof (asymbol);
3106 size += strlen (syms[i]->name) + 2;
3107 }
3108 }
90e3cdf2 3109
699733f6
AM
3110 s = *ret = bfd_malloc (size);
3111 if (s == NULL)
3112 {
7356fed5 3113 count = -1;
c9727e01 3114 goto done;
699733f6 3115 }
90e3cdf2 3116
699733f6 3117 names = (char *) (s + count);
90e3cdf2 3118
595da8c5 3119 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3120 {
699733f6 3121 asymbol *sym;
90e3cdf2 3122
595da8c5 3123 while (r < opd->relocation + relcount
699733f6
AM
3124 && r->address < syms[i]->value + opd->vma)
3125 ++r;
90e3cdf2 3126
595da8c5 3127 if (r == opd->relocation + relcount)
699733f6
AM
3128 break;
3129
3130 if (r->address != syms[i]->value + opd->vma)
3131 continue;
3132
3133 if (r->howto->type != R_PPC64_ADDR64)
3134 continue;
90e3cdf2 3135
699733f6
AM
3136 sym = *r->sym_ptr_ptr;
3137 if (!sym_exists_at (syms, opdsymend, symcount,
3138 sym->section->id, sym->value + r->addend))
3139 {
3140 size_t len;
3141
3142 *s = *syms[i];
6ba2a415 3143 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3144 s->section = sym->section;
3145 s->value = sym->value + r->addend;
3146 s->name = names;
3147 *names++ = '.';
3148 len = strlen (syms[i]->name);
3149 memcpy (names, syms[i]->name, len + 1);
3150 names += len + 1;
6f610d07
UW
3151 /* Have udata.p point back to the original symbol this
3152 synthetic symbol was derived from. */
3153 s->udata.p = syms[i];
699733f6
AM
3154 s++;
3155 }
3156 }
3157 }
3158 else
90e3cdf2 3159 {
468392fb 3160 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
699733f6
AM
3161 bfd_byte *contents;
3162 size_t size;
468392fb
AM
3163 long plt_count = 0;
3164 bfd_vma glink_vma = 0, resolv_vma = 0;
3165 asection *dynamic, *glink = NULL, *relplt = NULL;
3166 arelent *p;
90e3cdf2 3167
699733f6
AM
3168 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3169 {
3170 if (contents)
c9727e01
AM
3171 {
3172 free_contents_and_exit:
3173 free (contents);
3174 }
7356fed5 3175 count = -1;
c9727e01 3176 goto done;
699733f6 3177 }
90e3cdf2 3178
699733f6
AM
3179 size = 0;
3180 for (i = secsymend; i < opdsymend; ++i)
3181 {
3182 bfd_vma ent;
90e3cdf2 3183
5ef11c02
AM
3184 /* Ignore bogus symbols. */
3185 if (syms[i]->value > opd->size - 8)
3186 continue;
3187
699733f6
AM
3188 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3189 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3190 {
3191 ++count;
3192 size += sizeof (asymbol);
3193 size += strlen (syms[i]->name) + 2;
3194 }
3195 }
90e3cdf2 3196
468392fb 3197 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3198 if (dyn_count != 0
3199 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3200 {
3201 bfd_byte *dynbuf, *extdyn, *extdynend;
3202 size_t extdynsize;
3203 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3204
3205 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3206 goto free_contents_and_exit;
3207
3208 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3209 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3210
3211 extdyn = dynbuf;
3212 extdynend = extdyn + dynamic->size;
3213 for (; extdyn < extdynend; extdyn += extdynsize)
3214 {
3215 Elf_Internal_Dyn dyn;
3216 (*swap_dyn_in) (abfd, extdyn, &dyn);
3217
3218 if (dyn.d_tag == DT_NULL)
3219 break;
3220
3221 if (dyn.d_tag == DT_PPC64_GLINK)
3222 {
3223 /* The first glink stub starts at offset 32; see comment in
3224 ppc64_elf_finish_dynamic_sections. */
3225 glink_vma = dyn.d_un.d_val + 32;
3226 /* The .glink section usually does not survive the final
3227 link; search for the section (usually .text) where the
3228 glink stubs now reside. */
3229 glink = bfd_sections_find_if (abfd, section_covers_vma,
3230 &glink_vma);
3231 break;
3232 }
3233 }
3234
3235 free (dynbuf);
3236 }
3237
3238 if (glink != NULL)
3239 {
3240 /* Determine __glink trampoline by reading the relative branch
3241 from the first glink stub. */
3242 bfd_byte buf[4];
3243 if (bfd_get_section_contents (abfd, glink, buf,
3244 glink_vma + 4 - glink->vma, 4))
3245 {
3246 unsigned int insn = bfd_get_32 (abfd, buf);
3247 insn ^= B_DOT;
3248 if ((insn & ~0x3fffffc) == 0)
3249 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3250 }
3251
3252 if (resolv_vma)
3253 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3254
066ee829
AM
3255 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3256 if (relplt != NULL)
3257 {
3258 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3259 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3260 goto free_contents_and_exit;
468392fb 3261
066ee829
AM
3262 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3263 size += plt_count * sizeof (asymbol);
468392fb 3264
066ee829
AM
3265 p = relplt->relocation;
3266 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3267 {
3268 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3269 if (p->addend != 0)
3270 size += sizeof ("+0x") - 1 + 16;
3271 }
066ee829 3272 }
468392fb
AM
3273 }
3274
699733f6
AM
3275 s = *ret = bfd_malloc (size);
3276 if (s == NULL)
7356fed5 3277 goto free_contents_and_exit;
90e3cdf2 3278
468392fb 3279 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3280
699733f6 3281 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3282 {
699733f6 3283 bfd_vma ent;
90e3cdf2 3284
5ef11c02
AM
3285 if (syms[i]->value > opd->size - 8)
3286 continue;
3287
699733f6
AM
3288 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3289 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3290 {
c9727e01 3291 long lo, hi;
699733f6 3292 size_t len;
c9727e01 3293 asection *sec = abfd->sections;
90e3cdf2 3294
699733f6
AM
3295 *s = *syms[i];
3296 lo = codesecsym;
3297 hi = codesecsymend;
3298 while (lo < hi)
3299 {
c9727e01 3300 long mid = (lo + hi) >> 1;
699733f6
AM
3301 if (syms[mid]->section->vma < ent)
3302 lo = mid + 1;
3303 else if (syms[mid]->section->vma > ent)
3304 hi = mid;
3305 else
c9727e01
AM
3306 {
3307 sec = syms[mid]->section;
3308 break;
3309 }
699733f6
AM
3310 }
3311
c9727e01 3312 if (lo >= hi && lo > codesecsym)
699733f6 3313 sec = syms[lo - 1]->section;
699733f6
AM
3314
3315 for (; sec != NULL; sec = sec->next)
3316 {
3317 if (sec->vma > ent)
3318 break;
3319 if ((sec->flags & SEC_ALLOC) == 0
3320 || (sec->flags & SEC_LOAD) == 0)
3321 break;
3322 if ((sec->flags & SEC_CODE) != 0)
3323 s->section = sec;
3324 }
6ba2a415 3325 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3326 s->value = ent - s->section->vma;
3327 s->name = names;
3328 *names++ = '.';
3329 len = strlen (syms[i]->name);
3330 memcpy (names, syms[i]->name, len + 1);
3331 names += len + 1;
6f610d07
UW
3332 /* Have udata.p point back to the original symbol this
3333 synthetic symbol was derived from. */
3334 s->udata.p = syms[i];
699733f6 3335 s++;
90e3cdf2 3336 }
90e3cdf2 3337 }
699733f6 3338 free (contents);
468392fb
AM
3339
3340 if (glink != NULL && relplt != NULL)
3341 {
3342 if (resolv_vma)
3343 {
3344 /* Add a symbol for the main glink trampoline. */
86a4952b 3345 memset (s, 0, sizeof *s);
468392fb 3346 s->the_bfd = abfd;
6ba2a415 3347 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3348 s->section = glink;
3349 s->value = resolv_vma - glink->vma;
3350 s->name = names;
3351 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3352 names += sizeof ("__glink_PLTresolve");
3353 s++;
3354 count++;
3355 }
3356
3357 /* FIXME: It would be very much nicer to put sym@plt on the
3358 stub rather than on the glink branch table entry. The
3359 objdump disassembler would then use a sensible symbol
3360 name on plt calls. The difficulty in doing so is
3361 a) finding the stubs, and,
3362 b) matching stubs against plt entries, and,
3363 c) there can be multiple stubs for a given plt entry.
3364
3365 Solving (a) could be done by code scanning, but older
3366 ppc64 binaries used different stubs to current code.
3367 (b) is the tricky one since you need to known the toc
3368 pointer for at least one function that uses a pic stub to
3369 be able to calculate the plt address referenced.
3370 (c) means gdb would need to set multiple breakpoints (or
3371 find the glink branch itself) when setting breakpoints
3372 for pending shared library loads. */
3373 p = relplt->relocation;
3374 for (i = 0; i < plt_count; i++, p++)
3375 {
3376 size_t len;
3377
3378 *s = **p->sym_ptr_ptr;
3379 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3380 we are defining a symbol, ensure one of them is set. */
3381 if ((s->flags & BSF_LOCAL) == 0)
3382 s->flags |= BSF_GLOBAL;
6ba2a415 3383 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3384 s->section = glink;
3385 s->value = glink_vma - glink->vma;
3386 s->name = names;
3387 s->udata.p = NULL;
3388 len = strlen ((*p->sym_ptr_ptr)->name);
3389 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3390 names += len;
e054468f
AM
3391 if (p->addend != 0)
3392 {
3393 memcpy (names, "+0x", sizeof ("+0x") - 1);
3394 names += sizeof ("+0x") - 1;
3395 bfd_sprintf_vma (abfd, names, p->addend);
3396 names += strlen (names);
3397 }
468392fb
AM
3398 memcpy (names, "@plt", sizeof ("@plt"));
3399 names += sizeof ("@plt");
3400 s++;
3401 glink_vma += 8;
3402 if (i >= 0x8000)
3403 glink_vma += 4;
3404 }
3405 count += plt_count;
3406 }
90e3cdf2
JJ
3407 }
3408
c9727e01 3409 done:
a7535cf3 3410 free (syms);
90e3cdf2
JJ
3411 return count;
3412}
5bd4f169 3413\f
65f38f15
AM
3414/* The following functions are specific to the ELF linker, while
3415 functions above are used generally. Those named ppc64_elf_* are
3416 called by the main ELF linker code. They appear in this file more
3417 or less in the order in which they are called. eg.
3418 ppc64_elf_check_relocs is called early in the link process,
3419 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3420 called.
3421
3422 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3423 functions have both a function code symbol and a function descriptor
3424 symbol. A call to foo in a relocatable object file looks like:
3425
3426 . .text
3427 . x:
3428 . bl .foo
3429 . nop
3430
3431 The function definition in another object file might be:
3432
3433 . .section .opd
3434 . foo: .quad .foo
3435 . .quad .TOC.@tocbase
3436 . .quad 0
3437 .
3438 . .text
3439 . .foo: blr
3440
3441 When the linker resolves the call during a static link, the branch
3442 unsurprisingly just goes to .foo and the .opd information is unused.
3443 If the function definition is in a shared library, things are a little
3444 different: The call goes via a plt call stub, the opd information gets
3445 copied to the plt, and the linker patches the nop.
3446
3447 . x:
3448 . bl .foo_stub
3449 . ld 2,40(1)
3450 .
3451 .
3452 . .foo_stub:
3453 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 3454 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
3455 . std 2,40(1) # this is the general idea
3456 . ld 11,0(12)
3457 . ld 2,8(12)
3458 . mtctr 11
3459 . ld 11,16(12)
3460 . bctr
3461 .
3462 . .section .plt
3463 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3464
3465 The "reloc ()" notation is supposed to indicate that the linker emits
3466 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3467 copying.
3468
3469 What are the difficulties here? Well, firstly, the relocations
3470 examined by the linker in check_relocs are against the function code
3471 sym .foo, while the dynamic relocation in the plt is emitted against
3472 the function descriptor symbol, foo. Somewhere along the line, we need
3473 to carefully copy dynamic link information from one symbol to the other.
3474 Secondly, the generic part of the elf linker will make .foo a dynamic
3475 symbol as is normal for most other backends. We need foo dynamic
3476 instead, at least for an application final link. However, when
3477 creating a shared library containing foo, we need to have both symbols
3478 dynamic so that references to .foo are satisfied during the early
3479 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3480 definition from some other object, eg. a static library.
3481
3482 Update: As of August 2004, we support a new convention. Function
3483 calls may use the function descriptor symbol, ie. "bl foo". This
3484 behaves exactly as "bl .foo". */
65f38f15
AM
3485
3486/* The linker needs to keep track of the number of relocs that it
3487 decides to copy as dynamic relocs in check_relocs for each symbol.
3488 This is so that it can later discard them if they are found to be
3489 unnecessary. We store the information in a field extending the
3490 regular ELF linker hash table. */
3491
3492struct ppc_dyn_relocs
3493{
3494 struct ppc_dyn_relocs *next;
3495
3496 /* The input section of the reloc. */
3497 asection *sec;
3498
3499 /* Total number of relocs copied for the input section. */
3500 bfd_size_type count;
3501
3502 /* Number of pc-relative relocs copied for the input section. */
3503 bfd_size_type pc_count;
3504};
3505
1d483afe 3506/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3507 selects those that must be copied when linking a shared library,
3508 even when the symbol is local. */
65f38f15 3509
1d483afe
AM
3510static int
3511must_be_dyn_reloc (struct bfd_link_info *info,
3512 enum elf_ppc64_reloc_type r_type)
3513{
3514 switch (r_type)
3515 {
3516 default:
3517 return 1;
3518
3519 case R_PPC64_REL32:
3520 case R_PPC64_REL64:
3521 case R_PPC64_REL30:
3522 return 0;
3523
3524 case R_PPC64_TPREL16:
3525 case R_PPC64_TPREL16_LO:
3526 case R_PPC64_TPREL16_HI:
3527 case R_PPC64_TPREL16_HA:
3528 case R_PPC64_TPREL16_DS:
3529 case R_PPC64_TPREL16_LO_DS:
3530 case R_PPC64_TPREL16_HIGHER:
3531 case R_PPC64_TPREL16_HIGHERA:
3532 case R_PPC64_TPREL16_HIGHEST:
3533 case R_PPC64_TPREL16_HIGHESTA:
3534 case R_PPC64_TPREL64:
3535 return !info->executable;
3536 }
3537}
65f38f15 3538
f4656909
AM
3539/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3540 copying dynamic variables from a shared lib into an app's dynbss
3541 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3542 shared lib. With code that gcc generates, it's vital that this be
3543 enabled; In the PowerPC64 ABI, the address of a function is actually
3544 the address of a function descriptor, which resides in the .opd
3545 section. gcc uses the descriptor directly rather than going via the
3546 GOT as some other ABI's do, which means that initialized function
3547 pointers must reference the descriptor. Thus, a function pointer
3548 initialized to the address of a function in a shared library will
3549 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3550 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3551 presents a problem as a plt entry for that function is also
3552 initialized from the function descriptor symbol and the copy reloc
3553 may not be initialized first. */
a23b6845 3554#define ELIMINATE_COPY_RELOCS 1
f4656909 3555
721956f4
AM
3556/* Section name for stubs is the associated section name plus this
3557 string. */
3558#define STUB_SUFFIX ".stub"
3559
3560/* Linker stubs.
3561 ppc_stub_long_branch:
3562 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3563 destination, but a 24 bit branch in a stub section will reach.
3564 . b dest
3565
3566 ppc_stub_plt_branch:
3567 Similar to the above, but a 24 bit branch in the stub section won't
3568 reach its destination.
87e226ce
AM
3569 . addis %r12,%r2,xxx@toc@ha
3570 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3571 . mtctr %r11
3572 . bctr
3573
3574 ppc_stub_plt_call:
2c66dc6c
AM
3575 Used to call a function in a shared library. If it so happens that
3576 the plt entry referenced crosses a 64k boundary, then an extra
ac2df442 3577 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
87e226ce 3578 . addis %r12,%r2,xxx@toc@ha
721956f4 3579 . std %r2,40(%r1)
87e226ce 3580 . ld %r11,xxx+0@toc@l(%r12)
721956f4 3581 . mtctr %r11
ac2df442 3582 . ld %r2,xxx+8@toc@l(%r12)
87e226ce 3583 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3584 . bctr
ad8e1ba5
AM
3585
3586 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3587 code to adjust the value and save r2 to support multiple toc sections.
3588 A ppc_stub_long_branch with an r2 offset looks like:
3589 . std %r2,40(%r1)
3590 . addis %r2,%r2,off@ha
3591 . addi %r2,%r2,off@l
3592 . b dest
3593
3594 A ppc_stub_plt_branch with an r2 offset looks like:
3595 . std %r2,40(%r1)
3596 . addis %r12,%r2,xxx@toc@ha
3597 . ld %r11,xxx@toc@l(%r12)
3598 . addis %r2,%r2,off@ha
3599 . addi %r2,%r2,off@l
3600 . mtctr %r11
3601 . bctr
ac2df442
AM
3602
3603 In cases where the "addis" instruction would add zero, the "addis" is
3604 omitted and following instructions modified slightly in some cases.
721956f4
AM
3605*/
3606
3607enum ppc_stub_type {
3608 ppc_stub_none,
3609 ppc_stub_long_branch,
ad8e1ba5 3610 ppc_stub_long_branch_r2off,
721956f4 3611 ppc_stub_plt_branch,
ad8e1ba5 3612 ppc_stub_plt_branch_r2off,
721956f4
AM
3613 ppc_stub_plt_call
3614};
3615
3616struct ppc_stub_hash_entry {
3617
3618 /* Base hash table entry structure. */
3619 struct bfd_hash_entry root;
3620
ad8e1ba5
AM
3621 enum ppc_stub_type stub_type;
3622
721956f4
AM
3623 /* The stub section. */
3624 asection *stub_sec;
3625
3626 /* Offset within stub_sec of the beginning of this stub. */
3627 bfd_vma stub_offset;
3628
3629 /* Given the symbol's value and its section we can determine its final
3630 value when building the stubs (so the stub knows where to jump. */
3631 bfd_vma target_value;
3632 asection *target_section;
3633
721956f4
AM
3634 /* The symbol table entry, if any, that this was derived from. */
3635 struct ppc_link_hash_entry *h;
e054468f 3636 struct plt_entry *plt_ent;
721956f4 3637
411e1bfb
AM
3638 /* And the reloc addend that this was derived from. */
3639 bfd_vma addend;
3640
721956f4
AM
3641 /* Where this stub is being called from, or, in the case of combined
3642 stub sections, the first input section in the group. */
3643 asection *id_sec;
3644};
3645
3646struct ppc_branch_hash_entry {
3647
3648 /* Base hash table entry structure. */
3649 struct bfd_hash_entry root;
3650
c456f082 3651 /* Offset within branch lookup table. */
721956f4
AM
3652 unsigned int offset;
3653
3654 /* Generation marker. */
3655 unsigned int iter;
3656};
65f38f15
AM
3657
3658struct ppc_link_hash_entry
3659{
3660 struct elf_link_hash_entry elf;
3661
b3fac117
AM
3662 union {
3663 /* A pointer to the most recently used stub hash entry against this
3664 symbol. */
3665 struct ppc_stub_hash_entry *stub_cache;
3666
3667 /* A pointer to the next symbol starting with a '.' */
3668 struct ppc_link_hash_entry *next_dot_sym;
3669 } u;
721956f4 3670
65f38f15
AM
3671 /* Track dynamic relocs copied for this symbol. */
3672 struct ppc_dyn_relocs *dyn_relocs;
e86ce104 3673
721956f4 3674 /* Link between function code and descriptor symbols. */
34814b9f 3675 struct ppc_link_hash_entry *oh;
721956f4 3676
e86ce104
AM
3677 /* Flag function code and descriptor symbols. */
3678 unsigned int is_func:1;
3679 unsigned int is_func_descriptor:1;
908b32fc 3680 unsigned int fake:1;
411e1bfb 3681
c5614fa4
AM
3682 /* Whether global opd/toc sym has been adjusted or not.
3683 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3684 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3685 unsigned int adjust_done:1;
3686
99877b66
AM
3687 /* Set if we twiddled this symbol to weak at some stage. */
3688 unsigned int was_undefined:1;
3689
411e1bfb 3690 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3691 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3692 corresponding relocs are encountered during check_relocs.
3693 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3694 indicate the corresponding GOT entry type is not needed.
3695 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3696 a TPREL one. We use a separate flag rather than setting TPREL
3697 just for convenience in distinguishing the two cases. */
3698#define TLS_GD 1 /* GD reloc. */
3699#define TLS_LD 2 /* LD reloc. */
3700#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3701#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3702#define TLS_TLS 16 /* Any TLS reloc. */
3703#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3704#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3705#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3706 unsigned char tls_mask;
65f38f15
AM
3707};
3708
3709/* ppc64 ELF linker hash table. */
3710
3711struct ppc_link_hash_table
3712{
3713 struct elf_link_hash_table elf;
3714
721956f4
AM
3715 /* The stub hash table. */
3716 struct bfd_hash_table stub_hash_table;
3717
3718 /* Another hash table for plt_branch stubs. */
3719 struct bfd_hash_table branch_hash_table;
3720
3721 /* Linker stub bfd. */
3722 bfd *stub_bfd;
3723
3724 /* Linker call-backs. */
4ce794b7
AM
3725 asection * (*add_stub_section) (const char *, asection *);
3726 void (*layout_sections_again) (void);
721956f4
AM
3727
3728 /* Array to keep track of which stub sections have been created, and
3729 information on stub grouping. */
3730 struct map_stub {
3731 /* This is the section to which stubs in the group will be attached. */
3732 asection *link_sec;
3733 /* The stub section. */
3734 asection *stub_sec;
ad8e1ba5
AM
3735 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3736 bfd_vma toc_off;
721956f4
AM
3737 } *stub_group;
3738
ad8e1ba5
AM
3739 /* Temp used when calculating TOC pointers. */
3740 bfd_vma toc_curr;
bf102f86
AM
3741 bfd *toc_bfd;
3742 asection *toc_first_sec;
ad8e1ba5 3743
8f3bab57
AM
3744 /* Highest input section id. */
3745 int top_id;
3746
734b6cf9
AM
3747 /* Highest output section index. */
3748 int top_index;
3749
b3fac117
AM
3750 /* Used when adding symbols. */
3751 struct ppc_link_hash_entry *dot_syms;
3752
734b6cf9
AM
3753 /* List of input sections for each output section. */
3754 asection **input_list;
721956f4 3755
65f38f15 3756 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3757 asection *got;
4ce794b7
AM
3758 asection *plt;
3759 asection *relplt;
e054468f
AM
3760 asection *iplt;
3761 asection *reliplt;
4ce794b7
AM
3762 asection *dynbss;
3763 asection *relbss;
3764 asection *glink;
82bd7b59 3765 asection *sfpr;
4ce794b7
AM
3766 asection *brlt;
3767 asection *relbrlt;
ec338859 3768
8387904d
AM
3769 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3770 struct ppc_link_hash_entry *tls_get_addr;
3771 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3772
927be08e
AM
3773 /* The size of reliplt used by got entry relocs. */
3774 bfd_size_type got_reli_size;
3775
9b5ecbd0
AM
3776 /* Statistics. */
3777 unsigned long stub_count[ppc_stub_plt_call];
3778
ee75fd95
AM
3779 /* Number of stubs against global syms. */
3780 unsigned long stub_globals;
3781
ad8e1ba5 3782 /* Set if we should emit symbols for stubs. */
99877b66 3783 unsigned int emit_stub_syms:1;
ad8e1ba5 3784
a7f2871e
AM
3785 /* Set if __tls_get_addr optimization should not be done. */
3786 unsigned int no_tls_get_addr_opt:1;
3787
4c52953f 3788 /* Support for multiple toc sections. */
33c0ec9d 3789 unsigned int do_multi_toc:1;
4c52953f 3790 unsigned int multi_toc_needed:1;
927be08e 3791 unsigned int second_toc_pass:1;
67f0cbdb 3792 unsigned int do_toc_opt:1;
4c52953f 3793
5d1634d7 3794 /* Set on error. */
99877b66 3795 unsigned int stub_error:1;
721956f4 3796
7d9616d7 3797 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3798 unsigned int twiddled_syms:1;
721956f4
AM
3799
3800 /* Incremented every time we size stubs. */
3801 unsigned int stub_iteration;
5d1634d7 3802
87d72d41
AM
3803 /* Small local sym cache. */
3804 struct sym_cache sym_cache;
65f38f15
AM
3805};
3806
4c52953f
AM
3807/* Rename some of the generic section flags to better document how they
3808 are used here. */
b0dddeec
AM
3809
3810/* Nonzero if this section has TLS related relocations. */
3811#define has_tls_reloc sec_flg0
3812
3813/* Nonzero if this section has a call to __tls_get_addr. */
3814#define has_tls_get_addr_call sec_flg1
3815
3816/* Nonzero if this section has any toc or got relocs. */
3817#define has_toc_reloc sec_flg2
3818
3819/* Nonzero if this section has a call to another section that uses
3820 the toc or got. */
d77c8a4b 3821#define makes_toc_func_call sec_flg3
b0dddeec
AM
3822
3823/* Recursion protection when determining above flag. */
d77c8a4b 3824#define call_check_in_progress sec_flg4
70cc837d 3825#define call_check_done sec_flg5
4c52953f 3826
65f38f15
AM
3827/* Get the ppc64 ELF linker hash table from a link_info structure. */
3828
3829#define ppc_hash_table(p) \
4dfe6ac6
NC
3830 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3831 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3832
721956f4
AM
3833#define ppc_stub_hash_lookup(table, string, create, copy) \
3834 ((struct ppc_stub_hash_entry *) \
3835 bfd_hash_lookup ((table), (string), (create), (copy)))
3836
3837#define ppc_branch_hash_lookup(table, string, create, copy) \
3838 ((struct ppc_branch_hash_entry *) \
3839 bfd_hash_lookup ((table), (string), (create), (copy)))
3840
3841/* Create an entry in the stub hash table. */
3842
3843static struct bfd_hash_entry *
4ce794b7
AM
3844stub_hash_newfunc (struct bfd_hash_entry *entry,
3845 struct bfd_hash_table *table,
3846 const char *string)
721956f4
AM
3847{
3848 /* Allocate the structure if it has not already been allocated by a
3849 subclass. */
3850 if (entry == NULL)
3851 {
3852 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3853 if (entry == NULL)
3854 return entry;
3855 }
3856
3857 /* Call the allocation method of the superclass. */
3858 entry = bfd_hash_newfunc (entry, table, string);
3859 if (entry != NULL)
3860 {
3861 struct ppc_stub_hash_entry *eh;
3862
3863 /* Initialize the local fields. */
3864 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3865 eh->stub_type = ppc_stub_none;
721956f4
AM
3866 eh->stub_sec = NULL;
3867 eh->stub_offset = 0;
3868 eh->target_value = 0;
3869 eh->target_section = NULL;
721956f4
AM
3870 eh->h = NULL;
3871 eh->id_sec = NULL;
3872 }
3873
3874 return entry;
3875}
3876
3877/* Create an entry in the branch hash table. */
3878
3879static struct bfd_hash_entry *
4ce794b7
AM
3880branch_hash_newfunc (struct bfd_hash_entry *entry,
3881 struct bfd_hash_table *table,
3882 const char *string)
721956f4
AM
3883{
3884 /* Allocate the structure if it has not already been allocated by a
3885 subclass. */
3886 if (entry == NULL)
3887 {
3888 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3889 if (entry == NULL)
3890 return entry;
3891 }
3892
3893 /* Call the allocation method of the superclass. */
3894 entry = bfd_hash_newfunc (entry, table, string);
3895 if (entry != NULL)
3896 {
3897 struct ppc_branch_hash_entry *eh;
3898
3899 /* Initialize the local fields. */
3900 eh = (struct ppc_branch_hash_entry *) entry;
3901 eh->offset = 0;
3902 eh->iter = 0;
3903 }
3904
3905 return entry;
3906}
3907
65f38f15
AM
3908/* Create an entry in a ppc64 ELF linker hash table. */
3909
3910static struct bfd_hash_entry *
4ce794b7
AM
3911link_hash_newfunc (struct bfd_hash_entry *entry,
3912 struct bfd_hash_table *table,
3913 const char *string)
65f38f15
AM
3914{
3915 /* Allocate the structure if it has not already been allocated by a
3916 subclass. */
3917 if (entry == NULL)
3918 {
3919 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3920 if (entry == NULL)
3921 return entry;
3922 }
3923
3924 /* Call the allocation method of the superclass. */
3925 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3926 if (entry != NULL)
3927 {
3928 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3929
b3fac117 3930 memset (&eh->u.stub_cache, 0,
908b32fc 3931 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3932 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3933
3934 /* When making function calls, old ABI code references function entry
3935 points (dot symbols), while new ABI code references the function
3936 descriptor symbol. We need to make any combination of reference and
3937 definition work together, without breaking archive linking.
3938
3939 For a defined function "foo" and an undefined call to "bar":
3940 An old object defines "foo" and ".foo", references ".bar" (possibly
3941 "bar" too).
3942 A new object defines "foo" and references "bar".
3943
3944 A new object thus has no problem with its undefined symbols being
3945 satisfied by definitions in an old object. On the other hand, the
3946 old object won't have ".bar" satisfied by a new object.
3947
3948 Keep a list of newly added dot-symbols. */
3949
3950 if (string[0] == '.')
3951 {
3952 struct ppc_link_hash_table *htab;
3953
3954 htab = (struct ppc_link_hash_table *) table;
3955 eh->u.next_dot_sym = htab->dot_syms;
3956 htab->dot_syms = eh;
3957 }
65f38f15
AM
3958 }
3959
3960 return entry;
3961}
3962
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
3254fd24
AM
3993 /* Initializing two fields of the union is just cosmetic. We really
3994 only care about glist, but when compiled on a 32-bit host the
3995 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3996 debugger inspection of these fields look nicer. */
a6aa5195
AM
3997 htab->elf.init_got_refcount.refcount = 0;
3998 htab->elf.init_got_refcount.glist = NULL;
3999 htab->elf.init_plt_refcount.refcount = 0;
4000 htab->elf.init_plt_refcount.glist = NULL;
4001 htab->elf.init_got_offset.offset = 0;
4002 htab->elf.init_got_offset.glist = NULL;
4003 htab->elf.init_plt_offset.offset = 0;
4004 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4005
65f38f15
AM
4006 return &htab->elf.root;
4007}
4008
721956f4
AM
4009/* Free the derived linker hash table. */
4010
4011static void
4ce794b7 4012ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4
AM
4013{
4014 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
4015
4016 bfd_hash_table_free (&ret->stub_hash_table);
4017 bfd_hash_table_free (&ret->branch_hash_table);
4018 _bfd_generic_link_hash_table_free (hash);
4019}
4020
e717da7e
AM
4021/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4022
4023void
4024ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4025{
4026 struct ppc_link_hash_table *htab;
4027
4028 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4029
4030/* Always hook our dynamic sections into the first bfd, which is the
4031 linker created stub bfd. This ensures that the GOT header is at
4032 the start of the output TOC section. */
4033 htab = ppc_hash_table (info);
4dfe6ac6
NC
4034 if (htab == NULL)
4035 return;
e717da7e
AM
4036 htab->stub_bfd = abfd;
4037 htab->elf.dynobj = abfd;
4038}
4039
721956f4
AM
4040/* Build a name for an entry in the stub hash table. */
4041
4042static char *
4ce794b7
AM
4043ppc_stub_name (const asection *input_section,
4044 const asection *sym_sec,
4045 const struct ppc_link_hash_entry *h,
4046 const Elf_Internal_Rela *rel)
721956f4
AM
4047{
4048 char *stub_name;
4049 bfd_size_type len;
4050
4051 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4052 offsets from a sym as a branch target? In fact, we could
4053 probably assume the addend is always zero. */
4054 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4055
4056 if (h)
4057 {
4058 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4059 stub_name = bfd_malloc (len);
46de2a7c
AM
4060 if (stub_name == NULL)
4061 return stub_name;
4062
4063 sprintf (stub_name, "%08x.%s+%x",
4064 input_section->id & 0xffffffff,
4065 h->elf.root.root.string,
4066 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4067 }
4068 else
4069 {
ad8e1ba5 4070 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4071 stub_name = bfd_malloc (len);
46de2a7c
AM
4072 if (stub_name == NULL)
4073 return stub_name;
4074
4075 sprintf (stub_name, "%08x.%x:%x+%x",
4076 input_section->id & 0xffffffff,
4077 sym_sec->id & 0xffffffff,
4078 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4079 (int) rel->r_addend & 0xffffffff);
721956f4 4080 }
ee75fd95
AM
4081 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4082 stub_name[len - 2] = 0;
721956f4
AM
4083 return stub_name;
4084}
4085
4086/* Look up an entry in the stub hash. Stub entries are cached because
4087 creating the stub name takes a bit of time. */
4088
4089static struct ppc_stub_hash_entry *
4ce794b7
AM
4090ppc_get_stub_entry (const asection *input_section,
4091 const asection *sym_sec,
039b3fef 4092 struct ppc_link_hash_entry *h,
4ce794b7
AM
4093 const Elf_Internal_Rela *rel,
4094 struct ppc_link_hash_table *htab)
721956f4
AM
4095{
4096 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4097 const asection *id_sec;
4098
4099 /* If this input section is part of a group of sections sharing one
4100 stub section, then use the id of the first section in the group.
4101 Stub names need to include a section id, as there may well be
4102 more than one stub used to reach say, printf, and we need to
4103 distinguish between them. */
4104 id_sec = htab->stub_group[input_section->id].link_sec;
4105
b3fac117
AM
4106 if (h != NULL && h->u.stub_cache != NULL
4107 && h->u.stub_cache->h == h
4108 && h->u.stub_cache->id_sec == id_sec)
721956f4 4109 {
b3fac117 4110 stub_entry = h->u.stub_cache;
721956f4
AM
4111 }
4112 else
4113 {
4114 char *stub_name;
4115
4116 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4117 if (stub_name == NULL)
4118 return NULL;
4119
4120 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4121 stub_name, FALSE, FALSE);
721956f4 4122 if (h != NULL)
b3fac117 4123 h->u.stub_cache = stub_entry;
721956f4
AM
4124
4125 free (stub_name);
4126 }
4127
4128 return stub_entry;
4129}
4130
4131/* Add a new stub entry to the stub hash. Not all fields of the new
4132 stub entry are initialised. */
4133
4134static struct ppc_stub_hash_entry *
4ce794b7
AM
4135ppc_add_stub (const char *stub_name,
4136 asection *section,
4137 struct ppc_link_hash_table *htab)
721956f4
AM
4138{
4139 asection *link_sec;
4140 asection *stub_sec;
4141 struct ppc_stub_hash_entry *stub_entry;
4142
4143 link_sec = htab->stub_group[section->id].link_sec;
4144 stub_sec = htab->stub_group[section->id].stub_sec;
4145 if (stub_sec == NULL)
4146 {
4147 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4148 if (stub_sec == NULL)
4149 {
d4c88bbb 4150 size_t namelen;
721956f4
AM
4151 bfd_size_type len;
4152 char *s_name;
4153
d4c88bbb
AM
4154 namelen = strlen (link_sec->name);
4155 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4156 s_name = bfd_alloc (htab->stub_bfd, len);
4157 if (s_name == NULL)
4158 return NULL;
4159
d4c88bbb
AM
4160 memcpy (s_name, link_sec->name, namelen);
4161 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4162 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4163 if (stub_sec == NULL)
4164 return NULL;
4165 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4166 }
4167 htab->stub_group[section->id].stub_sec = stub_sec;
4168 }
4169
4170 /* Enter this entry into the linker stub hash table. */
4171 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4172 TRUE, FALSE);
721956f4
AM
4173 if (stub_entry == NULL)
4174 {
d003868e
AM
4175 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
4176 section->owner, stub_name);
721956f4
AM
4177 return NULL;
4178 }
4179
4180 stub_entry->stub_sec = stub_sec;
4181 stub_entry->stub_offset = 0;
4182 stub_entry->id_sec = link_sec;
4183 return stub_entry;
4184}
4185
82bd7b59
AM
4186/* Create sections for linker generated code. */
4187
b34976b6 4188static bfd_boolean
4ce794b7 4189create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
4190{
4191 struct ppc_link_hash_table *htab;
4192 flagword flags;
4193
4194 htab = ppc_hash_table (info);
4dfe6ac6
NC
4195 if (htab == NULL)
4196 return FALSE;
82bd7b59
AM
4197
4198 /* Create .sfpr for code to save and restore fp regs. */
4199 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4200 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
4201 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4202 flags);
82bd7b59 4203 if (htab->sfpr == NULL
82bd7b59 4204 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 4205 return FALSE;
82bd7b59 4206
721956f4 4207 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
4208 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4209 flags);
4ce794b7 4210 if (htab->glink == NULL
ee4bf8d2 4211 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 4212 return FALSE;
82bd7b59 4213
e054468f
AM
4214 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4215 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4216 if (htab->iplt == NULL
4217 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4218 return FALSE;
4219
4220 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4221 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4222 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4223 ".rela.iplt",
4224 flags);
4225 if (htab->reliplt == NULL
4226 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4227 return FALSE;
4228
c456f082 4229 /* Create branch lookup table for plt_branch stubs. */
e4e0193e
AM
4230 flags = (SEC_ALLOC | SEC_LOAD
4231 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4232 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4233 flags);
4ce794b7 4234 if (htab->brlt == NULL
4ce794b7 4235 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 4236 return FALSE;
721956f4 4237
e4e0193e 4238 if (!info->shared)
c456f082
AM
4239 return TRUE;
4240
e4e0193e
AM
4241 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4242 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4243 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4244 ".rela.branch_lt",
4245 flags);
e054468f 4246 if (htab->relbrlt == NULL
c456f082
AM
4247 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4248 return FALSE;
4249
b34976b6 4250 return TRUE;
82bd7b59
AM
4251}
4252
e717da7e
AM
4253/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4254 not already done. */
65f38f15 4255
b34976b6 4256static bfd_boolean
e717da7e 4257create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4258{
e717da7e
AM
4259 asection *got, *relgot;
4260 flagword flags;
4261 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4262
0c8d6e5c 4263 if (!is_ppc64_elf (abfd))
0ffa91dd 4264 return FALSE;
4dfe6ac6
NC
4265 if (htab == NULL)
4266 return FALSE;
0ffa91dd 4267
4ce794b7 4268 if (!htab->got)
e717da7e
AM
4269 {
4270 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4271 return FALSE;
4272
4273 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4274 if (!htab->got)
4275 abort ();
4276 }
4277
4278 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4279 | SEC_LINKER_CREATED);
4280
c456f082 4281 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4282 if (!got
e717da7e
AM
4283 || !bfd_set_section_alignment (abfd, got, 3))
4284 return FALSE;
65f38f15 4285
c456f082
AM
4286 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4287 flags | SEC_READONLY);
e717da7e 4288 if (!relgot
e717da7e 4289 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4290 return FALSE;
e717da7e
AM
4291
4292 ppc64_elf_tdata (abfd)->got = got;
4293 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4294 return TRUE;
65f38f15 4295}
5bd4f169 4296
82bd7b59 4297/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4298
b34976b6 4299static bfd_boolean
4ce794b7 4300ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4301{
65f38f15 4302 struct ppc_link_hash_table *htab;
5bd4f169 4303
65f38f15 4304 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4305 return FALSE;
65f38f15 4306
e717da7e 4307 htab = ppc_hash_table (info);
4dfe6ac6
NC
4308 if (htab == NULL)
4309 return FALSE;
4310
e717da7e
AM
4311 if (!htab->got)
4312 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
4313 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4314 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4315 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 4316 if (!info->shared)
4ce794b7 4317 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 4318
e717da7e 4319 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4320 || (!info->shared && !htab->relbss))
65f38f15
AM
4321 abort ();
4322
b34976b6 4323 return TRUE;
5bd4f169
AM
4324}
4325
b31867b6
AM
4326/* Follow indirect and warning symbol links. */
4327
4328static inline struct bfd_link_hash_entry *
4329follow_link (struct bfd_link_hash_entry *h)
4330{
4331 while (h->type == bfd_link_hash_indirect
4332 || h->type == bfd_link_hash_warning)
4333 h = h->u.i.link;
4334 return h;
4335}
4336
4337static inline struct elf_link_hash_entry *
4338elf_follow_link (struct elf_link_hash_entry *h)
4339{
4340 return (struct elf_link_hash_entry *) follow_link (&h->root);
4341}
4342
4343static inline struct ppc_link_hash_entry *
4344ppc_follow_link (struct ppc_link_hash_entry *h)
4345{
4346 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4347}
4348
40d16e0b
AM
4349/* Merge PLT info on FROM with that on TO. */
4350
4351static void
4352move_plt_plist (struct ppc_link_hash_entry *from,
4353 struct ppc_link_hash_entry *to)
4354{
4355 if (from->elf.plt.plist != NULL)
4356 {
4357 if (to->elf.plt.plist != NULL)
4358 {
4359 struct plt_entry **entp;
4360 struct plt_entry *ent;
4361
4362 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4363 {
4364 struct plt_entry *dent;
4365
4366 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4367 if (dent->addend == ent->addend)
4368 {
4369 dent->plt.refcount += ent->plt.refcount;
4370 *entp = ent->next;
4371 break;
4372 }
4373 if (dent == NULL)
4374 entp = &ent->next;
4375 }
4376 *entp = to->elf.plt.plist;
4377 }
4378
4379 to->elf.plt.plist = from->elf.plt.plist;
4380 from->elf.plt.plist = NULL;
4381 }
4382}
4383
65f38f15
AM
4384/* Copy the extra info we tack onto an elf_link_hash_entry. */
4385
4386static void
fcfa13d2
AM
4387ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4388 struct elf_link_hash_entry *dir,
4389 struct elf_link_hash_entry *ind)
65f38f15
AM
4390{
4391 struct ppc_link_hash_entry *edir, *eind;
4392
4393 edir = (struct ppc_link_hash_entry *) dir;
4394 eind = (struct ppc_link_hash_entry *) ind;
4395
411e1bfb 4396 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4397 if (eind->dyn_relocs != NULL)
65f38f15 4398 {
bbd7ec4a
AM
4399 if (edir->dyn_relocs != NULL)
4400 {
4401 struct ppc_dyn_relocs **pp;
4402 struct ppc_dyn_relocs *p;
4403
fcfa13d2 4404 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4405 list. Merge any entries against the same section. */
4406 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4407 {
4408 struct ppc_dyn_relocs *q;
4409
4410 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4411 if (q->sec == p->sec)
4412 {
4413 q->pc_count += p->pc_count;
4414 q->count += p->count;
4415 *pp = p->next;
4416 break;
4417 }
4418 if (q == NULL)
4419 pp = &p->next;
4420 }
4421 *pp = edir->dyn_relocs;
4422 }
4423
65f38f15
AM
4424 edir->dyn_relocs = eind->dyn_relocs;
4425 eind->dyn_relocs = NULL;
4426 }
65f38f15 4427
6349e628
AM
4428 edir->is_func |= eind->is_func;
4429 edir->is_func_descriptor |= eind->is_func_descriptor;
58ac9f71 4430 edir->tls_mask |= eind->tls_mask;
b31867b6
AM
4431 if (eind->oh != NULL)
4432 edir->oh = ppc_follow_link (eind->oh);
6349e628 4433
81848ca0 4434 /* If called to transfer flags for a weakdef during processing
f5385ebf 4435 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
81848ca0 4436 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
f5385ebf
AM
4437 if (!(ELIMINATE_COPY_RELOCS
4438 && eind->elf.root.type != bfd_link_hash_indirect
4439 && edir->elf.dynamic_adjusted))
4440 edir->elf.non_got_ref |= eind->elf.non_got_ref;
81848ca0 4441
f5385ebf
AM
4442 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4443 edir->elf.ref_regular |= eind->elf.ref_regular;
4444 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4445 edir->elf.needs_plt |= eind->elf.needs_plt;
6349e628
AM
4446
4447 /* If we were called to copy over info for a weak sym, that's all. */
4448 if (eind->elf.root.type != bfd_link_hash_indirect)
4449 return;
4450
81848ca0
AM
4451 /* Copy over got entries that we may have already seen to the
4452 symbol which just became indirect. */
411e1bfb
AM
4453 if (eind->elf.got.glist != NULL)
4454 {
4455 if (edir->elf.got.glist != NULL)
4456 {
4457 struct got_entry **entp;
4458 struct got_entry *ent;
4459
4460 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4461 {
4462 struct got_entry *dent;
4463
4464 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4465 if (dent->addend == ent->addend
e717da7e 4466 && dent->owner == ent->owner
411e1bfb
AM
4467 && dent->tls_type == ent->tls_type)
4468 {
4469 dent->got.refcount += ent->got.refcount;
4470 *entp = ent->next;
4471 break;
4472 }
4473 if (dent == NULL)
4474 entp = &ent->next;
4475 }
4476 *entp = edir->elf.got.glist;
4477 }
4478
4479 edir->elf.got.glist = eind->elf.got.glist;
4480 eind->elf.got.glist = NULL;
4481 }
4482
4483 /* And plt entries. */
40d16e0b 4484 move_plt_plist (eind, edir);
411e1bfb 4485
fcfa13d2 4486 if (eind->elf.dynindx != -1)
411e1bfb 4487 {
fcfa13d2
AM
4488 if (edir->elf.dynindx != -1)
4489 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4490 edir->elf.dynstr_index);
411e1bfb
AM
4491 edir->elf.dynindx = eind->elf.dynindx;
4492 edir->elf.dynstr_index = eind->elf.dynstr_index;
4493 eind->elf.dynindx = -1;
4494 eind->elf.dynstr_index = 0;
4495 }
411e1bfb
AM
4496}
4497
8387904d
AM
4498/* Find the function descriptor hash entry from the given function code
4499 hash entry FH. Link the entries via their OH fields. */
4500
4501static struct ppc_link_hash_entry *
b31867b6 4502lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4503{
4504 struct ppc_link_hash_entry *fdh = fh->oh;
4505
4506 if (fdh == NULL)
4507 {
4508 const char *fd_name = fh->elf.root.root.string + 1;
4509
4510 fdh = (struct ppc_link_hash_entry *)
4511 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4512 if (fdh == NULL)
4513 return fdh;
4514
4515 fdh->is_func_descriptor = 1;
4516 fdh->oh = fh;
4517 fh->is_func = 1;
4518 fh->oh = fdh;
8387904d
AM
4519 }
4520
b31867b6 4521 return ppc_follow_link (fdh);
8387904d
AM
4522}
4523
bb700d78
AM
4524/* Make a fake function descriptor sym for the code sym FH. */
4525
4526static struct ppc_link_hash_entry *
4527make_fdh (struct bfd_link_info *info,
908b32fc 4528 struct ppc_link_hash_entry *fh)
bb700d78
AM
4529{
4530 bfd *abfd;
4531 asymbol *newsym;
4532 struct bfd_link_hash_entry *bh;
4533 struct ppc_link_hash_entry *fdh;
4534
4535 abfd = fh->elf.root.u.undef.abfd;
4536 newsym = bfd_make_empty_symbol (abfd);
4537 newsym->name = fh->elf.root.root.string + 1;
4538 newsym->section = bfd_und_section_ptr;
4539 newsym->value = 0;
908b32fc 4540 newsym->flags = BSF_WEAK;
bb700d78
AM
4541
4542 bh = NULL;
4543 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4544 newsym->flags, newsym->section,
4545 newsym->value, NULL, FALSE, FALSE,
4546 &bh))
4547 return NULL;
4548
4549 fdh = (struct ppc_link_hash_entry *) bh;
4550 fdh->elf.non_elf = 0;
908b32fc
AM
4551 fdh->fake = 1;
4552 fdh->is_func_descriptor = 1;
4553 fdh->oh = fh;
4554 fh->is_func = 1;
4555 fh->oh = fdh;
bb700d78
AM
4556 return fdh;
4557}
4558
8387904d
AM
4559/* Fix function descriptor symbols defined in .opd sections to be
4560 function type. */
555cd476
AM
4561
4562static bfd_boolean
c16153ae 4563ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4564 struct bfd_link_info *info,
555cd476 4565 Elf_Internal_Sym *isym,
b3fac117 4566 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4567 flagword *flags ATTRIBUTE_UNUSED,
4568 asection **sec,
4569 bfd_vma *value ATTRIBUTE_UNUSED)
4570{
e054468f 4571 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4572 {
4573 if ((ibfd->flags & DYNAMIC) == 0)
4574 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4575 }
e054468f
AM
4576 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4577 ;
4578 else if (*sec != NULL
70cc837d 4579 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4580 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4581
555cd476
AM
4582 return TRUE;
4583}
4584
8387904d 4585/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4586 inclusion of a new ABI object archive that defines "bar".
4587 NAME is a symbol defined in an archive. Return a symbol in the hash
4588 table that might be satisfied by the archive symbols. */
8387904d
AM
4589
4590static struct elf_link_hash_entry *
4591ppc64_elf_archive_symbol_lookup (bfd *abfd,
4592 struct bfd_link_info *info,
4593 const char *name)
4594{
4595 struct elf_link_hash_entry *h;
4596 char *dot_name;
4597 size_t len;
4598
4599 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4600 if (h != NULL
4601 /* Don't return this sym if it is a fake function descriptor
4602 created by add_symbol_adjust. */
4603 && !(h->root.type == bfd_link_hash_undefweak
4604 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4605 return h;
4606
4607 if (name[0] == '.')
4608 return h;
4609
4610 len = strlen (name);
4611 dot_name = bfd_alloc (abfd, len + 2);
4612 if (dot_name == NULL)
4613 return (struct elf_link_hash_entry *) 0 - 1;
4614 dot_name[0] = '.';
4615 memcpy (dot_name + 1, name, len + 1);
4616 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4617 bfd_release (abfd, dot_name);
4618 return h;
4619}
4620
4621/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4622 new ABI object defines "bar". Well, at least, undefined dot symbols
4623 are made weak. This stops later archive searches from including an
4624 object if we already have a function descriptor definition. It also
35b0ce59
AM
4625 prevents the linker complaining about undefined symbols.
4626 We also check and correct mismatched symbol visibility here. The
4627 most restrictive visibility of the function descriptor and the
4628 function entry symbol is used. */
8387904d
AM
4629
4630static bfd_boolean
b3fac117 4631add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4632{
8387904d
AM
4633 struct ppc_link_hash_table *htab;
4634 struct ppc_link_hash_entry *fdh;
4635
b3fac117 4636 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4637 return TRUE;
4638
b3fac117
AM
4639 if (eh->elf.root.type == bfd_link_hash_warning)
4640 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4641
b3fac117
AM
4642 if (eh->elf.root.root.string[0] != '.')
4643 abort ();
8387904d 4644
b3fac117 4645 htab = ppc_hash_table (info);
4dfe6ac6
NC
4646 if (htab == NULL)
4647 return FALSE;
4648
b31867b6
AM
4649 fdh = lookup_fdh (eh, htab);
4650 if (fdh == NULL)
4651 {
4652 if (!info->relocatable
4653 && (eh->elf.root.type == bfd_link_hash_undefined
4654 || eh->elf.root.type == bfd_link_hash_undefweak)
4655 && eh->elf.ref_regular)
4656 {
4657 /* Make an undefweak function descriptor sym, which is enough to
4658 pull in an --as-needed shared lib, but won't cause link
4659 errors. Archives are handled elsewhere. */
4660 fdh = make_fdh (info, eh);
4661 if (fdh == NULL)
4662 return FALSE;
4663 fdh->elf.ref_regular = 1;
4664 }
bb700d78 4665 }
b31867b6 4666 else
8387904d 4667 {
35b0ce59
AM
4668 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4669 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4670 if (entry_vis < descr_vis)
4671 fdh->elf.other += entry_vis - descr_vis;
4672 else if (entry_vis > descr_vis)
4673 eh->elf.other += descr_vis - entry_vis;
4674
e87d886e
AM
4675 if ((fdh->elf.root.type == bfd_link_hash_defined
4676 || fdh->elf.root.type == bfd_link_hash_defweak)
4677 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4678 {
4679 eh->elf.root.type = bfd_link_hash_undefweak;
4680 eh->was_undefined = 1;
4681 htab->twiddled_syms = 1;
4682 }
8387904d 4683 }
99877b66 4684
8387904d
AM
4685 return TRUE;
4686}
4687
b3fac117
AM
4688/* Process list of dot-symbols we made in link_hash_newfunc. */
4689
8387904d 4690static bfd_boolean
7d9616d7 4691ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4692{
99877b66 4693 struct ppc_link_hash_table *htab;
b3fac117 4694 struct ppc_link_hash_entry **p, *eh;
433817dd 4695
0c8d6e5c 4696 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4697 return TRUE;
4dfe6ac6
NC
4698 htab = ppc_hash_table (info);
4699 if (htab == NULL)
4700 return FALSE;
35b0ce59 4701
0c8d6e5c 4702 if (is_ppc64_elf (ibfd))
b3fac117
AM
4703 {
4704 p = &htab->dot_syms;
4705 while ((eh = *p) != NULL)
4706 {
4707 *p = NULL;
4708 if (!add_symbol_adjust (eh, info))
4709 return FALSE;
4710 p = &eh->u.next_dot_sym;
4711 }
4712 }
4713
4714 /* Clear the list for non-ppc64 input files. */
4715 p = &htab->dot_syms;
4716 while ((eh = *p) != NULL)
4717 {
4718 *p = NULL;
4719 p = &eh->u.next_dot_sym;
4720 }
99877b66
AM
4721
4722 /* We need to fix the undefs list for any syms we have twiddled to
4723 undef_weak. */
4724 if (htab->twiddled_syms)
4725 {
77cfaee6 4726 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4727 htab->twiddled_syms = 0;
4728 }
b3fac117 4729 return TRUE;
8387904d
AM
4730}
4731
97fed1c9
JJ
4732/* Undo hash table changes when an --as-needed input file is determined
4733 not to be needed. */
4734
4735static bfd_boolean
4736ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4737 struct bfd_link_info *info)
4738{
4dfe6ac6
NC
4739 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4740
4741 if (htab == NULL)
4742 return FALSE;
4743
4744 htab->dot_syms = NULL;
97fed1c9
JJ
4745 return TRUE;
4746}
4747
e054468f 4748static struct plt_entry **
4ce794b7
AM
4749update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4750 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4751{
4752 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4753 struct plt_entry **local_plt;
f961d9dd 4754 unsigned char *local_got_tls_masks;
411e1bfb
AM
4755
4756 if (local_got_ents == NULL)
4757 {
4758 bfd_size_type size = symtab_hdr->sh_info;
4759
e054468f
AM
4760 size *= (sizeof (*local_got_ents)
4761 + sizeof (*local_plt)
4762 + sizeof (*local_got_tls_masks));
4ce794b7 4763 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4764 if (local_got_ents == NULL)
e054468f 4765 return NULL;
411e1bfb
AM
4766 elf_local_got_ents (abfd) = local_got_ents;
4767 }
4768
e054468f 4769 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4770 {
4771 struct got_entry *ent;
4772
4773 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4774 if (ent->addend == r_addend
4775 && ent->owner == abfd
4776 && ent->tls_type == tls_type)
411e1bfb
AM
4777 break;
4778 if (ent == NULL)
4779 {
4780 bfd_size_type amt = sizeof (*ent);
4ce794b7 4781 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4782 if (ent == NULL)
4783 return FALSE;
4784 ent->next = local_got_ents[r_symndx];
4785 ent->addend = r_addend;
e717da7e 4786 ent->owner = abfd;
411e1bfb 4787 ent->tls_type = tls_type;
927be08e 4788 ent->is_indirect = FALSE;
411e1bfb
AM
4789 ent->got.refcount = 0;
4790 local_got_ents[r_symndx] = ent;
4791 }
4792 ent->got.refcount += 1;
4793 }
4794
e054468f 4795 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4796 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 4797 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
4798
4799 return local_plt + r_symndx;
65f38f15
AM
4800}
4801
411e1bfb 4802static bfd_boolean
e054468f 4803update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4804{
411e1bfb 4805 struct plt_entry *ent;
1e2f5b6e 4806
e054468f 4807 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4808 if (ent->addend == addend)
4809 break;
4810 if (ent == NULL)
1e2f5b6e 4811 {
411e1bfb 4812 bfd_size_type amt = sizeof (*ent);
4ce794b7 4813 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4814 if (ent == NULL)
4815 return FALSE;
e054468f 4816 ent->next = *plist;
411e1bfb
AM
4817 ent->addend = addend;
4818 ent->plt.refcount = 0;
e054468f 4819 *plist = ent;
1e2f5b6e 4820 }
411e1bfb 4821 ent->plt.refcount += 1;
b34976b6 4822 return TRUE;
1e2f5b6e
AM
4823}
4824
e054468f
AM
4825static bfd_boolean
4826is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4827{
4828 return (r_type == R_PPC64_REL24
4829 || r_type == R_PPC64_REL14
4830 || r_type == R_PPC64_REL14_BRTAKEN
4831 || r_type == R_PPC64_REL14_BRNTAKEN
4832 || r_type == R_PPC64_ADDR24
4833 || r_type == R_PPC64_ADDR14
4834 || r_type == R_PPC64_ADDR14_BRTAKEN
4835 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4836}
4837
5bd4f169 4838/* Look through the relocs for a section during the first phase, and
65f38f15 4839 calculate needed space in the global offset table, procedure
5d1634d7 4840 linkage table, and dynamic reloc sections. */
5bd4f169 4841
b34976b6 4842static bfd_boolean
4ce794b7
AM
4843ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4844 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4845{
65f38f15 4846 struct ppc_link_hash_table *htab;
5bd4f169 4847 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4848 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4849 const Elf_Internal_Rela *rel;
4850 const Elf_Internal_Rela *rel_end;
5bd4f169 4851 asection *sreloc;
1e2f5b6e 4852 asection **opd_sym_map;
3a71aa26 4853 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 4854
1049f94e 4855 if (info->relocatable)
b34976b6 4856 return TRUE;
5bd4f169 4857
680a3378
AM
4858 /* Don't do anything special with non-loaded, non-alloced sections.
4859 In particular, any relocs in such sections should not affect GOT
4860 and PLT reference counting (ie. we don't allow them to create GOT
4861 or PLT entries), there's no possibility or desire to optimize TLS
4862 relocs, and there's not much point in propagating relocs to shared
4863 libs that the dynamic linker won't relocate. */
4864 if ((sec->flags & SEC_ALLOC) == 0)
4865 return TRUE;
4866
0c8d6e5c 4867 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4868
65f38f15 4869 htab = ppc_hash_table (info);
4dfe6ac6
NC
4870 if (htab == NULL)
4871 return FALSE;
4872
3a71aa26
AM
4873 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4874 FALSE, FALSE, TRUE);
4875 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4876 FALSE, FALSE, TRUE);
0ffa91dd 4877 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4878 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4879 sreloc = NULL;
1e2f5b6e 4880 opd_sym_map = NULL;
70cc837d 4881 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
4882 {
4883 /* Garbage collection needs some extra help with .opd sections.
4884 We don't want to necessarily keep everything referenced by
4885 relocs in .opd, as that would keep all functions. Instead,
4886 if we reference an .opd symbol (a function descriptor), we
4887 want to keep the function code symbol's section. This is
4888 easy for global symbols, but for local syms we need to keep
74f0fb50 4889 information about the associated function section. */
1e2f5b6e
AM
4890 bfd_size_type amt;
4891
74f0fb50 4892 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 4893 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4894 if (opd_sym_map == NULL)
b34976b6 4895 return FALSE;
74f0fb50 4896 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
4897 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4898 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4899 }
5bd4f169 4900
82bd7b59
AM
4901 if (htab->sfpr == NULL
4902 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4903 return FALSE;
82bd7b59 4904
5bd4f169
AM
4905 rel_end = relocs + sec->reloc_count;
4906 for (rel = relocs; rel < rel_end; rel++)
4907 {
4908 unsigned long r_symndx;
4909 struct elf_link_hash_entry *h;
04c9666a 4910 enum elf_ppc64_reloc_type r_type;
727fc41e 4911 int tls_type;
7c8fe5c4 4912 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 4913 struct plt_entry **ifunc;
5bd4f169
AM
4914
4915 r_symndx = ELF64_R_SYM (rel->r_info);
4916 if (r_symndx < symtab_hdr->sh_info)
4917 h = NULL;
4918 else
973a3492
L
4919 {
4920 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4921 h = elf_follow_link (h);
973a3492 4922 }
5bd4f169 4923
727fc41e 4924 tls_type = 0;
e054468f 4925 ifunc = NULL;
25f23106
AM
4926 if (h != NULL)
4927 {
4928 if (h->type == STT_GNU_IFUNC)
4929 {
4930 h->needs_plt = 1;
4931 ifunc = &h->plt.plist;
4932 }
4933 }
4934 else
4935 {
4936 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4937 abfd, r_symndx);
4938 if (isym == NULL)
4939 return FALSE;
4940
4941 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4942 {
4943 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4944 rel->r_addend, PLT_IFUNC);
4945 if (ifunc == NULL)
4946 return FALSE;
4947 }
4948 }
4ce794b7 4949 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
4950 if (is_branch_reloc (r_type))
4951 {
4952 if (h != NULL && (h == tga || h == dottga))
4953 {
4954 if (rel != relocs
4955 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4956 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4957 /* We have a new-style __tls_get_addr call with a marker
4958 reloc. */
4959 ;
4960 else
4961 /* Mark this section as having an old-style call. */
4962 sec->has_tls_get_addr_call = 1;
4963 }
727fc41e 4964
e054468f 4965 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
4966 if (ifunc != NULL
4967 && !update_plt_info (abfd, ifunc, rel->r_addend))
4968 return FALSE;
4969 }
727fc41e 4970
a33d1f77 4971 switch (r_type)
5bd4f169 4972 {
727fc41e
AM
4973 case R_PPC64_TLSGD:
4974 case R_PPC64_TLSLD:
4975 /* These special tls relocs tie a call to __tls_get_addr with
4976 its parameter symbol. */
4977 break;
4978
411e1bfb
AM
4979 case R_PPC64_GOT_TLSLD16:
4980 case R_PPC64_GOT_TLSLD16_LO:
4981 case R_PPC64_GOT_TLSLD16_HI:
4982 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4983 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4984 goto dogottls;
4985
4986 case R_PPC64_GOT_TLSGD16:
4987 case R_PPC64_GOT_TLSGD16_LO:
4988 case R_PPC64_GOT_TLSGD16_HI:
4989 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4990 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4991 goto dogottls;
4992
4993 case R_PPC64_GOT_TPREL16_DS:
4994 case R_PPC64_GOT_TPREL16_LO_DS:
4995 case R_PPC64_GOT_TPREL16_HI:
4996 case R_PPC64_GOT_TPREL16_HA:
1d483afe 4997 if (!info->executable)
411e1bfb
AM
4998 info->flags |= DF_STATIC_TLS;
4999 tls_type = TLS_TLS | TLS_TPREL;
5000 goto dogottls;
5001
5002 case R_PPC64_GOT_DTPREL16_DS:
5003 case R_PPC64_GOT_DTPREL16_LO_DS:
5004 case R_PPC64_GOT_DTPREL16_HI:
5005 case R_PPC64_GOT_DTPREL16_HA:
5006 tls_type = TLS_TLS | TLS_DTPREL;
5007 dogottls:
5008 sec->has_tls_reloc = 1;
5009 /* Fall thru */
5010
5bd4f169 5011 case R_PPC64_GOT16:
5bd4f169 5012 case R_PPC64_GOT16_DS:
65f38f15
AM
5013 case R_PPC64_GOT16_HA:
5014 case R_PPC64_GOT16_HI:
5015 case R_PPC64_GOT16_LO:
5bd4f169 5016 case R_PPC64_GOT16_LO_DS:
65f38f15 5017 /* This symbol requires a global offset table entry. */
4c52953f 5018 sec->has_toc_reloc = 1;
33c0ec9d
AM
5019 if (r_type == R_PPC64_GOT_TLSLD16
5020 || r_type == R_PPC64_GOT_TLSGD16
5021 || r_type == R_PPC64_GOT_TPREL16_DS
5022 || r_type == R_PPC64_GOT_DTPREL16_DS
5023 || r_type == R_PPC64_GOT16
5024 || r_type == R_PPC64_GOT16_DS)
5025 {
5026 htab->do_multi_toc = 1;
d77c8a4b 5027 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5028 }
5029
e717da7e
AM
5030 if (ppc64_elf_tdata (abfd)->got == NULL
5031 && !create_got_section (abfd, info))
b34976b6 5032 return FALSE;
5bd4f169
AM
5033
5034 if (h != NULL)
5035 {
411e1bfb
AM
5036 struct ppc_link_hash_entry *eh;
5037 struct got_entry *ent;
65f38f15 5038
411e1bfb
AM
5039 eh = (struct ppc_link_hash_entry *) h;
5040 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5041 if (ent->addend == rel->r_addend
e717da7e 5042 && ent->owner == abfd
411e1bfb
AM
5043 && ent->tls_type == tls_type)
5044 break;
5045 if (ent == NULL)
5bd4f169 5046 {
411e1bfb 5047 bfd_size_type amt = sizeof (*ent);
4ce794b7 5048 ent = bfd_alloc (abfd, amt);
411e1bfb 5049 if (ent == NULL)
b34976b6 5050 return FALSE;
411e1bfb
AM
5051 ent->next = eh->elf.got.glist;
5052 ent->addend = rel->r_addend;
e717da7e 5053 ent->owner = abfd;
411e1bfb 5054 ent->tls_type = tls_type;
927be08e 5055 ent->is_indirect = FALSE;
411e1bfb
AM
5056 ent->got.refcount = 0;
5057 eh->elf.got.glist = ent;
5bd4f169 5058 }
411e1bfb 5059 ent->got.refcount += 1;
e7b938ca 5060 eh->tls_mask |= tls_type;
5bd4f169 5061 }
411e1bfb
AM
5062 else
5063 /* This is a global offset table entry for a local symbol. */
5064 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5065 rel->r_addend, tls_type))
5066 return FALSE;
5bd4f169
AM
5067 break;
5068
5bd4f169 5069 case R_PPC64_PLT16_HA:
65f38f15
AM
5070 case R_PPC64_PLT16_HI:
5071 case R_PPC64_PLT16_LO:
5072 case R_PPC64_PLT32:
5073 case R_PPC64_PLT64:
5bd4f169 5074 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5075 actually build the entry in adjust_dynamic_symbol,
5076 because this might be a case of linking PIC code without
5077 linking in any dynamic objects, in which case we don't
5078 need to generate a procedure linkage table after all. */
5bd4f169
AM
5079 if (h == NULL)
5080 {
5081 /* It does not make sense to have a procedure linkage
3fad3c7c 5082 table entry for a local symbol. */
5bd4f169 5083 bfd_set_error (bfd_error_bad_value);
b34976b6 5084 return FALSE;
5bd4f169 5085 }
411e1bfb 5086 else
e054468f
AM
5087 {
5088 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5089 return FALSE;
5090 h->needs_plt = 1;
5091 if (h->root.root.string[0] == '.'
5092 && h->root.root.string[1] != '\0')
5093 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5094 }
5bd4f169
AM
5095 break;
5096
5097 /* The following relocations don't need to propagate the
5098 relocation if linking a shared object since they are
5099 section relative. */
5100 case R_PPC64_SECTOFF:
5101 case R_PPC64_SECTOFF_LO:
5102 case R_PPC64_SECTOFF_HI:
5103 case R_PPC64_SECTOFF_HA:
5104 case R_PPC64_SECTOFF_DS:
5105 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5106 case R_PPC64_DTPREL16:
5107 case R_PPC64_DTPREL16_LO:
5108 case R_PPC64_DTPREL16_HI:
5109 case R_PPC64_DTPREL16_HA:
5110 case R_PPC64_DTPREL16_DS:
5111 case R_PPC64_DTPREL16_LO_DS:
5112 case R_PPC64_DTPREL16_HIGHER:
5113 case R_PPC64_DTPREL16_HIGHERA:
5114 case R_PPC64_DTPREL16_HIGHEST:
5115 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5116 break;
5117
ad8e1ba5 5118 /* Nor do these. */
25f23106
AM
5119 case R_PPC64_REL16:
5120 case R_PPC64_REL16_LO:
5121 case R_PPC64_REL16_HI:
5122 case R_PPC64_REL16_HA:
5123 break;
5124
ad8e1ba5 5125 case R_PPC64_TOC16:
33c0ec9d
AM
5126 case R_PPC64_TOC16_DS:
5127 htab->do_multi_toc = 1;
d77c8a4b 5128 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5129 case R_PPC64_TOC16_LO:
5130 case R_PPC64_TOC16_HI:
5131 case R_PPC64_TOC16_HA:
ad8e1ba5 5132 case R_PPC64_TOC16_LO_DS:
4c52953f 5133 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5134 break;
5135
5bd4f169
AM
5136 /* This relocation describes the C++ object vtable hierarchy.
5137 Reconstruct it for later use during GC. */
5138 case R_PPC64_GNU_VTINHERIT:
c152c796 5139 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5140 return FALSE;
5bd4f169
AM
5141 break;
5142
5143 /* This relocation describes which C++ vtable entries are actually
5144 used. Record for later use during GC. */
5145 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5146 BFD_ASSERT (h != NULL);
5147 if (h != NULL
5148 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5149 return FALSE;
5bd4f169
AM
5150 break;
5151
721956f4
AM
5152 case R_PPC64_REL14:
5153 case R_PPC64_REL14_BRTAKEN:
5154 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5155 {
5156 asection *dest = NULL;
5157
5158 /* Heuristic: If jumping outside our section, chances are
5159 we are going to need a stub. */
5160 if (h != NULL)
5161 {
5162 /* If the sym is weak it may be overridden later, so
5163 don't assume we know where a weak sym lives. */
5164 if (h->root.type == bfd_link_hash_defined)
5165 dest = h->root.u.def.section;
5166 }
5167 else
87d72d41
AM
5168 {
5169 Elf_Internal_Sym *isym;
5170
5171 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5172 abfd, r_symndx);
5173 if (isym == NULL)
5174 return FALSE;
5175
5176 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5177 }
5178
220c76dd 5179 if (dest != sec)
7c8fe5c4 5180 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5181 }
721956f4
AM
5182 /* Fall through. */
5183
5d1634d7 5184 case R_PPC64_REL24:
e054468f 5185 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5186 {
5187 /* We may need a .plt entry if the function this reloc
5188 refers to is in a shared lib. */
e054468f 5189 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5190 return FALSE;
e054468f
AM
5191 h->needs_plt = 1;
5192 if (h->root.root.string[0] == '.'
5193 && h->root.root.string[1] != '\0')
5194 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5195 if (h == tga || h == dottga)
411e1bfb 5196 sec->has_tls_reloc = 1;
411e1bfb
AM
5197 }
5198 break;
5199
5200 case R_PPC64_TPREL64:
5201 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5202 if (!info->executable)
411e1bfb
AM
5203 info->flags |= DF_STATIC_TLS;
5204 goto dotlstoc;
5205
5206 case R_PPC64_DTPMOD64:
5207 if (rel + 1 < rel_end
5208 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5209 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5210 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5211 else
951fd09b 5212 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5213 goto dotlstoc;
5214
5215 case R_PPC64_DTPREL64:
5216 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5217 if (rel != relocs
5218 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5219 && rel[-1].r_offset == rel->r_offset - 8)
5220 /* This is the second reloc of a dtpmod, dtprel pair.
5221 Don't mark with TLS_DTPREL. */
5222 goto dodyn;
5223
5224 dotlstoc:
5225 sec->has_tls_reloc = 1;
5226 if (h != NULL)
5227 {
5228 struct ppc_link_hash_entry *eh;
5229 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5230 eh->tls_mask |= tls_type;
411e1bfb
AM
5231 }
5232 else
5233 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5234 rel->r_addend, tls_type))
5235 return FALSE;
5236
7c8fe5c4
AM
5237 ppc64_sec = ppc64_elf_section_data (sec);
5238 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5239 {
3a71aa26
AM
5240 bfd_size_type amt;
5241
e7b938ca 5242 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5243 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5244 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5245 if (ppc64_sec->u.toc.symndx == NULL)
5246 return FALSE;
5247 amt = sec->size * sizeof (bfd_vma) / 8;
5248 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5249 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5250 return FALSE;
7c8fe5c4
AM
5251 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5252 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5253 }
5254 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5255 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5256 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5257
5258 /* Mark the second slot of a GD or LD entry.
5259 -1 to indicate GD and -2 to indicate LD. */
5260 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5261 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5262 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5263 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5264 goto dodyn;
5265
5266 case R_PPC64_TPREL16:
5267 case R_PPC64_TPREL16_LO:
5268 case R_PPC64_TPREL16_HI:
5269 case R_PPC64_TPREL16_HA:
5270 case R_PPC64_TPREL16_DS:
5271 case R_PPC64_TPREL16_LO_DS:
5272 case R_PPC64_TPREL16_HIGHER:
5273 case R_PPC64_TPREL16_HIGHERA:
5274 case R_PPC64_TPREL16_HIGHEST:
5275 case R_PPC64_TPREL16_HIGHESTA:
5276 if (info->shared)
5277 {
1d483afe
AM
5278 if (!info->executable)
5279 info->flags |= DF_STATIC_TLS;
411e1bfb 5280 goto dodyn;
5d1634d7
AM
5281 }
5282 break;
5283
e86ce104 5284 case R_PPC64_ADDR64:
1e2f5b6e 5285 if (opd_sym_map != NULL
1e2f5b6e 5286 && rel + 1 < rel_end
4ce794b7 5287 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5288 {
8387904d
AM
5289 if (h != NULL)
5290 {
5291 if (h->root.root.string[0] == '.'
5292 && h->root.root.string[1] != 0
b31867b6 5293 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5294 ;
5295 else
5296 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5297 }
5298 else
5299 {
5300 asection *s;
87d72d41 5301 Elf_Internal_Sym *isym;
1e2f5b6e 5302
87d72d41
AM
5303 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5304 abfd, r_symndx);
5305 if (isym == NULL)
8387904d 5306 return FALSE;
87d72d41
AM
5307
5308 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5309 if (s != NULL && s != sec)
3f764659 5310 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5311 }
1e2f5b6e 5312 }
e86ce104
AM
5313 /* Fall through. */
5314
04c9666a 5315 case R_PPC64_REL30:
5bd4f169 5316 case R_PPC64_REL32:
04c9666a 5317 case R_PPC64_REL64:
65f38f15
AM
5318 case R_PPC64_ADDR14:
5319 case R_PPC64_ADDR14_BRNTAKEN:
5320 case R_PPC64_ADDR14_BRTAKEN:
5321 case R_PPC64_ADDR16:
5322 case R_PPC64_ADDR16_DS:
5323 case R_PPC64_ADDR16_HA:
5324 case R_PPC64_ADDR16_HI:
5325 case R_PPC64_ADDR16_HIGHER:
5326 case R_PPC64_ADDR16_HIGHERA:
5327 case R_PPC64_ADDR16_HIGHEST:
5328 case R_PPC64_ADDR16_HIGHESTA:
5329 case R_PPC64_ADDR16_LO:
5330 case R_PPC64_ADDR16_LO_DS:
5331 case R_PPC64_ADDR24:
65f38f15 5332 case R_PPC64_ADDR32:
65f38f15
AM
5333 case R_PPC64_UADDR16:
5334 case R_PPC64_UADDR32:
5335 case R_PPC64_UADDR64:
5bd4f169 5336 case R_PPC64_TOC:
81848ca0
AM
5337 if (h != NULL && !info->shared)
5338 /* We may need a copy reloc. */
f5385ebf 5339 h->non_got_ref = 1;
81848ca0 5340
41bd81ab 5341 /* Don't propagate .opd relocs. */
1e2f5b6e 5342 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5343 break;
e86ce104 5344
65f38f15
AM
5345 /* If we are creating a shared library, and this is a reloc
5346 against a global symbol, or a non PC relative reloc
5347 against a local symbol, then we need to copy the reloc
5348 into the shared library. However, if we are linking with
5349 -Bsymbolic, we do not need to copy a reloc against a
5350 global symbol which is defined in an object we are
5351 including in the link (i.e., DEF_REGULAR is set). At
5352 this point we have not seen all the input files, so it is
5353 possible that DEF_REGULAR is not set now but will be set
5354 later (it is never cleared). In case of a weak definition,
5355 DEF_REGULAR may be cleared later by a strong definition in
5356 a shared library. We account for that possibility below by
f4656909 5357 storing information in the dyn_relocs field of the hash
65f38f15
AM
5358 table entry. A similar situation occurs when creating
5359 shared libraries and symbol visibility changes render the
5360 symbol local.
5361
5362 If on the other hand, we are creating an executable, we
5363 may need to keep relocations for symbols satisfied by a
5364 dynamic library if we manage to avoid copy relocs for the
5365 symbol. */
411e1bfb 5366 dodyn:
65f38f15 5367 if ((info->shared
1d483afe 5368 && (must_be_dyn_reloc (info, r_type)
65f38f15
AM
5369 || (h != NULL
5370 && (! info->symbolic
5371 || h->root.type == bfd_link_hash_defweak
f5385ebf 5372 || !h->def_regular))))
f4656909
AM
5373 || (ELIMINATE_COPY_RELOCS
5374 && !info->shared
65f38f15
AM
5375 && h != NULL
5376 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5377 || !h->def_regular))
5378 || (!info->shared
5379 && ifunc != NULL))
5bd4f169 5380 {
ec338859
AM
5381 struct ppc_dyn_relocs *p;
5382 struct ppc_dyn_relocs **head;
5383
65f38f15
AM
5384 /* We must copy these reloc types into the output file.
5385 Create a reloc section in dynobj and make room for
5386 this reloc. */
5bd4f169
AM
5387 if (sreloc == NULL)
5388 {
83bac4b0
NC
5389 sreloc = _bfd_elf_make_dynamic_reloc_section
5390 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5391
5bd4f169 5392 if (sreloc == NULL)
83bac4b0 5393 return FALSE;
5bd4f169
AM
5394 }
5395
65f38f15
AM
5396 /* If this is a global symbol, we count the number of
5397 relocations we need for this symbol. */
5398 if (h != NULL)
5399 {
ec338859 5400 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
5401 }
5402 else
5403 {
ec338859
AM
5404 /* Track dynamic relocs needed for local syms too.
5405 We really need local syms available to do this
5406 easily. Oh well. */
ec338859 5407 asection *s;
6edfbbad 5408 void *vpp;
87d72d41 5409 Elf_Internal_Sym *isym;
6edfbbad 5410
87d72d41
AM
5411 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5412 abfd, r_symndx);
5413 if (isym == NULL)
b34976b6 5414 return FALSE;
ec338859 5415
87d72d41
AM
5416 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5417 if (s == NULL)
5418 s = sec;
5419
6edfbbad
DJ
5420 vpp = &elf_section_data (s)->local_dynrel;
5421 head = (struct ppc_dyn_relocs **) vpp;
65f38f15 5422 }
ec338859
AM
5423
5424 p = *head;
5425 if (p == NULL || p->sec != sec)
5426 {
4ce794b7 5427 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 5428 if (p == NULL)
b34976b6 5429 return FALSE;
ec338859
AM
5430 p->next = *head;
5431 *head = p;
5432 p->sec = sec;
5433 p->count = 0;
5434 p->pc_count = 0;
5435 }
5436
5437 p->count += 1;
1d483afe 5438 if (!must_be_dyn_reloc (info, r_type))
ec338859 5439 p->pc_count += 1;
65f38f15 5440 }
5bd4f169 5441 break;
65f38f15
AM
5442
5443 default:
96e0dda4 5444 break;
5bd4f169
AM
5445 }
5446 }
5447
b34976b6 5448 return TRUE;
5bd4f169
AM
5449}
5450
8387904d
AM
5451/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5452 of the code entry point, and its section. */
5453
5454static bfd_vma
5455opd_entry_value (asection *opd_sec,
5456 bfd_vma offset,
5457 asection **code_sec,
5458 bfd_vma *code_off)
5459{
5460 bfd *opd_bfd = opd_sec->owner;
8860955f 5461 Elf_Internal_Rela *relocs;
8387904d 5462 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5463 bfd_vma val;
8387904d 5464
4b85d634
AM
5465 /* No relocs implies we are linking a --just-symbols object. */
5466 if (opd_sec->reloc_count == 0)
5467 {
4b85d634
AM
5468 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
5469 return (bfd_vma) -1;
3b36f7e6 5470
4b85d634
AM
5471 if (code_sec != NULL)
5472 {
5473 asection *sec, *likely = NULL;
5474 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5475 if (sec->vma <= val
5476 && (sec->flags & SEC_LOAD) != 0
5477 && (sec->flags & SEC_ALLOC) != 0)
5478 likely = sec;
5479 if (likely != NULL)
5480 {
5481 *code_sec = likely;
5482 if (code_off != NULL)
5483 *code_off = val - likely->vma;
5484 }
5485 }
5486 return val;
5487 }
5488
0c8d6e5c 5489 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5490
8860955f
AM
5491 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5492 if (relocs == NULL)
5493 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5494
8387904d 5495 /* Go find the opd reloc at the sym address. */
8860955f 5496 lo = relocs;
8387904d
AM
5497 BFD_ASSERT (lo != NULL);
5498 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5499 val = (bfd_vma) -1;
8387904d
AM
5500 while (lo < hi)
5501 {
5502 look = lo + (hi - lo) / 2;
5503 if (look->r_offset < offset)
5504 lo = look + 1;
5505 else if (look->r_offset > offset)
5506 hi = look;
5507 else
5508 {
0ffa91dd
NC
5509 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5510
8387904d
AM
5511 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5512 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5513 {
5514 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5515 asection *sec;
5516
5517 if (symndx < symtab_hdr->sh_info)
5518 {
5519 Elf_Internal_Sym *sym;
5520
5521 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5522 if (sym == NULL)
5523 {
5524 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5525 symtab_hdr->sh_info,
5526 0, NULL, NULL, NULL);
5527 if (sym == NULL)
645ea6a9 5528 break;
8387904d
AM
5529 symtab_hdr->contents = (bfd_byte *) sym;
5530 }
5531
5532 sym += symndx;
5533 val = sym->st_value;
cb33740c 5534 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5535 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5536 }
5537 else
5538 {
5539 struct elf_link_hash_entry **sym_hashes;
5540 struct elf_link_hash_entry *rh;
5541
5542 sym_hashes = elf_sym_hashes (opd_bfd);
5543 rh = sym_hashes[symndx - symtab_hdr->sh_info];
b31867b6 5544 rh = elf_follow_link (rh);
8387904d
AM
5545 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5546 || rh->root.type == bfd_link_hash_defweak);
5547 val = rh->root.u.def.value;
5548 sec = rh->root.u.def.section;
5549 }
5550 val += look->r_addend;
5551 if (code_off != NULL)
5552 *code_off = val;
5553 if (code_sec != NULL)
5554 *code_sec = sec;
5555 if (sec != NULL && sec->output_section != NULL)
5556 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5557 }
5558 break;
5559 }
5560 }
645ea6a9 5561
645ea6a9 5562 return val;
8387904d
AM
5563}
5564
854b41e7
AM
5565/* Return true if symbol is defined in a regular object file. */
5566
5567static bfd_boolean
5568is_static_defined (struct elf_link_hash_entry *h)
5569{
5570 return ((h->root.type == bfd_link_hash_defined
5571 || h->root.type == bfd_link_hash_defweak)
5572 && h->root.u.def.section != NULL
5573 && h->root.u.def.section->output_section != NULL);
5574}
5575
b31867b6
AM
5576/* If FDH is a function descriptor symbol, return the associated code
5577 entry symbol if it is defined. Return NULL otherwise. */
5578
5579static struct ppc_link_hash_entry *
5580defined_code_entry (struct ppc_link_hash_entry *fdh)
5581{
5582 if (fdh->is_func_descriptor)
5583 {
5584 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5585 if (fh->elf.root.type == bfd_link_hash_defined
5586 || fh->elf.root.type == bfd_link_hash_defweak)
5587 return fh;
5588 }
5589 return NULL;
5590}
5591
5592/* If FH is a function code entry symbol, return the associated
5593 function descriptor symbol if it is defined. Return NULL otherwise. */
5594
5595static struct ppc_link_hash_entry *
5596defined_func_desc (struct ppc_link_hash_entry *fh)
5597{
5598 if (fh->oh != NULL
5599 && fh->oh->is_func_descriptor)
5600 {
5601 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5602 if (fdh->elf.root.type == bfd_link_hash_defined
5603 || fdh->elf.root.type == bfd_link_hash_defweak)
5604 return fdh;
5605 }
5606 return NULL;
5607}
5608
74f0fb50
AM
5609/* Mark all our entry sym sections, both opd and code section. */
5610
5611static void
5612ppc64_elf_gc_keep (struct bfd_link_info *info)
5613{
5614 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5615 struct bfd_sym_chain *sym;
5616
4dfe6ac6
NC
5617 if (htab == NULL)
5618 return;
5619
74f0fb50
AM
5620 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5621 {
b31867b6 5622 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5623 asection *sec;
5624
5625 eh = (struct ppc_link_hash_entry *)
b31867b6 5626 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5627 if (eh == NULL)
5628 continue;
5629 if (eh->elf.root.type != bfd_link_hash_defined
5630 && eh->elf.root.type != bfd_link_hash_defweak)
5631 continue;
5632
b31867b6
AM
5633 fh = defined_code_entry (eh);
5634 if (fh != NULL)
74f0fb50 5635 {
b31867b6 5636 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5637 sec->flags |= SEC_KEEP;
5638 }
5639 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5640 && opd_entry_value (eh->elf.root.u.def.section,
5641 eh->elf.root.u.def.value,
5642 &sec, NULL) != (bfd_vma) -1)
5643 sec->flags |= SEC_KEEP;
5644
5645 sec = eh->elf.root.u.def.section;
5646 sec->flags |= SEC_KEEP;
5647 }
5648}
5649
64d03ab5
AM
5650/* Mark sections containing dynamically referenced symbols. When
5651 building shared libraries, we must assume that any visible symbol is
5652 referenced. */
5653
5654static bfd_boolean
5655ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5656{
5657 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5658 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5659 struct ppc_link_hash_entry *fdh;
64d03ab5
AM
5660
5661 if (eh->elf.root.type == bfd_link_hash_warning)
5662 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5663
5664 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5665 fdh = defined_func_desc (eh);
5666 if (fdh != NULL)
5667 eh = fdh;
64d03ab5
AM
5668
5669 if ((eh->elf.root.type == bfd_link_hash_defined
5670 || eh->elf.root.type == bfd_link_hash_defweak)
5671 && (eh->elf.ref_dynamic
5672 || (!info->executable
5673 && eh->elf.def_regular
5674 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5675 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5676 {
5677 asection *code_sec;
b31867b6 5678 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5679
5680 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5681
5682 /* Function descriptor syms cause the associated
5683 function code sym section to be marked. */
b31867b6
AM
5684 fh = defined_code_entry (eh);
5685 if (fh != NULL)
5686 {
5687 code_sec = fh->elf.root.u.def.section;
5688 code_sec->flags |= SEC_KEEP;
5689 }
64d03ab5
AM
5690 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5691 && opd_entry_value (eh->elf.root.u.def.section,
5692 eh->elf.root.u.def.value,
5693 &code_sec, NULL) != (bfd_vma) -1)
5694 code_sec->flags |= SEC_KEEP;
5695 }
5696
5697 return TRUE;
5698}
5699
5bd4f169
AM
5700/* Return the section that should be marked against GC for a given
5701 relocation. */
5702
5703static asection *
4ce794b7 5704ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5705 struct bfd_link_info *info,
4ce794b7
AM
5706 Elf_Internal_Rela *rel,
5707 struct elf_link_hash_entry *h,
5708 Elf_Internal_Sym *sym)
5bd4f169 5709{
ccfa59ea
AM
5710 asection *rsec;
5711
ccfa59ea
AM
5712 /* Syms return NULL if we're marking .opd, so we avoid marking all
5713 function sections, as all functions are referenced in .opd. */
5714 rsec = NULL;
5715 if (get_opd_info (sec) != NULL)
5716 return rsec;
1e2f5b6e 5717
5bd4f169
AM
5718 if (h != NULL)
5719 {
04c9666a 5720 enum elf_ppc64_reloc_type r_type;
b31867b6 5721 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5722
4ce794b7 5723 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5724 switch (r_type)
5bd4f169
AM
5725 {
5726 case R_PPC64_GNU_VTINHERIT:
5727 case R_PPC64_GNU_VTENTRY:
5728 break;
5729
5730 default:
5731 switch (h->root.type)
5732 {
5733 case bfd_link_hash_defined:
5734 case bfd_link_hash_defweak:
ccfa59ea 5735 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5736 fdh = defined_func_desc (eh);
5737 if (fdh != NULL)
5738 eh = fdh;
1e2f5b6e
AM
5739
5740 /* Function descriptor syms cause the associated
5741 function code sym section to be marked. */
b31867b6
AM
5742 fh = defined_code_entry (eh);
5743 if (fh != NULL)
ccfa59ea
AM
5744 {
5745 /* They also mark their opd section. */
74f0fb50 5746 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5747
b31867b6 5748 rsec = fh->elf.root.u.def.section;
ccfa59ea 5749 }
8387904d
AM
5750 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5751 && opd_entry_value (eh->elf.root.u.def.section,
5752 eh->elf.root.u.def.value,
5753 &rsec, NULL) != (bfd_vma) -1)
74f0fb50 5754 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5755 else
1e2f5b6e
AM
5756 rsec = h->root.u.def.section;
5757 break;
5bd4f169
AM
5758
5759 case bfd_link_hash_common:
1e2f5b6e
AM
5760 rsec = h->root.u.c.p->section;
5761 break;
5bd4f169
AM
5762
5763 default:
fb34365b 5764 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5765 }
5766 }
5767 }
5768 else
5769 {
74f0fb50 5770 struct _opd_sec_data *opd;
1e2f5b6e
AM
5771
5772 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5773 opd = get_opd_info (rsec);
5774 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5775 {
74f0fb50 5776 rsec->gc_mark = 1;
ccfa59ea 5777
74f0fb50 5778 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5779 }
5bd4f169
AM
5780 }
5781
1e2f5b6e 5782 return rsec;
5bd4f169
AM
5783}
5784
65f38f15
AM
5785/* Update the .got, .plt. and dynamic reloc reference counts for the
5786 section being removed. */
5bd4f169 5787
b34976b6 5788static bfd_boolean
4ce794b7
AM
5789ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5790 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5791{
411e1bfb 5792 struct ppc_link_hash_table *htab;
5bd4f169
AM
5793 Elf_Internal_Shdr *symtab_hdr;
5794 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5795 struct got_entry **local_got_ents;
5bd4f169 5796 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5797
7dda2462
TG
5798 if (info->relocatable)
5799 return TRUE;
5800
680a3378
AM
5801 if ((sec->flags & SEC_ALLOC) == 0)
5802 return TRUE;
5803
ec338859
AM
5804 elf_section_data (sec)->local_dynrel = NULL;
5805
411e1bfb 5806 htab = ppc_hash_table (info);
4dfe6ac6
NC
5807 if (htab == NULL)
5808 return FALSE;
5809
0ffa91dd 5810 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5811 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5812 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5813
5814 relend = relocs + sec->reloc_count;
5815 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5816 {
5817 unsigned long r_symndx;
04c9666a 5818 enum elf_ppc64_reloc_type r_type;
58ac9f71 5819 struct elf_link_hash_entry *h = NULL;
f961d9dd 5820 unsigned char tls_type = 0;
5bd4f169 5821
a33d1f77 5822 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5823 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5824 if (r_symndx >= symtab_hdr->sh_info)
5825 {
5826 struct ppc_link_hash_entry *eh;
5827 struct ppc_dyn_relocs **pp;
5828 struct ppc_dyn_relocs *p;
5829
5830 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5831 h = elf_follow_link (h);
58ac9f71
AM
5832 eh = (struct ppc_link_hash_entry *) h;
5833
5834 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5835 if (p->sec == sec)
5836 {
5837 /* Everything must go for SEC. */
5838 *pp = p->next;
5839 break;
5840 }
5841 }
5842
e054468f
AM
5843 if (is_branch_reloc (r_type))
5844 {
5845 struct plt_entry **ifunc = NULL;
5846 if (h != NULL)
5847 {
5848 if (h->type == STT_GNU_IFUNC)
5849 ifunc = &h->plt.plist;
5850 }
5851 else if (local_got_ents != NULL)
5852 {
5853 struct plt_entry **local_plt = (struct plt_entry **)
5854 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5855 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
5856 (local_plt + symtab_hdr->sh_info);
5857 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5858 ifunc = local_plt + r_symndx;
5859 }
5860 if (ifunc != NULL)
5861 {
5862 struct plt_entry *ent;
5863
5864 for (ent = *ifunc; ent != NULL; ent = ent->next)
5865 if (ent->addend == rel->r_addend)
5866 break;
5867 if (ent == NULL)
5868 abort ();
5869 if (ent->plt.refcount > 0)
5870 ent->plt.refcount -= 1;
5871 continue;
5872 }
5873 }
5874
a33d1f77
AM
5875 switch (r_type)
5876 {
411e1bfb
AM
5877 case R_PPC64_GOT_TLSLD16:
5878 case R_PPC64_GOT_TLSLD16_LO:
5879 case R_PPC64_GOT_TLSLD16_HI:
5880 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5881 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5882 goto dogot;
5883
5884 case R_PPC64_GOT_TLSGD16:
5885 case R_PPC64_GOT_TLSGD16_LO:
5886 case R_PPC64_GOT_TLSGD16_HI:
5887 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5888 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5889 goto dogot;
5890
5891 case R_PPC64_GOT_TPREL16_DS:
5892 case R_PPC64_GOT_TPREL16_LO_DS:
5893 case R_PPC64_GOT_TPREL16_HI:
5894 case R_PPC64_GOT_TPREL16_HA:
5895 tls_type = TLS_TLS | TLS_TPREL;
5896 goto dogot;
5897
5898 case R_PPC64_GOT_DTPREL16_DS:
5899 case R_PPC64_GOT_DTPREL16_LO_DS:
5900 case R_PPC64_GOT_DTPREL16_HI:
5901 case R_PPC64_GOT_DTPREL16_HA:
5902 tls_type = TLS_TLS | TLS_DTPREL;
5903 goto dogot;
5904
a33d1f77
AM
5905 case R_PPC64_GOT16:
5906 case R_PPC64_GOT16_DS:
5907 case R_PPC64_GOT16_HA:
5908 case R_PPC64_GOT16_HI:
5909 case R_PPC64_GOT16_LO:
5910 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5911 dogot:
5912 {
5913 struct got_entry *ent;
5914
58ac9f71
AM
5915 if (h != NULL)
5916 ent = h->got.glist;
411e1bfb
AM
5917 else
5918 ent = local_got_ents[r_symndx];
5919
5920 for (; ent != NULL; ent = ent->next)
5921 if (ent->addend == rel->r_addend
e717da7e 5922 && ent->owner == abfd
411e1bfb
AM
5923 && ent->tls_type == tls_type)
5924 break;
5925 if (ent == NULL)
5926 abort ();
5927 if (ent->got.refcount > 0)
5928 ent->got.refcount -= 1;
5929 }
a33d1f77 5930 break;
65f38f15 5931
a33d1f77
AM
5932 case R_PPC64_PLT16_HA:
5933 case R_PPC64_PLT16_HI:
5934 case R_PPC64_PLT16_LO:
5935 case R_PPC64_PLT32:
5936 case R_PPC64_PLT64:
721956f4
AM
5937 case R_PPC64_REL14:
5938 case R_PPC64_REL14_BRNTAKEN:
5939 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5940 case R_PPC64_REL24:
58ac9f71 5941 if (h != NULL)
5d1634d7 5942 {
411e1bfb
AM
5943 struct plt_entry *ent;
5944
411e1bfb
AM
5945 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5946 if (ent->addend == rel->r_addend)
5947 break;
7c6c1722 5948 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 5949 ent->plt.refcount -= 1;
5d1634d7 5950 }
e86ce104 5951 break;
5d1634d7 5952
a33d1f77
AM
5953 default:
5954 break;
5955 }
5956 }
b34976b6 5957 return TRUE;
5bd4f169
AM
5958}
5959
deb0e272
AM
5960/* The maximum size of .sfpr. */
5961#define SFPR_MAX (218*4)
5962
5963struct sfpr_def_parms
5964{
699733f6
AM
5965 const char name[12];
5966 unsigned char lo, hi;
deb0e272
AM
5967 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5968 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5969};
5970
5971/* Auto-generate _save*, _rest* functions in .sfpr. */
5972
4dfe6ac6 5973static bfd_boolean
deb0e272
AM
5974sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5975{
5976 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5977 unsigned int i;
5978 size_t len = strlen (parm->name);
5979 bfd_boolean writing = FALSE;
699733f6 5980 char sym[16];
deb0e272 5981
4dfe6ac6
NC
5982 if (htab == NULL)
5983 return FALSE;
5984
deb0e272
AM
5985 memcpy (sym, parm->name, len);
5986 sym[len + 2] = 0;
5987
5988 for (i = parm->lo; i <= parm->hi; i++)
5989 {
5990 struct elf_link_hash_entry *h;
5991
5992 sym[len + 0] = i / 10 + '0';
5993 sym[len + 1] = i % 10 + '0';
5994 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5995 if (h != NULL
f5385ebf 5996 && !h->def_regular)
deb0e272
AM
5997 {
5998 h->root.type = bfd_link_hash_defined;
5999 h->root.u.def.section = htab->sfpr;
6000 h->root.u.def.value = htab->sfpr->size;
6001 h->type = STT_FUNC;
f5385ebf 6002 h->def_regular = 1;
deb0e272
AM
6003 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6004 writing = TRUE;
6005 if (htab->sfpr->contents == NULL)
6006 {
6007 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6008 if (htab->sfpr->contents == NULL)
6009 return FALSE;
6010 }
6011 }
6012 if (writing)
6013 {
6014 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6015 if (i != parm->hi)
6016 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6017 else
6018 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6019 htab->sfpr->size = p - htab->sfpr->contents;
6020 }
6021 }
6022
6023 return TRUE;
6024}
6025
6026static bfd_byte *
6027savegpr0 (bfd *abfd, bfd_byte *p, int r)
6028{
6029 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6030 return p + 4;
6031}
6032
6033static bfd_byte *
6034savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6035{
6036 p = savegpr0 (abfd, p, r);
6037 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6038 p = p + 4;
6039 bfd_put_32 (abfd, BLR, p);
6040 return p + 4;
6041}
6042
6043static bfd_byte *
6044restgpr0 (bfd *abfd, bfd_byte *p, int r)
6045{
6046 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6047 return p + 4;
6048}
6049
6050static bfd_byte *
6051restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6052{
6053 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6054 p = p + 4;
6055 p = restgpr0 (abfd, p, r);
6056 bfd_put_32 (abfd, MTLR_R0, p);
6057 p = p + 4;
6058 if (r == 29)
6059 {
6060 p = restgpr0 (abfd, p, 30);
6061 p = restgpr0 (abfd, p, 31);
6062 }
6063 bfd_put_32 (abfd, BLR, p);
6064 return p + 4;
6065}
6066
6067static bfd_byte *
6068savegpr1 (bfd *abfd, bfd_byte *p, int r)
6069{
6070 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6071 return p + 4;
6072}
6073
6074static bfd_byte *
6075savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6076{
6077 p = savegpr1 (abfd, p, r);
6078 bfd_put_32 (abfd, BLR, p);
6079 return p + 4;
6080}
6081
6082static bfd_byte *
6083restgpr1 (bfd *abfd, bfd_byte *p, int r)
6084{
6085 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6086 return p + 4;
6087}
6088
6089static bfd_byte *
6090restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6091{
6092 p = restgpr1 (abfd, p, r);
6093 bfd_put_32 (abfd, BLR, p);
6094 return p + 4;
6095}
6096
6097static bfd_byte *
6098savefpr (bfd *abfd, bfd_byte *p, int r)
6099{
6100 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6101 return p + 4;
6102}
6103
6104static bfd_byte *
6105savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6106{
6107 p = savefpr (abfd, p, r);
6108 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6109 p = p + 4;
6110 bfd_put_32 (abfd, BLR, p);
6111 return p + 4;
6112}
6113
6114static bfd_byte *
6115restfpr (bfd *abfd, bfd_byte *p, int r)
6116{
6117 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6118 return p + 4;
6119}
6120
6121static bfd_byte *
6122restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6123{
6124 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6125 p = p + 4;
6126 p = restfpr (abfd, p, r);
6127 bfd_put_32 (abfd, MTLR_R0, p);
6128 p = p + 4;
6129 if (r == 29)
6130 {
6131 p = restfpr (abfd, p, 30);
6132 p = restfpr (abfd, p, 31);
6133 }
6134 bfd_put_32 (abfd, BLR, p);
6135 return p + 4;
6136}
6137
6138static bfd_byte *
6139savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6140{
6141 p = savefpr (abfd, p, r);
6142 bfd_put_32 (abfd, BLR, p);
6143 return p + 4;
6144}
6145
6146static bfd_byte *
6147restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6148{
6149 p = restfpr (abfd, p, r);
6150 bfd_put_32 (abfd, BLR, p);
6151 return p + 4;
6152}
6153
6154static bfd_byte *
6155savevr (bfd *abfd, bfd_byte *p, int r)
6156{
6157 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6158 p = p + 4;
6159 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6160 return p + 4;
6161}
6162
6163static bfd_byte *
6164savevr_tail (bfd *abfd, bfd_byte *p, int r)
6165{
6166 p = savevr (abfd, p, r);
6167 bfd_put_32 (abfd, BLR, p);
6168 return p + 4;
6169}
6170
6171static bfd_byte *
6172restvr (bfd *abfd, bfd_byte *p, int r)
6173{
6174 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6175 p = p + 4;
6176 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6177 return p + 4;
6178}
6179
6180static bfd_byte *
6181restvr_tail (bfd *abfd, bfd_byte *p, int r)
6182{
6183 p = restvr (abfd, p, r);
6184 bfd_put_32 (abfd, BLR, p);
6185 return p + 4;
6186}
6187
e86ce104
AM
6188/* Called via elf_link_hash_traverse to transfer dynamic linking
6189 information on function code symbol entries to their corresponding
6190 function descriptor symbol entries. */
deb0e272 6191
b34976b6 6192static bfd_boolean
4ce794b7 6193func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6194{
e86ce104 6195 struct bfd_link_info *info;
65f38f15 6196 struct ppc_link_hash_table *htab;
411e1bfb 6197 struct plt_entry *ent;
50bc7936
AM
6198 struct ppc_link_hash_entry *fh;
6199 struct ppc_link_hash_entry *fdh;
6200 bfd_boolean force_local;
5bd4f169 6201
50bc7936
AM
6202 fh = (struct ppc_link_hash_entry *) h;
6203 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6204 return TRUE;
e86ce104 6205
50bc7936
AM
6206 if (fh->elf.root.type == bfd_link_hash_warning)
6207 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
e92d460e 6208
4ce794b7 6209 info = inf;
65f38f15 6210 htab = ppc_hash_table (info);
4dfe6ac6
NC
6211 if (htab == NULL)
6212 return FALSE;
5bd4f169 6213
c09bdfe5
AM
6214 /* Resolve undefined references to dot-symbols as the value
6215 in the function descriptor, if we have one in a regular object.
6216 This is to satisfy cases like ".quad .foo". Calls to functions
6217 in dynamic objects are handled elsewhere. */
6218 if (fh->elf.root.type == bfd_link_hash_undefweak
6219 && fh->was_undefined
b31867b6
AM
6220 && (fdh = defined_func_desc (fh)) != NULL
6221 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6222 && opd_entry_value (fdh->elf.root.u.def.section,
6223 fdh->elf.root.u.def.value,
c09bdfe5
AM
6224 &fh->elf.root.u.def.section,
6225 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6226 {
b31867b6 6227 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6228 fh->elf.forced_local = 1;
b31867b6
AM
6229 fh->elf.def_regular = fdh->elf.def_regular;
6230 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6231 }
6232
e86ce104
AM
6233 /* If this is a function code symbol, transfer dynamic linking
6234 information to the function descriptor symbol. */
50bc7936 6235 if (!fh->is_func)
b34976b6 6236 return TRUE;
e86ce104 6237
50bc7936 6238 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6239 if (ent->plt.refcount > 0)
6240 break;
50bc7936
AM
6241 if (ent == NULL
6242 || fh->elf.root.root.string[0] != '.'
6243 || fh->elf.root.root.string[1] == '\0')
6244 return TRUE;
5bd4f169 6245
50bc7936
AM
6246 /* Find the corresponding function descriptor symbol. Create it
6247 as undefined if necessary. */
5bd4f169 6248
b31867b6 6249 fdh = lookup_fdh (fh, htab);
50bc7936 6250 if (fdh == NULL
df131623 6251 && !info->executable
50bc7936
AM
6252 && (fh->elf.root.type == bfd_link_hash_undefined
6253 || fh->elf.root.type == bfd_link_hash_undefweak))
6254 {
908b32fc 6255 fdh = make_fdh (info, fh);
bb700d78
AM
6256 if (fdh == NULL)
6257 return FALSE;
50bc7936 6258 }
648cca2c 6259
908b32fc 6260 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6261 code symbol is strong undefined, make the fake sym the same.
6262 If the function code symbol is defined, then force the fake
6263 descriptor local; We can't support overriding of symbols in a
6264 shared library on a fake descriptor. */
908b32fc
AM
6265
6266 if (fdh != NULL
6267 && fdh->fake
433817dd 6268 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6269 {
433817dd
AM
6270 if (fh->elf.root.type == bfd_link_hash_undefined)
6271 {
6272 fdh->elf.root.type = bfd_link_hash_undefined;
6273 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6274 }
6275 else if (fh->elf.root.type == bfd_link_hash_defined
6276 || fh->elf.root.type == bfd_link_hash_defweak)
6277 {
6278 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6279 }
908b32fc
AM
6280 }
6281
50bc7936 6282 if (fdh != NULL
f5385ebf 6283 && !fdh->elf.forced_local
df131623 6284 && (!info->executable
f5385ebf
AM
6285 || fdh->elf.def_dynamic
6286 || fdh->elf.ref_dynamic
50bc7936
AM
6287 || (fdh->elf.root.type == bfd_link_hash_undefweak
6288 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6289 {
6290 if (fdh->elf.dynindx == -1)
c152c796 6291 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6292 return FALSE;
f5385ebf
AM
6293 fdh->elf.ref_regular |= fh->elf.ref_regular;
6294 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6295 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6296 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6297 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6298 {
40d16e0b 6299 move_plt_plist (fh, fdh);
f5385ebf 6300 fdh->elf.needs_plt = 1;
e86ce104 6301 }
50bc7936 6302 fdh->is_func_descriptor = 1;
34814b9f
AM
6303 fdh->oh = fh;
6304 fh->oh = fdh;
e86ce104
AM
6305 }
6306
50bc7936
AM
6307 /* Now that the info is on the function descriptor, clear the
6308 function code sym info. Any function code syms for which we
6309 don't have a definition in a regular file, we force local.
6310 This prevents a shared library from exporting syms that have
6311 been imported from another library. Function code syms that
6312 are really in the library we must leave global to prevent the
6313 linker dragging in a definition from a static library. */
93f3fa99
AM
6314 force_local = (!fh->elf.def_regular
6315 || fdh == NULL
6316 || !fdh->elf.def_regular
6317 || fdh->elf.forced_local);
50bc7936
AM
6318 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6319
b34976b6 6320 return TRUE;
e86ce104 6321}
40b8271b 6322
e86ce104 6323/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6324 this hook to a) provide some gcc support functions, and b) transfer
6325 dynamic linking information gathered so far on function code symbol
6326 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6327
b34976b6 6328static bfd_boolean
4ce794b7
AM
6329ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6330 struct bfd_link_info *info)
e86ce104
AM
6331{
6332 struct ppc_link_hash_table *htab;
82bd7b59 6333 unsigned int i;
deb0e272
AM
6334 const struct sfpr_def_parms funcs[] =
6335 {
6336 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6337 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6338 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6339 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6340 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6341 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6342 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6343 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6344 { "._savef", 14, 31, savefpr, savefpr1_tail },
6345 { "._restf", 14, 31, restfpr, restfpr1_tail },
6346 { "_savevr_", 20, 31, savevr, savevr_tail },
6347 { "_restvr_", 20, 31, restvr, restvr_tail }
6348 };
e86ce104
AM
6349
6350 htab = ppc_hash_table (info);
4dfe6ac6
NC
6351 if (htab == NULL)
6352 return FALSE;
6353
82bd7b59
AM
6354 if (htab->sfpr == NULL)
6355 /* We don't have any relocs. */
b34976b6 6356 return TRUE;
82bd7b59 6357
deb0e272
AM
6358 /* Provide any missing _save* and _rest* functions. */
6359 htab->sfpr->size = 0;
6360 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6361 if (!sfpr_define (info, &funcs[i]))
6362 return FALSE;
82bd7b59 6363
4ce794b7 6364 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6365
eea6121a 6366 if (htab->sfpr->size == 0)
8423293d 6367 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6368
b34976b6 6369 return TRUE;
e86ce104
AM
6370}
6371
6372/* Adjust a symbol defined by a dynamic object and referenced by a
6373 regular object. The current definition is in some section of the
6374 dynamic object, but we're not including those sections. We have to
6375 change the definition to something the rest of the link can
6376 understand. */
6377
b34976b6 6378static bfd_boolean
4ce794b7
AM
6379ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6380 struct elf_link_hash_entry *h)
e86ce104
AM
6381{
6382 struct ppc_link_hash_table *htab;
e86ce104 6383 asection *s;
e86ce104
AM
6384
6385 htab = ppc_hash_table (info);
4dfe6ac6
NC
6386 if (htab == NULL)
6387 return FALSE;
e86ce104
AM
6388
6389 /* Deal with function syms. */
6390 if (h->type == STT_FUNC
e054468f 6391 || h->type == STT_GNU_IFUNC
f5385ebf 6392 || h->needs_plt)
e86ce104
AM
6393 {
6394 /* Clear procedure linkage table information for any symbol that
6395 won't need a .plt entry. */
411e1bfb
AM
6396 struct plt_entry *ent;
6397 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6398 if (ent->plt.refcount > 0)
6399 break;
8387904d 6400 if (ent == NULL
e054468f
AM
6401 || (h->type != STT_GNU_IFUNC
6402 && (SYMBOL_CALLS_LOCAL (info, h)
6403 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6404 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6405 {
411e1bfb 6406 h->plt.plist = NULL;
f5385ebf 6407 h->needs_plt = 0;
40b8271b 6408 }
5bd4f169 6409 }
bbd7ec4a 6410 else
411e1bfb 6411 h->plt.plist = NULL;
5bd4f169
AM
6412
6413 /* If this is a weak symbol, and there is a real definition, the
6414 processor independent code will have arranged for us to see the
6415 real definition first, and we can just use the same value. */
f6e332e6 6416 if (h->u.weakdef != NULL)
5bd4f169 6417 {
f6e332e6
AM
6418 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6419 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6420 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6421 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6422 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6423 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6424 return TRUE;
5bd4f169
AM
6425 }
6426
5bd4f169
AM
6427 /* If we are creating a shared library, we must presume that the
6428 only references to the symbol are via the global offset table.
6429 For such cases we need not do anything here; the relocations will
6430 be handled correctly by relocate_section. */
6431 if (info->shared)
b34976b6 6432 return TRUE;
5bd4f169 6433
65f38f15
AM
6434 /* If there are no references to this symbol that do not use the
6435 GOT, we don't need to generate a copy reloc. */
f5385ebf 6436 if (!h->non_got_ref)
b34976b6 6437 return TRUE;
65f38f15 6438
b186458a
JJ
6439 /* Don't generate a copy reloc for symbols defined in the executable. */
6440 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6441 return TRUE;
6442
f4656909 6443 if (ELIMINATE_COPY_RELOCS)
65f38f15 6444 {
f4656909
AM
6445 struct ppc_link_hash_entry * eh;
6446 struct ppc_dyn_relocs *p;
65f38f15 6447
f4656909
AM
6448 eh = (struct ppc_link_hash_entry *) h;
6449 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6450 {
6451 s = p->sec->output_section;
6452 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6453 break;
6454 }
6455
6456 /* If we didn't find any dynamic relocs in read-only sections, then
6457 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6458 if (p == NULL)
6459 {
f5385ebf 6460 h->non_got_ref = 0;
f4656909
AM
6461 return TRUE;
6462 }
65f38f15
AM
6463 }
6464
5d35169e 6465 if (h->plt.plist != NULL)
97b639ba
AM
6466 {
6467 /* We should never get here, but unfortunately there are versions
6468 of gcc out there that improperly (for this ABI) put initialized
6469 function pointers, vtable refs and suchlike in read-only
6470 sections. Allow them to proceed, but warn that this might
6471 break at runtime. */
6472 (*_bfd_error_handler)
6473 (_("copy reloc against `%s' requires lazy plt linking; "
6474 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
6475 h->root.root.string);
6476 }
5d35169e
AM
6477
6478 /* This is a reference to a symbol defined by a dynamic object which
6479 is not a function. */
6480
909272ee
AM
6481 if (h->size == 0)
6482 {
6483 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
6484 h->root.root.string);
6485 return TRUE;
6486 }
6487
5bd4f169
AM
6488 /* We must allocate the symbol in our .dynbss section, which will
6489 become part of the .bss section of the executable. There will be
6490 an entry for this symbol in the .dynsym section. The dynamic
6491 object will contain position independent code, so all references
6492 from the dynamic object to this symbol will go through the global
6493 offset table. The dynamic linker will use the .dynsym entry to
6494 determine the address it must put in the global offset table, so
6495 both the dynamic object and the regular object will refer to the
6496 same memory location for the variable. */
5bd4f169 6497
04c9666a
AM
6498 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6499 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6500 runtime process image. We need to remember the offset into the
6501 .rela.bss section we are going to use. */
6502 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6503 {
eea6121a 6504 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6505 h->needs_copy = 1;
5bd4f169
AM
6506 }
6507
4ce794b7 6508 s = htab->dynbss;
5bd4f169 6509
027297b7 6510 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6511}
6512
e86ce104
AM
6513/* If given a function descriptor symbol, hide both the function code
6514 sym and the descriptor. */
6515static void
4ce794b7
AM
6516ppc64_elf_hide_symbol (struct bfd_link_info *info,
6517 struct elf_link_hash_entry *h,
6518 bfd_boolean force_local)
e86ce104 6519{
34814b9f 6520 struct ppc_link_hash_entry *eh;
e86ce104
AM
6521 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6522
34814b9f
AM
6523 eh = (struct ppc_link_hash_entry *) h;
6524 if (eh->is_func_descriptor)
e86ce104 6525 {
34814b9f 6526 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6527
721956f4 6528 if (fh == NULL)
d1329ca3
AM
6529 {
6530 const char *p, *q;
6531 struct ppc_link_hash_table *htab;
6532 char save;
6533
6534 /* We aren't supposed to use alloca in BFD because on
6535 systems which do not have alloca the version in libiberty
6536 calls xmalloc, which might cause the program to crash
6537 when it runs out of memory. This function doesn't have a
6538 return status, so there's no way to gracefully return an
6539 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6540 accessed; It's either a string in an ELF string table,
6541 or allocated in an objalloc structure. */
d1329ca3 6542
34814b9f 6543 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6544 save = *p;
6545 *(char *) p = '.';
6546 htab = ppc_hash_table (info);
4dfe6ac6
NC
6547 if (htab == NULL)
6548 return;
6549
34814b9f
AM
6550 fh = (struct ppc_link_hash_entry *)
6551 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6552 *(char *) p = save;
6553
6554 /* Unfortunately, if it so happens that the string we were
6555 looking for was allocated immediately before this string,
6556 then we overwrote the string terminator. That's the only
6557 reason the lookup should fail. */
6558 if (fh == NULL)
6559 {
34814b9f
AM
6560 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6561 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6562 --q, --p;
34814b9f
AM
6563 if (q < eh->elf.root.root.string && *p == '.')
6564 fh = (struct ppc_link_hash_entry *)
6565 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6566 }
6567 if (fh != NULL)
6568 {
34814b9f
AM
6569 eh->oh = fh;
6570 fh->oh = eh;
d1329ca3
AM
6571 }
6572 }
e86ce104 6573 if (fh != NULL)
34814b9f 6574 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6575 }
6576}
6577
411e1bfb 6578static bfd_boolean
8843416a
AM
6579get_sym_h (struct elf_link_hash_entry **hp,
6580 Elf_Internal_Sym **symp,
6581 asection **symsecp,
f961d9dd 6582 unsigned char **tls_maskp,
8843416a
AM
6583 Elf_Internal_Sym **locsymsp,
6584 unsigned long r_symndx,
6585 bfd *ibfd)
411e1bfb 6586{
0ffa91dd 6587 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6588
6589 if (r_symndx >= symtab_hdr->sh_info)
6590 {
6591 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6592 struct elf_link_hash_entry *h;
6593
6594 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6595 h = elf_follow_link (h);
411e1bfb
AM
6596
6597 if (hp != NULL)
6598 *hp = h;
6599
6600 if (symp != NULL)
6601 *symp = NULL;
6602
6603 if (symsecp != NULL)
6604 {
6605 asection *symsec = NULL;
6606 if (h->root.type == bfd_link_hash_defined
6607 || h->root.type == bfd_link_hash_defweak)
6608 symsec = h->root.u.def.section;
6609 *symsecp = symsec;
6610 }
6611
e7b938ca 6612 if (tls_maskp != NULL)
411e1bfb
AM
6613 {
6614 struct ppc_link_hash_entry *eh;
6615
6616 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6617 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6618 }
6619 }
6620 else
6621 {
6622 Elf_Internal_Sym *sym;
6623 Elf_Internal_Sym *locsyms = *locsymsp;
6624
6625 if (locsyms == NULL)
6626 {
6627 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6628 if (locsyms == NULL)
6629 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6630 symtab_hdr->sh_info,
6631 0, NULL, NULL, NULL);
6632 if (locsyms == NULL)
6633 return FALSE;
6634 *locsymsp = locsyms;
6635 }
6636 sym = locsyms + r_symndx;
6637
6638 if (hp != NULL)
6639 *hp = NULL;
6640
6641 if (symp != NULL)
6642 *symp = sym;
6643
6644 if (symsecp != NULL)
cb33740c 6645 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6646
e7b938ca 6647 if (tls_maskp != NULL)
411e1bfb
AM
6648 {
6649 struct got_entry **lgot_ents;
f961d9dd 6650 unsigned char *tls_mask;
411e1bfb 6651
e7b938ca 6652 tls_mask = NULL;
411e1bfb
AM
6653 lgot_ents = elf_local_got_ents (ibfd);
6654 if (lgot_ents != NULL)
6655 {
e054468f
AM
6656 struct plt_entry **local_plt = (struct plt_entry **)
6657 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6658 unsigned char *lgot_masks = (unsigned char *)
e054468f 6659 (local_plt + symtab_hdr->sh_info);
e7b938ca 6660 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6661 }
e7b938ca 6662 *tls_maskp = tls_mask;
411e1bfb
AM
6663 }
6664 }
6665 return TRUE;
6666}
6667
e7b938ca 6668/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6669 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6670 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6671
6672static int
f961d9dd 6673get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6674 unsigned long *toc_symndx,
6675 bfd_vma *toc_addend,
0d4792f7 6676 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6677 const Elf_Internal_Rela *rel,
6678 bfd *ibfd)
411e1bfb
AM
6679{
6680 unsigned long r_symndx;
0d4792f7 6681 int next_r;
411e1bfb
AM
6682 struct elf_link_hash_entry *h;
6683 Elf_Internal_Sym *sym;
6684 asection *sec;
6685 bfd_vma off;
6686
6687 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6688 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6689 return 0;
411e1bfb 6690
e7b938ca 6691 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6692 || sec == NULL
6bee8834 6693 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6694 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6695 return 1;
411e1bfb
AM
6696
6697 /* Look inside a TOC section too. */
6698 if (h != NULL)
6699 {
6700 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6701 off = h->root.u.def.value;
6702 }
6703 else
6704 off = sym->st_value;
6705 off += rel->r_addend;
6706 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6707 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6708 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6709 if (toc_symndx != NULL)
6710 *toc_symndx = r_symndx;
3a71aa26
AM
6711 if (toc_addend != NULL)
6712 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6713 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6714 return 0;
854b41e7 6715 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6716 && (next_r == -1 || next_r == -2))
6717 return 1 - next_r;
951fd09b 6718 return 1;
411e1bfb
AM
6719}
6720
754021d0 6721/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6722 code for the old ABI, these will already have been done. */
754021d0
AM
6723
6724static bfd_boolean
6725adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6726{
6727 struct ppc_link_hash_entry *eh;
6728 asection *sym_sec;
74f0fb50 6729 struct _opd_sec_data *opd;
754021d0
AM
6730
6731 if (h->root.type == bfd_link_hash_indirect)
6732 return TRUE;
6733
6734 if (h->root.type == bfd_link_hash_warning)
6735 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6736
6737 if (h->root.type != bfd_link_hash_defined
6738 && h->root.type != bfd_link_hash_defweak)
6739 return TRUE;
6740
6741 eh = (struct ppc_link_hash_entry *) h;
6742 if (eh->adjust_done)
6743 return TRUE;
6744
6745 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6746 opd = get_opd_info (sym_sec);
6747 if (opd != NULL && opd->adjust != NULL)
754021d0 6748 {
74f0fb50 6749 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6750 if (adjust == -1)
6751 {
6752 /* This entry has been deleted. */
b3fac117 6753 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6754 if (dsec == NULL)
6755 {
6756 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6757 if (elf_discarded_section (dsec))
6758 {
b3fac117 6759 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6760 break;
6761 }
6762 }
4025353c 6763 eh->elf.root.u.def.value = 0;
81688140 6764 eh->elf.root.u.def.section = dsec;
4025353c
AM
6765 }
6766 else
6767 eh->elf.root.u.def.value += adjust;
754021d0
AM
6768 eh->adjust_done = 1;
6769 }
6770 return TRUE;
6771}
6772
8c1d1bb8
AM
6773/* Handles decrementing dynamic reloc counts for the reloc specified by
6774 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6775 have already been determined. */
6776
6777static bfd_boolean
6778dec_dynrel_count (bfd_vma r_info,
6779 asection *sec,
6780 struct bfd_link_info *info,
6781 Elf_Internal_Sym **local_syms,
6782 struct elf_link_hash_entry *h,
6783 asection *sym_sec)
6784{
6785 enum elf_ppc64_reloc_type r_type;
6786 struct ppc_dyn_relocs *p;
6787 struct ppc_dyn_relocs **pp;
6788
6789 /* Can this reloc be dynamic? This switch, and later tests here
6790 should be kept in sync with the code in check_relocs. */
6791 r_type = ELF64_R_TYPE (r_info);
6792 switch (r_type)
6793 {
6794 default:
6795 return TRUE;
6796
6797 case R_PPC64_TPREL16:
6798 case R_PPC64_TPREL16_LO:
6799 case R_PPC64_TPREL16_HI:
6800 case R_PPC64_TPREL16_HA:
6801 case R_PPC64_TPREL16_DS:
6802 case R_PPC64_TPREL16_LO_DS:
6803 case R_PPC64_TPREL16_HIGHER:
6804 case R_PPC64_TPREL16_HIGHERA:
6805 case R_PPC64_TPREL16_HIGHEST:
6806 case R_PPC64_TPREL16_HIGHESTA:
6807 if (!info->shared)
6808 return TRUE;
6809
6810 case R_PPC64_TPREL64:
6811 case R_PPC64_DTPMOD64:
6812 case R_PPC64_DTPREL64:
6813 case R_PPC64_ADDR64:
6814 case R_PPC64_REL30:
6815 case R_PPC64_REL32:
6816 case R_PPC64_REL64:
6817 case R_PPC64_ADDR14:
6818 case R_PPC64_ADDR14_BRNTAKEN:
6819 case R_PPC64_ADDR14_BRTAKEN:
6820 case R_PPC64_ADDR16:
6821 case R_PPC64_ADDR16_DS:
6822 case R_PPC64_ADDR16_HA:
6823 case R_PPC64_ADDR16_HI:
6824 case R_PPC64_ADDR16_HIGHER:
6825 case R_PPC64_ADDR16_HIGHERA:
6826 case R_PPC64_ADDR16_HIGHEST:
6827 case R_PPC64_ADDR16_HIGHESTA:
6828 case R_PPC64_ADDR16_LO:
6829 case R_PPC64_ADDR16_LO_DS:
6830 case R_PPC64_ADDR24:
6831 case R_PPC64_ADDR32:
6832 case R_PPC64_UADDR16:
6833 case R_PPC64_UADDR32:
6834 case R_PPC64_UADDR64:
6835 case R_PPC64_TOC:
6836 break;
6837 }
6838
6839 if (local_syms != NULL)
6840 {
6841 unsigned long r_symndx;
6842 Elf_Internal_Sym *sym;
6843 bfd *ibfd = sec->owner;
6844
6845 r_symndx = ELF64_R_SYM (r_info);
6846 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6847 return FALSE;
6848 }
6849
6850 if ((info->shared
1d483afe 6851 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8
AM
6852 || (h != NULL
6853 && (!info->symbolic
6854 || h->root.type == bfd_link_hash_defweak
6855 || !h->def_regular))))
6856 || (ELIMINATE_COPY_RELOCS
6857 && !info->shared
6858 && h != NULL
6859 && (h->root.type == bfd_link_hash_defweak
6860 || !h->def_regular)))
6861 ;
6862 else
6863 return TRUE;
6864
6865 if (h != NULL)
6866 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6867 else
6edfbbad 6868 {
60124e18
AM
6869 if (sym_sec != NULL)
6870 {
6871 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6872 pp = (struct ppc_dyn_relocs **) vpp;
6873 }
6874 else
6875 {
6876 void *vpp = &elf_section_data (sec)->local_dynrel;
6877 pp = (struct ppc_dyn_relocs **) vpp;
6878 }
6879
6880 /* elf_gc_sweep may have already removed all dyn relocs associated
6881 with local syms for a given section. Don't report a dynreloc
6882 miscount. */
6883 if (*pp == NULL)
6884 return TRUE;
6edfbbad 6885 }
8c1d1bb8
AM
6886
6887 while ((p = *pp) != NULL)
6888 {
6889 if (p->sec == sec)
6890 {
1d483afe 6891 if (!must_be_dyn_reloc (info, r_type))
8c1d1bb8
AM
6892 p->pc_count -= 1;
6893 p->count -= 1;
6894 if (p->count == 0)
6895 *pp = p->next;
6896 return TRUE;
6897 }
6898 pp = &p->next;
6899 }
6900
6901 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6902 sec->owner, sec);
6903 bfd_set_error (bfd_error_bad_value);
6904 return FALSE;
6905}
6906
754021d0
AM
6907/* Remove unused Official Procedure Descriptor entries. Currently we
6908 only remove those associated with functions in discarded link-once
6909 sections, or weakly defined functions that have been overridden. It
6910 would be possible to remove many more entries for statically linked
6911 applications. */
6912
b34976b6 6913bfd_boolean
33c0ec9d 6914ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
6915{
6916 bfd *ibfd;
754021d0 6917 bfd_boolean some_edited = FALSE;
3f764659 6918 asection *need_pad = NULL;
1e2f5b6e 6919
411e1bfb 6920 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
6921 {
6922 asection *sec;
6923 Elf_Internal_Rela *relstart, *rel, *relend;
6924 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6925 Elf_Internal_Sym *local_syms;
1e2f5b6e 6926 bfd_vma offset;
74f0fb50 6927 struct _opd_sec_data *opd;
3f764659
JJ
6928 bfd_boolean need_edit, add_aux_fields;
6929 bfd_size_type cnt_16b = 0;
1e2f5b6e 6930
854b41e7
AM
6931 if (!is_ppc64_elf (ibfd))
6932 continue;
6933
1e2f5b6e 6934 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6935 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6936 continue;
6937
4b85d634
AM
6938 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6939 continue;
6940
1e2f5b6e
AM
6941 if (sec->output_section == bfd_abs_section_ptr)
6942 continue;
6943
6944 /* Look through the section relocs. */
6945 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6946 continue;
6947
6cdc0ccc 6948 local_syms = NULL;
0ffa91dd 6949 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6950
6951 /* Read the relocations. */
4ce794b7 6952 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6953 info->keep_memory);
1e2f5b6e 6954 if (relstart == NULL)
b34976b6 6955 return FALSE;
1e2f5b6e
AM
6956
6957 /* First run through the relocs to check they are sane, and to
6958 determine whether we need to edit this opd section. */
b34976b6 6959 need_edit = FALSE;
3f764659 6960 need_pad = sec;
1e2f5b6e
AM
6961 offset = 0;
6962 relend = relstart + sec->reloc_count;
50bc7936 6963 for (rel = relstart; rel < relend; )
1e2f5b6e 6964 {
04c9666a 6965 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6966 unsigned long r_symndx;
6967 asection *sym_sec;
6968 struct elf_link_hash_entry *h;
6969 Elf_Internal_Sym *sym;
6970
3f764659 6971 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6972 only interested in the reloc pointing to a function entry
6973 point. */
50bc7936
AM
6974 if (rel->r_offset != offset
6975 || rel + 1 >= relend
6976 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
6977 {
6978 /* If someone messes with .opd alignment then after a
6979 "ld -r" we might have padding in the middle of .opd.
6980 Also, there's nothing to prevent someone putting
6981 something silly in .opd with the assembler. No .opd
b34976b6 6982 optimization for them! */
3f764659 6983 broken_opd:
1e2f5b6e 6984 (*_bfd_error_handler)
d003868e 6985 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 6986 need_edit = FALSE;
1e2f5b6e
AM
6987 break;
6988 }
6989
50bc7936
AM
6990 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6991 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6992 {
6993 (*_bfd_error_handler)
d003868e
AM
6994 (_("%B: unexpected reloc type %u in .opd section"),
6995 ibfd, r_type);
50bc7936
AM
6996 need_edit = FALSE;
6997 break;
6998 }
6999
1e2f5b6e 7000 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7001 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7002 r_symndx, ibfd))
50bc7936 7003 goto error_ret;
1e2f5b6e
AM
7004
7005 if (sym_sec == NULL || sym_sec->owner == NULL)
7006 {
411e1bfb
AM
7007 const char *sym_name;
7008 if (h != NULL)
7009 sym_name = h->root.root.string;
7010 else
26c61ae5
L
7011 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7012 sym_sec);
411e1bfb 7013
1e2f5b6e 7014 (*_bfd_error_handler)
d003868e
AM
7015 (_("%B: undefined sym `%s' in .opd section"),
7016 ibfd, sym_name);
b34976b6 7017 need_edit = FALSE;
1e2f5b6e
AM
7018 break;
7019 }
7020
51020317
AM
7021 /* opd entries are always for functions defined in the
7022 current input bfd. If the symbol isn't defined in the
7023 input bfd, then we won't be using the function in this
7024 bfd; It must be defined in a linkonce section in another
7025 bfd, or is weak. It's also possible that we are
7026 discarding the function due to a linker script /DISCARD/,
7027 which we test for via the output_section. */
7028 if (sym_sec->owner != ibfd
7029 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7030 need_edit = TRUE;
1e2f5b6e 7031
50bc7936 7032 rel += 2;
3f764659
JJ
7033 if (rel == relend
7034 || (rel + 1 == relend && rel->r_offset == offset + 16))
7035 {
7036 if (sec->size == offset + 24)
7037 {
7038 need_pad = NULL;
7039 break;
7040 }
7041 if (rel == relend && sec->size == offset + 16)
7042 {
7043 cnt_16b++;
7044 break;
7045 }
7046 goto broken_opd;
7047 }
7048
7049 if (rel->r_offset == offset + 24)
7050 offset += 24;
7051 else if (rel->r_offset != offset + 16)
7052 goto broken_opd;
7053 else if (rel + 1 < relend
7054 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7055 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7056 {
7057 offset += 16;
7058 cnt_16b++;
7059 }
7060 else if (rel + 2 < relend
7061 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7062 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7063 {
7064 offset += 24;
7065 rel += 1;
7066 }
7067 else
7068 goto broken_opd;
1e2f5b6e
AM
7069 }
7070
3f764659
JJ
7071 add_aux_fields = non_overlapping && cnt_16b > 0;
7072
7073 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7074 {
7075 Elf_Internal_Rela *write_rel;
d4730f92 7076 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7077 bfd_byte *rptr, *wptr;
983bddc8 7078 bfd_byte *new_contents;
b34976b6 7079 bfd_boolean skip;
3f764659 7080 long opd_ent_size;
74f0fb50
AM
7081 bfd_size_type amt;
7082
983bddc8 7083 new_contents = NULL;
74f0fb50
AM
7084 amt = sec->size * sizeof (long) / 8;
7085 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7086 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7087 if (opd->adjust == NULL)
7088 return FALSE;
7089 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7090
7091 /* This seems a waste of time as input .opd sections are all
7092 zeros as generated by gcc, but I suppose there's no reason
7093 this will always be so. We might start putting something in
7094 the third word of .opd entries. */
7095 if ((sec->flags & SEC_IN_MEMORY) == 0)
7096 {
eea6121a
AM
7097 bfd_byte *loc;
7098 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7099 {
eea6121a
AM
7100 if (loc != NULL)
7101 free (loc);
50bc7936 7102 error_ret:
6cdc0ccc
AM
7103 if (local_syms != NULL
7104 && symtab_hdr->contents != (unsigned char *) local_syms)
7105 free (local_syms);
6cdc0ccc
AM
7106 if (elf_section_data (sec)->relocs != relstart)
7107 free (relstart);
b34976b6 7108 return FALSE;
6cdc0ccc 7109 }
1e2f5b6e
AM
7110 sec->contents = loc;
7111 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7112 }
7113
7114 elf_section_data (sec)->relocs = relstart;
7115
3f764659 7116 new_contents = sec->contents;
3f764659
JJ
7117 if (add_aux_fields)
7118 {
7119 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7120 if (new_contents == NULL)
7121 return FALSE;
7122 need_pad = FALSE;
3f764659 7123 }
b4f4e59f
AM
7124 wptr = new_contents;
7125 rptr = sec->contents;
3f764659 7126
1e2f5b6e 7127 write_rel = relstart;
b34976b6 7128 skip = FALSE;
1e2f5b6e 7129 offset = 0;
3f764659 7130 opd_ent_size = 0;
1e2f5b6e
AM
7131 for (rel = relstart; rel < relend; rel++)
7132 {
50bc7936
AM
7133 unsigned long r_symndx;
7134 asection *sym_sec;
7135 struct elf_link_hash_entry *h;
7136 Elf_Internal_Sym *sym;
7137
7138 r_symndx = ELF64_R_SYM (rel->r_info);
7139 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7140 r_symndx, ibfd))
50bc7936
AM
7141 goto error_ret;
7142
1e2f5b6e
AM
7143 if (rel->r_offset == offset)
7144 {
50bc7936 7145 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7146
7147 /* See if the .opd entry is full 24 byte or
7148 16 byte (with fd_aux entry overlapped with next
7149 fd_func). */
7150 opd_ent_size = 24;
7151 if ((rel + 2 == relend && sec->size == offset + 16)
7152 || (rel + 3 < relend
7153 && rel[2].r_offset == offset + 16
7154 && rel[3].r_offset == offset + 24
7155 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7156 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7157 opd_ent_size = 16;
7158
4025353c
AM
7159 if (h != NULL
7160 && h->root.root.string[0] == '.')
c4f68ce3 7161 {
4dfe6ac6
NC
7162 struct ppc_link_hash_table *htab;
7163
7164 htab = ppc_hash_table (info);
7165 if (htab != NULL)
7166 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7167 htab);
c4f68ce3
AM
7168 if (fdh != NULL
7169 && fdh->elf.root.type != bfd_link_hash_defined
7170 && fdh->elf.root.type != bfd_link_hash_defweak)
7171 fdh = NULL;
7172 }
1e2f5b6e 7173
51020317
AM
7174 skip = (sym_sec->owner != ibfd
7175 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7176 if (skip)
7177 {
4025353c 7178 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7179 {
7180 /* Arrange for the function descriptor sym
7181 to be dropped. */
d6fe2dc1
AM
7182 fdh->elf.root.u.def.value = 0;
7183 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7184 }
74f0fb50 7185 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7186 }
7187 else
1e2f5b6e
AM
7188 {
7189 /* We'll be keeping this opd entry. */
7190
4025353c 7191 if (fdh != NULL)
1e2f5b6e 7192 {
754021d0
AM
7193 /* Redefine the function descriptor symbol to
7194 this location in the opd section. It is
7195 necessary to update the value here rather
7196 than using an array of adjustments as we do
7197 for local symbols, because various places
7198 in the generic ELF code use the value
7199 stored in u.def.value. */
3f764659 7200 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7201 fdh->adjust_done = 1;
1e2f5b6e 7202 }
754021d0
AM
7203
7204 /* Local syms are a bit tricky. We could
7205 tweak them as they can be cached, but
7206 we'd need to look through the local syms
7207 for the function descriptor sym which we
7208 don't have at the moment. So keep an
7209 array of adjustments. */
74f0fb50 7210 opd->adjust[rel->r_offset / 8]
3f764659 7211 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7212
7213 if (wptr != rptr)
3f764659
JJ
7214 memcpy (wptr, rptr, opd_ent_size);
7215 wptr += opd_ent_size;
7216 if (add_aux_fields && opd_ent_size == 16)
7217 {
7218 memset (wptr, '\0', 8);
7219 wptr += 8;
7220 }
1e2f5b6e 7221 }
3f764659
JJ
7222 rptr += opd_ent_size;
7223 offset += opd_ent_size;
1e2f5b6e
AM
7224 }
7225
50bc7936
AM
7226 if (skip)
7227 {
60124e18
AM
7228 if (!NO_OPD_RELOCS
7229 && !info->relocatable
18d944df
AM
7230 && !dec_dynrel_count (rel->r_info, sec, info,
7231 NULL, h, sym_sec))
8c1d1bb8 7232 goto error_ret;
50bc7936
AM
7233 }
7234 else
1e2f5b6e 7235 {
50bc7936
AM
7236 /* We need to adjust any reloc offsets to point to the
7237 new opd entries. While we're at it, we may as well
7238 remove redundant relocs. */
74f0fb50 7239 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7240 if (write_rel != rel)
7241 memcpy (write_rel, rel, sizeof (*rel));
7242 ++write_rel;
7243 }
7244 }
7245
3f764659 7246 sec->size = wptr - new_contents;
1e2f5b6e 7247 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7248 if (add_aux_fields)
7249 {
7250 free (sec->contents);
7251 sec->contents = new_contents;
7252 }
7253
05bf9422 7254 /* Fudge the header size too, as this is used later in
cdcf6e38 7255 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7256 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7257 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7258 some_edited = TRUE;
1e2f5b6e 7259 }
6cdc0ccc 7260 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7261 free (relstart);
6cdc0ccc 7262
411e1bfb
AM
7263 if (local_syms != NULL
7264 && symtab_hdr->contents != (unsigned char *) local_syms)
7265 {
7266 if (!info->keep_memory)
7267 free (local_syms);
7268 else
7269 symtab_hdr->contents = (unsigned char *) local_syms;
7270 }
7271 }
7272
754021d0
AM
7273 if (some_edited)
7274 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7275
3f764659
JJ
7276 /* If we are doing a final link and the last .opd entry is just 16 byte
7277 long, add a 8 byte padding after it. */
7278 if (need_pad != NULL && !info->relocatable)
7279 {
7280 bfd_byte *p;
7281
7282 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7283 {
7284 BFD_ASSERT (need_pad->size > 0);
7285
7286 p = bfd_malloc (need_pad->size + 8);
7287 if (p == NULL)
7288 return FALSE;
699733f6 7289
3f764659
JJ
7290 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7291 p, 0, need_pad->size))
7292 return FALSE;
7293
7294 need_pad->contents = p;
7295 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7296 }
7297 else
7298 {
7299 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7300 if (p == NULL)
7301 return FALSE;
7302
7303 need_pad->contents = p;
7304 }
7305
7306 memset (need_pad->contents + need_pad->size, 0, 8);
7307 need_pad->size += 8;
7308 }
7309
411e1bfb
AM
7310 return TRUE;
7311}
7312
e1918d23 7313/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7314
e1918d23 7315asection *
33c0ec9d
AM
7316ppc64_elf_tls_setup (struct bfd_link_info *info,
7317 int no_tls_get_addr_opt,
7318 int *no_multi_toc)
411e1bfb 7319{
411e1bfb
AM
7320 struct ppc_link_hash_table *htab;
7321
411e1bfb 7322 htab = ppc_hash_table (info);
4dfe6ac6
NC
7323 if (htab == NULL)
7324 return NULL;
7325
33c0ec9d
AM
7326 if (*no_multi_toc)
7327 htab->do_multi_toc = 0;
7328 else if (!htab->do_multi_toc)
7329 *no_multi_toc = 1;
7330
3a71aa26
AM
7331 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7332 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7333 FALSE, FALSE, TRUE));
a7f2871e
AM
7334 /* Move dynamic linking info to the function descriptor sym. */
7335 if (htab->tls_get_addr != NULL)
7336 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7337 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7338 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7339 FALSE, FALSE, TRUE));
a7f2871e
AM
7340 if (!no_tls_get_addr_opt)
7341 {
7342 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7343
7344 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7345 FALSE, FALSE, TRUE);
7346 if (opt != NULL)
7347 func_desc_adjust (opt, info);
7348 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7349 FALSE, FALSE, TRUE);
7350 if (opt_fd != NULL
7351 && (opt_fd->root.type == bfd_link_hash_defined
7352 || opt_fd->root.type == bfd_link_hash_defweak))
7353 {
7354 /* If glibc supports an optimized __tls_get_addr call stub,
7355 signalled by the presence of __tls_get_addr_opt, and we'll
7356 be calling __tls_get_addr via a plt call stub, then
7357 make __tls_get_addr point to __tls_get_addr_opt. */
7358 tga_fd = &htab->tls_get_addr_fd->elf;
7359 if (htab->elf.dynamic_sections_created
7360 && tga_fd != NULL
7361 && (tga_fd->type == STT_FUNC
7362 || tga_fd->needs_plt)
7363 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7364 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7365 && tga_fd->root.type == bfd_link_hash_undefweak)))
7366 {
7367 struct plt_entry *ent;
7368
7369 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7370 if (ent->plt.refcount > 0)
7371 break;
7372 if (ent != NULL)
7373 {
7374 tga_fd->root.type = bfd_link_hash_indirect;
7375 tga_fd->root.u.i.link = &opt_fd->root;
7376 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7377 if (opt_fd->dynindx != -1)
7378 {
7379 /* Use __tls_get_addr_opt in dynamic relocations. */
7380 opt_fd->dynindx = -1;
7381 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7382 opt_fd->dynstr_index);
7383 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7384 return NULL;
a7f2871e
AM
7385 }
7386 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7387 tga = &htab->tls_get_addr->elf;
7388 if (opt != NULL && tga != NULL)
7389 {
7390 tga->root.type = bfd_link_hash_indirect;
7391 tga->root.u.i.link = &opt->root;
7392 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7393 _bfd_elf_link_hash_hide_symbol (info, opt,
7394 tga->forced_local);
7395 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7396 }
7397 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7398 htab->tls_get_addr_fd->is_func_descriptor = 1;
7399 if (htab->tls_get_addr != NULL)
7400 {
7401 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7402 htab->tls_get_addr->is_func = 1;
7403 }
7404 }
7405 }
7406 }
7407 else
7408 no_tls_get_addr_opt = TRUE;
7409 }
7410 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7411 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7412}
8387904d 7413
3a71aa26
AM
7414/* Return TRUE iff REL is a branch reloc with a global symbol matching
7415 HASH1 or HASH2. */
8387904d 7416
3a71aa26
AM
7417static bfd_boolean
7418branch_reloc_hash_match (const bfd *ibfd,
7419 const Elf_Internal_Rela *rel,
7420 const struct ppc_link_hash_entry *hash1,
7421 const struct ppc_link_hash_entry *hash2)
7422{
7423 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7424 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7425 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7426
e054468f 7427 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7428 {
3a71aa26
AM
7429 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7430 struct elf_link_hash_entry *h;
8387904d 7431
3a71aa26 7432 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7433 h = elf_follow_link (h);
3a71aa26
AM
7434 if (h == &hash1->elf || h == &hash2->elf)
7435 return TRUE;
a48ebf4d 7436 }
3a71aa26 7437 return FALSE;
951fd09b 7438}
411e1bfb 7439
951fd09b
AM
7440/* Run through all the TLS relocs looking for optimization
7441 opportunities. The linker has been hacked (see ppc64elf.em) to do
7442 a preliminary section layout so that we know the TLS segment
7443 offsets. We can't optimize earlier because some optimizations need
7444 to know the tp offset, and we need to optimize before allocating
7445 dynamic relocations. */
7446
7447bfd_boolean
33c0ec9d 7448ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7449{
7450 bfd *ibfd;
7451 asection *sec;
7452 struct ppc_link_hash_table *htab;
102890f0 7453 int pass;
951fd09b 7454
1d483afe 7455 if (info->relocatable || !info->executable)
411e1bfb
AM
7456 return TRUE;
7457
951fd09b 7458 htab = ppc_hash_table (info);
4dfe6ac6
NC
7459 if (htab == NULL)
7460 return FALSE;
7461
411e1bfb
AM
7462 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7463 {
7464 Elf_Internal_Sym *locsyms = NULL;
30038c59
AM
7465 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7466 unsigned char *toc_ref = NULL;
411e1bfb 7467
102890f0
AM
7468 /* Look at all the sections for this file. Make two passes over
7469 the relocs. On the first pass, mark toc entries involved
7470 with tls relocs, and check that tls relocs involved in
7471 setting up a tls_get_addr call are indeed followed by such a
7472 call. If they are not, exclude them from the optimizations
7473 done on the second pass. */
7474 for (pass = 0; pass < 2; ++pass)
7475 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7476 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7477 {
7478 Elf_Internal_Rela *relstart, *rel, *relend;
411e1bfb 7479
102890f0
AM
7480 /* Read the relocations. */
7481 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7482 info->keep_memory);
7483 if (relstart == NULL)
7484 return FALSE;
411e1bfb 7485
102890f0
AM
7486 relend = relstart + sec->reloc_count;
7487 for (rel = relstart; rel < relend; rel++)
7488 {
7489 enum elf_ppc64_reloc_type r_type;
7490 unsigned long r_symndx;
7491 struct elf_link_hash_entry *h;
7492 Elf_Internal_Sym *sym;
7493 asection *sym_sec;
f961d9dd
AM
7494 unsigned char *tls_mask;
7495 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7496 bfd_vma value;
7497 bfd_boolean ok_tprel, is_local;
7498 long toc_ref_index = 0;
7499 int expecting_tls_get_addr = 0;
411e1bfb 7500
102890f0
AM
7501 r_symndx = ELF64_R_SYM (rel->r_info);
7502 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7503 r_symndx, ibfd))
7504 {
7505 err_free_rel:
7506 if (elf_section_data (sec)->relocs != relstart)
7507 free (relstart);
7508 if (toc_ref != NULL)
7509 free (toc_ref);
7510 if (locsyms != NULL
0ffa91dd 7511 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7512 != (unsigned char *) locsyms))
7513 free (locsyms);
7514 return FALSE;
7515 }
411e1bfb 7516
102890f0
AM
7517 if (h != NULL)
7518 {
766bc656
AM
7519 if (h->root.type == bfd_link_hash_defined
7520 || h->root.type == bfd_link_hash_defweak)
7521 value = h->root.u.def.value;
7522 else if (h->root.type == bfd_link_hash_undefweak)
7523 value = 0;
7524 else
102890f0 7525 continue;
102890f0
AM
7526 }
7527 else
7528 /* Symbols referenced by TLS relocs must be of type
7529 STT_TLS. So no need for .opd local sym adjust. */
7530 value = sym->st_value;
7531
7532 ok_tprel = FALSE;
7533 is_local = FALSE;
7534 if (h == NULL
7535 || !h->def_dynamic)
7536 {
7537 is_local = TRUE;
766bc656
AM
7538 if (h != NULL
7539 && h->root.type == bfd_link_hash_undefweak)
7540 ok_tprel = TRUE;
7541 else
7542 {
7543 value += sym_sec->output_offset;
7544 value += sym_sec->output_section->vma;
7545 value -= htab->elf.tls_sec->vma;
7546 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7547 < (bfd_vma) 1 << 32);
7548 }
102890f0 7549 }
951fd09b 7550
102890f0
AM
7551 r_type = ELF64_R_TYPE (rel->r_info);
7552 switch (r_type)
7553 {
7554 case R_PPC64_GOT_TLSLD16:
7555 case R_PPC64_GOT_TLSLD16_LO:
7556 expecting_tls_get_addr = 1;
7557 /* Fall thru */
7558
7559 case R_PPC64_GOT_TLSLD16_HI:
7560 case R_PPC64_GOT_TLSLD16_HA:
7561 /* These relocs should never be against a symbol
7562 defined in a shared lib. Leave them alone if
7563 that turns out to be the case. */
7564 if (!is_local)
7565 continue;
411e1bfb 7566
102890f0 7567 /* LD -> LE */
411e1bfb 7568 tls_set = 0;
102890f0
AM
7569 tls_clear = TLS_LD;
7570 tls_type = TLS_TLS | TLS_LD;
7571 break;
411e1bfb 7572
102890f0
AM
7573 case R_PPC64_GOT_TLSGD16:
7574 case R_PPC64_GOT_TLSGD16_LO:
7575 expecting_tls_get_addr = 1;
7576 /* Fall thru */
7577
7578 case R_PPC64_GOT_TLSGD16_HI:
7579 case R_PPC64_GOT_TLSGD16_HA:
7580 if (ok_tprel)
7581 /* GD -> LE */
411e1bfb 7582 tls_set = 0;
102890f0
AM
7583 else
7584 /* GD -> IE */
7585 tls_set = TLS_TLS | TLS_TPRELGD;
7586 tls_clear = TLS_GD;
7587 tls_type = TLS_TLS | TLS_GD;
7588 break;
7589
7590 case R_PPC64_GOT_TPREL16_DS:
7591 case R_PPC64_GOT_TPREL16_LO_DS:
7592 case R_PPC64_GOT_TPREL16_HI:
7593 case R_PPC64_GOT_TPREL16_HA:
7594 if (ok_tprel)
7595 {
7596 /* IE -> LE */
7597 tls_set = 0;
7598 tls_clear = TLS_TPREL;
7599 tls_type = TLS_TLS | TLS_TPREL;
7600 break;
7601 }
411e1bfb
AM
7602 continue;
7603
102890f0
AM
7604 case R_PPC64_TOC16:
7605 case R_PPC64_TOC16_LO:
7606 case R_PPC64_TLS:
727fc41e
AM
7607 case R_PPC64_TLSGD:
7608 case R_PPC64_TLSLD:
102890f0
AM
7609 if (sym_sec == NULL || sym_sec != toc)
7610 continue;
7611
7612 /* Mark this toc entry as referenced by a TLS
7613 code sequence. We can do that now in the
7614 case of R_PPC64_TLS, and after checking for
7615 tls_get_addr for the TOC16 relocs. */
7616 if (toc_ref == NULL)
7617 {
7618 toc_ref = bfd_zmalloc (toc->size / 8);
7619 if (toc_ref == NULL)
7620 goto err_free_rel;
7621 }
7622 if (h != NULL)
7623 value = h->root.u.def.value;
7624 else
7625 value = sym->st_value;
7626 value += rel->r_addend;
7627 BFD_ASSERT (value < toc->size && value % 8 == 0);
7628 toc_ref_index = value / 8;
727fc41e
AM
7629 if (r_type == R_PPC64_TLS
7630 || r_type == R_PPC64_TLSGD
7631 || r_type == R_PPC64_TLSLD)
102890f0
AM
7632 {
7633 toc_ref[toc_ref_index] = 1;
7634 continue;
7635 }
7636
7637 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7638 continue;
7639
7640 tls_set = 0;
7641 tls_clear = 0;
7642 expecting_tls_get_addr = 2;
7643 break;
7644
7645 case R_PPC64_TPREL64:
7646 if (pass == 0
7647 || sec != toc
7648 || toc_ref == NULL
7649 || !toc_ref[rel->r_offset / 8])
7650 continue;
7651 if (ok_tprel)
7652 {
7653 /* IE -> LE */
7654 tls_set = TLS_EXPLICIT;
7655 tls_clear = TLS_TPREL;
7656 break;
7657 }
7658 continue;
7659
7660 case R_PPC64_DTPMOD64:
7661 if (pass == 0
7662 || sec != toc
7663 || toc_ref == NULL
7664 || !toc_ref[rel->r_offset / 8])
7665 continue;
7666 if (rel + 1 < relend
7667 && (rel[1].r_info
7668 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7669 && rel[1].r_offset == rel->r_offset + 8)
7670 {
7671 if (ok_tprel)
7672 /* GD -> LE */
7673 tls_set = TLS_EXPLICIT | TLS_GD;
7674 else
7675 /* GD -> IE */
7676 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7677 tls_clear = TLS_GD;
7678 }
7679 else
7680 {
7681 if (!is_local)
7682 continue;
7683
7684 /* LD -> LE */
7685 tls_set = TLS_EXPLICIT;
7686 tls_clear = TLS_LD;
7687 }
7688 break;
7689
7690 default:
7691 continue;
7692 }
7693
7694 if (pass == 0)
7695 {
727fc41e
AM
7696 if (!expecting_tls_get_addr
7697 || !sec->has_tls_get_addr_call)
102890f0
AM
7698 continue;
7699
3a71aa26
AM
7700 if (rel + 1 < relend
7701 && branch_reloc_hash_match (ibfd, rel + 1,
7702 htab->tls_get_addr,
7703 htab->tls_get_addr_fd))
102890f0 7704 {
3a71aa26 7705 if (expecting_tls_get_addr == 2)
102890f0 7706 {
3a71aa26 7707 /* Check for toc tls entries. */
f961d9dd 7708 unsigned char *toc_tls;
3a71aa26
AM
7709 int retval;
7710
7711 retval = get_tls_mask (&toc_tls, NULL, NULL,
7712 &locsyms,
7713 rel, ibfd);
7714 if (retval == 0)
7715 goto err_free_rel;
7716 if (retval > 1 && toc_tls != NULL)
7717 toc_ref[toc_ref_index] = 1;
102890f0 7718 }
3a71aa26 7719 continue;
102890f0
AM
7720 }
7721
7722 if (expecting_tls_get_addr != 1)
7723 continue;
7724
7725 /* Uh oh, we didn't find the expected call. We
7726 could just mark this symbol to exclude it
7727 from tls optimization but it's safer to skip
7728 the entire section. */
7729 sec->has_tls_reloc = 0;
7730 break;
7731 }
7732
85f7a9cb 7733 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
7734 {
7735 struct plt_entry *ent;
7736 for (ent = htab->tls_get_addr->elf.plt.plist;
7737 ent != NULL;
7738 ent = ent->next)
7739 if (ent->addend == 0)
411e1bfb 7740 {
102890f0 7741 if (ent->plt.refcount > 0)
30038c59 7742 {
102890f0
AM
7743 ent->plt.refcount -= 1;
7744 expecting_tls_get_addr = 0;
30038c59 7745 }
102890f0 7746 break;
411e1bfb 7747 }
102890f0 7748 }
411e1bfb 7749
85f7a9cb 7750 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
7751 {
7752 struct plt_entry *ent;
7753 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7754 ent != NULL;
7755 ent = ent->next)
7756 if (ent->addend == 0)
411e1bfb 7757 {
102890f0
AM
7758 if (ent->plt.refcount > 0)
7759 ent->plt.refcount -= 1;
7760 break;
411e1bfb 7761 }
102890f0 7762 }
411e1bfb 7763
102890f0 7764 if (tls_clear == 0)
30038c59
AM
7765 continue;
7766
102890f0
AM
7767 if ((tls_set & TLS_EXPLICIT) == 0)
7768 {
7769 struct got_entry *ent;
411e1bfb 7770
102890f0
AM
7771 /* Adjust got entry for this reloc. */
7772 if (h != NULL)
7773 ent = h->got.glist;
7774 else
7775 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7776
102890f0
AM
7777 for (; ent != NULL; ent = ent->next)
7778 if (ent->addend == rel->r_addend
7779 && ent->owner == ibfd
7780 && ent->tls_type == tls_type)
7781 break;
7782 if (ent == NULL)
7783 abort ();
411e1bfb 7784
102890f0
AM
7785 if (tls_set == 0)
7786 {
7787 /* We managed to get rid of a got entry. */
7788 if (ent->got.refcount > 0)
7789 ent->got.refcount -= 1;
7790 }
7791 }
7792 else
7793 {
7794 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7795 we'll lose one or two dyn relocs. */
7796 if (!dec_dynrel_count (rel->r_info, sec, info,
7797 NULL, h, sym_sec))
7798 return FALSE;
411e1bfb 7799
102890f0
AM
7800 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7801 {
7802 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7803 NULL, h, sym_sec))
7804 return FALSE;
7805 }
7806 }
411e1bfb 7807
102890f0
AM
7808 *tls_mask |= tls_set;
7809 *tls_mask &= ~tls_clear;
7810 }
8c1d1bb8 7811
102890f0
AM
7812 if (elf_section_data (sec)->relocs != relstart)
7813 free (relstart);
7814 }
411e1bfb 7815
727fc41e
AM
7816 if (toc_ref != NULL)
7817 free (toc_ref);
411e1bfb 7818
727fc41e
AM
7819 if (locsyms != NULL
7820 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7821 {
7822 if (!info->keep_memory)
7823 free (locsyms);
7824 else
7825 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7826 }
7827 }
b34976b6 7828 return TRUE;
1e2f5b6e 7829}
b34976b6 7830
c5614fa4
AM
7831/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7832 the values of any global symbols in a toc section that has been
7833 edited. Globals in toc sections should be a rarity, so this function
7834 sets a flag if any are found in toc sections other than the one just
7835 edited, so that futher hash table traversals can be avoided. */
7836
7837struct adjust_toc_info
7838{
7839 asection *toc;
7840 unsigned long *skip;
7841 bfd_boolean global_toc_syms;
7842};
7843
ba761f19
AM
7844enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7845
c5614fa4
AM
7846static bfd_boolean
7847adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7848{
7849 struct ppc_link_hash_entry *eh;
7850 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7851 unsigned long i;
c5614fa4
AM
7852
7853 if (h->root.type == bfd_link_hash_indirect)
7854 return TRUE;
7855
7856 if (h->root.type == bfd_link_hash_warning)
7857 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7858
7859 if (h->root.type != bfd_link_hash_defined
7860 && h->root.type != bfd_link_hash_defweak)
7861 return TRUE;
7862
7863 eh = (struct ppc_link_hash_entry *) h;
7864 if (eh->adjust_done)
7865 return TRUE;
7866
7867 if (eh->elf.root.u.def.section == toc_inf->toc)
7868 {
854b41e7
AM
7869 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7870 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7871 else
854b41e7
AM
7872 i = eh->elf.root.u.def.value >> 3;
7873
ba761f19 7874 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
7875 {
7876 (*_bfd_error_handler)
854b41e7
AM
7877 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7878 do
7879 ++i;
ba761f19 7880 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 7881 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 7882 }
854b41e7
AM
7883
7884 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
7885 eh->adjust_done = 1;
7886 }
7887 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7888 toc_inf->global_toc_syms = TRUE;
7889
7890 return TRUE;
7891}
7892
7893/* Examine all relocs referencing .toc sections in order to remove
7894 unused .toc entries. */
7895
7896bfd_boolean
33c0ec9d 7897ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
7898{
7899 bfd *ibfd;
7900 struct adjust_toc_info toc_inf;
67f0cbdb 7901 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 7902
67f0cbdb 7903 htab->do_toc_opt = 1;
c5614fa4
AM
7904 toc_inf.global_toc_syms = TRUE;
7905 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7906 {
7907 asection *toc, *sec;
7908 Elf_Internal_Shdr *symtab_hdr;
7909 Elf_Internal_Sym *local_syms;
425b145b 7910 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
7911 unsigned long *skip, *drop;
7912 unsigned char *used;
7913 unsigned char *keep, last, some_unused;
7914
854b41e7
AM
7915 if (!is_ppc64_elf (ibfd))
7916 continue;
7917
c5614fa4
AM
7918 toc = bfd_get_section_by_name (ibfd, ".toc");
7919 if (toc == NULL
92b7a70f 7920 || toc->size == 0
c5614fa4
AM
7921 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7922 || elf_discarded_section (toc))
7923 continue;
7924
425b145b 7925 toc_relocs = NULL;
c5614fa4 7926 local_syms = NULL;
0ffa91dd 7927 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
7928
7929 /* Look at sections dropped from the final link. */
7930 skip = NULL;
7931 relstart = NULL;
7932 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7933 {
7934 if (sec->reloc_count == 0
7935 || !elf_discarded_section (sec)
7936 || get_opd_info (sec)
7937 || (sec->flags & SEC_ALLOC) == 0
7938 || (sec->flags & SEC_DEBUGGING) != 0)
7939 continue;
7940
7941 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7942 if (relstart == NULL)
7943 goto error_ret;
7944
7945 /* Run through the relocs to see which toc entries might be
7946 unused. */
7947 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7948 {
7949 enum elf_ppc64_reloc_type r_type;
7950 unsigned long r_symndx;
7951 asection *sym_sec;
7952 struct elf_link_hash_entry *h;
7953 Elf_Internal_Sym *sym;
7954 bfd_vma val;
7955
7956 r_type = ELF64_R_TYPE (rel->r_info);
7957 switch (r_type)
7958 {
7959 default:
7960 continue;
7961
7962 case R_PPC64_TOC16:
7963 case R_PPC64_TOC16_LO:
7964 case R_PPC64_TOC16_HI:
7965 case R_PPC64_TOC16_HA:
7966 case R_PPC64_TOC16_DS:
7967 case R_PPC64_TOC16_LO_DS:
7968 break;
7969 }
7970
7971 r_symndx = ELF64_R_SYM (rel->r_info);
7972 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7973 r_symndx, ibfd))
7974 goto error_ret;
7975
7976 if (sym_sec != toc)
7977 continue;
7978
7979 if (h != NULL)
7980 val = h->root.u.def.value;
7981 else
7982 val = sym->st_value;
7983 val += rel->r_addend;
7984
7985 if (val >= toc->size)
7986 continue;
7987
7988 /* Anything in the toc ought to be aligned to 8 bytes.
7989 If not, don't mark as unused. */
7990 if (val & 7)
7991 continue;
7992
7993 if (skip == NULL)
7994 {
854b41e7 7995 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
7996 if (skip == NULL)
7997 goto error_ret;
7998 }
7999
ba761f19 8000 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8001 }
8002
8003 if (elf_section_data (sec)->relocs != relstart)
8004 free (relstart);
8005 }
8006
ba761f19
AM
8007 /* For largetoc loads of address constants, we can convert
8008 . addis rx,2,addr@got@ha
8009 . ld ry,addr@got@l(rx)
8010 to
8011 . addis rx,2,addr@toc@ha
8012 . addi ry,rx,addr@toc@l
8013 when addr is within 2G of the toc pointer. This then means
8014 that the word storing "addr" in the toc is no longer needed. */
8015
8016 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8017 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8018 && toc->reloc_count != 0)
8019 {
8020 /* Read toc relocs. */
425b145b
AM
8021 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8022 info->keep_memory);
8023 if (toc_relocs == NULL)
ba761f19
AM
8024 goto error_ret;
8025
425b145b 8026 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8027 {
8028 enum elf_ppc64_reloc_type r_type;
8029 unsigned long r_symndx;
8030 asection *sym_sec;
8031 struct elf_link_hash_entry *h;
8032 Elf_Internal_Sym *sym;
8033 bfd_vma val, addr;
8034
8035 r_type = ELF64_R_TYPE (rel->r_info);
8036 if (r_type != R_PPC64_ADDR64)
8037 continue;
8038
8039 r_symndx = ELF64_R_SYM (rel->r_info);
8040 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8041 r_symndx, ibfd))
8042 goto error_ret;
8043
425b145b
AM
8044 if (sym_sec == NULL
8045 || elf_discarded_section (sym_sec))
8046 continue;
8047
726d3ab0 8048 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8049 continue;
8050
8051 if (h != NULL)
bddc25c9
AM
8052 {
8053 if (h->type == STT_GNU_IFUNC)
8054 continue;
8055 val = h->root.u.def.value;
8056 }
ba761f19 8057 else
bddc25c9
AM
8058 {
8059 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8060 continue;
8061 val = sym->st_value;
8062 }
ba761f19
AM
8063 val += rel->r_addend;
8064 val += sym_sec->output_section->vma + sym_sec->output_offset;
8065
8066 /* We don't yet know the exact toc pointer value, but we
8067 know it will be somewhere in the toc section. Don't
8068 optimize if the difference from any possible toc
8069 pointer is outside [ff..f80008000, 7fff7fff]. */
8070 addr = toc->output_section->vma + TOC_BASE_OFF;
8071 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8072 continue;
8073
8074 addr = toc->output_section->vma + toc->output_section->rawsize;
8075 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8076 continue;
8077
8078 if (skip == NULL)
8079 {
8080 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8081 if (skip == NULL)
8082 goto error_ret;
8083 }
8084
8085 skip[rel->r_offset >> 3]
425b145b 8086 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8087 }
ba761f19
AM
8088 }
8089
c5614fa4
AM
8090 if (skip == NULL)
8091 continue;
8092
8093 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8094 if (used == NULL)
8095 {
8096 error_ret:
8097 if (local_syms != NULL
8098 && symtab_hdr->contents != (unsigned char *) local_syms)
8099 free (local_syms);
8100 if (sec != NULL
8101 && relstart != NULL
8102 && elf_section_data (sec)->relocs != relstart)
8103 free (relstart);
425b145b
AM
8104 if (toc_relocs != NULL
8105 && elf_section_data (toc)->relocs != toc_relocs)
8106 free (toc_relocs);
c5614fa4
AM
8107 if (skip != NULL)
8108 free (skip);
8109 return FALSE;
8110 }
8111
30038c59
AM
8112 /* Now check all kept sections that might reference the toc.
8113 Check the toc itself last. */
8114 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8115 : ibfd->sections);
c5614fa4 8116 sec != NULL;
c5614fa4 8117 sec = (sec == toc ? NULL
c5614fa4 8118 : sec->next == NULL ? toc
30038c59 8119 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8120 : sec->next))
8121 {
8122 int repeat;
8123
8124 if (sec->reloc_count == 0
8125 || elf_discarded_section (sec)
8126 || get_opd_info (sec)
8127 || (sec->flags & SEC_ALLOC) == 0
8128 || (sec->flags & SEC_DEBUGGING) != 0)
8129 continue;
8130
854b41e7
AM
8131 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8132 info->keep_memory);
c5614fa4
AM
8133 if (relstart == NULL)
8134 goto error_ret;
8135
8136 /* Mark toc entries referenced as used. */
8137 repeat = 0;
8138 do
8139 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8140 {
8141 enum elf_ppc64_reloc_type r_type;
8142 unsigned long r_symndx;
8143 asection *sym_sec;
8144 struct elf_link_hash_entry *h;
8145 Elf_Internal_Sym *sym;
8146 bfd_vma val;
8147
8148 r_type = ELF64_R_TYPE (rel->r_info);
8149 switch (r_type)
8150 {
8151 case R_PPC64_TOC16:
8152 case R_PPC64_TOC16_LO:
8153 case R_PPC64_TOC16_HI:
8154 case R_PPC64_TOC16_HA:
8155 case R_PPC64_TOC16_DS:
8156 case R_PPC64_TOC16_LO_DS:
8157 /* In case we're taking addresses of toc entries. */
8158 case R_PPC64_ADDR64:
8159 break;
8160
8161 default:
8162 continue;
8163 }
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 {
8169 free (used);
8170 goto error_ret;
8171 }
8172
8173 if (sym_sec != toc)
8174 continue;
8175
8176 if (h != NULL)
8177 val = h->root.u.def.value;
8178 else
8179 val = sym->st_value;
8180 val += rel->r_addend;
8181
8182 if (val >= toc->size)
8183 continue;
8184
ba761f19
AM
8185 if ((skip[val >> 3] & can_optimize) != 0)
8186 {
8187 bfd_vma off;
8188 unsigned char opc;
8189
8190 switch (r_type)
8191 {
8192 case R_PPC64_TOC16_HA:
8193 break;
8194
8195 case R_PPC64_TOC16_LO_DS:
8196 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8197 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8198 return FALSE;
8199 if ((opc & (0x3f << 2)) == (58u << 2))
8200 break;
8201 /* Fall thru */
8202
8203 default:
8204 /* Wrong sort of reloc, or not a ld. We may
8205 as well clear ref_from_discarded too. */
8206 skip[val >> 3] = 0;
8207 }
8208 }
8209
c5614fa4
AM
8210 /* For the toc section, we only mark as used if
8211 this entry itself isn't unused. */
8212 if (sec == toc
8213 && !used[val >> 3]
8214 && (used[rel->r_offset >> 3]
ba761f19 8215 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
c5614fa4
AM
8216 /* Do all the relocs again, to catch reference
8217 chains. */
8218 repeat = 1;
8219
8220 used[val >> 3] = 1;
8221 }
8222 while (repeat);
854b41e7
AM
8223
8224 if (elf_section_data (sec)->relocs != relstart)
8225 free (relstart);
c5614fa4
AM
8226 }
8227
8228 /* Merge the used and skip arrays. Assume that TOC
8229 doublewords not appearing as either used or unused belong
8230 to to an entry more than one doubleword in size. */
8231 for (drop = skip, keep = used, last = 0, some_unused = 0;
8232 drop < skip + (toc->size + 7) / 8;
8233 ++drop, ++keep)
8234 {
8235 if (*keep)
8236 {
ba761f19
AM
8237 *drop &= ~ref_from_discarded;
8238 if ((*drop & can_optimize) != 0)
8239 some_unused = 1;
c5614fa4
AM
8240 last = 0;
8241 }
8242 else if (*drop)
8243 {
8244 some_unused = 1;
ba761f19 8245 last = ref_from_discarded;
c5614fa4
AM
8246 }
8247 else
8248 *drop = last;
8249 }
8250
8251 free (used);
8252
8253 if (some_unused)
8254 {
8255 bfd_byte *contents, *src;
8256 unsigned long off;
d62b3684 8257 Elf_Internal_Sym *sym;
ba761f19 8258 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8259
8260 /* Shuffle the toc contents, and at the same time convert the
8261 skip array from booleans into offsets. */
8262 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8263 goto error_ret;
8264
8265 elf_section_data (toc)->this_hdr.contents = contents;
8266
8267 for (src = contents, off = 0, drop = skip;
8268 src < contents + toc->size;
8269 src += 8, ++drop)
8270 {
ba761f19
AM
8271 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8272 off += 8;
c5614fa4
AM
8273 else if (off != 0)
8274 {
8275 *drop = off;
8276 memcpy (src - off, src, 8);
8277 }
8278 }
854b41e7 8279 *drop = off;
c5614fa4
AM
8280 toc->rawsize = toc->size;
8281 toc->size = src - contents - off;
8282
ba761f19
AM
8283 /* Adjust addends for relocs against the toc section sym,
8284 and optimize any accesses we can. */
c5614fa4
AM
8285 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8286 {
8287 if (sec->reloc_count == 0
8288 || elf_discarded_section (sec))
8289 continue;
8290
8291 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8292 info->keep_memory);
c5614fa4
AM
8293 if (relstart == NULL)
8294 goto error_ret;
8295
8296 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8297 {
8298 enum elf_ppc64_reloc_type r_type;
8299 unsigned long r_symndx;
8300 asection *sym_sec;
8301 struct elf_link_hash_entry *h;
854b41e7 8302 bfd_vma val;
c5614fa4
AM
8303
8304 r_type = ELF64_R_TYPE (rel->r_info);
8305 switch (r_type)
8306 {
8307 default:
8308 continue;
8309
8310 case R_PPC64_TOC16:
8311 case R_PPC64_TOC16_LO:
8312 case R_PPC64_TOC16_HI:
8313 case R_PPC64_TOC16_HA:
8314 case R_PPC64_TOC16_DS:
8315 case R_PPC64_TOC16_LO_DS:
8316 case R_PPC64_ADDR64:
8317 break;
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 goto error_ret;
8324
ba761f19 8325 if (sym_sec != toc)
c5614fa4
AM
8326 continue;
8327
ba761f19
AM
8328 if (h != NULL)
8329 val = h->root.u.def.value;
8330 else
8331 {
8332 val = sym->st_value;
8333 if (val != 0)
8334 local_toc_syms = TRUE;
8335 }
8336
8337 val += rel->r_addend;
854b41e7
AM
8338
8339 if (val > toc->rawsize)
8340 val = toc->rawsize;
ba761f19
AM
8341 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8342 continue;
8343 else if ((skip[val >> 3] & can_optimize) != 0)
8344 {
8345 Elf_Internal_Rela *tocrel
425b145b 8346 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8347 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8348
8349 switch (r_type)
8350 {
8351 case R_PPC64_TOC16_HA:
8352 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8353 break;
8354
8355 case R_PPC64_TOC16_LO_DS:
8356 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8357 break;
8358
8359 default:
8360 abort ();
8361 }
8362 rel->r_addend = tocrel->r_addend;
8363 elf_section_data (sec)->relocs = relstart;
8364 continue;
8365 }
8366
8367 if (h != NULL || sym->st_value != 0)
8368 continue;
854b41e7
AM
8369
8370 rel->r_addend -= skip[val >> 3];
8371 elf_section_data (sec)->relocs = relstart;
c5614fa4 8372 }
854b41e7
AM
8373
8374 if (elf_section_data (sec)->relocs != relstart)
8375 free (relstart);
c5614fa4
AM
8376 }
8377
8378 /* We shouldn't have local or global symbols defined in the TOC,
8379 but handle them anyway. */
df22d223
AM
8380 if (local_syms != NULL)
8381 for (sym = local_syms;
8382 sym < local_syms + symtab_hdr->sh_info;
8383 ++sym)
8384 if (sym->st_value != 0
8385 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8386 {
8387 unsigned long i;
854b41e7 8388
df22d223
AM
8389 if (sym->st_value > toc->rawsize)
8390 i = toc->rawsize >> 3;
8391 else
8392 i = sym->st_value >> 3;
854b41e7 8393
df22d223
AM
8394 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8395 {
8396 if (local_toc_syms)
8397 (*_bfd_error_handler)
8398 (_("%s defined on removed toc entry"),
8399 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8400 do
8401 ++i;
8402 while ((skip[i] & (ref_from_discarded | can_optimize)));
8403 sym->st_value = (bfd_vma) i << 3;
8404 }
d62b3684 8405
df22d223
AM
8406 sym->st_value -= skip[i];
8407 symtab_hdr->contents = (unsigned char *) local_syms;
8408 }
c5614fa4 8409
854b41e7 8410 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8411 if (toc_inf.global_toc_syms)
8412 {
8413 toc_inf.toc = toc;
8414 toc_inf.skip = skip;
8415 toc_inf.global_toc_syms = FALSE;
8416 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8417 &toc_inf);
8418 }
854b41e7
AM
8419
8420 if (toc->reloc_count != 0)
8421 {
d4730f92 8422 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8423 Elf_Internal_Rela *wrel;
8424 bfd_size_type sz;
8425
854b41e7 8426 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8427 if (toc_relocs == NULL)
8428 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8429 info->keep_memory);
8430 if (toc_relocs == NULL)
8431 goto error_ret;
8432
425b145b
AM
8433 wrel = toc_relocs;
8434 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8435 if ((skip[rel->r_offset >> 3]
8436 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8437 {
8438 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8439 wrel->r_info = rel->r_info;
8440 wrel->r_addend = rel->r_addend;
8441 ++wrel;
8442 }
8443 else if (!dec_dynrel_count (rel->r_info, toc, info,
8444 &local_syms, NULL, NULL))
8445 goto error_ret;
8446
425b145b
AM
8447 elf_section_data (toc)->relocs = toc_relocs;
8448 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8449 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8450 sz = rel_hdr->sh_entsize;
8451 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8452 }
c5614fa4 8453 }
28be611c
AM
8454 else if (toc_relocs != NULL
8455 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8456 free (toc_relocs);
c5614fa4
AM
8457
8458 if (local_syms != NULL
8459 && symtab_hdr->contents != (unsigned char *) local_syms)
8460 {
8461 if (!info->keep_memory)
8462 free (local_syms);
8463 else
8464 symtab_hdr->contents = (unsigned char *) local_syms;
8465 }
8466 free (skip);
8467 }
8468
8469 return TRUE;
8470}
8471
1bbe0902
AM
8472/* Return true iff input section I references the TOC using
8473 instructions limited to +/-32k offsets. */
8474
8475bfd_boolean
8476ppc64_elf_has_small_toc_reloc (asection *i)
8477{
8478 return (is_ppc64_elf (i->owner)
8479 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8480}
8481
927be08e
AM
8482/* Allocate space for one GOT entry. */
8483
8484static void
8485allocate_got (struct elf_link_hash_entry *h,
8486 struct bfd_link_info *info,
8487 struct got_entry *gent)
8488{
8489 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8490 bfd_boolean dyn;
8491 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8492 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8493 ? 16 : 8);
8494 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8495 ? 2 : 1) * sizeof (Elf64_External_Rela);
8496 asection *got = ppc64_elf_tdata (gent->owner)->got;
8497
8498 gent->got.offset = got->size;
8499 got->size += entsize;
8500
8501 dyn = htab->elf.dynamic_sections_created;
8502 if ((info->shared
8503 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8504 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8505 || h->root.type != bfd_link_hash_undefweak))
8506 {
8507 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8508 relgot->size += rentsize;
8509 }
8510 else if (h->type == STT_GNU_IFUNC)
8511 {
8512 asection *relgot = htab->reliplt;
8513 relgot->size += rentsize;
8514 htab->got_reli_size += rentsize;
8515 }
8516}
8517
7865406b
AM
8518/* This function merges got entries in the same toc group. */
8519
8520static void
8521merge_got_entries (struct got_entry **pent)
8522{
8523 struct got_entry *ent, *ent2;
8524
8525 for (ent = *pent; ent != NULL; ent = ent->next)
8526 if (!ent->is_indirect)
8527 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8528 if (!ent2->is_indirect
8529 && ent2->addend == ent->addend
8530 && ent2->tls_type == ent->tls_type
8531 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8532 {
8533 ent2->is_indirect = TRUE;
8534 ent2->got.ent = ent;
8535 }
8536}
8537
65f38f15
AM
8538/* Allocate space in .plt, .got and associated reloc sections for
8539 dynamic relocs. */
5bd4f169 8540
b34976b6 8541static bfd_boolean
4ce794b7 8542allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8543{
65f38f15
AM
8544 struct bfd_link_info *info;
8545 struct ppc_link_hash_table *htab;
5bd4f169 8546 asection *s;
65f38f15
AM
8547 struct ppc_link_hash_entry *eh;
8548 struct ppc_dyn_relocs *p;
0b8bcf0d 8549 struct got_entry **pgent, *gent;
5bd4f169 8550
e92d460e 8551 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8552 return TRUE;
5bd4f169 8553
e92d460e
AM
8554 if (h->root.type == bfd_link_hash_warning)
8555 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8556
65f38f15
AM
8557 info = (struct bfd_link_info *) inf;
8558 htab = ppc_hash_table (info);
4dfe6ac6
NC
8559 if (htab == NULL)
8560 return FALSE;
5bd4f169 8561
e054468f
AM
8562 if ((htab->elf.dynamic_sections_created
8563 && h->dynindx != -1
8564 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8565 || h->type == STT_GNU_IFUNC)
5bd4f169 8566 {
411e1bfb
AM
8567 struct plt_entry *pent;
8568 bfd_boolean doneone = FALSE;
8569 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8570 if (pent->plt.refcount > 0)
8571 {
25f23106
AM
8572 if (!htab->elf.dynamic_sections_created
8573 || h->dynindx == -1)
e054468f
AM
8574 {
8575 s = htab->iplt;
8576 pent->plt.offset = s->size;
8577 s->size += PLT_ENTRY_SIZE;
8578 s = htab->reliplt;
8579 }
8580 else
8581 {
8582 /* If this is the first .plt entry, make room for the special
8583 first entry. */
8584 s = htab->plt;
8585 if (s->size == 0)
8586 s->size += PLT_INITIAL_ENTRY_SIZE;
8587
8588 pent->plt.offset = s->size;
8589
8590 /* Make room for this entry. */
8591 s->size += PLT_ENTRY_SIZE;
8592
8593 /* Make room for the .glink code. */
8594 s = htab->glink;
8595 if (s->size == 0)
8596 s->size += GLINK_CALL_STUB_SIZE;
8597 /* We need bigger stubs past index 32767. */
8598 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8599 s->size += 4;
8600 s->size += 2*4;
8601
8602 /* We also need to make an entry in the .rela.plt section. */
8603 s = htab->relplt;
8604 }
eea6121a 8605 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
8606 doneone = TRUE;
8607 }
8608 else
8609 pent->plt.offset = (bfd_vma) -1;
8610 if (!doneone)
65f38f15 8611 {
411e1bfb 8612 h->plt.plist = NULL;
f5385ebf 8613 h->needs_plt = 0;
65f38f15
AM
8614 }
8615 }
8616 else
8617 {
411e1bfb 8618 h->plt.plist = NULL;
f5385ebf 8619 h->needs_plt = 0;
65f38f15
AM
8620 }
8621
951fd09b
AM
8622 eh = (struct ppc_link_hash_entry *) h;
8623 /* Run through the TLS GD got entries first if we're changing them
8624 to TPREL. */
e7b938ca 8625 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
8626 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8627 if (gent->got.refcount > 0
8628 && (gent->tls_type & TLS_GD) != 0)
8629 {
8630 /* This was a GD entry that has been converted to TPREL. If
8631 there happens to be a TPREL entry we can use that one. */
8632 struct got_entry *ent;
8633 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8634 if (ent->got.refcount > 0
8635 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8636 && ent->addend == gent->addend
8637 && ent->owner == gent->owner)
951fd09b
AM
8638 {
8639 gent->got.refcount = 0;
8640 break;
8641 }
8642
8643 /* If not, then we'll be using our own TPREL entry. */
8644 if (gent->got.refcount != 0)
8645 gent->tls_type = TLS_TLS | TLS_TPREL;
8646 }
8647
7865406b
AM
8648 /* Remove any list entry that won't generate a word in the GOT before
8649 we call merge_got_entries. Otherwise we risk merging to empty
8650 entries. */
0b8bcf0d
AM
8651 pgent = &h->got.glist;
8652 while ((gent = *pgent) != NULL)
411e1bfb 8653 if (gent->got.refcount > 0)
7865406b
AM
8654 {
8655 if ((gent->tls_type & TLS_LD) != 0
8656 && !h->def_dynamic)
8657 {
8658 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8659 *pgent = gent->next;
8660 }
8661 else
8662 pgent = &gent->next;
8663 }
8664 else
8665 *pgent = gent->next;
8666
8667 if (!htab->do_multi_toc)
8668 merge_got_entries (&h->got.glist);
8669
8670 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8671 if (!gent->is_indirect)
411e1bfb
AM
8672 {
8673 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
8674 Undefined weak syms won't yet be marked as dynamic,
8675 nor will all TLS symbols. */
411e1bfb 8676 if (h->dynindx == -1
b099ab9f 8677 && !h->forced_local
25f23106 8678 && h->type != STT_GNU_IFUNC
b099ab9f 8679 && htab->elf.dynamic_sections_created)
411e1bfb 8680 {
c152c796 8681 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
8682 return FALSE;
8683 }
65f38f15 8684
0c8d6e5c 8685 if (!is_ppc64_elf (gent->owner))
927be08e 8686 abort ();
0ffa91dd 8687
927be08e 8688 allocate_got (h, info, gent);
411e1bfb 8689 }
65f38f15 8690
b099ab9f 8691 if (eh->dyn_relocs == NULL
25f23106
AM
8692 || (!htab->elf.dynamic_sections_created
8693 && h->type != STT_GNU_IFUNC))
b34976b6 8694 return TRUE;
65f38f15
AM
8695
8696 /* In the shared -Bsymbolic case, discard space allocated for
8697 dynamic pc-relative relocs against symbols which turn out to be
8698 defined in regular objects. For the normal shared case, discard
8699 space for relocs that have become local due to symbol visibility
8700 changes. */
8701
8702 if (info->shared)
8703 {
9c7a29a3 8704 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 8705 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
8706 generated via assembly. We want calls to protected symbols to
8707 resolve directly to the function rather than going via the plt.
8708 If people want function pointer comparisons to work as expected
8709 then they should avoid writing weird assembly. */
09695f56 8710 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15
AM
8711 {
8712 struct ppc_dyn_relocs **pp;
8713
8714 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 8715 {
65f38f15
AM
8716 p->count -= p->pc_count;
8717 p->pc_count = 0;
8718 if (p->count == 0)
8719 *pp = p->next;
8720 else
8721 pp = &p->next;
5bd4f169 8722 }
65f38f15 8723 }
4e795f50
AM
8724
8725 /* Also discard relocs on undefined weak syms with non-default
8726 visibility. */
cab87ef9
AM
8727 if (eh->dyn_relocs != NULL
8728 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
8729 {
8730 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8731 eh->dyn_relocs = NULL;
8732
8733 /* Make sure this symbol is output as a dynamic symbol.
8734 Undefined weak syms won't yet be marked as dynamic. */
8735 else if (h->dynindx == -1
8736 && !h->forced_local)
8737 {
8738 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8739 return FALSE;
8740 }
8741 }
65f38f15 8742 }
25f23106
AM
8743 else if (h->type == STT_GNU_IFUNC)
8744 {
8745 if (!h->non_got_ref)
8746 eh->dyn_relocs = NULL;
8747 }
f4656909 8748 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
8749 {
8750 /* For the non-shared case, discard space for relocs against
8751 symbols which turn out to need copy relocs or are not
8752 dynamic. */
8753
f5385ebf 8754 if (!h->non_got_ref
f5385ebf 8755 && !h->def_regular)
65f38f15
AM
8756 {
8757 /* Make sure this symbol is output as a dynamic symbol.
8758 Undefined weak syms won't yet be marked as dynamic. */
8759 if (h->dynindx == -1
f5385ebf 8760 && !h->forced_local)
65f38f15 8761 {
c152c796 8762 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8763 return FALSE;
65f38f15
AM
8764 }
8765
8766 /* If that succeeded, we know we'll be keeping all the
8767 relocs. */
8768 if (h->dynindx != -1)
8769 goto keep;
8770 }
8771
8772 eh->dyn_relocs = NULL;
8773
ec338859 8774 keep: ;
65f38f15
AM
8775 }
8776
8777 /* Finally, allocate space. */
8778 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8779 {
8780 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
8781 if (!htab->elf.dynamic_sections_created)
8782 sreloc = htab->reliplt;
eea6121a 8783 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
8784 }
8785
b34976b6 8786 return TRUE;
65f38f15
AM
8787}
8788
8789/* Find any dynamic relocs that apply to read-only sections. */
8790
b34976b6 8791static bfd_boolean
4ce794b7 8792readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
8793{
8794 struct ppc_link_hash_entry *eh;
8795 struct ppc_dyn_relocs *p;
8796
e92d460e
AM
8797 if (h->root.type == bfd_link_hash_warning)
8798 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8799
65f38f15
AM
8800 eh = (struct ppc_link_hash_entry *) h;
8801 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8802 {
8803 asection *s = p->sec->output_section;
8804
8805 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8806 {
4ce794b7 8807 struct bfd_link_info *info = inf;
65f38f15
AM
8808
8809 info->flags |= DF_TEXTREL;
8810
8811 /* Not an error, just cut short the traversal. */
b34976b6 8812 return FALSE;
65f38f15
AM
8813 }
8814 }
b34976b6 8815 return TRUE;
65f38f15
AM
8816}
8817
8818/* Set the sizes of the dynamic sections. */
8819
b34976b6 8820static bfd_boolean
4ce794b7
AM
8821ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8822 struct bfd_link_info *info)
65f38f15
AM
8823{
8824 struct ppc_link_hash_table *htab;
8825 bfd *dynobj;
8826 asection *s;
b34976b6 8827 bfd_boolean relocs;
65f38f15 8828 bfd *ibfd;
7865406b 8829 struct got_entry *first_tlsld;
65f38f15
AM
8830
8831 htab = ppc_hash_table (info);
4dfe6ac6
NC
8832 if (htab == NULL)
8833 return FALSE;
8834
65f38f15
AM
8835 dynobj = htab->elf.dynobj;
8836 if (dynobj == NULL)
8837 abort ();
8838
8839 if (htab->elf.dynamic_sections_created)
8840 {
8841 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 8842 if (info->executable)
65f38f15
AM
8843 {
8844 s = bfd_get_section_by_name (dynobj, ".interp");
8845 if (s == NULL)
8846 abort ();
eea6121a 8847 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
8848 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8849 }
8850 }
8851
8852 /* Set up .got offsets for local syms, and space for local dynamic
8853 relocs. */
8854 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8855 {
411e1bfb
AM
8856 struct got_entry **lgot_ents;
8857 struct got_entry **end_lgot_ents;
e054468f
AM
8858 struct plt_entry **local_plt;
8859 struct plt_entry **end_local_plt;
f961d9dd 8860 unsigned char *lgot_masks;
65f38f15
AM
8861 bfd_size_type locsymcount;
8862 Elf_Internal_Shdr *symtab_hdr;
8863 asection *srel;
8864
0c8d6e5c 8865 if (!is_ppc64_elf (ibfd))
65f38f15
AM
8866 continue;
8867
8868 for (s = ibfd->sections; s != NULL; s = s->next)
8869 {
ec338859 8870 struct ppc_dyn_relocs *p;
65f38f15 8871
6edfbbad 8872 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 8873 {
ec338859
AM
8874 if (!bfd_is_abs_section (p->sec)
8875 && bfd_is_abs_section (p->sec->output_section))
8876 {
8877 /* Input section has been discarded, either because
8878 it is a copy of a linkonce section or due to
8879 linker script /DISCARD/, so we'll be discarding
8880 the relocs too. */
8881 }
248866a8 8882 else if (p->count != 0)
ec338859
AM
8883 {
8884 srel = elf_section_data (p->sec)->sreloc;
25f23106
AM
8885 if (!htab->elf.dynamic_sections_created)
8886 srel = htab->reliplt;
eea6121a 8887 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
8888 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8889 info->flags |= DF_TEXTREL;
ec338859 8890 }
65f38f15
AM
8891 }
8892 }
8893
411e1bfb
AM
8894 lgot_ents = elf_local_got_ents (ibfd);
8895 if (!lgot_ents)
65f38f15
AM
8896 continue;
8897
0ffa91dd 8898 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 8899 locsymcount = symtab_hdr->sh_info;
411e1bfb 8900 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
8901 local_plt = (struct plt_entry **) end_lgot_ents;
8902 end_local_plt = local_plt + locsymcount;
f961d9dd 8903 lgot_masks = (unsigned char *) end_local_plt;
e717da7e
AM
8904 s = ppc64_elf_tdata (ibfd)->got;
8905 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 8906 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 8907 {
0b8bcf0d 8908 struct got_entry **pent, *ent;
411e1bfb 8909
0b8bcf0d
AM
8910 pent = lgot_ents;
8911 while ((ent = *pent) != NULL)
411e1bfb
AM
8912 if (ent->got.refcount > 0)
8913 {
e7b938ca 8914 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 8915 {
927be08e 8916 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 8917 *pent = ent->next;
411e1bfb
AM
8918 }
8919 else
8920 {
25f23106 8921 unsigned int num = 1;
eea6121a 8922 ent->got.offset = s->size;
e7b938ca 8923 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
25f23106
AM
8924 num = 2;
8925 s->size += num * 8;
8926 if (info->shared)
8927 srel->size += num * sizeof (Elf64_External_Rela);
8928 else if ((*lgot_masks & PLT_IFUNC) != 0)
927be08e
AM
8929 {
8930 htab->reliplt->size
8931 += num * sizeof (Elf64_External_Rela);
8932 htab->got_reli_size
8933 += num * sizeof (Elf64_External_Rela);
8934 }
0b8bcf0d 8935 pent = &ent->next;
411e1bfb
AM
8936 }
8937 }
8938 else
0b8bcf0d 8939 *pent = ent->next;
65f38f15 8940 }
e054468f
AM
8941
8942 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
8943 for (; local_plt < end_local_plt; ++local_plt)
8944 {
8945 struct plt_entry *ent;
8946
8947 for (ent = *local_plt; ent != NULL; ent = ent->next)
8948 if (ent->plt.refcount > 0)
8949 {
91d6fa6a 8950 s = htab->iplt;
e054468f
AM
8951 ent->plt.offset = s->size;
8952 s->size += PLT_ENTRY_SIZE;
8953
25f23106 8954 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
8955 }
8956 else
8957 ent->plt.offset = (bfd_vma) -1;
8958 }
65f38f15
AM
8959 }
8960
8961 /* Allocate global sym .plt and .got entries, and space for global
8962 sym dynamic relocs. */
4ce794b7 8963 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 8964
7865406b 8965 first_tlsld = NULL;
102890f0
AM
8966 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8967 {
7865406b
AM
8968 struct got_entry *ent;
8969
0c8d6e5c 8970 if (!is_ppc64_elf (ibfd))
102890f0
AM
8971 continue;
8972
7865406b
AM
8973 ent = ppc64_tlsld_got (ibfd);
8974 if (ent->got.refcount > 0)
102890f0 8975 {
7865406b 8976 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 8977 {
7865406b
AM
8978 ent->is_indirect = TRUE;
8979 ent->got.ent = first_tlsld;
8980 }
8981 else
8982 {
8983 if (first_tlsld == NULL)
8984 first_tlsld = ent;
8985 s = ppc64_elf_tdata (ibfd)->got;
8986 ent->got.offset = s->size;
8987 ent->owner = ibfd;
8988 s->size += 16;
8989 if (info->shared)
8990 {
8991 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
8992 srel->size += sizeof (Elf64_External_Rela);
8993 }
102890f0
AM
8994 }
8995 }
8996 else
7865406b 8997 ent->got.offset = (bfd_vma) -1;
102890f0
AM
8998 }
8999
65f38f15
AM
9000 /* We now have determined the sizes of the various dynamic sections.
9001 Allocate memory for them. */
b34976b6 9002 relocs = FALSE;
65f38f15
AM
9003 for (s = dynobj->sections; s != NULL; s = s->next)
9004 {
9005 if ((s->flags & SEC_LINKER_CREATED) == 0)
9006 continue;
9007
4ce794b7 9008 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9009 /* These haven't been allocated yet; don't strip. */
9010 continue;
e717da7e
AM
9011 else if (s == htab->got
9012 || s == htab->plt
e054468f 9013 || s == htab->iplt
c456f082
AM
9014 || s == htab->glink
9015 || s == htab->dynbss)
65f38f15
AM
9016 {
9017 /* Strip this section if we don't need it; see the
9018 comment below. */
5bd4f169 9019 }
70cc837d 9020 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9021 {
c456f082 9022 if (s->size != 0)
5bd4f169 9023 {
4ce794b7 9024 if (s != htab->relplt)
b34976b6 9025 relocs = TRUE;
5bd4f169
AM
9026
9027 /* We use the reloc_count field as a counter if we need
9028 to copy relocs into the output file. */
9029 s->reloc_count = 0;
9030 }
9031 }
65f38f15 9032 else
5bd4f169
AM
9033 {
9034 /* It's not one of our sections, so don't allocate space. */
9035 continue;
9036 }
9037
eea6121a 9038 if (s->size == 0)
5bd4f169 9039 {
c456f082
AM
9040 /* If we don't need this section, strip it from the
9041 output file. This is mostly to handle .rela.bss and
9042 .rela.plt. We must create both sections in
9043 create_dynamic_sections, because they must be created
9044 before the linker maps input sections to output
9045 sections. The linker does that before
9046 adjust_dynamic_symbol is called, and it is that
9047 function which decides whether anything needs to go
9048 into these sections. */
8423293d 9049 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9050 continue;
9051 }
9052
c456f082 9053 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9054 continue;
9055
65f38f15
AM
9056 /* Allocate memory for the section contents. We use bfd_zalloc
9057 here in case unused entries are not reclaimed before the
9058 section's contents are written out. This should not happen,
411e1bfb
AM
9059 but this way if it does we get a R_PPC64_NONE reloc in .rela
9060 sections instead of garbage.
9061 We also rely on the section contents being zero when writing
9062 the GOT. */
eea6121a 9063 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9064 if (s->contents == NULL)
b34976b6 9065 return FALSE;
5bd4f169
AM
9066 }
9067
e717da7e
AM
9068 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9069 {
0c8d6e5c 9070 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9071 continue;
9072
e717da7e
AM
9073 s = ppc64_elf_tdata (ibfd)->got;
9074 if (s != NULL && s != htab->got)
9075 {
eea6121a 9076 if (s->size == 0)
8423293d 9077 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9078 else
9079 {
eea6121a 9080 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9081 if (s->contents == NULL)
9082 return FALSE;
9083 }
9084 }
9085 s = ppc64_elf_tdata (ibfd)->relgot;
9086 if (s != NULL)
9087 {
eea6121a 9088 if (s->size == 0)
8423293d 9089 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9090 else
9091 {
eea6121a 9092 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9093 if (s->contents == NULL)
9094 return FALSE;
9095 relocs = TRUE;
9096 s->reloc_count = 0;
9097 }
9098 }
9099 }
9100
e86ce104 9101 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9102 {
9103 /* Add some entries to the .dynamic section. We fill in the
9104 values later, in ppc64_elf_finish_dynamic_sections, but we
9105 must add the entries now so that we get the correct size for
9106 the .dynamic section. The DT_DEBUG entry is filled in by the
9107 dynamic linker and used by the debugger. */
dc810e39 9108#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9109 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9110
36af4a4e 9111 if (info->executable)
5bd4f169 9112 {
dc810e39 9113 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9114 return FALSE;
5bd4f169
AM
9115 }
9116
eea6121a 9117 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9118 {
dc810e39
AM
9119 if (!add_dynamic_entry (DT_PLTGOT, 0)
9120 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9121 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9122 || !add_dynamic_entry (DT_JMPREL, 0)
9123 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9124 return FALSE;
5bd4f169
AM
9125 }
9126
19397422
AM
9127 if (NO_OPD_RELOCS)
9128 {
9129 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9130 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9131 return FALSE;
19397422
AM
9132 }
9133
a7f2871e
AM
9134 if (!htab->no_tls_get_addr_opt
9135 && htab->tls_get_addr_fd != NULL
9136 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9137 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9138 return FALSE;
9139
5bd4f169
AM
9140 if (relocs)
9141 {
dc810e39
AM
9142 if (!add_dynamic_entry (DT_RELA, 0)
9143 || !add_dynamic_entry (DT_RELASZ, 0)
9144 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9145 return FALSE;
5bd4f169 9146
65f38f15
AM
9147 /* If any dynamic relocs apply to a read-only section,
9148 then we need a DT_TEXTREL entry. */
248866a8 9149 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9150 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9151
65f38f15 9152 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9153 {
65f38f15 9154 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9155 return FALSE;
5bd4f169 9156 }
5bd4f169 9157 }
5bd4f169 9158 }
65f38f15 9159#undef add_dynamic_entry
5bd4f169 9160
b34976b6 9161 return TRUE;
5bd4f169
AM
9162}
9163
721956f4 9164/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9165
4ce794b7
AM
9166static inline enum ppc_stub_type
9167ppc_type_of_stub (asection *input_sec,
9168 const Elf_Internal_Rela *rel,
9169 struct ppc_link_hash_entry **hash,
e054468f 9170 struct plt_entry **plt_ent,
4ce794b7 9171 bfd_vma destination)
5bd4f169 9172{
721956f4
AM
9173 struct ppc_link_hash_entry *h = *hash;
9174 bfd_vma location;
9175 bfd_vma branch_offset;
9176 bfd_vma max_branch_offset;
4ce794b7 9177 enum elf_ppc64_reloc_type r_type;
5bd4f169 9178
721956f4
AM
9179 if (h != NULL)
9180 {
e054468f 9181 struct plt_entry *ent;
7fe2b9a6 9182 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9183 if (h->oh != NULL
9184 && h->oh->is_func_descriptor)
7b8f6675
AM
9185 {
9186 fdh = ppc_follow_link (h->oh);
9187 *hash = fdh;
9188 }
8387904d 9189
e054468f
AM
9190 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9191 if (ent->addend == rel->r_addend
9192 && ent->plt.offset != (bfd_vma) -1)
9193 {
e054468f
AM
9194 *plt_ent = ent;
9195 return ppc_stub_plt_call;
9196 }
5bd4f169 9197
7fe2b9a6
AM
9198 /* Here, we know we don't have a plt entry. If we don't have a
9199 either a defined function descriptor or a defined entry symbol
9200 in a regular object file, then it is pointless trying to make
9201 any other type of stub. */
854b41e7
AM
9202 if (!is_static_defined (&fdh->elf)
9203 && !is_static_defined (&h->elf))
721956f4 9204 return ppc_stub_none;
5d1634d7 9205 }
e054468f
AM
9206 else if (elf_local_got_ents (input_sec->owner) != NULL)
9207 {
9208 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9209 struct plt_entry **local_plt = (struct plt_entry **)
9210 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9211 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9212
9213 if (local_plt[r_symndx] != NULL)
9214 {
9215 struct plt_entry *ent;
9216
9217 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9218 if (ent->addend == rel->r_addend
9219 && ent->plt.offset != (bfd_vma) -1)
9220 {
9221 *plt_ent = ent;
9222 return ppc_stub_plt_call;
9223 }
9224 }
9225 }
5d1634d7 9226
721956f4
AM
9227 /* Determine where the call point is. */
9228 location = (input_sec->output_offset
9229 + input_sec->output_section->vma
9230 + rel->r_offset);
5d1634d7 9231
721956f4
AM
9232 branch_offset = destination - location;
9233 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9234
721956f4
AM
9235 /* Determine if a long branch stub is needed. */
9236 max_branch_offset = 1 << 25;
4ce794b7 9237 if (r_type != R_PPC64_REL24)
721956f4 9238 max_branch_offset = 1 << 15;
5d1634d7 9239
721956f4
AM
9240 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9241 /* We need a stub. Figure out whether a long_branch or plt_branch
9242 is needed later. */
9243 return ppc_stub_long_branch;
5d1634d7 9244
721956f4 9245 return ppc_stub_none;
5d1634d7
AM
9246}
9247
9248/* Build a .plt call stub. */
9249
4ce794b7 9250static inline bfd_byte *
176a0d42 9251build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
5d1634d7
AM
9252{
9253#define PPC_LO(v) ((v) & 0xffff)
9254#define PPC_HI(v) (((v) >> 16) & 0xffff)
9255#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9256
ac2df442
AM
9257 if (PPC_HA (offset) != 0)
9258 {
176a0d42
AM
9259 if (r != NULL)
9260 {
9261 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9262 r[1].r_offset = r[0].r_offset + 8;
9263 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9264 r[1].r_addend = r[0].r_addend;
9265 if (PPC_HA (offset + 16) != PPC_HA (offset))
9266 {
9267 r[2].r_offset = r[1].r_offset + 4;
9268 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9269 r[2].r_addend = r[0].r_addend;
9270 }
9271 else
9272 {
9273 r[2].r_offset = r[1].r_offset + 8;
9274 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9275 r[2].r_addend = r[0].r_addend + 8;
9276 r[3].r_offset = r[2].r_offset + 4;
9277 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9278 r[3].r_addend = r[0].r_addend + 16;
9279 }
9280 }
ac2df442
AM
9281 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9282 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9283 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9284 if (PPC_HA (offset + 16) != PPC_HA (offset))
9285 {
9286 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9287 offset = 0;
9288 }
9289 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9290 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9291 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9292 bfd_put_32 (obfd, BCTR, p), p += 4;
9293 }
9294 else
9295 {
176a0d42
AM
9296 if (r != NULL)
9297 {
9298 r[0].r_offset += 4;
9299 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9300 if (PPC_HA (offset + 16) != PPC_HA (offset))
9301 {
9302 r[1].r_offset = r[0].r_offset + 4;
9303 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9304 r[1].r_addend = r[0].r_addend;
9305 }
9306 else
9307 {
9308 r[1].r_offset = r[0].r_offset + 8;
9309 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9310 r[1].r_addend = r[0].r_addend + 16;
9311 r[2].r_offset = r[1].r_offset + 4;
9312 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9313 r[2].r_addend = r[0].r_addend + 8;
9314 }
9315 }
ac2df442
AM
9316 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9317 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9318 if (PPC_HA (offset + 16) != PPC_HA (offset))
9319 {
9320 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9321 offset = 0;
9322 }
9323 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9324 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9325 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9326 bfd_put_32 (obfd, BCTR, p), p += 4;
9327 }
5d1634d7
AM
9328 return p;
9329}
9330
a7f2871e
AM
9331/* Build a special .plt call stub for __tls_get_addr. */
9332
9333#define LD_R11_0R3 0xe9630000
9334#define LD_R12_0R3 0xe9830000
9335#define MR_R0_R3 0x7c601b78
9336#define CMPDI_R11_0 0x2c2b0000
9337#define ADD_R3_R12_R13 0x7c6c6a14
9338#define BEQLR 0x4d820020
9339#define MR_R3_R0 0x7c030378
9340#define MFLR_R11 0x7d6802a6
9341#define STD_R11_0R1 0xf9610000
9342#define BCTRL 0x4e800421
9343#define LD_R11_0R1 0xe9610000
9344#define LD_R2_0R1 0xe8410000
9345#define MTLR_R11 0x7d6803a6
9346
9347static inline bfd_byte *
9348build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9349 Elf_Internal_Rela *r)
9350{
9351 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9352 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9353 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9354 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9355 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9356 bfd_put_32 (obfd, BEQLR, p), p += 4;
9357 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9358 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9359 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9360
9361 if (r != NULL)
9362 r[0].r_offset += 9 * 4;
9363 p = build_plt_stub (obfd, p, offset, r);
9364 bfd_put_32 (obfd, BCTRL, p - 4);
9365
9366 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9367 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9368 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9369 bfd_put_32 (obfd, BLR, p), p += 4;
9370
9371 return p;
9372}
9373
176a0d42
AM
9374static Elf_Internal_Rela *
9375get_relocs (asection *sec, int count)
9376{
9377 Elf_Internal_Rela *relocs;
9378 struct bfd_elf_section_data *elfsec_data;
9379
9380 elfsec_data = elf_section_data (sec);
9381 relocs = elfsec_data->relocs;
9382 if (relocs == NULL)
9383 {
9384 bfd_size_type relsize;
9385 relsize = sec->reloc_count * sizeof (*relocs);
9386 relocs = bfd_alloc (sec->owner, relsize);
9387 if (relocs == NULL)
9388 return NULL;
9389 elfsec_data->relocs = relocs;
d4730f92
BS
9390 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9391 sizeof (Elf_Internal_Shdr));
9392 if (elfsec_data->rela.hdr == NULL)
9393 return NULL;
9394 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9395 * sizeof (Elf64_External_Rela));
9396 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9397 sec->reloc_count = 0;
9398 }
9399 relocs += sec->reloc_count;
9400 sec->reloc_count += count;
9401 return relocs;
9402}
9403
b34976b6 9404static bfd_boolean
4ce794b7 9405ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 9406{
721956f4
AM
9407 struct ppc_stub_hash_entry *stub_entry;
9408 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
9409 struct bfd_link_info *info;
9410 struct ppc_link_hash_table *htab;
721956f4
AM
9411 bfd_byte *loc;
9412 bfd_byte *p;
ee75fd95 9413 bfd_vma dest, off;
721956f4 9414 int size;
176a0d42 9415 Elf_Internal_Rela *r;
e054468f 9416 asection *plt;
5d1634d7 9417
721956f4
AM
9418 /* Massage our args to the form they really have. */
9419 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 9420 info = in_arg;
5d1634d7 9421
5d1634d7 9422 htab = ppc_hash_table (info);
4dfe6ac6
NC
9423 if (htab == NULL)
9424 return FALSE;
5d1634d7 9425
721956f4 9426 /* Make a note of the offset within the stubs for this entry. */
eea6121a 9427 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 9428 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 9429
4ce794b7 9430 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 9431 switch (stub_entry->stub_type)
5d1634d7 9432 {
721956f4 9433 case ppc_stub_long_branch:
ad8e1ba5 9434 case ppc_stub_long_branch_r2off:
721956f4 9435 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
9436 off = dest = (stub_entry->target_value
9437 + stub_entry->target_section->output_offset
9438 + stub_entry->target_section->output_section->vma);
5d1634d7 9439
721956f4
AM
9440 /* And this is where we are coming from. */
9441 off -= (stub_entry->stub_offset
97b639ba
AM
9442 + stub_entry->stub_sec->output_offset
9443 + stub_entry->stub_sec->output_section->vma);
e86ce104 9444
ac2df442
AM
9445 size = 4;
9446 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5
AM
9447 {
9448 bfd_vma r2off;
9449
9450 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9451 - htab->stub_group[stub_entry->id_sec->id].toc_off);
97b639ba 9452 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9453 loc += 4;
ac2df442
AM
9454 size = 12;
9455 if (PPC_HA (r2off) != 0)
9456 {
9457 size = 16;
9458 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9459 loc += 4;
9460 }
97b639ba 9461 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 9462 loc += 4;
ac2df442 9463 off -= size - 4;
ad8e1ba5 9464 }
97b639ba 9465 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 9466
5c3dead3
AM
9467 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9468 {
9469 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
9470 stub_entry->root.string);
9471 htab->stub_error = TRUE;
9472 return FALSE;
9473 }
ee75fd95
AM
9474
9475 if (info->emitrelocations)
9476 {
176a0d42
AM
9477 r = get_relocs (stub_entry->stub_sec, 1);
9478 if (r == NULL)
9479 return FALSE;
ee75fd95
AM
9480 r->r_offset = loc - stub_entry->stub_sec->contents;
9481 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9482 r->r_addend = dest;
9483 if (stub_entry->h != NULL)
9484 {
9485 struct elf_link_hash_entry **hashes;
9486 unsigned long symndx;
9487 struct ppc_link_hash_entry *h;
9488
9489 hashes = elf_sym_hashes (htab->stub_bfd);
9490 if (hashes == NULL)
9491 {
9492 bfd_size_type hsize;
9493
9494 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9495 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9496 if (hashes == NULL)
9497 return FALSE;
9498 elf_sym_hashes (htab->stub_bfd) = hashes;
9499 htab->stub_globals = 1;
9500 }
9501 symndx = htab->stub_globals++;
9502 h = stub_entry->h;
9503 hashes[symndx] = &h->elf;
9504 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9505 if (h->oh != NULL && h->oh->is_func)
b31867b6 9506 h = ppc_follow_link (h->oh);
ee75fd95
AM
9507 if (h->elf.root.u.def.section != stub_entry->target_section)
9508 /* H is an opd symbol. The addend must be zero. */
9509 r->r_addend = 0;
9510 else
9511 {
9512 off = (h->elf.root.u.def.value
9513 + h->elf.root.u.def.section->output_offset
9514 + h->elf.root.u.def.section->output_section->vma);
9515 r->r_addend -= off;
9516 }
9517 }
9518 }
721956f4 9519 break;
e86ce104 9520
721956f4 9521 case ppc_stub_plt_branch:
ad8e1ba5 9522 case ppc_stub_plt_branch_r2off:
721956f4
AM
9523 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9524 stub_entry->root.string + 9,
b34976b6 9525 FALSE, FALSE);
721956f4
AM
9526 if (br_entry == NULL)
9527 {
9528 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
5c3dead3 9529 stub_entry->root.string);
b34976b6
AM
9530 htab->stub_error = TRUE;
9531 return FALSE;
721956f4
AM
9532 }
9533
176a0d42
AM
9534 dest = (stub_entry->target_value
9535 + stub_entry->target_section->output_offset
9536 + stub_entry->target_section->output_section->vma);
721956f4 9537
176a0d42 9538 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 9539 htab->brlt->contents + br_entry->offset);
721956f4 9540
f94498ff 9541 if (br_entry->iter == htab->stub_iteration)
721956f4 9542 {
f94498ff 9543 br_entry->iter = 0;
84f5d08e 9544
f94498ff 9545 if (htab->relbrlt != NULL)
84f5d08e 9546 {
f94498ff
AM
9547 /* Create a reloc for the branch lookup table entry. */
9548 Elf_Internal_Rela rela;
9549 bfd_byte *rl;
9550
9551 rela.r_offset = (br_entry->offset
9552 + htab->brlt->output_offset
9553 + htab->brlt->output_section->vma);
9554 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9555 rela.r_addend = dest;
f94498ff
AM
9556
9557 rl = htab->relbrlt->contents;
9558 rl += (htab->relbrlt->reloc_count++
9559 * sizeof (Elf64_External_Rela));
9560 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9561 }
9562 else if (info->emitrelocations)
9563 {
176a0d42
AM
9564 r = get_relocs (htab->brlt, 1);
9565 if (r == NULL)
9566 return FALSE;
9567 /* brlt, being SEC_LINKER_CREATED does not go through the
9568 normal reloc processing. Symbols and offsets are not
9569 translated from input file to output file form, so
9570 set up the offset per the output file. */
f94498ff
AM
9571 r->r_offset = (br_entry->offset
9572 + htab->brlt->output_offset
9573 + htab->brlt->output_section->vma);
9574 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9575 r->r_addend = dest;
84f5d08e 9576 }
84f5d08e 9577 }
721956f4 9578
176a0d42
AM
9579 dest = (br_entry->offset
9580 + htab->brlt->output_offset
9581 + htab->brlt->output_section->vma);
9582
9583 off = (dest
4ce794b7 9584 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 9585 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9586
ad8e1ba5 9587 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7
AM
9588 {
9589 (*_bfd_error_handler)
e86ce104 9590 (_("linkage table error against `%s'"),
721956f4 9591 stub_entry->root.string);
5d1634d7 9592 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9593 htab->stub_error = TRUE;
9594 return FALSE;
5d1634d7 9595 }
41bd81ab 9596
176a0d42
AM
9597 if (info->emitrelocations)
9598 {
9599 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9600 if (r == NULL)
9601 return FALSE;
9602 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9603 if (bfd_big_endian (info->output_bfd))
9604 r[0].r_offset += 2;
176a0d42
AM
9605 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9606 r[0].r_offset += 4;
9607 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9608 r[0].r_addend = dest;
9609 if (PPC_HA (off) != 0)
9610 {
9611 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9612 r[1].r_offset = r[0].r_offset + 4;
9613 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9614 r[1].r_addend = r[0].r_addend;
9615 }
9616 }
9617
ad8e1ba5
AM
9618 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9619 {
176a0d42 9620 if (PPC_HA (off) != 0)
ac2df442
AM
9621 {
9622 size = 16;
176a0d42 9623 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9624 loc += 4;
176a0d42 9625 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9626 }
9627 else
9628 {
9629 size = 12;
176a0d42 9630 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442 9631 }
ad8e1ba5
AM
9632 }
9633 else
9634 {
9635 bfd_vma r2off;
9636
9637 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9638 - htab->stub_group[stub_entry->id_sec->id].toc_off);
97b639ba 9639 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9640 loc += 4;
ac2df442 9641 size = 20;
176a0d42 9642 if (PPC_HA (off) != 0)
ac2df442
AM
9643 {
9644 size += 4;
176a0d42 9645 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9646 loc += 4;
176a0d42 9647 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9648 loc += 4;
9649 }
9650 else
9651 {
176a0d42 9652 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442
AM
9653 loc += 4;
9654 }
9655
9656 if (PPC_HA (r2off) != 0)
9657 {
9658 size += 4;
9659 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9660 loc += 4;
9661 }
97b639ba 9662 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
9663 }
9664 loc += 4;
97b639ba 9665 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 9666 loc += 4;
97b639ba 9667 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 9668 break;
5d1634d7 9669
721956f4 9670 case ppc_stub_plt_call:
e054468f 9671 if (stub_entry->h != NULL
b31867b6
AM
9672 && stub_entry->h->is_func_descriptor
9673 && stub_entry->h->oh != NULL)
c862ae31 9674 {
b31867b6
AM
9675 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9676
9677 /* If the old-ABI "dot-symbol" is undefined make it weak so
9678 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9679 FIXME: We used to define the symbol on one of the call
9680 stubs instead, which is why we test symbol section id
9681 against htab->top_id in various places. Likely all
9682 these checks could now disappear. */
9683 if (fh->elf.root.type == bfd_link_hash_undefined)
9684 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
9685 }
9686
721956f4 9687 /* Now build the stub. */
e054468f 9688 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 9689 if (dest >= (bfd_vma) -2)
721956f4
AM
9690 abort ();
9691
e054468f 9692 plt = htab->plt;
25f23106
AM
9693 if (!htab->elf.dynamic_sections_created
9694 || stub_entry->h == NULL
9695 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9696 plt = htab->iplt;
9697
9698 dest += plt->output_offset + plt->output_section->vma;
9699
9700 if (stub_entry->h == NULL
9701 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9702 {
9703 Elf_Internal_Rela rela;
9704 bfd_byte *rl;
9705
9706 rela.r_offset = dest;
25f23106 9707 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
9708 rela.r_addend = (stub_entry->target_value
9709 + stub_entry->target_section->output_offset
9710 + stub_entry->target_section->output_section->vma);
9711
25f23106
AM
9712 rl = (htab->reliplt->contents
9713 + (htab->reliplt->reloc_count++
9714 * sizeof (Elf64_External_Rela)));
9715 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
9716 stub_entry->plt_ent->plt.offset |= 1;
9717 }
176a0d42
AM
9718
9719 off = (dest
e054468f 9720 - elf_gp (plt->output_section->owner)
176a0d42 9721 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9722
ad8e1ba5 9723 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4
AM
9724 {
9725 (*_bfd_error_handler)
9726 (_("linkage table error against `%s'"),
e054468f
AM
9727 stub_entry->h != NULL
9728 ? stub_entry->h->elf.root.root.string
9729 : "<local sym>");
721956f4 9730 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9731 htab->stub_error = TRUE;
9732 return FALSE;
721956f4
AM
9733 }
9734
176a0d42
AM
9735 r = NULL;
9736 if (info->emitrelocations)
9737 {
9738 r = get_relocs (stub_entry->stub_sec,
9739 (2 + (PPC_HA (off) != 0)
9740 + (PPC_HA (off + 16) == PPC_HA (off))));
9741 if (r == NULL)
9742 return FALSE;
9743 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9744 if (bfd_big_endian (info->output_bfd))
9745 r[0].r_offset += 2;
176a0d42
AM
9746 r[0].r_addend = dest;
9747 }
a7f2871e
AM
9748 if (stub_entry->h != NULL
9749 && (stub_entry->h == htab->tls_get_addr_fd
9750 || stub_entry->h == htab->tls_get_addr)
9751 && !htab->no_tls_get_addr_opt)
9752 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
9753 else
9754 p = build_plt_stub (htab->stub_bfd, loc, off, r);
721956f4
AM
9755 size = p - loc;
9756 break;
9757
9758 default:
9759 BFD_FAIL ();
b34976b6 9760 return FALSE;
721956f4
AM
9761 }
9762
eea6121a 9763 stub_entry->stub_sec->size += size;
97b639ba 9764
ee75fd95 9765 if (htab->emit_stub_syms)
97b639ba
AM
9766 {
9767 struct elf_link_hash_entry *h;
ee75fd95
AM
9768 size_t len1, len2;
9769 char *name;
9770 const char *const stub_str[] = { "long_branch",
9771 "long_branch_r2off",
9772 "plt_branch",
9773 "plt_branch_r2off",
9774 "plt_call" };
9775
9776 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9777 len2 = strlen (stub_entry->root.string);
9778 name = bfd_malloc (len1 + len2 + 2);
9779 if (name == NULL)
9780 return FALSE;
9781 memcpy (name, stub_entry->root.string, 9);
9782 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9783 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9784 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
9785 if (h == NULL)
9786 return FALSE;
9787 if (h->root.type == bfd_link_hash_new)
9788 {
9789 h->root.type = bfd_link_hash_defined;
9790 h->root.u.def.section = stub_entry->stub_sec;
9791 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
9792 h->ref_regular = 1;
9793 h->def_regular = 1;
9794 h->ref_regular_nonweak = 1;
9795 h->forced_local = 1;
9796 h->non_elf = 0;
97b639ba
AM
9797 }
9798 }
9799
b34976b6 9800 return TRUE;
721956f4
AM
9801}
9802
9803/* As above, but don't actually build the stub. Just bump offset so
9804 we know stub section sizes, and select plt_branch stubs where
9805 long_branch stubs won't do. */
9806
b34976b6 9807static bfd_boolean
4ce794b7 9808ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
9809{
9810 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 9811 struct bfd_link_info *info;
721956f4
AM
9812 struct ppc_link_hash_table *htab;
9813 bfd_vma off;
9814 int size;
9815
9816 /* Massage our args to the form they really have. */
9817 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
9818 info = in_arg;
9819
9820 htab = ppc_hash_table (info);
4dfe6ac6
NC
9821 if (htab == NULL)
9822 return FALSE;
721956f4
AM
9823
9824 if (stub_entry->stub_type == ppc_stub_plt_call)
9825 {
e054468f
AM
9826 asection *plt;
9827 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 9828 if (off >= (bfd_vma) -2)
411e1bfb 9829 abort ();
e054468f 9830 plt = htab->plt;
25f23106
AM
9831 if (!htab->elf.dynamic_sections_created
9832 || stub_entry->h == NULL
9833 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9834 plt = htab->iplt;
9835 off += (plt->output_offset
9836 + plt->output_section->vma
9837 - elf_gp (plt->output_section->owner)
ad8e1ba5 9838 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9839
ad8e1ba5 9840 size = PLT_CALL_STUB_SIZE;
ac2df442
AM
9841 if (PPC_HA (off) == 0)
9842 size -= 4;
4ce794b7 9843 if (PPC_HA (off + 16) != PPC_HA (off))
721956f4 9844 size += 4;
a7f2871e
AM
9845 if (stub_entry->h != NULL
9846 && (stub_entry->h == htab->tls_get_addr_fd
9847 || stub_entry->h == htab->tls_get_addr)
9848 && !htab->no_tls_get_addr_opt)
9849 size += 13 * 4;
176a0d42
AM
9850 if (info->emitrelocations)
9851 {
9852 stub_entry->stub_sec->reloc_count
9853 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9854 stub_entry->stub_sec->flags |= SEC_RELOC;
9855 }
721956f4
AM
9856 }
9857 else
9858 {
ad8e1ba5
AM
9859 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9860 variants. */
ac2df442
AM
9861 bfd_vma r2off = 0;
9862
721956f4
AM
9863 off = (stub_entry->target_value
9864 + stub_entry->target_section->output_offset
9865 + stub_entry->target_section->output_section->vma);
eea6121a 9866 off -= (stub_entry->stub_sec->size
721956f4
AM
9867 + stub_entry->stub_sec->output_offset
9868 + stub_entry->stub_sec->output_section->vma);
9869
ad8e1ba5
AM
9870 /* Reset the stub type from the plt variant in case we now
9871 can reach with a shorter stub. */
9872 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9873 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9874
9875 size = 4;
9876 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9877 {
ac2df442
AM
9878 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
9879 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9880 size = 12;
9881 if (PPC_HA (r2off) != 0)
9882 size = 16;
9883 off -= size - 4;
ad8e1ba5
AM
9884 }
9885
9886 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
9887 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9888 {
9889 struct ppc_branch_hash_entry *br_entry;
9890
9891 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9892 stub_entry->root.string + 9,
b34976b6 9893 TRUE, FALSE);
721956f4
AM
9894 if (br_entry == NULL)
9895 {
9896 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
5c3dead3 9897 stub_entry->root.string);
b34976b6
AM
9898 htab->stub_error = TRUE;
9899 return FALSE;
721956f4
AM
9900 }
9901
9902 if (br_entry->iter != htab->stub_iteration)
9903 {
9904 br_entry->iter = htab->stub_iteration;
eea6121a
AM
9905 br_entry->offset = htab->brlt->size;
9906 htab->brlt->size += 8;
63bc6f6c 9907
ee75fd95 9908 if (htab->relbrlt != NULL)
eea6121a 9909 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
9910 else if (info->emitrelocations)
9911 {
9912 htab->brlt->reloc_count += 1;
9913 htab->brlt->flags |= SEC_RELOC;
9914 }
721956f4 9915 }
ad8e1ba5
AM
9916
9917 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
9918 off = (br_entry->offset
9919 + htab->brlt->output_offset
9920 + htab->brlt->output_section->vma
9921 - elf_gp (htab->brlt->output_section->owner)
9922 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9923
176a0d42
AM
9924 if (info->emitrelocations)
9925 {
9926 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
9927 stub_entry->stub_sec->flags |= SEC_RELOC;
9928 }
9929
ac2df442
AM
9930 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9931 {
9932 size = 12;
176a0d42 9933 if (PPC_HA (off) != 0)
ac2df442
AM
9934 size = 16;
9935 }
9936 else
9937 {
9938 size = 20;
176a0d42 9939 if (PPC_HA (off) != 0)
ac2df442
AM
9940 size += 4;
9941
9942 if (PPC_HA (r2off) != 0)
9943 size += 4;
9944 }
721956f4 9945 }
84f5d08e
AM
9946 else if (info->emitrelocations)
9947 {
9948 stub_entry->stub_sec->reloc_count += 1;
9949 stub_entry->stub_sec->flags |= SEC_RELOC;
9950 }
721956f4
AM
9951 }
9952
eea6121a 9953 stub_entry->stub_sec->size += size;
b34976b6 9954 return TRUE;
721956f4
AM
9955}
9956
9957/* Set up various things so that we can make a list of input sections
9958 for each output section included in the link. Returns -1 on error,
cedb70c5 9959 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
9960
9961int
927be08e
AM
9962ppc64_elf_setup_section_lists
9963 (struct bfd_link_info *info,
9964 asection *(*add_stub_section) (const char *, asection *),
9965 void (*layout_sections_again) (void))
721956f4
AM
9966{
9967 bfd *input_bfd;
734b6cf9 9968 int top_id, top_index, id;
721956f4 9969 asection *section;
734b6cf9 9970 asection **input_list;
721956f4
AM
9971 bfd_size_type amt;
9972 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9973
4dfe6ac6
NC
9974 if (htab == NULL)
9975 return -1;
927be08e
AM
9976 /* Stash our params away. */
9977 htab->add_stub_section = add_stub_section;
9978 htab->layout_sections_again = layout_sections_again;
4c52953f 9979
4ce794b7 9980 if (htab->brlt == NULL)
721956f4
AM
9981 return 0;
9982
1e2f5b6e 9983 /* Find the top input section id. */
3d6f9012 9984 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
9985 input_bfd != NULL;
9986 input_bfd = input_bfd->link_next)
9987 {
721956f4
AM
9988 for (section = input_bfd->sections;
9989 section != NULL;
9990 section = section->next)
9991 {
9992 if (top_id < section->id)
9993 top_id = section->id;
9994 }
9995 }
721956f4 9996
8f3bab57 9997 htab->top_id = top_id;
721956f4 9998 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 9999 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10000 if (htab->stub_group == NULL)
10001 return -1;
10002
3d6f9012
AM
10003 /* Set toc_off for com, und, abs and ind sections. */
10004 for (id = 0; id < 3; id++)
10005 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10006
734b6cf9
AM
10007 /* We can't use output_bfd->section_count here to find the top output
10008 section index as some sections may have been removed, and
8423293d 10009 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10010 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10011 section != NULL;
10012 section = section->next)
10013 {
10014 if (top_index < section->index)
10015 top_index = section->index;
10016 }
10017
10018 htab->top_index = top_index;
10019 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10020 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10021 htab->input_list = input_list;
10022 if (input_list == NULL)
10023 return -1;
10024
721956f4
AM
10025 return 1;
10026}
10027
927be08e
AM
10028/* Set up for first pass at multitoc partitioning. */
10029
10030void
10031ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10032{
10033 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10034
10035 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10036 htab->toc_curr = elf_gp (info->output_bfd);
10037 htab->toc_bfd = NULL;
10038 htab->toc_first_sec = NULL;
10039}
10040
e717da7e
AM
10041/* The linker repeatedly calls this function for each TOC input section
10042 and linker generated GOT section. Group input bfds such that the toc
927be08e 10043 within a group is less than 64k in size. */
ad8e1ba5 10044
927be08e 10045bfd_boolean
4ce794b7 10046ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10047{
10048 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10049 bfd_vma addr, off, limit;
ad8e1ba5 10050
4dfe6ac6
NC
10051 if (htab == NULL)
10052 return FALSE;
10053
927be08e 10054 if (!htab->second_toc_pass)
4c52953f 10055 {
927be08e 10056 /* Keep track of the first .toc or .got section for this input bfd. */
bf102f86
AM
10057 if (htab->toc_bfd != isec->owner)
10058 {
10059 htab->toc_bfd = isec->owner;
10060 htab->toc_first_sec = isec;
10061 }
927be08e 10062
bf102f86
AM
10063 addr = isec->output_offset + isec->output_section->vma;
10064 off = addr - htab->toc_curr;
d77c8a4b
AM
10065 limit = 0x80008000;
10066 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10067 limit = 0x10000;
10068 if (off + isec->size > limit)
bf102f86
AM
10069 {
10070 addr = (htab->toc_first_sec->output_offset
10071 + htab->toc_first_sec->output_section->vma);
10072 htab->toc_curr = addr;
10073 }
99877b66 10074
927be08e
AM
10075 /* toc_curr is the base address of this toc group. Set elf_gp
10076 for the input section to be the offset relative to the
10077 output toc base plus 0x8000. Making the input elf_gp an
10078 offset allows us to move the toc as a whole without
10079 recalculating input elf_gp. */
10080 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10081 off += TOC_BASE_OFF;
10082
10083 /* Die if someone uses a linker script that doesn't keep input
10084 file .toc and .got together. */
10085 if (elf_gp (isec->owner) != 0
10086 && elf_gp (isec->owner) != off)
10087 return FALSE;
10088
10089 elf_gp (isec->owner) = off;
10090 return TRUE;
4c52953f 10091 }
927be08e
AM
10092
10093 /* During the second pass toc_first_sec points to the start of
10094 a toc group, and toc_curr is used to track the old elf_gp.
10095 We use toc_bfd to ensure we only look at each bfd once. */
10096 if (htab->toc_bfd == isec->owner)
10097 return TRUE;
10098 htab->toc_bfd = isec->owner;
10099
10100 if (htab->toc_first_sec == NULL
10101 || htab->toc_curr != elf_gp (isec->owner))
10102 {
10103 htab->toc_curr = elf_gp (isec->owner);
10104 htab->toc_first_sec = isec;
10105 }
10106 addr = (htab->toc_first_sec->output_offset
10107 + htab->toc_first_sec->output_section->vma);
10108 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10109 elf_gp (isec->owner) = off;
10110
10111 return TRUE;
ad8e1ba5
AM
10112}
10113
927be08e
AM
10114/* Called via elf_link_hash_traverse to merge GOT entries for global
10115 symbol H. */
10116
10117static bfd_boolean
10118merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10119{
10120 if (h->root.type == bfd_link_hash_indirect)
10121 return TRUE;
10122
10123 if (h->root.type == bfd_link_hash_warning)
10124 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10125
10126 merge_got_entries (&h->got.glist);
10127
10128 return TRUE;
10129}
10130
10131/* Called via elf_link_hash_traverse to allocate GOT entries for global
10132 symbol H. */
10133
10134static bfd_boolean
10135reallocate_got (struct elf_link_hash_entry *h, void *inf)
10136{
10137 struct got_entry *gent;
10138
10139 if (h->root.type == bfd_link_hash_indirect)
10140 return TRUE;
10141
10142 if (h->root.type == bfd_link_hash_warning)
10143 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10144
10145 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10146 if (!gent->is_indirect)
10147 allocate_got (h, (struct bfd_link_info *) inf, gent);
10148 return TRUE;
10149}
10150
10151/* Called on the first multitoc pass after the last call to
10152 ppc64_elf_next_toc_section. This function removes duplicate GOT
10153 entries. */
10154
10155bfd_boolean
10156ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10157{
10158 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10159 struct bfd *ibfd, *ibfd2;
10160 bfd_boolean done_something;
10161
10162 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10163
7865406b
AM
10164 if (!htab->do_multi_toc)
10165 return FALSE;
10166
d0fae19d 10167 /* Merge global sym got entries within a toc group. */
927be08e
AM
10168 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10169
10170 /* And tlsld_got. */
10171 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10172 {
10173 struct got_entry *ent, *ent2;
10174
10175 if (!is_ppc64_elf (ibfd))
10176 continue;
10177
10178 ent = ppc64_tlsld_got (ibfd);
10179 if (!ent->is_indirect
10180 && ent->got.offset != (bfd_vma) -1)
10181 {
10182 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10183 {
10184 if (!is_ppc64_elf (ibfd2))
10185 continue;
10186
10187 ent2 = ppc64_tlsld_got (ibfd2);
10188 if (!ent2->is_indirect
10189 && ent2->got.offset != (bfd_vma) -1
10190 && elf_gp (ibfd2) == elf_gp (ibfd))
10191 {
10192 ent2->is_indirect = TRUE;
10193 ent2->got.ent = ent;
10194 }
10195 }
10196 }
10197 }
10198
10199 /* Zap sizes of got sections. */
10200 htab->reliplt->rawsize = htab->reliplt->size;
10201 htab->reliplt->size -= htab->got_reli_size;
10202 htab->got_reli_size = 0;
10203
10204 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10205 {
10206 asection *got, *relgot;
10207
10208 if (!is_ppc64_elf (ibfd))
10209 continue;
10210
10211 got = ppc64_elf_tdata (ibfd)->got;
10212 if (got != NULL)
10213 {
10214 got->rawsize = got->size;
10215 got->size = 0;
10216 relgot = ppc64_elf_tdata (ibfd)->relgot;
10217 relgot->rawsize = relgot->size;
10218 relgot->size = 0;
10219 }
10220 }
10221
10222 /* Now reallocate the got, local syms first. We don't need to
10223 allocate section contents again since we never increase size. */
10224 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10225 {
10226 struct got_entry **lgot_ents;
10227 struct got_entry **end_lgot_ents;
10228 struct plt_entry **local_plt;
10229 struct plt_entry **end_local_plt;
f961d9dd 10230 unsigned char *lgot_masks;
927be08e
AM
10231 bfd_size_type locsymcount;
10232 Elf_Internal_Shdr *symtab_hdr;
10233 asection *s, *srel;
10234
10235 if (!is_ppc64_elf (ibfd))
10236 continue;
10237
10238 lgot_ents = elf_local_got_ents (ibfd);
10239 if (!lgot_ents)
10240 continue;
10241
10242 symtab_hdr = &elf_symtab_hdr (ibfd);
10243 locsymcount = symtab_hdr->sh_info;
10244 end_lgot_ents = lgot_ents + locsymcount;
10245 local_plt = (struct plt_entry **) end_lgot_ents;
10246 end_local_plt = local_plt + locsymcount;
f961d9dd 10247 lgot_masks = (unsigned char *) end_local_plt;
927be08e
AM
10248 s = ppc64_elf_tdata (ibfd)->got;
10249 srel = ppc64_elf_tdata (ibfd)->relgot;
10250 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10251 {
10252 struct got_entry *ent;
10253
10254 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d
AM
10255 {
10256 unsigned int num = 1;
10257 ent->got.offset = s->size;
10258 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10259 num = 2;
10260 s->size += num * 8;
10261 if (info->shared)
10262 srel->size += num * sizeof (Elf64_External_Rela);
10263 else if ((*lgot_masks & PLT_IFUNC) != 0)
10264 {
10265 htab->reliplt->size
10266 += num * sizeof (Elf64_External_Rela);
10267 htab->got_reli_size
10268 += num * sizeof (Elf64_External_Rela);
10269 }
10270 }
927be08e
AM
10271 }
10272 }
10273
10274 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10275
10276 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10277 {
10278 struct got_entry *ent;
10279
10280 if (!is_ppc64_elf (ibfd))
10281 continue;
10282
10283 ent = ppc64_tlsld_got (ibfd);
10284 if (!ent->is_indirect
10285 && ent->got.offset != (bfd_vma) -1)
10286 {
10287 asection *s = ppc64_elf_tdata (ibfd)->got;
10288 ent->got.offset = s->size;
10289 s->size += 16;
10290 if (info->shared)
10291 {
10292 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10293 srel->size += sizeof (Elf64_External_Rela);
10294 }
10295 }
10296 }
10297
10298 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10299 if (!done_something)
10300 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10301 {
10302 asection *got;
10303
10304 if (!is_ppc64_elf (ibfd))
10305 continue;
10306
10307 got = ppc64_elf_tdata (ibfd)->got;
10308 if (got != NULL)
10309 {
10310 done_something = got->rawsize != got->size;
10311 if (done_something)
10312 break;
10313 }
10314 }
10315
10316 if (done_something)
10317 (*htab->layout_sections_again) ();
10318
10319 /* Set up for second pass over toc sections to recalculate elf_gp
10320 on input sections. */
10321 htab->toc_bfd = NULL;
10322 htab->toc_first_sec = NULL;
10323 htab->second_toc_pass = TRUE;
10324 return done_something;
10325}
10326
10327/* Called after second pass of multitoc partitioning. */
10328
10329void
10330ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10331{
10332 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10333
10334 /* After the second pass, toc_curr tracks the TOC offset used
10335 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 10336 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
10337}
10338
9b5ecbd0
AM
10339/* No toc references were found in ISEC. If the code in ISEC makes no
10340 calls, then there's no need to use toc adjusting stubs when branching
10341 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
10342 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10343 needed, and 2 if a cyclical call-graph was found but no other reason
10344 for a stub was detected. If called from the top level, a return of
10345 2 means the same as a return of 0. */
9b5ecbd0
AM
10346
10347static int
4ce794b7 10348toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 10349{
9b5ecbd0 10350 int ret;
70cc837d
AM
10351
10352 /* Mark this section as checked. */
10353 isec->call_check_done = 1;
9b5ecbd0 10354
772119ce
AM
10355 /* We know none of our code bearing sections will need toc stubs. */
10356 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10357 return 0;
10358
eea6121a 10359 if (isec->size == 0)
082c50f8
AM
10360 return 0;
10361
4c52953f
AM
10362 if (isec->output_section == NULL)
10363 return 0;
10364
4c52953f 10365 ret = 0;
70cc837d 10366 if (isec->reloc_count != 0)
9b5ecbd0 10367 {
70cc837d
AM
10368 Elf_Internal_Rela *relstart, *rel;
10369 Elf_Internal_Sym *local_syms;
10370 struct ppc_link_hash_table *htab;
2917689a 10371
70cc837d
AM
10372 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10373 info->keep_memory);
10374 if (relstart == NULL)
10375 return -1;
90aecf7a 10376
70cc837d
AM
10377 /* Look for branches to outside of this section. */
10378 local_syms = NULL;
10379 htab = ppc_hash_table (info);
10380 if (htab == NULL)
10381 return -1;
4c52953f 10382
70cc837d 10383 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 10384 {
70cc837d
AM
10385 enum elf_ppc64_reloc_type r_type;
10386 unsigned long r_symndx;
10387 struct elf_link_hash_entry *h;
10388 struct ppc_link_hash_entry *eh;
10389 Elf_Internal_Sym *sym;
10390 asection *sym_sec;
10391 struct _opd_sec_data *opd;
10392 bfd_vma sym_value;
10393 bfd_vma dest;
10394
10395 r_type = ELF64_R_TYPE (rel->r_info);
10396 if (r_type != R_PPC64_REL24
10397 && r_type != R_PPC64_REL14
10398 && r_type != R_PPC64_REL14_BRTAKEN
10399 && r_type != R_PPC64_REL14_BRNTAKEN)
10400 continue;
4c52953f 10401
70cc837d
AM
10402 r_symndx = ELF64_R_SYM (rel->r_info);
10403 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10404 isec->owner))
4c52953f 10405 {
70cc837d
AM
10406 ret = -1;
10407 break;
10408 }
4c52953f 10409
70cc837d
AM
10410 /* Calls to dynamic lib functions go through a plt call stub
10411 that uses r2. */
10412 eh = (struct ppc_link_hash_entry *) h;
10413 if (eh != NULL
10414 && (eh->elf.plt.plist != NULL
10415 || (eh->oh != NULL
10416 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10417 {
10418 ret = 1;
10419 break;
4c52953f
AM
10420 }
10421
70cc837d
AM
10422 if (sym_sec == NULL)
10423 /* Ignore other undefined symbols. */
4c52953f 10424 continue;
4c52953f 10425
70cc837d
AM
10426 /* Assume branches to other sections not included in the
10427 link need stubs too, to cover -R and absolute syms. */
10428 if (sym_sec->output_section == NULL)
10429 {
10430 ret = 1;
10431 break;
10432 }
4c52953f 10433
70cc837d
AM
10434 if (h == NULL)
10435 sym_value = sym->st_value;
10436 else
10437 {
10438 if (h->root.type != bfd_link_hash_defined
10439 && h->root.type != bfd_link_hash_defweak)
10440 abort ();
10441 sym_value = h->root.u.def.value;
10442 }
10443 sym_value += rel->r_addend;
4c52953f 10444
70cc837d
AM
10445 /* If this branch reloc uses an opd sym, find the code section. */
10446 opd = get_opd_info (sym_sec);
10447 if (opd != NULL)
10448 {
10449 if (h == NULL && opd->adjust != NULL)
10450 {
10451 long adjust;
4c52953f 10452
70cc837d
AM
10453 adjust = opd->adjust[sym->st_value / 8];
10454 if (adjust == -1)
10455 /* Assume deleted functions won't ever be called. */
10456 continue;
10457 sym_value += adjust;
10458 }
4c52953f 10459
70cc837d
AM
10460 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10461 if (dest == (bfd_vma) -1)
10462 continue;
10463 }
10464 else
10465 dest = (sym_value
10466 + sym_sec->output_offset
10467 + sym_sec->output_section->vma);
4c52953f 10468
70cc837d
AM
10469 /* Ignore branch to self. */
10470 if (sym_sec == isec)
10471 continue;
4c52953f 10472
70cc837d
AM
10473 /* If the called function uses the toc, we need a stub. */
10474 if (sym_sec->has_toc_reloc
10475 || sym_sec->makes_toc_func_call)
4c52953f 10476 {
70cc837d 10477 ret = 1;
4c52953f
AM
10478 break;
10479 }
70cc837d
AM
10480
10481 /* Assume any branch that needs a long branch stub might in fact
10482 need a plt_branch stub. A plt_branch stub uses r2. */
10483 else if (dest - (isec->output_offset
10484 + isec->output_section->vma
10485 + rel->r_offset) + (1 << 25) >= (2 << 25))
4c52953f 10486 {
70cc837d
AM
10487 ret = 1;
10488 break;
10489 }
10490
10491 /* If calling back to a section in the process of being
10492 tested, we can't say for sure that no toc adjusting stubs
10493 are needed, so don't return zero. */
10494 else if (sym_sec->call_check_in_progress)
10495 ret = 2;
10496
10497 /* Branches to another section that itself doesn't have any TOC
10498 references are OK. Recursively call ourselves to check. */
10499 else if (!sym_sec->call_check_done)
10500 {
10501 int recur;
10502
10503 /* Mark current section as indeterminate, so that other
10504 sections that call back to current won't be marked as
10505 known. */
10506 isec->call_check_in_progress = 1;
10507 recur = toc_adjusting_stub_needed (info, sym_sec);
10508 isec->call_check_in_progress = 0;
10509
4c52953f
AM
10510 if (recur != 0)
10511 {
70cc837d
AM
10512 ret = recur;
10513 if (recur != 2)
10514 break;
4c52953f
AM
10515 }
10516 }
4c52953f 10517 }
70cc837d
AM
10518
10519 if (local_syms != NULL
10520 && (elf_symtab_hdr (isec->owner).contents
10521 != (unsigned char *) local_syms))
10522 free (local_syms);
10523 if (elf_section_data (isec)->relocs != relstart)
10524 free (relstart);
9b5ecbd0
AM
10525 }
10526
70cc837d
AM
10527 if ((ret & 1) == 0
10528 && isec->map_head.s != NULL
10529 && (strcmp (isec->output_section->name, ".init") == 0
10530 || strcmp (isec->output_section->name, ".fini") == 0))
10531 {
10532 if (isec->map_head.s->has_toc_reloc
10533 || isec->map_head.s->makes_toc_func_call)
10534 ret = 1;
10535 else if (!isec->map_head.s->call_check_done)
10536 {
10537 int recur;
10538 isec->call_check_in_progress = 1;
10539 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10540 isec->call_check_in_progress = 0;
10541 if (recur != 0)
10542 ret = recur;
10543 }
10544 }
10545
10546 if (ret == 1)
10547 isec->makes_toc_func_call = 1;
4c52953f 10548
9b5ecbd0
AM
10549 return ret;
10550}
10551
721956f4
AM
10552/* The linker repeatedly calls this function for each input section,
10553 in the order that input sections are linked into output sections.
10554 Build lists of input sections to determine groupings between which
10555 we may insert linker stubs. */
10556
9b5ecbd0 10557bfd_boolean
4ce794b7 10558ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
10559{
10560 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10561
4dfe6ac6
NC
10562 if (htab == NULL)
10563 return FALSE;
10564
734b6cf9
AM
10565 if ((isec->output_section->flags & SEC_CODE) != 0
10566 && isec->output_section->index <= htab->top_index)
721956f4 10567 {
734b6cf9 10568 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 10569 /* Steal the link_sec pointer for our list. */
721956f4 10570#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
10571 /* This happens to make the list in reverse order,
10572 which is what we want. */
734b6cf9
AM
10573 PREV_SEC (isec) = *list;
10574 *list = isec;
721956f4 10575 }
ad8e1ba5 10576
4c52953f 10577 if (htab->multi_toc_needed)
9b5ecbd0 10578 {
4c52953f
AM
10579 /* If a code section has a function that uses the TOC then we need
10580 to use the right TOC (obviously). Also, make sure that .opd gets
10581 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
10582 can't find their function symbol (shouldn't ever happen now).
10583 Also specially treat .fixup for the linux kernel. .fixup
10584 contains branches, but only back to the function that hit an
10585 exception. */
10586 if (isec->has_toc_reloc
10587 || (isec->flags & SEC_CODE) == 0
10588 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
10589 {
10590 if (elf_gp (isec->owner) != 0)
10591 htab->toc_curr = elf_gp (isec->owner);
10592 }
6683a28d
AM
10593 else
10594 {
10595 if (!isec->call_check_done
10596 && toc_adjusting_stub_needed (info, isec) < 0)
10597 return FALSE;
10598 /* If we make a local call from this section, ie. a branch
10599 without a following nop, then we have no place to put a
10600 toc restoring insn. We must use the same toc group as
10601 the callee.
10602 Testing makes_toc_func_call actually tests for *any*
10603 calls to functions that need a good toc pointer. A more
10604 precise test would be better, as this one will set
10605 incorrect values for pasted .init/.fini fragments.
10606 (Fixed later in check_pasted_section.) */
10607 if (isec->makes_toc_func_call
10608 && elf_gp (isec->owner) != 0)
10609 htab->toc_curr = elf_gp (isec->owner);
10610 }
9b5ecbd0 10611 }
ad8e1ba5
AM
10612
10613 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 10614 Use the last TOC base. */
ad8e1ba5 10615 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 10616 return TRUE;
721956f4
AM
10617}
10618
70cc837d
AM
10619/* Check that all .init and .fini sections use the same toc, if they
10620 have toc relocs. */
10621
10622static bfd_boolean
10623check_pasted_section (struct bfd_link_info *info, const char *name)
10624{
10625 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10626
10627 if (o != NULL)
10628 {
10629 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10630 bfd_vma toc_off = 0;
10631 asection *i;
10632
10633 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10634 if (i->has_toc_reloc)
10635 {
10636 if (toc_off == 0)
10637 toc_off = htab->stub_group[i->id].toc_off;
10638 else if (toc_off != htab->stub_group[i->id].toc_off)
10639 return FALSE;
10640 }
6683a28d
AM
10641
10642 if (toc_off == 0)
10643 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10644 if (i->makes_toc_func_call)
10645 {
10646 toc_off = htab->stub_group[i->id].toc_off;
10647 break;
10648 }
10649
70cc837d
AM
10650 /* Make sure the whole pasted function uses the same toc offset. */
10651 if (toc_off != 0)
10652 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10653 htab->stub_group[i->id].toc_off = toc_off;
10654 }
10655 return TRUE;
10656}
10657
10658bfd_boolean
10659ppc64_elf_check_init_fini (struct bfd_link_info *info)
10660{
10661 return (check_pasted_section (info, ".init")
10662 & check_pasted_section (info, ".fini"));
10663}
10664
721956f4
AM
10665/* See whether we can group stub sections together. Grouping stub
10666 sections may result in fewer stubs. More importantly, we need to
10667 put all .init* and .fini* stubs at the beginning of the .init or
10668 .fini output sections respectively, because glibc splits the
10669 _init and _fini functions into multiple parts. Putting a stub in
10670 the middle of a function is not a good idea. */
10671
10672static void
4ce794b7
AM
10673group_sections (struct ppc_link_hash_table *htab,
10674 bfd_size_type stub_group_size,
10675 bfd_boolean stubs_always_before_branch)
721956f4 10676{
7c8fe5c4
AM
10677 asection **list;
10678 bfd_size_type stub14_group_size;
10679 bfd_boolean suppress_size_errors;
10680
10681 suppress_size_errors = FALSE;
10682 stub14_group_size = stub_group_size;
10683 if (stub_group_size == 1)
10684 {
10685 /* Default values. */
10686 if (stubs_always_before_branch)
10687 {
10688 stub_group_size = 0x1e00000;
10689 stub14_group_size = 0x7800;
10690 }
10691 else
10692 {
10693 stub_group_size = 0x1c00000;
10694 stub14_group_size = 0x7000;
10695 }
10696 suppress_size_errors = TRUE;
10697 }
10698
10699 list = htab->input_list + htab->top_index;
734b6cf9 10700 do
721956f4 10701 {
734b6cf9
AM
10702 asection *tail = *list;
10703 while (tail != NULL)
721956f4 10704 {
734b6cf9
AM
10705 asection *curr;
10706 asection *prev;
10707 bfd_size_type total;
10708 bfd_boolean big_sec;
10709 bfd_vma curr_toc;
10710
10711 curr = tail;
eea6121a 10712 total = tail->size;
6bee8834
AM
10713 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10714 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
10715 ? stub14_group_size : stub_group_size);
10716 if (big_sec && !suppress_size_errors)
5c3dead3
AM
10717 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10718 tail->owner, tail);
734b6cf9
AM
10719 curr_toc = htab->stub_group[tail->id].toc_off;
10720
10721 while ((prev = PREV_SEC (curr)) != NULL
10722 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
10723 < (ppc64_elf_section_data (prev) != NULL
10724 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10725 ? stub14_group_size : stub_group_size))
ad8e1ba5 10726 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
10727 curr = prev;
10728
10729 /* OK, the size from the start of CURR to the end is less
10730 than stub_group_size and thus can be handled by one stub
10731 section. (or the tail section is itself larger than
10732 stub_group_size, in which case we may be toast.) We
10733 should really be keeping track of the total size of stubs
10734 added here, as stubs contribute to the final output
10735 section size. That's a little tricky, and this way will
10736 only break if stubs added make the total size more than
10737 2^25, ie. for the default stub_group_size, if stubs total
10738 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10739 do
721956f4
AM
10740 {
10741 prev = PREV_SEC (tail);
734b6cf9 10742 /* Set up this stub group. */
721956f4
AM
10743 htab->stub_group[tail->id].link_sec = curr;
10744 }
734b6cf9
AM
10745 while (tail != curr && (tail = prev) != NULL);
10746
10747 /* But wait, there's more! Input sections up to stub_group_size
10748 bytes before the stub section can be handled by it too.
10749 Don't do this if we have a really large section after the
10750 stubs, as adding more stubs increases the chance that
10751 branches may not reach into the stub section. */
10752 if (!stubs_always_before_branch && !big_sec)
10753 {
10754 total = 0;
10755 while (prev != NULL
10756 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
10757 < (ppc64_elf_section_data (prev) != NULL
10758 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10759 ? stub14_group_size : stub_group_size))
734b6cf9
AM
10760 && htab->stub_group[prev->id].toc_off == curr_toc)
10761 {
10762 tail = prev;
10763 prev = PREV_SEC (tail);
10764 htab->stub_group[tail->id].link_sec = curr;
10765 }
10766 }
10767 tail = prev;
721956f4
AM
10768 }
10769 }
734b6cf9
AM
10770 while (list-- != htab->input_list);
10771 free (htab->input_list);
721956f4
AM
10772#undef PREV_SEC
10773}
10774
721956f4
AM
10775/* Determine and set the size of the stub section for a final link.
10776
10777 The basic idea here is to examine all the relocations looking for
10778 PC-relative calls to a target that is unreachable with a "bl"
10779 instruction. */
10780
b34976b6 10781bfd_boolean
927be08e 10782ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
721956f4
AM
10783{
10784 bfd_size_type stub_group_size;
b34976b6 10785 bfd_boolean stubs_always_before_branch;
721956f4
AM
10786 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10787
4dfe6ac6
NC
10788 if (htab == NULL)
10789 return FALSE;
10790
721956f4
AM
10791 stubs_always_before_branch = group_size < 0;
10792 if (group_size < 0)
10793 stub_group_size = -group_size;
10794 else
10795 stub_group_size = group_size;
721956f4
AM
10796
10797 group_sections (htab, stub_group_size, stubs_always_before_branch);
10798
721956f4
AM
10799 while (1)
10800 {
10801 bfd *input_bfd;
10802 unsigned int bfd_indx;
10803 asection *stub_sec;
721956f4
AM
10804
10805 htab->stub_iteration += 1;
721956f4
AM
10806
10807 for (input_bfd = info->input_bfds, bfd_indx = 0;
10808 input_bfd != NULL;
10809 input_bfd = input_bfd->link_next, bfd_indx++)
10810 {
10811 Elf_Internal_Shdr *symtab_hdr;
10812 asection *section;
6cdc0ccc 10813 Elf_Internal_Sym *local_syms = NULL;
721956f4 10814
0c8d6e5c 10815 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
10816 continue;
10817
721956f4 10818 /* We'll need the symbol table in a second. */
0ffa91dd 10819 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
10820 if (symtab_hdr->sh_info == 0)
10821 continue;
10822
721956f4
AM
10823 /* Walk over each section attached to the input bfd. */
10824 for (section = input_bfd->sections;
10825 section != NULL;
10826 section = section->next)
10827 {
721956f4 10828 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
10829
10830 /* If there aren't any relocs, then there's nothing more
10831 to do. */
10832 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
10833 || (section->flags & SEC_ALLOC) == 0
10834 || (section->flags & SEC_LOAD) == 0
10835 || (section->flags & SEC_CODE) == 0
721956f4
AM
10836 || section->reloc_count == 0)
10837 continue;
10838
10839 /* If this section is a link-once section that will be
10840 discarded, then don't create any stubs. */
10841 if (section->output_section == NULL
927be08e 10842 || section->output_section->owner != info->output_bfd)
721956f4
AM
10843 continue;
10844
1e2f5b6e
AM
10845 /* Get the relocs. */
10846 internal_relocs
4ce794b7 10847 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 10848 info->keep_memory);
721956f4 10849 if (internal_relocs == NULL)
1e2f5b6e 10850 goto error_ret_free_local;
721956f4
AM
10851
10852 /* Now examine each relocation. */
10853 irela = internal_relocs;
10854 irelaend = irela + section->reloc_count;
10855 for (; irela < irelaend; irela++)
10856 {
4ce794b7
AM
10857 enum elf_ppc64_reloc_type r_type;
10858 unsigned int r_indx;
721956f4
AM
10859 enum ppc_stub_type stub_type;
10860 struct ppc_stub_hash_entry *stub_entry;
8387904d 10861 asection *sym_sec, *code_sec;
e054468f 10862 bfd_vma sym_value, code_value;
721956f4 10863 bfd_vma destination;
8843416a 10864 bfd_boolean ok_dest;
721956f4 10865 struct ppc_link_hash_entry *hash;
8387904d 10866 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
10867 struct elf_link_hash_entry *h;
10868 Elf_Internal_Sym *sym;
721956f4
AM
10869 char *stub_name;
10870 const asection *id_sec;
74f0fb50 10871 struct _opd_sec_data *opd;
e054468f 10872 struct plt_entry *plt_ent;
721956f4
AM
10873
10874 r_type = ELF64_R_TYPE (irela->r_info);
10875 r_indx = ELF64_R_SYM (irela->r_info);
10876
4ce794b7 10877 if (r_type >= R_PPC64_max)
721956f4
AM
10878 {
10879 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 10880 goto error_ret_free_internal;
721956f4
AM
10881 }
10882
10883 /* Only look for stubs on branch instructions. */
4ce794b7
AM
10884 if (r_type != R_PPC64_REL24
10885 && r_type != R_PPC64_REL14
10886 && r_type != R_PPC64_REL14_BRTAKEN
10887 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
10888 continue;
10889
10890 /* Now determine the call target, its name, value,
10891 section. */
411e1bfb
AM
10892 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10893 r_indx, input_bfd))
10894 goto error_ret_free_internal;
10895 hash = (struct ppc_link_hash_entry *) h;
10896
8843416a 10897 ok_dest = FALSE;
8387904d 10898 fdh = NULL;
7fe2b9a6 10899 sym_value = 0;
411e1bfb 10900 if (hash == NULL)
721956f4 10901 {
411e1bfb 10902 sym_value = sym->st_value;
8843416a 10903 ok_dest = TRUE;
721956f4 10904 }
7fe2b9a6
AM
10905 else if (hash->elf.root.type == bfd_link_hash_defined
10906 || hash->elf.root.type == bfd_link_hash_defweak)
10907 {
10908 sym_value = hash->elf.root.u.def.value;
10909 if (sym_sec->output_section != NULL)
10910 ok_dest = TRUE;
10911 }
10912 else if (hash->elf.root.type == bfd_link_hash_undefweak
10913 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 10914 {
99877b66 10915 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
10916 use the func descriptor sym instead if it is
10917 defined. */
ceb1f1ef 10918 if (hash->elf.root.root.string[0] == '.'
b31867b6 10919 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 10920 {
8387904d
AM
10921 if (fdh->elf.root.type == bfd_link_hash_defined
10922 || fdh->elf.root.type == bfd_link_hash_defweak)
10923 {
10924 sym_sec = fdh->elf.root.u.def.section;
10925 sym_value = fdh->elf.root.u.def.value;
10926 if (sym_sec->output_section != NULL)
10927 ok_dest = TRUE;
10928 }
99877b66
AM
10929 else
10930 fdh = NULL;
8387904d 10931 }
7fe2b9a6
AM
10932 }
10933 else
10934 {
10935 bfd_set_error (bfd_error_bad_value);
10936 goto error_ret_free_internal;
721956f4
AM
10937 }
10938
8843416a
AM
10939 destination = 0;
10940 if (ok_dest)
10941 {
10942 sym_value += irela->r_addend;
10943 destination = (sym_value
10944 + sym_sec->output_offset
10945 + sym_sec->output_section->vma);
10946 }
10947
8387904d 10948 code_sec = sym_sec;
e054468f 10949 code_value = sym_value;
74f0fb50
AM
10950 opd = get_opd_info (sym_sec);
10951 if (opd != NULL)
8387904d
AM
10952 {
10953 bfd_vma dest;
10954
74f0fb50 10955 if (hash == NULL && opd->adjust != NULL)
8387904d 10956 {
74f0fb50 10957 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
10958 if (adjust == -1)
10959 continue;
e054468f 10960 code_value += adjust;
8387904d
AM
10961 sym_value += adjust;
10962 }
10963 dest = opd_entry_value (sym_sec, sym_value,
e054468f 10964 &code_sec, &code_value);
8387904d
AM
10965 if (dest != (bfd_vma) -1)
10966 {
10967 destination = dest;
10968 if (fdh != NULL)
10969 {
10970 /* Fixup old ABI sym to point at code
10971 entry. */
99877b66 10972 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 10973 hash->elf.root.u.def.section = code_sec;
e054468f 10974 hash->elf.root.u.def.value = code_value;
8387904d
AM
10975 }
10976 }
10977 }
10978
721956f4 10979 /* Determine what (if any) linker stub is needed. */
e054468f 10980 plt_ent = NULL;
721956f4 10981 stub_type = ppc_type_of_stub (section, irela, &hash,
e054468f 10982 &plt_ent, destination);
ad8e1ba5
AM
10983
10984 if (stub_type != ppc_stub_plt_call)
10985 {
10986 /* Check whether we need a TOC adjusting stub.
10987 Since the linker pastes together pieces from
10988 different object files when creating the
10989 _init and _fini functions, it may be that a
10990 call to what looks like a local sym is in
10991 fact a call needing a TOC adjustment. */
8387904d
AM
10992 if (code_sec != NULL
10993 && code_sec->output_section != NULL
10994 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 10995 != htab->stub_group[section->id].toc_off)
4c52953f
AM
10996 && (code_sec->has_toc_reloc
10997 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
10998 stub_type = ppc_stub_long_branch_r2off;
10999 }
11000
721956f4
AM
11001 if (stub_type == ppc_stub_none)
11002 continue;
11003
411e1bfb
AM
11004 /* __tls_get_addr calls might be eliminated. */
11005 if (stub_type != ppc_stub_plt_call
11006 && hash != NULL
8387904d
AM
11007 && (hash == htab->tls_get_addr
11008 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11009 && section->has_tls_reloc
11010 && irela != internal_relocs)
11011 {
11012 /* Get tls info. */
f961d9dd 11013 unsigned char *tls_mask;
411e1bfb 11014
3a71aa26 11015 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11016 irela - 1, input_bfd))
11017 goto error_ret_free_internal;
e7b938ca 11018 if (*tls_mask != 0)
411e1bfb
AM
11019 continue;
11020 }
11021
721956f4
AM
11022 /* Support for grouping stub sections. */
11023 id_sec = htab->stub_group[section->id].link_sec;
11024
11025 /* Get the name of this stub. */
11026 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11027 if (!stub_name)
11028 goto error_ret_free_internal;
11029
11030 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11031 stub_name, FALSE, FALSE);
721956f4
AM
11032 if (stub_entry != NULL)
11033 {
11034 /* The proper stub has already been created. */
11035 free (stub_name);
11036 continue;
11037 }
11038
11039 stub_entry = ppc_add_stub (stub_name, section, htab);
11040 if (stub_entry == NULL)
11041 {
11042 free (stub_name);
6cdc0ccc
AM
11043 error_ret_free_internal:
11044 if (elf_section_data (section)->relocs == NULL)
11045 free (internal_relocs);
11046 error_ret_free_local:
11047 if (local_syms != NULL
11048 && (symtab_hdr->contents
11049 != (unsigned char *) local_syms))
11050 free (local_syms);
b34976b6 11051 return FALSE;
721956f4
AM
11052 }
11053
ad8e1ba5 11054 stub_entry->stub_type = stub_type;
e054468f
AM
11055 if (stub_type != ppc_stub_plt_call)
11056 {
11057 stub_entry->target_value = code_value;
11058 stub_entry->target_section = code_sec;
11059 }
11060 else
11061 {
11062 stub_entry->target_value = sym_value;
11063 stub_entry->target_section = sym_sec;
11064 }
721956f4 11065 stub_entry->h = hash;
e054468f 11066 stub_entry->plt_ent = plt_ent;
411e1bfb 11067 stub_entry->addend = irela->r_addend;
ee75fd95
AM
11068
11069 if (stub_entry->h != NULL)
11070 htab->stub_globals += 1;
721956f4
AM
11071 }
11072
11073 /* We're done with the internal relocs, free them. */
6cdc0ccc 11074 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 11075 free (internal_relocs);
721956f4 11076 }
6cdc0ccc
AM
11077
11078 if (local_syms != NULL
11079 && symtab_hdr->contents != (unsigned char *) local_syms)
11080 {
11081 if (!info->keep_memory)
11082 free (local_syms);
11083 else
11084 symtab_hdr->contents = (unsigned char *) local_syms;
11085 }
721956f4
AM
11086 }
11087
5c3dead3 11088 /* We may have added some stubs. Find out the new size of the
721956f4
AM
11089 stub sections. */
11090 for (stub_sec = htab->stub_bfd->sections;
11091 stub_sec != NULL;
11092 stub_sec = stub_sec->next)
e717da7e 11093 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 11094 {
5c3dead3 11095 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
11096 stub_sec->size = 0;
11097 stub_sec->reloc_count = 0;
84f5d08e 11098 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 11099 }
eea6121a
AM
11100
11101 htab->brlt->size = 0;
84f5d08e
AM
11102 htab->brlt->reloc_count = 0;
11103 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 11104 if (htab->relbrlt != NULL)
eea6121a 11105 htab->relbrlt->size = 0;
721956f4 11106
63bc6f6c 11107 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 11108
176a0d42
AM
11109 if (info->emitrelocations
11110 && htab->glink != NULL && htab->glink->size != 0)
11111 {
11112 htab->glink->reloc_count = 1;
11113 htab->glink->flags |= SEC_RELOC;
11114 }
11115
5c3dead3
AM
11116 for (stub_sec = htab->stub_bfd->sections;
11117 stub_sec != NULL;
11118 stub_sec = stub_sec->next)
11119 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11120 && stub_sec->rawsize != stub_sec->size)
11121 break;
11122
11123 /* Exit from this loop when no stubs have been added, and no stubs
11124 have changed size. */
11125 if (stub_sec == NULL)
11126 break;
11127
721956f4
AM
11128 /* Ask the linker to do its stuff. */
11129 (*htab->layout_sections_again) ();
11130 }
11131
c456f082 11132 /* It would be nice to strip htab->brlt from the output if the
afbe61cf
AM
11133 section is empty, but it's too late. If we strip sections here,
11134 the dynamic symbol table is corrupted since the section symbol
11135 for the stripped section isn't written. */
721956f4 11136
b34976b6 11137 return TRUE;
721956f4
AM
11138}
11139
11140/* Called after we have determined section placement. If sections
805fc799 11141 move, we'll be called again. Provide a value for TOCstart. */
721956f4 11142
805fc799 11143bfd_vma
4ce794b7 11144ppc64_elf_toc (bfd *obfd)
721956f4 11145{
805fc799
AM
11146 asection *s;
11147 bfd_vma TOCstart;
721956f4 11148
805fc799
AM
11149 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11150 order. The TOC starts where the first of these sections starts. */
11151 s = bfd_get_section_by_name (obfd, ".got");
e054468f 11152 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11153 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 11154 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11155 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 11156 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11157 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 11158 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
11159 {
11160 /* This may happen for
11161 o references to TOC base (SYM@toc / TOC[tc0]) without a
11162 .toc directive
11163 o bad linker script
11164 o --gc-sections and empty TOC sections
11165
11166 FIXME: Warn user? */
11167
11168 /* Look for a likely section. We probably won't even be
11169 using TOCstart. */
11170 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11171 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11172 | SEC_EXCLUDE))
805fc799
AM
11173 == (SEC_ALLOC | SEC_SMALL_DATA))
11174 break;
721956f4 11175 if (s == NULL)
805fc799 11176 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11177 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
11178 == (SEC_ALLOC | SEC_SMALL_DATA))
11179 break;
721956f4 11180 if (s == NULL)
805fc799 11181 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11182 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11183 == SEC_ALLOC)
805fc799 11184 break;
721956f4 11185 if (s == NULL)
805fc799 11186 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11187 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
11188 break;
11189 }
721956f4 11190
805fc799
AM
11191 TOCstart = 0;
11192 if (s != NULL)
11193 TOCstart = s->output_section->vma + s->output_offset;
721956f4 11194
805fc799 11195 return TOCstart;
721956f4
AM
11196}
11197
11198/* Build all the stubs associated with the current output file.
11199 The stubs are kept in a hash table attached to the main linker
11200 hash table. This function is called via gldelf64ppc_finish. */
11201
b34976b6 11202bfd_boolean
4ce794b7
AM
11203ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11204 struct bfd_link_info *info,
11205 char **stats)
5d1634d7
AM
11206{
11207 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 11208 asection *stub_sec;
5d1634d7 11209 bfd_byte *p;
e717da7e 11210 int stub_sec_count = 0;
5d1634d7 11211
4dfe6ac6
NC
11212 if (htab == NULL)
11213 return FALSE;
11214
ad8e1ba5 11215 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
11216
11217 /* Allocate memory to hold the linker stubs. */
721956f4
AM
11218 for (stub_sec = htab->stub_bfd->sections;
11219 stub_sec != NULL;
11220 stub_sec = stub_sec->next)
eea6121a
AM
11221 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11222 && stub_sec->size != 0)
e717da7e 11223 {
eea6121a
AM
11224 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11225 if (stub_sec->contents == NULL)
11226 return FALSE;
11227 /* We want to check that built size is the same as calculated
11228 size. rawsize is a convenient location to use. */
11229 stub_sec->rawsize = stub_sec->size;
11230 stub_sec->size = 0;
e717da7e 11231 }
5d1634d7 11232
23eb7e01 11233 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 11234 {
9f951329 11235 unsigned int indx;
ad8e1ba5 11236 bfd_vma plt0;
9f951329 11237
721956f4 11238 /* Build the .glink plt call stub. */
97b639ba
AM
11239 if (htab->emit_stub_syms)
11240 {
11241 struct elf_link_hash_entry *h;
468392fb
AM
11242 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11243 TRUE, FALSE, FALSE);
97b639ba
AM
11244 if (h == NULL)
11245 return FALSE;
11246 if (h->root.type == bfd_link_hash_new)
11247 {
11248 h->root.type = bfd_link_hash_defined;
11249 h->root.u.def.section = htab->glink;
ee4bf8d2 11250 h->root.u.def.value = 8;
f5385ebf
AM
11251 h->ref_regular = 1;
11252 h->def_regular = 1;
11253 h->ref_regular_nonweak = 1;
11254 h->forced_local = 1;
11255 h->non_elf = 0;
97b639ba
AM
11256 }
11257 }
176a0d42
AM
11258 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11259 if (info->emitrelocations)
11260 {
11261 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11262 if (r == NULL)
11263 return FALSE;
11264 r->r_offset = (htab->glink->output_offset
11265 + htab->glink->output_section->vma);
11266 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11267 r->r_addend = plt0;
11268 }
4ce794b7 11269 p = htab->glink->contents;
176a0d42 11270 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
11271 bfd_put_64 (htab->glink->owner, plt0, p);
11272 p += 8;
11273 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 11274 p += 4;
ee4bf8d2 11275 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 11276 p += 4;
ee4bf8d2 11277 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 11278 p += 4;
ee4bf8d2 11279 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 11280 p += 4;
ee4bf8d2 11281 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 11282 p += 4;
ee4bf8d2 11283 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 11284 p += 4;
ee4bf8d2 11285 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 11286 p += 4;
4ce794b7 11287 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 11288 p += 4;
4ce794b7 11289 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 11290 p += 4;
4ce794b7 11291 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 11292 p += 4;
4ce794b7 11293 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 11294 p += 4;
ee4bf8d2
AM
11295 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11296 {
11297 bfd_put_32 (htab->glink->owner, NOP, p);
11298 p += 4;
11299 }
ad8e1ba5 11300
9f951329
AM
11301 /* Build the .glink lazy link call stubs. */
11302 indx = 0;
eea6121a 11303 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
11304 {
11305 if (indx < 0x8000)
11306 {
4ce794b7 11307 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
11308 p += 4;
11309 }
11310 else
11311 {
4ce794b7 11312 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 11313 p += 4;
4ce794b7 11314 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
11315 p += 4;
11316 }
4ce794b7 11317 bfd_put_32 (htab->glink->owner,
ee4bf8d2 11318 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 11319 indx++;
9f951329
AM
11320 p += 4;
11321 }
eea6121a 11322 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 11323 }
5d1634d7 11324
eea6121a 11325 if (htab->brlt->size != 0)
721956f4 11326 {
4ce794b7 11327 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 11328 htab->brlt->size);
4ce794b7 11329 if (htab->brlt->contents == NULL)
b34976b6 11330 return FALSE;
721956f4 11331 }
ee75fd95 11332 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
11333 {
11334 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 11335 htab->relbrlt->size);
63bc6f6c
AM
11336 if (htab->relbrlt->contents == NULL)
11337 return FALSE;
11338 }
5d1634d7 11339
721956f4
AM
11340 /* Build the stubs as directed by the stub hash table. */
11341 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 11342
aa8a7074
AM
11343 if (htab->relbrlt != NULL)
11344 htab->relbrlt->reloc_count = 0;
11345
721956f4
AM
11346 for (stub_sec = htab->stub_bfd->sections;
11347 stub_sec != NULL;
11348 stub_sec = stub_sec->next)
e717da7e
AM
11349 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11350 {
11351 stub_sec_count += 1;
eea6121a 11352 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
11353 break;
11354 }
5d1634d7 11355
721956f4 11356 if (stub_sec != NULL
eea6121a 11357 || htab->glink->rawsize != htab->glink->size)
5d1634d7 11358 {
b34976b6 11359 htab->stub_error = TRUE;
721956f4 11360 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5d1634d7 11361 }
721956f4 11362
d2a300cf
AM
11363 if (htab->stub_error)
11364 return FALSE;
11365
11366 if (stats != NULL)
11367 {
11368 *stats = bfd_malloc (500);
11369 if (*stats == NULL)
11370 return FALSE;
11371
ee75fd95 11372 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
11373 " branch %lu\n"
11374 " toc adjust %lu\n"
11375 " long branch %lu\n"
11376 " long toc adj %lu\n"
11377 " plt call %lu"),
e717da7e 11378 stub_sec_count,
ee75fd95 11379 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
11380 htab->stub_count[ppc_stub_long_branch - 1],
11381 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11382 htab->stub_count[ppc_stub_plt_branch - 1],
11383 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11384 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
11385 }
11386 return TRUE;
5bd4f169
AM
11387}
11388
99877b66
AM
11389/* This function undoes the changes made by add_symbol_adjust. */
11390
11391static bfd_boolean
11392undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11393{
11394 struct ppc_link_hash_entry *eh;
11395
11396 if (h->root.type == bfd_link_hash_indirect)
11397 return TRUE;
11398
11399 if (h->root.type == bfd_link_hash_warning)
11400 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11401
11402 eh = (struct ppc_link_hash_entry *) h;
11403 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11404 return TRUE;
11405
11406 eh->elf.root.type = bfd_link_hash_undefined;
11407 return TRUE;
11408}
11409
11410void
11411ppc64_elf_restore_symbols (struct bfd_link_info *info)
11412{
11413 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
11414
11415 if (htab != NULL)
11416 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
11417}
11418
60124e18
AM
11419/* What to do when ld finds relocations against symbols defined in
11420 discarded sections. */
11421
11422static unsigned int
11423ppc64_elf_action_discarded (asection *sec)
11424{
11425 if (strcmp (".opd", sec->name) == 0)
11426 return 0;
11427
11428 if (strcmp (".toc", sec->name) == 0)
11429 return 0;
11430
bce50a28
JJ
11431 if (strcmp (".toc1", sec->name) == 0)
11432 return 0;
11433
60124e18
AM
11434 return _bfd_elf_default_action_discarded (sec);
11435}
11436
ba761f19 11437/* REL points to a low-part reloc on a largetoc instruction sequence.
67f0cbdb 11438 Find the matching high-part reloc instruction and verify that it
d983b7a0
AM
11439 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11440 the high-part reloc. */
67f0cbdb
AM
11441
11442static const Elf_Internal_Rela *
11443ha_reloc_match (const Elf_Internal_Rela *relocs,
11444 const Elf_Internal_Rela *rel,
d983b7a0
AM
11445 unsigned int *reg,
11446 bfd_boolean match_addend,
67f0cbdb
AM
11447 const bfd *input_bfd,
11448 const bfd_byte *contents)
11449{
11450 enum elf_ppc64_reloc_type r_type, r_type_ha;
11451 bfd_vma r_info_ha, r_addend;
11452
11453 r_type = ELF64_R_TYPE (rel->r_info);
11454 switch (r_type)
11455 {
11456 case R_PPC64_GOT_TLSLD16_LO:
11457 case R_PPC64_GOT_TLSGD16_LO:
11458 case R_PPC64_GOT_TPREL16_LO_DS:
11459 case R_PPC64_GOT_DTPREL16_LO_DS:
11460 case R_PPC64_GOT16_LO:
11461 case R_PPC64_TOC16_LO:
11462 r_type_ha = r_type + 2;
11463 break;
11464 case R_PPC64_GOT16_LO_DS:
11465 r_type_ha = R_PPC64_GOT16_HA;
11466 break;
11467 case R_PPC64_TOC16_LO_DS:
11468 r_type_ha = R_PPC64_TOC16_HA;
11469 break;
11470 default:
11471 abort ();
11472 }
11473 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11474 r_addend = rel->r_addend;
11475
11476 while (--rel >= relocs)
11477 if (rel->r_info == r_info_ha
d983b7a0
AM
11478 && (!match_addend
11479 || rel->r_addend == r_addend))
67f0cbdb
AM
11480 {
11481 const bfd_byte *p = contents + (rel->r_offset & ~3);
11482 unsigned int insn = bfd_get_32 (input_bfd, p);
d983b7a0
AM
11483 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11484 && (insn & (0x1f << 21)) == (*reg << 21))
11485 {
11486 *reg = (insn >> 16) & 0x1f;
11487 return rel;
11488 }
67f0cbdb
AM
11489 break;
11490 }
11491 return NULL;
11492}
11493
5bd4f169
AM
11494/* The RELOCATE_SECTION function is called by the ELF backend linker
11495 to handle the relocations for a section.
11496
11497 The relocs are always passed as Rela structures; if the section
11498 actually uses Rel structures, the r_addend field will always be
11499 zero.
11500
11501 This function is responsible for adjust the section contents as
11502 necessary, and (if using Rela relocs and generating a
1049f94e 11503 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
11504 necessary.
11505
11506 This function does not have to worry about setting the reloc
11507 address or the reloc symbol index.
11508
11509 LOCAL_SYMS is a pointer to the swapped in local symbols.
11510
11511 LOCAL_SECTIONS is an array giving the section in the input file
11512 corresponding to the st_shndx field of each local symbol.
11513
11514 The global hash table entry for the global symbols can be found
11515 via elf_sym_hashes (input_bfd).
11516
1049f94e 11517 When generating relocatable output, this function must handle
5bd4f169
AM
11518 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11519 going to be the section symbol corresponding to the output
11520 section, which means that the addend must be adjusted
11521 accordingly. */
11522
b34976b6 11523static bfd_boolean
4ce794b7
AM
11524ppc64_elf_relocate_section (bfd *output_bfd,
11525 struct bfd_link_info *info,
11526 bfd *input_bfd,
11527 asection *input_section,
11528 bfd_byte *contents,
11529 Elf_Internal_Rela *relocs,
11530 Elf_Internal_Sym *local_syms,
11531 asection **local_sections)
5bd4f169 11532{
65f38f15 11533 struct ppc_link_hash_table *htab;
5bd4f169
AM
11534 Elf_Internal_Shdr *symtab_hdr;
11535 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
11536 Elf_Internal_Rela *rel;
11537 Elf_Internal_Rela *relend;
411e1bfb
AM
11538 Elf_Internal_Rela outrel;
11539 bfd_byte *loc;
411e1bfb 11540 struct got_entry **local_got_ents;
d983b7a0 11541 unsigned char *ha_opt;
5bd4f169 11542 bfd_vma TOCstart;
d983b7a0 11543 bfd_boolean no_ha_opt;
b34976b6
AM
11544 bfd_boolean ret = TRUE;
11545 bfd_boolean is_opd;
96e0dda4 11546 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 11547 bfd_boolean is_power4 = FALSE;
4fe5ca5b 11548 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 11549
65f38f15 11550 /* Initialize howto table if needed. */
5bd4f169 11551 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
11552 ppc_howto_init ();
11553
65f38f15 11554 htab = ppc_hash_table (info);
4dfe6ac6
NC
11555 if (htab == NULL)
11556 return FALSE;
ee75fd95
AM
11557
11558 /* Don't relocate stub sections. */
11559 if (input_section->owner == htab->stub_bfd)
11560 return TRUE;
11561
0c8d6e5c 11562 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 11563
411e1bfb 11564 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 11565 TOCstart = elf_gp (output_bfd);
0ffa91dd 11566 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 11567 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 11568 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
d983b7a0
AM
11569 ha_opt = NULL;
11570 no_ha_opt = FALSE;
65f38f15 11571
5bd4f169
AM
11572 rel = relocs;
11573 relend = relocs + input_section->reloc_count;
11574 for (; rel < relend; rel++)
11575 {
04c9666a 11576 enum elf_ppc64_reloc_type r_type;
4cc603a5 11577 bfd_vma addend, orig_addend;
5bd4f169
AM
11578 bfd_reloc_status_type r;
11579 Elf_Internal_Sym *sym;
11580 asection *sec;
039b3fef
AM
11581 struct elf_link_hash_entry *h_elf;
11582 struct ppc_link_hash_entry *h;
11583 struct ppc_link_hash_entry *fdh;
5bd4f169 11584 const char *sym_name;
0d4792f7 11585 unsigned long r_symndx, toc_symndx;
3a71aa26 11586 bfd_vma toc_addend;
f961d9dd
AM
11587 unsigned char tls_mask, tls_gd, tls_type;
11588 unsigned char sym_type;
5bd4f169 11589 bfd_vma relocation;
b34976b6
AM
11590 bfd_boolean unresolved_reloc;
11591 bfd_boolean warned;
67f0cbdb
AM
11592 unsigned int insn;
11593 bfd_vma mask;
721956f4
AM
11594 struct ppc_stub_hash_entry *stub_entry;
11595 bfd_vma max_br_offset;
11596 bfd_vma from;
5bd4f169 11597
4ce794b7 11598 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 11599 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
11600
11601 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11602 symbol of the previous ADDR64 reloc. The symbol gives us the
11603 proper TOC base to use. */
11604 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11605 && rel != relocs
11606 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11607 && is_opd)
11608 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11609
4ce794b7
AM
11610 sym = NULL;
11611 sec = NULL;
039b3fef 11612 h_elf = NULL;
4ce794b7 11613 sym_name = NULL;
b34976b6
AM
11614 unresolved_reloc = FALSE;
11615 warned = FALSE;
4cc603a5 11616 orig_addend = rel->r_addend;
65f38f15 11617
0b13192e 11618 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
11619 {
11620 /* It's a local symbol. */
74f0fb50 11621 struct _opd_sec_data *opd;
4025353c 11622
5bd4f169
AM
11623 sym = local_syms + r_symndx;
11624 sec = local_sections[r_symndx];
26c61ae5 11625 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 11626 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 11627 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
11628 opd = get_opd_info (sec);
11629 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 11630 {
74f0fb50 11631 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
11632 if (adjust == -1)
11633 relocation = 0;
11634 else
4cc603a5
AM
11635 {
11636 /* If this is a relocation against the opd section sym
11637 and we have edited .opd, adjust the reloc addend so
11638 that ld -r and ld --emit-relocs output is correct.
11639 If it is a reloc against some other .opd symbol,
11640 then the symbol value will be adjusted later. */
11641 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11642 rel->r_addend += adjust;
11643 else
11644 relocation += adjust;
11645 }
1e2f5b6e 11646 }
5bd4f169
AM
11647 }
11648 else
11649 {
b2a8e766
AM
11650 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11651 r_symndx, symtab_hdr, sym_hashes,
039b3fef 11652 h_elf, sec, relocation,
b2a8e766 11653 unresolved_reloc, warned);
039b3fef
AM
11654 sym_name = h_elf->root.root.string;
11655 sym_type = h_elf->type;
5bd4f169 11656 }
039b3fef 11657 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 11658
ab96bf03 11659 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
11660 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11661 rel, relend,
11662 ppc64_elf_howto_table[r_type],
11663 contents);
ab96bf03
AM
11664
11665 if (info->relocatable)
11666 continue;
11667
951fd09b
AM
11668 /* TLS optimizations. Replace instruction sequences and relocs
11669 based on information we collected in tls_optimize. We edit
11670 RELOCS so that --emit-relocs will output something sensible
11671 for the final instruction stream. */
11672 tls_mask = 0;
11673 tls_gd = 0;
0d4792f7 11674 toc_symndx = 0;
727fc41e
AM
11675 if (h != NULL)
11676 tls_mask = h->tls_mask;
11677 else if (local_got_ents != NULL)
411e1bfb 11678 {
e054468f
AM
11679 struct plt_entry **local_plt = (struct plt_entry **)
11680 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 11681 unsigned char *lgot_masks = (unsigned char *)
e054468f 11682 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
11683 tls_mask = lgot_masks[r_symndx];
11684 }
11685 if (tls_mask == 0
11686 && (r_type == R_PPC64_TLS
11687 || r_type == R_PPC64_TLSGD
11688 || r_type == R_PPC64_TLSLD))
11689 {
11690 /* Check for toc tls entries. */
f961d9dd 11691 unsigned char *toc_tls;
0d4792f7 11692
727fc41e
AM
11693 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11694 &local_syms, rel, input_bfd))
11695 return FALSE;
0d4792f7 11696
727fc41e
AM
11697 if (toc_tls)
11698 tls_mask = *toc_tls;
0d4792f7
AM
11699 }
11700
11701 /* Check that tls relocs are used with tls syms, and non-tls
11702 relocs are used with non-tls syms. */
cf35638d 11703 if (r_symndx != STN_UNDEF
0d4792f7
AM
11704 && r_type != R_PPC64_NONE
11705 && (h == NULL
039b3fef
AM
11706 || h->elf.root.type == bfd_link_hash_defined
11707 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
11708 && (IS_PPC64_TLS_RELOC (r_type)
11709 != (sym_type == STT_TLS
11710 || (sym_type == STT_SECTION
11711 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 11712 {
727fc41e
AM
11713 if (tls_mask != 0
11714 && (r_type == R_PPC64_TLS
11715 || r_type == R_PPC64_TLSGD
11716 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
11717 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11718 ;
11719 else
11720 (*_bfd_error_handler)
1d483afe 11721 (!IS_PPC64_TLS_RELOC (r_type)
d003868e
AM
11722 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
11723 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
11724 input_bfd,
11725 input_section,
0d4792f7
AM
11726 (long) rel->r_offset,
11727 ppc64_elf_howto_table[r_type]->name,
11728 sym_name);
411e1bfb
AM
11729 }
11730
11731 /* Ensure reloc mapping code below stays sane. */
11732 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11733 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11734 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11735 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11736 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11737 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11738 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11739 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11740 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11741 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11742 abort ();
0d4792f7 11743
411e1bfb
AM
11744 switch (r_type)
11745 {
11746 default:
411e1bfb
AM
11747 break;
11748
ba761f19
AM
11749 case R_PPC64_LO_DS_OPT:
11750 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11751 if ((insn & (0x3f << 26)) != 58u << 26)
11752 abort ();
11753 insn += (14u << 26) - (58u << 26);
11754 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11755 r_type = R_PPC64_TOC16_LO;
11756 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11757 break;
11758
411e1bfb
AM
11759 case R_PPC64_TOC16:
11760 case R_PPC64_TOC16_LO:
11761 case R_PPC64_TOC16_DS:
11762 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
11763 {
11764 /* Check for toc tls entries. */
f961d9dd 11765 unsigned char *toc_tls;
951fd09b 11766 int retval;
411e1bfb 11767
3a71aa26
AM
11768 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11769 &local_syms, rel, input_bfd);
951fd09b 11770 if (retval == 0)
411e1bfb
AM
11771 return FALSE;
11772
11773 if (toc_tls)
11774 {
951fd09b 11775 tls_mask = *toc_tls;
411e1bfb
AM
11776 if (r_type == R_PPC64_TOC16_DS
11777 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
11778 {
11779 if (tls_mask != 0
11780 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11781 goto toctprel;
11782 }
411e1bfb 11783 else
951fd09b
AM
11784 {
11785 /* If we found a GD reloc pair, then we might be
11786 doing a GD->IE transition. */
11787 if (retval == 2)
11788 {
11789 tls_gd = TLS_TPRELGD;
11790 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 11791 goto tls_ldgd_opt;
951fd09b
AM
11792 }
11793 else if (retval == 3)
11794 {
11795 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 11796 goto tls_ldgd_opt;
951fd09b
AM
11797 }
11798 }
411e1bfb
AM
11799 }
11800 }
11801 break;
11802
9d6ded02
AM
11803 case R_PPC64_GOT_TPREL16_HI:
11804 case R_PPC64_GOT_TPREL16_HA:
11805 if (tls_mask != 0
11806 && (tls_mask & TLS_TPREL) == 0)
11807 {
11808 rel->r_offset -= d_offset;
11809 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11810 r_type = R_PPC64_NONE;
11811 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11812 }
11813 break;
11814
411e1bfb
AM
11815 case R_PPC64_GOT_TPREL16_DS:
11816 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
11817 if (tls_mask != 0
11818 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11819 {
81407a69 11820 toctprel:
4fe5ca5b 11821 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
11822 insn &= 31 << 21;
11823 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 11824 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 11825 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
11826 if (toc_symndx != 0)
11827 {
11828 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11829 rel->r_addend = toc_addend;
0d4792f7
AM
11830 /* We changed the symbol. Start over in order to
11831 get h, sym, sec etc. right. */
11832 rel--;
11833 continue;
11834 }
11835 else
11836 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11837 }
11838 break;
11839
11840 case R_PPC64_TLS:
951fd09b
AM
11841 if (tls_mask != 0
11842 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11843 {
411e1bfb 11844 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
11845 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11846 if (insn == 0)
411e1bfb 11847 abort ();
411e1bfb 11848 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 11849 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
11850 PPC64_TPREL16_LO which is at low-order half-word. */
11851 rel->r_offset += d_offset;
0d4792f7
AM
11852 r_type = R_PPC64_TPREL16_LO;
11853 if (toc_symndx != 0)
11854 {
11855 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11856 rel->r_addend = toc_addend;
0d4792f7
AM
11857 /* We changed the symbol. Start over in order to
11858 get h, sym, sec etc. right. */
11859 rel--;
11860 continue;
11861 }
11862 else
11863 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11864 }
11865 break;
11866
411e1bfb
AM
11867 case R_PPC64_GOT_TLSGD16_HI:
11868 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
11869 tls_gd = TLS_TPRELGD;
11870 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11871 goto tls_gdld_hi;
11872 break;
11873
411e1bfb
AM
11874 case R_PPC64_GOT_TLSLD16_HI:
11875 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 11876 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 11877 {
951fd09b
AM
11878 tls_gdld_hi:
11879 if ((tls_mask & tls_gd) != 0)
11880 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11881 + R_PPC64_GOT_TPREL16_DS);
11882 else
411e1bfb 11883 {
4fe5ca5b 11884 rel->r_offset -= d_offset;
727ac201 11885 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 11886 r_type = R_PPC64_NONE;
411e1bfb 11887 }
951fd09b 11888 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11889 }
11890 break;
11891
951fd09b
AM
11892 case R_PPC64_GOT_TLSGD16:
11893 case R_PPC64_GOT_TLSGD16_LO:
11894 tls_gd = TLS_TPRELGD;
11895 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 11896 goto tls_ldgd_opt;
951fd09b 11897 break;
411e1bfb 11898
951fd09b
AM
11899 case R_PPC64_GOT_TLSLD16:
11900 case R_PPC64_GOT_TLSLD16_LO:
11901 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11902 {
3a71aa26 11903 unsigned int insn1, insn2, insn3;
102890f0
AM
11904 bfd_vma offset;
11905
11906 tls_ldgd_opt:
727fc41e
AM
11907 offset = (bfd_vma) -1;
11908 /* If not using the newer R_PPC64_TLSGD/LD to mark
11909 __tls_get_addr calls, we must trust that the call
11910 stays with its arg setup insns, ie. that the next
11911 reloc is the __tls_get_addr call associated with
11912 the current reloc. Edit both insns. */
11913 if (input_section->has_tls_get_addr_call
11914 && rel + 1 < relend
11915 && branch_reloc_hash_match (input_bfd, rel + 1,
11916 htab->tls_get_addr,
11917 htab->tls_get_addr_fd))
11918 offset = rel[1].r_offset;
102890f0 11919 if ((tls_mask & tls_gd) != 0)
411e1bfb 11920 {
102890f0 11921 /* IE */
3a71aa26
AM
11922 insn1 = bfd_get_32 (output_bfd,
11923 contents + rel->r_offset - d_offset);
102890f0
AM
11924 insn1 &= (1 << 26) - (1 << 2);
11925 insn1 |= 58 << 26; /* ld */
11926 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 11927 if (offset != (bfd_vma) -1)
f58d5a2d 11928 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
11929 if ((tls_mask & TLS_EXPLICIT) == 0)
11930 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11931 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 11932 else
102890f0
AM
11933 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
11934 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11935 }
11936 else
11937 {
11938 /* LE */
11939 insn1 = 0x3c6d0000; /* addis 3,13,0 */
11940 insn2 = 0x38630000; /* addi 3,3,0 */
11941 if (tls_gd == 0)
951fd09b 11942 {
102890f0 11943 /* Was an LD reloc. */
1d483afe
AM
11944 if (toc_symndx)
11945 sec = local_sections[toc_symndx];
11946 for (r_symndx = 0;
11947 r_symndx < symtab_hdr->sh_info;
11948 r_symndx++)
11949 if (local_sections[r_symndx] == sec)
11950 break;
11951 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 11952 r_symndx = STN_UNDEF;
102890f0 11953 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 11954 if (r_symndx != STN_UNDEF)
1d483afe
AM
11955 rel->r_addend -= (local_syms[r_symndx].st_value
11956 + sec->output_offset
11957 + sec->output_section->vma);
951fd09b 11958 }
102890f0 11959 else if (toc_symndx != 0)
3a71aa26
AM
11960 {
11961 r_symndx = toc_symndx;
11962 rel->r_addend = toc_addend;
11963 }
102890f0
AM
11964 r_type = R_PPC64_TPREL16_HA;
11965 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
11966 if (offset != (bfd_vma) -1)
11967 {
11968 rel[1].r_info = ELF64_R_INFO (r_symndx,
11969 R_PPC64_TPREL16_LO);
11970 rel[1].r_offset = offset + d_offset;
11971 rel[1].r_addend = rel->r_addend;
11972 }
102890f0 11973 }
3a71aa26
AM
11974 bfd_put_32 (output_bfd, insn1,
11975 contents + rel->r_offset - d_offset);
727fc41e
AM
11976 if (offset != (bfd_vma) -1)
11977 {
11978 insn3 = bfd_get_32 (output_bfd,
11979 contents + offset + 4);
11980 if (insn3 == NOP
11981 || insn3 == CROR_151515 || insn3 == CROR_313131)
11982 {
11983 rel[1].r_offset += 4;
11984 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
11985 insn2 = NOP;
11986 }
11987 bfd_put_32 (output_bfd, insn2, contents + offset);
11988 }
11989 if ((tls_mask & tls_gd) == 0
11990 && (tls_gd == 0 || toc_symndx != 0))
11991 {
11992 /* We changed the symbol. Start over in order
11993 to get h, sym, sec etc. right. */
11994 rel--;
11995 continue;
11996 }
11997 }
11998 break;
11999
12000 case R_PPC64_TLSGD:
12001 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12002 {
12003 unsigned int insn2, insn3;
12004 bfd_vma offset = rel->r_offset;
12005
12006 if ((tls_mask & TLS_TPRELGD) != 0)
12007 {
12008 /* IE */
12009 r_type = R_PPC64_NONE;
12010 insn2 = 0x7c636a14; /* add 3,3,13 */
12011 }
12012 else
12013 {
12014 /* LE */
12015 if (toc_symndx != 0)
12016 {
12017 r_symndx = toc_symndx;
12018 rel->r_addend = toc_addend;
12019 }
12020 r_type = R_PPC64_TPREL16_LO;
12021 rel->r_offset = offset + d_offset;
12022 insn2 = 0x38630000; /* addi 3,3,0 */
12023 }
12024 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12025 /* Zap the reloc on the _tls_get_addr call too. */
12026 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12027 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
12028 insn3 = bfd_get_32 (output_bfd,
12029 contents + offset + 4);
102890f0
AM
12030 if (insn3 == NOP
12031 || insn3 == CROR_151515 || insn3 == CROR_313131)
12032 {
727fc41e 12033 rel->r_offset += 4;
3a71aa26
AM
12034 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12035 insn2 = NOP;
102890f0 12036 }
102890f0 12037 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 12038 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 12039 {
102890f0
AM
12040 rel--;
12041 continue;
411e1bfb 12042 }
411e1bfb 12043 }
411e1bfb
AM
12044 break;
12045
727fc41e
AM
12046 case R_PPC64_TLSLD:
12047 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12048 {
12049 unsigned int insn2, insn3;
12050 bfd_vma offset = rel->r_offset;
12051
12052 if (toc_symndx)
12053 sec = local_sections[toc_symndx];
12054 for (r_symndx = 0;
12055 r_symndx < symtab_hdr->sh_info;
12056 r_symndx++)
12057 if (local_sections[r_symndx] == sec)
12058 break;
12059 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12060 r_symndx = STN_UNDEF;
727fc41e 12061 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12062 if (r_symndx != STN_UNDEF)
727fc41e
AM
12063 rel->r_addend -= (local_syms[r_symndx].st_value
12064 + sec->output_offset
12065 + sec->output_section->vma);
12066
12067 r_type = R_PPC64_TPREL16_LO;
12068 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12069 rel->r_offset = offset + d_offset;
12070 /* Zap the reloc on the _tls_get_addr call too. */
12071 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12072 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
12073 insn2 = 0x38630000; /* addi 3,3,0 */
12074 insn3 = bfd_get_32 (output_bfd,
12075 contents + offset + 4);
12076 if (insn3 == NOP
12077 || insn3 == CROR_151515 || insn3 == CROR_313131)
12078 {
12079 rel->r_offset += 4;
12080 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12081 insn2 = NOP;
12082 }
12083 bfd_put_32 (output_bfd, insn2, contents + offset);
12084 rel--;
12085 continue;
12086 }
12087 break;
12088
411e1bfb 12089 case R_PPC64_DTPMOD64:
951fd09b
AM
12090 if (rel + 1 < relend
12091 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12092 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 12093 {
951fd09b
AM
12094 if ((tls_mask & TLS_GD) == 0)
12095 {
12096 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12097 if ((tls_mask & TLS_TPRELGD) != 0)
12098 r_type = R_PPC64_TPREL64;
12099 else
12100 {
4ce794b7 12101 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
12102 r_type = R_PPC64_NONE;
12103 }
12104 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12105 }
12106 }
12107 else
12108 {
12109 if ((tls_mask & TLS_LD) == 0)
411e1bfb 12110 {
4ce794b7 12111 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 12112 r_type = R_PPC64_NONE;
951fd09b 12113 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 12114 }
411e1bfb
AM
12115 }
12116 break;
12117
12118 case R_PPC64_TPREL64:
951fd09b 12119 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
12120 {
12121 r_type = R_PPC64_NONE;
12122 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12123 }
12124 break;
12125 }
12126
12127 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 12128 insn = 0;
b25116a9
AM
12129 max_br_offset = 1 << 25;
12130 addend = rel->r_addend;
65f38f15 12131 switch (r_type)
5bd4f169
AM
12132 {
12133 default:
65f38f15 12134 break;
5bd4f169 12135
65f38f15
AM
12136 /* Branch taken prediction relocations. */
12137 case R_PPC64_ADDR14_BRTAKEN:
12138 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
12139 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12140 /* Fall thru. */
65f38f15 12141
86c76c7b 12142 /* Branch not taken prediction relocations. */
65f38f15
AM
12143 case R_PPC64_ADDR14_BRNTAKEN:
12144 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
12145 insn |= bfd_get_32 (output_bfd,
12146 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 12147 /* Fall thru. */
86c76c7b 12148
b25116a9
AM
12149 case R_PPC64_REL14:
12150 max_br_offset = 1 << 15;
12151 /* Fall thru. */
5bd4f169 12152
65f38f15 12153 case R_PPC64_REL24:
ad8e1ba5
AM
12154 /* Calls to functions with a different TOC, such as calls to
12155 shared objects, need to alter the TOC pointer. This is
12156 done using a linkage stub. A REL24 branching to these
12157 linkage stubs needs to be followed by a nop, as the nop
12158 will be replaced with an instruction to restore the TOC
12159 base pointer. */
8387904d 12160 fdh = h;
b31867b6
AM
12161 if (h != NULL
12162 && h->oh != NULL
12163 && h->oh->is_func_descriptor)
12164 fdh = ppc_follow_link (h->oh);
6abec6bc
AM
12165 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12166 if (stub_entry != NULL
ad8e1ba5
AM
12167 && (stub_entry->stub_type == ppc_stub_plt_call
12168 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12169 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 12170 {
b25116a9 12171 bfd_boolean can_plt_call = FALSE;
721956f4 12172
eea6121a 12173 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 12174 {
b25116a9
AM
12175 unsigned long nop;
12176 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12177 if (nop == NOP
12178 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 12179 {
a7f2871e
AM
12180 if (h != NULL
12181 && (h == htab->tls_get_addr_fd
12182 || h == htab->tls_get_addr)
12183 && !htab->no_tls_get_addr_opt)
12184 {
12185 /* Special stub used, leave nop alone. */
12186 }
12187 else
12188 bfd_put_32 (input_bfd, LD_R2_40R1,
12189 contents + rel->r_offset + 4);
b25116a9 12190 can_plt_call = TRUE;
41bd81ab 12191 }
5bd4f169 12192 }
721956f4
AM
12193
12194 if (!can_plt_call)
12195 {
ad8e1ba5
AM
12196 if (stub_entry->stub_type == ppc_stub_plt_call)
12197 {
12198 /* If this is a plain branch rather than a branch
4fa1c237
AM
12199 and link, don't require a nop. However, don't
12200 allow tail calls in a shared library as they
12201 will result in r2 being corrupted. */
b25116a9
AM
12202 unsigned long br;
12203 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 12204 if (info->executable && (br & 1) == 0)
b25116a9 12205 can_plt_call = TRUE;
15b552e9
AM
12206 else
12207 stub_entry = NULL;
ad8e1ba5 12208 }
6ab189d5 12209 else if (h != NULL
039b3fef 12210 && strcmp (h->elf.root.root.string,
6ab189d5
AM
12211 ".__libc_start_main") == 0)
12212 {
12213 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 12214 can_plt_call = TRUE;
6ab189d5 12215 }
ad8e1ba5
AM
12216 else
12217 {
12218 if (strcmp (input_section->output_section->name,
12219 ".init") == 0
12220 || strcmp (input_section->output_section->name,
12221 ".fini") == 0)
12222 (*_bfd_error_handler)
d003868e 12223 (_("%B(%A+0x%lx): automatic multiple TOCs "
ad8e1ba5
AM
12224 "not supported using your crt files; "
12225 "recompile with -mminimal-toc or upgrade gcc"),
d003868e
AM
12226 input_bfd,
12227 input_section,
ad8e1ba5
AM
12228 (long) rel->r_offset);
12229 else
12230 (*_bfd_error_handler)
d003868e 12231 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
ad8e1ba5
AM
12232 "does not allow automatic multiple TOCs; "
12233 "recompile with -mminimal-toc or "
12234 "-fno-optimize-sibling-calls, "
12235 "or make `%s' extern"),
d003868e
AM
12236 input_bfd,
12237 input_section,
ad8e1ba5
AM
12238 (long) rel->r_offset,
12239 sym_name,
12240 sym_name);
12241 bfd_set_error (bfd_error_bad_value);
12242 ret = FALSE;
12243 }
721956f4
AM
12244 }
12245
b25116a9
AM
12246 if (can_plt_call
12247 && stub_entry->stub_type == ppc_stub_plt_call)
12248 unresolved_reloc = FALSE;
12249 }
12250
6abec6bc
AM
12251 if ((stub_entry == NULL
12252 || stub_entry->stub_type == ppc_stub_long_branch
12253 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
12254 && get_opd_info (sec) != NULL)
12255 {
12256 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
12257 bfd_vma off = (relocation + addend
12258 - sec->output_section->vma
12259 - sec->output_offset);
8387904d
AM
12260 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12261 if (dest != (bfd_vma) -1)
12262 {
12263 relocation = dest;
12264 addend = 0;
12265 }
12266 }
12267
b25116a9
AM
12268 /* If the branch is out of reach we ought to have a long
12269 branch stub. */
12270 from = (rel->r_offset
12271 + input_section->output_offset
12272 + input_section->output_section->vma);
12273
6abec6bc
AM
12274 if (stub_entry != NULL
12275 && (stub_entry->stub_type == ppc_stub_long_branch
12276 || stub_entry->stub_type == ppc_stub_plt_branch)
12277 && (r_type == R_PPC64_ADDR14_BRTAKEN
12278 || r_type == R_PPC64_ADDR14_BRNTAKEN
12279 || (relocation + addend - from + max_br_offset
12280 < 2 * max_br_offset)))
12281 /* Don't use the stub if this branch is in range. */
12282 stub_entry = NULL;
b25116a9
AM
12283
12284 if (stub_entry != NULL)
12285 {
12286 /* Munge up the value and addend so that we call the stub
12287 rather than the procedure directly. */
12288 relocation = (stub_entry->stub_offset
12289 + stub_entry->stub_sec->output_offset
12290 + stub_entry->stub_sec->output_section->vma);
12291 addend = 0;
12292 }
12293
12294 if (insn != 0)
12295 {
12296 if (is_power4)
721956f4 12297 {
b25116a9
AM
12298 /* Set 'a' bit. This is 0b00010 in BO field for branch
12299 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12300 for branch on CTR insns (BO == 1a00t or 1a01t). */
12301 if ((insn & (0x14 << 21)) == (0x04 << 21))
12302 insn |= 0x02 << 21;
12303 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12304 insn |= 0x08 << 21;
12305 else
12306 break;
12307 }
12308 else
12309 {
12310 /* Invert 'y' bit if not the default. */
4cc603a5 12311 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 12312 insn ^= 0x01 << 21;
721956f4 12313 }
b25116a9
AM
12314
12315 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 12316 }
e86ce104 12317
06da1e8e
AM
12318 /* NOP out calls to undefined weak functions.
12319 We can thus call a weak function without first
12320 checking whether the function is defined. */
b25116a9 12321 else if (h != NULL
039b3fef 12322 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 12323 && h->elf.dynindx == -1
b25116a9
AM
12324 && r_type == R_PPC64_REL24
12325 && relocation == 0
4cc603a5 12326 && addend == 0)
e86ce104 12327 {
06da1e8e
AM
12328 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12329 continue;
e86ce104 12330 }
65f38f15
AM
12331 break;
12332 }
5bd4f169 12333
65f38f15 12334 /* Set `addend'. */
411e1bfb 12335 tls_type = 0;
65f38f15
AM
12336 switch (r_type)
12337 {
12338 default:
12339 (*_bfd_error_handler)
d003868e
AM
12340 (_("%B: unknown relocation type %d for symbol %s"),
12341 input_bfd, (int) r_type, sym_name);
5bd4f169 12342
65f38f15 12343 bfd_set_error (bfd_error_bad_value);
b34976b6 12344 ret = FALSE;
65f38f15 12345 continue;
5bd4f169 12346
65f38f15 12347 case R_PPC64_NONE:
411e1bfb 12348 case R_PPC64_TLS:
727fc41e
AM
12349 case R_PPC64_TLSGD:
12350 case R_PPC64_TLSLD:
04c9666a
AM
12351 case R_PPC64_GNU_VTINHERIT:
12352 case R_PPC64_GNU_VTENTRY:
65f38f15 12353 continue;
5bd4f169
AM
12354
12355 /* GOT16 relocations. Like an ADDR16 using the symbol's
12356 address in the GOT as relocation value instead of the
411e1bfb 12357 symbol's value itself. Also, create a GOT entry for the
5bd4f169 12358 symbol and put the symbol value there. */
411e1bfb
AM
12359 case R_PPC64_GOT_TLSGD16:
12360 case R_PPC64_GOT_TLSGD16_LO:
12361 case R_PPC64_GOT_TLSGD16_HI:
12362 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 12363 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
12364 goto dogot;
12365
12366 case R_PPC64_GOT_TLSLD16:
12367 case R_PPC64_GOT_TLSLD16_LO:
12368 case R_PPC64_GOT_TLSLD16_HI:
12369 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12370 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
12371 goto dogot;
12372
12373 case R_PPC64_GOT_TPREL16_DS:
12374 case R_PPC64_GOT_TPREL16_LO_DS:
12375 case R_PPC64_GOT_TPREL16_HI:
12376 case R_PPC64_GOT_TPREL16_HA:
12377 tls_type = TLS_TLS | TLS_TPREL;
12378 goto dogot;
12379
12380 case R_PPC64_GOT_DTPREL16_DS:
12381 case R_PPC64_GOT_DTPREL16_LO_DS:
12382 case R_PPC64_GOT_DTPREL16_HI:
12383 case R_PPC64_GOT_DTPREL16_HA:
12384 tls_type = TLS_TLS | TLS_DTPREL;
12385 goto dogot;
12386
65f38f15
AM
12387 case R_PPC64_GOT16:
12388 case R_PPC64_GOT16_LO:
12389 case R_PPC64_GOT16_HI:
12390 case R_PPC64_GOT16_HA:
12391 case R_PPC64_GOT16_DS:
12392 case R_PPC64_GOT16_LO_DS:
411e1bfb 12393 dogot:
5bd4f169
AM
12394 {
12395 /* Relocation is to the entry for this symbol in the global
12396 offset table. */
e717da7e 12397 asection *got;
d881513a 12398 bfd_vma *offp;
5bd4f169 12399 bfd_vma off;
d881513a 12400 unsigned long indx = 0;
927be08e 12401 struct got_entry *ent;
65f38f15 12402
d881513a
AM
12403 if (tls_type == (TLS_TLS | TLS_LD)
12404 && (h == NULL
f5385ebf 12405 || !h->elf.def_dynamic))
927be08e 12406 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 12407 else
5bd4f169 12408 {
5bd4f169 12409
d881513a
AM
12410 if (h != NULL)
12411 {
12412 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
12413 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12414 &h->elf)
d881513a 12415 || (info->shared
726d3ab0 12416 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
12417 /* This is actually a static link, or it is a
12418 -Bsymbolic link and the symbol is defined
12419 locally, or the symbol was forced to be local
12420 because of a version file. */
12421 ;
12422 else
12423 {
039b3fef 12424 indx = h->elf.dynindx;
d881513a
AM
12425 unresolved_reloc = FALSE;
12426 }
039b3fef 12427 ent = h->elf.got.glist;
d881513a 12428 }
411e1bfb 12429 else
5bd4f169 12430 {
d881513a
AM
12431 if (local_got_ents == NULL)
12432 abort ();
12433 ent = local_got_ents[r_symndx];
5bd4f169 12434 }
d881513a
AM
12435
12436 for (; ent != NULL; ent = ent->next)
4cc603a5 12437 if (ent->addend == orig_addend
e717da7e 12438 && ent->owner == input_bfd
d881513a
AM
12439 && ent->tls_type == tls_type)
12440 break;
5bd4f169 12441 }
411e1bfb 12442
927be08e
AM
12443 if (ent == NULL)
12444 abort ();
12445 if (ent->is_indirect)
12446 ent = ent->got.ent;
12447 offp = &ent->got.offset;
12448 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
12449 if (got == NULL)
12450 abort ();
12451
411e1bfb
AM
12452 /* The offset must always be a multiple of 8. We use the
12453 least significant bit to record whether we have already
12454 processed this entry. */
d881513a 12455 off = *offp;
411e1bfb
AM
12456 if ((off & 1) != 0)
12457 off &= ~1;
5bd4f169
AM
12458 else
12459 {
411e1bfb
AM
12460 /* Generate relocs for the dynamic linker, except in
12461 the case of TLSLD where we'll use one entry per
12462 module. */
25f23106
AM
12463 asection *relgot;
12464 bfd_boolean ifunc;
e717da7e 12465
d881513a 12466 *offp = off | 1;
25f23106
AM
12467 relgot = NULL;
12468 ifunc = (h != NULL
12469 ? h->elf.type == STT_GNU_IFUNC
12470 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
4e795f50 12471 if ((info->shared || indx != 0)
927be08e
AM
12472 && (h == NULL
12473 || (tls_type == (TLS_TLS | TLS_LD)
12474 && !h->elf.def_dynamic)
039b3fef
AM
12475 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12476 || h->elf.root.type != bfd_link_hash_undefweak))
927be08e 12477 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106
AM
12478 else if (ifunc)
12479 relgot = htab->reliplt;
12480 if (relgot != NULL)
5bd4f169 12481 {
e717da7e
AM
12482 outrel.r_offset = (got->output_section->vma
12483 + got->output_offset
411e1bfb 12484 + off);
4cc603a5 12485 outrel.r_addend = addend;
d881513a 12486 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 12487 {
411e1bfb 12488 outrel.r_addend = 0;
e515b051 12489 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
12490 if (tls_type == (TLS_TLS | TLS_GD))
12491 {
e717da7e
AM
12492 loc = relgot->contents;
12493 loc += (relgot->reloc_count++
d881513a
AM
12494 * sizeof (Elf64_External_Rela));
12495 bfd_elf64_swap_reloca_out (output_bfd,
12496 &outrel, loc);
e515b051 12497 outrel.r_offset += 8;
4cc603a5 12498 outrel.r_addend = addend;
d881513a
AM
12499 outrel.r_info
12500 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 12501 }
411e1bfb 12502 }
951fd09b 12503 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 12504 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 12505 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12506 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
12507 else if (indx != 0)
12508 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12509 else
81407a69 12510 {
25f23106
AM
12511 if (ifunc)
12512 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12513 else
12514 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
12515
12516 /* Write the .got section contents for the sake
12517 of prelink. */
e717da7e 12518 loc = got->contents + off;
23fbd6fa
JJ
12519 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12520 loc);
81407a69 12521 }
81407a69
AM
12522
12523 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
12524 {
12525 outrel.r_addend += relocation;
12526 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 12527 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 12528 }
e717da7e
AM
12529 loc = relgot->contents;
12530 loc += (relgot->reloc_count++
411e1bfb
AM
12531 * sizeof (Elf64_External_Rela));
12532 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12533 }
12534
ad8e1ba5 12535 /* Init the .got section contents here if we're not
81407a69 12536 emitting a reloc. */
d881513a 12537 else
411e1bfb 12538 {
4cc603a5 12539 relocation += addend;
7b609f53
AM
12540 if (tls_type == (TLS_TLS | TLS_LD))
12541 relocation = 1;
12542 else if (tls_type != 0)
411e1bfb 12543 {
e1918d23 12544 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 12545 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12546 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 12547
7b609f53
AM
12548 if (tls_type == (TLS_TLS | TLS_GD))
12549 {
12550 bfd_put_64 (output_bfd, relocation,
e717da7e 12551 got->contents + off + 8);
7b609f53
AM
12552 relocation = 1;
12553 }
411e1bfb 12554 }
7b609f53 12555
411e1bfb 12556 bfd_put_64 (output_bfd, relocation,
e717da7e 12557 got->contents + off);
5bd4f169
AM
12558 }
12559 }
12560
65f38f15
AM
12561 if (off >= (bfd_vma) -2)
12562 abort ();
12563
bf102f86
AM
12564 relocation = got->output_section->vma + got->output_offset + off;
12565 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 12566 }
65f38f15
AM
12567 break;
12568
12569 case R_PPC64_PLT16_HA:
12570 case R_PPC64_PLT16_HI:
12571 case R_PPC64_PLT16_LO:
12572 case R_PPC64_PLT32:
12573 case R_PPC64_PLT64:
12574 /* Relocation is to the entry for this symbol in the
12575 procedure linkage table. */
12576
12577 /* Resolve a PLT reloc against a local symbol directly,
12578 without using the procedure linkage table. */
12579 if (h == NULL)
12580 break;
12581
411e1bfb
AM
12582 /* It's possible that we didn't make a PLT entry for this
12583 symbol. This happens when statically linking PIC code,
12584 or when using -Bsymbolic. Go find a match if there is a
12585 PLT entry. */
4ce794b7 12586 if (htab->plt != NULL)
65f38f15 12587 {
411e1bfb 12588 struct plt_entry *ent;
039b3fef 12589 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 12590 if (ent->addend == orig_addend
411e1bfb
AM
12591 && ent->plt.offset != (bfd_vma) -1)
12592 {
4ce794b7
AM
12593 relocation = (htab->plt->output_section->vma
12594 + htab->plt->output_offset
411e1bfb
AM
12595 + ent->plt.offset);
12596 unresolved_reloc = FALSE;
12597 }
65f38f15 12598 }
65f38f15 12599 break;
5bd4f169 12600
0b13192e
AM
12601 case R_PPC64_TOC:
12602 /* Relocation value is TOC base. */
12603 relocation = TOCstart;
cf35638d 12604 if (r_symndx == STN_UNDEF)
0b13192e 12605 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
12606 else if (unresolved_reloc)
12607 ;
12608 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
12609 relocation += htab->stub_group[sec->id].toc_off;
12610 else
12611 unresolved_reloc = TRUE;
ab96bf03 12612 goto dodyn;
0b13192e 12613
5bd4f169
AM
12614 /* TOC16 relocs. We want the offset relative to the TOC base,
12615 which is the address of the start of the TOC plus 0x8000.
12616 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12617 in this order. */
65f38f15
AM
12618 case R_PPC64_TOC16:
12619 case R_PPC64_TOC16_LO:
12620 case R_PPC64_TOC16_HI:
12621 case R_PPC64_TOC16_DS:
12622 case R_PPC64_TOC16_LO_DS:
12623 case R_PPC64_TOC16_HA:
ad8e1ba5 12624 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
12625 break;
12626
12627 /* Relocate against the beginning of the section. */
65f38f15
AM
12628 case R_PPC64_SECTOFF:
12629 case R_PPC64_SECTOFF_LO:
12630 case R_PPC64_SECTOFF_HI:
12631 case R_PPC64_SECTOFF_DS:
12632 case R_PPC64_SECTOFF_LO_DS:
12633 case R_PPC64_SECTOFF_HA:
4ce794b7 12634 if (sec != NULL)
65f38f15 12635 addend -= sec->output_section->vma;
5bd4f169
AM
12636 break;
12637
25f23106
AM
12638 case R_PPC64_REL16:
12639 case R_PPC64_REL16_LO:
12640 case R_PPC64_REL16_HI:
12641 case R_PPC64_REL16_HA:
12642 break;
12643
721956f4
AM
12644 case R_PPC64_REL14:
12645 case R_PPC64_REL14_BRNTAKEN:
12646 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
12647 case R_PPC64_REL24:
12648 break;
12649
411e1bfb
AM
12650 case R_PPC64_TPREL16:
12651 case R_PPC64_TPREL16_LO:
12652 case R_PPC64_TPREL16_HI:
12653 case R_PPC64_TPREL16_HA:
12654 case R_PPC64_TPREL16_DS:
12655 case R_PPC64_TPREL16_LO_DS:
12656 case R_PPC64_TPREL16_HIGHER:
12657 case R_PPC64_TPREL16_HIGHERA:
12658 case R_PPC64_TPREL16_HIGHEST:
12659 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
12660 if (h != NULL
12661 && h->elf.root.type == bfd_link_hash_undefweak
12662 && h->elf.dynindx == -1)
12663 {
12664 /* Make this relocation against an undefined weak symbol
12665 resolve to zero. This is really just a tweak, since
12666 code using weak externs ought to check that they are
12667 defined before using them. */
12668 bfd_byte *p = contents + rel->r_offset - d_offset;
12669
12670 insn = bfd_get_32 (output_bfd, p);
12671 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12672 if (insn != 0)
12673 bfd_put_32 (output_bfd, insn, p);
12674 break;
12675 }
e1918d23 12676 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12677 if (info->shared)
12678 /* The TPREL16 relocs shouldn't really be used in shared
12679 libs as they will result in DT_TEXTREL being set, but
12680 support them anyway. */
12681 goto dodyn;
12682 break;
12683
12684 case R_PPC64_DTPREL16:
12685 case R_PPC64_DTPREL16_LO:
12686 case R_PPC64_DTPREL16_HI:
12687 case R_PPC64_DTPREL16_HA:
12688 case R_PPC64_DTPREL16_DS:
12689 case R_PPC64_DTPREL16_LO_DS:
12690 case R_PPC64_DTPREL16_HIGHER:
12691 case R_PPC64_DTPREL16_HIGHERA:
12692 case R_PPC64_DTPREL16_HIGHEST:
12693 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 12694 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12695 break;
12696
e515b051
AM
12697 case R_PPC64_DTPMOD64:
12698 relocation = 1;
12699 addend = 0;
12700 goto dodyn;
12701
411e1bfb 12702 case R_PPC64_TPREL64:
e1918d23 12703 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12704 goto dodyn;
12705
12706 case R_PPC64_DTPREL64:
e1918d23 12707 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12708 /* Fall thru */
12709
65f38f15
AM
12710 /* Relocations that may need to be propagated if this is a
12711 dynamic object. */
04c9666a 12712 case R_PPC64_REL30:
65f38f15
AM
12713 case R_PPC64_REL32:
12714 case R_PPC64_REL64:
12715 case R_PPC64_ADDR14:
12716 case R_PPC64_ADDR14_BRNTAKEN:
12717 case R_PPC64_ADDR14_BRTAKEN:
12718 case R_PPC64_ADDR16:
12719 case R_PPC64_ADDR16_DS:
12720 case R_PPC64_ADDR16_HA:
12721 case R_PPC64_ADDR16_HI:
12722 case R_PPC64_ADDR16_HIGHER:
12723 case R_PPC64_ADDR16_HIGHERA:
12724 case R_PPC64_ADDR16_HIGHEST:
12725 case R_PPC64_ADDR16_HIGHESTA:
12726 case R_PPC64_ADDR16_LO:
12727 case R_PPC64_ADDR16_LO_DS:
12728 case R_PPC64_ADDR24:
65f38f15
AM
12729 case R_PPC64_ADDR32:
12730 case R_PPC64_ADDR64:
12731 case R_PPC64_UADDR16:
12732 case R_PPC64_UADDR32:
12733 case R_PPC64_UADDR64:
411e1bfb 12734 dodyn:
5d1634d7 12735 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
12736 break;
12737
41bd81ab
AM
12738 if (NO_OPD_RELOCS && is_opd)
12739 break;
12740
65f38f15 12741 if ((info->shared
4e795f50 12742 && (h == NULL
039b3fef
AM
12743 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12744 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 12745 && (must_be_dyn_reloc (info, r_type)
039b3fef 12746 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
12747 || (ELIMINATE_COPY_RELOCS
12748 && !info->shared
65f38f15 12749 && h != NULL
039b3fef 12750 && h->elf.dynindx != -1
f5385ebf 12751 && !h->elf.non_got_ref
25f23106
AM
12752 && !h->elf.def_regular)
12753 || (!info->shared
12754 && (h != NULL
12755 ? h->elf.type == STT_GNU_IFUNC
12756 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 12757 {
b34976b6 12758 bfd_boolean skip, relocate;
65f38f15 12759 asection *sreloc;
1cf1f670 12760 bfd_vma out_off;
65f38f15
AM
12761
12762 /* When generating a dynamic object, these relocations
12763 are copied into the output file to be resolved at run
12764 time. */
12765
b34976b6
AM
12766 skip = FALSE;
12767 relocate = FALSE;
65f38f15 12768
1cf1f670
AM
12769 out_off = _bfd_elf_section_offset (output_bfd, info,
12770 input_section, rel->r_offset);
12771 if (out_off == (bfd_vma) -1)
b34976b6 12772 skip = TRUE;
1cf1f670 12773 else if (out_off == (bfd_vma) -2)
b34976b6 12774 skip = TRUE, relocate = TRUE;
1cf1f670
AM
12775 out_off += (input_section->output_section->vma
12776 + input_section->output_offset);
12777 outrel.r_offset = out_off;
411e1bfb 12778 outrel.r_addend = rel->r_addend;
65f38f15 12779
1cf1f670
AM
12780 /* Optimize unaligned reloc use. */
12781 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12782 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12783 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12784 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12785 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12786 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12787 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12788 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12789 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12790
65f38f15 12791 if (skip)
0bb2d96a 12792 memset (&outrel, 0, sizeof outrel);
726d3ab0 12793 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
12794 && !is_opd
12795 && r_type != R_PPC64_TOC)
039b3fef 12796 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
12797 else
12798 {
41bd81ab
AM
12799 /* This symbol is local, or marked to become local,
12800 or this is an opd section reloc which must point
12801 at a local function. */
65f38f15 12802 outrel.r_addend += relocation;
e86ce104 12803 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 12804 {
3fad3c7c 12805 if (is_opd && h != NULL)
afbe61cf
AM
12806 {
12807 /* Lie about opd entries. This case occurs
12808 when building shared libraries and we
12809 reference a function in another shared
3fad3c7c
AM
12810 lib. The same thing happens for a weak
12811 definition in an application that's
12812 overridden by a strong definition in a
12813 shared lib. (I believe this is a generic
12814 bug in binutils handling of weak syms.)
12815 In these cases we won't use the opd
1e2f5b6e 12816 entry in this lib. */
b34976b6 12817 unresolved_reloc = FALSE;
afbe61cf 12818 }
25f23106
AM
12819 if (!is_opd
12820 && r_type == R_PPC64_ADDR64
12821 && (h != NULL
12822 ? h->elf.type == STT_GNU_IFUNC
12823 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12824 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12825 else
12826 {
12827 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 12828
25f23106
AM
12829 /* We need to relocate .opd contents for ld.so.
12830 Prelink also wants simple and consistent rules
12831 for relocs. This make all RELATIVE relocs have
12832 *r_offset equal to r_addend. */
12833 relocate = TRUE;
12834 }
65f38f15
AM
12835 }
12836 else
12837 {
12838 long indx = 0;
12839
25f23106
AM
12840 if (h != NULL
12841 ? h->elf.type == STT_GNU_IFUNC
12842 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12843 {
12844 (*_bfd_error_handler)
12845 (_("%B(%A+0x%lx): relocation %s for indirect "
12846 "function %s unsupported"),
12847 input_bfd,
12848 input_section,
12849 (long) rel->r_offset,
12850 ppc64_elf_howto_table[r_type]->name,
12851 sym_name);
12852 ret = FALSE;
12853 }
cf35638d 12854 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
12855 ;
12856 else if (sec == NULL || sec->owner == NULL)
12857 {
12858 bfd_set_error (bfd_error_bad_value);
b34976b6 12859 return FALSE;
65f38f15
AM
12860 }
12861 else
12862 {
12863 asection *osec;
12864
12865 osec = sec->output_section;
12866 indx = elf_section_data (osec)->dynindx;
12867
74541ad4
AM
12868 if (indx == 0)
12869 {
12870 if ((osec->flags & SEC_READONLY) == 0
12871 && htab->elf.data_index_section != NULL)
12872 osec = htab->elf.data_index_section;
12873 else
12874 osec = htab->elf.text_index_section;
12875 indx = elf_section_data (osec)->dynindx;
12876 }
12877 BFD_ASSERT (indx != 0);
12878
65f38f15
AM
12879 /* We are turning this relocation into one
12880 against a section symbol, so subtract out
12881 the output section's address but not the
12882 offset of the input section in the output
12883 section. */
12884 outrel.r_addend -= osec->vma;
12885 }
12886
12887 outrel.r_info = ELF64_R_INFO (indx, r_type);
12888 }
12889 }
12890
12891 sreloc = elf_section_data (input_section)->sreloc;
25f23106
AM
12892 if (!htab->elf.dynamic_sections_created)
12893 sreloc = htab->reliplt;
65f38f15
AM
12894 if (sreloc == NULL)
12895 abort ();
12896
dfbb6ac9
AM
12897 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12898 >= sreloc->size)
12899 abort ();
947216bf
AM
12900 loc = sreloc->contents;
12901 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
12902 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12903
12904 /* If this reloc is against an external symbol, it will
12905 be computed at runtime, so there's no need to do
81407a69
AM
12906 anything now. However, for the sake of prelink ensure
12907 that the section contents are a known value. */
65f38f15 12908 if (! relocate)
81407a69
AM
12909 {
12910 unresolved_reloc = FALSE;
12911 /* The value chosen here is quite arbitrary as ld.so
12912 ignores section contents except for the special
12913 case of .opd where the contents might be accessed
12914 before relocation. Choose zero, as that won't
12915 cause reloc overflow. */
12916 relocation = 0;
12917 addend = 0;
12918 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12919 to improve backward compatibility with older
12920 versions of ld. */
12921 if (r_type == R_PPC64_ADDR64)
12922 addend = outrel.r_addend;
12923 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 12924 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
12925 addend = (input_section->output_section->vma
12926 + input_section->output_offset
12927 + rel->r_offset);
81407a69 12928 }
65f38f15 12929 }
5bd4f169
AM
12930 break;
12931
65f38f15
AM
12932 case R_PPC64_COPY:
12933 case R_PPC64_GLOB_DAT:
12934 case R_PPC64_JMP_SLOT:
25f23106 12935 case R_PPC64_JMP_IREL:
65f38f15
AM
12936 case R_PPC64_RELATIVE:
12937 /* We shouldn't ever see these dynamic relocs in relocatable
12938 files. */
ae9a127f 12939 /* Fall through. */
65f38f15
AM
12940
12941 case R_PPC64_PLTGOT16:
12942 case R_PPC64_PLTGOT16_DS:
12943 case R_PPC64_PLTGOT16_HA:
12944 case R_PPC64_PLTGOT16_HI:
12945 case R_PPC64_PLTGOT16_LO:
12946 case R_PPC64_PLTGOT16_LO_DS:
12947 case R_PPC64_PLTREL32:
12948 case R_PPC64_PLTREL64:
12949 /* These ones haven't been implemented yet. */
12950
12951 (*_bfd_error_handler)
d003868e
AM
12952 (_("%B: relocation %s is not supported for symbol %s."),
12953 input_bfd,
4ce794b7 12954 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
12955
12956 bfd_set_error (bfd_error_invalid_operation);
b34976b6 12957 ret = FALSE;
5bd4f169 12958 continue;
65f38f15 12959 }
5bd4f169 12960
67f0cbdb
AM
12961 /* Multi-instruction sequences that access the TOC can be
12962 optimized, eg. addis ra,r2,0; addi rb,ra,x;
12963 to nop; addi rb,r2,x; */
12964 switch (r_type)
12965 {
12966 default:
12967 break;
12968
12969 case R_PPC64_GOT_TLSLD16_HI:
12970 case R_PPC64_GOT_TLSGD16_HI:
12971 case R_PPC64_GOT_TPREL16_HI:
12972 case R_PPC64_GOT_DTPREL16_HI:
12973 case R_PPC64_GOT16_HI:
12974 case R_PPC64_TOC16_HI:
12975 /* These relocs would only be useful if building up an
12976 offset to later add to r2, perhaps in an indexed
12977 addressing mode instruction. Don't try to optimize.
12978 Unfortunately, the possibility of someone building up an
12979 offset like this or even with the HA relocs, means that
12980 we need to check the high insn when optimizing the low
12981 insn. */
12982 break;
12983
12984 case R_PPC64_GOT_TLSLD16_HA:
12985 case R_PPC64_GOT_TLSGD16_HA:
12986 case R_PPC64_GOT_TPREL16_HA:
12987 case R_PPC64_GOT_DTPREL16_HA:
12988 case R_PPC64_GOT16_HA:
12989 case R_PPC64_TOC16_HA:
d983b7a0 12990 /* nop is done later. */
67f0cbdb
AM
12991 break;
12992
12993 case R_PPC64_GOT_TLSLD16_LO:
12994 case R_PPC64_GOT_TLSGD16_LO:
12995 case R_PPC64_GOT_TPREL16_LO_DS:
12996 case R_PPC64_GOT_DTPREL16_LO_DS:
12997 case R_PPC64_GOT16_LO:
12998 case R_PPC64_GOT16_LO_DS:
12999 case R_PPC64_TOC16_LO:
13000 case R_PPC64_TOC16_LO_DS:
13001 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
13002 {
13003 bfd_byte *p = contents + (rel->r_offset & ~3);
13004 insn = bfd_get_32 (input_bfd, p);
13005 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13006 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13007 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13008 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13009 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13010 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13011 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13012 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13013 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13014 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13015 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13016 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13017 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13018 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13019 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13020 && (insn & 3) != 1)
13021 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13022 && ((insn & 3) == 0 || (insn & 3) == 3)))
13023 {
13024 unsigned int reg = (insn >> 16) & 0x1f;
d983b7a0
AM
13025 const Elf_Internal_Rela *ha;
13026 bfd_boolean match_addend;
13027
13028 match_addend = (sym != NULL
13029 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13030 ha = ha_reloc_match (relocs, rel, &reg, match_addend,
13031 input_bfd, contents);
13032 if (ha != NULL)
67f0cbdb
AM
13033 {
13034 insn &= ~(0x1f << 16);
d983b7a0 13035 insn |= reg << 16;
67f0cbdb 13036 bfd_put_32 (input_bfd, insn, p);
d983b7a0
AM
13037 if (ha_opt == NULL)
13038 {
13039 ha_opt = bfd_zmalloc (input_section->reloc_count);
13040 if (ha_opt == NULL)
13041 return FALSE;
13042 }
13043 ha_opt[ha - relocs] = 1;
67f0cbdb 13044 }
d983b7a0
AM
13045 else
13046 /* If we don't find a matching high part insn,
13047 something is fishy. Refuse to nop any high
13048 part insn in this section. */
13049 no_ha_opt = TRUE;
67f0cbdb
AM
13050 }
13051 }
13052 break;
13053 }
13054
65f38f15
AM
13055 /* Do any further special processing. */
13056 switch (r_type)
13057 {
13058 default:
13059 break;
13060
13061 case R_PPC64_ADDR16_HA:
25f23106 13062 case R_PPC64_REL16_HA:
65f38f15
AM
13063 case R_PPC64_ADDR16_HIGHERA:
13064 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
13065 case R_PPC64_TOC16_HA:
13066 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
13067 case R_PPC64_TPREL16_HA:
13068 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
13069 case R_PPC64_TPREL16_HIGHER:
13070 case R_PPC64_TPREL16_HIGHERA:
13071 case R_PPC64_TPREL16_HIGHEST:
13072 case R_PPC64_TPREL16_HIGHESTA:
13073 case R_PPC64_DTPREL16_HIGHER:
13074 case R_PPC64_DTPREL16_HIGHERA:
13075 case R_PPC64_DTPREL16_HIGHEST:
13076 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
13077 /* It's just possible that this symbol is a weak symbol
13078 that's not actually defined anywhere. In that case,
13079 'sec' would be NULL, and we should leave the symbol
13080 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
13081 if (sec == NULL)
13082 break;
13083 /* Fall thru */
13084
13085 case R_PPC64_GOT16_HA:
13086 case R_PPC64_PLTGOT16_HA:
13087 case R_PPC64_PLT16_HA:
13088 case R_PPC64_GOT_TLSGD16_HA:
13089 case R_PPC64_GOT_TLSLD16_HA:
13090 case R_PPC64_GOT_TPREL16_HA:
13091 case R_PPC64_GOT_DTPREL16_HA:
13092 /* Add 0x10000 if sign bit in 0:15 is set.
13093 Bits 0:15 are not used. */
13094 addend += 0x8000;
65f38f15
AM
13095 break;
13096
13097 case R_PPC64_ADDR16_DS:
13098 case R_PPC64_ADDR16_LO_DS:
13099 case R_PPC64_GOT16_DS:
13100 case R_PPC64_GOT16_LO_DS:
13101 case R_PPC64_PLT16_LO_DS:
13102 case R_PPC64_SECTOFF_DS:
13103 case R_PPC64_SECTOFF_LO_DS:
13104 case R_PPC64_TOC16_DS:
13105 case R_PPC64_TOC16_LO_DS:
13106 case R_PPC64_PLTGOT16_DS:
13107 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
13108 case R_PPC64_GOT_TPREL16_DS:
13109 case R_PPC64_GOT_TPREL16_LO_DS:
13110 case R_PPC64_GOT_DTPREL16_DS:
13111 case R_PPC64_GOT_DTPREL16_LO_DS:
13112 case R_PPC64_TPREL16_DS:
13113 case R_PPC64_TPREL16_LO_DS:
13114 case R_PPC64_DTPREL16_DS:
13115 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
13116 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13117 mask = 3;
13118 /* If this reloc is against an lq insn, then the value must be
13119 a multiple of 16. This is somewhat of a hack, but the
13120 "correct" way to do this by defining _DQ forms of all the
13121 _DS relocs bloats all reloc switches in this file. It
13122 doesn't seem to make much sense to use any of these relocs
13123 in data, so testing the insn should be safe. */
494dac0c 13124 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
13125 mask = 15;
13126 if (((relocation + addend) & mask) != 0)
65f38f15
AM
13127 {
13128 (*_bfd_error_handler)
d003868e
AM
13129 (_("%B: error: relocation %s not a multiple of %d"),
13130 input_bfd,
4ce794b7 13131 ppc64_elf_howto_table[r_type]->name,
adadcc0c 13132 mask + 1);
65f38f15 13133 bfd_set_error (bfd_error_bad_value);
b34976b6 13134 ret = FALSE;
65f38f15
AM
13135 continue;
13136 }
13137 break;
5bd4f169
AM
13138 }
13139
239e1f3a
AM
13140 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13141 because such sections are not SEC_ALLOC and thus ld.so will
13142 not process them. */
65f38f15 13143 if (unresolved_reloc
239e1f3a 13144 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 13145 && h->elf.def_dynamic))
9c07fe7c
AM
13146 {
13147 (*_bfd_error_handler)
d003868e
AM
13148 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
13149 input_bfd,
13150 input_section,
9c07fe7c 13151 (long) rel->r_offset,
7b609f53 13152 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 13153 h->elf.root.root.string);
b34976b6 13154 ret = FALSE;
9c07fe7c 13155 }
5bd4f169 13156
65f38f15 13157 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
13158 input_bfd,
13159 input_section,
13160 contents,
411e1bfb 13161 rel->r_offset,
5bd4f169
AM
13162 relocation,
13163 addend);
13164
ef60b7ff 13165 if (r != bfd_reloc_ok)
5bd4f169 13166 {
cd27b276
AM
13167 if (sym_name == NULL)
13168 sym_name = "(null)";
13169 if (r == bfd_reloc_overflow)
5bd4f169 13170 {
cd27b276
AM
13171 if (warned)
13172 continue;
13173 if (h != NULL
039b3fef 13174 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 13175 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
13176 {
13177 /* Assume this is a call protected by other code that
13178 detects the symbol is undefined. If this is the case,
13179 we can safely ignore the overflow. If not, the
13180 program is hosed anyway, and a little warning isn't
13181 going to help. */
13182
13183 continue;
13184 }
13185
ef60b7ff 13186 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
13187 (info, (h ? &h->elf.root : NULL), sym_name,
13188 ppc64_elf_howto_table[r_type]->name,
4cc603a5 13189 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 13190 return FALSE;
ef60b7ff
AM
13191 }
13192 else
13193 {
13194 (*_bfd_error_handler)
d003868e
AM
13195 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
13196 input_bfd,
13197 input_section,
7b609f53 13198 (long) rel->r_offset,
4ce794b7 13199 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
13200 sym_name,
13201 (int) r);
b34976b6 13202 ret = FALSE;
ef60b7ff 13203 }
5bd4f169 13204 }
5bd4f169
AM
13205 }
13206
d983b7a0
AM
13207 if (ha_opt != NULL)
13208 {
13209 if (!no_ha_opt)
13210 {
13211 unsigned char *opt = ha_opt;
13212 rel = relocs;
13213 relend = relocs + input_section->reloc_count;
13214 for (; rel < relend; opt++, rel++)
13215 if (*opt != 0)
13216 {
13217 bfd_byte *p = contents + (rel->r_offset & ~3);
13218 bfd_put_32 (input_bfd, NOP, p);
13219 }
13220 }
13221 free (ha_opt);
13222 }
13223
645ea6a9
AM
13224 /* If we're emitting relocations, then shortly after this function
13225 returns, reloc offsets and addends for this section will be
13226 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
13227 file rather than the input. Save a copy of the relocs for
13228 opd_entry_value. */
4cc603a5 13229 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
13230 {
13231 bfd_size_type amt;
13232 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13233 rel = bfd_alloc (input_bfd, amt);
13234 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13235 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13236 if (rel == NULL)
13237 return FALSE;
13238 memcpy (rel, relocs, amt);
13239 }
5bd4f169
AM
13240 return ret;
13241}
13242
754021d0
AM
13243/* Adjust the value of any local symbols in opd sections. */
13244
6e0b88f1 13245static int
754021d0
AM
13246ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13247 const char *name ATTRIBUTE_UNUSED,
13248 Elf_Internal_Sym *elfsym,
13249 asection *input_sec,
13250 struct elf_link_hash_entry *h)
13251{
74f0fb50
AM
13252 struct _opd_sec_data *opd;
13253 long adjust;
754021d0
AM
13254 bfd_vma value;
13255
4025353c 13256 if (h != NULL)
6e0b88f1 13257 return 1;
4025353c 13258
74f0fb50
AM
13259 opd = get_opd_info (input_sec);
13260 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 13261 return 1;
754021d0
AM
13262
13263 value = elfsym->st_value - input_sec->output_offset;
13264 if (!info->relocatable)
13265 value -= input_sec->output_section->vma;
13266
74f0fb50 13267 adjust = opd->adjust[value / 8];
4025353c 13268 if (adjust == -1)
6e0b88f1
AM
13269 return 2;
13270
13271 elfsym->st_value += adjust;
13272 return 1;
754021d0
AM
13273}
13274
5bd4f169
AM
13275/* Finish up dynamic symbol handling. We set the contents of various
13276 dynamic sections here. */
13277
b34976b6 13278static bfd_boolean
4ce794b7
AM
13279ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13280 struct bfd_link_info *info,
13281 struct elf_link_hash_entry *h,
13282 Elf_Internal_Sym *sym)
5bd4f169 13283{
65f38f15 13284 struct ppc_link_hash_table *htab;
8387904d
AM
13285 struct plt_entry *ent;
13286 Elf_Internal_Rela rela;
13287 bfd_byte *loc;
5bd4f169 13288
65f38f15 13289 htab = ppc_hash_table (info);
4dfe6ac6
NC
13290 if (htab == NULL)
13291 return FALSE;
5bd4f169 13292
8387904d
AM
13293 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13294 if (ent->plt.offset != (bfd_vma) -1)
13295 {
13296 /* This symbol has an entry in the procedure linkage
13297 table. Set it up. */
e054468f
AM
13298 if (!htab->elf.dynamic_sections_created
13299 || h->dynindx == -1)
13300 {
13301 BFD_ASSERT (h->type == STT_GNU_IFUNC
13302 && h->def_regular
13303 && (h->root.type == bfd_link_hash_defined
13304 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
13305 rela.r_offset = (htab->iplt->output_section->vma
13306 + htab->iplt->output_offset
13307 + ent->plt.offset);
13308 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
13309 rela.r_addend = (h->root.u.def.value
13310 + h->root.u.def.section->output_offset
13311 + h->root.u.def.section->output_section->vma
13312 + ent->addend);
25f23106
AM
13313 loc = (htab->reliplt->contents
13314 + (htab->reliplt->reloc_count++
13315 * sizeof (Elf64_External_Rela)));
e054468f
AM
13316 }
13317 else
13318 {
25f23106
AM
13319 rela.r_offset = (htab->plt->output_section->vma
13320 + htab->plt->output_offset
13321 + ent->plt.offset);
e054468f
AM
13322 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13323 rela.r_addend = ent->addend;
25f23106
AM
13324 loc = (htab->relplt->contents
13325 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13326 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 13327 }
8387904d
AM
13328 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13329 }
5bd4f169 13330
f5385ebf 13331 if (h->needs_copy)
5bd4f169 13332 {
65f38f15 13333 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 13334
65f38f15
AM
13335 if (h->dynindx == -1
13336 || (h->root.type != bfd_link_hash_defined
13337 && h->root.type != bfd_link_hash_defweak)
4ce794b7 13338 || htab->relbss == NULL)
65f38f15 13339 abort ();
5bd4f169
AM
13340
13341 rela.r_offset = (h->root.u.def.value
13342 + h->root.u.def.section->output_section->vma
13343 + h->root.u.def.section->output_offset);
13344 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13345 rela.r_addend = 0;
4ce794b7
AM
13346 loc = htab->relbss->contents;
13347 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 13348 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
13349 }
13350
5bd4f169
AM
13351 /* Mark some specially defined symbols as absolute. */
13352 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13353 sym->st_shndx = SHN_ABS;
13354
b34976b6 13355 return TRUE;
5bd4f169
AM
13356}
13357
65f38f15
AM
13358/* Used to decide how to sort relocs in an optimal manner for the
13359 dynamic linker, before writing them out. */
13360
13361static enum elf_reloc_type_class
4ce794b7 13362ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 13363{
04c9666a 13364 enum elf_ppc64_reloc_type r_type;
a33d1f77 13365
4ce794b7 13366 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 13367 switch (r_type)
65f38f15
AM
13368 {
13369 case R_PPC64_RELATIVE:
13370 return reloc_class_relative;
13371 case R_PPC64_JMP_SLOT:
13372 return reloc_class_plt;
13373 case R_PPC64_COPY:
13374 return reloc_class_copy;
13375 default:
13376 return reloc_class_normal;
13377 }
13378}
13379
5bd4f169
AM
13380/* Finish up the dynamic sections. */
13381
b34976b6 13382static bfd_boolean
4ce794b7
AM
13383ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13384 struct bfd_link_info *info)
5bd4f169 13385{
65f38f15
AM
13386 struct ppc_link_hash_table *htab;
13387 bfd *dynobj;
5bd4f169 13388 asection *sdyn;
5bd4f169 13389
65f38f15 13390 htab = ppc_hash_table (info);
4dfe6ac6
NC
13391 if (htab == NULL)
13392 return FALSE;
13393
65f38f15 13394 dynobj = htab->elf.dynobj;
5bd4f169
AM
13395 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13396
65f38f15 13397 if (htab->elf.dynamic_sections_created)
5bd4f169 13398 {
5bd4f169
AM
13399 Elf64_External_Dyn *dyncon, *dynconend;
13400
4ce794b7 13401 if (sdyn == NULL || htab->got == NULL)
65f38f15 13402 abort ();
5bd4f169
AM
13403
13404 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 13405 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
13406 for (; dyncon < dynconend; dyncon++)
13407 {
13408 Elf_Internal_Dyn dyn;
19397422 13409 asection *s;
5bd4f169
AM
13410
13411 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13412
13413 switch (dyn.d_tag)
13414 {
65f38f15
AM
13415 default:
13416 continue;
5bd4f169 13417
5d1634d7 13418 case DT_PPC64_GLINK:
4ce794b7 13419 s = htab->glink;
6348e046 13420 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
13421 /* We stupidly defined DT_PPC64_GLINK to be the start
13422 of glink rather than the first entry point, which is
13423 what ld.so needs, and now have a bigger stub to
13424 support automatic multiple TOCs. */
13425 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
13426 break;
13427
19397422
AM
13428 case DT_PPC64_OPD:
13429 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13430 if (s == NULL)
13431 continue;
13432 dyn.d_un.d_ptr = s->vma;
19397422
AM
13433 break;
13434
13435 case DT_PPC64_OPDSZ:
13436 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13437 if (s == NULL)
13438 continue;
eea6121a 13439 dyn.d_un.d_val = s->size;
19397422
AM
13440 break;
13441
65f38f15 13442 case DT_PLTGOT:
4ce794b7 13443 s = htab->plt;
6348e046 13444 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
13445 break;
13446
13447 case DT_JMPREL:
4ce794b7 13448 s = htab->relplt;
6348e046 13449 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 13450 break;
5bd4f169 13451
65f38f15 13452 case DT_PLTRELSZ:
eea6121a 13453 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
13454 break;
13455
13456 case DT_RELASZ:
13457 /* Don't count procedure linkage table relocs in the
13458 overall reloc count. */
4ce794b7 13459 s = htab->relplt;
6348e046
AM
13460 if (s == NULL)
13461 continue;
eea6121a 13462 dyn.d_un.d_val -= s->size;
6348e046
AM
13463 break;
13464
13465 case DT_RELA:
13466 /* We may not be using the standard ELF linker script.
13467 If .rela.plt is the first .rela section, we adjust
13468 DT_RELA to not include it. */
4ce794b7 13469 s = htab->relplt;
6348e046
AM
13470 if (s == NULL)
13471 continue;
13472 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13473 continue;
eea6121a 13474 dyn.d_un.d_ptr += s->size;
65f38f15 13475 break;
5bd4f169 13476 }
5bd4f169 13477
65f38f15 13478 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 13479 }
5bd4f169
AM
13480 }
13481
eea6121a 13482 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
13483 {
13484 /* Fill in the first entry in the global offset table.
13485 We use it to hold the link-time TOCbase. */
13486 bfd_put_64 (output_bfd,
60ee0d4a 13487 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 13488 htab->got->contents);
5d1634d7
AM
13489
13490 /* Set .got entry size. */
4ce794b7 13491 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
13492 }
13493
eea6121a 13494 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
13495 {
13496 /* Set .plt entry size. */
4ce794b7 13497 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
13498 = PLT_ENTRY_SIZE;
13499 }
13500
84f5d08e
AM
13501 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13502 brlt ourselves if emitrelocations. */
13503 if (htab->brlt != NULL
13504 && htab->brlt->reloc_count != 0
13505 && !_bfd_elf_link_output_relocs (output_bfd,
13506 htab->brlt,
d4730f92 13507 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
13508 elf_section_data (htab->brlt)->relocs,
13509 NULL))
13510 return FALSE;
13511
176a0d42
AM
13512 if (htab->glink != NULL
13513 && htab->glink->reloc_count != 0
13514 && !_bfd_elf_link_output_relocs (output_bfd,
13515 htab->glink,
d4730f92 13516 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
13517 elf_section_data (htab->glink)->relocs,
13518 NULL))
13519 return FALSE;
13520
e717da7e 13521 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
13522 since we didn't add them to DYNOBJ. We know dynobj is the first
13523 bfd. */
e717da7e
AM
13524 while ((dynobj = dynobj->link_next) != NULL)
13525 {
13526 asection *s;
7b53ace3 13527
0c8d6e5c 13528 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
13529 continue;
13530
e717da7e
AM
13531 s = ppc64_elf_tdata (dynobj)->got;
13532 if (s != NULL
eea6121a 13533 && s->size != 0
e717da7e
AM
13534 && s->output_section != bfd_abs_section_ptr
13535 && !bfd_set_section_contents (output_bfd, s->output_section,
13536 s->contents, s->output_offset,
eea6121a 13537 s->size))
e717da7e
AM
13538 return FALSE;
13539 s = ppc64_elf_tdata (dynobj)->relgot;
13540 if (s != NULL
eea6121a 13541 && s->size != 0
e717da7e
AM
13542 && s->output_section != bfd_abs_section_ptr
13543 && !bfd_set_section_contents (output_bfd, s->output_section,
13544 s->contents, s->output_offset,
eea6121a 13545 s->size))
e717da7e
AM
13546 return FALSE;
13547 }
f6c52c13 13548
b34976b6 13549 return TRUE;
5bd4f169
AM
13550}
13551
5bd4f169 13552#include "elf64-target.h"