]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf64-ppc.c
*** empty log message ***
[thirdparty/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
3a71aa26 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
df22d223 3 2009, 2010, 2011 Free Software Foundation, Inc.
5bd4f169
AM
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 6 Largely rewritten by Alan Modra.
5bd4f169 7
ae9a127f 8 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 9
ae9a127f
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
ae9a127f 13 (at your option) any later version.
5bd4f169 14
ae9a127f
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
5bd4f169 19
4ce794b7
AM
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 23
cd123cb7 24
4ce794b7
AM
25/* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 28
3db64b00 29#include "sysdep.h"
183e98be 30#include <stdarg.h>
5bd4f169 31#include "bfd.h"
5bd4f169
AM
32#include "bfdlink.h"
33#include "libbfd.h"
34#include "elf-bfd.h"
04c9666a 35#include "elf/ppc64.h"
5d1634d7 36#include "elf64-ppc.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
56static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *);
5bd4f169 58
ad8e1ba5
AM
59#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
60#define TARGET_LITTLE_NAME "elf64-powerpcle"
61#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
24718e3b 67#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
ad8e1ba5 74#define elf_backend_got_header_size 8
ad8e1ba5
AM
75#define elf_backend_can_gc_sections 1
76#define elf_backend_can_refcount 1
77#define elf_backend_rela_normal 1
6bfdb61b 78#define elf_backend_default_execstack 0
ad8e1ba5 79
e717da7e 80#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 81#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
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
9df0ef5f
AM
3737 /* Set if PLT call stubs should load r11. */
3738 unsigned int plt_static_chain:1;
3739
ad8e1ba5 3740 /* Set if we should emit symbols for stubs. */
99877b66 3741 unsigned int emit_stub_syms:1;
ad8e1ba5 3742
a7f2871e
AM
3743 /* Set if __tls_get_addr optimization should not be done. */
3744 unsigned int no_tls_get_addr_opt:1;
3745
4c52953f 3746 /* Support for multiple toc sections. */
33c0ec9d 3747 unsigned int do_multi_toc:1;
4c52953f 3748 unsigned int multi_toc_needed:1;
927be08e 3749 unsigned int second_toc_pass:1;
67f0cbdb 3750 unsigned int do_toc_opt:1;
4c52953f 3751
5d1634d7 3752 /* Set on error. */
99877b66 3753 unsigned int stub_error:1;
721956f4 3754
7d9616d7 3755 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3756 unsigned int twiddled_syms:1;
721956f4
AM
3757
3758 /* Incremented every time we size stubs. */
3759 unsigned int stub_iteration;
5d1634d7 3760
87d72d41
AM
3761 /* Small local sym cache. */
3762 struct sym_cache sym_cache;
65f38f15
AM
3763};
3764
4c52953f
AM
3765/* Rename some of the generic section flags to better document how they
3766 are used here. */
b0dddeec
AM
3767
3768/* Nonzero if this section has TLS related relocations. */
3769#define has_tls_reloc sec_flg0
3770
3771/* Nonzero if this section has a call to __tls_get_addr. */
3772#define has_tls_get_addr_call sec_flg1
3773
3774/* Nonzero if this section has any toc or got relocs. */
3775#define has_toc_reloc sec_flg2
3776
3777/* Nonzero if this section has a call to another section that uses
3778 the toc or got. */
d77c8a4b 3779#define makes_toc_func_call sec_flg3
b0dddeec
AM
3780
3781/* Recursion protection when determining above flag. */
d77c8a4b 3782#define call_check_in_progress sec_flg4
70cc837d 3783#define call_check_done sec_flg5
4c52953f 3784
65f38f15
AM
3785/* Get the ppc64 ELF linker hash table from a link_info structure. */
3786
3787#define ppc_hash_table(p) \
4dfe6ac6
NC
3788 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3789 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3790
721956f4
AM
3791#define ppc_stub_hash_lookup(table, string, create, copy) \
3792 ((struct ppc_stub_hash_entry *) \
3793 bfd_hash_lookup ((table), (string), (create), (copy)))
3794
3795#define ppc_branch_hash_lookup(table, string, create, copy) \
3796 ((struct ppc_branch_hash_entry *) \
3797 bfd_hash_lookup ((table), (string), (create), (copy)))
3798
3799/* Create an entry in the stub hash table. */
3800
3801static struct bfd_hash_entry *
4ce794b7
AM
3802stub_hash_newfunc (struct bfd_hash_entry *entry,
3803 struct bfd_hash_table *table,
3804 const char *string)
721956f4
AM
3805{
3806 /* Allocate the structure if it has not already been allocated by a
3807 subclass. */
3808 if (entry == NULL)
3809 {
3810 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3811 if (entry == NULL)
3812 return entry;
3813 }
3814
3815 /* Call the allocation method of the superclass. */
3816 entry = bfd_hash_newfunc (entry, table, string);
3817 if (entry != NULL)
3818 {
3819 struct ppc_stub_hash_entry *eh;
3820
3821 /* Initialize the local fields. */
3822 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3823 eh->stub_type = ppc_stub_none;
721956f4
AM
3824 eh->stub_sec = NULL;
3825 eh->stub_offset = 0;
3826 eh->target_value = 0;
3827 eh->target_section = NULL;
721956f4
AM
3828 eh->h = NULL;
3829 eh->id_sec = NULL;
3830 }
3831
3832 return entry;
3833}
3834
3835/* Create an entry in the branch hash table. */
3836
3837static struct bfd_hash_entry *
4ce794b7
AM
3838branch_hash_newfunc (struct bfd_hash_entry *entry,
3839 struct bfd_hash_table *table,
3840 const char *string)
721956f4
AM
3841{
3842 /* Allocate the structure if it has not already been allocated by a
3843 subclass. */
3844 if (entry == NULL)
3845 {
3846 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3847 if (entry == NULL)
3848 return entry;
3849 }
3850
3851 /* Call the allocation method of the superclass. */
3852 entry = bfd_hash_newfunc (entry, table, string);
3853 if (entry != NULL)
3854 {
3855 struct ppc_branch_hash_entry *eh;
3856
3857 /* Initialize the local fields. */
3858 eh = (struct ppc_branch_hash_entry *) entry;
3859 eh->offset = 0;
3860 eh->iter = 0;
3861 }
3862
3863 return entry;
3864}
3865
65f38f15
AM
3866/* Create an entry in a ppc64 ELF linker hash table. */
3867
3868static struct bfd_hash_entry *
4ce794b7
AM
3869link_hash_newfunc (struct bfd_hash_entry *entry,
3870 struct bfd_hash_table *table,
3871 const char *string)
65f38f15
AM
3872{
3873 /* Allocate the structure if it has not already been allocated by a
3874 subclass. */
3875 if (entry == NULL)
3876 {
3877 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3878 if (entry == NULL)
3879 return entry;
3880 }
3881
3882 /* Call the allocation method of the superclass. */
3883 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3884 if (entry != NULL)
3885 {
3886 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3887
b3fac117 3888 memset (&eh->u.stub_cache, 0,
908b32fc 3889 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3890 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3891
3892 /* When making function calls, old ABI code references function entry
3893 points (dot symbols), while new ABI code references the function
3894 descriptor symbol. We need to make any combination of reference and
3895 definition work together, without breaking archive linking.
3896
3897 For a defined function "foo" and an undefined call to "bar":
3898 An old object defines "foo" and ".foo", references ".bar" (possibly
3899 "bar" too).
3900 A new object defines "foo" and references "bar".
3901
3902 A new object thus has no problem with its undefined symbols being
3903 satisfied by definitions in an old object. On the other hand, the
3904 old object won't have ".bar" satisfied by a new object.
3905
3906 Keep a list of newly added dot-symbols. */
3907
3908 if (string[0] == '.')
3909 {
3910 struct ppc_link_hash_table *htab;
3911
3912 htab = (struct ppc_link_hash_table *) table;
3913 eh->u.next_dot_sym = htab->dot_syms;
3914 htab->dot_syms = eh;
3915 }
65f38f15
AM
3916 }
3917
3918 return entry;
3919}
3920
3921/* Create a ppc64 ELF linker hash table. */
3922
3923static struct bfd_link_hash_table *
4ce794b7 3924ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3925{
3926 struct ppc_link_hash_table *htab;
3927 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3928
4ce794b7 3929 htab = bfd_zmalloc (amt);
65f38f15
AM
3930 if (htab == NULL)
3931 return NULL;
3932
66eb6687 3933 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3934 sizeof (struct ppc_link_hash_entry),
3935 PPC64_ELF_DATA))
65f38f15 3936 {
e2d34d7d 3937 free (htab);
65f38f15
AM
3938 return NULL;
3939 }
3940
721956f4 3941 /* Init the stub hash table too. */
66eb6687
AM
3942 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3943 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
3944 return NULL;
3945
3946 /* And the branch hash table. */
66eb6687
AM
3947 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3948 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
3949 return NULL;
3950
3254fd24
AM
3951 /* Initializing two fields of the union is just cosmetic. We really
3952 only care about glist, but when compiled on a 32-bit host the
3953 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3954 debugger inspection of these fields look nicer. */
a6aa5195
AM
3955 htab->elf.init_got_refcount.refcount = 0;
3956 htab->elf.init_got_refcount.glist = NULL;
3957 htab->elf.init_plt_refcount.refcount = 0;
3958 htab->elf.init_plt_refcount.glist = NULL;
3959 htab->elf.init_got_offset.offset = 0;
3960 htab->elf.init_got_offset.glist = NULL;
3961 htab->elf.init_plt_offset.offset = 0;
3962 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3963
65f38f15
AM
3964 return &htab->elf.root;
3965}
3966
721956f4
AM
3967/* Free the derived linker hash table. */
3968
3969static void
4ce794b7 3970ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4
AM
3971{
3972 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3973
3974 bfd_hash_table_free (&ret->stub_hash_table);
3975 bfd_hash_table_free (&ret->branch_hash_table);
3976 _bfd_generic_link_hash_table_free (hash);
3977}
3978
e717da7e
AM
3979/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3980
3981void
3982ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3983{
3984 struct ppc_link_hash_table *htab;
3985
3986 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3987
3988/* Always hook our dynamic sections into the first bfd, which is the
3989 linker created stub bfd. This ensures that the GOT header is at
3990 the start of the output TOC section. */
3991 htab = ppc_hash_table (info);
4dfe6ac6
NC
3992 if (htab == NULL)
3993 return;
e717da7e
AM
3994 htab->stub_bfd = abfd;
3995 htab->elf.dynobj = abfd;
3996}
3997
721956f4
AM
3998/* Build a name for an entry in the stub hash table. */
3999
4000static char *
4ce794b7
AM
4001ppc_stub_name (const asection *input_section,
4002 const asection *sym_sec,
4003 const struct ppc_link_hash_entry *h,
4004 const Elf_Internal_Rela *rel)
721956f4
AM
4005{
4006 char *stub_name;
4007 bfd_size_type len;
4008
4009 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4010 offsets from a sym as a branch target? In fact, we could
4011 probably assume the addend is always zero. */
4012 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4013
4014 if (h)
4015 {
4016 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4017 stub_name = bfd_malloc (len);
46de2a7c
AM
4018 if (stub_name == NULL)
4019 return stub_name;
4020
4021 sprintf (stub_name, "%08x.%s+%x",
4022 input_section->id & 0xffffffff,
4023 h->elf.root.root.string,
4024 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4025 }
4026 else
4027 {
ad8e1ba5 4028 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4029 stub_name = bfd_malloc (len);
46de2a7c
AM
4030 if (stub_name == NULL)
4031 return stub_name;
4032
4033 sprintf (stub_name, "%08x.%x:%x+%x",
4034 input_section->id & 0xffffffff,
4035 sym_sec->id & 0xffffffff,
4036 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4037 (int) rel->r_addend & 0xffffffff);
721956f4 4038 }
ee75fd95
AM
4039 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4040 stub_name[len - 2] = 0;
721956f4
AM
4041 return stub_name;
4042}
4043
4044/* Look up an entry in the stub hash. Stub entries are cached because
4045 creating the stub name takes a bit of time. */
4046
4047static struct ppc_stub_hash_entry *
4ce794b7
AM
4048ppc_get_stub_entry (const asection *input_section,
4049 const asection *sym_sec,
039b3fef 4050 struct ppc_link_hash_entry *h,
4ce794b7
AM
4051 const Elf_Internal_Rela *rel,
4052 struct ppc_link_hash_table *htab)
721956f4
AM
4053{
4054 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4055 const asection *id_sec;
4056
4057 /* If this input section is part of a group of sections sharing one
4058 stub section, then use the id of the first section in the group.
4059 Stub names need to include a section id, as there may well be
4060 more than one stub used to reach say, printf, and we need to
4061 distinguish between them. */
4062 id_sec = htab->stub_group[input_section->id].link_sec;
4063
b3fac117
AM
4064 if (h != NULL && h->u.stub_cache != NULL
4065 && h->u.stub_cache->h == h
4066 && h->u.stub_cache->id_sec == id_sec)
721956f4 4067 {
b3fac117 4068 stub_entry = h->u.stub_cache;
721956f4
AM
4069 }
4070 else
4071 {
4072 char *stub_name;
4073
4074 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4075 if (stub_name == NULL)
4076 return NULL;
4077
4078 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4079 stub_name, FALSE, FALSE);
721956f4 4080 if (h != NULL)
b3fac117 4081 h->u.stub_cache = stub_entry;
721956f4
AM
4082
4083 free (stub_name);
4084 }
4085
4086 return stub_entry;
4087}
4088
4089/* Add a new stub entry to the stub hash. Not all fields of the new
4090 stub entry are initialised. */
4091
4092static struct ppc_stub_hash_entry *
4ce794b7
AM
4093ppc_add_stub (const char *stub_name,
4094 asection *section,
25f53a85 4095 struct bfd_link_info *info)
721956f4 4096{
25f53a85 4097 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4
AM
4098 asection *link_sec;
4099 asection *stub_sec;
4100 struct ppc_stub_hash_entry *stub_entry;
4101
4102 link_sec = htab->stub_group[section->id].link_sec;
4103 stub_sec = htab->stub_group[section->id].stub_sec;
4104 if (stub_sec == NULL)
4105 {
4106 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4107 if (stub_sec == NULL)
4108 {
d4c88bbb 4109 size_t namelen;
721956f4
AM
4110 bfd_size_type len;
4111 char *s_name;
4112
d4c88bbb
AM
4113 namelen = strlen (link_sec->name);
4114 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4115 s_name = bfd_alloc (htab->stub_bfd, len);
4116 if (s_name == NULL)
4117 return NULL;
4118
d4c88bbb
AM
4119 memcpy (s_name, link_sec->name, namelen);
4120 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4121 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4122 if (stub_sec == NULL)
4123 return NULL;
4124 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4125 }
4126 htab->stub_group[section->id].stub_sec = stub_sec;
4127 }
4128
4129 /* Enter this entry into the linker stub hash table. */
4130 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4131 TRUE, FALSE);
721956f4
AM
4132 if (stub_entry == NULL)
4133 {
25f53a85
AM
4134 info->callbacks->einfo (_("%B: cannot create stub entry %s\n"),
4135 section->owner, stub_name);
721956f4
AM
4136 return NULL;
4137 }
4138
4139 stub_entry->stub_sec = stub_sec;
4140 stub_entry->stub_offset = 0;
4141 stub_entry->id_sec = link_sec;
4142 return stub_entry;
4143}
4144
82bd7b59
AM
4145/* Create sections for linker generated code. */
4146
b34976b6 4147static bfd_boolean
4ce794b7 4148create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
4149{
4150 struct ppc_link_hash_table *htab;
4151 flagword flags;
4152
4153 htab = ppc_hash_table (info);
4dfe6ac6
NC
4154 if (htab == NULL)
4155 return FALSE;
82bd7b59
AM
4156
4157 /* Create .sfpr for code to save and restore fp regs. */
4158 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4159 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
4160 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4161 flags);
82bd7b59 4162 if (htab->sfpr == NULL
82bd7b59 4163 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 4164 return FALSE;
82bd7b59 4165
721956f4 4166 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
4167 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4168 flags);
4ce794b7 4169 if (htab->glink == NULL
ee4bf8d2 4170 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 4171 return FALSE;
82bd7b59 4172
e054468f
AM
4173 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4174 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4175 if (htab->iplt == NULL
4176 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4177 return FALSE;
4178
4179 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4180 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4181 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4182 ".rela.iplt",
4183 flags);
4184 if (htab->reliplt == NULL
4185 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4186 return FALSE;
4187
c456f082 4188 /* Create branch lookup table for plt_branch stubs. */
e4e0193e
AM
4189 flags = (SEC_ALLOC | SEC_LOAD
4190 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4191 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4192 flags);
4ce794b7 4193 if (htab->brlt == NULL
4ce794b7 4194 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 4195 return FALSE;
721956f4 4196
e4e0193e 4197 if (!info->shared)
c456f082
AM
4198 return TRUE;
4199
e4e0193e
AM
4200 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4201 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4202 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4203 ".rela.branch_lt",
4204 flags);
e054468f 4205 if (htab->relbrlt == NULL
c456f082
AM
4206 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4207 return FALSE;
4208
b34976b6 4209 return TRUE;
82bd7b59
AM
4210}
4211
e717da7e
AM
4212/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4213 not already done. */
65f38f15 4214
b34976b6 4215static bfd_boolean
e717da7e 4216create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4217{
e717da7e
AM
4218 asection *got, *relgot;
4219 flagword flags;
4220 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4221
0c8d6e5c 4222 if (!is_ppc64_elf (abfd))
0ffa91dd 4223 return FALSE;
4dfe6ac6
NC
4224 if (htab == NULL)
4225 return FALSE;
0ffa91dd 4226
4ce794b7 4227 if (!htab->got)
e717da7e
AM
4228 {
4229 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4230 return FALSE;
4231
4232 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4233 if (!htab->got)
4234 abort ();
4235 }
4236
4237 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4238 | SEC_LINKER_CREATED);
4239
c456f082 4240 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4241 if (!got
e717da7e
AM
4242 || !bfd_set_section_alignment (abfd, got, 3))
4243 return FALSE;
65f38f15 4244
c456f082
AM
4245 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4246 flags | SEC_READONLY);
e717da7e 4247 if (!relgot
e717da7e 4248 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4249 return FALSE;
e717da7e
AM
4250
4251 ppc64_elf_tdata (abfd)->got = got;
4252 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4253 return TRUE;
65f38f15 4254}
5bd4f169 4255
82bd7b59 4256/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4257
b34976b6 4258static bfd_boolean
4ce794b7 4259ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4260{
65f38f15 4261 struct ppc_link_hash_table *htab;
5bd4f169 4262
65f38f15 4263 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4264 return FALSE;
65f38f15 4265
e717da7e 4266 htab = ppc_hash_table (info);
4dfe6ac6
NC
4267 if (htab == NULL)
4268 return FALSE;
4269
e717da7e
AM
4270 if (!htab->got)
4271 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
4272 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4273 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4274 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 4275 if (!info->shared)
4ce794b7 4276 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 4277
e717da7e 4278 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4279 || (!info->shared && !htab->relbss))
65f38f15
AM
4280 abort ();
4281
b34976b6 4282 return TRUE;
5bd4f169
AM
4283}
4284
b31867b6
AM
4285/* Follow indirect and warning symbol links. */
4286
4287static inline struct bfd_link_hash_entry *
4288follow_link (struct bfd_link_hash_entry *h)
4289{
4290 while (h->type == bfd_link_hash_indirect
4291 || h->type == bfd_link_hash_warning)
4292 h = h->u.i.link;
4293 return h;
4294}
4295
4296static inline struct elf_link_hash_entry *
4297elf_follow_link (struct elf_link_hash_entry *h)
4298{
4299 return (struct elf_link_hash_entry *) follow_link (&h->root);
4300}
4301
4302static inline struct ppc_link_hash_entry *
4303ppc_follow_link (struct ppc_link_hash_entry *h)
4304{
4305 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4306}
4307
40d16e0b
AM
4308/* Merge PLT info on FROM with that on TO. */
4309
4310static void
4311move_plt_plist (struct ppc_link_hash_entry *from,
4312 struct ppc_link_hash_entry *to)
4313{
4314 if (from->elf.plt.plist != NULL)
4315 {
4316 if (to->elf.plt.plist != NULL)
4317 {
4318 struct plt_entry **entp;
4319 struct plt_entry *ent;
4320
4321 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4322 {
4323 struct plt_entry *dent;
4324
4325 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4326 if (dent->addend == ent->addend)
4327 {
4328 dent->plt.refcount += ent->plt.refcount;
4329 *entp = ent->next;
4330 break;
4331 }
4332 if (dent == NULL)
4333 entp = &ent->next;
4334 }
4335 *entp = to->elf.plt.plist;
4336 }
4337
4338 to->elf.plt.plist = from->elf.plt.plist;
4339 from->elf.plt.plist = NULL;
4340 }
4341}
4342
65f38f15
AM
4343/* Copy the extra info we tack onto an elf_link_hash_entry. */
4344
4345static void
fcfa13d2
AM
4346ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4347 struct elf_link_hash_entry *dir,
4348 struct elf_link_hash_entry *ind)
65f38f15
AM
4349{
4350 struct ppc_link_hash_entry *edir, *eind;
4351
4352 edir = (struct ppc_link_hash_entry *) dir;
4353 eind = (struct ppc_link_hash_entry *) ind;
4354
c79d6685
AM
4355 edir->is_func |= eind->is_func;
4356 edir->is_func_descriptor |= eind->is_func_descriptor;
4357 edir->tls_mask |= eind->tls_mask;
4358 if (eind->oh != NULL)
4359 edir->oh = ppc_follow_link (eind->oh);
4360
4361 /* If called to transfer flags for a weakdef during processing
4362 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4363 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4364 if (!(ELIMINATE_COPY_RELOCS
4365 && eind->elf.root.type != bfd_link_hash_indirect
4366 && edir->elf.dynamic_adjusted))
4367 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4368
4369 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4370 edir->elf.ref_regular |= eind->elf.ref_regular;
4371 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4372 edir->elf.needs_plt |= eind->elf.needs_plt;
4373
4374 /* If we were called to copy over info for a weak sym, that's all. */
4375 if (eind->elf.root.type != bfd_link_hash_indirect)
4376 return;
4377
411e1bfb 4378 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4379 if (eind->dyn_relocs != NULL)
65f38f15 4380 {
bbd7ec4a
AM
4381 if (edir->dyn_relocs != NULL)
4382 {
6061a67d
AM
4383 struct elf_dyn_relocs **pp;
4384 struct elf_dyn_relocs *p;
bbd7ec4a 4385
fcfa13d2 4386 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4387 list. Merge any entries against the same section. */
4388 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4389 {
6061a67d 4390 struct elf_dyn_relocs *q;
bbd7ec4a
AM
4391
4392 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4393 if (q->sec == p->sec)
4394 {
4395 q->pc_count += p->pc_count;
4396 q->count += p->count;
4397 *pp = p->next;
4398 break;
4399 }
4400 if (q == NULL)
4401 pp = &p->next;
4402 }
4403 *pp = edir->dyn_relocs;
4404 }
4405
65f38f15
AM
4406 edir->dyn_relocs = eind->dyn_relocs;
4407 eind->dyn_relocs = NULL;
4408 }
65f38f15 4409
81848ca0
AM
4410 /* Copy over got entries that we may have already seen to the
4411 symbol which just became indirect. */
411e1bfb
AM
4412 if (eind->elf.got.glist != NULL)
4413 {
4414 if (edir->elf.got.glist != NULL)
4415 {
4416 struct got_entry **entp;
4417 struct got_entry *ent;
4418
4419 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4420 {
4421 struct got_entry *dent;
4422
4423 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4424 if (dent->addend == ent->addend
e717da7e 4425 && dent->owner == ent->owner
411e1bfb
AM
4426 && dent->tls_type == ent->tls_type)
4427 {
4428 dent->got.refcount += ent->got.refcount;
4429 *entp = ent->next;
4430 break;
4431 }
4432 if (dent == NULL)
4433 entp = &ent->next;
4434 }
4435 *entp = edir->elf.got.glist;
4436 }
4437
4438 edir->elf.got.glist = eind->elf.got.glist;
4439 eind->elf.got.glist = NULL;
4440 }
4441
4442 /* And plt entries. */
40d16e0b 4443 move_plt_plist (eind, edir);
411e1bfb 4444
fcfa13d2 4445 if (eind->elf.dynindx != -1)
411e1bfb 4446 {
fcfa13d2
AM
4447 if (edir->elf.dynindx != -1)
4448 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4449 edir->elf.dynstr_index);
411e1bfb
AM
4450 edir->elf.dynindx = eind->elf.dynindx;
4451 edir->elf.dynstr_index = eind->elf.dynstr_index;
4452 eind->elf.dynindx = -1;
4453 eind->elf.dynstr_index = 0;
4454 }
411e1bfb
AM
4455}
4456
8387904d
AM
4457/* Find the function descriptor hash entry from the given function code
4458 hash entry FH. Link the entries via their OH fields. */
4459
4460static struct ppc_link_hash_entry *
b31867b6 4461lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4462{
4463 struct ppc_link_hash_entry *fdh = fh->oh;
4464
4465 if (fdh == NULL)
4466 {
4467 const char *fd_name = fh->elf.root.root.string + 1;
4468
4469 fdh = (struct ppc_link_hash_entry *)
4470 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4471 if (fdh == NULL)
4472 return fdh;
4473
4474 fdh->is_func_descriptor = 1;
4475 fdh->oh = fh;
4476 fh->is_func = 1;
4477 fh->oh = fdh;
8387904d
AM
4478 }
4479
b31867b6 4480 return ppc_follow_link (fdh);
8387904d
AM
4481}
4482
bb700d78
AM
4483/* Make a fake function descriptor sym for the code sym FH. */
4484
4485static struct ppc_link_hash_entry *
4486make_fdh (struct bfd_link_info *info,
908b32fc 4487 struct ppc_link_hash_entry *fh)
bb700d78
AM
4488{
4489 bfd *abfd;
4490 asymbol *newsym;
4491 struct bfd_link_hash_entry *bh;
4492 struct ppc_link_hash_entry *fdh;
4493
4494 abfd = fh->elf.root.u.undef.abfd;
4495 newsym = bfd_make_empty_symbol (abfd);
4496 newsym->name = fh->elf.root.root.string + 1;
4497 newsym->section = bfd_und_section_ptr;
4498 newsym->value = 0;
908b32fc 4499 newsym->flags = BSF_WEAK;
bb700d78
AM
4500
4501 bh = NULL;
4502 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4503 newsym->flags, newsym->section,
4504 newsym->value, NULL, FALSE, FALSE,
4505 &bh))
4506 return NULL;
4507
4508 fdh = (struct ppc_link_hash_entry *) bh;
4509 fdh->elf.non_elf = 0;
908b32fc
AM
4510 fdh->fake = 1;
4511 fdh->is_func_descriptor = 1;
4512 fdh->oh = fh;
4513 fh->is_func = 1;
4514 fh->oh = fdh;
bb700d78
AM
4515 return fdh;
4516}
4517
8387904d
AM
4518/* Fix function descriptor symbols defined in .opd sections to be
4519 function type. */
555cd476
AM
4520
4521static bfd_boolean
c16153ae 4522ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4523 struct bfd_link_info *info,
555cd476 4524 Elf_Internal_Sym *isym,
b3fac117 4525 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4526 flagword *flags ATTRIBUTE_UNUSED,
4527 asection **sec,
4528 bfd_vma *value ATTRIBUTE_UNUSED)
4529{
f64b2e8d
NC
4530 if ((ibfd->flags & DYNAMIC) == 0
4531 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4532 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4533
e054468f 4534 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4535 {
4536 if ((ibfd->flags & DYNAMIC) == 0)
f64b2e8d 4537 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
c16153ae 4538 }
e054468f
AM
4539 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4540 ;
4541 else if (*sec != NULL
70cc837d 4542 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4543 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4544
555cd476
AM
4545 return TRUE;
4546}
4547
8387904d 4548/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4549 inclusion of a new ABI object archive that defines "bar".
4550 NAME is a symbol defined in an archive. Return a symbol in the hash
4551 table that might be satisfied by the archive symbols. */
8387904d
AM
4552
4553static struct elf_link_hash_entry *
4554ppc64_elf_archive_symbol_lookup (bfd *abfd,
4555 struct bfd_link_info *info,
4556 const char *name)
4557{
4558 struct elf_link_hash_entry *h;
4559 char *dot_name;
4560 size_t len;
4561
4562 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4563 if (h != NULL
4564 /* Don't return this sym if it is a fake function descriptor
4565 created by add_symbol_adjust. */
4566 && !(h->root.type == bfd_link_hash_undefweak
4567 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4568 return h;
4569
4570 if (name[0] == '.')
4571 return h;
4572
4573 len = strlen (name);
4574 dot_name = bfd_alloc (abfd, len + 2);
4575 if (dot_name == NULL)
4576 return (struct elf_link_hash_entry *) 0 - 1;
4577 dot_name[0] = '.';
4578 memcpy (dot_name + 1, name, len + 1);
4579 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4580 bfd_release (abfd, dot_name);
4581 return h;
4582}
4583
4584/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4585 new ABI object defines "bar". Well, at least, undefined dot symbols
4586 are made weak. This stops later archive searches from including an
4587 object if we already have a function descriptor definition. It also
35b0ce59
AM
4588 prevents the linker complaining about undefined symbols.
4589 We also check and correct mismatched symbol visibility here. The
4590 most restrictive visibility of the function descriptor and the
4591 function entry symbol is used. */
8387904d
AM
4592
4593static bfd_boolean
b3fac117 4594add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4595{
8387904d
AM
4596 struct ppc_link_hash_table *htab;
4597 struct ppc_link_hash_entry *fdh;
4598
b3fac117 4599 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4600 return TRUE;
4601
b3fac117
AM
4602 if (eh->elf.root.type == bfd_link_hash_warning)
4603 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4604
b3fac117
AM
4605 if (eh->elf.root.root.string[0] != '.')
4606 abort ();
8387904d 4607
b3fac117 4608 htab = ppc_hash_table (info);
4dfe6ac6
NC
4609 if (htab == NULL)
4610 return FALSE;
4611
b31867b6
AM
4612 fdh = lookup_fdh (eh, htab);
4613 if (fdh == NULL)
4614 {
4615 if (!info->relocatable
4616 && (eh->elf.root.type == bfd_link_hash_undefined
4617 || eh->elf.root.type == bfd_link_hash_undefweak)
4618 && eh->elf.ref_regular)
4619 {
4620 /* Make an undefweak function descriptor sym, which is enough to
4621 pull in an --as-needed shared lib, but won't cause link
4622 errors. Archives are handled elsewhere. */
4623 fdh = make_fdh (info, eh);
4624 if (fdh == NULL)
4625 return FALSE;
4626 fdh->elf.ref_regular = 1;
4627 }
bb700d78 4628 }
b31867b6 4629 else
8387904d 4630 {
35b0ce59
AM
4631 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4632 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4633 if (entry_vis < descr_vis)
4634 fdh->elf.other += entry_vis - descr_vis;
4635 else if (entry_vis > descr_vis)
4636 eh->elf.other += descr_vis - entry_vis;
4637
e87d886e
AM
4638 if ((fdh->elf.root.type == bfd_link_hash_defined
4639 || fdh->elf.root.type == bfd_link_hash_defweak)
4640 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4641 {
4642 eh->elf.root.type = bfd_link_hash_undefweak;
4643 eh->was_undefined = 1;
4644 htab->twiddled_syms = 1;
4645 }
8387904d 4646 }
99877b66 4647
8387904d
AM
4648 return TRUE;
4649}
4650
b3fac117
AM
4651/* Process list of dot-symbols we made in link_hash_newfunc. */
4652
8387904d 4653static bfd_boolean
7d9616d7 4654ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4655{
99877b66 4656 struct ppc_link_hash_table *htab;
b3fac117 4657 struct ppc_link_hash_entry **p, *eh;
433817dd 4658
0c8d6e5c 4659 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4660 return TRUE;
4dfe6ac6
NC
4661 htab = ppc_hash_table (info);
4662 if (htab == NULL)
4663 return FALSE;
35b0ce59 4664
0c8d6e5c 4665 if (is_ppc64_elf (ibfd))
b3fac117
AM
4666 {
4667 p = &htab->dot_syms;
4668 while ((eh = *p) != NULL)
4669 {
4670 *p = NULL;
4671 if (!add_symbol_adjust (eh, info))
4672 return FALSE;
4673 p = &eh->u.next_dot_sym;
4674 }
4675 }
4676
4677 /* Clear the list for non-ppc64 input files. */
4678 p = &htab->dot_syms;
4679 while ((eh = *p) != NULL)
4680 {
4681 *p = NULL;
4682 p = &eh->u.next_dot_sym;
4683 }
99877b66
AM
4684
4685 /* We need to fix the undefs list for any syms we have twiddled to
4686 undef_weak. */
4687 if (htab->twiddled_syms)
4688 {
77cfaee6 4689 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4690 htab->twiddled_syms = 0;
4691 }
b3fac117 4692 return TRUE;
8387904d
AM
4693}
4694
97fed1c9
JJ
4695/* Undo hash table changes when an --as-needed input file is determined
4696 not to be needed. */
4697
4698static bfd_boolean
4699ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4700 struct bfd_link_info *info)
4701{
4dfe6ac6
NC
4702 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4703
4704 if (htab == NULL)
4705 return FALSE;
4706
4707 htab->dot_syms = NULL;
97fed1c9
JJ
4708 return TRUE;
4709}
4710
aa374f67
AM
4711/* If --just-symbols against a final linked binary, then assume we need
4712 toc adjusting stubs when calling functions defined there. */
4713
4714static void
4715ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4716{
4717 if ((sec->flags & SEC_CODE) != 0
4718 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4719 && is_ppc64_elf (sec->owner))
4720 {
4721 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4722 if (got != NULL
4723 && got->size >= elf_backend_got_header_size
4724 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4725 sec->has_toc_reloc = 1;
4726 }
4727 _bfd_elf_link_just_syms (sec, info);
4728}
4729
e054468f 4730static struct plt_entry **
4ce794b7
AM
4731update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4732 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4733{
4734 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4735 struct plt_entry **local_plt;
f961d9dd 4736 unsigned char *local_got_tls_masks;
411e1bfb
AM
4737
4738 if (local_got_ents == NULL)
4739 {
4740 bfd_size_type size = symtab_hdr->sh_info;
4741
e054468f
AM
4742 size *= (sizeof (*local_got_ents)
4743 + sizeof (*local_plt)
4744 + sizeof (*local_got_tls_masks));
4ce794b7 4745 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4746 if (local_got_ents == NULL)
e054468f 4747 return NULL;
411e1bfb
AM
4748 elf_local_got_ents (abfd) = local_got_ents;
4749 }
4750
e054468f 4751 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4752 {
4753 struct got_entry *ent;
4754
4755 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4756 if (ent->addend == r_addend
4757 && ent->owner == abfd
4758 && ent->tls_type == tls_type)
411e1bfb
AM
4759 break;
4760 if (ent == NULL)
4761 {
4762 bfd_size_type amt = sizeof (*ent);
4ce794b7 4763 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4764 if (ent == NULL)
4765 return FALSE;
4766 ent->next = local_got_ents[r_symndx];
4767 ent->addend = r_addend;
e717da7e 4768 ent->owner = abfd;
411e1bfb 4769 ent->tls_type = tls_type;
927be08e 4770 ent->is_indirect = FALSE;
411e1bfb
AM
4771 ent->got.refcount = 0;
4772 local_got_ents[r_symndx] = ent;
4773 }
4774 ent->got.refcount += 1;
4775 }
4776
e054468f 4777 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4778 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 4779 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
4780
4781 return local_plt + r_symndx;
65f38f15
AM
4782}
4783
411e1bfb 4784static bfd_boolean
e054468f 4785update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4786{
411e1bfb 4787 struct plt_entry *ent;
1e2f5b6e 4788
e054468f 4789 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4790 if (ent->addend == addend)
4791 break;
4792 if (ent == NULL)
1e2f5b6e 4793 {
411e1bfb 4794 bfd_size_type amt = sizeof (*ent);
4ce794b7 4795 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4796 if (ent == NULL)
4797 return FALSE;
e054468f 4798 ent->next = *plist;
411e1bfb
AM
4799 ent->addend = addend;
4800 ent->plt.refcount = 0;
e054468f 4801 *plist = ent;
1e2f5b6e 4802 }
411e1bfb 4803 ent->plt.refcount += 1;
b34976b6 4804 return TRUE;
1e2f5b6e
AM
4805}
4806
e054468f
AM
4807static bfd_boolean
4808is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4809{
4810 return (r_type == R_PPC64_REL24
4811 || r_type == R_PPC64_REL14
4812 || r_type == R_PPC64_REL14_BRTAKEN
4813 || r_type == R_PPC64_REL14_BRNTAKEN
4814 || r_type == R_PPC64_ADDR24
4815 || r_type == R_PPC64_ADDR14
4816 || r_type == R_PPC64_ADDR14_BRTAKEN
4817 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4818}
4819
5bd4f169 4820/* Look through the relocs for a section during the first phase, and
65f38f15 4821 calculate needed space in the global offset table, procedure
5d1634d7 4822 linkage table, and dynamic reloc sections. */
5bd4f169 4823
b34976b6 4824static bfd_boolean
4ce794b7
AM
4825ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4826 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4827{
65f38f15 4828 struct ppc_link_hash_table *htab;
5bd4f169 4829 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4830 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4831 const Elf_Internal_Rela *rel;
4832 const Elf_Internal_Rela *rel_end;
5bd4f169 4833 asection *sreloc;
1e2f5b6e 4834 asection **opd_sym_map;
3a71aa26 4835 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 4836
1049f94e 4837 if (info->relocatable)
b34976b6 4838 return TRUE;
5bd4f169 4839
680a3378
AM
4840 /* Don't do anything special with non-loaded, non-alloced sections.
4841 In particular, any relocs in such sections should not affect GOT
4842 and PLT reference counting (ie. we don't allow them to create GOT
4843 or PLT entries), there's no possibility or desire to optimize TLS
4844 relocs, and there's not much point in propagating relocs to shared
4845 libs that the dynamic linker won't relocate. */
4846 if ((sec->flags & SEC_ALLOC) == 0)
4847 return TRUE;
4848
0c8d6e5c 4849 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4850
65f38f15 4851 htab = ppc_hash_table (info);
4dfe6ac6
NC
4852 if (htab == NULL)
4853 return FALSE;
4854
3a71aa26
AM
4855 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4856 FALSE, FALSE, TRUE);
4857 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4858 FALSE, FALSE, TRUE);
0ffa91dd 4859 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4860 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4861 sreloc = NULL;
1e2f5b6e 4862 opd_sym_map = NULL;
70cc837d 4863 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
4864 {
4865 /* Garbage collection needs some extra help with .opd sections.
4866 We don't want to necessarily keep everything referenced by
4867 relocs in .opd, as that would keep all functions. Instead,
4868 if we reference an .opd symbol (a function descriptor), we
4869 want to keep the function code symbol's section. This is
4870 easy for global symbols, but for local syms we need to keep
74f0fb50 4871 information about the associated function section. */
1e2f5b6e
AM
4872 bfd_size_type amt;
4873
74f0fb50 4874 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 4875 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4876 if (opd_sym_map == NULL)
b34976b6 4877 return FALSE;
74f0fb50 4878 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
4879 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4880 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4881 }
5bd4f169 4882
82bd7b59
AM
4883 if (htab->sfpr == NULL
4884 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4885 return FALSE;
82bd7b59 4886
5bd4f169
AM
4887 rel_end = relocs + sec->reloc_count;
4888 for (rel = relocs; rel < rel_end; rel++)
4889 {
4890 unsigned long r_symndx;
4891 struct elf_link_hash_entry *h;
04c9666a 4892 enum elf_ppc64_reloc_type r_type;
727fc41e 4893 int tls_type;
7c8fe5c4 4894 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 4895 struct plt_entry **ifunc;
5bd4f169
AM
4896
4897 r_symndx = ELF64_R_SYM (rel->r_info);
4898 if (r_symndx < symtab_hdr->sh_info)
4899 h = NULL;
4900 else
973a3492
L
4901 {
4902 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4903 h = elf_follow_link (h);
973a3492 4904 }
5bd4f169 4905
727fc41e 4906 tls_type = 0;
e054468f 4907 ifunc = NULL;
25f23106
AM
4908 if (h != NULL)
4909 {
4910 if (h->type == STT_GNU_IFUNC)
4911 {
4912 h->needs_plt = 1;
4913 ifunc = &h->plt.plist;
4914 }
4915 }
4916 else
4917 {
4918 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4919 abfd, r_symndx);
4920 if (isym == NULL)
4921 return FALSE;
4922
4923 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4924 {
4925 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4926 rel->r_addend, PLT_IFUNC);
4927 if (ifunc == NULL)
4928 return FALSE;
4929 }
4930 }
4ce794b7 4931 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
4932 if (is_branch_reloc (r_type))
4933 {
4934 if (h != NULL && (h == tga || h == dottga))
4935 {
4936 if (rel != relocs
4937 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4938 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4939 /* We have a new-style __tls_get_addr call with a marker
4940 reloc. */
4941 ;
4942 else
4943 /* Mark this section as having an old-style call. */
4944 sec->has_tls_get_addr_call = 1;
4945 }
727fc41e 4946
e054468f 4947 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
4948 if (ifunc != NULL
4949 && !update_plt_info (abfd, ifunc, rel->r_addend))
4950 return FALSE;
4951 }
727fc41e 4952
a33d1f77 4953 switch (r_type)
5bd4f169 4954 {
727fc41e
AM
4955 case R_PPC64_TLSGD:
4956 case R_PPC64_TLSLD:
4957 /* These special tls relocs tie a call to __tls_get_addr with
4958 its parameter symbol. */
4959 break;
4960
411e1bfb
AM
4961 case R_PPC64_GOT_TLSLD16:
4962 case R_PPC64_GOT_TLSLD16_LO:
4963 case R_PPC64_GOT_TLSLD16_HI:
4964 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 4965 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4966 goto dogottls;
4967
4968 case R_PPC64_GOT_TLSGD16:
4969 case R_PPC64_GOT_TLSGD16_LO:
4970 case R_PPC64_GOT_TLSGD16_HI:
4971 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4972 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4973 goto dogottls;
4974
4975 case R_PPC64_GOT_TPREL16_DS:
4976 case R_PPC64_GOT_TPREL16_LO_DS:
4977 case R_PPC64_GOT_TPREL16_HI:
4978 case R_PPC64_GOT_TPREL16_HA:
1d483afe 4979 if (!info->executable)
411e1bfb
AM
4980 info->flags |= DF_STATIC_TLS;
4981 tls_type = TLS_TLS | TLS_TPREL;
4982 goto dogottls;
4983
4984 case R_PPC64_GOT_DTPREL16_DS:
4985 case R_PPC64_GOT_DTPREL16_LO_DS:
4986 case R_PPC64_GOT_DTPREL16_HI:
4987 case R_PPC64_GOT_DTPREL16_HA:
4988 tls_type = TLS_TLS | TLS_DTPREL;
4989 dogottls:
4990 sec->has_tls_reloc = 1;
4991 /* Fall thru */
4992
5bd4f169 4993 case R_PPC64_GOT16:
5bd4f169 4994 case R_PPC64_GOT16_DS:
65f38f15
AM
4995 case R_PPC64_GOT16_HA:
4996 case R_PPC64_GOT16_HI:
4997 case R_PPC64_GOT16_LO:
5bd4f169 4998 case R_PPC64_GOT16_LO_DS:
65f38f15 4999 /* This symbol requires a global offset table entry. */
4c52953f 5000 sec->has_toc_reloc = 1;
33c0ec9d
AM
5001 if (r_type == R_PPC64_GOT_TLSLD16
5002 || r_type == R_PPC64_GOT_TLSGD16
5003 || r_type == R_PPC64_GOT_TPREL16_DS
5004 || r_type == R_PPC64_GOT_DTPREL16_DS
5005 || r_type == R_PPC64_GOT16
5006 || r_type == R_PPC64_GOT16_DS)
5007 {
5008 htab->do_multi_toc = 1;
d77c8a4b 5009 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5010 }
5011
e717da7e
AM
5012 if (ppc64_elf_tdata (abfd)->got == NULL
5013 && !create_got_section (abfd, info))
b34976b6 5014 return FALSE;
5bd4f169
AM
5015
5016 if (h != NULL)
5017 {
411e1bfb
AM
5018 struct ppc_link_hash_entry *eh;
5019 struct got_entry *ent;
65f38f15 5020
411e1bfb
AM
5021 eh = (struct ppc_link_hash_entry *) h;
5022 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5023 if (ent->addend == rel->r_addend
e717da7e 5024 && ent->owner == abfd
411e1bfb
AM
5025 && ent->tls_type == tls_type)
5026 break;
5027 if (ent == NULL)
5bd4f169 5028 {
411e1bfb 5029 bfd_size_type amt = sizeof (*ent);
4ce794b7 5030 ent = bfd_alloc (abfd, amt);
411e1bfb 5031 if (ent == NULL)
b34976b6 5032 return FALSE;
411e1bfb
AM
5033 ent->next = eh->elf.got.glist;
5034 ent->addend = rel->r_addend;
e717da7e 5035 ent->owner = abfd;
411e1bfb 5036 ent->tls_type = tls_type;
927be08e 5037 ent->is_indirect = FALSE;
411e1bfb
AM
5038 ent->got.refcount = 0;
5039 eh->elf.got.glist = ent;
5bd4f169 5040 }
411e1bfb 5041 ent->got.refcount += 1;
e7b938ca 5042 eh->tls_mask |= tls_type;
5bd4f169 5043 }
411e1bfb
AM
5044 else
5045 /* This is a global offset table entry for a local symbol. */
5046 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5047 rel->r_addend, tls_type))
5048 return FALSE;
5bd4f169
AM
5049 break;
5050
5bd4f169 5051 case R_PPC64_PLT16_HA:
65f38f15
AM
5052 case R_PPC64_PLT16_HI:
5053 case R_PPC64_PLT16_LO:
5054 case R_PPC64_PLT32:
5055 case R_PPC64_PLT64:
5bd4f169 5056 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5057 actually build the entry in adjust_dynamic_symbol,
5058 because this might be a case of linking PIC code without
5059 linking in any dynamic objects, in which case we don't
5060 need to generate a procedure linkage table after all. */
5bd4f169
AM
5061 if (h == NULL)
5062 {
5063 /* It does not make sense to have a procedure linkage
3fad3c7c 5064 table entry for a local symbol. */
5bd4f169 5065 bfd_set_error (bfd_error_bad_value);
b34976b6 5066 return FALSE;
5bd4f169 5067 }
411e1bfb 5068 else
e054468f
AM
5069 {
5070 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5071 return FALSE;
5072 h->needs_plt = 1;
5073 if (h->root.root.string[0] == '.'
5074 && h->root.root.string[1] != '\0')
5075 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5076 }
5bd4f169
AM
5077 break;
5078
5079 /* The following relocations don't need to propagate the
5080 relocation if linking a shared object since they are
5081 section relative. */
5082 case R_PPC64_SECTOFF:
5083 case R_PPC64_SECTOFF_LO:
5084 case R_PPC64_SECTOFF_HI:
5085 case R_PPC64_SECTOFF_HA:
5086 case R_PPC64_SECTOFF_DS:
5087 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5088 case R_PPC64_DTPREL16:
5089 case R_PPC64_DTPREL16_LO:
5090 case R_PPC64_DTPREL16_HI:
5091 case R_PPC64_DTPREL16_HA:
5092 case R_PPC64_DTPREL16_DS:
5093 case R_PPC64_DTPREL16_LO_DS:
5094 case R_PPC64_DTPREL16_HIGHER:
5095 case R_PPC64_DTPREL16_HIGHERA:
5096 case R_PPC64_DTPREL16_HIGHEST:
5097 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5098 break;
5099
ad8e1ba5 5100 /* Nor do these. */
25f23106
AM
5101 case R_PPC64_REL16:
5102 case R_PPC64_REL16_LO:
5103 case R_PPC64_REL16_HI:
5104 case R_PPC64_REL16_HA:
5105 break;
5106
ad8e1ba5 5107 case R_PPC64_TOC16:
33c0ec9d
AM
5108 case R_PPC64_TOC16_DS:
5109 htab->do_multi_toc = 1;
d77c8a4b 5110 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5111 case R_PPC64_TOC16_LO:
5112 case R_PPC64_TOC16_HI:
5113 case R_PPC64_TOC16_HA:
ad8e1ba5 5114 case R_PPC64_TOC16_LO_DS:
4c52953f 5115 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5116 break;
5117
5bd4f169
AM
5118 /* This relocation describes the C++ object vtable hierarchy.
5119 Reconstruct it for later use during GC. */
5120 case R_PPC64_GNU_VTINHERIT:
c152c796 5121 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5122 return FALSE;
5bd4f169
AM
5123 break;
5124
5125 /* This relocation describes which C++ vtable entries are actually
5126 used. Record for later use during GC. */
5127 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5128 BFD_ASSERT (h != NULL);
5129 if (h != NULL
5130 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5131 return FALSE;
5bd4f169
AM
5132 break;
5133
721956f4
AM
5134 case R_PPC64_REL14:
5135 case R_PPC64_REL14_BRTAKEN:
5136 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5137 {
5138 asection *dest = NULL;
5139
5140 /* Heuristic: If jumping outside our section, chances are
5141 we are going to need a stub. */
5142 if (h != NULL)
5143 {
5144 /* If the sym is weak it may be overridden later, so
5145 don't assume we know where a weak sym lives. */
5146 if (h->root.type == bfd_link_hash_defined)
5147 dest = h->root.u.def.section;
5148 }
5149 else
87d72d41
AM
5150 {
5151 Elf_Internal_Sym *isym;
5152
5153 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5154 abfd, r_symndx);
5155 if (isym == NULL)
5156 return FALSE;
5157
5158 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5159 }
5160
220c76dd 5161 if (dest != sec)
7c8fe5c4 5162 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5163 }
721956f4
AM
5164 /* Fall through. */
5165
5d1634d7 5166 case R_PPC64_REL24:
e054468f 5167 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5168 {
5169 /* We may need a .plt entry if the function this reloc
5170 refers to is in a shared lib. */
e054468f 5171 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5172 return FALSE;
e054468f
AM
5173 h->needs_plt = 1;
5174 if (h->root.root.string[0] == '.'
5175 && h->root.root.string[1] != '\0')
5176 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5177 if (h == tga || h == dottga)
411e1bfb 5178 sec->has_tls_reloc = 1;
411e1bfb
AM
5179 }
5180 break;
5181
5182 case R_PPC64_TPREL64:
5183 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5184 if (!info->executable)
411e1bfb
AM
5185 info->flags |= DF_STATIC_TLS;
5186 goto dotlstoc;
5187
5188 case R_PPC64_DTPMOD64:
5189 if (rel + 1 < rel_end
5190 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5191 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5192 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5193 else
951fd09b 5194 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5195 goto dotlstoc;
5196
5197 case R_PPC64_DTPREL64:
5198 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5199 if (rel != relocs
5200 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5201 && rel[-1].r_offset == rel->r_offset - 8)
5202 /* This is the second reloc of a dtpmod, dtprel pair.
5203 Don't mark with TLS_DTPREL. */
5204 goto dodyn;
5205
5206 dotlstoc:
5207 sec->has_tls_reloc = 1;
5208 if (h != NULL)
5209 {
5210 struct ppc_link_hash_entry *eh;
5211 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5212 eh->tls_mask |= tls_type;
411e1bfb
AM
5213 }
5214 else
5215 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5216 rel->r_addend, tls_type))
5217 return FALSE;
5218
7c8fe5c4
AM
5219 ppc64_sec = ppc64_elf_section_data (sec);
5220 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5221 {
3a71aa26
AM
5222 bfd_size_type amt;
5223
e7b938ca 5224 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5225 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5226 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5227 if (ppc64_sec->u.toc.symndx == NULL)
5228 return FALSE;
5229 amt = sec->size * sizeof (bfd_vma) / 8;
5230 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5231 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5232 return FALSE;
7c8fe5c4
AM
5233 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5234 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5235 }
5236 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5237 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5238 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5239
5240 /* Mark the second slot of a GD or LD entry.
5241 -1 to indicate GD and -2 to indicate LD. */
5242 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5243 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5244 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5245 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5246 goto dodyn;
5247
5248 case R_PPC64_TPREL16:
5249 case R_PPC64_TPREL16_LO:
5250 case R_PPC64_TPREL16_HI:
5251 case R_PPC64_TPREL16_HA:
5252 case R_PPC64_TPREL16_DS:
5253 case R_PPC64_TPREL16_LO_DS:
5254 case R_PPC64_TPREL16_HIGHER:
5255 case R_PPC64_TPREL16_HIGHERA:
5256 case R_PPC64_TPREL16_HIGHEST:
5257 case R_PPC64_TPREL16_HIGHESTA:
5258 if (info->shared)
5259 {
1d483afe
AM
5260 if (!info->executable)
5261 info->flags |= DF_STATIC_TLS;
411e1bfb 5262 goto dodyn;
5d1634d7
AM
5263 }
5264 break;
5265
e86ce104 5266 case R_PPC64_ADDR64:
1e2f5b6e 5267 if (opd_sym_map != NULL
1e2f5b6e 5268 && rel + 1 < rel_end
4ce794b7 5269 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5270 {
8387904d
AM
5271 if (h != NULL)
5272 {
5273 if (h->root.root.string[0] == '.'
5274 && h->root.root.string[1] != 0
b31867b6 5275 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5276 ;
5277 else
5278 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5279 }
5280 else
5281 {
5282 asection *s;
87d72d41 5283 Elf_Internal_Sym *isym;
1e2f5b6e 5284
87d72d41
AM
5285 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5286 abfd, r_symndx);
5287 if (isym == NULL)
8387904d 5288 return FALSE;
87d72d41
AM
5289
5290 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5291 if (s != NULL && s != sec)
3f764659 5292 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5293 }
1e2f5b6e 5294 }
e86ce104
AM
5295 /* Fall through. */
5296
04c9666a 5297 case R_PPC64_REL30:
5bd4f169 5298 case R_PPC64_REL32:
04c9666a 5299 case R_PPC64_REL64:
65f38f15
AM
5300 case R_PPC64_ADDR14:
5301 case R_PPC64_ADDR14_BRNTAKEN:
5302 case R_PPC64_ADDR14_BRTAKEN:
5303 case R_PPC64_ADDR16:
5304 case R_PPC64_ADDR16_DS:
5305 case R_PPC64_ADDR16_HA:
5306 case R_PPC64_ADDR16_HI:
5307 case R_PPC64_ADDR16_HIGHER:
5308 case R_PPC64_ADDR16_HIGHERA:
5309 case R_PPC64_ADDR16_HIGHEST:
5310 case R_PPC64_ADDR16_HIGHESTA:
5311 case R_PPC64_ADDR16_LO:
5312 case R_PPC64_ADDR16_LO_DS:
5313 case R_PPC64_ADDR24:
65f38f15 5314 case R_PPC64_ADDR32:
65f38f15
AM
5315 case R_PPC64_UADDR16:
5316 case R_PPC64_UADDR32:
5317 case R_PPC64_UADDR64:
5bd4f169 5318 case R_PPC64_TOC:
81848ca0
AM
5319 if (h != NULL && !info->shared)
5320 /* We may need a copy reloc. */
f5385ebf 5321 h->non_got_ref = 1;
81848ca0 5322
41bd81ab 5323 /* Don't propagate .opd relocs. */
1e2f5b6e 5324 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5325 break;
e86ce104 5326
65f38f15
AM
5327 /* If we are creating a shared library, and this is a reloc
5328 against a global symbol, or a non PC relative reloc
5329 against a local symbol, then we need to copy the reloc
5330 into the shared library. However, if we are linking with
5331 -Bsymbolic, we do not need to copy a reloc against a
5332 global symbol which is defined in an object we are
5333 including in the link (i.e., DEF_REGULAR is set). At
5334 this point we have not seen all the input files, so it is
5335 possible that DEF_REGULAR is not set now but will be set
5336 later (it is never cleared). In case of a weak definition,
5337 DEF_REGULAR may be cleared later by a strong definition in
5338 a shared library. We account for that possibility below by
f4656909 5339 storing information in the dyn_relocs field of the hash
65f38f15
AM
5340 table entry. A similar situation occurs when creating
5341 shared libraries and symbol visibility changes render the
5342 symbol local.
5343
5344 If on the other hand, we are creating an executable, we
5345 may need to keep relocations for symbols satisfied by a
5346 dynamic library if we manage to avoid copy relocs for the
5347 symbol. */
411e1bfb 5348 dodyn:
65f38f15 5349 if ((info->shared
1d483afe 5350 && (must_be_dyn_reloc (info, r_type)
65f38f15
AM
5351 || (h != NULL
5352 && (! info->symbolic
5353 || h->root.type == bfd_link_hash_defweak
f5385ebf 5354 || !h->def_regular))))
f4656909
AM
5355 || (ELIMINATE_COPY_RELOCS
5356 && !info->shared
65f38f15
AM
5357 && h != NULL
5358 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5359 || !h->def_regular))
5360 || (!info->shared
5361 && ifunc != NULL))
5bd4f169 5362 {
6061a67d
AM
5363 struct elf_dyn_relocs *p;
5364 struct elf_dyn_relocs **head;
ec338859 5365
65f38f15
AM
5366 /* We must copy these reloc types into the output file.
5367 Create a reloc section in dynobj and make room for
5368 this reloc. */
5bd4f169
AM
5369 if (sreloc == NULL)
5370 {
83bac4b0
NC
5371 sreloc = _bfd_elf_make_dynamic_reloc_section
5372 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5373
5bd4f169 5374 if (sreloc == NULL)
83bac4b0 5375 return FALSE;
5bd4f169
AM
5376 }
5377
65f38f15
AM
5378 /* If this is a global symbol, we count the number of
5379 relocations we need for this symbol. */
5380 if (h != NULL)
5381 {
ec338859 5382 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
5383 }
5384 else
5385 {
ec338859
AM
5386 /* Track dynamic relocs needed for local syms too.
5387 We really need local syms available to do this
5388 easily. Oh well. */
ec338859 5389 asection *s;
6edfbbad 5390 void *vpp;
87d72d41 5391 Elf_Internal_Sym *isym;
6edfbbad 5392
87d72d41
AM
5393 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5394 abfd, r_symndx);
5395 if (isym == NULL)
b34976b6 5396 return FALSE;
ec338859 5397
87d72d41
AM
5398 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5399 if (s == NULL)
5400 s = sec;
5401
6edfbbad 5402 vpp = &elf_section_data (s)->local_dynrel;
6061a67d 5403 head = (struct elf_dyn_relocs **) vpp;
65f38f15 5404 }
ec338859
AM
5405
5406 p = *head;
5407 if (p == NULL || p->sec != sec)
5408 {
4ce794b7 5409 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 5410 if (p == NULL)
b34976b6 5411 return FALSE;
ec338859
AM
5412 p->next = *head;
5413 *head = p;
5414 p->sec = sec;
5415 p->count = 0;
5416 p->pc_count = 0;
5417 }
5418
5419 p->count += 1;
1d483afe 5420 if (!must_be_dyn_reloc (info, r_type))
ec338859 5421 p->pc_count += 1;
65f38f15 5422 }
5bd4f169 5423 break;
65f38f15
AM
5424
5425 default:
96e0dda4 5426 break;
5bd4f169
AM
5427 }
5428 }
5429
b34976b6 5430 return TRUE;
5bd4f169
AM
5431}
5432
8387904d
AM
5433/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5434 of the code entry point, and its section. */
5435
5436static bfd_vma
5437opd_entry_value (asection *opd_sec,
5438 bfd_vma offset,
5439 asection **code_sec,
5440 bfd_vma *code_off)
5441{
5442 bfd *opd_bfd = opd_sec->owner;
8860955f 5443 Elf_Internal_Rela *relocs;
8387904d 5444 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5445 bfd_vma val;
8387904d 5446
4b85d634
AM
5447 /* No relocs implies we are linking a --just-symbols object. */
5448 if (opd_sec->reloc_count == 0)
5449 {
aa374f67
AM
5450 char buf[8];
5451
5452 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
4b85d634 5453 return (bfd_vma) -1;
3b36f7e6 5454
aa374f67 5455 val = bfd_get_64 (opd_bfd, buf);
4b85d634
AM
5456 if (code_sec != NULL)
5457 {
5458 asection *sec, *likely = NULL;
5459 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5460 if (sec->vma <= val
5461 && (sec->flags & SEC_LOAD) != 0
5462 && (sec->flags & SEC_ALLOC) != 0)
5463 likely = sec;
5464 if (likely != NULL)
5465 {
5466 *code_sec = likely;
5467 if (code_off != NULL)
5468 *code_off = val - likely->vma;
5469 }
5470 }
5471 return val;
5472 }
5473
0c8d6e5c 5474 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5475
8860955f
AM
5476 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5477 if (relocs == NULL)
5478 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5479
8387904d 5480 /* Go find the opd reloc at the sym address. */
8860955f 5481 lo = relocs;
8387904d
AM
5482 BFD_ASSERT (lo != NULL);
5483 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5484 val = (bfd_vma) -1;
8387904d
AM
5485 while (lo < hi)
5486 {
5487 look = lo + (hi - lo) / 2;
5488 if (look->r_offset < offset)
5489 lo = look + 1;
5490 else if (look->r_offset > offset)
5491 hi = look;
5492 else
5493 {
0ffa91dd
NC
5494 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5495
8387904d
AM
5496 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5497 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5498 {
5499 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5500 asection *sec;
5501
5502 if (symndx < symtab_hdr->sh_info)
5503 {
5504 Elf_Internal_Sym *sym;
5505
5506 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5507 if (sym == NULL)
5508 {
5509 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5510 symtab_hdr->sh_info,
5511 0, NULL, NULL, NULL);
5512 if (sym == NULL)
645ea6a9 5513 break;
8387904d
AM
5514 symtab_hdr->contents = (bfd_byte *) sym;
5515 }
5516
5517 sym += symndx;
5518 val = sym->st_value;
cb33740c 5519 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5520 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5521 }
5522 else
5523 {
5524 struct elf_link_hash_entry **sym_hashes;
5525 struct elf_link_hash_entry *rh;
5526
5527 sym_hashes = elf_sym_hashes (opd_bfd);
5528 rh = sym_hashes[symndx - symtab_hdr->sh_info];
b31867b6 5529 rh = elf_follow_link (rh);
8387904d
AM
5530 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5531 || rh->root.type == bfd_link_hash_defweak);
5532 val = rh->root.u.def.value;
5533 sec = rh->root.u.def.section;
5534 }
5535 val += look->r_addend;
5536 if (code_off != NULL)
5537 *code_off = val;
5538 if (code_sec != NULL)
5539 *code_sec = sec;
5540 if (sec != NULL && sec->output_section != NULL)
5541 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5542 }
5543 break;
5544 }
5545 }
645ea6a9 5546
645ea6a9 5547 return val;
8387904d
AM
5548}
5549
854b41e7
AM
5550/* Return true if symbol is defined in a regular object file. */
5551
5552static bfd_boolean
5553is_static_defined (struct elf_link_hash_entry *h)
5554{
5555 return ((h->root.type == bfd_link_hash_defined
5556 || h->root.type == bfd_link_hash_defweak)
5557 && h->root.u.def.section != NULL
5558 && h->root.u.def.section->output_section != NULL);
5559}
5560
b31867b6
AM
5561/* If FDH is a function descriptor symbol, return the associated code
5562 entry symbol if it is defined. Return NULL otherwise. */
5563
5564static struct ppc_link_hash_entry *
5565defined_code_entry (struct ppc_link_hash_entry *fdh)
5566{
5567 if (fdh->is_func_descriptor)
5568 {
5569 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5570 if (fh->elf.root.type == bfd_link_hash_defined
5571 || fh->elf.root.type == bfd_link_hash_defweak)
5572 return fh;
5573 }
5574 return NULL;
5575}
5576
5577/* If FH is a function code entry symbol, return the associated
5578 function descriptor symbol if it is defined. Return NULL otherwise. */
5579
5580static struct ppc_link_hash_entry *
5581defined_func_desc (struct ppc_link_hash_entry *fh)
5582{
5583 if (fh->oh != NULL
5584 && fh->oh->is_func_descriptor)
5585 {
5586 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5587 if (fdh->elf.root.type == bfd_link_hash_defined
5588 || fdh->elf.root.type == bfd_link_hash_defweak)
5589 return fdh;
5590 }
5591 return NULL;
5592}
5593
74f0fb50
AM
5594/* Mark all our entry sym sections, both opd and code section. */
5595
5596static void
5597ppc64_elf_gc_keep (struct bfd_link_info *info)
5598{
5599 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5600 struct bfd_sym_chain *sym;
5601
4dfe6ac6
NC
5602 if (htab == NULL)
5603 return;
5604
74f0fb50
AM
5605 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5606 {
b31867b6 5607 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5608 asection *sec;
5609
5610 eh = (struct ppc_link_hash_entry *)
b31867b6 5611 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5612 if (eh == NULL)
5613 continue;
5614 if (eh->elf.root.type != bfd_link_hash_defined
5615 && eh->elf.root.type != bfd_link_hash_defweak)
5616 continue;
5617
b31867b6
AM
5618 fh = defined_code_entry (eh);
5619 if (fh != NULL)
74f0fb50 5620 {
b31867b6 5621 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5622 sec->flags |= SEC_KEEP;
5623 }
5624 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5625 && opd_entry_value (eh->elf.root.u.def.section,
5626 eh->elf.root.u.def.value,
5627 &sec, NULL) != (bfd_vma) -1)
5628 sec->flags |= SEC_KEEP;
5629
5630 sec = eh->elf.root.u.def.section;
5631 sec->flags |= SEC_KEEP;
5632 }
5633}
5634
64d03ab5
AM
5635/* Mark sections containing dynamically referenced symbols. When
5636 building shared libraries, we must assume that any visible symbol is
5637 referenced. */
5638
5639static bfd_boolean
5640ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5641{
5642 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5643 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5644 struct ppc_link_hash_entry *fdh;
64d03ab5 5645
64d03ab5 5646 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5647 fdh = defined_func_desc (eh);
5648 if (fdh != NULL)
5649 eh = fdh;
64d03ab5
AM
5650
5651 if ((eh->elf.root.type == bfd_link_hash_defined
5652 || eh->elf.root.type == bfd_link_hash_defweak)
5653 && (eh->elf.ref_dynamic
5654 || (!info->executable
5655 && eh->elf.def_regular
5656 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5657 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5658 {
5659 asection *code_sec;
b31867b6 5660 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5661
5662 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5663
5664 /* Function descriptor syms cause the associated
5665 function code sym section to be marked. */
b31867b6
AM
5666 fh = defined_code_entry (eh);
5667 if (fh != NULL)
5668 {
5669 code_sec = fh->elf.root.u.def.section;
5670 code_sec->flags |= SEC_KEEP;
5671 }
64d03ab5
AM
5672 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5673 && opd_entry_value (eh->elf.root.u.def.section,
5674 eh->elf.root.u.def.value,
5675 &code_sec, NULL) != (bfd_vma) -1)
5676 code_sec->flags |= SEC_KEEP;
5677 }
5678
5679 return TRUE;
5680}
5681
5bd4f169
AM
5682/* Return the section that should be marked against GC for a given
5683 relocation. */
5684
5685static asection *
4ce794b7 5686ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5687 struct bfd_link_info *info,
4ce794b7
AM
5688 Elf_Internal_Rela *rel,
5689 struct elf_link_hash_entry *h,
5690 Elf_Internal_Sym *sym)
5bd4f169 5691{
ccfa59ea
AM
5692 asection *rsec;
5693
ccfa59ea
AM
5694 /* Syms return NULL if we're marking .opd, so we avoid marking all
5695 function sections, as all functions are referenced in .opd. */
5696 rsec = NULL;
5697 if (get_opd_info (sec) != NULL)
5698 return rsec;
1e2f5b6e 5699
5bd4f169
AM
5700 if (h != NULL)
5701 {
04c9666a 5702 enum elf_ppc64_reloc_type r_type;
b31867b6 5703 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5704
4ce794b7 5705 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5706 switch (r_type)
5bd4f169
AM
5707 {
5708 case R_PPC64_GNU_VTINHERIT:
5709 case R_PPC64_GNU_VTENTRY:
5710 break;
5711
5712 default:
5713 switch (h->root.type)
5714 {
5715 case bfd_link_hash_defined:
5716 case bfd_link_hash_defweak:
ccfa59ea 5717 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5718 fdh = defined_func_desc (eh);
5719 if (fdh != NULL)
5720 eh = fdh;
1e2f5b6e
AM
5721
5722 /* Function descriptor syms cause the associated
5723 function code sym section to be marked. */
b31867b6
AM
5724 fh = defined_code_entry (eh);
5725 if (fh != NULL)
ccfa59ea
AM
5726 {
5727 /* They also mark their opd section. */
74f0fb50 5728 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5729
b31867b6 5730 rsec = fh->elf.root.u.def.section;
ccfa59ea 5731 }
8387904d
AM
5732 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5733 && opd_entry_value (eh->elf.root.u.def.section,
5734 eh->elf.root.u.def.value,
5735 &rsec, NULL) != (bfd_vma) -1)
74f0fb50 5736 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5737 else
1e2f5b6e
AM
5738 rsec = h->root.u.def.section;
5739 break;
5bd4f169
AM
5740
5741 case bfd_link_hash_common:
1e2f5b6e
AM
5742 rsec = h->root.u.c.p->section;
5743 break;
5bd4f169
AM
5744
5745 default:
fb34365b 5746 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5747 }
5748 }
5749 }
5750 else
5751 {
74f0fb50 5752 struct _opd_sec_data *opd;
1e2f5b6e
AM
5753
5754 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5755 opd = get_opd_info (rsec);
5756 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5757 {
74f0fb50 5758 rsec->gc_mark = 1;
ccfa59ea 5759
74f0fb50 5760 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5761 }
5bd4f169
AM
5762 }
5763
1e2f5b6e 5764 return rsec;
5bd4f169
AM
5765}
5766
65f38f15
AM
5767/* Update the .got, .plt. and dynamic reloc reference counts for the
5768 section being removed. */
5bd4f169 5769
b34976b6 5770static bfd_boolean
4ce794b7
AM
5771ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5772 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5773{
411e1bfb 5774 struct ppc_link_hash_table *htab;
5bd4f169
AM
5775 Elf_Internal_Shdr *symtab_hdr;
5776 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5777 struct got_entry **local_got_ents;
5bd4f169 5778 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5779
7dda2462
TG
5780 if (info->relocatable)
5781 return TRUE;
5782
680a3378
AM
5783 if ((sec->flags & SEC_ALLOC) == 0)
5784 return TRUE;
5785
ec338859
AM
5786 elf_section_data (sec)->local_dynrel = NULL;
5787
411e1bfb 5788 htab = ppc_hash_table (info);
4dfe6ac6
NC
5789 if (htab == NULL)
5790 return FALSE;
5791
0ffa91dd 5792 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5793 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5794 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5795
5796 relend = relocs + sec->reloc_count;
5797 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5798 {
5799 unsigned long r_symndx;
04c9666a 5800 enum elf_ppc64_reloc_type r_type;
58ac9f71 5801 struct elf_link_hash_entry *h = NULL;
f961d9dd 5802 unsigned char tls_type = 0;
5bd4f169 5803
a33d1f77 5804 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5805 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5806 if (r_symndx >= symtab_hdr->sh_info)
5807 {
5808 struct ppc_link_hash_entry *eh;
6061a67d
AM
5809 struct elf_dyn_relocs **pp;
5810 struct elf_dyn_relocs *p;
58ac9f71
AM
5811
5812 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5813 h = elf_follow_link (h);
58ac9f71
AM
5814 eh = (struct ppc_link_hash_entry *) h;
5815
5816 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5817 if (p->sec == sec)
5818 {
5819 /* Everything must go for SEC. */
5820 *pp = p->next;
5821 break;
5822 }
5823 }
5824
e054468f
AM
5825 if (is_branch_reloc (r_type))
5826 {
5827 struct plt_entry **ifunc = NULL;
5828 if (h != NULL)
5829 {
5830 if (h->type == STT_GNU_IFUNC)
5831 ifunc = &h->plt.plist;
5832 }
5833 else if (local_got_ents != NULL)
5834 {
5835 struct plt_entry **local_plt = (struct plt_entry **)
5836 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5837 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
5838 (local_plt + symtab_hdr->sh_info);
5839 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5840 ifunc = local_plt + r_symndx;
5841 }
5842 if (ifunc != NULL)
5843 {
5844 struct plt_entry *ent;
5845
5846 for (ent = *ifunc; ent != NULL; ent = ent->next)
5847 if (ent->addend == rel->r_addend)
5848 break;
5849 if (ent == NULL)
5850 abort ();
5851 if (ent->plt.refcount > 0)
5852 ent->plt.refcount -= 1;
5853 continue;
5854 }
5855 }
5856
a33d1f77
AM
5857 switch (r_type)
5858 {
411e1bfb
AM
5859 case R_PPC64_GOT_TLSLD16:
5860 case R_PPC64_GOT_TLSLD16_LO:
5861 case R_PPC64_GOT_TLSLD16_HI:
5862 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5863 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5864 goto dogot;
5865
5866 case R_PPC64_GOT_TLSGD16:
5867 case R_PPC64_GOT_TLSGD16_LO:
5868 case R_PPC64_GOT_TLSGD16_HI:
5869 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5870 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5871 goto dogot;
5872
5873 case R_PPC64_GOT_TPREL16_DS:
5874 case R_PPC64_GOT_TPREL16_LO_DS:
5875 case R_PPC64_GOT_TPREL16_HI:
5876 case R_PPC64_GOT_TPREL16_HA:
5877 tls_type = TLS_TLS | TLS_TPREL;
5878 goto dogot;
5879
5880 case R_PPC64_GOT_DTPREL16_DS:
5881 case R_PPC64_GOT_DTPREL16_LO_DS:
5882 case R_PPC64_GOT_DTPREL16_HI:
5883 case R_PPC64_GOT_DTPREL16_HA:
5884 tls_type = TLS_TLS | TLS_DTPREL;
5885 goto dogot;
5886
a33d1f77
AM
5887 case R_PPC64_GOT16:
5888 case R_PPC64_GOT16_DS:
5889 case R_PPC64_GOT16_HA:
5890 case R_PPC64_GOT16_HI:
5891 case R_PPC64_GOT16_LO:
5892 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5893 dogot:
5894 {
5895 struct got_entry *ent;
5896
58ac9f71
AM
5897 if (h != NULL)
5898 ent = h->got.glist;
411e1bfb
AM
5899 else
5900 ent = local_got_ents[r_symndx];
5901
5902 for (; ent != NULL; ent = ent->next)
5903 if (ent->addend == rel->r_addend
e717da7e 5904 && ent->owner == abfd
411e1bfb
AM
5905 && ent->tls_type == tls_type)
5906 break;
5907 if (ent == NULL)
5908 abort ();
5909 if (ent->got.refcount > 0)
5910 ent->got.refcount -= 1;
5911 }
a33d1f77 5912 break;
65f38f15 5913
a33d1f77
AM
5914 case R_PPC64_PLT16_HA:
5915 case R_PPC64_PLT16_HI:
5916 case R_PPC64_PLT16_LO:
5917 case R_PPC64_PLT32:
5918 case R_PPC64_PLT64:
721956f4
AM
5919 case R_PPC64_REL14:
5920 case R_PPC64_REL14_BRNTAKEN:
5921 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5922 case R_PPC64_REL24:
58ac9f71 5923 if (h != NULL)
5d1634d7 5924 {
411e1bfb
AM
5925 struct plt_entry *ent;
5926
411e1bfb
AM
5927 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5928 if (ent->addend == rel->r_addend)
5929 break;
7c6c1722 5930 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 5931 ent->plt.refcount -= 1;
5d1634d7 5932 }
e86ce104 5933 break;
5d1634d7 5934
a33d1f77
AM
5935 default:
5936 break;
5937 }
5938 }
b34976b6 5939 return TRUE;
5bd4f169
AM
5940}
5941
deb0e272
AM
5942/* The maximum size of .sfpr. */
5943#define SFPR_MAX (218*4)
5944
5945struct sfpr_def_parms
5946{
699733f6
AM
5947 const char name[12];
5948 unsigned char lo, hi;
deb0e272
AM
5949 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5950 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5951};
5952
5953/* Auto-generate _save*, _rest* functions in .sfpr. */
5954
4dfe6ac6 5955static bfd_boolean
deb0e272
AM
5956sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5957{
5958 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5959 unsigned int i;
5960 size_t len = strlen (parm->name);
5961 bfd_boolean writing = FALSE;
699733f6 5962 char sym[16];
deb0e272 5963
4dfe6ac6
NC
5964 if (htab == NULL)
5965 return FALSE;
5966
deb0e272
AM
5967 memcpy (sym, parm->name, len);
5968 sym[len + 2] = 0;
5969
5970 for (i = parm->lo; i <= parm->hi; i++)
5971 {
5972 struct elf_link_hash_entry *h;
5973
5974 sym[len + 0] = i / 10 + '0';
5975 sym[len + 1] = i % 10 + '0';
5976 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5977 if (h != NULL
f5385ebf 5978 && !h->def_regular)
deb0e272
AM
5979 {
5980 h->root.type = bfd_link_hash_defined;
5981 h->root.u.def.section = htab->sfpr;
5982 h->root.u.def.value = htab->sfpr->size;
5983 h->type = STT_FUNC;
f5385ebf 5984 h->def_regular = 1;
deb0e272
AM
5985 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5986 writing = TRUE;
5987 if (htab->sfpr->contents == NULL)
5988 {
5989 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5990 if (htab->sfpr->contents == NULL)
5991 return FALSE;
5992 }
5993 }
5994 if (writing)
5995 {
5996 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5997 if (i != parm->hi)
5998 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5999 else
6000 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6001 htab->sfpr->size = p - htab->sfpr->contents;
6002 }
6003 }
6004
6005 return TRUE;
6006}
6007
6008static bfd_byte *
6009savegpr0 (bfd *abfd, bfd_byte *p, int r)
6010{
6011 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6012 return p + 4;
6013}
6014
6015static bfd_byte *
6016savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6017{
6018 p = savegpr0 (abfd, p, r);
6019 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6020 p = p + 4;
6021 bfd_put_32 (abfd, BLR, p);
6022 return p + 4;
6023}
6024
6025static bfd_byte *
6026restgpr0 (bfd *abfd, bfd_byte *p, int r)
6027{
6028 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6029 return p + 4;
6030}
6031
6032static bfd_byte *
6033restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6034{
6035 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6036 p = p + 4;
6037 p = restgpr0 (abfd, p, r);
6038 bfd_put_32 (abfd, MTLR_R0, p);
6039 p = p + 4;
6040 if (r == 29)
6041 {
6042 p = restgpr0 (abfd, p, 30);
6043 p = restgpr0 (abfd, p, 31);
6044 }
6045 bfd_put_32 (abfd, BLR, p);
6046 return p + 4;
6047}
6048
6049static bfd_byte *
6050savegpr1 (bfd *abfd, bfd_byte *p, int r)
6051{
6052 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6053 return p + 4;
6054}
6055
6056static bfd_byte *
6057savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6058{
6059 p = savegpr1 (abfd, p, r);
6060 bfd_put_32 (abfd, BLR, p);
6061 return p + 4;
6062}
6063
6064static bfd_byte *
6065restgpr1 (bfd *abfd, bfd_byte *p, int r)
6066{
6067 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6068 return p + 4;
6069}
6070
6071static bfd_byte *
6072restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6073{
6074 p = restgpr1 (abfd, p, r);
6075 bfd_put_32 (abfd, BLR, p);
6076 return p + 4;
6077}
6078
6079static bfd_byte *
6080savefpr (bfd *abfd, bfd_byte *p, int r)
6081{
6082 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6083 return p + 4;
6084}
6085
6086static bfd_byte *
6087savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6088{
6089 p = savefpr (abfd, p, r);
6090 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6091 p = p + 4;
6092 bfd_put_32 (abfd, BLR, p);
6093 return p + 4;
6094}
6095
6096static bfd_byte *
6097restfpr (bfd *abfd, bfd_byte *p, int r)
6098{
6099 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6100 return p + 4;
6101}
6102
6103static bfd_byte *
6104restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6105{
6106 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6107 p = p + 4;
6108 p = restfpr (abfd, p, r);
6109 bfd_put_32 (abfd, MTLR_R0, p);
6110 p = p + 4;
6111 if (r == 29)
6112 {
6113 p = restfpr (abfd, p, 30);
6114 p = restfpr (abfd, p, 31);
6115 }
6116 bfd_put_32 (abfd, BLR, p);
6117 return p + 4;
6118}
6119
6120static bfd_byte *
6121savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6122{
6123 p = savefpr (abfd, p, r);
6124 bfd_put_32 (abfd, BLR, p);
6125 return p + 4;
6126}
6127
6128static bfd_byte *
6129restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6130{
6131 p = restfpr (abfd, p, r);
6132 bfd_put_32 (abfd, BLR, p);
6133 return p + 4;
6134}
6135
6136static bfd_byte *
6137savevr (bfd *abfd, bfd_byte *p, int r)
6138{
6139 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6140 p = p + 4;
6141 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6142 return p + 4;
6143}
6144
6145static bfd_byte *
6146savevr_tail (bfd *abfd, bfd_byte *p, int r)
6147{
6148 p = savevr (abfd, p, r);
6149 bfd_put_32 (abfd, BLR, p);
6150 return p + 4;
6151}
6152
6153static bfd_byte *
6154restvr (bfd *abfd, bfd_byte *p, int r)
6155{
6156 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6157 p = p + 4;
6158 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6159 return p + 4;
6160}
6161
6162static bfd_byte *
6163restvr_tail (bfd *abfd, bfd_byte *p, int r)
6164{
6165 p = restvr (abfd, p, r);
6166 bfd_put_32 (abfd, BLR, p);
6167 return p + 4;
6168}
6169
e86ce104
AM
6170/* Called via elf_link_hash_traverse to transfer dynamic linking
6171 information on function code symbol entries to their corresponding
6172 function descriptor symbol entries. */
deb0e272 6173
b34976b6 6174static bfd_boolean
4ce794b7 6175func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6176{
e86ce104 6177 struct bfd_link_info *info;
65f38f15 6178 struct ppc_link_hash_table *htab;
411e1bfb 6179 struct plt_entry *ent;
50bc7936
AM
6180 struct ppc_link_hash_entry *fh;
6181 struct ppc_link_hash_entry *fdh;
6182 bfd_boolean force_local;
5bd4f169 6183
50bc7936
AM
6184 fh = (struct ppc_link_hash_entry *) h;
6185 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6186 return TRUE;
e86ce104 6187
4ce794b7 6188 info = inf;
65f38f15 6189 htab = ppc_hash_table (info);
4dfe6ac6
NC
6190 if (htab == NULL)
6191 return FALSE;
5bd4f169 6192
c09bdfe5
AM
6193 /* Resolve undefined references to dot-symbols as the value
6194 in the function descriptor, if we have one in a regular object.
6195 This is to satisfy cases like ".quad .foo". Calls to functions
6196 in dynamic objects are handled elsewhere. */
6197 if (fh->elf.root.type == bfd_link_hash_undefweak
6198 && fh->was_undefined
b31867b6
AM
6199 && (fdh = defined_func_desc (fh)) != NULL
6200 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6201 && opd_entry_value (fdh->elf.root.u.def.section,
6202 fdh->elf.root.u.def.value,
c09bdfe5
AM
6203 &fh->elf.root.u.def.section,
6204 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6205 {
b31867b6 6206 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6207 fh->elf.forced_local = 1;
b31867b6
AM
6208 fh->elf.def_regular = fdh->elf.def_regular;
6209 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6210 }
6211
e86ce104
AM
6212 /* If this is a function code symbol, transfer dynamic linking
6213 information to the function descriptor symbol. */
50bc7936 6214 if (!fh->is_func)
b34976b6 6215 return TRUE;
e86ce104 6216
50bc7936 6217 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6218 if (ent->plt.refcount > 0)
6219 break;
50bc7936
AM
6220 if (ent == NULL
6221 || fh->elf.root.root.string[0] != '.'
6222 || fh->elf.root.root.string[1] == '\0')
6223 return TRUE;
5bd4f169 6224
50bc7936
AM
6225 /* Find the corresponding function descriptor symbol. Create it
6226 as undefined if necessary. */
5bd4f169 6227
b31867b6 6228 fdh = lookup_fdh (fh, htab);
50bc7936 6229 if (fdh == NULL
df131623 6230 && !info->executable
50bc7936
AM
6231 && (fh->elf.root.type == bfd_link_hash_undefined
6232 || fh->elf.root.type == bfd_link_hash_undefweak))
6233 {
908b32fc 6234 fdh = make_fdh (info, fh);
bb700d78
AM
6235 if (fdh == NULL)
6236 return FALSE;
50bc7936 6237 }
648cca2c 6238
908b32fc 6239 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6240 code symbol is strong undefined, make the fake sym the same.
6241 If the function code symbol is defined, then force the fake
6242 descriptor local; We can't support overriding of symbols in a
6243 shared library on a fake descriptor. */
908b32fc
AM
6244
6245 if (fdh != NULL
6246 && fdh->fake
433817dd 6247 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6248 {
433817dd
AM
6249 if (fh->elf.root.type == bfd_link_hash_undefined)
6250 {
6251 fdh->elf.root.type = bfd_link_hash_undefined;
6252 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6253 }
6254 else if (fh->elf.root.type == bfd_link_hash_defined
6255 || fh->elf.root.type == bfd_link_hash_defweak)
6256 {
6257 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6258 }
908b32fc
AM
6259 }
6260
50bc7936 6261 if (fdh != NULL
f5385ebf 6262 && !fdh->elf.forced_local
df131623 6263 && (!info->executable
f5385ebf
AM
6264 || fdh->elf.def_dynamic
6265 || fdh->elf.ref_dynamic
50bc7936
AM
6266 || (fdh->elf.root.type == bfd_link_hash_undefweak
6267 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6268 {
6269 if (fdh->elf.dynindx == -1)
c152c796 6270 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6271 return FALSE;
f5385ebf
AM
6272 fdh->elf.ref_regular |= fh->elf.ref_regular;
6273 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6274 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6275 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6276 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6277 {
40d16e0b 6278 move_plt_plist (fh, fdh);
f5385ebf 6279 fdh->elf.needs_plt = 1;
e86ce104 6280 }
50bc7936 6281 fdh->is_func_descriptor = 1;
34814b9f
AM
6282 fdh->oh = fh;
6283 fh->oh = fdh;
e86ce104
AM
6284 }
6285
50bc7936
AM
6286 /* Now that the info is on the function descriptor, clear the
6287 function code sym info. Any function code syms for which we
6288 don't have a definition in a regular file, we force local.
6289 This prevents a shared library from exporting syms that have
6290 been imported from another library. Function code syms that
6291 are really in the library we must leave global to prevent the
6292 linker dragging in a definition from a static library. */
93f3fa99
AM
6293 force_local = (!fh->elf.def_regular
6294 || fdh == NULL
6295 || !fdh->elf.def_regular
6296 || fdh->elf.forced_local);
50bc7936
AM
6297 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6298
b34976b6 6299 return TRUE;
e86ce104 6300}
40b8271b 6301
e86ce104 6302/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6303 this hook to a) provide some gcc support functions, and b) transfer
6304 dynamic linking information gathered so far on function code symbol
6305 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6306
b34976b6 6307static bfd_boolean
4ce794b7
AM
6308ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6309 struct bfd_link_info *info)
e86ce104
AM
6310{
6311 struct ppc_link_hash_table *htab;
82bd7b59 6312 unsigned int i;
deb0e272
AM
6313 const struct sfpr_def_parms funcs[] =
6314 {
6315 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6316 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6317 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6318 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6319 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6320 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6321 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6322 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6323 { "._savef", 14, 31, savefpr, savefpr1_tail },
6324 { "._restf", 14, 31, restfpr, restfpr1_tail },
6325 { "_savevr_", 20, 31, savevr, savevr_tail },
6326 { "_restvr_", 20, 31, restvr, restvr_tail }
6327 };
e86ce104
AM
6328
6329 htab = ppc_hash_table (info);
4dfe6ac6
NC
6330 if (htab == NULL)
6331 return FALSE;
6332
82bd7b59
AM
6333 if (htab->sfpr == NULL)
6334 /* We don't have any relocs. */
b34976b6 6335 return TRUE;
82bd7b59 6336
deb0e272
AM
6337 /* Provide any missing _save* and _rest* functions. */
6338 htab->sfpr->size = 0;
6339 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6340 if (!sfpr_define (info, &funcs[i]))
6341 return FALSE;
82bd7b59 6342
4ce794b7 6343 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6344
eea6121a 6345 if (htab->sfpr->size == 0)
8423293d 6346 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6347
b34976b6 6348 return TRUE;
e86ce104
AM
6349}
6350
6351/* Adjust a symbol defined by a dynamic object and referenced by a
6352 regular object. The current definition is in some section of the
6353 dynamic object, but we're not including those sections. We have to
6354 change the definition to something the rest of the link can
6355 understand. */
6356
b34976b6 6357static bfd_boolean
4ce794b7
AM
6358ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6359 struct elf_link_hash_entry *h)
e86ce104
AM
6360{
6361 struct ppc_link_hash_table *htab;
e86ce104 6362 asection *s;
e86ce104
AM
6363
6364 htab = ppc_hash_table (info);
4dfe6ac6
NC
6365 if (htab == NULL)
6366 return FALSE;
e86ce104
AM
6367
6368 /* Deal with function syms. */
6369 if (h->type == STT_FUNC
e054468f 6370 || h->type == STT_GNU_IFUNC
f5385ebf 6371 || h->needs_plt)
e86ce104
AM
6372 {
6373 /* Clear procedure linkage table information for any symbol that
6374 won't need a .plt entry. */
411e1bfb
AM
6375 struct plt_entry *ent;
6376 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6377 if (ent->plt.refcount > 0)
6378 break;
8387904d 6379 if (ent == NULL
e054468f
AM
6380 || (h->type != STT_GNU_IFUNC
6381 && (SYMBOL_CALLS_LOCAL (info, h)
6382 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6383 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6384 {
411e1bfb 6385 h->plt.plist = NULL;
f5385ebf 6386 h->needs_plt = 0;
40b8271b 6387 }
5bd4f169 6388 }
bbd7ec4a 6389 else
411e1bfb 6390 h->plt.plist = NULL;
5bd4f169
AM
6391
6392 /* If this is a weak symbol, and there is a real definition, the
6393 processor independent code will have arranged for us to see the
6394 real definition first, and we can just use the same value. */
f6e332e6 6395 if (h->u.weakdef != NULL)
5bd4f169 6396 {
f6e332e6
AM
6397 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6398 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6399 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6400 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6401 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6402 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6403 return TRUE;
5bd4f169
AM
6404 }
6405
5bd4f169
AM
6406 /* If we are creating a shared library, we must presume that the
6407 only references to the symbol are via the global offset table.
6408 For such cases we need not do anything here; the relocations will
6409 be handled correctly by relocate_section. */
6410 if (info->shared)
b34976b6 6411 return TRUE;
5bd4f169 6412
65f38f15
AM
6413 /* If there are no references to this symbol that do not use the
6414 GOT, we don't need to generate a copy reloc. */
f5385ebf 6415 if (!h->non_got_ref)
b34976b6 6416 return TRUE;
65f38f15 6417
b186458a
JJ
6418 /* Don't generate a copy reloc for symbols defined in the executable. */
6419 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6420 return TRUE;
6421
f4656909 6422 if (ELIMINATE_COPY_RELOCS)
65f38f15 6423 {
f4656909 6424 struct ppc_link_hash_entry * eh;
6061a67d 6425 struct elf_dyn_relocs *p;
65f38f15 6426
f4656909
AM
6427 eh = (struct ppc_link_hash_entry *) h;
6428 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6429 {
6430 s = p->sec->output_section;
6431 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6432 break;
6433 }
6434
6435 /* If we didn't find any dynamic relocs in read-only sections, then
6436 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6437 if (p == NULL)
6438 {
f5385ebf 6439 h->non_got_ref = 0;
f4656909
AM
6440 return TRUE;
6441 }
65f38f15
AM
6442 }
6443
5d35169e 6444 if (h->plt.plist != NULL)
97b639ba
AM
6445 {
6446 /* We should never get here, but unfortunately there are versions
6447 of gcc out there that improperly (for this ABI) put initialized
6448 function pointers, vtable refs and suchlike in read-only
6449 sections. Allow them to proceed, but warn that this might
6450 break at runtime. */
25f53a85 6451 info->callbacks->einfo
97b639ba 6452 (_("copy reloc against `%s' requires lazy plt linking; "
25f53a85 6453 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
97b639ba
AM
6454 h->root.root.string);
6455 }
5d35169e
AM
6456
6457 /* This is a reference to a symbol defined by a dynamic object which
6458 is not a function. */
6459
909272ee
AM
6460 if (h->size == 0)
6461 {
25f53a85
AM
6462 info->callbacks->einfo (_("dynamic variable `%s' is zero size\n"),
6463 h->root.root.string);
909272ee
AM
6464 return TRUE;
6465 }
6466
5bd4f169
AM
6467 /* We must allocate the symbol in our .dynbss section, which will
6468 become part of the .bss section of the executable. There will be
6469 an entry for this symbol in the .dynsym section. The dynamic
6470 object will contain position independent code, so all references
6471 from the dynamic object to this symbol will go through the global
6472 offset table. The dynamic linker will use the .dynsym entry to
6473 determine the address it must put in the global offset table, so
6474 both the dynamic object and the regular object will refer to the
6475 same memory location for the variable. */
5bd4f169 6476
04c9666a
AM
6477 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6478 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6479 runtime process image. We need to remember the offset into the
6480 .rela.bss section we are going to use. */
6481 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6482 {
eea6121a 6483 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6484 h->needs_copy = 1;
5bd4f169
AM
6485 }
6486
4ce794b7 6487 s = htab->dynbss;
5bd4f169 6488
027297b7 6489 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6490}
6491
e86ce104
AM
6492/* If given a function descriptor symbol, hide both the function code
6493 sym and the descriptor. */
6494static void
4ce794b7
AM
6495ppc64_elf_hide_symbol (struct bfd_link_info *info,
6496 struct elf_link_hash_entry *h,
6497 bfd_boolean force_local)
e86ce104 6498{
34814b9f 6499 struct ppc_link_hash_entry *eh;
e86ce104
AM
6500 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6501
34814b9f
AM
6502 eh = (struct ppc_link_hash_entry *) h;
6503 if (eh->is_func_descriptor)
e86ce104 6504 {
34814b9f 6505 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6506
721956f4 6507 if (fh == NULL)
d1329ca3
AM
6508 {
6509 const char *p, *q;
6510 struct ppc_link_hash_table *htab;
6511 char save;
6512
6513 /* We aren't supposed to use alloca in BFD because on
6514 systems which do not have alloca the version in libiberty
6515 calls xmalloc, which might cause the program to crash
6516 when it runs out of memory. This function doesn't have a
6517 return status, so there's no way to gracefully return an
6518 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6519 accessed; It's either a string in an ELF string table,
6520 or allocated in an objalloc structure. */
d1329ca3 6521
34814b9f 6522 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6523 save = *p;
6524 *(char *) p = '.';
6525 htab = ppc_hash_table (info);
4dfe6ac6
NC
6526 if (htab == NULL)
6527 return;
6528
34814b9f
AM
6529 fh = (struct ppc_link_hash_entry *)
6530 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6531 *(char *) p = save;
6532
6533 /* Unfortunately, if it so happens that the string we were
6534 looking for was allocated immediately before this string,
6535 then we overwrote the string terminator. That's the only
6536 reason the lookup should fail. */
6537 if (fh == NULL)
6538 {
34814b9f
AM
6539 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6540 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6541 --q, --p;
34814b9f
AM
6542 if (q < eh->elf.root.root.string && *p == '.')
6543 fh = (struct ppc_link_hash_entry *)
6544 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6545 }
6546 if (fh != NULL)
6547 {
34814b9f
AM
6548 eh->oh = fh;
6549 fh->oh = eh;
d1329ca3
AM
6550 }
6551 }
e86ce104 6552 if (fh != NULL)
34814b9f 6553 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6554 }
6555}
6556
411e1bfb 6557static bfd_boolean
8843416a
AM
6558get_sym_h (struct elf_link_hash_entry **hp,
6559 Elf_Internal_Sym **symp,
6560 asection **symsecp,
f961d9dd 6561 unsigned char **tls_maskp,
8843416a
AM
6562 Elf_Internal_Sym **locsymsp,
6563 unsigned long r_symndx,
6564 bfd *ibfd)
411e1bfb 6565{
0ffa91dd 6566 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6567
6568 if (r_symndx >= symtab_hdr->sh_info)
6569 {
6570 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6571 struct elf_link_hash_entry *h;
6572
6573 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6574 h = elf_follow_link (h);
411e1bfb
AM
6575
6576 if (hp != NULL)
6577 *hp = h;
6578
6579 if (symp != NULL)
6580 *symp = NULL;
6581
6582 if (symsecp != NULL)
6583 {
6584 asection *symsec = NULL;
6585 if (h->root.type == bfd_link_hash_defined
6586 || h->root.type == bfd_link_hash_defweak)
6587 symsec = h->root.u.def.section;
6588 *symsecp = symsec;
6589 }
6590
e7b938ca 6591 if (tls_maskp != NULL)
411e1bfb
AM
6592 {
6593 struct ppc_link_hash_entry *eh;
6594
6595 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6596 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6597 }
6598 }
6599 else
6600 {
6601 Elf_Internal_Sym *sym;
6602 Elf_Internal_Sym *locsyms = *locsymsp;
6603
6604 if (locsyms == NULL)
6605 {
6606 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6607 if (locsyms == NULL)
6608 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6609 symtab_hdr->sh_info,
6610 0, NULL, NULL, NULL);
6611 if (locsyms == NULL)
6612 return FALSE;
6613 *locsymsp = locsyms;
6614 }
6615 sym = locsyms + r_symndx;
6616
6617 if (hp != NULL)
6618 *hp = NULL;
6619
6620 if (symp != NULL)
6621 *symp = sym;
6622
6623 if (symsecp != NULL)
cb33740c 6624 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6625
e7b938ca 6626 if (tls_maskp != NULL)
411e1bfb
AM
6627 {
6628 struct got_entry **lgot_ents;
f961d9dd 6629 unsigned char *tls_mask;
411e1bfb 6630
e7b938ca 6631 tls_mask = NULL;
411e1bfb
AM
6632 lgot_ents = elf_local_got_ents (ibfd);
6633 if (lgot_ents != NULL)
6634 {
e054468f
AM
6635 struct plt_entry **local_plt = (struct plt_entry **)
6636 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6637 unsigned char *lgot_masks = (unsigned char *)
e054468f 6638 (local_plt + symtab_hdr->sh_info);
e7b938ca 6639 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6640 }
e7b938ca 6641 *tls_maskp = tls_mask;
411e1bfb
AM
6642 }
6643 }
6644 return TRUE;
6645}
6646
e7b938ca 6647/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6648 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6649 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6650
6651static int
f961d9dd 6652get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6653 unsigned long *toc_symndx,
6654 bfd_vma *toc_addend,
0d4792f7 6655 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6656 const Elf_Internal_Rela *rel,
6657 bfd *ibfd)
411e1bfb
AM
6658{
6659 unsigned long r_symndx;
0d4792f7 6660 int next_r;
411e1bfb
AM
6661 struct elf_link_hash_entry *h;
6662 Elf_Internal_Sym *sym;
6663 asection *sec;
6664 bfd_vma off;
6665
6666 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6667 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6668 return 0;
411e1bfb 6669
e7b938ca 6670 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6671 || sec == NULL
6bee8834 6672 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6673 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6674 return 1;
411e1bfb
AM
6675
6676 /* Look inside a TOC section too. */
6677 if (h != NULL)
6678 {
6679 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6680 off = h->root.u.def.value;
6681 }
6682 else
6683 off = sym->st_value;
6684 off += rel->r_addend;
6685 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6686 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6687 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6688 if (toc_symndx != NULL)
6689 *toc_symndx = r_symndx;
3a71aa26
AM
6690 if (toc_addend != NULL)
6691 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6692 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6693 return 0;
854b41e7 6694 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6695 && (next_r == -1 || next_r == -2))
6696 return 1 - next_r;
951fd09b 6697 return 1;
411e1bfb
AM
6698}
6699
754021d0 6700/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6701 code for the old ABI, these will already have been done. */
754021d0
AM
6702
6703static bfd_boolean
6704adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6705{
6706 struct ppc_link_hash_entry *eh;
6707 asection *sym_sec;
74f0fb50 6708 struct _opd_sec_data *opd;
754021d0
AM
6709
6710 if (h->root.type == bfd_link_hash_indirect)
6711 return TRUE;
6712
754021d0
AM
6713 if (h->root.type != bfd_link_hash_defined
6714 && h->root.type != bfd_link_hash_defweak)
6715 return TRUE;
6716
6717 eh = (struct ppc_link_hash_entry *) h;
6718 if (eh->adjust_done)
6719 return TRUE;
6720
6721 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6722 opd = get_opd_info (sym_sec);
6723 if (opd != NULL && opd->adjust != NULL)
754021d0 6724 {
74f0fb50 6725 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6726 if (adjust == -1)
6727 {
6728 /* This entry has been deleted. */
b3fac117 6729 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6730 if (dsec == NULL)
6731 {
6732 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6733 if (elf_discarded_section (dsec))
6734 {
b3fac117 6735 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6736 break;
6737 }
6738 }
4025353c 6739 eh->elf.root.u.def.value = 0;
81688140 6740 eh->elf.root.u.def.section = dsec;
4025353c
AM
6741 }
6742 else
6743 eh->elf.root.u.def.value += adjust;
754021d0
AM
6744 eh->adjust_done = 1;
6745 }
6746 return TRUE;
6747}
6748
8c1d1bb8
AM
6749/* Handles decrementing dynamic reloc counts for the reloc specified by
6750 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6751 have already been determined. */
6752
6753static bfd_boolean
6754dec_dynrel_count (bfd_vma r_info,
6755 asection *sec,
6756 struct bfd_link_info *info,
6757 Elf_Internal_Sym **local_syms,
6758 struct elf_link_hash_entry *h,
6759 asection *sym_sec)
6760{
6761 enum elf_ppc64_reloc_type r_type;
6061a67d
AM
6762 struct elf_dyn_relocs *p;
6763 struct elf_dyn_relocs **pp;
8c1d1bb8
AM
6764
6765 /* Can this reloc be dynamic? This switch, and later tests here
6766 should be kept in sync with the code in check_relocs. */
6767 r_type = ELF64_R_TYPE (r_info);
6768 switch (r_type)
6769 {
6770 default:
6771 return TRUE;
6772
6773 case R_PPC64_TPREL16:
6774 case R_PPC64_TPREL16_LO:
6775 case R_PPC64_TPREL16_HI:
6776 case R_PPC64_TPREL16_HA:
6777 case R_PPC64_TPREL16_DS:
6778 case R_PPC64_TPREL16_LO_DS:
6779 case R_PPC64_TPREL16_HIGHER:
6780 case R_PPC64_TPREL16_HIGHERA:
6781 case R_PPC64_TPREL16_HIGHEST:
6782 case R_PPC64_TPREL16_HIGHESTA:
6783 if (!info->shared)
6784 return TRUE;
6785
6786 case R_PPC64_TPREL64:
6787 case R_PPC64_DTPMOD64:
6788 case R_PPC64_DTPREL64:
6789 case R_PPC64_ADDR64:
6790 case R_PPC64_REL30:
6791 case R_PPC64_REL32:
6792 case R_PPC64_REL64:
6793 case R_PPC64_ADDR14:
6794 case R_PPC64_ADDR14_BRNTAKEN:
6795 case R_PPC64_ADDR14_BRTAKEN:
6796 case R_PPC64_ADDR16:
6797 case R_PPC64_ADDR16_DS:
6798 case R_PPC64_ADDR16_HA:
6799 case R_PPC64_ADDR16_HI:
6800 case R_PPC64_ADDR16_HIGHER:
6801 case R_PPC64_ADDR16_HIGHERA:
6802 case R_PPC64_ADDR16_HIGHEST:
6803 case R_PPC64_ADDR16_HIGHESTA:
6804 case R_PPC64_ADDR16_LO:
6805 case R_PPC64_ADDR16_LO_DS:
6806 case R_PPC64_ADDR24:
6807 case R_PPC64_ADDR32:
6808 case R_PPC64_UADDR16:
6809 case R_PPC64_UADDR32:
6810 case R_PPC64_UADDR64:
6811 case R_PPC64_TOC:
6812 break;
6813 }
6814
6815 if (local_syms != NULL)
6816 {
6817 unsigned long r_symndx;
6818 Elf_Internal_Sym *sym;
6819 bfd *ibfd = sec->owner;
6820
6821 r_symndx = ELF64_R_SYM (r_info);
6822 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6823 return FALSE;
6824 }
6825
6826 if ((info->shared
1d483afe 6827 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8
AM
6828 || (h != NULL
6829 && (!info->symbolic
6830 || h->root.type == bfd_link_hash_defweak
6831 || !h->def_regular))))
6832 || (ELIMINATE_COPY_RELOCS
6833 && !info->shared
6834 && h != NULL
6835 && (h->root.type == bfd_link_hash_defweak
6836 || !h->def_regular)))
6837 ;
6838 else
6839 return TRUE;
6840
6841 if (h != NULL)
6842 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6843 else
6edfbbad 6844 {
60124e18
AM
6845 if (sym_sec != NULL)
6846 {
6847 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6061a67d 6848 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6849 }
6850 else
6851 {
6852 void *vpp = &elf_section_data (sec)->local_dynrel;
6061a67d 6853 pp = (struct elf_dyn_relocs **) vpp;
60124e18
AM
6854 }
6855
6856 /* elf_gc_sweep may have already removed all dyn relocs associated
6857 with local syms for a given section. Don't report a dynreloc
6858 miscount. */
6859 if (*pp == NULL)
6860 return TRUE;
6edfbbad 6861 }
8c1d1bb8
AM
6862
6863 while ((p = *pp) != NULL)
6864 {
6865 if (p->sec == sec)
6866 {
1d483afe 6867 if (!must_be_dyn_reloc (info, r_type))
8c1d1bb8
AM
6868 p->pc_count -= 1;
6869 p->count -= 1;
6870 if (p->count == 0)
6871 *pp = p->next;
6872 return TRUE;
6873 }
6874 pp = &p->next;
6875 }
6876
25f53a85
AM
6877 info->callbacks->einfo (_("dynreloc miscount for %B, section %A\n"),
6878 sec->owner, sec);
8c1d1bb8
AM
6879 bfd_set_error (bfd_error_bad_value);
6880 return FALSE;
6881}
6882
754021d0
AM
6883/* Remove unused Official Procedure Descriptor entries. Currently we
6884 only remove those associated with functions in discarded link-once
6885 sections, or weakly defined functions that have been overridden. It
6886 would be possible to remove many more entries for statically linked
6887 applications. */
6888
b34976b6 6889bfd_boolean
33c0ec9d 6890ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
6891{
6892 bfd *ibfd;
754021d0 6893 bfd_boolean some_edited = FALSE;
3f764659 6894 asection *need_pad = NULL;
1e2f5b6e 6895
411e1bfb 6896 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
6897 {
6898 asection *sec;
6899 Elf_Internal_Rela *relstart, *rel, *relend;
6900 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6901 Elf_Internal_Sym *local_syms;
1e2f5b6e 6902 bfd_vma offset;
74f0fb50 6903 struct _opd_sec_data *opd;
3f764659
JJ
6904 bfd_boolean need_edit, add_aux_fields;
6905 bfd_size_type cnt_16b = 0;
1e2f5b6e 6906
854b41e7
AM
6907 if (!is_ppc64_elf (ibfd))
6908 continue;
6909
1e2f5b6e 6910 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6911 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6912 continue;
6913
4b85d634
AM
6914 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6915 continue;
6916
1e2f5b6e
AM
6917 if (sec->output_section == bfd_abs_section_ptr)
6918 continue;
6919
6920 /* Look through the section relocs. */
6921 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6922 continue;
6923
6cdc0ccc 6924 local_syms = NULL;
0ffa91dd 6925 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6926
6927 /* Read the relocations. */
4ce794b7 6928 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6929 info->keep_memory);
1e2f5b6e 6930 if (relstart == NULL)
b34976b6 6931 return FALSE;
1e2f5b6e
AM
6932
6933 /* First run through the relocs to check they are sane, and to
6934 determine whether we need to edit this opd section. */
b34976b6 6935 need_edit = FALSE;
3f764659 6936 need_pad = sec;
1e2f5b6e
AM
6937 offset = 0;
6938 relend = relstart + sec->reloc_count;
50bc7936 6939 for (rel = relstart; rel < relend; )
1e2f5b6e 6940 {
04c9666a 6941 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6942 unsigned long r_symndx;
6943 asection *sym_sec;
6944 struct elf_link_hash_entry *h;
6945 Elf_Internal_Sym *sym;
6946
3f764659 6947 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6948 only interested in the reloc pointing to a function entry
6949 point. */
50bc7936
AM
6950 if (rel->r_offset != offset
6951 || rel + 1 >= relend
6952 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
6953 {
6954 /* If someone messes with .opd alignment then after a
6955 "ld -r" we might have padding in the middle of .opd.
6956 Also, there's nothing to prevent someone putting
6957 something silly in .opd with the assembler. No .opd
b34976b6 6958 optimization for them! */
3f764659 6959 broken_opd:
1e2f5b6e 6960 (*_bfd_error_handler)
d003868e 6961 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 6962 need_edit = FALSE;
1e2f5b6e
AM
6963 break;
6964 }
6965
50bc7936
AM
6966 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6967 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6968 {
6969 (*_bfd_error_handler)
d003868e
AM
6970 (_("%B: unexpected reloc type %u in .opd section"),
6971 ibfd, r_type);
50bc7936
AM
6972 need_edit = FALSE;
6973 break;
6974 }
6975
1e2f5b6e 6976 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6977 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6978 r_symndx, ibfd))
50bc7936 6979 goto error_ret;
1e2f5b6e
AM
6980
6981 if (sym_sec == NULL || sym_sec->owner == NULL)
6982 {
411e1bfb
AM
6983 const char *sym_name;
6984 if (h != NULL)
6985 sym_name = h->root.root.string;
6986 else
26c61ae5
L
6987 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6988 sym_sec);
411e1bfb 6989
1e2f5b6e 6990 (*_bfd_error_handler)
d003868e
AM
6991 (_("%B: undefined sym `%s' in .opd section"),
6992 ibfd, sym_name);
b34976b6 6993 need_edit = FALSE;
1e2f5b6e
AM
6994 break;
6995 }
6996
51020317
AM
6997 /* opd entries are always for functions defined in the
6998 current input bfd. If the symbol isn't defined in the
6999 input bfd, then we won't be using the function in this
7000 bfd; It must be defined in a linkonce section in another
7001 bfd, or is weak. It's also possible that we are
7002 discarding the function due to a linker script /DISCARD/,
7003 which we test for via the output_section. */
7004 if (sym_sec->owner != ibfd
7005 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7006 need_edit = TRUE;
1e2f5b6e 7007
50bc7936 7008 rel += 2;
3f764659
JJ
7009 if (rel == relend
7010 || (rel + 1 == relend && rel->r_offset == offset + 16))
7011 {
7012 if (sec->size == offset + 24)
7013 {
7014 need_pad = NULL;
7015 break;
7016 }
7017 if (rel == relend && sec->size == offset + 16)
7018 {
7019 cnt_16b++;
7020 break;
7021 }
7022 goto broken_opd;
7023 }
7024
7025 if (rel->r_offset == offset + 24)
7026 offset += 24;
7027 else if (rel->r_offset != offset + 16)
7028 goto broken_opd;
7029 else if (rel + 1 < relend
7030 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7031 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7032 {
7033 offset += 16;
7034 cnt_16b++;
7035 }
7036 else if (rel + 2 < relend
7037 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7038 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7039 {
7040 offset += 24;
7041 rel += 1;
7042 }
7043 else
7044 goto broken_opd;
1e2f5b6e
AM
7045 }
7046
3f764659
JJ
7047 add_aux_fields = non_overlapping && cnt_16b > 0;
7048
7049 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7050 {
7051 Elf_Internal_Rela *write_rel;
d4730f92 7052 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7053 bfd_byte *rptr, *wptr;
983bddc8 7054 bfd_byte *new_contents;
b34976b6 7055 bfd_boolean skip;
3f764659 7056 long opd_ent_size;
74f0fb50
AM
7057 bfd_size_type amt;
7058
983bddc8 7059 new_contents = NULL;
74f0fb50
AM
7060 amt = sec->size * sizeof (long) / 8;
7061 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7062 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7063 if (opd->adjust == NULL)
7064 return FALSE;
7065 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7066
7067 /* This seems a waste of time as input .opd sections are all
7068 zeros as generated by gcc, but I suppose there's no reason
7069 this will always be so. We might start putting something in
7070 the third word of .opd entries. */
7071 if ((sec->flags & SEC_IN_MEMORY) == 0)
7072 {
eea6121a
AM
7073 bfd_byte *loc;
7074 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7075 {
eea6121a
AM
7076 if (loc != NULL)
7077 free (loc);
50bc7936 7078 error_ret:
6cdc0ccc
AM
7079 if (local_syms != NULL
7080 && symtab_hdr->contents != (unsigned char *) local_syms)
7081 free (local_syms);
6cdc0ccc
AM
7082 if (elf_section_data (sec)->relocs != relstart)
7083 free (relstart);
b34976b6 7084 return FALSE;
6cdc0ccc 7085 }
1e2f5b6e
AM
7086 sec->contents = loc;
7087 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7088 }
7089
7090 elf_section_data (sec)->relocs = relstart;
7091
3f764659 7092 new_contents = sec->contents;
3f764659
JJ
7093 if (add_aux_fields)
7094 {
7095 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7096 if (new_contents == NULL)
7097 return FALSE;
7098 need_pad = FALSE;
3f764659 7099 }
b4f4e59f
AM
7100 wptr = new_contents;
7101 rptr = sec->contents;
3f764659 7102
1e2f5b6e 7103 write_rel = relstart;
b34976b6 7104 skip = FALSE;
1e2f5b6e 7105 offset = 0;
3f764659 7106 opd_ent_size = 0;
1e2f5b6e
AM
7107 for (rel = relstart; rel < relend; rel++)
7108 {
50bc7936
AM
7109 unsigned long r_symndx;
7110 asection *sym_sec;
7111 struct elf_link_hash_entry *h;
7112 Elf_Internal_Sym *sym;
7113
7114 r_symndx = ELF64_R_SYM (rel->r_info);
7115 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7116 r_symndx, ibfd))
50bc7936
AM
7117 goto error_ret;
7118
1e2f5b6e
AM
7119 if (rel->r_offset == offset)
7120 {
50bc7936 7121 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7122
7123 /* See if the .opd entry is full 24 byte or
7124 16 byte (with fd_aux entry overlapped with next
7125 fd_func). */
7126 opd_ent_size = 24;
7127 if ((rel + 2 == relend && sec->size == offset + 16)
7128 || (rel + 3 < relend
7129 && rel[2].r_offset == offset + 16
7130 && rel[3].r_offset == offset + 24
7131 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7132 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7133 opd_ent_size = 16;
7134
4025353c
AM
7135 if (h != NULL
7136 && h->root.root.string[0] == '.')
c4f68ce3 7137 {
4dfe6ac6
NC
7138 struct ppc_link_hash_table *htab;
7139
7140 htab = ppc_hash_table (info);
7141 if (htab != NULL)
7142 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7143 htab);
c4f68ce3
AM
7144 if (fdh != NULL
7145 && fdh->elf.root.type != bfd_link_hash_defined
7146 && fdh->elf.root.type != bfd_link_hash_defweak)
7147 fdh = NULL;
7148 }
1e2f5b6e 7149
51020317
AM
7150 skip = (sym_sec->owner != ibfd
7151 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7152 if (skip)
7153 {
4025353c 7154 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7155 {
7156 /* Arrange for the function descriptor sym
7157 to be dropped. */
d6fe2dc1
AM
7158 fdh->elf.root.u.def.value = 0;
7159 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7160 }
74f0fb50 7161 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7162 }
7163 else
1e2f5b6e
AM
7164 {
7165 /* We'll be keeping this opd entry. */
7166
4025353c 7167 if (fdh != NULL)
1e2f5b6e 7168 {
754021d0
AM
7169 /* Redefine the function descriptor symbol to
7170 this location in the opd section. It is
7171 necessary to update the value here rather
7172 than using an array of adjustments as we do
7173 for local symbols, because various places
7174 in the generic ELF code use the value
7175 stored in u.def.value. */
3f764659 7176 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7177 fdh->adjust_done = 1;
1e2f5b6e 7178 }
754021d0
AM
7179
7180 /* Local syms are a bit tricky. We could
7181 tweak them as they can be cached, but
7182 we'd need to look through the local syms
7183 for the function descriptor sym which we
7184 don't have at the moment. So keep an
7185 array of adjustments. */
74f0fb50 7186 opd->adjust[rel->r_offset / 8]
3f764659 7187 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7188
7189 if (wptr != rptr)
3f764659
JJ
7190 memcpy (wptr, rptr, opd_ent_size);
7191 wptr += opd_ent_size;
7192 if (add_aux_fields && opd_ent_size == 16)
7193 {
7194 memset (wptr, '\0', 8);
7195 wptr += 8;
7196 }
1e2f5b6e 7197 }
3f764659
JJ
7198 rptr += opd_ent_size;
7199 offset += opd_ent_size;
1e2f5b6e
AM
7200 }
7201
50bc7936
AM
7202 if (skip)
7203 {
60124e18
AM
7204 if (!NO_OPD_RELOCS
7205 && !info->relocatable
18d944df
AM
7206 && !dec_dynrel_count (rel->r_info, sec, info,
7207 NULL, h, sym_sec))
8c1d1bb8 7208 goto error_ret;
50bc7936
AM
7209 }
7210 else
1e2f5b6e 7211 {
50bc7936
AM
7212 /* We need to adjust any reloc offsets to point to the
7213 new opd entries. While we're at it, we may as well
7214 remove redundant relocs. */
74f0fb50 7215 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7216 if (write_rel != rel)
7217 memcpy (write_rel, rel, sizeof (*rel));
7218 ++write_rel;
7219 }
7220 }
7221
3f764659 7222 sec->size = wptr - new_contents;
1e2f5b6e 7223 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7224 if (add_aux_fields)
7225 {
7226 free (sec->contents);
7227 sec->contents = new_contents;
7228 }
7229
05bf9422 7230 /* Fudge the header size too, as this is used later in
cdcf6e38 7231 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7232 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7233 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7234 some_edited = TRUE;
1e2f5b6e 7235 }
6cdc0ccc 7236 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7237 free (relstart);
6cdc0ccc 7238
411e1bfb
AM
7239 if (local_syms != NULL
7240 && symtab_hdr->contents != (unsigned char *) local_syms)
7241 {
7242 if (!info->keep_memory)
7243 free (local_syms);
7244 else
7245 symtab_hdr->contents = (unsigned char *) local_syms;
7246 }
7247 }
7248
754021d0
AM
7249 if (some_edited)
7250 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7251
3f764659
JJ
7252 /* If we are doing a final link and the last .opd entry is just 16 byte
7253 long, add a 8 byte padding after it. */
7254 if (need_pad != NULL && !info->relocatable)
7255 {
7256 bfd_byte *p;
7257
7258 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7259 {
7260 BFD_ASSERT (need_pad->size > 0);
7261
7262 p = bfd_malloc (need_pad->size + 8);
7263 if (p == NULL)
7264 return FALSE;
699733f6 7265
3f764659
JJ
7266 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7267 p, 0, need_pad->size))
7268 return FALSE;
7269
7270 need_pad->contents = p;
7271 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7272 }
7273 else
7274 {
7275 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7276 if (p == NULL)
7277 return FALSE;
7278
7279 need_pad->contents = p;
7280 }
7281
7282 memset (need_pad->contents + need_pad->size, 0, 8);
7283 need_pad->size += 8;
7284 }
7285
411e1bfb
AM
7286 return TRUE;
7287}
7288
e1918d23 7289/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7290
e1918d23 7291asection *
33c0ec9d
AM
7292ppc64_elf_tls_setup (struct bfd_link_info *info,
7293 int no_tls_get_addr_opt,
7294 int *no_multi_toc)
411e1bfb 7295{
411e1bfb
AM
7296 struct ppc_link_hash_table *htab;
7297
411e1bfb 7298 htab = ppc_hash_table (info);
4dfe6ac6
NC
7299 if (htab == NULL)
7300 return NULL;
7301
33c0ec9d
AM
7302 if (*no_multi_toc)
7303 htab->do_multi_toc = 0;
7304 else if (!htab->do_multi_toc)
7305 *no_multi_toc = 1;
7306
3a71aa26
AM
7307 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7308 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7309 FALSE, FALSE, TRUE));
a7f2871e
AM
7310 /* Move dynamic linking info to the function descriptor sym. */
7311 if (htab->tls_get_addr != NULL)
7312 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7313 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7314 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7315 FALSE, FALSE, TRUE));
a7f2871e
AM
7316 if (!no_tls_get_addr_opt)
7317 {
7318 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7319
7320 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7321 FALSE, FALSE, TRUE);
7322 if (opt != NULL)
7323 func_desc_adjust (opt, info);
7324 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7325 FALSE, FALSE, TRUE);
7326 if (opt_fd != NULL
7327 && (opt_fd->root.type == bfd_link_hash_defined
7328 || opt_fd->root.type == bfd_link_hash_defweak))
7329 {
7330 /* If glibc supports an optimized __tls_get_addr call stub,
7331 signalled by the presence of __tls_get_addr_opt, and we'll
7332 be calling __tls_get_addr via a plt call stub, then
7333 make __tls_get_addr point to __tls_get_addr_opt. */
7334 tga_fd = &htab->tls_get_addr_fd->elf;
7335 if (htab->elf.dynamic_sections_created
7336 && tga_fd != NULL
7337 && (tga_fd->type == STT_FUNC
7338 || tga_fd->needs_plt)
7339 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7340 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7341 && tga_fd->root.type == bfd_link_hash_undefweak)))
7342 {
7343 struct plt_entry *ent;
7344
7345 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7346 if (ent->plt.refcount > 0)
7347 break;
7348 if (ent != NULL)
7349 {
7350 tga_fd->root.type = bfd_link_hash_indirect;
7351 tga_fd->root.u.i.link = &opt_fd->root;
7352 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7353 if (opt_fd->dynindx != -1)
7354 {
7355 /* Use __tls_get_addr_opt in dynamic relocations. */
7356 opt_fd->dynindx = -1;
7357 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7358 opt_fd->dynstr_index);
7359 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7360 return NULL;
a7f2871e
AM
7361 }
7362 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7363 tga = &htab->tls_get_addr->elf;
7364 if (opt != NULL && tga != NULL)
7365 {
7366 tga->root.type = bfd_link_hash_indirect;
7367 tga->root.u.i.link = &opt->root;
7368 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7369 _bfd_elf_link_hash_hide_symbol (info, opt,
7370 tga->forced_local);
7371 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7372 }
7373 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7374 htab->tls_get_addr_fd->is_func_descriptor = 1;
7375 if (htab->tls_get_addr != NULL)
7376 {
7377 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7378 htab->tls_get_addr->is_func = 1;
7379 }
7380 }
7381 }
7382 }
7383 else
7384 no_tls_get_addr_opt = TRUE;
7385 }
7386 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7387 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7388}
8387904d 7389
3a71aa26
AM
7390/* Return TRUE iff REL is a branch reloc with a global symbol matching
7391 HASH1 or HASH2. */
8387904d 7392
3a71aa26
AM
7393static bfd_boolean
7394branch_reloc_hash_match (const bfd *ibfd,
7395 const Elf_Internal_Rela *rel,
7396 const struct ppc_link_hash_entry *hash1,
7397 const struct ppc_link_hash_entry *hash2)
7398{
7399 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7400 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7401 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7402
e054468f 7403 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7404 {
3a71aa26
AM
7405 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7406 struct elf_link_hash_entry *h;
8387904d 7407
3a71aa26 7408 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7409 h = elf_follow_link (h);
3a71aa26
AM
7410 if (h == &hash1->elf || h == &hash2->elf)
7411 return TRUE;
a48ebf4d 7412 }
3a71aa26 7413 return FALSE;
951fd09b 7414}
411e1bfb 7415
951fd09b
AM
7416/* Run through all the TLS relocs looking for optimization
7417 opportunities. The linker has been hacked (see ppc64elf.em) to do
7418 a preliminary section layout so that we know the TLS segment
7419 offsets. We can't optimize earlier because some optimizations need
7420 to know the tp offset, and we need to optimize before allocating
7421 dynamic relocations. */
7422
7423bfd_boolean
33c0ec9d 7424ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7425{
7426 bfd *ibfd;
7427 asection *sec;
7428 struct ppc_link_hash_table *htab;
663a1470 7429 unsigned char *toc_ref;
102890f0 7430 int pass;
951fd09b 7431
1d483afe 7432 if (info->relocatable || !info->executable)
411e1bfb
AM
7433 return TRUE;
7434
951fd09b 7435 htab = ppc_hash_table (info);
4dfe6ac6
NC
7436 if (htab == NULL)
7437 return FALSE;
7438
663a1470
AM
7439 /* Make two passes over the relocs. On the first pass, mark toc
7440 entries involved with tls relocs, and check that tls relocs
7441 involved in setting up a tls_get_addr call are indeed followed by
7442 such a call. If they are not, we can't do any tls optimization.
7443 On the second pass twiddle tls_mask flags to notify
7444 relocate_section that optimization can be done, and adjust got
7445 and plt refcounts. */
7446 toc_ref = NULL;
7447 for (pass = 0; pass < 2; ++pass)
7448 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7449 {
7450 Elf_Internal_Sym *locsyms = NULL;
7451 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7452
102890f0
AM
7453 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7454 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7455 {
7456 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7457 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7458
102890f0
AM
7459 /* Read the relocations. */
7460 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7461 info->keep_memory);
7462 if (relstart == NULL)
7463 return FALSE;
411e1bfb 7464
102890f0
AM
7465 relend = relstart + sec->reloc_count;
7466 for (rel = relstart; rel < relend; rel++)
7467 {
7468 enum elf_ppc64_reloc_type r_type;
7469 unsigned long r_symndx;
7470 struct elf_link_hash_entry *h;
7471 Elf_Internal_Sym *sym;
7472 asection *sym_sec;
f961d9dd
AM
7473 unsigned char *tls_mask;
7474 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7475 bfd_vma value;
7476 bfd_boolean ok_tprel, is_local;
7477 long toc_ref_index = 0;
7478 int expecting_tls_get_addr = 0;
663a1470 7479 bfd_boolean ret = FALSE;
411e1bfb 7480
102890f0
AM
7481 r_symndx = ELF64_R_SYM (rel->r_info);
7482 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7483 r_symndx, ibfd))
7484 {
7485 err_free_rel:
7486 if (elf_section_data (sec)->relocs != relstart)
7487 free (relstart);
7488 if (toc_ref != NULL)
7489 free (toc_ref);
7490 if (locsyms != NULL
0ffa91dd 7491 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7492 != (unsigned char *) locsyms))
7493 free (locsyms);
663a1470 7494 return ret;
102890f0 7495 }
411e1bfb 7496
102890f0
AM
7497 if (h != NULL)
7498 {
766bc656
AM
7499 if (h->root.type == bfd_link_hash_defined
7500 || h->root.type == bfd_link_hash_defweak)
7501 value = h->root.u.def.value;
7502 else if (h->root.type == bfd_link_hash_undefweak)
7503 value = 0;
7504 else
663a1470
AM
7505 {
7506 found_tls_get_addr_arg = 0;
7507 continue;
7508 }
102890f0
AM
7509 }
7510 else
7511 /* Symbols referenced by TLS relocs must be of type
7512 STT_TLS. So no need for .opd local sym adjust. */
7513 value = sym->st_value;
7514
7515 ok_tprel = FALSE;
7516 is_local = FALSE;
7517 if (h == NULL
7518 || !h->def_dynamic)
7519 {
7520 is_local = TRUE;
766bc656
AM
7521 if (h != NULL
7522 && h->root.type == bfd_link_hash_undefweak)
7523 ok_tprel = TRUE;
7524 else
7525 {
7526 value += sym_sec->output_offset;
7527 value += sym_sec->output_section->vma;
7528 value -= htab->elf.tls_sec->vma;
7529 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7530 < (bfd_vma) 1 << 32);
7531 }
102890f0 7532 }
951fd09b 7533
102890f0 7534 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7535 /* If this section has old-style __tls_get_addr calls
7536 without marker relocs, then check that each
7537 __tls_get_addr call reloc is preceded by a reloc
7538 that conceivably belongs to the __tls_get_addr arg
7539 setup insn. If we don't find matching arg setup
7540 relocs, don't do any tls optimization. */
7541 if (pass == 0
7542 && sec->has_tls_get_addr_call
7543 && h != NULL
7544 && (h == &htab->tls_get_addr->elf
7545 || h == &htab->tls_get_addr_fd->elf)
7546 && !found_tls_get_addr_arg
7547 && is_branch_reloc (r_type))
7548 {
25f53a85 7549 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
663a1470
AM
7550 "TLS optimization disabled\n"),
7551 ibfd, sec, rel->r_offset);
7552 ret = TRUE;
7553 goto err_free_rel;
7554 }
7555
7556 found_tls_get_addr_arg = 0;
102890f0
AM
7557 switch (r_type)
7558 {
7559 case R_PPC64_GOT_TLSLD16:
7560 case R_PPC64_GOT_TLSLD16_LO:
7561 expecting_tls_get_addr = 1;
663a1470 7562 found_tls_get_addr_arg = 1;
102890f0
AM
7563 /* Fall thru */
7564
7565 case R_PPC64_GOT_TLSLD16_HI:
7566 case R_PPC64_GOT_TLSLD16_HA:
7567 /* These relocs should never be against a symbol
7568 defined in a shared lib. Leave them alone if
7569 that turns out to be the case. */
7570 if (!is_local)
7571 continue;
411e1bfb 7572
102890f0 7573 /* LD -> LE */
411e1bfb 7574 tls_set = 0;
102890f0
AM
7575 tls_clear = TLS_LD;
7576 tls_type = TLS_TLS | TLS_LD;
7577 break;
411e1bfb 7578
102890f0
AM
7579 case R_PPC64_GOT_TLSGD16:
7580 case R_PPC64_GOT_TLSGD16_LO:
7581 expecting_tls_get_addr = 1;
663a1470 7582 found_tls_get_addr_arg = 1;
102890f0
AM
7583 /* Fall thru */
7584
7585 case R_PPC64_GOT_TLSGD16_HI:
7586 case R_PPC64_GOT_TLSGD16_HA:
7587 if (ok_tprel)
7588 /* GD -> LE */
411e1bfb 7589 tls_set = 0;
102890f0
AM
7590 else
7591 /* GD -> IE */
7592 tls_set = TLS_TLS | TLS_TPRELGD;
7593 tls_clear = TLS_GD;
7594 tls_type = TLS_TLS | TLS_GD;
7595 break;
7596
7597 case R_PPC64_GOT_TPREL16_DS:
7598 case R_PPC64_GOT_TPREL16_LO_DS:
7599 case R_PPC64_GOT_TPREL16_HI:
7600 case R_PPC64_GOT_TPREL16_HA:
7601 if (ok_tprel)
7602 {
7603 /* IE -> LE */
7604 tls_set = 0;
7605 tls_clear = TLS_TPREL;
7606 tls_type = TLS_TLS | TLS_TPREL;
7607 break;
7608 }
411e1bfb
AM
7609 continue;
7610
727fc41e
AM
7611 case R_PPC64_TLSGD:
7612 case R_PPC64_TLSLD:
663a1470
AM
7613 found_tls_get_addr_arg = 1;
7614 /* Fall thru */
7615
7616 case R_PPC64_TLS:
7617 case R_PPC64_TOC16:
7618 case R_PPC64_TOC16_LO:
102890f0
AM
7619 if (sym_sec == NULL || sym_sec != toc)
7620 continue;
7621
7622 /* Mark this toc entry as referenced by a TLS
7623 code sequence. We can do that now in the
7624 case of R_PPC64_TLS, and after checking for
7625 tls_get_addr for the TOC16 relocs. */
7626 if (toc_ref == NULL)
663a1470
AM
7627 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7628 if (toc_ref == NULL)
7629 goto err_free_rel;
7630
102890f0
AM
7631 if (h != NULL)
7632 value = h->root.u.def.value;
7633 else
7634 value = sym->st_value;
7635 value += rel->r_addend;
7636 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 7637 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7638 if (r_type == R_PPC64_TLS
7639 || r_type == R_PPC64_TLSGD
7640 || r_type == R_PPC64_TLSLD)
102890f0
AM
7641 {
7642 toc_ref[toc_ref_index] = 1;
7643 continue;
7644 }
7645
7646 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7647 continue;
7648
7649 tls_set = 0;
7650 tls_clear = 0;
7651 expecting_tls_get_addr = 2;
7652 break;
7653
7654 case R_PPC64_TPREL64:
7655 if (pass == 0
7656 || sec != toc
7657 || toc_ref == NULL
663a1470 7658 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7659 continue;
7660 if (ok_tprel)
7661 {
7662 /* IE -> LE */
7663 tls_set = TLS_EXPLICIT;
7664 tls_clear = TLS_TPREL;
7665 break;
7666 }
7667 continue;
7668
7669 case R_PPC64_DTPMOD64:
7670 if (pass == 0
7671 || sec != toc
7672 || toc_ref == NULL
663a1470 7673 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7674 continue;
7675 if (rel + 1 < relend
7676 && (rel[1].r_info
7677 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7678 && rel[1].r_offset == rel->r_offset + 8)
7679 {
7680 if (ok_tprel)
7681 /* GD -> LE */
7682 tls_set = TLS_EXPLICIT | TLS_GD;
7683 else
7684 /* GD -> IE */
7685 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7686 tls_clear = TLS_GD;
7687 }
7688 else
7689 {
7690 if (!is_local)
7691 continue;
7692
7693 /* LD -> LE */
7694 tls_set = TLS_EXPLICIT;
7695 tls_clear = TLS_LD;
7696 }
7697 break;
7698
7699 default:
7700 continue;
7701 }
7702
7703 if (pass == 0)
7704 {
727fc41e
AM
7705 if (!expecting_tls_get_addr
7706 || !sec->has_tls_get_addr_call)
102890f0
AM
7707 continue;
7708
3a71aa26
AM
7709 if (rel + 1 < relend
7710 && branch_reloc_hash_match (ibfd, rel + 1,
7711 htab->tls_get_addr,
7712 htab->tls_get_addr_fd))
102890f0 7713 {
3a71aa26 7714 if (expecting_tls_get_addr == 2)
102890f0 7715 {
3a71aa26 7716 /* Check for toc tls entries. */
f961d9dd 7717 unsigned char *toc_tls;
3a71aa26
AM
7718 int retval;
7719
7720 retval = get_tls_mask (&toc_tls, NULL, NULL,
7721 &locsyms,
7722 rel, ibfd);
7723 if (retval == 0)
7724 goto err_free_rel;
663a1470
AM
7725 if (toc_tls != NULL)
7726 {
7727 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7728 found_tls_get_addr_arg = 1;
7729 if (retval > 1)
7730 toc_ref[toc_ref_index] = 1;
7731 }
102890f0 7732 }
3a71aa26 7733 continue;
102890f0
AM
7734 }
7735
7736 if (expecting_tls_get_addr != 1)
7737 continue;
7738
7739 /* Uh oh, we didn't find the expected call. We
7740 could just mark this symbol to exclude it
7741 from tls optimization but it's safer to skip
663a1470 7742 the entire optimization. */
25f53a85 7743 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
663a1470
AM
7744 "TLS optimization disabled\n"),
7745 ibfd, sec, rel->r_offset);
7746 ret = TRUE;
7747 goto err_free_rel;
102890f0
AM
7748 }
7749
85f7a9cb 7750 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
7751 {
7752 struct plt_entry *ent;
7753 for (ent = htab->tls_get_addr->elf.plt.plist;
7754 ent != NULL;
7755 ent = ent->next)
7756 if (ent->addend == 0)
411e1bfb 7757 {
102890f0 7758 if (ent->plt.refcount > 0)
30038c59 7759 {
102890f0
AM
7760 ent->plt.refcount -= 1;
7761 expecting_tls_get_addr = 0;
30038c59 7762 }
102890f0 7763 break;
411e1bfb 7764 }
102890f0 7765 }
411e1bfb 7766
85f7a9cb 7767 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
7768 {
7769 struct plt_entry *ent;
7770 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7771 ent != NULL;
7772 ent = ent->next)
7773 if (ent->addend == 0)
411e1bfb 7774 {
102890f0
AM
7775 if (ent->plt.refcount > 0)
7776 ent->plt.refcount -= 1;
7777 break;
411e1bfb 7778 }
102890f0 7779 }
411e1bfb 7780
102890f0 7781 if (tls_clear == 0)
30038c59
AM
7782 continue;
7783
102890f0
AM
7784 if ((tls_set & TLS_EXPLICIT) == 0)
7785 {
7786 struct got_entry *ent;
411e1bfb 7787
102890f0
AM
7788 /* Adjust got entry for this reloc. */
7789 if (h != NULL)
7790 ent = h->got.glist;
7791 else
7792 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7793
102890f0
AM
7794 for (; ent != NULL; ent = ent->next)
7795 if (ent->addend == rel->r_addend
7796 && ent->owner == ibfd
7797 && ent->tls_type == tls_type)
7798 break;
7799 if (ent == NULL)
7800 abort ();
411e1bfb 7801
102890f0
AM
7802 if (tls_set == 0)
7803 {
7804 /* We managed to get rid of a got entry. */
7805 if (ent->got.refcount > 0)
7806 ent->got.refcount -= 1;
7807 }
7808 }
7809 else
7810 {
7811 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7812 we'll lose one or two dyn relocs. */
7813 if (!dec_dynrel_count (rel->r_info, sec, info,
7814 NULL, h, sym_sec))
7815 return FALSE;
411e1bfb 7816
102890f0
AM
7817 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7818 {
7819 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7820 NULL, h, sym_sec))
7821 return FALSE;
7822 }
7823 }
411e1bfb 7824
102890f0
AM
7825 *tls_mask |= tls_set;
7826 *tls_mask &= ~tls_clear;
7827 }
8c1d1bb8 7828
102890f0
AM
7829 if (elf_section_data (sec)->relocs != relstart)
7830 free (relstart);
7831 }
411e1bfb 7832
663a1470
AM
7833 if (locsyms != NULL
7834 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7835 {
7836 if (!info->keep_memory)
7837 free (locsyms);
7838 else
7839 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7840 }
7841 }
411e1bfb 7842
663a1470
AM
7843 if (toc_ref != NULL)
7844 free (toc_ref);
b34976b6 7845 return TRUE;
1e2f5b6e 7846}
b34976b6 7847
c5614fa4
AM
7848/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7849 the values of any global symbols in a toc section that has been
7850 edited. Globals in toc sections should be a rarity, so this function
7851 sets a flag if any are found in toc sections other than the one just
7852 edited, so that futher hash table traversals can be avoided. */
7853
7854struct adjust_toc_info
7855{
7856 asection *toc;
7857 unsigned long *skip;
7858 bfd_boolean global_toc_syms;
7859};
7860
ba761f19
AM
7861enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7862
c5614fa4
AM
7863static bfd_boolean
7864adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7865{
7866 struct ppc_link_hash_entry *eh;
7867 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7868 unsigned long i;
c5614fa4
AM
7869
7870 if (h->root.type == bfd_link_hash_indirect)
7871 return TRUE;
7872
c5614fa4
AM
7873 if (h->root.type != bfd_link_hash_defined
7874 && h->root.type != bfd_link_hash_defweak)
7875 return TRUE;
7876
7877 eh = (struct ppc_link_hash_entry *) h;
7878 if (eh->adjust_done)
7879 return TRUE;
7880
7881 if (eh->elf.root.u.def.section == toc_inf->toc)
7882 {
854b41e7
AM
7883 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7884 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7885 else
854b41e7
AM
7886 i = eh->elf.root.u.def.value >> 3;
7887
ba761f19 7888 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
7889 {
7890 (*_bfd_error_handler)
854b41e7
AM
7891 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7892 do
7893 ++i;
ba761f19 7894 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 7895 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 7896 }
854b41e7
AM
7897
7898 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
7899 eh->adjust_done = 1;
7900 }
7901 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7902 toc_inf->global_toc_syms = TRUE;
7903
7904 return TRUE;
7905}
7906
7907/* Examine all relocs referencing .toc sections in order to remove
7908 unused .toc entries. */
7909
7910bfd_boolean
33c0ec9d 7911ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
7912{
7913 bfd *ibfd;
7914 struct adjust_toc_info toc_inf;
67f0cbdb 7915 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 7916
67f0cbdb 7917 htab->do_toc_opt = 1;
c5614fa4
AM
7918 toc_inf.global_toc_syms = TRUE;
7919 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7920 {
7921 asection *toc, *sec;
7922 Elf_Internal_Shdr *symtab_hdr;
7923 Elf_Internal_Sym *local_syms;
425b145b 7924 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
7925 unsigned long *skip, *drop;
7926 unsigned char *used;
7927 unsigned char *keep, last, some_unused;
7928
854b41e7
AM
7929 if (!is_ppc64_elf (ibfd))
7930 continue;
7931
c5614fa4
AM
7932 toc = bfd_get_section_by_name (ibfd, ".toc");
7933 if (toc == NULL
92b7a70f 7934 || toc->size == 0
c5614fa4
AM
7935 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7936 || elf_discarded_section (toc))
7937 continue;
7938
425b145b 7939 toc_relocs = NULL;
c5614fa4 7940 local_syms = NULL;
0ffa91dd 7941 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
7942
7943 /* Look at sections dropped from the final link. */
7944 skip = NULL;
7945 relstart = NULL;
7946 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7947 {
7948 if (sec->reloc_count == 0
7949 || !elf_discarded_section (sec)
7950 || get_opd_info (sec)
7951 || (sec->flags & SEC_ALLOC) == 0
7952 || (sec->flags & SEC_DEBUGGING) != 0)
7953 continue;
7954
7955 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7956 if (relstart == NULL)
7957 goto error_ret;
7958
7959 /* Run through the relocs to see which toc entries might be
7960 unused. */
7961 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7962 {
7963 enum elf_ppc64_reloc_type r_type;
7964 unsigned long r_symndx;
7965 asection *sym_sec;
7966 struct elf_link_hash_entry *h;
7967 Elf_Internal_Sym *sym;
7968 bfd_vma val;
7969
7970 r_type = ELF64_R_TYPE (rel->r_info);
7971 switch (r_type)
7972 {
7973 default:
7974 continue;
7975
7976 case R_PPC64_TOC16:
7977 case R_PPC64_TOC16_LO:
7978 case R_PPC64_TOC16_HI:
7979 case R_PPC64_TOC16_HA:
7980 case R_PPC64_TOC16_DS:
7981 case R_PPC64_TOC16_LO_DS:
7982 break;
7983 }
7984
7985 r_symndx = ELF64_R_SYM (rel->r_info);
7986 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7987 r_symndx, ibfd))
7988 goto error_ret;
7989
7990 if (sym_sec != toc)
7991 continue;
7992
7993 if (h != NULL)
7994 val = h->root.u.def.value;
7995 else
7996 val = sym->st_value;
7997 val += rel->r_addend;
7998
7999 if (val >= toc->size)
8000 continue;
8001
8002 /* Anything in the toc ought to be aligned to 8 bytes.
8003 If not, don't mark as unused. */
8004 if (val & 7)
8005 continue;
8006
8007 if (skip == NULL)
8008 {
854b41e7 8009 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8010 if (skip == NULL)
8011 goto error_ret;
8012 }
8013
ba761f19 8014 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8015 }
8016
8017 if (elf_section_data (sec)->relocs != relstart)
8018 free (relstart);
8019 }
8020
ba761f19
AM
8021 /* For largetoc loads of address constants, we can convert
8022 . addis rx,2,addr@got@ha
8023 . ld ry,addr@got@l(rx)
8024 to
8025 . addis rx,2,addr@toc@ha
8026 . addi ry,rx,addr@toc@l
8027 when addr is within 2G of the toc pointer. This then means
8028 that the word storing "addr" in the toc is no longer needed. */
8029
8030 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8031 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8032 && toc->reloc_count != 0)
8033 {
8034 /* Read toc relocs. */
425b145b
AM
8035 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8036 info->keep_memory);
8037 if (toc_relocs == NULL)
ba761f19
AM
8038 goto error_ret;
8039
425b145b 8040 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8041 {
8042 enum elf_ppc64_reloc_type r_type;
8043 unsigned long r_symndx;
8044 asection *sym_sec;
8045 struct elf_link_hash_entry *h;
8046 Elf_Internal_Sym *sym;
8047 bfd_vma val, addr;
8048
8049 r_type = ELF64_R_TYPE (rel->r_info);
8050 if (r_type != R_PPC64_ADDR64)
8051 continue;
8052
8053 r_symndx = ELF64_R_SYM (rel->r_info);
8054 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8055 r_symndx, ibfd))
8056 goto error_ret;
8057
425b145b
AM
8058 if (sym_sec == NULL
8059 || elf_discarded_section (sym_sec))
8060 continue;
8061
726d3ab0 8062 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8063 continue;
8064
8065 if (h != NULL)
bddc25c9
AM
8066 {
8067 if (h->type == STT_GNU_IFUNC)
8068 continue;
8069 val = h->root.u.def.value;
8070 }
ba761f19 8071 else
bddc25c9
AM
8072 {
8073 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8074 continue;
8075 val = sym->st_value;
8076 }
ba761f19
AM
8077 val += rel->r_addend;
8078 val += sym_sec->output_section->vma + sym_sec->output_offset;
8079
8080 /* We don't yet know the exact toc pointer value, but we
8081 know it will be somewhere in the toc section. Don't
8082 optimize if the difference from any possible toc
8083 pointer is outside [ff..f80008000, 7fff7fff]. */
8084 addr = toc->output_section->vma + TOC_BASE_OFF;
8085 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8086 continue;
8087
8088 addr = toc->output_section->vma + toc->output_section->rawsize;
8089 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8090 continue;
8091
8092 if (skip == NULL)
8093 {
8094 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8095 if (skip == NULL)
8096 goto error_ret;
8097 }
8098
8099 skip[rel->r_offset >> 3]
425b145b 8100 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8101 }
ba761f19
AM
8102 }
8103
c5614fa4
AM
8104 if (skip == NULL)
8105 continue;
8106
8107 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8108 if (used == NULL)
8109 {
8110 error_ret:
8111 if (local_syms != NULL
8112 && symtab_hdr->contents != (unsigned char *) local_syms)
8113 free (local_syms);
8114 if (sec != NULL
8115 && relstart != NULL
8116 && elf_section_data (sec)->relocs != relstart)
8117 free (relstart);
425b145b
AM
8118 if (toc_relocs != NULL
8119 && elf_section_data (toc)->relocs != toc_relocs)
8120 free (toc_relocs);
c5614fa4
AM
8121 if (skip != NULL)
8122 free (skip);
8123 return FALSE;
8124 }
8125
30038c59
AM
8126 /* Now check all kept sections that might reference the toc.
8127 Check the toc itself last. */
8128 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8129 : ibfd->sections);
c5614fa4 8130 sec != NULL;
c5614fa4 8131 sec = (sec == toc ? NULL
c5614fa4 8132 : sec->next == NULL ? toc
30038c59 8133 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8134 : sec->next))
8135 {
8136 int repeat;
8137
8138 if (sec->reloc_count == 0
8139 || elf_discarded_section (sec)
8140 || get_opd_info (sec)
8141 || (sec->flags & SEC_ALLOC) == 0
8142 || (sec->flags & SEC_DEBUGGING) != 0)
8143 continue;
8144
854b41e7
AM
8145 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8146 info->keep_memory);
c5614fa4
AM
8147 if (relstart == NULL)
8148 goto error_ret;
8149
8150 /* Mark toc entries referenced as used. */
8151 repeat = 0;
8152 do
8153 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8154 {
8155 enum elf_ppc64_reloc_type r_type;
8156 unsigned long r_symndx;
8157 asection *sym_sec;
8158 struct elf_link_hash_entry *h;
8159 Elf_Internal_Sym *sym;
8160 bfd_vma val;
8161
8162 r_type = ELF64_R_TYPE (rel->r_info);
8163 switch (r_type)
8164 {
8165 case R_PPC64_TOC16:
8166 case R_PPC64_TOC16_LO:
8167 case R_PPC64_TOC16_HI:
8168 case R_PPC64_TOC16_HA:
8169 case R_PPC64_TOC16_DS:
8170 case R_PPC64_TOC16_LO_DS:
8171 /* In case we're taking addresses of toc entries. */
8172 case R_PPC64_ADDR64:
8173 break;
8174
8175 default:
8176 continue;
8177 }
8178
8179 r_symndx = ELF64_R_SYM (rel->r_info);
8180 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8181 r_symndx, ibfd))
8182 {
8183 free (used);
8184 goto error_ret;
8185 }
8186
8187 if (sym_sec != toc)
8188 continue;
8189
8190 if (h != NULL)
8191 val = h->root.u.def.value;
8192 else
8193 val = sym->st_value;
8194 val += rel->r_addend;
8195
8196 if (val >= toc->size)
8197 continue;
8198
ba761f19
AM
8199 if ((skip[val >> 3] & can_optimize) != 0)
8200 {
8201 bfd_vma off;
8202 unsigned char opc;
8203
8204 switch (r_type)
8205 {
8206 case R_PPC64_TOC16_HA:
8207 break;
8208
8209 case R_PPC64_TOC16_LO_DS:
8210 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8211 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8212 return FALSE;
8213 if ((opc & (0x3f << 2)) == (58u << 2))
8214 break;
8215 /* Fall thru */
8216
8217 default:
8218 /* Wrong sort of reloc, or not a ld. We may
8219 as well clear ref_from_discarded too. */
8220 skip[val >> 3] = 0;
8221 }
8222 }
8223
c5614fa4
AM
8224 /* For the toc section, we only mark as used if
8225 this entry itself isn't unused. */
8226 if (sec == toc
8227 && !used[val >> 3]
8228 && (used[rel->r_offset >> 3]
ba761f19 8229 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
c5614fa4
AM
8230 /* Do all the relocs again, to catch reference
8231 chains. */
8232 repeat = 1;
8233
8234 used[val >> 3] = 1;
8235 }
8236 while (repeat);
854b41e7
AM
8237
8238 if (elf_section_data (sec)->relocs != relstart)
8239 free (relstart);
c5614fa4
AM
8240 }
8241
8242 /* Merge the used and skip arrays. Assume that TOC
8243 doublewords not appearing as either used or unused belong
8244 to to an entry more than one doubleword in size. */
8245 for (drop = skip, keep = used, last = 0, some_unused = 0;
8246 drop < skip + (toc->size + 7) / 8;
8247 ++drop, ++keep)
8248 {
8249 if (*keep)
8250 {
ba761f19
AM
8251 *drop &= ~ref_from_discarded;
8252 if ((*drop & can_optimize) != 0)
8253 some_unused = 1;
c5614fa4
AM
8254 last = 0;
8255 }
8256 else if (*drop)
8257 {
8258 some_unused = 1;
ba761f19 8259 last = ref_from_discarded;
c5614fa4
AM
8260 }
8261 else
8262 *drop = last;
8263 }
8264
8265 free (used);
8266
8267 if (some_unused)
8268 {
8269 bfd_byte *contents, *src;
8270 unsigned long off;
d62b3684 8271 Elf_Internal_Sym *sym;
ba761f19 8272 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8273
8274 /* Shuffle the toc contents, and at the same time convert the
8275 skip array from booleans into offsets. */
8276 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8277 goto error_ret;
8278
8279 elf_section_data (toc)->this_hdr.contents = contents;
8280
8281 for (src = contents, off = 0, drop = skip;
8282 src < contents + toc->size;
8283 src += 8, ++drop)
8284 {
ba761f19
AM
8285 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8286 off += 8;
c5614fa4
AM
8287 else if (off != 0)
8288 {
8289 *drop = off;
8290 memcpy (src - off, src, 8);
8291 }
8292 }
854b41e7 8293 *drop = off;
c5614fa4
AM
8294 toc->rawsize = toc->size;
8295 toc->size = src - contents - off;
8296
ba761f19
AM
8297 /* Adjust addends for relocs against the toc section sym,
8298 and optimize any accesses we can. */
c5614fa4
AM
8299 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8300 {
8301 if (sec->reloc_count == 0
8302 || elf_discarded_section (sec))
8303 continue;
8304
8305 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8306 info->keep_memory);
c5614fa4
AM
8307 if (relstart == NULL)
8308 goto error_ret;
8309
8310 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8311 {
8312 enum elf_ppc64_reloc_type r_type;
8313 unsigned long r_symndx;
8314 asection *sym_sec;
8315 struct elf_link_hash_entry *h;
854b41e7 8316 bfd_vma val;
c5614fa4
AM
8317
8318 r_type = ELF64_R_TYPE (rel->r_info);
8319 switch (r_type)
8320 {
8321 default:
8322 continue;
8323
8324 case R_PPC64_TOC16:
8325 case R_PPC64_TOC16_LO:
8326 case R_PPC64_TOC16_HI:
8327 case R_PPC64_TOC16_HA:
8328 case R_PPC64_TOC16_DS:
8329 case R_PPC64_TOC16_LO_DS:
8330 case R_PPC64_ADDR64:
8331 break;
8332 }
8333
8334 r_symndx = ELF64_R_SYM (rel->r_info);
8335 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8336 r_symndx, ibfd))
8337 goto error_ret;
8338
ba761f19 8339 if (sym_sec != toc)
c5614fa4
AM
8340 continue;
8341
ba761f19
AM
8342 if (h != NULL)
8343 val = h->root.u.def.value;
8344 else
8345 {
8346 val = sym->st_value;
8347 if (val != 0)
8348 local_toc_syms = TRUE;
8349 }
8350
8351 val += rel->r_addend;
854b41e7
AM
8352
8353 if (val > toc->rawsize)
8354 val = toc->rawsize;
ba761f19
AM
8355 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8356 continue;
8357 else if ((skip[val >> 3] & can_optimize) != 0)
8358 {
8359 Elf_Internal_Rela *tocrel
425b145b 8360 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8361 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8362
8363 switch (r_type)
8364 {
8365 case R_PPC64_TOC16_HA:
8366 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8367 break;
8368
8369 case R_PPC64_TOC16_LO_DS:
8370 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8371 break;
8372
8373 default:
8374 abort ();
8375 }
8376 rel->r_addend = tocrel->r_addend;
8377 elf_section_data (sec)->relocs = relstart;
8378 continue;
8379 }
8380
8381 if (h != NULL || sym->st_value != 0)
8382 continue;
854b41e7
AM
8383
8384 rel->r_addend -= skip[val >> 3];
8385 elf_section_data (sec)->relocs = relstart;
c5614fa4 8386 }
854b41e7
AM
8387
8388 if (elf_section_data (sec)->relocs != relstart)
8389 free (relstart);
c5614fa4
AM
8390 }
8391
8392 /* We shouldn't have local or global symbols defined in the TOC,
8393 but handle them anyway. */
df22d223
AM
8394 if (local_syms != NULL)
8395 for (sym = local_syms;
8396 sym < local_syms + symtab_hdr->sh_info;
8397 ++sym)
8398 if (sym->st_value != 0
8399 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8400 {
8401 unsigned long i;
854b41e7 8402
df22d223
AM
8403 if (sym->st_value > toc->rawsize)
8404 i = toc->rawsize >> 3;
8405 else
8406 i = sym->st_value >> 3;
854b41e7 8407
df22d223
AM
8408 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8409 {
8410 if (local_toc_syms)
8411 (*_bfd_error_handler)
8412 (_("%s defined on removed toc entry"),
8413 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8414 do
8415 ++i;
8416 while ((skip[i] & (ref_from_discarded | can_optimize)));
8417 sym->st_value = (bfd_vma) i << 3;
8418 }
d62b3684 8419
df22d223
AM
8420 sym->st_value -= skip[i];
8421 symtab_hdr->contents = (unsigned char *) local_syms;
8422 }
c5614fa4 8423
854b41e7 8424 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8425 if (toc_inf.global_toc_syms)
8426 {
8427 toc_inf.toc = toc;
8428 toc_inf.skip = skip;
8429 toc_inf.global_toc_syms = FALSE;
8430 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8431 &toc_inf);
8432 }
854b41e7
AM
8433
8434 if (toc->reloc_count != 0)
8435 {
d4730f92 8436 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8437 Elf_Internal_Rela *wrel;
8438 bfd_size_type sz;
8439
854b41e7 8440 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8441 if (toc_relocs == NULL)
8442 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8443 info->keep_memory);
8444 if (toc_relocs == NULL)
8445 goto error_ret;
8446
425b145b
AM
8447 wrel = toc_relocs;
8448 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8449 if ((skip[rel->r_offset >> 3]
8450 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8451 {
8452 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8453 wrel->r_info = rel->r_info;
8454 wrel->r_addend = rel->r_addend;
8455 ++wrel;
8456 }
8457 else if (!dec_dynrel_count (rel->r_info, toc, info,
8458 &local_syms, NULL, NULL))
8459 goto error_ret;
8460
425b145b
AM
8461 elf_section_data (toc)->relocs = toc_relocs;
8462 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8463 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8464 sz = rel_hdr->sh_entsize;
8465 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8466 }
c5614fa4 8467 }
28be611c
AM
8468 else if (toc_relocs != NULL
8469 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8470 free (toc_relocs);
c5614fa4
AM
8471
8472 if (local_syms != NULL
8473 && symtab_hdr->contents != (unsigned char *) local_syms)
8474 {
8475 if (!info->keep_memory)
8476 free (local_syms);
8477 else
8478 symtab_hdr->contents = (unsigned char *) local_syms;
8479 }
8480 free (skip);
8481 }
8482
8483 return TRUE;
8484}
8485
1bbe0902
AM
8486/* Return true iff input section I references the TOC using
8487 instructions limited to +/-32k offsets. */
8488
8489bfd_boolean
8490ppc64_elf_has_small_toc_reloc (asection *i)
8491{
8492 return (is_ppc64_elf (i->owner)
8493 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8494}
8495
927be08e
AM
8496/* Allocate space for one GOT entry. */
8497
8498static void
8499allocate_got (struct elf_link_hash_entry *h,
8500 struct bfd_link_info *info,
8501 struct got_entry *gent)
8502{
8503 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8504 bfd_boolean dyn;
8505 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8506 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8507 ? 16 : 8);
8508 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8509 ? 2 : 1) * sizeof (Elf64_External_Rela);
8510 asection *got = ppc64_elf_tdata (gent->owner)->got;
8511
8512 gent->got.offset = got->size;
8513 got->size += entsize;
8514
8515 dyn = htab->elf.dynamic_sections_created;
8516 if ((info->shared
8517 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8518 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8519 || h->root.type != bfd_link_hash_undefweak))
8520 {
8521 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8522 relgot->size += rentsize;
8523 }
8524 else if (h->type == STT_GNU_IFUNC)
8525 {
8526 asection *relgot = htab->reliplt;
8527 relgot->size += rentsize;
8528 htab->got_reli_size += rentsize;
8529 }
8530}
8531
7865406b
AM
8532/* This function merges got entries in the same toc group. */
8533
8534static void
8535merge_got_entries (struct got_entry **pent)
8536{
8537 struct got_entry *ent, *ent2;
8538
8539 for (ent = *pent; ent != NULL; ent = ent->next)
8540 if (!ent->is_indirect)
8541 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8542 if (!ent2->is_indirect
8543 && ent2->addend == ent->addend
8544 && ent2->tls_type == ent->tls_type
8545 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8546 {
8547 ent2->is_indirect = TRUE;
8548 ent2->got.ent = ent;
8549 }
8550}
8551
65f38f15
AM
8552/* Allocate space in .plt, .got and associated reloc sections for
8553 dynamic relocs. */
5bd4f169 8554
b34976b6 8555static bfd_boolean
4ce794b7 8556allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8557{
65f38f15
AM
8558 struct bfd_link_info *info;
8559 struct ppc_link_hash_table *htab;
5bd4f169 8560 asection *s;
65f38f15 8561 struct ppc_link_hash_entry *eh;
6061a67d 8562 struct elf_dyn_relocs *p;
0b8bcf0d 8563 struct got_entry **pgent, *gent;
5bd4f169 8564
e92d460e 8565 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8566 return TRUE;
5bd4f169 8567
65f38f15
AM
8568 info = (struct bfd_link_info *) inf;
8569 htab = ppc_hash_table (info);
4dfe6ac6
NC
8570 if (htab == NULL)
8571 return FALSE;
5bd4f169 8572
e054468f
AM
8573 if ((htab->elf.dynamic_sections_created
8574 && h->dynindx != -1
8575 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8576 || h->type == STT_GNU_IFUNC)
5bd4f169 8577 {
411e1bfb
AM
8578 struct plt_entry *pent;
8579 bfd_boolean doneone = FALSE;
8580 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8581 if (pent->plt.refcount > 0)
8582 {
25f23106
AM
8583 if (!htab->elf.dynamic_sections_created
8584 || h->dynindx == -1)
e054468f
AM
8585 {
8586 s = htab->iplt;
8587 pent->plt.offset = s->size;
8588 s->size += PLT_ENTRY_SIZE;
8589 s = htab->reliplt;
8590 }
8591 else
8592 {
8593 /* If this is the first .plt entry, make room for the special
8594 first entry. */
8595 s = htab->plt;
8596 if (s->size == 0)
8597 s->size += PLT_INITIAL_ENTRY_SIZE;
8598
8599 pent->plt.offset = s->size;
8600
8601 /* Make room for this entry. */
8602 s->size += PLT_ENTRY_SIZE;
8603
8604 /* Make room for the .glink code. */
8605 s = htab->glink;
8606 if (s->size == 0)
8607 s->size += GLINK_CALL_STUB_SIZE;
8608 /* We need bigger stubs past index 32767. */
8609 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8610 s->size += 4;
8611 s->size += 2*4;
8612
8613 /* We also need to make an entry in the .rela.plt section. */
8614 s = htab->relplt;
8615 }
eea6121a 8616 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
8617 doneone = TRUE;
8618 }
8619 else
8620 pent->plt.offset = (bfd_vma) -1;
8621 if (!doneone)
65f38f15 8622 {
411e1bfb 8623 h->plt.plist = NULL;
f5385ebf 8624 h->needs_plt = 0;
65f38f15
AM
8625 }
8626 }
8627 else
8628 {
411e1bfb 8629 h->plt.plist = NULL;
f5385ebf 8630 h->needs_plt = 0;
65f38f15
AM
8631 }
8632
951fd09b
AM
8633 eh = (struct ppc_link_hash_entry *) h;
8634 /* Run through the TLS GD got entries first if we're changing them
8635 to TPREL. */
e7b938ca 8636 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
8637 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8638 if (gent->got.refcount > 0
8639 && (gent->tls_type & TLS_GD) != 0)
8640 {
8641 /* This was a GD entry that has been converted to TPREL. If
8642 there happens to be a TPREL entry we can use that one. */
8643 struct got_entry *ent;
8644 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8645 if (ent->got.refcount > 0
8646 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8647 && ent->addend == gent->addend
8648 && ent->owner == gent->owner)
951fd09b
AM
8649 {
8650 gent->got.refcount = 0;
8651 break;
8652 }
8653
8654 /* If not, then we'll be using our own TPREL entry. */
8655 if (gent->got.refcount != 0)
8656 gent->tls_type = TLS_TLS | TLS_TPREL;
8657 }
8658
7865406b
AM
8659 /* Remove any list entry that won't generate a word in the GOT before
8660 we call merge_got_entries. Otherwise we risk merging to empty
8661 entries. */
0b8bcf0d
AM
8662 pgent = &h->got.glist;
8663 while ((gent = *pgent) != NULL)
411e1bfb 8664 if (gent->got.refcount > 0)
7865406b
AM
8665 {
8666 if ((gent->tls_type & TLS_LD) != 0
8667 && !h->def_dynamic)
8668 {
8669 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8670 *pgent = gent->next;
8671 }
8672 else
8673 pgent = &gent->next;
8674 }
8675 else
8676 *pgent = gent->next;
8677
8678 if (!htab->do_multi_toc)
8679 merge_got_entries (&h->got.glist);
8680
8681 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8682 if (!gent->is_indirect)
411e1bfb
AM
8683 {
8684 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
8685 Undefined weak syms won't yet be marked as dynamic,
8686 nor will all TLS symbols. */
411e1bfb 8687 if (h->dynindx == -1
b099ab9f 8688 && !h->forced_local
25f23106 8689 && h->type != STT_GNU_IFUNC
b099ab9f 8690 && htab->elf.dynamic_sections_created)
411e1bfb 8691 {
c152c796 8692 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
8693 return FALSE;
8694 }
65f38f15 8695
0c8d6e5c 8696 if (!is_ppc64_elf (gent->owner))
927be08e 8697 abort ();
0ffa91dd 8698
927be08e 8699 allocate_got (h, info, gent);
411e1bfb 8700 }
65f38f15 8701
b099ab9f 8702 if (eh->dyn_relocs == NULL
25f23106
AM
8703 || (!htab->elf.dynamic_sections_created
8704 && h->type != STT_GNU_IFUNC))
b34976b6 8705 return TRUE;
65f38f15
AM
8706
8707 /* In the shared -Bsymbolic case, discard space allocated for
8708 dynamic pc-relative relocs against symbols which turn out to be
8709 defined in regular objects. For the normal shared case, discard
8710 space for relocs that have become local due to symbol visibility
8711 changes. */
8712
8713 if (info->shared)
8714 {
9c7a29a3 8715 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 8716 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
8717 generated via assembly. We want calls to protected symbols to
8718 resolve directly to the function rather than going via the plt.
8719 If people want function pointer comparisons to work as expected
8720 then they should avoid writing weird assembly. */
09695f56 8721 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15 8722 {
6061a67d 8723 struct elf_dyn_relocs **pp;
65f38f15
AM
8724
8725 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 8726 {
65f38f15
AM
8727 p->count -= p->pc_count;
8728 p->pc_count = 0;
8729 if (p->count == 0)
8730 *pp = p->next;
8731 else
8732 pp = &p->next;
5bd4f169 8733 }
65f38f15 8734 }
4e795f50
AM
8735
8736 /* Also discard relocs on undefined weak syms with non-default
8737 visibility. */
cab87ef9
AM
8738 if (eh->dyn_relocs != NULL
8739 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
8740 {
8741 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8742 eh->dyn_relocs = NULL;
8743
8744 /* Make sure this symbol is output as a dynamic symbol.
8745 Undefined weak syms won't yet be marked as dynamic. */
8746 else if (h->dynindx == -1
8747 && !h->forced_local)
8748 {
8749 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8750 return FALSE;
8751 }
8752 }
65f38f15 8753 }
25f23106
AM
8754 else if (h->type == STT_GNU_IFUNC)
8755 {
8756 if (!h->non_got_ref)
8757 eh->dyn_relocs = NULL;
8758 }
f4656909 8759 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
8760 {
8761 /* For the non-shared case, discard space for relocs against
8762 symbols which turn out to need copy relocs or are not
8763 dynamic. */
8764
f5385ebf 8765 if (!h->non_got_ref
f5385ebf 8766 && !h->def_regular)
65f38f15
AM
8767 {
8768 /* Make sure this symbol is output as a dynamic symbol.
8769 Undefined weak syms won't yet be marked as dynamic. */
8770 if (h->dynindx == -1
f5385ebf 8771 && !h->forced_local)
65f38f15 8772 {
c152c796 8773 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8774 return FALSE;
65f38f15
AM
8775 }
8776
8777 /* If that succeeded, we know we'll be keeping all the
8778 relocs. */
8779 if (h->dynindx != -1)
8780 goto keep;
8781 }
8782
8783 eh->dyn_relocs = NULL;
8784
ec338859 8785 keep: ;
65f38f15
AM
8786 }
8787
8788 /* Finally, allocate space. */
8789 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8790 {
8791 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
8792 if (!htab->elf.dynamic_sections_created)
8793 sreloc = htab->reliplt;
eea6121a 8794 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
8795 }
8796
b34976b6 8797 return TRUE;
65f38f15
AM
8798}
8799
8800/* Find any dynamic relocs that apply to read-only sections. */
8801
b34976b6 8802static bfd_boolean
4ce794b7 8803readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
8804{
8805 struct ppc_link_hash_entry *eh;
6061a67d 8806 struct elf_dyn_relocs *p;
65f38f15
AM
8807
8808 eh = (struct ppc_link_hash_entry *) h;
8809 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8810 {
8811 asection *s = p->sec->output_section;
8812
8813 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8814 {
4ce794b7 8815 struct bfd_link_info *info = inf;
65f38f15
AM
8816
8817 info->flags |= DF_TEXTREL;
8818
8819 /* Not an error, just cut short the traversal. */
b34976b6 8820 return FALSE;
65f38f15
AM
8821 }
8822 }
b34976b6 8823 return TRUE;
65f38f15
AM
8824}
8825
8826/* Set the sizes of the dynamic sections. */
8827
b34976b6 8828static bfd_boolean
4ce794b7
AM
8829ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8830 struct bfd_link_info *info)
65f38f15
AM
8831{
8832 struct ppc_link_hash_table *htab;
8833 bfd *dynobj;
8834 asection *s;
b34976b6 8835 bfd_boolean relocs;
65f38f15 8836 bfd *ibfd;
7865406b 8837 struct got_entry *first_tlsld;
65f38f15
AM
8838
8839 htab = ppc_hash_table (info);
4dfe6ac6
NC
8840 if (htab == NULL)
8841 return FALSE;
8842
65f38f15
AM
8843 dynobj = htab->elf.dynobj;
8844 if (dynobj == NULL)
8845 abort ();
8846
8847 if (htab->elf.dynamic_sections_created)
8848 {
8849 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 8850 if (info->executable)
65f38f15
AM
8851 {
8852 s = bfd_get_section_by_name (dynobj, ".interp");
8853 if (s == NULL)
8854 abort ();
eea6121a 8855 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
8856 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8857 }
8858 }
8859
8860 /* Set up .got offsets for local syms, and space for local dynamic
8861 relocs. */
8862 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8863 {
411e1bfb
AM
8864 struct got_entry **lgot_ents;
8865 struct got_entry **end_lgot_ents;
e054468f
AM
8866 struct plt_entry **local_plt;
8867 struct plt_entry **end_local_plt;
f961d9dd 8868 unsigned char *lgot_masks;
65f38f15
AM
8869 bfd_size_type locsymcount;
8870 Elf_Internal_Shdr *symtab_hdr;
8871 asection *srel;
8872
0c8d6e5c 8873 if (!is_ppc64_elf (ibfd))
65f38f15
AM
8874 continue;
8875
8876 for (s = ibfd->sections; s != NULL; s = s->next)
8877 {
6061a67d 8878 struct elf_dyn_relocs *p;
65f38f15 8879
6edfbbad 8880 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 8881 {
ec338859
AM
8882 if (!bfd_is_abs_section (p->sec)
8883 && bfd_is_abs_section (p->sec->output_section))
8884 {
8885 /* Input section has been discarded, either because
8886 it is a copy of a linkonce section or due to
8887 linker script /DISCARD/, so we'll be discarding
8888 the relocs too. */
8889 }
248866a8 8890 else if (p->count != 0)
ec338859
AM
8891 {
8892 srel = elf_section_data (p->sec)->sreloc;
25f23106
AM
8893 if (!htab->elf.dynamic_sections_created)
8894 srel = htab->reliplt;
eea6121a 8895 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
8896 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8897 info->flags |= DF_TEXTREL;
ec338859 8898 }
65f38f15
AM
8899 }
8900 }
8901
411e1bfb
AM
8902 lgot_ents = elf_local_got_ents (ibfd);
8903 if (!lgot_ents)
65f38f15
AM
8904 continue;
8905
0ffa91dd 8906 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 8907 locsymcount = symtab_hdr->sh_info;
411e1bfb 8908 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
8909 local_plt = (struct plt_entry **) end_lgot_ents;
8910 end_local_plt = local_plt + locsymcount;
f961d9dd 8911 lgot_masks = (unsigned char *) end_local_plt;
e717da7e
AM
8912 s = ppc64_elf_tdata (ibfd)->got;
8913 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 8914 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 8915 {
0b8bcf0d 8916 struct got_entry **pent, *ent;
411e1bfb 8917
0b8bcf0d
AM
8918 pent = lgot_ents;
8919 while ((ent = *pent) != NULL)
411e1bfb
AM
8920 if (ent->got.refcount > 0)
8921 {
e7b938ca 8922 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 8923 {
927be08e 8924 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 8925 *pent = ent->next;
411e1bfb
AM
8926 }
8927 else
8928 {
25f23106 8929 unsigned int num = 1;
eea6121a 8930 ent->got.offset = s->size;
e7b938ca 8931 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
25f23106
AM
8932 num = 2;
8933 s->size += num * 8;
8934 if (info->shared)
8935 srel->size += num * sizeof (Elf64_External_Rela);
8936 else if ((*lgot_masks & PLT_IFUNC) != 0)
927be08e
AM
8937 {
8938 htab->reliplt->size
8939 += num * sizeof (Elf64_External_Rela);
8940 htab->got_reli_size
8941 += num * sizeof (Elf64_External_Rela);
8942 }
0b8bcf0d 8943 pent = &ent->next;
411e1bfb
AM
8944 }
8945 }
8946 else
0b8bcf0d 8947 *pent = ent->next;
65f38f15 8948 }
e054468f
AM
8949
8950 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
8951 for (; local_plt < end_local_plt; ++local_plt)
8952 {
8953 struct plt_entry *ent;
8954
8955 for (ent = *local_plt; ent != NULL; ent = ent->next)
8956 if (ent->plt.refcount > 0)
8957 {
91d6fa6a 8958 s = htab->iplt;
e054468f
AM
8959 ent->plt.offset = s->size;
8960 s->size += PLT_ENTRY_SIZE;
8961
25f23106 8962 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
8963 }
8964 else
8965 ent->plt.offset = (bfd_vma) -1;
8966 }
65f38f15
AM
8967 }
8968
8969 /* Allocate global sym .plt and .got entries, and space for global
8970 sym dynamic relocs. */
4ce794b7 8971 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 8972
7865406b 8973 first_tlsld = NULL;
102890f0
AM
8974 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8975 {
7865406b
AM
8976 struct got_entry *ent;
8977
0c8d6e5c 8978 if (!is_ppc64_elf (ibfd))
102890f0
AM
8979 continue;
8980
7865406b
AM
8981 ent = ppc64_tlsld_got (ibfd);
8982 if (ent->got.refcount > 0)
102890f0 8983 {
7865406b 8984 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 8985 {
7865406b
AM
8986 ent->is_indirect = TRUE;
8987 ent->got.ent = first_tlsld;
8988 }
8989 else
8990 {
8991 if (first_tlsld == NULL)
8992 first_tlsld = ent;
8993 s = ppc64_elf_tdata (ibfd)->got;
8994 ent->got.offset = s->size;
8995 ent->owner = ibfd;
8996 s->size += 16;
8997 if (info->shared)
8998 {
8999 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9000 srel->size += sizeof (Elf64_External_Rela);
9001 }
102890f0
AM
9002 }
9003 }
9004 else
7865406b 9005 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9006 }
9007
65f38f15
AM
9008 /* We now have determined the sizes of the various dynamic sections.
9009 Allocate memory for them. */
b34976b6 9010 relocs = FALSE;
65f38f15
AM
9011 for (s = dynobj->sections; s != NULL; s = s->next)
9012 {
9013 if ((s->flags & SEC_LINKER_CREATED) == 0)
9014 continue;
9015
4ce794b7 9016 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9017 /* These haven't been allocated yet; don't strip. */
9018 continue;
e717da7e
AM
9019 else if (s == htab->got
9020 || s == htab->plt
e054468f 9021 || s == htab->iplt
c456f082
AM
9022 || s == htab->glink
9023 || s == htab->dynbss)
65f38f15
AM
9024 {
9025 /* Strip this section if we don't need it; see the
9026 comment below. */
5bd4f169 9027 }
70cc837d 9028 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9029 {
c456f082 9030 if (s->size != 0)
5bd4f169 9031 {
4ce794b7 9032 if (s != htab->relplt)
b34976b6 9033 relocs = TRUE;
5bd4f169
AM
9034
9035 /* We use the reloc_count field as a counter if we need
9036 to copy relocs into the output file. */
9037 s->reloc_count = 0;
9038 }
9039 }
65f38f15 9040 else
5bd4f169
AM
9041 {
9042 /* It's not one of our sections, so don't allocate space. */
9043 continue;
9044 }
9045
eea6121a 9046 if (s->size == 0)
5bd4f169 9047 {
c456f082
AM
9048 /* If we don't need this section, strip it from the
9049 output file. This is mostly to handle .rela.bss and
9050 .rela.plt. We must create both sections in
9051 create_dynamic_sections, because they must be created
9052 before the linker maps input sections to output
9053 sections. The linker does that before
9054 adjust_dynamic_symbol is called, and it is that
9055 function which decides whether anything needs to go
9056 into these sections. */
8423293d 9057 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9058 continue;
9059 }
9060
c456f082 9061 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9062 continue;
9063
65f38f15
AM
9064 /* Allocate memory for the section contents. We use bfd_zalloc
9065 here in case unused entries are not reclaimed before the
9066 section's contents are written out. This should not happen,
411e1bfb
AM
9067 but this way if it does we get a R_PPC64_NONE reloc in .rela
9068 sections instead of garbage.
9069 We also rely on the section contents being zero when writing
9070 the GOT. */
eea6121a 9071 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9072 if (s->contents == NULL)
b34976b6 9073 return FALSE;
5bd4f169
AM
9074 }
9075
e717da7e
AM
9076 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9077 {
0c8d6e5c 9078 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9079 continue;
9080
e717da7e
AM
9081 s = ppc64_elf_tdata (ibfd)->got;
9082 if (s != NULL && s != htab->got)
9083 {
eea6121a 9084 if (s->size == 0)
8423293d 9085 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9086 else
9087 {
eea6121a 9088 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9089 if (s->contents == NULL)
9090 return FALSE;
9091 }
9092 }
9093 s = ppc64_elf_tdata (ibfd)->relgot;
9094 if (s != NULL)
9095 {
eea6121a 9096 if (s->size == 0)
8423293d 9097 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9098 else
9099 {
eea6121a 9100 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9101 if (s->contents == NULL)
9102 return FALSE;
9103 relocs = TRUE;
9104 s->reloc_count = 0;
9105 }
9106 }
9107 }
9108
e86ce104 9109 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9110 {
9111 /* Add some entries to the .dynamic section. We fill in the
9112 values later, in ppc64_elf_finish_dynamic_sections, but we
9113 must add the entries now so that we get the correct size for
9114 the .dynamic section. The DT_DEBUG entry is filled in by the
9115 dynamic linker and used by the debugger. */
dc810e39 9116#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9117 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9118
36af4a4e 9119 if (info->executable)
5bd4f169 9120 {
dc810e39 9121 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9122 return FALSE;
5bd4f169
AM
9123 }
9124
eea6121a 9125 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9126 {
dc810e39
AM
9127 if (!add_dynamic_entry (DT_PLTGOT, 0)
9128 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9129 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9130 || !add_dynamic_entry (DT_JMPREL, 0)
9131 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9132 return FALSE;
5bd4f169
AM
9133 }
9134
19397422
AM
9135 if (NO_OPD_RELOCS)
9136 {
9137 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9138 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9139 return FALSE;
19397422
AM
9140 }
9141
a7f2871e
AM
9142 if (!htab->no_tls_get_addr_opt
9143 && htab->tls_get_addr_fd != NULL
9144 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9145 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9146 return FALSE;
9147
5bd4f169
AM
9148 if (relocs)
9149 {
dc810e39
AM
9150 if (!add_dynamic_entry (DT_RELA, 0)
9151 || !add_dynamic_entry (DT_RELASZ, 0)
9152 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9153 return FALSE;
5bd4f169 9154
65f38f15
AM
9155 /* If any dynamic relocs apply to a read-only section,
9156 then we need a DT_TEXTREL entry. */
248866a8 9157 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9158 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9159
65f38f15 9160 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9161 {
65f38f15 9162 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9163 return FALSE;
5bd4f169 9164 }
5bd4f169 9165 }
5bd4f169 9166 }
65f38f15 9167#undef add_dynamic_entry
5bd4f169 9168
b34976b6 9169 return TRUE;
5bd4f169
AM
9170}
9171
721956f4 9172/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9173
4ce794b7
AM
9174static inline enum ppc_stub_type
9175ppc_type_of_stub (asection *input_sec,
9176 const Elf_Internal_Rela *rel,
9177 struct ppc_link_hash_entry **hash,
e054468f 9178 struct plt_entry **plt_ent,
4ce794b7 9179 bfd_vma destination)
5bd4f169 9180{
721956f4
AM
9181 struct ppc_link_hash_entry *h = *hash;
9182 bfd_vma location;
9183 bfd_vma branch_offset;
9184 bfd_vma max_branch_offset;
4ce794b7 9185 enum elf_ppc64_reloc_type r_type;
5bd4f169 9186
721956f4
AM
9187 if (h != NULL)
9188 {
e054468f 9189 struct plt_entry *ent;
7fe2b9a6 9190 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9191 if (h->oh != NULL
9192 && h->oh->is_func_descriptor)
7b8f6675
AM
9193 {
9194 fdh = ppc_follow_link (h->oh);
9195 *hash = fdh;
9196 }
8387904d 9197
e054468f
AM
9198 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9199 if (ent->addend == rel->r_addend
9200 && ent->plt.offset != (bfd_vma) -1)
9201 {
e054468f
AM
9202 *plt_ent = ent;
9203 return ppc_stub_plt_call;
9204 }
5bd4f169 9205
7fe2b9a6
AM
9206 /* Here, we know we don't have a plt entry. If we don't have a
9207 either a defined function descriptor or a defined entry symbol
9208 in a regular object file, then it is pointless trying to make
9209 any other type of stub. */
854b41e7
AM
9210 if (!is_static_defined (&fdh->elf)
9211 && !is_static_defined (&h->elf))
721956f4 9212 return ppc_stub_none;
5d1634d7 9213 }
e054468f
AM
9214 else if (elf_local_got_ents (input_sec->owner) != NULL)
9215 {
9216 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9217 struct plt_entry **local_plt = (struct plt_entry **)
9218 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9219 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9220
9221 if (local_plt[r_symndx] != NULL)
9222 {
9223 struct plt_entry *ent;
9224
9225 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9226 if (ent->addend == rel->r_addend
9227 && ent->plt.offset != (bfd_vma) -1)
9228 {
9229 *plt_ent = ent;
9230 return ppc_stub_plt_call;
9231 }
9232 }
9233 }
5d1634d7 9234
721956f4
AM
9235 /* Determine where the call point is. */
9236 location = (input_sec->output_offset
9237 + input_sec->output_section->vma
9238 + rel->r_offset);
5d1634d7 9239
721956f4
AM
9240 branch_offset = destination - location;
9241 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9242
721956f4
AM
9243 /* Determine if a long branch stub is needed. */
9244 max_branch_offset = 1 << 25;
4ce794b7 9245 if (r_type != R_PPC64_REL24)
721956f4 9246 max_branch_offset = 1 << 15;
5d1634d7 9247
721956f4
AM
9248 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9249 /* We need a stub. Figure out whether a long_branch or plt_branch
9250 is needed later. */
9251 return ppc_stub_long_branch;
5d1634d7 9252
721956f4 9253 return ppc_stub_none;
5d1634d7
AM
9254}
9255
9256/* Build a .plt call stub. */
9257
4ce794b7 9258static inline bfd_byte *
9df0ef5f
AM
9259build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r,
9260 bfd_boolean plt_static_chain)
5d1634d7
AM
9261{
9262#define PPC_LO(v) ((v) & 0xffff)
9263#define PPC_HI(v) (((v) >> 16) & 0xffff)
9264#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9265
ac2df442
AM
9266 if (PPC_HA (offset) != 0)
9267 {
176a0d42
AM
9268 if (r != NULL)
9269 {
9270 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9271 r[1].r_offset = r[0].r_offset + 8;
9272 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9273 r[1].r_addend = r[0].r_addend;
9274 if (PPC_HA (offset + 16) != PPC_HA (offset))
9275 {
9276 r[2].r_offset = r[1].r_offset + 4;
9277 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9278 r[2].r_addend = r[0].r_addend;
9279 }
9280 else
9281 {
9282 r[2].r_offset = r[1].r_offset + 8;
9283 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9284 r[2].r_addend = r[0].r_addend + 8;
9285 r[3].r_offset = r[2].r_offset + 4;
9286 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9287 r[3].r_addend = r[0].r_addend + 16;
9288 }
9289 }
ac2df442
AM
9290 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9291 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9292 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9df0ef5f 9293 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9294 {
9295 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9296 offset = 0;
9297 }
9298 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9299 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9df0ef5f
AM
9300 if (plt_static_chain)
9301 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
9302 bfd_put_32 (obfd, BCTR, p), p += 4;
9303 }
9304 else
9305 {
176a0d42
AM
9306 if (r != NULL)
9307 {
9308 r[0].r_offset += 4;
9309 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9310 if (PPC_HA (offset + 16) != PPC_HA (offset))
9311 {
9312 r[1].r_offset = r[0].r_offset + 4;
9313 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9314 r[1].r_addend = r[0].r_addend;
9315 }
9316 else
9317 {
9318 r[1].r_offset = r[0].r_offset + 8;
9319 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9320 r[1].r_addend = r[0].r_addend + 16;
9321 r[2].r_offset = r[1].r_offset + 4;
9322 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9323 r[2].r_addend = r[0].r_addend + 8;
9324 }
9325 }
ac2df442
AM
9326 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9327 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9df0ef5f 9328 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
ac2df442
AM
9329 {
9330 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9331 offset = 0;
9332 }
9333 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9df0ef5f
AM
9334 if (plt_static_chain)
9335 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
ac2df442
AM
9336 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9337 bfd_put_32 (obfd, BCTR, p), p += 4;
9338 }
5d1634d7
AM
9339 return p;
9340}
9341
a7f2871e
AM
9342/* Build a special .plt call stub for __tls_get_addr. */
9343
9344#define LD_R11_0R3 0xe9630000
9345#define LD_R12_0R3 0xe9830000
9346#define MR_R0_R3 0x7c601b78
9347#define CMPDI_R11_0 0x2c2b0000
9348#define ADD_R3_R12_R13 0x7c6c6a14
9349#define BEQLR 0x4d820020
9350#define MR_R3_R0 0x7c030378
9351#define MFLR_R11 0x7d6802a6
9352#define STD_R11_0R1 0xf9610000
9353#define BCTRL 0x4e800421
9354#define LD_R11_0R1 0xe9610000
9355#define LD_R2_0R1 0xe8410000
9356#define MTLR_R11 0x7d6803a6
9357
9358static inline bfd_byte *
9359build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9df0ef5f 9360 Elf_Internal_Rela *r, bfd_boolean plt_static_chain)
a7f2871e
AM
9361{
9362 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9363 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9364 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9365 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9366 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9367 bfd_put_32 (obfd, BEQLR, p), p += 4;
9368 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9369 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9370 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9371
9372 if (r != NULL)
9373 r[0].r_offset += 9 * 4;
9df0ef5f 9374 p = build_plt_stub (obfd, p, offset, r, plt_static_chain);
a7f2871e
AM
9375 bfd_put_32 (obfd, BCTRL, p - 4);
9376
9377 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9378 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9379 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9380 bfd_put_32 (obfd, BLR, p), p += 4;
9381
9382 return p;
9383}
9384
176a0d42
AM
9385static Elf_Internal_Rela *
9386get_relocs (asection *sec, int count)
9387{
9388 Elf_Internal_Rela *relocs;
9389 struct bfd_elf_section_data *elfsec_data;
9390
9391 elfsec_data = elf_section_data (sec);
9392 relocs = elfsec_data->relocs;
9393 if (relocs == NULL)
9394 {
9395 bfd_size_type relsize;
9396 relsize = sec->reloc_count * sizeof (*relocs);
9397 relocs = bfd_alloc (sec->owner, relsize);
9398 if (relocs == NULL)
9399 return NULL;
9400 elfsec_data->relocs = relocs;
d4730f92
BS
9401 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9402 sizeof (Elf_Internal_Shdr));
9403 if (elfsec_data->rela.hdr == NULL)
9404 return NULL;
9405 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9406 * sizeof (Elf64_External_Rela));
9407 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9408 sec->reloc_count = 0;
9409 }
9410 relocs += sec->reloc_count;
9411 sec->reloc_count += count;
9412 return relocs;
9413}
9414
aa374f67 9415static bfd_vma
25f53a85 9416get_r2off (struct bfd_link_info *info,
aa374f67
AM
9417 struct ppc_stub_hash_entry *stub_entry)
9418{
25f53a85 9419 struct ppc_link_hash_table *htab = ppc_hash_table (info);
aa374f67
AM
9420 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9421
9422 if (r2off == 0)
9423 {
9424 /* Support linking -R objects. Get the toc pointer from the
9425 opd entry. */
9426 char buf[8];
9427 asection *opd = stub_entry->h->elf.root.u.def.section;
9428 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9429
9430 if (strcmp (opd->name, ".opd") != 0
9431 || opd->reloc_count != 0)
9432 {
25f53a85
AM
9433 info->callbacks->einfo (_("cannot find opd entry toc for %s\n"),
9434 stub_entry->h->elf.root.root.string);
aa374f67
AM
9435 bfd_set_error (bfd_error_bad_value);
9436 return 0;
9437 }
9438 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9439 return 0;
9440 r2off = bfd_get_64 (opd->owner, buf);
25f53a85 9441 r2off -= elf_gp (info->output_bfd);
aa374f67
AM
9442 }
9443 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9444 return r2off;
9445}
9446
b34976b6 9447static bfd_boolean
4ce794b7 9448ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 9449{
721956f4
AM
9450 struct ppc_stub_hash_entry *stub_entry;
9451 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
9452 struct bfd_link_info *info;
9453 struct ppc_link_hash_table *htab;
721956f4
AM
9454 bfd_byte *loc;
9455 bfd_byte *p;
ee75fd95 9456 bfd_vma dest, off;
721956f4 9457 int size;
176a0d42 9458 Elf_Internal_Rela *r;
e054468f 9459 asection *plt;
5d1634d7 9460
721956f4
AM
9461 /* Massage our args to the form they really have. */
9462 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 9463 info = in_arg;
5d1634d7 9464
5d1634d7 9465 htab = ppc_hash_table (info);
4dfe6ac6
NC
9466 if (htab == NULL)
9467 return FALSE;
5d1634d7 9468
721956f4 9469 /* Make a note of the offset within the stubs for this entry. */
eea6121a 9470 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 9471 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 9472
4ce794b7 9473 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 9474 switch (stub_entry->stub_type)
5d1634d7 9475 {
721956f4 9476 case ppc_stub_long_branch:
ad8e1ba5 9477 case ppc_stub_long_branch_r2off:
721956f4 9478 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
9479 off = dest = (stub_entry->target_value
9480 + stub_entry->target_section->output_offset
9481 + stub_entry->target_section->output_section->vma);
5d1634d7 9482
721956f4
AM
9483 /* And this is where we are coming from. */
9484 off -= (stub_entry->stub_offset
97b639ba
AM
9485 + stub_entry->stub_sec->output_offset
9486 + stub_entry->stub_sec->output_section->vma);
e86ce104 9487
ac2df442
AM
9488 size = 4;
9489 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 9490 {
25f53a85 9491 bfd_vma r2off = get_r2off (info, stub_entry);
ad8e1ba5 9492
aa374f67
AM
9493 if (r2off == 0)
9494 {
9495 htab->stub_error = TRUE;
9496 return FALSE;
9497 }
97b639ba 9498 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9499 loc += 4;
ac2df442
AM
9500 size = 12;
9501 if (PPC_HA (r2off) != 0)
9502 {
9503 size = 16;
9504 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9505 loc += 4;
9506 }
97b639ba 9507 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 9508 loc += 4;
ac2df442 9509 off -= size - 4;
ad8e1ba5 9510 }
97b639ba 9511 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 9512
5c3dead3
AM
9513 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9514 {
25f53a85
AM
9515 info->callbacks->einfo (_("long branch stub `%s' offset overflow\n"),
9516 stub_entry->root.string);
5c3dead3
AM
9517 htab->stub_error = TRUE;
9518 return FALSE;
9519 }
ee75fd95
AM
9520
9521 if (info->emitrelocations)
9522 {
176a0d42
AM
9523 r = get_relocs (stub_entry->stub_sec, 1);
9524 if (r == NULL)
9525 return FALSE;
ee75fd95
AM
9526 r->r_offset = loc - stub_entry->stub_sec->contents;
9527 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9528 r->r_addend = dest;
9529 if (stub_entry->h != NULL)
9530 {
9531 struct elf_link_hash_entry **hashes;
9532 unsigned long symndx;
9533 struct ppc_link_hash_entry *h;
9534
9535 hashes = elf_sym_hashes (htab->stub_bfd);
9536 if (hashes == NULL)
9537 {
9538 bfd_size_type hsize;
9539
9540 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9541 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9542 if (hashes == NULL)
9543 return FALSE;
9544 elf_sym_hashes (htab->stub_bfd) = hashes;
9545 htab->stub_globals = 1;
9546 }
9547 symndx = htab->stub_globals++;
9548 h = stub_entry->h;
9549 hashes[symndx] = &h->elf;
9550 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9551 if (h->oh != NULL && h->oh->is_func)
b31867b6 9552 h = ppc_follow_link (h->oh);
ee75fd95
AM
9553 if (h->elf.root.u.def.section != stub_entry->target_section)
9554 /* H is an opd symbol. The addend must be zero. */
9555 r->r_addend = 0;
9556 else
9557 {
9558 off = (h->elf.root.u.def.value
9559 + h->elf.root.u.def.section->output_offset
9560 + h->elf.root.u.def.section->output_section->vma);
9561 r->r_addend -= off;
9562 }
9563 }
9564 }
721956f4 9565 break;
e86ce104 9566
721956f4 9567 case ppc_stub_plt_branch:
ad8e1ba5 9568 case ppc_stub_plt_branch_r2off:
721956f4
AM
9569 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9570 stub_entry->root.string + 9,
b34976b6 9571 FALSE, FALSE);
721956f4
AM
9572 if (br_entry == NULL)
9573 {
25f53a85
AM
9574 info->callbacks->einfo (_("can't find branch stub `%s'\n"),
9575 stub_entry->root.string);
b34976b6
AM
9576 htab->stub_error = TRUE;
9577 return FALSE;
721956f4
AM
9578 }
9579
176a0d42
AM
9580 dest = (stub_entry->target_value
9581 + stub_entry->target_section->output_offset
9582 + stub_entry->target_section->output_section->vma);
721956f4 9583
176a0d42 9584 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 9585 htab->brlt->contents + br_entry->offset);
721956f4 9586
f94498ff 9587 if (br_entry->iter == htab->stub_iteration)
721956f4 9588 {
f94498ff 9589 br_entry->iter = 0;
84f5d08e 9590
f94498ff 9591 if (htab->relbrlt != NULL)
84f5d08e 9592 {
f94498ff
AM
9593 /* Create a reloc for the branch lookup table entry. */
9594 Elf_Internal_Rela rela;
9595 bfd_byte *rl;
9596
9597 rela.r_offset = (br_entry->offset
9598 + htab->brlt->output_offset
9599 + htab->brlt->output_section->vma);
9600 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9601 rela.r_addend = dest;
f94498ff
AM
9602
9603 rl = htab->relbrlt->contents;
9604 rl += (htab->relbrlt->reloc_count++
9605 * sizeof (Elf64_External_Rela));
9606 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9607 }
9608 else if (info->emitrelocations)
9609 {
176a0d42
AM
9610 r = get_relocs (htab->brlt, 1);
9611 if (r == NULL)
9612 return FALSE;
9613 /* brlt, being SEC_LINKER_CREATED does not go through the
9614 normal reloc processing. Symbols and offsets are not
9615 translated from input file to output file form, so
9616 set up the offset per the output file. */
f94498ff
AM
9617 r->r_offset = (br_entry->offset
9618 + htab->brlt->output_offset
9619 + htab->brlt->output_section->vma);
9620 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9621 r->r_addend = dest;
84f5d08e 9622 }
84f5d08e 9623 }
721956f4 9624
176a0d42
AM
9625 dest = (br_entry->offset
9626 + htab->brlt->output_offset
9627 + htab->brlt->output_section->vma);
9628
9629 off = (dest
4ce794b7 9630 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 9631 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9632
ad8e1ba5 9633 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7 9634 {
25f53a85
AM
9635 info->callbacks->einfo
9636 (_("linkage table error against `%s'\n"),
721956f4 9637 stub_entry->root.string);
5d1634d7 9638 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9639 htab->stub_error = TRUE;
9640 return FALSE;
5d1634d7 9641 }
41bd81ab 9642
176a0d42
AM
9643 if (info->emitrelocations)
9644 {
9645 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9646 if (r == NULL)
9647 return FALSE;
9648 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9649 if (bfd_big_endian (info->output_bfd))
9650 r[0].r_offset += 2;
176a0d42
AM
9651 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9652 r[0].r_offset += 4;
9653 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9654 r[0].r_addend = dest;
9655 if (PPC_HA (off) != 0)
9656 {
9657 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9658 r[1].r_offset = r[0].r_offset + 4;
9659 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9660 r[1].r_addend = r[0].r_addend;
9661 }
9662 }
9663
ad8e1ba5
AM
9664 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9665 {
176a0d42 9666 if (PPC_HA (off) != 0)
ac2df442
AM
9667 {
9668 size = 16;
176a0d42 9669 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9670 loc += 4;
176a0d42 9671 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9672 }
9673 else
9674 {
9675 size = 12;
176a0d42 9676 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442 9677 }
ad8e1ba5
AM
9678 }
9679 else
9680 {
25f53a85 9681 bfd_vma r2off = get_r2off (info, stub_entry);
aa374f67
AM
9682
9683 if (r2off == 0)
9684 {
9685 htab->stub_error = TRUE;
9686 return FALSE;
9687 }
ad8e1ba5 9688
97b639ba 9689 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9690 loc += 4;
ac2df442 9691 size = 20;
176a0d42 9692 if (PPC_HA (off) != 0)
ac2df442
AM
9693 {
9694 size += 4;
176a0d42 9695 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9696 loc += 4;
176a0d42 9697 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9698 loc += 4;
9699 }
9700 else
9701 {
176a0d42 9702 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442
AM
9703 loc += 4;
9704 }
9705
9706 if (PPC_HA (r2off) != 0)
9707 {
9708 size += 4;
9709 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9710 loc += 4;
9711 }
97b639ba 9712 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
9713 }
9714 loc += 4;
97b639ba 9715 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 9716 loc += 4;
97b639ba 9717 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 9718 break;
5d1634d7 9719
721956f4 9720 case ppc_stub_plt_call:
e054468f 9721 if (stub_entry->h != NULL
b31867b6
AM
9722 && stub_entry->h->is_func_descriptor
9723 && stub_entry->h->oh != NULL)
c862ae31 9724 {
b31867b6
AM
9725 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9726
9727 /* If the old-ABI "dot-symbol" is undefined make it weak so
9728 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9729 FIXME: We used to define the symbol on one of the call
9730 stubs instead, which is why we test symbol section id
9731 against htab->top_id in various places. Likely all
9732 these checks could now disappear. */
9733 if (fh->elf.root.type == bfd_link_hash_undefined)
9734 fh->elf.root.type = bfd_link_hash_undefweak;
9507a174
AM
9735 /* Stop undo_symbol_twiddle changing it back to undefined. */
9736 fh->was_undefined = 0;
c862ae31
AM
9737 }
9738
721956f4 9739 /* Now build the stub. */
e054468f 9740 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 9741 if (dest >= (bfd_vma) -2)
721956f4
AM
9742 abort ();
9743
e054468f 9744 plt = htab->plt;
25f23106
AM
9745 if (!htab->elf.dynamic_sections_created
9746 || stub_entry->h == NULL
9747 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9748 plt = htab->iplt;
9749
9750 dest += plt->output_offset + plt->output_section->vma;
9751
9752 if (stub_entry->h == NULL
9753 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9754 {
9755 Elf_Internal_Rela rela;
9756 bfd_byte *rl;
9757
9758 rela.r_offset = dest;
25f23106 9759 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
9760 rela.r_addend = (stub_entry->target_value
9761 + stub_entry->target_section->output_offset
9762 + stub_entry->target_section->output_section->vma);
9763
25f23106
AM
9764 rl = (htab->reliplt->contents
9765 + (htab->reliplt->reloc_count++
9766 * sizeof (Elf64_External_Rela)));
9767 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
9768 stub_entry->plt_ent->plt.offset |= 1;
9769 }
176a0d42
AM
9770
9771 off = (dest
e054468f 9772 - elf_gp (plt->output_section->owner)
176a0d42 9773 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9774
ad8e1ba5 9775 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4 9776 {
25f53a85
AM
9777 info->callbacks->einfo
9778 (_("linkage table error against `%s'\n"),
e054468f
AM
9779 stub_entry->h != NULL
9780 ? stub_entry->h->elf.root.root.string
9781 : "<local sym>");
721956f4 9782 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9783 htab->stub_error = TRUE;
9784 return FALSE;
721956f4
AM
9785 }
9786
176a0d42
AM
9787 r = NULL;
9788 if (info->emitrelocations)
9789 {
9790 r = get_relocs (stub_entry->stub_sec,
9791 (2 + (PPC_HA (off) != 0)
9792 + (PPC_HA (off + 16) == PPC_HA (off))));
9793 if (r == NULL)
9794 return FALSE;
9795 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9796 if (bfd_big_endian (info->output_bfd))
9797 r[0].r_offset += 2;
176a0d42
AM
9798 r[0].r_addend = dest;
9799 }
a7f2871e
AM
9800 if (stub_entry->h != NULL
9801 && (stub_entry->h == htab->tls_get_addr_fd
9802 || stub_entry->h == htab->tls_get_addr)
9803 && !htab->no_tls_get_addr_opt)
9df0ef5f
AM
9804 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r,
9805 htab->plt_static_chain);
a7f2871e 9806 else
9df0ef5f
AM
9807 p = build_plt_stub (htab->stub_bfd, loc, off, r,
9808 htab->plt_static_chain);
721956f4
AM
9809 size = p - loc;
9810 break;
9811
9812 default:
9813 BFD_FAIL ();
b34976b6 9814 return FALSE;
721956f4
AM
9815 }
9816
eea6121a 9817 stub_entry->stub_sec->size += size;
97b639ba 9818
ee75fd95 9819 if (htab->emit_stub_syms)
97b639ba
AM
9820 {
9821 struct elf_link_hash_entry *h;
ee75fd95
AM
9822 size_t len1, len2;
9823 char *name;
9824 const char *const stub_str[] = { "long_branch",
9825 "long_branch_r2off",
9826 "plt_branch",
9827 "plt_branch_r2off",
9828 "plt_call" };
9829
9830 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9831 len2 = strlen (stub_entry->root.string);
9832 name = bfd_malloc (len1 + len2 + 2);
9833 if (name == NULL)
9834 return FALSE;
9835 memcpy (name, stub_entry->root.string, 9);
9836 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9837 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9838 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
9839 if (h == NULL)
9840 return FALSE;
9841 if (h->root.type == bfd_link_hash_new)
9842 {
9843 h->root.type = bfd_link_hash_defined;
9844 h->root.u.def.section = stub_entry->stub_sec;
9845 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
9846 h->ref_regular = 1;
9847 h->def_regular = 1;
9848 h->ref_regular_nonweak = 1;
9849 h->forced_local = 1;
9850 h->non_elf = 0;
97b639ba
AM
9851 }
9852 }
9853
b34976b6 9854 return TRUE;
721956f4
AM
9855}
9856
9857/* As above, but don't actually build the stub. Just bump offset so
9858 we know stub section sizes, and select plt_branch stubs where
9859 long_branch stubs won't do. */
9860
b34976b6 9861static bfd_boolean
4ce794b7 9862ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
9863{
9864 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 9865 struct bfd_link_info *info;
721956f4
AM
9866 struct ppc_link_hash_table *htab;
9867 bfd_vma off;
9868 int size;
9869
9870 /* Massage our args to the form they really have. */
9871 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
9872 info = in_arg;
9873
9874 htab = ppc_hash_table (info);
4dfe6ac6
NC
9875 if (htab == NULL)
9876 return FALSE;
721956f4
AM
9877
9878 if (stub_entry->stub_type == ppc_stub_plt_call)
9879 {
e054468f
AM
9880 asection *plt;
9881 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 9882 if (off >= (bfd_vma) -2)
411e1bfb 9883 abort ();
e054468f 9884 plt = htab->plt;
25f23106
AM
9885 if (!htab->elf.dynamic_sections_created
9886 || stub_entry->h == NULL
9887 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9888 plt = htab->iplt;
9889 off += (plt->output_offset
9890 + plt->output_section->vma
9891 - elf_gp (plt->output_section->owner)
ad8e1ba5 9892 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9893
ad8e1ba5 9894 size = PLT_CALL_STUB_SIZE;
9df0ef5f
AM
9895 if (!htab->plt_static_chain)
9896 size -= 4;
ac2df442
AM
9897 if (PPC_HA (off) == 0)
9898 size -= 4;
4ce794b7 9899 if (PPC_HA (off + 16) != PPC_HA (off))
721956f4 9900 size += 4;
a7f2871e
AM
9901 if (stub_entry->h != NULL
9902 && (stub_entry->h == htab->tls_get_addr_fd
9903 || stub_entry->h == htab->tls_get_addr)
9904 && !htab->no_tls_get_addr_opt)
9905 size += 13 * 4;
176a0d42
AM
9906 if (info->emitrelocations)
9907 {
9908 stub_entry->stub_sec->reloc_count
9909 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9910 stub_entry->stub_sec->flags |= SEC_RELOC;
9911 }
721956f4
AM
9912 }
9913 else
9914 {
ad8e1ba5
AM
9915 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9916 variants. */
ac2df442
AM
9917 bfd_vma r2off = 0;
9918
721956f4
AM
9919 off = (stub_entry->target_value
9920 + stub_entry->target_section->output_offset
9921 + stub_entry->target_section->output_section->vma);
eea6121a 9922 off -= (stub_entry->stub_sec->size
721956f4
AM
9923 + stub_entry->stub_sec->output_offset
9924 + stub_entry->stub_sec->output_section->vma);
9925
ad8e1ba5
AM
9926 /* Reset the stub type from the plt variant in case we now
9927 can reach with a shorter stub. */
9928 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9929 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9930
9931 size = 4;
9932 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9933 {
25f53a85 9934 r2off = get_r2off (info, stub_entry);
aa374f67
AM
9935 if (r2off == 0)
9936 {
9937 htab->stub_error = TRUE;
9938 return FALSE;
9939 }
ac2df442
AM
9940 size = 12;
9941 if (PPC_HA (r2off) != 0)
9942 size = 16;
9943 off -= size - 4;
ad8e1ba5
AM
9944 }
9945
9946 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
9947 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9948 {
9949 struct ppc_branch_hash_entry *br_entry;
9950
9951 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9952 stub_entry->root.string + 9,
b34976b6 9953 TRUE, FALSE);
721956f4
AM
9954 if (br_entry == NULL)
9955 {
25f53a85
AM
9956 info->callbacks->einfo (_("can't build branch stub `%s'\n"),
9957 stub_entry->root.string);
b34976b6
AM
9958 htab->stub_error = TRUE;
9959 return FALSE;
721956f4
AM
9960 }
9961
9962 if (br_entry->iter != htab->stub_iteration)
9963 {
9964 br_entry->iter = htab->stub_iteration;
eea6121a
AM
9965 br_entry->offset = htab->brlt->size;
9966 htab->brlt->size += 8;
63bc6f6c 9967
ee75fd95 9968 if (htab->relbrlt != NULL)
eea6121a 9969 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
9970 else if (info->emitrelocations)
9971 {
9972 htab->brlt->reloc_count += 1;
9973 htab->brlt->flags |= SEC_RELOC;
9974 }
721956f4 9975 }
ad8e1ba5
AM
9976
9977 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
9978 off = (br_entry->offset
9979 + htab->brlt->output_offset
9980 + htab->brlt->output_section->vma
9981 - elf_gp (htab->brlt->output_section->owner)
9982 - htab->stub_group[stub_entry->id_sec->id].toc_off);
9983
176a0d42
AM
9984 if (info->emitrelocations)
9985 {
9986 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
9987 stub_entry->stub_sec->flags |= SEC_RELOC;
9988 }
9989
ac2df442
AM
9990 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9991 {
9992 size = 12;
176a0d42 9993 if (PPC_HA (off) != 0)
ac2df442
AM
9994 size = 16;
9995 }
9996 else
9997 {
9998 size = 20;
176a0d42 9999 if (PPC_HA (off) != 0)
ac2df442
AM
10000 size += 4;
10001
10002 if (PPC_HA (r2off) != 0)
10003 size += 4;
10004 }
721956f4 10005 }
84f5d08e
AM
10006 else if (info->emitrelocations)
10007 {
10008 stub_entry->stub_sec->reloc_count += 1;
10009 stub_entry->stub_sec->flags |= SEC_RELOC;
10010 }
721956f4
AM
10011 }
10012
eea6121a 10013 stub_entry->stub_sec->size += size;
b34976b6 10014 return TRUE;
721956f4
AM
10015}
10016
10017/* Set up various things so that we can make a list of input sections
10018 for each output section included in the link. Returns -1 on error,
cedb70c5 10019 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10020
10021int
927be08e
AM
10022ppc64_elf_setup_section_lists
10023 (struct bfd_link_info *info,
10024 asection *(*add_stub_section) (const char *, asection *),
10025 void (*layout_sections_again) (void))
721956f4
AM
10026{
10027 bfd *input_bfd;
734b6cf9 10028 int top_id, top_index, id;
721956f4 10029 asection *section;
734b6cf9 10030 asection **input_list;
721956f4
AM
10031 bfd_size_type amt;
10032 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10033
4dfe6ac6
NC
10034 if (htab == NULL)
10035 return -1;
927be08e
AM
10036 /* Stash our params away. */
10037 htab->add_stub_section = add_stub_section;
10038 htab->layout_sections_again = layout_sections_again;
4c52953f 10039
4ce794b7 10040 if (htab->brlt == NULL)
721956f4
AM
10041 return 0;
10042
1e2f5b6e 10043 /* Find the top input section id. */
3d6f9012 10044 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10045 input_bfd != NULL;
10046 input_bfd = input_bfd->link_next)
10047 {
721956f4
AM
10048 for (section = input_bfd->sections;
10049 section != NULL;
10050 section = section->next)
10051 {
10052 if (top_id < section->id)
10053 top_id = section->id;
10054 }
10055 }
721956f4 10056
8f3bab57 10057 htab->top_id = top_id;
721956f4 10058 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10059 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10060 if (htab->stub_group == NULL)
10061 return -1;
10062
3d6f9012
AM
10063 /* Set toc_off for com, und, abs and ind sections. */
10064 for (id = 0; id < 3; id++)
10065 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10066
734b6cf9
AM
10067 /* We can't use output_bfd->section_count here to find the top output
10068 section index as some sections may have been removed, and
8423293d 10069 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10070 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10071 section != NULL;
10072 section = section->next)
10073 {
10074 if (top_index < section->index)
10075 top_index = section->index;
10076 }
10077
10078 htab->top_index = top_index;
10079 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10080 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10081 htab->input_list = input_list;
10082 if (input_list == NULL)
10083 return -1;
10084
721956f4
AM
10085 return 1;
10086}
10087
927be08e
AM
10088/* Set up for first pass at multitoc partitioning. */
10089
10090void
10091ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10092{
10093 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10094
10095 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10096 htab->toc_curr = elf_gp (info->output_bfd);
10097 htab->toc_bfd = NULL;
10098 htab->toc_first_sec = NULL;
10099}
10100
e717da7e
AM
10101/* The linker repeatedly calls this function for each TOC input section
10102 and linker generated GOT section. Group input bfds such that the toc
927be08e 10103 within a group is less than 64k in size. */
ad8e1ba5 10104
927be08e 10105bfd_boolean
4ce794b7 10106ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10107{
10108 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10109 bfd_vma addr, off, limit;
ad8e1ba5 10110
4dfe6ac6
NC
10111 if (htab == NULL)
10112 return FALSE;
10113
927be08e 10114 if (!htab->second_toc_pass)
4c52953f 10115 {
927be08e 10116 /* Keep track of the first .toc or .got section for this input bfd. */
bf102f86
AM
10117 if (htab->toc_bfd != isec->owner)
10118 {
10119 htab->toc_bfd = isec->owner;
10120 htab->toc_first_sec = isec;
10121 }
927be08e 10122
bf102f86
AM
10123 addr = isec->output_offset + isec->output_section->vma;
10124 off = addr - htab->toc_curr;
d77c8a4b
AM
10125 limit = 0x80008000;
10126 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10127 limit = 0x10000;
10128 if (off + isec->size > limit)
bf102f86
AM
10129 {
10130 addr = (htab->toc_first_sec->output_offset
10131 + htab->toc_first_sec->output_section->vma);
10132 htab->toc_curr = addr;
10133 }
99877b66 10134
927be08e
AM
10135 /* toc_curr is the base address of this toc group. Set elf_gp
10136 for the input section to be the offset relative to the
10137 output toc base plus 0x8000. Making the input elf_gp an
10138 offset allows us to move the toc as a whole without
10139 recalculating input elf_gp. */
10140 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10141 off += TOC_BASE_OFF;
10142
10143 /* Die if someone uses a linker script that doesn't keep input
10144 file .toc and .got together. */
10145 if (elf_gp (isec->owner) != 0
10146 && elf_gp (isec->owner) != off)
10147 return FALSE;
10148
10149 elf_gp (isec->owner) = off;
10150 return TRUE;
4c52953f 10151 }
927be08e
AM
10152
10153 /* During the second pass toc_first_sec points to the start of
10154 a toc group, and toc_curr is used to track the old elf_gp.
10155 We use toc_bfd to ensure we only look at each bfd once. */
10156 if (htab->toc_bfd == isec->owner)
10157 return TRUE;
10158 htab->toc_bfd = isec->owner;
10159
10160 if (htab->toc_first_sec == NULL
10161 || htab->toc_curr != elf_gp (isec->owner))
10162 {
10163 htab->toc_curr = elf_gp (isec->owner);
10164 htab->toc_first_sec = isec;
10165 }
10166 addr = (htab->toc_first_sec->output_offset
10167 + htab->toc_first_sec->output_section->vma);
10168 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10169 elf_gp (isec->owner) = off;
10170
10171 return TRUE;
ad8e1ba5
AM
10172}
10173
927be08e
AM
10174/* Called via elf_link_hash_traverse to merge GOT entries for global
10175 symbol H. */
10176
10177static bfd_boolean
10178merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10179{
10180 if (h->root.type == bfd_link_hash_indirect)
10181 return TRUE;
10182
927be08e
AM
10183 merge_got_entries (&h->got.glist);
10184
10185 return TRUE;
10186}
10187
10188/* Called via elf_link_hash_traverse to allocate GOT entries for global
10189 symbol H. */
10190
10191static bfd_boolean
10192reallocate_got (struct elf_link_hash_entry *h, void *inf)
10193{
10194 struct got_entry *gent;
10195
10196 if (h->root.type == bfd_link_hash_indirect)
10197 return TRUE;
10198
927be08e
AM
10199 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10200 if (!gent->is_indirect)
10201 allocate_got (h, (struct bfd_link_info *) inf, gent);
10202 return TRUE;
10203}
10204
10205/* Called on the first multitoc pass after the last call to
10206 ppc64_elf_next_toc_section. This function removes duplicate GOT
10207 entries. */
10208
10209bfd_boolean
10210ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10211{
10212 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10213 struct bfd *ibfd, *ibfd2;
10214 bfd_boolean done_something;
10215
10216 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10217
7865406b
AM
10218 if (!htab->do_multi_toc)
10219 return FALSE;
10220
d0fae19d 10221 /* Merge global sym got entries within a toc group. */
927be08e
AM
10222 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10223
10224 /* And tlsld_got. */
10225 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10226 {
10227 struct got_entry *ent, *ent2;
10228
10229 if (!is_ppc64_elf (ibfd))
10230 continue;
10231
10232 ent = ppc64_tlsld_got (ibfd);
10233 if (!ent->is_indirect
10234 && ent->got.offset != (bfd_vma) -1)
10235 {
10236 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10237 {
10238 if (!is_ppc64_elf (ibfd2))
10239 continue;
10240
10241 ent2 = ppc64_tlsld_got (ibfd2);
10242 if (!ent2->is_indirect
10243 && ent2->got.offset != (bfd_vma) -1
10244 && elf_gp (ibfd2) == elf_gp (ibfd))
10245 {
10246 ent2->is_indirect = TRUE;
10247 ent2->got.ent = ent;
10248 }
10249 }
10250 }
10251 }
10252
10253 /* Zap sizes of got sections. */
10254 htab->reliplt->rawsize = htab->reliplt->size;
10255 htab->reliplt->size -= htab->got_reli_size;
10256 htab->got_reli_size = 0;
10257
10258 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10259 {
10260 asection *got, *relgot;
10261
10262 if (!is_ppc64_elf (ibfd))
10263 continue;
10264
10265 got = ppc64_elf_tdata (ibfd)->got;
10266 if (got != NULL)
10267 {
10268 got->rawsize = got->size;
10269 got->size = 0;
10270 relgot = ppc64_elf_tdata (ibfd)->relgot;
10271 relgot->rawsize = relgot->size;
10272 relgot->size = 0;
10273 }
10274 }
10275
10276 /* Now reallocate the got, local syms first. We don't need to
10277 allocate section contents again since we never increase size. */
10278 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10279 {
10280 struct got_entry **lgot_ents;
10281 struct got_entry **end_lgot_ents;
10282 struct plt_entry **local_plt;
10283 struct plt_entry **end_local_plt;
f961d9dd 10284 unsigned char *lgot_masks;
927be08e
AM
10285 bfd_size_type locsymcount;
10286 Elf_Internal_Shdr *symtab_hdr;
10287 asection *s, *srel;
10288
10289 if (!is_ppc64_elf (ibfd))
10290 continue;
10291
10292 lgot_ents = elf_local_got_ents (ibfd);
10293 if (!lgot_ents)
10294 continue;
10295
10296 symtab_hdr = &elf_symtab_hdr (ibfd);
10297 locsymcount = symtab_hdr->sh_info;
10298 end_lgot_ents = lgot_ents + locsymcount;
10299 local_plt = (struct plt_entry **) end_lgot_ents;
10300 end_local_plt = local_plt + locsymcount;
f961d9dd 10301 lgot_masks = (unsigned char *) end_local_plt;
927be08e
AM
10302 s = ppc64_elf_tdata (ibfd)->got;
10303 srel = ppc64_elf_tdata (ibfd)->relgot;
10304 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10305 {
10306 struct got_entry *ent;
10307
10308 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d
AM
10309 {
10310 unsigned int num = 1;
10311 ent->got.offset = s->size;
10312 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10313 num = 2;
10314 s->size += num * 8;
10315 if (info->shared)
10316 srel->size += num * sizeof (Elf64_External_Rela);
10317 else if ((*lgot_masks & PLT_IFUNC) != 0)
10318 {
10319 htab->reliplt->size
10320 += num * sizeof (Elf64_External_Rela);
10321 htab->got_reli_size
10322 += num * sizeof (Elf64_External_Rela);
10323 }
10324 }
927be08e
AM
10325 }
10326 }
10327
10328 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10329
10330 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10331 {
10332 struct got_entry *ent;
10333
10334 if (!is_ppc64_elf (ibfd))
10335 continue;
10336
10337 ent = ppc64_tlsld_got (ibfd);
10338 if (!ent->is_indirect
10339 && ent->got.offset != (bfd_vma) -1)
10340 {
10341 asection *s = ppc64_elf_tdata (ibfd)->got;
10342 ent->got.offset = s->size;
10343 s->size += 16;
10344 if (info->shared)
10345 {
10346 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10347 srel->size += sizeof (Elf64_External_Rela);
10348 }
10349 }
10350 }
10351
10352 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10353 if (!done_something)
10354 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10355 {
10356 asection *got;
10357
10358 if (!is_ppc64_elf (ibfd))
10359 continue;
10360
10361 got = ppc64_elf_tdata (ibfd)->got;
10362 if (got != NULL)
10363 {
10364 done_something = got->rawsize != got->size;
10365 if (done_something)
10366 break;
10367 }
10368 }
10369
10370 if (done_something)
10371 (*htab->layout_sections_again) ();
10372
10373 /* Set up for second pass over toc sections to recalculate elf_gp
10374 on input sections. */
10375 htab->toc_bfd = NULL;
10376 htab->toc_first_sec = NULL;
10377 htab->second_toc_pass = TRUE;
10378 return done_something;
10379}
10380
10381/* Called after second pass of multitoc partitioning. */
10382
10383void
10384ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10385{
10386 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10387
10388 /* After the second pass, toc_curr tracks the TOC offset used
10389 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 10390 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
10391}
10392
9b5ecbd0
AM
10393/* No toc references were found in ISEC. If the code in ISEC makes no
10394 calls, then there's no need to use toc adjusting stubs when branching
10395 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
10396 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10397 needed, and 2 if a cyclical call-graph was found but no other reason
10398 for a stub was detected. If called from the top level, a return of
10399 2 means the same as a return of 0. */
9b5ecbd0
AM
10400
10401static int
4ce794b7 10402toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 10403{
9b5ecbd0 10404 int ret;
70cc837d
AM
10405
10406 /* Mark this section as checked. */
10407 isec->call_check_done = 1;
9b5ecbd0 10408
772119ce
AM
10409 /* We know none of our code bearing sections will need toc stubs. */
10410 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10411 return 0;
10412
eea6121a 10413 if (isec->size == 0)
082c50f8
AM
10414 return 0;
10415
4c52953f
AM
10416 if (isec->output_section == NULL)
10417 return 0;
10418
4c52953f 10419 ret = 0;
70cc837d 10420 if (isec->reloc_count != 0)
9b5ecbd0 10421 {
70cc837d
AM
10422 Elf_Internal_Rela *relstart, *rel;
10423 Elf_Internal_Sym *local_syms;
10424 struct ppc_link_hash_table *htab;
2917689a 10425
70cc837d
AM
10426 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10427 info->keep_memory);
10428 if (relstart == NULL)
10429 return -1;
90aecf7a 10430
70cc837d
AM
10431 /* Look for branches to outside of this section. */
10432 local_syms = NULL;
10433 htab = ppc_hash_table (info);
10434 if (htab == NULL)
10435 return -1;
4c52953f 10436
70cc837d 10437 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 10438 {
70cc837d
AM
10439 enum elf_ppc64_reloc_type r_type;
10440 unsigned long r_symndx;
10441 struct elf_link_hash_entry *h;
10442 struct ppc_link_hash_entry *eh;
10443 Elf_Internal_Sym *sym;
10444 asection *sym_sec;
10445 struct _opd_sec_data *opd;
10446 bfd_vma sym_value;
10447 bfd_vma dest;
10448
10449 r_type = ELF64_R_TYPE (rel->r_info);
10450 if (r_type != R_PPC64_REL24
10451 && r_type != R_PPC64_REL14
10452 && r_type != R_PPC64_REL14_BRTAKEN
10453 && r_type != R_PPC64_REL14_BRNTAKEN)
10454 continue;
4c52953f 10455
70cc837d
AM
10456 r_symndx = ELF64_R_SYM (rel->r_info);
10457 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10458 isec->owner))
4c52953f 10459 {
70cc837d
AM
10460 ret = -1;
10461 break;
10462 }
4c52953f 10463
70cc837d
AM
10464 /* Calls to dynamic lib functions go through a plt call stub
10465 that uses r2. */
10466 eh = (struct ppc_link_hash_entry *) h;
10467 if (eh != NULL
10468 && (eh->elf.plt.plist != NULL
10469 || (eh->oh != NULL
10470 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10471 {
10472 ret = 1;
10473 break;
4c52953f
AM
10474 }
10475
70cc837d
AM
10476 if (sym_sec == NULL)
10477 /* Ignore other undefined symbols. */
4c52953f 10478 continue;
4c52953f 10479
70cc837d
AM
10480 /* Assume branches to other sections not included in the
10481 link need stubs too, to cover -R and absolute syms. */
10482 if (sym_sec->output_section == NULL)
10483 {
10484 ret = 1;
10485 break;
10486 }
4c52953f 10487
70cc837d
AM
10488 if (h == NULL)
10489 sym_value = sym->st_value;
10490 else
10491 {
10492 if (h->root.type != bfd_link_hash_defined
10493 && h->root.type != bfd_link_hash_defweak)
10494 abort ();
10495 sym_value = h->root.u.def.value;
10496 }
10497 sym_value += rel->r_addend;
4c52953f 10498
70cc837d
AM
10499 /* If this branch reloc uses an opd sym, find the code section. */
10500 opd = get_opd_info (sym_sec);
10501 if (opd != NULL)
10502 {
10503 if (h == NULL && opd->adjust != NULL)
10504 {
10505 long adjust;
4c52953f 10506
70cc837d
AM
10507 adjust = opd->adjust[sym->st_value / 8];
10508 if (adjust == -1)
10509 /* Assume deleted functions won't ever be called. */
10510 continue;
10511 sym_value += adjust;
10512 }
4c52953f 10513
70cc837d
AM
10514 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10515 if (dest == (bfd_vma) -1)
10516 continue;
10517 }
10518 else
10519 dest = (sym_value
10520 + sym_sec->output_offset
10521 + sym_sec->output_section->vma);
4c52953f 10522
70cc837d
AM
10523 /* Ignore branch to self. */
10524 if (sym_sec == isec)
10525 continue;
4c52953f 10526
70cc837d
AM
10527 /* If the called function uses the toc, we need a stub. */
10528 if (sym_sec->has_toc_reloc
10529 || sym_sec->makes_toc_func_call)
4c52953f 10530 {
70cc837d 10531 ret = 1;
4c52953f
AM
10532 break;
10533 }
70cc837d
AM
10534
10535 /* Assume any branch that needs a long branch stub might in fact
10536 need a plt_branch stub. A plt_branch stub uses r2. */
10537 else if (dest - (isec->output_offset
10538 + isec->output_section->vma
10539 + rel->r_offset) + (1 << 25) >= (2 << 25))
4c52953f 10540 {
70cc837d
AM
10541 ret = 1;
10542 break;
10543 }
10544
10545 /* If calling back to a section in the process of being
10546 tested, we can't say for sure that no toc adjusting stubs
10547 are needed, so don't return zero. */
10548 else if (sym_sec->call_check_in_progress)
10549 ret = 2;
10550
10551 /* Branches to another section that itself doesn't have any TOC
10552 references are OK. Recursively call ourselves to check. */
10553 else if (!sym_sec->call_check_done)
10554 {
10555 int recur;
10556
10557 /* Mark current section as indeterminate, so that other
10558 sections that call back to current won't be marked as
10559 known. */
10560 isec->call_check_in_progress = 1;
10561 recur = toc_adjusting_stub_needed (info, sym_sec);
10562 isec->call_check_in_progress = 0;
10563
4c52953f
AM
10564 if (recur != 0)
10565 {
70cc837d
AM
10566 ret = recur;
10567 if (recur != 2)
10568 break;
4c52953f
AM
10569 }
10570 }
4c52953f 10571 }
70cc837d
AM
10572
10573 if (local_syms != NULL
10574 && (elf_symtab_hdr (isec->owner).contents
10575 != (unsigned char *) local_syms))
10576 free (local_syms);
10577 if (elf_section_data (isec)->relocs != relstart)
10578 free (relstart);
9b5ecbd0
AM
10579 }
10580
70cc837d
AM
10581 if ((ret & 1) == 0
10582 && isec->map_head.s != NULL
10583 && (strcmp (isec->output_section->name, ".init") == 0
10584 || strcmp (isec->output_section->name, ".fini") == 0))
10585 {
10586 if (isec->map_head.s->has_toc_reloc
10587 || isec->map_head.s->makes_toc_func_call)
10588 ret = 1;
10589 else if (!isec->map_head.s->call_check_done)
10590 {
10591 int recur;
10592 isec->call_check_in_progress = 1;
10593 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10594 isec->call_check_in_progress = 0;
10595 if (recur != 0)
10596 ret = recur;
10597 }
10598 }
10599
10600 if (ret == 1)
10601 isec->makes_toc_func_call = 1;
4c52953f 10602
9b5ecbd0
AM
10603 return ret;
10604}
10605
721956f4
AM
10606/* The linker repeatedly calls this function for each input section,
10607 in the order that input sections are linked into output sections.
10608 Build lists of input sections to determine groupings between which
10609 we may insert linker stubs. */
10610
9b5ecbd0 10611bfd_boolean
4ce794b7 10612ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
10613{
10614 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10615
4dfe6ac6
NC
10616 if (htab == NULL)
10617 return FALSE;
10618
734b6cf9
AM
10619 if ((isec->output_section->flags & SEC_CODE) != 0
10620 && isec->output_section->index <= htab->top_index)
721956f4 10621 {
734b6cf9 10622 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 10623 /* Steal the link_sec pointer for our list. */
721956f4 10624#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
10625 /* This happens to make the list in reverse order,
10626 which is what we want. */
734b6cf9
AM
10627 PREV_SEC (isec) = *list;
10628 *list = isec;
721956f4 10629 }
ad8e1ba5 10630
4c52953f 10631 if (htab->multi_toc_needed)
9b5ecbd0 10632 {
4c52953f
AM
10633 /* If a code section has a function that uses the TOC then we need
10634 to use the right TOC (obviously). Also, make sure that .opd gets
10635 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
10636 can't find their function symbol (shouldn't ever happen now).
10637 Also specially treat .fixup for the linux kernel. .fixup
10638 contains branches, but only back to the function that hit an
10639 exception. */
10640 if (isec->has_toc_reloc
10641 || (isec->flags & SEC_CODE) == 0
10642 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
10643 {
10644 if (elf_gp (isec->owner) != 0)
10645 htab->toc_curr = elf_gp (isec->owner);
10646 }
6683a28d
AM
10647 else
10648 {
10649 if (!isec->call_check_done
10650 && toc_adjusting_stub_needed (info, isec) < 0)
10651 return FALSE;
10652 /* If we make a local call from this section, ie. a branch
10653 without a following nop, then we have no place to put a
10654 toc restoring insn. We must use the same toc group as
10655 the callee.
10656 Testing makes_toc_func_call actually tests for *any*
10657 calls to functions that need a good toc pointer. A more
10658 precise test would be better, as this one will set
10659 incorrect values for pasted .init/.fini fragments.
10660 (Fixed later in check_pasted_section.) */
10661 if (isec->makes_toc_func_call
10662 && elf_gp (isec->owner) != 0)
10663 htab->toc_curr = elf_gp (isec->owner);
10664 }
9b5ecbd0 10665 }
ad8e1ba5
AM
10666
10667 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 10668 Use the last TOC base. */
ad8e1ba5 10669 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 10670 return TRUE;
721956f4
AM
10671}
10672
70cc837d
AM
10673/* Check that all .init and .fini sections use the same toc, if they
10674 have toc relocs. */
10675
10676static bfd_boolean
10677check_pasted_section (struct bfd_link_info *info, const char *name)
10678{
10679 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10680
10681 if (o != NULL)
10682 {
10683 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10684 bfd_vma toc_off = 0;
10685 asection *i;
10686
10687 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10688 if (i->has_toc_reloc)
10689 {
10690 if (toc_off == 0)
10691 toc_off = htab->stub_group[i->id].toc_off;
10692 else if (toc_off != htab->stub_group[i->id].toc_off)
10693 return FALSE;
10694 }
6683a28d
AM
10695
10696 if (toc_off == 0)
10697 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10698 if (i->makes_toc_func_call)
10699 {
10700 toc_off = htab->stub_group[i->id].toc_off;
10701 break;
10702 }
10703
70cc837d
AM
10704 /* Make sure the whole pasted function uses the same toc offset. */
10705 if (toc_off != 0)
10706 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10707 htab->stub_group[i->id].toc_off = toc_off;
10708 }
10709 return TRUE;
10710}
10711
10712bfd_boolean
10713ppc64_elf_check_init_fini (struct bfd_link_info *info)
10714{
10715 return (check_pasted_section (info, ".init")
10716 & check_pasted_section (info, ".fini"));
10717}
10718
721956f4
AM
10719/* See whether we can group stub sections together. Grouping stub
10720 sections may result in fewer stubs. More importantly, we need to
10721 put all .init* and .fini* stubs at the beginning of the .init or
10722 .fini output sections respectively, because glibc splits the
10723 _init and _fini functions into multiple parts. Putting a stub in
10724 the middle of a function is not a good idea. */
10725
10726static void
4ce794b7
AM
10727group_sections (struct ppc_link_hash_table *htab,
10728 bfd_size_type stub_group_size,
10729 bfd_boolean stubs_always_before_branch)
721956f4 10730{
7c8fe5c4
AM
10731 asection **list;
10732 bfd_size_type stub14_group_size;
10733 bfd_boolean suppress_size_errors;
10734
10735 suppress_size_errors = FALSE;
10736 stub14_group_size = stub_group_size;
10737 if (stub_group_size == 1)
10738 {
10739 /* Default values. */
10740 if (stubs_always_before_branch)
10741 {
10742 stub_group_size = 0x1e00000;
10743 stub14_group_size = 0x7800;
10744 }
10745 else
10746 {
10747 stub_group_size = 0x1c00000;
10748 stub14_group_size = 0x7000;
10749 }
10750 suppress_size_errors = TRUE;
10751 }
10752
10753 list = htab->input_list + htab->top_index;
734b6cf9 10754 do
721956f4 10755 {
734b6cf9
AM
10756 asection *tail = *list;
10757 while (tail != NULL)
721956f4 10758 {
734b6cf9
AM
10759 asection *curr;
10760 asection *prev;
10761 bfd_size_type total;
10762 bfd_boolean big_sec;
10763 bfd_vma curr_toc;
10764
10765 curr = tail;
eea6121a 10766 total = tail->size;
6bee8834
AM
10767 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10768 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
10769 ? stub14_group_size : stub_group_size);
10770 if (big_sec && !suppress_size_errors)
5c3dead3
AM
10771 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10772 tail->owner, tail);
734b6cf9
AM
10773 curr_toc = htab->stub_group[tail->id].toc_off;
10774
10775 while ((prev = PREV_SEC (curr)) != NULL
10776 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
10777 < (ppc64_elf_section_data (prev) != NULL
10778 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10779 ? stub14_group_size : stub_group_size))
ad8e1ba5 10780 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
10781 curr = prev;
10782
10783 /* OK, the size from the start of CURR to the end is less
10784 than stub_group_size and thus can be handled by one stub
10785 section. (or the tail section is itself larger than
10786 stub_group_size, in which case we may be toast.) We
10787 should really be keeping track of the total size of stubs
10788 added here, as stubs contribute to the final output
10789 section size. That's a little tricky, and this way will
10790 only break if stubs added make the total size more than
10791 2^25, ie. for the default stub_group_size, if stubs total
10792 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10793 do
721956f4
AM
10794 {
10795 prev = PREV_SEC (tail);
734b6cf9 10796 /* Set up this stub group. */
721956f4
AM
10797 htab->stub_group[tail->id].link_sec = curr;
10798 }
734b6cf9
AM
10799 while (tail != curr && (tail = prev) != NULL);
10800
10801 /* But wait, there's more! Input sections up to stub_group_size
10802 bytes before the stub section can be handled by it too.
10803 Don't do this if we have a really large section after the
10804 stubs, as adding more stubs increases the chance that
10805 branches may not reach into the stub section. */
10806 if (!stubs_always_before_branch && !big_sec)
10807 {
10808 total = 0;
10809 while (prev != NULL
10810 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
10811 < (ppc64_elf_section_data (prev) != NULL
10812 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10813 ? stub14_group_size : stub_group_size))
734b6cf9
AM
10814 && htab->stub_group[prev->id].toc_off == curr_toc)
10815 {
10816 tail = prev;
10817 prev = PREV_SEC (tail);
10818 htab->stub_group[tail->id].link_sec = curr;
10819 }
10820 }
10821 tail = prev;
721956f4
AM
10822 }
10823 }
734b6cf9
AM
10824 while (list-- != htab->input_list);
10825 free (htab->input_list);
721956f4
AM
10826#undef PREV_SEC
10827}
10828
721956f4
AM
10829/* Determine and set the size of the stub section for a final link.
10830
10831 The basic idea here is to examine all the relocations looking for
10832 PC-relative calls to a target that is unreachable with a "bl"
10833 instruction. */
10834
b34976b6 10835bfd_boolean
9df0ef5f
AM
10836ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
10837 bfd_boolean plt_static_chain)
721956f4
AM
10838{
10839 bfd_size_type stub_group_size;
b34976b6 10840 bfd_boolean stubs_always_before_branch;
721956f4
AM
10841 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10842
4dfe6ac6
NC
10843 if (htab == NULL)
10844 return FALSE;
10845
9df0ef5f 10846 htab->plt_static_chain = plt_static_chain;
721956f4
AM
10847 stubs_always_before_branch = group_size < 0;
10848 if (group_size < 0)
10849 stub_group_size = -group_size;
10850 else
10851 stub_group_size = group_size;
721956f4
AM
10852
10853 group_sections (htab, stub_group_size, stubs_always_before_branch);
10854
721956f4
AM
10855 while (1)
10856 {
10857 bfd *input_bfd;
10858 unsigned int bfd_indx;
10859 asection *stub_sec;
721956f4
AM
10860
10861 htab->stub_iteration += 1;
721956f4
AM
10862
10863 for (input_bfd = info->input_bfds, bfd_indx = 0;
10864 input_bfd != NULL;
10865 input_bfd = input_bfd->link_next, bfd_indx++)
10866 {
10867 Elf_Internal_Shdr *symtab_hdr;
10868 asection *section;
6cdc0ccc 10869 Elf_Internal_Sym *local_syms = NULL;
721956f4 10870
0c8d6e5c 10871 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
10872 continue;
10873
721956f4 10874 /* We'll need the symbol table in a second. */
0ffa91dd 10875 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
10876 if (symtab_hdr->sh_info == 0)
10877 continue;
10878
721956f4
AM
10879 /* Walk over each section attached to the input bfd. */
10880 for (section = input_bfd->sections;
10881 section != NULL;
10882 section = section->next)
10883 {
721956f4 10884 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
10885
10886 /* If there aren't any relocs, then there's nothing more
10887 to do. */
10888 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
10889 || (section->flags & SEC_ALLOC) == 0
10890 || (section->flags & SEC_LOAD) == 0
10891 || (section->flags & SEC_CODE) == 0
721956f4
AM
10892 || section->reloc_count == 0)
10893 continue;
10894
10895 /* If this section is a link-once section that will be
10896 discarded, then don't create any stubs. */
10897 if (section->output_section == NULL
927be08e 10898 || section->output_section->owner != info->output_bfd)
721956f4
AM
10899 continue;
10900
1e2f5b6e
AM
10901 /* Get the relocs. */
10902 internal_relocs
4ce794b7 10903 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 10904 info->keep_memory);
721956f4 10905 if (internal_relocs == NULL)
1e2f5b6e 10906 goto error_ret_free_local;
721956f4
AM
10907
10908 /* Now examine each relocation. */
10909 irela = internal_relocs;
10910 irelaend = irela + section->reloc_count;
10911 for (; irela < irelaend; irela++)
10912 {
4ce794b7
AM
10913 enum elf_ppc64_reloc_type r_type;
10914 unsigned int r_indx;
721956f4
AM
10915 enum ppc_stub_type stub_type;
10916 struct ppc_stub_hash_entry *stub_entry;
8387904d 10917 asection *sym_sec, *code_sec;
e054468f 10918 bfd_vma sym_value, code_value;
721956f4 10919 bfd_vma destination;
8843416a 10920 bfd_boolean ok_dest;
721956f4 10921 struct ppc_link_hash_entry *hash;
8387904d 10922 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
10923 struct elf_link_hash_entry *h;
10924 Elf_Internal_Sym *sym;
721956f4
AM
10925 char *stub_name;
10926 const asection *id_sec;
74f0fb50 10927 struct _opd_sec_data *opd;
e054468f 10928 struct plt_entry *plt_ent;
721956f4
AM
10929
10930 r_type = ELF64_R_TYPE (irela->r_info);
10931 r_indx = ELF64_R_SYM (irela->r_info);
10932
4ce794b7 10933 if (r_type >= R_PPC64_max)
721956f4
AM
10934 {
10935 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 10936 goto error_ret_free_internal;
721956f4
AM
10937 }
10938
10939 /* Only look for stubs on branch instructions. */
4ce794b7
AM
10940 if (r_type != R_PPC64_REL24
10941 && r_type != R_PPC64_REL14
10942 && r_type != R_PPC64_REL14_BRTAKEN
10943 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
10944 continue;
10945
10946 /* Now determine the call target, its name, value,
10947 section. */
411e1bfb
AM
10948 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10949 r_indx, input_bfd))
10950 goto error_ret_free_internal;
10951 hash = (struct ppc_link_hash_entry *) h;
10952
8843416a 10953 ok_dest = FALSE;
8387904d 10954 fdh = NULL;
7fe2b9a6 10955 sym_value = 0;
411e1bfb 10956 if (hash == NULL)
721956f4 10957 {
411e1bfb 10958 sym_value = sym->st_value;
8843416a 10959 ok_dest = TRUE;
721956f4 10960 }
7fe2b9a6
AM
10961 else if (hash->elf.root.type == bfd_link_hash_defined
10962 || hash->elf.root.type == bfd_link_hash_defweak)
10963 {
10964 sym_value = hash->elf.root.u.def.value;
10965 if (sym_sec->output_section != NULL)
10966 ok_dest = TRUE;
10967 }
10968 else if (hash->elf.root.type == bfd_link_hash_undefweak
10969 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 10970 {
99877b66 10971 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
10972 use the func descriptor sym instead if it is
10973 defined. */
ceb1f1ef 10974 if (hash->elf.root.root.string[0] == '.'
b31867b6 10975 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 10976 {
8387904d
AM
10977 if (fdh->elf.root.type == bfd_link_hash_defined
10978 || fdh->elf.root.type == bfd_link_hash_defweak)
10979 {
10980 sym_sec = fdh->elf.root.u.def.section;
10981 sym_value = fdh->elf.root.u.def.value;
10982 if (sym_sec->output_section != NULL)
10983 ok_dest = TRUE;
10984 }
99877b66
AM
10985 else
10986 fdh = NULL;
8387904d 10987 }
7fe2b9a6
AM
10988 }
10989 else
10990 {
10991 bfd_set_error (bfd_error_bad_value);
10992 goto error_ret_free_internal;
721956f4
AM
10993 }
10994
8843416a
AM
10995 destination = 0;
10996 if (ok_dest)
10997 {
10998 sym_value += irela->r_addend;
10999 destination = (sym_value
11000 + sym_sec->output_offset
11001 + sym_sec->output_section->vma);
11002 }
11003
8387904d 11004 code_sec = sym_sec;
e054468f 11005 code_value = sym_value;
74f0fb50
AM
11006 opd = get_opd_info (sym_sec);
11007 if (opd != NULL)
8387904d
AM
11008 {
11009 bfd_vma dest;
11010
74f0fb50 11011 if (hash == NULL && opd->adjust != NULL)
8387904d 11012 {
74f0fb50 11013 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11014 if (adjust == -1)
11015 continue;
e054468f 11016 code_value += adjust;
8387904d
AM
11017 sym_value += adjust;
11018 }
11019 dest = opd_entry_value (sym_sec, sym_value,
e054468f 11020 &code_sec, &code_value);
8387904d
AM
11021 if (dest != (bfd_vma) -1)
11022 {
11023 destination = dest;
11024 if (fdh != NULL)
11025 {
11026 /* Fixup old ABI sym to point at code
11027 entry. */
99877b66 11028 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11029 hash->elf.root.u.def.section = code_sec;
e054468f 11030 hash->elf.root.u.def.value = code_value;
8387904d
AM
11031 }
11032 }
11033 }
11034
721956f4 11035 /* Determine what (if any) linker stub is needed. */
e054468f 11036 plt_ent = NULL;
721956f4 11037 stub_type = ppc_type_of_stub (section, irela, &hash,
e054468f 11038 &plt_ent, destination);
ad8e1ba5
AM
11039
11040 if (stub_type != ppc_stub_plt_call)
11041 {
11042 /* Check whether we need a TOC adjusting stub.
11043 Since the linker pastes together pieces from
11044 different object files when creating the
11045 _init and _fini functions, it may be that a
11046 call to what looks like a local sym is in
11047 fact a call needing a TOC adjustment. */
8387904d
AM
11048 if (code_sec != NULL
11049 && code_sec->output_section != NULL
11050 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11051 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11052 && (code_sec->has_toc_reloc
11053 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
11054 stub_type = ppc_stub_long_branch_r2off;
11055 }
11056
721956f4
AM
11057 if (stub_type == ppc_stub_none)
11058 continue;
11059
411e1bfb
AM
11060 /* __tls_get_addr calls might be eliminated. */
11061 if (stub_type != ppc_stub_plt_call
11062 && hash != NULL
8387904d
AM
11063 && (hash == htab->tls_get_addr
11064 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11065 && section->has_tls_reloc
11066 && irela != internal_relocs)
11067 {
11068 /* Get tls info. */
f961d9dd 11069 unsigned char *tls_mask;
411e1bfb 11070
3a71aa26 11071 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11072 irela - 1, input_bfd))
11073 goto error_ret_free_internal;
e7b938ca 11074 if (*tls_mask != 0)
411e1bfb
AM
11075 continue;
11076 }
11077
721956f4
AM
11078 /* Support for grouping stub sections. */
11079 id_sec = htab->stub_group[section->id].link_sec;
11080
11081 /* Get the name of this stub. */
11082 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11083 if (!stub_name)
11084 goto error_ret_free_internal;
11085
11086 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11087 stub_name, FALSE, FALSE);
721956f4
AM
11088 if (stub_entry != NULL)
11089 {
11090 /* The proper stub has already been created. */
11091 free (stub_name);
11092 continue;
11093 }
11094
25f53a85 11095 stub_entry = ppc_add_stub (stub_name, section, info);
721956f4
AM
11096 if (stub_entry == NULL)
11097 {
11098 free (stub_name);
6cdc0ccc
AM
11099 error_ret_free_internal:
11100 if (elf_section_data (section)->relocs == NULL)
11101 free (internal_relocs);
11102 error_ret_free_local:
11103 if (local_syms != NULL
11104 && (symtab_hdr->contents
11105 != (unsigned char *) local_syms))
11106 free (local_syms);
b34976b6 11107 return FALSE;
721956f4
AM
11108 }
11109
ad8e1ba5 11110 stub_entry->stub_type = stub_type;
e054468f
AM
11111 if (stub_type != ppc_stub_plt_call)
11112 {
11113 stub_entry->target_value = code_value;
11114 stub_entry->target_section = code_sec;
11115 }
11116 else
11117 {
11118 stub_entry->target_value = sym_value;
11119 stub_entry->target_section = sym_sec;
11120 }
721956f4 11121 stub_entry->h = hash;
e054468f 11122 stub_entry->plt_ent = plt_ent;
411e1bfb 11123 stub_entry->addend = irela->r_addend;
ee75fd95
AM
11124
11125 if (stub_entry->h != NULL)
11126 htab->stub_globals += 1;
721956f4
AM
11127 }
11128
11129 /* We're done with the internal relocs, free them. */
6cdc0ccc 11130 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 11131 free (internal_relocs);
721956f4 11132 }
6cdc0ccc
AM
11133
11134 if (local_syms != NULL
11135 && symtab_hdr->contents != (unsigned char *) local_syms)
11136 {
11137 if (!info->keep_memory)
11138 free (local_syms);
11139 else
11140 symtab_hdr->contents = (unsigned char *) local_syms;
11141 }
721956f4
AM
11142 }
11143
5c3dead3 11144 /* We may have added some stubs. Find out the new size of the
721956f4
AM
11145 stub sections. */
11146 for (stub_sec = htab->stub_bfd->sections;
11147 stub_sec != NULL;
11148 stub_sec = stub_sec->next)
e717da7e 11149 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 11150 {
5c3dead3 11151 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
11152 stub_sec->size = 0;
11153 stub_sec->reloc_count = 0;
84f5d08e 11154 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 11155 }
eea6121a
AM
11156
11157 htab->brlt->size = 0;
84f5d08e
AM
11158 htab->brlt->reloc_count = 0;
11159 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 11160 if (htab->relbrlt != NULL)
eea6121a 11161 htab->relbrlt->size = 0;
721956f4 11162
63bc6f6c 11163 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 11164
176a0d42
AM
11165 if (info->emitrelocations
11166 && htab->glink != NULL && htab->glink->size != 0)
11167 {
11168 htab->glink->reloc_count = 1;
11169 htab->glink->flags |= SEC_RELOC;
11170 }
11171
5c3dead3
AM
11172 for (stub_sec = htab->stub_bfd->sections;
11173 stub_sec != NULL;
11174 stub_sec = stub_sec->next)
11175 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11176 && stub_sec->rawsize != stub_sec->size)
11177 break;
11178
11179 /* Exit from this loop when no stubs have been added, and no stubs
11180 have changed size. */
11181 if (stub_sec == NULL)
11182 break;
11183
721956f4
AM
11184 /* Ask the linker to do its stuff. */
11185 (*htab->layout_sections_again) ();
11186 }
11187
c456f082 11188 /* It would be nice to strip htab->brlt from the output if the
afbe61cf
AM
11189 section is empty, but it's too late. If we strip sections here,
11190 the dynamic symbol table is corrupted since the section symbol
11191 for the stripped section isn't written. */
721956f4 11192
b34976b6 11193 return TRUE;
721956f4
AM
11194}
11195
11196/* Called after we have determined section placement. If sections
805fc799 11197 move, we'll be called again. Provide a value for TOCstart. */
721956f4 11198
805fc799 11199bfd_vma
4ce794b7 11200ppc64_elf_toc (bfd *obfd)
721956f4 11201{
805fc799
AM
11202 asection *s;
11203 bfd_vma TOCstart;
721956f4 11204
805fc799
AM
11205 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11206 order. The TOC starts where the first of these sections starts. */
11207 s = bfd_get_section_by_name (obfd, ".got");
e054468f 11208 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11209 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 11210 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11211 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 11212 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11213 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 11214 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
11215 {
11216 /* This may happen for
11217 o references to TOC base (SYM@toc / TOC[tc0]) without a
11218 .toc directive
11219 o bad linker script
11220 o --gc-sections and empty TOC sections
11221
11222 FIXME: Warn user? */
11223
11224 /* Look for a likely section. We probably won't even be
11225 using TOCstart. */
11226 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11227 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11228 | SEC_EXCLUDE))
805fc799
AM
11229 == (SEC_ALLOC | SEC_SMALL_DATA))
11230 break;
721956f4 11231 if (s == NULL)
805fc799 11232 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11233 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
11234 == (SEC_ALLOC | SEC_SMALL_DATA))
11235 break;
721956f4 11236 if (s == NULL)
805fc799 11237 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11238 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11239 == SEC_ALLOC)
805fc799 11240 break;
721956f4 11241 if (s == NULL)
805fc799 11242 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11243 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
11244 break;
11245 }
721956f4 11246
805fc799
AM
11247 TOCstart = 0;
11248 if (s != NULL)
11249 TOCstart = s->output_section->vma + s->output_offset;
721956f4 11250
805fc799 11251 return TOCstart;
721956f4
AM
11252}
11253
11254/* Build all the stubs associated with the current output file.
11255 The stubs are kept in a hash table attached to the main linker
11256 hash table. This function is called via gldelf64ppc_finish. */
11257
b34976b6 11258bfd_boolean
4ce794b7
AM
11259ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11260 struct bfd_link_info *info,
11261 char **stats)
5d1634d7
AM
11262{
11263 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 11264 asection *stub_sec;
5d1634d7 11265 bfd_byte *p;
e717da7e 11266 int stub_sec_count = 0;
5d1634d7 11267
4dfe6ac6
NC
11268 if (htab == NULL)
11269 return FALSE;
11270
ad8e1ba5 11271 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
11272
11273 /* Allocate memory to hold the linker stubs. */
721956f4
AM
11274 for (stub_sec = htab->stub_bfd->sections;
11275 stub_sec != NULL;
11276 stub_sec = stub_sec->next)
eea6121a
AM
11277 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11278 && stub_sec->size != 0)
e717da7e 11279 {
eea6121a
AM
11280 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11281 if (stub_sec->contents == NULL)
11282 return FALSE;
11283 /* We want to check that built size is the same as calculated
11284 size. rawsize is a convenient location to use. */
11285 stub_sec->rawsize = stub_sec->size;
11286 stub_sec->size = 0;
e717da7e 11287 }
5d1634d7 11288
23eb7e01 11289 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 11290 {
9f951329 11291 unsigned int indx;
ad8e1ba5 11292 bfd_vma plt0;
9f951329 11293
721956f4 11294 /* Build the .glink plt call stub. */
97b639ba
AM
11295 if (htab->emit_stub_syms)
11296 {
11297 struct elf_link_hash_entry *h;
468392fb
AM
11298 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11299 TRUE, FALSE, FALSE);
97b639ba
AM
11300 if (h == NULL)
11301 return FALSE;
11302 if (h->root.type == bfd_link_hash_new)
11303 {
11304 h->root.type = bfd_link_hash_defined;
11305 h->root.u.def.section = htab->glink;
ee4bf8d2 11306 h->root.u.def.value = 8;
f5385ebf
AM
11307 h->ref_regular = 1;
11308 h->def_regular = 1;
11309 h->ref_regular_nonweak = 1;
11310 h->forced_local = 1;
11311 h->non_elf = 0;
97b639ba
AM
11312 }
11313 }
176a0d42
AM
11314 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11315 if (info->emitrelocations)
11316 {
11317 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11318 if (r == NULL)
11319 return FALSE;
11320 r->r_offset = (htab->glink->output_offset
11321 + htab->glink->output_section->vma);
11322 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11323 r->r_addend = plt0;
11324 }
4ce794b7 11325 p = htab->glink->contents;
176a0d42 11326 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
11327 bfd_put_64 (htab->glink->owner, plt0, p);
11328 p += 8;
11329 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 11330 p += 4;
ee4bf8d2 11331 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 11332 p += 4;
ee4bf8d2 11333 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 11334 p += 4;
ee4bf8d2 11335 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 11336 p += 4;
ee4bf8d2 11337 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 11338 p += 4;
ee4bf8d2 11339 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 11340 p += 4;
ee4bf8d2 11341 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 11342 p += 4;
4ce794b7 11343 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 11344 p += 4;
4ce794b7 11345 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 11346 p += 4;
4ce794b7 11347 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 11348 p += 4;
4ce794b7 11349 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 11350 p += 4;
ee4bf8d2
AM
11351 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11352 {
11353 bfd_put_32 (htab->glink->owner, NOP, p);
11354 p += 4;
11355 }
ad8e1ba5 11356
9f951329
AM
11357 /* Build the .glink lazy link call stubs. */
11358 indx = 0;
eea6121a 11359 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
11360 {
11361 if (indx < 0x8000)
11362 {
4ce794b7 11363 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
11364 p += 4;
11365 }
11366 else
11367 {
4ce794b7 11368 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 11369 p += 4;
4ce794b7 11370 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
11371 p += 4;
11372 }
4ce794b7 11373 bfd_put_32 (htab->glink->owner,
ee4bf8d2 11374 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 11375 indx++;
9f951329
AM
11376 p += 4;
11377 }
eea6121a 11378 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 11379 }
5d1634d7 11380
eea6121a 11381 if (htab->brlt->size != 0)
721956f4 11382 {
4ce794b7 11383 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 11384 htab->brlt->size);
4ce794b7 11385 if (htab->brlt->contents == NULL)
b34976b6 11386 return FALSE;
721956f4 11387 }
ee75fd95 11388 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
11389 {
11390 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 11391 htab->relbrlt->size);
63bc6f6c
AM
11392 if (htab->relbrlt->contents == NULL)
11393 return FALSE;
11394 }
5d1634d7 11395
721956f4
AM
11396 /* Build the stubs as directed by the stub hash table. */
11397 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 11398
aa8a7074
AM
11399 if (htab->relbrlt != NULL)
11400 htab->relbrlt->reloc_count = 0;
11401
721956f4
AM
11402 for (stub_sec = htab->stub_bfd->sections;
11403 stub_sec != NULL;
11404 stub_sec = stub_sec->next)
e717da7e
AM
11405 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11406 {
11407 stub_sec_count += 1;
eea6121a 11408 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
11409 break;
11410 }
5d1634d7 11411
721956f4 11412 if (stub_sec != NULL
eea6121a 11413 || htab->glink->rawsize != htab->glink->size)
5d1634d7 11414 {
b34976b6 11415 htab->stub_error = TRUE;
25f53a85 11416 info->callbacks->einfo (_("stubs don't match calculated size\n"));
5d1634d7 11417 }
721956f4 11418
d2a300cf
AM
11419 if (htab->stub_error)
11420 return FALSE;
11421
11422 if (stats != NULL)
11423 {
11424 *stats = bfd_malloc (500);
11425 if (*stats == NULL)
11426 return FALSE;
11427
ee75fd95 11428 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
11429 " branch %lu\n"
11430 " toc adjust %lu\n"
11431 " long branch %lu\n"
11432 " long toc adj %lu\n"
11433 " plt call %lu"),
e717da7e 11434 stub_sec_count,
ee75fd95 11435 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
11436 htab->stub_count[ppc_stub_long_branch - 1],
11437 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11438 htab->stub_count[ppc_stub_plt_branch - 1],
11439 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11440 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
11441 }
11442 return TRUE;
5bd4f169
AM
11443}
11444
99877b66
AM
11445/* This function undoes the changes made by add_symbol_adjust. */
11446
11447static bfd_boolean
11448undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11449{
11450 struct ppc_link_hash_entry *eh;
11451
11452 if (h->root.type == bfd_link_hash_indirect)
11453 return TRUE;
11454
99877b66
AM
11455 eh = (struct ppc_link_hash_entry *) h;
11456 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11457 return TRUE;
11458
11459 eh->elf.root.type = bfd_link_hash_undefined;
11460 return TRUE;
11461}
11462
11463void
11464ppc64_elf_restore_symbols (struct bfd_link_info *info)
11465{
11466 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
11467
11468 if (htab != NULL)
11469 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
11470}
11471
60124e18
AM
11472/* What to do when ld finds relocations against symbols defined in
11473 discarded sections. */
11474
11475static unsigned int
11476ppc64_elf_action_discarded (asection *sec)
11477{
11478 if (strcmp (".opd", sec->name) == 0)
11479 return 0;
11480
11481 if (strcmp (".toc", sec->name) == 0)
11482 return 0;
11483
bce50a28
JJ
11484 if (strcmp (".toc1", sec->name) == 0)
11485 return 0;
11486
60124e18
AM
11487 return _bfd_elf_default_action_discarded (sec);
11488}
11489
ba761f19 11490/* REL points to a low-part reloc on a largetoc instruction sequence.
67f0cbdb 11491 Find the matching high-part reloc instruction and verify that it
d983b7a0
AM
11492 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11493 the high-part reloc. */
67f0cbdb
AM
11494
11495static const Elf_Internal_Rela *
11496ha_reloc_match (const Elf_Internal_Rela *relocs,
11497 const Elf_Internal_Rela *rel,
d983b7a0
AM
11498 unsigned int *reg,
11499 bfd_boolean match_addend,
67f0cbdb
AM
11500 const bfd *input_bfd,
11501 const bfd_byte *contents)
11502{
11503 enum elf_ppc64_reloc_type r_type, r_type_ha;
11504 bfd_vma r_info_ha, r_addend;
11505
11506 r_type = ELF64_R_TYPE (rel->r_info);
11507 switch (r_type)
11508 {
11509 case R_PPC64_GOT_TLSLD16_LO:
11510 case R_PPC64_GOT_TLSGD16_LO:
11511 case R_PPC64_GOT_TPREL16_LO_DS:
11512 case R_PPC64_GOT_DTPREL16_LO_DS:
11513 case R_PPC64_GOT16_LO:
11514 case R_PPC64_TOC16_LO:
11515 r_type_ha = r_type + 2;
11516 break;
11517 case R_PPC64_GOT16_LO_DS:
11518 r_type_ha = R_PPC64_GOT16_HA;
11519 break;
11520 case R_PPC64_TOC16_LO_DS:
11521 r_type_ha = R_PPC64_TOC16_HA;
11522 break;
11523 default:
11524 abort ();
11525 }
11526 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11527 r_addend = rel->r_addend;
11528
11529 while (--rel >= relocs)
11530 if (rel->r_info == r_info_ha
d983b7a0
AM
11531 && (!match_addend
11532 || rel->r_addend == r_addend))
67f0cbdb
AM
11533 {
11534 const bfd_byte *p = contents + (rel->r_offset & ~3);
11535 unsigned int insn = bfd_get_32 (input_bfd, p);
d983b7a0
AM
11536 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11537 && (insn & (0x1f << 21)) == (*reg << 21))
11538 {
11539 *reg = (insn >> 16) & 0x1f;
11540 return rel;
11541 }
67f0cbdb
AM
11542 break;
11543 }
11544 return NULL;
11545}
11546
5bd4f169
AM
11547/* The RELOCATE_SECTION function is called by the ELF backend linker
11548 to handle the relocations for a section.
11549
11550 The relocs are always passed as Rela structures; if the section
11551 actually uses Rel structures, the r_addend field will always be
11552 zero.
11553
11554 This function is responsible for adjust the section contents as
11555 necessary, and (if using Rela relocs and generating a
1049f94e 11556 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
11557 necessary.
11558
11559 This function does not have to worry about setting the reloc
11560 address or the reloc symbol index.
11561
11562 LOCAL_SYMS is a pointer to the swapped in local symbols.
11563
11564 LOCAL_SECTIONS is an array giving the section in the input file
11565 corresponding to the st_shndx field of each local symbol.
11566
11567 The global hash table entry for the global symbols can be found
11568 via elf_sym_hashes (input_bfd).
11569
1049f94e 11570 When generating relocatable output, this function must handle
5bd4f169
AM
11571 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11572 going to be the section symbol corresponding to the output
11573 section, which means that the addend must be adjusted
11574 accordingly. */
11575
b34976b6 11576static bfd_boolean
4ce794b7
AM
11577ppc64_elf_relocate_section (bfd *output_bfd,
11578 struct bfd_link_info *info,
11579 bfd *input_bfd,
11580 asection *input_section,
11581 bfd_byte *contents,
11582 Elf_Internal_Rela *relocs,
11583 Elf_Internal_Sym *local_syms,
11584 asection **local_sections)
5bd4f169 11585{
65f38f15 11586 struct ppc_link_hash_table *htab;
5bd4f169
AM
11587 Elf_Internal_Shdr *symtab_hdr;
11588 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
11589 Elf_Internal_Rela *rel;
11590 Elf_Internal_Rela *relend;
411e1bfb
AM
11591 Elf_Internal_Rela outrel;
11592 bfd_byte *loc;
411e1bfb 11593 struct got_entry **local_got_ents;
d983b7a0 11594 unsigned char *ha_opt;
5bd4f169 11595 bfd_vma TOCstart;
d983b7a0 11596 bfd_boolean no_ha_opt;
b34976b6
AM
11597 bfd_boolean ret = TRUE;
11598 bfd_boolean is_opd;
96e0dda4 11599 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 11600 bfd_boolean is_power4 = FALSE;
4fe5ca5b 11601 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 11602
65f38f15 11603 /* Initialize howto table if needed. */
5bd4f169 11604 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
11605 ppc_howto_init ();
11606
65f38f15 11607 htab = ppc_hash_table (info);
4dfe6ac6
NC
11608 if (htab == NULL)
11609 return FALSE;
ee75fd95
AM
11610
11611 /* Don't relocate stub sections. */
11612 if (input_section->owner == htab->stub_bfd)
11613 return TRUE;
11614
0c8d6e5c 11615 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 11616
411e1bfb 11617 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 11618 TOCstart = elf_gp (output_bfd);
0ffa91dd 11619 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 11620 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 11621 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
d983b7a0
AM
11622 ha_opt = NULL;
11623 no_ha_opt = FALSE;
65f38f15 11624
5bd4f169
AM
11625 rel = relocs;
11626 relend = relocs + input_section->reloc_count;
11627 for (; rel < relend; rel++)
11628 {
04c9666a 11629 enum elf_ppc64_reloc_type r_type;
4cc603a5 11630 bfd_vma addend, orig_addend;
5bd4f169
AM
11631 bfd_reloc_status_type r;
11632 Elf_Internal_Sym *sym;
11633 asection *sec;
039b3fef
AM
11634 struct elf_link_hash_entry *h_elf;
11635 struct ppc_link_hash_entry *h;
11636 struct ppc_link_hash_entry *fdh;
5bd4f169 11637 const char *sym_name;
0d4792f7 11638 unsigned long r_symndx, toc_symndx;
3a71aa26 11639 bfd_vma toc_addend;
f961d9dd
AM
11640 unsigned char tls_mask, tls_gd, tls_type;
11641 unsigned char sym_type;
5bd4f169 11642 bfd_vma relocation;
b34976b6
AM
11643 bfd_boolean unresolved_reloc;
11644 bfd_boolean warned;
67f0cbdb 11645 unsigned int insn;
e11840f9 11646 unsigned int mask;
721956f4
AM
11647 struct ppc_stub_hash_entry *stub_entry;
11648 bfd_vma max_br_offset;
11649 bfd_vma from;
5bd4f169 11650
4ce794b7 11651 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 11652 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
11653
11654 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11655 symbol of the previous ADDR64 reloc. The symbol gives us the
11656 proper TOC base to use. */
11657 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11658 && rel != relocs
11659 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11660 && is_opd)
11661 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11662
4ce794b7
AM
11663 sym = NULL;
11664 sec = NULL;
039b3fef 11665 h_elf = NULL;
4ce794b7 11666 sym_name = NULL;
b34976b6
AM
11667 unresolved_reloc = FALSE;
11668 warned = FALSE;
4cc603a5 11669 orig_addend = rel->r_addend;
65f38f15 11670
0b13192e 11671 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
11672 {
11673 /* It's a local symbol. */
74f0fb50 11674 struct _opd_sec_data *opd;
4025353c 11675
5bd4f169
AM
11676 sym = local_syms + r_symndx;
11677 sec = local_sections[r_symndx];
26c61ae5 11678 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 11679 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 11680 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
11681 opd = get_opd_info (sec);
11682 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 11683 {
74f0fb50 11684 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
11685 if (adjust == -1)
11686 relocation = 0;
11687 else
4cc603a5
AM
11688 {
11689 /* If this is a relocation against the opd section sym
11690 and we have edited .opd, adjust the reloc addend so
11691 that ld -r and ld --emit-relocs output is correct.
11692 If it is a reloc against some other .opd symbol,
11693 then the symbol value will be adjusted later. */
11694 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11695 rel->r_addend += adjust;
11696 else
11697 relocation += adjust;
11698 }
1e2f5b6e 11699 }
5bd4f169
AM
11700 }
11701 else
11702 {
b2a8e766
AM
11703 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11704 r_symndx, symtab_hdr, sym_hashes,
039b3fef 11705 h_elf, sec, relocation,
b2a8e766 11706 unresolved_reloc, warned);
039b3fef
AM
11707 sym_name = h_elf->root.root.string;
11708 sym_type = h_elf->type;
5bd4f169 11709 }
039b3fef 11710 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 11711
ab96bf03 11712 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
11713 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11714 rel, relend,
11715 ppc64_elf_howto_table[r_type],
11716 contents);
ab96bf03
AM
11717
11718 if (info->relocatable)
11719 continue;
11720
951fd09b
AM
11721 /* TLS optimizations. Replace instruction sequences and relocs
11722 based on information we collected in tls_optimize. We edit
11723 RELOCS so that --emit-relocs will output something sensible
11724 for the final instruction stream. */
11725 tls_mask = 0;
11726 tls_gd = 0;
0d4792f7 11727 toc_symndx = 0;
727fc41e
AM
11728 if (h != NULL)
11729 tls_mask = h->tls_mask;
11730 else if (local_got_ents != NULL)
411e1bfb 11731 {
e054468f
AM
11732 struct plt_entry **local_plt = (struct plt_entry **)
11733 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 11734 unsigned char *lgot_masks = (unsigned char *)
e054468f 11735 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
11736 tls_mask = lgot_masks[r_symndx];
11737 }
11738 if (tls_mask == 0
11739 && (r_type == R_PPC64_TLS
11740 || r_type == R_PPC64_TLSGD
11741 || r_type == R_PPC64_TLSLD))
11742 {
11743 /* Check for toc tls entries. */
f961d9dd 11744 unsigned char *toc_tls;
0d4792f7 11745
727fc41e
AM
11746 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11747 &local_syms, rel, input_bfd))
11748 return FALSE;
0d4792f7 11749
727fc41e
AM
11750 if (toc_tls)
11751 tls_mask = *toc_tls;
0d4792f7
AM
11752 }
11753
11754 /* Check that tls relocs are used with tls syms, and non-tls
11755 relocs are used with non-tls syms. */
cf35638d 11756 if (r_symndx != STN_UNDEF
0d4792f7
AM
11757 && r_type != R_PPC64_NONE
11758 && (h == NULL
039b3fef
AM
11759 || h->elf.root.type == bfd_link_hash_defined
11760 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
11761 && (IS_PPC64_TLS_RELOC (r_type)
11762 != (sym_type == STT_TLS
11763 || (sym_type == STT_SECTION
11764 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 11765 {
727fc41e
AM
11766 if (tls_mask != 0
11767 && (r_type == R_PPC64_TLS
11768 || r_type == R_PPC64_TLSGD
11769 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
11770 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11771 ;
11772 else
25f53a85 11773 info->callbacks->einfo
1d483afe 11774 (!IS_PPC64_TLS_RELOC (r_type)
25f53a85
AM
11775 ? _("%H: %s used with TLS symbol %s\n")
11776 : _("%H: %s used with non-TLS symbol %s\n"),
11777 input_bfd, input_section, rel->r_offset,
0d4792f7
AM
11778 ppc64_elf_howto_table[r_type]->name,
11779 sym_name);
411e1bfb
AM
11780 }
11781
11782 /* Ensure reloc mapping code below stays sane. */
11783 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11784 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11785 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11786 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11787 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11788 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11789 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11790 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11791 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11792 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11793 abort ();
0d4792f7 11794
411e1bfb
AM
11795 switch (r_type)
11796 {
11797 default:
411e1bfb
AM
11798 break;
11799
ba761f19
AM
11800 case R_PPC64_LO_DS_OPT:
11801 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11802 if ((insn & (0x3f << 26)) != 58u << 26)
11803 abort ();
11804 insn += (14u << 26) - (58u << 26);
11805 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11806 r_type = R_PPC64_TOC16_LO;
11807 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11808 break;
11809
411e1bfb
AM
11810 case R_PPC64_TOC16:
11811 case R_PPC64_TOC16_LO:
11812 case R_PPC64_TOC16_DS:
11813 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
11814 {
11815 /* Check for toc tls entries. */
f961d9dd 11816 unsigned char *toc_tls;
951fd09b 11817 int retval;
411e1bfb 11818
3a71aa26
AM
11819 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11820 &local_syms, rel, input_bfd);
951fd09b 11821 if (retval == 0)
411e1bfb
AM
11822 return FALSE;
11823
11824 if (toc_tls)
11825 {
951fd09b 11826 tls_mask = *toc_tls;
411e1bfb
AM
11827 if (r_type == R_PPC64_TOC16_DS
11828 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
11829 {
11830 if (tls_mask != 0
11831 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11832 goto toctprel;
11833 }
411e1bfb 11834 else
951fd09b
AM
11835 {
11836 /* If we found a GD reloc pair, then we might be
11837 doing a GD->IE transition. */
11838 if (retval == 2)
11839 {
11840 tls_gd = TLS_TPRELGD;
11841 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 11842 goto tls_ldgd_opt;
951fd09b
AM
11843 }
11844 else if (retval == 3)
11845 {
11846 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 11847 goto tls_ldgd_opt;
951fd09b
AM
11848 }
11849 }
411e1bfb
AM
11850 }
11851 }
11852 break;
11853
9d6ded02
AM
11854 case R_PPC64_GOT_TPREL16_HI:
11855 case R_PPC64_GOT_TPREL16_HA:
11856 if (tls_mask != 0
11857 && (tls_mask & TLS_TPREL) == 0)
11858 {
11859 rel->r_offset -= d_offset;
11860 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11861 r_type = R_PPC64_NONE;
11862 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11863 }
11864 break;
11865
411e1bfb
AM
11866 case R_PPC64_GOT_TPREL16_DS:
11867 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
11868 if (tls_mask != 0
11869 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11870 {
81407a69 11871 toctprel:
4fe5ca5b 11872 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
11873 insn &= 31 << 21;
11874 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 11875 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 11876 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
11877 if (toc_symndx != 0)
11878 {
11879 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11880 rel->r_addend = toc_addend;
0d4792f7
AM
11881 /* We changed the symbol. Start over in order to
11882 get h, sym, sec etc. right. */
11883 rel--;
11884 continue;
11885 }
11886 else
11887 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11888 }
11889 break;
11890
11891 case R_PPC64_TLS:
951fd09b
AM
11892 if (tls_mask != 0
11893 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11894 {
411e1bfb 11895 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
11896 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11897 if (insn == 0)
411e1bfb 11898 abort ();
411e1bfb 11899 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 11900 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
11901 PPC64_TPREL16_LO which is at low-order half-word. */
11902 rel->r_offset += d_offset;
0d4792f7
AM
11903 r_type = R_PPC64_TPREL16_LO;
11904 if (toc_symndx != 0)
11905 {
11906 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11907 rel->r_addend = toc_addend;
0d4792f7
AM
11908 /* We changed the symbol. Start over in order to
11909 get h, sym, sec etc. right. */
11910 rel--;
11911 continue;
11912 }
11913 else
11914 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11915 }
11916 break;
11917
411e1bfb
AM
11918 case R_PPC64_GOT_TLSGD16_HI:
11919 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
11920 tls_gd = TLS_TPRELGD;
11921 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11922 goto tls_gdld_hi;
11923 break;
11924
411e1bfb
AM
11925 case R_PPC64_GOT_TLSLD16_HI:
11926 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 11927 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 11928 {
951fd09b
AM
11929 tls_gdld_hi:
11930 if ((tls_mask & tls_gd) != 0)
11931 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11932 + R_PPC64_GOT_TPREL16_DS);
11933 else
411e1bfb 11934 {
4fe5ca5b 11935 rel->r_offset -= d_offset;
727ac201 11936 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 11937 r_type = R_PPC64_NONE;
411e1bfb 11938 }
951fd09b 11939 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11940 }
11941 break;
11942
951fd09b
AM
11943 case R_PPC64_GOT_TLSGD16:
11944 case R_PPC64_GOT_TLSGD16_LO:
11945 tls_gd = TLS_TPRELGD;
11946 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 11947 goto tls_ldgd_opt;
951fd09b 11948 break;
411e1bfb 11949
951fd09b
AM
11950 case R_PPC64_GOT_TLSLD16:
11951 case R_PPC64_GOT_TLSLD16_LO:
11952 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
11953 {
3a71aa26 11954 unsigned int insn1, insn2, insn3;
102890f0
AM
11955 bfd_vma offset;
11956
11957 tls_ldgd_opt:
727fc41e
AM
11958 offset = (bfd_vma) -1;
11959 /* If not using the newer R_PPC64_TLSGD/LD to mark
11960 __tls_get_addr calls, we must trust that the call
11961 stays with its arg setup insns, ie. that the next
11962 reloc is the __tls_get_addr call associated with
11963 the current reloc. Edit both insns. */
11964 if (input_section->has_tls_get_addr_call
11965 && rel + 1 < relend
11966 && branch_reloc_hash_match (input_bfd, rel + 1,
11967 htab->tls_get_addr,
11968 htab->tls_get_addr_fd))
11969 offset = rel[1].r_offset;
102890f0 11970 if ((tls_mask & tls_gd) != 0)
411e1bfb 11971 {
102890f0 11972 /* IE */
3a71aa26
AM
11973 insn1 = bfd_get_32 (output_bfd,
11974 contents + rel->r_offset - d_offset);
102890f0
AM
11975 insn1 &= (1 << 26) - (1 << 2);
11976 insn1 |= 58 << 26; /* ld */
11977 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 11978 if (offset != (bfd_vma) -1)
f58d5a2d 11979 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
11980 if ((tls_mask & TLS_EXPLICIT) == 0)
11981 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11982 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 11983 else
102890f0
AM
11984 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
11985 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11986 }
11987 else
11988 {
11989 /* LE */
11990 insn1 = 0x3c6d0000; /* addis 3,13,0 */
11991 insn2 = 0x38630000; /* addi 3,3,0 */
11992 if (tls_gd == 0)
951fd09b 11993 {
102890f0 11994 /* Was an LD reloc. */
1d483afe
AM
11995 if (toc_symndx)
11996 sec = local_sections[toc_symndx];
11997 for (r_symndx = 0;
11998 r_symndx < symtab_hdr->sh_info;
11999 r_symndx++)
12000 if (local_sections[r_symndx] == sec)
12001 break;
12002 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12003 r_symndx = STN_UNDEF;
102890f0 12004 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12005 if (r_symndx != STN_UNDEF)
1d483afe
AM
12006 rel->r_addend -= (local_syms[r_symndx].st_value
12007 + sec->output_offset
12008 + sec->output_section->vma);
951fd09b 12009 }
102890f0 12010 else if (toc_symndx != 0)
3a71aa26
AM
12011 {
12012 r_symndx = toc_symndx;
12013 rel->r_addend = toc_addend;
12014 }
102890f0
AM
12015 r_type = R_PPC64_TPREL16_HA;
12016 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
12017 if (offset != (bfd_vma) -1)
12018 {
12019 rel[1].r_info = ELF64_R_INFO (r_symndx,
12020 R_PPC64_TPREL16_LO);
12021 rel[1].r_offset = offset + d_offset;
12022 rel[1].r_addend = rel->r_addend;
12023 }
102890f0 12024 }
3a71aa26
AM
12025 bfd_put_32 (output_bfd, insn1,
12026 contents + rel->r_offset - d_offset);
727fc41e
AM
12027 if (offset != (bfd_vma) -1)
12028 {
12029 insn3 = bfd_get_32 (output_bfd,
12030 contents + offset + 4);
12031 if (insn3 == NOP
12032 || insn3 == CROR_151515 || insn3 == CROR_313131)
12033 {
12034 rel[1].r_offset += 4;
12035 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12036 insn2 = NOP;
12037 }
12038 bfd_put_32 (output_bfd, insn2, contents + offset);
12039 }
12040 if ((tls_mask & tls_gd) == 0
12041 && (tls_gd == 0 || toc_symndx != 0))
12042 {
12043 /* We changed the symbol. Start over in order
12044 to get h, sym, sec etc. right. */
12045 rel--;
12046 continue;
12047 }
12048 }
12049 break;
12050
12051 case R_PPC64_TLSGD:
12052 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12053 {
12054 unsigned int insn2, insn3;
12055 bfd_vma offset = rel->r_offset;
12056
12057 if ((tls_mask & TLS_TPRELGD) != 0)
12058 {
12059 /* IE */
12060 r_type = R_PPC64_NONE;
12061 insn2 = 0x7c636a14; /* add 3,3,13 */
12062 }
12063 else
12064 {
12065 /* LE */
12066 if (toc_symndx != 0)
12067 {
12068 r_symndx = toc_symndx;
12069 rel->r_addend = toc_addend;
12070 }
12071 r_type = R_PPC64_TPREL16_LO;
12072 rel->r_offset = offset + d_offset;
12073 insn2 = 0x38630000; /* addi 3,3,0 */
12074 }
12075 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12076 /* Zap the reloc on the _tls_get_addr call too. */
12077 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12078 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
12079 insn3 = bfd_get_32 (output_bfd,
12080 contents + offset + 4);
102890f0
AM
12081 if (insn3 == NOP
12082 || insn3 == CROR_151515 || insn3 == CROR_313131)
12083 {
727fc41e 12084 rel->r_offset += 4;
3a71aa26
AM
12085 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12086 insn2 = NOP;
102890f0 12087 }
102890f0 12088 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 12089 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 12090 {
102890f0
AM
12091 rel--;
12092 continue;
411e1bfb 12093 }
411e1bfb 12094 }
411e1bfb
AM
12095 break;
12096
727fc41e
AM
12097 case R_PPC64_TLSLD:
12098 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12099 {
12100 unsigned int insn2, insn3;
12101 bfd_vma offset = rel->r_offset;
12102
12103 if (toc_symndx)
12104 sec = local_sections[toc_symndx];
12105 for (r_symndx = 0;
12106 r_symndx < symtab_hdr->sh_info;
12107 r_symndx++)
12108 if (local_sections[r_symndx] == sec)
12109 break;
12110 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12111 r_symndx = STN_UNDEF;
727fc41e 12112 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12113 if (r_symndx != STN_UNDEF)
727fc41e
AM
12114 rel->r_addend -= (local_syms[r_symndx].st_value
12115 + sec->output_offset
12116 + sec->output_section->vma);
12117
12118 r_type = R_PPC64_TPREL16_LO;
12119 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12120 rel->r_offset = offset + d_offset;
12121 /* Zap the reloc on the _tls_get_addr call too. */
12122 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12123 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
12124 insn2 = 0x38630000; /* addi 3,3,0 */
12125 insn3 = bfd_get_32 (output_bfd,
12126 contents + offset + 4);
12127 if (insn3 == NOP
12128 || insn3 == CROR_151515 || insn3 == CROR_313131)
12129 {
12130 rel->r_offset += 4;
12131 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12132 insn2 = NOP;
12133 }
12134 bfd_put_32 (output_bfd, insn2, contents + offset);
12135 rel--;
12136 continue;
12137 }
12138 break;
12139
411e1bfb 12140 case R_PPC64_DTPMOD64:
951fd09b
AM
12141 if (rel + 1 < relend
12142 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12143 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 12144 {
951fd09b
AM
12145 if ((tls_mask & TLS_GD) == 0)
12146 {
12147 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12148 if ((tls_mask & TLS_TPRELGD) != 0)
12149 r_type = R_PPC64_TPREL64;
12150 else
12151 {
4ce794b7 12152 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
12153 r_type = R_PPC64_NONE;
12154 }
12155 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12156 }
12157 }
12158 else
12159 {
12160 if ((tls_mask & TLS_LD) == 0)
411e1bfb 12161 {
4ce794b7 12162 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 12163 r_type = R_PPC64_NONE;
951fd09b 12164 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 12165 }
411e1bfb
AM
12166 }
12167 break;
12168
12169 case R_PPC64_TPREL64:
951fd09b 12170 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
12171 {
12172 r_type = R_PPC64_NONE;
12173 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12174 }
12175 break;
12176 }
12177
12178 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 12179 insn = 0;
b25116a9
AM
12180 max_br_offset = 1 << 25;
12181 addend = rel->r_addend;
65f38f15 12182 switch (r_type)
5bd4f169
AM
12183 {
12184 default:
65f38f15 12185 break;
5bd4f169 12186
65f38f15
AM
12187 /* Branch taken prediction relocations. */
12188 case R_PPC64_ADDR14_BRTAKEN:
12189 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
12190 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12191 /* Fall thru. */
65f38f15 12192
86c76c7b 12193 /* Branch not taken prediction relocations. */
65f38f15
AM
12194 case R_PPC64_ADDR14_BRNTAKEN:
12195 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
12196 insn |= bfd_get_32 (output_bfd,
12197 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 12198 /* Fall thru. */
86c76c7b 12199
b25116a9
AM
12200 case R_PPC64_REL14:
12201 max_br_offset = 1 << 15;
12202 /* Fall thru. */
5bd4f169 12203
65f38f15 12204 case R_PPC64_REL24:
ad8e1ba5
AM
12205 /* Calls to functions with a different TOC, such as calls to
12206 shared objects, need to alter the TOC pointer. This is
12207 done using a linkage stub. A REL24 branching to these
12208 linkage stubs needs to be followed by a nop, as the nop
12209 will be replaced with an instruction to restore the TOC
12210 base pointer. */
8387904d 12211 fdh = h;
b31867b6
AM
12212 if (h != NULL
12213 && h->oh != NULL
12214 && h->oh->is_func_descriptor)
12215 fdh = ppc_follow_link (h->oh);
6abec6bc
AM
12216 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12217 if (stub_entry != NULL
ad8e1ba5
AM
12218 && (stub_entry->stub_type == ppc_stub_plt_call
12219 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12220 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 12221 {
b25116a9 12222 bfd_boolean can_plt_call = FALSE;
721956f4 12223
eea6121a 12224 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 12225 {
b25116a9
AM
12226 unsigned long nop;
12227 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12228 if (nop == NOP
12229 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 12230 {
a7f2871e
AM
12231 if (h != NULL
12232 && (h == htab->tls_get_addr_fd
12233 || h == htab->tls_get_addr)
12234 && !htab->no_tls_get_addr_opt)
12235 {
12236 /* Special stub used, leave nop alone. */
12237 }
12238 else
12239 bfd_put_32 (input_bfd, LD_R2_40R1,
12240 contents + rel->r_offset + 4);
b25116a9 12241 can_plt_call = TRUE;
41bd81ab 12242 }
5bd4f169 12243 }
721956f4
AM
12244
12245 if (!can_plt_call)
12246 {
ad8e1ba5
AM
12247 if (stub_entry->stub_type == ppc_stub_plt_call)
12248 {
12249 /* If this is a plain branch rather than a branch
4fa1c237
AM
12250 and link, don't require a nop. However, don't
12251 allow tail calls in a shared library as they
12252 will result in r2 being corrupted. */
b25116a9
AM
12253 unsigned long br;
12254 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 12255 if (info->executable && (br & 1) == 0)
b25116a9 12256 can_plt_call = TRUE;
15b552e9
AM
12257 else
12258 stub_entry = NULL;
ad8e1ba5 12259 }
6ab189d5 12260 else if (h != NULL
039b3fef 12261 && strcmp (h->elf.root.root.string,
6ab189d5
AM
12262 ".__libc_start_main") == 0)
12263 {
12264 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 12265 can_plt_call = TRUE;
6ab189d5 12266 }
ad8e1ba5
AM
12267 else
12268 {
12269 if (strcmp (input_section->output_section->name,
12270 ".init") == 0
12271 || strcmp (input_section->output_section->name,
12272 ".fini") == 0)
25f53a85
AM
12273 info->callbacks->einfo
12274 (_("%H: automatic multiple TOCs "
ad8e1ba5 12275 "not supported using your crt files; "
25f53a85
AM
12276 "recompile with -mminimal-toc or upgrade gcc\n"),
12277 input_bfd, input_section, rel->r_offset);
ad8e1ba5 12278 else
25f53a85
AM
12279 info->callbacks->einfo
12280 (_("%H: sibling call optimization to `%s' "
ad8e1ba5
AM
12281 "does not allow automatic multiple TOCs; "
12282 "recompile with -mminimal-toc or "
12283 "-fno-optimize-sibling-calls, "
25f53a85
AM
12284 "or make `%s' extern\n"),
12285 input_bfd, input_section, rel->r_offset,
ad8e1ba5
AM
12286 sym_name,
12287 sym_name);
12288 bfd_set_error (bfd_error_bad_value);
12289 ret = FALSE;
12290 }
721956f4
AM
12291 }
12292
b25116a9
AM
12293 if (can_plt_call
12294 && stub_entry->stub_type == ppc_stub_plt_call)
12295 unresolved_reloc = FALSE;
12296 }
12297
6abec6bc
AM
12298 if ((stub_entry == NULL
12299 || stub_entry->stub_type == ppc_stub_long_branch
12300 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
12301 && get_opd_info (sec) != NULL)
12302 {
12303 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
12304 bfd_vma off = (relocation + addend
12305 - sec->output_section->vma
12306 - sec->output_offset);
8387904d
AM
12307 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12308 if (dest != (bfd_vma) -1)
12309 {
12310 relocation = dest;
12311 addend = 0;
12312 }
12313 }
12314
b25116a9
AM
12315 /* If the branch is out of reach we ought to have a long
12316 branch stub. */
12317 from = (rel->r_offset
12318 + input_section->output_offset
12319 + input_section->output_section->vma);
12320
6abec6bc
AM
12321 if (stub_entry != NULL
12322 && (stub_entry->stub_type == ppc_stub_long_branch
12323 || stub_entry->stub_type == ppc_stub_plt_branch)
12324 && (r_type == R_PPC64_ADDR14_BRTAKEN
12325 || r_type == R_PPC64_ADDR14_BRNTAKEN
12326 || (relocation + addend - from + max_br_offset
12327 < 2 * max_br_offset)))
12328 /* Don't use the stub if this branch is in range. */
12329 stub_entry = NULL;
b25116a9
AM
12330
12331 if (stub_entry != NULL)
12332 {
12333 /* Munge up the value and addend so that we call the stub
12334 rather than the procedure directly. */
12335 relocation = (stub_entry->stub_offset
12336 + stub_entry->stub_sec->output_offset
12337 + stub_entry->stub_sec->output_section->vma);
12338 addend = 0;
12339 }
12340
12341 if (insn != 0)
12342 {
12343 if (is_power4)
721956f4 12344 {
b25116a9
AM
12345 /* Set 'a' bit. This is 0b00010 in BO field for branch
12346 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12347 for branch on CTR insns (BO == 1a00t or 1a01t). */
12348 if ((insn & (0x14 << 21)) == (0x04 << 21))
12349 insn |= 0x02 << 21;
12350 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12351 insn |= 0x08 << 21;
12352 else
12353 break;
12354 }
12355 else
12356 {
12357 /* Invert 'y' bit if not the default. */
4cc603a5 12358 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 12359 insn ^= 0x01 << 21;
721956f4 12360 }
b25116a9
AM
12361
12362 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 12363 }
e86ce104 12364
06da1e8e
AM
12365 /* NOP out calls to undefined weak functions.
12366 We can thus call a weak function without first
12367 checking whether the function is defined. */
b25116a9 12368 else if (h != NULL
039b3fef 12369 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 12370 && h->elf.dynindx == -1
b25116a9
AM
12371 && r_type == R_PPC64_REL24
12372 && relocation == 0
4cc603a5 12373 && addend == 0)
e86ce104 12374 {
06da1e8e
AM
12375 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12376 continue;
e86ce104 12377 }
65f38f15
AM
12378 break;
12379 }
5bd4f169 12380
65f38f15 12381 /* Set `addend'. */
411e1bfb 12382 tls_type = 0;
65f38f15
AM
12383 switch (r_type)
12384 {
12385 default:
25f53a85
AM
12386 info->callbacks->einfo
12387 (_("%B: unknown relocation type %d for symbol %s\n"),
d003868e 12388 input_bfd, (int) r_type, sym_name);
5bd4f169 12389
65f38f15 12390 bfd_set_error (bfd_error_bad_value);
b34976b6 12391 ret = FALSE;
65f38f15 12392 continue;
5bd4f169 12393
65f38f15 12394 case R_PPC64_NONE:
411e1bfb 12395 case R_PPC64_TLS:
727fc41e
AM
12396 case R_PPC64_TLSGD:
12397 case R_PPC64_TLSLD:
04c9666a
AM
12398 case R_PPC64_GNU_VTINHERIT:
12399 case R_PPC64_GNU_VTENTRY:
65f38f15 12400 continue;
5bd4f169
AM
12401
12402 /* GOT16 relocations. Like an ADDR16 using the symbol's
12403 address in the GOT as relocation value instead of the
411e1bfb 12404 symbol's value itself. Also, create a GOT entry for the
5bd4f169 12405 symbol and put the symbol value there. */
411e1bfb
AM
12406 case R_PPC64_GOT_TLSGD16:
12407 case R_PPC64_GOT_TLSGD16_LO:
12408 case R_PPC64_GOT_TLSGD16_HI:
12409 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 12410 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
12411 goto dogot;
12412
12413 case R_PPC64_GOT_TLSLD16:
12414 case R_PPC64_GOT_TLSLD16_LO:
12415 case R_PPC64_GOT_TLSLD16_HI:
12416 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12417 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
12418 goto dogot;
12419
12420 case R_PPC64_GOT_TPREL16_DS:
12421 case R_PPC64_GOT_TPREL16_LO_DS:
12422 case R_PPC64_GOT_TPREL16_HI:
12423 case R_PPC64_GOT_TPREL16_HA:
12424 tls_type = TLS_TLS | TLS_TPREL;
12425 goto dogot;
12426
12427 case R_PPC64_GOT_DTPREL16_DS:
12428 case R_PPC64_GOT_DTPREL16_LO_DS:
12429 case R_PPC64_GOT_DTPREL16_HI:
12430 case R_PPC64_GOT_DTPREL16_HA:
12431 tls_type = TLS_TLS | TLS_DTPREL;
12432 goto dogot;
12433
65f38f15
AM
12434 case R_PPC64_GOT16:
12435 case R_PPC64_GOT16_LO:
12436 case R_PPC64_GOT16_HI:
12437 case R_PPC64_GOT16_HA:
12438 case R_PPC64_GOT16_DS:
12439 case R_PPC64_GOT16_LO_DS:
411e1bfb 12440 dogot:
5bd4f169
AM
12441 {
12442 /* Relocation is to the entry for this symbol in the global
12443 offset table. */
e717da7e 12444 asection *got;
d881513a 12445 bfd_vma *offp;
5bd4f169 12446 bfd_vma off;
d881513a 12447 unsigned long indx = 0;
927be08e 12448 struct got_entry *ent;
65f38f15 12449
d881513a
AM
12450 if (tls_type == (TLS_TLS | TLS_LD)
12451 && (h == NULL
f5385ebf 12452 || !h->elf.def_dynamic))
927be08e 12453 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 12454 else
5bd4f169 12455 {
5bd4f169 12456
d881513a
AM
12457 if (h != NULL)
12458 {
12459 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
12460 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12461 &h->elf)
d881513a 12462 || (info->shared
726d3ab0 12463 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
12464 /* This is actually a static link, or it is a
12465 -Bsymbolic link and the symbol is defined
12466 locally, or the symbol was forced to be local
12467 because of a version file. */
12468 ;
12469 else
12470 {
039b3fef 12471 indx = h->elf.dynindx;
d881513a
AM
12472 unresolved_reloc = FALSE;
12473 }
039b3fef 12474 ent = h->elf.got.glist;
d881513a 12475 }
411e1bfb 12476 else
5bd4f169 12477 {
d881513a
AM
12478 if (local_got_ents == NULL)
12479 abort ();
12480 ent = local_got_ents[r_symndx];
5bd4f169 12481 }
d881513a
AM
12482
12483 for (; ent != NULL; ent = ent->next)
4cc603a5 12484 if (ent->addend == orig_addend
e717da7e 12485 && ent->owner == input_bfd
d881513a
AM
12486 && ent->tls_type == tls_type)
12487 break;
5bd4f169 12488 }
411e1bfb 12489
927be08e
AM
12490 if (ent == NULL)
12491 abort ();
12492 if (ent->is_indirect)
12493 ent = ent->got.ent;
12494 offp = &ent->got.offset;
12495 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
12496 if (got == NULL)
12497 abort ();
12498
411e1bfb
AM
12499 /* The offset must always be a multiple of 8. We use the
12500 least significant bit to record whether we have already
12501 processed this entry. */
d881513a 12502 off = *offp;
411e1bfb
AM
12503 if ((off & 1) != 0)
12504 off &= ~1;
5bd4f169
AM
12505 else
12506 {
411e1bfb
AM
12507 /* Generate relocs for the dynamic linker, except in
12508 the case of TLSLD where we'll use one entry per
12509 module. */
25f23106
AM
12510 asection *relgot;
12511 bfd_boolean ifunc;
e717da7e 12512
d881513a 12513 *offp = off | 1;
25f23106
AM
12514 relgot = NULL;
12515 ifunc = (h != NULL
12516 ? h->elf.type == STT_GNU_IFUNC
12517 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
4e795f50 12518 if ((info->shared || indx != 0)
927be08e
AM
12519 && (h == NULL
12520 || (tls_type == (TLS_TLS | TLS_LD)
12521 && !h->elf.def_dynamic)
039b3fef
AM
12522 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12523 || h->elf.root.type != bfd_link_hash_undefweak))
927be08e 12524 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106
AM
12525 else if (ifunc)
12526 relgot = htab->reliplt;
12527 if (relgot != NULL)
5bd4f169 12528 {
e717da7e
AM
12529 outrel.r_offset = (got->output_section->vma
12530 + got->output_offset
411e1bfb 12531 + off);
4cc603a5 12532 outrel.r_addend = addend;
d881513a 12533 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 12534 {
411e1bfb 12535 outrel.r_addend = 0;
e515b051 12536 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
12537 if (tls_type == (TLS_TLS | TLS_GD))
12538 {
e717da7e
AM
12539 loc = relgot->contents;
12540 loc += (relgot->reloc_count++
d881513a
AM
12541 * sizeof (Elf64_External_Rela));
12542 bfd_elf64_swap_reloca_out (output_bfd,
12543 &outrel, loc);
e515b051 12544 outrel.r_offset += 8;
4cc603a5 12545 outrel.r_addend = addend;
d881513a
AM
12546 outrel.r_info
12547 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 12548 }
411e1bfb 12549 }
951fd09b 12550 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 12551 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 12552 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12553 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
12554 else if (indx != 0)
12555 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12556 else
81407a69 12557 {
25f23106
AM
12558 if (ifunc)
12559 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12560 else
12561 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
12562
12563 /* Write the .got section contents for the sake
12564 of prelink. */
e717da7e 12565 loc = got->contents + off;
23fbd6fa
JJ
12566 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12567 loc);
81407a69 12568 }
81407a69
AM
12569
12570 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
12571 {
12572 outrel.r_addend += relocation;
12573 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 12574 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 12575 }
e717da7e
AM
12576 loc = relgot->contents;
12577 loc += (relgot->reloc_count++
411e1bfb
AM
12578 * sizeof (Elf64_External_Rela));
12579 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12580 }
12581
ad8e1ba5 12582 /* Init the .got section contents here if we're not
81407a69 12583 emitting a reloc. */
d881513a 12584 else
411e1bfb 12585 {
4cc603a5 12586 relocation += addend;
7b609f53
AM
12587 if (tls_type == (TLS_TLS | TLS_LD))
12588 relocation = 1;
12589 else if (tls_type != 0)
411e1bfb 12590 {
e1918d23 12591 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 12592 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12593 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 12594
7b609f53
AM
12595 if (tls_type == (TLS_TLS | TLS_GD))
12596 {
12597 bfd_put_64 (output_bfd, relocation,
e717da7e 12598 got->contents + off + 8);
7b609f53
AM
12599 relocation = 1;
12600 }
411e1bfb 12601 }
7b609f53 12602
411e1bfb 12603 bfd_put_64 (output_bfd, relocation,
e717da7e 12604 got->contents + off);
5bd4f169
AM
12605 }
12606 }
12607
65f38f15
AM
12608 if (off >= (bfd_vma) -2)
12609 abort ();
12610
bf102f86
AM
12611 relocation = got->output_section->vma + got->output_offset + off;
12612 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 12613 }
65f38f15
AM
12614 break;
12615
12616 case R_PPC64_PLT16_HA:
12617 case R_PPC64_PLT16_HI:
12618 case R_PPC64_PLT16_LO:
12619 case R_PPC64_PLT32:
12620 case R_PPC64_PLT64:
12621 /* Relocation is to the entry for this symbol in the
12622 procedure linkage table. */
12623
12624 /* Resolve a PLT reloc against a local symbol directly,
12625 without using the procedure linkage table. */
12626 if (h == NULL)
12627 break;
12628
411e1bfb
AM
12629 /* It's possible that we didn't make a PLT entry for this
12630 symbol. This happens when statically linking PIC code,
12631 or when using -Bsymbolic. Go find a match if there is a
12632 PLT entry. */
4ce794b7 12633 if (htab->plt != NULL)
65f38f15 12634 {
411e1bfb 12635 struct plt_entry *ent;
039b3fef 12636 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 12637 if (ent->addend == orig_addend
411e1bfb
AM
12638 && ent->plt.offset != (bfd_vma) -1)
12639 {
4ce794b7
AM
12640 relocation = (htab->plt->output_section->vma
12641 + htab->plt->output_offset
411e1bfb
AM
12642 + ent->plt.offset);
12643 unresolved_reloc = FALSE;
12644 }
65f38f15 12645 }
65f38f15 12646 break;
5bd4f169 12647
0b13192e
AM
12648 case R_PPC64_TOC:
12649 /* Relocation value is TOC base. */
12650 relocation = TOCstart;
cf35638d 12651 if (r_symndx == STN_UNDEF)
0b13192e 12652 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
12653 else if (unresolved_reloc)
12654 ;
12655 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
12656 relocation += htab->stub_group[sec->id].toc_off;
12657 else
12658 unresolved_reloc = TRUE;
ab96bf03 12659 goto dodyn;
0b13192e 12660
5bd4f169
AM
12661 /* TOC16 relocs. We want the offset relative to the TOC base,
12662 which is the address of the start of the TOC plus 0x8000.
12663 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12664 in this order. */
65f38f15
AM
12665 case R_PPC64_TOC16:
12666 case R_PPC64_TOC16_LO:
12667 case R_PPC64_TOC16_HI:
12668 case R_PPC64_TOC16_DS:
12669 case R_PPC64_TOC16_LO_DS:
12670 case R_PPC64_TOC16_HA:
ad8e1ba5 12671 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
12672 break;
12673
12674 /* Relocate against the beginning of the section. */
65f38f15
AM
12675 case R_PPC64_SECTOFF:
12676 case R_PPC64_SECTOFF_LO:
12677 case R_PPC64_SECTOFF_HI:
12678 case R_PPC64_SECTOFF_DS:
12679 case R_PPC64_SECTOFF_LO_DS:
12680 case R_PPC64_SECTOFF_HA:
4ce794b7 12681 if (sec != NULL)
65f38f15 12682 addend -= sec->output_section->vma;
5bd4f169
AM
12683 break;
12684
25f23106
AM
12685 case R_PPC64_REL16:
12686 case R_PPC64_REL16_LO:
12687 case R_PPC64_REL16_HI:
12688 case R_PPC64_REL16_HA:
12689 break;
12690
721956f4
AM
12691 case R_PPC64_REL14:
12692 case R_PPC64_REL14_BRNTAKEN:
12693 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
12694 case R_PPC64_REL24:
12695 break;
12696
411e1bfb
AM
12697 case R_PPC64_TPREL16:
12698 case R_PPC64_TPREL16_LO:
12699 case R_PPC64_TPREL16_HI:
12700 case R_PPC64_TPREL16_HA:
12701 case R_PPC64_TPREL16_DS:
12702 case R_PPC64_TPREL16_LO_DS:
12703 case R_PPC64_TPREL16_HIGHER:
12704 case R_PPC64_TPREL16_HIGHERA:
12705 case R_PPC64_TPREL16_HIGHEST:
12706 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
12707 if (h != NULL
12708 && h->elf.root.type == bfd_link_hash_undefweak
12709 && h->elf.dynindx == -1)
12710 {
12711 /* Make this relocation against an undefined weak symbol
12712 resolve to zero. This is really just a tweak, since
12713 code using weak externs ought to check that they are
12714 defined before using them. */
12715 bfd_byte *p = contents + rel->r_offset - d_offset;
12716
12717 insn = bfd_get_32 (output_bfd, p);
12718 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12719 if (insn != 0)
12720 bfd_put_32 (output_bfd, insn, p);
12721 break;
12722 }
e1918d23 12723 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12724 if (info->shared)
12725 /* The TPREL16 relocs shouldn't really be used in shared
12726 libs as they will result in DT_TEXTREL being set, but
12727 support them anyway. */
12728 goto dodyn;
12729 break;
12730
12731 case R_PPC64_DTPREL16:
12732 case R_PPC64_DTPREL16_LO:
12733 case R_PPC64_DTPREL16_HI:
12734 case R_PPC64_DTPREL16_HA:
12735 case R_PPC64_DTPREL16_DS:
12736 case R_PPC64_DTPREL16_LO_DS:
12737 case R_PPC64_DTPREL16_HIGHER:
12738 case R_PPC64_DTPREL16_HIGHERA:
12739 case R_PPC64_DTPREL16_HIGHEST:
12740 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 12741 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12742 break;
12743
e515b051
AM
12744 case R_PPC64_DTPMOD64:
12745 relocation = 1;
12746 addend = 0;
12747 goto dodyn;
12748
411e1bfb 12749 case R_PPC64_TPREL64:
e1918d23 12750 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12751 goto dodyn;
12752
12753 case R_PPC64_DTPREL64:
e1918d23 12754 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12755 /* Fall thru */
12756
65f38f15
AM
12757 /* Relocations that may need to be propagated if this is a
12758 dynamic object. */
04c9666a 12759 case R_PPC64_REL30:
65f38f15
AM
12760 case R_PPC64_REL32:
12761 case R_PPC64_REL64:
12762 case R_PPC64_ADDR14:
12763 case R_PPC64_ADDR14_BRNTAKEN:
12764 case R_PPC64_ADDR14_BRTAKEN:
12765 case R_PPC64_ADDR16:
12766 case R_PPC64_ADDR16_DS:
12767 case R_PPC64_ADDR16_HA:
12768 case R_PPC64_ADDR16_HI:
12769 case R_PPC64_ADDR16_HIGHER:
12770 case R_PPC64_ADDR16_HIGHERA:
12771 case R_PPC64_ADDR16_HIGHEST:
12772 case R_PPC64_ADDR16_HIGHESTA:
12773 case R_PPC64_ADDR16_LO:
12774 case R_PPC64_ADDR16_LO_DS:
12775 case R_PPC64_ADDR24:
65f38f15
AM
12776 case R_PPC64_ADDR32:
12777 case R_PPC64_ADDR64:
12778 case R_PPC64_UADDR16:
12779 case R_PPC64_UADDR32:
12780 case R_PPC64_UADDR64:
411e1bfb 12781 dodyn:
5d1634d7 12782 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
12783 break;
12784
41bd81ab
AM
12785 if (NO_OPD_RELOCS && is_opd)
12786 break;
12787
65f38f15 12788 if ((info->shared
4e795f50 12789 && (h == NULL
039b3fef
AM
12790 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12791 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 12792 && (must_be_dyn_reloc (info, r_type)
039b3fef 12793 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
12794 || (ELIMINATE_COPY_RELOCS
12795 && !info->shared
65f38f15 12796 && h != NULL
039b3fef 12797 && h->elf.dynindx != -1
f5385ebf 12798 && !h->elf.non_got_ref
25f23106
AM
12799 && !h->elf.def_regular)
12800 || (!info->shared
12801 && (h != NULL
12802 ? h->elf.type == STT_GNU_IFUNC
12803 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 12804 {
b34976b6 12805 bfd_boolean skip, relocate;
65f38f15 12806 asection *sreloc;
1cf1f670 12807 bfd_vma out_off;
65f38f15
AM
12808
12809 /* When generating a dynamic object, these relocations
12810 are copied into the output file to be resolved at run
12811 time. */
12812
b34976b6
AM
12813 skip = FALSE;
12814 relocate = FALSE;
65f38f15 12815
1cf1f670
AM
12816 out_off = _bfd_elf_section_offset (output_bfd, info,
12817 input_section, rel->r_offset);
12818 if (out_off == (bfd_vma) -1)
b34976b6 12819 skip = TRUE;
1cf1f670 12820 else if (out_off == (bfd_vma) -2)
b34976b6 12821 skip = TRUE, relocate = TRUE;
1cf1f670
AM
12822 out_off += (input_section->output_section->vma
12823 + input_section->output_offset);
12824 outrel.r_offset = out_off;
411e1bfb 12825 outrel.r_addend = rel->r_addend;
65f38f15 12826
1cf1f670
AM
12827 /* Optimize unaligned reloc use. */
12828 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12829 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12830 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12831 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12832 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12833 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12834 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12835 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12836 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12837
65f38f15 12838 if (skip)
0bb2d96a 12839 memset (&outrel, 0, sizeof outrel);
726d3ab0 12840 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
12841 && !is_opd
12842 && r_type != R_PPC64_TOC)
039b3fef 12843 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
12844 else
12845 {
41bd81ab
AM
12846 /* This symbol is local, or marked to become local,
12847 or this is an opd section reloc which must point
12848 at a local function. */
65f38f15 12849 outrel.r_addend += relocation;
e86ce104 12850 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 12851 {
3fad3c7c 12852 if (is_opd && h != NULL)
afbe61cf
AM
12853 {
12854 /* Lie about opd entries. This case occurs
12855 when building shared libraries and we
12856 reference a function in another shared
3fad3c7c
AM
12857 lib. The same thing happens for a weak
12858 definition in an application that's
12859 overridden by a strong definition in a
12860 shared lib. (I believe this is a generic
12861 bug in binutils handling of weak syms.)
12862 In these cases we won't use the opd
1e2f5b6e 12863 entry in this lib. */
b34976b6 12864 unresolved_reloc = FALSE;
afbe61cf 12865 }
25f23106
AM
12866 if (!is_opd
12867 && r_type == R_PPC64_ADDR64
12868 && (h != NULL
12869 ? h->elf.type == STT_GNU_IFUNC
12870 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12871 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12872 else
12873 {
12874 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 12875
25f23106
AM
12876 /* We need to relocate .opd contents for ld.so.
12877 Prelink also wants simple and consistent rules
12878 for relocs. This make all RELATIVE relocs have
12879 *r_offset equal to r_addend. */
12880 relocate = TRUE;
12881 }
65f38f15
AM
12882 }
12883 else
12884 {
12885 long indx = 0;
12886
25f23106
AM
12887 if (h != NULL
12888 ? h->elf.type == STT_GNU_IFUNC
12889 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12890 {
25f53a85
AM
12891 info->callbacks->einfo
12892 (_("%H: relocation %s for indirect "
12893 "function %s unsupported\n"),
12894 input_bfd, input_section, rel->r_offset,
25f23106
AM
12895 ppc64_elf_howto_table[r_type]->name,
12896 sym_name);
12897 ret = FALSE;
12898 }
cf35638d 12899 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
12900 ;
12901 else if (sec == NULL || sec->owner == NULL)
12902 {
12903 bfd_set_error (bfd_error_bad_value);
b34976b6 12904 return FALSE;
65f38f15
AM
12905 }
12906 else
12907 {
12908 asection *osec;
12909
12910 osec = sec->output_section;
12911 indx = elf_section_data (osec)->dynindx;
12912
74541ad4
AM
12913 if (indx == 0)
12914 {
12915 if ((osec->flags & SEC_READONLY) == 0
12916 && htab->elf.data_index_section != NULL)
12917 osec = htab->elf.data_index_section;
12918 else
12919 osec = htab->elf.text_index_section;
12920 indx = elf_section_data (osec)->dynindx;
12921 }
12922 BFD_ASSERT (indx != 0);
12923
65f38f15
AM
12924 /* We are turning this relocation into one
12925 against a section symbol, so subtract out
12926 the output section's address but not the
12927 offset of the input section in the output
12928 section. */
12929 outrel.r_addend -= osec->vma;
12930 }
12931
12932 outrel.r_info = ELF64_R_INFO (indx, r_type);
12933 }
12934 }
12935
12936 sreloc = elf_section_data (input_section)->sreloc;
25f23106
AM
12937 if (!htab->elf.dynamic_sections_created)
12938 sreloc = htab->reliplt;
65f38f15
AM
12939 if (sreloc == NULL)
12940 abort ();
12941
dfbb6ac9
AM
12942 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
12943 >= sreloc->size)
12944 abort ();
947216bf
AM
12945 loc = sreloc->contents;
12946 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
12947 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12948
12949 /* If this reloc is against an external symbol, it will
12950 be computed at runtime, so there's no need to do
81407a69
AM
12951 anything now. However, for the sake of prelink ensure
12952 that the section contents are a known value. */
65f38f15 12953 if (! relocate)
81407a69
AM
12954 {
12955 unresolved_reloc = FALSE;
12956 /* The value chosen here is quite arbitrary as ld.so
12957 ignores section contents except for the special
12958 case of .opd where the contents might be accessed
12959 before relocation. Choose zero, as that won't
12960 cause reloc overflow. */
12961 relocation = 0;
12962 addend = 0;
12963 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
12964 to improve backward compatibility with older
12965 versions of ld. */
12966 if (r_type == R_PPC64_ADDR64)
12967 addend = outrel.r_addend;
12968 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 12969 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
12970 addend = (input_section->output_section->vma
12971 + input_section->output_offset
12972 + rel->r_offset);
81407a69 12973 }
65f38f15 12974 }
5bd4f169
AM
12975 break;
12976
65f38f15
AM
12977 case R_PPC64_COPY:
12978 case R_PPC64_GLOB_DAT:
12979 case R_PPC64_JMP_SLOT:
25f23106 12980 case R_PPC64_JMP_IREL:
65f38f15
AM
12981 case R_PPC64_RELATIVE:
12982 /* We shouldn't ever see these dynamic relocs in relocatable
12983 files. */
ae9a127f 12984 /* Fall through. */
65f38f15
AM
12985
12986 case R_PPC64_PLTGOT16:
12987 case R_PPC64_PLTGOT16_DS:
12988 case R_PPC64_PLTGOT16_HA:
12989 case R_PPC64_PLTGOT16_HI:
12990 case R_PPC64_PLTGOT16_LO:
12991 case R_PPC64_PLTGOT16_LO_DS:
12992 case R_PPC64_PLTREL32:
12993 case R_PPC64_PLTREL64:
12994 /* These ones haven't been implemented yet. */
12995
25f53a85
AM
12996 info->callbacks->einfo
12997 (_("%B: relocation %s is not supported for symbol %s\n"),
d003868e 12998 input_bfd,
4ce794b7 12999 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
13000
13001 bfd_set_error (bfd_error_invalid_operation);
b34976b6 13002 ret = FALSE;
5bd4f169 13003 continue;
65f38f15 13004 }
5bd4f169 13005
67f0cbdb
AM
13006 /* Multi-instruction sequences that access the TOC can be
13007 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13008 to nop; addi rb,r2,x; */
13009 switch (r_type)
13010 {
13011 default:
13012 break;
13013
13014 case R_PPC64_GOT_TLSLD16_HI:
13015 case R_PPC64_GOT_TLSGD16_HI:
13016 case R_PPC64_GOT_TPREL16_HI:
13017 case R_PPC64_GOT_DTPREL16_HI:
13018 case R_PPC64_GOT16_HI:
13019 case R_PPC64_TOC16_HI:
13020 /* These relocs would only be useful if building up an
13021 offset to later add to r2, perhaps in an indexed
13022 addressing mode instruction. Don't try to optimize.
13023 Unfortunately, the possibility of someone building up an
13024 offset like this or even with the HA relocs, means that
13025 we need to check the high insn when optimizing the low
13026 insn. */
13027 break;
13028
13029 case R_PPC64_GOT_TLSLD16_HA:
13030 case R_PPC64_GOT_TLSGD16_HA:
13031 case R_PPC64_GOT_TPREL16_HA:
13032 case R_PPC64_GOT_DTPREL16_HA:
13033 case R_PPC64_GOT16_HA:
13034 case R_PPC64_TOC16_HA:
d983b7a0 13035 /* nop is done later. */
67f0cbdb
AM
13036 break;
13037
13038 case R_PPC64_GOT_TLSLD16_LO:
13039 case R_PPC64_GOT_TLSGD16_LO:
13040 case R_PPC64_GOT_TPREL16_LO_DS:
13041 case R_PPC64_GOT_DTPREL16_LO_DS:
13042 case R_PPC64_GOT16_LO:
13043 case R_PPC64_GOT16_LO_DS:
13044 case R_PPC64_TOC16_LO:
13045 case R_PPC64_TOC16_LO_DS:
13046 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
13047 {
13048 bfd_byte *p = contents + (rel->r_offset & ~3);
13049 insn = bfd_get_32 (input_bfd, p);
13050 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13051 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13052 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13053 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13054 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13055 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13056 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13057 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13058 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13059 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13060 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13061 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13062 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13063 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13064 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13065 && (insn & 3) != 1)
13066 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13067 && ((insn & 3) == 0 || (insn & 3) == 3)))
13068 {
13069 unsigned int reg = (insn >> 16) & 0x1f;
d983b7a0
AM
13070 const Elf_Internal_Rela *ha;
13071 bfd_boolean match_addend;
13072
13073 match_addend = (sym != NULL
13074 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13075 ha = ha_reloc_match (relocs, rel, &reg, match_addend,
13076 input_bfd, contents);
13077 if (ha != NULL)
67f0cbdb
AM
13078 {
13079 insn &= ~(0x1f << 16);
d983b7a0 13080 insn |= reg << 16;
67f0cbdb 13081 bfd_put_32 (input_bfd, insn, p);
d983b7a0
AM
13082 if (ha_opt == NULL)
13083 {
13084 ha_opt = bfd_zmalloc (input_section->reloc_count);
13085 if (ha_opt == NULL)
13086 return FALSE;
13087 }
13088 ha_opt[ha - relocs] = 1;
67f0cbdb 13089 }
d983b7a0
AM
13090 else
13091 /* If we don't find a matching high part insn,
13092 something is fishy. Refuse to nop any high
13093 part insn in this section. */
13094 no_ha_opt = TRUE;
67f0cbdb
AM
13095 }
13096 }
13097 break;
13098 }
13099
65f38f15
AM
13100 /* Do any further special processing. */
13101 switch (r_type)
13102 {
13103 default:
13104 break;
13105
13106 case R_PPC64_ADDR16_HA:
25f23106 13107 case R_PPC64_REL16_HA:
65f38f15
AM
13108 case R_PPC64_ADDR16_HIGHERA:
13109 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
13110 case R_PPC64_TOC16_HA:
13111 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
13112 case R_PPC64_TPREL16_HA:
13113 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
13114 case R_PPC64_TPREL16_HIGHER:
13115 case R_PPC64_TPREL16_HIGHERA:
13116 case R_PPC64_TPREL16_HIGHEST:
13117 case R_PPC64_TPREL16_HIGHESTA:
13118 case R_PPC64_DTPREL16_HIGHER:
13119 case R_PPC64_DTPREL16_HIGHERA:
13120 case R_PPC64_DTPREL16_HIGHEST:
13121 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
13122 /* It's just possible that this symbol is a weak symbol
13123 that's not actually defined anywhere. In that case,
13124 'sec' would be NULL, and we should leave the symbol
13125 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
13126 if (sec == NULL)
13127 break;
13128 /* Fall thru */
13129
13130 case R_PPC64_GOT16_HA:
13131 case R_PPC64_PLTGOT16_HA:
13132 case R_PPC64_PLT16_HA:
13133 case R_PPC64_GOT_TLSGD16_HA:
13134 case R_PPC64_GOT_TLSLD16_HA:
13135 case R_PPC64_GOT_TPREL16_HA:
13136 case R_PPC64_GOT_DTPREL16_HA:
13137 /* Add 0x10000 if sign bit in 0:15 is set.
13138 Bits 0:15 are not used. */
13139 addend += 0x8000;
65f38f15
AM
13140 break;
13141
13142 case R_PPC64_ADDR16_DS:
13143 case R_PPC64_ADDR16_LO_DS:
13144 case R_PPC64_GOT16_DS:
13145 case R_PPC64_GOT16_LO_DS:
13146 case R_PPC64_PLT16_LO_DS:
13147 case R_PPC64_SECTOFF_DS:
13148 case R_PPC64_SECTOFF_LO_DS:
13149 case R_PPC64_TOC16_DS:
13150 case R_PPC64_TOC16_LO_DS:
13151 case R_PPC64_PLTGOT16_DS:
13152 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
13153 case R_PPC64_GOT_TPREL16_DS:
13154 case R_PPC64_GOT_TPREL16_LO_DS:
13155 case R_PPC64_GOT_DTPREL16_DS:
13156 case R_PPC64_GOT_DTPREL16_LO_DS:
13157 case R_PPC64_TPREL16_DS:
13158 case R_PPC64_TPREL16_LO_DS:
13159 case R_PPC64_DTPREL16_DS:
13160 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
13161 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13162 mask = 3;
13163 /* If this reloc is against an lq insn, then the value must be
13164 a multiple of 16. This is somewhat of a hack, but the
13165 "correct" way to do this by defining _DQ forms of all the
13166 _DS relocs bloats all reloc switches in this file. It
13167 doesn't seem to make much sense to use any of these relocs
13168 in data, so testing the insn should be safe. */
494dac0c 13169 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
13170 mask = 15;
13171 if (((relocation + addend) & mask) != 0)
65f38f15 13172 {
25f53a85
AM
13173 info->callbacks->einfo
13174 (_("%H: error: %s not a multiple of %u\n"),
13175 input_bfd, input_section, rel->r_offset,
4ce794b7 13176 ppc64_elf_howto_table[r_type]->name,
adadcc0c 13177 mask + 1);
65f38f15 13178 bfd_set_error (bfd_error_bad_value);
b34976b6 13179 ret = FALSE;
65f38f15
AM
13180 continue;
13181 }
13182 break;
5bd4f169
AM
13183 }
13184
239e1f3a
AM
13185 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13186 because such sections are not SEC_ALLOC and thus ld.so will
13187 not process them. */
65f38f15 13188 if (unresolved_reloc
239e1f3a 13189 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 13190 && h->elf.def_dynamic))
9c07fe7c 13191 {
25f53a85
AM
13192 info->callbacks->einfo
13193 (_("%H: unresolvable %s relocation against symbol `%s'\n"),
13194 input_bfd, input_section, rel->r_offset,
7b609f53 13195 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 13196 h->elf.root.root.string);
b34976b6 13197 ret = FALSE;
9c07fe7c 13198 }
5bd4f169 13199
65f38f15 13200 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
13201 input_bfd,
13202 input_section,
13203 contents,
411e1bfb 13204 rel->r_offset,
5bd4f169
AM
13205 relocation,
13206 addend);
13207
ef60b7ff 13208 if (r != bfd_reloc_ok)
5bd4f169 13209 {
cd27b276
AM
13210 if (sym_name == NULL)
13211 sym_name = "(null)";
13212 if (r == bfd_reloc_overflow)
5bd4f169 13213 {
cd27b276
AM
13214 if (warned)
13215 continue;
13216 if (h != NULL
039b3fef 13217 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 13218 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
13219 {
13220 /* Assume this is a call protected by other code that
13221 detects the symbol is undefined. If this is the case,
13222 we can safely ignore the overflow. If not, the
13223 program is hosed anyway, and a little warning isn't
13224 going to help. */
13225
13226 continue;
13227 }
13228
ef60b7ff 13229 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
13230 (info, (h ? &h->elf.root : NULL), sym_name,
13231 ppc64_elf_howto_table[r_type]->name,
4cc603a5 13232 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 13233 return FALSE;
ef60b7ff
AM
13234 }
13235 else
13236 {
25f53a85
AM
13237 info->callbacks->einfo
13238 (_("%H: %s reloc against `%s': error %d\n"),
13239 input_bfd, input_section, rel->r_offset,
4ce794b7 13240 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
13241 sym_name,
13242 (int) r);
b34976b6 13243 ret = FALSE;
ef60b7ff 13244 }
5bd4f169 13245 }
5bd4f169
AM
13246 }
13247
d983b7a0
AM
13248 if (ha_opt != NULL)
13249 {
13250 if (!no_ha_opt)
13251 {
13252 unsigned char *opt = ha_opt;
13253 rel = relocs;
13254 relend = relocs + input_section->reloc_count;
13255 for (; rel < relend; opt++, rel++)
13256 if (*opt != 0)
13257 {
13258 bfd_byte *p = contents + (rel->r_offset & ~3);
13259 bfd_put_32 (input_bfd, NOP, p);
13260 }
13261 }
13262 free (ha_opt);
13263 }
13264
645ea6a9
AM
13265 /* If we're emitting relocations, then shortly after this function
13266 returns, reloc offsets and addends for this section will be
13267 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
13268 file rather than the input. Save a copy of the relocs for
13269 opd_entry_value. */
4cc603a5 13270 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
13271 {
13272 bfd_size_type amt;
13273 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13274 rel = bfd_alloc (input_bfd, amt);
13275 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13276 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13277 if (rel == NULL)
13278 return FALSE;
13279 memcpy (rel, relocs, amt);
13280 }
5bd4f169
AM
13281 return ret;
13282}
13283
754021d0
AM
13284/* Adjust the value of any local symbols in opd sections. */
13285
6e0b88f1 13286static int
754021d0
AM
13287ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13288 const char *name ATTRIBUTE_UNUSED,
13289 Elf_Internal_Sym *elfsym,
13290 asection *input_sec,
13291 struct elf_link_hash_entry *h)
13292{
74f0fb50
AM
13293 struct _opd_sec_data *opd;
13294 long adjust;
754021d0
AM
13295 bfd_vma value;
13296
4025353c 13297 if (h != NULL)
6e0b88f1 13298 return 1;
4025353c 13299
74f0fb50
AM
13300 opd = get_opd_info (input_sec);
13301 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 13302 return 1;
754021d0
AM
13303
13304 value = elfsym->st_value - input_sec->output_offset;
13305 if (!info->relocatable)
13306 value -= input_sec->output_section->vma;
13307
74f0fb50 13308 adjust = opd->adjust[value / 8];
4025353c 13309 if (adjust == -1)
6e0b88f1
AM
13310 return 2;
13311
13312 elfsym->st_value += adjust;
13313 return 1;
754021d0
AM
13314}
13315
5bd4f169
AM
13316/* Finish up dynamic symbol handling. We set the contents of various
13317 dynamic sections here. */
13318
b34976b6 13319static bfd_boolean
4ce794b7
AM
13320ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13321 struct bfd_link_info *info,
13322 struct elf_link_hash_entry *h,
13323 Elf_Internal_Sym *sym)
5bd4f169 13324{
65f38f15 13325 struct ppc_link_hash_table *htab;
8387904d
AM
13326 struct plt_entry *ent;
13327 Elf_Internal_Rela rela;
13328 bfd_byte *loc;
5bd4f169 13329
65f38f15 13330 htab = ppc_hash_table (info);
4dfe6ac6
NC
13331 if (htab == NULL)
13332 return FALSE;
5bd4f169 13333
8387904d
AM
13334 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13335 if (ent->plt.offset != (bfd_vma) -1)
13336 {
13337 /* This symbol has an entry in the procedure linkage
13338 table. Set it up. */
e054468f
AM
13339 if (!htab->elf.dynamic_sections_created
13340 || h->dynindx == -1)
13341 {
13342 BFD_ASSERT (h->type == STT_GNU_IFUNC
13343 && h->def_regular
13344 && (h->root.type == bfd_link_hash_defined
13345 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
13346 rela.r_offset = (htab->iplt->output_section->vma
13347 + htab->iplt->output_offset
13348 + ent->plt.offset);
13349 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
13350 rela.r_addend = (h->root.u.def.value
13351 + h->root.u.def.section->output_offset
13352 + h->root.u.def.section->output_section->vma
13353 + ent->addend);
25f23106
AM
13354 loc = (htab->reliplt->contents
13355 + (htab->reliplt->reloc_count++
13356 * sizeof (Elf64_External_Rela)));
e054468f
AM
13357 }
13358 else
13359 {
25f23106
AM
13360 rela.r_offset = (htab->plt->output_section->vma
13361 + htab->plt->output_offset
13362 + ent->plt.offset);
e054468f
AM
13363 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13364 rela.r_addend = ent->addend;
25f23106
AM
13365 loc = (htab->relplt->contents
13366 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13367 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 13368 }
8387904d
AM
13369 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13370 }
5bd4f169 13371
f5385ebf 13372 if (h->needs_copy)
5bd4f169 13373 {
65f38f15 13374 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 13375
65f38f15
AM
13376 if (h->dynindx == -1
13377 || (h->root.type != bfd_link_hash_defined
13378 && h->root.type != bfd_link_hash_defweak)
4ce794b7 13379 || htab->relbss == NULL)
65f38f15 13380 abort ();
5bd4f169
AM
13381
13382 rela.r_offset = (h->root.u.def.value
13383 + h->root.u.def.section->output_section->vma
13384 + h->root.u.def.section->output_offset);
13385 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13386 rela.r_addend = 0;
4ce794b7
AM
13387 loc = htab->relbss->contents;
13388 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 13389 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
13390 }
13391
5bd4f169
AM
13392 /* Mark some specially defined symbols as absolute. */
13393 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13394 sym->st_shndx = SHN_ABS;
13395
b34976b6 13396 return TRUE;
5bd4f169
AM
13397}
13398
65f38f15
AM
13399/* Used to decide how to sort relocs in an optimal manner for the
13400 dynamic linker, before writing them out. */
13401
13402static enum elf_reloc_type_class
4ce794b7 13403ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 13404{
04c9666a 13405 enum elf_ppc64_reloc_type r_type;
a33d1f77 13406
4ce794b7 13407 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 13408 switch (r_type)
65f38f15
AM
13409 {
13410 case R_PPC64_RELATIVE:
13411 return reloc_class_relative;
13412 case R_PPC64_JMP_SLOT:
13413 return reloc_class_plt;
13414 case R_PPC64_COPY:
13415 return reloc_class_copy;
13416 default:
13417 return reloc_class_normal;
13418 }
13419}
13420
5bd4f169
AM
13421/* Finish up the dynamic sections. */
13422
b34976b6 13423static bfd_boolean
4ce794b7
AM
13424ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13425 struct bfd_link_info *info)
5bd4f169 13426{
65f38f15
AM
13427 struct ppc_link_hash_table *htab;
13428 bfd *dynobj;
5bd4f169 13429 asection *sdyn;
5bd4f169 13430
65f38f15 13431 htab = ppc_hash_table (info);
4dfe6ac6
NC
13432 if (htab == NULL)
13433 return FALSE;
13434
65f38f15 13435 dynobj = htab->elf.dynobj;
5bd4f169
AM
13436 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13437
65f38f15 13438 if (htab->elf.dynamic_sections_created)
5bd4f169 13439 {
5bd4f169
AM
13440 Elf64_External_Dyn *dyncon, *dynconend;
13441
4ce794b7 13442 if (sdyn == NULL || htab->got == NULL)
65f38f15 13443 abort ();
5bd4f169
AM
13444
13445 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 13446 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
13447 for (; dyncon < dynconend; dyncon++)
13448 {
13449 Elf_Internal_Dyn dyn;
19397422 13450 asection *s;
5bd4f169
AM
13451
13452 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13453
13454 switch (dyn.d_tag)
13455 {
65f38f15
AM
13456 default:
13457 continue;
5bd4f169 13458
5d1634d7 13459 case DT_PPC64_GLINK:
4ce794b7 13460 s = htab->glink;
6348e046 13461 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
13462 /* We stupidly defined DT_PPC64_GLINK to be the start
13463 of glink rather than the first entry point, which is
13464 what ld.so needs, and now have a bigger stub to
13465 support automatic multiple TOCs. */
13466 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
13467 break;
13468
19397422
AM
13469 case DT_PPC64_OPD:
13470 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13471 if (s == NULL)
13472 continue;
13473 dyn.d_un.d_ptr = s->vma;
19397422
AM
13474 break;
13475
13476 case DT_PPC64_OPDSZ:
13477 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13478 if (s == NULL)
13479 continue;
eea6121a 13480 dyn.d_un.d_val = s->size;
19397422
AM
13481 break;
13482
65f38f15 13483 case DT_PLTGOT:
4ce794b7 13484 s = htab->plt;
6348e046 13485 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
13486 break;
13487
13488 case DT_JMPREL:
4ce794b7 13489 s = htab->relplt;
6348e046 13490 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 13491 break;
5bd4f169 13492
65f38f15 13493 case DT_PLTRELSZ:
eea6121a 13494 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
13495 break;
13496
13497 case DT_RELASZ:
13498 /* Don't count procedure linkage table relocs in the
13499 overall reloc count. */
4ce794b7 13500 s = htab->relplt;
6348e046
AM
13501 if (s == NULL)
13502 continue;
eea6121a 13503 dyn.d_un.d_val -= s->size;
6348e046
AM
13504 break;
13505
13506 case DT_RELA:
13507 /* We may not be using the standard ELF linker script.
13508 If .rela.plt is the first .rela section, we adjust
13509 DT_RELA to not include it. */
4ce794b7 13510 s = htab->relplt;
6348e046
AM
13511 if (s == NULL)
13512 continue;
13513 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13514 continue;
eea6121a 13515 dyn.d_un.d_ptr += s->size;
65f38f15 13516 break;
5bd4f169 13517 }
5bd4f169 13518
65f38f15 13519 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 13520 }
5bd4f169
AM
13521 }
13522
eea6121a 13523 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
13524 {
13525 /* Fill in the first entry in the global offset table.
13526 We use it to hold the link-time TOCbase. */
13527 bfd_put_64 (output_bfd,
60ee0d4a 13528 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 13529 htab->got->contents);
5d1634d7
AM
13530
13531 /* Set .got entry size. */
4ce794b7 13532 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
13533 }
13534
eea6121a 13535 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
13536 {
13537 /* Set .plt entry size. */
4ce794b7 13538 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
13539 = PLT_ENTRY_SIZE;
13540 }
13541
84f5d08e
AM
13542 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13543 brlt ourselves if emitrelocations. */
13544 if (htab->brlt != NULL
13545 && htab->brlt->reloc_count != 0
13546 && !_bfd_elf_link_output_relocs (output_bfd,
13547 htab->brlt,
d4730f92 13548 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
13549 elf_section_data (htab->brlt)->relocs,
13550 NULL))
13551 return FALSE;
13552
176a0d42
AM
13553 if (htab->glink != NULL
13554 && htab->glink->reloc_count != 0
13555 && !_bfd_elf_link_output_relocs (output_bfd,
13556 htab->glink,
d4730f92 13557 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
13558 elf_section_data (htab->glink)->relocs,
13559 NULL))
13560 return FALSE;
13561
e717da7e 13562 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
13563 since we didn't add them to DYNOBJ. We know dynobj is the first
13564 bfd. */
e717da7e
AM
13565 while ((dynobj = dynobj->link_next) != NULL)
13566 {
13567 asection *s;
7b53ace3 13568
0c8d6e5c 13569 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
13570 continue;
13571
e717da7e
AM
13572 s = ppc64_elf_tdata (dynobj)->got;
13573 if (s != NULL
eea6121a 13574 && s->size != 0
e717da7e
AM
13575 && s->output_section != bfd_abs_section_ptr
13576 && !bfd_set_section_contents (output_bfd, s->output_section,
13577 s->contents, s->output_offset,
eea6121a 13578 s->size))
e717da7e
AM
13579 return FALSE;
13580 s = ppc64_elf_tdata (dynobj)->relgot;
13581 if (s != NULL
eea6121a 13582 && s->size != 0
e717da7e
AM
13583 && s->output_section != bfd_abs_section_ptr
13584 && !bfd_set_section_contents (output_bfd, s->output_section,
13585 s->contents, s->output_offset,
eea6121a 13586 s->size))
e717da7e
AM
13587 return FALSE;
13588 }
f6c52c13 13589
b34976b6 13590 return TRUE;
5bd4f169
AM
13591}
13592
5bd4f169 13593#include "elf64-target.h"