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