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