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