* emultempl/elf32.em (gld${EMULATION_NAME}_add_options,
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
3a71aa26 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
df22d223 3 2009, 2010, 2011 Free Software Foundation, Inc.
5bd4f169
AM
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 6 Largely rewritten by Alan Modra.
5bd4f169 7
ae9a127f 8 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 9
ae9a127f
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
ae9a127f 13 (at your option) any later version.
5bd4f169 14
ae9a127f
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
5bd4f169 19
4ce794b7
AM
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 23
cd123cb7 24
4ce794b7
AM
25/* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 28
3db64b00 29#include "sysdep.h"
183e98be 30#include <stdarg.h>
5bd4f169 31#include "bfd.h"
5bd4f169
AM
32#include "bfdlink.h"
33#include "libbfd.h"
34#include "elf-bfd.h"
04c9666a 35#include "elf/ppc64.h"
5d1634d7 36#include "elf64-ppc.h"
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
aa374f67 82#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
c79d6685 83#define bfd_elf64_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
ad8e1ba5
AM
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
aa374f67 88#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
89
90#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
91#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
92#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 93#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
94#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
95#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 96#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 97#define elf_backend_check_directives ppc64_elf_process_dot_syms
97fed1c9 98#define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
8387904d 99#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 100#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 101#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 102#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
103#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
104#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
105#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
106#define elf_backend_hide_symbol ppc64_elf_hide_symbol
107#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
108#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 109#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 110#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
111#define elf_backend_relocate_section ppc64_elf_relocate_section
112#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
113#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
114#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 115#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 116#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 117#define elf_backend_post_process_headers _bfd_elf_set_osabi
ad8e1ba5 118
5bd4f169
AM
119/* The name of the dynamic interpreter. This is put in the .interp
120 section. */
121#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
122
123/* The size in bytes of an entry in the procedure linkage table. */
124#define PLT_ENTRY_SIZE 24
125
126/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 127#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
128
129/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
130#define TOC_BASE_OFF 0x8000
131
132/* Offset of tp and dtp pointers from start of TLS block. */
133#define TP_OFFSET 0x7000
134#define DTP_OFFSET 0x8000
5bd4f169 135
ad8e1ba5
AM
136/* .plt call stub instructions. The normal stub is like this, but
137 sometimes the .plt entry crosses a 64k boundary and we need to
ac2df442 138 insert an addi to adjust r12. */
ad8e1ba5 139#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7
AM
140#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
141#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
142#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
5d1634d7 143#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
ac2df442 144#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
5d1634d7
AM
145 /* ld %r11,xxx+16@l(%r12) */
146#define BCTR 0x4e800420 /* bctr */
147
5d1634d7 148
ee4bf8d2 149#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
ac2df442 150#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
ad8e1ba5
AM
151#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
152#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
153
ac2df442
AM
154#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
155#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
156
ad8e1ba5
AM
157#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
158
ee4bf8d2 159/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 160#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
161 /* 0: */
162 /* .quad plt0-1f */
163 /* __glink: */
164#define MFLR_R12 0x7d8802a6 /* mflr %12 */
165#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
166 /* 1: */
167#define MFLR_R11 0x7d6802a6 /* mflr %11 */
168#define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
169#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
170#define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
171 /* ld %11,0(%12) */
172 /* ld %2,8(%12) */
173 /* mtctr %11 */
174 /* ld %11,16(%12) */
175 /* bctr */
5d1634d7
AM
176
177/* Pad with this. */
178#define NOP 0x60000000
179
721956f4
AM
180/* Some other nops. */
181#define CROR_151515 0x4def7b82
182#define CROR_313131 0x4ffffb82
183
cedb70c5 184/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
185#define LI_R0_0 0x38000000 /* li %r0,0 */
186#define B_DOT 0x48000000 /* b . */
187
188/* After that, we need two instructions to load the index, followed by
189 a branch. */
190#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 191#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 192
deb0e272
AM
193/* Instructions used by the save and restore reg functions. */
194#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
195#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
196#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
197#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
198#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
199#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
200#define LI_R12_0 0x39800000 /* li %r12,0 */
201#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
202#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
203#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
204#define BLR 0x4e800020 /* blr */
205
41bd81ab
AM
206/* Since .opd is an array of descriptors and each entry will end up
207 with identical R_PPC64_RELATIVE relocs, there is really no need to
208 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 209 relocate .opd without reloc entries. */
41bd81ab
AM
210#ifndef NO_OPD_RELOCS
211#define NO_OPD_RELOCS 0
212#endif
5bd4f169 213\f
f5e87a1d 214#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 215
5bd4f169 216/* Relocation HOWTO's. */
04c9666a 217static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
218
219static reloc_howto_type ppc64_elf_howto_raw[] = {
220 /* This reloc does nothing. */
221 HOWTO (R_PPC64_NONE, /* type */
222 0, /* rightshift */
411e1bfb
AM
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
b34976b6 225 FALSE, /* pc_relative */
5bd4f169 226 0, /* bitpos */
f5e87a1d 227 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
228 bfd_elf_generic_reloc, /* special_function */
229 "R_PPC64_NONE", /* name */
b34976b6 230 FALSE, /* partial_inplace */
d006db6c 231 0, /* src_mask */
5bd4f169 232 0, /* dst_mask */
b34976b6 233 FALSE), /* pcrel_offset */
5bd4f169
AM
234
235 /* A standard 32 bit relocation. */
236 HOWTO (R_PPC64_ADDR32, /* type */
237 0, /* rightshift */
238 2, /* size (0 = byte, 1 = short, 2 = long) */
239 32, /* bitsize */
b34976b6 240 FALSE, /* pc_relative */
5bd4f169
AM
241 0, /* bitpos */
242 complain_overflow_bitfield, /* complain_on_overflow */
243 bfd_elf_generic_reloc, /* special_function */
244 "R_PPC64_ADDR32", /* name */
b34976b6 245 FALSE, /* partial_inplace */
5bd4f169
AM
246 0, /* src_mask */
247 0xffffffff, /* dst_mask */
b34976b6 248 FALSE), /* pcrel_offset */
5bd4f169
AM
249
250 /* An absolute 26 bit branch; the lower two bits must be zero.
251 FIXME: we don't check that, we just clear them. */
252 HOWTO (R_PPC64_ADDR24, /* type */
253 0, /* rightshift */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 26, /* bitsize */
b34976b6 256 FALSE, /* pc_relative */
5bd4f169
AM
257 0, /* bitpos */
258 complain_overflow_bitfield, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_PPC64_ADDR24", /* name */
b34976b6 261 FALSE, /* partial_inplace */
d006db6c 262 0, /* src_mask */
f5e87a1d 263 0x03fffffc, /* dst_mask */
b34976b6 264 FALSE), /* pcrel_offset */
5bd4f169
AM
265
266 /* A standard 16 bit relocation. */
267 HOWTO (R_PPC64_ADDR16, /* type */
268 0, /* rightshift */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
b34976b6 271 FALSE, /* pc_relative */
5bd4f169
AM
272 0, /* bitpos */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR16", /* name */
b34976b6 276 FALSE, /* partial_inplace */
5bd4f169
AM
277 0, /* src_mask */
278 0xffff, /* dst_mask */
b34976b6 279 FALSE), /* pcrel_offset */
5bd4f169
AM
280
281 /* A 16 bit relocation without overflow. */
282 HOWTO (R_PPC64_ADDR16_LO, /* type */
283 0, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
b34976b6 286 FALSE, /* pc_relative */
5bd4f169
AM
287 0, /* bitpos */
288 complain_overflow_dont,/* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC64_ADDR16_LO", /* name */
b34976b6 291 FALSE, /* partial_inplace */
5bd4f169
AM
292 0, /* src_mask */
293 0xffff, /* dst_mask */
b34976b6 294 FALSE), /* pcrel_offset */
5bd4f169
AM
295
296 /* Bits 16-31 of an address. */
297 HOWTO (R_PPC64_ADDR16_HI, /* type */
298 16, /* rightshift */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
300 16, /* bitsize */
b34976b6 301 FALSE, /* pc_relative */
5bd4f169
AM
302 0, /* bitpos */
303 complain_overflow_dont, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* special_function */
305 "R_PPC64_ADDR16_HI", /* name */
b34976b6 306 FALSE, /* partial_inplace */
5bd4f169
AM
307 0, /* src_mask */
308 0xffff, /* dst_mask */
b34976b6 309 FALSE), /* pcrel_offset */
5bd4f169
AM
310
311 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
312 bits, treated as a signed number, is negative. */
313 HOWTO (R_PPC64_ADDR16_HA, /* type */
314 16, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 16, /* bitsize */
b34976b6 317 FALSE, /* pc_relative */
5bd4f169
AM
318 0, /* bitpos */
319 complain_overflow_dont, /* complain_on_overflow */
805fc799 320 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 321 "R_PPC64_ADDR16_HA", /* name */
b34976b6 322 FALSE, /* partial_inplace */
5bd4f169
AM
323 0, /* src_mask */
324 0xffff, /* dst_mask */
b34976b6 325 FALSE), /* pcrel_offset */
5bd4f169
AM
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOWTO (R_PPC64_ADDR14, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
b34976b6 333 FALSE, /* pc_relative */
5bd4f169
AM
334 0, /* bitpos */
335 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 336 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 337 "R_PPC64_ADDR14", /* name */
b34976b6 338 FALSE, /* partial_inplace */
d006db6c 339 0, /* src_mask */
f5e87a1d 340 0x0000fffc, /* dst_mask */
b34976b6 341 FALSE), /* pcrel_offset */
5bd4f169
AM
342
343 /* An absolute 16 bit branch, for which bit 10 should be set to
344 indicate that the branch is expected to be taken. The lower two
345 bits must be zero. */
346 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
b34976b6 350 FALSE, /* pc_relative */
5bd4f169
AM
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 353 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 354 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 355 FALSE, /* partial_inplace */
d006db6c 356 0, /* src_mask */
f5e87a1d 357 0x0000fffc, /* dst_mask */
b34976b6 358 FALSE), /* pcrel_offset */
5bd4f169
AM
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
b34976b6 367 FALSE, /* pc_relative */
5bd4f169
AM
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 370 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 371 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 372 FALSE, /* partial_inplace */
d006db6c 373 0, /* src_mask */
f5e87a1d 374 0x0000fffc, /* dst_mask */
b34976b6 375 FALSE), /* pcrel_offset */
5bd4f169
AM
376
377 /* A relative 26 bit branch; the lower two bits must be zero. */
378 HOWTO (R_PPC64_REL24, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 26, /* bitsize */
b34976b6 382 TRUE, /* pc_relative */
5bd4f169
AM
383 0, /* bitpos */
384 complain_overflow_signed, /* complain_on_overflow */
2441e016 385 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 386 "R_PPC64_REL24", /* name */
b34976b6 387 FALSE, /* partial_inplace */
d006db6c 388 0, /* src_mask */
f5e87a1d 389 0x03fffffc, /* dst_mask */
b34976b6 390 TRUE), /* pcrel_offset */
5bd4f169
AM
391
392 /* A relative 16 bit branch; the lower two bits must be zero. */
393 HOWTO (R_PPC64_REL14, /* type */
394 0, /* rightshift */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
b34976b6 397 TRUE, /* pc_relative */
5bd4f169
AM
398 0, /* bitpos */
399 complain_overflow_signed, /* complain_on_overflow */
2441e016 400 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 401 "R_PPC64_REL14", /* name */
b34976b6 402 FALSE, /* partial_inplace */
d006db6c 403 0, /* src_mask */
f5e87a1d 404 0x0000fffc, /* dst_mask */
b34976b6 405 TRUE), /* pcrel_offset */
5bd4f169
AM
406
407 /* A relative 16 bit branch. Bit 10 should be set to indicate that
408 the branch is expected to be taken. The lower two bits must be
409 zero. */
410 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
411 0, /* rightshift */
412 2, /* size (0 = byte, 1 = short, 2 = long) */
413 16, /* bitsize */
b34976b6 414 TRUE, /* pc_relative */
5bd4f169
AM
415 0, /* bitpos */
416 complain_overflow_signed, /* complain_on_overflow */
805fc799 417 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 418 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 419 FALSE, /* partial_inplace */
d006db6c 420 0, /* src_mask */
f5e87a1d 421 0x0000fffc, /* dst_mask */
b34976b6 422 TRUE), /* pcrel_offset */
5bd4f169
AM
423
424 /* A relative 16 bit branch. Bit 10 should be set to indicate that
425 the branch is not expected to be taken. The lower two bits must
426 be zero. */
427 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
b34976b6 431 TRUE, /* pc_relative */
5bd4f169
AM
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
805fc799 434 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 435 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 436 FALSE, /* partial_inplace */
d006db6c 437 0, /* src_mask */
f5e87a1d 438 0x0000fffc, /* dst_mask */
b34976b6 439 TRUE), /* pcrel_offset */
5bd4f169
AM
440
441 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
442 symbol. */
443 HOWTO (R_PPC64_GOT16, /* type */
444 0, /* rightshift */
445 1, /* size (0 = byte, 1 = short, 2 = long) */
446 16, /* bitsize */
b34976b6 447 FALSE, /* pc_relative */
5bd4f169
AM
448 0, /* bitpos */
449 complain_overflow_signed, /* complain_on_overflow */
805fc799 450 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 451 "R_PPC64_GOT16", /* name */
b34976b6 452 FALSE, /* partial_inplace */
5bd4f169
AM
453 0, /* src_mask */
454 0xffff, /* dst_mask */
b34976b6 455 FALSE), /* pcrel_offset */
5bd4f169
AM
456
457 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
458 the symbol. */
459 HOWTO (R_PPC64_GOT16_LO, /* type */
460 0, /* rightshift */
461 1, /* size (0 = byte, 1 = short, 2 = long) */
462 16, /* bitsize */
b34976b6 463 FALSE, /* pc_relative */
5bd4f169
AM
464 0, /* bitpos */
465 complain_overflow_dont, /* complain_on_overflow */
805fc799 466 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 467 "R_PPC64_GOT16_LO", /* name */
b34976b6 468 FALSE, /* partial_inplace */
5bd4f169
AM
469 0, /* src_mask */
470 0xffff, /* dst_mask */
b34976b6 471 FALSE), /* pcrel_offset */
5bd4f169
AM
472
473 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
474 the symbol. */
475 HOWTO (R_PPC64_GOT16_HI, /* type */
476 16, /* rightshift */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
478 16, /* bitsize */
b34976b6 479 FALSE, /* pc_relative */
5bd4f169
AM
480 0, /* bitpos */
481 complain_overflow_dont,/* complain_on_overflow */
805fc799 482 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 483 "R_PPC64_GOT16_HI", /* name */
b34976b6 484 FALSE, /* partial_inplace */
5bd4f169
AM
485 0, /* src_mask */
486 0xffff, /* dst_mask */
b34976b6 487 FALSE), /* pcrel_offset */
5bd4f169
AM
488
489 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
490 the symbol. */
491 HOWTO (R_PPC64_GOT16_HA, /* type */
492 16, /* rightshift */
493 1, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
b34976b6 495 FALSE, /* pc_relative */
5bd4f169
AM
496 0, /* bitpos */
497 complain_overflow_dont,/* complain_on_overflow */
805fc799 498 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 499 "R_PPC64_GOT16_HA", /* name */
b34976b6 500 FALSE, /* partial_inplace */
5bd4f169
AM
501 0, /* src_mask */
502 0xffff, /* dst_mask */
b34976b6 503 FALSE), /* pcrel_offset */
5bd4f169
AM
504
505 /* This is used only by the dynamic linker. The symbol should exist
506 both in the object being run and in some shared library. The
507 dynamic linker copies the data addressed by the symbol from the
508 shared library into the object, because the object being
509 run has to have the data at some particular address. */
510 HOWTO (R_PPC64_COPY, /* type */
511 0, /* rightshift */
f5e87a1d
AM
512 0, /* this one is variable size */
513 0, /* bitsize */
b34976b6 514 FALSE, /* pc_relative */
5bd4f169 515 0, /* bitpos */
f5e87a1d
AM
516 complain_overflow_dont, /* complain_on_overflow */
517 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 518 "R_PPC64_COPY", /* name */
b34976b6 519 FALSE, /* partial_inplace */
5bd4f169
AM
520 0, /* src_mask */
521 0, /* dst_mask */
b34976b6 522 FALSE), /* pcrel_offset */
5bd4f169
AM
523
524 /* Like R_PPC64_ADDR64, but used when setting global offset table
525 entries. */
526 HOWTO (R_PPC64_GLOB_DAT, /* type */
527 0, /* rightshift */
528 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
529 64, /* bitsize */
b34976b6 530 FALSE, /* pc_relative */
5bd4f169
AM
531 0, /* bitpos */
532 complain_overflow_dont, /* complain_on_overflow */
805fc799 533 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 534 "R_PPC64_GLOB_DAT", /* name */
b34976b6 535 FALSE, /* partial_inplace */
5bd4f169 536 0, /* src_mask */
f5e87a1d 537 ONES (64), /* dst_mask */
b34976b6 538 FALSE), /* pcrel_offset */
5bd4f169
AM
539
540 /* Created by the link editor. Marks a procedure linkage table
541 entry for a symbol. */
542 HOWTO (R_PPC64_JMP_SLOT, /* type */
543 0, /* rightshift */
544 0, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
b34976b6 546 FALSE, /* pc_relative */
5bd4f169
AM
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
805fc799 549 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 550 "R_PPC64_JMP_SLOT", /* name */
b34976b6 551 FALSE, /* partial_inplace */
5bd4f169
AM
552 0, /* src_mask */
553 0, /* dst_mask */
b34976b6 554 FALSE), /* pcrel_offset */
5bd4f169
AM
555
556 /* Used only by the dynamic linker. When the object is run, this
557 doubleword64 is set to the load address of the object, plus the
558 addend. */
559 HOWTO (R_PPC64_RELATIVE, /* type */
560 0, /* rightshift */
561 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
562 64, /* bitsize */
b34976b6 563 FALSE, /* pc_relative */
5bd4f169
AM
564 0, /* bitpos */
565 complain_overflow_dont, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_PPC64_RELATIVE", /* name */
b34976b6 568 FALSE, /* partial_inplace */
5bd4f169 569 0, /* src_mask */
f5e87a1d 570 ONES (64), /* dst_mask */
b34976b6 571 FALSE), /* pcrel_offset */
5bd4f169
AM
572
573 /* Like R_PPC64_ADDR32, but may be unaligned. */
574 HOWTO (R_PPC64_UADDR32, /* type */
575 0, /* rightshift */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
577 32, /* bitsize */
b34976b6 578 FALSE, /* pc_relative */
5bd4f169
AM
579 0, /* bitpos */
580 complain_overflow_bitfield, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_UADDR32", /* name */
b34976b6 583 FALSE, /* partial_inplace */
5bd4f169
AM
584 0, /* src_mask */
585 0xffffffff, /* dst_mask */
b34976b6 586 FALSE), /* pcrel_offset */
5bd4f169
AM
587
588 /* Like R_PPC64_ADDR16, but may be unaligned. */
589 HOWTO (R_PPC64_UADDR16, /* type */
590 0, /* rightshift */
591 1, /* size (0 = byte, 1 = short, 2 = long) */
592 16, /* bitsize */
b34976b6 593 FALSE, /* pc_relative */
5bd4f169
AM
594 0, /* bitpos */
595 complain_overflow_bitfield, /* complain_on_overflow */
596 bfd_elf_generic_reloc, /* special_function */
597 "R_PPC64_UADDR16", /* name */
b34976b6 598 FALSE, /* partial_inplace */
5bd4f169
AM
599 0, /* src_mask */
600 0xffff, /* dst_mask */
b34976b6 601 FALSE), /* pcrel_offset */
5bd4f169
AM
602
603 /* 32-bit PC relative. */
604 HOWTO (R_PPC64_REL32, /* type */
605 0, /* rightshift */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
607 32, /* bitsize */
b34976b6 608 TRUE, /* pc_relative */
5bd4f169 609 0, /* bitpos */
cedb70c5 610 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
611 complain_overflow_signed, /* complain_on_overflow */
612 bfd_elf_generic_reloc, /* special_function */
613 "R_PPC64_REL32", /* name */
b34976b6 614 FALSE, /* partial_inplace */
5bd4f169
AM
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
b34976b6 617 TRUE), /* pcrel_offset */
5bd4f169 618
10ed1bba 619 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
620 HOWTO (R_PPC64_PLT32, /* type */
621 0, /* rightshift */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
623 32, /* bitsize */
b34976b6 624 FALSE, /* pc_relative */
5bd4f169
AM
625 0, /* bitpos */
626 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 627 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 628 "R_PPC64_PLT32", /* name */
b34976b6 629 FALSE, /* partial_inplace */
5bd4f169 630 0, /* src_mask */
f5e87a1d 631 0xffffffff, /* dst_mask */
b34976b6 632 FALSE), /* pcrel_offset */
5bd4f169
AM
633
634 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
635 FIXME: R_PPC64_PLTREL32 not supported. */
636 HOWTO (R_PPC64_PLTREL32, /* type */
637 0, /* rightshift */
638 2, /* size (0 = byte, 1 = short, 2 = long) */
639 32, /* bitsize */
b34976b6 640 TRUE, /* pc_relative */
5bd4f169
AM
641 0, /* bitpos */
642 complain_overflow_signed, /* complain_on_overflow */
643 bfd_elf_generic_reloc, /* special_function */
644 "R_PPC64_PLTREL32", /* name */
b34976b6 645 FALSE, /* partial_inplace */
5bd4f169 646 0, /* src_mask */
f5e87a1d 647 0xffffffff, /* dst_mask */
b34976b6 648 TRUE), /* pcrel_offset */
5bd4f169
AM
649
650 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
651 the symbol. */
652 HOWTO (R_PPC64_PLT16_LO, /* type */
653 0, /* rightshift */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
b34976b6 656 FALSE, /* pc_relative */
5bd4f169
AM
657 0, /* bitpos */
658 complain_overflow_dont, /* complain_on_overflow */
805fc799 659 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 660 "R_PPC64_PLT16_LO", /* name */
b34976b6 661 FALSE, /* partial_inplace */
5bd4f169
AM
662 0, /* src_mask */
663 0xffff, /* dst_mask */
b34976b6 664 FALSE), /* pcrel_offset */
5bd4f169
AM
665
666 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
667 the symbol. */
668 HOWTO (R_PPC64_PLT16_HI, /* type */
669 16, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
b34976b6 672 FALSE, /* pc_relative */
5bd4f169
AM
673 0, /* bitpos */
674 complain_overflow_dont, /* complain_on_overflow */
805fc799 675 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 676 "R_PPC64_PLT16_HI", /* name */
b34976b6 677 FALSE, /* partial_inplace */
5bd4f169
AM
678 0, /* src_mask */
679 0xffff, /* dst_mask */
b34976b6 680 FALSE), /* pcrel_offset */
5bd4f169
AM
681
682 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
683 the symbol. */
684 HOWTO (R_PPC64_PLT16_HA, /* type */
685 16, /* rightshift */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
687 16, /* bitsize */
b34976b6 688 FALSE, /* pc_relative */
5bd4f169
AM
689 0, /* bitpos */
690 complain_overflow_dont, /* complain_on_overflow */
805fc799 691 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 692 "R_PPC64_PLT16_HA", /* name */
b34976b6 693 FALSE, /* partial_inplace */
5bd4f169
AM
694 0, /* src_mask */
695 0xffff, /* dst_mask */
b34976b6 696 FALSE), /* pcrel_offset */
5bd4f169 697
c061c2d8 698 /* 16-bit section relative relocation. */
5bd4f169
AM
699 HOWTO (R_PPC64_SECTOFF, /* type */
700 0, /* rightshift */
c061c2d8
AM
701 1, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
b34976b6 703 FALSE, /* pc_relative */
5bd4f169
AM
704 0, /* bitpos */
705 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 706 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 707 "R_PPC64_SECTOFF", /* name */
b34976b6 708 FALSE, /* partial_inplace */
5bd4f169 709 0, /* src_mask */
c061c2d8 710 0xffff, /* dst_mask */
b34976b6 711 FALSE), /* pcrel_offset */
5bd4f169 712
c061c2d8 713 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
714 HOWTO (R_PPC64_SECTOFF_LO, /* type */
715 0, /* rightshift */
716 1, /* size (0 = byte, 1 = short, 2 = long) */
717 16, /* bitsize */
b34976b6 718 FALSE, /* pc_relative */
5bd4f169
AM
719 0, /* bitpos */
720 complain_overflow_dont, /* complain_on_overflow */
805fc799 721 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 722 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 723 FALSE, /* partial_inplace */
5bd4f169
AM
724 0, /* src_mask */
725 0xffff, /* dst_mask */
b34976b6 726 FALSE), /* pcrel_offset */
5bd4f169
AM
727
728 /* 16-bit upper half section relative relocation. */
729 HOWTO (R_PPC64_SECTOFF_HI, /* type */
730 16, /* rightshift */
731 1, /* size (0 = byte, 1 = short, 2 = long) */
732 16, /* bitsize */
b34976b6 733 FALSE, /* pc_relative */
5bd4f169
AM
734 0, /* bitpos */
735 complain_overflow_dont, /* complain_on_overflow */
805fc799 736 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 737 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 738 FALSE, /* partial_inplace */
5bd4f169
AM
739 0, /* src_mask */
740 0xffff, /* dst_mask */
b34976b6 741 FALSE), /* pcrel_offset */
5bd4f169
AM
742
743 /* 16-bit upper half adjusted section relative relocation. */
744 HOWTO (R_PPC64_SECTOFF_HA, /* type */
745 16, /* rightshift */
746 1, /* size (0 = byte, 1 = short, 2 = long) */
747 16, /* bitsize */
b34976b6 748 FALSE, /* pc_relative */
5bd4f169
AM
749 0, /* bitpos */
750 complain_overflow_dont, /* complain_on_overflow */
805fc799 751 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 752 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 753 FALSE, /* partial_inplace */
5bd4f169
AM
754 0, /* src_mask */
755 0xffff, /* dst_mask */
b34976b6 756 FALSE), /* pcrel_offset */
5bd4f169 757
04c9666a
AM
758 /* Like R_PPC64_REL24 without touching the two least significant bits. */
759 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
760 2, /* rightshift */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
762 30, /* bitsize */
b34976b6 763 TRUE, /* pc_relative */
5bd4f169
AM
764 0, /* bitpos */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
04c9666a 767 "R_PPC64_REL30", /* name */
b34976b6 768 FALSE, /* partial_inplace */
d006db6c 769 0, /* src_mask */
5bd4f169 770 0xfffffffc, /* dst_mask */
b34976b6 771 TRUE), /* pcrel_offset */
5bd4f169
AM
772
773 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
774
775 /* A standard 64-bit relocation. */
776 HOWTO (R_PPC64_ADDR64, /* type */
777 0, /* rightshift */
778 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
779 64, /* bitsize */
b34976b6 780 FALSE, /* pc_relative */
5bd4f169
AM
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_PPC64_ADDR64", /* name */
b34976b6 785 FALSE, /* partial_inplace */
5bd4f169 786 0, /* src_mask */
f5e87a1d 787 ONES (64), /* dst_mask */
b34976b6 788 FALSE), /* pcrel_offset */
5bd4f169
AM
789
790 /* The bits 32-47 of an address. */
791 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
792 32, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
b34976b6 795 FALSE, /* pc_relative */
5bd4f169
AM
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 800 FALSE, /* partial_inplace */
5bd4f169
AM
801 0, /* src_mask */
802 0xffff, /* dst_mask */
b34976b6 803 FALSE), /* pcrel_offset */
5bd4f169
AM
804
805 /* The bits 32-47 of an address, plus 1 if the contents of the low
806 16 bits, treated as a signed number, is negative. */
807 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
808 32, /* rightshift */
809 1, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
b34976b6 811 FALSE, /* pc_relative */
5bd4f169
AM
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
805fc799 814 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 815 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 816 FALSE, /* partial_inplace */
5bd4f169
AM
817 0, /* src_mask */
818 0xffff, /* dst_mask */
b34976b6 819 FALSE), /* pcrel_offset */
5bd4f169
AM
820
821 /* The bits 48-63 of an address. */
822 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
823 48, /* rightshift */
824 1, /* size (0 = byte, 1 = short, 2 = long) */
825 16, /* bitsize */
b34976b6 826 FALSE, /* pc_relative */
5bd4f169
AM
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 831 FALSE, /* partial_inplace */
5bd4f169
AM
832 0, /* src_mask */
833 0xffff, /* dst_mask */
b34976b6 834 FALSE), /* pcrel_offset */
5bd4f169
AM
835
836 /* The bits 48-63 of an address, plus 1 if the contents of the low
837 16 bits, treated as a signed number, is negative. */
838 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
839 48, /* rightshift */
840 1, /* size (0 = byte, 1 = short, 2 = long) */
841 16, /* bitsize */
b34976b6 842 FALSE, /* pc_relative */
5bd4f169
AM
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
805fc799 845 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 846 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 847 FALSE, /* partial_inplace */
5bd4f169
AM
848 0, /* src_mask */
849 0xffff, /* dst_mask */
b34976b6 850 FALSE), /* pcrel_offset */
5bd4f169
AM
851
852 /* Like ADDR64, but may be unaligned. */
853 HOWTO (R_PPC64_UADDR64, /* type */
854 0, /* rightshift */
855 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 64, /* bitsize */
b34976b6 857 FALSE, /* pc_relative */
5bd4f169
AM
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC64_UADDR64", /* name */
b34976b6 862 FALSE, /* partial_inplace */
5bd4f169 863 0, /* src_mask */
f5e87a1d 864 ONES (64), /* dst_mask */
b34976b6 865 FALSE), /* pcrel_offset */
5bd4f169
AM
866
867 /* 64-bit relative relocation. */
868 HOWTO (R_PPC64_REL64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
b34976b6 872 TRUE, /* pc_relative */
5bd4f169
AM
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 "R_PPC64_REL64", /* name */
b34976b6 877 FALSE, /* partial_inplace */
5bd4f169 878 0, /* src_mask */
f5e87a1d 879 ONES (64), /* dst_mask */
b34976b6 880 TRUE), /* pcrel_offset */
5bd4f169 881
cedb70c5 882 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
883 HOWTO (R_PPC64_PLT64, /* type */
884 0, /* rightshift */
885 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886 64, /* bitsize */
b34976b6 887 FALSE, /* pc_relative */
5bd4f169
AM
888 0, /* bitpos */
889 complain_overflow_dont, /* complain_on_overflow */
805fc799 890 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 891 "R_PPC64_PLT64", /* name */
b34976b6 892 FALSE, /* partial_inplace */
5bd4f169 893 0, /* src_mask */
f5e87a1d 894 ONES (64), /* dst_mask */
b34976b6 895 FALSE), /* pcrel_offset */
5bd4f169
AM
896
897 /* 64-bit PC relative relocation to the symbol's procedure linkage
898 table. */
899 /* FIXME: R_PPC64_PLTREL64 not supported. */
900 HOWTO (R_PPC64_PLTREL64, /* type */
901 0, /* rightshift */
902 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
903 64, /* bitsize */
b34976b6 904 TRUE, /* pc_relative */
5bd4f169
AM
905 0, /* bitpos */
906 complain_overflow_dont, /* complain_on_overflow */
805fc799 907 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 908 "R_PPC64_PLTREL64", /* name */
b34976b6 909 FALSE, /* partial_inplace */
5bd4f169 910 0, /* src_mask */
f5e87a1d 911 ONES (64), /* dst_mask */
b34976b6 912 TRUE), /* pcrel_offset */
5bd4f169
AM
913
914 /* 16 bit TOC-relative relocation. */
915
916 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
917 HOWTO (R_PPC64_TOC16, /* type */
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
b34976b6 921 FALSE, /* pc_relative */
5bd4f169
AM
922 0, /* bitpos */
923 complain_overflow_signed, /* complain_on_overflow */
805fc799 924 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 925 "R_PPC64_TOC16", /* name */
b34976b6 926 FALSE, /* partial_inplace */
5bd4f169
AM
927 0, /* src_mask */
928 0xffff, /* dst_mask */
b34976b6 929 FALSE), /* pcrel_offset */
5bd4f169
AM
930
931 /* 16 bit TOC-relative relocation without overflow. */
932
933 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
934 HOWTO (R_PPC64_TOC16_LO, /* type */
935 0, /* rightshift */
936 1, /* size (0 = byte, 1 = short, 2 = long) */
937 16, /* bitsize */
b34976b6 938 FALSE, /* pc_relative */
5bd4f169
AM
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
805fc799 941 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 942 "R_PPC64_TOC16_LO", /* name */
b34976b6 943 FALSE, /* partial_inplace */
5bd4f169
AM
944 0, /* src_mask */
945 0xffff, /* dst_mask */
b34976b6 946 FALSE), /* pcrel_offset */
5bd4f169
AM
947
948 /* 16 bit TOC-relative relocation, high 16 bits. */
949
950 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
951 HOWTO (R_PPC64_TOC16_HI, /* type */
952 16, /* rightshift */
953 1, /* size (0 = byte, 1 = short, 2 = long) */
954 16, /* bitsize */
b34976b6 955 FALSE, /* pc_relative */
5bd4f169
AM
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
805fc799 958 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 959 "R_PPC64_TOC16_HI", /* name */
b34976b6 960 FALSE, /* partial_inplace */
5bd4f169
AM
961 0, /* src_mask */
962 0xffff, /* dst_mask */
b34976b6 963 FALSE), /* pcrel_offset */
5bd4f169
AM
964
965 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
966 contents of the low 16 bits, treated as a signed number, is
967 negative. */
968
969 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
970 HOWTO (R_PPC64_TOC16_HA, /* type */
971 16, /* rightshift */
972 1, /* size (0 = byte, 1 = short, 2 = long) */
973 16, /* bitsize */
b34976b6 974 FALSE, /* pc_relative */
5bd4f169
AM
975 0, /* bitpos */
976 complain_overflow_dont, /* complain_on_overflow */
805fc799 977 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 978 "R_PPC64_TOC16_HA", /* name */
b34976b6 979 FALSE, /* partial_inplace */
5bd4f169
AM
980 0, /* src_mask */
981 0xffff, /* dst_mask */
b34976b6 982 FALSE), /* pcrel_offset */
5bd4f169
AM
983
984 /* 64-bit relocation; insert value of TOC base (.TOC.). */
985
986 /* R_PPC64_TOC 51 doubleword64 .TOC. */
987 HOWTO (R_PPC64_TOC, /* type */
988 0, /* rightshift */
989 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
990 64, /* bitsize */
b34976b6 991 FALSE, /* pc_relative */
5bd4f169
AM
992 0, /* bitpos */
993 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 994 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 995 "R_PPC64_TOC", /* name */
b34976b6 996 FALSE, /* partial_inplace */
5bd4f169 997 0, /* src_mask */
f5e87a1d 998 ONES (64), /* dst_mask */
b34976b6 999 FALSE), /* pcrel_offset */
5bd4f169
AM
1000
1001 /* Like R_PPC64_GOT16, but also informs the link editor that the
1002 value to relocate may (!) refer to a PLT entry which the link
1003 editor (a) may replace with the symbol value. If the link editor
1004 is unable to fully resolve the symbol, it may (b) create a PLT
1005 entry and store the address to the new PLT entry in the GOT.
1006 This permits lazy resolution of function symbols at run time.
1007 The link editor may also skip all of this and just (c) emit a
1008 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1009 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1010 HOWTO (R_PPC64_PLTGOT16, /* type */
1011 0, /* rightshift */
1012 1, /* size (0 = byte, 1 = short, 2 = long) */
1013 16, /* bitsize */
b34976b6 1014 FALSE, /* pc_relative */
5bd4f169
AM
1015 0, /* bitpos */
1016 complain_overflow_signed, /* complain_on_overflow */
805fc799 1017 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1018 "R_PPC64_PLTGOT16", /* name */
1019 FALSE, /* partial_inplace */
1020 0, /* src_mask */
1021 0xffff, /* dst_mask */
1022 FALSE), /* pcrel_offset */
1023
1024 /* Like R_PPC64_PLTGOT16, but without overflow. */
1025 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1026 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1027 0, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_dont, /* complain_on_overflow */
1033 ppc64_elf_unhandled_reloc, /* special_function */
1034 "R_PPC64_PLTGOT16_LO", /* name */
1035 FALSE, /* partial_inplace */
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1039
1040 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1041 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1042 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1043 16, /* rightshift */
1044 1, /* size (0 = byte, 1 = short, 2 = long) */
1045 16, /* bitsize */
1046 FALSE, /* pc_relative */
1047 0, /* bitpos */
1048 complain_overflow_dont, /* complain_on_overflow */
1049 ppc64_elf_unhandled_reloc, /* special_function */
1050 "R_PPC64_PLTGOT16_HI", /* name */
1051 FALSE, /* partial_inplace */
1052 0, /* src_mask */
1053 0xffff, /* dst_mask */
1054 FALSE), /* pcrel_offset */
1055
1056 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1057 1 if the contents of the low 16 bits, treated as a signed number,
1058 is negative. */
1059 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1060 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1061 16, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 FALSE, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_dont,/* complain_on_overflow */
1067 ppc64_elf_unhandled_reloc, /* special_function */
1068 "R_PPC64_PLTGOT16_HA", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xffff, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1073
1074 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1075 HOWTO (R_PPC64_ADDR16_DS, /* type */
1076 0, /* rightshift */
1077 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 FALSE, /* pc_relative */
1080 0, /* bitpos */
1081 complain_overflow_bitfield, /* complain_on_overflow */
1082 bfd_elf_generic_reloc, /* special_function */
1083 "R_PPC64_ADDR16_DS", /* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xfffc, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1088
1089 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_dont,/* complain_on_overflow */
1097 bfd_elf_generic_reloc, /* special_function */
1098 "R_PPC64_ADDR16_LO_DS",/* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_GOT16_DS, /* type */
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_signed, /* complain_on_overflow */
1112 ppc64_elf_unhandled_reloc, /* special_function */
1113 "R_PPC64_GOT16_DS", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1118
1119 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_dont, /* complain_on_overflow */
1127 ppc64_elf_unhandled_reloc, /* special_function */
1128 "R_PPC64_GOT16_LO_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1133
1134 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc64_elf_unhandled_reloc, /* special_function */
1143 "R_PPC64_PLT16_LO_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1148
1149 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_bitfield, /* complain_on_overflow */
1157 ppc64_elf_sectoff_reloc, /* special_function */
1158 "R_PPC64_SECTOFF_DS", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1163
1164 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1166 0, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 ppc64_elf_sectoff_reloc, /* special_function */
1173 "R_PPC64_SECTOFF_LO_DS",/* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1178
1179 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_TOC16_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_signed, /* complain_on_overflow */
1187 ppc64_elf_toc_reloc, /* special_function */
1188 "R_PPC64_TOC16_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1193
1194 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1195 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1196 0, /* rightshift */
1197 1, /* size (0 = byte, 1 = short, 2 = long) */
1198 16, /* bitsize */
1199 FALSE, /* pc_relative */
1200 0, /* bitpos */
1201 complain_overflow_dont, /* complain_on_overflow */
1202 ppc64_elf_toc_reloc, /* special_function */
1203 "R_PPC64_TOC16_LO_DS", /* name */
1204 FALSE, /* partial_inplace */
1205 0, /* src_mask */
1206 0xfffc, /* dst_mask */
1207 FALSE), /* pcrel_offset */
1208
1209 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1210 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1211 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1212 0, /* rightshift */
1213 1, /* size (0 = byte, 1 = short, 2 = long) */
1214 16, /* bitsize */
1215 FALSE, /* pc_relative */
1216 0, /* bitpos */
1217 complain_overflow_signed, /* complain_on_overflow */
1218 ppc64_elf_unhandled_reloc, /* special_function */
1219 "R_PPC64_PLTGOT16_DS", /* name */
1220 FALSE, /* partial_inplace */
1221 0, /* src_mask */
1222 0xfffc, /* dst_mask */
1223 FALSE), /* pcrel_offset */
1224
1225 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1226 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1227 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1228 0, /* rightshift */
1229 1, /* size (0 = byte, 1 = short, 2 = long) */
1230 16, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 ppc64_elf_unhandled_reloc, /* special_function */
1235 "R_PPC64_PLTGOT16_LO_DS",/* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0xfffc, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1240
727fc41e 1241 /* Marker relocs for TLS. */
411e1bfb
AM
1242 HOWTO (R_PPC64_TLS,
1243 0, /* rightshift */
1244 2, /* size (0 = byte, 1 = short, 2 = long) */
1245 32, /* bitsize */
1246 FALSE, /* pc_relative */
1247 0, /* bitpos */
1248 complain_overflow_dont, /* complain_on_overflow */
1249 bfd_elf_generic_reloc, /* special_function */
1250 "R_PPC64_TLS", /* name */
1251 FALSE, /* partial_inplace */
1252 0, /* src_mask */
1253 0, /* dst_mask */
1254 FALSE), /* pcrel_offset */
1255
727fc41e
AM
1256 HOWTO (R_PPC64_TLSGD,
1257 0, /* rightshift */
1258 2, /* size (0 = byte, 1 = short, 2 = long) */
1259 32, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont, /* complain_on_overflow */
1263 bfd_elf_generic_reloc, /* special_function */
1264 "R_PPC64_TLSGD", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1269
1270 HOWTO (R_PPC64_TLSLD,
1271 0, /* rightshift */
1272 2, /* size (0 = byte, 1 = short, 2 = long) */
1273 32, /* bitsize */
1274 FALSE, /* pc_relative */
1275 0, /* bitpos */
1276 complain_overflow_dont, /* complain_on_overflow */
1277 bfd_elf_generic_reloc, /* special_function */
1278 "R_PPC64_TLSLD", /* name */
1279 FALSE, /* partial_inplace */
1280 0, /* src_mask */
1281 0, /* dst_mask */
1282 FALSE), /* pcrel_offset */
1283
411e1bfb
AM
1284 /* Computes the load module index of the load module that contains the
1285 definition of its TLS sym. */
1286 HOWTO (R_PPC64_DTPMOD64,
1287 0, /* rightshift */
1288 4, /* size (0 = byte, 1 = short, 2 = long) */
1289 64, /* bitsize */
1290 FALSE, /* pc_relative */
1291 0, /* bitpos */
1292 complain_overflow_dont, /* complain_on_overflow */
1293 ppc64_elf_unhandled_reloc, /* special_function */
1294 "R_PPC64_DTPMOD64", /* name */
1295 FALSE, /* partial_inplace */
1296 0, /* src_mask */
1297 ONES (64), /* dst_mask */
1298 FALSE), /* pcrel_offset */
1299
1300 /* Computes a dtv-relative displacement, the difference between the value
1301 of sym+add and the base address of the thread-local storage block that
1302 contains the definition of sym, minus 0x8000. */
1303 HOWTO (R_PPC64_DTPREL64,
1304 0, /* rightshift */
1305 4, /* size (0 = byte, 1 = short, 2 = long) */
1306 64, /* bitsize */
1307 FALSE, /* pc_relative */
1308 0, /* bitpos */
1309 complain_overflow_dont, /* complain_on_overflow */
1310 ppc64_elf_unhandled_reloc, /* special_function */
1311 "R_PPC64_DTPREL64", /* name */
1312 FALSE, /* partial_inplace */
1313 0, /* src_mask */
1314 ONES (64), /* dst_mask */
1315 FALSE), /* pcrel_offset */
1316
1317 /* A 16 bit dtprel reloc. */
1318 HOWTO (R_PPC64_DTPREL16,
1319 0, /* rightshift */
1320 1, /* size (0 = byte, 1 = short, 2 = long) */
1321 16, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_signed, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL16", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 0xffff, /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 /* Like DTPREL16, but no overflow. */
1333 HOWTO (R_PPC64_DTPREL16_LO,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_dont, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16_LO", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1348 HOWTO (R_PPC64_DTPREL16_HI,
1349 16, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPREL16_HI", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HA,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc64_elf_unhandled_reloc, /* special_function */
1371 "R_PPC64_DTPREL16_HA", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1376
1377 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HIGHER,
1379 32, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc64_elf_unhandled_reloc, /* special_function */
1386 "R_PPC64_DTPREL16_HIGHER", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1391
1392 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1394 32, /* rightshift */
1395 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 16, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_dont, /* complain_on_overflow */
1400 ppc64_elf_unhandled_reloc, /* special_function */
1401 "R_PPC64_DTPREL16_HIGHERA", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1409 48, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_dont, /* complain_on_overflow */
1415 ppc64_elf_unhandled_reloc, /* special_function */
1416 "R_PPC64_DTPREL16_HIGHEST", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1424 48, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 FALSE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont, /* complain_on_overflow */
1430 ppc64_elf_unhandled_reloc, /* special_function */
1431 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1436
1437 /* Like DTPREL16, but for insns with a DS field. */
1438 HOWTO (R_PPC64_DTPREL16_DS,
1439 0, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_signed, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_DS", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xfffc, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451
1452 /* Like DTPREL16_DS, but no overflow. */
1453 HOWTO (R_PPC64_DTPREL16_LO_DS,
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_dont, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_LO_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466
1467 /* Computes a tp-relative displacement, the difference between the value of
1468 sym+add and the value of the thread pointer (r13). */
1469 HOWTO (R_PPC64_TPREL64,
1470 0, /* rightshift */
1471 4, /* size (0 = byte, 1 = short, 2 = long) */
1472 64, /* bitsize */
1473 FALSE, /* pc_relative */
1474 0, /* bitpos */
1475 complain_overflow_dont, /* complain_on_overflow */
1476 ppc64_elf_unhandled_reloc, /* special_function */
1477 "R_PPC64_TPREL64", /* name */
1478 FALSE, /* partial_inplace */
1479 0, /* src_mask */
1480 ONES (64), /* dst_mask */
1481 FALSE), /* pcrel_offset */
1482
1483 /* A 16 bit tprel reloc. */
1484 HOWTO (R_PPC64_TPREL16,
1485 0, /* rightshift */
1486 1, /* size (0 = byte, 1 = short, 2 = long) */
1487 16, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_signed, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL16", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 0xffff, /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* Like TPREL16, but no overflow. */
1499 HOWTO (R_PPC64_TPREL16_LO,
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_dont, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16_LO", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512
1513 /* Like TPREL16_LO, but next higher group of 16 bits. */
1514 HOWTO (R_PPC64_TPREL16_HI,
1515 16, /* rightshift */
1516 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 16, /* bitsize */
1518 FALSE, /* pc_relative */
1519 0, /* bitpos */
1520 complain_overflow_dont, /* complain_on_overflow */
1521 ppc64_elf_unhandled_reloc, /* special_function */
1522 "R_PPC64_TPREL16_HI", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527
1528 /* Like TPREL16_HI, but adjust for low 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HA,
1530 16, /* rightshift */
1531 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 16, /* bitsize */
1533 FALSE, /* pc_relative */
1534 0, /* bitpos */
1535 complain_overflow_dont, /* complain_on_overflow */
1536 ppc64_elf_unhandled_reloc, /* special_function */
1537 "R_PPC64_TPREL16_HA", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1542
1543 /* Like TPREL16_HI, but next higher group of 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HIGHER,
1545 32, /* rightshift */
1546 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 16, /* bitsize */
1548 FALSE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 ppc64_elf_unhandled_reloc, /* special_function */
1552 "R_PPC64_TPREL16_HIGHER", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557
1558 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHERA,
1560 32, /* rightshift */
1561 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 16, /* bitsize */
1563 FALSE, /* pc_relative */
1564 0, /* bitpos */
1565 complain_overflow_dont, /* complain_on_overflow */
1566 ppc64_elf_unhandled_reloc, /* special_function */
1567 "R_PPC64_TPREL16_HIGHERA", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572
1573 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHEST,
1575 48, /* rightshift */
1576 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 16, /* bitsize */
1578 FALSE, /* pc_relative */
1579 0, /* bitpos */
1580 complain_overflow_dont, /* complain_on_overflow */
1581 ppc64_elf_unhandled_reloc, /* special_function */
1582 "R_PPC64_TPREL16_HIGHEST", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1587
1588 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1590 48, /* rightshift */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont, /* complain_on_overflow */
1596 ppc64_elf_unhandled_reloc, /* special_function */
1597 "R_PPC64_TPREL16_HIGHESTA", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1602
1603 /* Like TPREL16, but for insns with a DS field. */
1604 HOWTO (R_PPC64_TPREL16_DS,
1605 0, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_signed, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_DS", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xfffc, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1617
1618 /* Like TPREL16_DS, but no overflow. */
1619 HOWTO (R_PPC64_TPREL16_LO_DS,
1620 0, /* rightshift */
1621 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 16, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_dont, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_LO_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1632
1633 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1634 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1635 to the first entry relative to the TOC base (r2). */
1636 HOWTO (R_PPC64_GOT_TLSGD16,
1637 0, /* rightshift */
1638 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 16, /* bitsize */
1640 FALSE, /* pc_relative */
1641 0, /* bitpos */
1642 complain_overflow_signed, /* complain_on_overflow */
1643 ppc64_elf_unhandled_reloc, /* special_function */
1644 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1645 FALSE, /* partial_inplace */
5bd4f169
AM
1646 0, /* src_mask */
1647 0xffff, /* dst_mask */
b34976b6 1648 FALSE), /* pcrel_offset */
5bd4f169 1649
411e1bfb
AM
1650 /* Like GOT_TLSGD16, but no overflow. */
1651 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1652 0, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
b34976b6 1655 FALSE, /* pc_relative */
5bd4f169
AM
1656 0, /* bitpos */
1657 complain_overflow_dont, /* complain_on_overflow */
805fc799 1658 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1659 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1660 FALSE, /* partial_inplace */
5bd4f169
AM
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
b34976b6 1663 FALSE), /* pcrel_offset */
5bd4f169 1664
411e1bfb
AM
1665 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1667 16, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
b34976b6 1670 FALSE, /* pc_relative */
5bd4f169
AM
1671 0, /* bitpos */
1672 complain_overflow_dont, /* complain_on_overflow */
805fc799 1673 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1674 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1675 FALSE, /* partial_inplace */
5bd4f169
AM
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
b34976b6 1678 FALSE), /* pcrel_offset */
5bd4f169 1679
411e1bfb
AM
1680 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1682 16, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
b34976b6 1685 FALSE, /* pc_relative */
5bd4f169 1686 0, /* bitpos */
411e1bfb 1687 complain_overflow_dont, /* complain_on_overflow */
805fc799 1688 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1689 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1690 FALSE, /* partial_inplace */
5bd4f169
AM
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
b34976b6 1693 FALSE), /* pcrel_offset */
5bd4f169 1694
411e1bfb
AM
1695 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1696 with values (sym+add)@dtpmod and zero, and computes the offset to the
1697 first entry relative to the TOC base (r2). */
1698 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1699 0, /* rightshift */
1700 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
b34976b6 1702 FALSE, /* pc_relative */
5bd4f169 1703 0, /* bitpos */
411e1bfb
AM
1704 complain_overflow_signed, /* complain_on_overflow */
1705 ppc64_elf_unhandled_reloc, /* special_function */
1706 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1707 FALSE, /* partial_inplace */
d006db6c 1708 0, /* src_mask */
411e1bfb 1709 0xffff, /* dst_mask */
b34976b6 1710 FALSE), /* pcrel_offset */
5bd4f169 1711
411e1bfb
AM
1712 /* Like GOT_TLSLD16, but no overflow. */
1713 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
b34976b6 1717 FALSE, /* pc_relative */
5bd4f169 1718 0, /* bitpos */
411e1bfb
AM
1719 complain_overflow_dont, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1722 FALSE, /* partial_inplace */
d006db6c 1723 0, /* src_mask */
411e1bfb 1724 0xffff, /* dst_mask */
b34976b6 1725 FALSE), /* pcrel_offset */
5bd4f169 1726
411e1bfb
AM
1727 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1729 16, /* rightshift */
5bd4f169
AM
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
b34976b6 1732 FALSE, /* pc_relative */
5bd4f169 1733 0, /* bitpos */
411e1bfb 1734 complain_overflow_dont, /* complain_on_overflow */
805fc799 1735 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1736 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1737 FALSE, /* partial_inplace */
d006db6c 1738 0, /* src_mask */
411e1bfb 1739 0xffff, /* dst_mask */
b34976b6 1740 FALSE), /* pcrel_offset */
5bd4f169 1741
411e1bfb
AM
1742 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1744 16, /* rightshift */
5bd4f169
AM
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
b34976b6 1747 FALSE, /* pc_relative */
5bd4f169
AM
1748 0, /* bitpos */
1749 complain_overflow_dont, /* complain_on_overflow */
805fc799 1750 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1751 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1752 FALSE, /* partial_inplace */
d006db6c 1753 0, /* src_mask */
411e1bfb 1754 0xffff, /* dst_mask */
b34976b6 1755 FALSE), /* pcrel_offset */
5bd4f169 1756
411e1bfb
AM
1757 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1758 the offset to the entry relative to the TOC base (r2). */
1759 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1760 0, /* rightshift */
1761 1, /* size (0 = byte, 1 = short, 2 = long) */
1762 16, /* bitsize */
b34976b6 1763 FALSE, /* pc_relative */
5bd4f169 1764 0, /* bitpos */
411e1bfb 1765 complain_overflow_signed, /* complain_on_overflow */
805fc799 1766 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1767 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1768 FALSE, /* partial_inplace */
d006db6c 1769 0, /* src_mask */
5bd4f169 1770 0xfffc, /* dst_mask */
b34976b6 1771 FALSE), /* pcrel_offset */
5bd4f169 1772
411e1bfb
AM
1773 /* Like GOT_DTPREL16_DS, but no overflow. */
1774 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1775 0, /* rightshift */
c061c2d8
AM
1776 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 16, /* bitsize */
b34976b6 1778 FALSE, /* pc_relative */
5bd4f169 1779 0, /* bitpos */
411e1bfb
AM
1780 complain_overflow_dont, /* complain_on_overflow */
1781 ppc64_elf_unhandled_reloc, /* special_function */
1782 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1783 FALSE, /* partial_inplace */
d006db6c 1784 0, /* src_mask */
c061c2d8 1785 0xfffc, /* dst_mask */
b34976b6 1786 FALSE), /* pcrel_offset */
5bd4f169 1787
411e1bfb
AM
1788 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1790 16, /* rightshift */
5bd4f169
AM
1791 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 16, /* bitsize */
b34976b6 1793 FALSE, /* pc_relative */
5bd4f169
AM
1794 0, /* bitpos */
1795 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1796 ppc64_elf_unhandled_reloc, /* special_function */
1797 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1798 FALSE, /* partial_inplace */
d006db6c 1799 0, /* src_mask */
411e1bfb 1800 0xffff, /* dst_mask */
b34976b6 1801 FALSE), /* pcrel_offset */
5bd4f169 1802
411e1bfb
AM
1803 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1805 16, /* rightshift */
1806 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 16, /* bitsize */
1808 FALSE, /* pc_relative */
1809 0, /* bitpos */
1810 complain_overflow_dont, /* complain_on_overflow */
1811 ppc64_elf_unhandled_reloc, /* special_function */
1812 "R_PPC64_GOT_DTPREL16_HA", /* name */
1813 FALSE, /* partial_inplace */
1814 0, /* src_mask */
1815 0xffff, /* dst_mask */
1816 FALSE), /* pcrel_offset */
1817
1818 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1819 offset to the entry relative to the TOC base (r2). */
1820 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1821 0, /* rightshift */
1822 1, /* size (0 = byte, 1 = short, 2 = long) */
1823 16, /* bitsize */
b34976b6 1824 FALSE, /* pc_relative */
5bd4f169
AM
1825 0, /* bitpos */
1826 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1827 ppc64_elf_unhandled_reloc, /* special_function */
1828 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1829 FALSE, /* partial_inplace */
d006db6c 1830 0, /* src_mask */
ad8e1ba5 1831 0xfffc, /* dst_mask */
b34976b6 1832 FALSE), /* pcrel_offset */
5bd4f169 1833
411e1bfb
AM
1834 /* Like GOT_TPREL16_DS, but no overflow. */
1835 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1836 0, /* rightshift */
1837 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 16, /* bitsize */
b34976b6 1839 FALSE, /* pc_relative */
5bd4f169
AM
1840 0, /* bitpos */
1841 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1844 FALSE, /* partial_inplace */
d006db6c 1845 0, /* src_mask */
ad8e1ba5 1846 0xfffc, /* dst_mask */
b34976b6 1847 FALSE), /* pcrel_offset */
5bd4f169 1848
411e1bfb
AM
1849 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1850 HOWTO (R_PPC64_GOT_TPREL16_HI,
1851 16, /* rightshift */
5bd4f169
AM
1852 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 16, /* bitsize */
b34976b6 1854 FALSE, /* pc_relative */
5bd4f169 1855 0, /* bitpos */
411e1bfb 1856 complain_overflow_dont, /* complain_on_overflow */
805fc799 1857 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1858 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1859 FALSE, /* partial_inplace */
d006db6c 1860 0, /* src_mask */
411e1bfb 1861 0xffff, /* dst_mask */
b34976b6 1862 FALSE), /* pcrel_offset */
5bd4f169 1863
411e1bfb
AM
1864 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HA,
1866 16, /* rightshift */
5bd4f169
AM
1867 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 16, /* bitsize */
b34976b6 1869 FALSE, /* pc_relative */
5bd4f169
AM
1870 0, /* bitpos */
1871 complain_overflow_dont, /* complain_on_overflow */
805fc799 1872 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1873 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1874 FALSE, /* partial_inplace */
d006db6c 1875 0, /* src_mask */
411e1bfb 1876 0xffff, /* dst_mask */
b34976b6 1877 FALSE), /* pcrel_offset */
5bd4f169 1878
25f23106
AM
1879 HOWTO (R_PPC64_JMP_IREL, /* type */
1880 0, /* rightshift */
1881 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1882 0, /* bitsize */
1883 FALSE, /* pc_relative */
1884 0, /* bitpos */
1885 complain_overflow_dont, /* complain_on_overflow */
1886 ppc64_elf_unhandled_reloc, /* special_function */
1887 "R_PPC64_JMP_IREL", /* name */
1888 FALSE, /* partial_inplace */
1889 0, /* src_mask */
1890 0, /* dst_mask */
1891 FALSE), /* pcrel_offset */
1892
e054468f
AM
1893 HOWTO (R_PPC64_IRELATIVE, /* type */
1894 0, /* rightshift */
1895 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1896 64, /* bitsize */
1897 FALSE, /* pc_relative */
1898 0, /* bitpos */
1899 complain_overflow_dont, /* complain_on_overflow */
1900 bfd_elf_generic_reloc, /* special_function */
1901 "R_PPC64_IRELATIVE", /* name */
1902 FALSE, /* partial_inplace */
1903 0, /* src_mask */
1904 ONES (64), /* dst_mask */
1905 FALSE), /* pcrel_offset */
1906
25f23106
AM
1907 /* A 16 bit relative relocation. */
1908 HOWTO (R_PPC64_REL16, /* type */
1909 0, /* rightshift */
1910 1, /* size (0 = byte, 1 = short, 2 = long) */
1911 16, /* bitsize */
1912 TRUE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_bitfield, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_REL16", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 0xffff, /* dst_mask */
1920 TRUE), /* pcrel_offset */
1921
1922 /* A 16 bit relative relocation without overflow. */
1923 HOWTO (R_PPC64_REL16_LO, /* type */
1924 0, /* rightshift */
1925 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 TRUE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_dont,/* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16_LO", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1936
1937 /* The high order 16 bits of a relative address. */
1938 HOWTO (R_PPC64_REL16_HI, /* type */
1939 16, /* rightshift */
1940 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 TRUE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont, /* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 "R_PPC64_REL16_HI", /* name */
1947 FALSE, /* partial_inplace */
1948 0, /* src_mask */
1949 0xffff, /* dst_mask */
1950 TRUE), /* pcrel_offset */
1951
1952 /* The high order 16 bits of a relative address, plus 1 if the contents of
1953 the low 16 bits, treated as a signed number, is negative. */
1954 HOWTO (R_PPC64_REL16_HA, /* type */
1955 16, /* rightshift */
1956 1, /* size (0 = byte, 1 = short, 2 = long) */
1957 16, /* bitsize */
1958 TRUE, /* pc_relative */
1959 0, /* bitpos */
1960 complain_overflow_dont, /* complain_on_overflow */
1961 ppc64_elf_ha_reloc, /* special_function */
1962 "R_PPC64_REL16_HA", /* name */
1963 FALSE, /* partial_inplace */
1964 0, /* src_mask */
1965 0xffff, /* dst_mask */
1966 TRUE), /* pcrel_offset */
1967
5bd4f169
AM
1968 /* GNU extension to record C++ vtable hierarchy. */
1969 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1970 0, /* rightshift */
1971 0, /* size (0 = byte, 1 = short, 2 = long) */
1972 0, /* bitsize */
b34976b6 1973 FALSE, /* pc_relative */
5bd4f169
AM
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 NULL, /* special_function */
1977 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 1978 FALSE, /* partial_inplace */
5bd4f169
AM
1979 0, /* src_mask */
1980 0, /* dst_mask */
b34976b6 1981 FALSE), /* pcrel_offset */
5bd4f169
AM
1982
1983 /* GNU extension to record C++ vtable member usage. */
1984 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1985 0, /* rightshift */
1986 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 0, /* bitsize */
b34976b6 1988 FALSE, /* pc_relative */
5bd4f169
AM
1989 0, /* bitpos */
1990 complain_overflow_dont, /* complain_on_overflow */
1991 NULL, /* special_function */
1992 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 1993 FALSE, /* partial_inplace */
5bd4f169
AM
1994 0, /* src_mask */
1995 0, /* dst_mask */
b34976b6 1996 FALSE), /* pcrel_offset */
5bd4f169
AM
1997};
1998
1999\f
2000/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2001 be done. */
2002
2003static void
4ce794b7 2004ppc_howto_init (void)
5bd4f169
AM
2005{
2006 unsigned int i, type;
2007
2008 for (i = 0;
2009 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2010 i++)
2011 {
2012 type = ppc64_elf_howto_raw[i].type;
2013 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2014 / sizeof (ppc64_elf_howto_table[0])));
2015 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2016 }
2017}
2018
2019static reloc_howto_type *
4ce794b7
AM
2020ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2021 bfd_reloc_code_real_type code)
5bd4f169 2022{
411e1bfb 2023 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2024
2025 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2026 /* Initialize howto table if needed. */
2027 ppc_howto_init ();
2028
4ce794b7 2029 switch (code)
5bd4f169
AM
2030 {
2031 default:
4ce794b7 2032 return NULL;
5bd4f169 2033
411e1bfb
AM
2034 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2035 break;
2036 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2037 break;
2038 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2039 break;
2040 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2041 break;
2042 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2043 break;
2044 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2045 break;
2046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2047 break;
411e1bfb 2048 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2049 break;
411e1bfb 2050 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2051 break;
411e1bfb 2052 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2053 break;
411e1bfb 2054 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2055 break;
411e1bfb 2056 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2057 break;
411e1bfb 2058 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2059 break;
411e1bfb 2060 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2061 break;
411e1bfb 2062 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2063 break;
411e1bfb 2064 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2065 break;
411e1bfb 2066 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2067 break;
411e1bfb 2068 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2069 break;
411e1bfb 2070 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2071 break;
411e1bfb 2072 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2073 break;
411e1bfb 2074 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2075 break;
411e1bfb 2076 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2077 break;
411e1bfb 2078 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2079 break;
411e1bfb 2080 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2081 break;
411e1bfb 2082 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2083 break;
411e1bfb 2084 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2085 break;
411e1bfb 2086 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2087 break;
411e1bfb 2088 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2089 break;
411e1bfb 2090 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2091 break;
411e1bfb 2092 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2093 break;
411e1bfb 2094 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2095 break;
411e1bfb 2096 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2097 break;
411e1bfb 2098 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2099 break;
411e1bfb 2100 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2101 break;
411e1bfb 2102 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2103 break;
411e1bfb 2104 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2105 break;
411e1bfb 2106 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2107 break;
411e1bfb 2108 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2109 break;
411e1bfb 2110 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2111 break;
411e1bfb 2112 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2113 break;
411e1bfb 2114 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2115 break;
411e1bfb 2116 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2117 break;
411e1bfb 2118 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2119 break;
411e1bfb 2120 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2121 break;
411e1bfb 2122 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2123 break;
411e1bfb 2124 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2125 break;
411e1bfb 2126 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2127 break;
411e1bfb 2128 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2129 break;
411e1bfb 2130 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2131 break;
411e1bfb 2132 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2133 break;
411e1bfb 2134 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2135 break;
411e1bfb 2136 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2137 break;
411e1bfb 2138 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2139 break;
411e1bfb 2140 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2141 break;
411e1bfb 2142 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2143 break;
411e1bfb 2144 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2145 break;
411e1bfb 2146 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2147 break;
411e1bfb 2148 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2149 break;
411e1bfb 2150 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2151 break;
411e1bfb 2152 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2153 break;
727fc41e
AM
2154 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2155 break;
2156 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2157 break;
411e1bfb 2158 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2159 break;
411e1bfb 2160 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2161 break;
411e1bfb 2162 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2163 break;
411e1bfb 2164 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2165 break;
411e1bfb 2166 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2167 break;
411e1bfb 2168 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2169 break;
411e1bfb
AM
2170 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2171 break;
2172 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2173 break;
2174 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2175 break;
2176 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2177 break;
2178 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2179 break;
2180 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2181 break;
2182 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2183 break;
2184 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2185 break;
2186 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2187 break;
2188 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2189 break;
2190 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2191 break;
2192 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2193 break;
2194 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2195 break;
2196 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2197 break;
2198 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2199 break;
2200 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2201 break;
2202 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2203 break;
2204 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2205 break;
2206 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2207 break;
2208 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2209 break;
2210 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2211 break;
2212 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2213 break;
2214 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2215 break;
2216 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2217 break;
2218 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2219 break;
2220 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2221 break;
2222 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2223 break;
2224 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2225 break;
2226 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2227 break;
2228 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2229 break;
2230 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2231 break;
2232 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2233 break;
2234 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2235 break;
25f23106
AM
2236 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2237 break;
2238 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2239 break;
2240 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2241 break;
2242 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2243 break;
411e1bfb
AM
2244 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2245 break;
2246 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2247 break;
2248 }
2249
4ce794b7 2250 return ppc64_elf_howto_table[r];
5bd4f169
AM
2251};
2252
157090f7
AM
2253static reloc_howto_type *
2254ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2255 const char *r_name)
2256{
2257 unsigned int i;
2258
2259 for (i = 0;
2260 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2261 i++)
2262 if (ppc64_elf_howto_raw[i].name != NULL
2263 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2264 return &ppc64_elf_howto_raw[i];
2265
2266 return NULL;
2267}
2268
5bd4f169
AM
2269/* Set the howto pointer for a PowerPC ELF reloc. */
2270
2271static void
4ce794b7
AM
2272ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2273 Elf_Internal_Rela *dst)
5bd4f169 2274{
65f38f15
AM
2275 unsigned int type;
2276
ef60b7ff 2277 /* Initialize howto table if needed. */
5bd4f169 2278 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2279 ppc_howto_init ();
2280
65f38f15 2281 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2282 if (type >= (sizeof (ppc64_elf_howto_table)
2283 / sizeof (ppc64_elf_howto_table[0])))
2284 {
2285 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2286 abfd, (int) type);
2287 type = R_PPC64_NONE;
d0fb9a8d 2288 }
65f38f15 2289 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2290}
2291
04c9666a 2292/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2293
2294static bfd_reloc_status_type
4ce794b7
AM
2295ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2296 void *data, asection *input_section,
2297 bfd *output_bfd, char **error_message)
5bd4f169 2298{
805fc799
AM
2299 /* If this is a relocatable link (output_bfd test tells us), just
2300 call the generic function. Any adjustment will be done at final
2301 link time. */
2302 if (output_bfd != NULL)
cedb70c5 2303 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2304 input_section, output_bfd, error_message);
2305
2306 /* Adjust the addend for sign extension of the low 16 bits.
2307 We won't actually be using the low 16 bits, so trashing them
2308 doesn't matter. */
2309 reloc_entry->addend += 0x8000;
2310 return bfd_reloc_continue;
2311}
5bd4f169 2312
2441e016
AM
2313static bfd_reloc_status_type
2314ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2315 void *data, asection *input_section,
2316 bfd *output_bfd, char **error_message)
2317{
2318 if (output_bfd != NULL)
2319 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2320 input_section, output_bfd, error_message);
2321
699733f6
AM
2322 if (strcmp (symbol->section->name, ".opd") == 0
2323 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2324 {
2325 bfd_vma dest = opd_entry_value (symbol->section,
2326 symbol->value + reloc_entry->addend,
2327 NULL, NULL);
2328 if (dest != (bfd_vma) -1)
2329 reloc_entry->addend = dest - (symbol->value
2330 + symbol->section->output_section->vma
2331 + symbol->section->output_offset);
2332 }
2333 return bfd_reloc_continue;
2334}
2335
805fc799 2336static bfd_reloc_status_type
4ce794b7
AM
2337ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2338 void *data, asection *input_section,
2339 bfd *output_bfd, char **error_message)
805fc799
AM
2340{
2341 long insn;
04c9666a 2342 enum elf_ppc64_reloc_type r_type;
805fc799
AM
2343 bfd_size_type octets;
2344 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 2345 bfd_boolean is_power4 = FALSE;
805fc799
AM
2346
2347 /* If this is a relocatable link (output_bfd test tells us), just
2348 call the generic function. Any adjustment will be done at final
2349 link time. */
5bd4f169 2350 if (output_bfd != NULL)
cedb70c5 2351 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2352 input_section, output_bfd, error_message);
2353
2354 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2355 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2356 insn &= ~(0x01 << 21);
4ce794b7 2357 r_type = reloc_entry->howto->type;
805fc799
AM
2358 if (r_type == R_PPC64_ADDR14_BRTAKEN
2359 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2360 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799
AM
2361
2362 if (is_power4)
5bd4f169 2363 {
805fc799
AM
2364 /* Set 'a' bit. This is 0b00010 in BO field for branch
2365 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2366 for branch on CTR insns (BO == 1a00t or 1a01t). */
2367 if ((insn & (0x14 << 21)) == (0x04 << 21))
2368 insn |= 0x02 << 21;
2369 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2370 insn |= 0x08 << 21;
2371 else
2441e016 2372 goto out;
5bd4f169 2373 }
805fc799
AM
2374 else
2375 {
2376 bfd_vma target = 0;
2377 bfd_vma from;
5bd4f169 2378
805fc799
AM
2379 if (!bfd_is_com_section (symbol->section))
2380 target = symbol->value;
2381 target += symbol->section->output_section->vma;
2382 target += symbol->section->output_offset;
2383 target += reloc_entry->addend;
5bd4f169 2384
805fc799
AM
2385 from = (reloc_entry->address
2386 + input_section->output_offset
2387 + input_section->output_section->vma);
5bd4f169 2388
805fc799
AM
2389 /* Invert 'y' bit if not the default. */
2390 if ((bfd_signed_vma) (target - from) < 0)
2391 insn ^= 0x01 << 21;
2392 }
4ce794b7 2393 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2394 out:
2395 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2396 input_section, output_bfd, error_message);
805fc799 2397}
5bd4f169 2398
805fc799 2399static bfd_reloc_status_type
4ce794b7
AM
2400ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2401 void *data, asection *input_section,
2402 bfd *output_bfd, char **error_message)
805fc799
AM
2403{
2404 /* If this is a relocatable link (output_bfd test tells us), just
2405 call the generic function. Any adjustment will be done at final
2406 link time. */
2407 if (output_bfd != NULL)
cedb70c5 2408 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2409 input_section, output_bfd, error_message);
5bd4f169 2410
805fc799
AM
2411 /* Subtract the symbol section base address. */
2412 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2413 return bfd_reloc_continue;
2414}
2415
805fc799 2416static bfd_reloc_status_type
4ce794b7
AM
2417ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2418 void *data, asection *input_section,
2419 bfd *output_bfd, char **error_message)
805fc799
AM
2420{
2421 /* If this is a relocatable link (output_bfd test tells us), just
2422 call the generic function. Any adjustment will be done at final
2423 link time. */
2424 if (output_bfd != NULL)
cedb70c5 2425 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2426 input_section, output_bfd, error_message);
2427
2428 /* Subtract the symbol section base address. */
2429 reloc_entry->addend -= symbol->section->output_section->vma;
2430
2431 /* Adjust the addend for sign extension of the low 16 bits. */
2432 reloc_entry->addend += 0x8000;
2433 return bfd_reloc_continue;
2434}
2435
2436static bfd_reloc_status_type
4ce794b7
AM
2437ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2438 void *data, asection *input_section,
2439 bfd *output_bfd, char **error_message)
805fc799
AM
2440{
2441 bfd_vma TOCstart;
2442
2443 /* If this is a relocatable link (output_bfd test tells us), just
2444 call the generic function. Any adjustment will be done at final
2445 link time. */
2446 if (output_bfd != NULL)
cedb70c5 2447 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2448 input_section, output_bfd, error_message);
2449
2450 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2451 if (TOCstart == 0)
2452 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2453
2454 /* Subtract the TOC base address. */
2455 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2456 return bfd_reloc_continue;
2457}
2458
2459static bfd_reloc_status_type
4ce794b7
AM
2460ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2461 void *data, asection *input_section,
2462 bfd *output_bfd, char **error_message)
805fc799
AM
2463{
2464 bfd_vma TOCstart;
2465
2466 /* If this is a relocatable link (output_bfd test tells us), just
2467 call the generic function. Any adjustment will be done at final
2468 link time. */
2469 if (output_bfd != NULL)
cedb70c5 2470 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2471 input_section, output_bfd, error_message);
2472
2473 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2474 if (TOCstart == 0)
2475 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2476
2477 /* Subtract the TOC base address. */
2478 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2479
2480 /* Adjust the addend for sign extension of the low 16 bits. */
2481 reloc_entry->addend += 0x8000;
2482 return bfd_reloc_continue;
2483}
2484
2485static bfd_reloc_status_type
4ce794b7
AM
2486ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487 void *data, asection *input_section,
2488 bfd *output_bfd, char **error_message)
805fc799
AM
2489{
2490 bfd_vma TOCstart;
2491 bfd_size_type octets;
2492
2493 /* If this is a relocatable link (output_bfd test tells us), just
2494 call the generic function. Any adjustment will be done at final
2495 link time. */
2496 if (output_bfd != NULL)
cedb70c5 2497 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2498 input_section, output_bfd, error_message);
2499
2500 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2501 if (TOCstart == 0)
2502 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2503
2504 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2505 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2506 return bfd_reloc_ok;
2507}
2508
2509static bfd_reloc_status_type
4ce794b7
AM
2510ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2511 void *data, asection *input_section,
2512 bfd *output_bfd, char **error_message)
805fc799
AM
2513{
2514 /* If this is a relocatable link (output_bfd test tells us), just
2515 call the generic function. Any adjustment will be done at final
2516 link time. */
2517 if (output_bfd != NULL)
cedb70c5 2518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2519 input_section, output_bfd, error_message);
2520
2521 if (error_message != NULL)
2522 {
2523 static char buf[60];
2524 sprintf (buf, "generic linker can't handle %s",
2525 reloc_entry->howto->name);
2526 *error_message = buf;
2527 }
2528 return bfd_reloc_dangerous;
2529}
2530
927be08e
AM
2531/* Track GOT entries needed for a given symbol. We might need more
2532 than one got entry per symbol. */
2533struct got_entry
2534{
2535 struct got_entry *next;
2536
2537 /* The symbol addend that we'll be placing in the GOT. */
2538 bfd_vma addend;
2539
2540 /* Unlike other ELF targets, we use separate GOT entries for the same
2541 symbol referenced from different input files. This is to support
2542 automatic multiple TOC/GOT sections, where the TOC base can vary
2543 from one input file to another. After partitioning into TOC groups
2544 we merge entries within the group.
2545
2546 Point to the BFD owning this GOT entry. */
2547 bfd *owner;
2548
2549 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2550 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2551 unsigned char tls_type;
927be08e
AM
2552
2553 /* Non-zero if got.ent points to real entry. */
f961d9dd 2554 unsigned char is_indirect;
927be08e
AM
2555
2556 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2557 union
2558 {
2559 bfd_signed_vma refcount;
2560 bfd_vma offset;
2561 struct got_entry *ent;
2562 } got;
2563};
2564
2565/* The same for PLT. */
2566struct plt_entry
2567{
2568 struct plt_entry *next;
2569
2570 bfd_vma addend;
2571
2572 union
2573 {
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 } plt;
2577};
2578
e717da7e
AM
2579struct ppc64_elf_obj_tdata
2580{
2581 struct elf_obj_tdata elf;
2582
2583 /* Shortcuts to dynamic linker sections. */
2584 asection *got;
2585 asection *relgot;
2586
b3fac117
AM
2587 /* Used during garbage collection. We attach global symbols defined
2588 on removed .opd entries to this section so that the sym is removed. */
2589 asection *deleted_section;
81688140 2590
927be08e 2591 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2592 sections means we potentially need one of these for each input bfd. */
927be08e 2593 struct got_entry tlsld_got;
8860955f
AM
2594
2595 /* A copy of relocs before they are modified for --emit-relocs. */
2596 Elf_Internal_Rela *opd_relocs;
d77c8a4b
AM
2597
2598 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2599 the reloc to be in the range -32768 to 32767. */
2600 unsigned int has_small_toc_reloc;
e717da7e
AM
2601};
2602
2603#define ppc64_elf_tdata(bfd) \
2604 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2605
2606#define ppc64_tlsld_got(bfd) \
2607 (&ppc64_elf_tdata (bfd)->tlsld_got)
2608
0c8d6e5c
AM
2609#define is_ppc64_elf(bfd) \
2610 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2611 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2612
e717da7e
AM
2613/* Override the generic function because we store some extras. */
2614
2615static bfd_boolean
2616ppc64_elf_mkobject (bfd *abfd)
2617{
0ffa91dd 2618 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2619 PPC64_ELF_DATA);
e717da7e
AM
2620}
2621
feee612b
AM
2622/* Fix bad default arch selected for a 64 bit input bfd when the
2623 default is 32 bit. */
2624
b34976b6 2625static bfd_boolean
4ce794b7 2626ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2627{
2628 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2629 {
2630 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2631
2632 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2633 {
2634 /* Relies on arch after 32 bit default being 64 bit default. */
2635 abfd->arch_info = abfd->arch_info->next;
2636 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2637 }
2638 }
b34976b6 2639 return TRUE;
feee612b
AM
2640}
2641
d37c89e5
AM
2642/* Support for core dump NOTE sections. */
2643
2644static bfd_boolean
2645ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2646{
eea6121a 2647 size_t offset, size;
d37c89e5
AM
2648
2649 if (note->descsz != 504)
2650 return FALSE;
2651
2652 /* pr_cursig */
2653 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2654
2655 /* pr_pid */
261b8d08 2656 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2657
2658 /* pr_reg */
2659 offset = 112;
eea6121a 2660 size = 384;
d37c89e5
AM
2661
2662 /* Make a ".reg/999" section. */
2663 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2664 size, note->descpos + offset);
d37c89e5
AM
2665}
2666
2667static bfd_boolean
2668ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2669{
2670 if (note->descsz != 136)
2671 return FALSE;
2672
bc989cdc
JK
2673 elf_tdata (abfd)->core_pid
2674 = bfd_get_32 (abfd, note->descdata + 24);
d37c89e5
AM
2675 elf_tdata (abfd)->core_program
2676 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2677 elf_tdata (abfd)->core_command
2678 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2679
2680 return TRUE;
2681}
2682
183e98be
AM
2683static char *
2684ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2685 ...)
2686{
2687 switch (note_type)
2688 {
2689 default:
2690 return NULL;
2691
2692 case NT_PRPSINFO:
2693 {
2694 char data[136];
2695 va_list ap;
2696
2697 va_start (ap, note_type);
2698 memset (data, 0, 40);
2699 strncpy (data + 40, va_arg (ap, const char *), 16);
2700 strncpy (data + 56, va_arg (ap, const char *), 80);
2701 va_end (ap);
2702 return elfcore_write_note (abfd, buf, bufsiz,
2703 "CORE", note_type, data, sizeof (data));
2704 }
2705
2706 case NT_PRSTATUS:
2707 {
2708 char data[504];
2709 va_list ap;
2710 long pid;
2711 int cursig;
2712 const void *greg;
2713
2714 va_start (ap, note_type);
2715 memset (data, 0, 112);
2716 pid = va_arg (ap, long);
2717 bfd_put_32 (abfd, pid, data + 32);
2718 cursig = va_arg (ap, int);
2719 bfd_put_16 (abfd, cursig, data + 12);
2720 greg = va_arg (ap, const void *);
2721 memcpy (data + 112, greg, 384);
2722 memset (data + 496, 0, 8);
2723 va_end (ap);
2724 return elfcore_write_note (abfd, buf, bufsiz,
2725 "CORE", note_type, data, sizeof (data));
2726 }
2727 }
2728}
2729
5d35169e
AM
2730/* Add extra PPC sections. */
2731
b35d266b 2732static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2733{
0112cd26
NC
2734 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2735 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2736 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2737 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2738 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2739 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2740 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2741};
2742
7c8fe5c4
AM
2743enum _ppc64_sec_type {
2744 sec_normal = 0,
2745 sec_opd = 1,
2746 sec_toc = 2
2747};
2748
f0abc2a1
AM
2749struct _ppc64_elf_section_data
2750{
2751 struct bfd_elf_section_data elf;
411e1bfb 2752
f0abc2a1
AM
2753 union
2754 {
74f0fb50
AM
2755 /* An array with one entry for each opd function descriptor. */
2756 struct _opd_sec_data
2757 {
2758 /* Points to the function code section for local opd entries. */
2759 asection **func_sec;
2760
2761 /* After editing .opd, adjust references to opd local syms. */
2762 long *adjust;
2763 } opd;
7c8fe5c4 2764
3a71aa26
AM
2765 /* An array for toc sections, indexed by offset/8. */
2766 struct _toc_sec_data
2767 {
2768 /* Specifies the relocation symbol index used at a given toc offset. */
2769 unsigned *symndx;
2770
2771 /* And the relocation addend. */
2772 bfd_vma *add;
2773 } toc;
7c8fe5c4
AM
2774 } u;
2775
2776 enum _ppc64_sec_type sec_type:2;
411e1bfb 2777
7c8fe5c4
AM
2778 /* Flag set when small branches are detected. Used to
2779 select suitable defaults for the stub group size. */
2780 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2781};
2782
2783#define ppc64_elf_section_data(sec) \
411e1bfb 2784 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2785
2786static bfd_boolean
4ce794b7 2787ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2788{
f592407e
AM
2789 if (!sec->used_by_bfd)
2790 {
2791 struct _ppc64_elf_section_data *sdata;
2792 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2793
f592407e
AM
2794 sdata = bfd_zalloc (abfd, amt);
2795 if (sdata == NULL)
2796 return FALSE;
2797 sec->used_by_bfd = sdata;
2798 }
f0abc2a1
AM
2799
2800 return _bfd_elf_new_section_hook (abfd, sec);
2801}
4025353c 2802
74f0fb50 2803static struct _opd_sec_data *
4025353c
AM
2804get_opd_info (asection * sec)
2805{
2806 if (sec != NULL
2807 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2808 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2809 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2810 return NULL;
2811}
90e3cdf2
JJ
2812\f
2813/* Parameters for the qsort hook. */
90e3cdf2
JJ
2814static bfd_boolean synthetic_relocatable;
2815
699733f6 2816/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2817
2818static int
2819compare_symbols (const void *ap, const void *bp)
2820{
2821 const asymbol *a = * (const asymbol **) ap;
2822 const asymbol *b = * (const asymbol **) bp;
2823
699733f6
AM
2824 /* Section symbols first. */
2825 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2826 return -1;
699733f6 2827 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2828 return 1;
2829
699733f6 2830 /* then .opd symbols. */
ffcfec52
AM
2831 if (strcmp (a->section->name, ".opd") == 0
2832 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2833 return -1;
ffcfec52
AM
2834 if (strcmp (a->section->name, ".opd") != 0
2835 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2836 return 1;
2837
699733f6 2838 /* then other code symbols. */
90e3cdf2
JJ
2839 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2840 == (SEC_CODE | SEC_ALLOC)
2841 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2842 != (SEC_CODE | SEC_ALLOC))
2843 return -1;
2844
2845 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2846 != (SEC_CODE | SEC_ALLOC)
2847 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2848 == (SEC_CODE | SEC_ALLOC))
2849 return 1;
2850
2851 if (synthetic_relocatable)
2852 {
2853 if (a->section->id < b->section->id)
2854 return -1;
2855
2856 if (a->section->id > b->section->id)
2857 return 1;
2858 }
2859
2860 if (a->value + a->section->vma < b->value + b->section->vma)
2861 return -1;
2862
2863 if (a->value + a->section->vma > b->value + b->section->vma)
2864 return 1;
2865
4d35a0aa
AM
2866 /* For syms with the same value, prefer strong dynamic global function
2867 syms over other syms. */
2868 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2869 return -1;
2870
2871 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2872 return 1;
2873
2874 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2875 return -1;
2876
2877 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2878 return 1;
2879
2880 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2881 return -1;
2882
2883 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2884 return 1;
2885
2886 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2887 return -1;
2888
2889 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2890 return 1;
2891
90e3cdf2
JJ
2892 return 0;
2893}
2894
699733f6 2895/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2896
699733f6
AM
2897static asymbol *
2898sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2899{
699733f6 2900 long mid;
90e3cdf2 2901
699733f6
AM
2902 if (id == -1)
2903 {
2904 while (lo < hi)
2905 {
2906 mid = (lo + hi) >> 1;
2907 if (syms[mid]->value + syms[mid]->section->vma < value)
2908 lo = mid + 1;
2909 else if (syms[mid]->value + syms[mid]->section->vma > value)
2910 hi = mid;
2911 else
2912 return syms[mid];
2913 }
2914 }
2915 else
2916 {
2917 while (lo < hi)
2918 {
2919 mid = (lo + hi) >> 1;
2920 if (syms[mid]->section->id < id)
2921 lo = mid + 1;
2922 else if (syms[mid]->section->id > id)
2923 hi = mid;
2924 else if (syms[mid]->value < value)
2925 lo = mid + 1;
2926 else if (syms[mid]->value > value)
2927 hi = mid;
2928 else
2929 return syms[mid];
2930 }
2931 }
2932 return NULL;
90e3cdf2
JJ
2933}
2934
468392fb
AM
2935static bfd_boolean
2936section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2937{
2938 bfd_vma vma = *(bfd_vma *) ptr;
2939 return ((section->flags & SEC_ALLOC) != 0
2940 && section->vma <= vma
2941 && vma < section->vma + section->size);
2942}
2943
699733f6 2944/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 2945 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
2946
2947static long
a7535cf3
AM
2948ppc64_elf_get_synthetic_symtab (bfd *abfd,
2949 long static_count, asymbol **static_syms,
2950 long dyn_count, asymbol **dyn_syms,
c9727e01 2951 asymbol **ret)
90e3cdf2
JJ
2952{
2953 asymbol *s;
699733f6
AM
2954 long i;
2955 long count;
90e3cdf2 2956 char *names;
a7535cf3 2957 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2958 asection *opd;
90e3cdf2 2959 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2960 asymbol **syms;
90e3cdf2
JJ
2961
2962 *ret = NULL;
2963
2964 opd = bfd_get_section_by_name (abfd, ".opd");
2965 if (opd == NULL)
2966 return 0;
2967
a7535cf3 2968 symcount = static_count;
c9727e01 2969 if (!relocatable)
a7535cf3 2970 symcount += dyn_count;
90e3cdf2 2971 if (symcount == 0)
c9727e01 2972 return 0;
90e3cdf2 2973
a7535cf3
AM
2974 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2975 if (syms == NULL)
7356fed5 2976 return -1;
a7535cf3
AM
2977
2978 if (!relocatable && static_count != 0 && dyn_count != 0)
2979 {
2980 /* Use both symbol tables. */
2981 memcpy (syms, static_syms, static_count * sizeof (*syms));
2982 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2983 }
2984 else if (!relocatable && static_count == 0)
2985 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2986 else
2987 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2988
90e3cdf2 2989 synthetic_relocatable = relocatable;
595da8c5 2990 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2991
c9727e01
AM
2992 if (!relocatable && symcount > 1)
2993 {
2994 long j;
2995 /* Trim duplicate syms, since we may have merged the normal and
2996 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 2997 different values, so trim any with the same value. */
c9727e01
AM
2998 for (i = 1, j = 1; i < symcount; ++i)
2999 if (syms[i - 1]->value + syms[i - 1]->section->vma
3000 != syms[i]->value + syms[i]->section->vma)
3001 syms[j++] = syms[i];
3002 symcount = j;
3003 }
3004
699733f6 3005 i = 0;
ffcfec52 3006 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3007 ++i;
3008 codesecsym = i;
90e3cdf2 3009
699733f6
AM
3010 for (; i < symcount; ++i)
3011 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3012 != (SEC_CODE | SEC_ALLOC))
3013 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3014 break;
3015 codesecsymend = i;
90e3cdf2 3016
699733f6
AM
3017 for (; i < symcount; ++i)
3018 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3019 break;
3020 secsymend = i;
90e3cdf2 3021
699733f6 3022 for (; i < symcount; ++i)
ffcfec52 3023 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3024 break;
3025 opdsymend = i;
90e3cdf2 3026
699733f6
AM
3027 for (; i < symcount; ++i)
3028 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3029 != (SEC_CODE | SEC_ALLOC))
3030 break;
3031 symcount = i;
3032
c9727e01 3033 count = 0;
90e3cdf2 3034
699733f6 3035 if (relocatable)
90e3cdf2 3036 {
699733f6
AM
3037 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3038 arelent *r;
3039 size_t size;
3040 long relcount;
90e3cdf2 3041
468392fb
AM
3042 if (opdsymend == secsymend)
3043 goto done;
3044
699733f6 3045 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3046 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3047 if (relcount == 0)
c9727e01 3048 goto done;
90e3cdf2 3049
7356fed5
AM
3050 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3051 {
3052 count = -1;
3053 goto done;
3054 }
3055
699733f6 3056 size = 0;
595da8c5 3057 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3058 {
3059 asymbol *sym;
90e3cdf2 3060
595da8c5 3061 while (r < opd->relocation + relcount
699733f6
AM
3062 && r->address < syms[i]->value + opd->vma)
3063 ++r;
90e3cdf2 3064
595da8c5 3065 if (r == opd->relocation + relcount)
699733f6 3066 break;
90e3cdf2 3067
699733f6
AM
3068 if (r->address != syms[i]->value + opd->vma)
3069 continue;
90e3cdf2 3070
699733f6
AM
3071 if (r->howto->type != R_PPC64_ADDR64)
3072 continue;
90e3cdf2 3073
699733f6
AM
3074 sym = *r->sym_ptr_ptr;
3075 if (!sym_exists_at (syms, opdsymend, symcount,
3076 sym->section->id, sym->value + r->addend))
3077 {
3078 ++count;
3079 size += sizeof (asymbol);
3080 size += strlen (syms[i]->name) + 2;
3081 }
3082 }
90e3cdf2 3083
699733f6
AM
3084 s = *ret = bfd_malloc (size);
3085 if (s == NULL)
3086 {
7356fed5 3087 count = -1;
c9727e01 3088 goto done;
699733f6 3089 }
90e3cdf2 3090
699733f6 3091 names = (char *) (s + count);
90e3cdf2 3092
595da8c5 3093 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3094 {
699733f6 3095 asymbol *sym;
90e3cdf2 3096
595da8c5 3097 while (r < opd->relocation + relcount
699733f6
AM
3098 && r->address < syms[i]->value + opd->vma)
3099 ++r;
90e3cdf2 3100
595da8c5 3101 if (r == opd->relocation + relcount)
699733f6
AM
3102 break;
3103
3104 if (r->address != syms[i]->value + opd->vma)
3105 continue;
3106
3107 if (r->howto->type != R_PPC64_ADDR64)
3108 continue;
90e3cdf2 3109
699733f6
AM
3110 sym = *r->sym_ptr_ptr;
3111 if (!sym_exists_at (syms, opdsymend, symcount,
3112 sym->section->id, sym->value + r->addend))
3113 {
3114 size_t len;
3115
3116 *s = *syms[i];
6ba2a415 3117 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3118 s->section = sym->section;
3119 s->value = sym->value + r->addend;
3120 s->name = names;
3121 *names++ = '.';
3122 len = strlen (syms[i]->name);
3123 memcpy (names, syms[i]->name, len + 1);
3124 names += len + 1;
6f610d07
UW
3125 /* Have udata.p point back to the original symbol this
3126 synthetic symbol was derived from. */
3127 s->udata.p = syms[i];
699733f6
AM
3128 s++;
3129 }
3130 }
3131 }
3132 else
90e3cdf2 3133 {
468392fb 3134 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
699733f6
AM
3135 bfd_byte *contents;
3136 size_t size;
468392fb
AM
3137 long plt_count = 0;
3138 bfd_vma glink_vma = 0, resolv_vma = 0;
3139 asection *dynamic, *glink = NULL, *relplt = NULL;
3140 arelent *p;
90e3cdf2 3141
699733f6
AM
3142 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3143 {
3144 if (contents)
c9727e01
AM
3145 {
3146 free_contents_and_exit:
3147 free (contents);
3148 }
7356fed5 3149 count = -1;
c9727e01 3150 goto done;
699733f6 3151 }
90e3cdf2 3152
699733f6
AM
3153 size = 0;
3154 for (i = secsymend; i < opdsymend; ++i)
3155 {
3156 bfd_vma ent;
90e3cdf2 3157
5ef11c02
AM
3158 /* Ignore bogus symbols. */
3159 if (syms[i]->value > opd->size - 8)
3160 continue;
3161
699733f6
AM
3162 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3163 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3164 {
3165 ++count;
3166 size += sizeof (asymbol);
3167 size += strlen (syms[i]->name) + 2;
3168 }
3169 }
90e3cdf2 3170
468392fb 3171 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3172 if (dyn_count != 0
3173 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3174 {
3175 bfd_byte *dynbuf, *extdyn, *extdynend;
3176 size_t extdynsize;
3177 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3178
3179 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3180 goto free_contents_and_exit;
3181
3182 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3183 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3184
3185 extdyn = dynbuf;
3186 extdynend = extdyn + dynamic->size;
3187 for (; extdyn < extdynend; extdyn += extdynsize)
3188 {
3189 Elf_Internal_Dyn dyn;
3190 (*swap_dyn_in) (abfd, extdyn, &dyn);
3191
3192 if (dyn.d_tag == DT_NULL)
3193 break;
3194
3195 if (dyn.d_tag == DT_PPC64_GLINK)
3196 {
3197 /* The first glink stub starts at offset 32; see comment in
3198 ppc64_elf_finish_dynamic_sections. */
3199 glink_vma = dyn.d_un.d_val + 32;
3200 /* The .glink section usually does not survive the final
3201 link; search for the section (usually .text) where the
3202 glink stubs now reside. */
3203 glink = bfd_sections_find_if (abfd, section_covers_vma,
3204 &glink_vma);
3205 break;
3206 }
3207 }
3208
3209 free (dynbuf);
3210 }
3211
3212 if (glink != NULL)
3213 {
3214 /* Determine __glink trampoline by reading the relative branch
3215 from the first glink stub. */
3216 bfd_byte buf[4];
3217 if (bfd_get_section_contents (abfd, glink, buf,
3218 glink_vma + 4 - glink->vma, 4))
3219 {
3220 unsigned int insn = bfd_get_32 (abfd, buf);
3221 insn ^= B_DOT;
3222 if ((insn & ~0x3fffffc) == 0)
3223 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3224 }
3225
3226 if (resolv_vma)
3227 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3228
066ee829
AM
3229 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3230 if (relplt != NULL)
3231 {
3232 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3233 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3234 goto free_contents_and_exit;
468392fb 3235
066ee829
AM
3236 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3237 size += plt_count * sizeof (asymbol);
468392fb 3238
066ee829
AM
3239 p = relplt->relocation;
3240 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3241 {
3242 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3243 if (p->addend != 0)
3244 size += sizeof ("+0x") - 1 + 16;
3245 }
066ee829 3246 }
468392fb
AM
3247 }
3248
699733f6
AM
3249 s = *ret = bfd_malloc (size);
3250 if (s == NULL)
7356fed5 3251 goto free_contents_and_exit;
90e3cdf2 3252
468392fb 3253 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3254
699733f6 3255 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3256 {
699733f6 3257 bfd_vma ent;
90e3cdf2 3258
5ef11c02
AM
3259 if (syms[i]->value > opd->size - 8)
3260 continue;
3261
699733f6
AM
3262 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3263 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3264 {
c9727e01 3265 long lo, hi;
699733f6 3266 size_t len;
c9727e01 3267 asection *sec = abfd->sections;
90e3cdf2 3268
699733f6
AM
3269 *s = *syms[i];
3270 lo = codesecsym;
3271 hi = codesecsymend;
3272 while (lo < hi)
3273 {
c9727e01 3274 long mid = (lo + hi) >> 1;
699733f6
AM
3275 if (syms[mid]->section->vma < ent)
3276 lo = mid + 1;
3277 else if (syms[mid]->section->vma > ent)
3278 hi = mid;
3279 else
c9727e01
AM
3280 {
3281 sec = syms[mid]->section;
3282 break;
3283 }
699733f6
AM
3284 }
3285
c9727e01 3286 if (lo >= hi && lo > codesecsym)
699733f6 3287 sec = syms[lo - 1]->section;
699733f6
AM
3288
3289 for (; sec != NULL; sec = sec->next)
3290 {
3291 if (sec->vma > ent)
3292 break;
63524580
JK
3293 /* SEC_LOAD may not be set if SEC is from a separate debug
3294 info file. */
3295 if ((sec->flags & SEC_ALLOC) == 0)
699733f6
AM
3296 break;
3297 if ((sec->flags & SEC_CODE) != 0)
3298 s->section = sec;
3299 }
6ba2a415 3300 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3301 s->value = ent - s->section->vma;
3302 s->name = names;
3303 *names++ = '.';
3304 len = strlen (syms[i]->name);
3305 memcpy (names, syms[i]->name, len + 1);
3306 names += len + 1;
6f610d07
UW
3307 /* Have udata.p point back to the original symbol this
3308 synthetic symbol was derived from. */
3309 s->udata.p = syms[i];
699733f6 3310 s++;
90e3cdf2 3311 }
90e3cdf2 3312 }
699733f6 3313 free (contents);
468392fb
AM
3314
3315 if (glink != NULL && relplt != NULL)
3316 {
3317 if (resolv_vma)
3318 {
3319 /* Add a symbol for the main glink trampoline. */
86a4952b 3320 memset (s, 0, sizeof *s);
468392fb 3321 s->the_bfd = abfd;
6ba2a415 3322 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3323 s->section = glink;
3324 s->value = resolv_vma - glink->vma;
3325 s->name = names;
3326 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3327 names += sizeof ("__glink_PLTresolve");
3328 s++;
3329 count++;
3330 }
3331
3332 /* FIXME: It would be very much nicer to put sym@plt on the
3333 stub rather than on the glink branch table entry. The
3334 objdump disassembler would then use a sensible symbol
3335 name on plt calls. The difficulty in doing so is
3336 a) finding the stubs, and,
3337 b) matching stubs against plt entries, and,
3338 c) there can be multiple stubs for a given plt entry.
3339
3340 Solving (a) could be done by code scanning, but older
3341 ppc64 binaries used different stubs to current code.
3342 (b) is the tricky one since you need to known the toc
3343 pointer for at least one function that uses a pic stub to
3344 be able to calculate the plt address referenced.
3345 (c) means gdb would need to set multiple breakpoints (or
3346 find the glink branch itself) when setting breakpoints
3347 for pending shared library loads. */
3348 p = relplt->relocation;
3349 for (i = 0; i < plt_count; i++, p++)
3350 {
3351 size_t len;
3352
3353 *s = **p->sym_ptr_ptr;
3354 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3355 we are defining a symbol, ensure one of them is set. */
3356 if ((s->flags & BSF_LOCAL) == 0)
3357 s->flags |= BSF_GLOBAL;
6ba2a415 3358 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3359 s->section = glink;
3360 s->value = glink_vma - glink->vma;
3361 s->name = names;
3362 s->udata.p = NULL;
3363 len = strlen ((*p->sym_ptr_ptr)->name);
3364 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3365 names += len;
e054468f
AM
3366 if (p->addend != 0)
3367 {
3368 memcpy (names, "+0x", sizeof ("+0x") - 1);
3369 names += sizeof ("+0x") - 1;
3370 bfd_sprintf_vma (abfd, names, p->addend);
3371 names += strlen (names);
3372 }
468392fb
AM
3373 memcpy (names, "@plt", sizeof ("@plt"));
3374 names += sizeof ("@plt");
3375 s++;
3376 glink_vma += 8;
3377 if (i >= 0x8000)
3378 glink_vma += 4;
3379 }
3380 count += plt_count;
3381 }
90e3cdf2
JJ
3382 }
3383
c9727e01 3384 done:
a7535cf3 3385 free (syms);
90e3cdf2
JJ
3386 return count;
3387}
5bd4f169 3388\f
65f38f15
AM
3389/* The following functions are specific to the ELF linker, while
3390 functions above are used generally. Those named ppc64_elf_* are
3391 called by the main ELF linker code. They appear in this file more
3392 or less in the order in which they are called. eg.
3393 ppc64_elf_check_relocs is called early in the link process,
3394 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3395 called.
3396
3397 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3398 functions have both a function code symbol and a function descriptor
3399 symbol. A call to foo in a relocatable object file looks like:
3400
3401 . .text
3402 . x:
3403 . bl .foo
3404 . nop
3405
3406 The function definition in another object file might be:
3407
3408 . .section .opd
3409 . foo: .quad .foo
3410 . .quad .TOC.@tocbase
3411 . .quad 0
3412 .
3413 . .text
3414 . .foo: blr
3415
3416 When the linker resolves the call during a static link, the branch
3417 unsurprisingly just goes to .foo and the .opd information is unused.
3418 If the function definition is in a shared library, things are a little
3419 different: The call goes via a plt call stub, the opd information gets
3420 copied to the plt, and the linker patches the nop.
3421
3422 . x:
3423 . bl .foo_stub
3424 . ld 2,40(1)
3425 .
3426 .
3427 . .foo_stub:
3428 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 3429 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
3430 . std 2,40(1) # this is the general idea
3431 . ld 11,0(12)
3432 . ld 2,8(12)
3433 . mtctr 11
3434 . ld 11,16(12)
3435 . bctr
3436 .
3437 . .section .plt
3438 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3439
3440 The "reloc ()" notation is supposed to indicate that the linker emits
3441 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3442 copying.
3443
3444 What are the difficulties here? Well, firstly, the relocations
3445 examined by the linker in check_relocs are against the function code
3446 sym .foo, while the dynamic relocation in the plt is emitted against
3447 the function descriptor symbol, foo. Somewhere along the line, we need
3448 to carefully copy dynamic link information from one symbol to the other.
3449 Secondly, the generic part of the elf linker will make .foo a dynamic
3450 symbol as is normal for most other backends. We need foo dynamic
3451 instead, at least for an application final link. However, when
3452 creating a shared library containing foo, we need to have both symbols
3453 dynamic so that references to .foo are satisfied during the early
3454 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3455 definition from some other object, eg. a static library.
3456
3457 Update: As of August 2004, we support a new convention. Function
3458 calls may use the function descriptor symbol, ie. "bl foo". This
3459 behaves exactly as "bl .foo". */
65f38f15 3460
1d483afe 3461/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3462 selects those that must be copied when linking a shared library,
3463 even when the symbol is local. */
65f38f15 3464
1d483afe
AM
3465static int
3466must_be_dyn_reloc (struct bfd_link_info *info,
3467 enum elf_ppc64_reloc_type r_type)
3468{
3469 switch (r_type)
3470 {
3471 default:
3472 return 1;
3473
3474 case R_PPC64_REL32:
3475 case R_PPC64_REL64:
3476 case R_PPC64_REL30:
3477 return 0;
3478
3479 case R_PPC64_TPREL16:
3480 case R_PPC64_TPREL16_LO:
3481 case R_PPC64_TPREL16_HI:
3482 case R_PPC64_TPREL16_HA:
3483 case R_PPC64_TPREL16_DS:
3484 case R_PPC64_TPREL16_LO_DS:
3485 case R_PPC64_TPREL16_HIGHER:
3486 case R_PPC64_TPREL16_HIGHERA:
3487 case R_PPC64_TPREL16_HIGHEST:
3488 case R_PPC64_TPREL16_HIGHESTA:
3489 case R_PPC64_TPREL64:
3490 return !info->executable;
3491 }
3492}
65f38f15 3493
f4656909
AM
3494/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3495 copying dynamic variables from a shared lib into an app's dynbss
3496 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3497 shared lib. With code that gcc generates, it's vital that this be
3498 enabled; In the PowerPC64 ABI, the address of a function is actually
3499 the address of a function descriptor, which resides in the .opd
3500 section. gcc uses the descriptor directly rather than going via the
3501 GOT as some other ABI's do, which means that initialized function
3502 pointers must reference the descriptor. Thus, a function pointer
3503 initialized to the address of a function in a shared library will
3504 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3505 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3506 presents a problem as a plt entry for that function is also
3507 initialized from the function descriptor symbol and the copy reloc
3508 may not be initialized first. */
a23b6845 3509#define ELIMINATE_COPY_RELOCS 1
f4656909 3510
721956f4
AM
3511/* Section name for stubs is the associated section name plus this
3512 string. */
3513#define STUB_SUFFIX ".stub"
3514
3515/* Linker stubs.
3516 ppc_stub_long_branch:
3517 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3518 destination, but a 24 bit branch in a stub section will reach.
3519 . b dest
3520
3521 ppc_stub_plt_branch:
3522 Similar to the above, but a 24 bit branch in the stub section won't
3523 reach its destination.
87e226ce
AM
3524 . addis %r12,%r2,xxx@toc@ha
3525 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3526 . mtctr %r11
3527 . bctr
3528
3529 ppc_stub_plt_call:
2c66dc6c
AM
3530 Used to call a function in a shared library. If it so happens that
3531 the plt entry referenced crosses a 64k boundary, then an extra
ac2df442 3532 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
87e226ce 3533 . addis %r12,%r2,xxx@toc@ha
721956f4 3534 . std %r2,40(%r1)
87e226ce 3535 . ld %r11,xxx+0@toc@l(%r12)
721956f4 3536 . mtctr %r11
ac2df442 3537 . ld %r2,xxx+8@toc@l(%r12)
87e226ce 3538 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3539 . bctr
ad8e1ba5
AM
3540
3541 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3542 code to adjust the value and save r2 to support multiple toc sections.
3543 A ppc_stub_long_branch with an r2 offset looks like:
3544 . std %r2,40(%r1)
3545 . addis %r2,%r2,off@ha
3546 . addi %r2,%r2,off@l
3547 . b dest
3548
3549 A ppc_stub_plt_branch with an r2 offset looks like:
3550 . std %r2,40(%r1)
3551 . addis %r12,%r2,xxx@toc@ha
3552 . ld %r11,xxx@toc@l(%r12)
3553 . addis %r2,%r2,off@ha
3554 . addi %r2,%r2,off@l
3555 . mtctr %r11
3556 . bctr
ac2df442
AM
3557
3558 In cases where the "addis" instruction would add zero, the "addis" is
3559 omitted and following instructions modified slightly in some cases.
721956f4
AM
3560*/
3561
3562enum ppc_stub_type {
3563 ppc_stub_none,
3564 ppc_stub_long_branch,
ad8e1ba5 3565 ppc_stub_long_branch_r2off,
721956f4 3566 ppc_stub_plt_branch,
ad8e1ba5 3567 ppc_stub_plt_branch_r2off,
721956f4
AM
3568 ppc_stub_plt_call
3569};
3570
3571struct ppc_stub_hash_entry {
3572
3573 /* Base hash table entry structure. */
3574 struct bfd_hash_entry root;
3575
ad8e1ba5
AM
3576 enum ppc_stub_type stub_type;
3577
721956f4
AM
3578 /* The stub section. */
3579 asection *stub_sec;
3580
3581 /* Offset within stub_sec of the beginning of this stub. */
3582 bfd_vma stub_offset;
3583
3584 /* Given the symbol's value and its section we can determine its final
3585 value when building the stubs (so the stub knows where to jump. */
3586 bfd_vma target_value;
3587 asection *target_section;
3588
721956f4
AM
3589 /* The symbol table entry, if any, that this was derived from. */
3590 struct ppc_link_hash_entry *h;
e054468f 3591 struct plt_entry *plt_ent;
721956f4 3592
411e1bfb
AM
3593 /* And the reloc addend that this was derived from. */
3594 bfd_vma addend;
3595
721956f4
AM
3596 /* Where this stub is being called from, or, in the case of combined
3597 stub sections, the first input section in the group. */
3598 asection *id_sec;
3599};
3600
3601struct ppc_branch_hash_entry {
3602
3603 /* Base hash table entry structure. */
3604 struct bfd_hash_entry root;
3605
c456f082 3606 /* Offset within branch lookup table. */
721956f4
AM
3607 unsigned int offset;
3608
3609 /* Generation marker. */
3610 unsigned int iter;
3611};
65f38f15
AM
3612
3613struct ppc_link_hash_entry
3614{
3615 struct elf_link_hash_entry elf;
3616
b3fac117
AM
3617 union {
3618 /* A pointer to the most recently used stub hash entry against this
3619 symbol. */
3620 struct ppc_stub_hash_entry *stub_cache;
3621
3622 /* A pointer to the next symbol starting with a '.' */
3623 struct ppc_link_hash_entry *next_dot_sym;
3624 } u;
721956f4 3625
65f38f15 3626 /* Track dynamic relocs copied for this symbol. */
6061a67d 3627 struct elf_dyn_relocs *dyn_relocs;
e86ce104 3628
721956f4 3629 /* Link between function code and descriptor symbols. */
34814b9f 3630 struct ppc_link_hash_entry *oh;
721956f4 3631
e86ce104
AM
3632 /* Flag function code and descriptor symbols. */
3633 unsigned int is_func:1;
3634 unsigned int is_func_descriptor:1;
908b32fc 3635 unsigned int fake:1;
411e1bfb 3636
c5614fa4
AM
3637 /* Whether global opd/toc sym has been adjusted or not.
3638 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3639 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3640 unsigned int adjust_done:1;
3641
99877b66
AM
3642 /* Set if we twiddled this symbol to weak at some stage. */
3643 unsigned int was_undefined:1;
3644
411e1bfb 3645 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3646 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3647 corresponding relocs are encountered during check_relocs.
3648 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3649 indicate the corresponding GOT entry type is not needed.
3650 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3651 a TPREL one. We use a separate flag rather than setting TPREL
3652 just for convenience in distinguishing the two cases. */
3653#define TLS_GD 1 /* GD reloc. */
3654#define TLS_LD 2 /* LD reloc. */
3655#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3656#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3657#define TLS_TLS 16 /* Any TLS reloc. */
3658#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3659#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3660#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3661 unsigned char tls_mask;
65f38f15
AM
3662};
3663
3664/* ppc64 ELF linker hash table. */
3665
3666struct ppc_link_hash_table
3667{
3668 struct elf_link_hash_table elf;
3669
721956f4
AM
3670 /* The stub hash table. */
3671 struct bfd_hash_table stub_hash_table;
3672
3673 /* Another hash table for plt_branch stubs. */
3674 struct bfd_hash_table branch_hash_table;
3675
3676 /* Linker stub bfd. */
3677 bfd *stub_bfd;
3678
3679 /* Linker call-backs. */
4ce794b7
AM
3680 asection * (*add_stub_section) (const char *, asection *);
3681 void (*layout_sections_again) (void);
721956f4
AM
3682
3683 /* Array to keep track of which stub sections have been created, and
3684 information on stub grouping. */
3685 struct map_stub {
3686 /* This is the section to which stubs in the group will be attached. */
3687 asection *link_sec;
3688 /* The stub section. */
3689 asection *stub_sec;
ad8e1ba5
AM
3690 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3691 bfd_vma toc_off;
721956f4
AM
3692 } *stub_group;
3693
ad8e1ba5
AM
3694 /* Temp used when calculating TOC pointers. */
3695 bfd_vma toc_curr;
bf102f86
AM
3696 bfd *toc_bfd;
3697 asection *toc_first_sec;
ad8e1ba5 3698
8f3bab57
AM
3699 /* Highest input section id. */
3700 int top_id;
3701
734b6cf9
AM
3702 /* Highest output section index. */
3703 int top_index;
3704
b3fac117
AM
3705 /* Used when adding symbols. */
3706 struct ppc_link_hash_entry *dot_syms;
3707
734b6cf9
AM
3708 /* List of input sections for each output section. */
3709 asection **input_list;
721956f4 3710
65f38f15 3711 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3712 asection *got;
4ce794b7
AM
3713 asection *plt;
3714 asection *relplt;
e054468f
AM
3715 asection *iplt;
3716 asection *reliplt;
4ce794b7
AM
3717 asection *dynbss;
3718 asection *relbss;
3719 asection *glink;
82bd7b59 3720 asection *sfpr;
4ce794b7
AM
3721 asection *brlt;
3722 asection *relbrlt;
ec338859 3723
8387904d
AM
3724 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3725 struct ppc_link_hash_entry *tls_get_addr;
3726 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3727
927be08e
AM
3728 /* The size of reliplt used by got entry relocs. */
3729 bfd_size_type got_reli_size;
3730
9b5ecbd0
AM
3731 /* Statistics. */
3732 unsigned long stub_count[ppc_stub_plt_call];
3733
ee75fd95
AM
3734 /* Number of stubs against global syms. */
3735 unsigned long stub_globals;
3736
ad8e1ba5 3737 /* Set if we should emit symbols for stubs. */
99877b66 3738 unsigned int emit_stub_syms:1;
ad8e1ba5 3739
a7f2871e
AM
3740 /* Set if __tls_get_addr optimization should not be done. */
3741 unsigned int no_tls_get_addr_opt:1;
3742
4c52953f 3743 /* Support for multiple toc sections. */
33c0ec9d 3744 unsigned int do_multi_toc:1;
4c52953f 3745 unsigned int multi_toc_needed:1;
927be08e 3746 unsigned int second_toc_pass:1;
67f0cbdb 3747 unsigned int do_toc_opt:1;
4c52953f 3748
5d1634d7 3749 /* Set on error. */
99877b66 3750 unsigned int stub_error:1;
721956f4 3751
7d9616d7 3752 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3753 unsigned int twiddled_syms:1;
721956f4
AM
3754
3755 /* Incremented every time we size stubs. */
3756 unsigned int stub_iteration;
5d1634d7 3757
87d72d41
AM
3758 /* Small local sym cache. */
3759 struct sym_cache sym_cache;
65f38f15
AM
3760};
3761
4c52953f
AM
3762/* Rename some of the generic section flags to better document how they
3763 are used here. */
b0dddeec
AM
3764
3765/* Nonzero if this section has TLS related relocations. */
3766#define has_tls_reloc sec_flg0
3767
3768/* Nonzero if this section has a call to __tls_get_addr. */
3769#define has_tls_get_addr_call sec_flg1
3770
3771/* Nonzero if this section has any toc or got relocs. */
3772#define has_toc_reloc sec_flg2
3773
3774/* Nonzero if this section has a call to another section that uses
3775 the toc or got. */
d77c8a4b 3776#define makes_toc_func_call sec_flg3
b0dddeec
AM
3777
3778/* Recursion protection when determining above flag. */
d77c8a4b 3779#define call_check_in_progress sec_flg4
70cc837d 3780#define call_check_done sec_flg5
4c52953f 3781
65f38f15
AM
3782/* Get the ppc64 ELF linker hash table from a link_info structure. */
3783
3784#define ppc_hash_table(p) \
4dfe6ac6
NC
3785 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3786 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3787
721956f4
AM
3788#define ppc_stub_hash_lookup(table, string, create, copy) \
3789 ((struct ppc_stub_hash_entry *) \
3790 bfd_hash_lookup ((table), (string), (create), (copy)))
3791
3792#define ppc_branch_hash_lookup(table, string, create, copy) \
3793 ((struct ppc_branch_hash_entry *) \
3794 bfd_hash_lookup ((table), (string), (create), (copy)))
3795
3796/* Create an entry in the stub hash table. */
3797
3798static struct bfd_hash_entry *
4ce794b7
AM
3799stub_hash_newfunc (struct bfd_hash_entry *entry,
3800 struct bfd_hash_table *table,
3801 const char *string)
721956f4
AM
3802{
3803 /* Allocate the structure if it has not already been allocated by a
3804 subclass. */
3805 if (entry == NULL)
3806 {
3807 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3808 if (entry == NULL)
3809 return entry;
3810 }
3811
3812 /* Call the allocation method of the superclass. */
3813 entry = bfd_hash_newfunc (entry, table, string);
3814 if (entry != NULL)
3815 {
3816 struct ppc_stub_hash_entry *eh;
3817
3818 /* Initialize the local fields. */
3819 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3820 eh->stub_type = ppc_stub_none;
721956f4
AM
3821 eh->stub_sec = NULL;
3822 eh->stub_offset = 0;
3823 eh->target_value = 0;
3824 eh->target_section = NULL;
721956f4
AM
3825 eh->h = NULL;
3826 eh->id_sec = NULL;
3827 }
3828
3829 return entry;
3830}
3831
3832/* Create an entry in the branch hash table. */
3833
3834static struct bfd_hash_entry *
4ce794b7
AM
3835branch_hash_newfunc (struct bfd_hash_entry *entry,
3836 struct bfd_hash_table *table,
3837 const char *string)
721956f4
AM
3838{
3839 /* Allocate the structure if it has not already been allocated by a
3840 subclass. */
3841 if (entry == NULL)
3842 {
3843 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3844 if (entry == NULL)
3845 return entry;
3846 }
3847
3848 /* Call the allocation method of the superclass. */
3849 entry = bfd_hash_newfunc (entry, table, string);
3850 if (entry != NULL)
3851 {
3852 struct ppc_branch_hash_entry *eh;
3853
3854 /* Initialize the local fields. */
3855 eh = (struct ppc_branch_hash_entry *) entry;
3856 eh->offset = 0;
3857 eh->iter = 0;
3858 }
3859
3860 return entry;
3861}
3862
65f38f15
AM
3863/* Create an entry in a ppc64 ELF linker hash table. */
3864
3865static struct bfd_hash_entry *
4ce794b7
AM
3866link_hash_newfunc (struct bfd_hash_entry *entry,
3867 struct bfd_hash_table *table,
3868 const char *string)
65f38f15
AM
3869{
3870 /* Allocate the structure if it has not already been allocated by a
3871 subclass. */
3872 if (entry == NULL)
3873 {
3874 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3875 if (entry == NULL)
3876 return entry;
3877 }
3878
3879 /* Call the allocation method of the superclass. */
3880 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3881 if (entry != NULL)
3882 {
3883 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3884
b3fac117 3885 memset (&eh->u.stub_cache, 0,
908b32fc 3886 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3887 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3888
3889 /* When making function calls, old ABI code references function entry
3890 points (dot symbols), while new ABI code references the function
3891 descriptor symbol. We need to make any combination of reference and
3892 definition work together, without breaking archive linking.
3893
3894 For a defined function "foo" and an undefined call to "bar":
3895 An old object defines "foo" and ".foo", references ".bar" (possibly
3896 "bar" too).
3897 A new object defines "foo" and references "bar".
3898
3899 A new object thus has no problem with its undefined symbols being
3900 satisfied by definitions in an old object. On the other hand, the
3901 old object won't have ".bar" satisfied by a new object.
3902
3903 Keep a list of newly added dot-symbols. */
3904
3905 if (string[0] == '.')
3906 {
3907 struct ppc_link_hash_table *htab;
3908
3909 htab = (struct ppc_link_hash_table *) table;
3910 eh->u.next_dot_sym = htab->dot_syms;
3911 htab->dot_syms = eh;
3912 }
65f38f15
AM
3913 }
3914
3915 return entry;
3916}
3917
3918/* Create a ppc64 ELF linker hash table. */
3919
3920static struct bfd_link_hash_table *
4ce794b7 3921ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3922{
3923 struct ppc_link_hash_table *htab;
3924 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3925
4ce794b7 3926 htab = bfd_zmalloc (amt);
65f38f15
AM
3927 if (htab == NULL)
3928 return NULL;
3929
66eb6687 3930 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3931 sizeof (struct ppc_link_hash_entry),
3932 PPC64_ELF_DATA))
65f38f15 3933 {
e2d34d7d 3934 free (htab);
65f38f15
AM
3935 return NULL;
3936 }
3937
721956f4 3938 /* Init the stub hash table too. */
66eb6687
AM
3939 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3940 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
3941 return NULL;
3942
3943 /* And the branch hash table. */
66eb6687
AM
3944 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3945 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
3946 return NULL;
3947
3254fd24
AM
3948 /* Initializing two fields of the union is just cosmetic. We really
3949 only care about glist, but when compiled on a 32-bit host the
3950 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3951 debugger inspection of these fields look nicer. */
a6aa5195
AM
3952 htab->elf.init_got_refcount.refcount = 0;
3953 htab->elf.init_got_refcount.glist = NULL;
3954 htab->elf.init_plt_refcount.refcount = 0;
3955 htab->elf.init_plt_refcount.glist = NULL;
3956 htab->elf.init_got_offset.offset = 0;
3957 htab->elf.init_got_offset.glist = NULL;
3958 htab->elf.init_plt_offset.offset = 0;
3959 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3960
65f38f15
AM
3961 return &htab->elf.root;
3962}
3963
721956f4
AM
3964/* Free the derived linker hash table. */
3965
3966static void
4ce794b7 3967ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4
AM
3968{
3969 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3970
3971 bfd_hash_table_free (&ret->stub_hash_table);
3972 bfd_hash_table_free (&ret->branch_hash_table);
3973 _bfd_generic_link_hash_table_free (hash);
3974}
3975
e717da7e
AM
3976/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3977
3978void
3979ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3980{
3981 struct ppc_link_hash_table *htab;
3982
3983 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3984
3985/* Always hook our dynamic sections into the first bfd, which is the
3986 linker created stub bfd. This ensures that the GOT header is at
3987 the start of the output TOC section. */
3988 htab = ppc_hash_table (info);
4dfe6ac6
NC
3989 if (htab == NULL)
3990 return;
e717da7e
AM
3991 htab->stub_bfd = abfd;
3992 htab->elf.dynobj = abfd;
3993}
3994
721956f4
AM
3995/* Build a name for an entry in the stub hash table. */
3996
3997static char *
4ce794b7
AM
3998ppc_stub_name (const asection *input_section,
3999 const asection *sym_sec,
4000 const struct ppc_link_hash_entry *h,
4001 const Elf_Internal_Rela *rel)
721956f4
AM
4002{
4003 char *stub_name;
4004 bfd_size_type len;
4005
4006 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4007 offsets from a sym as a branch target? In fact, we could
4008 probably assume the addend is always zero. */
4009 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4010
4011 if (h)
4012 {
4013 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4014 stub_name = bfd_malloc (len);
46de2a7c
AM
4015 if (stub_name == NULL)
4016 return stub_name;
4017
4018 sprintf (stub_name, "%08x.%s+%x",
4019 input_section->id & 0xffffffff,
4020 h->elf.root.root.string,
4021 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4022 }
4023 else
4024 {
ad8e1ba5 4025 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4026 stub_name = bfd_malloc (len);
46de2a7c
AM
4027 if (stub_name == NULL)
4028 return stub_name;
4029
4030 sprintf (stub_name, "%08x.%x:%x+%x",
4031 input_section->id & 0xffffffff,
4032 sym_sec->id & 0xffffffff,
4033 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4034 (int) rel->r_addend & 0xffffffff);
721956f4 4035 }
ee75fd95
AM
4036 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4037 stub_name[len - 2] = 0;
721956f4
AM
4038 return stub_name;
4039}
4040
4041/* Look up an entry in the stub hash. Stub entries are cached because
4042 creating the stub name takes a bit of time. */
4043
4044static struct ppc_stub_hash_entry *
4ce794b7
AM
4045ppc_get_stub_entry (const asection *input_section,
4046 const asection *sym_sec,
039b3fef 4047 struct ppc_link_hash_entry *h,
4ce794b7
AM
4048 const Elf_Internal_Rela *rel,
4049 struct ppc_link_hash_table *htab)
721956f4
AM
4050{
4051 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4052 const asection *id_sec;
4053
4054 /* If this input section is part of a group of sections sharing one
4055 stub section, then use the id of the first section in the group.
4056 Stub names need to include a section id, as there may well be
4057 more than one stub used to reach say, printf, and we need to
4058 distinguish between them. */
4059 id_sec = htab->stub_group[input_section->id].link_sec;
4060
b3fac117
AM
4061 if (h != NULL && h->u.stub_cache != NULL
4062 && h->u.stub_cache->h == h
4063 && h->u.stub_cache->id_sec == id_sec)
721956f4 4064 {
b3fac117 4065 stub_entry = h->u.stub_cache;
721956f4
AM
4066 }
4067 else
4068 {
4069 char *stub_name;
4070
4071 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4072 if (stub_name == NULL)
4073 return NULL;
4074
4075 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4076 stub_name, FALSE, FALSE);
721956f4 4077 if (h != NULL)
b3fac117 4078 h->u.stub_cache = stub_entry;
721956f4
AM
4079
4080 free (stub_name);
4081 }
4082
4083 return stub_entry;
4084}
4085
4086/* Add a new stub entry to the stub hash. Not all fields of the new
4087 stub entry are initialised. */
4088
4089static struct ppc_stub_hash_entry *
4ce794b7
AM
4090ppc_add_stub (const char *stub_name,
4091 asection *section,
25f53a85 4092 struct bfd_link_info *info)
721956f4 4093{
25f53a85 4094 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4095 asection *link_sec;
4096 asection *stub_sec;
4097 struct ppc_stub_hash_entry *stub_entry;
4098
4099 link_sec = htab->stub_group[section->id].link_sec;
4100 stub_sec = htab->stub_group[section->id].stub_sec;
4101 if (stub_sec == NULL)
4102 {
4103 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4104 if (stub_sec == NULL)
4105 {
d4c88bbb 4106 size_t namelen;
721956f4
AM
4107 bfd_size_type len;
4108 char *s_name;
4109
d4c88bbb
AM
4110 namelen = strlen (link_sec->name);
4111 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4112 s_name = bfd_alloc (htab->stub_bfd, len);
4113 if (s_name == NULL)
4114 return NULL;
4115
d4c88bbb
AM
4116 memcpy (s_name, link_sec->name, namelen);
4117 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4118 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4119 if (stub_sec == NULL)
4120 return NULL;
4121 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4122 }
4123 htab->stub_group[section->id].stub_sec = stub_sec;
4124 }
4125
4126 /* Enter this entry into the linker stub hash table. */
4127 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4128 TRUE, FALSE);
721956f4
AM
4129 if (stub_entry == NULL)
4130 {
25f53a85
AM
4131 info->callbacks->einfo (_("%B: cannot create stub entry %s\n"),
4132 section->owner, stub_name);
721956f4
AM
4133 return NULL;
4134 }
4135
4136 stub_entry->stub_sec = stub_sec;
4137 stub_entry->stub_offset = 0;
4138 stub_entry->id_sec = link_sec;
4139 return stub_entry;
4140}
4141
82bd7b59
AM
4142/* Create sections for linker generated code. */
4143
b34976b6 4144static bfd_boolean
4ce794b7 4145create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
4146{
4147 struct ppc_link_hash_table *htab;
4148 flagword flags;
4149
4150 htab = ppc_hash_table (info);
4dfe6ac6
NC
4151 if (htab == NULL)
4152 return FALSE;
82bd7b59
AM
4153
4154 /* Create .sfpr for code to save and restore fp regs. */
4155 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4156 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
4157 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4158 flags);
82bd7b59 4159 if (htab->sfpr == NULL
82bd7b59 4160 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 4161 return FALSE;
82bd7b59 4162
721956f4 4163 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
4164 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4165 flags);
4ce794b7 4166 if (htab->glink == NULL
ee4bf8d2 4167 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 4168 return FALSE;
82bd7b59 4169
e054468f
AM
4170 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4171 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4172 if (htab->iplt == NULL
4173 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4174 return FALSE;
4175
4176 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4177 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4178 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4179 ".rela.iplt",
4180 flags);
4181 if (htab->reliplt == NULL
4182 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4183 return FALSE;
4184
c456f082 4185 /* Create branch lookup table for plt_branch stubs. */
e4e0193e
AM
4186 flags = (SEC_ALLOC | SEC_LOAD
4187 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4188 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4189 flags);
4ce794b7 4190 if (htab->brlt == NULL
4ce794b7 4191 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 4192 return FALSE;
721956f4 4193
e4e0193e 4194 if (!info->shared)
c456f082
AM
4195 return TRUE;
4196
e4e0193e
AM
4197 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4198 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4199 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4200 ".rela.branch_lt",
4201 flags);
e054468f 4202 if (htab->relbrlt == NULL
c456f082
AM
4203 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4204 return FALSE;
4205
b34976b6 4206 return TRUE;
82bd7b59
AM
4207}
4208
e717da7e
AM
4209/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4210 not already done. */
65f38f15 4211
b34976b6 4212static bfd_boolean
e717da7e 4213create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4214{
e717da7e
AM
4215 asection *got, *relgot;
4216 flagword flags;
4217 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4218
0c8d6e5c 4219 if (!is_ppc64_elf (abfd))
0ffa91dd 4220 return FALSE;
4dfe6ac6
NC
4221 if (htab == NULL)
4222 return FALSE;
0ffa91dd 4223
4ce794b7 4224 if (!htab->got)
e717da7e
AM
4225 {
4226 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4227 return FALSE;
4228
4229 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4230 if (!htab->got)
4231 abort ();
4232 }
4233
4234 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4235 | SEC_LINKER_CREATED);
4236
c456f082 4237 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4238 if (!got
e717da7e
AM
4239 || !bfd_set_section_alignment (abfd, got, 3))
4240 return FALSE;
65f38f15 4241
c456f082
AM
4242 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4243 flags | SEC_READONLY);
e717da7e 4244 if (!relgot
e717da7e 4245 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4246 return FALSE;
e717da7e
AM
4247
4248 ppc64_elf_tdata (abfd)->got = got;
4249 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4250 return TRUE;
65f38f15 4251}
5bd4f169 4252
82bd7b59 4253/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4254
b34976b6 4255static bfd_boolean
4ce794b7 4256ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4257{
65f38f15 4258 struct ppc_link_hash_table *htab;
5bd4f169 4259
65f38f15 4260 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4261 return FALSE;
65f38f15 4262
e717da7e 4263 htab = ppc_hash_table (info);
4dfe6ac6
NC
4264 if (htab == NULL)
4265 return FALSE;
4266
e717da7e
AM
4267 if (!htab->got)
4268 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
4269 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4270 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4271 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 4272 if (!info->shared)
4ce794b7 4273 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 4274
e717da7e 4275 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4276 || (!info->shared && !htab->relbss))
65f38f15
AM
4277 abort ();
4278
b34976b6 4279 return TRUE;
5bd4f169
AM
4280}
4281
b31867b6
AM
4282/* Follow indirect and warning symbol links. */
4283
4284static inline struct bfd_link_hash_entry *
4285follow_link (struct bfd_link_hash_entry *h)
4286{
4287 while (h->type == bfd_link_hash_indirect
4288 || h->type == bfd_link_hash_warning)
4289 h = h->u.i.link;
4290 return h;
4291}
4292
4293static inline struct elf_link_hash_entry *
4294elf_follow_link (struct elf_link_hash_entry *h)
4295{
4296 return (struct elf_link_hash_entry *) follow_link (&h->root);
4297}
4298
4299static inline struct ppc_link_hash_entry *
4300ppc_follow_link (struct ppc_link_hash_entry *h)
4301{
4302 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4303}
4304
40d16e0b
AM
4305/* Merge PLT info on FROM with that on TO. */
4306
4307static void
4308move_plt_plist (struct ppc_link_hash_entry *from,
4309 struct ppc_link_hash_entry *to)
4310{
4311 if (from->elf.plt.plist != NULL)
4312 {
4313 if (to->elf.plt.plist != NULL)
4314 {
4315 struct plt_entry **entp;
4316 struct plt_entry *ent;
4317
4318 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4319 {
4320 struct plt_entry *dent;
4321
4322 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4323 if (dent->addend == ent->addend)
4324 {
4325 dent->plt.refcount += ent->plt.refcount;
4326 *entp = ent->next;
4327 break;
4328 }
4329 if (dent == NULL)
4330 entp = &ent->next;
4331 }
4332 *entp = to->elf.plt.plist;
4333 }
4334
4335 to->elf.plt.plist = from->elf.plt.plist;
4336 from->elf.plt.plist = NULL;
4337 }
4338}
4339
65f38f15
AM
4340/* Copy the extra info we tack onto an elf_link_hash_entry. */
4341
4342static void
fcfa13d2
AM
4343ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4344 struct elf_link_hash_entry *dir,
4345 struct elf_link_hash_entry *ind)
65f38f15
AM
4346{
4347 struct ppc_link_hash_entry *edir, *eind;
4348
4349 edir = (struct ppc_link_hash_entry *) dir;
4350 eind = (struct ppc_link_hash_entry *) ind;
4351
c79d6685
AM
4352 edir->is_func |= eind->is_func;
4353 edir->is_func_descriptor |= eind->is_func_descriptor;
4354 edir->tls_mask |= eind->tls_mask;
4355 if (eind->oh != NULL)
4356 edir->oh = ppc_follow_link (eind->oh);
4357
4358 /* If called to transfer flags for a weakdef during processing
4359 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4360 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4361 if (!(ELIMINATE_COPY_RELOCS
4362 && eind->elf.root.type != bfd_link_hash_indirect
4363 && edir->elf.dynamic_adjusted))
4364 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4365
4366 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4367 edir->elf.ref_regular |= eind->elf.ref_regular;
4368 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4369 edir->elf.needs_plt |= eind->elf.needs_plt;
4370
4371 /* If we were called to copy over info for a weak sym, that's all. */
4372 if (eind->elf.root.type != bfd_link_hash_indirect)
4373 return;
4374
411e1bfb 4375 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4376 if (eind->dyn_relocs != NULL)
65f38f15 4377 {
bbd7ec4a
AM
4378 if (edir->dyn_relocs != NULL)
4379 {
6061a67d
AM
4380 struct elf_dyn_relocs **pp;
4381 struct elf_dyn_relocs *p;
bbd7ec4a 4382
fcfa13d2 4383 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4384 list. Merge any entries against the same section. */
4385 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4386 {
6061a67d 4387 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4388
4389 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4390 if (q->sec == p->sec)
4391 {
4392 q->pc_count += p->pc_count;
4393 q->count += p->count;
4394 *pp = p->next;
4395 break;
4396 }
4397 if (q == NULL)
4398 pp = &p->next;
4399 }
4400 *pp = edir->dyn_relocs;
4401 }
4402
65f38f15
AM
4403 edir->dyn_relocs = eind->dyn_relocs;
4404 eind->dyn_relocs = NULL;
4405 }
65f38f15 4406
81848ca0
AM
4407 /* Copy over got entries that we may have already seen to the
4408 symbol which just became indirect. */
411e1bfb
AM
4409 if (eind->elf.got.glist != NULL)
4410 {
4411 if (edir->elf.got.glist != NULL)
4412 {
4413 struct got_entry **entp;
4414 struct got_entry *ent;
4415
4416 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4417 {
4418 struct got_entry *dent;
4419
4420 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4421 if (dent->addend == ent->addend
e717da7e 4422 && dent->owner == ent->owner
411e1bfb
AM
4423 && dent->tls_type == ent->tls_type)
4424 {
4425 dent->got.refcount += ent->got.refcount;
4426 *entp = ent->next;
4427 break;
4428 }
4429 if (dent == NULL)
4430 entp = &ent->next;
4431 }
4432 *entp = edir->elf.got.glist;
4433 }
4434
4435 edir->elf.got.glist = eind->elf.got.glist;
4436 eind->elf.got.glist = NULL;
4437 }
4438
4439 /* And plt entries. */
40d16e0b 4440 move_plt_plist (eind, edir);
411e1bfb 4441
fcfa13d2 4442 if (eind->elf.dynindx != -1)
411e1bfb 4443 {
fcfa13d2
AM
4444 if (edir->elf.dynindx != -1)
4445 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4446 edir->elf.dynstr_index);
411e1bfb
AM
4447 edir->elf.dynindx = eind->elf.dynindx;
4448 edir->elf.dynstr_index = eind->elf.dynstr_index;
4449 eind->elf.dynindx = -1;
4450 eind->elf.dynstr_index = 0;
4451 }
411e1bfb
AM
4452}
4453
8387904d
AM
4454/* Find the function descriptor hash entry from the given function code
4455 hash entry FH. Link the entries via their OH fields. */
4456
4457static struct ppc_link_hash_entry *
b31867b6 4458lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4459{
4460 struct ppc_link_hash_entry *fdh = fh->oh;
4461
4462 if (fdh == NULL)
4463 {
4464 const char *fd_name = fh->elf.root.root.string + 1;
4465
4466 fdh = (struct ppc_link_hash_entry *)
4467 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4468 if (fdh == NULL)
4469 return fdh;
4470
4471 fdh->is_func_descriptor = 1;
4472 fdh->oh = fh;
4473 fh->is_func = 1;
4474 fh->oh = fdh;
8387904d
AM
4475 }
4476
b31867b6 4477 return ppc_follow_link (fdh);
8387904d
AM
4478}
4479
bb700d78
AM
4480/* Make a fake function descriptor sym for the code sym FH. */
4481
4482static struct ppc_link_hash_entry *
4483make_fdh (struct bfd_link_info *info,
908b32fc 4484 struct ppc_link_hash_entry *fh)
bb700d78
AM
4485{
4486 bfd *abfd;
4487 asymbol *newsym;
4488 struct bfd_link_hash_entry *bh;
4489 struct ppc_link_hash_entry *fdh;
4490
4491 abfd = fh->elf.root.u.undef.abfd;
4492 newsym = bfd_make_empty_symbol (abfd);
4493 newsym->name = fh->elf.root.root.string + 1;
4494 newsym->section = bfd_und_section_ptr;
4495 newsym->value = 0;
908b32fc 4496 newsym->flags = BSF_WEAK;
bb700d78
AM
4497
4498 bh = NULL;
4499 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4500 newsym->flags, newsym->section,
4501 newsym->value, NULL, FALSE, FALSE,
4502 &bh))
4503 return NULL;
4504
4505 fdh = (struct ppc_link_hash_entry *) bh;
4506 fdh->elf.non_elf = 0;
908b32fc
AM
4507 fdh->fake = 1;
4508 fdh->is_func_descriptor = 1;
4509 fdh->oh = fh;
4510 fh->is_func = 1;
4511 fh->oh = fdh;
bb700d78
AM
4512 return fdh;
4513}
4514
8387904d
AM
4515/* Fix function descriptor symbols defined in .opd sections to be
4516 function type. */
555cd476
AM
4517
4518static bfd_boolean
c16153ae 4519ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4520 struct bfd_link_info *info,
555cd476 4521 Elf_Internal_Sym *isym,
b3fac117 4522 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4523 flagword *flags ATTRIBUTE_UNUSED,
4524 asection **sec,
4525 bfd_vma *value ATTRIBUTE_UNUSED)
4526{
f64b2e8d
NC
4527 if ((ibfd->flags & DYNAMIC) == 0
4528 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4529 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4530
e054468f 4531 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4532 {
4533 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4534 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4535 }
e054468f
AM
4536 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4537 ;
4538 else if (*sec != NULL
70cc837d 4539 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4540 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4541
555cd476
AM
4542 return TRUE;
4543}
4544
8387904d 4545/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4546 inclusion of a new ABI object archive that defines "bar".
4547 NAME is a symbol defined in an archive. Return a symbol in the hash
4548 table that might be satisfied by the archive symbols. */
8387904d
AM
4549
4550static struct elf_link_hash_entry *
4551ppc64_elf_archive_symbol_lookup (bfd *abfd,
4552 struct bfd_link_info *info,
4553 const char *name)
4554{
4555 struct elf_link_hash_entry *h;
4556 char *dot_name;
4557 size_t len;
4558
4559 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4560 if (h != NULL
4561 /* Don't return this sym if it is a fake function descriptor
4562 created by add_symbol_adjust. */
4563 && !(h->root.type == bfd_link_hash_undefweak
4564 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4565 return h;
4566
4567 if (name[0] == '.')
4568 return h;
4569
4570 len = strlen (name);
4571 dot_name = bfd_alloc (abfd, len + 2);
4572 if (dot_name == NULL)
4573 return (struct elf_link_hash_entry *) 0 - 1;
4574 dot_name[0] = '.';
4575 memcpy (dot_name + 1, name, len + 1);
4576 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4577 bfd_release (abfd, dot_name);
4578 return h;
4579}
4580
4581/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4582 new ABI object defines "bar". Well, at least, undefined dot symbols
4583 are made weak. This stops later archive searches from including an
4584 object if we already have a function descriptor definition. It also
35b0ce59
AM
4585 prevents the linker complaining about undefined symbols.
4586 We also check and correct mismatched symbol visibility here. The
4587 most restrictive visibility of the function descriptor and the
4588 function entry symbol is used. */
8387904d
AM
4589
4590static bfd_boolean
b3fac117 4591add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4592{
8387904d
AM
4593 struct ppc_link_hash_table *htab;
4594 struct ppc_link_hash_entry *fdh;
4595
b3fac117 4596 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4597 return TRUE;
4598
b3fac117
AM
4599 if (eh->elf.root.type == bfd_link_hash_warning)
4600 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4601
b3fac117
AM
4602 if (eh->elf.root.root.string[0] != '.')
4603 abort ();
8387904d 4604
b3fac117 4605 htab = ppc_hash_table (info);
4dfe6ac6
NC
4606 if (htab == NULL)
4607 return FALSE;
4608
b31867b6
AM
4609 fdh = lookup_fdh (eh, htab);
4610 if (fdh == NULL)
4611 {
4612 if (!info->relocatable
4613 && (eh->elf.root.type == bfd_link_hash_undefined
4614 || eh->elf.root.type == bfd_link_hash_undefweak)
4615 && eh->elf.ref_regular)
4616 {
4617 /* Make an undefweak function descriptor sym, which is enough to
4618 pull in an --as-needed shared lib, but won't cause link
4619 errors. Archives are handled elsewhere. */
4620 fdh = make_fdh (info, eh);
4621 if (fdh == NULL)
4622 return FALSE;
4623 fdh->elf.ref_regular = 1;
4624 }
bb700d78 4625 }
b31867b6 4626 else
8387904d 4627 {
35b0ce59
AM
4628 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4629 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4630 if (entry_vis < descr_vis)
4631 fdh->elf.other += entry_vis - descr_vis;
4632 else if (entry_vis > descr_vis)
4633 eh->elf.other += descr_vis - entry_vis;
4634
e87d886e
AM
4635 if ((fdh->elf.root.type == bfd_link_hash_defined
4636 || fdh->elf.root.type == bfd_link_hash_defweak)
4637 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4638 {
4639 eh->elf.root.type = bfd_link_hash_undefweak;
4640 eh->was_undefined = 1;
4641 htab->twiddled_syms = 1;
4642 }
8387904d 4643 }
99877b66 4644
8387904d
AM
4645 return TRUE;
4646}
4647
b3fac117
AM
4648/* Process list of dot-symbols we made in link_hash_newfunc. */
4649
8387904d 4650static bfd_boolean
7d9616d7 4651ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4652{
99877b66 4653 struct ppc_link_hash_table *htab;
b3fac117 4654 struct ppc_link_hash_entry **p, *eh;
433817dd 4655
0c8d6e5c 4656 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4657 return TRUE;
4dfe6ac6
NC
4658 htab = ppc_hash_table (info);
4659 if (htab == NULL)
4660 return FALSE;
35b0ce59 4661
0c8d6e5c 4662 if (is_ppc64_elf (ibfd))
b3fac117
AM
4663 {
4664 p = &htab->dot_syms;
4665 while ((eh = *p) != NULL)
4666 {
4667 *p = NULL;
4668 if (!add_symbol_adjust (eh, info))
4669 return FALSE;
4670 p = &eh->u.next_dot_sym;
4671 }
4672 }
4673
4674 /* Clear the list for non-ppc64 input files. */
4675 p = &htab->dot_syms;
4676 while ((eh = *p) != NULL)
4677 {
4678 *p = NULL;
4679 p = &eh->u.next_dot_sym;
4680 }
99877b66
AM
4681
4682 /* We need to fix the undefs list for any syms we have twiddled to
4683 undef_weak. */
4684 if (htab->twiddled_syms)
4685 {
77cfaee6 4686 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4687 htab->twiddled_syms = 0;
4688 }
b3fac117 4689 return TRUE;
8387904d
AM
4690}
4691
97fed1c9
JJ
4692/* Undo hash table changes when an --as-needed input file is determined
4693 not to be needed. */
4694
4695static bfd_boolean
4696ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4697 struct bfd_link_info *info)
4698{
4dfe6ac6
NC
4699 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4700
4701 if (htab == NULL)
4702 return FALSE;
4703
4704 htab->dot_syms = NULL;
97fed1c9
JJ
4705 return TRUE;
4706}
4707
aa374f67
AM
4708/* If --just-symbols against a final linked binary, then assume we need
4709 toc adjusting stubs when calling functions defined there. */
4710
4711static void
4712ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4713{
4714 if ((sec->flags & SEC_CODE) != 0
4715 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4716 && is_ppc64_elf (sec->owner))
4717 {
4718 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4719 if (got != NULL
4720 && got->size >= elf_backend_got_header_size
4721 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4722 sec->has_toc_reloc = 1;
4723 }
4724 _bfd_elf_link_just_syms (sec, info);
4725}
4726
e054468f 4727static struct plt_entry **
4ce794b7
AM
4728update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4729 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4730{
4731 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4732 struct plt_entry **local_plt;
f961d9dd 4733 unsigned char *local_got_tls_masks;
411e1bfb
AM
4734
4735 if (local_got_ents == NULL)
4736 {
4737 bfd_size_type size = symtab_hdr->sh_info;
4738
e054468f
AM
4739 size *= (sizeof (*local_got_ents)
4740 + sizeof (*local_plt)
4741 + sizeof (*local_got_tls_masks));
4ce794b7 4742 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4743 if (local_got_ents == NULL)
e054468f 4744 return NULL;
411e1bfb
AM
4745 elf_local_got_ents (abfd) = local_got_ents;
4746 }
4747
e054468f 4748 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4749 {
4750 struct got_entry *ent;
4751
4752 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4753 if (ent->addend == r_addend
4754 && ent->owner == abfd
4755 && ent->tls_type == tls_type)
411e1bfb
AM
4756 break;
4757 if (ent == NULL)
4758 {
4759 bfd_size_type amt = sizeof (*ent);
4ce794b7 4760 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4761 if (ent == NULL)
4762 return FALSE;
4763 ent->next = local_got_ents[r_symndx];
4764 ent->addend = r_addend;
e717da7e 4765 ent->owner = abfd;
411e1bfb 4766 ent->tls_type = tls_type;
927be08e 4767 ent->is_indirect = FALSE;
411e1bfb
AM
4768 ent->got.refcount = 0;
4769 local_got_ents[r_symndx] = ent;
4770 }
4771 ent->got.refcount += 1;
4772 }
4773
e054468f 4774 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4775 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 4776 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
4777
4778 return local_plt + r_symndx;
65f38f15
AM
4779}
4780
411e1bfb 4781static bfd_boolean
e054468f 4782update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4783{
411e1bfb 4784 struct plt_entry *ent;
1e2f5b6e 4785
e054468f 4786 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4787 if (ent->addend == addend)
4788 break;
4789 if (ent == NULL)
1e2f5b6e 4790 {
411e1bfb 4791 bfd_size_type amt = sizeof (*ent);
4ce794b7 4792 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4793 if (ent == NULL)
4794 return FALSE;
e054468f 4795 ent->next = *plist;
411e1bfb
AM
4796 ent->addend = addend;
4797 ent->plt.refcount = 0;
e054468f 4798 *plist = ent;
1e2f5b6e 4799 }
411e1bfb 4800 ent->plt.refcount += 1;
b34976b6 4801 return TRUE;
1e2f5b6e
AM
4802}
4803
e054468f
AM
4804static bfd_boolean
4805is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4806{
4807 return (r_type == R_PPC64_REL24
4808 || r_type == R_PPC64_REL14
4809 || r_type == R_PPC64_REL14_BRTAKEN
4810 || r_type == R_PPC64_REL14_BRNTAKEN
4811 || r_type == R_PPC64_ADDR24
4812 || r_type == R_PPC64_ADDR14
4813 || r_type == R_PPC64_ADDR14_BRTAKEN
4814 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4815}
4816
5bd4f169 4817/* Look through the relocs for a section during the first phase, and
65f38f15 4818 calculate needed space in the global offset table, procedure
5d1634d7 4819 linkage table, and dynamic reloc sections. */
5bd4f169 4820
b34976b6 4821static bfd_boolean
4ce794b7
AM
4822ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4823 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4824{
65f38f15 4825 struct ppc_link_hash_table *htab;
5bd4f169 4826 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4827 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4828 const Elf_Internal_Rela *rel;
4829 const Elf_Internal_Rela *rel_end;
5bd4f169 4830 asection *sreloc;
1e2f5b6e 4831 asection **opd_sym_map;
3a71aa26 4832 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 4833
1049f94e 4834 if (info->relocatable)
b34976b6 4835 return TRUE;
5bd4f169 4836
680a3378
AM
4837 /* Don't do anything special with non-loaded, non-alloced sections.
4838 In particular, any relocs in such sections should not affect GOT
4839 and PLT reference counting (ie. we don't allow them to create GOT
4840 or PLT entries), there's no possibility or desire to optimize TLS
4841 relocs, and there's not much point in propagating relocs to shared
4842 libs that the dynamic linker won't relocate. */
4843 if ((sec->flags & SEC_ALLOC) == 0)
4844 return TRUE;
4845
0c8d6e5c 4846 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4847
65f38f15 4848 htab = ppc_hash_table (info);
4dfe6ac6
NC
4849 if (htab == NULL)
4850 return FALSE;
4851
3a71aa26
AM
4852 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4853 FALSE, FALSE, TRUE);
4854 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4855 FALSE, FALSE, TRUE);
0ffa91dd 4856 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4857 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4858 sreloc = NULL;
1e2f5b6e 4859 opd_sym_map = NULL;
70cc837d 4860 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
4861 {
4862 /* Garbage collection needs some extra help with .opd sections.
4863 We don't want to necessarily keep everything referenced by
4864 relocs in .opd, as that would keep all functions. Instead,
4865 if we reference an .opd symbol (a function descriptor), we
4866 want to keep the function code symbol's section. This is
4867 easy for global symbols, but for local syms we need to keep
74f0fb50 4868 information about the associated function section. */
1e2f5b6e
AM
4869 bfd_size_type amt;
4870
74f0fb50 4871 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 4872 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4873 if (opd_sym_map == NULL)
b34976b6 4874 return FALSE;
74f0fb50 4875 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
4876 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4877 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4878 }
5bd4f169 4879
82bd7b59
AM
4880 if (htab->sfpr == NULL
4881 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4882 return FALSE;
82bd7b59 4883
5bd4f169
AM
4884 rel_end = relocs + sec->reloc_count;
4885 for (rel = relocs; rel < rel_end; rel++)
4886 {
4887 unsigned long r_symndx;
4888 struct elf_link_hash_entry *h;
04c9666a 4889 enum elf_ppc64_reloc_type r_type;
727fc41e 4890 int tls_type;
7c8fe5c4 4891 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 4892 struct plt_entry **ifunc;
5bd4f169
AM
4893
4894 r_symndx = ELF64_R_SYM (rel->r_info);
4895 if (r_symndx < symtab_hdr->sh_info)
4896 h = NULL;
4897 else
973a3492
L
4898 {
4899 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4900 h = elf_follow_link (h);
973a3492 4901 }
5bd4f169 4902
727fc41e 4903 tls_type = 0;
e054468f 4904 ifunc = NULL;
25f23106
AM
4905 if (h != NULL)
4906 {
4907 if (h->type == STT_GNU_IFUNC)
4908 {
4909 h->needs_plt = 1;
4910 ifunc = &h->plt.plist;
4911 }
4912 }
4913 else
4914 {
4915 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4916 abfd, r_symndx);
4917 if (isym == NULL)
4918 return FALSE;
4919
4920 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4921 {
4922 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4923 rel->r_addend, PLT_IFUNC);
4924 if (ifunc == NULL)
4925 return FALSE;
4926 }
4927 }
4ce794b7 4928 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
4929 if (is_branch_reloc (r_type))
4930 {
4931 if (h != NULL && (h == tga || h == dottga))
4932 {
4933 if (rel != relocs
4934 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4935 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4936 /* We have a new-style __tls_get_addr call with a marker
4937 reloc. */
4938 ;
4939 else
4940 /* Mark this section as having an old-style call. */
4941 sec->has_tls_get_addr_call = 1;
4942 }
727fc41e 4943
e054468f 4944 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
4945 if (ifunc != NULL
4946 && !update_plt_info (abfd, ifunc, rel->r_addend))
4947 return FALSE;
4948 }
727fc41e 4949
a33d1f77 4950 switch (r_type)
5bd4f169 4951 {
727fc41e
AM
4952 case R_PPC64_TLSGD:
4953 case R_PPC64_TLSLD:
4954 /* These special tls relocs tie a call to __tls_get_addr with
4955 its parameter symbol. */
4956 break;
4957
411e1bfb
AM
4958 case R_PPC64_GOT_TLSLD16:
4959 case R_PPC64_GOT_TLSLD16_LO:
4960 case R_PPC64_GOT_TLSLD16_HI:
4961 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4962 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4963 goto dogottls;
4964
4965 case R_PPC64_GOT_TLSGD16:
4966 case R_PPC64_GOT_TLSGD16_LO:
4967 case R_PPC64_GOT_TLSGD16_HI:
4968 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4969 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4970 goto dogottls;
4971
4972 case R_PPC64_GOT_TPREL16_DS:
4973 case R_PPC64_GOT_TPREL16_LO_DS:
4974 case R_PPC64_GOT_TPREL16_HI:
4975 case R_PPC64_GOT_TPREL16_HA:
1d483afe 4976 if (!info->executable)
411e1bfb
AM
4977 info->flags |= DF_STATIC_TLS;
4978 tls_type = TLS_TLS | TLS_TPREL;
4979 goto dogottls;
4980
4981 case R_PPC64_GOT_DTPREL16_DS:
4982 case R_PPC64_GOT_DTPREL16_LO_DS:
4983 case R_PPC64_GOT_DTPREL16_HI:
4984 case R_PPC64_GOT_DTPREL16_HA:
4985 tls_type = TLS_TLS | TLS_DTPREL;
4986 dogottls:
4987 sec->has_tls_reloc = 1;
4988 /* Fall thru */
4989
5bd4f169 4990 case R_PPC64_GOT16:
5bd4f169 4991 case R_PPC64_GOT16_DS:
65f38f15
AM
4992 case R_PPC64_GOT16_HA:
4993 case R_PPC64_GOT16_HI:
4994 case R_PPC64_GOT16_LO:
5bd4f169 4995 case R_PPC64_GOT16_LO_DS:
65f38f15 4996 /* This symbol requires a global offset table entry. */
4c52953f 4997 sec->has_toc_reloc = 1;
33c0ec9d
AM
4998 if (r_type == R_PPC64_GOT_TLSLD16
4999 || r_type == R_PPC64_GOT_TLSGD16
5000 || r_type == R_PPC64_GOT_TPREL16_DS
5001 || r_type == R_PPC64_GOT_DTPREL16_DS
5002 || r_type == R_PPC64_GOT16
5003 || r_type == R_PPC64_GOT16_DS)
5004 {
5005 htab->do_multi_toc = 1;
d77c8a4b 5006 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5007 }
5008
e717da7e
AM
5009 if (ppc64_elf_tdata (abfd)->got == NULL
5010 && !create_got_section (abfd, info))
b34976b6 5011 return FALSE;
5bd4f169
AM
5012
5013 if (h != NULL)
5014 {
411e1bfb
AM
5015 struct ppc_link_hash_entry *eh;
5016 struct got_entry *ent;
65f38f15 5017
411e1bfb
AM
5018 eh = (struct ppc_link_hash_entry *) h;
5019 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5020 if (ent->addend == rel->r_addend
e717da7e 5021 && ent->owner == abfd
411e1bfb
AM
5022 && ent->tls_type == tls_type)
5023 break;
5024 if (ent == NULL)
5bd4f169 5025 {
411e1bfb 5026 bfd_size_type amt = sizeof (*ent);
4ce794b7 5027 ent = bfd_alloc (abfd, amt);
411e1bfb 5028 if (ent == NULL)
b34976b6 5029 return FALSE;
411e1bfb
AM
5030 ent->next = eh->elf.got.glist;
5031 ent->addend = rel->r_addend;
e717da7e 5032 ent->owner = abfd;
411e1bfb 5033 ent->tls_type = tls_type;
927be08e 5034 ent->is_indirect = FALSE;
411e1bfb
AM
5035 ent->got.refcount = 0;
5036 eh->elf.got.glist = ent;
5bd4f169 5037 }
411e1bfb 5038 ent->got.refcount += 1;
e7b938ca 5039 eh->tls_mask |= tls_type;
5bd4f169 5040 }
411e1bfb
AM
5041 else
5042 /* This is a global offset table entry for a local symbol. */
5043 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5044 rel->r_addend, tls_type))
5045 return FALSE;
5bd4f169
AM
5046 break;
5047
5bd4f169 5048 case R_PPC64_PLT16_HA:
65f38f15
AM
5049 case R_PPC64_PLT16_HI:
5050 case R_PPC64_PLT16_LO:
5051 case R_PPC64_PLT32:
5052 case R_PPC64_PLT64:
5bd4f169 5053 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5054 actually build the entry in adjust_dynamic_symbol,
5055 because this might be a case of linking PIC code without
5056 linking in any dynamic objects, in which case we don't
5057 need to generate a procedure linkage table after all. */
5bd4f169
AM
5058 if (h == NULL)
5059 {
5060 /* It does not make sense to have a procedure linkage
3fad3c7c 5061 table entry for a local symbol. */
5bd4f169 5062 bfd_set_error (bfd_error_bad_value);
b34976b6 5063 return FALSE;
5bd4f169 5064 }
411e1bfb 5065 else
e054468f
AM
5066 {
5067 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5068 return FALSE;
5069 h->needs_plt = 1;
5070 if (h->root.root.string[0] == '.'
5071 && h->root.root.string[1] != '\0')
5072 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5073 }
5bd4f169
AM
5074 break;
5075
5076 /* The following relocations don't need to propagate the
5077 relocation if linking a shared object since they are
5078 section relative. */
5079 case R_PPC64_SECTOFF:
5080 case R_PPC64_SECTOFF_LO:
5081 case R_PPC64_SECTOFF_HI:
5082 case R_PPC64_SECTOFF_HA:
5083 case R_PPC64_SECTOFF_DS:
5084 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5085 case R_PPC64_DTPREL16:
5086 case R_PPC64_DTPREL16_LO:
5087 case R_PPC64_DTPREL16_HI:
5088 case R_PPC64_DTPREL16_HA:
5089 case R_PPC64_DTPREL16_DS:
5090 case R_PPC64_DTPREL16_LO_DS:
5091 case R_PPC64_DTPREL16_HIGHER:
5092 case R_PPC64_DTPREL16_HIGHERA:
5093 case R_PPC64_DTPREL16_HIGHEST:
5094 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5095 break;
5096
ad8e1ba5 5097 /* Nor do these. */
25f23106
AM
5098 case R_PPC64_REL16:
5099 case R_PPC64_REL16_LO:
5100 case R_PPC64_REL16_HI:
5101 case R_PPC64_REL16_HA:
5102 break;
5103
ad8e1ba5 5104 case R_PPC64_TOC16:
33c0ec9d
AM
5105 case R_PPC64_TOC16_DS:
5106 htab->do_multi_toc = 1;
d77c8a4b 5107 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5108 case R_PPC64_TOC16_LO:
5109 case R_PPC64_TOC16_HI:
5110 case R_PPC64_TOC16_HA:
ad8e1ba5 5111 case R_PPC64_TOC16_LO_DS:
4c52953f 5112 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5113 break;
5114
5bd4f169
AM
5115 /* This relocation describes the C++ object vtable hierarchy.
5116 Reconstruct it for later use during GC. */
5117 case R_PPC64_GNU_VTINHERIT:
c152c796 5118 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5119 return FALSE;
5bd4f169
AM
5120 break;
5121
5122 /* This relocation describes which C++ vtable entries are actually
5123 used. Record for later use during GC. */
5124 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5125 BFD_ASSERT (h != NULL);
5126 if (h != NULL
5127 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5128 return FALSE;
5bd4f169
AM
5129 break;
5130
721956f4
AM
5131 case R_PPC64_REL14:
5132 case R_PPC64_REL14_BRTAKEN:
5133 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5134 {
5135 asection *dest = NULL;
5136
5137 /* Heuristic: If jumping outside our section, chances are
5138 we are going to need a stub. */
5139 if (h != NULL)
5140 {
5141 /* If the sym is weak it may be overridden later, so
5142 don't assume we know where a weak sym lives. */
5143 if (h->root.type == bfd_link_hash_defined)
5144 dest = h->root.u.def.section;
5145 }
5146 else
87d72d41
AM
5147 {
5148 Elf_Internal_Sym *isym;
5149
5150 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5151 abfd, r_symndx);
5152 if (isym == NULL)
5153 return FALSE;
5154
5155 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5156 }
5157
220c76dd 5158 if (dest != sec)
7c8fe5c4 5159 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5160 }
721956f4
AM
5161 /* Fall through. */
5162
5d1634d7 5163 case R_PPC64_REL24:
e054468f 5164 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5165 {
5166 /* We may need a .plt entry if the function this reloc
5167 refers to is in a shared lib. */
e054468f 5168 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5169 return FALSE;
e054468f
AM
5170 h->needs_plt = 1;
5171 if (h->root.root.string[0] == '.'
5172 && h->root.root.string[1] != '\0')
5173 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5174 if (h == tga || h == dottga)
411e1bfb 5175 sec->has_tls_reloc = 1;
411e1bfb
AM
5176 }
5177 break;
5178
5179 case R_PPC64_TPREL64:
5180 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5181 if (!info->executable)
411e1bfb
AM
5182 info->flags |= DF_STATIC_TLS;
5183 goto dotlstoc;
5184
5185 case R_PPC64_DTPMOD64:
5186 if (rel + 1 < rel_end
5187 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5188 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5189 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5190 else
951fd09b 5191 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5192 goto dotlstoc;
5193
5194 case R_PPC64_DTPREL64:
5195 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5196 if (rel != relocs
5197 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5198 && rel[-1].r_offset == rel->r_offset - 8)
5199 /* This is the second reloc of a dtpmod, dtprel pair.
5200 Don't mark with TLS_DTPREL. */
5201 goto dodyn;
5202
5203 dotlstoc:
5204 sec->has_tls_reloc = 1;
5205 if (h != NULL)
5206 {
5207 struct ppc_link_hash_entry *eh;
5208 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5209 eh->tls_mask |= tls_type;
411e1bfb
AM
5210 }
5211 else
5212 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5213 rel->r_addend, tls_type))
5214 return FALSE;
5215
7c8fe5c4
AM
5216 ppc64_sec = ppc64_elf_section_data (sec);
5217 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5218 {
3a71aa26
AM
5219 bfd_size_type amt;
5220
e7b938ca 5221 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5222 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5223 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5224 if (ppc64_sec->u.toc.symndx == NULL)
5225 return FALSE;
5226 amt = sec->size * sizeof (bfd_vma) / 8;
5227 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5228 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5229 return FALSE;
7c8fe5c4
AM
5230 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5231 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5232 }
5233 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5234 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5235 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5236
5237 /* Mark the second slot of a GD or LD entry.
5238 -1 to indicate GD and -2 to indicate LD. */
5239 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5240 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5241 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5242 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5243 goto dodyn;
5244
5245 case R_PPC64_TPREL16:
5246 case R_PPC64_TPREL16_LO:
5247 case R_PPC64_TPREL16_HI:
5248 case R_PPC64_TPREL16_HA:
5249 case R_PPC64_TPREL16_DS:
5250 case R_PPC64_TPREL16_LO_DS:
5251 case R_PPC64_TPREL16_HIGHER:
5252 case R_PPC64_TPREL16_HIGHERA:
5253 case R_PPC64_TPREL16_HIGHEST:
5254 case R_PPC64_TPREL16_HIGHESTA:
5255 if (info->shared)
5256 {
1d483afe
AM
5257 if (!info->executable)
5258 info->flags |= DF_STATIC_TLS;
411e1bfb 5259 goto dodyn;
5d1634d7
AM
5260 }
5261 break;
5262
e86ce104 5263 case R_PPC64_ADDR64:
1e2f5b6e 5264 if (opd_sym_map != NULL
1e2f5b6e 5265 && rel + 1 < rel_end
4ce794b7 5266 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5267 {
8387904d
AM
5268 if (h != NULL)
5269 {
5270 if (h->root.root.string[0] == '.'
5271 && h->root.root.string[1] != 0
b31867b6 5272 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5273 ;
5274 else
5275 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5276 }
5277 else
5278 {
5279 asection *s;
87d72d41 5280 Elf_Internal_Sym *isym;
1e2f5b6e 5281
87d72d41
AM
5282 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5283 abfd, r_symndx);
5284 if (isym == NULL)
8387904d 5285 return FALSE;
87d72d41
AM
5286
5287 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5288 if (s != NULL && s != sec)
3f764659 5289 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5290 }
1e2f5b6e 5291 }
e86ce104
AM
5292 /* Fall through. */
5293
04c9666a 5294 case R_PPC64_REL30:
5bd4f169 5295 case R_PPC64_REL32:
04c9666a 5296 case R_PPC64_REL64:
65f38f15
AM
5297 case R_PPC64_ADDR14:
5298 case R_PPC64_ADDR14_BRNTAKEN:
5299 case R_PPC64_ADDR14_BRTAKEN:
5300 case R_PPC64_ADDR16:
5301 case R_PPC64_ADDR16_DS:
5302 case R_PPC64_ADDR16_HA:
5303 case R_PPC64_ADDR16_HI:
5304 case R_PPC64_ADDR16_HIGHER:
5305 case R_PPC64_ADDR16_HIGHERA:
5306 case R_PPC64_ADDR16_HIGHEST:
5307 case R_PPC64_ADDR16_HIGHESTA:
5308 case R_PPC64_ADDR16_LO:
5309 case R_PPC64_ADDR16_LO_DS:
5310 case R_PPC64_ADDR24:
65f38f15 5311 case R_PPC64_ADDR32:
65f38f15
AM
5312 case R_PPC64_UADDR16:
5313 case R_PPC64_UADDR32:
5314 case R_PPC64_UADDR64:
5bd4f169 5315 case R_PPC64_TOC:
81848ca0
AM
5316 if (h != NULL && !info->shared)
5317 /* We may need a copy reloc. */
f5385ebf 5318 h->non_got_ref = 1;
81848ca0 5319
41bd81ab 5320 /* Don't propagate .opd relocs. */
1e2f5b6e 5321 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5322 break;
e86ce104 5323
65f38f15
AM
5324 /* If we are creating a shared library, and this is a reloc
5325 against a global symbol, or a non PC relative reloc
5326 against a local symbol, then we need to copy the reloc
5327 into the shared library. However, if we are linking with
5328 -Bsymbolic, we do not need to copy a reloc against a
5329 global symbol which is defined in an object we are
5330 including in the link (i.e., DEF_REGULAR is set). At
5331 this point we have not seen all the input files, so it is
5332 possible that DEF_REGULAR is not set now but will be set
5333 later (it is never cleared). In case of a weak definition,
5334 DEF_REGULAR may be cleared later by a strong definition in
5335 a shared library. We account for that possibility below by
f4656909 5336 storing information in the dyn_relocs field of the hash
65f38f15
AM
5337 table entry. A similar situation occurs when creating
5338 shared libraries and symbol visibility changes render the
5339 symbol local.
5340
5341 If on the other hand, we are creating an executable, we
5342 may need to keep relocations for symbols satisfied by a
5343 dynamic library if we manage to avoid copy relocs for the
5344 symbol. */
411e1bfb 5345 dodyn:
65f38f15 5346 if ((info->shared
1d483afe 5347 && (must_be_dyn_reloc (info, r_type)
65f38f15
AM
5348 || (h != NULL
5349 && (! info->symbolic
5350 || h->root.type == bfd_link_hash_defweak
f5385ebf 5351 || !h->def_regular))))
f4656909
AM
5352 || (ELIMINATE_COPY_RELOCS
5353 && !info->shared
65f38f15
AM
5354 && h != NULL
5355 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5356 || !h->def_regular))
5357 || (!info->shared
5358 && ifunc != NULL))
5bd4f169 5359 {
6061a67d
AM
5360 struct elf_dyn_relocs *p;
5361 struct elf_dyn_relocs **head;
ec338859 5362
65f38f15
AM
5363 /* We must copy these reloc types into the output file.
5364 Create a reloc section in dynobj and make room for
5365 this reloc. */
5bd4f169
AM
5366 if (sreloc == NULL)
5367 {
83bac4b0
NC
5368 sreloc = _bfd_elf_make_dynamic_reloc_section
5369 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5370
5bd4f169 5371 if (sreloc == NULL)
83bac4b0 5372 return FALSE;
5bd4f169
AM
5373 }
5374
65f38f15
AM
5375 /* If this is a global symbol, we count the number of
5376 relocations we need for this symbol. */
5377 if (h != NULL)
5378 {
ec338859 5379 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
5380 }
5381 else
5382 {
ec338859
AM
5383 /* Track dynamic relocs needed for local syms too.
5384 We really need local syms available to do this
5385 easily. Oh well. */
ec338859 5386 asection *s;
6edfbbad 5387 void *vpp;
87d72d41 5388 Elf_Internal_Sym *isym;
6edfbbad 5389
87d72d41
AM
5390 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5391 abfd, r_symndx);
5392 if (isym == NULL)
b34976b6 5393 return FALSE;
ec338859 5394
87d72d41
AM
5395 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5396 if (s == NULL)
5397 s = sec;
5398
6edfbbad 5399 vpp = &elf_section_data (s)->local_dynrel;
6061a67d 5400 head = (struct elf_dyn_relocs **) vpp;
65f38f15 5401 }
ec338859
AM
5402
5403 p = *head;
5404 if (p == NULL || p->sec != sec)
5405 {
4ce794b7 5406 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 5407 if (p == NULL)
b34976b6 5408 return FALSE;
ec338859
AM
5409 p->next = *head;
5410 *head = p;
5411 p->sec = sec;
5412 p->count = 0;
5413 p->pc_count = 0;
5414 }
5415
5416 p->count += 1;
1d483afe 5417 if (!must_be_dyn_reloc (info, r_type))
ec338859 5418 p->pc_count += 1;
65f38f15 5419 }
5bd4f169 5420 break;
65f38f15
AM
5421
5422 default:
96e0dda4 5423 break;
5bd4f169
AM
5424 }
5425 }
5426
b34976b6 5427 return TRUE;
5bd4f169
AM
5428}
5429
8387904d
AM
5430/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5431 of the code entry point, and its section. */
5432
5433static bfd_vma
5434opd_entry_value (asection *opd_sec,
5435 bfd_vma offset,
5436 asection **code_sec,
5437 bfd_vma *code_off)
5438{
5439 bfd *opd_bfd = opd_sec->owner;
8860955f 5440 Elf_Internal_Rela *relocs;
8387904d 5441 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5442 bfd_vma val;
8387904d 5443
4b85d634
AM
5444 /* No relocs implies we are linking a --just-symbols object. */
5445 if (opd_sec->reloc_count == 0)
5446 {
aa374f67
AM
5447 char buf[8];
5448
5449 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
4b85d634 5450 return (bfd_vma) -1;
3b36f7e6 5451
aa374f67 5452 val = bfd_get_64 (opd_bfd, buf);
4b85d634
AM
5453 if (code_sec != NULL)
5454 {
5455 asection *sec, *likely = NULL;
5456 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5457 if (sec->vma <= val
5458 && (sec->flags & SEC_LOAD) != 0
5459 && (sec->flags & SEC_ALLOC) != 0)
5460 likely = sec;
5461 if (likely != NULL)
5462 {
5463 *code_sec = likely;
5464 if (code_off != NULL)
5465 *code_off = val - likely->vma;
5466 }
5467 }
5468 return val;
5469 }
5470
0c8d6e5c 5471 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5472
8860955f
AM
5473 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5474 if (relocs == NULL)
5475 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5476
8387904d 5477 /* Go find the opd reloc at the sym address. */
8860955f 5478 lo = relocs;
8387904d
AM
5479 BFD_ASSERT (lo != NULL);
5480 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5481 val = (bfd_vma) -1;
8387904d
AM
5482 while (lo < hi)
5483 {
5484 look = lo + (hi - lo) / 2;
5485 if (look->r_offset < offset)
5486 lo = look + 1;
5487 else if (look->r_offset > offset)
5488 hi = look;
5489 else
5490 {
0ffa91dd
NC
5491 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5492
8387904d
AM
5493 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5494 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5495 {
5496 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5497 asection *sec;
5498
5499 if (symndx < symtab_hdr->sh_info)
5500 {
5501 Elf_Internal_Sym *sym;
5502
5503 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5504 if (sym == NULL)
5505 {
5506 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5507 symtab_hdr->sh_info,
5508 0, NULL, NULL, NULL);
5509 if (sym == NULL)
645ea6a9 5510 break;
8387904d
AM
5511 symtab_hdr->contents = (bfd_byte *) sym;
5512 }
5513
5514 sym += symndx;
5515 val = sym->st_value;
cb33740c 5516 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5517 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5518 }
5519 else
5520 {
5521 struct elf_link_hash_entry **sym_hashes;
5522 struct elf_link_hash_entry *rh;
5523
5524 sym_hashes = elf_sym_hashes (opd_bfd);
5525 rh = sym_hashes[symndx - symtab_hdr->sh_info];
b31867b6 5526 rh = elf_follow_link (rh);
8387904d
AM
5527 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5528 || rh->root.type == bfd_link_hash_defweak);
5529 val = rh->root.u.def.value;
5530 sec = rh->root.u.def.section;
5531 }
5532 val += look->r_addend;
5533 if (code_off != NULL)
5534 *code_off = val;
5535 if (code_sec != NULL)
5536 *code_sec = sec;
5537 if (sec != NULL && sec->output_section != NULL)
5538 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5539 }
5540 break;
5541 }
5542 }
645ea6a9 5543
645ea6a9 5544 return val;
8387904d
AM
5545}
5546
854b41e7
AM
5547/* Return true if symbol is defined in a regular object file. */
5548
5549static bfd_boolean
5550is_static_defined (struct elf_link_hash_entry *h)
5551{
5552 return ((h->root.type == bfd_link_hash_defined
5553 || h->root.type == bfd_link_hash_defweak)
5554 && h->root.u.def.section != NULL
5555 && h->root.u.def.section->output_section != NULL);
5556}
5557
b31867b6
AM
5558/* If FDH is a function descriptor symbol, return the associated code
5559 entry symbol if it is defined. Return NULL otherwise. */
5560
5561static struct ppc_link_hash_entry *
5562defined_code_entry (struct ppc_link_hash_entry *fdh)
5563{
5564 if (fdh->is_func_descriptor)
5565 {
5566 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5567 if (fh->elf.root.type == bfd_link_hash_defined
5568 || fh->elf.root.type == bfd_link_hash_defweak)
5569 return fh;
5570 }
5571 return NULL;
5572}
5573
5574/* If FH is a function code entry symbol, return the associated
5575 function descriptor symbol if it is defined. Return NULL otherwise. */
5576
5577static struct ppc_link_hash_entry *
5578defined_func_desc (struct ppc_link_hash_entry *fh)
5579{
5580 if (fh->oh != NULL
5581 && fh->oh->is_func_descriptor)
5582 {
5583 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5584 if (fdh->elf.root.type == bfd_link_hash_defined
5585 || fdh->elf.root.type == bfd_link_hash_defweak)
5586 return fdh;
5587 }
5588 return NULL;
5589}
5590
74f0fb50
AM
5591/* Mark all our entry sym sections, both opd and code section. */
5592
5593static void
5594ppc64_elf_gc_keep (struct bfd_link_info *info)
5595{
5596 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5597 struct bfd_sym_chain *sym;
5598
4dfe6ac6
NC
5599 if (htab == NULL)
5600 return;
5601
74f0fb50
AM
5602 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5603 {
b31867b6 5604 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5605 asection *sec;
5606
5607 eh = (struct ppc_link_hash_entry *)
b31867b6 5608 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5609 if (eh == NULL)
5610 continue;
5611 if (eh->elf.root.type != bfd_link_hash_defined
5612 && eh->elf.root.type != bfd_link_hash_defweak)
5613 continue;
5614
b31867b6
AM
5615 fh = defined_code_entry (eh);
5616 if (fh != NULL)
74f0fb50 5617 {
b31867b6 5618 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5619 sec->flags |= SEC_KEEP;
5620 }
5621 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5622 && opd_entry_value (eh->elf.root.u.def.section,
5623 eh->elf.root.u.def.value,
5624 &sec, NULL) != (bfd_vma) -1)
5625 sec->flags |= SEC_KEEP;
5626
5627 sec = eh->elf.root.u.def.section;
5628 sec->flags |= SEC_KEEP;
5629 }
5630}
5631
64d03ab5
AM
5632/* Mark sections containing dynamically referenced symbols. When
5633 building shared libraries, we must assume that any visible symbol is
5634 referenced. */
5635
5636static bfd_boolean
5637ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5638{
5639 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5640 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5641 struct ppc_link_hash_entry *fdh;
64d03ab5 5642
64d03ab5 5643 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5644 fdh = defined_func_desc (eh);
5645 if (fdh != NULL)
5646 eh = fdh;
64d03ab5
AM
5647
5648 if ((eh->elf.root.type == bfd_link_hash_defined
5649 || eh->elf.root.type == bfd_link_hash_defweak)
5650 && (eh->elf.ref_dynamic
5651 || (!info->executable
5652 && eh->elf.def_regular
5653 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5654 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5655 {
5656 asection *code_sec;
b31867b6 5657 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5658
5659 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5660
5661 /* Function descriptor syms cause the associated
5662 function code sym section to be marked. */
b31867b6
AM
5663 fh = defined_code_entry (eh);
5664 if (fh != NULL)
5665 {
5666 code_sec = fh->elf.root.u.def.section;
5667 code_sec->flags |= SEC_KEEP;
5668 }
64d03ab5
AM
5669 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5670 && opd_entry_value (eh->elf.root.u.def.section,
5671 eh->elf.root.u.def.value,
5672 &code_sec, NULL) != (bfd_vma) -1)
5673 code_sec->flags |= SEC_KEEP;
5674 }
5675
5676 return TRUE;
5677}
5678
5bd4f169
AM
5679/* Return the section that should be marked against GC for a given
5680 relocation. */
5681
5682static asection *
4ce794b7 5683ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5684 struct bfd_link_info *info,
4ce794b7
AM
5685 Elf_Internal_Rela *rel,
5686 struct elf_link_hash_entry *h,
5687 Elf_Internal_Sym *sym)
5bd4f169 5688{
ccfa59ea
AM
5689 asection *rsec;
5690
ccfa59ea
AM
5691 /* Syms return NULL if we're marking .opd, so we avoid marking all
5692 function sections, as all functions are referenced in .opd. */
5693 rsec = NULL;
5694 if (get_opd_info (sec) != NULL)
5695 return rsec;
1e2f5b6e 5696
5bd4f169
AM
5697 if (h != NULL)
5698 {
04c9666a 5699 enum elf_ppc64_reloc_type r_type;
b31867b6 5700 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5701
4ce794b7 5702 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5703 switch (r_type)
5bd4f169
AM
5704 {
5705 case R_PPC64_GNU_VTINHERIT:
5706 case R_PPC64_GNU_VTENTRY:
5707 break;
5708
5709 default:
5710 switch (h->root.type)
5711 {
5712 case bfd_link_hash_defined:
5713 case bfd_link_hash_defweak:
ccfa59ea 5714 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5715 fdh = defined_func_desc (eh);
5716 if (fdh != NULL)
5717 eh = fdh;
1e2f5b6e
AM
5718
5719 /* Function descriptor syms cause the associated
5720 function code sym section to be marked. */
b31867b6
AM
5721 fh = defined_code_entry (eh);
5722 if (fh != NULL)
ccfa59ea
AM
5723 {
5724 /* They also mark their opd section. */
74f0fb50 5725 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5726
b31867b6 5727 rsec = fh->elf.root.u.def.section;
ccfa59ea 5728 }
8387904d
AM
5729 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5730 && opd_entry_value (eh->elf.root.u.def.section,
5731 eh->elf.root.u.def.value,
5732 &rsec, NULL) != (bfd_vma) -1)
74f0fb50 5733 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5734 else
1e2f5b6e
AM
5735 rsec = h->root.u.def.section;
5736 break;
5bd4f169
AM
5737
5738 case bfd_link_hash_common:
1e2f5b6e
AM
5739 rsec = h->root.u.c.p->section;
5740 break;
5bd4f169
AM
5741
5742 default:
fb34365b 5743 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5744 }
5745 }
5746 }
5747 else
5748 {
74f0fb50 5749 struct _opd_sec_data *opd;
1e2f5b6e
AM
5750
5751 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5752 opd = get_opd_info (rsec);
5753 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5754 {
74f0fb50 5755 rsec->gc_mark = 1;
ccfa59ea 5756
74f0fb50 5757 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5758 }
5bd4f169
AM
5759 }
5760
1e2f5b6e 5761 return rsec;
5bd4f169
AM
5762}
5763
65f38f15
AM
5764/* Update the .got, .plt. and dynamic reloc reference counts for the
5765 section being removed. */
5bd4f169 5766
b34976b6 5767static bfd_boolean
4ce794b7
AM
5768ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5769 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5770{
411e1bfb 5771 struct ppc_link_hash_table *htab;
5bd4f169
AM
5772 Elf_Internal_Shdr *symtab_hdr;
5773 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5774 struct got_entry **local_got_ents;
5bd4f169 5775 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5776
7dda2462
TG
5777 if (info->relocatable)
5778 return TRUE;
5779
680a3378
AM
5780 if ((sec->flags & SEC_ALLOC) == 0)
5781 return TRUE;
5782
ec338859
AM
5783 elf_section_data (sec)->local_dynrel = NULL;
5784
411e1bfb 5785 htab = ppc_hash_table (info);
4dfe6ac6
NC
5786 if (htab == NULL)
5787 return FALSE;
5788
0ffa91dd 5789 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5790 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5791 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5792
5793 relend = relocs + sec->reloc_count;
5794 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5795 {
5796 unsigned long r_symndx;
04c9666a 5797 enum elf_ppc64_reloc_type r_type;
58ac9f71 5798 struct elf_link_hash_entry *h = NULL;
f961d9dd 5799 unsigned char tls_type = 0;
5bd4f169 5800
a33d1f77 5801 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5802 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5803 if (r_symndx >= symtab_hdr->sh_info)
5804 {
5805 struct ppc_link_hash_entry *eh;
6061a67d
AM
5806 struct elf_dyn_relocs **pp;
5807 struct elf_dyn_relocs *p;
58ac9f71
AM
5808
5809 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5810 h = elf_follow_link (h);
58ac9f71
AM
5811 eh = (struct ppc_link_hash_entry *) h;
5812
5813 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5814 if (p->sec == sec)
5815 {
5816 /* Everything must go for SEC. */
5817 *pp = p->next;
5818 break;
5819 }
5820 }
5821
e054468f
AM
5822 if (is_branch_reloc (r_type))
5823 {
5824 struct plt_entry **ifunc = NULL;
5825 if (h != NULL)
5826 {
5827 if (h->type == STT_GNU_IFUNC)
5828 ifunc = &h->plt.plist;
5829 }
5830 else if (local_got_ents != NULL)
5831 {
5832 struct plt_entry **local_plt = (struct plt_entry **)
5833 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5834 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
5835 (local_plt + symtab_hdr->sh_info);
5836 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5837 ifunc = local_plt + r_symndx;
5838 }
5839 if (ifunc != NULL)
5840 {
5841 struct plt_entry *ent;
5842
5843 for (ent = *ifunc; ent != NULL; ent = ent->next)
5844 if (ent->addend == rel->r_addend)
5845 break;
5846 if (ent == NULL)
5847 abort ();
5848 if (ent->plt.refcount > 0)
5849 ent->plt.refcount -= 1;
5850 continue;
5851 }
5852 }
5853
a33d1f77
AM
5854 switch (r_type)
5855 {
411e1bfb
AM
5856 case R_PPC64_GOT_TLSLD16:
5857 case R_PPC64_GOT_TLSLD16_LO:
5858 case R_PPC64_GOT_TLSLD16_HI:
5859 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5860 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5861 goto dogot;
5862
5863 case R_PPC64_GOT_TLSGD16:
5864 case R_PPC64_GOT_TLSGD16_LO:
5865 case R_PPC64_GOT_TLSGD16_HI:
5866 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5867 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5868 goto dogot;
5869
5870 case R_PPC64_GOT_TPREL16_DS:
5871 case R_PPC64_GOT_TPREL16_LO_DS:
5872 case R_PPC64_GOT_TPREL16_HI:
5873 case R_PPC64_GOT_TPREL16_HA:
5874 tls_type = TLS_TLS | TLS_TPREL;
5875 goto dogot;
5876
5877 case R_PPC64_GOT_DTPREL16_DS:
5878 case R_PPC64_GOT_DTPREL16_LO_DS:
5879 case R_PPC64_GOT_DTPREL16_HI:
5880 case R_PPC64_GOT_DTPREL16_HA:
5881 tls_type = TLS_TLS | TLS_DTPREL;
5882 goto dogot;
5883
a33d1f77
AM
5884 case R_PPC64_GOT16:
5885 case R_PPC64_GOT16_DS:
5886 case R_PPC64_GOT16_HA:
5887 case R_PPC64_GOT16_HI:
5888 case R_PPC64_GOT16_LO:
5889 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5890 dogot:
5891 {
5892 struct got_entry *ent;
5893
58ac9f71
AM
5894 if (h != NULL)
5895 ent = h->got.glist;
411e1bfb
AM
5896 else
5897 ent = local_got_ents[r_symndx];
5898
5899 for (; ent != NULL; ent = ent->next)
5900 if (ent->addend == rel->r_addend
e717da7e 5901 && ent->owner == abfd
411e1bfb
AM
5902 && ent->tls_type == tls_type)
5903 break;
5904 if (ent == NULL)
5905 abort ();
5906 if (ent->got.refcount > 0)
5907 ent->got.refcount -= 1;
5908 }
a33d1f77 5909 break;
65f38f15 5910
a33d1f77
AM
5911 case R_PPC64_PLT16_HA:
5912 case R_PPC64_PLT16_HI:
5913 case R_PPC64_PLT16_LO:
5914 case R_PPC64_PLT32:
5915 case R_PPC64_PLT64:
721956f4
AM
5916 case R_PPC64_REL14:
5917 case R_PPC64_REL14_BRNTAKEN:
5918 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5919 case R_PPC64_REL24:
58ac9f71 5920 if (h != NULL)
5d1634d7 5921 {
411e1bfb
AM
5922 struct plt_entry *ent;
5923
411e1bfb
AM
5924 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5925 if (ent->addend == rel->r_addend)
5926 break;
7c6c1722 5927 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 5928 ent->plt.refcount -= 1;
5d1634d7 5929 }
e86ce104 5930 break;
5d1634d7 5931
a33d1f77
AM
5932 default:
5933 break;
5934 }
5935 }
b34976b6 5936 return TRUE;
5bd4f169
AM
5937}
5938
deb0e272
AM
5939/* The maximum size of .sfpr. */
5940#define SFPR_MAX (218*4)
5941
5942struct sfpr_def_parms
5943{
699733f6
AM
5944 const char name[12];
5945 unsigned char lo, hi;
deb0e272
AM
5946 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5947 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5948};
5949
5950/* Auto-generate _save*, _rest* functions in .sfpr. */
5951
4dfe6ac6 5952static bfd_boolean
deb0e272
AM
5953sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5954{
5955 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5956 unsigned int i;
5957 size_t len = strlen (parm->name);
5958 bfd_boolean writing = FALSE;
699733f6 5959 char sym[16];
deb0e272 5960
4dfe6ac6
NC
5961 if (htab == NULL)
5962 return FALSE;
5963
deb0e272
AM
5964 memcpy (sym, parm->name, len);
5965 sym[len + 2] = 0;
5966
5967 for (i = parm->lo; i <= parm->hi; i++)
5968 {
5969 struct elf_link_hash_entry *h;
5970
5971 sym[len + 0] = i / 10 + '0';
5972 sym[len + 1] = i % 10 + '0';
5973 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5974 if (h != NULL
f5385ebf 5975 && !h->def_regular)
deb0e272
AM
5976 {
5977 h->root.type = bfd_link_hash_defined;
5978 h->root.u.def.section = htab->sfpr;
5979 h->root.u.def.value = htab->sfpr->size;
5980 h->type = STT_FUNC;
f5385ebf 5981 h->def_regular = 1;
deb0e272
AM
5982 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5983 writing = TRUE;
5984 if (htab->sfpr->contents == NULL)
5985 {
5986 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5987 if (htab->sfpr->contents == NULL)
5988 return FALSE;
5989 }
5990 }
5991 if (writing)
5992 {
5993 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5994 if (i != parm->hi)
5995 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5996 else
5997 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5998 htab->sfpr->size = p - htab->sfpr->contents;
5999 }
6000 }
6001
6002 return TRUE;
6003}
6004
6005static bfd_byte *
6006savegpr0 (bfd *abfd, bfd_byte *p, int r)
6007{
6008 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6009 return p + 4;
6010}
6011
6012static bfd_byte *
6013savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6014{
6015 p = savegpr0 (abfd, p, r);
6016 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6017 p = p + 4;
6018 bfd_put_32 (abfd, BLR, p);
6019 return p + 4;
6020}
6021
6022static bfd_byte *
6023restgpr0 (bfd *abfd, bfd_byte *p, int r)
6024{
6025 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6026 return p + 4;
6027}
6028
6029static bfd_byte *
6030restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6031{
6032 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6033 p = p + 4;
6034 p = restgpr0 (abfd, p, r);
6035 bfd_put_32 (abfd, MTLR_R0, p);
6036 p = p + 4;
6037 if (r == 29)
6038 {
6039 p = restgpr0 (abfd, p, 30);
6040 p = restgpr0 (abfd, p, 31);
6041 }
6042 bfd_put_32 (abfd, BLR, p);
6043 return p + 4;
6044}
6045
6046static bfd_byte *
6047savegpr1 (bfd *abfd, bfd_byte *p, int r)
6048{
6049 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6050 return p + 4;
6051}
6052
6053static bfd_byte *
6054savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6055{
6056 p = savegpr1 (abfd, p, r);
6057 bfd_put_32 (abfd, BLR, p);
6058 return p + 4;
6059}
6060
6061static bfd_byte *
6062restgpr1 (bfd *abfd, bfd_byte *p, int r)
6063{
6064 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6065 return p + 4;
6066}
6067
6068static bfd_byte *
6069restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6070{
6071 p = restgpr1 (abfd, p, r);
6072 bfd_put_32 (abfd, BLR, p);
6073 return p + 4;
6074}
6075
6076static bfd_byte *
6077savefpr (bfd *abfd, bfd_byte *p, int r)
6078{
6079 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6080 return p + 4;
6081}
6082
6083static bfd_byte *
6084savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6085{
6086 p = savefpr (abfd, p, r);
6087 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6088 p = p + 4;
6089 bfd_put_32 (abfd, BLR, p);
6090 return p + 4;
6091}
6092
6093static bfd_byte *
6094restfpr (bfd *abfd, bfd_byte *p, int r)
6095{
6096 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6097 return p + 4;
6098}
6099
6100static bfd_byte *
6101restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6102{
6103 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6104 p = p + 4;
6105 p = restfpr (abfd, p, r);
6106 bfd_put_32 (abfd, MTLR_R0, p);
6107 p = p + 4;
6108 if (r == 29)
6109 {
6110 p = restfpr (abfd, p, 30);
6111 p = restfpr (abfd, p, 31);
6112 }
6113 bfd_put_32 (abfd, BLR, p);
6114 return p + 4;
6115}
6116
6117static bfd_byte *
6118savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6119{
6120 p = savefpr (abfd, p, r);
6121 bfd_put_32 (abfd, BLR, p);
6122 return p + 4;
6123}
6124
6125static bfd_byte *
6126restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6127{
6128 p = restfpr (abfd, p, r);
6129 bfd_put_32 (abfd, BLR, p);
6130 return p + 4;
6131}
6132
6133static bfd_byte *
6134savevr (bfd *abfd, bfd_byte *p, int r)
6135{
6136 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6137 p = p + 4;
6138 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6139 return p + 4;
6140}
6141
6142static bfd_byte *
6143savevr_tail (bfd *abfd, bfd_byte *p, int r)
6144{
6145 p = savevr (abfd, p, r);
6146 bfd_put_32 (abfd, BLR, p);
6147 return p + 4;
6148}
6149
6150static bfd_byte *
6151restvr (bfd *abfd, bfd_byte *p, int r)
6152{
6153 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6154 p = p + 4;
6155 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6156 return p + 4;
6157}
6158
6159static bfd_byte *
6160restvr_tail (bfd *abfd, bfd_byte *p, int r)
6161{
6162 p = restvr (abfd, p, r);
6163 bfd_put_32 (abfd, BLR, p);
6164 return p + 4;
6165}
6166
e86ce104
AM
6167/* Called via elf_link_hash_traverse to transfer dynamic linking
6168 information on function code symbol entries to their corresponding
6169 function descriptor symbol entries. */
deb0e272 6170
b34976b6 6171static bfd_boolean
4ce794b7 6172func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6173{
e86ce104 6174 struct bfd_link_info *info;
65f38f15 6175 struct ppc_link_hash_table *htab;
411e1bfb 6176 struct plt_entry *ent;
50bc7936
AM
6177 struct ppc_link_hash_entry *fh;
6178 struct ppc_link_hash_entry *fdh;
6179 bfd_boolean force_local;
5bd4f169 6180
50bc7936
AM
6181 fh = (struct ppc_link_hash_entry *) h;
6182 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6183 return TRUE;
e86ce104 6184
4ce794b7 6185 info = inf;
65f38f15 6186 htab = ppc_hash_table (info);
4dfe6ac6
NC
6187 if (htab == NULL)
6188 return FALSE;
5bd4f169 6189
c09bdfe5
AM
6190 /* Resolve undefined references to dot-symbols as the value
6191 in the function descriptor, if we have one in a regular object.
6192 This is to satisfy cases like ".quad .foo". Calls to functions
6193 in dynamic objects are handled elsewhere. */
6194 if (fh->elf.root.type == bfd_link_hash_undefweak
6195 && fh->was_undefined
b31867b6
AM
6196 && (fdh = defined_func_desc (fh)) != NULL
6197 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6198 && opd_entry_value (fdh->elf.root.u.def.section,
6199 fdh->elf.root.u.def.value,
c09bdfe5
AM
6200 &fh->elf.root.u.def.section,
6201 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6202 {
b31867b6 6203 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6204 fh->elf.forced_local = 1;
b31867b6
AM
6205 fh->elf.def_regular = fdh->elf.def_regular;
6206 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6207 }
6208
e86ce104
AM
6209 /* If this is a function code symbol, transfer dynamic linking
6210 information to the function descriptor symbol. */
50bc7936 6211 if (!fh->is_func)
b34976b6 6212 return TRUE;
e86ce104 6213
50bc7936 6214 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6215 if (ent->plt.refcount > 0)
6216 break;
50bc7936
AM
6217 if (ent == NULL
6218 || fh->elf.root.root.string[0] != '.'
6219 || fh->elf.root.root.string[1] == '\0')
6220 return TRUE;
5bd4f169 6221
50bc7936
AM
6222 /* Find the corresponding function descriptor symbol. Create it
6223 as undefined if necessary. */
5bd4f169 6224
b31867b6 6225 fdh = lookup_fdh (fh, htab);
50bc7936 6226 if (fdh == NULL
df131623 6227 && !info->executable
50bc7936
AM
6228 && (fh->elf.root.type == bfd_link_hash_undefined
6229 || fh->elf.root.type == bfd_link_hash_undefweak))
6230 {
908b32fc 6231 fdh = make_fdh (info, fh);
bb700d78
AM
6232 if (fdh == NULL)
6233 return FALSE;
50bc7936 6234 }
648cca2c 6235
908b32fc 6236 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6237 code symbol is strong undefined, make the fake sym the same.
6238 If the function code symbol is defined, then force the fake
6239 descriptor local; We can't support overriding of symbols in a
6240 shared library on a fake descriptor. */
908b32fc
AM
6241
6242 if (fdh != NULL
6243 && fdh->fake
433817dd 6244 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6245 {
433817dd
AM
6246 if (fh->elf.root.type == bfd_link_hash_undefined)
6247 {
6248 fdh->elf.root.type = bfd_link_hash_undefined;
6249 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6250 }
6251 else if (fh->elf.root.type == bfd_link_hash_defined
6252 || fh->elf.root.type == bfd_link_hash_defweak)
6253 {
6254 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6255 }
908b32fc
AM
6256 }
6257
50bc7936 6258 if (fdh != NULL
f5385ebf 6259 && !fdh->elf.forced_local
df131623 6260 && (!info->executable
f5385ebf
AM
6261 || fdh->elf.def_dynamic
6262 || fdh->elf.ref_dynamic
50bc7936
AM
6263 || (fdh->elf.root.type == bfd_link_hash_undefweak
6264 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6265 {
6266 if (fdh->elf.dynindx == -1)
c152c796 6267 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6268 return FALSE;
f5385ebf
AM
6269 fdh->elf.ref_regular |= fh->elf.ref_regular;
6270 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6271 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6272 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6273 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6274 {
40d16e0b 6275 move_plt_plist (fh, fdh);
f5385ebf 6276 fdh->elf.needs_plt = 1;
e86ce104 6277 }
50bc7936 6278 fdh->is_func_descriptor = 1;
34814b9f
AM
6279 fdh->oh = fh;
6280 fh->oh = fdh;
e86ce104
AM
6281 }
6282
50bc7936
AM
6283 /* Now that the info is on the function descriptor, clear the
6284 function code sym info. Any function code syms for which we
6285 don't have a definition in a regular file, we force local.
6286 This prevents a shared library from exporting syms that have
6287 been imported from another library. Function code syms that
6288 are really in the library we must leave global to prevent the
6289 linker dragging in a definition from a static library. */
93f3fa99
AM
6290 force_local = (!fh->elf.def_regular
6291 || fdh == NULL
6292 || !fdh->elf.def_regular
6293 || fdh->elf.forced_local);
50bc7936
AM
6294 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6295
b34976b6 6296 return TRUE;
e86ce104 6297}
40b8271b 6298
e86ce104 6299/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6300 this hook to a) provide some gcc support functions, and b) transfer
6301 dynamic linking information gathered so far on function code symbol
6302 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6303
b34976b6 6304static bfd_boolean
4ce794b7
AM
6305ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6306 struct bfd_link_info *info)
e86ce104
AM
6307{
6308 struct ppc_link_hash_table *htab;
82bd7b59 6309 unsigned int i;
deb0e272
AM
6310 const struct sfpr_def_parms funcs[] =
6311 {
6312 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6313 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6314 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6315 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6316 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6317 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6318 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6319 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6320 { "._savef", 14, 31, savefpr, savefpr1_tail },
6321 { "._restf", 14, 31, restfpr, restfpr1_tail },
6322 { "_savevr_", 20, 31, savevr, savevr_tail },
6323 { "_restvr_", 20, 31, restvr, restvr_tail }
6324 };
e86ce104
AM
6325
6326 htab = ppc_hash_table (info);
4dfe6ac6
NC
6327 if (htab == NULL)
6328 return FALSE;
6329
82bd7b59
AM
6330 if (htab->sfpr == NULL)
6331 /* We don't have any relocs. */
b34976b6 6332 return TRUE;
82bd7b59 6333
deb0e272
AM
6334 /* Provide any missing _save* and _rest* functions. */
6335 htab->sfpr->size = 0;
6336 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6337 if (!sfpr_define (info, &funcs[i]))
6338 return FALSE;
82bd7b59 6339
4ce794b7 6340 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6341
eea6121a 6342 if (htab->sfpr->size == 0)
8423293d 6343 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6344
b34976b6 6345 return TRUE;
e86ce104
AM
6346}
6347
6348/* Adjust a symbol defined by a dynamic object and referenced by a
6349 regular object. The current definition is in some section of the
6350 dynamic object, but we're not including those sections. We have to
6351 change the definition to something the rest of the link can
6352 understand. */
6353
b34976b6 6354static bfd_boolean
4ce794b7
AM
6355ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6356 struct elf_link_hash_entry *h)
e86ce104
AM
6357{
6358 struct ppc_link_hash_table *htab;
e86ce104 6359 asection *s;
e86ce104
AM
6360
6361 htab = ppc_hash_table (info);
4dfe6ac6
NC
6362 if (htab == NULL)
6363 return FALSE;
e86ce104
AM
6364
6365 /* Deal with function syms. */
6366 if (h->type == STT_FUNC
e054468f 6367 || h->type == STT_GNU_IFUNC
f5385ebf 6368 || h->needs_plt)
e86ce104
AM
6369 {
6370 /* Clear procedure linkage table information for any symbol that
6371 won't need a .plt entry. */
411e1bfb
AM
6372 struct plt_entry *ent;
6373 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6374 if (ent->plt.refcount > 0)
6375 break;
8387904d 6376 if (ent == NULL
e054468f
AM
6377 || (h->type != STT_GNU_IFUNC
6378 && (SYMBOL_CALLS_LOCAL (info, h)
6379 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6380 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6381 {
411e1bfb 6382 h->plt.plist = NULL;
f5385ebf 6383 h->needs_plt = 0;
40b8271b 6384 }
5bd4f169 6385 }
bbd7ec4a 6386 else
411e1bfb 6387 h->plt.plist = NULL;
5bd4f169
AM
6388
6389 /* If this is a weak symbol, and there is a real definition, the
6390 processor independent code will have arranged for us to see the
6391 real definition first, and we can just use the same value. */
f6e332e6 6392 if (h->u.weakdef != NULL)
5bd4f169 6393 {
f6e332e6
AM
6394 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6395 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6396 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6397 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6398 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6399 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6400 return TRUE;
5bd4f169
AM
6401 }
6402
5bd4f169
AM
6403 /* If we are creating a shared library, we must presume that the
6404 only references to the symbol are via the global offset table.
6405 For such cases we need not do anything here; the relocations will
6406 be handled correctly by relocate_section. */
6407 if (info->shared)
b34976b6 6408 return TRUE;
5bd4f169 6409
65f38f15
AM
6410 /* If there are no references to this symbol that do not use the
6411 GOT, we don't need to generate a copy reloc. */
f5385ebf 6412 if (!h->non_got_ref)
b34976b6 6413 return TRUE;
65f38f15 6414
b186458a
JJ
6415 /* Don't generate a copy reloc for symbols defined in the executable. */
6416 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6417 return TRUE;
6418
f4656909 6419 if (ELIMINATE_COPY_RELOCS)
65f38f15 6420 {
f4656909 6421 struct ppc_link_hash_entry * eh;
6061a67d 6422 struct elf_dyn_relocs *p;
65f38f15 6423
f4656909
AM
6424 eh = (struct ppc_link_hash_entry *) h;
6425 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6426 {
6427 s = p->sec->output_section;
6428 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6429 break;
6430 }
6431
6432 /* If we didn't find any dynamic relocs in read-only sections, then
6433 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6434 if (p == NULL)
6435 {
f5385ebf 6436 h->non_got_ref = 0;
f4656909
AM
6437 return TRUE;
6438 }
65f38f15
AM
6439 }
6440
5d35169e 6441 if (h->plt.plist != NULL)
97b639ba
AM
6442 {
6443 /* We should never get here, but unfortunately there are versions
6444 of gcc out there that improperly (for this ABI) put initialized
6445 function pointers, vtable refs and suchlike in read-only
6446 sections. Allow them to proceed, but warn that this might
6447 break at runtime. */
25f53a85 6448 info->callbacks->einfo
97b639ba 6449 (_("copy reloc against `%s' requires lazy plt linking; "
25f53a85 6450 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6451 h->root.root.string);
6452 }
5d35169e
AM
6453
6454 /* This is a reference to a symbol defined by a dynamic object which
6455 is not a function. */
6456
909272ee
AM
6457 if (h->size == 0)
6458 {
25f53a85
AM
6459 info->callbacks->einfo (_("dynamic variable `%s' is zero size\n"),
6460 h->root.root.string);
909272ee
AM
6461 return TRUE;
6462 }
6463
5bd4f169
AM
6464 /* We must allocate the symbol in our .dynbss section, which will
6465 become part of the .bss section of the executable. There will be
6466 an entry for this symbol in the .dynsym section. The dynamic
6467 object will contain position independent code, so all references
6468 from the dynamic object to this symbol will go through the global
6469 offset table. The dynamic linker will use the .dynsym entry to
6470 determine the address it must put in the global offset table, so
6471 both the dynamic object and the regular object will refer to the
6472 same memory location for the variable. */
5bd4f169 6473
04c9666a
AM
6474 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6475 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6476 runtime process image. We need to remember the offset into the
6477 .rela.bss section we are going to use. */
6478 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6479 {
eea6121a 6480 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6481 h->needs_copy = 1;
5bd4f169
AM
6482 }
6483
4ce794b7 6484 s = htab->dynbss;
5bd4f169 6485
027297b7 6486 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6487}
6488
e86ce104
AM
6489/* If given a function descriptor symbol, hide both the function code
6490 sym and the descriptor. */
6491static void
4ce794b7
AM
6492ppc64_elf_hide_symbol (struct bfd_link_info *info,
6493 struct elf_link_hash_entry *h,
6494 bfd_boolean force_local)
e86ce104 6495{
34814b9f 6496 struct ppc_link_hash_entry *eh;
e86ce104
AM
6497 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6498
34814b9f
AM
6499 eh = (struct ppc_link_hash_entry *) h;
6500 if (eh->is_func_descriptor)
e86ce104 6501 {
34814b9f 6502 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6503
721956f4 6504 if (fh == NULL)
d1329ca3
AM
6505 {
6506 const char *p, *q;
6507 struct ppc_link_hash_table *htab;
6508 char save;
6509
6510 /* We aren't supposed to use alloca in BFD because on
6511 systems which do not have alloca the version in libiberty
6512 calls xmalloc, which might cause the program to crash
6513 when it runs out of memory. This function doesn't have a
6514 return status, so there's no way to gracefully return an
6515 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6516 accessed; It's either a string in an ELF string table,
6517 or allocated in an objalloc structure. */
d1329ca3 6518
34814b9f 6519 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6520 save = *p;
6521 *(char *) p = '.';
6522 htab = ppc_hash_table (info);
4dfe6ac6
NC
6523 if (htab == NULL)
6524 return;
6525
34814b9f
AM
6526 fh = (struct ppc_link_hash_entry *)
6527 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6528 *(char *) p = save;
6529
6530 /* Unfortunately, if it so happens that the string we were
6531 looking for was allocated immediately before this string,
6532 then we overwrote the string terminator. That's the only
6533 reason the lookup should fail. */
6534 if (fh == NULL)
6535 {
34814b9f
AM
6536 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6537 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6538 --q, --p;
34814b9f
AM
6539 if (q < eh->elf.root.root.string && *p == '.')
6540 fh = (struct ppc_link_hash_entry *)
6541 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6542 }
6543 if (fh != NULL)
6544 {
34814b9f
AM
6545 eh->oh = fh;
6546 fh->oh = eh;
d1329ca3
AM
6547 }
6548 }
e86ce104 6549 if (fh != NULL)
34814b9f 6550 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6551 }
6552}
6553
411e1bfb 6554static bfd_boolean
8843416a
AM
6555get_sym_h (struct elf_link_hash_entry **hp,
6556 Elf_Internal_Sym **symp,
6557 asection **symsecp,
f961d9dd 6558 unsigned char **tls_maskp,
8843416a
AM
6559 Elf_Internal_Sym **locsymsp,
6560 unsigned long r_symndx,
6561 bfd *ibfd)
411e1bfb 6562{
0ffa91dd 6563 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6564
6565 if (r_symndx >= symtab_hdr->sh_info)
6566 {
6567 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6568 struct elf_link_hash_entry *h;
6569
6570 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6571 h = elf_follow_link (h);
411e1bfb
AM
6572
6573 if (hp != NULL)
6574 *hp = h;
6575
6576 if (symp != NULL)
6577 *symp = NULL;
6578
6579 if (symsecp != NULL)
6580 {
6581 asection *symsec = NULL;
6582 if (h->root.type == bfd_link_hash_defined
6583 || h->root.type == bfd_link_hash_defweak)
6584 symsec = h->root.u.def.section;
6585 *symsecp = symsec;
6586 }
6587
e7b938ca 6588 if (tls_maskp != NULL)
411e1bfb
AM
6589 {
6590 struct ppc_link_hash_entry *eh;
6591
6592 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6593 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6594 }
6595 }
6596 else
6597 {
6598 Elf_Internal_Sym *sym;
6599 Elf_Internal_Sym *locsyms = *locsymsp;
6600
6601 if (locsyms == NULL)
6602 {
6603 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6604 if (locsyms == NULL)
6605 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6606 symtab_hdr->sh_info,
6607 0, NULL, NULL, NULL);
6608 if (locsyms == NULL)
6609 return FALSE;
6610 *locsymsp = locsyms;
6611 }
6612 sym = locsyms + r_symndx;
6613
6614 if (hp != NULL)
6615 *hp = NULL;
6616
6617 if (symp != NULL)
6618 *symp = sym;
6619
6620 if (symsecp != NULL)
cb33740c 6621 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6622
e7b938ca 6623 if (tls_maskp != NULL)
411e1bfb
AM
6624 {
6625 struct got_entry **lgot_ents;
f961d9dd 6626 unsigned char *tls_mask;
411e1bfb 6627
e7b938ca 6628 tls_mask = NULL;
411e1bfb
AM
6629 lgot_ents = elf_local_got_ents (ibfd);
6630 if (lgot_ents != NULL)
6631 {
e054468f
AM
6632 struct plt_entry **local_plt = (struct plt_entry **)
6633 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6634 unsigned char *lgot_masks = (unsigned char *)
e054468f 6635 (local_plt + symtab_hdr->sh_info);
e7b938ca 6636 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6637 }
e7b938ca 6638 *tls_maskp = tls_mask;
411e1bfb
AM
6639 }
6640 }
6641 return TRUE;
6642}
6643
e7b938ca 6644/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6645 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6646 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6647
6648static int
f961d9dd 6649get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6650 unsigned long *toc_symndx,
6651 bfd_vma *toc_addend,
0d4792f7 6652 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6653 const Elf_Internal_Rela *rel,
6654 bfd *ibfd)
411e1bfb
AM
6655{
6656 unsigned long r_symndx;
0d4792f7 6657 int next_r;
411e1bfb
AM
6658 struct elf_link_hash_entry *h;
6659 Elf_Internal_Sym *sym;
6660 asection *sec;
6661 bfd_vma off;
6662
6663 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6664 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6665 return 0;
411e1bfb 6666
e7b938ca 6667 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6668 || sec == NULL
6bee8834 6669 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6670 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6671 return 1;
411e1bfb
AM
6672
6673 /* Look inside a TOC section too. */
6674 if (h != NULL)
6675 {
6676 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6677 off = h->root.u.def.value;
6678 }
6679 else
6680 off = sym->st_value;
6681 off += rel->r_addend;
6682 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6683 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6684 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6685 if (toc_symndx != NULL)
6686 *toc_symndx = r_symndx;
3a71aa26
AM
6687 if (toc_addend != NULL)
6688 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6689 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6690 return 0;
854b41e7 6691 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6692 && (next_r == -1 || next_r == -2))
6693 return 1 - next_r;
951fd09b 6694 return 1;
411e1bfb
AM
6695}
6696
754021d0 6697/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6698 code for the old ABI, these will already have been done. */
754021d0
AM
6699
6700static bfd_boolean
6701adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6702{
6703 struct ppc_link_hash_entry *eh;
6704 asection *sym_sec;
74f0fb50 6705 struct _opd_sec_data *opd;
754021d0
AM
6706
6707 if (h->root.type == bfd_link_hash_indirect)
6708 return TRUE;
6709
754021d0
AM
6710 if (h->root.type != bfd_link_hash_defined
6711 && h->root.type != bfd_link_hash_defweak)
6712 return TRUE;
6713
6714 eh = (struct ppc_link_hash_entry *) h;
6715 if (eh->adjust_done)
6716 return TRUE;
6717
6718 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6719 opd = get_opd_info (sym_sec);
6720 if (opd != NULL && opd->adjust != NULL)
754021d0 6721 {
74f0fb50 6722 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6723 if (adjust == -1)
6724 {
6725 /* This entry has been deleted. */
b3fac117 6726 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6727 if (dsec == NULL)
6728 {
6729 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6730 if (elf_discarded_section (dsec))
6731 {
b3fac117 6732 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6733 break;
6734 }
6735 }
4025353c 6736 eh->elf.root.u.def.value = 0;
81688140 6737 eh->elf.root.u.def.section = dsec;
4025353c
AM
6738 }
6739 else
6740 eh->elf.root.u.def.value += adjust;
754021d0
AM
6741 eh->adjust_done = 1;
6742 }
6743 return TRUE;
6744}
6745
8c1d1bb8
AM
6746/* Handles decrementing dynamic reloc counts for the reloc specified by
6747 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6748 have already been determined. */
6749
6750static bfd_boolean
6751dec_dynrel_count (bfd_vma r_info,
6752 asection *sec,
6753 struct bfd_link_info *info,
6754 Elf_Internal_Sym **local_syms,
6755 struct elf_link_hash_entry *h,
6756 asection *sym_sec)
6757{
6758 enum elf_ppc64_reloc_type r_type;
6061a67d
AM
6759 struct elf_dyn_relocs *p;
6760 struct elf_dyn_relocs **pp;
8c1d1bb8
AM
6761
6762 /* Can this reloc be dynamic? This switch, and later tests here
6763 should be kept in sync with the code in check_relocs. */
6764 r_type = ELF64_R_TYPE (r_info);
6765 switch (r_type)
6766 {
6767 default:
6768 return TRUE;
6769
6770 case R_PPC64_TPREL16:
6771 case R_PPC64_TPREL16_LO:
6772 case R_PPC64_TPREL16_HI:
6773 case R_PPC64_TPREL16_HA:
6774 case R_PPC64_TPREL16_DS:
6775 case R_PPC64_TPREL16_LO_DS:
6776 case R_PPC64_TPREL16_HIGHER:
6777 case R_PPC64_TPREL16_HIGHERA:
6778 case R_PPC64_TPREL16_HIGHEST:
6779 case R_PPC64_TPREL16_HIGHESTA:
6780 if (!info->shared)
6781 return TRUE;
6782
6783 case R_PPC64_TPREL64:
6784 case R_PPC64_DTPMOD64:
6785 case R_PPC64_DTPREL64:
6786 case R_PPC64_ADDR64:
6787 case R_PPC64_REL30:
6788 case R_PPC64_REL32:
6789 case R_PPC64_REL64:
6790 case R_PPC64_ADDR14:
6791 case R_PPC64_ADDR14_BRNTAKEN:
6792 case R_PPC64_ADDR14_BRTAKEN:
6793 case R_PPC64_ADDR16:
6794 case R_PPC64_ADDR16_DS:
6795 case R_PPC64_ADDR16_HA:
6796 case R_PPC64_ADDR16_HI:
6797 case R_PPC64_ADDR16_HIGHER:
6798 case R_PPC64_ADDR16_HIGHERA:
6799 case R_PPC64_ADDR16_HIGHEST:
6800 case R_PPC64_ADDR16_HIGHESTA:
6801 case R_PPC64_ADDR16_LO:
6802 case R_PPC64_ADDR16_LO_DS:
6803 case R_PPC64_ADDR24:
6804 case R_PPC64_ADDR32:
6805 case R_PPC64_UADDR16:
6806 case R_PPC64_UADDR32:
6807 case R_PPC64_UADDR64:
6808 case R_PPC64_TOC:
6809 break;
6810 }
6811
6812 if (local_syms != NULL)
6813 {
6814 unsigned long r_symndx;
6815 Elf_Internal_Sym *sym;
6816 bfd *ibfd = sec->owner;
6817
6818 r_symndx = ELF64_R_SYM (r_info);
6819 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6820 return FALSE;
6821 }
6822
6823 if ((info->shared
1d483afe 6824 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8
AM
6825 || (h != NULL
6826 && (!info->symbolic
6827 || h->root.type == bfd_link_hash_defweak
6828 || !h->def_regular))))
6829 || (ELIMINATE_COPY_RELOCS
6830 && !info->shared
6831 && h != NULL
6832 && (h->root.type == bfd_link_hash_defweak
6833 || !h->def_regular)))
6834 ;
6835 else
6836 return TRUE;
6837
6838 if (h != NULL)
6839 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6840 else
6edfbbad 6841 {
60124e18
AM
6842 if (sym_sec != NULL)
6843 {
6844 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6061a67d 6845 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6846 }
6847 else
6848 {
6849 void *vpp = &elf_section_data (sec)->local_dynrel;
6061a67d 6850 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6851 }
6852
6853 /* elf_gc_sweep may have already removed all dyn relocs associated
6854 with local syms for a given section. Don't report a dynreloc
6855 miscount. */
6856 if (*pp == NULL)
6857 return TRUE;
6edfbbad 6858 }
8c1d1bb8
AM
6859
6860 while ((p = *pp) != NULL)
6861 {
6862 if (p->sec == sec)
6863 {
1d483afe 6864 if (!must_be_dyn_reloc (info, r_type))
8c1d1bb8
AM
6865 p->pc_count -= 1;
6866 p->count -= 1;
6867 if (p->count == 0)
6868 *pp = p->next;
6869 return TRUE;
6870 }
6871 pp = &p->next;
6872 }
6873
25f53a85
AM
6874 info->callbacks->einfo (_("dynreloc miscount for %B, section %A\n"),
6875 sec->owner, sec);
8c1d1bb8
AM
6876 bfd_set_error (bfd_error_bad_value);
6877 return FALSE;
6878}
6879
754021d0
AM
6880/* Remove unused Official Procedure Descriptor entries. Currently we
6881 only remove those associated with functions in discarded link-once
6882 sections, or weakly defined functions that have been overridden. It
6883 would be possible to remove many more entries for statically linked
6884 applications. */
6885
b34976b6 6886bfd_boolean
33c0ec9d 6887ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
6888{
6889 bfd *ibfd;
754021d0 6890 bfd_boolean some_edited = FALSE;
3f764659 6891 asection *need_pad = NULL;
1e2f5b6e 6892
411e1bfb 6893 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
6894 {
6895 asection *sec;
6896 Elf_Internal_Rela *relstart, *rel, *relend;
6897 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6898 Elf_Internal_Sym *local_syms;
1e2f5b6e 6899 bfd_vma offset;
74f0fb50 6900 struct _opd_sec_data *opd;
3f764659
JJ
6901 bfd_boolean need_edit, add_aux_fields;
6902 bfd_size_type cnt_16b = 0;
1e2f5b6e 6903
854b41e7
AM
6904 if (!is_ppc64_elf (ibfd))
6905 continue;
6906
1e2f5b6e 6907 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6908 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6909 continue;
6910
4b85d634
AM
6911 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6912 continue;
6913
1e2f5b6e
AM
6914 if (sec->output_section == bfd_abs_section_ptr)
6915 continue;
6916
6917 /* Look through the section relocs. */
6918 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6919 continue;
6920
6cdc0ccc 6921 local_syms = NULL;
0ffa91dd 6922 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6923
6924 /* Read the relocations. */
4ce794b7 6925 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6926 info->keep_memory);
1e2f5b6e 6927 if (relstart == NULL)
b34976b6 6928 return FALSE;
1e2f5b6e
AM
6929
6930 /* First run through the relocs to check they are sane, and to
6931 determine whether we need to edit this opd section. */
b34976b6 6932 need_edit = FALSE;
3f764659 6933 need_pad = sec;
1e2f5b6e
AM
6934 offset = 0;
6935 relend = relstart + sec->reloc_count;
50bc7936 6936 for (rel = relstart; rel < relend; )
1e2f5b6e 6937 {
04c9666a 6938 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6939 unsigned long r_symndx;
6940 asection *sym_sec;
6941 struct elf_link_hash_entry *h;
6942 Elf_Internal_Sym *sym;
6943
3f764659 6944 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6945 only interested in the reloc pointing to a function entry
6946 point. */
50bc7936
AM
6947 if (rel->r_offset != offset
6948 || rel + 1 >= relend
6949 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
6950 {
6951 /* If someone messes with .opd alignment then after a
6952 "ld -r" we might have padding in the middle of .opd.
6953 Also, there's nothing to prevent someone putting
6954 something silly in .opd with the assembler. No .opd
b34976b6 6955 optimization for them! */
3f764659 6956 broken_opd:
1e2f5b6e 6957 (*_bfd_error_handler)
d003868e 6958 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 6959 need_edit = FALSE;
1e2f5b6e
AM
6960 break;
6961 }
6962
50bc7936
AM
6963 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6964 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6965 {
6966 (*_bfd_error_handler)
d003868e
AM
6967 (_("%B: unexpected reloc type %u in .opd section"),
6968 ibfd, r_type);
50bc7936
AM
6969 need_edit = FALSE;
6970 break;
6971 }
6972
1e2f5b6e 6973 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6974 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6975 r_symndx, ibfd))
50bc7936 6976 goto error_ret;
1e2f5b6e
AM
6977
6978 if (sym_sec == NULL || sym_sec->owner == NULL)
6979 {
411e1bfb
AM
6980 const char *sym_name;
6981 if (h != NULL)
6982 sym_name = h->root.root.string;
6983 else
26c61ae5
L
6984 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6985 sym_sec);
411e1bfb 6986
1e2f5b6e 6987 (*_bfd_error_handler)
d003868e
AM
6988 (_("%B: undefined sym `%s' in .opd section"),
6989 ibfd, sym_name);
b34976b6 6990 need_edit = FALSE;
1e2f5b6e
AM
6991 break;
6992 }
6993
51020317
AM
6994 /* opd entries are always for functions defined in the
6995 current input bfd. If the symbol isn't defined in the
6996 input bfd, then we won't be using the function in this
6997 bfd; It must be defined in a linkonce section in another
6998 bfd, or is weak. It's also possible that we are
6999 discarding the function due to a linker script /DISCARD/,
7000 which we test for via the output_section. */
7001 if (sym_sec->owner != ibfd
7002 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7003 need_edit = TRUE;
1e2f5b6e 7004
50bc7936 7005 rel += 2;
3f764659
JJ
7006 if (rel == relend
7007 || (rel + 1 == relend && rel->r_offset == offset + 16))
7008 {
7009 if (sec->size == offset + 24)
7010 {
7011 need_pad = NULL;
7012 break;
7013 }
7014 if (rel == relend && sec->size == offset + 16)
7015 {
7016 cnt_16b++;
7017 break;
7018 }
7019 goto broken_opd;
7020 }
7021
7022 if (rel->r_offset == offset + 24)
7023 offset += 24;
7024 else if (rel->r_offset != offset + 16)
7025 goto broken_opd;
7026 else if (rel + 1 < relend
7027 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7028 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7029 {
7030 offset += 16;
7031 cnt_16b++;
7032 }
7033 else if (rel + 2 < relend
7034 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7035 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7036 {
7037 offset += 24;
7038 rel += 1;
7039 }
7040 else
7041 goto broken_opd;
1e2f5b6e
AM
7042 }
7043
3f764659
JJ
7044 add_aux_fields = non_overlapping && cnt_16b > 0;
7045
7046 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7047 {
7048 Elf_Internal_Rela *write_rel;
d4730f92 7049 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7050 bfd_byte *rptr, *wptr;
983bddc8 7051 bfd_byte *new_contents;
b34976b6 7052 bfd_boolean skip;
3f764659 7053 long opd_ent_size;
74f0fb50
AM
7054 bfd_size_type amt;
7055
983bddc8 7056 new_contents = NULL;
74f0fb50
AM
7057 amt = sec->size * sizeof (long) / 8;
7058 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7059 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7060 if (opd->adjust == NULL)
7061 return FALSE;
7062 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7063
7064 /* This seems a waste of time as input .opd sections are all
7065 zeros as generated by gcc, but I suppose there's no reason
7066 this will always be so. We might start putting something in
7067 the third word of .opd entries. */
7068 if ((sec->flags & SEC_IN_MEMORY) == 0)
7069 {
eea6121a
AM
7070 bfd_byte *loc;
7071 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7072 {
eea6121a
AM
7073 if (loc != NULL)
7074 free (loc);
50bc7936 7075 error_ret:
6cdc0ccc
AM
7076 if (local_syms != NULL
7077 && symtab_hdr->contents != (unsigned char *) local_syms)
7078 free (local_syms);
6cdc0ccc
AM
7079 if (elf_section_data (sec)->relocs != relstart)
7080 free (relstart);
b34976b6 7081 return FALSE;
6cdc0ccc 7082 }
1e2f5b6e
AM
7083 sec->contents = loc;
7084 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7085 }
7086
7087 elf_section_data (sec)->relocs = relstart;
7088
3f764659 7089 new_contents = sec->contents;
3f764659
JJ
7090 if (add_aux_fields)
7091 {
7092 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7093 if (new_contents == NULL)
7094 return FALSE;
7095 need_pad = FALSE;
3f764659 7096 }
b4f4e59f
AM
7097 wptr = new_contents;
7098 rptr = sec->contents;
3f764659 7099
1e2f5b6e 7100 write_rel = relstart;
b34976b6 7101 skip = FALSE;
1e2f5b6e 7102 offset = 0;
3f764659 7103 opd_ent_size = 0;
1e2f5b6e
AM
7104 for (rel = relstart; rel < relend; rel++)
7105 {
50bc7936
AM
7106 unsigned long r_symndx;
7107 asection *sym_sec;
7108 struct elf_link_hash_entry *h;
7109 Elf_Internal_Sym *sym;
7110
7111 r_symndx = ELF64_R_SYM (rel->r_info);
7112 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7113 r_symndx, ibfd))
50bc7936
AM
7114 goto error_ret;
7115
1e2f5b6e
AM
7116 if (rel->r_offset == offset)
7117 {
50bc7936 7118 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7119
7120 /* See if the .opd entry is full 24 byte or
7121 16 byte (with fd_aux entry overlapped with next
7122 fd_func). */
7123 opd_ent_size = 24;
7124 if ((rel + 2 == relend && sec->size == offset + 16)
7125 || (rel + 3 < relend
7126 && rel[2].r_offset == offset + 16
7127 && rel[3].r_offset == offset + 24
7128 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7129 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7130 opd_ent_size = 16;
7131
4025353c
AM
7132 if (h != NULL
7133 && h->root.root.string[0] == '.')
c4f68ce3 7134 {
4dfe6ac6
NC
7135 struct ppc_link_hash_table *htab;
7136
7137 htab = ppc_hash_table (info);
7138 if (htab != NULL)
7139 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7140 htab);
c4f68ce3
AM
7141 if (fdh != NULL
7142 && fdh->elf.root.type != bfd_link_hash_defined
7143 && fdh->elf.root.type != bfd_link_hash_defweak)
7144 fdh = NULL;
7145 }
1e2f5b6e 7146
51020317
AM
7147 skip = (sym_sec->owner != ibfd
7148 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7149 if (skip)
7150 {
4025353c 7151 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7152 {
7153 /* Arrange for the function descriptor sym
7154 to be dropped. */
d6fe2dc1
AM
7155 fdh->elf.root.u.def.value = 0;
7156 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7157 }
74f0fb50 7158 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7159 }
7160 else
1e2f5b6e
AM
7161 {
7162 /* We'll be keeping this opd entry. */
7163
4025353c 7164 if (fdh != NULL)
1e2f5b6e 7165 {
754021d0
AM
7166 /* Redefine the function descriptor symbol to
7167 this location in the opd section. It is
7168 necessary to update the value here rather
7169 than using an array of adjustments as we do
7170 for local symbols, because various places
7171 in the generic ELF code use the value
7172 stored in u.def.value. */
3f764659 7173 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7174 fdh->adjust_done = 1;
1e2f5b6e 7175 }
754021d0
AM
7176
7177 /* Local syms are a bit tricky. We could
7178 tweak them as they can be cached, but
7179 we'd need to look through the local syms
7180 for the function descriptor sym which we
7181 don't have at the moment. So keep an
7182 array of adjustments. */
74f0fb50 7183 opd->adjust[rel->r_offset / 8]
3f764659 7184 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7185
7186 if (wptr != rptr)
3f764659
JJ
7187 memcpy (wptr, rptr, opd_ent_size);
7188 wptr += opd_ent_size;
7189 if (add_aux_fields && opd_ent_size == 16)
7190 {
7191 memset (wptr, '\0', 8);
7192 wptr += 8;
7193 }
1e2f5b6e 7194 }
3f764659
JJ
7195 rptr += opd_ent_size;
7196 offset += opd_ent_size;
1e2f5b6e
AM
7197 }
7198
50bc7936
AM
7199 if (skip)
7200 {
60124e18
AM
7201 if (!NO_OPD_RELOCS
7202 && !info->relocatable
18d944df
AM
7203 && !dec_dynrel_count (rel->r_info, sec, info,
7204 NULL, h, sym_sec))
8c1d1bb8 7205 goto error_ret;
50bc7936
AM
7206 }
7207 else
1e2f5b6e 7208 {
50bc7936
AM
7209 /* We need to adjust any reloc offsets to point to the
7210 new opd entries. While we're at it, we may as well
7211 remove redundant relocs. */
74f0fb50 7212 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7213 if (write_rel != rel)
7214 memcpy (write_rel, rel, sizeof (*rel));
7215 ++write_rel;
7216 }
7217 }
7218
3f764659 7219 sec->size = wptr - new_contents;
1e2f5b6e 7220 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7221 if (add_aux_fields)
7222 {
7223 free (sec->contents);
7224 sec->contents = new_contents;
7225 }
7226
05bf9422 7227 /* Fudge the header size too, as this is used later in
cdcf6e38 7228 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7229 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7230 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7231 some_edited = TRUE;
1e2f5b6e 7232 }
6cdc0ccc 7233 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7234 free (relstart);
6cdc0ccc 7235
411e1bfb
AM
7236 if (local_syms != NULL
7237 && symtab_hdr->contents != (unsigned char *) local_syms)
7238 {
7239 if (!info->keep_memory)
7240 free (local_syms);
7241 else
7242 symtab_hdr->contents = (unsigned char *) local_syms;
7243 }
7244 }
7245
754021d0
AM
7246 if (some_edited)
7247 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7248
3f764659
JJ
7249 /* If we are doing a final link and the last .opd entry is just 16 byte
7250 long, add a 8 byte padding after it. */
7251 if (need_pad != NULL && !info->relocatable)
7252 {
7253 bfd_byte *p;
7254
7255 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7256 {
7257 BFD_ASSERT (need_pad->size > 0);
7258
7259 p = bfd_malloc (need_pad->size + 8);
7260 if (p == NULL)
7261 return FALSE;
699733f6 7262
3f764659
JJ
7263 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7264 p, 0, need_pad->size))
7265 return FALSE;
7266
7267 need_pad->contents = p;
7268 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7269 }
7270 else
7271 {
7272 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7273 if (p == NULL)
7274 return FALSE;
7275
7276 need_pad->contents = p;
7277 }
7278
7279 memset (need_pad->contents + need_pad->size, 0, 8);
7280 need_pad->size += 8;
7281 }
7282
411e1bfb
AM
7283 return TRUE;
7284}
7285
e1918d23 7286/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7287
e1918d23 7288asection *
33c0ec9d
AM
7289ppc64_elf_tls_setup (struct bfd_link_info *info,
7290 int no_tls_get_addr_opt,
7291 int *no_multi_toc)
411e1bfb 7292{
411e1bfb
AM
7293 struct ppc_link_hash_table *htab;
7294
411e1bfb 7295 htab = ppc_hash_table (info);
4dfe6ac6
NC
7296 if (htab == NULL)
7297 return NULL;
7298
33c0ec9d
AM
7299 if (*no_multi_toc)
7300 htab->do_multi_toc = 0;
7301 else if (!htab->do_multi_toc)
7302 *no_multi_toc = 1;
7303
3a71aa26
AM
7304 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7305 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7306 FALSE, FALSE, TRUE));
a7f2871e
AM
7307 /* Move dynamic linking info to the function descriptor sym. */
7308 if (htab->tls_get_addr != NULL)
7309 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7310 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7311 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7312 FALSE, FALSE, TRUE));
a7f2871e
AM
7313 if (!no_tls_get_addr_opt)
7314 {
7315 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7316
7317 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7318 FALSE, FALSE, TRUE);
7319 if (opt != NULL)
7320 func_desc_adjust (opt, info);
7321 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7322 FALSE, FALSE, TRUE);
7323 if (opt_fd != NULL
7324 && (opt_fd->root.type == bfd_link_hash_defined
7325 || opt_fd->root.type == bfd_link_hash_defweak))
7326 {
7327 /* If glibc supports an optimized __tls_get_addr call stub,
7328 signalled by the presence of __tls_get_addr_opt, and we'll
7329 be calling __tls_get_addr via a plt call stub, then
7330 make __tls_get_addr point to __tls_get_addr_opt. */
7331 tga_fd = &htab->tls_get_addr_fd->elf;
7332 if (htab->elf.dynamic_sections_created
7333 && tga_fd != NULL
7334 && (tga_fd->type == STT_FUNC
7335 || tga_fd->needs_plt)
7336 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7337 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7338 && tga_fd->root.type == bfd_link_hash_undefweak)))
7339 {
7340 struct plt_entry *ent;
7341
7342 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7343 if (ent->plt.refcount > 0)
7344 break;
7345 if (ent != NULL)
7346 {
7347 tga_fd->root.type = bfd_link_hash_indirect;
7348 tga_fd->root.u.i.link = &opt_fd->root;
7349 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7350 if (opt_fd->dynindx != -1)
7351 {
7352 /* Use __tls_get_addr_opt in dynamic relocations. */
7353 opt_fd->dynindx = -1;
7354 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7355 opt_fd->dynstr_index);
7356 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7357 return NULL;
a7f2871e
AM
7358 }
7359 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7360 tga = &htab->tls_get_addr->elf;
7361 if (opt != NULL && tga != NULL)
7362 {
7363 tga->root.type = bfd_link_hash_indirect;
7364 tga->root.u.i.link = &opt->root;
7365 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7366 _bfd_elf_link_hash_hide_symbol (info, opt,
7367 tga->forced_local);
7368 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7369 }
7370 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7371 htab->tls_get_addr_fd->is_func_descriptor = 1;
7372 if (htab->tls_get_addr != NULL)
7373 {
7374 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7375 htab->tls_get_addr->is_func = 1;
7376 }
7377 }
7378 }
7379 }
7380 else
7381 no_tls_get_addr_opt = TRUE;
7382 }
7383 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7384 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7385}
8387904d 7386
3a71aa26
AM
7387/* Return TRUE iff REL is a branch reloc with a global symbol matching
7388 HASH1 or HASH2. */
8387904d 7389
3a71aa26
AM
7390static bfd_boolean
7391branch_reloc_hash_match (const bfd *ibfd,
7392 const Elf_Internal_Rela *rel,
7393 const struct ppc_link_hash_entry *hash1,
7394 const struct ppc_link_hash_entry *hash2)
7395{
7396 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7397 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7398 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7399
e054468f 7400 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7401 {
3a71aa26
AM
7402 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7403 struct elf_link_hash_entry *h;
8387904d 7404
3a71aa26 7405 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7406 h = elf_follow_link (h);
3a71aa26
AM
7407 if (h == &hash1->elf || h == &hash2->elf)
7408 return TRUE;
a48ebf4d 7409 }
3a71aa26 7410 return FALSE;
951fd09b 7411}
411e1bfb 7412
951fd09b
AM
7413/* Run through all the TLS relocs looking for optimization
7414 opportunities. The linker has been hacked (see ppc64elf.em) to do
7415 a preliminary section layout so that we know the TLS segment
7416 offsets. We can't optimize earlier because some optimizations need
7417 to know the tp offset, and we need to optimize before allocating
7418 dynamic relocations. */
7419
7420bfd_boolean
33c0ec9d 7421ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7422{
7423 bfd *ibfd;
7424 asection *sec;
7425 struct ppc_link_hash_table *htab;
663a1470 7426 unsigned char *toc_ref;
102890f0 7427 int pass;
951fd09b 7428
1d483afe 7429 if (info->relocatable || !info->executable)
411e1bfb
AM
7430 return TRUE;
7431
951fd09b 7432 htab = ppc_hash_table (info);
4dfe6ac6
NC
7433 if (htab == NULL)
7434 return FALSE;
7435
663a1470
AM
7436 /* Make two passes over the relocs. On the first pass, mark toc
7437 entries involved with tls relocs, and check that tls relocs
7438 involved in setting up a tls_get_addr call are indeed followed by
7439 such a call. If they are not, we can't do any tls optimization.
7440 On the second pass twiddle tls_mask flags to notify
7441 relocate_section that optimization can be done, and adjust got
7442 and plt refcounts. */
7443 toc_ref = NULL;
7444 for (pass = 0; pass < 2; ++pass)
7445 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7446 {
7447 Elf_Internal_Sym *locsyms = NULL;
7448 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7449
102890f0
AM
7450 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7451 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7452 {
7453 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7454 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7455
102890f0
AM
7456 /* Read the relocations. */
7457 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7458 info->keep_memory);
7459 if (relstart == NULL)
7460 return FALSE;
411e1bfb 7461
102890f0
AM
7462 relend = relstart + sec->reloc_count;
7463 for (rel = relstart; rel < relend; rel++)
7464 {
7465 enum elf_ppc64_reloc_type r_type;
7466 unsigned long r_symndx;
7467 struct elf_link_hash_entry *h;
7468 Elf_Internal_Sym *sym;
7469 asection *sym_sec;
f961d9dd
AM
7470 unsigned char *tls_mask;
7471 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7472 bfd_vma value;
7473 bfd_boolean ok_tprel, is_local;
7474 long toc_ref_index = 0;
7475 int expecting_tls_get_addr = 0;
663a1470 7476 bfd_boolean ret = FALSE;
411e1bfb 7477
102890f0
AM
7478 r_symndx = ELF64_R_SYM (rel->r_info);
7479 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7480 r_symndx, ibfd))
7481 {
7482 err_free_rel:
7483 if (elf_section_data (sec)->relocs != relstart)
7484 free (relstart);
7485 if (toc_ref != NULL)
7486 free (toc_ref);
7487 if (locsyms != NULL
0ffa91dd 7488 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7489 != (unsigned char *) locsyms))
7490 free (locsyms);
663a1470 7491 return ret;
102890f0 7492 }
411e1bfb 7493
102890f0
AM
7494 if (h != NULL)
7495 {
766bc656
AM
7496 if (h->root.type == bfd_link_hash_defined
7497 || h->root.type == bfd_link_hash_defweak)
7498 value = h->root.u.def.value;
7499 else if (h->root.type == bfd_link_hash_undefweak)
7500 value = 0;
7501 else
663a1470
AM
7502 {
7503 found_tls_get_addr_arg = 0;
7504 continue;
7505 }
102890f0
AM
7506 }
7507 else
7508 /* Symbols referenced by TLS relocs must be of type
7509 STT_TLS. So no need for .opd local sym adjust. */
7510 value = sym->st_value;
7511
7512 ok_tprel = FALSE;
7513 is_local = FALSE;
7514 if (h == NULL
7515 || !h->def_dynamic)
7516 {
7517 is_local = TRUE;
766bc656
AM
7518 if (h != NULL
7519 && h->root.type == bfd_link_hash_undefweak)
7520 ok_tprel = TRUE;
7521 else
7522 {
7523 value += sym_sec->output_offset;
7524 value += sym_sec->output_section->vma;
7525 value -= htab->elf.tls_sec->vma;
7526 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7527 < (bfd_vma) 1 << 32);
7528 }
102890f0 7529 }
951fd09b 7530
102890f0 7531 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7532 /* If this section has old-style __tls_get_addr calls
7533 without marker relocs, then check that each
7534 __tls_get_addr call reloc is preceded by a reloc
7535 that conceivably belongs to the __tls_get_addr arg
7536 setup insn. If we don't find matching arg setup
7537 relocs, don't do any tls optimization. */
7538 if (pass == 0
7539 && sec->has_tls_get_addr_call
7540 && h != NULL
7541 && (h == &htab->tls_get_addr->elf
7542 || h == &htab->tls_get_addr_fd->elf)
7543 && !found_tls_get_addr_arg
7544 && is_branch_reloc (r_type))
7545 {
25f53a85 7546 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7547 "TLS optimization disabled\n"),
7548 ibfd, sec, rel->r_offset);
7549 ret = TRUE;
7550 goto err_free_rel;
7551 }
7552
7553 found_tls_get_addr_arg = 0;
102890f0
AM
7554 switch (r_type)
7555 {
7556 case R_PPC64_GOT_TLSLD16:
7557 case R_PPC64_GOT_TLSLD16_LO:
7558 expecting_tls_get_addr = 1;
663a1470 7559 found_tls_get_addr_arg = 1;
102890f0
AM
7560 /* Fall thru */
7561
7562 case R_PPC64_GOT_TLSLD16_HI:
7563 case R_PPC64_GOT_TLSLD16_HA:
7564 /* These relocs should never be against a symbol
7565 defined in a shared lib. Leave them alone if
7566 that turns out to be the case. */
7567 if (!is_local)
7568 continue;
411e1bfb 7569
102890f0 7570 /* LD -> LE */
411e1bfb 7571 tls_set = 0;
102890f0
AM
7572 tls_clear = TLS_LD;
7573 tls_type = TLS_TLS | TLS_LD;
7574 break;
411e1bfb 7575
102890f0
AM
7576 case R_PPC64_GOT_TLSGD16:
7577 case R_PPC64_GOT_TLSGD16_LO:
7578 expecting_tls_get_addr = 1;
663a1470 7579 found_tls_get_addr_arg = 1;
102890f0
AM
7580 /* Fall thru */
7581
7582 case R_PPC64_GOT_TLSGD16_HI:
7583 case R_PPC64_GOT_TLSGD16_HA:
7584 if (ok_tprel)
7585 /* GD -> LE */
411e1bfb 7586 tls_set = 0;
102890f0
AM
7587 else
7588 /* GD -> IE */
7589 tls_set = TLS_TLS | TLS_TPRELGD;
7590 tls_clear = TLS_GD;
7591 tls_type = TLS_TLS | TLS_GD;
7592 break;
7593
7594 case R_PPC64_GOT_TPREL16_DS:
7595 case R_PPC64_GOT_TPREL16_LO_DS:
7596 case R_PPC64_GOT_TPREL16_HI:
7597 case R_PPC64_GOT_TPREL16_HA:
7598 if (ok_tprel)
7599 {
7600 /* IE -> LE */
7601 tls_set = 0;
7602 tls_clear = TLS_TPREL;
7603 tls_type = TLS_TLS | TLS_TPREL;
7604 break;
7605 }
411e1bfb
AM
7606 continue;
7607
727fc41e
AM
7608 case R_PPC64_TLSGD:
7609 case R_PPC64_TLSLD:
663a1470
AM
7610 found_tls_get_addr_arg = 1;
7611 /* Fall thru */
7612
7613 case R_PPC64_TLS:
7614 case R_PPC64_TOC16:
7615 case R_PPC64_TOC16_LO:
102890f0
AM
7616 if (sym_sec == NULL || sym_sec != toc)
7617 continue;
7618
7619 /* Mark this toc entry as referenced by a TLS
7620 code sequence. We can do that now in the
7621 case of R_PPC64_TLS, and after checking for
7622 tls_get_addr for the TOC16 relocs. */
7623 if (toc_ref == NULL)
663a1470
AM
7624 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7625 if (toc_ref == NULL)
7626 goto err_free_rel;
7627
102890f0
AM
7628 if (h != NULL)
7629 value = h->root.u.def.value;
7630 else
7631 value = sym->st_value;
7632 value += rel->r_addend;
7633 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 7634 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7635 if (r_type == R_PPC64_TLS
7636 || r_type == R_PPC64_TLSGD
7637 || r_type == R_PPC64_TLSLD)
102890f0
AM
7638 {
7639 toc_ref[toc_ref_index] = 1;
7640 continue;
7641 }
7642
7643 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7644 continue;
7645
7646 tls_set = 0;
7647 tls_clear = 0;
7648 expecting_tls_get_addr = 2;
7649 break;
7650
7651 case R_PPC64_TPREL64:
7652 if (pass == 0
7653 || sec != toc
7654 || toc_ref == NULL
663a1470 7655 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7656 continue;
7657 if (ok_tprel)
7658 {
7659 /* IE -> LE */
7660 tls_set = TLS_EXPLICIT;
7661 tls_clear = TLS_TPREL;
7662 break;
7663 }
7664 continue;
7665
7666 case R_PPC64_DTPMOD64:
7667 if (pass == 0
7668 || sec != toc
7669 || toc_ref == NULL
663a1470 7670 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7671 continue;
7672 if (rel + 1 < relend
7673 && (rel[1].r_info
7674 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7675 && rel[1].r_offset == rel->r_offset + 8)
7676 {
7677 if (ok_tprel)
7678 /* GD -> LE */
7679 tls_set = TLS_EXPLICIT | TLS_GD;
7680 else
7681 /* GD -> IE */
7682 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7683 tls_clear = TLS_GD;
7684 }
7685 else
7686 {
7687 if (!is_local)
7688 continue;
7689
7690 /* LD -> LE */
7691 tls_set = TLS_EXPLICIT;
7692 tls_clear = TLS_LD;
7693 }
7694 break;
7695
7696 default:
7697 continue;
7698 }
7699
7700 if (pass == 0)
7701 {
727fc41e
AM
7702 if (!expecting_tls_get_addr
7703 || !sec->has_tls_get_addr_call)
102890f0
AM
7704 continue;
7705
3a71aa26
AM
7706 if (rel + 1 < relend
7707 && branch_reloc_hash_match (ibfd, rel + 1,
7708 htab->tls_get_addr,
7709 htab->tls_get_addr_fd))
102890f0 7710 {
3a71aa26 7711 if (expecting_tls_get_addr == 2)
102890f0 7712 {
3a71aa26 7713 /* Check for toc tls entries. */
f961d9dd 7714 unsigned char *toc_tls;
3a71aa26
AM
7715 int retval;
7716
7717 retval = get_tls_mask (&toc_tls, NULL, NULL,
7718 &locsyms,
7719 rel, ibfd);
7720 if (retval == 0)
7721 goto err_free_rel;
663a1470
AM
7722 if (toc_tls != NULL)
7723 {
7724 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7725 found_tls_get_addr_arg = 1;
7726 if (retval > 1)
7727 toc_ref[toc_ref_index] = 1;
7728 }
102890f0 7729 }
3a71aa26 7730 continue;
102890f0
AM
7731 }
7732
7733 if (expecting_tls_get_addr != 1)
7734 continue;
7735
7736 /* Uh oh, we didn't find the expected call. We
7737 could just mark this symbol to exclude it
7738 from tls optimization but it's safer to skip
663a1470 7739 the entire optimization. */
25f53a85 7740 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7741 "TLS optimization disabled\n"),
7742 ibfd, sec, rel->r_offset);
7743 ret = TRUE;
7744 goto err_free_rel;
102890f0
AM
7745 }
7746
85f7a9cb 7747 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
7748 {
7749 struct plt_entry *ent;
7750 for (ent = htab->tls_get_addr->elf.plt.plist;
7751 ent != NULL;
7752 ent = ent->next)
7753 if (ent->addend == 0)
411e1bfb 7754 {
102890f0 7755 if (ent->plt.refcount > 0)
30038c59 7756 {
102890f0
AM
7757 ent->plt.refcount -= 1;
7758 expecting_tls_get_addr = 0;
30038c59 7759 }
102890f0 7760 break;
411e1bfb 7761 }
102890f0 7762 }
411e1bfb 7763
85f7a9cb 7764 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
7765 {
7766 struct plt_entry *ent;
7767 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7768 ent != NULL;
7769 ent = ent->next)
7770 if (ent->addend == 0)
411e1bfb 7771 {
102890f0
AM
7772 if (ent->plt.refcount > 0)
7773 ent->plt.refcount -= 1;
7774 break;
411e1bfb 7775 }
102890f0 7776 }
411e1bfb 7777
102890f0 7778 if (tls_clear == 0)
30038c59
AM
7779 continue;
7780
102890f0
AM
7781 if ((tls_set & TLS_EXPLICIT) == 0)
7782 {
7783 struct got_entry *ent;
411e1bfb 7784
102890f0
AM
7785 /* Adjust got entry for this reloc. */
7786 if (h != NULL)
7787 ent = h->got.glist;
7788 else
7789 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7790
102890f0
AM
7791 for (; ent != NULL; ent = ent->next)
7792 if (ent->addend == rel->r_addend
7793 && ent->owner == ibfd
7794 && ent->tls_type == tls_type)
7795 break;
7796 if (ent == NULL)
7797 abort ();
411e1bfb 7798
102890f0
AM
7799 if (tls_set == 0)
7800 {
7801 /* We managed to get rid of a got entry. */
7802 if (ent->got.refcount > 0)
7803 ent->got.refcount -= 1;
7804 }
7805 }
7806 else
7807 {
7808 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7809 we'll lose one or two dyn relocs. */
7810 if (!dec_dynrel_count (rel->r_info, sec, info,
7811 NULL, h, sym_sec))
7812 return FALSE;
411e1bfb 7813
102890f0
AM
7814 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7815 {
7816 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7817 NULL, h, sym_sec))
7818 return FALSE;
7819 }
7820 }
411e1bfb 7821
102890f0
AM
7822 *tls_mask |= tls_set;
7823 *tls_mask &= ~tls_clear;
7824 }
8c1d1bb8 7825
102890f0
AM
7826 if (elf_section_data (sec)->relocs != relstart)
7827 free (relstart);
7828 }
411e1bfb 7829
663a1470
AM
7830 if (locsyms != NULL
7831 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7832 {
7833 if (!info->keep_memory)
7834 free (locsyms);
7835 else
7836 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7837 }
7838 }
411e1bfb 7839
663a1470
AM
7840 if (toc_ref != NULL)
7841 free (toc_ref);
b34976b6 7842 return TRUE;
1e2f5b6e 7843}
b34976b6 7844
c5614fa4
AM
7845/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7846 the values of any global symbols in a toc section that has been
7847 edited. Globals in toc sections should be a rarity, so this function
7848 sets a flag if any are found in toc sections other than the one just
7849 edited, so that futher hash table traversals can be avoided. */
7850
7851struct adjust_toc_info
7852{
7853 asection *toc;
7854 unsigned long *skip;
7855 bfd_boolean global_toc_syms;
7856};
7857
ba761f19
AM
7858enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7859
c5614fa4
AM
7860static bfd_boolean
7861adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7862{
7863 struct ppc_link_hash_entry *eh;
7864 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7865 unsigned long i;
c5614fa4
AM
7866
7867 if (h->root.type == bfd_link_hash_indirect)
7868 return TRUE;
7869
c5614fa4
AM
7870 if (h->root.type != bfd_link_hash_defined
7871 && h->root.type != bfd_link_hash_defweak)
7872 return TRUE;
7873
7874 eh = (struct ppc_link_hash_entry *) h;
7875 if (eh->adjust_done)
7876 return TRUE;
7877
7878 if (eh->elf.root.u.def.section == toc_inf->toc)
7879 {
854b41e7
AM
7880 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7881 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7882 else
854b41e7
AM
7883 i = eh->elf.root.u.def.value >> 3;
7884
ba761f19 7885 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
7886 {
7887 (*_bfd_error_handler)
854b41e7
AM
7888 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7889 do
7890 ++i;
ba761f19 7891 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 7892 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 7893 }
854b41e7
AM
7894
7895 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
7896 eh->adjust_done = 1;
7897 }
7898 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7899 toc_inf->global_toc_syms = TRUE;
7900
7901 return TRUE;
7902}
7903
7904/* Examine all relocs referencing .toc sections in order to remove
7905 unused .toc entries. */
7906
7907bfd_boolean
33c0ec9d 7908ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
7909{
7910 bfd *ibfd;
7911 struct adjust_toc_info toc_inf;
67f0cbdb 7912 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 7913
67f0cbdb 7914 htab->do_toc_opt = 1;
c5614fa4
AM
7915 toc_inf.global_toc_syms = TRUE;
7916 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7917 {
7918 asection *toc, *sec;
7919 Elf_Internal_Shdr *symtab_hdr;
7920 Elf_Internal_Sym *local_syms;
425b145b 7921 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
7922 unsigned long *skip, *drop;
7923 unsigned char *used;
7924 unsigned char *keep, last, some_unused;
7925
854b41e7
AM
7926 if (!is_ppc64_elf (ibfd))
7927 continue;
7928
c5614fa4
AM
7929 toc = bfd_get_section_by_name (ibfd, ".toc");
7930 if (toc == NULL
92b7a70f 7931 || toc->size == 0
c5614fa4
AM
7932 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7933 || elf_discarded_section (toc))
7934 continue;
7935
425b145b 7936 toc_relocs = NULL;
c5614fa4 7937 local_syms = NULL;
0ffa91dd 7938 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
7939
7940 /* Look at sections dropped from the final link. */
7941 skip = NULL;
7942 relstart = NULL;
7943 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7944 {
7945 if (sec->reloc_count == 0
7946 || !elf_discarded_section (sec)
7947 || get_opd_info (sec)
7948 || (sec->flags & SEC_ALLOC) == 0
7949 || (sec->flags & SEC_DEBUGGING) != 0)
7950 continue;
7951
7952 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7953 if (relstart == NULL)
7954 goto error_ret;
7955
7956 /* Run through the relocs to see which toc entries might be
7957 unused. */
7958 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7959 {
7960 enum elf_ppc64_reloc_type r_type;
7961 unsigned long r_symndx;
7962 asection *sym_sec;
7963 struct elf_link_hash_entry *h;
7964 Elf_Internal_Sym *sym;
7965 bfd_vma val;
7966
7967 r_type = ELF64_R_TYPE (rel->r_info);
7968 switch (r_type)
7969 {
7970 default:
7971 continue;
7972
7973 case R_PPC64_TOC16:
7974 case R_PPC64_TOC16_LO:
7975 case R_PPC64_TOC16_HI:
7976 case R_PPC64_TOC16_HA:
7977 case R_PPC64_TOC16_DS:
7978 case R_PPC64_TOC16_LO_DS:
7979 break;
7980 }
7981
7982 r_symndx = ELF64_R_SYM (rel->r_info);
7983 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7984 r_symndx, ibfd))
7985 goto error_ret;
7986
7987 if (sym_sec != toc)
7988 continue;
7989
7990 if (h != NULL)
7991 val = h->root.u.def.value;
7992 else
7993 val = sym->st_value;
7994 val += rel->r_addend;
7995
7996 if (val >= toc->size)
7997 continue;
7998
7999 /* Anything in the toc ought to be aligned to 8 bytes.
8000 If not, don't mark as unused. */
8001 if (val & 7)
8002 continue;
8003
8004 if (skip == NULL)
8005 {
854b41e7 8006 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8007 if (skip == NULL)
8008 goto error_ret;
8009 }
8010
ba761f19 8011 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8012 }
8013
8014 if (elf_section_data (sec)->relocs != relstart)
8015 free (relstart);
8016 }
8017
ba761f19
AM
8018 /* For largetoc loads of address constants, we can convert
8019 . addis rx,2,addr@got@ha
8020 . ld ry,addr@got@l(rx)
8021 to
8022 . addis rx,2,addr@toc@ha
8023 . addi ry,rx,addr@toc@l
8024 when addr is within 2G of the toc pointer. This then means
8025 that the word storing "addr" in the toc is no longer needed. */
8026
8027 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8028 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8029 && toc->reloc_count != 0)
8030 {
8031 /* Read toc relocs. */
425b145b
AM
8032 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8033 info->keep_memory);
8034 if (toc_relocs == NULL)
ba761f19
AM
8035 goto error_ret;
8036
425b145b 8037 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8038 {
8039 enum elf_ppc64_reloc_type r_type;
8040 unsigned long r_symndx;
8041 asection *sym_sec;
8042 struct elf_link_hash_entry *h;
8043 Elf_Internal_Sym *sym;
8044 bfd_vma val, addr;
8045
8046 r_type = ELF64_R_TYPE (rel->r_info);
8047 if (r_type != R_PPC64_ADDR64)
8048 continue;
8049
8050 r_symndx = ELF64_R_SYM (rel->r_info);
8051 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8052 r_symndx, ibfd))
8053 goto error_ret;
8054
425b145b
AM
8055 if (sym_sec == NULL
8056 || elf_discarded_section (sym_sec))
8057 continue;
8058
726d3ab0 8059 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8060 continue;
8061
8062 if (h != NULL)
bddc25c9
AM
8063 {
8064 if (h->type == STT_GNU_IFUNC)
8065 continue;
8066 val = h->root.u.def.value;
8067 }
ba761f19 8068 else
bddc25c9
AM
8069 {
8070 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8071 continue;
8072 val = sym->st_value;
8073 }
ba761f19
AM
8074 val += rel->r_addend;
8075 val += sym_sec->output_section->vma + sym_sec->output_offset;
8076
8077 /* We don't yet know the exact toc pointer value, but we
8078 know it will be somewhere in the toc section. Don't
8079 optimize if the difference from any possible toc
8080 pointer is outside [ff..f80008000, 7fff7fff]. */
8081 addr = toc->output_section->vma + TOC_BASE_OFF;
8082 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8083 continue;
8084
8085 addr = toc->output_section->vma + toc->output_section->rawsize;
8086 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8087 continue;
8088
8089 if (skip == NULL)
8090 {
8091 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8092 if (skip == NULL)
8093 goto error_ret;
8094 }
8095
8096 skip[rel->r_offset >> 3]
425b145b 8097 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8098 }
ba761f19
AM
8099 }
8100
c5614fa4
AM
8101 if (skip == NULL)
8102 continue;
8103
8104 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8105 if (used == NULL)
8106 {
8107 error_ret:
8108 if (local_syms != NULL
8109 && symtab_hdr->contents != (unsigned char *) local_syms)
8110 free (local_syms);
8111 if (sec != NULL
8112 && relstart != NULL
8113 && elf_section_data (sec)->relocs != relstart)
8114 free (relstart);
425b145b
AM
8115 if (toc_relocs != NULL
8116 && elf_section_data (toc)->relocs != toc_relocs)
8117 free (toc_relocs);
c5614fa4
AM
8118 if (skip != NULL)
8119 free (skip);
8120 return FALSE;
8121 }
8122
30038c59
AM
8123 /* Now check all kept sections that might reference the toc.
8124 Check the toc itself last. */
8125 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8126 : ibfd->sections);
c5614fa4 8127 sec != NULL;
c5614fa4 8128 sec = (sec == toc ? NULL
c5614fa4 8129 : sec->next == NULL ? toc
30038c59 8130 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8131 : sec->next))
8132 {
8133 int repeat;
8134
8135 if (sec->reloc_count == 0
8136 || elf_discarded_section (sec)
8137 || get_opd_info (sec)
8138 || (sec->flags & SEC_ALLOC) == 0
8139 || (sec->flags & SEC_DEBUGGING) != 0)
8140 continue;
8141
854b41e7
AM
8142 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8143 info->keep_memory);
c5614fa4
AM
8144 if (relstart == NULL)
8145 goto error_ret;
8146
8147 /* Mark toc entries referenced as used. */
8148 repeat = 0;
8149 do
8150 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8151 {
8152 enum elf_ppc64_reloc_type r_type;
8153 unsigned long r_symndx;
8154 asection *sym_sec;
8155 struct elf_link_hash_entry *h;
8156 Elf_Internal_Sym *sym;
8157 bfd_vma val;
8158
8159 r_type = ELF64_R_TYPE (rel->r_info);
8160 switch (r_type)
8161 {
8162 case R_PPC64_TOC16:
8163 case R_PPC64_TOC16_LO:
8164 case R_PPC64_TOC16_HI:
8165 case R_PPC64_TOC16_HA:
8166 case R_PPC64_TOC16_DS:
8167 case R_PPC64_TOC16_LO_DS:
8168 /* In case we're taking addresses of toc entries. */
8169 case R_PPC64_ADDR64:
8170 break;
8171
8172 default:
8173 continue;
8174 }
8175
8176 r_symndx = ELF64_R_SYM (rel->r_info);
8177 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8178 r_symndx, ibfd))
8179 {
8180 free (used);
8181 goto error_ret;
8182 }
8183
8184 if (sym_sec != toc)
8185 continue;
8186
8187 if (h != NULL)
8188 val = h->root.u.def.value;
8189 else
8190 val = sym->st_value;
8191 val += rel->r_addend;
8192
8193 if (val >= toc->size)
8194 continue;
8195
ba761f19
AM
8196 if ((skip[val >> 3] & can_optimize) != 0)
8197 {
8198 bfd_vma off;
8199 unsigned char opc;
8200
8201 switch (r_type)
8202 {
8203 case R_PPC64_TOC16_HA:
8204 break;
8205
8206 case R_PPC64_TOC16_LO_DS:
8207 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8208 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8209 return FALSE;
8210 if ((opc & (0x3f << 2)) == (58u << 2))
8211 break;
8212 /* Fall thru */
8213
8214 default:
8215 /* Wrong sort of reloc, or not a ld. We may
8216 as well clear ref_from_discarded too. */
8217 skip[val >> 3] = 0;
8218 }
8219 }
8220
c5614fa4
AM
8221 /* For the toc section, we only mark as used if
8222 this entry itself isn't unused. */
8223 if (sec == toc
8224 && !used[val >> 3]
8225 && (used[rel->r_offset >> 3]
ba761f19 8226 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
c5614fa4
AM
8227 /* Do all the relocs again, to catch reference
8228 chains. */
8229 repeat = 1;
8230
8231 used[val >> 3] = 1;
8232 }
8233 while (repeat);
854b41e7
AM
8234
8235 if (elf_section_data (sec)->relocs != relstart)
8236 free (relstart);
c5614fa4
AM
8237 }
8238
8239 /* Merge the used and skip arrays. Assume that TOC
8240 doublewords not appearing as either used or unused belong
8241 to to an entry more than one doubleword in size. */
8242 for (drop = skip, keep = used, last = 0, some_unused = 0;
8243 drop < skip + (toc->size + 7) / 8;
8244 ++drop, ++keep)
8245 {
8246 if (*keep)
8247 {
ba761f19
AM
8248 *drop &= ~ref_from_discarded;
8249 if ((*drop & can_optimize) != 0)
8250 some_unused = 1;
c5614fa4
AM
8251 last = 0;
8252 }
8253 else if (*drop)
8254 {
8255 some_unused = 1;
ba761f19 8256 last = ref_from_discarded;
c5614fa4
AM
8257 }
8258 else
8259 *drop = last;
8260 }
8261
8262 free (used);
8263
8264 if (some_unused)
8265 {
8266 bfd_byte *contents, *src;
8267 unsigned long off;
d62b3684 8268 Elf_Internal_Sym *sym;
ba761f19 8269 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8270
8271 /* Shuffle the toc contents, and at the same time convert the
8272 skip array from booleans into offsets. */
8273 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8274 goto error_ret;
8275
8276 elf_section_data (toc)->this_hdr.contents = contents;
8277
8278 for (src = contents, off = 0, drop = skip;
8279 src < contents + toc->size;
8280 src += 8, ++drop)
8281 {
ba761f19
AM
8282 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8283 off += 8;
c5614fa4
AM
8284 else if (off != 0)
8285 {
8286 *drop = off;
8287 memcpy (src - off, src, 8);
8288 }
8289 }
854b41e7 8290 *drop = off;
c5614fa4
AM
8291 toc->rawsize = toc->size;
8292 toc->size = src - contents - off;
8293
ba761f19
AM
8294 /* Adjust addends for relocs against the toc section sym,
8295 and optimize any accesses we can. */
c5614fa4
AM
8296 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8297 {
8298 if (sec->reloc_count == 0
8299 || elf_discarded_section (sec))
8300 continue;
8301
8302 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8303 info->keep_memory);
c5614fa4
AM
8304 if (relstart == NULL)
8305 goto error_ret;
8306
8307 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8308 {
8309 enum elf_ppc64_reloc_type r_type;
8310 unsigned long r_symndx;
8311 asection *sym_sec;
8312 struct elf_link_hash_entry *h;
854b41e7 8313 bfd_vma val;
c5614fa4
AM
8314
8315 r_type = ELF64_R_TYPE (rel->r_info);
8316 switch (r_type)
8317 {
8318 default:
8319 continue;
8320
8321 case R_PPC64_TOC16:
8322 case R_PPC64_TOC16_LO:
8323 case R_PPC64_TOC16_HI:
8324 case R_PPC64_TOC16_HA:
8325 case R_PPC64_TOC16_DS:
8326 case R_PPC64_TOC16_LO_DS:
8327 case R_PPC64_ADDR64:
8328 break;
8329 }
8330
8331 r_symndx = ELF64_R_SYM (rel->r_info);
8332 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8333 r_symndx, ibfd))
8334 goto error_ret;
8335
ba761f19 8336 if (sym_sec != toc)
c5614fa4
AM
8337 continue;
8338
ba761f19
AM
8339 if (h != NULL)
8340 val = h->root.u.def.value;
8341 else
8342 {
8343 val = sym->st_value;
8344 if (val != 0)
8345 local_toc_syms = TRUE;
8346 }
8347
8348 val += rel->r_addend;
854b41e7
AM
8349
8350 if (val > toc->rawsize)
8351 val = toc->rawsize;
ba761f19
AM
8352 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8353 continue;
8354 else if ((skip[val >> 3] & can_optimize) != 0)
8355 {
8356 Elf_Internal_Rela *tocrel
425b145b 8357 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8358 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8359
8360 switch (r_type)
8361 {
8362 case R_PPC64_TOC16_HA:
8363 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8364 break;
8365
8366 case R_PPC64_TOC16_LO_DS:
8367 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8368 break;
8369
8370 default:
8371 abort ();
8372 }
8373 rel->r_addend = tocrel->r_addend;
8374 elf_section_data (sec)->relocs = relstart;
8375 continue;
8376 }
8377
8378 if (h != NULL || sym->st_value != 0)
8379 continue;
854b41e7
AM
8380
8381 rel->r_addend -= skip[val >> 3];
8382 elf_section_data (sec)->relocs = relstart;
c5614fa4 8383 }
854b41e7
AM
8384
8385 if (elf_section_data (sec)->relocs != relstart)
8386 free (relstart);
c5614fa4
AM
8387 }
8388
8389 /* We shouldn't have local or global symbols defined in the TOC,
8390 but handle them anyway. */
df22d223
AM
8391 if (local_syms != NULL)
8392 for (sym = local_syms;
8393 sym < local_syms + symtab_hdr->sh_info;
8394 ++sym)
8395 if (sym->st_value != 0
8396 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8397 {
8398 unsigned long i;
854b41e7 8399
df22d223
AM
8400 if (sym->st_value > toc->rawsize)
8401 i = toc->rawsize >> 3;
8402 else
8403 i = sym->st_value >> 3;
854b41e7 8404
df22d223
AM
8405 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8406 {
8407 if (local_toc_syms)
8408 (*_bfd_error_handler)
8409 (_("%s defined on removed toc entry"),
8410 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8411 do
8412 ++i;
8413 while ((skip[i] & (ref_from_discarded | can_optimize)));
8414 sym->st_value = (bfd_vma) i << 3;
8415 }
d62b3684 8416
df22d223
AM
8417 sym->st_value -= skip[i];
8418 symtab_hdr->contents = (unsigned char *) local_syms;
8419 }
c5614fa4 8420
854b41e7 8421 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8422 if (toc_inf.global_toc_syms)
8423 {
8424 toc_inf.toc = toc;
8425 toc_inf.skip = skip;
8426 toc_inf.global_toc_syms = FALSE;
8427 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8428 &toc_inf);
8429 }
854b41e7
AM
8430
8431 if (toc->reloc_count != 0)
8432 {
d4730f92 8433 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8434 Elf_Internal_Rela *wrel;
8435 bfd_size_type sz;
8436
854b41e7 8437 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8438 if (toc_relocs == NULL)
8439 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8440 info->keep_memory);
8441 if (toc_relocs == NULL)
8442 goto error_ret;
8443
425b145b
AM
8444 wrel = toc_relocs;
8445 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8446 if ((skip[rel->r_offset >> 3]
8447 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8448 {
8449 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8450 wrel->r_info = rel->r_info;
8451 wrel->r_addend = rel->r_addend;
8452 ++wrel;
8453 }
8454 else if (!dec_dynrel_count (rel->r_info, toc, info,
8455 &local_syms, NULL, NULL))
8456 goto error_ret;
8457
425b145b
AM
8458 elf_section_data (toc)->relocs = toc_relocs;
8459 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8460 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8461 sz = rel_hdr->sh_entsize;
8462 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8463 }
c5614fa4 8464 }
28be611c
AM
8465 else if (toc_relocs != NULL
8466 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8467 free (toc_relocs);
c5614fa4
AM
8468
8469 if (local_syms != NULL
8470 && symtab_hdr->contents != (unsigned char *) local_syms)
8471 {
8472 if (!info->keep_memory)
8473 free (local_syms);
8474 else
8475 symtab_hdr->contents = (unsigned char *) local_syms;
8476 }
8477 free (skip);
8478 }
8479
8480 return TRUE;
8481}
8482
1bbe0902
AM
8483/* Return true iff input section I references the TOC using
8484 instructions limited to +/-32k offsets. */
8485
8486bfd_boolean
8487ppc64_elf_has_small_toc_reloc (asection *i)
8488{
8489 return (is_ppc64_elf (i->owner)
8490 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8491}
8492
927be08e
AM
8493/* Allocate space for one GOT entry. */
8494
8495static void
8496allocate_got (struct elf_link_hash_entry *h,
8497 struct bfd_link_info *info,
8498 struct got_entry *gent)
8499{
8500 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8501 bfd_boolean dyn;
8502 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8503 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8504 ? 16 : 8);
8505 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8506 ? 2 : 1) * sizeof (Elf64_External_Rela);
8507 asection *got = ppc64_elf_tdata (gent->owner)->got;
8508
8509 gent->got.offset = got->size;
8510 got->size += entsize;
8511
8512 dyn = htab->elf.dynamic_sections_created;
8513 if ((info->shared
8514 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8515 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8516 || h->root.type != bfd_link_hash_undefweak))
8517 {
8518 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8519 relgot->size += rentsize;
8520 }
8521 else if (h->type == STT_GNU_IFUNC)
8522 {
8523 asection *relgot = htab->reliplt;
8524 relgot->size += rentsize;
8525 htab->got_reli_size += rentsize;
8526 }
8527}
8528
7865406b
AM
8529/* This function merges got entries in the same toc group. */
8530
8531static void
8532merge_got_entries (struct got_entry **pent)
8533{
8534 struct got_entry *ent, *ent2;
8535
8536 for (ent = *pent; ent != NULL; ent = ent->next)
8537 if (!ent->is_indirect)
8538 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8539 if (!ent2->is_indirect
8540 && ent2->addend == ent->addend
8541 && ent2->tls_type == ent->tls_type
8542 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8543 {
8544 ent2->is_indirect = TRUE;
8545 ent2->got.ent = ent;
8546 }
8547}
8548
65f38f15
AM
8549/* Allocate space in .plt, .got and associated reloc sections for
8550 dynamic relocs. */
5bd4f169 8551
b34976b6 8552static bfd_boolean
4ce794b7 8553allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8554{
65f38f15
AM
8555 struct bfd_link_info *info;
8556 struct ppc_link_hash_table *htab;
5bd4f169 8557 asection *s;
65f38f15 8558 struct ppc_link_hash_entry *eh;
6061a67d 8559 struct elf_dyn_relocs *p;
0b8bcf0d 8560 struct got_entry **pgent, *gent;
5bd4f169 8561
e92d460e 8562 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8563 return TRUE;
5bd4f169 8564
65f38f15
AM
8565 info = (struct bfd_link_info *) inf;
8566 htab = ppc_hash_table (info);
4dfe6ac6
NC
8567 if (htab == NULL)
8568 return FALSE;
5bd4f169 8569
e054468f
AM
8570 if ((htab->elf.dynamic_sections_created
8571 && h->dynindx != -1
8572 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8573 || h->type == STT_GNU_IFUNC)
5bd4f169 8574 {
411e1bfb
AM
8575 struct plt_entry *pent;
8576 bfd_boolean doneone = FALSE;
8577 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8578 if (pent->plt.refcount > 0)
8579 {
25f23106
AM
8580 if (!htab->elf.dynamic_sections_created
8581 || h->dynindx == -1)
e054468f
AM
8582 {
8583 s = htab->iplt;
8584 pent->plt.offset = s->size;
8585 s->size += PLT_ENTRY_SIZE;
8586 s = htab->reliplt;
8587 }
8588 else
8589 {
8590 /* If this is the first .plt entry, make room for the special
8591 first entry. */
8592 s = htab->plt;
8593 if (s->size == 0)
8594 s->size += PLT_INITIAL_ENTRY_SIZE;
8595
8596 pent->plt.offset = s->size;
8597
8598 /* Make room for this entry. */
8599 s->size += PLT_ENTRY_SIZE;
8600
8601 /* Make room for the .glink code. */
8602 s = htab->glink;
8603 if (s->size == 0)
8604 s->size += GLINK_CALL_STUB_SIZE;
8605 /* We need bigger stubs past index 32767. */
8606 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8607 s->size += 4;
8608 s->size += 2*4;
8609
8610 /* We also need to make an entry in the .rela.plt section. */
8611 s = htab->relplt;
8612 }
eea6121a 8613 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
8614 doneone = TRUE;
8615 }
8616 else
8617 pent->plt.offset = (bfd_vma) -1;
8618 if (!doneone)
65f38f15 8619 {
411e1bfb 8620 h->plt.plist = NULL;
f5385ebf 8621 h->needs_plt = 0;
65f38f15
AM
8622 }
8623 }
8624 else
8625 {
411e1bfb 8626 h->plt.plist = NULL;
f5385ebf 8627 h->needs_plt = 0;
65f38f15
AM
8628 }
8629
951fd09b
AM
8630 eh = (struct ppc_link_hash_entry *) h;
8631 /* Run through the TLS GD got entries first if we're changing them
8632 to TPREL. */
e7b938ca 8633 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
8634 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8635 if (gent->got.refcount > 0
8636 && (gent->tls_type & TLS_GD) != 0)
8637 {
8638 /* This was a GD entry that has been converted to TPREL. If
8639 there happens to be a TPREL entry we can use that one. */
8640 struct got_entry *ent;
8641 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8642 if (ent->got.refcount > 0
8643 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8644 && ent->addend == gent->addend
8645 && ent->owner == gent->owner)
951fd09b
AM
8646 {
8647 gent->got.refcount = 0;
8648 break;
8649 }
8650
8651 /* If not, then we'll be using our own TPREL entry. */
8652 if (gent->got.refcount != 0)
8653 gent->tls_type = TLS_TLS | TLS_TPREL;
8654 }
8655
7865406b
AM
8656 /* Remove any list entry that won't generate a word in the GOT before
8657 we call merge_got_entries. Otherwise we risk merging to empty
8658 entries. */
0b8bcf0d
AM
8659 pgent = &h->got.glist;
8660 while ((gent = *pgent) != NULL)
411e1bfb 8661 if (gent->got.refcount > 0)
7865406b
AM
8662 {
8663 if ((gent->tls_type & TLS_LD) != 0
8664 && !h->def_dynamic)
8665 {
8666 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8667 *pgent = gent->next;
8668 }
8669 else
8670 pgent = &gent->next;
8671 }
8672 else
8673 *pgent = gent->next;
8674
8675 if (!htab->do_multi_toc)
8676 merge_got_entries (&h->got.glist);
8677
8678 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8679 if (!gent->is_indirect)
411e1bfb
AM
8680 {
8681 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
8682 Undefined weak syms won't yet be marked as dynamic,
8683 nor will all TLS symbols. */
411e1bfb 8684 if (h->dynindx == -1
b099ab9f 8685 && !h->forced_local
25f23106 8686 && h->type != STT_GNU_IFUNC
b099ab9f 8687 && htab->elf.dynamic_sections_created)
411e1bfb 8688 {
c152c796 8689 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
8690 return FALSE;
8691 }
65f38f15 8692
0c8d6e5c 8693 if (!is_ppc64_elf (gent->owner))
927be08e 8694 abort ();
0ffa91dd 8695
927be08e 8696 allocate_got (h, info, gent);
411e1bfb 8697 }
65f38f15 8698
b099ab9f 8699 if (eh->dyn_relocs == NULL
25f23106
AM
8700 || (!htab->elf.dynamic_sections_created
8701 && h->type != STT_GNU_IFUNC))
b34976b6 8702 return TRUE;
65f38f15
AM
8703
8704 /* In the shared -Bsymbolic case, discard space allocated for
8705 dynamic pc-relative relocs against symbols which turn out to be
8706 defined in regular objects. For the normal shared case, discard
8707 space for relocs that have become local due to symbol visibility
8708 changes. */
8709
8710 if (info->shared)
8711 {
9c7a29a3 8712 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 8713 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
8714 generated via assembly. We want calls to protected symbols to
8715 resolve directly to the function rather than going via the plt.
8716 If people want function pointer comparisons to work as expected
8717 then they should avoid writing weird assembly. */
09695f56 8718 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 8719 {
6061a67d 8720 struct elf_dyn_relocs **pp;
65f38f15
AM
8721
8722 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 8723 {
65f38f15
AM
8724 p->count -= p->pc_count;
8725 p->pc_count = 0;
8726 if (p->count == 0)
8727 *pp = p->next;
8728 else
8729 pp = &p->next;
5bd4f169 8730 }
65f38f15 8731 }
4e795f50
AM
8732
8733 /* Also discard relocs on undefined weak syms with non-default
8734 visibility. */
cab87ef9
AM
8735 if (eh->dyn_relocs != NULL
8736 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
8737 {
8738 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8739 eh->dyn_relocs = NULL;
8740
8741 /* Make sure this symbol is output as a dynamic symbol.
8742 Undefined weak syms won't yet be marked as dynamic. */
8743 else if (h->dynindx == -1
8744 && !h->forced_local)
8745 {
8746 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8747 return FALSE;
8748 }
8749 }
65f38f15 8750 }
25f23106
AM
8751 else if (h->type == STT_GNU_IFUNC)
8752 {
8753 if (!h->non_got_ref)
8754 eh->dyn_relocs = NULL;
8755 }
f4656909 8756 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
8757 {
8758 /* For the non-shared case, discard space for relocs against
8759 symbols which turn out to need copy relocs or are not
8760 dynamic. */
8761
f5385ebf 8762 if (!h->non_got_ref
f5385ebf 8763 && !h->def_regular)
65f38f15
AM
8764 {
8765 /* Make sure this symbol is output as a dynamic symbol.
8766 Undefined weak syms won't yet be marked as dynamic. */
8767 if (h->dynindx == -1
f5385ebf 8768 && !h->forced_local)
65f38f15 8769 {
c152c796 8770 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8771 return FALSE;
65f38f15
AM
8772 }
8773
8774 /* If that succeeded, we know we'll be keeping all the
8775 relocs. */
8776 if (h->dynindx != -1)
8777 goto keep;
8778 }
8779
8780 eh->dyn_relocs = NULL;
8781
ec338859 8782 keep: ;
65f38f15
AM
8783 }
8784
8785 /* Finally, allocate space. */
8786 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8787 {
8788 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
8789 if (!htab->elf.dynamic_sections_created)
8790 sreloc = htab->reliplt;
eea6121a 8791 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
8792 }
8793
b34976b6 8794 return TRUE;
65f38f15
AM
8795}
8796
8797/* Find any dynamic relocs that apply to read-only sections. */
8798
b34976b6 8799static bfd_boolean
4ce794b7 8800readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
8801{
8802 struct ppc_link_hash_entry *eh;
6061a67d 8803 struct elf_dyn_relocs *p;
65f38f15
AM
8804
8805 eh = (struct ppc_link_hash_entry *) h;
8806 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8807 {
8808 asection *s = p->sec->output_section;
8809
8810 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8811 {
4ce794b7 8812 struct bfd_link_info *info = inf;
65f38f15
AM
8813
8814 info->flags |= DF_TEXTREL;
8815
8816 /* Not an error, just cut short the traversal. */
b34976b6 8817 return FALSE;
65f38f15
AM
8818 }
8819 }
b34976b6 8820 return TRUE;
65f38f15
AM
8821}
8822
8823/* Set the sizes of the dynamic sections. */
8824
b34976b6 8825static bfd_boolean
4ce794b7
AM
8826ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8827 struct bfd_link_info *info)
65f38f15
AM
8828{
8829 struct ppc_link_hash_table *htab;
8830 bfd *dynobj;
8831 asection *s;
b34976b6 8832 bfd_boolean relocs;
65f38f15 8833 bfd *ibfd;
7865406b 8834 struct got_entry *first_tlsld;
65f38f15
AM
8835
8836 htab = ppc_hash_table (info);
4dfe6ac6
NC
8837 if (htab == NULL)
8838 return FALSE;
8839
65f38f15
AM
8840 dynobj = htab->elf.dynobj;
8841 if (dynobj == NULL)
8842 abort ();
8843
8844 if (htab->elf.dynamic_sections_created)
8845 {
8846 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 8847 if (info->executable)
65f38f15
AM
8848 {
8849 s = bfd_get_section_by_name (dynobj, ".interp");
8850 if (s == NULL)
8851 abort ();
eea6121a 8852 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
8853 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8854 }
8855 }
8856
8857 /* Set up .got offsets for local syms, and space for local dynamic
8858 relocs. */
8859 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8860 {
411e1bfb
AM
8861 struct got_entry **lgot_ents;
8862 struct got_entry **end_lgot_ents;
e054468f
AM
8863 struct plt_entry **local_plt;
8864 struct plt_entry **end_local_plt;
f961d9dd 8865 unsigned char *lgot_masks;
65f38f15
AM
8866 bfd_size_type locsymcount;
8867 Elf_Internal_Shdr *symtab_hdr;
8868 asection *srel;
8869
0c8d6e5c 8870 if (!is_ppc64_elf (ibfd))
65f38f15
AM
8871 continue;
8872
8873 for (s = ibfd->sections; s != NULL; s = s->next)
8874 {
6061a67d 8875 struct elf_dyn_relocs *p;
65f38f15 8876
6edfbbad 8877 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 8878 {
ec338859
AM
8879 if (!bfd_is_abs_section (p->sec)
8880 && bfd_is_abs_section (p->sec->output_section))
8881 {
8882 /* Input section has been discarded, either because
8883 it is a copy of a linkonce section or due to
8884 linker script /DISCARD/, so we'll be discarding
8885 the relocs too. */
8886 }
248866a8 8887 else if (p->count != 0)
ec338859
AM
8888 {
8889 srel = elf_section_data (p->sec)->sreloc;
25f23106
AM
8890 if (!htab->elf.dynamic_sections_created)
8891 srel = htab->reliplt;
eea6121a 8892 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
8893 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8894 info->flags |= DF_TEXTREL;
ec338859 8895 }
65f38f15
AM
8896 }
8897 }
8898
411e1bfb
AM
8899 lgot_ents = elf_local_got_ents (ibfd);
8900 if (!lgot_ents)
65f38f15
AM
8901 continue;
8902
0ffa91dd 8903 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 8904 locsymcount = symtab_hdr->sh_info;
411e1bfb 8905 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
8906 local_plt = (struct plt_entry **) end_lgot_ents;
8907 end_local_plt = local_plt + locsymcount;
f961d9dd 8908 lgot_masks = (unsigned char *) end_local_plt;
e717da7e
AM
8909 s = ppc64_elf_tdata (ibfd)->got;
8910 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 8911 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 8912 {
0b8bcf0d 8913 struct got_entry **pent, *ent;
411e1bfb 8914
0b8bcf0d
AM
8915 pent = lgot_ents;
8916 while ((ent = *pent) != NULL)
411e1bfb
AM
8917 if (ent->got.refcount > 0)
8918 {
e7b938ca 8919 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 8920 {
927be08e 8921 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 8922 *pent = ent->next;
411e1bfb
AM
8923 }
8924 else
8925 {
25f23106 8926 unsigned int num = 1;
eea6121a 8927 ent->got.offset = s->size;
e7b938ca 8928 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
25f23106
AM
8929 num = 2;
8930 s->size += num * 8;
8931 if (info->shared)
8932 srel->size += num * sizeof (Elf64_External_Rela);
8933 else if ((*lgot_masks & PLT_IFUNC) != 0)
927be08e
AM
8934 {
8935 htab->reliplt->size
8936 += num * sizeof (Elf64_External_Rela);
8937 htab->got_reli_size
8938 += num * sizeof (Elf64_External_Rela);
8939 }
0b8bcf0d 8940 pent = &ent->next;
411e1bfb
AM
8941 }
8942 }
8943 else
0b8bcf0d 8944 *pent = ent->next;
65f38f15 8945 }
e054468f
AM
8946
8947 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
8948 for (; local_plt < end_local_plt; ++local_plt)
8949 {
8950 struct plt_entry *ent;
8951
8952 for (ent = *local_plt; ent != NULL; ent = ent->next)
8953 if (ent->plt.refcount > 0)
8954 {
91d6fa6a 8955 s = htab->iplt;
e054468f
AM
8956 ent->plt.offset = s->size;
8957 s->size += PLT_ENTRY_SIZE;
8958
25f23106 8959 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
8960 }
8961 else
8962 ent->plt.offset = (bfd_vma) -1;
8963 }
65f38f15
AM
8964 }
8965
8966 /* Allocate global sym .plt and .got entries, and space for global
8967 sym dynamic relocs. */
4ce794b7 8968 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 8969
7865406b 8970 first_tlsld = NULL;
102890f0
AM
8971 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8972 {
7865406b
AM
8973 struct got_entry *ent;
8974
0c8d6e5c 8975 if (!is_ppc64_elf (ibfd))
102890f0
AM
8976 continue;
8977
7865406b
AM
8978 ent = ppc64_tlsld_got (ibfd);
8979 if (ent->got.refcount > 0)
102890f0 8980 {
7865406b 8981 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 8982 {
7865406b
AM
8983 ent->is_indirect = TRUE;
8984 ent->got.ent = first_tlsld;
8985 }
8986 else
8987 {
8988 if (first_tlsld == NULL)
8989 first_tlsld = ent;
8990 s = ppc64_elf_tdata (ibfd)->got;
8991 ent->got.offset = s->size;
8992 ent->owner = ibfd;
8993 s->size += 16;
8994 if (info->shared)
8995 {
8996 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
8997 srel->size += sizeof (Elf64_External_Rela);
8998 }
102890f0
AM
8999 }
9000 }
9001 else
7865406b 9002 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9003 }
9004
65f38f15
AM
9005 /* We now have determined the sizes of the various dynamic sections.
9006 Allocate memory for them. */
b34976b6 9007 relocs = FALSE;
65f38f15
AM
9008 for (s = dynobj->sections; s != NULL; s = s->next)
9009 {
9010 if ((s->flags & SEC_LINKER_CREATED) == 0)
9011 continue;
9012
4ce794b7 9013 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9014 /* These haven't been allocated yet; don't strip. */
9015 continue;
e717da7e
AM
9016 else if (s == htab->got
9017 || s == htab->plt
e054468f 9018 || s == htab->iplt
c456f082
AM
9019 || s == htab->glink
9020 || s == htab->dynbss)
65f38f15
AM
9021 {
9022 /* Strip this section if we don't need it; see the
9023 comment below. */
5bd4f169 9024 }
70cc837d 9025 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9026 {
c456f082 9027 if (s->size != 0)
5bd4f169 9028 {
4ce794b7 9029 if (s != htab->relplt)
b34976b6 9030 relocs = TRUE;
5bd4f169
AM
9031
9032 /* We use the reloc_count field as a counter if we need
9033 to copy relocs into the output file. */
9034 s->reloc_count = 0;
9035 }
9036 }
65f38f15 9037 else
5bd4f169
AM
9038 {
9039 /* It's not one of our sections, so don't allocate space. */
9040 continue;
9041 }
9042
eea6121a 9043 if (s->size == 0)
5bd4f169 9044 {
c456f082
AM
9045 /* If we don't need this section, strip it from the
9046 output file. This is mostly to handle .rela.bss and
9047 .rela.plt. We must create both sections in
9048 create_dynamic_sections, because they must be created
9049 before the linker maps input sections to output
9050 sections. The linker does that before
9051 adjust_dynamic_symbol is called, and it is that
9052 function which decides whether anything needs to go
9053 into these sections. */
8423293d 9054 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9055 continue;
9056 }
9057
c456f082 9058 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9059 continue;
9060
65f38f15
AM
9061 /* Allocate memory for the section contents. We use bfd_zalloc
9062 here in case unused entries are not reclaimed before the
9063 section's contents are written out. This should not happen,
411e1bfb
AM
9064 but this way if it does we get a R_PPC64_NONE reloc in .rela
9065 sections instead of garbage.
9066 We also rely on the section contents being zero when writing
9067 the GOT. */
eea6121a 9068 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9069 if (s->contents == NULL)
b34976b6 9070 return FALSE;
5bd4f169
AM
9071 }
9072
e717da7e
AM
9073 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9074 {
0c8d6e5c 9075 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9076 continue;
9077
e717da7e
AM
9078 s = ppc64_elf_tdata (ibfd)->got;
9079 if (s != NULL && s != htab->got)
9080 {
eea6121a 9081 if (s->size == 0)
8423293d 9082 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9083 else
9084 {
eea6121a 9085 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9086 if (s->contents == NULL)
9087 return FALSE;
9088 }
9089 }
9090 s = ppc64_elf_tdata (ibfd)->relgot;
9091 if (s != NULL)
9092 {
eea6121a 9093 if (s->size == 0)
8423293d 9094 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9095 else
9096 {
eea6121a 9097 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9098 if (s->contents == NULL)
9099 return FALSE;
9100 relocs = TRUE;
9101 s->reloc_count = 0;
9102 }
9103 }
9104 }
9105
e86ce104 9106 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9107 {
9108 /* Add some entries to the .dynamic section. We fill in the
9109 values later, in ppc64_elf_finish_dynamic_sections, but we
9110 must add the entries now so that we get the correct size for
9111 the .dynamic section. The DT_DEBUG entry is filled in by the
9112 dynamic linker and used by the debugger. */
dc810e39 9113#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9114 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9115
36af4a4e 9116 if (info->executable)
5bd4f169 9117 {
dc810e39 9118 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9119 return FALSE;
5bd4f169
AM
9120 }
9121
eea6121a 9122 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9123 {
dc810e39
AM
9124 if (!add_dynamic_entry (DT_PLTGOT, 0)
9125 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9126 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9127 || !add_dynamic_entry (DT_JMPREL, 0)
9128 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9129 return FALSE;
5bd4f169
AM
9130 }
9131
19397422
AM
9132 if (NO_OPD_RELOCS)
9133 {
9134 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9135 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9136 return FALSE;
19397422
AM
9137 }
9138
a7f2871e
AM
9139 if (!htab->no_tls_get_addr_opt
9140 && htab->tls_get_addr_fd != NULL
9141 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9142 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9143 return FALSE;
9144
5bd4f169
AM
9145 if (relocs)
9146 {
dc810e39
AM
9147 if (!add_dynamic_entry (DT_RELA, 0)
9148 || !add_dynamic_entry (DT_RELASZ, 0)
9149 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9150 return FALSE;
5bd4f169 9151
65f38f15
AM
9152 /* If any dynamic relocs apply to a read-only section,
9153 then we need a DT_TEXTREL entry. */
248866a8 9154 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9155 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9156
65f38f15 9157 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9158 {
65f38f15 9159 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9160 return FALSE;
5bd4f169 9161 }
5bd4f169 9162 }
5bd4f169 9163 }
65f38f15 9164#undef add_dynamic_entry
5bd4f169 9165
b34976b6 9166 return TRUE;
5bd4f169
AM
9167}
9168
721956f4 9169/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9170
4ce794b7
AM
9171static inline enum ppc_stub_type
9172ppc_type_of_stub (asection *input_sec,
9173 const Elf_Internal_Rela *rel,
9174 struct ppc_link_hash_entry **hash,
e054468f 9175 struct plt_entry **plt_ent,
4ce794b7 9176 bfd_vma destination)
5bd4f169 9177{
721956f4
AM
9178 struct ppc_link_hash_entry *h = *hash;
9179 bfd_vma location;
9180 bfd_vma branch_offset;
9181 bfd_vma max_branch_offset;
4ce794b7 9182 enum elf_ppc64_reloc_type r_type;
5bd4f169 9183
721956f4
AM
9184 if (h != NULL)
9185 {
e054468f 9186 struct plt_entry *ent;
7fe2b9a6 9187 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9188 if (h->oh != NULL
9189 && h->oh->is_func_descriptor)
7b8f6675
AM
9190 {
9191 fdh = ppc_follow_link (h->oh);
9192 *hash = fdh;
9193 }
8387904d 9194
e054468f
AM
9195 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9196 if (ent->addend == rel->r_addend
9197 && ent->plt.offset != (bfd_vma) -1)
9198 {
e054468f
AM
9199 *plt_ent = ent;
9200 return ppc_stub_plt_call;
9201 }
5bd4f169 9202
7fe2b9a6
AM
9203 /* Here, we know we don't have a plt entry. If we don't have a
9204 either a defined function descriptor or a defined entry symbol
9205 in a regular object file, then it is pointless trying to make
9206 any other type of stub. */
854b41e7
AM
9207 if (!is_static_defined (&fdh->elf)
9208 && !is_static_defined (&h->elf))
721956f4 9209 return ppc_stub_none;
5d1634d7 9210 }
e054468f
AM
9211 else if (elf_local_got_ents (input_sec->owner) != NULL)
9212 {
9213 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9214 struct plt_entry **local_plt = (struct plt_entry **)
9215 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9216 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9217
9218 if (local_plt[r_symndx] != NULL)
9219 {
9220 struct plt_entry *ent;
9221
9222 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9223 if (ent->addend == rel->r_addend
9224 && ent->plt.offset != (bfd_vma) -1)
9225 {
9226 *plt_ent = ent;
9227 return ppc_stub_plt_call;
9228 }
9229 }
9230 }
5d1634d7 9231
721956f4
AM
9232 /* Determine where the call point is. */
9233 location = (input_sec->output_offset
9234 + input_sec->output_section->vma
9235 + rel->r_offset);
5d1634d7 9236
721956f4
AM
9237 branch_offset = destination - location;
9238 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9239
721956f4
AM
9240 /* Determine if a long branch stub is needed. */
9241 max_branch_offset = 1 << 25;
4ce794b7 9242 if (r_type != R_PPC64_REL24)
721956f4 9243 max_branch_offset = 1 << 15;
5d1634d7 9244
721956f4
AM
9245 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9246 /* We need a stub. Figure out whether a long_branch or plt_branch
9247 is needed later. */
9248 return ppc_stub_long_branch;
5d1634d7 9249
721956f4 9250 return ppc_stub_none;
5d1634d7
AM
9251}
9252
9253/* Build a .plt call stub. */
9254
4ce794b7 9255static inline bfd_byte *
176a0d42 9256build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
5d1634d7
AM
9257{
9258#define PPC_LO(v) ((v) & 0xffff)
9259#define PPC_HI(v) (((v) >> 16) & 0xffff)
9260#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9261
ac2df442
AM
9262 if (PPC_HA (offset) != 0)
9263 {
176a0d42
AM
9264 if (r != NULL)
9265 {
9266 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9267 r[1].r_offset = r[0].r_offset + 8;
9268 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9269 r[1].r_addend = r[0].r_addend;
9270 if (PPC_HA (offset + 16) != PPC_HA (offset))
9271 {
9272 r[2].r_offset = r[1].r_offset + 4;
9273 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9274 r[2].r_addend = r[0].r_addend;
9275 }
9276 else
9277 {
9278 r[2].r_offset = r[1].r_offset + 8;
9279 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9280 r[2].r_addend = r[0].r_addend + 8;
9281 r[3].r_offset = r[2].r_offset + 4;
9282 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9283 r[3].r_addend = r[0].r_addend + 16;
9284 }
9285 }
ac2df442
AM
9286 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9287 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9288 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9289 if (PPC_HA (offset + 16) != PPC_HA (offset))
9290 {
9291 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9292 offset = 0;
9293 }
9294 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9295 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9296 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9297 bfd_put_32 (obfd, BCTR, p), p += 4;
9298 }
9299 else
9300 {
176a0d42
AM
9301 if (r != NULL)
9302 {
9303 r[0].r_offset += 4;
9304 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9305 if (PPC_HA (offset + 16) != PPC_HA (offset))
9306 {
9307 r[1].r_offset = r[0].r_offset + 4;
9308 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9309 r[1].r_addend = r[0].r_addend;
9310 }
9311 else
9312 {
9313 r[1].r_offset = r[0].r_offset + 8;
9314 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9315 r[1].r_addend = r[0].r_addend + 16;
9316 r[2].r_offset = r[1].r_offset + 4;
9317 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9318 r[2].r_addend = r[0].r_addend + 8;
9319 }
9320 }
ac2df442
AM
9321 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9322 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9323 if (PPC_HA (offset + 16) != PPC_HA (offset))
9324 {
9325 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9326 offset = 0;
9327 }
9328 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9329 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9330 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9331 bfd_put_32 (obfd, BCTR, p), p += 4;
9332 }
5d1634d7
AM
9333 return p;
9334}
9335
a7f2871e
AM
9336/* Build a special .plt call stub for __tls_get_addr. */
9337
9338#define LD_R11_0R3 0xe9630000
9339#define LD_R12_0R3 0xe9830000
9340#define MR_R0_R3 0x7c601b78
9341#define CMPDI_R11_0 0x2c2b0000
9342#define ADD_R3_R12_R13 0x7c6c6a14
9343#define BEQLR 0x4d820020
9344#define MR_R3_R0 0x7c030378
9345#define MFLR_R11 0x7d6802a6
9346#define STD_R11_0R1 0xf9610000
9347#define BCTRL 0x4e800421
9348#define LD_R11_0R1 0xe9610000
9349#define LD_R2_0R1 0xe8410000
9350#define MTLR_R11 0x7d6803a6
9351
9352static inline bfd_byte *
9353build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9354 Elf_Internal_Rela *r)
9355{
9356 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9357 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9358 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9359 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9360 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9361 bfd_put_32 (obfd, BEQLR, p), p += 4;
9362 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9363 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9364 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9365
9366 if (r != NULL)
9367 r[0].r_offset += 9 * 4;
9368 p = build_plt_stub (obfd, p, offset, r);
9369 bfd_put_32 (obfd, BCTRL, p - 4);
9370
9371 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9372 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9373 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9374 bfd_put_32 (obfd, BLR, p), p += 4;
9375
9376 return p;
9377}
9378
176a0d42
AM
9379static Elf_Internal_Rela *
9380get_relocs (asection *sec, int count)
9381{
9382 Elf_Internal_Rela *relocs;
9383 struct bfd_elf_section_data *elfsec_data;
9384
9385 elfsec_data = elf_section_data (sec);
9386 relocs = elfsec_data->relocs;
9387 if (relocs == NULL)
9388 {
9389 bfd_size_type relsize;
9390 relsize = sec->reloc_count * sizeof (*relocs);
9391 relocs = bfd_alloc (sec->owner, relsize);
9392 if (relocs == NULL)
9393 return NULL;
9394 elfsec_data->relocs = relocs;
d4730f92
BS
9395 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9396 sizeof (Elf_Internal_Shdr));
9397 if (elfsec_data->rela.hdr == NULL)
9398 return NULL;
9399 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9400 * sizeof (Elf64_External_Rela));
9401 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9402 sec->reloc_count = 0;
9403 }
9404 relocs += sec->reloc_count;
9405 sec->reloc_count += count;
9406 return relocs;
9407}
9408
aa374f67 9409static bfd_vma
25f53a85 9410get_r2off (struct bfd_link_info *info,
aa374f67
AM
9411 struct ppc_stub_hash_entry *stub_entry)
9412{
25f53a85 9413 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
9414 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9415
9416 if (r2off == 0)
9417 {
9418 /* Support linking -R objects. Get the toc pointer from the
9419 opd entry. */
9420 char buf[8];
9421 asection *opd = stub_entry->h->elf.root.u.def.section;
9422 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9423
9424 if (strcmp (opd->name, ".opd") != 0
9425 || opd->reloc_count != 0)
9426 {
25f53a85
AM
9427 info->callbacks->einfo (_("cannot find opd entry toc for %s\n"),
9428 stub_entry->h->elf.root.root.string);
aa374f67
AM
9429 bfd_set_error (bfd_error_bad_value);
9430 return 0;
9431 }
9432 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9433 return 0;
9434 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 9435 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
9436 }
9437 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9438 return r2off;
9439}
9440
b34976b6 9441static bfd_boolean
4ce794b7 9442ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 9443{
721956f4
AM
9444 struct ppc_stub_hash_entry *stub_entry;
9445 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
9446 struct bfd_link_info *info;
9447 struct ppc_link_hash_table *htab;
721956f4
AM
9448 bfd_byte *loc;
9449 bfd_byte *p;
ee75fd95 9450 bfd_vma dest, off;
721956f4 9451 int size;
176a0d42 9452 Elf_Internal_Rela *r;
e054468f 9453 asection *plt;
5d1634d7 9454
721956f4
AM
9455 /* Massage our args to the form they really have. */
9456 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 9457 info = in_arg;
5d1634d7 9458
5d1634d7 9459 htab = ppc_hash_table (info);
4dfe6ac6
NC
9460 if (htab == NULL)
9461 return FALSE;
5d1634d7 9462
721956f4 9463 /* Make a note of the offset within the stubs for this entry. */
eea6121a 9464 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 9465 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 9466
4ce794b7 9467 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 9468 switch (stub_entry->stub_type)
5d1634d7 9469 {
721956f4 9470 case ppc_stub_long_branch:
ad8e1ba5 9471 case ppc_stub_long_branch_r2off:
721956f4 9472 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
9473 off = dest = (stub_entry->target_value
9474 + stub_entry->target_section->output_offset
9475 + stub_entry->target_section->output_section->vma);
5d1634d7 9476
721956f4
AM
9477 /* And this is where we are coming from. */
9478 off -= (stub_entry->stub_offset
97b639ba
AM
9479 + stub_entry->stub_sec->output_offset
9480 + stub_entry->stub_sec->output_section->vma);
e86ce104 9481
ac2df442
AM
9482 size = 4;
9483 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 9484 {
25f53a85 9485 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 9486
aa374f67
AM
9487 if (r2off == 0)
9488 {
9489 htab->stub_error = TRUE;
9490 return FALSE;
9491 }
97b639ba 9492 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9493 loc += 4;
ac2df442
AM
9494 size = 12;
9495 if (PPC_HA (r2off) != 0)
9496 {
9497 size = 16;
9498 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9499 loc += 4;
9500 }
97b639ba 9501 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 9502 loc += 4;
ac2df442 9503 off -= size - 4;
ad8e1ba5 9504 }
97b639ba 9505 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 9506
5c3dead3
AM
9507 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9508 {
25f53a85
AM
9509 info->callbacks->einfo (_("long branch stub `%s' offset overflow\n"),
9510 stub_entry->root.string);
5c3dead3
AM
9511 htab->stub_error = TRUE;
9512 return FALSE;
9513 }
ee75fd95
AM
9514
9515 if (info->emitrelocations)
9516 {
176a0d42
AM
9517 r = get_relocs (stub_entry->stub_sec, 1);
9518 if (r == NULL)
9519 return FALSE;
ee75fd95
AM
9520 r->r_offset = loc - stub_entry->stub_sec->contents;
9521 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9522 r->r_addend = dest;
9523 if (stub_entry->h != NULL)
9524 {
9525 struct elf_link_hash_entry **hashes;
9526 unsigned long symndx;
9527 struct ppc_link_hash_entry *h;
9528
9529 hashes = elf_sym_hashes (htab->stub_bfd);
9530 if (hashes == NULL)
9531 {
9532 bfd_size_type hsize;
9533
9534 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9535 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9536 if (hashes == NULL)
9537 return FALSE;
9538 elf_sym_hashes (htab->stub_bfd) = hashes;
9539 htab->stub_globals = 1;
9540 }
9541 symndx = htab->stub_globals++;
9542 h = stub_entry->h;
9543 hashes[symndx] = &h->elf;
9544 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9545 if (h->oh != NULL && h->oh->is_func)
b31867b6 9546 h = ppc_follow_link (h->oh);
ee75fd95
AM
9547 if (h->elf.root.u.def.section != stub_entry->target_section)
9548 /* H is an opd symbol. The addend must be zero. */
9549 r->r_addend = 0;
9550 else
9551 {
9552 off = (h->elf.root.u.def.value
9553 + h->elf.root.u.def.section->output_offset
9554 + h->elf.root.u.def.section->output_section->vma);
9555 r->r_addend -= off;
9556 }
9557 }
9558 }
721956f4 9559 break;
e86ce104 9560
721956f4 9561 case ppc_stub_plt_branch:
ad8e1ba5 9562 case ppc_stub_plt_branch_r2off:
721956f4
AM
9563 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9564 stub_entry->root.string + 9,
b34976b6 9565 FALSE, FALSE);
721956f4
AM
9566 if (br_entry == NULL)
9567 {
25f53a85
AM
9568 info->callbacks->einfo (_("can't find branch stub `%s'\n"),
9569 stub_entry->root.string);
b34976b6
AM
9570 htab->stub_error = TRUE;
9571 return FALSE;
721956f4
AM
9572 }
9573
176a0d42
AM
9574 dest = (stub_entry->target_value
9575 + stub_entry->target_section->output_offset
9576 + stub_entry->target_section->output_section->vma);
721956f4 9577
176a0d42 9578 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 9579 htab->brlt->contents + br_entry->offset);
721956f4 9580
f94498ff 9581 if (br_entry->iter == htab->stub_iteration)
721956f4 9582 {
f94498ff 9583 br_entry->iter = 0;
84f5d08e 9584
f94498ff 9585 if (htab->relbrlt != NULL)
84f5d08e 9586 {
f94498ff
AM
9587 /* Create a reloc for the branch lookup table entry. */
9588 Elf_Internal_Rela rela;
9589 bfd_byte *rl;
9590
9591 rela.r_offset = (br_entry->offset
9592 + htab->brlt->output_offset
9593 + htab->brlt->output_section->vma);
9594 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9595 rela.r_addend = dest;
f94498ff
AM
9596
9597 rl = htab->relbrlt->contents;
9598 rl += (htab->relbrlt->reloc_count++
9599 * sizeof (Elf64_External_Rela));
9600 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9601 }
9602 else if (info->emitrelocations)
9603 {
176a0d42
AM
9604 r = get_relocs (htab->brlt, 1);
9605 if (r == NULL)
9606 return FALSE;
9607 /* brlt, being SEC_LINKER_CREATED does not go through the
9608 normal reloc processing. Symbols and offsets are not
9609 translated from input file to output file form, so
9610 set up the offset per the output file. */
f94498ff
AM
9611 r->r_offset = (br_entry->offset
9612 + htab->brlt->output_offset
9613 + htab->brlt->output_section->vma);
9614 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9615 r->r_addend = dest;
84f5d08e 9616 }
84f5d08e 9617 }
721956f4 9618
176a0d42
AM
9619 dest = (br_entry->offset
9620 + htab->brlt->output_offset
9621 + htab->brlt->output_section->vma);
9622
9623 off = (dest
4ce794b7 9624 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 9625 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9626
ad8e1ba5 9627 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 9628 {
25f53a85
AM
9629 info->callbacks->einfo
9630 (_("linkage table error against `%s'\n"),
721956f4 9631 stub_entry->root.string);
5d1634d7 9632 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9633 htab->stub_error = TRUE;
9634 return FALSE;
5d1634d7 9635 }
41bd81ab 9636
176a0d42
AM
9637 if (info->emitrelocations)
9638 {
9639 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9640 if (r == NULL)
9641 return FALSE;
9642 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9643 if (bfd_big_endian (info->output_bfd))
9644 r[0].r_offset += 2;
176a0d42
AM
9645 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9646 r[0].r_offset += 4;
9647 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9648 r[0].r_addend = dest;
9649 if (PPC_HA (off) != 0)
9650 {
9651 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9652 r[1].r_offset = r[0].r_offset + 4;
9653 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9654 r[1].r_addend = r[0].r_addend;
9655 }
9656 }
9657
ad8e1ba5
AM
9658 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9659 {
176a0d42 9660 if (PPC_HA (off) != 0)
ac2df442
AM
9661 {
9662 size = 16;
176a0d42 9663 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9664 loc += 4;
176a0d42 9665 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9666 }
9667 else
9668 {
9669 size = 12;
176a0d42 9670 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442 9671 }
ad8e1ba5
AM
9672 }
9673 else
9674 {
25f53a85 9675 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
9676
9677 if (r2off == 0)
9678 {
9679 htab->stub_error = TRUE;
9680 return FALSE;
9681 }
ad8e1ba5 9682
97b639ba 9683 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9684 loc += 4;
ac2df442 9685 size = 20;
176a0d42 9686 if (PPC_HA (off) != 0)
ac2df442
AM
9687 {
9688 size += 4;
176a0d42 9689 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9690 loc += 4;
176a0d42 9691 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9692 loc += 4;
9693 }
9694 else
9695 {
176a0d42 9696 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442
AM
9697 loc += 4;
9698 }
9699
9700 if (PPC_HA (r2off) != 0)
9701 {
9702 size += 4;
9703 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9704 loc += 4;
9705 }
97b639ba 9706 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
9707 }
9708 loc += 4;
97b639ba 9709 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 9710 loc += 4;
97b639ba 9711 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 9712 break;
5d1634d7 9713
721956f4 9714 case ppc_stub_plt_call:
e054468f 9715 if (stub_entry->h != NULL
b31867b6
AM
9716 && stub_entry->h->is_func_descriptor
9717 && stub_entry->h->oh != NULL)
c862ae31 9718 {
b31867b6
AM
9719 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9720
9721 /* If the old-ABI "dot-symbol" is undefined make it weak so
9722 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9723 FIXME: We used to define the symbol on one of the call
9724 stubs instead, which is why we test symbol section id
9725 against htab->top_id in various places. Likely all
9726 these checks could now disappear. */
9727 if (fh->elf.root.type == bfd_link_hash_undefined)
9728 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
9729 /* Stop undo_symbol_twiddle changing it back to undefined. */
9730 fh->was_undefined = 0;
c862ae31
AM
9731 }
9732
721956f4 9733 /* Now build the stub. */
e054468f 9734 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 9735 if (dest >= (bfd_vma) -2)
721956f4
AM
9736 abort ();
9737
e054468f 9738 plt = htab->plt;
25f23106
AM
9739 if (!htab->elf.dynamic_sections_created
9740 || stub_entry->h == NULL
9741 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9742 plt = htab->iplt;
9743
9744 dest += plt->output_offset + plt->output_section->vma;
9745
9746 if (stub_entry->h == NULL
9747 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9748 {
9749 Elf_Internal_Rela rela;
9750 bfd_byte *rl;
9751
9752 rela.r_offset = dest;
25f23106 9753 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
9754 rela.r_addend = (stub_entry->target_value
9755 + stub_entry->target_section->output_offset
9756 + stub_entry->target_section->output_section->vma);
9757
25f23106
AM
9758 rl = (htab->reliplt->contents
9759 + (htab->reliplt->reloc_count++
9760 * sizeof (Elf64_External_Rela)));
9761 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
9762 stub_entry->plt_ent->plt.offset |= 1;
9763 }
176a0d42
AM
9764
9765 off = (dest
e054468f 9766 - elf_gp (plt->output_section->owner)
176a0d42 9767 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9768
ad8e1ba5 9769 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 9770 {
25f53a85
AM
9771 info->callbacks->einfo
9772 (_("linkage table error against `%s'\n"),
e054468f
AM
9773 stub_entry->h != NULL
9774 ? stub_entry->h->elf.root.root.string
9775 : "<local sym>");
721956f4 9776 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9777 htab->stub_error = TRUE;
9778 return FALSE;
721956f4
AM
9779 }
9780
176a0d42
AM
9781 r = NULL;
9782 if (info->emitrelocations)
9783 {
9784 r = get_relocs (stub_entry->stub_sec,
9785 (2 + (PPC_HA (off) != 0)
9786 + (PPC_HA (off + 16) == PPC_HA (off))));
9787 if (r == NULL)
9788 return FALSE;
9789 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9790 if (bfd_big_endian (info->output_bfd))
9791 r[0].r_offset += 2;
176a0d42
AM
9792 r[0].r_addend = dest;
9793 }
a7f2871e
AM
9794 if (stub_entry->h != NULL
9795 && (stub_entry->h == htab->tls_get_addr_fd
9796 || stub_entry->h == htab->tls_get_addr)
9797 && !htab->no_tls_get_addr_opt)
9798 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
9799 else
9800 p = build_plt_stub (htab->stub_bfd, loc, off, r);
721956f4
AM
9801 size = p - loc;
9802 break;
9803
9804 default:
9805 BFD_FAIL ();
b34976b6 9806 return FALSE;
721956f4
AM
9807 }
9808
eea6121a 9809 stub_entry->stub_sec->size += size;
97b639ba 9810
ee75fd95 9811 if (htab->emit_stub_syms)
97b639ba
AM
9812 {
9813 struct elf_link_hash_entry *h;
ee75fd95
AM
9814 size_t len1, len2;
9815 char *name;
9816 const char *const stub_str[] = { "long_branch",
9817 "long_branch_r2off",
9818 "plt_branch",
9819 "plt_branch_r2off",
9820 "plt_call" };
9821
9822 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9823 len2 = strlen (stub_entry->root.string);
9824 name = bfd_malloc (len1 + len2 + 2);
9825 if (name == NULL)
9826 return FALSE;
9827 memcpy (name, stub_entry->root.string, 9);
9828 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9829 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9830 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
9831 if (h == NULL)
9832 return FALSE;
9833 if (h->root.type == bfd_link_hash_new)
9834 {
9835 h->root.type = bfd_link_hash_defined;
9836 h->root.u.def.section = stub_entry->stub_sec;
9837 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
9838 h->ref_regular = 1;
9839 h->def_regular = 1;
9840 h->ref_regular_nonweak = 1;
9841 h->forced_local = 1;
9842 h->non_elf = 0;
97b639ba
AM
9843 }
9844 }
9845
b34976b6 9846 return TRUE;
721956f4
AM
9847}
9848
9849/* As above, but don't actually build the stub. Just bump offset so
9850 we know stub section sizes, and select plt_branch stubs where
9851 long_branch stubs won't do. */
9852
b34976b6 9853static bfd_boolean
4ce794b7 9854ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
9855{
9856 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 9857 struct bfd_link_info *info;
721956f4
AM
9858 struct ppc_link_hash_table *htab;
9859 bfd_vma off;
9860 int size;
9861
9862 /* Massage our args to the form they really have. */
9863 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
9864 info = in_arg;
9865
9866 htab = ppc_hash_table (info);
4dfe6ac6
NC
9867 if (htab == NULL)
9868 return FALSE;
721956f4
AM
9869
9870 if (stub_entry->stub_type == ppc_stub_plt_call)
9871 {
e054468f
AM
9872 asection *plt;
9873 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 9874 if (off >= (bfd_vma) -2)
411e1bfb 9875 abort ();
e054468f 9876 plt = htab->plt;
25f23106
AM
9877 if (!htab->elf.dynamic_sections_created
9878 || stub_entry->h == NULL
9879 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9880 plt = htab->iplt;
9881 off += (plt->output_offset
9882 + plt->output_section->vma
9883 - elf_gp (plt->output_section->owner)
ad8e1ba5 9884 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9885
ad8e1ba5 9886 size = PLT_CALL_STUB_SIZE;
ac2df442
AM
9887 if (PPC_HA (off) == 0)
9888 size -= 4;
4ce794b7 9889 if (PPC_HA (off + 16) != PPC_HA (off))
721956f4 9890 size += 4;
a7f2871e
AM
9891 if (stub_entry->h != NULL
9892 && (stub_entry->h == htab->tls_get_addr_fd
9893 || stub_entry->h == htab->tls_get_addr)
9894 && !htab->no_tls_get_addr_opt)
9895 size += 13 * 4;
176a0d42
AM
9896 if (info->emitrelocations)
9897 {
9898 stub_entry->stub_sec->reloc_count
9899 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9900 stub_entry->stub_sec->flags |= SEC_RELOC;
9901 }
721956f4
AM
9902 }
9903 else
9904 {
ad8e1ba5
AM
9905 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9906 variants. */
ac2df442
AM
9907 bfd_vma r2off = 0;
9908
721956f4
AM
9909 off = (stub_entry->target_value
9910 + stub_entry->target_section->output_offset
9911 + stub_entry->target_section->output_section->vma);
eea6121a 9912 off -= (stub_entry->stub_sec->size
721956f4
AM
9913 + stub_entry->stub_sec->output_offset
9914 + stub_entry->stub_sec->output_section->vma);
9915
ad8e1ba5
AM
9916 /* Reset the stub type from the plt variant in case we now
9917 can reach with a shorter stub. */
9918 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9919 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9920
9921 size = 4;
9922 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9923 {
25f53a85 9924 r2off = get_r2off (info, stub_entry);
aa374f67
AM
9925 if (r2off == 0)
9926 {
9927 htab->stub_error = TRUE;
9928 return FALSE;
9929 }
ac2df442
AM
9930 size = 12;
9931 if (PPC_HA (r2off) != 0)
9932 size = 16;
9933 off -= size - 4;
ad8e1ba5
AM
9934 }
9935
9936 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
9937 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9938 {
9939 struct ppc_branch_hash_entry *br_entry;
9940
9941 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9942 stub_entry->root.string + 9,
b34976b6 9943 TRUE, FALSE);
721956f4
AM
9944 if (br_entry == NULL)
9945 {
25f53a85
AM
9946 info->callbacks->einfo (_("can't build branch stub `%s'\n"),
9947 stub_entry->root.string);
b34976b6
AM
9948 htab->stub_error = TRUE;
9949 return FALSE;
721956f4
AM
9950 }
9951
9952 if (br_entry->iter != htab->stub_iteration)
9953 {
9954 br_entry->iter = htab->stub_iteration;
eea6121a
AM
9955 br_entry->offset = htab->brlt->size;
9956 htab->brlt->size += 8;
63bc6f6c 9957
ee75fd95 9958 if (htab->relbrlt != NULL)
eea6121a 9959 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
9960 else if (info->emitrelocations)
9961 {
9962 htab->brlt->reloc_count += 1;
9963 htab->brlt->flags |= SEC_RELOC;
9964 }
721956f4 9965 }
ad8e1ba5
AM
9966
9967 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
9968 off = (br_entry->offset
9969 + htab->brlt->output_offset
9970 + htab->brlt->output_section->vma
9971 - elf_gp (htab->brlt->output_section->owner)
9972 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9973
176a0d42
AM
9974 if (info->emitrelocations)
9975 {
9976 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
9977 stub_entry->stub_sec->flags |= SEC_RELOC;
9978 }
9979
ac2df442
AM
9980 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9981 {
9982 size = 12;
176a0d42 9983 if (PPC_HA (off) != 0)
ac2df442
AM
9984 size = 16;
9985 }
9986 else
9987 {
9988 size = 20;
176a0d42 9989 if (PPC_HA (off) != 0)
ac2df442
AM
9990 size += 4;
9991
9992 if (PPC_HA (r2off) != 0)
9993 size += 4;
9994 }
721956f4 9995 }
84f5d08e
AM
9996 else if (info->emitrelocations)
9997 {
9998 stub_entry->stub_sec->reloc_count += 1;
9999 stub_entry->stub_sec->flags |= SEC_RELOC;
10000 }
721956f4
AM
10001 }
10002
eea6121a 10003 stub_entry->stub_sec->size += size;
b34976b6 10004 return TRUE;
721956f4
AM
10005}
10006
10007/* Set up various things so that we can make a list of input sections
10008 for each output section included in the link. Returns -1 on error,
cedb70c5 10009 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10010
10011int
927be08e
AM
10012ppc64_elf_setup_section_lists
10013 (struct bfd_link_info *info,
10014 asection *(*add_stub_section) (const char *, asection *),
10015 void (*layout_sections_again) (void))
721956f4
AM
10016{
10017 bfd *input_bfd;
734b6cf9 10018 int top_id, top_index, id;
721956f4 10019 asection *section;
734b6cf9 10020 asection **input_list;
721956f4
AM
10021 bfd_size_type amt;
10022 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10023
4dfe6ac6
NC
10024 if (htab == NULL)
10025 return -1;
927be08e
AM
10026 /* Stash our params away. */
10027 htab->add_stub_section = add_stub_section;
10028 htab->layout_sections_again = layout_sections_again;
4c52953f 10029
4ce794b7 10030 if (htab->brlt == NULL)
721956f4
AM
10031 return 0;
10032
1e2f5b6e 10033 /* Find the top input section id. */
3d6f9012 10034 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10035 input_bfd != NULL;
10036 input_bfd = input_bfd->link_next)
10037 {
721956f4
AM
10038 for (section = input_bfd->sections;
10039 section != NULL;
10040 section = section->next)
10041 {
10042 if (top_id < section->id)
10043 top_id = section->id;
10044 }
10045 }
721956f4 10046
8f3bab57 10047 htab->top_id = top_id;
721956f4 10048 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10049 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10050 if (htab->stub_group == NULL)
10051 return -1;
10052
3d6f9012
AM
10053 /* Set toc_off for com, und, abs and ind sections. */
10054 for (id = 0; id < 3; id++)
10055 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10056
734b6cf9
AM
10057 /* We can't use output_bfd->section_count here to find the top output
10058 section index as some sections may have been removed, and
8423293d 10059 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10060 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10061 section != NULL;
10062 section = section->next)
10063 {
10064 if (top_index < section->index)
10065 top_index = section->index;
10066 }
10067
10068 htab->top_index = top_index;
10069 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10070 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10071 htab->input_list = input_list;
10072 if (input_list == NULL)
10073 return -1;
10074
721956f4
AM
10075 return 1;
10076}
10077
927be08e
AM
10078/* Set up for first pass at multitoc partitioning. */
10079
10080void
10081ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10082{
10083 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10084
10085 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10086 htab->toc_curr = elf_gp (info->output_bfd);
10087 htab->toc_bfd = NULL;
10088 htab->toc_first_sec = NULL;
10089}
10090
e717da7e
AM
10091/* The linker repeatedly calls this function for each TOC input section
10092 and linker generated GOT section. Group input bfds such that the toc
927be08e 10093 within a group is less than 64k in size. */
ad8e1ba5 10094
927be08e 10095bfd_boolean
4ce794b7 10096ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10097{
10098 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10099 bfd_vma addr, off, limit;
ad8e1ba5 10100
4dfe6ac6
NC
10101 if (htab == NULL)
10102 return FALSE;
10103
927be08e 10104 if (!htab->second_toc_pass)
4c52953f 10105 {
927be08e 10106 /* Keep track of the first .toc or .got section for this input bfd. */
bf102f86
AM
10107 if (htab->toc_bfd != isec->owner)
10108 {
10109 htab->toc_bfd = isec->owner;
10110 htab->toc_first_sec = isec;
10111 }
927be08e 10112
bf102f86
AM
10113 addr = isec->output_offset + isec->output_section->vma;
10114 off = addr - htab->toc_curr;
d77c8a4b
AM
10115 limit = 0x80008000;
10116 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10117 limit = 0x10000;
10118 if (off + isec->size > limit)
bf102f86
AM
10119 {
10120 addr = (htab->toc_first_sec->output_offset
10121 + htab->toc_first_sec->output_section->vma);
10122 htab->toc_curr = addr;
10123 }
99877b66 10124
927be08e
AM
10125 /* toc_curr is the base address of this toc group. Set elf_gp
10126 for the input section to be the offset relative to the
10127 output toc base plus 0x8000. Making the input elf_gp an
10128 offset allows us to move the toc as a whole without
10129 recalculating input elf_gp. */
10130 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10131 off += TOC_BASE_OFF;
10132
10133 /* Die if someone uses a linker script that doesn't keep input
10134 file .toc and .got together. */
10135 if (elf_gp (isec->owner) != 0
10136 && elf_gp (isec->owner) != off)
10137 return FALSE;
10138
10139 elf_gp (isec->owner) = off;
10140 return TRUE;
4c52953f 10141 }
927be08e
AM
10142
10143 /* During the second pass toc_first_sec points to the start of
10144 a toc group, and toc_curr is used to track the old elf_gp.
10145 We use toc_bfd to ensure we only look at each bfd once. */
10146 if (htab->toc_bfd == isec->owner)
10147 return TRUE;
10148 htab->toc_bfd = isec->owner;
10149
10150 if (htab->toc_first_sec == NULL
10151 || htab->toc_curr != elf_gp (isec->owner))
10152 {
10153 htab->toc_curr = elf_gp (isec->owner);
10154 htab->toc_first_sec = isec;
10155 }
10156 addr = (htab->toc_first_sec->output_offset
10157 + htab->toc_first_sec->output_section->vma);
10158 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10159 elf_gp (isec->owner) = off;
10160
10161 return TRUE;
ad8e1ba5
AM
10162}
10163
927be08e
AM
10164/* Called via elf_link_hash_traverse to merge GOT entries for global
10165 symbol H. */
10166
10167static bfd_boolean
10168merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10169{
10170 if (h->root.type == bfd_link_hash_indirect)
10171 return TRUE;
10172
927be08e
AM
10173 merge_got_entries (&h->got.glist);
10174
10175 return TRUE;
10176}
10177
10178/* Called via elf_link_hash_traverse to allocate GOT entries for global
10179 symbol H. */
10180
10181static bfd_boolean
10182reallocate_got (struct elf_link_hash_entry *h, void *inf)
10183{
10184 struct got_entry *gent;
10185
10186 if (h->root.type == bfd_link_hash_indirect)
10187 return TRUE;
10188
927be08e
AM
10189 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10190 if (!gent->is_indirect)
10191 allocate_got (h, (struct bfd_link_info *) inf, gent);
10192 return TRUE;
10193}
10194
10195/* Called on the first multitoc pass after the last call to
10196 ppc64_elf_next_toc_section. This function removes duplicate GOT
10197 entries. */
10198
10199bfd_boolean
10200ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10201{
10202 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10203 struct bfd *ibfd, *ibfd2;
10204 bfd_boolean done_something;
10205
10206 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10207
7865406b
AM
10208 if (!htab->do_multi_toc)
10209 return FALSE;
10210
d0fae19d 10211 /* Merge global sym got entries within a toc group. */
927be08e
AM
10212 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10213
10214 /* And tlsld_got. */
10215 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10216 {
10217 struct got_entry *ent, *ent2;
10218
10219 if (!is_ppc64_elf (ibfd))
10220 continue;
10221
10222 ent = ppc64_tlsld_got (ibfd);
10223 if (!ent->is_indirect
10224 && ent->got.offset != (bfd_vma) -1)
10225 {
10226 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10227 {
10228 if (!is_ppc64_elf (ibfd2))
10229 continue;
10230
10231 ent2 = ppc64_tlsld_got (ibfd2);
10232 if (!ent2->is_indirect
10233 && ent2->got.offset != (bfd_vma) -1
10234 && elf_gp (ibfd2) == elf_gp (ibfd))
10235 {
10236 ent2->is_indirect = TRUE;
10237 ent2->got.ent = ent;
10238 }
10239 }
10240 }
10241 }
10242
10243 /* Zap sizes of got sections. */
10244 htab->reliplt->rawsize = htab->reliplt->size;
10245 htab->reliplt->size -= htab->got_reli_size;
10246 htab->got_reli_size = 0;
10247
10248 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10249 {
10250 asection *got, *relgot;
10251
10252 if (!is_ppc64_elf (ibfd))
10253 continue;
10254
10255 got = ppc64_elf_tdata (ibfd)->got;
10256 if (got != NULL)
10257 {
10258 got->rawsize = got->size;
10259 got->size = 0;
10260 relgot = ppc64_elf_tdata (ibfd)->relgot;
10261 relgot->rawsize = relgot->size;
10262 relgot->size = 0;
10263 }
10264 }
10265
10266 /* Now reallocate the got, local syms first. We don't need to
10267 allocate section contents again since we never increase size. */
10268 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10269 {
10270 struct got_entry **lgot_ents;
10271 struct got_entry **end_lgot_ents;
10272 struct plt_entry **local_plt;
10273 struct plt_entry **end_local_plt;
f961d9dd 10274 unsigned char *lgot_masks;
927be08e
AM
10275 bfd_size_type locsymcount;
10276 Elf_Internal_Shdr *symtab_hdr;
10277 asection *s, *srel;
10278
10279 if (!is_ppc64_elf (ibfd))
10280 continue;
10281
10282 lgot_ents = elf_local_got_ents (ibfd);
10283 if (!lgot_ents)
10284 continue;
10285
10286 symtab_hdr = &elf_symtab_hdr (ibfd);
10287 locsymcount = symtab_hdr->sh_info;
10288 end_lgot_ents = lgot_ents + locsymcount;
10289 local_plt = (struct plt_entry **) end_lgot_ents;
10290 end_local_plt = local_plt + locsymcount;
f961d9dd 10291 lgot_masks = (unsigned char *) end_local_plt;
927be08e
AM
10292 s = ppc64_elf_tdata (ibfd)->got;
10293 srel = ppc64_elf_tdata (ibfd)->relgot;
10294 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10295 {
10296 struct got_entry *ent;
10297
10298 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d
AM
10299 {
10300 unsigned int num = 1;
10301 ent->got.offset = s->size;
10302 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10303 num = 2;
10304 s->size += num * 8;
10305 if (info->shared)
10306 srel->size += num * sizeof (Elf64_External_Rela);
10307 else if ((*lgot_masks & PLT_IFUNC) != 0)
10308 {
10309 htab->reliplt->size
10310 += num * sizeof (Elf64_External_Rela);
10311 htab->got_reli_size
10312 += num * sizeof (Elf64_External_Rela);
10313 }
10314 }
927be08e
AM
10315 }
10316 }
10317
10318 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10319
10320 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10321 {
10322 struct got_entry *ent;
10323
10324 if (!is_ppc64_elf (ibfd))
10325 continue;
10326
10327 ent = ppc64_tlsld_got (ibfd);
10328 if (!ent->is_indirect
10329 && ent->got.offset != (bfd_vma) -1)
10330 {
10331 asection *s = ppc64_elf_tdata (ibfd)->got;
10332 ent->got.offset = s->size;
10333 s->size += 16;
10334 if (info->shared)
10335 {
10336 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10337 srel->size += sizeof (Elf64_External_Rela);
10338 }
10339 }
10340 }
10341
10342 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10343 if (!done_something)
10344 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10345 {
10346 asection *got;
10347
10348 if (!is_ppc64_elf (ibfd))
10349 continue;
10350
10351 got = ppc64_elf_tdata (ibfd)->got;
10352 if (got != NULL)
10353 {
10354 done_something = got->rawsize != got->size;
10355 if (done_something)
10356 break;
10357 }
10358 }
10359
10360 if (done_something)
10361 (*htab->layout_sections_again) ();
10362
10363 /* Set up for second pass over toc sections to recalculate elf_gp
10364 on input sections. */
10365 htab->toc_bfd = NULL;
10366 htab->toc_first_sec = NULL;
10367 htab->second_toc_pass = TRUE;
10368 return done_something;
10369}
10370
10371/* Called after second pass of multitoc partitioning. */
10372
10373void
10374ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10375{
10376 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10377
10378 /* After the second pass, toc_curr tracks the TOC offset used
10379 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 10380 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
10381}
10382
9b5ecbd0
AM
10383/* No toc references were found in ISEC. If the code in ISEC makes no
10384 calls, then there's no need to use toc adjusting stubs when branching
10385 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
10386 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10387 needed, and 2 if a cyclical call-graph was found but no other reason
10388 for a stub was detected. If called from the top level, a return of
10389 2 means the same as a return of 0. */
9b5ecbd0
AM
10390
10391static int
4ce794b7 10392toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 10393{
9b5ecbd0 10394 int ret;
70cc837d
AM
10395
10396 /* Mark this section as checked. */
10397 isec->call_check_done = 1;
9b5ecbd0 10398
772119ce
AM
10399 /* We know none of our code bearing sections will need toc stubs. */
10400 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10401 return 0;
10402
eea6121a 10403 if (isec->size == 0)
082c50f8
AM
10404 return 0;
10405
4c52953f
AM
10406 if (isec->output_section == NULL)
10407 return 0;
10408
4c52953f 10409 ret = 0;
70cc837d 10410 if (isec->reloc_count != 0)
9b5ecbd0 10411 {
70cc837d
AM
10412 Elf_Internal_Rela *relstart, *rel;
10413 Elf_Internal_Sym *local_syms;
10414 struct ppc_link_hash_table *htab;
2917689a 10415
70cc837d
AM
10416 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10417 info->keep_memory);
10418 if (relstart == NULL)
10419 return -1;
90aecf7a 10420
70cc837d
AM
10421 /* Look for branches to outside of this section. */
10422 local_syms = NULL;
10423 htab = ppc_hash_table (info);
10424 if (htab == NULL)
10425 return -1;
4c52953f 10426
70cc837d 10427 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 10428 {
70cc837d
AM
10429 enum elf_ppc64_reloc_type r_type;
10430 unsigned long r_symndx;
10431 struct elf_link_hash_entry *h;
10432 struct ppc_link_hash_entry *eh;
10433 Elf_Internal_Sym *sym;
10434 asection *sym_sec;
10435 struct _opd_sec_data *opd;
10436 bfd_vma sym_value;
10437 bfd_vma dest;
10438
10439 r_type = ELF64_R_TYPE (rel->r_info);
10440 if (r_type != R_PPC64_REL24
10441 && r_type != R_PPC64_REL14
10442 && r_type != R_PPC64_REL14_BRTAKEN
10443 && r_type != R_PPC64_REL14_BRNTAKEN)
10444 continue;
4c52953f 10445
70cc837d
AM
10446 r_symndx = ELF64_R_SYM (rel->r_info);
10447 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10448 isec->owner))
4c52953f 10449 {
70cc837d
AM
10450 ret = -1;
10451 break;
10452 }
4c52953f 10453
70cc837d
AM
10454 /* Calls to dynamic lib functions go through a plt call stub
10455 that uses r2. */
10456 eh = (struct ppc_link_hash_entry *) h;
10457 if (eh != NULL
10458 && (eh->elf.plt.plist != NULL
10459 || (eh->oh != NULL
10460 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10461 {
10462 ret = 1;
10463 break;
4c52953f
AM
10464 }
10465
70cc837d
AM
10466 if (sym_sec == NULL)
10467 /* Ignore other undefined symbols. */
4c52953f 10468 continue;
4c52953f 10469
70cc837d
AM
10470 /* Assume branches to other sections not included in the
10471 link need stubs too, to cover -R and absolute syms. */
10472 if (sym_sec->output_section == NULL)
10473 {
10474 ret = 1;
10475 break;
10476 }
4c52953f 10477
70cc837d
AM
10478 if (h == NULL)
10479 sym_value = sym->st_value;
10480 else
10481 {
10482 if (h->root.type != bfd_link_hash_defined
10483 && h->root.type != bfd_link_hash_defweak)
10484 abort ();
10485 sym_value = h->root.u.def.value;
10486 }
10487 sym_value += rel->r_addend;
4c52953f 10488
70cc837d
AM
10489 /* If this branch reloc uses an opd sym, find the code section. */
10490 opd = get_opd_info (sym_sec);
10491 if (opd != NULL)
10492 {
10493 if (h == NULL && opd->adjust != NULL)
10494 {
10495 long adjust;
4c52953f 10496
70cc837d
AM
10497 adjust = opd->adjust[sym->st_value / 8];
10498 if (adjust == -1)
10499 /* Assume deleted functions won't ever be called. */
10500 continue;
10501 sym_value += adjust;
10502 }
4c52953f 10503
70cc837d
AM
10504 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10505 if (dest == (bfd_vma) -1)
10506 continue;
10507 }
10508 else
10509 dest = (sym_value
10510 + sym_sec->output_offset
10511 + sym_sec->output_section->vma);
4c52953f 10512
70cc837d
AM
10513 /* Ignore branch to self. */
10514 if (sym_sec == isec)
10515 continue;
4c52953f 10516
70cc837d
AM
10517 /* If the called function uses the toc, we need a stub. */
10518 if (sym_sec->has_toc_reloc
10519 || sym_sec->makes_toc_func_call)
4c52953f 10520 {
70cc837d 10521 ret = 1;
4c52953f
AM
10522 break;
10523 }
70cc837d
AM
10524
10525 /* Assume any branch that needs a long branch stub might in fact
10526 need a plt_branch stub. A plt_branch stub uses r2. */
10527 else if (dest - (isec->output_offset
10528 + isec->output_section->vma
10529 + rel->r_offset) + (1 << 25) >= (2 << 25))
4c52953f 10530 {
70cc837d
AM
10531 ret = 1;
10532 break;
10533 }
10534
10535 /* If calling back to a section in the process of being
10536 tested, we can't say for sure that no toc adjusting stubs
10537 are needed, so don't return zero. */
10538 else if (sym_sec->call_check_in_progress)
10539 ret = 2;
10540
10541 /* Branches to another section that itself doesn't have any TOC
10542 references are OK. Recursively call ourselves to check. */
10543 else if (!sym_sec->call_check_done)
10544 {
10545 int recur;
10546
10547 /* Mark current section as indeterminate, so that other
10548 sections that call back to current won't be marked as
10549 known. */
10550 isec->call_check_in_progress = 1;
10551 recur = toc_adjusting_stub_needed (info, sym_sec);
10552 isec->call_check_in_progress = 0;
10553
4c52953f
AM
10554 if (recur != 0)
10555 {
70cc837d
AM
10556 ret = recur;
10557 if (recur != 2)
10558 break;
4c52953f
AM
10559 }
10560 }
4c52953f 10561 }
70cc837d
AM
10562
10563 if (local_syms != NULL
10564 && (elf_symtab_hdr (isec->owner).contents
10565 != (unsigned char *) local_syms))
10566 free (local_syms);
10567 if (elf_section_data (isec)->relocs != relstart)
10568 free (relstart);
9b5ecbd0
AM
10569 }
10570
70cc837d
AM
10571 if ((ret & 1) == 0
10572 && isec->map_head.s != NULL
10573 && (strcmp (isec->output_section->name, ".init") == 0
10574 || strcmp (isec->output_section->name, ".fini") == 0))
10575 {
10576 if (isec->map_head.s->has_toc_reloc
10577 || isec->map_head.s->makes_toc_func_call)
10578 ret = 1;
10579 else if (!isec->map_head.s->call_check_done)
10580 {
10581 int recur;
10582 isec->call_check_in_progress = 1;
10583 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10584 isec->call_check_in_progress = 0;
10585 if (recur != 0)
10586 ret = recur;
10587 }
10588 }
10589
10590 if (ret == 1)
10591 isec->makes_toc_func_call = 1;
4c52953f 10592
9b5ecbd0
AM
10593 return ret;
10594}
10595
721956f4
AM
10596/* The linker repeatedly calls this function for each input section,
10597 in the order that input sections are linked into output sections.
10598 Build lists of input sections to determine groupings between which
10599 we may insert linker stubs. */
10600
9b5ecbd0 10601bfd_boolean
4ce794b7 10602ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
10603{
10604 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10605
4dfe6ac6
NC
10606 if (htab == NULL)
10607 return FALSE;
10608
734b6cf9
AM
10609 if ((isec->output_section->flags & SEC_CODE) != 0
10610 && isec->output_section->index <= htab->top_index)
721956f4 10611 {
734b6cf9 10612 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 10613 /* Steal the link_sec pointer for our list. */
721956f4 10614#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
10615 /* This happens to make the list in reverse order,
10616 which is what we want. */
734b6cf9
AM
10617 PREV_SEC (isec) = *list;
10618 *list = isec;
721956f4 10619 }
ad8e1ba5 10620
4c52953f 10621 if (htab->multi_toc_needed)
9b5ecbd0 10622 {
4c52953f
AM
10623 /* If a code section has a function that uses the TOC then we need
10624 to use the right TOC (obviously). Also, make sure that .opd gets
10625 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
10626 can't find their function symbol (shouldn't ever happen now).
10627 Also specially treat .fixup for the linux kernel. .fixup
10628 contains branches, but only back to the function that hit an
10629 exception. */
10630 if (isec->has_toc_reloc
10631 || (isec->flags & SEC_CODE) == 0
10632 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
10633 {
10634 if (elf_gp (isec->owner) != 0)
10635 htab->toc_curr = elf_gp (isec->owner);
10636 }
6683a28d
AM
10637 else
10638 {
10639 if (!isec->call_check_done
10640 && toc_adjusting_stub_needed (info, isec) < 0)
10641 return FALSE;
10642 /* If we make a local call from this section, ie. a branch
10643 without a following nop, then we have no place to put a
10644 toc restoring insn. We must use the same toc group as
10645 the callee.
10646 Testing makes_toc_func_call actually tests for *any*
10647 calls to functions that need a good toc pointer. A more
10648 precise test would be better, as this one will set
10649 incorrect values for pasted .init/.fini fragments.
10650 (Fixed later in check_pasted_section.) */
10651 if (isec->makes_toc_func_call
10652 && elf_gp (isec->owner) != 0)
10653 htab->toc_curr = elf_gp (isec->owner);
10654 }
9b5ecbd0 10655 }
ad8e1ba5
AM
10656
10657 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 10658 Use the last TOC base. */
ad8e1ba5 10659 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 10660 return TRUE;
721956f4
AM
10661}
10662
70cc837d
AM
10663/* Check that all .init and .fini sections use the same toc, if they
10664 have toc relocs. */
10665
10666static bfd_boolean
10667check_pasted_section (struct bfd_link_info *info, const char *name)
10668{
10669 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10670
10671 if (o != NULL)
10672 {
10673 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10674 bfd_vma toc_off = 0;
10675 asection *i;
10676
10677 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10678 if (i->has_toc_reloc)
10679 {
10680 if (toc_off == 0)
10681 toc_off = htab->stub_group[i->id].toc_off;
10682 else if (toc_off != htab->stub_group[i->id].toc_off)
10683 return FALSE;
10684 }
6683a28d
AM
10685
10686 if (toc_off == 0)
10687 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10688 if (i->makes_toc_func_call)
10689 {
10690 toc_off = htab->stub_group[i->id].toc_off;
10691 break;
10692 }
10693
70cc837d
AM
10694 /* Make sure the whole pasted function uses the same toc offset. */
10695 if (toc_off != 0)
10696 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10697 htab->stub_group[i->id].toc_off = toc_off;
10698 }
10699 return TRUE;
10700}
10701
10702bfd_boolean
10703ppc64_elf_check_init_fini (struct bfd_link_info *info)
10704{
10705 return (check_pasted_section (info, ".init")
10706 & check_pasted_section (info, ".fini"));
10707}
10708
721956f4
AM
10709/* See whether we can group stub sections together. Grouping stub
10710 sections may result in fewer stubs. More importantly, we need to
10711 put all .init* and .fini* stubs at the beginning of the .init or
10712 .fini output sections respectively, because glibc splits the
10713 _init and _fini functions into multiple parts. Putting a stub in
10714 the middle of a function is not a good idea. */
10715
10716static void
4ce794b7
AM
10717group_sections (struct ppc_link_hash_table *htab,
10718 bfd_size_type stub_group_size,
10719 bfd_boolean stubs_always_before_branch)
721956f4 10720{
7c8fe5c4
AM
10721 asection **list;
10722 bfd_size_type stub14_group_size;
10723 bfd_boolean suppress_size_errors;
10724
10725 suppress_size_errors = FALSE;
10726 stub14_group_size = stub_group_size;
10727 if (stub_group_size == 1)
10728 {
10729 /* Default values. */
10730 if (stubs_always_before_branch)
10731 {
10732 stub_group_size = 0x1e00000;
10733 stub14_group_size = 0x7800;
10734 }
10735 else
10736 {
10737 stub_group_size = 0x1c00000;
10738 stub14_group_size = 0x7000;
10739 }
10740 suppress_size_errors = TRUE;
10741 }
10742
10743 list = htab->input_list + htab->top_index;
734b6cf9 10744 do
721956f4 10745 {
734b6cf9
AM
10746 asection *tail = *list;
10747 while (tail != NULL)
721956f4 10748 {
734b6cf9
AM
10749 asection *curr;
10750 asection *prev;
10751 bfd_size_type total;
10752 bfd_boolean big_sec;
10753 bfd_vma curr_toc;
10754
10755 curr = tail;
eea6121a 10756 total = tail->size;
6bee8834
AM
10757 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10758 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
10759 ? stub14_group_size : stub_group_size);
10760 if (big_sec && !suppress_size_errors)
5c3dead3
AM
10761 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10762 tail->owner, tail);
734b6cf9
AM
10763 curr_toc = htab->stub_group[tail->id].toc_off;
10764
10765 while ((prev = PREV_SEC (curr)) != NULL
10766 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
10767 < (ppc64_elf_section_data (prev) != NULL
10768 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10769 ? stub14_group_size : stub_group_size))
ad8e1ba5 10770 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
10771 curr = prev;
10772
10773 /* OK, the size from the start of CURR to the end is less
10774 than stub_group_size and thus can be handled by one stub
10775 section. (or the tail section is itself larger than
10776 stub_group_size, in which case we may be toast.) We
10777 should really be keeping track of the total size of stubs
10778 added here, as stubs contribute to the final output
10779 section size. That's a little tricky, and this way will
10780 only break if stubs added make the total size more than
10781 2^25, ie. for the default stub_group_size, if stubs total
10782 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10783 do
721956f4
AM
10784 {
10785 prev = PREV_SEC (tail);
734b6cf9 10786 /* Set up this stub group. */
721956f4
AM
10787 htab->stub_group[tail->id].link_sec = curr;
10788 }
734b6cf9
AM
10789 while (tail != curr && (tail = prev) != NULL);
10790
10791 /* But wait, there's more! Input sections up to stub_group_size
10792 bytes before the stub section can be handled by it too.
10793 Don't do this if we have a really large section after the
10794 stubs, as adding more stubs increases the chance that
10795 branches may not reach into the stub section. */
10796 if (!stubs_always_before_branch && !big_sec)
10797 {
10798 total = 0;
10799 while (prev != NULL
10800 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
10801 < (ppc64_elf_section_data (prev) != NULL
10802 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10803 ? stub14_group_size : stub_group_size))
734b6cf9
AM
10804 && htab->stub_group[prev->id].toc_off == curr_toc)
10805 {
10806 tail = prev;
10807 prev = PREV_SEC (tail);
10808 htab->stub_group[tail->id].link_sec = curr;
10809 }
10810 }
10811 tail = prev;
721956f4
AM
10812 }
10813 }
734b6cf9
AM
10814 while (list-- != htab->input_list);
10815 free (htab->input_list);
721956f4
AM
10816#undef PREV_SEC
10817}
10818
721956f4
AM
10819/* Determine and set the size of the stub section for a final link.
10820
10821 The basic idea here is to examine all the relocations looking for
10822 PC-relative calls to a target that is unreachable with a "bl"
10823 instruction. */
10824
b34976b6 10825bfd_boolean
927be08e 10826ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
721956f4
AM
10827{
10828 bfd_size_type stub_group_size;
b34976b6 10829 bfd_boolean stubs_always_before_branch;
721956f4
AM
10830 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10831
4dfe6ac6
NC
10832 if (htab == NULL)
10833 return FALSE;
10834
721956f4
AM
10835 stubs_always_before_branch = group_size < 0;
10836 if (group_size < 0)
10837 stub_group_size = -group_size;
10838 else
10839 stub_group_size = group_size;
721956f4
AM
10840
10841 group_sections (htab, stub_group_size, stubs_always_before_branch);
10842
721956f4
AM
10843 while (1)
10844 {
10845 bfd *input_bfd;
10846 unsigned int bfd_indx;
10847 asection *stub_sec;
721956f4
AM
10848
10849 htab->stub_iteration += 1;
721956f4
AM
10850
10851 for (input_bfd = info->input_bfds, bfd_indx = 0;
10852 input_bfd != NULL;
10853 input_bfd = input_bfd->link_next, bfd_indx++)
10854 {
10855 Elf_Internal_Shdr *symtab_hdr;
10856 asection *section;
6cdc0ccc 10857 Elf_Internal_Sym *local_syms = NULL;
721956f4 10858
0c8d6e5c 10859 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
10860 continue;
10861
721956f4 10862 /* We'll need the symbol table in a second. */
0ffa91dd 10863 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
10864 if (symtab_hdr->sh_info == 0)
10865 continue;
10866
721956f4
AM
10867 /* Walk over each section attached to the input bfd. */
10868 for (section = input_bfd->sections;
10869 section != NULL;
10870 section = section->next)
10871 {
721956f4 10872 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
10873
10874 /* If there aren't any relocs, then there's nothing more
10875 to do. */
10876 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
10877 || (section->flags & SEC_ALLOC) == 0
10878 || (section->flags & SEC_LOAD) == 0
10879 || (section->flags & SEC_CODE) == 0
721956f4
AM
10880 || section->reloc_count == 0)
10881 continue;
10882
10883 /* If this section is a link-once section that will be
10884 discarded, then don't create any stubs. */
10885 if (section->output_section == NULL
927be08e 10886 || section->output_section->owner != info->output_bfd)
721956f4
AM
10887 continue;
10888
1e2f5b6e
AM
10889 /* Get the relocs. */
10890 internal_relocs
4ce794b7 10891 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 10892 info->keep_memory);
721956f4 10893 if (internal_relocs == NULL)
1e2f5b6e 10894 goto error_ret_free_local;
721956f4
AM
10895
10896 /* Now examine each relocation. */
10897 irela = internal_relocs;
10898 irelaend = irela + section->reloc_count;
10899 for (; irela < irelaend; irela++)
10900 {
4ce794b7
AM
10901 enum elf_ppc64_reloc_type r_type;
10902 unsigned int r_indx;
721956f4
AM
10903 enum ppc_stub_type stub_type;
10904 struct ppc_stub_hash_entry *stub_entry;
8387904d 10905 asection *sym_sec, *code_sec;
e054468f 10906 bfd_vma sym_value, code_value;
721956f4 10907 bfd_vma destination;
8843416a 10908 bfd_boolean ok_dest;
721956f4 10909 struct ppc_link_hash_entry *hash;
8387904d 10910 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
10911 struct elf_link_hash_entry *h;
10912 Elf_Internal_Sym *sym;
721956f4
AM
10913 char *stub_name;
10914 const asection *id_sec;
74f0fb50 10915 struct _opd_sec_data *opd;
e054468f 10916 struct plt_entry *plt_ent;
721956f4
AM
10917
10918 r_type = ELF64_R_TYPE (irela->r_info);
10919 r_indx = ELF64_R_SYM (irela->r_info);
10920
4ce794b7 10921 if (r_type >= R_PPC64_max)
721956f4
AM
10922 {
10923 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 10924 goto error_ret_free_internal;
721956f4
AM
10925 }
10926
10927 /* Only look for stubs on branch instructions. */
4ce794b7
AM
10928 if (r_type != R_PPC64_REL24
10929 && r_type != R_PPC64_REL14
10930 && r_type != R_PPC64_REL14_BRTAKEN
10931 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
10932 continue;
10933
10934 /* Now determine the call target, its name, value,
10935 section. */
411e1bfb
AM
10936 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10937 r_indx, input_bfd))
10938 goto error_ret_free_internal;
10939 hash = (struct ppc_link_hash_entry *) h;
10940
8843416a 10941 ok_dest = FALSE;
8387904d 10942 fdh = NULL;
7fe2b9a6 10943 sym_value = 0;
411e1bfb 10944 if (hash == NULL)
721956f4 10945 {
411e1bfb 10946 sym_value = sym->st_value;
8843416a 10947 ok_dest = TRUE;
721956f4 10948 }
7fe2b9a6
AM
10949 else if (hash->elf.root.type == bfd_link_hash_defined
10950 || hash->elf.root.type == bfd_link_hash_defweak)
10951 {
10952 sym_value = hash->elf.root.u.def.value;
10953 if (sym_sec->output_section != NULL)
10954 ok_dest = TRUE;
10955 }
10956 else if (hash->elf.root.type == bfd_link_hash_undefweak
10957 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 10958 {
99877b66 10959 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
10960 use the func descriptor sym instead if it is
10961 defined. */
ceb1f1ef 10962 if (hash->elf.root.root.string[0] == '.'
b31867b6 10963 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 10964 {
8387904d
AM
10965 if (fdh->elf.root.type == bfd_link_hash_defined
10966 || fdh->elf.root.type == bfd_link_hash_defweak)
10967 {
10968 sym_sec = fdh->elf.root.u.def.section;
10969 sym_value = fdh->elf.root.u.def.value;
10970 if (sym_sec->output_section != NULL)
10971 ok_dest = TRUE;
10972 }
99877b66
AM
10973 else
10974 fdh = NULL;
8387904d 10975 }
7fe2b9a6
AM
10976 }
10977 else
10978 {
10979 bfd_set_error (bfd_error_bad_value);
10980 goto error_ret_free_internal;
721956f4
AM
10981 }
10982
8843416a
AM
10983 destination = 0;
10984 if (ok_dest)
10985 {
10986 sym_value += irela->r_addend;
10987 destination = (sym_value
10988 + sym_sec->output_offset
10989 + sym_sec->output_section->vma);
10990 }
10991
8387904d 10992 code_sec = sym_sec;
e054468f 10993 code_value = sym_value;
74f0fb50
AM
10994 opd = get_opd_info (sym_sec);
10995 if (opd != NULL)
8387904d
AM
10996 {
10997 bfd_vma dest;
10998
74f0fb50 10999 if (hash == NULL && opd->adjust != NULL)
8387904d 11000 {
74f0fb50 11001 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11002 if (adjust == -1)
11003 continue;
e054468f 11004 code_value += adjust;
8387904d
AM
11005 sym_value += adjust;
11006 }
11007 dest = opd_entry_value (sym_sec, sym_value,
e054468f 11008 &code_sec, &code_value);
8387904d
AM
11009 if (dest != (bfd_vma) -1)
11010 {
11011 destination = dest;
11012 if (fdh != NULL)
11013 {
11014 /* Fixup old ABI sym to point at code
11015 entry. */
99877b66 11016 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11017 hash->elf.root.u.def.section = code_sec;
e054468f 11018 hash->elf.root.u.def.value = code_value;
8387904d
AM
11019 }
11020 }
11021 }
11022
721956f4 11023 /* Determine what (if any) linker stub is needed. */
e054468f 11024 plt_ent = NULL;
721956f4 11025 stub_type = ppc_type_of_stub (section, irela, &hash,
e054468f 11026 &plt_ent, destination);
ad8e1ba5
AM
11027
11028 if (stub_type != ppc_stub_plt_call)
11029 {
11030 /* Check whether we need a TOC adjusting stub.
11031 Since the linker pastes together pieces from
11032 different object files when creating the
11033 _init and _fini functions, it may be that a
11034 call to what looks like a local sym is in
11035 fact a call needing a TOC adjustment. */
8387904d
AM
11036 if (code_sec != NULL
11037 && code_sec->output_section != NULL
11038 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11039 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11040 && (code_sec->has_toc_reloc
11041 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
11042 stub_type = ppc_stub_long_branch_r2off;
11043 }
11044
721956f4
AM
11045 if (stub_type == ppc_stub_none)
11046 continue;
11047
411e1bfb
AM
11048 /* __tls_get_addr calls might be eliminated. */
11049 if (stub_type != ppc_stub_plt_call
11050 && hash != NULL
8387904d
AM
11051 && (hash == htab->tls_get_addr
11052 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11053 && section->has_tls_reloc
11054 && irela != internal_relocs)
11055 {
11056 /* Get tls info. */
f961d9dd 11057 unsigned char *tls_mask;
411e1bfb 11058
3a71aa26 11059 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11060 irela - 1, input_bfd))
11061 goto error_ret_free_internal;
e7b938ca 11062 if (*tls_mask != 0)
411e1bfb
AM
11063 continue;
11064 }
11065
721956f4
AM
11066 /* Support for grouping stub sections. */
11067 id_sec = htab->stub_group[section->id].link_sec;
11068
11069 /* Get the name of this stub. */
11070 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11071 if (!stub_name)
11072 goto error_ret_free_internal;
11073
11074 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11075 stub_name, FALSE, FALSE);
721956f4
AM
11076 if (stub_entry != NULL)
11077 {
11078 /* The proper stub has already been created. */
11079 free (stub_name);
11080 continue;
11081 }
11082
25f53a85 11083 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
11084 if (stub_entry == NULL)
11085 {
11086 free (stub_name);
6cdc0ccc
AM
11087 error_ret_free_internal:
11088 if (elf_section_data (section)->relocs == NULL)
11089 free (internal_relocs);
11090 error_ret_free_local:
11091 if (local_syms != NULL
11092 && (symtab_hdr->contents
11093 != (unsigned char *) local_syms))
11094 free (local_syms);
b34976b6 11095 return FALSE;
721956f4
AM
11096 }
11097
ad8e1ba5 11098 stub_entry->stub_type = stub_type;
e054468f
AM
11099 if (stub_type != ppc_stub_plt_call)
11100 {
11101 stub_entry->target_value = code_value;
11102 stub_entry->target_section = code_sec;
11103 }
11104 else
11105 {
11106 stub_entry->target_value = sym_value;
11107 stub_entry->target_section = sym_sec;
11108 }
721956f4 11109 stub_entry->h = hash;
e054468f 11110 stub_entry->plt_ent = plt_ent;
411e1bfb 11111 stub_entry->addend = irela->r_addend;
ee75fd95
AM
11112
11113 if (stub_entry->h != NULL)
11114 htab->stub_globals += 1;
721956f4
AM
11115 }
11116
11117 /* We're done with the internal relocs, free them. */
6cdc0ccc 11118 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 11119 free (internal_relocs);
721956f4 11120 }
6cdc0ccc
AM
11121
11122 if (local_syms != NULL
11123 && symtab_hdr->contents != (unsigned char *) local_syms)
11124 {
11125 if (!info->keep_memory)
11126 free (local_syms);
11127 else
11128 symtab_hdr->contents = (unsigned char *) local_syms;
11129 }
721956f4
AM
11130 }
11131
5c3dead3 11132 /* We may have added some stubs. Find out the new size of the
721956f4
AM
11133 stub sections. */
11134 for (stub_sec = htab->stub_bfd->sections;
11135 stub_sec != NULL;
11136 stub_sec = stub_sec->next)
e717da7e 11137 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 11138 {
5c3dead3 11139 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
11140 stub_sec->size = 0;
11141 stub_sec->reloc_count = 0;
84f5d08e 11142 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 11143 }
eea6121a
AM
11144
11145 htab->brlt->size = 0;
84f5d08e
AM
11146 htab->brlt->reloc_count = 0;
11147 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 11148 if (htab->relbrlt != NULL)
eea6121a 11149 htab->relbrlt->size = 0;
721956f4 11150
63bc6f6c 11151 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 11152
176a0d42
AM
11153 if (info->emitrelocations
11154 && htab->glink != NULL && htab->glink->size != 0)
11155 {
11156 htab->glink->reloc_count = 1;
11157 htab->glink->flags |= SEC_RELOC;
11158 }
11159
5c3dead3
AM
11160 for (stub_sec = htab->stub_bfd->sections;
11161 stub_sec != NULL;
11162 stub_sec = stub_sec->next)
11163 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11164 && stub_sec->rawsize != stub_sec->size)
11165 break;
11166
11167 /* Exit from this loop when no stubs have been added, and no stubs
11168 have changed size. */
11169 if (stub_sec == NULL)
11170 break;
11171
721956f4
AM
11172 /* Ask the linker to do its stuff. */
11173 (*htab->layout_sections_again) ();
11174 }
11175
c456f082 11176 /* It would be nice to strip htab->brlt from the output if the
afbe61cf
AM
11177 section is empty, but it's too late. If we strip sections here,
11178 the dynamic symbol table is corrupted since the section symbol
11179 for the stripped section isn't written. */
721956f4 11180
b34976b6 11181 return TRUE;
721956f4
AM
11182}
11183
11184/* Called after we have determined section placement. If sections
805fc799 11185 move, we'll be called again. Provide a value for TOCstart. */
721956f4 11186
805fc799 11187bfd_vma
4ce794b7 11188ppc64_elf_toc (bfd *obfd)
721956f4 11189{
805fc799
AM
11190 asection *s;
11191 bfd_vma TOCstart;
721956f4 11192
805fc799
AM
11193 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11194 order. The TOC starts where the first of these sections starts. */
11195 s = bfd_get_section_by_name (obfd, ".got");
e054468f 11196 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11197 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 11198 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11199 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 11200 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11201 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 11202 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
11203 {
11204 /* This may happen for
11205 o references to TOC base (SYM@toc / TOC[tc0]) without a
11206 .toc directive
11207 o bad linker script
11208 o --gc-sections and empty TOC sections
11209
11210 FIXME: Warn user? */
11211
11212 /* Look for a likely section. We probably won't even be
11213 using TOCstart. */
11214 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11215 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11216 | SEC_EXCLUDE))
805fc799
AM
11217 == (SEC_ALLOC | SEC_SMALL_DATA))
11218 break;
721956f4 11219 if (s == NULL)
805fc799 11220 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11221 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
11222 == (SEC_ALLOC | SEC_SMALL_DATA))
11223 break;
721956f4 11224 if (s == NULL)
805fc799 11225 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11226 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11227 == SEC_ALLOC)
805fc799 11228 break;
721956f4 11229 if (s == NULL)
805fc799 11230 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11231 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
11232 break;
11233 }
721956f4 11234
805fc799
AM
11235 TOCstart = 0;
11236 if (s != NULL)
11237 TOCstart = s->output_section->vma + s->output_offset;
721956f4 11238
805fc799 11239 return TOCstart;
721956f4
AM
11240}
11241
11242/* Build all the stubs associated with the current output file.
11243 The stubs are kept in a hash table attached to the main linker
11244 hash table. This function is called via gldelf64ppc_finish. */
11245
b34976b6 11246bfd_boolean
4ce794b7
AM
11247ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11248 struct bfd_link_info *info,
11249 char **stats)
5d1634d7
AM
11250{
11251 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 11252 asection *stub_sec;
5d1634d7 11253 bfd_byte *p;
e717da7e 11254 int stub_sec_count = 0;
5d1634d7 11255
4dfe6ac6
NC
11256 if (htab == NULL)
11257 return FALSE;
11258
ad8e1ba5 11259 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
11260
11261 /* Allocate memory to hold the linker stubs. */
721956f4
AM
11262 for (stub_sec = htab->stub_bfd->sections;
11263 stub_sec != NULL;
11264 stub_sec = stub_sec->next)
eea6121a
AM
11265 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11266 && stub_sec->size != 0)
e717da7e 11267 {
eea6121a
AM
11268 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11269 if (stub_sec->contents == NULL)
11270 return FALSE;
11271 /* We want to check that built size is the same as calculated
11272 size. rawsize is a convenient location to use. */
11273 stub_sec->rawsize = stub_sec->size;
11274 stub_sec->size = 0;
e717da7e 11275 }
5d1634d7 11276
23eb7e01 11277 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 11278 {
9f951329 11279 unsigned int indx;
ad8e1ba5 11280 bfd_vma plt0;
9f951329 11281
721956f4 11282 /* Build the .glink plt call stub. */
97b639ba
AM
11283 if (htab->emit_stub_syms)
11284 {
11285 struct elf_link_hash_entry *h;
468392fb
AM
11286 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11287 TRUE, FALSE, FALSE);
97b639ba
AM
11288 if (h == NULL)
11289 return FALSE;
11290 if (h->root.type == bfd_link_hash_new)
11291 {
11292 h->root.type = bfd_link_hash_defined;
11293 h->root.u.def.section = htab->glink;
ee4bf8d2 11294 h->root.u.def.value = 8;
f5385ebf
AM
11295 h->ref_regular = 1;
11296 h->def_regular = 1;
11297 h->ref_regular_nonweak = 1;
11298 h->forced_local = 1;
11299 h->non_elf = 0;
97b639ba
AM
11300 }
11301 }
176a0d42
AM
11302 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11303 if (info->emitrelocations)
11304 {
11305 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11306 if (r == NULL)
11307 return FALSE;
11308 r->r_offset = (htab->glink->output_offset
11309 + htab->glink->output_section->vma);
11310 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11311 r->r_addend = plt0;
11312 }
4ce794b7 11313 p = htab->glink->contents;
176a0d42 11314 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
11315 bfd_put_64 (htab->glink->owner, plt0, p);
11316 p += 8;
11317 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 11318 p += 4;
ee4bf8d2 11319 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 11320 p += 4;
ee4bf8d2 11321 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 11322 p += 4;
ee4bf8d2 11323 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 11324 p += 4;
ee4bf8d2 11325 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 11326 p += 4;
ee4bf8d2 11327 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 11328 p += 4;
ee4bf8d2 11329 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 11330 p += 4;
4ce794b7 11331 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 11332 p += 4;
4ce794b7 11333 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 11334 p += 4;
4ce794b7 11335 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 11336 p += 4;
4ce794b7 11337 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 11338 p += 4;
ee4bf8d2
AM
11339 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11340 {
11341 bfd_put_32 (htab->glink->owner, NOP, p);
11342 p += 4;
11343 }
ad8e1ba5 11344
9f951329
AM
11345 /* Build the .glink lazy link call stubs. */
11346 indx = 0;
eea6121a 11347 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
11348 {
11349 if (indx < 0x8000)
11350 {
4ce794b7 11351 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
11352 p += 4;
11353 }
11354 else
11355 {
4ce794b7 11356 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 11357 p += 4;
4ce794b7 11358 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
11359 p += 4;
11360 }
4ce794b7 11361 bfd_put_32 (htab->glink->owner,
ee4bf8d2 11362 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 11363 indx++;
9f951329
AM
11364 p += 4;
11365 }
eea6121a 11366 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 11367 }
5d1634d7 11368
eea6121a 11369 if (htab->brlt->size != 0)
721956f4 11370 {
4ce794b7 11371 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 11372 htab->brlt->size);
4ce794b7 11373 if (htab->brlt->contents == NULL)
b34976b6 11374 return FALSE;
721956f4 11375 }
ee75fd95 11376 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
11377 {
11378 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 11379 htab->relbrlt->size);
63bc6f6c
AM
11380 if (htab->relbrlt->contents == NULL)
11381 return FALSE;
11382 }
5d1634d7 11383
721956f4
AM
11384 /* Build the stubs as directed by the stub hash table. */
11385 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 11386
aa8a7074
AM
11387 if (htab->relbrlt != NULL)
11388 htab->relbrlt->reloc_count = 0;
11389
721956f4
AM
11390 for (stub_sec = htab->stub_bfd->sections;
11391 stub_sec != NULL;
11392 stub_sec = stub_sec->next)
e717da7e
AM
11393 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11394 {
11395 stub_sec_count += 1;
eea6121a 11396 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
11397 break;
11398 }
5d1634d7 11399
721956f4 11400 if (stub_sec != NULL
eea6121a 11401 || htab->glink->rawsize != htab->glink->size)
5d1634d7 11402 {
b34976b6 11403 htab->stub_error = TRUE;
25f53a85 11404 info->callbacks->einfo (_("stubs don't match calculated size\n"));
5d1634d7 11405 }
721956f4 11406
d2a300cf
AM
11407 if (htab->stub_error)
11408 return FALSE;
11409
11410 if (stats != NULL)
11411 {
11412 *stats = bfd_malloc (500);
11413 if (*stats == NULL)
11414 return FALSE;
11415
ee75fd95 11416 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
11417 " branch %lu\n"
11418 " toc adjust %lu\n"
11419 " long branch %lu\n"
11420 " long toc adj %lu\n"
11421 " plt call %lu"),
e717da7e 11422 stub_sec_count,
ee75fd95 11423 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
11424 htab->stub_count[ppc_stub_long_branch - 1],
11425 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11426 htab->stub_count[ppc_stub_plt_branch - 1],
11427 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11428 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
11429 }
11430 return TRUE;
5bd4f169
AM
11431}
11432
99877b66
AM
11433/* This function undoes the changes made by add_symbol_adjust. */
11434
11435static bfd_boolean
11436undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11437{
11438 struct ppc_link_hash_entry *eh;
11439
11440 if (h->root.type == bfd_link_hash_indirect)
11441 return TRUE;
11442
99877b66
AM
11443 eh = (struct ppc_link_hash_entry *) h;
11444 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11445 return TRUE;
11446
11447 eh->elf.root.type = bfd_link_hash_undefined;
11448 return TRUE;
11449}
11450
11451void
11452ppc64_elf_restore_symbols (struct bfd_link_info *info)
11453{
11454 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
11455
11456 if (htab != NULL)
11457 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
11458}
11459
60124e18
AM
11460/* What to do when ld finds relocations against symbols defined in
11461 discarded sections. */
11462
11463static unsigned int
11464ppc64_elf_action_discarded (asection *sec)
11465{
11466 if (strcmp (".opd", sec->name) == 0)
11467 return 0;
11468
11469 if (strcmp (".toc", sec->name) == 0)
11470 return 0;
11471
bce50a28
JJ
11472 if (strcmp (".toc1", sec->name) == 0)
11473 return 0;
11474
60124e18
AM
11475 return _bfd_elf_default_action_discarded (sec);
11476}
11477
ba761f19 11478/* REL points to a low-part reloc on a largetoc instruction sequence.
67f0cbdb 11479 Find the matching high-part reloc instruction and verify that it
d983b7a0
AM
11480 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11481 the high-part reloc. */
67f0cbdb
AM
11482
11483static const Elf_Internal_Rela *
11484ha_reloc_match (const Elf_Internal_Rela *relocs,
11485 const Elf_Internal_Rela *rel,
d983b7a0
AM
11486 unsigned int *reg,
11487 bfd_boolean match_addend,
67f0cbdb
AM
11488 const bfd *input_bfd,
11489 const bfd_byte *contents)
11490{
11491 enum elf_ppc64_reloc_type r_type, r_type_ha;
11492 bfd_vma r_info_ha, r_addend;
11493
11494 r_type = ELF64_R_TYPE (rel->r_info);
11495 switch (r_type)
11496 {
11497 case R_PPC64_GOT_TLSLD16_LO:
11498 case R_PPC64_GOT_TLSGD16_LO:
11499 case R_PPC64_GOT_TPREL16_LO_DS:
11500 case R_PPC64_GOT_DTPREL16_LO_DS:
11501 case R_PPC64_GOT16_LO:
11502 case R_PPC64_TOC16_LO:
11503 r_type_ha = r_type + 2;
11504 break;
11505 case R_PPC64_GOT16_LO_DS:
11506 r_type_ha = R_PPC64_GOT16_HA;
11507 break;
11508 case R_PPC64_TOC16_LO_DS:
11509 r_type_ha = R_PPC64_TOC16_HA;
11510 break;
11511 default:
11512 abort ();
11513 }
11514 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11515 r_addend = rel->r_addend;
11516
11517 while (--rel >= relocs)
11518 if (rel->r_info == r_info_ha
d983b7a0
AM
11519 && (!match_addend
11520 || rel->r_addend == r_addend))
67f0cbdb
AM
11521 {
11522 const bfd_byte *p = contents + (rel->r_offset & ~3);
11523 unsigned int insn = bfd_get_32 (input_bfd, p);
d983b7a0
AM
11524 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11525 && (insn & (0x1f << 21)) == (*reg << 21))
11526 {
11527 *reg = (insn >> 16) & 0x1f;
11528 return rel;
11529 }
67f0cbdb
AM
11530 break;
11531 }
11532 return NULL;
11533}
11534
5bd4f169
AM
11535/* The RELOCATE_SECTION function is called by the ELF backend linker
11536 to handle the relocations for a section.
11537
11538 The relocs are always passed as Rela structures; if the section
11539 actually uses Rel structures, the r_addend field will always be
11540 zero.
11541
11542 This function is responsible for adjust the section contents as
11543 necessary, and (if using Rela relocs and generating a
1049f94e 11544 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
11545 necessary.
11546
11547 This function does not have to worry about setting the reloc
11548 address or the reloc symbol index.
11549
11550 LOCAL_SYMS is a pointer to the swapped in local symbols.
11551
11552 LOCAL_SECTIONS is an array giving the section in the input file
11553 corresponding to the st_shndx field of each local symbol.
11554
11555 The global hash table entry for the global symbols can be found
11556 via elf_sym_hashes (input_bfd).
11557
1049f94e 11558 When generating relocatable output, this function must handle
5bd4f169
AM
11559 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11560 going to be the section symbol corresponding to the output
11561 section, which means that the addend must be adjusted
11562 accordingly. */
11563
b34976b6 11564static bfd_boolean
4ce794b7
AM
11565ppc64_elf_relocate_section (bfd *output_bfd,
11566 struct bfd_link_info *info,
11567 bfd *input_bfd,
11568 asection *input_section,
11569 bfd_byte *contents,
11570 Elf_Internal_Rela *relocs,
11571 Elf_Internal_Sym *local_syms,
11572 asection **local_sections)
5bd4f169 11573{
65f38f15 11574 struct ppc_link_hash_table *htab;
5bd4f169
AM
11575 Elf_Internal_Shdr *symtab_hdr;
11576 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
11577 Elf_Internal_Rela *rel;
11578 Elf_Internal_Rela *relend;
411e1bfb
AM
11579 Elf_Internal_Rela outrel;
11580 bfd_byte *loc;
411e1bfb 11581 struct got_entry **local_got_ents;
d983b7a0 11582 unsigned char *ha_opt;
5bd4f169 11583 bfd_vma TOCstart;
d983b7a0 11584 bfd_boolean no_ha_opt;
b34976b6
AM
11585 bfd_boolean ret = TRUE;
11586 bfd_boolean is_opd;
96e0dda4 11587 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 11588 bfd_boolean is_power4 = FALSE;
4fe5ca5b 11589 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 11590
65f38f15 11591 /* Initialize howto table if needed. */
5bd4f169 11592 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
11593 ppc_howto_init ();
11594
65f38f15 11595 htab = ppc_hash_table (info);
4dfe6ac6
NC
11596 if (htab == NULL)
11597 return FALSE;
ee75fd95
AM
11598
11599 /* Don't relocate stub sections. */
11600 if (input_section->owner == htab->stub_bfd)
11601 return TRUE;
11602
0c8d6e5c 11603 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 11604
411e1bfb 11605 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 11606 TOCstart = elf_gp (output_bfd);
0ffa91dd 11607 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 11608 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 11609 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
d983b7a0
AM
11610 ha_opt = NULL;
11611 no_ha_opt = FALSE;
65f38f15 11612
5bd4f169
AM
11613 rel = relocs;
11614 relend = relocs + input_section->reloc_count;
11615 for (; rel < relend; rel++)
11616 {
04c9666a 11617 enum elf_ppc64_reloc_type r_type;
4cc603a5 11618 bfd_vma addend, orig_addend;
5bd4f169
AM
11619 bfd_reloc_status_type r;
11620 Elf_Internal_Sym *sym;
11621 asection *sec;
039b3fef
AM
11622 struct elf_link_hash_entry *h_elf;
11623 struct ppc_link_hash_entry *h;
11624 struct ppc_link_hash_entry *fdh;
5bd4f169 11625 const char *sym_name;
0d4792f7 11626 unsigned long r_symndx, toc_symndx;
3a71aa26 11627 bfd_vma toc_addend;
f961d9dd
AM
11628 unsigned char tls_mask, tls_gd, tls_type;
11629 unsigned char sym_type;
5bd4f169 11630 bfd_vma relocation;
b34976b6
AM
11631 bfd_boolean unresolved_reloc;
11632 bfd_boolean warned;
67f0cbdb 11633 unsigned int insn;
e11840f9 11634 unsigned int mask;
721956f4
AM
11635 struct ppc_stub_hash_entry *stub_entry;
11636 bfd_vma max_br_offset;
11637 bfd_vma from;
5bd4f169 11638
4ce794b7 11639 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 11640 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
11641
11642 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11643 symbol of the previous ADDR64 reloc. The symbol gives us the
11644 proper TOC base to use. */
11645 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11646 && rel != relocs
11647 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11648 && is_opd)
11649 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11650
4ce794b7
AM
11651 sym = NULL;
11652 sec = NULL;
039b3fef 11653 h_elf = NULL;
4ce794b7 11654 sym_name = NULL;
b34976b6
AM
11655 unresolved_reloc = FALSE;
11656 warned = FALSE;
4cc603a5 11657 orig_addend = rel->r_addend;
65f38f15 11658
0b13192e 11659 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
11660 {
11661 /* It's a local symbol. */
74f0fb50 11662 struct _opd_sec_data *opd;
4025353c 11663
5bd4f169
AM
11664 sym = local_syms + r_symndx;
11665 sec = local_sections[r_symndx];
26c61ae5 11666 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 11667 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 11668 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
11669 opd = get_opd_info (sec);
11670 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 11671 {
74f0fb50 11672 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
11673 if (adjust == -1)
11674 relocation = 0;
11675 else
4cc603a5
AM
11676 {
11677 /* If this is a relocation against the opd section sym
11678 and we have edited .opd, adjust the reloc addend so
11679 that ld -r and ld --emit-relocs output is correct.
11680 If it is a reloc against some other .opd symbol,
11681 then the symbol value will be adjusted later. */
11682 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11683 rel->r_addend += adjust;
11684 else
11685 relocation += adjust;
11686 }
1e2f5b6e 11687 }
5bd4f169
AM
11688 }
11689 else
11690 {
b2a8e766
AM
11691 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11692 r_symndx, symtab_hdr, sym_hashes,
039b3fef 11693 h_elf, sec, relocation,
b2a8e766 11694 unresolved_reloc, warned);
039b3fef
AM
11695 sym_name = h_elf->root.root.string;
11696 sym_type = h_elf->type;
5bd4f169 11697 }
039b3fef 11698 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 11699
ab96bf03 11700 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
11701 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11702 rel, relend,
11703 ppc64_elf_howto_table[r_type],
11704 contents);
ab96bf03
AM
11705
11706 if (info->relocatable)
11707 continue;
11708
951fd09b
AM
11709 /* TLS optimizations. Replace instruction sequences and relocs
11710 based on information we collected in tls_optimize. We edit
11711 RELOCS so that --emit-relocs will output something sensible
11712 for the final instruction stream. */
11713 tls_mask = 0;
11714 tls_gd = 0;
0d4792f7 11715 toc_symndx = 0;
727fc41e
AM
11716 if (h != NULL)
11717 tls_mask = h->tls_mask;
11718 else if (local_got_ents != NULL)
411e1bfb 11719 {
e054468f
AM
11720 struct plt_entry **local_plt = (struct plt_entry **)
11721 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 11722 unsigned char *lgot_masks = (unsigned char *)
e054468f 11723 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
11724 tls_mask = lgot_masks[r_symndx];
11725 }
11726 if (tls_mask == 0
11727 && (r_type == R_PPC64_TLS
11728 || r_type == R_PPC64_TLSGD
11729 || r_type == R_PPC64_TLSLD))
11730 {
11731 /* Check for toc tls entries. */
f961d9dd 11732 unsigned char *toc_tls;
0d4792f7 11733
727fc41e
AM
11734 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11735 &local_syms, rel, input_bfd))
11736 return FALSE;
0d4792f7 11737
727fc41e
AM
11738 if (toc_tls)
11739 tls_mask = *toc_tls;
0d4792f7
AM
11740 }
11741
11742 /* Check that tls relocs are used with tls syms, and non-tls
11743 relocs are used with non-tls syms. */
cf35638d 11744 if (r_symndx != STN_UNDEF
0d4792f7
AM
11745 && r_type != R_PPC64_NONE
11746 && (h == NULL
039b3fef
AM
11747 || h->elf.root.type == bfd_link_hash_defined
11748 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
11749 && (IS_PPC64_TLS_RELOC (r_type)
11750 != (sym_type == STT_TLS
11751 || (sym_type == STT_SECTION
11752 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 11753 {
727fc41e
AM
11754 if (tls_mask != 0
11755 && (r_type == R_PPC64_TLS
11756 || r_type == R_PPC64_TLSGD
11757 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
11758 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11759 ;
11760 else
25f53a85 11761 info->callbacks->einfo
1d483afe 11762 (!IS_PPC64_TLS_RELOC (r_type)
25f53a85
AM
11763 ? _("%H: %s used with TLS symbol %s\n")
11764 : _("%H: %s used with non-TLS symbol %s\n"),
11765 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
11766 ppc64_elf_howto_table[r_type]->name,
11767 sym_name);
411e1bfb
AM
11768 }
11769
11770 /* Ensure reloc mapping code below stays sane. */
11771 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11772 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11773 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11774 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11775 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11776 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11777 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11778 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11779 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11780 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11781 abort ();
0d4792f7 11782
411e1bfb
AM
11783 switch (r_type)
11784 {
11785 default:
411e1bfb
AM
11786 break;
11787
ba761f19
AM
11788 case R_PPC64_LO_DS_OPT:
11789 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11790 if ((insn & (0x3f << 26)) != 58u << 26)
11791 abort ();
11792 insn += (14u << 26) - (58u << 26);
11793 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11794 r_type = R_PPC64_TOC16_LO;
11795 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11796 break;
11797
411e1bfb
AM
11798 case R_PPC64_TOC16:
11799 case R_PPC64_TOC16_LO:
11800 case R_PPC64_TOC16_DS:
11801 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
11802 {
11803 /* Check for toc tls entries. */
f961d9dd 11804 unsigned char *toc_tls;
951fd09b 11805 int retval;
411e1bfb 11806
3a71aa26
AM
11807 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11808 &local_syms, rel, input_bfd);
951fd09b 11809 if (retval == 0)
411e1bfb
AM
11810 return FALSE;
11811
11812 if (toc_tls)
11813 {
951fd09b 11814 tls_mask = *toc_tls;
411e1bfb
AM
11815 if (r_type == R_PPC64_TOC16_DS
11816 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
11817 {
11818 if (tls_mask != 0
11819 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11820 goto toctprel;
11821 }
411e1bfb 11822 else
951fd09b
AM
11823 {
11824 /* If we found a GD reloc pair, then we might be
11825 doing a GD->IE transition. */
11826 if (retval == 2)
11827 {
11828 tls_gd = TLS_TPRELGD;
11829 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 11830 goto tls_ldgd_opt;
951fd09b
AM
11831 }
11832 else if (retval == 3)
11833 {
11834 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 11835 goto tls_ldgd_opt;
951fd09b
AM
11836 }
11837 }
411e1bfb
AM
11838 }
11839 }
11840 break;
11841
9d6ded02
AM
11842 case R_PPC64_GOT_TPREL16_HI:
11843 case R_PPC64_GOT_TPREL16_HA:
11844 if (tls_mask != 0
11845 && (tls_mask & TLS_TPREL) == 0)
11846 {
11847 rel->r_offset -= d_offset;
11848 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11849 r_type = R_PPC64_NONE;
11850 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11851 }
11852 break;
11853
411e1bfb
AM
11854 case R_PPC64_GOT_TPREL16_DS:
11855 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
11856 if (tls_mask != 0
11857 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11858 {
81407a69 11859 toctprel:
4fe5ca5b 11860 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
11861 insn &= 31 << 21;
11862 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 11863 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 11864 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
11865 if (toc_symndx != 0)
11866 {
11867 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11868 rel->r_addend = toc_addend;
0d4792f7
AM
11869 /* We changed the symbol. Start over in order to
11870 get h, sym, sec etc. right. */
11871 rel--;
11872 continue;
11873 }
11874 else
11875 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11876 }
11877 break;
11878
11879 case R_PPC64_TLS:
951fd09b
AM
11880 if (tls_mask != 0
11881 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11882 {
411e1bfb 11883 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
11884 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11885 if (insn == 0)
411e1bfb 11886 abort ();
411e1bfb 11887 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 11888 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
11889 PPC64_TPREL16_LO which is at low-order half-word. */
11890 rel->r_offset += d_offset;
0d4792f7
AM
11891 r_type = R_PPC64_TPREL16_LO;
11892 if (toc_symndx != 0)
11893 {
11894 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11895 rel->r_addend = toc_addend;
0d4792f7
AM
11896 /* We changed the symbol. Start over in order to
11897 get h, sym, sec etc. right. */
11898 rel--;
11899 continue;
11900 }
11901 else
11902 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11903 }
11904 break;
11905
411e1bfb
AM
11906 case R_PPC64_GOT_TLSGD16_HI:
11907 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
11908 tls_gd = TLS_TPRELGD;
11909 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11910 goto tls_gdld_hi;
11911 break;
11912
411e1bfb
AM
11913 case R_PPC64_GOT_TLSLD16_HI:
11914 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 11915 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 11916 {
951fd09b
AM
11917 tls_gdld_hi:
11918 if ((tls_mask & tls_gd) != 0)
11919 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11920 + R_PPC64_GOT_TPREL16_DS);
11921 else
411e1bfb 11922 {
4fe5ca5b 11923 rel->r_offset -= d_offset;
727ac201 11924 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 11925 r_type = R_PPC64_NONE;
411e1bfb 11926 }
951fd09b 11927 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11928 }
11929 break;
11930
951fd09b
AM
11931 case R_PPC64_GOT_TLSGD16:
11932 case R_PPC64_GOT_TLSGD16_LO:
11933 tls_gd = TLS_TPRELGD;
11934 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 11935 goto tls_ldgd_opt;
951fd09b 11936 break;
411e1bfb 11937
951fd09b
AM
11938 case R_PPC64_GOT_TLSLD16:
11939 case R_PPC64_GOT_TLSLD16_LO:
11940 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11941 {
3a71aa26 11942 unsigned int insn1, insn2, insn3;
102890f0
AM
11943 bfd_vma offset;
11944
11945 tls_ldgd_opt:
727fc41e
AM
11946 offset = (bfd_vma) -1;
11947 /* If not using the newer R_PPC64_TLSGD/LD to mark
11948 __tls_get_addr calls, we must trust that the call
11949 stays with its arg setup insns, ie. that the next
11950 reloc is the __tls_get_addr call associated with
11951 the current reloc. Edit both insns. */
11952 if (input_section->has_tls_get_addr_call
11953 && rel + 1 < relend
11954 && branch_reloc_hash_match (input_bfd, rel + 1,
11955 htab->tls_get_addr,
11956 htab->tls_get_addr_fd))
11957 offset = rel[1].r_offset;
102890f0 11958 if ((tls_mask & tls_gd) != 0)
411e1bfb 11959 {
102890f0 11960 /* IE */
3a71aa26
AM
11961 insn1 = bfd_get_32 (output_bfd,
11962 contents + rel->r_offset - d_offset);
102890f0
AM
11963 insn1 &= (1 << 26) - (1 << 2);
11964 insn1 |= 58 << 26; /* ld */
11965 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 11966 if (offset != (bfd_vma) -1)
f58d5a2d 11967 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
11968 if ((tls_mask & TLS_EXPLICIT) == 0)
11969 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11970 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 11971 else
102890f0
AM
11972 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
11973 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11974 }
11975 else
11976 {
11977 /* LE */
11978 insn1 = 0x3c6d0000; /* addis 3,13,0 */
11979 insn2 = 0x38630000; /* addi 3,3,0 */
11980 if (tls_gd == 0)
951fd09b 11981 {
102890f0 11982 /* Was an LD reloc. */
1d483afe
AM
11983 if (toc_symndx)
11984 sec = local_sections[toc_symndx];
11985 for (r_symndx = 0;
11986 r_symndx < symtab_hdr->sh_info;
11987 r_symndx++)
11988 if (local_sections[r_symndx] == sec)
11989 break;
11990 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 11991 r_symndx = STN_UNDEF;
102890f0 11992 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 11993 if (r_symndx != STN_UNDEF)
1d483afe
AM
11994 rel->r_addend -= (local_syms[r_symndx].st_value
11995 + sec->output_offset
11996 + sec->output_section->vma);
951fd09b 11997 }
102890f0 11998 else if (toc_symndx != 0)
3a71aa26
AM
11999 {
12000 r_symndx = toc_symndx;
12001 rel->r_addend = toc_addend;
12002 }
102890f0
AM
12003 r_type = R_PPC64_TPREL16_HA;
12004 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
12005 if (offset != (bfd_vma) -1)
12006 {
12007 rel[1].r_info = ELF64_R_INFO (r_symndx,
12008 R_PPC64_TPREL16_LO);
12009 rel[1].r_offset = offset + d_offset;
12010 rel[1].r_addend = rel->r_addend;
12011 }
102890f0 12012 }
3a71aa26
AM
12013 bfd_put_32 (output_bfd, insn1,
12014 contents + rel->r_offset - d_offset);
727fc41e
AM
12015 if (offset != (bfd_vma) -1)
12016 {
12017 insn3 = bfd_get_32 (output_bfd,
12018 contents + offset + 4);
12019 if (insn3 == NOP
12020 || insn3 == CROR_151515 || insn3 == CROR_313131)
12021 {
12022 rel[1].r_offset += 4;
12023 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12024 insn2 = NOP;
12025 }
12026 bfd_put_32 (output_bfd, insn2, contents + offset);
12027 }
12028 if ((tls_mask & tls_gd) == 0
12029 && (tls_gd == 0 || toc_symndx != 0))
12030 {
12031 /* We changed the symbol. Start over in order
12032 to get h, sym, sec etc. right. */
12033 rel--;
12034 continue;
12035 }
12036 }
12037 break;
12038
12039 case R_PPC64_TLSGD:
12040 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12041 {
12042 unsigned int insn2, insn3;
12043 bfd_vma offset = rel->r_offset;
12044
12045 if ((tls_mask & TLS_TPRELGD) != 0)
12046 {
12047 /* IE */
12048 r_type = R_PPC64_NONE;
12049 insn2 = 0x7c636a14; /* add 3,3,13 */
12050 }
12051 else
12052 {
12053 /* LE */
12054 if (toc_symndx != 0)
12055 {
12056 r_symndx = toc_symndx;
12057 rel->r_addend = toc_addend;
12058 }
12059 r_type = R_PPC64_TPREL16_LO;
12060 rel->r_offset = offset + d_offset;
12061 insn2 = 0x38630000; /* addi 3,3,0 */
12062 }
12063 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12064 /* Zap the reloc on the _tls_get_addr call too. */
12065 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12066 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
12067 insn3 = bfd_get_32 (output_bfd,
12068 contents + offset + 4);
102890f0
AM
12069 if (insn3 == NOP
12070 || insn3 == CROR_151515 || insn3 == CROR_313131)
12071 {
727fc41e 12072 rel->r_offset += 4;
3a71aa26
AM
12073 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12074 insn2 = NOP;
102890f0 12075 }
102890f0 12076 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 12077 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 12078 {
102890f0
AM
12079 rel--;
12080 continue;
411e1bfb 12081 }
411e1bfb 12082 }
411e1bfb
AM
12083 break;
12084
727fc41e
AM
12085 case R_PPC64_TLSLD:
12086 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12087 {
12088 unsigned int insn2, insn3;
12089 bfd_vma offset = rel->r_offset;
12090
12091 if (toc_symndx)
12092 sec = local_sections[toc_symndx];
12093 for (r_symndx = 0;
12094 r_symndx < symtab_hdr->sh_info;
12095 r_symndx++)
12096 if (local_sections[r_symndx] == sec)
12097 break;
12098 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12099 r_symndx = STN_UNDEF;
727fc41e 12100 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12101 if (r_symndx != STN_UNDEF)
727fc41e
AM
12102 rel->r_addend -= (local_syms[r_symndx].st_value
12103 + sec->output_offset
12104 + sec->output_section->vma);
12105
12106 r_type = R_PPC64_TPREL16_LO;
12107 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12108 rel->r_offset = offset + d_offset;
12109 /* Zap the reloc on the _tls_get_addr call too. */
12110 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12111 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
12112 insn2 = 0x38630000; /* addi 3,3,0 */
12113 insn3 = bfd_get_32 (output_bfd,
12114 contents + offset + 4);
12115 if (insn3 == NOP
12116 || insn3 == CROR_151515 || insn3 == CROR_313131)
12117 {
12118 rel->r_offset += 4;
12119 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12120 insn2 = NOP;
12121 }
12122 bfd_put_32 (output_bfd, insn2, contents + offset);
12123 rel--;
12124 continue;
12125 }
12126 break;
12127
411e1bfb 12128 case R_PPC64_DTPMOD64:
951fd09b
AM
12129 if (rel + 1 < relend
12130 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12131 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 12132 {
951fd09b
AM
12133 if ((tls_mask & TLS_GD) == 0)
12134 {
12135 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12136 if ((tls_mask & TLS_TPRELGD) != 0)
12137 r_type = R_PPC64_TPREL64;
12138 else
12139 {
4ce794b7 12140 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
12141 r_type = R_PPC64_NONE;
12142 }
12143 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12144 }
12145 }
12146 else
12147 {
12148 if ((tls_mask & TLS_LD) == 0)
411e1bfb 12149 {
4ce794b7 12150 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 12151 r_type = R_PPC64_NONE;
951fd09b 12152 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 12153 }
411e1bfb
AM
12154 }
12155 break;
12156
12157 case R_PPC64_TPREL64:
951fd09b 12158 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
12159 {
12160 r_type = R_PPC64_NONE;
12161 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12162 }
12163 break;
12164 }
12165
12166 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 12167 insn = 0;
b25116a9
AM
12168 max_br_offset = 1 << 25;
12169 addend = rel->r_addend;
65f38f15 12170 switch (r_type)
5bd4f169
AM
12171 {
12172 default:
65f38f15 12173 break;
5bd4f169 12174
65f38f15
AM
12175 /* Branch taken prediction relocations. */
12176 case R_PPC64_ADDR14_BRTAKEN:
12177 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
12178 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12179 /* Fall thru. */
65f38f15 12180
86c76c7b 12181 /* Branch not taken prediction relocations. */
65f38f15
AM
12182 case R_PPC64_ADDR14_BRNTAKEN:
12183 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
12184 insn |= bfd_get_32 (output_bfd,
12185 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 12186 /* Fall thru. */
86c76c7b 12187
b25116a9
AM
12188 case R_PPC64_REL14:
12189 max_br_offset = 1 << 15;
12190 /* Fall thru. */
5bd4f169 12191
65f38f15 12192 case R_PPC64_REL24:
ad8e1ba5
AM
12193 /* Calls to functions with a different TOC, such as calls to
12194 shared objects, need to alter the TOC pointer. This is
12195 done using a linkage stub. A REL24 branching to these
12196 linkage stubs needs to be followed by a nop, as the nop
12197 will be replaced with an instruction to restore the TOC
12198 base pointer. */
8387904d 12199 fdh = h;
b31867b6
AM
12200 if (h != NULL
12201 && h->oh != NULL
12202 && h->oh->is_func_descriptor)
12203 fdh = ppc_follow_link (h->oh);
6abec6bc
AM
12204 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12205 if (stub_entry != NULL
ad8e1ba5
AM
12206 && (stub_entry->stub_type == ppc_stub_plt_call
12207 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12208 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 12209 {
b25116a9 12210 bfd_boolean can_plt_call = FALSE;
721956f4 12211
eea6121a 12212 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 12213 {
b25116a9
AM
12214 unsigned long nop;
12215 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12216 if (nop == NOP
12217 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 12218 {
a7f2871e
AM
12219 if (h != NULL
12220 && (h == htab->tls_get_addr_fd
12221 || h == htab->tls_get_addr)
12222 && !htab->no_tls_get_addr_opt)
12223 {
12224 /* Special stub used, leave nop alone. */
12225 }
12226 else
12227 bfd_put_32 (input_bfd, LD_R2_40R1,
12228 contents + rel->r_offset + 4);
b25116a9 12229 can_plt_call = TRUE;
41bd81ab 12230 }
5bd4f169 12231 }
721956f4
AM
12232
12233 if (!can_plt_call)
12234 {
ad8e1ba5
AM
12235 if (stub_entry->stub_type == ppc_stub_plt_call)
12236 {
12237 /* If this is a plain branch rather than a branch
4fa1c237
AM
12238 and link, don't require a nop. However, don't
12239 allow tail calls in a shared library as they
12240 will result in r2 being corrupted. */
b25116a9
AM
12241 unsigned long br;
12242 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 12243 if (info->executable && (br & 1) == 0)
b25116a9 12244 can_plt_call = TRUE;
15b552e9
AM
12245 else
12246 stub_entry = NULL;
ad8e1ba5 12247 }
6ab189d5 12248 else if (h != NULL
039b3fef 12249 && strcmp (h->elf.root.root.string,
6ab189d5
AM
12250 ".__libc_start_main") == 0)
12251 {
12252 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 12253 can_plt_call = TRUE;
6ab189d5 12254 }
ad8e1ba5
AM
12255 else
12256 {
12257 if (strcmp (input_section->output_section->name,
12258 ".init") == 0
12259 || strcmp (input_section->output_section->name,
12260 ".fini") == 0)
25f53a85
AM
12261 info->callbacks->einfo
12262 (_("%H: automatic multiple TOCs "
ad8e1ba5 12263 "not supported using your crt files; "
25f53a85
AM
12264 "recompile with -mminimal-toc or upgrade gcc\n"),
12265 input_bfd, input_section, rel->r_offset);
ad8e1ba5 12266 else
25f53a85
AM
12267 info->callbacks->einfo
12268 (_("%H: sibling call optimization to `%s' "
ad8e1ba5
AM
12269 "does not allow automatic multiple TOCs; "
12270 "recompile with -mminimal-toc or "
12271 "-fno-optimize-sibling-calls, "
25f53a85
AM
12272 "or make `%s' extern\n"),
12273 input_bfd, input_section, rel->r_offset,
ad8e1ba5
AM
12274 sym_name,
12275 sym_name);
12276 bfd_set_error (bfd_error_bad_value);
12277 ret = FALSE;
12278 }
721956f4
AM
12279 }
12280
b25116a9
AM
12281 if (can_plt_call
12282 && stub_entry->stub_type == ppc_stub_plt_call)
12283 unresolved_reloc = FALSE;
12284 }
12285
6abec6bc
AM
12286 if ((stub_entry == NULL
12287 || stub_entry->stub_type == ppc_stub_long_branch
12288 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
12289 && get_opd_info (sec) != NULL)
12290 {
12291 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
12292 bfd_vma off = (relocation + addend
12293 - sec->output_section->vma
12294 - sec->output_offset);
8387904d
AM
12295 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12296 if (dest != (bfd_vma) -1)
12297 {
12298 relocation = dest;
12299 addend = 0;
12300 }
12301 }
12302
b25116a9
AM
12303 /* If the branch is out of reach we ought to have a long
12304 branch stub. */
12305 from = (rel->r_offset
12306 + input_section->output_offset
12307 + input_section->output_section->vma);
12308
6abec6bc
AM
12309 if (stub_entry != NULL
12310 && (stub_entry->stub_type == ppc_stub_long_branch
12311 || stub_entry->stub_type == ppc_stub_plt_branch)
12312 && (r_type == R_PPC64_ADDR14_BRTAKEN
12313 || r_type == R_PPC64_ADDR14_BRNTAKEN
12314 || (relocation + addend - from + max_br_offset
12315 < 2 * max_br_offset)))
12316 /* Don't use the stub if this branch is in range. */
12317 stub_entry = NULL;
b25116a9
AM
12318
12319 if (stub_entry != NULL)
12320 {
12321 /* Munge up the value and addend so that we call the stub
12322 rather than the procedure directly. */
12323 relocation = (stub_entry->stub_offset
12324 + stub_entry->stub_sec->output_offset
12325 + stub_entry->stub_sec->output_section->vma);
12326 addend = 0;
12327 }
12328
12329 if (insn != 0)
12330 {
12331 if (is_power4)
721956f4 12332 {
b25116a9
AM
12333 /* Set 'a' bit. This is 0b00010 in BO field for branch
12334 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12335 for branch on CTR insns (BO == 1a00t or 1a01t). */
12336 if ((insn & (0x14 << 21)) == (0x04 << 21))
12337 insn |= 0x02 << 21;
12338 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12339 insn |= 0x08 << 21;
12340 else
12341 break;
12342 }
12343 else
12344 {
12345 /* Invert 'y' bit if not the default. */
4cc603a5 12346 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 12347 insn ^= 0x01 << 21;
721956f4 12348 }
b25116a9
AM
12349
12350 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 12351 }
e86ce104 12352
06da1e8e
AM
12353 /* NOP out calls to undefined weak functions.
12354 We can thus call a weak function without first
12355 checking whether the function is defined. */
b25116a9 12356 else if (h != NULL
039b3fef 12357 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 12358 && h->elf.dynindx == -1
b25116a9
AM
12359 && r_type == R_PPC64_REL24
12360 && relocation == 0
4cc603a5 12361 && addend == 0)
e86ce104 12362 {
06da1e8e
AM
12363 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12364 continue;
e86ce104 12365 }
65f38f15
AM
12366 break;
12367 }
5bd4f169 12368
65f38f15 12369 /* Set `addend'. */
411e1bfb 12370 tls_type = 0;
65f38f15
AM
12371 switch (r_type)
12372 {
12373 default:
25f53a85
AM
12374 info->callbacks->einfo
12375 (_("%B: unknown relocation type %d for symbol %s\n"),
d003868e 12376 input_bfd, (int) r_type, sym_name);
5bd4f169 12377
65f38f15 12378 bfd_set_error (bfd_error_bad_value);
b34976b6 12379 ret = FALSE;
65f38f15 12380 continue;
5bd4f169 12381
65f38f15 12382 case R_PPC64_NONE:
411e1bfb 12383 case R_PPC64_TLS:
727fc41e
AM
12384 case R_PPC64_TLSGD:
12385 case R_PPC64_TLSLD:
04c9666a
AM
12386 case R_PPC64_GNU_VTINHERIT:
12387 case R_PPC64_GNU_VTENTRY:
65f38f15 12388 continue;
5bd4f169
AM
12389
12390 /* GOT16 relocations. Like an ADDR16 using the symbol's
12391 address in the GOT as relocation value instead of the
411e1bfb 12392 symbol's value itself. Also, create a GOT entry for the
5bd4f169 12393 symbol and put the symbol value there. */
411e1bfb
AM
12394 case R_PPC64_GOT_TLSGD16:
12395 case R_PPC64_GOT_TLSGD16_LO:
12396 case R_PPC64_GOT_TLSGD16_HI:
12397 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 12398 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
12399 goto dogot;
12400
12401 case R_PPC64_GOT_TLSLD16:
12402 case R_PPC64_GOT_TLSLD16_LO:
12403 case R_PPC64_GOT_TLSLD16_HI:
12404 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12405 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
12406 goto dogot;
12407
12408 case R_PPC64_GOT_TPREL16_DS:
12409 case R_PPC64_GOT_TPREL16_LO_DS:
12410 case R_PPC64_GOT_TPREL16_HI:
12411 case R_PPC64_GOT_TPREL16_HA:
12412 tls_type = TLS_TLS | TLS_TPREL;
12413 goto dogot;
12414
12415 case R_PPC64_GOT_DTPREL16_DS:
12416 case R_PPC64_GOT_DTPREL16_LO_DS:
12417 case R_PPC64_GOT_DTPREL16_HI:
12418 case R_PPC64_GOT_DTPREL16_HA:
12419 tls_type = TLS_TLS | TLS_DTPREL;
12420 goto dogot;
12421
65f38f15
AM
12422 case R_PPC64_GOT16:
12423 case R_PPC64_GOT16_LO:
12424 case R_PPC64_GOT16_HI:
12425 case R_PPC64_GOT16_HA:
12426 case R_PPC64_GOT16_DS:
12427 case R_PPC64_GOT16_LO_DS:
411e1bfb 12428 dogot:
5bd4f169
AM
12429 {
12430 /* Relocation is to the entry for this symbol in the global
12431 offset table. */
e717da7e 12432 asection *got;
d881513a 12433 bfd_vma *offp;
5bd4f169 12434 bfd_vma off;
d881513a 12435 unsigned long indx = 0;
927be08e 12436 struct got_entry *ent;
65f38f15 12437
d881513a
AM
12438 if (tls_type == (TLS_TLS | TLS_LD)
12439 && (h == NULL
f5385ebf 12440 || !h->elf.def_dynamic))
927be08e 12441 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 12442 else
5bd4f169 12443 {
5bd4f169 12444
d881513a
AM
12445 if (h != NULL)
12446 {
12447 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
12448 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12449 &h->elf)
d881513a 12450 || (info->shared
726d3ab0 12451 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
12452 /* This is actually a static link, or it is a
12453 -Bsymbolic link and the symbol is defined
12454 locally, or the symbol was forced to be local
12455 because of a version file. */
12456 ;
12457 else
12458 {
039b3fef 12459 indx = h->elf.dynindx;
d881513a
AM
12460 unresolved_reloc = FALSE;
12461 }
039b3fef 12462 ent = h->elf.got.glist;
d881513a 12463 }
411e1bfb 12464 else
5bd4f169 12465 {
d881513a
AM
12466 if (local_got_ents == NULL)
12467 abort ();
12468 ent = local_got_ents[r_symndx];
5bd4f169 12469 }
d881513a
AM
12470
12471 for (; ent != NULL; ent = ent->next)
4cc603a5 12472 if (ent->addend == orig_addend
e717da7e 12473 && ent->owner == input_bfd
d881513a
AM
12474 && ent->tls_type == tls_type)
12475 break;
5bd4f169 12476 }
411e1bfb 12477
927be08e
AM
12478 if (ent == NULL)
12479 abort ();
12480 if (ent->is_indirect)
12481 ent = ent->got.ent;
12482 offp = &ent->got.offset;
12483 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
12484 if (got == NULL)
12485 abort ();
12486
411e1bfb
AM
12487 /* The offset must always be a multiple of 8. We use the
12488 least significant bit to record whether we have already
12489 processed this entry. */
d881513a 12490 off = *offp;
411e1bfb
AM
12491 if ((off & 1) != 0)
12492 off &= ~1;
5bd4f169
AM
12493 else
12494 {
411e1bfb
AM
12495 /* Generate relocs for the dynamic linker, except in
12496 the case of TLSLD where we'll use one entry per
12497 module. */
25f23106
AM
12498 asection *relgot;
12499 bfd_boolean ifunc;
e717da7e 12500
d881513a 12501 *offp = off | 1;
25f23106
AM
12502 relgot = NULL;
12503 ifunc = (h != NULL
12504 ? h->elf.type == STT_GNU_IFUNC
12505 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
4e795f50 12506 if ((info->shared || indx != 0)
927be08e
AM
12507 && (h == NULL
12508 || (tls_type == (TLS_TLS | TLS_LD)
12509 && !h->elf.def_dynamic)
039b3fef
AM
12510 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12511 || h->elf.root.type != bfd_link_hash_undefweak))
927be08e 12512 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106
AM
12513 else if (ifunc)
12514 relgot = htab->reliplt;
12515 if (relgot != NULL)
5bd4f169 12516 {
e717da7e
AM
12517 outrel.r_offset = (got->output_section->vma
12518 + got->output_offset
411e1bfb 12519 + off);
4cc603a5 12520 outrel.r_addend = addend;
d881513a 12521 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 12522 {
411e1bfb 12523 outrel.r_addend = 0;
e515b051 12524 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
12525 if (tls_type == (TLS_TLS | TLS_GD))
12526 {
e717da7e
AM
12527 loc = relgot->contents;
12528 loc += (relgot->reloc_count++
d881513a
AM
12529 * sizeof (Elf64_External_Rela));
12530 bfd_elf64_swap_reloca_out (output_bfd,
12531 &outrel, loc);
e515b051 12532 outrel.r_offset += 8;
4cc603a5 12533 outrel.r_addend = addend;
d881513a
AM
12534 outrel.r_info
12535 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 12536 }
411e1bfb 12537 }
951fd09b 12538 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 12539 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 12540 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12541 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
12542 else if (indx != 0)
12543 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12544 else
81407a69 12545 {
25f23106
AM
12546 if (ifunc)
12547 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12548 else
12549 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
12550
12551 /* Write the .got section contents for the sake
12552 of prelink. */
e717da7e 12553 loc = got->contents + off;
23fbd6fa
JJ
12554 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12555 loc);
81407a69 12556 }
81407a69
AM
12557
12558 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
12559 {
12560 outrel.r_addend += relocation;
12561 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 12562 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 12563 }
e717da7e
AM
12564 loc = relgot->contents;
12565 loc += (relgot->reloc_count++
411e1bfb
AM
12566 * sizeof (Elf64_External_Rela));
12567 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12568 }
12569
ad8e1ba5 12570 /* Init the .got section contents here if we're not
81407a69 12571 emitting a reloc. */
d881513a 12572 else
411e1bfb 12573 {
4cc603a5 12574 relocation += addend;
7b609f53
AM
12575 if (tls_type == (TLS_TLS | TLS_LD))
12576 relocation = 1;
12577 else if (tls_type != 0)
411e1bfb 12578 {
e1918d23 12579 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 12580 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12581 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 12582
7b609f53
AM
12583 if (tls_type == (TLS_TLS | TLS_GD))
12584 {
12585 bfd_put_64 (output_bfd, relocation,
e717da7e 12586 got->contents + off + 8);
7b609f53
AM
12587 relocation = 1;
12588 }
411e1bfb 12589 }
7b609f53 12590
411e1bfb 12591 bfd_put_64 (output_bfd, relocation,
e717da7e 12592 got->contents + off);
5bd4f169
AM
12593 }
12594 }
12595
65f38f15
AM
12596 if (off >= (bfd_vma) -2)
12597 abort ();
12598
bf102f86
AM
12599 relocation = got->output_section->vma + got->output_offset + off;
12600 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 12601 }
65f38f15
AM
12602 break;
12603
12604 case R_PPC64_PLT16_HA:
12605 case R_PPC64_PLT16_HI:
12606 case R_PPC64_PLT16_LO:
12607 case R_PPC64_PLT32:
12608 case R_PPC64_PLT64:
12609 /* Relocation is to the entry for this symbol in the
12610 procedure linkage table. */
12611
12612 /* Resolve a PLT reloc against a local symbol directly,
12613 without using the procedure linkage table. */
12614 if (h == NULL)
12615 break;
12616
411e1bfb
AM
12617 /* It's possible that we didn't make a PLT entry for this
12618 symbol. This happens when statically linking PIC code,
12619 or when using -Bsymbolic. Go find a match if there is a
12620 PLT entry. */
4ce794b7 12621 if (htab->plt != NULL)
65f38f15 12622 {
411e1bfb 12623 struct plt_entry *ent;
039b3fef 12624 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 12625 if (ent->addend == orig_addend
411e1bfb
AM
12626 && ent->plt.offset != (bfd_vma) -1)
12627 {
4ce794b7
AM
12628 relocation = (htab->plt->output_section->vma
12629 + htab->plt->output_offset
411e1bfb
AM
12630 + ent->plt.offset);
12631 unresolved_reloc = FALSE;
12632 }
65f38f15 12633 }
65f38f15 12634 break;
5bd4f169 12635
0b13192e
AM
12636 case R_PPC64_TOC:
12637 /* Relocation value is TOC base. */
12638 relocation = TOCstart;
cf35638d 12639 if (r_symndx == STN_UNDEF)
0b13192e 12640 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
12641 else if (unresolved_reloc)
12642 ;
12643 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
12644 relocation += htab->stub_group[sec->id].toc_off;
12645 else
12646 unresolved_reloc = TRUE;
ab96bf03 12647 goto dodyn;
0b13192e 12648
5bd4f169
AM
12649 /* TOC16 relocs. We want the offset relative to the TOC base,
12650 which is the address of the start of the TOC plus 0x8000.
12651 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12652 in this order. */
65f38f15
AM
12653 case R_PPC64_TOC16:
12654 case R_PPC64_TOC16_LO:
12655 case R_PPC64_TOC16_HI:
12656 case R_PPC64_TOC16_DS:
12657 case R_PPC64_TOC16_LO_DS:
12658 case R_PPC64_TOC16_HA:
ad8e1ba5 12659 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
12660 break;
12661
12662 /* Relocate against the beginning of the section. */
65f38f15
AM
12663 case R_PPC64_SECTOFF:
12664 case R_PPC64_SECTOFF_LO:
12665 case R_PPC64_SECTOFF_HI:
12666 case R_PPC64_SECTOFF_DS:
12667 case R_PPC64_SECTOFF_LO_DS:
12668 case R_PPC64_SECTOFF_HA:
4ce794b7 12669 if (sec != NULL)
65f38f15 12670 addend -= sec->output_section->vma;
5bd4f169
AM
12671 break;
12672
25f23106
AM
12673 case R_PPC64_REL16:
12674 case R_PPC64_REL16_LO:
12675 case R_PPC64_REL16_HI:
12676 case R_PPC64_REL16_HA:
12677 break;
12678
721956f4
AM
12679 case R_PPC64_REL14:
12680 case R_PPC64_REL14_BRNTAKEN:
12681 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
12682 case R_PPC64_REL24:
12683 break;
12684
411e1bfb
AM
12685 case R_PPC64_TPREL16:
12686 case R_PPC64_TPREL16_LO:
12687 case R_PPC64_TPREL16_HI:
12688 case R_PPC64_TPREL16_HA:
12689 case R_PPC64_TPREL16_DS:
12690 case R_PPC64_TPREL16_LO_DS:
12691 case R_PPC64_TPREL16_HIGHER:
12692 case R_PPC64_TPREL16_HIGHERA:
12693 case R_PPC64_TPREL16_HIGHEST:
12694 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
12695 if (h != NULL
12696 && h->elf.root.type == bfd_link_hash_undefweak
12697 && h->elf.dynindx == -1)
12698 {
12699 /* Make this relocation against an undefined weak symbol
12700 resolve to zero. This is really just a tweak, since
12701 code using weak externs ought to check that they are
12702 defined before using them. */
12703 bfd_byte *p = contents + rel->r_offset - d_offset;
12704
12705 insn = bfd_get_32 (output_bfd, p);
12706 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12707 if (insn != 0)
12708 bfd_put_32 (output_bfd, insn, p);
12709 break;
12710 }
e1918d23 12711 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12712 if (info->shared)
12713 /* The TPREL16 relocs shouldn't really be used in shared
12714 libs as they will result in DT_TEXTREL being set, but
12715 support them anyway. */
12716 goto dodyn;
12717 break;
12718
12719 case R_PPC64_DTPREL16:
12720 case R_PPC64_DTPREL16_LO:
12721 case R_PPC64_DTPREL16_HI:
12722 case R_PPC64_DTPREL16_HA:
12723 case R_PPC64_DTPREL16_DS:
12724 case R_PPC64_DTPREL16_LO_DS:
12725 case R_PPC64_DTPREL16_HIGHER:
12726 case R_PPC64_DTPREL16_HIGHERA:
12727 case R_PPC64_DTPREL16_HIGHEST:
12728 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 12729 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12730 break;
12731
e515b051
AM
12732 case R_PPC64_DTPMOD64:
12733 relocation = 1;
12734 addend = 0;
12735 goto dodyn;
12736
411e1bfb 12737 case R_PPC64_TPREL64:
e1918d23 12738 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12739 goto dodyn;
12740
12741 case R_PPC64_DTPREL64:
e1918d23 12742 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12743 /* Fall thru */
12744
65f38f15
AM
12745 /* Relocations that may need to be propagated if this is a
12746 dynamic object. */
04c9666a 12747 case R_PPC64_REL30:
65f38f15
AM
12748 case R_PPC64_REL32:
12749 case R_PPC64_REL64:
12750 case R_PPC64_ADDR14:
12751 case R_PPC64_ADDR14_BRNTAKEN:
12752 case R_PPC64_ADDR14_BRTAKEN:
12753 case R_PPC64_ADDR16:
12754 case R_PPC64_ADDR16_DS:
12755 case R_PPC64_ADDR16_HA:
12756 case R_PPC64_ADDR16_HI:
12757 case R_PPC64_ADDR16_HIGHER:
12758 case R_PPC64_ADDR16_HIGHERA:
12759 case R_PPC64_ADDR16_HIGHEST:
12760 case R_PPC64_ADDR16_HIGHESTA:
12761 case R_PPC64_ADDR16_LO:
12762 case R_PPC64_ADDR16_LO_DS:
12763 case R_PPC64_ADDR24:
65f38f15
AM
12764 case R_PPC64_ADDR32:
12765 case R_PPC64_ADDR64:
12766 case R_PPC64_UADDR16:
12767 case R_PPC64_UADDR32:
12768 case R_PPC64_UADDR64:
411e1bfb 12769 dodyn:
5d1634d7 12770 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
12771 break;
12772
41bd81ab
AM
12773 if (NO_OPD_RELOCS && is_opd)
12774 break;
12775
65f38f15 12776 if ((info->shared
4e795f50 12777 && (h == NULL
039b3fef
AM
12778 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12779 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 12780 && (must_be_dyn_reloc (info, r_type)
039b3fef 12781 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
12782 || (ELIMINATE_COPY_RELOCS
12783 && !info->shared
65f38f15 12784 && h != NULL
039b3fef 12785 && h->elf.dynindx != -1
f5385ebf 12786 && !h->elf.non_got_ref
25f23106
AM
12787 && !h->elf.def_regular)
12788 || (!info->shared
12789 && (h != NULL
12790 ? h->elf.type == STT_GNU_IFUNC
12791 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 12792 {
b34976b6 12793 bfd_boolean skip, relocate;
65f38f15 12794 asection *sreloc;
1cf1f670 12795 bfd_vma out_off;
65f38f15
AM
12796
12797 /* When generating a dynamic object, these relocations
12798 are copied into the output file to be resolved at run
12799 time. */
12800
b34976b6
AM
12801 skip = FALSE;
12802 relocate = FALSE;
65f38f15 12803
1cf1f670
AM
12804 out_off = _bfd_elf_section_offset (output_bfd, info,
12805 input_section, rel->r_offset);
12806 if (out_off == (bfd_vma) -1)
b34976b6 12807 skip = TRUE;
1cf1f670 12808 else if (out_off == (bfd_vma) -2)
b34976b6 12809 skip = TRUE, relocate = TRUE;
1cf1f670
AM
12810 out_off += (input_section->output_section->vma
12811 + input_section->output_offset);
12812 outrel.r_offset = out_off;
411e1bfb 12813 outrel.r_addend = rel->r_addend;
65f38f15 12814
1cf1f670
AM
12815 /* Optimize unaligned reloc use. */
12816 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12817 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12818 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12819 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12820 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12821 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12822 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12823 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12824 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12825
65f38f15 12826 if (skip)
0bb2d96a 12827 memset (&outrel, 0, sizeof outrel);
726d3ab0 12828 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
12829 && !is_opd
12830 && r_type != R_PPC64_TOC)
039b3fef 12831 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
12832 else
12833 {
41bd81ab
AM
12834 /* This symbol is local, or marked to become local,
12835 or this is an opd section reloc which must point
12836 at a local function. */
65f38f15 12837 outrel.r_addend += relocation;
e86ce104 12838 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 12839 {
3fad3c7c 12840 if (is_opd && h != NULL)
afbe61cf
AM
12841 {
12842 /* Lie about opd entries. This case occurs
12843 when building shared libraries and we
12844 reference a function in another shared
3fad3c7c
AM
12845 lib. The same thing happens for a weak
12846 definition in an application that's
12847 overridden by a strong definition in a
12848 shared lib. (I believe this is a generic
12849 bug in binutils handling of weak syms.)
12850 In these cases we won't use the opd
1e2f5b6e 12851 entry in this lib. */
b34976b6 12852 unresolved_reloc = FALSE;
afbe61cf 12853 }
25f23106
AM
12854 if (!is_opd
12855 && r_type == R_PPC64_ADDR64
12856 && (h != NULL
12857 ? h->elf.type == STT_GNU_IFUNC
12858 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12859 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12860 else
12861 {
12862 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 12863
25f23106
AM
12864 /* We need to relocate .opd contents for ld.so.
12865 Prelink also wants simple and consistent rules
12866 for relocs. This make all RELATIVE relocs have
12867 *r_offset equal to r_addend. */
12868 relocate = TRUE;
12869 }
65f38f15
AM
12870 }
12871 else
12872 {
12873 long indx = 0;
12874
25f23106
AM
12875 if (h != NULL
12876 ? h->elf.type == STT_GNU_IFUNC
12877 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12878 {
25f53a85
AM
12879 info->callbacks->einfo
12880 (_("%H: relocation %s for indirect "
12881 "function %s unsupported\n"),
12882 input_bfd, input_section, rel->r_offset,
25f23106
AM
12883 ppc64_elf_howto_table[r_type]->name,
12884 sym_name);
12885 ret = FALSE;
12886 }
cf35638d 12887 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
12888 ;
12889 else if (sec == NULL || sec->owner == NULL)
12890 {
12891 bfd_set_error (bfd_error_bad_value);
b34976b6 12892 return FALSE;
65f38f15
AM
12893 }
12894 else
12895 {
12896 asection *osec;
12897
12898 osec = sec->output_section;
12899 indx = elf_section_data (osec)->dynindx;
12900
74541ad4
AM
12901 if (indx == 0)
12902 {
12903 if ((osec->flags & SEC_READONLY) == 0
12904 && htab->elf.data_index_section != NULL)
12905 osec = htab->elf.data_index_section;
12906 else
12907 osec = htab->elf.text_index_section;
12908 indx = elf_section_data (osec)->dynindx;
12909 }
12910 BFD_ASSERT (indx != 0);
12911
65f38f15
AM
12912 /* We are turning this relocation into one
12913 against a section symbol, so subtract out
12914 the output section's address but not the
12915 offset of the input section in the output
12916 section. */
12917 outrel.r_addend -= osec->vma;
12918 }
12919
12920 outrel.r_info = ELF64_R_INFO (indx, r_type);
12921 }
12922 }
12923
12924 sreloc = elf_section_data (input_section)->sreloc;
25f23106
AM
12925 if (!htab->elf.dynamic_sections_created)
12926 sreloc = htab->reliplt;
65f38f15
AM
12927 if (sreloc == NULL)
12928 abort ();
12929
dfbb6ac9
AM
12930 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12931 >= sreloc->size)
12932 abort ();
947216bf
AM
12933 loc = sreloc->contents;
12934 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
12935 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12936
12937 /* If this reloc is against an external symbol, it will
12938 be computed at runtime, so there's no need to do
81407a69
AM
12939 anything now. However, for the sake of prelink ensure
12940 that the section contents are a known value. */
65f38f15 12941 if (! relocate)
81407a69
AM
12942 {
12943 unresolved_reloc = FALSE;
12944 /* The value chosen here is quite arbitrary as ld.so
12945 ignores section contents except for the special
12946 case of .opd where the contents might be accessed
12947 before relocation. Choose zero, as that won't
12948 cause reloc overflow. */
12949 relocation = 0;
12950 addend = 0;
12951 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12952 to improve backward compatibility with older
12953 versions of ld. */
12954 if (r_type == R_PPC64_ADDR64)
12955 addend = outrel.r_addend;
12956 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 12957 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
12958 addend = (input_section->output_section->vma
12959 + input_section->output_offset
12960 + rel->r_offset);
81407a69 12961 }
65f38f15 12962 }
5bd4f169
AM
12963 break;
12964
65f38f15
AM
12965 case R_PPC64_COPY:
12966 case R_PPC64_GLOB_DAT:
12967 case R_PPC64_JMP_SLOT:
25f23106 12968 case R_PPC64_JMP_IREL:
65f38f15
AM
12969 case R_PPC64_RELATIVE:
12970 /* We shouldn't ever see these dynamic relocs in relocatable
12971 files. */
ae9a127f 12972 /* Fall through. */
65f38f15
AM
12973
12974 case R_PPC64_PLTGOT16:
12975 case R_PPC64_PLTGOT16_DS:
12976 case R_PPC64_PLTGOT16_HA:
12977 case R_PPC64_PLTGOT16_HI:
12978 case R_PPC64_PLTGOT16_LO:
12979 case R_PPC64_PLTGOT16_LO_DS:
12980 case R_PPC64_PLTREL32:
12981 case R_PPC64_PLTREL64:
12982 /* These ones haven't been implemented yet. */
12983
25f53a85
AM
12984 info->callbacks->einfo
12985 (_("%B: relocation %s is not supported for symbol %s\n"),
d003868e 12986 input_bfd,
4ce794b7 12987 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
12988
12989 bfd_set_error (bfd_error_invalid_operation);
b34976b6 12990 ret = FALSE;
5bd4f169 12991 continue;
65f38f15 12992 }
5bd4f169 12993
67f0cbdb
AM
12994 /* Multi-instruction sequences that access the TOC can be
12995 optimized, eg. addis ra,r2,0; addi rb,ra,x;
12996 to nop; addi rb,r2,x; */
12997 switch (r_type)
12998 {
12999 default:
13000 break;
13001
13002 case R_PPC64_GOT_TLSLD16_HI:
13003 case R_PPC64_GOT_TLSGD16_HI:
13004 case R_PPC64_GOT_TPREL16_HI:
13005 case R_PPC64_GOT_DTPREL16_HI:
13006 case R_PPC64_GOT16_HI:
13007 case R_PPC64_TOC16_HI:
13008 /* These relocs would only be useful if building up an
13009 offset to later add to r2, perhaps in an indexed
13010 addressing mode instruction. Don't try to optimize.
13011 Unfortunately, the possibility of someone building up an
13012 offset like this or even with the HA relocs, means that
13013 we need to check the high insn when optimizing the low
13014 insn. */
13015 break;
13016
13017 case R_PPC64_GOT_TLSLD16_HA:
13018 case R_PPC64_GOT_TLSGD16_HA:
13019 case R_PPC64_GOT_TPREL16_HA:
13020 case R_PPC64_GOT_DTPREL16_HA:
13021 case R_PPC64_GOT16_HA:
13022 case R_PPC64_TOC16_HA:
d983b7a0 13023 /* nop is done later. */
67f0cbdb
AM
13024 break;
13025
13026 case R_PPC64_GOT_TLSLD16_LO:
13027 case R_PPC64_GOT_TLSGD16_LO:
13028 case R_PPC64_GOT_TPREL16_LO_DS:
13029 case R_PPC64_GOT_DTPREL16_LO_DS:
13030 case R_PPC64_GOT16_LO:
13031 case R_PPC64_GOT16_LO_DS:
13032 case R_PPC64_TOC16_LO:
13033 case R_PPC64_TOC16_LO_DS:
13034 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
13035 {
13036 bfd_byte *p = contents + (rel->r_offset & ~3);
13037 insn = bfd_get_32 (input_bfd, p);
13038 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13039 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13040 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13041 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13042 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13043 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13044 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13045 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13046 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13047 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13048 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13049 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13050 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13051 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13052 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13053 && (insn & 3) != 1)
13054 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13055 && ((insn & 3) == 0 || (insn & 3) == 3)))
13056 {
13057 unsigned int reg = (insn >> 16) & 0x1f;
d983b7a0
AM
13058 const Elf_Internal_Rela *ha;
13059 bfd_boolean match_addend;
13060
13061 match_addend = (sym != NULL
13062 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13063 ha = ha_reloc_match (relocs, rel, &reg, match_addend,
13064 input_bfd, contents);
13065 if (ha != NULL)
67f0cbdb
AM
13066 {
13067 insn &= ~(0x1f << 16);
d983b7a0 13068 insn |= reg << 16;
67f0cbdb 13069 bfd_put_32 (input_bfd, insn, p);
d983b7a0
AM
13070 if (ha_opt == NULL)
13071 {
13072 ha_opt = bfd_zmalloc (input_section->reloc_count);
13073 if (ha_opt == NULL)
13074 return FALSE;
13075 }
13076 ha_opt[ha - relocs] = 1;
67f0cbdb 13077 }
d983b7a0
AM
13078 else
13079 /* If we don't find a matching high part insn,
13080 something is fishy. Refuse to nop any high
13081 part insn in this section. */
13082 no_ha_opt = TRUE;
67f0cbdb
AM
13083 }
13084 }
13085 break;
13086 }
13087
65f38f15
AM
13088 /* Do any further special processing. */
13089 switch (r_type)
13090 {
13091 default:
13092 break;
13093
13094 case R_PPC64_ADDR16_HA:
25f23106 13095 case R_PPC64_REL16_HA:
65f38f15
AM
13096 case R_PPC64_ADDR16_HIGHERA:
13097 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
13098 case R_PPC64_TOC16_HA:
13099 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
13100 case R_PPC64_TPREL16_HA:
13101 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
13102 case R_PPC64_TPREL16_HIGHER:
13103 case R_PPC64_TPREL16_HIGHERA:
13104 case R_PPC64_TPREL16_HIGHEST:
13105 case R_PPC64_TPREL16_HIGHESTA:
13106 case R_PPC64_DTPREL16_HIGHER:
13107 case R_PPC64_DTPREL16_HIGHERA:
13108 case R_PPC64_DTPREL16_HIGHEST:
13109 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
13110 /* It's just possible that this symbol is a weak symbol
13111 that's not actually defined anywhere. In that case,
13112 'sec' would be NULL, and we should leave the symbol
13113 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
13114 if (sec == NULL)
13115 break;
13116 /* Fall thru */
13117
13118 case R_PPC64_GOT16_HA:
13119 case R_PPC64_PLTGOT16_HA:
13120 case R_PPC64_PLT16_HA:
13121 case R_PPC64_GOT_TLSGD16_HA:
13122 case R_PPC64_GOT_TLSLD16_HA:
13123 case R_PPC64_GOT_TPREL16_HA:
13124 case R_PPC64_GOT_DTPREL16_HA:
13125 /* Add 0x10000 if sign bit in 0:15 is set.
13126 Bits 0:15 are not used. */
13127 addend += 0x8000;
65f38f15
AM
13128 break;
13129
13130 case R_PPC64_ADDR16_DS:
13131 case R_PPC64_ADDR16_LO_DS:
13132 case R_PPC64_GOT16_DS:
13133 case R_PPC64_GOT16_LO_DS:
13134 case R_PPC64_PLT16_LO_DS:
13135 case R_PPC64_SECTOFF_DS:
13136 case R_PPC64_SECTOFF_LO_DS:
13137 case R_PPC64_TOC16_DS:
13138 case R_PPC64_TOC16_LO_DS:
13139 case R_PPC64_PLTGOT16_DS:
13140 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
13141 case R_PPC64_GOT_TPREL16_DS:
13142 case R_PPC64_GOT_TPREL16_LO_DS:
13143 case R_PPC64_GOT_DTPREL16_DS:
13144 case R_PPC64_GOT_DTPREL16_LO_DS:
13145 case R_PPC64_TPREL16_DS:
13146 case R_PPC64_TPREL16_LO_DS:
13147 case R_PPC64_DTPREL16_DS:
13148 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
13149 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13150 mask = 3;
13151 /* If this reloc is against an lq insn, then the value must be
13152 a multiple of 16. This is somewhat of a hack, but the
13153 "correct" way to do this by defining _DQ forms of all the
13154 _DS relocs bloats all reloc switches in this file. It
13155 doesn't seem to make much sense to use any of these relocs
13156 in data, so testing the insn should be safe. */
494dac0c 13157 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
13158 mask = 15;
13159 if (((relocation + addend) & mask) != 0)
65f38f15 13160 {
25f53a85
AM
13161 info->callbacks->einfo
13162 (_("%H: error: %s not a multiple of %u\n"),
13163 input_bfd, input_section, rel->r_offset,
4ce794b7 13164 ppc64_elf_howto_table[r_type]->name,
adadcc0c 13165 mask + 1);
65f38f15 13166 bfd_set_error (bfd_error_bad_value);
b34976b6 13167 ret = FALSE;
65f38f15
AM
13168 continue;
13169 }
13170 break;
5bd4f169
AM
13171 }
13172
239e1f3a
AM
13173 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13174 because such sections are not SEC_ALLOC and thus ld.so will
13175 not process them. */
65f38f15 13176 if (unresolved_reloc
239e1f3a 13177 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 13178 && h->elf.def_dynamic))
9c07fe7c 13179 {
25f53a85
AM
13180 info->callbacks->einfo
13181 (_("%H: unresolvable %s relocation against symbol `%s'\n"),
13182 input_bfd, input_section, rel->r_offset,
7b609f53 13183 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 13184 h->elf.root.root.string);
b34976b6 13185 ret = FALSE;
9c07fe7c 13186 }
5bd4f169 13187
65f38f15 13188 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
13189 input_bfd,
13190 input_section,
13191 contents,
411e1bfb 13192 rel->r_offset,
5bd4f169
AM
13193 relocation,
13194 addend);
13195
ef60b7ff 13196 if (r != bfd_reloc_ok)
5bd4f169 13197 {
cd27b276
AM
13198 if (sym_name == NULL)
13199 sym_name = "(null)";
13200 if (r == bfd_reloc_overflow)
5bd4f169 13201 {
cd27b276
AM
13202 if (warned)
13203 continue;
13204 if (h != NULL
039b3fef 13205 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 13206 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
13207 {
13208 /* Assume this is a call protected by other code that
13209 detects the symbol is undefined. If this is the case,
13210 we can safely ignore the overflow. If not, the
13211 program is hosed anyway, and a little warning isn't
13212 going to help. */
13213
13214 continue;
13215 }
13216
ef60b7ff 13217 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
13218 (info, (h ? &h->elf.root : NULL), sym_name,
13219 ppc64_elf_howto_table[r_type]->name,
4cc603a5 13220 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 13221 return FALSE;
ef60b7ff
AM
13222 }
13223 else
13224 {
25f53a85
AM
13225 info->callbacks->einfo
13226 (_("%H: %s reloc against `%s': error %d\n"),
13227 input_bfd, input_section, rel->r_offset,
4ce794b7 13228 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
13229 sym_name,
13230 (int) r);
b34976b6 13231 ret = FALSE;
ef60b7ff 13232 }
5bd4f169 13233 }
5bd4f169
AM
13234 }
13235
d983b7a0
AM
13236 if (ha_opt != NULL)
13237 {
13238 if (!no_ha_opt)
13239 {
13240 unsigned char *opt = ha_opt;
13241 rel = relocs;
13242 relend = relocs + input_section->reloc_count;
13243 for (; rel < relend; opt++, rel++)
13244 if (*opt != 0)
13245 {
13246 bfd_byte *p = contents + (rel->r_offset & ~3);
13247 bfd_put_32 (input_bfd, NOP, p);
13248 }
13249 }
13250 free (ha_opt);
13251 }
13252
645ea6a9
AM
13253 /* If we're emitting relocations, then shortly after this function
13254 returns, reloc offsets and addends for this section will be
13255 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
13256 file rather than the input. Save a copy of the relocs for
13257 opd_entry_value. */
4cc603a5 13258 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
13259 {
13260 bfd_size_type amt;
13261 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13262 rel = bfd_alloc (input_bfd, amt);
13263 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13264 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13265 if (rel == NULL)
13266 return FALSE;
13267 memcpy (rel, relocs, amt);
13268 }
5bd4f169
AM
13269 return ret;
13270}
13271
754021d0
AM
13272/* Adjust the value of any local symbols in opd sections. */
13273
6e0b88f1 13274static int
754021d0
AM
13275ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13276 const char *name ATTRIBUTE_UNUSED,
13277 Elf_Internal_Sym *elfsym,
13278 asection *input_sec,
13279 struct elf_link_hash_entry *h)
13280{
74f0fb50
AM
13281 struct _opd_sec_data *opd;
13282 long adjust;
754021d0
AM
13283 bfd_vma value;
13284
4025353c 13285 if (h != NULL)
6e0b88f1 13286 return 1;
4025353c 13287
74f0fb50
AM
13288 opd = get_opd_info (input_sec);
13289 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 13290 return 1;
754021d0
AM
13291
13292 value = elfsym->st_value - input_sec->output_offset;
13293 if (!info->relocatable)
13294 value -= input_sec->output_section->vma;
13295
74f0fb50 13296 adjust = opd->adjust[value / 8];
4025353c 13297 if (adjust == -1)
6e0b88f1
AM
13298 return 2;
13299
13300 elfsym->st_value += adjust;
13301 return 1;
754021d0
AM
13302}
13303
5bd4f169
AM
13304/* Finish up dynamic symbol handling. We set the contents of various
13305 dynamic sections here. */
13306
b34976b6 13307static bfd_boolean
4ce794b7
AM
13308ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13309 struct bfd_link_info *info,
13310 struct elf_link_hash_entry *h,
13311 Elf_Internal_Sym *sym)
5bd4f169 13312{
65f38f15 13313 struct ppc_link_hash_table *htab;
8387904d
AM
13314 struct plt_entry *ent;
13315 Elf_Internal_Rela rela;
13316 bfd_byte *loc;
5bd4f169 13317
65f38f15 13318 htab = ppc_hash_table (info);
4dfe6ac6
NC
13319 if (htab == NULL)
13320 return FALSE;
5bd4f169 13321
8387904d
AM
13322 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13323 if (ent->plt.offset != (bfd_vma) -1)
13324 {
13325 /* This symbol has an entry in the procedure linkage
13326 table. Set it up. */
e054468f
AM
13327 if (!htab->elf.dynamic_sections_created
13328 || h->dynindx == -1)
13329 {
13330 BFD_ASSERT (h->type == STT_GNU_IFUNC
13331 && h->def_regular
13332 && (h->root.type == bfd_link_hash_defined
13333 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
13334 rela.r_offset = (htab->iplt->output_section->vma
13335 + htab->iplt->output_offset
13336 + ent->plt.offset);
13337 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
13338 rela.r_addend = (h->root.u.def.value
13339 + h->root.u.def.section->output_offset
13340 + h->root.u.def.section->output_section->vma
13341 + ent->addend);
25f23106
AM
13342 loc = (htab->reliplt->contents
13343 + (htab->reliplt->reloc_count++
13344 * sizeof (Elf64_External_Rela)));
e054468f
AM
13345 }
13346 else
13347 {
25f23106
AM
13348 rela.r_offset = (htab->plt->output_section->vma
13349 + htab->plt->output_offset
13350 + ent->plt.offset);
e054468f
AM
13351 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13352 rela.r_addend = ent->addend;
25f23106
AM
13353 loc = (htab->relplt->contents
13354 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13355 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 13356 }
8387904d
AM
13357 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13358 }
5bd4f169 13359
f5385ebf 13360 if (h->needs_copy)
5bd4f169 13361 {
65f38f15 13362 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 13363
65f38f15
AM
13364 if (h->dynindx == -1
13365 || (h->root.type != bfd_link_hash_defined
13366 && h->root.type != bfd_link_hash_defweak)
4ce794b7 13367 || htab->relbss == NULL)
65f38f15 13368 abort ();
5bd4f169
AM
13369
13370 rela.r_offset = (h->root.u.def.value
13371 + h->root.u.def.section->output_section->vma
13372 + h->root.u.def.section->output_offset);
13373 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13374 rela.r_addend = 0;
4ce794b7
AM
13375 loc = htab->relbss->contents;
13376 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 13377 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
13378 }
13379
5bd4f169
AM
13380 /* Mark some specially defined symbols as absolute. */
13381 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13382 sym->st_shndx = SHN_ABS;
13383
b34976b6 13384 return TRUE;
5bd4f169
AM
13385}
13386
65f38f15
AM
13387/* Used to decide how to sort relocs in an optimal manner for the
13388 dynamic linker, before writing them out. */
13389
13390static enum elf_reloc_type_class
4ce794b7 13391ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 13392{
04c9666a 13393 enum elf_ppc64_reloc_type r_type;
a33d1f77 13394
4ce794b7 13395 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 13396 switch (r_type)
65f38f15
AM
13397 {
13398 case R_PPC64_RELATIVE:
13399 return reloc_class_relative;
13400 case R_PPC64_JMP_SLOT:
13401 return reloc_class_plt;
13402 case R_PPC64_COPY:
13403 return reloc_class_copy;
13404 default:
13405 return reloc_class_normal;
13406 }
13407}
13408
5bd4f169
AM
13409/* Finish up the dynamic sections. */
13410
b34976b6 13411static bfd_boolean
4ce794b7
AM
13412ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13413 struct bfd_link_info *info)
5bd4f169 13414{
65f38f15
AM
13415 struct ppc_link_hash_table *htab;
13416 bfd *dynobj;
5bd4f169 13417 asection *sdyn;
5bd4f169 13418
65f38f15 13419 htab = ppc_hash_table (info);
4dfe6ac6
NC
13420 if (htab == NULL)
13421 return FALSE;
13422
65f38f15 13423 dynobj = htab->elf.dynobj;
5bd4f169
AM
13424 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13425
65f38f15 13426 if (htab->elf.dynamic_sections_created)
5bd4f169 13427 {
5bd4f169
AM
13428 Elf64_External_Dyn *dyncon, *dynconend;
13429
4ce794b7 13430 if (sdyn == NULL || htab->got == NULL)
65f38f15 13431 abort ();
5bd4f169
AM
13432
13433 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 13434 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
13435 for (; dyncon < dynconend; dyncon++)
13436 {
13437 Elf_Internal_Dyn dyn;
19397422 13438 asection *s;
5bd4f169
AM
13439
13440 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13441
13442 switch (dyn.d_tag)
13443 {
65f38f15
AM
13444 default:
13445 continue;
5bd4f169 13446
5d1634d7 13447 case DT_PPC64_GLINK:
4ce794b7 13448 s = htab->glink;
6348e046 13449 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
13450 /* We stupidly defined DT_PPC64_GLINK to be the start
13451 of glink rather than the first entry point, which is
13452 what ld.so needs, and now have a bigger stub to
13453 support automatic multiple TOCs. */
13454 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
13455 break;
13456
19397422
AM
13457 case DT_PPC64_OPD:
13458 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13459 if (s == NULL)
13460 continue;
13461 dyn.d_un.d_ptr = s->vma;
19397422
AM
13462 break;
13463
13464 case DT_PPC64_OPDSZ:
13465 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13466 if (s == NULL)
13467 continue;
eea6121a 13468 dyn.d_un.d_val = s->size;
19397422
AM
13469 break;
13470
65f38f15 13471 case DT_PLTGOT:
4ce794b7 13472 s = htab->plt;
6348e046 13473 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
13474 break;
13475
13476 case DT_JMPREL:
4ce794b7 13477 s = htab->relplt;
6348e046 13478 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 13479 break;
5bd4f169 13480
65f38f15 13481 case DT_PLTRELSZ:
eea6121a 13482 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
13483 break;
13484
13485 case DT_RELASZ:
13486 /* Don't count procedure linkage table relocs in the
13487 overall reloc count. */
4ce794b7 13488 s = htab->relplt;
6348e046
AM
13489 if (s == NULL)
13490 continue;
eea6121a 13491 dyn.d_un.d_val -= s->size;
6348e046
AM
13492 break;
13493
13494 case DT_RELA:
13495 /* We may not be using the standard ELF linker script.
13496 If .rela.plt is the first .rela section, we adjust
13497 DT_RELA to not include it. */
4ce794b7 13498 s = htab->relplt;
6348e046
AM
13499 if (s == NULL)
13500 continue;
13501 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13502 continue;
eea6121a 13503 dyn.d_un.d_ptr += s->size;
65f38f15 13504 break;
5bd4f169 13505 }
5bd4f169 13506
65f38f15 13507 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 13508 }
5bd4f169
AM
13509 }
13510
eea6121a 13511 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
13512 {
13513 /* Fill in the first entry in the global offset table.
13514 We use it to hold the link-time TOCbase. */
13515 bfd_put_64 (output_bfd,
60ee0d4a 13516 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 13517 htab->got->contents);
5d1634d7
AM
13518
13519 /* Set .got entry size. */
4ce794b7 13520 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
13521 }
13522
eea6121a 13523 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
13524 {
13525 /* Set .plt entry size. */
4ce794b7 13526 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
13527 = PLT_ENTRY_SIZE;
13528 }
13529
84f5d08e
AM
13530 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13531 brlt ourselves if emitrelocations. */
13532 if (htab->brlt != NULL
13533 && htab->brlt->reloc_count != 0
13534 && !_bfd_elf_link_output_relocs (output_bfd,
13535 htab->brlt,
d4730f92 13536 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
13537 elf_section_data (htab->brlt)->relocs,
13538 NULL))
13539 return FALSE;
13540
176a0d42
AM
13541 if (htab->glink != NULL
13542 && htab->glink->reloc_count != 0
13543 && !_bfd_elf_link_output_relocs (output_bfd,
13544 htab->glink,
d4730f92 13545 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
13546 elf_section_data (htab->glink)->relocs,
13547 NULL))
13548 return FALSE;
13549
e717da7e 13550 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
13551 since we didn't add them to DYNOBJ. We know dynobj is the first
13552 bfd. */
e717da7e
AM
13553 while ((dynobj = dynobj->link_next) != NULL)
13554 {
13555 asection *s;
7b53ace3 13556
0c8d6e5c 13557 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
13558 continue;
13559
e717da7e
AM
13560 s = ppc64_elf_tdata (dynobj)->got;
13561 if (s != NULL
eea6121a 13562 && s->size != 0
e717da7e
AM
13563 && s->output_section != bfd_abs_section_ptr
13564 && !bfd_set_section_contents (output_bfd, s->output_section,
13565 s->contents, s->output_offset,
eea6121a 13566 s->size))
e717da7e
AM
13567 return FALSE;
13568 s = ppc64_elf_tdata (dynobj)->relgot;
13569 if (s != NULL
eea6121a 13570 && s->size != 0
e717da7e
AM
13571 && s->output_section != bfd_abs_section_ptr
13572 && !bfd_set_section_contents (output_bfd, s->output_section,
13573 s->contents, s->output_offset,
eea6121a 13574 s->size))
e717da7e
AM
13575 return FALSE;
13576 }
f6c52c13 13577
b34976b6 13578 return TRUE;
5bd4f169
AM
13579}
13580
5bd4f169 13581#include "elf64-target.h"
This page took 1.808241 seconds and 4 git commands to generate.