sim: bfin: respect the port level on signals to the SIC
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
3a71aa26 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
df22d223 3 2009, 2010, 2011 Free Software Foundation, Inc.
5bd4f169
AM
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
32ca9640 6 Largely rewritten by Alan Modra.
5bd4f169 7
ae9a127f 8 This file is part of BFD, the Binary File Descriptor library.
5bd4f169 9
ae9a127f
NC
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
cd123cb7 12 the Free Software Foundation; either version 3 of the License, or
ae9a127f 13 (at your option) any later version.
5bd4f169 14
ae9a127f
NC
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
5bd4f169 19
4ce794b7
AM
20 You should have received a copy of the GNU General Public License along
21 with this program; if not, write to the Free Software Foundation, Inc.,
3e110533 22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
5bd4f169 23
cd123cb7 24
4ce794b7
AM
25/* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169 28
3db64b00 29#include "sysdep.h"
183e98be 30#include <stdarg.h>
5bd4f169 31#include "bfd.h"
5bd4f169
AM
32#include "bfdlink.h"
33#include "libbfd.h"
34#include "elf-bfd.h"
04c9666a 35#include "elf/ppc64.h"
5d1634d7 36#include "elf64-ppc.h"
5bd4f169 37
805fc799 38static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
40static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 54static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
56static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *);
5bd4f169 58
ad8e1ba5
AM
59#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
60#define TARGET_LITTLE_NAME "elf64-powerpcle"
61#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
62#define TARGET_BIG_NAME "elf64-powerpc"
63#define ELF_ARCH bfd_arch_powerpc
ae95ffa6 64#define ELF_TARGET_ID PPC64_ELF_DATA
ad8e1ba5
AM
65#define ELF_MACHINE_CODE EM_PPC64
66#define ELF_MAXPAGESIZE 0x10000
24718e3b 67#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
68#define elf_info_to_howto ppc64_elf_info_to_howto
69
70#define elf_backend_want_got_sym 0
71#define elf_backend_want_plt_sym 0
72#define elf_backend_plt_alignment 3
73#define elf_backend_plt_not_loaded 1
ad8e1ba5 74#define elf_backend_got_header_size 8
ad8e1ba5
AM
75#define elf_backend_can_gc_sections 1
76#define elf_backend_can_refcount 1
77#define elf_backend_rela_normal 1
6bfdb61b 78#define elf_backend_default_execstack 0
ad8e1ba5 79
e717da7e 80#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5 81#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
aa374f67 82#define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
ad8e1ba5
AM
83#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
84#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
85#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
86#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 87#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
aa374f67 88#define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
ad8e1ba5
AM
89
90#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
91#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
92#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
183e98be 93#define elf_backend_write_core_note ppc64_elf_write_core_note
ad8e1ba5
AM
94#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
95#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 96#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
7d9616d7 97#define elf_backend_check_directives ppc64_elf_process_dot_syms
97fed1c9 98#define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
8387904d 99#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 100#define elf_backend_check_relocs ppc64_elf_check_relocs
74f0fb50 101#define elf_backend_gc_keep ppc64_elf_gc_keep
64d03ab5 102#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
103#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
104#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
105#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
106#define elf_backend_hide_symbol ppc64_elf_hide_symbol
107#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
108#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 109#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 110#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
111#define elf_backend_relocate_section ppc64_elf_relocate_section
112#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
113#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
114#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 115#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 116#define elf_backend_special_sections ppc64_elf_special_sections
e054468f 117#define elf_backend_post_process_headers _bfd_elf_set_osabi
ad8e1ba5 118
5bd4f169
AM
119/* The name of the dynamic interpreter. This is put in the .interp
120 section. */
121#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
122
123/* The size in bytes of an entry in the procedure linkage table. */
124#define PLT_ENTRY_SIZE 24
125
126/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 127#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
128
129/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
130#define TOC_BASE_OFF 0x8000
131
132/* Offset of tp and dtp pointers from start of TLS block. */
133#define TP_OFFSET 0x7000
134#define DTP_OFFSET 0x8000
5bd4f169 135
ad8e1ba5
AM
136/* .plt call stub instructions. The normal stub is like this, but
137 sometimes the .plt entry crosses a 64k boundary and we need to
ac2df442 138 insert an addi to adjust r12. */
ad8e1ba5 139#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7
AM
140#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
141#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
142#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
5d1634d7 143#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
ac2df442 144#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
5d1634d7
AM
145 /* ld %r11,xxx+16@l(%r12) */
146#define BCTR 0x4e800420 /* bctr */
147
5d1634d7 148
ee4bf8d2 149#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
ac2df442 150#define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
ad8e1ba5
AM
151#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
152#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
153
ac2df442
AM
154#define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
155#define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
156
ad8e1ba5
AM
157#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
158
ee4bf8d2 159/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 160#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
161 /* 0: */
162 /* .quad plt0-1f */
163 /* __glink: */
164#define MFLR_R12 0x7d8802a6 /* mflr %12 */
165#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
166 /* 1: */
167#define MFLR_R11 0x7d6802a6 /* mflr %11 */
168#define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
169#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
170#define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
171 /* ld %11,0(%12) */
172 /* ld %2,8(%12) */
173 /* mtctr %11 */
174 /* ld %11,16(%12) */
175 /* bctr */
5d1634d7
AM
176
177/* Pad with this. */
178#define NOP 0x60000000
179
721956f4
AM
180/* Some other nops. */
181#define CROR_151515 0x4def7b82
182#define CROR_313131 0x4ffffb82
183
cedb70c5 184/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
185#define LI_R0_0 0x38000000 /* li %r0,0 */
186#define B_DOT 0x48000000 /* b . */
187
188/* After that, we need two instructions to load the index, followed by
189 a branch. */
190#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 191#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 192
deb0e272
AM
193/* Instructions used by the save and restore reg functions. */
194#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
195#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
196#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
197#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
198#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
199#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
200#define LI_R12_0 0x39800000 /* li %r12,0 */
201#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
202#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
203#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
204#define BLR 0x4e800020 /* blr */
205
41bd81ab
AM
206/* Since .opd is an array of descriptors and each entry will end up
207 with identical R_PPC64_RELATIVE relocs, there is really no need to
208 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 209 relocate .opd without reloc entries. */
41bd81ab
AM
210#ifndef NO_OPD_RELOCS
211#define NO_OPD_RELOCS 0
212#endif
5bd4f169 213\f
f5e87a1d 214#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 215
5bd4f169 216/* Relocation HOWTO's. */
04c9666a 217static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
218
219static reloc_howto_type ppc64_elf_howto_raw[] = {
220 /* This reloc does nothing. */
221 HOWTO (R_PPC64_NONE, /* type */
222 0, /* rightshift */
411e1bfb
AM
223 2, /* size (0 = byte, 1 = short, 2 = long) */
224 32, /* bitsize */
b34976b6 225 FALSE, /* pc_relative */
5bd4f169 226 0, /* bitpos */
f5e87a1d 227 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
228 bfd_elf_generic_reloc, /* special_function */
229 "R_PPC64_NONE", /* name */
b34976b6 230 FALSE, /* partial_inplace */
d006db6c 231 0, /* src_mask */
5bd4f169 232 0, /* dst_mask */
b34976b6 233 FALSE), /* pcrel_offset */
5bd4f169
AM
234
235 /* A standard 32 bit relocation. */
236 HOWTO (R_PPC64_ADDR32, /* type */
237 0, /* rightshift */
238 2, /* size (0 = byte, 1 = short, 2 = long) */
239 32, /* bitsize */
b34976b6 240 FALSE, /* pc_relative */
5bd4f169
AM
241 0, /* bitpos */
242 complain_overflow_bitfield, /* complain_on_overflow */
243 bfd_elf_generic_reloc, /* special_function */
244 "R_PPC64_ADDR32", /* name */
b34976b6 245 FALSE, /* partial_inplace */
5bd4f169
AM
246 0, /* src_mask */
247 0xffffffff, /* dst_mask */
b34976b6 248 FALSE), /* pcrel_offset */
5bd4f169
AM
249
250 /* An absolute 26 bit branch; the lower two bits must be zero.
251 FIXME: we don't check that, we just clear them. */
252 HOWTO (R_PPC64_ADDR24, /* type */
253 0, /* rightshift */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 26, /* bitsize */
b34976b6 256 FALSE, /* pc_relative */
5bd4f169
AM
257 0, /* bitpos */
258 complain_overflow_bitfield, /* complain_on_overflow */
259 bfd_elf_generic_reloc, /* special_function */
260 "R_PPC64_ADDR24", /* name */
b34976b6 261 FALSE, /* partial_inplace */
d006db6c 262 0, /* src_mask */
f5e87a1d 263 0x03fffffc, /* dst_mask */
b34976b6 264 FALSE), /* pcrel_offset */
5bd4f169
AM
265
266 /* A standard 16 bit relocation. */
267 HOWTO (R_PPC64_ADDR16, /* type */
268 0, /* rightshift */
269 1, /* size (0 = byte, 1 = short, 2 = long) */
270 16, /* bitsize */
b34976b6 271 FALSE, /* pc_relative */
5bd4f169
AM
272 0, /* bitpos */
273 complain_overflow_bitfield, /* complain_on_overflow */
274 bfd_elf_generic_reloc, /* special_function */
275 "R_PPC64_ADDR16", /* name */
b34976b6 276 FALSE, /* partial_inplace */
5bd4f169
AM
277 0, /* src_mask */
278 0xffff, /* dst_mask */
b34976b6 279 FALSE), /* pcrel_offset */
5bd4f169
AM
280
281 /* A 16 bit relocation without overflow. */
282 HOWTO (R_PPC64_ADDR16_LO, /* type */
283 0, /* rightshift */
284 1, /* size (0 = byte, 1 = short, 2 = long) */
285 16, /* bitsize */
b34976b6 286 FALSE, /* pc_relative */
5bd4f169
AM
287 0, /* bitpos */
288 complain_overflow_dont,/* complain_on_overflow */
289 bfd_elf_generic_reloc, /* special_function */
290 "R_PPC64_ADDR16_LO", /* name */
b34976b6 291 FALSE, /* partial_inplace */
5bd4f169
AM
292 0, /* src_mask */
293 0xffff, /* dst_mask */
b34976b6 294 FALSE), /* pcrel_offset */
5bd4f169
AM
295
296 /* Bits 16-31 of an address. */
297 HOWTO (R_PPC64_ADDR16_HI, /* type */
298 16, /* rightshift */
299 1, /* size (0 = byte, 1 = short, 2 = long) */
300 16, /* bitsize */
b34976b6 301 FALSE, /* pc_relative */
5bd4f169
AM
302 0, /* bitpos */
303 complain_overflow_dont, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* special_function */
305 "R_PPC64_ADDR16_HI", /* name */
b34976b6 306 FALSE, /* partial_inplace */
5bd4f169
AM
307 0, /* src_mask */
308 0xffff, /* dst_mask */
b34976b6 309 FALSE), /* pcrel_offset */
5bd4f169
AM
310
311 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
312 bits, treated as a signed number, is negative. */
313 HOWTO (R_PPC64_ADDR16_HA, /* type */
314 16, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 16, /* bitsize */
b34976b6 317 FALSE, /* pc_relative */
5bd4f169
AM
318 0, /* bitpos */
319 complain_overflow_dont, /* complain_on_overflow */
805fc799 320 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 321 "R_PPC64_ADDR16_HA", /* name */
b34976b6 322 FALSE, /* partial_inplace */
5bd4f169
AM
323 0, /* src_mask */
324 0xffff, /* dst_mask */
b34976b6 325 FALSE), /* pcrel_offset */
5bd4f169
AM
326
327 /* An absolute 16 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOWTO (R_PPC64_ADDR14, /* type */
330 0, /* rightshift */
331 2, /* size (0 = byte, 1 = short, 2 = long) */
332 16, /* bitsize */
b34976b6 333 FALSE, /* pc_relative */
5bd4f169
AM
334 0, /* bitpos */
335 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 336 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 337 "R_PPC64_ADDR14", /* name */
b34976b6 338 FALSE, /* partial_inplace */
d006db6c 339 0, /* src_mask */
f5e87a1d 340 0x0000fffc, /* dst_mask */
b34976b6 341 FALSE), /* pcrel_offset */
5bd4f169
AM
342
343 /* An absolute 16 bit branch, for which bit 10 should be set to
344 indicate that the branch is expected to be taken. The lower two
345 bits must be zero. */
346 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
347 0, /* rightshift */
348 2, /* size (0 = byte, 1 = short, 2 = long) */
349 16, /* bitsize */
b34976b6 350 FALSE, /* pc_relative */
5bd4f169
AM
351 0, /* bitpos */
352 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 353 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 354 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 355 FALSE, /* partial_inplace */
d006db6c 356 0, /* src_mask */
f5e87a1d 357 0x0000fffc, /* dst_mask */
b34976b6 358 FALSE), /* pcrel_offset */
5bd4f169
AM
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
364 0, /* rightshift */
365 2, /* size (0 = byte, 1 = short, 2 = long) */
366 16, /* bitsize */
b34976b6 367 FALSE, /* pc_relative */
5bd4f169
AM
368 0, /* bitpos */
369 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 370 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 371 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 372 FALSE, /* partial_inplace */
d006db6c 373 0, /* src_mask */
f5e87a1d 374 0x0000fffc, /* dst_mask */
b34976b6 375 FALSE), /* pcrel_offset */
5bd4f169
AM
376
377 /* A relative 26 bit branch; the lower two bits must be zero. */
378 HOWTO (R_PPC64_REL24, /* type */
379 0, /* rightshift */
380 2, /* size (0 = byte, 1 = short, 2 = long) */
381 26, /* bitsize */
b34976b6 382 TRUE, /* pc_relative */
5bd4f169
AM
383 0, /* bitpos */
384 complain_overflow_signed, /* complain_on_overflow */
2441e016 385 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 386 "R_PPC64_REL24", /* name */
b34976b6 387 FALSE, /* partial_inplace */
d006db6c 388 0, /* src_mask */
f5e87a1d 389 0x03fffffc, /* dst_mask */
b34976b6 390 TRUE), /* pcrel_offset */
5bd4f169
AM
391
392 /* A relative 16 bit branch; the lower two bits must be zero. */
393 HOWTO (R_PPC64_REL14, /* type */
394 0, /* rightshift */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
396 16, /* bitsize */
b34976b6 397 TRUE, /* pc_relative */
5bd4f169
AM
398 0, /* bitpos */
399 complain_overflow_signed, /* complain_on_overflow */
2441e016 400 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 401 "R_PPC64_REL14", /* name */
b34976b6 402 FALSE, /* partial_inplace */
d006db6c 403 0, /* src_mask */
f5e87a1d 404 0x0000fffc, /* dst_mask */
b34976b6 405 TRUE), /* pcrel_offset */
5bd4f169
AM
406
407 /* A relative 16 bit branch. Bit 10 should be set to indicate that
408 the branch is expected to be taken. The lower two bits must be
409 zero. */
410 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
411 0, /* rightshift */
412 2, /* size (0 = byte, 1 = short, 2 = long) */
413 16, /* bitsize */
b34976b6 414 TRUE, /* pc_relative */
5bd4f169
AM
415 0, /* bitpos */
416 complain_overflow_signed, /* complain_on_overflow */
805fc799 417 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 418 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 419 FALSE, /* partial_inplace */
d006db6c 420 0, /* src_mask */
f5e87a1d 421 0x0000fffc, /* dst_mask */
b34976b6 422 TRUE), /* pcrel_offset */
5bd4f169
AM
423
424 /* A relative 16 bit branch. Bit 10 should be set to indicate that
425 the branch is not expected to be taken. The lower two bits must
426 be zero. */
427 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
428 0, /* rightshift */
429 2, /* size (0 = byte, 1 = short, 2 = long) */
430 16, /* bitsize */
b34976b6 431 TRUE, /* pc_relative */
5bd4f169
AM
432 0, /* bitpos */
433 complain_overflow_signed, /* complain_on_overflow */
805fc799 434 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 435 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 436 FALSE, /* partial_inplace */
d006db6c 437 0, /* src_mask */
f5e87a1d 438 0x0000fffc, /* dst_mask */
b34976b6 439 TRUE), /* pcrel_offset */
5bd4f169
AM
440
441 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
442 symbol. */
443 HOWTO (R_PPC64_GOT16, /* type */
444 0, /* rightshift */
445 1, /* size (0 = byte, 1 = short, 2 = long) */
446 16, /* bitsize */
b34976b6 447 FALSE, /* pc_relative */
5bd4f169
AM
448 0, /* bitpos */
449 complain_overflow_signed, /* complain_on_overflow */
805fc799 450 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 451 "R_PPC64_GOT16", /* name */
b34976b6 452 FALSE, /* partial_inplace */
5bd4f169
AM
453 0, /* src_mask */
454 0xffff, /* dst_mask */
b34976b6 455 FALSE), /* pcrel_offset */
5bd4f169
AM
456
457 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
458 the symbol. */
459 HOWTO (R_PPC64_GOT16_LO, /* type */
460 0, /* rightshift */
461 1, /* size (0 = byte, 1 = short, 2 = long) */
462 16, /* bitsize */
b34976b6 463 FALSE, /* pc_relative */
5bd4f169
AM
464 0, /* bitpos */
465 complain_overflow_dont, /* complain_on_overflow */
805fc799 466 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 467 "R_PPC64_GOT16_LO", /* name */
b34976b6 468 FALSE, /* partial_inplace */
5bd4f169
AM
469 0, /* src_mask */
470 0xffff, /* dst_mask */
b34976b6 471 FALSE), /* pcrel_offset */
5bd4f169
AM
472
473 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
474 the symbol. */
475 HOWTO (R_PPC64_GOT16_HI, /* type */
476 16, /* rightshift */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
478 16, /* bitsize */
b34976b6 479 FALSE, /* pc_relative */
5bd4f169
AM
480 0, /* bitpos */
481 complain_overflow_dont,/* complain_on_overflow */
805fc799 482 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 483 "R_PPC64_GOT16_HI", /* name */
b34976b6 484 FALSE, /* partial_inplace */
5bd4f169
AM
485 0, /* src_mask */
486 0xffff, /* dst_mask */
b34976b6 487 FALSE), /* pcrel_offset */
5bd4f169
AM
488
489 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
490 the symbol. */
491 HOWTO (R_PPC64_GOT16_HA, /* type */
492 16, /* rightshift */
493 1, /* size (0 = byte, 1 = short, 2 = long) */
494 16, /* bitsize */
b34976b6 495 FALSE, /* pc_relative */
5bd4f169
AM
496 0, /* bitpos */
497 complain_overflow_dont,/* complain_on_overflow */
805fc799 498 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 499 "R_PPC64_GOT16_HA", /* name */
b34976b6 500 FALSE, /* partial_inplace */
5bd4f169
AM
501 0, /* src_mask */
502 0xffff, /* dst_mask */
b34976b6 503 FALSE), /* pcrel_offset */
5bd4f169
AM
504
505 /* This is used only by the dynamic linker. The symbol should exist
506 both in the object being run and in some shared library. The
507 dynamic linker copies the data addressed by the symbol from the
508 shared library into the object, because the object being
509 run has to have the data at some particular address. */
510 HOWTO (R_PPC64_COPY, /* type */
511 0, /* rightshift */
f5e87a1d
AM
512 0, /* this one is variable size */
513 0, /* bitsize */
b34976b6 514 FALSE, /* pc_relative */
5bd4f169 515 0, /* bitpos */
f5e87a1d
AM
516 complain_overflow_dont, /* complain_on_overflow */
517 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 518 "R_PPC64_COPY", /* name */
b34976b6 519 FALSE, /* partial_inplace */
5bd4f169
AM
520 0, /* src_mask */
521 0, /* dst_mask */
b34976b6 522 FALSE), /* pcrel_offset */
5bd4f169
AM
523
524 /* Like R_PPC64_ADDR64, but used when setting global offset table
525 entries. */
526 HOWTO (R_PPC64_GLOB_DAT, /* type */
527 0, /* rightshift */
528 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
529 64, /* bitsize */
b34976b6 530 FALSE, /* pc_relative */
5bd4f169
AM
531 0, /* bitpos */
532 complain_overflow_dont, /* complain_on_overflow */
805fc799 533 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 534 "R_PPC64_GLOB_DAT", /* name */
b34976b6 535 FALSE, /* partial_inplace */
5bd4f169 536 0, /* src_mask */
f5e87a1d 537 ONES (64), /* dst_mask */
b34976b6 538 FALSE), /* pcrel_offset */
5bd4f169
AM
539
540 /* Created by the link editor. Marks a procedure linkage table
541 entry for a symbol. */
542 HOWTO (R_PPC64_JMP_SLOT, /* type */
543 0, /* rightshift */
544 0, /* size (0 = byte, 1 = short, 2 = long) */
545 0, /* bitsize */
b34976b6 546 FALSE, /* pc_relative */
5bd4f169
AM
547 0, /* bitpos */
548 complain_overflow_dont, /* complain_on_overflow */
805fc799 549 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 550 "R_PPC64_JMP_SLOT", /* name */
b34976b6 551 FALSE, /* partial_inplace */
5bd4f169
AM
552 0, /* src_mask */
553 0, /* dst_mask */
b34976b6 554 FALSE), /* pcrel_offset */
5bd4f169
AM
555
556 /* Used only by the dynamic linker. When the object is run, this
557 doubleword64 is set to the load address of the object, plus the
558 addend. */
559 HOWTO (R_PPC64_RELATIVE, /* type */
560 0, /* rightshift */
561 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
562 64, /* bitsize */
b34976b6 563 FALSE, /* pc_relative */
5bd4f169
AM
564 0, /* bitpos */
565 complain_overflow_dont, /* complain_on_overflow */
566 bfd_elf_generic_reloc, /* special_function */
567 "R_PPC64_RELATIVE", /* name */
b34976b6 568 FALSE, /* partial_inplace */
5bd4f169 569 0, /* src_mask */
f5e87a1d 570 ONES (64), /* dst_mask */
b34976b6 571 FALSE), /* pcrel_offset */
5bd4f169
AM
572
573 /* Like R_PPC64_ADDR32, but may be unaligned. */
574 HOWTO (R_PPC64_UADDR32, /* type */
575 0, /* rightshift */
576 2, /* size (0 = byte, 1 = short, 2 = long) */
577 32, /* bitsize */
b34976b6 578 FALSE, /* pc_relative */
5bd4f169
AM
579 0, /* bitpos */
580 complain_overflow_bitfield, /* complain_on_overflow */
581 bfd_elf_generic_reloc, /* special_function */
582 "R_PPC64_UADDR32", /* name */
b34976b6 583 FALSE, /* partial_inplace */
5bd4f169
AM
584 0, /* src_mask */
585 0xffffffff, /* dst_mask */
b34976b6 586 FALSE), /* pcrel_offset */
5bd4f169
AM
587
588 /* Like R_PPC64_ADDR16, but may be unaligned. */
589 HOWTO (R_PPC64_UADDR16, /* type */
590 0, /* rightshift */
591 1, /* size (0 = byte, 1 = short, 2 = long) */
592 16, /* bitsize */
b34976b6 593 FALSE, /* pc_relative */
5bd4f169
AM
594 0, /* bitpos */
595 complain_overflow_bitfield, /* complain_on_overflow */
596 bfd_elf_generic_reloc, /* special_function */
597 "R_PPC64_UADDR16", /* name */
b34976b6 598 FALSE, /* partial_inplace */
5bd4f169
AM
599 0, /* src_mask */
600 0xffff, /* dst_mask */
b34976b6 601 FALSE), /* pcrel_offset */
5bd4f169
AM
602
603 /* 32-bit PC relative. */
604 HOWTO (R_PPC64_REL32, /* type */
605 0, /* rightshift */
606 2, /* size (0 = byte, 1 = short, 2 = long) */
607 32, /* bitsize */
b34976b6 608 TRUE, /* pc_relative */
5bd4f169 609 0, /* bitpos */
cedb70c5 610 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
611 complain_overflow_signed, /* complain_on_overflow */
612 bfd_elf_generic_reloc, /* special_function */
613 "R_PPC64_REL32", /* name */
b34976b6 614 FALSE, /* partial_inplace */
5bd4f169
AM
615 0, /* src_mask */
616 0xffffffff, /* dst_mask */
b34976b6 617 TRUE), /* pcrel_offset */
5bd4f169 618
10ed1bba 619 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
620 HOWTO (R_PPC64_PLT32, /* type */
621 0, /* rightshift */
622 2, /* size (0 = byte, 1 = short, 2 = long) */
623 32, /* bitsize */
b34976b6 624 FALSE, /* pc_relative */
5bd4f169
AM
625 0, /* bitpos */
626 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 627 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 628 "R_PPC64_PLT32", /* name */
b34976b6 629 FALSE, /* partial_inplace */
5bd4f169 630 0, /* src_mask */
f5e87a1d 631 0xffffffff, /* dst_mask */
b34976b6 632 FALSE), /* pcrel_offset */
5bd4f169
AM
633
634 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
635 FIXME: R_PPC64_PLTREL32 not supported. */
636 HOWTO (R_PPC64_PLTREL32, /* type */
637 0, /* rightshift */
638 2, /* size (0 = byte, 1 = short, 2 = long) */
639 32, /* bitsize */
b34976b6 640 TRUE, /* pc_relative */
5bd4f169
AM
641 0, /* bitpos */
642 complain_overflow_signed, /* complain_on_overflow */
643 bfd_elf_generic_reloc, /* special_function */
644 "R_PPC64_PLTREL32", /* name */
b34976b6 645 FALSE, /* partial_inplace */
5bd4f169 646 0, /* src_mask */
f5e87a1d 647 0xffffffff, /* dst_mask */
b34976b6 648 TRUE), /* pcrel_offset */
5bd4f169
AM
649
650 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
651 the symbol. */
652 HOWTO (R_PPC64_PLT16_LO, /* type */
653 0, /* rightshift */
654 1, /* size (0 = byte, 1 = short, 2 = long) */
655 16, /* bitsize */
b34976b6 656 FALSE, /* pc_relative */
5bd4f169
AM
657 0, /* bitpos */
658 complain_overflow_dont, /* complain_on_overflow */
805fc799 659 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 660 "R_PPC64_PLT16_LO", /* name */
b34976b6 661 FALSE, /* partial_inplace */
5bd4f169
AM
662 0, /* src_mask */
663 0xffff, /* dst_mask */
b34976b6 664 FALSE), /* pcrel_offset */
5bd4f169
AM
665
666 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
667 the symbol. */
668 HOWTO (R_PPC64_PLT16_HI, /* type */
669 16, /* rightshift */
670 1, /* size (0 = byte, 1 = short, 2 = long) */
671 16, /* bitsize */
b34976b6 672 FALSE, /* pc_relative */
5bd4f169
AM
673 0, /* bitpos */
674 complain_overflow_dont, /* complain_on_overflow */
805fc799 675 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 676 "R_PPC64_PLT16_HI", /* name */
b34976b6 677 FALSE, /* partial_inplace */
5bd4f169
AM
678 0, /* src_mask */
679 0xffff, /* dst_mask */
b34976b6 680 FALSE), /* pcrel_offset */
5bd4f169
AM
681
682 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
683 the symbol. */
684 HOWTO (R_PPC64_PLT16_HA, /* type */
685 16, /* rightshift */
686 1, /* size (0 = byte, 1 = short, 2 = long) */
687 16, /* bitsize */
b34976b6 688 FALSE, /* pc_relative */
5bd4f169
AM
689 0, /* bitpos */
690 complain_overflow_dont, /* complain_on_overflow */
805fc799 691 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 692 "R_PPC64_PLT16_HA", /* name */
b34976b6 693 FALSE, /* partial_inplace */
5bd4f169
AM
694 0, /* src_mask */
695 0xffff, /* dst_mask */
b34976b6 696 FALSE), /* pcrel_offset */
5bd4f169 697
c061c2d8 698 /* 16-bit section relative relocation. */
5bd4f169
AM
699 HOWTO (R_PPC64_SECTOFF, /* type */
700 0, /* rightshift */
c061c2d8
AM
701 1, /* size (0 = byte, 1 = short, 2 = long) */
702 16, /* bitsize */
b34976b6 703 FALSE, /* pc_relative */
5bd4f169
AM
704 0, /* bitpos */
705 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 706 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 707 "R_PPC64_SECTOFF", /* name */
b34976b6 708 FALSE, /* partial_inplace */
5bd4f169 709 0, /* src_mask */
c061c2d8 710 0xffff, /* dst_mask */
b34976b6 711 FALSE), /* pcrel_offset */
5bd4f169 712
c061c2d8 713 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
714 HOWTO (R_PPC64_SECTOFF_LO, /* type */
715 0, /* rightshift */
716 1, /* size (0 = byte, 1 = short, 2 = long) */
717 16, /* bitsize */
b34976b6 718 FALSE, /* pc_relative */
5bd4f169
AM
719 0, /* bitpos */
720 complain_overflow_dont, /* complain_on_overflow */
805fc799 721 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 722 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 723 FALSE, /* partial_inplace */
5bd4f169
AM
724 0, /* src_mask */
725 0xffff, /* dst_mask */
b34976b6 726 FALSE), /* pcrel_offset */
5bd4f169
AM
727
728 /* 16-bit upper half section relative relocation. */
729 HOWTO (R_PPC64_SECTOFF_HI, /* type */
730 16, /* rightshift */
731 1, /* size (0 = byte, 1 = short, 2 = long) */
732 16, /* bitsize */
b34976b6 733 FALSE, /* pc_relative */
5bd4f169
AM
734 0, /* bitpos */
735 complain_overflow_dont, /* complain_on_overflow */
805fc799 736 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 737 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 738 FALSE, /* partial_inplace */
5bd4f169
AM
739 0, /* src_mask */
740 0xffff, /* dst_mask */
b34976b6 741 FALSE), /* pcrel_offset */
5bd4f169
AM
742
743 /* 16-bit upper half adjusted section relative relocation. */
744 HOWTO (R_PPC64_SECTOFF_HA, /* type */
745 16, /* rightshift */
746 1, /* size (0 = byte, 1 = short, 2 = long) */
747 16, /* bitsize */
b34976b6 748 FALSE, /* pc_relative */
5bd4f169
AM
749 0, /* bitpos */
750 complain_overflow_dont, /* complain_on_overflow */
805fc799 751 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 752 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 753 FALSE, /* partial_inplace */
5bd4f169
AM
754 0, /* src_mask */
755 0xffff, /* dst_mask */
b34976b6 756 FALSE), /* pcrel_offset */
5bd4f169 757
04c9666a
AM
758 /* Like R_PPC64_REL24 without touching the two least significant bits. */
759 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
760 2, /* rightshift */
761 2, /* size (0 = byte, 1 = short, 2 = long) */
762 30, /* bitsize */
b34976b6 763 TRUE, /* pc_relative */
5bd4f169
AM
764 0, /* bitpos */
765 complain_overflow_dont, /* complain_on_overflow */
766 bfd_elf_generic_reloc, /* special_function */
04c9666a 767 "R_PPC64_REL30", /* name */
b34976b6 768 FALSE, /* partial_inplace */
d006db6c 769 0, /* src_mask */
5bd4f169 770 0xfffffffc, /* dst_mask */
b34976b6 771 TRUE), /* pcrel_offset */
5bd4f169
AM
772
773 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
774
775 /* A standard 64-bit relocation. */
776 HOWTO (R_PPC64_ADDR64, /* type */
777 0, /* rightshift */
778 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
779 64, /* bitsize */
b34976b6 780 FALSE, /* pc_relative */
5bd4f169
AM
781 0, /* bitpos */
782 complain_overflow_dont, /* complain_on_overflow */
783 bfd_elf_generic_reloc, /* special_function */
784 "R_PPC64_ADDR64", /* name */
b34976b6 785 FALSE, /* partial_inplace */
5bd4f169 786 0, /* src_mask */
f5e87a1d 787 ONES (64), /* dst_mask */
b34976b6 788 FALSE), /* pcrel_offset */
5bd4f169
AM
789
790 /* The bits 32-47 of an address. */
791 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
792 32, /* rightshift */
793 1, /* size (0 = byte, 1 = short, 2 = long) */
794 16, /* bitsize */
b34976b6 795 FALSE, /* pc_relative */
5bd4f169
AM
796 0, /* bitpos */
797 complain_overflow_dont, /* complain_on_overflow */
798 bfd_elf_generic_reloc, /* special_function */
799 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 800 FALSE, /* partial_inplace */
5bd4f169
AM
801 0, /* src_mask */
802 0xffff, /* dst_mask */
b34976b6 803 FALSE), /* pcrel_offset */
5bd4f169
AM
804
805 /* The bits 32-47 of an address, plus 1 if the contents of the low
806 16 bits, treated as a signed number, is negative. */
807 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
808 32, /* rightshift */
809 1, /* size (0 = byte, 1 = short, 2 = long) */
810 16, /* bitsize */
b34976b6 811 FALSE, /* pc_relative */
5bd4f169
AM
812 0, /* bitpos */
813 complain_overflow_dont, /* complain_on_overflow */
805fc799 814 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 815 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 816 FALSE, /* partial_inplace */
5bd4f169
AM
817 0, /* src_mask */
818 0xffff, /* dst_mask */
b34976b6 819 FALSE), /* pcrel_offset */
5bd4f169
AM
820
821 /* The bits 48-63 of an address. */
822 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
823 48, /* rightshift */
824 1, /* size (0 = byte, 1 = short, 2 = long) */
825 16, /* bitsize */
b34976b6 826 FALSE, /* pc_relative */
5bd4f169
AM
827 0, /* bitpos */
828 complain_overflow_dont, /* complain_on_overflow */
829 bfd_elf_generic_reloc, /* special_function */
830 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 831 FALSE, /* partial_inplace */
5bd4f169
AM
832 0, /* src_mask */
833 0xffff, /* dst_mask */
b34976b6 834 FALSE), /* pcrel_offset */
5bd4f169
AM
835
836 /* The bits 48-63 of an address, plus 1 if the contents of the low
837 16 bits, treated as a signed number, is negative. */
838 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
839 48, /* rightshift */
840 1, /* size (0 = byte, 1 = short, 2 = long) */
841 16, /* bitsize */
b34976b6 842 FALSE, /* pc_relative */
5bd4f169
AM
843 0, /* bitpos */
844 complain_overflow_dont, /* complain_on_overflow */
805fc799 845 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 846 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 847 FALSE, /* partial_inplace */
5bd4f169
AM
848 0, /* src_mask */
849 0xffff, /* dst_mask */
b34976b6 850 FALSE), /* pcrel_offset */
5bd4f169
AM
851
852 /* Like ADDR64, but may be unaligned. */
853 HOWTO (R_PPC64_UADDR64, /* type */
854 0, /* rightshift */
855 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
856 64, /* bitsize */
b34976b6 857 FALSE, /* pc_relative */
5bd4f169
AM
858 0, /* bitpos */
859 complain_overflow_dont, /* complain_on_overflow */
860 bfd_elf_generic_reloc, /* special_function */
861 "R_PPC64_UADDR64", /* name */
b34976b6 862 FALSE, /* partial_inplace */
5bd4f169 863 0, /* src_mask */
f5e87a1d 864 ONES (64), /* dst_mask */
b34976b6 865 FALSE), /* pcrel_offset */
5bd4f169
AM
866
867 /* 64-bit relative relocation. */
868 HOWTO (R_PPC64_REL64, /* type */
869 0, /* rightshift */
870 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
871 64, /* bitsize */
b34976b6 872 TRUE, /* pc_relative */
5bd4f169
AM
873 0, /* bitpos */
874 complain_overflow_dont, /* complain_on_overflow */
875 bfd_elf_generic_reloc, /* special_function */
876 "R_PPC64_REL64", /* name */
b34976b6 877 FALSE, /* partial_inplace */
5bd4f169 878 0, /* src_mask */
f5e87a1d 879 ONES (64), /* dst_mask */
b34976b6 880 TRUE), /* pcrel_offset */
5bd4f169 881
cedb70c5 882 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
883 HOWTO (R_PPC64_PLT64, /* type */
884 0, /* rightshift */
885 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
886 64, /* bitsize */
b34976b6 887 FALSE, /* pc_relative */
5bd4f169
AM
888 0, /* bitpos */
889 complain_overflow_dont, /* complain_on_overflow */
805fc799 890 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 891 "R_PPC64_PLT64", /* name */
b34976b6 892 FALSE, /* partial_inplace */
5bd4f169 893 0, /* src_mask */
f5e87a1d 894 ONES (64), /* dst_mask */
b34976b6 895 FALSE), /* pcrel_offset */
5bd4f169
AM
896
897 /* 64-bit PC relative relocation to the symbol's procedure linkage
898 table. */
899 /* FIXME: R_PPC64_PLTREL64 not supported. */
900 HOWTO (R_PPC64_PLTREL64, /* type */
901 0, /* rightshift */
902 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
903 64, /* bitsize */
b34976b6 904 TRUE, /* pc_relative */
5bd4f169
AM
905 0, /* bitpos */
906 complain_overflow_dont, /* complain_on_overflow */
805fc799 907 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 908 "R_PPC64_PLTREL64", /* name */
b34976b6 909 FALSE, /* partial_inplace */
5bd4f169 910 0, /* src_mask */
f5e87a1d 911 ONES (64), /* dst_mask */
b34976b6 912 TRUE), /* pcrel_offset */
5bd4f169
AM
913
914 /* 16 bit TOC-relative relocation. */
915
916 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
917 HOWTO (R_PPC64_TOC16, /* type */
918 0, /* rightshift */
919 1, /* size (0 = byte, 1 = short, 2 = long) */
920 16, /* bitsize */
b34976b6 921 FALSE, /* pc_relative */
5bd4f169
AM
922 0, /* bitpos */
923 complain_overflow_signed, /* complain_on_overflow */
805fc799 924 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 925 "R_PPC64_TOC16", /* name */
b34976b6 926 FALSE, /* partial_inplace */
5bd4f169
AM
927 0, /* src_mask */
928 0xffff, /* dst_mask */
b34976b6 929 FALSE), /* pcrel_offset */
5bd4f169
AM
930
931 /* 16 bit TOC-relative relocation without overflow. */
932
933 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
934 HOWTO (R_PPC64_TOC16_LO, /* type */
935 0, /* rightshift */
936 1, /* size (0 = byte, 1 = short, 2 = long) */
937 16, /* bitsize */
b34976b6 938 FALSE, /* pc_relative */
5bd4f169
AM
939 0, /* bitpos */
940 complain_overflow_dont, /* complain_on_overflow */
805fc799 941 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 942 "R_PPC64_TOC16_LO", /* name */
b34976b6 943 FALSE, /* partial_inplace */
5bd4f169
AM
944 0, /* src_mask */
945 0xffff, /* dst_mask */
b34976b6 946 FALSE), /* pcrel_offset */
5bd4f169
AM
947
948 /* 16 bit TOC-relative relocation, high 16 bits. */
949
950 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
951 HOWTO (R_PPC64_TOC16_HI, /* type */
952 16, /* rightshift */
953 1, /* size (0 = byte, 1 = short, 2 = long) */
954 16, /* bitsize */
b34976b6 955 FALSE, /* pc_relative */
5bd4f169
AM
956 0, /* bitpos */
957 complain_overflow_dont, /* complain_on_overflow */
805fc799 958 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 959 "R_PPC64_TOC16_HI", /* name */
b34976b6 960 FALSE, /* partial_inplace */
5bd4f169
AM
961 0, /* src_mask */
962 0xffff, /* dst_mask */
b34976b6 963 FALSE), /* pcrel_offset */
5bd4f169
AM
964
965 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
966 contents of the low 16 bits, treated as a signed number, is
967 negative. */
968
969 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
970 HOWTO (R_PPC64_TOC16_HA, /* type */
971 16, /* rightshift */
972 1, /* size (0 = byte, 1 = short, 2 = long) */
973 16, /* bitsize */
b34976b6 974 FALSE, /* pc_relative */
5bd4f169
AM
975 0, /* bitpos */
976 complain_overflow_dont, /* complain_on_overflow */
805fc799 977 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 978 "R_PPC64_TOC16_HA", /* name */
b34976b6 979 FALSE, /* partial_inplace */
5bd4f169
AM
980 0, /* src_mask */
981 0xffff, /* dst_mask */
b34976b6 982 FALSE), /* pcrel_offset */
5bd4f169
AM
983
984 /* 64-bit relocation; insert value of TOC base (.TOC.). */
985
986 /* R_PPC64_TOC 51 doubleword64 .TOC. */
987 HOWTO (R_PPC64_TOC, /* type */
988 0, /* rightshift */
989 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
990 64, /* bitsize */
b34976b6 991 FALSE, /* pc_relative */
5bd4f169
AM
992 0, /* bitpos */
993 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 994 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 995 "R_PPC64_TOC", /* name */
b34976b6 996 FALSE, /* partial_inplace */
5bd4f169 997 0, /* src_mask */
f5e87a1d 998 ONES (64), /* dst_mask */
b34976b6 999 FALSE), /* pcrel_offset */
5bd4f169
AM
1000
1001 /* Like R_PPC64_GOT16, but also informs the link editor that the
1002 value to relocate may (!) refer to a PLT entry which the link
1003 editor (a) may replace with the symbol value. If the link editor
1004 is unable to fully resolve the symbol, it may (b) create a PLT
1005 entry and store the address to the new PLT entry in the GOT.
1006 This permits lazy resolution of function symbols at run time.
1007 The link editor may also skip all of this and just (c) emit a
1008 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1009 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1010 HOWTO (R_PPC64_PLTGOT16, /* type */
1011 0, /* rightshift */
1012 1, /* size (0 = byte, 1 = short, 2 = long) */
1013 16, /* bitsize */
b34976b6 1014 FALSE, /* pc_relative */
5bd4f169
AM
1015 0, /* bitpos */
1016 complain_overflow_signed, /* complain_on_overflow */
805fc799 1017 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1018 "R_PPC64_PLTGOT16", /* name */
1019 FALSE, /* partial_inplace */
1020 0, /* src_mask */
1021 0xffff, /* dst_mask */
1022 FALSE), /* pcrel_offset */
1023
1024 /* Like R_PPC64_PLTGOT16, but without overflow. */
1025 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1026 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1027 0, /* rightshift */
1028 1, /* size (0 = byte, 1 = short, 2 = long) */
1029 16, /* bitsize */
1030 FALSE, /* pc_relative */
1031 0, /* bitpos */
1032 complain_overflow_dont, /* complain_on_overflow */
1033 ppc64_elf_unhandled_reloc, /* special_function */
1034 "R_PPC64_PLTGOT16_LO", /* name */
1035 FALSE, /* partial_inplace */
1036 0, /* src_mask */
1037 0xffff, /* dst_mask */
1038 FALSE), /* pcrel_offset */
1039
1040 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1041 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1042 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1043 16, /* rightshift */
1044 1, /* size (0 = byte, 1 = short, 2 = long) */
1045 16, /* bitsize */
1046 FALSE, /* pc_relative */
1047 0, /* bitpos */
1048 complain_overflow_dont, /* complain_on_overflow */
1049 ppc64_elf_unhandled_reloc, /* special_function */
1050 "R_PPC64_PLTGOT16_HI", /* name */
1051 FALSE, /* partial_inplace */
1052 0, /* src_mask */
1053 0xffff, /* dst_mask */
1054 FALSE), /* pcrel_offset */
1055
1056 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1057 1 if the contents of the low 16 bits, treated as a signed number,
1058 is negative. */
1059 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1060 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1061 16, /* rightshift */
1062 1, /* size (0 = byte, 1 = short, 2 = long) */
1063 16, /* bitsize */
1064 FALSE, /* pc_relative */
1065 0, /* bitpos */
1066 complain_overflow_dont,/* complain_on_overflow */
1067 ppc64_elf_unhandled_reloc, /* special_function */
1068 "R_PPC64_PLTGOT16_HA", /* name */
1069 FALSE, /* partial_inplace */
1070 0, /* src_mask */
1071 0xffff, /* dst_mask */
1072 FALSE), /* pcrel_offset */
1073
1074 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1075 HOWTO (R_PPC64_ADDR16_DS, /* type */
1076 0, /* rightshift */
1077 1, /* size (0 = byte, 1 = short, 2 = long) */
1078 16, /* bitsize */
1079 FALSE, /* pc_relative */
1080 0, /* bitpos */
1081 complain_overflow_bitfield, /* complain_on_overflow */
1082 bfd_elf_generic_reloc, /* special_function */
1083 "R_PPC64_ADDR16_DS", /* name */
1084 FALSE, /* partial_inplace */
1085 0, /* src_mask */
1086 0xfffc, /* dst_mask */
1087 FALSE), /* pcrel_offset */
1088
1089 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1090 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1091 0, /* rightshift */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1093 16, /* bitsize */
1094 FALSE, /* pc_relative */
1095 0, /* bitpos */
1096 complain_overflow_dont,/* complain_on_overflow */
1097 bfd_elf_generic_reloc, /* special_function */
1098 "R_PPC64_ADDR16_LO_DS",/* name */
1099 FALSE, /* partial_inplace */
1100 0, /* src_mask */
1101 0xfffc, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1103
1104 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1105 HOWTO (R_PPC64_GOT16_DS, /* type */
1106 0, /* rightshift */
1107 1, /* size (0 = byte, 1 = short, 2 = long) */
1108 16, /* bitsize */
1109 FALSE, /* pc_relative */
1110 0, /* bitpos */
1111 complain_overflow_signed, /* complain_on_overflow */
1112 ppc64_elf_unhandled_reloc, /* special_function */
1113 "R_PPC64_GOT16_DS", /* name */
1114 FALSE, /* partial_inplace */
1115 0, /* src_mask */
1116 0xfffc, /* dst_mask */
1117 FALSE), /* pcrel_offset */
1118
1119 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1120 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1121 0, /* rightshift */
1122 1, /* size (0 = byte, 1 = short, 2 = long) */
1123 16, /* bitsize */
1124 FALSE, /* pc_relative */
1125 0, /* bitpos */
1126 complain_overflow_dont, /* complain_on_overflow */
1127 ppc64_elf_unhandled_reloc, /* special_function */
1128 "R_PPC64_GOT16_LO_DS", /* name */
1129 FALSE, /* partial_inplace */
1130 0, /* src_mask */
1131 0xfffc, /* dst_mask */
1132 FALSE), /* pcrel_offset */
1133
1134 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1135 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1136 0, /* rightshift */
1137 1, /* size (0 = byte, 1 = short, 2 = long) */
1138 16, /* bitsize */
1139 FALSE, /* pc_relative */
1140 0, /* bitpos */
1141 complain_overflow_dont, /* complain_on_overflow */
1142 ppc64_elf_unhandled_reloc, /* special_function */
1143 "R_PPC64_PLT16_LO_DS", /* name */
1144 FALSE, /* partial_inplace */
1145 0, /* src_mask */
1146 0xfffc, /* dst_mask */
1147 FALSE), /* pcrel_offset */
1148
1149 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1150 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1151 0, /* rightshift */
1152 1, /* size (0 = byte, 1 = short, 2 = long) */
1153 16, /* bitsize */
1154 FALSE, /* pc_relative */
1155 0, /* bitpos */
1156 complain_overflow_bitfield, /* complain_on_overflow */
1157 ppc64_elf_sectoff_reloc, /* special_function */
1158 "R_PPC64_SECTOFF_DS", /* name */
1159 FALSE, /* partial_inplace */
1160 0, /* src_mask */
1161 0xfffc, /* dst_mask */
1162 FALSE), /* pcrel_offset */
1163
1164 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1165 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1166 0, /* rightshift */
1167 1, /* size (0 = byte, 1 = short, 2 = long) */
1168 16, /* bitsize */
1169 FALSE, /* pc_relative */
1170 0, /* bitpos */
1171 complain_overflow_dont, /* complain_on_overflow */
1172 ppc64_elf_sectoff_reloc, /* special_function */
1173 "R_PPC64_SECTOFF_LO_DS",/* name */
1174 FALSE, /* partial_inplace */
1175 0, /* src_mask */
1176 0xfffc, /* dst_mask */
1177 FALSE), /* pcrel_offset */
1178
1179 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1180 HOWTO (R_PPC64_TOC16_DS, /* type */
1181 0, /* rightshift */
1182 1, /* size (0 = byte, 1 = short, 2 = long) */
1183 16, /* bitsize */
1184 FALSE, /* pc_relative */
1185 0, /* bitpos */
1186 complain_overflow_signed, /* complain_on_overflow */
1187 ppc64_elf_toc_reloc, /* special_function */
1188 "R_PPC64_TOC16_DS", /* name */
1189 FALSE, /* partial_inplace */
1190 0, /* src_mask */
1191 0xfffc, /* dst_mask */
1192 FALSE), /* pcrel_offset */
1193
1194 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1195 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1196 0, /* rightshift */
1197 1, /* size (0 = byte, 1 = short, 2 = long) */
1198 16, /* bitsize */
1199 FALSE, /* pc_relative */
1200 0, /* bitpos */
1201 complain_overflow_dont, /* complain_on_overflow */
1202 ppc64_elf_toc_reloc, /* special_function */
1203 "R_PPC64_TOC16_LO_DS", /* name */
1204 FALSE, /* partial_inplace */
1205 0, /* src_mask */
1206 0xfffc, /* dst_mask */
1207 FALSE), /* pcrel_offset */
1208
1209 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1210 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
6bfdb61b 1211 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
411e1bfb
AM
1212 0, /* rightshift */
1213 1, /* size (0 = byte, 1 = short, 2 = long) */
1214 16, /* bitsize */
1215 FALSE, /* pc_relative */
1216 0, /* bitpos */
1217 complain_overflow_signed, /* complain_on_overflow */
1218 ppc64_elf_unhandled_reloc, /* special_function */
1219 "R_PPC64_PLTGOT16_DS", /* name */
1220 FALSE, /* partial_inplace */
1221 0, /* src_mask */
1222 0xfffc, /* dst_mask */
1223 FALSE), /* pcrel_offset */
1224
1225 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1226 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1227 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1228 0, /* rightshift */
1229 1, /* size (0 = byte, 1 = short, 2 = long) */
1230 16, /* bitsize */
1231 FALSE, /* pc_relative */
1232 0, /* bitpos */
1233 complain_overflow_dont, /* complain_on_overflow */
1234 ppc64_elf_unhandled_reloc, /* special_function */
1235 "R_PPC64_PLTGOT16_LO_DS",/* name */
1236 FALSE, /* partial_inplace */
1237 0, /* src_mask */
1238 0xfffc, /* dst_mask */
1239 FALSE), /* pcrel_offset */
1240
727fc41e 1241 /* Marker relocs for TLS. */
411e1bfb
AM
1242 HOWTO (R_PPC64_TLS,
1243 0, /* rightshift */
1244 2, /* size (0 = byte, 1 = short, 2 = long) */
1245 32, /* bitsize */
1246 FALSE, /* pc_relative */
1247 0, /* bitpos */
1248 complain_overflow_dont, /* complain_on_overflow */
1249 bfd_elf_generic_reloc, /* special_function */
1250 "R_PPC64_TLS", /* name */
1251 FALSE, /* partial_inplace */
1252 0, /* src_mask */
1253 0, /* dst_mask */
1254 FALSE), /* pcrel_offset */
1255
727fc41e
AM
1256 HOWTO (R_PPC64_TLSGD,
1257 0, /* rightshift */
1258 2, /* size (0 = byte, 1 = short, 2 = long) */
1259 32, /* bitsize */
1260 FALSE, /* pc_relative */
1261 0, /* bitpos */
1262 complain_overflow_dont, /* complain_on_overflow */
1263 bfd_elf_generic_reloc, /* special_function */
1264 "R_PPC64_TLSGD", /* name */
1265 FALSE, /* partial_inplace */
1266 0, /* src_mask */
1267 0, /* dst_mask */
1268 FALSE), /* pcrel_offset */
1269
1270 HOWTO (R_PPC64_TLSLD,
1271 0, /* rightshift */
1272 2, /* size (0 = byte, 1 = short, 2 = long) */
1273 32, /* bitsize */
1274 FALSE, /* pc_relative */
1275 0, /* bitpos */
1276 complain_overflow_dont, /* complain_on_overflow */
1277 bfd_elf_generic_reloc, /* special_function */
1278 "R_PPC64_TLSLD", /* name */
1279 FALSE, /* partial_inplace */
1280 0, /* src_mask */
1281 0, /* dst_mask */
1282 FALSE), /* pcrel_offset */
1283
411e1bfb
AM
1284 /* Computes the load module index of the load module that contains the
1285 definition of its TLS sym. */
1286 HOWTO (R_PPC64_DTPMOD64,
1287 0, /* rightshift */
1288 4, /* size (0 = byte, 1 = short, 2 = long) */
1289 64, /* bitsize */
1290 FALSE, /* pc_relative */
1291 0, /* bitpos */
1292 complain_overflow_dont, /* complain_on_overflow */
1293 ppc64_elf_unhandled_reloc, /* special_function */
1294 "R_PPC64_DTPMOD64", /* name */
1295 FALSE, /* partial_inplace */
1296 0, /* src_mask */
1297 ONES (64), /* dst_mask */
1298 FALSE), /* pcrel_offset */
1299
1300 /* Computes a dtv-relative displacement, the difference between the value
1301 of sym+add and the base address of the thread-local storage block that
1302 contains the definition of sym, minus 0x8000. */
1303 HOWTO (R_PPC64_DTPREL64,
1304 0, /* rightshift */
1305 4, /* size (0 = byte, 1 = short, 2 = long) */
1306 64, /* bitsize */
1307 FALSE, /* pc_relative */
1308 0, /* bitpos */
1309 complain_overflow_dont, /* complain_on_overflow */
1310 ppc64_elf_unhandled_reloc, /* special_function */
1311 "R_PPC64_DTPREL64", /* name */
1312 FALSE, /* partial_inplace */
1313 0, /* src_mask */
1314 ONES (64), /* dst_mask */
1315 FALSE), /* pcrel_offset */
1316
1317 /* A 16 bit dtprel reloc. */
1318 HOWTO (R_PPC64_DTPREL16,
1319 0, /* rightshift */
1320 1, /* size (0 = byte, 1 = short, 2 = long) */
1321 16, /* bitsize */
1322 FALSE, /* pc_relative */
1323 0, /* bitpos */
1324 complain_overflow_signed, /* complain_on_overflow */
1325 ppc64_elf_unhandled_reloc, /* special_function */
1326 "R_PPC64_DTPREL16", /* name */
1327 FALSE, /* partial_inplace */
1328 0, /* src_mask */
1329 0xffff, /* dst_mask */
1330 FALSE), /* pcrel_offset */
1331
1332 /* Like DTPREL16, but no overflow. */
1333 HOWTO (R_PPC64_DTPREL16_LO,
1334 0, /* rightshift */
1335 1, /* size (0 = byte, 1 = short, 2 = long) */
1336 16, /* bitsize */
1337 FALSE, /* pc_relative */
1338 0, /* bitpos */
1339 complain_overflow_dont, /* complain_on_overflow */
1340 ppc64_elf_unhandled_reloc, /* special_function */
1341 "R_PPC64_DTPREL16_LO", /* name */
1342 FALSE, /* partial_inplace */
1343 0, /* src_mask */
1344 0xffff, /* dst_mask */
1345 FALSE), /* pcrel_offset */
1346
1347 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1348 HOWTO (R_PPC64_DTPREL16_HI,
1349 16, /* rightshift */
1350 1, /* size (0 = byte, 1 = short, 2 = long) */
1351 16, /* bitsize */
1352 FALSE, /* pc_relative */
1353 0, /* bitpos */
1354 complain_overflow_dont, /* complain_on_overflow */
1355 ppc64_elf_unhandled_reloc, /* special_function */
1356 "R_PPC64_DTPREL16_HI", /* name */
1357 FALSE, /* partial_inplace */
1358 0, /* src_mask */
1359 0xffff, /* dst_mask */
1360 FALSE), /* pcrel_offset */
1361
1362 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1363 HOWTO (R_PPC64_DTPREL16_HA,
1364 16, /* rightshift */
1365 1, /* size (0 = byte, 1 = short, 2 = long) */
1366 16, /* bitsize */
1367 FALSE, /* pc_relative */
1368 0, /* bitpos */
1369 complain_overflow_dont, /* complain_on_overflow */
1370 ppc64_elf_unhandled_reloc, /* special_function */
1371 "R_PPC64_DTPREL16_HA", /* name */
1372 FALSE, /* partial_inplace */
1373 0, /* src_mask */
1374 0xffff, /* dst_mask */
1375 FALSE), /* pcrel_offset */
1376
1377 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1378 HOWTO (R_PPC64_DTPREL16_HIGHER,
1379 32, /* rightshift */
1380 1, /* size (0 = byte, 1 = short, 2 = long) */
1381 16, /* bitsize */
1382 FALSE, /* pc_relative */
1383 0, /* bitpos */
1384 complain_overflow_dont, /* complain_on_overflow */
1385 ppc64_elf_unhandled_reloc, /* special_function */
1386 "R_PPC64_DTPREL16_HIGHER", /* name */
1387 FALSE, /* partial_inplace */
1388 0, /* src_mask */
1389 0xffff, /* dst_mask */
1390 FALSE), /* pcrel_offset */
1391
1392 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1393 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1394 32, /* rightshift */
1395 1, /* size (0 = byte, 1 = short, 2 = long) */
1396 16, /* bitsize */
1397 FALSE, /* pc_relative */
1398 0, /* bitpos */
1399 complain_overflow_dont, /* complain_on_overflow */
1400 ppc64_elf_unhandled_reloc, /* special_function */
1401 "R_PPC64_DTPREL16_HIGHERA", /* name */
1402 FALSE, /* partial_inplace */
1403 0, /* src_mask */
1404 0xffff, /* dst_mask */
1405 FALSE), /* pcrel_offset */
1406
1407 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1408 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1409 48, /* rightshift */
1410 1, /* size (0 = byte, 1 = short, 2 = long) */
1411 16, /* bitsize */
1412 FALSE, /* pc_relative */
1413 0, /* bitpos */
1414 complain_overflow_dont, /* complain_on_overflow */
1415 ppc64_elf_unhandled_reloc, /* special_function */
1416 "R_PPC64_DTPREL16_HIGHEST", /* name */
1417 FALSE, /* partial_inplace */
1418 0, /* src_mask */
1419 0xffff, /* dst_mask */
1420 FALSE), /* pcrel_offset */
1421
1422 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1423 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1424 48, /* rightshift */
1425 1, /* size (0 = byte, 1 = short, 2 = long) */
1426 16, /* bitsize */
1427 FALSE, /* pc_relative */
1428 0, /* bitpos */
1429 complain_overflow_dont, /* complain_on_overflow */
1430 ppc64_elf_unhandled_reloc, /* special_function */
1431 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1432 FALSE, /* partial_inplace */
1433 0, /* src_mask */
1434 0xffff, /* dst_mask */
1435 FALSE), /* pcrel_offset */
1436
1437 /* Like DTPREL16, but for insns with a DS field. */
1438 HOWTO (R_PPC64_DTPREL16_DS,
1439 0, /* rightshift */
1440 1, /* size (0 = byte, 1 = short, 2 = long) */
1441 16, /* bitsize */
1442 FALSE, /* pc_relative */
1443 0, /* bitpos */
1444 complain_overflow_signed, /* complain_on_overflow */
1445 ppc64_elf_unhandled_reloc, /* special_function */
1446 "R_PPC64_DTPREL16_DS", /* name */
1447 FALSE, /* partial_inplace */
1448 0, /* src_mask */
1449 0xfffc, /* dst_mask */
1450 FALSE), /* pcrel_offset */
1451
1452 /* Like DTPREL16_DS, but no overflow. */
1453 HOWTO (R_PPC64_DTPREL16_LO_DS,
1454 0, /* rightshift */
1455 1, /* size (0 = byte, 1 = short, 2 = long) */
1456 16, /* bitsize */
1457 FALSE, /* pc_relative */
1458 0, /* bitpos */
1459 complain_overflow_dont, /* complain_on_overflow */
1460 ppc64_elf_unhandled_reloc, /* special_function */
1461 "R_PPC64_DTPREL16_LO_DS", /* name */
1462 FALSE, /* partial_inplace */
1463 0, /* src_mask */
1464 0xfffc, /* dst_mask */
1465 FALSE), /* pcrel_offset */
1466
1467 /* Computes a tp-relative displacement, the difference between the value of
1468 sym+add and the value of the thread pointer (r13). */
1469 HOWTO (R_PPC64_TPREL64,
1470 0, /* rightshift */
1471 4, /* size (0 = byte, 1 = short, 2 = long) */
1472 64, /* bitsize */
1473 FALSE, /* pc_relative */
1474 0, /* bitpos */
1475 complain_overflow_dont, /* complain_on_overflow */
1476 ppc64_elf_unhandled_reloc, /* special_function */
1477 "R_PPC64_TPREL64", /* name */
1478 FALSE, /* partial_inplace */
1479 0, /* src_mask */
1480 ONES (64), /* dst_mask */
1481 FALSE), /* pcrel_offset */
1482
1483 /* A 16 bit tprel reloc. */
1484 HOWTO (R_PPC64_TPREL16,
1485 0, /* rightshift */
1486 1, /* size (0 = byte, 1 = short, 2 = long) */
1487 16, /* bitsize */
1488 FALSE, /* pc_relative */
1489 0, /* bitpos */
1490 complain_overflow_signed, /* complain_on_overflow */
1491 ppc64_elf_unhandled_reloc, /* special_function */
1492 "R_PPC64_TPREL16", /* name */
1493 FALSE, /* partial_inplace */
1494 0, /* src_mask */
1495 0xffff, /* dst_mask */
1496 FALSE), /* pcrel_offset */
1497
1498 /* Like TPREL16, but no overflow. */
1499 HOWTO (R_PPC64_TPREL16_LO,
1500 0, /* rightshift */
1501 1, /* size (0 = byte, 1 = short, 2 = long) */
1502 16, /* bitsize */
1503 FALSE, /* pc_relative */
1504 0, /* bitpos */
1505 complain_overflow_dont, /* complain_on_overflow */
1506 ppc64_elf_unhandled_reloc, /* special_function */
1507 "R_PPC64_TPREL16_LO", /* name */
1508 FALSE, /* partial_inplace */
1509 0, /* src_mask */
1510 0xffff, /* dst_mask */
1511 FALSE), /* pcrel_offset */
1512
1513 /* Like TPREL16_LO, but next higher group of 16 bits. */
1514 HOWTO (R_PPC64_TPREL16_HI,
1515 16, /* rightshift */
1516 1, /* size (0 = byte, 1 = short, 2 = long) */
1517 16, /* bitsize */
1518 FALSE, /* pc_relative */
1519 0, /* bitpos */
1520 complain_overflow_dont, /* complain_on_overflow */
1521 ppc64_elf_unhandled_reloc, /* special_function */
1522 "R_PPC64_TPREL16_HI", /* name */
1523 FALSE, /* partial_inplace */
1524 0, /* src_mask */
1525 0xffff, /* dst_mask */
1526 FALSE), /* pcrel_offset */
1527
1528 /* Like TPREL16_HI, but adjust for low 16 bits. */
1529 HOWTO (R_PPC64_TPREL16_HA,
1530 16, /* rightshift */
1531 1, /* size (0 = byte, 1 = short, 2 = long) */
1532 16, /* bitsize */
1533 FALSE, /* pc_relative */
1534 0, /* bitpos */
1535 complain_overflow_dont, /* complain_on_overflow */
1536 ppc64_elf_unhandled_reloc, /* special_function */
1537 "R_PPC64_TPREL16_HA", /* name */
1538 FALSE, /* partial_inplace */
1539 0, /* src_mask */
1540 0xffff, /* dst_mask */
1541 FALSE), /* pcrel_offset */
1542
1543 /* Like TPREL16_HI, but next higher group of 16 bits. */
1544 HOWTO (R_PPC64_TPREL16_HIGHER,
1545 32, /* rightshift */
1546 1, /* size (0 = byte, 1 = short, 2 = long) */
1547 16, /* bitsize */
1548 FALSE, /* pc_relative */
1549 0, /* bitpos */
1550 complain_overflow_dont, /* complain_on_overflow */
1551 ppc64_elf_unhandled_reloc, /* special_function */
1552 "R_PPC64_TPREL16_HIGHER", /* name */
1553 FALSE, /* partial_inplace */
1554 0, /* src_mask */
1555 0xffff, /* dst_mask */
1556 FALSE), /* pcrel_offset */
1557
1558 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1559 HOWTO (R_PPC64_TPREL16_HIGHERA,
1560 32, /* rightshift */
1561 1, /* size (0 = byte, 1 = short, 2 = long) */
1562 16, /* bitsize */
1563 FALSE, /* pc_relative */
1564 0, /* bitpos */
1565 complain_overflow_dont, /* complain_on_overflow */
1566 ppc64_elf_unhandled_reloc, /* special_function */
1567 "R_PPC64_TPREL16_HIGHERA", /* name */
1568 FALSE, /* partial_inplace */
1569 0, /* src_mask */
1570 0xffff, /* dst_mask */
1571 FALSE), /* pcrel_offset */
1572
1573 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1574 HOWTO (R_PPC64_TPREL16_HIGHEST,
1575 48, /* rightshift */
1576 1, /* size (0 = byte, 1 = short, 2 = long) */
1577 16, /* bitsize */
1578 FALSE, /* pc_relative */
1579 0, /* bitpos */
1580 complain_overflow_dont, /* complain_on_overflow */
1581 ppc64_elf_unhandled_reloc, /* special_function */
1582 "R_PPC64_TPREL16_HIGHEST", /* name */
1583 FALSE, /* partial_inplace */
1584 0, /* src_mask */
1585 0xffff, /* dst_mask */
1586 FALSE), /* pcrel_offset */
1587
1588 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1589 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1590 48, /* rightshift */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize */
1593 FALSE, /* pc_relative */
1594 0, /* bitpos */
1595 complain_overflow_dont, /* complain_on_overflow */
1596 ppc64_elf_unhandled_reloc, /* special_function */
1597 "R_PPC64_TPREL16_HIGHESTA", /* name */
1598 FALSE, /* partial_inplace */
1599 0, /* src_mask */
1600 0xffff, /* dst_mask */
1601 FALSE), /* pcrel_offset */
1602
1603 /* Like TPREL16, but for insns with a DS field. */
1604 HOWTO (R_PPC64_TPREL16_DS,
1605 0, /* rightshift */
1606 1, /* size (0 = byte, 1 = short, 2 = long) */
1607 16, /* bitsize */
1608 FALSE, /* pc_relative */
1609 0, /* bitpos */
1610 complain_overflow_signed, /* complain_on_overflow */
1611 ppc64_elf_unhandled_reloc, /* special_function */
1612 "R_PPC64_TPREL16_DS", /* name */
1613 FALSE, /* partial_inplace */
1614 0, /* src_mask */
1615 0xfffc, /* dst_mask */
1616 FALSE), /* pcrel_offset */
1617
1618 /* Like TPREL16_DS, but no overflow. */
1619 HOWTO (R_PPC64_TPREL16_LO_DS,
1620 0, /* rightshift */
1621 1, /* size (0 = byte, 1 = short, 2 = long) */
1622 16, /* bitsize */
1623 FALSE, /* pc_relative */
1624 0, /* bitpos */
1625 complain_overflow_dont, /* complain_on_overflow */
1626 ppc64_elf_unhandled_reloc, /* special_function */
1627 "R_PPC64_TPREL16_LO_DS", /* name */
1628 FALSE, /* partial_inplace */
1629 0, /* src_mask */
1630 0xfffc, /* dst_mask */
1631 FALSE), /* pcrel_offset */
1632
1633 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1634 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1635 to the first entry relative to the TOC base (r2). */
1636 HOWTO (R_PPC64_GOT_TLSGD16,
1637 0, /* rightshift */
1638 1, /* size (0 = byte, 1 = short, 2 = long) */
1639 16, /* bitsize */
1640 FALSE, /* pc_relative */
1641 0, /* bitpos */
1642 complain_overflow_signed, /* complain_on_overflow */
1643 ppc64_elf_unhandled_reloc, /* special_function */
1644 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1645 FALSE, /* partial_inplace */
5bd4f169
AM
1646 0, /* src_mask */
1647 0xffff, /* dst_mask */
b34976b6 1648 FALSE), /* pcrel_offset */
5bd4f169 1649
411e1bfb
AM
1650 /* Like GOT_TLSGD16, but no overflow. */
1651 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1652 0, /* rightshift */
1653 1, /* size (0 = byte, 1 = short, 2 = long) */
1654 16, /* bitsize */
b34976b6 1655 FALSE, /* pc_relative */
5bd4f169
AM
1656 0, /* bitpos */
1657 complain_overflow_dont, /* complain_on_overflow */
805fc799 1658 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1659 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1660 FALSE, /* partial_inplace */
5bd4f169
AM
1661 0, /* src_mask */
1662 0xffff, /* dst_mask */
b34976b6 1663 FALSE), /* pcrel_offset */
5bd4f169 1664
411e1bfb
AM
1665 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1666 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1667 16, /* rightshift */
1668 1, /* size (0 = byte, 1 = short, 2 = long) */
1669 16, /* bitsize */
b34976b6 1670 FALSE, /* pc_relative */
5bd4f169
AM
1671 0, /* bitpos */
1672 complain_overflow_dont, /* complain_on_overflow */
805fc799 1673 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1674 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1675 FALSE, /* partial_inplace */
5bd4f169
AM
1676 0, /* src_mask */
1677 0xffff, /* dst_mask */
b34976b6 1678 FALSE), /* pcrel_offset */
5bd4f169 1679
411e1bfb
AM
1680 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1681 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1682 16, /* rightshift */
1683 1, /* size (0 = byte, 1 = short, 2 = long) */
1684 16, /* bitsize */
b34976b6 1685 FALSE, /* pc_relative */
5bd4f169 1686 0, /* bitpos */
411e1bfb 1687 complain_overflow_dont, /* complain_on_overflow */
805fc799 1688 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1689 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1690 FALSE, /* partial_inplace */
5bd4f169
AM
1691 0, /* src_mask */
1692 0xffff, /* dst_mask */
b34976b6 1693 FALSE), /* pcrel_offset */
5bd4f169 1694
411e1bfb
AM
1695 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1696 with values (sym+add)@dtpmod and zero, and computes the offset to the
1697 first entry relative to the TOC base (r2). */
1698 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1699 0, /* rightshift */
1700 1, /* size (0 = byte, 1 = short, 2 = long) */
1701 16, /* bitsize */
b34976b6 1702 FALSE, /* pc_relative */
5bd4f169 1703 0, /* bitpos */
411e1bfb
AM
1704 complain_overflow_signed, /* complain_on_overflow */
1705 ppc64_elf_unhandled_reloc, /* special_function */
1706 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1707 FALSE, /* partial_inplace */
d006db6c 1708 0, /* src_mask */
411e1bfb 1709 0xffff, /* dst_mask */
b34976b6 1710 FALSE), /* pcrel_offset */
5bd4f169 1711
411e1bfb
AM
1712 /* Like GOT_TLSLD16, but no overflow. */
1713 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1714 0, /* rightshift */
1715 1, /* size (0 = byte, 1 = short, 2 = long) */
1716 16, /* bitsize */
b34976b6 1717 FALSE, /* pc_relative */
5bd4f169 1718 0, /* bitpos */
411e1bfb
AM
1719 complain_overflow_dont, /* complain_on_overflow */
1720 ppc64_elf_unhandled_reloc, /* special_function */
1721 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1722 FALSE, /* partial_inplace */
d006db6c 1723 0, /* src_mask */
411e1bfb 1724 0xffff, /* dst_mask */
b34976b6 1725 FALSE), /* pcrel_offset */
5bd4f169 1726
411e1bfb
AM
1727 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1728 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1729 16, /* rightshift */
5bd4f169
AM
1730 1, /* size (0 = byte, 1 = short, 2 = long) */
1731 16, /* bitsize */
b34976b6 1732 FALSE, /* pc_relative */
5bd4f169 1733 0, /* bitpos */
411e1bfb 1734 complain_overflow_dont, /* complain_on_overflow */
805fc799 1735 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1736 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1737 FALSE, /* partial_inplace */
d006db6c 1738 0, /* src_mask */
411e1bfb 1739 0xffff, /* dst_mask */
b34976b6 1740 FALSE), /* pcrel_offset */
5bd4f169 1741
411e1bfb
AM
1742 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1743 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1744 16, /* rightshift */
5bd4f169
AM
1745 1, /* size (0 = byte, 1 = short, 2 = long) */
1746 16, /* bitsize */
b34976b6 1747 FALSE, /* pc_relative */
5bd4f169
AM
1748 0, /* bitpos */
1749 complain_overflow_dont, /* complain_on_overflow */
805fc799 1750 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1751 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1752 FALSE, /* partial_inplace */
d006db6c 1753 0, /* src_mask */
411e1bfb 1754 0xffff, /* dst_mask */
b34976b6 1755 FALSE), /* pcrel_offset */
5bd4f169 1756
411e1bfb
AM
1757 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1758 the offset to the entry relative to the TOC base (r2). */
1759 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1760 0, /* rightshift */
1761 1, /* size (0 = byte, 1 = short, 2 = long) */
1762 16, /* bitsize */
b34976b6 1763 FALSE, /* pc_relative */
5bd4f169 1764 0, /* bitpos */
411e1bfb 1765 complain_overflow_signed, /* complain_on_overflow */
805fc799 1766 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1767 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1768 FALSE, /* partial_inplace */
d006db6c 1769 0, /* src_mask */
5bd4f169 1770 0xfffc, /* dst_mask */
b34976b6 1771 FALSE), /* pcrel_offset */
5bd4f169 1772
411e1bfb
AM
1773 /* Like GOT_DTPREL16_DS, but no overflow. */
1774 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1775 0, /* rightshift */
c061c2d8
AM
1776 1, /* size (0 = byte, 1 = short, 2 = long) */
1777 16, /* bitsize */
b34976b6 1778 FALSE, /* pc_relative */
5bd4f169 1779 0, /* bitpos */
411e1bfb
AM
1780 complain_overflow_dont, /* complain_on_overflow */
1781 ppc64_elf_unhandled_reloc, /* special_function */
1782 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1783 FALSE, /* partial_inplace */
d006db6c 1784 0, /* src_mask */
c061c2d8 1785 0xfffc, /* dst_mask */
b34976b6 1786 FALSE), /* pcrel_offset */
5bd4f169 1787
411e1bfb
AM
1788 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1789 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1790 16, /* rightshift */
5bd4f169
AM
1791 1, /* size (0 = byte, 1 = short, 2 = long) */
1792 16, /* bitsize */
b34976b6 1793 FALSE, /* pc_relative */
5bd4f169
AM
1794 0, /* bitpos */
1795 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1796 ppc64_elf_unhandled_reloc, /* special_function */
1797 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1798 FALSE, /* partial_inplace */
d006db6c 1799 0, /* src_mask */
411e1bfb 1800 0xffff, /* dst_mask */
b34976b6 1801 FALSE), /* pcrel_offset */
5bd4f169 1802
411e1bfb
AM
1803 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1804 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1805 16, /* rightshift */
1806 1, /* size (0 = byte, 1 = short, 2 = long) */
1807 16, /* bitsize */
1808 FALSE, /* pc_relative */
1809 0, /* bitpos */
1810 complain_overflow_dont, /* complain_on_overflow */
1811 ppc64_elf_unhandled_reloc, /* special_function */
1812 "R_PPC64_GOT_DTPREL16_HA", /* name */
1813 FALSE, /* partial_inplace */
1814 0, /* src_mask */
1815 0xffff, /* dst_mask */
1816 FALSE), /* pcrel_offset */
1817
1818 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1819 offset to the entry relative to the TOC base (r2). */
1820 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1821 0, /* rightshift */
1822 1, /* size (0 = byte, 1 = short, 2 = long) */
1823 16, /* bitsize */
b34976b6 1824 FALSE, /* pc_relative */
5bd4f169
AM
1825 0, /* bitpos */
1826 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1827 ppc64_elf_unhandled_reloc, /* special_function */
1828 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1829 FALSE, /* partial_inplace */
d006db6c 1830 0, /* src_mask */
ad8e1ba5 1831 0xfffc, /* dst_mask */
b34976b6 1832 FALSE), /* pcrel_offset */
5bd4f169 1833
411e1bfb
AM
1834 /* Like GOT_TPREL16_DS, but no overflow. */
1835 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1836 0, /* rightshift */
1837 1, /* size (0 = byte, 1 = short, 2 = long) */
1838 16, /* bitsize */
b34976b6 1839 FALSE, /* pc_relative */
5bd4f169
AM
1840 0, /* bitpos */
1841 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1842 ppc64_elf_unhandled_reloc, /* special_function */
1843 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1844 FALSE, /* partial_inplace */
d006db6c 1845 0, /* src_mask */
ad8e1ba5 1846 0xfffc, /* dst_mask */
b34976b6 1847 FALSE), /* pcrel_offset */
5bd4f169 1848
411e1bfb
AM
1849 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1850 HOWTO (R_PPC64_GOT_TPREL16_HI,
1851 16, /* rightshift */
5bd4f169
AM
1852 1, /* size (0 = byte, 1 = short, 2 = long) */
1853 16, /* bitsize */
b34976b6 1854 FALSE, /* pc_relative */
5bd4f169 1855 0, /* bitpos */
411e1bfb 1856 complain_overflow_dont, /* complain_on_overflow */
805fc799 1857 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1858 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1859 FALSE, /* partial_inplace */
d006db6c 1860 0, /* src_mask */
411e1bfb 1861 0xffff, /* dst_mask */
b34976b6 1862 FALSE), /* pcrel_offset */
5bd4f169 1863
411e1bfb
AM
1864 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1865 HOWTO (R_PPC64_GOT_TPREL16_HA,
1866 16, /* rightshift */
5bd4f169
AM
1867 1, /* size (0 = byte, 1 = short, 2 = long) */
1868 16, /* bitsize */
b34976b6 1869 FALSE, /* pc_relative */
5bd4f169
AM
1870 0, /* bitpos */
1871 complain_overflow_dont, /* complain_on_overflow */
805fc799 1872 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1873 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1874 FALSE, /* partial_inplace */
d006db6c 1875 0, /* src_mask */
411e1bfb 1876 0xffff, /* dst_mask */
b34976b6 1877 FALSE), /* pcrel_offset */
5bd4f169 1878
25f23106
AM
1879 HOWTO (R_PPC64_JMP_IREL, /* type */
1880 0, /* rightshift */
1881 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1882 0, /* bitsize */
1883 FALSE, /* pc_relative */
1884 0, /* bitpos */
1885 complain_overflow_dont, /* complain_on_overflow */
1886 ppc64_elf_unhandled_reloc, /* special_function */
1887 "R_PPC64_JMP_IREL", /* name */
1888 FALSE, /* partial_inplace */
1889 0, /* src_mask */
1890 0, /* dst_mask */
1891 FALSE), /* pcrel_offset */
1892
e054468f
AM
1893 HOWTO (R_PPC64_IRELATIVE, /* type */
1894 0, /* rightshift */
1895 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1896 64, /* bitsize */
1897 FALSE, /* pc_relative */
1898 0, /* bitpos */
1899 complain_overflow_dont, /* complain_on_overflow */
1900 bfd_elf_generic_reloc, /* special_function */
1901 "R_PPC64_IRELATIVE", /* name */
1902 FALSE, /* partial_inplace */
1903 0, /* src_mask */
1904 ONES (64), /* dst_mask */
1905 FALSE), /* pcrel_offset */
1906
25f23106
AM
1907 /* A 16 bit relative relocation. */
1908 HOWTO (R_PPC64_REL16, /* type */
1909 0, /* rightshift */
1910 1, /* size (0 = byte, 1 = short, 2 = long) */
1911 16, /* bitsize */
1912 TRUE, /* pc_relative */
1913 0, /* bitpos */
1914 complain_overflow_bitfield, /* complain_on_overflow */
1915 bfd_elf_generic_reloc, /* special_function */
1916 "R_PPC64_REL16", /* name */
1917 FALSE, /* partial_inplace */
1918 0, /* src_mask */
1919 0xffff, /* dst_mask */
1920 TRUE), /* pcrel_offset */
1921
1922 /* A 16 bit relative relocation without overflow. */
1923 HOWTO (R_PPC64_REL16_LO, /* type */
1924 0, /* rightshift */
1925 1, /* size (0 = byte, 1 = short, 2 = long) */
1926 16, /* bitsize */
1927 TRUE, /* pc_relative */
1928 0, /* bitpos */
1929 complain_overflow_dont,/* complain_on_overflow */
1930 bfd_elf_generic_reloc, /* special_function */
1931 "R_PPC64_REL16_LO", /* name */
1932 FALSE, /* partial_inplace */
1933 0, /* src_mask */
1934 0xffff, /* dst_mask */
1935 TRUE), /* pcrel_offset */
1936
1937 /* The high order 16 bits of a relative address. */
1938 HOWTO (R_PPC64_REL16_HI, /* type */
1939 16, /* rightshift */
1940 1, /* size (0 = byte, 1 = short, 2 = long) */
1941 16, /* bitsize */
1942 TRUE, /* pc_relative */
1943 0, /* bitpos */
1944 complain_overflow_dont, /* complain_on_overflow */
1945 bfd_elf_generic_reloc, /* special_function */
1946 "R_PPC64_REL16_HI", /* name */
1947 FALSE, /* partial_inplace */
1948 0, /* src_mask */
1949 0xffff, /* dst_mask */
1950 TRUE), /* pcrel_offset */
1951
1952 /* The high order 16 bits of a relative address, plus 1 if the contents of
1953 the low 16 bits, treated as a signed number, is negative. */
1954 HOWTO (R_PPC64_REL16_HA, /* type */
1955 16, /* rightshift */
1956 1, /* size (0 = byte, 1 = short, 2 = long) */
1957 16, /* bitsize */
1958 TRUE, /* pc_relative */
1959 0, /* bitpos */
1960 complain_overflow_dont, /* complain_on_overflow */
1961 ppc64_elf_ha_reloc, /* special_function */
1962 "R_PPC64_REL16_HA", /* name */
1963 FALSE, /* partial_inplace */
1964 0, /* src_mask */
1965 0xffff, /* dst_mask */
1966 TRUE), /* pcrel_offset */
1967
5bd4f169
AM
1968 /* GNU extension to record C++ vtable hierarchy. */
1969 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1970 0, /* rightshift */
1971 0, /* size (0 = byte, 1 = short, 2 = long) */
1972 0, /* bitsize */
b34976b6 1973 FALSE, /* pc_relative */
5bd4f169
AM
1974 0, /* bitpos */
1975 complain_overflow_dont, /* complain_on_overflow */
1976 NULL, /* special_function */
1977 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 1978 FALSE, /* partial_inplace */
5bd4f169
AM
1979 0, /* src_mask */
1980 0, /* dst_mask */
b34976b6 1981 FALSE), /* pcrel_offset */
5bd4f169
AM
1982
1983 /* GNU extension to record C++ vtable member usage. */
1984 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1985 0, /* rightshift */
1986 0, /* size (0 = byte, 1 = short, 2 = long) */
1987 0, /* bitsize */
b34976b6 1988 FALSE, /* pc_relative */
5bd4f169
AM
1989 0, /* bitpos */
1990 complain_overflow_dont, /* complain_on_overflow */
1991 NULL, /* special_function */
1992 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 1993 FALSE, /* partial_inplace */
5bd4f169
AM
1994 0, /* src_mask */
1995 0, /* dst_mask */
b34976b6 1996 FALSE), /* pcrel_offset */
5bd4f169
AM
1997};
1998
1999\f
2000/* Initialize the ppc64_elf_howto_table, so that linear accesses can
2001 be done. */
2002
2003static void
4ce794b7 2004ppc_howto_init (void)
5bd4f169
AM
2005{
2006 unsigned int i, type;
2007
2008 for (i = 0;
2009 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2010 i++)
2011 {
2012 type = ppc64_elf_howto_raw[i].type;
2013 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2014 / sizeof (ppc64_elf_howto_table[0])));
2015 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2016 }
2017}
2018
2019static reloc_howto_type *
4ce794b7
AM
2020ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2021 bfd_reloc_code_real_type code)
5bd4f169 2022{
411e1bfb 2023 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
2024
2025 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2026 /* Initialize howto table if needed. */
2027 ppc_howto_init ();
2028
4ce794b7 2029 switch (code)
5bd4f169
AM
2030 {
2031 default:
4ce794b7 2032 return NULL;
5bd4f169 2033
411e1bfb
AM
2034 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2035 break;
2036 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2037 break;
2038 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2039 break;
2040 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2041 break;
2042 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2043 break;
2044 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2045 break;
2046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 2047 break;
411e1bfb 2048 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 2049 break;
411e1bfb 2050 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 2051 break;
411e1bfb 2052 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 2053 break;
411e1bfb 2054 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 2055 break;
411e1bfb 2056 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 2057 break;
411e1bfb 2058 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 2059 break;
411e1bfb 2060 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 2061 break;
411e1bfb 2062 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 2063 break;
411e1bfb 2064 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 2065 break;
411e1bfb 2066 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 2067 break;
411e1bfb 2068 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 2069 break;
411e1bfb 2070 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 2071 break;
411e1bfb 2072 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 2073 break;
411e1bfb 2074 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 2075 break;
411e1bfb 2076 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 2077 break;
411e1bfb 2078 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 2079 break;
411e1bfb 2080 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 2081 break;
411e1bfb 2082 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 2083 break;
411e1bfb 2084 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 2085 break;
411e1bfb 2086 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 2087 break;
411e1bfb 2088 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 2089 break;
411e1bfb 2090 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 2091 break;
411e1bfb 2092 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 2093 break;
411e1bfb 2094 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 2095 break;
411e1bfb 2096 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 2097 break;
411e1bfb 2098 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 2099 break;
411e1bfb 2100 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 2101 break;
411e1bfb 2102 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 2103 break;
411e1bfb 2104 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 2105 break;
411e1bfb 2106 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 2107 break;
411e1bfb 2108 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 2109 break;
411e1bfb 2110 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 2111 break;
411e1bfb 2112 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 2113 break;
411e1bfb 2114 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 2115 break;
411e1bfb 2116 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 2117 break;
411e1bfb 2118 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 2119 break;
411e1bfb 2120 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 2121 break;
411e1bfb 2122 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 2123 break;
411e1bfb 2124 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 2125 break;
411e1bfb 2126 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 2127 break;
411e1bfb 2128 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 2129 break;
411e1bfb 2130 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2131 break;
411e1bfb 2132 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2133 break;
411e1bfb 2134 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2135 break;
411e1bfb 2136 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2137 break;
411e1bfb 2138 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2139 break;
411e1bfb 2140 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2141 break;
411e1bfb 2142 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2143 break;
411e1bfb 2144 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2145 break;
411e1bfb 2146 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2147 break;
411e1bfb 2148 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2149 break;
411e1bfb 2150 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2151 break;
411e1bfb 2152 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2153 break;
727fc41e
AM
2154 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2155 break;
2156 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2157 break;
411e1bfb 2158 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2159 break;
411e1bfb 2160 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2161 break;
411e1bfb 2162 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2163 break;
411e1bfb 2164 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2165 break;
411e1bfb 2166 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2167 break;
411e1bfb 2168 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2169 break;
411e1bfb
AM
2170 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2171 break;
2172 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2173 break;
2174 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2175 break;
2176 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2177 break;
2178 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2179 break;
2180 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2181 break;
2182 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2183 break;
2184 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2185 break;
2186 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2187 break;
2188 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2189 break;
2190 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2191 break;
2192 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2193 break;
2194 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2195 break;
2196 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2197 break;
2198 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2199 break;
2200 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2201 break;
2202 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2203 break;
2204 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2205 break;
2206 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2207 break;
2208 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2209 break;
2210 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2211 break;
2212 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2213 break;
2214 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2215 break;
2216 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2217 break;
2218 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2219 break;
2220 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2221 break;
2222 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2223 break;
2224 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2225 break;
2226 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2227 break;
2228 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2229 break;
2230 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2231 break;
2232 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2233 break;
2234 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2235 break;
25f23106
AM
2236 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2237 break;
2238 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2239 break;
2240 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2241 break;
2242 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2243 break;
411e1bfb
AM
2244 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2245 break;
2246 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2247 break;
2248 }
2249
4ce794b7 2250 return ppc64_elf_howto_table[r];
5bd4f169
AM
2251};
2252
157090f7
AM
2253static reloc_howto_type *
2254ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2255 const char *r_name)
2256{
2257 unsigned int i;
2258
2259 for (i = 0;
2260 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2261 i++)
2262 if (ppc64_elf_howto_raw[i].name != NULL
2263 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2264 return &ppc64_elf_howto_raw[i];
2265
2266 return NULL;
2267}
2268
5bd4f169
AM
2269/* Set the howto pointer for a PowerPC ELF reloc. */
2270
2271static void
4ce794b7
AM
2272ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2273 Elf_Internal_Rela *dst)
5bd4f169 2274{
65f38f15
AM
2275 unsigned int type;
2276
ef60b7ff 2277 /* Initialize howto table if needed. */
5bd4f169 2278 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2279 ppc_howto_init ();
2280
65f38f15 2281 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2282 if (type >= (sizeof (ppc64_elf_howto_table)
2283 / sizeof (ppc64_elf_howto_table[0])))
2284 {
2285 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2286 abfd, (int) type);
2287 type = R_PPC64_NONE;
d0fb9a8d 2288 }
65f38f15 2289 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2290}
2291
04c9666a 2292/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2293
2294static bfd_reloc_status_type
4ce794b7
AM
2295ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2296 void *data, asection *input_section,
2297 bfd *output_bfd, char **error_message)
5bd4f169 2298{
805fc799
AM
2299 /* If this is a relocatable link (output_bfd test tells us), just
2300 call the generic function. Any adjustment will be done at final
2301 link time. */
2302 if (output_bfd != NULL)
cedb70c5 2303 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2304 input_section, output_bfd, error_message);
2305
2306 /* Adjust the addend for sign extension of the low 16 bits.
2307 We won't actually be using the low 16 bits, so trashing them
2308 doesn't matter. */
2309 reloc_entry->addend += 0x8000;
2310 return bfd_reloc_continue;
2311}
5bd4f169 2312
2441e016
AM
2313static bfd_reloc_status_type
2314ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2315 void *data, asection *input_section,
2316 bfd *output_bfd, char **error_message)
2317{
2318 if (output_bfd != NULL)
2319 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2320 input_section, output_bfd, error_message);
2321
699733f6
AM
2322 if (strcmp (symbol->section->name, ".opd") == 0
2323 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2324 {
2325 bfd_vma dest = opd_entry_value (symbol->section,
2326 symbol->value + reloc_entry->addend,
2327 NULL, NULL);
2328 if (dest != (bfd_vma) -1)
2329 reloc_entry->addend = dest - (symbol->value
2330 + symbol->section->output_section->vma
2331 + symbol->section->output_offset);
2332 }
2333 return bfd_reloc_continue;
2334}
2335
805fc799 2336static bfd_reloc_status_type
4ce794b7
AM
2337ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2338 void *data, asection *input_section,
2339 bfd *output_bfd, char **error_message)
805fc799
AM
2340{
2341 long insn;
04c9666a 2342 enum elf_ppc64_reloc_type r_type;
805fc799
AM
2343 bfd_size_type octets;
2344 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 2345 bfd_boolean is_power4 = FALSE;
805fc799
AM
2346
2347 /* If this is a relocatable link (output_bfd test tells us), just
2348 call the generic function. Any adjustment will be done at final
2349 link time. */
5bd4f169 2350 if (output_bfd != NULL)
cedb70c5 2351 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2352 input_section, output_bfd, error_message);
2353
2354 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2355 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2356 insn &= ~(0x01 << 21);
4ce794b7 2357 r_type = reloc_entry->howto->type;
805fc799
AM
2358 if (r_type == R_PPC64_ADDR14_BRTAKEN
2359 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2360 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799
AM
2361
2362 if (is_power4)
5bd4f169 2363 {
805fc799
AM
2364 /* Set 'a' bit. This is 0b00010 in BO field for branch
2365 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2366 for branch on CTR insns (BO == 1a00t or 1a01t). */
2367 if ((insn & (0x14 << 21)) == (0x04 << 21))
2368 insn |= 0x02 << 21;
2369 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2370 insn |= 0x08 << 21;
2371 else
2441e016 2372 goto out;
5bd4f169 2373 }
805fc799
AM
2374 else
2375 {
2376 bfd_vma target = 0;
2377 bfd_vma from;
5bd4f169 2378
805fc799
AM
2379 if (!bfd_is_com_section (symbol->section))
2380 target = symbol->value;
2381 target += symbol->section->output_section->vma;
2382 target += symbol->section->output_offset;
2383 target += reloc_entry->addend;
5bd4f169 2384
805fc799
AM
2385 from = (reloc_entry->address
2386 + input_section->output_offset
2387 + input_section->output_section->vma);
5bd4f169 2388
805fc799
AM
2389 /* Invert 'y' bit if not the default. */
2390 if ((bfd_signed_vma) (target - from) < 0)
2391 insn ^= 0x01 << 21;
2392 }
4ce794b7 2393 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2394 out:
2395 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2396 input_section, output_bfd, error_message);
805fc799 2397}
5bd4f169 2398
805fc799 2399static bfd_reloc_status_type
4ce794b7
AM
2400ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2401 void *data, asection *input_section,
2402 bfd *output_bfd, char **error_message)
805fc799
AM
2403{
2404 /* If this is a relocatable link (output_bfd test tells us), just
2405 call the generic function. Any adjustment will be done at final
2406 link time. */
2407 if (output_bfd != NULL)
cedb70c5 2408 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2409 input_section, output_bfd, error_message);
5bd4f169 2410
805fc799
AM
2411 /* Subtract the symbol section base address. */
2412 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2413 return bfd_reloc_continue;
2414}
2415
805fc799 2416static bfd_reloc_status_type
4ce794b7
AM
2417ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2418 void *data, asection *input_section,
2419 bfd *output_bfd, char **error_message)
805fc799
AM
2420{
2421 /* If this is a relocatable link (output_bfd test tells us), just
2422 call the generic function. Any adjustment will be done at final
2423 link time. */
2424 if (output_bfd != NULL)
cedb70c5 2425 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2426 input_section, output_bfd, error_message);
2427
2428 /* Subtract the symbol section base address. */
2429 reloc_entry->addend -= symbol->section->output_section->vma;
2430
2431 /* Adjust the addend for sign extension of the low 16 bits. */
2432 reloc_entry->addend += 0x8000;
2433 return bfd_reloc_continue;
2434}
2435
2436static bfd_reloc_status_type
4ce794b7
AM
2437ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2438 void *data, asection *input_section,
2439 bfd *output_bfd, char **error_message)
805fc799
AM
2440{
2441 bfd_vma TOCstart;
2442
2443 /* If this is a relocatable link (output_bfd test tells us), just
2444 call the generic function. Any adjustment will be done at final
2445 link time. */
2446 if (output_bfd != NULL)
cedb70c5 2447 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2448 input_section, output_bfd, error_message);
2449
2450 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2451 if (TOCstart == 0)
2452 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2453
2454 /* Subtract the TOC base address. */
2455 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2456 return bfd_reloc_continue;
2457}
2458
2459static bfd_reloc_status_type
4ce794b7
AM
2460ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2461 void *data, asection *input_section,
2462 bfd *output_bfd, char **error_message)
805fc799
AM
2463{
2464 bfd_vma TOCstart;
2465
2466 /* If this is a relocatable link (output_bfd test tells us), just
2467 call the generic function. Any adjustment will be done at final
2468 link time. */
2469 if (output_bfd != NULL)
cedb70c5 2470 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2471 input_section, output_bfd, error_message);
2472
2473 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2474 if (TOCstart == 0)
2475 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2476
2477 /* Subtract the TOC base address. */
2478 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2479
2480 /* Adjust the addend for sign extension of the low 16 bits. */
2481 reloc_entry->addend += 0x8000;
2482 return bfd_reloc_continue;
2483}
2484
2485static bfd_reloc_status_type
4ce794b7
AM
2486ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2487 void *data, asection *input_section,
2488 bfd *output_bfd, char **error_message)
805fc799
AM
2489{
2490 bfd_vma TOCstart;
2491 bfd_size_type octets;
2492
2493 /* If this is a relocatable link (output_bfd test tells us), just
2494 call the generic function. Any adjustment will be done at final
2495 link time. */
2496 if (output_bfd != NULL)
cedb70c5 2497 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2498 input_section, output_bfd, error_message);
2499
2500 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2501 if (TOCstart == 0)
2502 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2503
2504 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2505 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2506 return bfd_reloc_ok;
2507}
2508
2509static bfd_reloc_status_type
4ce794b7
AM
2510ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2511 void *data, asection *input_section,
2512 bfd *output_bfd, char **error_message)
805fc799
AM
2513{
2514 /* If this is a relocatable link (output_bfd test tells us), just
2515 call the generic function. Any adjustment will be done at final
2516 link time. */
2517 if (output_bfd != NULL)
cedb70c5 2518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2519 input_section, output_bfd, error_message);
2520
2521 if (error_message != NULL)
2522 {
2523 static char buf[60];
2524 sprintf (buf, "generic linker can't handle %s",
2525 reloc_entry->howto->name);
2526 *error_message = buf;
2527 }
2528 return bfd_reloc_dangerous;
2529}
2530
927be08e
AM
2531/* Track GOT entries needed for a given symbol. We might need more
2532 than one got entry per symbol. */
2533struct got_entry
2534{
2535 struct got_entry *next;
2536
2537 /* The symbol addend that we'll be placing in the GOT. */
2538 bfd_vma addend;
2539
2540 /* Unlike other ELF targets, we use separate GOT entries for the same
2541 symbol referenced from different input files. This is to support
2542 automatic multiple TOC/GOT sections, where the TOC base can vary
2543 from one input file to another. After partitioning into TOC groups
2544 we merge entries within the group.
2545
2546 Point to the BFD owning this GOT entry. */
2547 bfd *owner;
2548
2549 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2550 TLS_TPREL or TLS_DTPREL for tls entries. */
f961d9dd 2551 unsigned char tls_type;
927be08e
AM
2552
2553 /* Non-zero if got.ent points to real entry. */
f961d9dd 2554 unsigned char is_indirect;
927be08e
AM
2555
2556 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2557 union
2558 {
2559 bfd_signed_vma refcount;
2560 bfd_vma offset;
2561 struct got_entry *ent;
2562 } got;
2563};
2564
2565/* The same for PLT. */
2566struct plt_entry
2567{
2568 struct plt_entry *next;
2569
2570 bfd_vma addend;
2571
2572 union
2573 {
2574 bfd_signed_vma refcount;
2575 bfd_vma offset;
2576 } plt;
2577};
2578
e717da7e
AM
2579struct ppc64_elf_obj_tdata
2580{
2581 struct elf_obj_tdata elf;
2582
2583 /* Shortcuts to dynamic linker sections. */
2584 asection *got;
2585 asection *relgot;
2586
b3fac117
AM
2587 /* Used during garbage collection. We attach global symbols defined
2588 on removed .opd entries to this section so that the sym is removed. */
2589 asection *deleted_section;
81688140 2590
927be08e 2591 /* TLS local dynamic got entry handling. Support for multiple GOT
e717da7e 2592 sections means we potentially need one of these for each input bfd. */
927be08e 2593 struct got_entry tlsld_got;
8860955f
AM
2594
2595 /* A copy of relocs before they are modified for --emit-relocs. */
2596 Elf_Internal_Rela *opd_relocs;
d77c8a4b
AM
2597
2598 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2599 the reloc to be in the range -32768 to 32767. */
2600 unsigned int has_small_toc_reloc;
e717da7e
AM
2601};
2602
2603#define ppc64_elf_tdata(bfd) \
2604 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2605
2606#define ppc64_tlsld_got(bfd) \
2607 (&ppc64_elf_tdata (bfd)->tlsld_got)
2608
0c8d6e5c
AM
2609#define is_ppc64_elf(bfd) \
2610 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
4dfe6ac6 2611 && elf_object_id (bfd) == PPC64_ELF_DATA)
0c8d6e5c 2612
e717da7e
AM
2613/* Override the generic function because we store some extras. */
2614
2615static bfd_boolean
2616ppc64_elf_mkobject (bfd *abfd)
2617{
0ffa91dd 2618 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
4dfe6ac6 2619 PPC64_ELF_DATA);
e717da7e
AM
2620}
2621
feee612b
AM
2622/* Fix bad default arch selected for a 64 bit input bfd when the
2623 default is 32 bit. */
2624
b34976b6 2625static bfd_boolean
4ce794b7 2626ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2627{
2628 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2629 {
2630 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2631
2632 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2633 {
2634 /* Relies on arch after 32 bit default being 64 bit default. */
2635 abfd->arch_info = abfd->arch_info->next;
2636 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2637 }
2638 }
b34976b6 2639 return TRUE;
feee612b
AM
2640}
2641
d37c89e5
AM
2642/* Support for core dump NOTE sections. */
2643
2644static bfd_boolean
2645ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2646{
eea6121a 2647 size_t offset, size;
d37c89e5
AM
2648
2649 if (note->descsz != 504)
2650 return FALSE;
2651
2652 /* pr_cursig */
2653 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2654
2655 /* pr_pid */
261b8d08 2656 elf_tdata (abfd)->core_lwpid = bfd_get_32 (abfd, note->descdata + 32);
d37c89e5
AM
2657
2658 /* pr_reg */
2659 offset = 112;
eea6121a 2660 size = 384;
d37c89e5
AM
2661
2662 /* Make a ".reg/999" section. */
2663 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2664 size, note->descpos + offset);
d37c89e5
AM
2665}
2666
2667static bfd_boolean
2668ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2669{
2670 if (note->descsz != 136)
2671 return FALSE;
2672
2673 elf_tdata (abfd)->core_program
2674 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2675 elf_tdata (abfd)->core_command
2676 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2677
2678 return TRUE;
2679}
2680
183e98be
AM
2681static char *
2682ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2683 ...)
2684{
2685 switch (note_type)
2686 {
2687 default:
2688 return NULL;
2689
2690 case NT_PRPSINFO:
2691 {
2692 char data[136];
2693 va_list ap;
2694
2695 va_start (ap, note_type);
2696 memset (data, 0, 40);
2697 strncpy (data + 40, va_arg (ap, const char *), 16);
2698 strncpy (data + 56, va_arg (ap, const char *), 80);
2699 va_end (ap);
2700 return elfcore_write_note (abfd, buf, bufsiz,
2701 "CORE", note_type, data, sizeof (data));
2702 }
2703
2704 case NT_PRSTATUS:
2705 {
2706 char data[504];
2707 va_list ap;
2708 long pid;
2709 int cursig;
2710 const void *greg;
2711
2712 va_start (ap, note_type);
2713 memset (data, 0, 112);
2714 pid = va_arg (ap, long);
2715 bfd_put_32 (abfd, pid, data + 32);
2716 cursig = va_arg (ap, int);
2717 bfd_put_16 (abfd, cursig, data + 12);
2718 greg = va_arg (ap, const void *);
2719 memcpy (data + 112, greg, 384);
2720 memset (data + 496, 0, 8);
2721 va_end (ap);
2722 return elfcore_write_note (abfd, buf, bufsiz,
2723 "CORE", note_type, data, sizeof (data));
2724 }
2725 }
2726}
2727
5bd4f169
AM
2728/* Merge backend specific data from an object file to the output
2729 object file when linking. */
2f6d9989 2730
b34976b6 2731static bfd_boolean
4ce794b7 2732ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5bd4f169 2733{
5bd4f169
AM
2734 /* Check if we have the same endianess. */
2735 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
87e226ce 2736 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
5bd4f169
AM
2737 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2738 {
2739 const char *msg;
2740
2741 if (bfd_big_endian (ibfd))
d003868e 2742 msg = _("%B: compiled for a big endian system "
4ce794b7 2743 "and target is little endian");
5bd4f169 2744 else
d003868e 2745 msg = _("%B: compiled for a little endian system "
4ce794b7 2746 "and target is big endian");
5bd4f169 2747
d003868e 2748 (*_bfd_error_handler) (msg, ibfd);
5bd4f169
AM
2749
2750 bfd_set_error (bfd_error_wrong_format);
b34976b6 2751 return FALSE;
5bd4f169
AM
2752 }
2753
b34976b6 2754 return TRUE;
5bd4f169 2755}
f0abc2a1 2756
5d35169e
AM
2757/* Add extra PPC sections. */
2758
b35d266b 2759static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2760{
0112cd26
NC
2761 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2762 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2763 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2764 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2765 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2766 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2767 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2768};
2769
7c8fe5c4
AM
2770enum _ppc64_sec_type {
2771 sec_normal = 0,
2772 sec_opd = 1,
2773 sec_toc = 2
2774};
2775
f0abc2a1
AM
2776struct _ppc64_elf_section_data
2777{
2778 struct bfd_elf_section_data elf;
411e1bfb 2779
f0abc2a1
AM
2780 union
2781 {
74f0fb50
AM
2782 /* An array with one entry for each opd function descriptor. */
2783 struct _opd_sec_data
2784 {
2785 /* Points to the function code section for local opd entries. */
2786 asection **func_sec;
2787
2788 /* After editing .opd, adjust references to opd local syms. */
2789 long *adjust;
2790 } opd;
7c8fe5c4 2791
3a71aa26
AM
2792 /* An array for toc sections, indexed by offset/8. */
2793 struct _toc_sec_data
2794 {
2795 /* Specifies the relocation symbol index used at a given toc offset. */
2796 unsigned *symndx;
2797
2798 /* And the relocation addend. */
2799 bfd_vma *add;
2800 } toc;
7c8fe5c4
AM
2801 } u;
2802
2803 enum _ppc64_sec_type sec_type:2;
411e1bfb 2804
7c8fe5c4
AM
2805 /* Flag set when small branches are detected. Used to
2806 select suitable defaults for the stub group size. */
2807 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2808};
2809
2810#define ppc64_elf_section_data(sec) \
411e1bfb 2811 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2812
2813static bfd_boolean
4ce794b7 2814ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2815{
f592407e
AM
2816 if (!sec->used_by_bfd)
2817 {
2818 struct _ppc64_elf_section_data *sdata;
2819 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2820
f592407e
AM
2821 sdata = bfd_zalloc (abfd, amt);
2822 if (sdata == NULL)
2823 return FALSE;
2824 sec->used_by_bfd = sdata;
2825 }
f0abc2a1
AM
2826
2827 return _bfd_elf_new_section_hook (abfd, sec);
2828}
4025353c 2829
74f0fb50 2830static struct _opd_sec_data *
4025353c
AM
2831get_opd_info (asection * sec)
2832{
2833 if (sec != NULL
2834 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4 2835 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
74f0fb50 2836 return &ppc64_elf_section_data (sec)->u.opd;
4025353c
AM
2837 return NULL;
2838}
90e3cdf2
JJ
2839\f
2840/* Parameters for the qsort hook. */
90e3cdf2
JJ
2841static bfd_boolean synthetic_relocatable;
2842
699733f6 2843/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2844
2845static int
2846compare_symbols (const void *ap, const void *bp)
2847{
2848 const asymbol *a = * (const asymbol **) ap;
2849 const asymbol *b = * (const asymbol **) bp;
2850
699733f6
AM
2851 /* Section symbols first. */
2852 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2853 return -1;
699733f6 2854 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2855 return 1;
2856
699733f6 2857 /* then .opd symbols. */
ffcfec52
AM
2858 if (strcmp (a->section->name, ".opd") == 0
2859 && strcmp (b->section->name, ".opd") != 0)
90e3cdf2 2860 return -1;
ffcfec52
AM
2861 if (strcmp (a->section->name, ".opd") != 0
2862 && strcmp (b->section->name, ".opd") == 0)
90e3cdf2
JJ
2863 return 1;
2864
699733f6 2865 /* then other code symbols. */
90e3cdf2
JJ
2866 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2867 == (SEC_CODE | SEC_ALLOC)
2868 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2869 != (SEC_CODE | SEC_ALLOC))
2870 return -1;
2871
2872 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2873 != (SEC_CODE | SEC_ALLOC)
2874 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2875 == (SEC_CODE | SEC_ALLOC))
2876 return 1;
2877
2878 if (synthetic_relocatable)
2879 {
2880 if (a->section->id < b->section->id)
2881 return -1;
2882
2883 if (a->section->id > b->section->id)
2884 return 1;
2885 }
2886
2887 if (a->value + a->section->vma < b->value + b->section->vma)
2888 return -1;
2889
2890 if (a->value + a->section->vma > b->value + b->section->vma)
2891 return 1;
2892
4d35a0aa
AM
2893 /* For syms with the same value, prefer strong dynamic global function
2894 syms over other syms. */
2895 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2896 return -1;
2897
2898 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2899 return 1;
2900
2901 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2902 return -1;
2903
2904 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2905 return 1;
2906
2907 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2908 return -1;
2909
2910 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2911 return 1;
2912
2913 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2914 return -1;
2915
2916 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2917 return 1;
2918
90e3cdf2
JJ
2919 return 0;
2920}
2921
699733f6 2922/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2923
699733f6
AM
2924static asymbol *
2925sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2926{
699733f6 2927 long mid;
90e3cdf2 2928
699733f6
AM
2929 if (id == -1)
2930 {
2931 while (lo < hi)
2932 {
2933 mid = (lo + hi) >> 1;
2934 if (syms[mid]->value + syms[mid]->section->vma < value)
2935 lo = mid + 1;
2936 else if (syms[mid]->value + syms[mid]->section->vma > value)
2937 hi = mid;
2938 else
2939 return syms[mid];
2940 }
2941 }
2942 else
2943 {
2944 while (lo < hi)
2945 {
2946 mid = (lo + hi) >> 1;
2947 if (syms[mid]->section->id < id)
2948 lo = mid + 1;
2949 else if (syms[mid]->section->id > id)
2950 hi = mid;
2951 else if (syms[mid]->value < value)
2952 lo = mid + 1;
2953 else if (syms[mid]->value > value)
2954 hi = mid;
2955 else
2956 return syms[mid];
2957 }
2958 }
2959 return NULL;
90e3cdf2
JJ
2960}
2961
468392fb
AM
2962static bfd_boolean
2963section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2964{
2965 bfd_vma vma = *(bfd_vma *) ptr;
2966 return ((section->flags & SEC_ALLOC) != 0
2967 && section->vma <= vma
2968 && vma < section->vma + section->size);
2969}
2970
699733f6 2971/* Create synthetic symbols, effectively restoring "dot-symbol" function
468392fb 2972 entry syms. Also generate @plt symbols for the glink branch table. */
90e3cdf2
JJ
2973
2974static long
a7535cf3
AM
2975ppc64_elf_get_synthetic_symtab (bfd *abfd,
2976 long static_count, asymbol **static_syms,
2977 long dyn_count, asymbol **dyn_syms,
c9727e01 2978 asymbol **ret)
90e3cdf2
JJ
2979{
2980 asymbol *s;
699733f6
AM
2981 long i;
2982 long count;
90e3cdf2 2983 char *names;
a7535cf3 2984 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2985 asection *opd;
90e3cdf2 2986 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2987 asymbol **syms;
90e3cdf2
JJ
2988
2989 *ret = NULL;
2990
2991 opd = bfd_get_section_by_name (abfd, ".opd");
2992 if (opd == NULL)
2993 return 0;
2994
a7535cf3 2995 symcount = static_count;
c9727e01 2996 if (!relocatable)
a7535cf3 2997 symcount += dyn_count;
90e3cdf2 2998 if (symcount == 0)
c9727e01 2999 return 0;
90e3cdf2 3000
a7535cf3
AM
3001 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3002 if (syms == NULL)
7356fed5 3003 return -1;
a7535cf3
AM
3004
3005 if (!relocatable && static_count != 0 && dyn_count != 0)
3006 {
3007 /* Use both symbol tables. */
3008 memcpy (syms, static_syms, static_count * sizeof (*syms));
3009 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3010 }
3011 else if (!relocatable && static_count == 0)
3012 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3013 else
3014 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3015
90e3cdf2 3016 synthetic_relocatable = relocatable;
595da8c5 3017 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 3018
c9727e01
AM
3019 if (!relocatable && symcount > 1)
3020 {
3021 long j;
3022 /* Trim duplicate syms, since we may have merged the normal and
3023 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 3024 different values, so trim any with the same value. */
c9727e01
AM
3025 for (i = 1, j = 1; i < symcount; ++i)
3026 if (syms[i - 1]->value + syms[i - 1]->section->vma
3027 != syms[i]->value + syms[i]->section->vma)
3028 syms[j++] = syms[i];
3029 symcount = j;
3030 }
3031
699733f6 3032 i = 0;
ffcfec52 3033 if (strcmp (syms[i]->section->name, ".opd") == 0)
699733f6
AM
3034 ++i;
3035 codesecsym = i;
90e3cdf2 3036
699733f6
AM
3037 for (; i < symcount; ++i)
3038 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3039 != (SEC_CODE | SEC_ALLOC))
3040 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3041 break;
3042 codesecsymend = i;
90e3cdf2 3043
699733f6
AM
3044 for (; i < symcount; ++i)
3045 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3046 break;
3047 secsymend = i;
90e3cdf2 3048
699733f6 3049 for (; i < symcount; ++i)
ffcfec52 3050 if (strcmp (syms[i]->section->name, ".opd") != 0)
699733f6
AM
3051 break;
3052 opdsymend = i;
90e3cdf2 3053
699733f6
AM
3054 for (; i < symcount; ++i)
3055 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3056 != (SEC_CODE | SEC_ALLOC))
3057 break;
3058 symcount = i;
3059
c9727e01 3060 count = 0;
90e3cdf2 3061
699733f6 3062 if (relocatable)
90e3cdf2 3063 {
699733f6
AM
3064 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3065 arelent *r;
3066 size_t size;
3067 long relcount;
90e3cdf2 3068
468392fb
AM
3069 if (opdsymend == secsymend)
3070 goto done;
3071
699733f6 3072 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 3073 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 3074 if (relcount == 0)
c9727e01 3075 goto done;
90e3cdf2 3076
7356fed5
AM
3077 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3078 {
3079 count = -1;
3080 goto done;
3081 }
3082
699733f6 3083 size = 0;
595da8c5 3084 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
3085 {
3086 asymbol *sym;
90e3cdf2 3087
595da8c5 3088 while (r < opd->relocation + relcount
699733f6
AM
3089 && r->address < syms[i]->value + opd->vma)
3090 ++r;
90e3cdf2 3091
595da8c5 3092 if (r == opd->relocation + relcount)
699733f6 3093 break;
90e3cdf2 3094
699733f6
AM
3095 if (r->address != syms[i]->value + opd->vma)
3096 continue;
90e3cdf2 3097
699733f6
AM
3098 if (r->howto->type != R_PPC64_ADDR64)
3099 continue;
90e3cdf2 3100
699733f6
AM
3101 sym = *r->sym_ptr_ptr;
3102 if (!sym_exists_at (syms, opdsymend, symcount,
3103 sym->section->id, sym->value + r->addend))
3104 {
3105 ++count;
3106 size += sizeof (asymbol);
3107 size += strlen (syms[i]->name) + 2;
3108 }
3109 }
90e3cdf2 3110
699733f6
AM
3111 s = *ret = bfd_malloc (size);
3112 if (s == NULL)
3113 {
7356fed5 3114 count = -1;
c9727e01 3115 goto done;
699733f6 3116 }
90e3cdf2 3117
699733f6 3118 names = (char *) (s + count);
90e3cdf2 3119
595da8c5 3120 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 3121 {
699733f6 3122 asymbol *sym;
90e3cdf2 3123
595da8c5 3124 while (r < opd->relocation + relcount
699733f6
AM
3125 && r->address < syms[i]->value + opd->vma)
3126 ++r;
90e3cdf2 3127
595da8c5 3128 if (r == opd->relocation + relcount)
699733f6
AM
3129 break;
3130
3131 if (r->address != syms[i]->value + opd->vma)
3132 continue;
3133
3134 if (r->howto->type != R_PPC64_ADDR64)
3135 continue;
90e3cdf2 3136
699733f6
AM
3137 sym = *r->sym_ptr_ptr;
3138 if (!sym_exists_at (syms, opdsymend, symcount,
3139 sym->section->id, sym->value + r->addend))
3140 {
3141 size_t len;
3142
3143 *s = *syms[i];
6ba2a415 3144 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3145 s->section = sym->section;
3146 s->value = sym->value + r->addend;
3147 s->name = names;
3148 *names++ = '.';
3149 len = strlen (syms[i]->name);
3150 memcpy (names, syms[i]->name, len + 1);
3151 names += len + 1;
6f610d07
UW
3152 /* Have udata.p point back to the original symbol this
3153 synthetic symbol was derived from. */
3154 s->udata.p = syms[i];
699733f6
AM
3155 s++;
3156 }
3157 }
3158 }
3159 else
90e3cdf2 3160 {
468392fb 3161 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
699733f6
AM
3162 bfd_byte *contents;
3163 size_t size;
468392fb
AM
3164 long plt_count = 0;
3165 bfd_vma glink_vma = 0, resolv_vma = 0;
3166 asection *dynamic, *glink = NULL, *relplt = NULL;
3167 arelent *p;
90e3cdf2 3168
699733f6
AM
3169 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
3170 {
3171 if (contents)
c9727e01
AM
3172 {
3173 free_contents_and_exit:
3174 free (contents);
3175 }
7356fed5 3176 count = -1;
c9727e01 3177 goto done;
699733f6 3178 }
90e3cdf2 3179
699733f6
AM
3180 size = 0;
3181 for (i = secsymend; i < opdsymend; ++i)
3182 {
3183 bfd_vma ent;
90e3cdf2 3184
5ef11c02
AM
3185 /* Ignore bogus symbols. */
3186 if (syms[i]->value > opd->size - 8)
3187 continue;
3188
699733f6
AM
3189 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3190 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3191 {
3192 ++count;
3193 size += sizeof (asymbol);
3194 size += strlen (syms[i]->name) + 2;
3195 }
3196 }
90e3cdf2 3197
468392fb 3198 /* Get start of .glink stubs from DT_PPC64_GLINK. */
066ee829
AM
3199 if (dyn_count != 0
3200 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
468392fb
AM
3201 {
3202 bfd_byte *dynbuf, *extdyn, *extdynend;
3203 size_t extdynsize;
3204 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3205
3206 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3207 goto free_contents_and_exit;
3208
3209 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3210 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3211
3212 extdyn = dynbuf;
3213 extdynend = extdyn + dynamic->size;
3214 for (; extdyn < extdynend; extdyn += extdynsize)
3215 {
3216 Elf_Internal_Dyn dyn;
3217 (*swap_dyn_in) (abfd, extdyn, &dyn);
3218
3219 if (dyn.d_tag == DT_NULL)
3220 break;
3221
3222 if (dyn.d_tag == DT_PPC64_GLINK)
3223 {
3224 /* The first glink stub starts at offset 32; see comment in
3225 ppc64_elf_finish_dynamic_sections. */
3226 glink_vma = dyn.d_un.d_val + 32;
3227 /* The .glink section usually does not survive the final
3228 link; search for the section (usually .text) where the
3229 glink stubs now reside. */
3230 glink = bfd_sections_find_if (abfd, section_covers_vma,
3231 &glink_vma);
3232 break;
3233 }
3234 }
3235
3236 free (dynbuf);
3237 }
3238
3239 if (glink != NULL)
3240 {
3241 /* Determine __glink trampoline by reading the relative branch
3242 from the first glink stub. */
3243 bfd_byte buf[4];
3244 if (bfd_get_section_contents (abfd, glink, buf,
3245 glink_vma + 4 - glink->vma, 4))
3246 {
3247 unsigned int insn = bfd_get_32 (abfd, buf);
3248 insn ^= B_DOT;
3249 if ((insn & ~0x3fffffc) == 0)
3250 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3251 }
3252
3253 if (resolv_vma)
3254 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
468392fb 3255
066ee829
AM
3256 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3257 if (relplt != NULL)
3258 {
3259 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3260 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3261 goto free_contents_and_exit;
468392fb 3262
066ee829
AM
3263 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3264 size += plt_count * sizeof (asymbol);
468392fb 3265
066ee829
AM
3266 p = relplt->relocation;
3267 for (i = 0; i < plt_count; i++, p++)
e054468f
AM
3268 {
3269 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3270 if (p->addend != 0)
3271 size += sizeof ("+0x") - 1 + 16;
3272 }
066ee829 3273 }
468392fb
AM
3274 }
3275
699733f6
AM
3276 s = *ret = bfd_malloc (size);
3277 if (s == NULL)
7356fed5 3278 goto free_contents_and_exit;
90e3cdf2 3279
468392fb 3280 names = (char *) (s + count + plt_count + (resolv_vma != 0));
90e3cdf2 3281
699733f6 3282 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 3283 {
699733f6 3284 bfd_vma ent;
90e3cdf2 3285
5ef11c02
AM
3286 if (syms[i]->value > opd->size - 8)
3287 continue;
3288
699733f6
AM
3289 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3290 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 3291 {
c9727e01 3292 long lo, hi;
699733f6 3293 size_t len;
c9727e01 3294 asection *sec = abfd->sections;
90e3cdf2 3295
699733f6
AM
3296 *s = *syms[i];
3297 lo = codesecsym;
3298 hi = codesecsymend;
3299 while (lo < hi)
3300 {
c9727e01 3301 long mid = (lo + hi) >> 1;
699733f6
AM
3302 if (syms[mid]->section->vma < ent)
3303 lo = mid + 1;
3304 else if (syms[mid]->section->vma > ent)
3305 hi = mid;
3306 else
c9727e01
AM
3307 {
3308 sec = syms[mid]->section;
3309 break;
3310 }
699733f6
AM
3311 }
3312
c9727e01 3313 if (lo >= hi && lo > codesecsym)
699733f6 3314 sec = syms[lo - 1]->section;
699733f6
AM
3315
3316 for (; sec != NULL; sec = sec->next)
3317 {
3318 if (sec->vma > ent)
3319 break;
3320 if ((sec->flags & SEC_ALLOC) == 0
3321 || (sec->flags & SEC_LOAD) == 0)
3322 break;
3323 if ((sec->flags & SEC_CODE) != 0)
3324 s->section = sec;
3325 }
6ba2a415 3326 s->flags |= BSF_SYNTHETIC;
699733f6
AM
3327 s->value = ent - s->section->vma;
3328 s->name = names;
3329 *names++ = '.';
3330 len = strlen (syms[i]->name);
3331 memcpy (names, syms[i]->name, len + 1);
3332 names += len + 1;
6f610d07
UW
3333 /* Have udata.p point back to the original symbol this
3334 synthetic symbol was derived from. */
3335 s->udata.p = syms[i];
699733f6 3336 s++;
90e3cdf2 3337 }
90e3cdf2 3338 }
699733f6 3339 free (contents);
468392fb
AM
3340
3341 if (glink != NULL && relplt != NULL)
3342 {
3343 if (resolv_vma)
3344 {
3345 /* Add a symbol for the main glink trampoline. */
86a4952b 3346 memset (s, 0, sizeof *s);
468392fb 3347 s->the_bfd = abfd;
6ba2a415 3348 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
468392fb
AM
3349 s->section = glink;
3350 s->value = resolv_vma - glink->vma;
3351 s->name = names;
3352 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3353 names += sizeof ("__glink_PLTresolve");
3354 s++;
3355 count++;
3356 }
3357
3358 /* FIXME: It would be very much nicer to put sym@plt on the
3359 stub rather than on the glink branch table entry. The
3360 objdump disassembler would then use a sensible symbol
3361 name on plt calls. The difficulty in doing so is
3362 a) finding the stubs, and,
3363 b) matching stubs against plt entries, and,
3364 c) there can be multiple stubs for a given plt entry.
3365
3366 Solving (a) could be done by code scanning, but older
3367 ppc64 binaries used different stubs to current code.
3368 (b) is the tricky one since you need to known the toc
3369 pointer for at least one function that uses a pic stub to
3370 be able to calculate the plt address referenced.
3371 (c) means gdb would need to set multiple breakpoints (or
3372 find the glink branch itself) when setting breakpoints
3373 for pending shared library loads. */
3374 p = relplt->relocation;
3375 for (i = 0; i < plt_count; i++, p++)
3376 {
3377 size_t len;
3378
3379 *s = **p->sym_ptr_ptr;
3380 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3381 we are defining a symbol, ensure one of them is set. */
3382 if ((s->flags & BSF_LOCAL) == 0)
3383 s->flags |= BSF_GLOBAL;
6ba2a415 3384 s->flags |= BSF_SYNTHETIC;
468392fb
AM
3385 s->section = glink;
3386 s->value = glink_vma - glink->vma;
3387 s->name = names;
3388 s->udata.p = NULL;
3389 len = strlen ((*p->sym_ptr_ptr)->name);
3390 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3391 names += len;
e054468f
AM
3392 if (p->addend != 0)
3393 {
3394 memcpy (names, "+0x", sizeof ("+0x") - 1);
3395 names += sizeof ("+0x") - 1;
3396 bfd_sprintf_vma (abfd, names, p->addend);
3397 names += strlen (names);
3398 }
468392fb
AM
3399 memcpy (names, "@plt", sizeof ("@plt"));
3400 names += sizeof ("@plt");
3401 s++;
3402 glink_vma += 8;
3403 if (i >= 0x8000)
3404 glink_vma += 4;
3405 }
3406 count += plt_count;
3407 }
90e3cdf2
JJ
3408 }
3409
c9727e01 3410 done:
a7535cf3 3411 free (syms);
90e3cdf2
JJ
3412 return count;
3413}
5bd4f169 3414\f
65f38f15
AM
3415/* The following functions are specific to the ELF linker, while
3416 functions above are used generally. Those named ppc64_elf_* are
3417 called by the main ELF linker code. They appear in this file more
3418 or less in the order in which they are called. eg.
3419 ppc64_elf_check_relocs is called early in the link process,
3420 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
3421 called.
3422
3423 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3424 functions have both a function code symbol and a function descriptor
3425 symbol. A call to foo in a relocatable object file looks like:
3426
3427 . .text
3428 . x:
3429 . bl .foo
3430 . nop
3431
3432 The function definition in another object file might be:
3433
3434 . .section .opd
3435 . foo: .quad .foo
3436 . .quad .TOC.@tocbase
3437 . .quad 0
3438 .
3439 . .text
3440 . .foo: blr
3441
3442 When the linker resolves the call during a static link, the branch
3443 unsurprisingly just goes to .foo and the .opd information is unused.
3444 If the function definition is in a shared library, things are a little
3445 different: The call goes via a plt call stub, the opd information gets
3446 copied to the plt, and the linker patches the nop.
3447
3448 . x:
3449 . bl .foo_stub
3450 . ld 2,40(1)
3451 .
3452 .
3453 . .foo_stub:
3454 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 3455 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
3456 . std 2,40(1) # this is the general idea
3457 . ld 11,0(12)
3458 . ld 2,8(12)
3459 . mtctr 11
3460 . ld 11,16(12)
3461 . bctr
3462 .
3463 . .section .plt
3464 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3465
3466 The "reloc ()" notation is supposed to indicate that the linker emits
3467 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3468 copying.
3469
3470 What are the difficulties here? Well, firstly, the relocations
3471 examined by the linker in check_relocs are against the function code
3472 sym .foo, while the dynamic relocation in the plt is emitted against
3473 the function descriptor symbol, foo. Somewhere along the line, we need
3474 to carefully copy dynamic link information from one symbol to the other.
3475 Secondly, the generic part of the elf linker will make .foo a dynamic
3476 symbol as is normal for most other backends. We need foo dynamic
3477 instead, at least for an application final link. However, when
3478 creating a shared library containing foo, we need to have both symbols
3479 dynamic so that references to .foo are satisfied during the early
3480 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3481 definition from some other object, eg. a static library.
3482
3483 Update: As of August 2004, we support a new convention. Function
3484 calls may use the function descriptor symbol, ie. "bl foo". This
3485 behaves exactly as "bl .foo". */
65f38f15
AM
3486
3487/* The linker needs to keep track of the number of relocs that it
3488 decides to copy as dynamic relocs in check_relocs for each symbol.
3489 This is so that it can later discard them if they are found to be
3490 unnecessary. We store the information in a field extending the
3491 regular ELF linker hash table. */
3492
3493struct ppc_dyn_relocs
3494{
3495 struct ppc_dyn_relocs *next;
3496
3497 /* The input section of the reloc. */
3498 asection *sec;
3499
3500 /* Total number of relocs copied for the input section. */
3501 bfd_size_type count;
3502
3503 /* Number of pc-relative relocs copied for the input section. */
3504 bfd_size_type pc_count;
3505};
3506
1d483afe 3507/* Of those relocs that might be copied as dynamic relocs, this function
58ac9f71
AM
3508 selects those that must be copied when linking a shared library,
3509 even when the symbol is local. */
65f38f15 3510
1d483afe
AM
3511static int
3512must_be_dyn_reloc (struct bfd_link_info *info,
3513 enum elf_ppc64_reloc_type r_type)
3514{
3515 switch (r_type)
3516 {
3517 default:
3518 return 1;
3519
3520 case R_PPC64_REL32:
3521 case R_PPC64_REL64:
3522 case R_PPC64_REL30:
3523 return 0;
3524
3525 case R_PPC64_TPREL16:
3526 case R_PPC64_TPREL16_LO:
3527 case R_PPC64_TPREL16_HI:
3528 case R_PPC64_TPREL16_HA:
3529 case R_PPC64_TPREL16_DS:
3530 case R_PPC64_TPREL16_LO_DS:
3531 case R_PPC64_TPREL16_HIGHER:
3532 case R_PPC64_TPREL16_HIGHERA:
3533 case R_PPC64_TPREL16_HIGHEST:
3534 case R_PPC64_TPREL16_HIGHESTA:
3535 case R_PPC64_TPREL64:
3536 return !info->executable;
3537 }
3538}
65f38f15 3539
f4656909
AM
3540/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3541 copying dynamic variables from a shared lib into an app's dynbss
3542 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3543 shared lib. With code that gcc generates, it's vital that this be
3544 enabled; In the PowerPC64 ABI, the address of a function is actually
3545 the address of a function descriptor, which resides in the .opd
3546 section. gcc uses the descriptor directly rather than going via the
3547 GOT as some other ABI's do, which means that initialized function
3548 pointers must reference the descriptor. Thus, a function pointer
3549 initialized to the address of a function in a shared library will
3550 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3551 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3552 presents a problem as a plt entry for that function is also
3553 initialized from the function descriptor symbol and the copy reloc
3554 may not be initialized first. */
a23b6845 3555#define ELIMINATE_COPY_RELOCS 1
f4656909 3556
721956f4
AM
3557/* Section name for stubs is the associated section name plus this
3558 string. */
3559#define STUB_SUFFIX ".stub"
3560
3561/* Linker stubs.
3562 ppc_stub_long_branch:
3563 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3564 destination, but a 24 bit branch in a stub section will reach.
3565 . b dest
3566
3567 ppc_stub_plt_branch:
3568 Similar to the above, but a 24 bit branch in the stub section won't
3569 reach its destination.
87e226ce
AM
3570 . addis %r12,%r2,xxx@toc@ha
3571 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3572 . mtctr %r11
3573 . bctr
3574
3575 ppc_stub_plt_call:
2c66dc6c
AM
3576 Used to call a function in a shared library. If it so happens that
3577 the plt entry referenced crosses a 64k boundary, then an extra
ac2df442 3578 "addi %r12,%r12,xxx@toc@l" will be inserted before the "mtctr".
87e226ce 3579 . addis %r12,%r2,xxx@toc@ha
721956f4 3580 . std %r2,40(%r1)
87e226ce 3581 . ld %r11,xxx+0@toc@l(%r12)
721956f4 3582 . mtctr %r11
ac2df442 3583 . ld %r2,xxx+8@toc@l(%r12)
87e226ce 3584 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3585 . bctr
ad8e1ba5
AM
3586
3587 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3588 code to adjust the value and save r2 to support multiple toc sections.
3589 A ppc_stub_long_branch with an r2 offset looks like:
3590 . std %r2,40(%r1)
3591 . addis %r2,%r2,off@ha
3592 . addi %r2,%r2,off@l
3593 . b dest
3594
3595 A ppc_stub_plt_branch with an r2 offset looks like:
3596 . std %r2,40(%r1)
3597 . addis %r12,%r2,xxx@toc@ha
3598 . ld %r11,xxx@toc@l(%r12)
3599 . addis %r2,%r2,off@ha
3600 . addi %r2,%r2,off@l
3601 . mtctr %r11
3602 . bctr
ac2df442
AM
3603
3604 In cases where the "addis" instruction would add zero, the "addis" is
3605 omitted and following instructions modified slightly in some cases.
721956f4
AM
3606*/
3607
3608enum ppc_stub_type {
3609 ppc_stub_none,
3610 ppc_stub_long_branch,
ad8e1ba5 3611 ppc_stub_long_branch_r2off,
721956f4 3612 ppc_stub_plt_branch,
ad8e1ba5 3613 ppc_stub_plt_branch_r2off,
721956f4
AM
3614 ppc_stub_plt_call
3615};
3616
3617struct ppc_stub_hash_entry {
3618
3619 /* Base hash table entry structure. */
3620 struct bfd_hash_entry root;
3621
ad8e1ba5
AM
3622 enum ppc_stub_type stub_type;
3623
721956f4
AM
3624 /* The stub section. */
3625 asection *stub_sec;
3626
3627 /* Offset within stub_sec of the beginning of this stub. */
3628 bfd_vma stub_offset;
3629
3630 /* Given the symbol's value and its section we can determine its final
3631 value when building the stubs (so the stub knows where to jump. */
3632 bfd_vma target_value;
3633 asection *target_section;
3634
721956f4
AM
3635 /* The symbol table entry, if any, that this was derived from. */
3636 struct ppc_link_hash_entry *h;
e054468f 3637 struct plt_entry *plt_ent;
721956f4 3638
411e1bfb
AM
3639 /* And the reloc addend that this was derived from. */
3640 bfd_vma addend;
3641
721956f4
AM
3642 /* Where this stub is being called from, or, in the case of combined
3643 stub sections, the first input section in the group. */
3644 asection *id_sec;
3645};
3646
3647struct ppc_branch_hash_entry {
3648
3649 /* Base hash table entry structure. */
3650 struct bfd_hash_entry root;
3651
c456f082 3652 /* Offset within branch lookup table. */
721956f4
AM
3653 unsigned int offset;
3654
3655 /* Generation marker. */
3656 unsigned int iter;
3657};
65f38f15
AM
3658
3659struct ppc_link_hash_entry
3660{
3661 struct elf_link_hash_entry elf;
3662
b3fac117
AM
3663 union {
3664 /* A pointer to the most recently used stub hash entry against this
3665 symbol. */
3666 struct ppc_stub_hash_entry *stub_cache;
3667
3668 /* A pointer to the next symbol starting with a '.' */
3669 struct ppc_link_hash_entry *next_dot_sym;
3670 } u;
721956f4 3671
65f38f15
AM
3672 /* Track dynamic relocs copied for this symbol. */
3673 struct ppc_dyn_relocs *dyn_relocs;
e86ce104 3674
721956f4 3675 /* Link between function code and descriptor symbols. */
34814b9f 3676 struct ppc_link_hash_entry *oh;
721956f4 3677
e86ce104
AM
3678 /* Flag function code and descriptor symbols. */
3679 unsigned int is_func:1;
3680 unsigned int is_func_descriptor:1;
908b32fc 3681 unsigned int fake:1;
411e1bfb 3682
c5614fa4
AM
3683 /* Whether global opd/toc sym has been adjusted or not.
3684 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3685 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3686 unsigned int adjust_done:1;
3687
99877b66
AM
3688 /* Set if we twiddled this symbol to weak at some stage. */
3689 unsigned int was_undefined:1;
3690
411e1bfb 3691 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3692 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3693 corresponding relocs are encountered during check_relocs.
3694 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3695 indicate the corresponding GOT entry type is not needed.
3696 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3697 a TPREL one. We use a separate flag rather than setting TPREL
3698 just for convenience in distinguishing the two cases. */
3699#define TLS_GD 1 /* GD reloc. */
3700#define TLS_LD 2 /* LD reloc. */
3701#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3702#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3703#define TLS_TLS 16 /* Any TLS reloc. */
3704#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3705#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
e054468f 3706#define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
f961d9dd 3707 unsigned char tls_mask;
65f38f15
AM
3708};
3709
3710/* ppc64 ELF linker hash table. */
3711
3712struct ppc_link_hash_table
3713{
3714 struct elf_link_hash_table elf;
3715
721956f4
AM
3716 /* The stub hash table. */
3717 struct bfd_hash_table stub_hash_table;
3718
3719 /* Another hash table for plt_branch stubs. */
3720 struct bfd_hash_table branch_hash_table;
3721
3722 /* Linker stub bfd. */
3723 bfd *stub_bfd;
3724
3725 /* Linker call-backs. */
4ce794b7
AM
3726 asection * (*add_stub_section) (const char *, asection *);
3727 void (*layout_sections_again) (void);
721956f4
AM
3728
3729 /* Array to keep track of which stub sections have been created, and
3730 information on stub grouping. */
3731 struct map_stub {
3732 /* This is the section to which stubs in the group will be attached. */
3733 asection *link_sec;
3734 /* The stub section. */
3735 asection *stub_sec;
ad8e1ba5
AM
3736 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3737 bfd_vma toc_off;
721956f4
AM
3738 } *stub_group;
3739
ad8e1ba5
AM
3740 /* Temp used when calculating TOC pointers. */
3741 bfd_vma toc_curr;
bf102f86
AM
3742 bfd *toc_bfd;
3743 asection *toc_first_sec;
ad8e1ba5 3744
8f3bab57
AM
3745 /* Highest input section id. */
3746 int top_id;
3747
734b6cf9
AM
3748 /* Highest output section index. */
3749 int top_index;
3750
b3fac117
AM
3751 /* Used when adding symbols. */
3752 struct ppc_link_hash_entry *dot_syms;
3753
734b6cf9
AM
3754 /* List of input sections for each output section. */
3755 asection **input_list;
721956f4 3756
65f38f15 3757 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3758 asection *got;
4ce794b7
AM
3759 asection *plt;
3760 asection *relplt;
e054468f
AM
3761 asection *iplt;
3762 asection *reliplt;
4ce794b7
AM
3763 asection *dynbss;
3764 asection *relbss;
3765 asection *glink;
82bd7b59 3766 asection *sfpr;
4ce794b7
AM
3767 asection *brlt;
3768 asection *relbrlt;
ec338859 3769
8387904d
AM
3770 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3771 struct ppc_link_hash_entry *tls_get_addr;
3772 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3773
927be08e
AM
3774 /* The size of reliplt used by got entry relocs. */
3775 bfd_size_type got_reli_size;
3776
9b5ecbd0
AM
3777 /* Statistics. */
3778 unsigned long stub_count[ppc_stub_plt_call];
3779
ee75fd95
AM
3780 /* Number of stubs against global syms. */
3781 unsigned long stub_globals;
3782
ad8e1ba5 3783 /* Set if we should emit symbols for stubs. */
99877b66 3784 unsigned int emit_stub_syms:1;
ad8e1ba5 3785
a7f2871e
AM
3786 /* Set if __tls_get_addr optimization should not be done. */
3787 unsigned int no_tls_get_addr_opt:1;
3788
4c52953f 3789 /* Support for multiple toc sections. */
33c0ec9d 3790 unsigned int do_multi_toc:1;
4c52953f 3791 unsigned int multi_toc_needed:1;
927be08e 3792 unsigned int second_toc_pass:1;
67f0cbdb 3793 unsigned int do_toc_opt:1;
4c52953f 3794
5d1634d7 3795 /* Set on error. */
99877b66 3796 unsigned int stub_error:1;
721956f4 3797
7d9616d7 3798 /* Temp used by ppc64_elf_process_dot_syms. */
99877b66 3799 unsigned int twiddled_syms:1;
721956f4
AM
3800
3801 /* Incremented every time we size stubs. */
3802 unsigned int stub_iteration;
5d1634d7 3803
87d72d41
AM
3804 /* Small local sym cache. */
3805 struct sym_cache sym_cache;
65f38f15
AM
3806};
3807
4c52953f
AM
3808/* Rename some of the generic section flags to better document how they
3809 are used here. */
b0dddeec
AM
3810
3811/* Nonzero if this section has TLS related relocations. */
3812#define has_tls_reloc sec_flg0
3813
3814/* Nonzero if this section has a call to __tls_get_addr. */
3815#define has_tls_get_addr_call sec_flg1
3816
3817/* Nonzero if this section has any toc or got relocs. */
3818#define has_toc_reloc sec_flg2
3819
3820/* Nonzero if this section has a call to another section that uses
3821 the toc or got. */
d77c8a4b 3822#define makes_toc_func_call sec_flg3
b0dddeec
AM
3823
3824/* Recursion protection when determining above flag. */
d77c8a4b 3825#define call_check_in_progress sec_flg4
70cc837d 3826#define call_check_done sec_flg5
4c52953f 3827
65f38f15
AM
3828/* Get the ppc64 ELF linker hash table from a link_info structure. */
3829
3830#define ppc_hash_table(p) \
4dfe6ac6
NC
3831 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3832 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
65f38f15 3833
721956f4
AM
3834#define ppc_stub_hash_lookup(table, string, create, copy) \
3835 ((struct ppc_stub_hash_entry *) \
3836 bfd_hash_lookup ((table), (string), (create), (copy)))
3837
3838#define ppc_branch_hash_lookup(table, string, create, copy) \
3839 ((struct ppc_branch_hash_entry *) \
3840 bfd_hash_lookup ((table), (string), (create), (copy)))
3841
3842/* Create an entry in the stub hash table. */
3843
3844static struct bfd_hash_entry *
4ce794b7
AM
3845stub_hash_newfunc (struct bfd_hash_entry *entry,
3846 struct bfd_hash_table *table,
3847 const char *string)
721956f4
AM
3848{
3849 /* Allocate the structure if it has not already been allocated by a
3850 subclass. */
3851 if (entry == NULL)
3852 {
3853 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3854 if (entry == NULL)
3855 return entry;
3856 }
3857
3858 /* Call the allocation method of the superclass. */
3859 entry = bfd_hash_newfunc (entry, table, string);
3860 if (entry != NULL)
3861 {
3862 struct ppc_stub_hash_entry *eh;
3863
3864 /* Initialize the local fields. */
3865 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3866 eh->stub_type = ppc_stub_none;
721956f4
AM
3867 eh->stub_sec = NULL;
3868 eh->stub_offset = 0;
3869 eh->target_value = 0;
3870 eh->target_section = NULL;
721956f4
AM
3871 eh->h = NULL;
3872 eh->id_sec = NULL;
3873 }
3874
3875 return entry;
3876}
3877
3878/* Create an entry in the branch hash table. */
3879
3880static struct bfd_hash_entry *
4ce794b7
AM
3881branch_hash_newfunc (struct bfd_hash_entry *entry,
3882 struct bfd_hash_table *table,
3883 const char *string)
721956f4
AM
3884{
3885 /* Allocate the structure if it has not already been allocated by a
3886 subclass. */
3887 if (entry == NULL)
3888 {
3889 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3890 if (entry == NULL)
3891 return entry;
3892 }
3893
3894 /* Call the allocation method of the superclass. */
3895 entry = bfd_hash_newfunc (entry, table, string);
3896 if (entry != NULL)
3897 {
3898 struct ppc_branch_hash_entry *eh;
3899
3900 /* Initialize the local fields. */
3901 eh = (struct ppc_branch_hash_entry *) entry;
3902 eh->offset = 0;
3903 eh->iter = 0;
3904 }
3905
3906 return entry;
3907}
3908
65f38f15
AM
3909/* Create an entry in a ppc64 ELF linker hash table. */
3910
3911static struct bfd_hash_entry *
4ce794b7
AM
3912link_hash_newfunc (struct bfd_hash_entry *entry,
3913 struct bfd_hash_table *table,
3914 const char *string)
65f38f15
AM
3915{
3916 /* Allocate the structure if it has not already been allocated by a
3917 subclass. */
3918 if (entry == NULL)
3919 {
3920 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3921 if (entry == NULL)
3922 return entry;
3923 }
3924
3925 /* Call the allocation method of the superclass. */
3926 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3927 if (entry != NULL)
3928 {
3929 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3930
b3fac117 3931 memset (&eh->u.stub_cache, 0,
908b32fc 3932 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3933 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3934
3935 /* When making function calls, old ABI code references function entry
3936 points (dot symbols), while new ABI code references the function
3937 descriptor symbol. We need to make any combination of reference and
3938 definition work together, without breaking archive linking.
3939
3940 For a defined function "foo" and an undefined call to "bar":
3941 An old object defines "foo" and ".foo", references ".bar" (possibly
3942 "bar" too).
3943 A new object defines "foo" and references "bar".
3944
3945 A new object thus has no problem with its undefined symbols being
3946 satisfied by definitions in an old object. On the other hand, the
3947 old object won't have ".bar" satisfied by a new object.
3948
3949 Keep a list of newly added dot-symbols. */
3950
3951 if (string[0] == '.')
3952 {
3953 struct ppc_link_hash_table *htab;
3954
3955 htab = (struct ppc_link_hash_table *) table;
3956 eh->u.next_dot_sym = htab->dot_syms;
3957 htab->dot_syms = eh;
3958 }
65f38f15
AM
3959 }
3960
3961 return entry;
3962}
3963
3964/* Create a ppc64 ELF linker hash table. */
3965
3966static struct bfd_link_hash_table *
4ce794b7 3967ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3968{
3969 struct ppc_link_hash_table *htab;
3970 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3971
4ce794b7 3972 htab = bfd_zmalloc (amt);
65f38f15
AM
3973 if (htab == NULL)
3974 return NULL;
3975
66eb6687 3976 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4dfe6ac6
NC
3977 sizeof (struct ppc_link_hash_entry),
3978 PPC64_ELF_DATA))
65f38f15 3979 {
e2d34d7d 3980 free (htab);
65f38f15
AM
3981 return NULL;
3982 }
3983
721956f4 3984 /* Init the stub hash table too. */
66eb6687
AM
3985 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3986 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
3987 return NULL;
3988
3989 /* And the branch hash table. */
66eb6687
AM
3990 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3991 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
3992 return NULL;
3993
3254fd24
AM
3994 /* Initializing two fields of the union is just cosmetic. We really
3995 only care about glist, but when compiled on a 32-bit host the
3996 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3997 debugger inspection of these fields look nicer. */
a6aa5195
AM
3998 htab->elf.init_got_refcount.refcount = 0;
3999 htab->elf.init_got_refcount.glist = NULL;
4000 htab->elf.init_plt_refcount.refcount = 0;
4001 htab->elf.init_plt_refcount.glist = NULL;
4002 htab->elf.init_got_offset.offset = 0;
4003 htab->elf.init_got_offset.glist = NULL;
4004 htab->elf.init_plt_offset.offset = 0;
4005 htab->elf.init_plt_offset.glist = NULL;
3254fd24 4006
65f38f15
AM
4007 return &htab->elf.root;
4008}
4009
721956f4
AM
4010/* Free the derived linker hash table. */
4011
4012static void
4ce794b7 4013ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4
AM
4014{
4015 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
4016
4017 bfd_hash_table_free (&ret->stub_hash_table);
4018 bfd_hash_table_free (&ret->branch_hash_table);
4019 _bfd_generic_link_hash_table_free (hash);
4020}
4021
e717da7e
AM
4022/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4023
4024void
4025ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4026{
4027 struct ppc_link_hash_table *htab;
4028
4029 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4030
4031/* Always hook our dynamic sections into the first bfd, which is the
4032 linker created stub bfd. This ensures that the GOT header is at
4033 the start of the output TOC section. */
4034 htab = ppc_hash_table (info);
4dfe6ac6
NC
4035 if (htab == NULL)
4036 return;
e717da7e
AM
4037 htab->stub_bfd = abfd;
4038 htab->elf.dynobj = abfd;
4039}
4040
721956f4
AM
4041/* Build a name for an entry in the stub hash table. */
4042
4043static char *
4ce794b7
AM
4044ppc_stub_name (const asection *input_section,
4045 const asection *sym_sec,
4046 const struct ppc_link_hash_entry *h,
4047 const Elf_Internal_Rela *rel)
721956f4
AM
4048{
4049 char *stub_name;
4050 bfd_size_type len;
4051
4052 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4053 offsets from a sym as a branch target? In fact, we could
4054 probably assume the addend is always zero. */
4055 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4056
4057 if (h)
4058 {
4059 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4060 stub_name = bfd_malloc (len);
46de2a7c
AM
4061 if (stub_name == NULL)
4062 return stub_name;
4063
4064 sprintf (stub_name, "%08x.%s+%x",
4065 input_section->id & 0xffffffff,
4066 h->elf.root.root.string,
4067 (int) rel->r_addend & 0xffffffff);
721956f4
AM
4068 }
4069 else
4070 {
ad8e1ba5 4071 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 4072 stub_name = bfd_malloc (len);
46de2a7c
AM
4073 if (stub_name == NULL)
4074 return stub_name;
4075
4076 sprintf (stub_name, "%08x.%x:%x+%x",
4077 input_section->id & 0xffffffff,
4078 sym_sec->id & 0xffffffff,
4079 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4080 (int) rel->r_addend & 0xffffffff);
721956f4 4081 }
ee75fd95
AM
4082 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4083 stub_name[len - 2] = 0;
721956f4
AM
4084 return stub_name;
4085}
4086
4087/* Look up an entry in the stub hash. Stub entries are cached because
4088 creating the stub name takes a bit of time. */
4089
4090static struct ppc_stub_hash_entry *
4ce794b7
AM
4091ppc_get_stub_entry (const asection *input_section,
4092 const asection *sym_sec,
039b3fef 4093 struct ppc_link_hash_entry *h,
4ce794b7
AM
4094 const Elf_Internal_Rela *rel,
4095 struct ppc_link_hash_table *htab)
721956f4
AM
4096{
4097 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
4098 const asection *id_sec;
4099
4100 /* If this input section is part of a group of sections sharing one
4101 stub section, then use the id of the first section in the group.
4102 Stub names need to include a section id, as there may well be
4103 more than one stub used to reach say, printf, and we need to
4104 distinguish between them. */
4105 id_sec = htab->stub_group[input_section->id].link_sec;
4106
b3fac117
AM
4107 if (h != NULL && h->u.stub_cache != NULL
4108 && h->u.stub_cache->h == h
4109 && h->u.stub_cache->id_sec == id_sec)
721956f4 4110 {
b3fac117 4111 stub_entry = h->u.stub_cache;
721956f4
AM
4112 }
4113 else
4114 {
4115 char *stub_name;
4116
4117 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4118 if (stub_name == NULL)
4119 return NULL;
4120
4121 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 4122 stub_name, FALSE, FALSE);
721956f4 4123 if (h != NULL)
b3fac117 4124 h->u.stub_cache = stub_entry;
721956f4
AM
4125
4126 free (stub_name);
4127 }
4128
4129 return stub_entry;
4130}
4131
4132/* Add a new stub entry to the stub hash. Not all fields of the new
4133 stub entry are initialised. */
4134
4135static struct ppc_stub_hash_entry *
4ce794b7
AM
4136ppc_add_stub (const char *stub_name,
4137 asection *section,
4138 struct ppc_link_hash_table *htab)
721956f4
AM
4139{
4140 asection *link_sec;
4141 asection *stub_sec;
4142 struct ppc_stub_hash_entry *stub_entry;
4143
4144 link_sec = htab->stub_group[section->id].link_sec;
4145 stub_sec = htab->stub_group[section->id].stub_sec;
4146 if (stub_sec == NULL)
4147 {
4148 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4149 if (stub_sec == NULL)
4150 {
d4c88bbb 4151 size_t namelen;
721956f4
AM
4152 bfd_size_type len;
4153 char *s_name;
4154
d4c88bbb
AM
4155 namelen = strlen (link_sec->name);
4156 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
4157 s_name = bfd_alloc (htab->stub_bfd, len);
4158 if (s_name == NULL)
4159 return NULL;
4160
d4c88bbb
AM
4161 memcpy (s_name, link_sec->name, namelen);
4162 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
4163 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4164 if (stub_sec == NULL)
4165 return NULL;
4166 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4167 }
4168 htab->stub_group[section->id].stub_sec = stub_sec;
4169 }
4170
4171 /* Enter this entry into the linker stub hash table. */
4172 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 4173 TRUE, FALSE);
721956f4
AM
4174 if (stub_entry == NULL)
4175 {
d003868e
AM
4176 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
4177 section->owner, stub_name);
721956f4
AM
4178 return NULL;
4179 }
4180
4181 stub_entry->stub_sec = stub_sec;
4182 stub_entry->stub_offset = 0;
4183 stub_entry->id_sec = link_sec;
4184 return stub_entry;
4185}
4186
82bd7b59
AM
4187/* Create sections for linker generated code. */
4188
b34976b6 4189static bfd_boolean
4ce794b7 4190create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
4191{
4192 struct ppc_link_hash_table *htab;
4193 flagword flags;
4194
4195 htab = ppc_hash_table (info);
4dfe6ac6
NC
4196 if (htab == NULL)
4197 return FALSE;
82bd7b59
AM
4198
4199 /* Create .sfpr for code to save and restore fp regs. */
4200 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4201 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
4202 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4203 flags);
82bd7b59 4204 if (htab->sfpr == NULL
82bd7b59 4205 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 4206 return FALSE;
82bd7b59 4207
721956f4 4208 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
4209 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4210 flags);
4ce794b7 4211 if (htab->glink == NULL
ee4bf8d2 4212 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 4213 return FALSE;
82bd7b59 4214
e054468f
AM
4215 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4216 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4217 if (htab->iplt == NULL
4218 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4219 return FALSE;
4220
4221 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4222 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4223 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4224 ".rela.iplt",
4225 flags);
4226 if (htab->reliplt == NULL
4227 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4228 return FALSE;
4229
c456f082 4230 /* Create branch lookup table for plt_branch stubs. */
e4e0193e
AM
4231 flags = (SEC_ALLOC | SEC_LOAD
4232 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4233 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4234 flags);
4ce794b7 4235 if (htab->brlt == NULL
4ce794b7 4236 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 4237 return FALSE;
721956f4 4238
e4e0193e 4239 if (!info->shared)
c456f082
AM
4240 return TRUE;
4241
e4e0193e
AM
4242 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4243 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4244 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4245 ".rela.branch_lt",
4246 flags);
e054468f 4247 if (htab->relbrlt == NULL
c456f082
AM
4248 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4249 return FALSE;
4250
b34976b6 4251 return TRUE;
82bd7b59
AM
4252}
4253
e717da7e
AM
4254/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4255 not already done. */
65f38f15 4256
b34976b6 4257static bfd_boolean
e717da7e 4258create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 4259{
e717da7e
AM
4260 asection *got, *relgot;
4261 flagword flags;
4262 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 4263
0c8d6e5c 4264 if (!is_ppc64_elf (abfd))
0ffa91dd 4265 return FALSE;
4dfe6ac6
NC
4266 if (htab == NULL)
4267 return FALSE;
0ffa91dd 4268
4ce794b7 4269 if (!htab->got)
e717da7e
AM
4270 {
4271 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4272 return FALSE;
4273
4274 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
4275 if (!htab->got)
4276 abort ();
4277 }
4278
4279 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4280 | SEC_LINKER_CREATED);
4281
c456f082 4282 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 4283 if (!got
e717da7e
AM
4284 || !bfd_set_section_alignment (abfd, got, 3))
4285 return FALSE;
65f38f15 4286
c456f082
AM
4287 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4288 flags | SEC_READONLY);
e717da7e 4289 if (!relgot
e717da7e 4290 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 4291 return FALSE;
e717da7e
AM
4292
4293 ppc64_elf_tdata (abfd)->got = got;
4294 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 4295 return TRUE;
65f38f15 4296}
5bd4f169 4297
82bd7b59 4298/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 4299
b34976b6 4300static bfd_boolean
4ce794b7 4301ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 4302{
65f38f15 4303 struct ppc_link_hash_table *htab;
5bd4f169 4304
65f38f15 4305 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 4306 return FALSE;
65f38f15 4307
e717da7e 4308 htab = ppc_hash_table (info);
4dfe6ac6
NC
4309 if (htab == NULL)
4310 return FALSE;
4311
e717da7e
AM
4312 if (!htab->got)
4313 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
4314 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
4315 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
4316 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 4317 if (!info->shared)
4ce794b7 4318 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 4319
e717da7e 4320 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 4321 || (!info->shared && !htab->relbss))
65f38f15
AM
4322 abort ();
4323
b34976b6 4324 return TRUE;
5bd4f169
AM
4325}
4326
b31867b6
AM
4327/* Follow indirect and warning symbol links. */
4328
4329static inline struct bfd_link_hash_entry *
4330follow_link (struct bfd_link_hash_entry *h)
4331{
4332 while (h->type == bfd_link_hash_indirect
4333 || h->type == bfd_link_hash_warning)
4334 h = h->u.i.link;
4335 return h;
4336}
4337
4338static inline struct elf_link_hash_entry *
4339elf_follow_link (struct elf_link_hash_entry *h)
4340{
4341 return (struct elf_link_hash_entry *) follow_link (&h->root);
4342}
4343
4344static inline struct ppc_link_hash_entry *
4345ppc_follow_link (struct ppc_link_hash_entry *h)
4346{
4347 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4348}
4349
40d16e0b
AM
4350/* Merge PLT info on FROM with that on TO. */
4351
4352static void
4353move_plt_plist (struct ppc_link_hash_entry *from,
4354 struct ppc_link_hash_entry *to)
4355{
4356 if (from->elf.plt.plist != NULL)
4357 {
4358 if (to->elf.plt.plist != NULL)
4359 {
4360 struct plt_entry **entp;
4361 struct plt_entry *ent;
4362
4363 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4364 {
4365 struct plt_entry *dent;
4366
4367 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4368 if (dent->addend == ent->addend)
4369 {
4370 dent->plt.refcount += ent->plt.refcount;
4371 *entp = ent->next;
4372 break;
4373 }
4374 if (dent == NULL)
4375 entp = &ent->next;
4376 }
4377 *entp = to->elf.plt.plist;
4378 }
4379
4380 to->elf.plt.plist = from->elf.plt.plist;
4381 from->elf.plt.plist = NULL;
4382 }
4383}
4384
65f38f15
AM
4385/* Copy the extra info we tack onto an elf_link_hash_entry. */
4386
4387static void
fcfa13d2
AM
4388ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4389 struct elf_link_hash_entry *dir,
4390 struct elf_link_hash_entry *ind)
65f38f15
AM
4391{
4392 struct ppc_link_hash_entry *edir, *eind;
4393
4394 edir = (struct ppc_link_hash_entry *) dir;
4395 eind = (struct ppc_link_hash_entry *) ind;
4396
411e1bfb 4397 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 4398 if (eind->dyn_relocs != NULL)
65f38f15 4399 {
bbd7ec4a
AM
4400 if (edir->dyn_relocs != NULL)
4401 {
4402 struct ppc_dyn_relocs **pp;
4403 struct ppc_dyn_relocs *p;
4404
fcfa13d2 4405 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
4406 list. Merge any entries against the same section. */
4407 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4408 {
4409 struct ppc_dyn_relocs *q;
4410
4411 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4412 if (q->sec == p->sec)
4413 {
4414 q->pc_count += p->pc_count;
4415 q->count += p->count;
4416 *pp = p->next;
4417 break;
4418 }
4419 if (q == NULL)
4420 pp = &p->next;
4421 }
4422 *pp = edir->dyn_relocs;
4423 }
4424
65f38f15
AM
4425 edir->dyn_relocs = eind->dyn_relocs;
4426 eind->dyn_relocs = NULL;
4427 }
65f38f15 4428
6349e628
AM
4429 edir->is_func |= eind->is_func;
4430 edir->is_func_descriptor |= eind->is_func_descriptor;
58ac9f71 4431 edir->tls_mask |= eind->tls_mask;
b31867b6
AM
4432 if (eind->oh != NULL)
4433 edir->oh = ppc_follow_link (eind->oh);
6349e628 4434
81848ca0 4435 /* If called to transfer flags for a weakdef during processing
f5385ebf 4436 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
81848ca0 4437 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
f5385ebf
AM
4438 if (!(ELIMINATE_COPY_RELOCS
4439 && eind->elf.root.type != bfd_link_hash_indirect
4440 && edir->elf.dynamic_adjusted))
4441 edir->elf.non_got_ref |= eind->elf.non_got_ref;
81848ca0 4442
f5385ebf
AM
4443 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4444 edir->elf.ref_regular |= eind->elf.ref_regular;
4445 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4446 edir->elf.needs_plt |= eind->elf.needs_plt;
6349e628
AM
4447
4448 /* If we were called to copy over info for a weak sym, that's all. */
4449 if (eind->elf.root.type != bfd_link_hash_indirect)
4450 return;
4451
81848ca0
AM
4452 /* Copy over got entries that we may have already seen to the
4453 symbol which just became indirect. */
411e1bfb
AM
4454 if (eind->elf.got.glist != NULL)
4455 {
4456 if (edir->elf.got.glist != NULL)
4457 {
4458 struct got_entry **entp;
4459 struct got_entry *ent;
4460
4461 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4462 {
4463 struct got_entry *dent;
4464
4465 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4466 if (dent->addend == ent->addend
e717da7e 4467 && dent->owner == ent->owner
411e1bfb
AM
4468 && dent->tls_type == ent->tls_type)
4469 {
4470 dent->got.refcount += ent->got.refcount;
4471 *entp = ent->next;
4472 break;
4473 }
4474 if (dent == NULL)
4475 entp = &ent->next;
4476 }
4477 *entp = edir->elf.got.glist;
4478 }
4479
4480 edir->elf.got.glist = eind->elf.got.glist;
4481 eind->elf.got.glist = NULL;
4482 }
4483
4484 /* And plt entries. */
40d16e0b 4485 move_plt_plist (eind, edir);
411e1bfb 4486
fcfa13d2 4487 if (eind->elf.dynindx != -1)
411e1bfb 4488 {
fcfa13d2
AM
4489 if (edir->elf.dynindx != -1)
4490 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4491 edir->elf.dynstr_index);
411e1bfb
AM
4492 edir->elf.dynindx = eind->elf.dynindx;
4493 edir->elf.dynstr_index = eind->elf.dynstr_index;
4494 eind->elf.dynindx = -1;
4495 eind->elf.dynstr_index = 0;
4496 }
411e1bfb
AM
4497}
4498
8387904d
AM
4499/* Find the function descriptor hash entry from the given function code
4500 hash entry FH. Link the entries via their OH fields. */
4501
4502static struct ppc_link_hash_entry *
b31867b6 4503lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
8387904d
AM
4504{
4505 struct ppc_link_hash_entry *fdh = fh->oh;
4506
4507 if (fdh == NULL)
4508 {
4509 const char *fd_name = fh->elf.root.root.string + 1;
4510
4511 fdh = (struct ppc_link_hash_entry *)
4512 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
b31867b6
AM
4513 if (fdh == NULL)
4514 return fdh;
4515
4516 fdh->is_func_descriptor = 1;
4517 fdh->oh = fh;
4518 fh->is_func = 1;
4519 fh->oh = fdh;
8387904d
AM
4520 }
4521
b31867b6 4522 return ppc_follow_link (fdh);
8387904d
AM
4523}
4524
bb700d78
AM
4525/* Make a fake function descriptor sym for the code sym FH. */
4526
4527static struct ppc_link_hash_entry *
4528make_fdh (struct bfd_link_info *info,
908b32fc 4529 struct ppc_link_hash_entry *fh)
bb700d78
AM
4530{
4531 bfd *abfd;
4532 asymbol *newsym;
4533 struct bfd_link_hash_entry *bh;
4534 struct ppc_link_hash_entry *fdh;
4535
4536 abfd = fh->elf.root.u.undef.abfd;
4537 newsym = bfd_make_empty_symbol (abfd);
4538 newsym->name = fh->elf.root.root.string + 1;
4539 newsym->section = bfd_und_section_ptr;
4540 newsym->value = 0;
908b32fc 4541 newsym->flags = BSF_WEAK;
bb700d78
AM
4542
4543 bh = NULL;
4544 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4545 newsym->flags, newsym->section,
4546 newsym->value, NULL, FALSE, FALSE,
4547 &bh))
4548 return NULL;
4549
4550 fdh = (struct ppc_link_hash_entry *) bh;
4551 fdh->elf.non_elf = 0;
908b32fc
AM
4552 fdh->fake = 1;
4553 fdh->is_func_descriptor = 1;
4554 fdh->oh = fh;
4555 fh->is_func = 1;
4556 fh->oh = fdh;
bb700d78
AM
4557 return fdh;
4558}
4559
8387904d
AM
4560/* Fix function descriptor symbols defined in .opd sections to be
4561 function type. */
555cd476
AM
4562
4563static bfd_boolean
c16153ae 4564ppc64_elf_add_symbol_hook (bfd *ibfd,
e054468f 4565 struct bfd_link_info *info,
555cd476 4566 Elf_Internal_Sym *isym,
b3fac117 4567 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4568 flagword *flags ATTRIBUTE_UNUSED,
4569 asection **sec,
4570 bfd_vma *value ATTRIBUTE_UNUSED)
4571{
e054468f 4572 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
c16153ae
L
4573 {
4574 if ((ibfd->flags & DYNAMIC) == 0)
4575 elf_tdata (info->output_bfd)->has_ifunc_symbols = TRUE;
4576 }
e054468f
AM
4577 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4578 ;
4579 else if (*sec != NULL
70cc837d 4580 && strcmp ((*sec)->name, ".opd") == 0)
555cd476 4581 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4582
555cd476
AM
4583 return TRUE;
4584}
4585
8387904d 4586/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4587 inclusion of a new ABI object archive that defines "bar".
4588 NAME is a symbol defined in an archive. Return a symbol in the hash
4589 table that might be satisfied by the archive symbols. */
8387904d
AM
4590
4591static struct elf_link_hash_entry *
4592ppc64_elf_archive_symbol_lookup (bfd *abfd,
4593 struct bfd_link_info *info,
4594 const char *name)
4595{
4596 struct elf_link_hash_entry *h;
4597 char *dot_name;
4598 size_t len;
4599
4600 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4601 if (h != NULL
4602 /* Don't return this sym if it is a fake function descriptor
4603 created by add_symbol_adjust. */
4604 && !(h->root.type == bfd_link_hash_undefweak
4605 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4606 return h;
4607
4608 if (name[0] == '.')
4609 return h;
4610
4611 len = strlen (name);
4612 dot_name = bfd_alloc (abfd, len + 2);
4613 if (dot_name == NULL)
4614 return (struct elf_link_hash_entry *) 0 - 1;
4615 dot_name[0] = '.';
4616 memcpy (dot_name + 1, name, len + 1);
4617 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4618 bfd_release (abfd, dot_name);
4619 return h;
4620}
4621
4622/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4623 new ABI object defines "bar". Well, at least, undefined dot symbols
4624 are made weak. This stops later archive searches from including an
4625 object if we already have a function descriptor definition. It also
35b0ce59
AM
4626 prevents the linker complaining about undefined symbols.
4627 We also check and correct mismatched symbol visibility here. The
4628 most restrictive visibility of the function descriptor and the
4629 function entry symbol is used. */
8387904d
AM
4630
4631static bfd_boolean
b3fac117 4632add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4633{
8387904d
AM
4634 struct ppc_link_hash_table *htab;
4635 struct ppc_link_hash_entry *fdh;
4636
b3fac117 4637 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4638 return TRUE;
4639
b3fac117
AM
4640 if (eh->elf.root.type == bfd_link_hash_warning)
4641 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4642
b3fac117
AM
4643 if (eh->elf.root.root.string[0] != '.')
4644 abort ();
8387904d 4645
b3fac117 4646 htab = ppc_hash_table (info);
4dfe6ac6
NC
4647 if (htab == NULL)
4648 return FALSE;
4649
b31867b6
AM
4650 fdh = lookup_fdh (eh, htab);
4651 if (fdh == NULL)
4652 {
4653 if (!info->relocatable
4654 && (eh->elf.root.type == bfd_link_hash_undefined
4655 || eh->elf.root.type == bfd_link_hash_undefweak)
4656 && eh->elf.ref_regular)
4657 {
4658 /* Make an undefweak function descriptor sym, which is enough to
4659 pull in an --as-needed shared lib, but won't cause link
4660 errors. Archives are handled elsewhere. */
4661 fdh = make_fdh (info, eh);
4662 if (fdh == NULL)
4663 return FALSE;
4664 fdh->elf.ref_regular = 1;
4665 }
bb700d78 4666 }
b31867b6 4667 else
8387904d 4668 {
35b0ce59
AM
4669 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4670 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4671 if (entry_vis < descr_vis)
4672 fdh->elf.other += entry_vis - descr_vis;
4673 else if (entry_vis > descr_vis)
4674 eh->elf.other += descr_vis - entry_vis;
4675
e87d886e
AM
4676 if ((fdh->elf.root.type == bfd_link_hash_defined
4677 || fdh->elf.root.type == bfd_link_hash_defweak)
4678 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4679 {
4680 eh->elf.root.type = bfd_link_hash_undefweak;
4681 eh->was_undefined = 1;
4682 htab->twiddled_syms = 1;
4683 }
8387904d 4684 }
99877b66 4685
8387904d
AM
4686 return TRUE;
4687}
4688
b3fac117
AM
4689/* Process list of dot-symbols we made in link_hash_newfunc. */
4690
8387904d 4691static bfd_boolean
7d9616d7 4692ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
8387904d 4693{
99877b66 4694 struct ppc_link_hash_table *htab;
b3fac117 4695 struct ppc_link_hash_entry **p, *eh;
433817dd 4696
0c8d6e5c 4697 if (!is_ppc64_elf (info->output_bfd))
35b0ce59 4698 return TRUE;
4dfe6ac6
NC
4699 htab = ppc_hash_table (info);
4700 if (htab == NULL)
4701 return FALSE;
35b0ce59 4702
0c8d6e5c 4703 if (is_ppc64_elf (ibfd))
b3fac117
AM
4704 {
4705 p = &htab->dot_syms;
4706 while ((eh = *p) != NULL)
4707 {
4708 *p = NULL;
4709 if (!add_symbol_adjust (eh, info))
4710 return FALSE;
4711 p = &eh->u.next_dot_sym;
4712 }
4713 }
4714
4715 /* Clear the list for non-ppc64 input files. */
4716 p = &htab->dot_syms;
4717 while ((eh = *p) != NULL)
4718 {
4719 *p = NULL;
4720 p = &eh->u.next_dot_sym;
4721 }
99877b66
AM
4722
4723 /* We need to fix the undefs list for any syms we have twiddled to
4724 undef_weak. */
4725 if (htab->twiddled_syms)
4726 {
77cfaee6 4727 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4728 htab->twiddled_syms = 0;
4729 }
b3fac117 4730 return TRUE;
8387904d
AM
4731}
4732
97fed1c9
JJ
4733/* Undo hash table changes when an --as-needed input file is determined
4734 not to be needed. */
4735
4736static bfd_boolean
4737ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4738 struct bfd_link_info *info)
4739{
4dfe6ac6
NC
4740 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4741
4742 if (htab == NULL)
4743 return FALSE;
4744
4745 htab->dot_syms = NULL;
97fed1c9
JJ
4746 return TRUE;
4747}
4748
aa374f67
AM
4749/* If --just-symbols against a final linked binary, then assume we need
4750 toc adjusting stubs when calling functions defined there. */
4751
4752static void
4753ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4754{
4755 if ((sec->flags & SEC_CODE) != 0
4756 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4757 && is_ppc64_elf (sec->owner))
4758 {
4759 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4760 if (got != NULL
4761 && got->size >= elf_backend_got_header_size
4762 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4763 sec->has_toc_reloc = 1;
4764 }
4765 _bfd_elf_link_just_syms (sec, info);
4766}
4767
e054468f 4768static struct plt_entry **
4ce794b7
AM
4769update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4770 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4771{
4772 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e054468f 4773 struct plt_entry **local_plt;
f961d9dd 4774 unsigned char *local_got_tls_masks;
411e1bfb
AM
4775
4776 if (local_got_ents == NULL)
4777 {
4778 bfd_size_type size = symtab_hdr->sh_info;
4779
e054468f
AM
4780 size *= (sizeof (*local_got_ents)
4781 + sizeof (*local_plt)
4782 + sizeof (*local_got_tls_masks));
4ce794b7 4783 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb 4784 if (local_got_ents == NULL)
e054468f 4785 return NULL;
411e1bfb
AM
4786 elf_local_got_ents (abfd) = local_got_ents;
4787 }
4788
e054468f 4789 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
411e1bfb
AM
4790 {
4791 struct got_entry *ent;
4792
4793 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4794 if (ent->addend == r_addend
4795 && ent->owner == abfd
4796 && ent->tls_type == tls_type)
411e1bfb
AM
4797 break;
4798 if (ent == NULL)
4799 {
4800 bfd_size_type amt = sizeof (*ent);
4ce794b7 4801 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4802 if (ent == NULL)
4803 return FALSE;
4804 ent->next = local_got_ents[r_symndx];
4805 ent->addend = r_addend;
e717da7e 4806 ent->owner = abfd;
411e1bfb 4807 ent->tls_type = tls_type;
927be08e 4808 ent->is_indirect = FALSE;
411e1bfb
AM
4809 ent->got.refcount = 0;
4810 local_got_ents[r_symndx] = ent;
4811 }
4812 ent->got.refcount += 1;
4813 }
4814
e054468f 4815 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
f961d9dd 4816 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
e7b938ca 4817 local_got_tls_masks[r_symndx] |= tls_type;
e054468f
AM
4818
4819 return local_plt + r_symndx;
65f38f15
AM
4820}
4821
411e1bfb 4822static bfd_boolean
e054468f 4823update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
1e2f5b6e 4824{
411e1bfb 4825 struct plt_entry *ent;
1e2f5b6e 4826
e054468f 4827 for (ent = *plist; ent != NULL; ent = ent->next)
411e1bfb
AM
4828 if (ent->addend == addend)
4829 break;
4830 if (ent == NULL)
1e2f5b6e 4831 {
411e1bfb 4832 bfd_size_type amt = sizeof (*ent);
4ce794b7 4833 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4834 if (ent == NULL)
4835 return FALSE;
e054468f 4836 ent->next = *plist;
411e1bfb
AM
4837 ent->addend = addend;
4838 ent->plt.refcount = 0;
e054468f 4839 *plist = ent;
1e2f5b6e 4840 }
411e1bfb 4841 ent->plt.refcount += 1;
b34976b6 4842 return TRUE;
1e2f5b6e
AM
4843}
4844
e054468f
AM
4845static bfd_boolean
4846is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4847{
4848 return (r_type == R_PPC64_REL24
4849 || r_type == R_PPC64_REL14
4850 || r_type == R_PPC64_REL14_BRTAKEN
4851 || r_type == R_PPC64_REL14_BRNTAKEN
4852 || r_type == R_PPC64_ADDR24
4853 || r_type == R_PPC64_ADDR14
4854 || r_type == R_PPC64_ADDR14_BRTAKEN
4855 || r_type == R_PPC64_ADDR14_BRNTAKEN);
4856}
4857
5bd4f169 4858/* Look through the relocs for a section during the first phase, and
65f38f15 4859 calculate needed space in the global offset table, procedure
5d1634d7 4860 linkage table, and dynamic reloc sections. */
5bd4f169 4861
b34976b6 4862static bfd_boolean
4ce794b7
AM
4863ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4864 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4865{
65f38f15 4866 struct ppc_link_hash_table *htab;
5bd4f169 4867 Elf_Internal_Shdr *symtab_hdr;
c7e2358a 4868 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
4869 const Elf_Internal_Rela *rel;
4870 const Elf_Internal_Rela *rel_end;
5bd4f169 4871 asection *sreloc;
1e2f5b6e 4872 asection **opd_sym_map;
3a71aa26 4873 struct elf_link_hash_entry *tga, *dottga;
5bd4f169 4874
1049f94e 4875 if (info->relocatable)
b34976b6 4876 return TRUE;
5bd4f169 4877
680a3378
AM
4878 /* Don't do anything special with non-loaded, non-alloced sections.
4879 In particular, any relocs in such sections should not affect GOT
4880 and PLT reference counting (ie. we don't allow them to create GOT
4881 or PLT entries), there's no possibility or desire to optimize TLS
4882 relocs, and there's not much point in propagating relocs to shared
4883 libs that the dynamic linker won't relocate. */
4884 if ((sec->flags & SEC_ALLOC) == 0)
4885 return TRUE;
4886
0c8d6e5c 4887 BFD_ASSERT (is_ppc64_elf (abfd));
0ffa91dd 4888
65f38f15 4889 htab = ppc_hash_table (info);
4dfe6ac6
NC
4890 if (htab == NULL)
4891 return FALSE;
4892
3a71aa26
AM
4893 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4894 FALSE, FALSE, TRUE);
4895 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4896 FALSE, FALSE, TRUE);
0ffa91dd 4897 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 4898 sym_hashes = elf_sym_hashes (abfd);
5bd4f169 4899 sreloc = NULL;
1e2f5b6e 4900 opd_sym_map = NULL;
70cc837d 4901 if (strcmp (sec->name, ".opd") == 0)
1e2f5b6e
AM
4902 {
4903 /* Garbage collection needs some extra help with .opd sections.
4904 We don't want to necessarily keep everything referenced by
4905 relocs in .opd, as that would keep all functions. Instead,
4906 if we reference an .opd symbol (a function descriptor), we
4907 want to keep the function code symbol's section. This is
4908 easy for global symbols, but for local syms we need to keep
74f0fb50 4909 information about the associated function section. */
1e2f5b6e
AM
4910 bfd_size_type amt;
4911
74f0fb50 4912 amt = sec->size * sizeof (*opd_sym_map) / 8;
4ce794b7 4913 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4914 if (opd_sym_map == NULL)
b34976b6 4915 return FALSE;
74f0fb50 4916 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
7c8fe5c4
AM
4917 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4918 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4919 }
5bd4f169 4920
82bd7b59
AM
4921 if (htab->sfpr == NULL
4922 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4923 return FALSE;
82bd7b59 4924
5bd4f169
AM
4925 rel_end = relocs + sec->reloc_count;
4926 for (rel = relocs; rel < rel_end; rel++)
4927 {
4928 unsigned long r_symndx;
4929 struct elf_link_hash_entry *h;
04c9666a 4930 enum elf_ppc64_reloc_type r_type;
727fc41e 4931 int tls_type;
7c8fe5c4 4932 struct _ppc64_elf_section_data *ppc64_sec;
e054468f 4933 struct plt_entry **ifunc;
5bd4f169
AM
4934
4935 r_symndx = ELF64_R_SYM (rel->r_info);
4936 if (r_symndx < symtab_hdr->sh_info)
4937 h = NULL;
4938 else
973a3492
L
4939 {
4940 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 4941 h = elf_follow_link (h);
973a3492 4942 }
5bd4f169 4943
727fc41e 4944 tls_type = 0;
e054468f 4945 ifunc = NULL;
25f23106
AM
4946 if (h != NULL)
4947 {
4948 if (h->type == STT_GNU_IFUNC)
4949 {
4950 h->needs_plt = 1;
4951 ifunc = &h->plt.plist;
4952 }
4953 }
4954 else
4955 {
4956 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
4957 abfd, r_symndx);
4958 if (isym == NULL)
4959 return FALSE;
4960
4961 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4962 {
4963 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4964 rel->r_addend, PLT_IFUNC);
4965 if (ifunc == NULL)
4966 return FALSE;
4967 }
4968 }
4ce794b7 4969 r_type = ELF64_R_TYPE (rel->r_info);
e054468f
AM
4970 if (is_branch_reloc (r_type))
4971 {
4972 if (h != NULL && (h == tga || h == dottga))
4973 {
4974 if (rel != relocs
4975 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
4976 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
4977 /* We have a new-style __tls_get_addr call with a marker
4978 reloc. */
4979 ;
4980 else
4981 /* Mark this section as having an old-style call. */
4982 sec->has_tls_get_addr_call = 1;
4983 }
727fc41e 4984
e054468f 4985 /* STT_GNU_IFUNC symbols must have a PLT entry. */
e054468f
AM
4986 if (ifunc != NULL
4987 && !update_plt_info (abfd, ifunc, rel->r_addend))
4988 return FALSE;
4989 }
727fc41e 4990
a33d1f77 4991 switch (r_type)
5bd4f169 4992 {
727fc41e
AM
4993 case R_PPC64_TLSGD:
4994 case R_PPC64_TLSLD:
4995 /* These special tls relocs tie a call to __tls_get_addr with
4996 its parameter symbol. */
4997 break;
4998
411e1bfb
AM
4999 case R_PPC64_GOT_TLSLD16:
5000 case R_PPC64_GOT_TLSLD16_LO:
5001 case R_PPC64_GOT_TLSLD16_HI:
5002 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5003 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5004 goto dogottls;
5005
5006 case R_PPC64_GOT_TLSGD16:
5007 case R_PPC64_GOT_TLSGD16_LO:
5008 case R_PPC64_GOT_TLSGD16_HI:
5009 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5010 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5011 goto dogottls;
5012
5013 case R_PPC64_GOT_TPREL16_DS:
5014 case R_PPC64_GOT_TPREL16_LO_DS:
5015 case R_PPC64_GOT_TPREL16_HI:
5016 case R_PPC64_GOT_TPREL16_HA:
1d483afe 5017 if (!info->executable)
411e1bfb
AM
5018 info->flags |= DF_STATIC_TLS;
5019 tls_type = TLS_TLS | TLS_TPREL;
5020 goto dogottls;
5021
5022 case R_PPC64_GOT_DTPREL16_DS:
5023 case R_PPC64_GOT_DTPREL16_LO_DS:
5024 case R_PPC64_GOT_DTPREL16_HI:
5025 case R_PPC64_GOT_DTPREL16_HA:
5026 tls_type = TLS_TLS | TLS_DTPREL;
5027 dogottls:
5028 sec->has_tls_reloc = 1;
5029 /* Fall thru */
5030
5bd4f169 5031 case R_PPC64_GOT16:
5bd4f169 5032 case R_PPC64_GOT16_DS:
65f38f15
AM
5033 case R_PPC64_GOT16_HA:
5034 case R_PPC64_GOT16_HI:
5035 case R_PPC64_GOT16_LO:
5bd4f169 5036 case R_PPC64_GOT16_LO_DS:
65f38f15 5037 /* This symbol requires a global offset table entry. */
4c52953f 5038 sec->has_toc_reloc = 1;
33c0ec9d
AM
5039 if (r_type == R_PPC64_GOT_TLSLD16
5040 || r_type == R_PPC64_GOT_TLSGD16
5041 || r_type == R_PPC64_GOT_TPREL16_DS
5042 || r_type == R_PPC64_GOT_DTPREL16_DS
5043 || r_type == R_PPC64_GOT16
5044 || r_type == R_PPC64_GOT16_DS)
5045 {
5046 htab->do_multi_toc = 1;
d77c8a4b 5047 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
33c0ec9d
AM
5048 }
5049
e717da7e
AM
5050 if (ppc64_elf_tdata (abfd)->got == NULL
5051 && !create_got_section (abfd, info))
b34976b6 5052 return FALSE;
5bd4f169
AM
5053
5054 if (h != NULL)
5055 {
411e1bfb
AM
5056 struct ppc_link_hash_entry *eh;
5057 struct got_entry *ent;
65f38f15 5058
411e1bfb
AM
5059 eh = (struct ppc_link_hash_entry *) h;
5060 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5061 if (ent->addend == rel->r_addend
e717da7e 5062 && ent->owner == abfd
411e1bfb
AM
5063 && ent->tls_type == tls_type)
5064 break;
5065 if (ent == NULL)
5bd4f169 5066 {
411e1bfb 5067 bfd_size_type amt = sizeof (*ent);
4ce794b7 5068 ent = bfd_alloc (abfd, amt);
411e1bfb 5069 if (ent == NULL)
b34976b6 5070 return FALSE;
411e1bfb
AM
5071 ent->next = eh->elf.got.glist;
5072 ent->addend = rel->r_addend;
e717da7e 5073 ent->owner = abfd;
411e1bfb 5074 ent->tls_type = tls_type;
927be08e 5075 ent->is_indirect = FALSE;
411e1bfb
AM
5076 ent->got.refcount = 0;
5077 eh->elf.got.glist = ent;
5bd4f169 5078 }
411e1bfb 5079 ent->got.refcount += 1;
e7b938ca 5080 eh->tls_mask |= tls_type;
5bd4f169 5081 }
411e1bfb
AM
5082 else
5083 /* This is a global offset table entry for a local symbol. */
5084 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5085 rel->r_addend, tls_type))
5086 return FALSE;
5bd4f169
AM
5087 break;
5088
5bd4f169 5089 case R_PPC64_PLT16_HA:
65f38f15
AM
5090 case R_PPC64_PLT16_HI:
5091 case R_PPC64_PLT16_LO:
5092 case R_PPC64_PLT32:
5093 case R_PPC64_PLT64:
5bd4f169 5094 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
5095 actually build the entry in adjust_dynamic_symbol,
5096 because this might be a case of linking PIC code without
5097 linking in any dynamic objects, in which case we don't
5098 need to generate a procedure linkage table after all. */
5bd4f169
AM
5099 if (h == NULL)
5100 {
5101 /* It does not make sense to have a procedure linkage
3fad3c7c 5102 table entry for a local symbol. */
5bd4f169 5103 bfd_set_error (bfd_error_bad_value);
b34976b6 5104 return FALSE;
5bd4f169 5105 }
411e1bfb 5106 else
e054468f
AM
5107 {
5108 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5109 return FALSE;
5110 h->needs_plt = 1;
5111 if (h->root.root.string[0] == '.'
5112 && h->root.root.string[1] != '\0')
5113 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5114 }
5bd4f169
AM
5115 break;
5116
5117 /* The following relocations don't need to propagate the
5118 relocation if linking a shared object since they are
5119 section relative. */
5120 case R_PPC64_SECTOFF:
5121 case R_PPC64_SECTOFF_LO:
5122 case R_PPC64_SECTOFF_HI:
5123 case R_PPC64_SECTOFF_HA:
5124 case R_PPC64_SECTOFF_DS:
5125 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
5126 case R_PPC64_DTPREL16:
5127 case R_PPC64_DTPREL16_LO:
5128 case R_PPC64_DTPREL16_HI:
5129 case R_PPC64_DTPREL16_HA:
5130 case R_PPC64_DTPREL16_DS:
5131 case R_PPC64_DTPREL16_LO_DS:
5132 case R_PPC64_DTPREL16_HIGHER:
5133 case R_PPC64_DTPREL16_HIGHERA:
5134 case R_PPC64_DTPREL16_HIGHEST:
5135 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
5136 break;
5137
ad8e1ba5 5138 /* Nor do these. */
25f23106
AM
5139 case R_PPC64_REL16:
5140 case R_PPC64_REL16_LO:
5141 case R_PPC64_REL16_HI:
5142 case R_PPC64_REL16_HA:
5143 break;
5144
ad8e1ba5 5145 case R_PPC64_TOC16:
33c0ec9d
AM
5146 case R_PPC64_TOC16_DS:
5147 htab->do_multi_toc = 1;
d77c8a4b 5148 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
ad8e1ba5
AM
5149 case R_PPC64_TOC16_LO:
5150 case R_PPC64_TOC16_HI:
5151 case R_PPC64_TOC16_HA:
ad8e1ba5 5152 case R_PPC64_TOC16_LO_DS:
4c52953f 5153 sec->has_toc_reloc = 1;
ad8e1ba5
AM
5154 break;
5155
5bd4f169
AM
5156 /* This relocation describes the C++ object vtable hierarchy.
5157 Reconstruct it for later use during GC. */
5158 case R_PPC64_GNU_VTINHERIT:
c152c796 5159 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 5160 return FALSE;
5bd4f169
AM
5161 break;
5162
5163 /* This relocation describes which C++ vtable entries are actually
5164 used. Record for later use during GC. */
5165 case R_PPC64_GNU_VTENTRY:
d17e0c6e
JB
5166 BFD_ASSERT (h != NULL);
5167 if (h != NULL
5168 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 5169 return FALSE;
5bd4f169
AM
5170 break;
5171
721956f4
AM
5172 case R_PPC64_REL14:
5173 case R_PPC64_REL14_BRTAKEN:
5174 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
5175 {
5176 asection *dest = NULL;
5177
5178 /* Heuristic: If jumping outside our section, chances are
5179 we are going to need a stub. */
5180 if (h != NULL)
5181 {
5182 /* If the sym is weak it may be overridden later, so
5183 don't assume we know where a weak sym lives. */
5184 if (h->root.type == bfd_link_hash_defined)
5185 dest = h->root.u.def.section;
5186 }
5187 else
87d72d41
AM
5188 {
5189 Elf_Internal_Sym *isym;
5190
5191 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5192 abfd, r_symndx);
5193 if (isym == NULL)
5194 return FALSE;
5195
5196 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5197 }
5198
220c76dd 5199 if (dest != sec)
7c8fe5c4 5200 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 5201 }
721956f4
AM
5202 /* Fall through. */
5203
5d1634d7 5204 case R_PPC64_REL24:
e054468f 5205 if (h != NULL && ifunc == NULL)
5d1634d7
AM
5206 {
5207 /* We may need a .plt entry if the function this reloc
5208 refers to is in a shared lib. */
e054468f 5209 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
411e1bfb 5210 return FALSE;
e054468f
AM
5211 h->needs_plt = 1;
5212 if (h->root.root.string[0] == '.'
5213 && h->root.root.string[1] != '\0')
5214 ((struct ppc_link_hash_entry *) h)->is_func = 1;
3a71aa26 5215 if (h == tga || h == dottga)
411e1bfb 5216 sec->has_tls_reloc = 1;
411e1bfb
AM
5217 }
5218 break;
5219
5220 case R_PPC64_TPREL64:
5221 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
1d483afe 5222 if (!info->executable)
411e1bfb
AM
5223 info->flags |= DF_STATIC_TLS;
5224 goto dotlstoc;
5225
5226 case R_PPC64_DTPMOD64:
5227 if (rel + 1 < rel_end
5228 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5229 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 5230 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 5231 else
951fd09b 5232 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
5233 goto dotlstoc;
5234
5235 case R_PPC64_DTPREL64:
5236 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5237 if (rel != relocs
5238 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5239 && rel[-1].r_offset == rel->r_offset - 8)
5240 /* This is the second reloc of a dtpmod, dtprel pair.
5241 Don't mark with TLS_DTPREL. */
5242 goto dodyn;
5243
5244 dotlstoc:
5245 sec->has_tls_reloc = 1;
5246 if (h != NULL)
5247 {
5248 struct ppc_link_hash_entry *eh;
5249 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 5250 eh->tls_mask |= tls_type;
411e1bfb
AM
5251 }
5252 else
5253 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5254 rel->r_addend, tls_type))
5255 return FALSE;
5256
7c8fe5c4
AM
5257 ppc64_sec = ppc64_elf_section_data (sec);
5258 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 5259 {
3a71aa26
AM
5260 bfd_size_type amt;
5261
e7b938ca 5262 /* One extra to simplify get_tls_mask. */
3a71aa26
AM
5263 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5264 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5265 if (ppc64_sec->u.toc.symndx == NULL)
5266 return FALSE;
5267 amt = sec->size * sizeof (bfd_vma) / 8;
5268 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5269 if (ppc64_sec->u.toc.add == NULL)
411e1bfb 5270 return FALSE;
7c8fe5c4
AM
5271 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5272 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
5273 }
5274 BFD_ASSERT (rel->r_offset % 8 == 0);
3a71aa26
AM
5275 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5276 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
951fd09b
AM
5277
5278 /* Mark the second slot of a GD or LD entry.
5279 -1 to indicate GD and -2 to indicate LD. */
5280 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
3a71aa26 5281 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 5282 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
3a71aa26 5283 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
5284 goto dodyn;
5285
5286 case R_PPC64_TPREL16:
5287 case R_PPC64_TPREL16_LO:
5288 case R_PPC64_TPREL16_HI:
5289 case R_PPC64_TPREL16_HA:
5290 case R_PPC64_TPREL16_DS:
5291 case R_PPC64_TPREL16_LO_DS:
5292 case R_PPC64_TPREL16_HIGHER:
5293 case R_PPC64_TPREL16_HIGHERA:
5294 case R_PPC64_TPREL16_HIGHEST:
5295 case R_PPC64_TPREL16_HIGHESTA:
5296 if (info->shared)
5297 {
1d483afe
AM
5298 if (!info->executable)
5299 info->flags |= DF_STATIC_TLS;
411e1bfb 5300 goto dodyn;
5d1634d7
AM
5301 }
5302 break;
5303
e86ce104 5304 case R_PPC64_ADDR64:
1e2f5b6e 5305 if (opd_sym_map != NULL
1e2f5b6e 5306 && rel + 1 < rel_end
4ce794b7 5307 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 5308 {
8387904d
AM
5309 if (h != NULL)
5310 {
5311 if (h->root.root.string[0] == '.'
5312 && h->root.root.string[1] != 0
b31867b6 5313 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
8387904d
AM
5314 ;
5315 else
5316 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5317 }
5318 else
5319 {
5320 asection *s;
87d72d41 5321 Elf_Internal_Sym *isym;
1e2f5b6e 5322
87d72d41
AM
5323 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5324 abfd, r_symndx);
5325 if (isym == NULL)
8387904d 5326 return FALSE;
87d72d41
AM
5327
5328 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5329 if (s != NULL && s != sec)
3f764659 5330 opd_sym_map[rel->r_offset / 8] = s;
8387904d 5331 }
1e2f5b6e 5332 }
e86ce104
AM
5333 /* Fall through. */
5334
04c9666a 5335 case R_PPC64_REL30:
5bd4f169 5336 case R_PPC64_REL32:
04c9666a 5337 case R_PPC64_REL64:
65f38f15
AM
5338 case R_PPC64_ADDR14:
5339 case R_PPC64_ADDR14_BRNTAKEN:
5340 case R_PPC64_ADDR14_BRTAKEN:
5341 case R_PPC64_ADDR16:
5342 case R_PPC64_ADDR16_DS:
5343 case R_PPC64_ADDR16_HA:
5344 case R_PPC64_ADDR16_HI:
5345 case R_PPC64_ADDR16_HIGHER:
5346 case R_PPC64_ADDR16_HIGHERA:
5347 case R_PPC64_ADDR16_HIGHEST:
5348 case R_PPC64_ADDR16_HIGHESTA:
5349 case R_PPC64_ADDR16_LO:
5350 case R_PPC64_ADDR16_LO_DS:
5351 case R_PPC64_ADDR24:
65f38f15 5352 case R_PPC64_ADDR32:
65f38f15
AM
5353 case R_PPC64_UADDR16:
5354 case R_PPC64_UADDR32:
5355 case R_PPC64_UADDR64:
5bd4f169 5356 case R_PPC64_TOC:
81848ca0
AM
5357 if (h != NULL && !info->shared)
5358 /* We may need a copy reloc. */
f5385ebf 5359 h->non_got_ref = 1;
81848ca0 5360
41bd81ab 5361 /* Don't propagate .opd relocs. */
1e2f5b6e 5362 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 5363 break;
e86ce104 5364
65f38f15
AM
5365 /* If we are creating a shared library, and this is a reloc
5366 against a global symbol, or a non PC relative reloc
5367 against a local symbol, then we need to copy the reloc
5368 into the shared library. However, if we are linking with
5369 -Bsymbolic, we do not need to copy a reloc against a
5370 global symbol which is defined in an object we are
5371 including in the link (i.e., DEF_REGULAR is set). At
5372 this point we have not seen all the input files, so it is
5373 possible that DEF_REGULAR is not set now but will be set
5374 later (it is never cleared). In case of a weak definition,
5375 DEF_REGULAR may be cleared later by a strong definition in
5376 a shared library. We account for that possibility below by
f4656909 5377 storing information in the dyn_relocs field of the hash
65f38f15
AM
5378 table entry. A similar situation occurs when creating
5379 shared libraries and symbol visibility changes render the
5380 symbol local.
5381
5382 If on the other hand, we are creating an executable, we
5383 may need to keep relocations for symbols satisfied by a
5384 dynamic library if we manage to avoid copy relocs for the
5385 symbol. */
411e1bfb 5386 dodyn:
65f38f15 5387 if ((info->shared
1d483afe 5388 && (must_be_dyn_reloc (info, r_type)
65f38f15
AM
5389 || (h != NULL
5390 && (! info->symbolic
5391 || h->root.type == bfd_link_hash_defweak
f5385ebf 5392 || !h->def_regular))))
f4656909
AM
5393 || (ELIMINATE_COPY_RELOCS
5394 && !info->shared
65f38f15
AM
5395 && h != NULL
5396 && (h->root.type == bfd_link_hash_defweak
25f23106
AM
5397 || !h->def_regular))
5398 || (!info->shared
5399 && ifunc != NULL))
5bd4f169 5400 {
ec338859
AM
5401 struct ppc_dyn_relocs *p;
5402 struct ppc_dyn_relocs **head;
5403
65f38f15
AM
5404 /* We must copy these reloc types into the output file.
5405 Create a reloc section in dynobj and make room for
5406 this reloc. */
5bd4f169
AM
5407 if (sreloc == NULL)
5408 {
83bac4b0
NC
5409 sreloc = _bfd_elf_make_dynamic_reloc_section
5410 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
65f38f15 5411
5bd4f169 5412 if (sreloc == NULL)
83bac4b0 5413 return FALSE;
5bd4f169
AM
5414 }
5415
65f38f15
AM
5416 /* If this is a global symbol, we count the number of
5417 relocations we need for this symbol. */
5418 if (h != NULL)
5419 {
ec338859 5420 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
5421 }
5422 else
5423 {
ec338859
AM
5424 /* Track dynamic relocs needed for local syms too.
5425 We really need local syms available to do this
5426 easily. Oh well. */
ec338859 5427 asection *s;
6edfbbad 5428 void *vpp;
87d72d41 5429 Elf_Internal_Sym *isym;
6edfbbad 5430
87d72d41
AM
5431 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5432 abfd, r_symndx);
5433 if (isym == NULL)
b34976b6 5434 return FALSE;
ec338859 5435
87d72d41
AM
5436 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5437 if (s == NULL)
5438 s = sec;
5439
6edfbbad
DJ
5440 vpp = &elf_section_data (s)->local_dynrel;
5441 head = (struct ppc_dyn_relocs **) vpp;
65f38f15 5442 }
ec338859
AM
5443
5444 p = *head;
5445 if (p == NULL || p->sec != sec)
5446 {
4ce794b7 5447 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 5448 if (p == NULL)
b34976b6 5449 return FALSE;
ec338859
AM
5450 p->next = *head;
5451 *head = p;
5452 p->sec = sec;
5453 p->count = 0;
5454 p->pc_count = 0;
5455 }
5456
5457 p->count += 1;
1d483afe 5458 if (!must_be_dyn_reloc (info, r_type))
ec338859 5459 p->pc_count += 1;
65f38f15 5460 }
5bd4f169 5461 break;
65f38f15
AM
5462
5463 default:
96e0dda4 5464 break;
5bd4f169
AM
5465 }
5466 }
5467
b34976b6 5468 return TRUE;
5bd4f169
AM
5469}
5470
8387904d
AM
5471/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5472 of the code entry point, and its section. */
5473
5474static bfd_vma
5475opd_entry_value (asection *opd_sec,
5476 bfd_vma offset,
5477 asection **code_sec,
5478 bfd_vma *code_off)
5479{
5480 bfd *opd_bfd = opd_sec->owner;
8860955f 5481 Elf_Internal_Rela *relocs;
8387904d 5482 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 5483 bfd_vma val;
8387904d 5484
4b85d634
AM
5485 /* No relocs implies we are linking a --just-symbols object. */
5486 if (opd_sec->reloc_count == 0)
5487 {
aa374f67
AM
5488 char buf[8];
5489
5490 if (!bfd_get_section_contents (opd_bfd, opd_sec, buf, offset, 8))
4b85d634 5491 return (bfd_vma) -1;
3b36f7e6 5492
aa374f67 5493 val = bfd_get_64 (opd_bfd, buf);
4b85d634
AM
5494 if (code_sec != NULL)
5495 {
5496 asection *sec, *likely = NULL;
5497 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5498 if (sec->vma <= val
5499 && (sec->flags & SEC_LOAD) != 0
5500 && (sec->flags & SEC_ALLOC) != 0)
5501 likely = sec;
5502 if (likely != NULL)
5503 {
5504 *code_sec = likely;
5505 if (code_off != NULL)
5506 *code_off = val - likely->vma;
5507 }
5508 }
5509 return val;
5510 }
5511
0c8d6e5c 5512 BFD_ASSERT (is_ppc64_elf (opd_bfd));
0ffa91dd 5513
8860955f
AM
5514 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
5515 if (relocs == NULL)
5516 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 5517
8387904d 5518 /* Go find the opd reloc at the sym address. */
8860955f 5519 lo = relocs;
8387904d
AM
5520 BFD_ASSERT (lo != NULL);
5521 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 5522 val = (bfd_vma) -1;
8387904d
AM
5523 while (lo < hi)
5524 {
5525 look = lo + (hi - lo) / 2;
5526 if (look->r_offset < offset)
5527 lo = look + 1;
5528 else if (look->r_offset > offset)
5529 hi = look;
5530 else
5531 {
0ffa91dd
NC
5532 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5533
8387904d
AM
5534 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5535 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5536 {
5537 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
5538 asection *sec;
5539
5540 if (symndx < symtab_hdr->sh_info)
5541 {
5542 Elf_Internal_Sym *sym;
5543
5544 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5545 if (sym == NULL)
5546 {
5547 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5548 symtab_hdr->sh_info,
5549 0, NULL, NULL, NULL);
5550 if (sym == NULL)
645ea6a9 5551 break;
8387904d
AM
5552 symtab_hdr->contents = (bfd_byte *) sym;
5553 }
5554
5555 sym += symndx;
5556 val = sym->st_value;
cb33740c 5557 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
8387904d
AM
5558 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5559 }
5560 else
5561 {
5562 struct elf_link_hash_entry **sym_hashes;
5563 struct elf_link_hash_entry *rh;
5564
5565 sym_hashes = elf_sym_hashes (opd_bfd);
5566 rh = sym_hashes[symndx - symtab_hdr->sh_info];
b31867b6 5567 rh = elf_follow_link (rh);
8387904d
AM
5568 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5569 || rh->root.type == bfd_link_hash_defweak);
5570 val = rh->root.u.def.value;
5571 sec = rh->root.u.def.section;
5572 }
5573 val += look->r_addend;
5574 if (code_off != NULL)
5575 *code_off = val;
5576 if (code_sec != NULL)
5577 *code_sec = sec;
5578 if (sec != NULL && sec->output_section != NULL)
5579 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5580 }
5581 break;
5582 }
5583 }
645ea6a9 5584
645ea6a9 5585 return val;
8387904d
AM
5586}
5587
854b41e7
AM
5588/* Return true if symbol is defined in a regular object file. */
5589
5590static bfd_boolean
5591is_static_defined (struct elf_link_hash_entry *h)
5592{
5593 return ((h->root.type == bfd_link_hash_defined
5594 || h->root.type == bfd_link_hash_defweak)
5595 && h->root.u.def.section != NULL
5596 && h->root.u.def.section->output_section != NULL);
5597}
5598
b31867b6
AM
5599/* If FDH is a function descriptor symbol, return the associated code
5600 entry symbol if it is defined. Return NULL otherwise. */
5601
5602static struct ppc_link_hash_entry *
5603defined_code_entry (struct ppc_link_hash_entry *fdh)
5604{
5605 if (fdh->is_func_descriptor)
5606 {
5607 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5608 if (fh->elf.root.type == bfd_link_hash_defined
5609 || fh->elf.root.type == bfd_link_hash_defweak)
5610 return fh;
5611 }
5612 return NULL;
5613}
5614
5615/* If FH is a function code entry symbol, return the associated
5616 function descriptor symbol if it is defined. Return NULL otherwise. */
5617
5618static struct ppc_link_hash_entry *
5619defined_func_desc (struct ppc_link_hash_entry *fh)
5620{
5621 if (fh->oh != NULL
5622 && fh->oh->is_func_descriptor)
5623 {
5624 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5625 if (fdh->elf.root.type == bfd_link_hash_defined
5626 || fdh->elf.root.type == bfd_link_hash_defweak)
5627 return fdh;
5628 }
5629 return NULL;
5630}
5631
74f0fb50
AM
5632/* Mark all our entry sym sections, both opd and code section. */
5633
5634static void
5635ppc64_elf_gc_keep (struct bfd_link_info *info)
5636{
5637 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5638 struct bfd_sym_chain *sym;
5639
4dfe6ac6
NC
5640 if (htab == NULL)
5641 return;
5642
74f0fb50
AM
5643 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5644 {
b31867b6 5645 struct ppc_link_hash_entry *eh, *fh;
74f0fb50
AM
5646 asection *sec;
5647
5648 eh = (struct ppc_link_hash_entry *)
b31867b6 5649 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
74f0fb50
AM
5650 if (eh == NULL)
5651 continue;
5652 if (eh->elf.root.type != bfd_link_hash_defined
5653 && eh->elf.root.type != bfd_link_hash_defweak)
5654 continue;
5655
b31867b6
AM
5656 fh = defined_code_entry (eh);
5657 if (fh != NULL)
74f0fb50 5658 {
b31867b6 5659 sec = fh->elf.root.u.def.section;
74f0fb50
AM
5660 sec->flags |= SEC_KEEP;
5661 }
5662 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5663 && opd_entry_value (eh->elf.root.u.def.section,
5664 eh->elf.root.u.def.value,
5665 &sec, NULL) != (bfd_vma) -1)
5666 sec->flags |= SEC_KEEP;
5667
5668 sec = eh->elf.root.u.def.section;
5669 sec->flags |= SEC_KEEP;
5670 }
5671}
5672
64d03ab5
AM
5673/* Mark sections containing dynamically referenced symbols. When
5674 building shared libraries, we must assume that any visible symbol is
5675 referenced. */
5676
5677static bfd_boolean
5678ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5679{
5680 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5681 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
b31867b6 5682 struct ppc_link_hash_entry *fdh;
64d03ab5
AM
5683
5684 if (eh->elf.root.type == bfd_link_hash_warning)
5685 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5686
5687 /* Dynamic linking info is on the func descriptor sym. */
b31867b6
AM
5688 fdh = defined_func_desc (eh);
5689 if (fdh != NULL)
5690 eh = fdh;
64d03ab5
AM
5691
5692 if ((eh->elf.root.type == bfd_link_hash_defined
5693 || eh->elf.root.type == bfd_link_hash_defweak)
5694 && (eh->elf.ref_dynamic
5695 || (!info->executable
5696 && eh->elf.def_regular
5697 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5698 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5699 {
5700 asection *code_sec;
b31867b6 5701 struct ppc_link_hash_entry *fh;
64d03ab5
AM
5702
5703 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5704
5705 /* Function descriptor syms cause the associated
5706 function code sym section to be marked. */
b31867b6
AM
5707 fh = defined_code_entry (eh);
5708 if (fh != NULL)
5709 {
5710 code_sec = fh->elf.root.u.def.section;
5711 code_sec->flags |= SEC_KEEP;
5712 }
64d03ab5
AM
5713 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5714 && opd_entry_value (eh->elf.root.u.def.section,
5715 eh->elf.root.u.def.value,
5716 &code_sec, NULL) != (bfd_vma) -1)
5717 code_sec->flags |= SEC_KEEP;
5718 }
5719
5720 return TRUE;
5721}
5722
5bd4f169
AM
5723/* Return the section that should be marked against GC for a given
5724 relocation. */
5725
5726static asection *
4ce794b7 5727ppc64_elf_gc_mark_hook (asection *sec,
fb34365b 5728 struct bfd_link_info *info,
4ce794b7
AM
5729 Elf_Internal_Rela *rel,
5730 struct elf_link_hash_entry *h,
5731 Elf_Internal_Sym *sym)
5bd4f169 5732{
ccfa59ea
AM
5733 asection *rsec;
5734
ccfa59ea
AM
5735 /* Syms return NULL if we're marking .opd, so we avoid marking all
5736 function sections, as all functions are referenced in .opd. */
5737 rsec = NULL;
5738 if (get_opd_info (sec) != NULL)
5739 return rsec;
1e2f5b6e 5740
5bd4f169
AM
5741 if (h != NULL)
5742 {
04c9666a 5743 enum elf_ppc64_reloc_type r_type;
b31867b6 5744 struct ppc_link_hash_entry *eh, *fh, *fdh;
a33d1f77 5745
4ce794b7 5746 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5747 switch (r_type)
5bd4f169
AM
5748 {
5749 case R_PPC64_GNU_VTINHERIT:
5750 case R_PPC64_GNU_VTENTRY:
5751 break;
5752
5753 default:
5754 switch (h->root.type)
5755 {
5756 case bfd_link_hash_defined:
5757 case bfd_link_hash_defweak:
ccfa59ea 5758 eh = (struct ppc_link_hash_entry *) h;
b31867b6
AM
5759 fdh = defined_func_desc (eh);
5760 if (fdh != NULL)
5761 eh = fdh;
1e2f5b6e
AM
5762
5763 /* Function descriptor syms cause the associated
5764 function code sym section to be marked. */
b31867b6
AM
5765 fh = defined_code_entry (eh);
5766 if (fh != NULL)
ccfa59ea
AM
5767 {
5768 /* They also mark their opd section. */
74f0fb50 5769 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5770
b31867b6 5771 rsec = fh->elf.root.u.def.section;
ccfa59ea 5772 }
8387904d
AM
5773 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5774 && opd_entry_value (eh->elf.root.u.def.section,
5775 eh->elf.root.u.def.value,
5776 &rsec, NULL) != (bfd_vma) -1)
74f0fb50 5777 eh->elf.root.u.def.section->gc_mark = 1;
ccfa59ea 5778 else
1e2f5b6e
AM
5779 rsec = h->root.u.def.section;
5780 break;
5bd4f169
AM
5781
5782 case bfd_link_hash_common:
1e2f5b6e
AM
5783 rsec = h->root.u.c.p->section;
5784 break;
5bd4f169
AM
5785
5786 default:
fb34365b 5787 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5bd4f169
AM
5788 }
5789 }
5790 }
5791 else
5792 {
74f0fb50 5793 struct _opd_sec_data *opd;
1e2f5b6e
AM
5794
5795 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
74f0fb50
AM
5796 opd = get_opd_info (rsec);
5797 if (opd != NULL && opd->func_sec != NULL)
ccfa59ea 5798 {
74f0fb50 5799 rsec->gc_mark = 1;
ccfa59ea 5800
74f0fb50 5801 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5802 }
5bd4f169
AM
5803 }
5804
1e2f5b6e 5805 return rsec;
5bd4f169
AM
5806}
5807
65f38f15
AM
5808/* Update the .got, .plt. and dynamic reloc reference counts for the
5809 section being removed. */
5bd4f169 5810
b34976b6 5811static bfd_boolean
4ce794b7
AM
5812ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5813 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5814{
411e1bfb 5815 struct ppc_link_hash_table *htab;
5bd4f169
AM
5816 Elf_Internal_Shdr *symtab_hdr;
5817 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5818 struct got_entry **local_got_ents;
5bd4f169 5819 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5820
7dda2462
TG
5821 if (info->relocatable)
5822 return TRUE;
5823
680a3378
AM
5824 if ((sec->flags & SEC_ALLOC) == 0)
5825 return TRUE;
5826
ec338859
AM
5827 elf_section_data (sec)->local_dynrel = NULL;
5828
411e1bfb 5829 htab = ppc_hash_table (info);
4dfe6ac6
NC
5830 if (htab == NULL)
5831 return FALSE;
5832
0ffa91dd 5833 symtab_hdr = &elf_symtab_hdr (abfd);
5bd4f169 5834 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5835 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5836
5837 relend = relocs + sec->reloc_count;
5838 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5839 {
5840 unsigned long r_symndx;
04c9666a 5841 enum elf_ppc64_reloc_type r_type;
58ac9f71 5842 struct elf_link_hash_entry *h = NULL;
f961d9dd 5843 unsigned char tls_type = 0;
5bd4f169 5844
a33d1f77 5845 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5846 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5847 if (r_symndx >= symtab_hdr->sh_info)
5848 {
5849 struct ppc_link_hash_entry *eh;
5850 struct ppc_dyn_relocs **pp;
5851 struct ppc_dyn_relocs *p;
5852
5853 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 5854 h = elf_follow_link (h);
58ac9f71
AM
5855 eh = (struct ppc_link_hash_entry *) h;
5856
5857 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5858 if (p->sec == sec)
5859 {
5860 /* Everything must go for SEC. */
5861 *pp = p->next;
5862 break;
5863 }
5864 }
5865
e054468f
AM
5866 if (is_branch_reloc (r_type))
5867 {
5868 struct plt_entry **ifunc = NULL;
5869 if (h != NULL)
5870 {
5871 if (h->type == STT_GNU_IFUNC)
5872 ifunc = &h->plt.plist;
5873 }
5874 else if (local_got_ents != NULL)
5875 {
5876 struct plt_entry **local_plt = (struct plt_entry **)
5877 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 5878 unsigned char *local_got_tls_masks = (unsigned char *)
e054468f
AM
5879 (local_plt + symtab_hdr->sh_info);
5880 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
5881 ifunc = local_plt + r_symndx;
5882 }
5883 if (ifunc != NULL)
5884 {
5885 struct plt_entry *ent;
5886
5887 for (ent = *ifunc; ent != NULL; ent = ent->next)
5888 if (ent->addend == rel->r_addend)
5889 break;
5890 if (ent == NULL)
5891 abort ();
5892 if (ent->plt.refcount > 0)
5893 ent->plt.refcount -= 1;
5894 continue;
5895 }
5896 }
5897
a33d1f77
AM
5898 switch (r_type)
5899 {
411e1bfb
AM
5900 case R_PPC64_GOT_TLSLD16:
5901 case R_PPC64_GOT_TLSLD16_LO:
5902 case R_PPC64_GOT_TLSLD16_HI:
5903 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 5904 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5905 goto dogot;
5906
5907 case R_PPC64_GOT_TLSGD16:
5908 case R_PPC64_GOT_TLSGD16_LO:
5909 case R_PPC64_GOT_TLSGD16_HI:
5910 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5911 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5912 goto dogot;
5913
5914 case R_PPC64_GOT_TPREL16_DS:
5915 case R_PPC64_GOT_TPREL16_LO_DS:
5916 case R_PPC64_GOT_TPREL16_HI:
5917 case R_PPC64_GOT_TPREL16_HA:
5918 tls_type = TLS_TLS | TLS_TPREL;
5919 goto dogot;
5920
5921 case R_PPC64_GOT_DTPREL16_DS:
5922 case R_PPC64_GOT_DTPREL16_LO_DS:
5923 case R_PPC64_GOT_DTPREL16_HI:
5924 case R_PPC64_GOT_DTPREL16_HA:
5925 tls_type = TLS_TLS | TLS_DTPREL;
5926 goto dogot;
5927
a33d1f77
AM
5928 case R_PPC64_GOT16:
5929 case R_PPC64_GOT16_DS:
5930 case R_PPC64_GOT16_HA:
5931 case R_PPC64_GOT16_HI:
5932 case R_PPC64_GOT16_LO:
5933 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5934 dogot:
5935 {
5936 struct got_entry *ent;
5937
58ac9f71
AM
5938 if (h != NULL)
5939 ent = h->got.glist;
411e1bfb
AM
5940 else
5941 ent = local_got_ents[r_symndx];
5942
5943 for (; ent != NULL; ent = ent->next)
5944 if (ent->addend == rel->r_addend
e717da7e 5945 && ent->owner == abfd
411e1bfb
AM
5946 && ent->tls_type == tls_type)
5947 break;
5948 if (ent == NULL)
5949 abort ();
5950 if (ent->got.refcount > 0)
5951 ent->got.refcount -= 1;
5952 }
a33d1f77 5953 break;
65f38f15 5954
a33d1f77
AM
5955 case R_PPC64_PLT16_HA:
5956 case R_PPC64_PLT16_HI:
5957 case R_PPC64_PLT16_LO:
5958 case R_PPC64_PLT32:
5959 case R_PPC64_PLT64:
721956f4
AM
5960 case R_PPC64_REL14:
5961 case R_PPC64_REL14_BRNTAKEN:
5962 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5963 case R_PPC64_REL24:
58ac9f71 5964 if (h != NULL)
5d1634d7 5965 {
411e1bfb
AM
5966 struct plt_entry *ent;
5967
411e1bfb
AM
5968 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5969 if (ent->addend == rel->r_addend)
5970 break;
7c6c1722 5971 if (ent != NULL && ent->plt.refcount > 0)
411e1bfb 5972 ent->plt.refcount -= 1;
5d1634d7 5973 }
e86ce104 5974 break;
5d1634d7 5975
a33d1f77
AM
5976 default:
5977 break;
5978 }
5979 }
b34976b6 5980 return TRUE;
5bd4f169
AM
5981}
5982
deb0e272
AM
5983/* The maximum size of .sfpr. */
5984#define SFPR_MAX (218*4)
5985
5986struct sfpr_def_parms
5987{
699733f6
AM
5988 const char name[12];
5989 unsigned char lo, hi;
deb0e272
AM
5990 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5991 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5992};
5993
5994/* Auto-generate _save*, _rest* functions in .sfpr. */
5995
4dfe6ac6 5996static bfd_boolean
deb0e272
AM
5997sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5998{
5999 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6000 unsigned int i;
6001 size_t len = strlen (parm->name);
6002 bfd_boolean writing = FALSE;
699733f6 6003 char sym[16];
deb0e272 6004
4dfe6ac6
NC
6005 if (htab == NULL)
6006 return FALSE;
6007
deb0e272
AM
6008 memcpy (sym, parm->name, len);
6009 sym[len + 2] = 0;
6010
6011 for (i = parm->lo; i <= parm->hi; i++)
6012 {
6013 struct elf_link_hash_entry *h;
6014
6015 sym[len + 0] = i / 10 + '0';
6016 sym[len + 1] = i % 10 + '0';
6017 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6018 if (h != NULL
f5385ebf 6019 && !h->def_regular)
deb0e272
AM
6020 {
6021 h->root.type = bfd_link_hash_defined;
6022 h->root.u.def.section = htab->sfpr;
6023 h->root.u.def.value = htab->sfpr->size;
6024 h->type = STT_FUNC;
f5385ebf 6025 h->def_regular = 1;
deb0e272
AM
6026 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6027 writing = TRUE;
6028 if (htab->sfpr->contents == NULL)
6029 {
6030 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6031 if (htab->sfpr->contents == NULL)
6032 return FALSE;
6033 }
6034 }
6035 if (writing)
6036 {
6037 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6038 if (i != parm->hi)
6039 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6040 else
6041 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6042 htab->sfpr->size = p - htab->sfpr->contents;
6043 }
6044 }
6045
6046 return TRUE;
6047}
6048
6049static bfd_byte *
6050savegpr0 (bfd *abfd, bfd_byte *p, int r)
6051{
6052 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6053 return p + 4;
6054}
6055
6056static bfd_byte *
6057savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6058{
6059 p = savegpr0 (abfd, p, r);
6060 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6061 p = p + 4;
6062 bfd_put_32 (abfd, BLR, p);
6063 return p + 4;
6064}
6065
6066static bfd_byte *
6067restgpr0 (bfd *abfd, bfd_byte *p, int r)
6068{
6069 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6070 return p + 4;
6071}
6072
6073static bfd_byte *
6074restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6075{
6076 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6077 p = p + 4;
6078 p = restgpr0 (abfd, p, r);
6079 bfd_put_32 (abfd, MTLR_R0, p);
6080 p = p + 4;
6081 if (r == 29)
6082 {
6083 p = restgpr0 (abfd, p, 30);
6084 p = restgpr0 (abfd, p, 31);
6085 }
6086 bfd_put_32 (abfd, BLR, p);
6087 return p + 4;
6088}
6089
6090static bfd_byte *
6091savegpr1 (bfd *abfd, bfd_byte *p, int r)
6092{
6093 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6094 return p + 4;
6095}
6096
6097static bfd_byte *
6098savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6099{
6100 p = savegpr1 (abfd, p, r);
6101 bfd_put_32 (abfd, BLR, p);
6102 return p + 4;
6103}
6104
6105static bfd_byte *
6106restgpr1 (bfd *abfd, bfd_byte *p, int r)
6107{
6108 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6109 return p + 4;
6110}
6111
6112static bfd_byte *
6113restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6114{
6115 p = restgpr1 (abfd, p, r);
6116 bfd_put_32 (abfd, BLR, p);
6117 return p + 4;
6118}
6119
6120static bfd_byte *
6121savefpr (bfd *abfd, bfd_byte *p, int r)
6122{
6123 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6124 return p + 4;
6125}
6126
6127static bfd_byte *
6128savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6129{
6130 p = savefpr (abfd, p, r);
6131 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6132 p = p + 4;
6133 bfd_put_32 (abfd, BLR, p);
6134 return p + 4;
6135}
6136
6137static bfd_byte *
6138restfpr (bfd *abfd, bfd_byte *p, int r)
6139{
6140 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6141 return p + 4;
6142}
6143
6144static bfd_byte *
6145restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6146{
6147 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6148 p = p + 4;
6149 p = restfpr (abfd, p, r);
6150 bfd_put_32 (abfd, MTLR_R0, p);
6151 p = p + 4;
6152 if (r == 29)
6153 {
6154 p = restfpr (abfd, p, 30);
6155 p = restfpr (abfd, p, 31);
6156 }
6157 bfd_put_32 (abfd, BLR, p);
6158 return p + 4;
6159}
6160
6161static bfd_byte *
6162savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6163{
6164 p = savefpr (abfd, p, r);
6165 bfd_put_32 (abfd, BLR, p);
6166 return p + 4;
6167}
6168
6169static bfd_byte *
6170restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6171{
6172 p = restfpr (abfd, p, r);
6173 bfd_put_32 (abfd, BLR, p);
6174 return p + 4;
6175}
6176
6177static bfd_byte *
6178savevr (bfd *abfd, bfd_byte *p, int r)
6179{
6180 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6181 p = p + 4;
6182 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6183 return p + 4;
6184}
6185
6186static bfd_byte *
6187savevr_tail (bfd *abfd, bfd_byte *p, int r)
6188{
6189 p = savevr (abfd, p, r);
6190 bfd_put_32 (abfd, BLR, p);
6191 return p + 4;
6192}
6193
6194static bfd_byte *
6195restvr (bfd *abfd, bfd_byte *p, int r)
6196{
6197 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6198 p = p + 4;
6199 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6200 return p + 4;
6201}
6202
6203static bfd_byte *
6204restvr_tail (bfd *abfd, bfd_byte *p, int r)
6205{
6206 p = restvr (abfd, p, r);
6207 bfd_put_32 (abfd, BLR, p);
6208 return p + 4;
6209}
6210
e86ce104
AM
6211/* Called via elf_link_hash_traverse to transfer dynamic linking
6212 information on function code symbol entries to their corresponding
6213 function descriptor symbol entries. */
deb0e272 6214
b34976b6 6215static bfd_boolean
4ce794b7 6216func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 6217{
e86ce104 6218 struct bfd_link_info *info;
65f38f15 6219 struct ppc_link_hash_table *htab;
411e1bfb 6220 struct plt_entry *ent;
50bc7936
AM
6221 struct ppc_link_hash_entry *fh;
6222 struct ppc_link_hash_entry *fdh;
6223 bfd_boolean force_local;
5bd4f169 6224
50bc7936
AM
6225 fh = (struct ppc_link_hash_entry *) h;
6226 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 6227 return TRUE;
e86ce104 6228
50bc7936
AM
6229 if (fh->elf.root.type == bfd_link_hash_warning)
6230 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
e92d460e 6231
4ce794b7 6232 info = inf;
65f38f15 6233 htab = ppc_hash_table (info);
4dfe6ac6
NC
6234 if (htab == NULL)
6235 return FALSE;
5bd4f169 6236
c09bdfe5
AM
6237 /* Resolve undefined references to dot-symbols as the value
6238 in the function descriptor, if we have one in a regular object.
6239 This is to satisfy cases like ".quad .foo". Calls to functions
6240 in dynamic objects are handled elsewhere. */
6241 if (fh->elf.root.type == bfd_link_hash_undefweak
6242 && fh->was_undefined
b31867b6
AM
6243 && (fdh = defined_func_desc (fh)) != NULL
6244 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6245 && opd_entry_value (fdh->elf.root.u.def.section,
6246 fdh->elf.root.u.def.value,
c09bdfe5
AM
6247 &fh->elf.root.u.def.section,
6248 &fh->elf.root.u.def.value) != (bfd_vma) -1)
6249 {
b31867b6 6250 fh->elf.root.type = fdh->elf.root.type;
f5385ebf 6251 fh->elf.forced_local = 1;
b31867b6
AM
6252 fh->elf.def_regular = fdh->elf.def_regular;
6253 fh->elf.def_dynamic = fdh->elf.def_dynamic;
c09bdfe5
AM
6254 }
6255
e86ce104
AM
6256 /* If this is a function code symbol, transfer dynamic linking
6257 information to the function descriptor symbol. */
50bc7936 6258 if (!fh->is_func)
b34976b6 6259 return TRUE;
e86ce104 6260
50bc7936 6261 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
6262 if (ent->plt.refcount > 0)
6263 break;
50bc7936
AM
6264 if (ent == NULL
6265 || fh->elf.root.root.string[0] != '.'
6266 || fh->elf.root.root.string[1] == '\0')
6267 return TRUE;
5bd4f169 6268
50bc7936
AM
6269 /* Find the corresponding function descriptor symbol. Create it
6270 as undefined if necessary. */
5bd4f169 6271
b31867b6 6272 fdh = lookup_fdh (fh, htab);
50bc7936 6273 if (fdh == NULL
df131623 6274 && !info->executable
50bc7936
AM
6275 && (fh->elf.root.type == bfd_link_hash_undefined
6276 || fh->elf.root.type == bfd_link_hash_undefweak))
6277 {
908b32fc 6278 fdh = make_fdh (info, fh);
bb700d78
AM
6279 if (fdh == NULL)
6280 return FALSE;
50bc7936 6281 }
648cca2c 6282
908b32fc 6283 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
6284 code symbol is strong undefined, make the fake sym the same.
6285 If the function code symbol is defined, then force the fake
6286 descriptor local; We can't support overriding of symbols in a
6287 shared library on a fake descriptor. */
908b32fc
AM
6288
6289 if (fdh != NULL
6290 && fdh->fake
433817dd 6291 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 6292 {
433817dd
AM
6293 if (fh->elf.root.type == bfd_link_hash_undefined)
6294 {
6295 fdh->elf.root.type = bfd_link_hash_undefined;
6296 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6297 }
6298 else if (fh->elf.root.type == bfd_link_hash_defined
6299 || fh->elf.root.type == bfd_link_hash_defweak)
6300 {
6301 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6302 }
908b32fc
AM
6303 }
6304
50bc7936 6305 if (fdh != NULL
f5385ebf 6306 && !fdh->elf.forced_local
df131623 6307 && (!info->executable
f5385ebf
AM
6308 || fdh->elf.def_dynamic
6309 || fdh->elf.ref_dynamic
50bc7936
AM
6310 || (fdh->elf.root.type == bfd_link_hash_undefweak
6311 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6312 {
6313 if (fdh->elf.dynindx == -1)
c152c796 6314 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 6315 return FALSE;
f5385ebf
AM
6316 fdh->elf.ref_regular |= fh->elf.ref_regular;
6317 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6318 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6319 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 6320 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 6321 {
40d16e0b 6322 move_plt_plist (fh, fdh);
f5385ebf 6323 fdh->elf.needs_plt = 1;
e86ce104 6324 }
50bc7936 6325 fdh->is_func_descriptor = 1;
34814b9f
AM
6326 fdh->oh = fh;
6327 fh->oh = fdh;
e86ce104
AM
6328 }
6329
50bc7936
AM
6330 /* Now that the info is on the function descriptor, clear the
6331 function code sym info. Any function code syms for which we
6332 don't have a definition in a regular file, we force local.
6333 This prevents a shared library from exporting syms that have
6334 been imported from another library. Function code syms that
6335 are really in the library we must leave global to prevent the
6336 linker dragging in a definition from a static library. */
93f3fa99
AM
6337 force_local = (!fh->elf.def_regular
6338 || fdh == NULL
6339 || !fdh->elf.def_regular
6340 || fdh->elf.forced_local);
50bc7936
AM
6341 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6342
b34976b6 6343 return TRUE;
e86ce104 6344}
40b8271b 6345
e86ce104 6346/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
6347 this hook to a) provide some gcc support functions, and b) transfer
6348 dynamic linking information gathered so far on function code symbol
6349 entries, to their corresponding function descriptor symbol entries. */
deb0e272 6350
b34976b6 6351static bfd_boolean
4ce794b7
AM
6352ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6353 struct bfd_link_info *info)
e86ce104
AM
6354{
6355 struct ppc_link_hash_table *htab;
82bd7b59 6356 unsigned int i;
deb0e272
AM
6357 const struct sfpr_def_parms funcs[] =
6358 {
6359 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6360 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6361 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6362 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6363 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6364 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6365 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6366 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6367 { "._savef", 14, 31, savefpr, savefpr1_tail },
6368 { "._restf", 14, 31, restfpr, restfpr1_tail },
6369 { "_savevr_", 20, 31, savevr, savevr_tail },
6370 { "_restvr_", 20, 31, restvr, restvr_tail }
6371 };
e86ce104
AM
6372
6373 htab = ppc_hash_table (info);
4dfe6ac6
NC
6374 if (htab == NULL)
6375 return FALSE;
6376
82bd7b59
AM
6377 if (htab->sfpr == NULL)
6378 /* We don't have any relocs. */
b34976b6 6379 return TRUE;
82bd7b59 6380
deb0e272
AM
6381 /* Provide any missing _save* and _rest* functions. */
6382 htab->sfpr->size = 0;
6383 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6384 if (!sfpr_define (info, &funcs[i]))
6385 return FALSE;
82bd7b59 6386
4ce794b7 6387 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 6388
eea6121a 6389 if (htab->sfpr->size == 0)
8423293d 6390 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 6391
b34976b6 6392 return TRUE;
e86ce104
AM
6393}
6394
6395/* Adjust a symbol defined by a dynamic object and referenced by a
6396 regular object. The current definition is in some section of the
6397 dynamic object, but we're not including those sections. We have to
6398 change the definition to something the rest of the link can
6399 understand. */
6400
b34976b6 6401static bfd_boolean
4ce794b7
AM
6402ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6403 struct elf_link_hash_entry *h)
e86ce104
AM
6404{
6405 struct ppc_link_hash_table *htab;
e86ce104 6406 asection *s;
e86ce104
AM
6407
6408 htab = ppc_hash_table (info);
4dfe6ac6
NC
6409 if (htab == NULL)
6410 return FALSE;
e86ce104
AM
6411
6412 /* Deal with function syms. */
6413 if (h->type == STT_FUNC
e054468f 6414 || h->type == STT_GNU_IFUNC
f5385ebf 6415 || h->needs_plt)
e86ce104
AM
6416 {
6417 /* Clear procedure linkage table information for any symbol that
6418 won't need a .plt entry. */
411e1bfb
AM
6419 struct plt_entry *ent;
6420 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6421 if (ent->plt.refcount > 0)
6422 break;
8387904d 6423 if (ent == NULL
e054468f
AM
6424 || (h->type != STT_GNU_IFUNC
6425 && (SYMBOL_CALLS_LOCAL (info, h)
6426 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6427 && h->root.type == bfd_link_hash_undefweak))))
40b8271b 6428 {
411e1bfb 6429 h->plt.plist = NULL;
f5385ebf 6430 h->needs_plt = 0;
40b8271b 6431 }
5bd4f169 6432 }
bbd7ec4a 6433 else
411e1bfb 6434 h->plt.plist = NULL;
5bd4f169
AM
6435
6436 /* If this is a weak symbol, and there is a real definition, the
6437 processor independent code will have arranged for us to see the
6438 real definition first, and we can just use the same value. */
f6e332e6 6439 if (h->u.weakdef != NULL)
5bd4f169 6440 {
f6e332e6
AM
6441 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6442 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6443 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6444 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 6445 if (ELIMINATE_COPY_RELOCS)
f6e332e6 6446 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 6447 return TRUE;
5bd4f169
AM
6448 }
6449
5bd4f169
AM
6450 /* If we are creating a shared library, we must presume that the
6451 only references to the symbol are via the global offset table.
6452 For such cases we need not do anything here; the relocations will
6453 be handled correctly by relocate_section. */
6454 if (info->shared)
b34976b6 6455 return TRUE;
5bd4f169 6456
65f38f15
AM
6457 /* If there are no references to this symbol that do not use the
6458 GOT, we don't need to generate a copy reloc. */
f5385ebf 6459 if (!h->non_got_ref)
b34976b6 6460 return TRUE;
65f38f15 6461
b186458a
JJ
6462 /* Don't generate a copy reloc for symbols defined in the executable. */
6463 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6464 return TRUE;
6465
f4656909 6466 if (ELIMINATE_COPY_RELOCS)
65f38f15 6467 {
f4656909
AM
6468 struct ppc_link_hash_entry * eh;
6469 struct ppc_dyn_relocs *p;
65f38f15 6470
f4656909
AM
6471 eh = (struct ppc_link_hash_entry *) h;
6472 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6473 {
6474 s = p->sec->output_section;
6475 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6476 break;
6477 }
6478
6479 /* If we didn't find any dynamic relocs in read-only sections, then
6480 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6481 if (p == NULL)
6482 {
f5385ebf 6483 h->non_got_ref = 0;
f4656909
AM
6484 return TRUE;
6485 }
65f38f15
AM
6486 }
6487
5d35169e 6488 if (h->plt.plist != NULL)
97b639ba
AM
6489 {
6490 /* We should never get here, but unfortunately there are versions
6491 of gcc out there that improperly (for this ABI) put initialized
6492 function pointers, vtable refs and suchlike in read-only
6493 sections. Allow them to proceed, but warn that this might
6494 break at runtime. */
6495 (*_bfd_error_handler)
6496 (_("copy reloc against `%s' requires lazy plt linking; "
6497 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
6498 h->root.root.string);
6499 }
5d35169e
AM
6500
6501 /* This is a reference to a symbol defined by a dynamic object which
6502 is not a function. */
6503
909272ee
AM
6504 if (h->size == 0)
6505 {
6506 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
6507 h->root.root.string);
6508 return TRUE;
6509 }
6510
5bd4f169
AM
6511 /* We must allocate the symbol in our .dynbss section, which will
6512 become part of the .bss section of the executable. There will be
6513 an entry for this symbol in the .dynsym section. The dynamic
6514 object will contain position independent code, so all references
6515 from the dynamic object to this symbol will go through the global
6516 offset table. The dynamic linker will use the .dynsym entry to
6517 determine the address it must put in the global offset table, so
6518 both the dynamic object and the regular object will refer to the
6519 same memory location for the variable. */
5bd4f169 6520
04c9666a
AM
6521 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6522 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
6523 runtime process image. We need to remember the offset into the
6524 .rela.bss section we are going to use. */
6525 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
6526 {
eea6121a 6527 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 6528 h->needs_copy = 1;
5bd4f169
AM
6529 }
6530
4ce794b7 6531 s = htab->dynbss;
5bd4f169 6532
027297b7 6533 return _bfd_elf_adjust_dynamic_copy (h, s);
5bd4f169
AM
6534}
6535
e86ce104
AM
6536/* If given a function descriptor symbol, hide both the function code
6537 sym and the descriptor. */
6538static void
4ce794b7
AM
6539ppc64_elf_hide_symbol (struct bfd_link_info *info,
6540 struct elf_link_hash_entry *h,
6541 bfd_boolean force_local)
e86ce104 6542{
34814b9f 6543 struct ppc_link_hash_entry *eh;
e86ce104
AM
6544 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6545
34814b9f
AM
6546 eh = (struct ppc_link_hash_entry *) h;
6547 if (eh->is_func_descriptor)
e86ce104 6548 {
34814b9f 6549 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 6550
721956f4 6551 if (fh == NULL)
d1329ca3
AM
6552 {
6553 const char *p, *q;
6554 struct ppc_link_hash_table *htab;
6555 char save;
6556
6557 /* We aren't supposed to use alloca in BFD because on
6558 systems which do not have alloca the version in libiberty
6559 calls xmalloc, which might cause the program to crash
6560 when it runs out of memory. This function doesn't have a
6561 return status, so there's no way to gracefully return an
6562 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
6563 accessed; It's either a string in an ELF string table,
6564 or allocated in an objalloc structure. */
d1329ca3 6565
34814b9f 6566 p = eh->elf.root.root.string - 1;
d1329ca3
AM
6567 save = *p;
6568 *(char *) p = '.';
6569 htab = ppc_hash_table (info);
4dfe6ac6
NC
6570 if (htab == NULL)
6571 return;
6572
34814b9f
AM
6573 fh = (struct ppc_link_hash_entry *)
6574 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6575 *(char *) p = save;
6576
6577 /* Unfortunately, if it so happens that the string we were
6578 looking for was allocated immediately before this string,
6579 then we overwrote the string terminator. That's the only
6580 reason the lookup should fail. */
6581 if (fh == NULL)
6582 {
34814b9f
AM
6583 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6584 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 6585 --q, --p;
34814b9f
AM
6586 if (q < eh->elf.root.root.string && *p == '.')
6587 fh = (struct ppc_link_hash_entry *)
6588 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
6589 }
6590 if (fh != NULL)
6591 {
34814b9f
AM
6592 eh->oh = fh;
6593 fh->oh = eh;
d1329ca3
AM
6594 }
6595 }
e86ce104 6596 if (fh != NULL)
34814b9f 6597 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
6598 }
6599}
6600
411e1bfb 6601static bfd_boolean
8843416a
AM
6602get_sym_h (struct elf_link_hash_entry **hp,
6603 Elf_Internal_Sym **symp,
6604 asection **symsecp,
f961d9dd 6605 unsigned char **tls_maskp,
8843416a
AM
6606 Elf_Internal_Sym **locsymsp,
6607 unsigned long r_symndx,
6608 bfd *ibfd)
411e1bfb 6609{
0ffa91dd 6610 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
411e1bfb
AM
6611
6612 if (r_symndx >= symtab_hdr->sh_info)
6613 {
6614 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6615 struct elf_link_hash_entry *h;
6616
6617 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 6618 h = elf_follow_link (h);
411e1bfb
AM
6619
6620 if (hp != NULL)
6621 *hp = h;
6622
6623 if (symp != NULL)
6624 *symp = NULL;
6625
6626 if (symsecp != NULL)
6627 {
6628 asection *symsec = NULL;
6629 if (h->root.type == bfd_link_hash_defined
6630 || h->root.type == bfd_link_hash_defweak)
6631 symsec = h->root.u.def.section;
6632 *symsecp = symsec;
6633 }
6634
e7b938ca 6635 if (tls_maskp != NULL)
411e1bfb
AM
6636 {
6637 struct ppc_link_hash_entry *eh;
6638
6639 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6640 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6641 }
6642 }
6643 else
6644 {
6645 Elf_Internal_Sym *sym;
6646 Elf_Internal_Sym *locsyms = *locsymsp;
6647
6648 if (locsyms == NULL)
6649 {
6650 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6651 if (locsyms == NULL)
6652 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6653 symtab_hdr->sh_info,
6654 0, NULL, NULL, NULL);
6655 if (locsyms == NULL)
6656 return FALSE;
6657 *locsymsp = locsyms;
6658 }
6659 sym = locsyms + r_symndx;
6660
6661 if (hp != NULL)
6662 *hp = NULL;
6663
6664 if (symp != NULL)
6665 *symp = sym;
6666
6667 if (symsecp != NULL)
cb33740c 6668 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
411e1bfb 6669
e7b938ca 6670 if (tls_maskp != NULL)
411e1bfb
AM
6671 {
6672 struct got_entry **lgot_ents;
f961d9dd 6673 unsigned char *tls_mask;
411e1bfb 6674
e7b938ca 6675 tls_mask = NULL;
411e1bfb
AM
6676 lgot_ents = elf_local_got_ents (ibfd);
6677 if (lgot_ents != NULL)
6678 {
e054468f
AM
6679 struct plt_entry **local_plt = (struct plt_entry **)
6680 (lgot_ents + symtab_hdr->sh_info);
f961d9dd 6681 unsigned char *lgot_masks = (unsigned char *)
e054468f 6682 (local_plt + symtab_hdr->sh_info);
e7b938ca 6683 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6684 }
e7b938ca 6685 *tls_maskp = tls_mask;
411e1bfb
AM
6686 }
6687 }
6688 return TRUE;
6689}
6690
e7b938ca 6691/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6692 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6693 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6694
6695static int
f961d9dd 6696get_tls_mask (unsigned char **tls_maskp,
3a71aa26
AM
6697 unsigned long *toc_symndx,
6698 bfd_vma *toc_addend,
0d4792f7 6699 Elf_Internal_Sym **locsymsp,
3a71aa26
AM
6700 const Elf_Internal_Rela *rel,
6701 bfd *ibfd)
411e1bfb
AM
6702{
6703 unsigned long r_symndx;
0d4792f7 6704 int next_r;
411e1bfb
AM
6705 struct elf_link_hash_entry *h;
6706 Elf_Internal_Sym *sym;
6707 asection *sec;
6708 bfd_vma off;
6709
6710 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6711 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6712 return 0;
411e1bfb 6713
e7b938ca 6714 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6715 || sec == NULL
6bee8834 6716 || ppc64_elf_section_data (sec) == NULL
7c8fe5c4 6717 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6718 return 1;
411e1bfb
AM
6719
6720 /* Look inside a TOC section too. */
6721 if (h != NULL)
6722 {
6723 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6724 off = h->root.u.def.value;
6725 }
6726 else
6727 off = sym->st_value;
6728 off += rel->r_addend;
6729 BFD_ASSERT (off % 8 == 0);
3a71aa26
AM
6730 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6731 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
0d4792f7
AM
6732 if (toc_symndx != NULL)
6733 *toc_symndx = r_symndx;
3a71aa26
AM
6734 if (toc_addend != NULL)
6735 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6736 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6737 return 0;
854b41e7 6738 if ((h == NULL || is_static_defined (h))
0d4792f7
AM
6739 && (next_r == -1 || next_r == -2))
6740 return 1 - next_r;
951fd09b 6741 return 1;
411e1bfb
AM
6742}
6743
754021d0 6744/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6745 code for the old ABI, these will already have been done. */
754021d0
AM
6746
6747static bfd_boolean
6748adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6749{
6750 struct ppc_link_hash_entry *eh;
6751 asection *sym_sec;
74f0fb50 6752 struct _opd_sec_data *opd;
754021d0
AM
6753
6754 if (h->root.type == bfd_link_hash_indirect)
6755 return TRUE;
6756
6757 if (h->root.type == bfd_link_hash_warning)
6758 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6759
6760 if (h->root.type != bfd_link_hash_defined
6761 && h->root.type != bfd_link_hash_defweak)
6762 return TRUE;
6763
6764 eh = (struct ppc_link_hash_entry *) h;
6765 if (eh->adjust_done)
6766 return TRUE;
6767
6768 sym_sec = eh->elf.root.u.def.section;
74f0fb50
AM
6769 opd = get_opd_info (sym_sec);
6770 if (opd != NULL && opd->adjust != NULL)
754021d0 6771 {
74f0fb50 6772 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6773 if (adjust == -1)
6774 {
6775 /* This entry has been deleted. */
b3fac117 6776 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6777 if (dsec == NULL)
6778 {
6779 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6780 if (elf_discarded_section (dsec))
6781 {
b3fac117 6782 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6783 break;
6784 }
6785 }
4025353c 6786 eh->elf.root.u.def.value = 0;
81688140 6787 eh->elf.root.u.def.section = dsec;
4025353c
AM
6788 }
6789 else
6790 eh->elf.root.u.def.value += adjust;
754021d0
AM
6791 eh->adjust_done = 1;
6792 }
6793 return TRUE;
6794}
6795
8c1d1bb8
AM
6796/* Handles decrementing dynamic reloc counts for the reloc specified by
6797 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6798 have already been determined. */
6799
6800static bfd_boolean
6801dec_dynrel_count (bfd_vma r_info,
6802 asection *sec,
6803 struct bfd_link_info *info,
6804 Elf_Internal_Sym **local_syms,
6805 struct elf_link_hash_entry *h,
6806 asection *sym_sec)
6807{
6808 enum elf_ppc64_reloc_type r_type;
6809 struct ppc_dyn_relocs *p;
6810 struct ppc_dyn_relocs **pp;
6811
6812 /* Can this reloc be dynamic? This switch, and later tests here
6813 should be kept in sync with the code in check_relocs. */
6814 r_type = ELF64_R_TYPE (r_info);
6815 switch (r_type)
6816 {
6817 default:
6818 return TRUE;
6819
6820 case R_PPC64_TPREL16:
6821 case R_PPC64_TPREL16_LO:
6822 case R_PPC64_TPREL16_HI:
6823 case R_PPC64_TPREL16_HA:
6824 case R_PPC64_TPREL16_DS:
6825 case R_PPC64_TPREL16_LO_DS:
6826 case R_PPC64_TPREL16_HIGHER:
6827 case R_PPC64_TPREL16_HIGHERA:
6828 case R_PPC64_TPREL16_HIGHEST:
6829 case R_PPC64_TPREL16_HIGHESTA:
6830 if (!info->shared)
6831 return TRUE;
6832
6833 case R_PPC64_TPREL64:
6834 case R_PPC64_DTPMOD64:
6835 case R_PPC64_DTPREL64:
6836 case R_PPC64_ADDR64:
6837 case R_PPC64_REL30:
6838 case R_PPC64_REL32:
6839 case R_PPC64_REL64:
6840 case R_PPC64_ADDR14:
6841 case R_PPC64_ADDR14_BRNTAKEN:
6842 case R_PPC64_ADDR14_BRTAKEN:
6843 case R_PPC64_ADDR16:
6844 case R_PPC64_ADDR16_DS:
6845 case R_PPC64_ADDR16_HA:
6846 case R_PPC64_ADDR16_HI:
6847 case R_PPC64_ADDR16_HIGHER:
6848 case R_PPC64_ADDR16_HIGHERA:
6849 case R_PPC64_ADDR16_HIGHEST:
6850 case R_PPC64_ADDR16_HIGHESTA:
6851 case R_PPC64_ADDR16_LO:
6852 case R_PPC64_ADDR16_LO_DS:
6853 case R_PPC64_ADDR24:
6854 case R_PPC64_ADDR32:
6855 case R_PPC64_UADDR16:
6856 case R_PPC64_UADDR32:
6857 case R_PPC64_UADDR64:
6858 case R_PPC64_TOC:
6859 break;
6860 }
6861
6862 if (local_syms != NULL)
6863 {
6864 unsigned long r_symndx;
6865 Elf_Internal_Sym *sym;
6866 bfd *ibfd = sec->owner;
6867
6868 r_symndx = ELF64_R_SYM (r_info);
6869 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6870 return FALSE;
6871 }
6872
6873 if ((info->shared
1d483afe 6874 && (must_be_dyn_reloc (info, r_type)
8c1d1bb8
AM
6875 || (h != NULL
6876 && (!info->symbolic
6877 || h->root.type == bfd_link_hash_defweak
6878 || !h->def_regular))))
6879 || (ELIMINATE_COPY_RELOCS
6880 && !info->shared
6881 && h != NULL
6882 && (h->root.type == bfd_link_hash_defweak
6883 || !h->def_regular)))
6884 ;
6885 else
6886 return TRUE;
6887
6888 if (h != NULL)
6889 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6890 else
6edfbbad 6891 {
60124e18
AM
6892 if (sym_sec != NULL)
6893 {
6894 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6895 pp = (struct ppc_dyn_relocs **) vpp;
6896 }
6897 else
6898 {
6899 void *vpp = &elf_section_data (sec)->local_dynrel;
6900 pp = (struct ppc_dyn_relocs **) vpp;
6901 }
6902
6903 /* elf_gc_sweep may have already removed all dyn relocs associated
6904 with local syms for a given section. Don't report a dynreloc
6905 miscount. */
6906 if (*pp == NULL)
6907 return TRUE;
6edfbbad 6908 }
8c1d1bb8
AM
6909
6910 while ((p = *pp) != NULL)
6911 {
6912 if (p->sec == sec)
6913 {
1d483afe 6914 if (!must_be_dyn_reloc (info, r_type))
8c1d1bb8
AM
6915 p->pc_count -= 1;
6916 p->count -= 1;
6917 if (p->count == 0)
6918 *pp = p->next;
6919 return TRUE;
6920 }
6921 pp = &p->next;
6922 }
6923
6924 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6925 sec->owner, sec);
6926 bfd_set_error (bfd_error_bad_value);
6927 return FALSE;
6928}
6929
754021d0
AM
6930/* Remove unused Official Procedure Descriptor entries. Currently we
6931 only remove those associated with functions in discarded link-once
6932 sections, or weakly defined functions that have been overridden. It
6933 would be possible to remove many more entries for statically linked
6934 applications. */
6935
b34976b6 6936bfd_boolean
33c0ec9d 6937ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
1e2f5b6e
AM
6938{
6939 bfd *ibfd;
754021d0 6940 bfd_boolean some_edited = FALSE;
3f764659 6941 asection *need_pad = NULL;
1e2f5b6e 6942
411e1bfb 6943 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
6944 {
6945 asection *sec;
6946 Elf_Internal_Rela *relstart, *rel, *relend;
6947 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6948 Elf_Internal_Sym *local_syms;
1e2f5b6e 6949 bfd_vma offset;
74f0fb50 6950 struct _opd_sec_data *opd;
3f764659
JJ
6951 bfd_boolean need_edit, add_aux_fields;
6952 bfd_size_type cnt_16b = 0;
1e2f5b6e 6953
854b41e7
AM
6954 if (!is_ppc64_elf (ibfd))
6955 continue;
6956
1e2f5b6e 6957 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6958 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6959 continue;
6960
4b85d634
AM
6961 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6962 continue;
6963
1e2f5b6e
AM
6964 if (sec->output_section == bfd_abs_section_ptr)
6965 continue;
6966
6967 /* Look through the section relocs. */
6968 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6969 continue;
6970
6cdc0ccc 6971 local_syms = NULL;
0ffa91dd 6972 symtab_hdr = &elf_symtab_hdr (ibfd);
1e2f5b6e
AM
6973
6974 /* Read the relocations. */
4ce794b7 6975 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6976 info->keep_memory);
1e2f5b6e 6977 if (relstart == NULL)
b34976b6 6978 return FALSE;
1e2f5b6e
AM
6979
6980 /* First run through the relocs to check they are sane, and to
6981 determine whether we need to edit this opd section. */
b34976b6 6982 need_edit = FALSE;
3f764659 6983 need_pad = sec;
1e2f5b6e
AM
6984 offset = 0;
6985 relend = relstart + sec->reloc_count;
50bc7936 6986 for (rel = relstart; rel < relend; )
1e2f5b6e 6987 {
04c9666a 6988 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6989 unsigned long r_symndx;
6990 asection *sym_sec;
6991 struct elf_link_hash_entry *h;
6992 Elf_Internal_Sym *sym;
6993
3f764659 6994 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6995 only interested in the reloc pointing to a function entry
6996 point. */
50bc7936
AM
6997 if (rel->r_offset != offset
6998 || rel + 1 >= relend
6999 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
7000 {
7001 /* If someone messes with .opd alignment then after a
7002 "ld -r" we might have padding in the middle of .opd.
7003 Also, there's nothing to prevent someone putting
7004 something silly in .opd with the assembler. No .opd
b34976b6 7005 optimization for them! */
3f764659 7006 broken_opd:
1e2f5b6e 7007 (*_bfd_error_handler)
d003868e 7008 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 7009 need_edit = FALSE;
1e2f5b6e
AM
7010 break;
7011 }
7012
50bc7936
AM
7013 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7014 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7015 {
7016 (*_bfd_error_handler)
d003868e
AM
7017 (_("%B: unexpected reloc type %u in .opd section"),
7018 ibfd, r_type);
50bc7936
AM
7019 need_edit = FALSE;
7020 break;
7021 }
7022
1e2f5b6e 7023 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
7024 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7025 r_symndx, ibfd))
50bc7936 7026 goto error_ret;
1e2f5b6e
AM
7027
7028 if (sym_sec == NULL || sym_sec->owner == NULL)
7029 {
411e1bfb
AM
7030 const char *sym_name;
7031 if (h != NULL)
7032 sym_name = h->root.root.string;
7033 else
26c61ae5
L
7034 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7035 sym_sec);
411e1bfb 7036
1e2f5b6e 7037 (*_bfd_error_handler)
d003868e
AM
7038 (_("%B: undefined sym `%s' in .opd section"),
7039 ibfd, sym_name);
b34976b6 7040 need_edit = FALSE;
1e2f5b6e
AM
7041 break;
7042 }
7043
51020317
AM
7044 /* opd entries are always for functions defined in the
7045 current input bfd. If the symbol isn't defined in the
7046 input bfd, then we won't be using the function in this
7047 bfd; It must be defined in a linkonce section in another
7048 bfd, or is weak. It's also possible that we are
7049 discarding the function due to a linker script /DISCARD/,
7050 which we test for via the output_section. */
7051 if (sym_sec->owner != ibfd
7052 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 7053 need_edit = TRUE;
1e2f5b6e 7054
50bc7936 7055 rel += 2;
3f764659
JJ
7056 if (rel == relend
7057 || (rel + 1 == relend && rel->r_offset == offset + 16))
7058 {
7059 if (sec->size == offset + 24)
7060 {
7061 need_pad = NULL;
7062 break;
7063 }
7064 if (rel == relend && sec->size == offset + 16)
7065 {
7066 cnt_16b++;
7067 break;
7068 }
7069 goto broken_opd;
7070 }
7071
7072 if (rel->r_offset == offset + 24)
7073 offset += 24;
7074 else if (rel->r_offset != offset + 16)
7075 goto broken_opd;
7076 else if (rel + 1 < relend
7077 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7078 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7079 {
7080 offset += 16;
7081 cnt_16b++;
7082 }
7083 else if (rel + 2 < relend
7084 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7085 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7086 {
7087 offset += 24;
7088 rel += 1;
7089 }
7090 else
7091 goto broken_opd;
1e2f5b6e
AM
7092 }
7093
3f764659
JJ
7094 add_aux_fields = non_overlapping && cnt_16b > 0;
7095
7096 if (need_edit || add_aux_fields)
1e2f5b6e
AM
7097 {
7098 Elf_Internal_Rela *write_rel;
d4730f92 7099 Elf_Internal_Shdr *rel_hdr;
1e2f5b6e 7100 bfd_byte *rptr, *wptr;
983bddc8 7101 bfd_byte *new_contents;
b34976b6 7102 bfd_boolean skip;
3f764659 7103 long opd_ent_size;
74f0fb50
AM
7104 bfd_size_type amt;
7105
983bddc8 7106 new_contents = NULL;
74f0fb50
AM
7107 amt = sec->size * sizeof (long) / 8;
7108 opd = &ppc64_elf_section_data (sec)->u.opd;
33c0ec9d 7109 opd->adjust = bfd_zalloc (sec->owner, amt);
74f0fb50
AM
7110 if (opd->adjust == NULL)
7111 return FALSE;
7112 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e
AM
7113
7114 /* This seems a waste of time as input .opd sections are all
7115 zeros as generated by gcc, but I suppose there's no reason
7116 this will always be so. We might start putting something in
7117 the third word of .opd entries. */
7118 if ((sec->flags & SEC_IN_MEMORY) == 0)
7119 {
eea6121a
AM
7120 bfd_byte *loc;
7121 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 7122 {
eea6121a
AM
7123 if (loc != NULL)
7124 free (loc);
50bc7936 7125 error_ret:
6cdc0ccc
AM
7126 if (local_syms != NULL
7127 && symtab_hdr->contents != (unsigned char *) local_syms)
7128 free (local_syms);
6cdc0ccc
AM
7129 if (elf_section_data (sec)->relocs != relstart)
7130 free (relstart);
b34976b6 7131 return FALSE;
6cdc0ccc 7132 }
1e2f5b6e
AM
7133 sec->contents = loc;
7134 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7135 }
7136
7137 elf_section_data (sec)->relocs = relstart;
7138
3f764659 7139 new_contents = sec->contents;
3f764659
JJ
7140 if (add_aux_fields)
7141 {
7142 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7143 if (new_contents == NULL)
7144 return FALSE;
7145 need_pad = FALSE;
3f764659 7146 }
b4f4e59f
AM
7147 wptr = new_contents;
7148 rptr = sec->contents;
3f764659 7149
1e2f5b6e 7150 write_rel = relstart;
b34976b6 7151 skip = FALSE;
1e2f5b6e 7152 offset = 0;
3f764659 7153 opd_ent_size = 0;
1e2f5b6e
AM
7154 for (rel = relstart; rel < relend; rel++)
7155 {
50bc7936
AM
7156 unsigned long r_symndx;
7157 asection *sym_sec;
7158 struct elf_link_hash_entry *h;
7159 Elf_Internal_Sym *sym;
7160
7161 r_symndx = ELF64_R_SYM (rel->r_info);
7162 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 7163 r_symndx, ibfd))
50bc7936
AM
7164 goto error_ret;
7165
1e2f5b6e
AM
7166 if (rel->r_offset == offset)
7167 {
50bc7936 7168 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
7169
7170 /* See if the .opd entry is full 24 byte or
7171 16 byte (with fd_aux entry overlapped with next
7172 fd_func). */
7173 opd_ent_size = 24;
7174 if ((rel + 2 == relend && sec->size == offset + 16)
7175 || (rel + 3 < relend
7176 && rel[2].r_offset == offset + 16
7177 && rel[3].r_offset == offset + 24
7178 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7179 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7180 opd_ent_size = 16;
7181
4025353c
AM
7182 if (h != NULL
7183 && h->root.root.string[0] == '.')
c4f68ce3 7184 {
4dfe6ac6
NC
7185 struct ppc_link_hash_table *htab;
7186
7187 htab = ppc_hash_table (info);
7188 if (htab != NULL)
7189 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7190 htab);
c4f68ce3
AM
7191 if (fdh != NULL
7192 && fdh->elf.root.type != bfd_link_hash_defined
7193 && fdh->elf.root.type != bfd_link_hash_defweak)
7194 fdh = NULL;
7195 }
1e2f5b6e 7196
51020317
AM
7197 skip = (sym_sec->owner != ibfd
7198 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
7199 if (skip)
7200 {
4025353c 7201 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
7202 {
7203 /* Arrange for the function descriptor sym
7204 to be dropped. */
d6fe2dc1
AM
7205 fdh->elf.root.u.def.value = 0;
7206 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 7207 }
74f0fb50 7208 opd->adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
7209 }
7210 else
1e2f5b6e
AM
7211 {
7212 /* We'll be keeping this opd entry. */
7213
4025353c 7214 if (fdh != NULL)
1e2f5b6e 7215 {
754021d0
AM
7216 /* Redefine the function descriptor symbol to
7217 this location in the opd section. It is
7218 necessary to update the value here rather
7219 than using an array of adjustments as we do
7220 for local symbols, because various places
7221 in the generic ELF code use the value
7222 stored in u.def.value. */
3f764659 7223 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 7224 fdh->adjust_done = 1;
1e2f5b6e 7225 }
754021d0
AM
7226
7227 /* Local syms are a bit tricky. We could
7228 tweak them as they can be cached, but
7229 we'd need to look through the local syms
7230 for the function descriptor sym which we
7231 don't have at the moment. So keep an
7232 array of adjustments. */
74f0fb50 7233 opd->adjust[rel->r_offset / 8]
3f764659 7234 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
7235
7236 if (wptr != rptr)
3f764659
JJ
7237 memcpy (wptr, rptr, opd_ent_size);
7238 wptr += opd_ent_size;
7239 if (add_aux_fields && opd_ent_size == 16)
7240 {
7241 memset (wptr, '\0', 8);
7242 wptr += 8;
7243 }
1e2f5b6e 7244 }
3f764659
JJ
7245 rptr += opd_ent_size;
7246 offset += opd_ent_size;
1e2f5b6e
AM
7247 }
7248
50bc7936
AM
7249 if (skip)
7250 {
60124e18
AM
7251 if (!NO_OPD_RELOCS
7252 && !info->relocatable
18d944df
AM
7253 && !dec_dynrel_count (rel->r_info, sec, info,
7254 NULL, h, sym_sec))
8c1d1bb8 7255 goto error_ret;
50bc7936
AM
7256 }
7257 else
1e2f5b6e 7258 {
50bc7936
AM
7259 /* We need to adjust any reloc offsets to point to the
7260 new opd entries. While we're at it, we may as well
7261 remove redundant relocs. */
74f0fb50 7262 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
7263 if (write_rel != rel)
7264 memcpy (write_rel, rel, sizeof (*rel));
7265 ++write_rel;
7266 }
7267 }
7268
3f764659 7269 sec->size = wptr - new_contents;
1e2f5b6e 7270 sec->reloc_count = write_rel - relstart;
3f764659
JJ
7271 if (add_aux_fields)
7272 {
7273 free (sec->contents);
7274 sec->contents = new_contents;
7275 }
7276
05bf9422 7277 /* Fudge the header size too, as this is used later in
cdcf6e38 7278 elf_bfd_final_link if we are emitting relocs. */
d4730f92
BS
7279 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7280 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
754021d0 7281 some_edited = TRUE;
1e2f5b6e 7282 }
6cdc0ccc 7283 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 7284 free (relstart);
6cdc0ccc 7285
411e1bfb
AM
7286 if (local_syms != NULL
7287 && symtab_hdr->contents != (unsigned char *) local_syms)
7288 {
7289 if (!info->keep_memory)
7290 free (local_syms);
7291 else
7292 symtab_hdr->contents = (unsigned char *) local_syms;
7293 }
7294 }
7295
754021d0
AM
7296 if (some_edited)
7297 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7298
3f764659
JJ
7299 /* If we are doing a final link and the last .opd entry is just 16 byte
7300 long, add a 8 byte padding after it. */
7301 if (need_pad != NULL && !info->relocatable)
7302 {
7303 bfd_byte *p;
7304
7305 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7306 {
7307 BFD_ASSERT (need_pad->size > 0);
7308
7309 p = bfd_malloc (need_pad->size + 8);
7310 if (p == NULL)
7311 return FALSE;
699733f6 7312
3f764659
JJ
7313 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7314 p, 0, need_pad->size))
7315 return FALSE;
7316
7317 need_pad->contents = p;
7318 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7319 }
7320 else
7321 {
7322 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7323 if (p == NULL)
7324 return FALSE;
7325
7326 need_pad->contents = p;
7327 }
7328
7329 memset (need_pad->contents + need_pad->size, 0, 8);
7330 need_pad->size += 8;
7331 }
7332
411e1bfb
AM
7333 return TRUE;
7334}
7335
e1918d23 7336/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 7337
e1918d23 7338asection *
33c0ec9d
AM
7339ppc64_elf_tls_setup (struct bfd_link_info *info,
7340 int no_tls_get_addr_opt,
7341 int *no_multi_toc)
411e1bfb 7342{
411e1bfb
AM
7343 struct ppc_link_hash_table *htab;
7344
411e1bfb 7345 htab = ppc_hash_table (info);
4dfe6ac6
NC
7346 if (htab == NULL)
7347 return NULL;
7348
33c0ec9d
AM
7349 if (*no_multi_toc)
7350 htab->do_multi_toc = 0;
7351 else if (!htab->do_multi_toc)
7352 *no_multi_toc = 1;
7353
3a71aa26
AM
7354 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7355 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7356 FALSE, FALSE, TRUE));
a7f2871e
AM
7357 /* Move dynamic linking info to the function descriptor sym. */
7358 if (htab->tls_get_addr != NULL)
7359 func_desc_adjust (&htab->tls_get_addr->elf, info);
3a71aa26
AM
7360 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7361 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7362 FALSE, FALSE, TRUE));
a7f2871e
AM
7363 if (!no_tls_get_addr_opt)
7364 {
7365 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7366
7367 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7368 FALSE, FALSE, TRUE);
7369 if (opt != NULL)
7370 func_desc_adjust (opt, info);
7371 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7372 FALSE, FALSE, TRUE);
7373 if (opt_fd != NULL
7374 && (opt_fd->root.type == bfd_link_hash_defined
7375 || opt_fd->root.type == bfd_link_hash_defweak))
7376 {
7377 /* If glibc supports an optimized __tls_get_addr call stub,
7378 signalled by the presence of __tls_get_addr_opt, and we'll
7379 be calling __tls_get_addr via a plt call stub, then
7380 make __tls_get_addr point to __tls_get_addr_opt. */
7381 tga_fd = &htab->tls_get_addr_fd->elf;
7382 if (htab->elf.dynamic_sections_created
7383 && tga_fd != NULL
7384 && (tga_fd->type == STT_FUNC
7385 || tga_fd->needs_plt)
7386 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7387 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7388 && tga_fd->root.type == bfd_link_hash_undefweak)))
7389 {
7390 struct plt_entry *ent;
7391
7392 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7393 if (ent->plt.refcount > 0)
7394 break;
7395 if (ent != NULL)
7396 {
7397 tga_fd->root.type = bfd_link_hash_indirect;
7398 tga_fd->root.u.i.link = &opt_fd->root;
7399 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7400 if (opt_fd->dynindx != -1)
7401 {
7402 /* Use __tls_get_addr_opt in dynamic relocations. */
7403 opt_fd->dynindx = -1;
7404 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7405 opt_fd->dynstr_index);
7406 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
854b41e7 7407 return NULL;
a7f2871e
AM
7408 }
7409 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7410 tga = &htab->tls_get_addr->elf;
7411 if (opt != NULL && tga != NULL)
7412 {
7413 tga->root.type = bfd_link_hash_indirect;
7414 tga->root.u.i.link = &opt->root;
7415 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7416 _bfd_elf_link_hash_hide_symbol (info, opt,
7417 tga->forced_local);
7418 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7419 }
7420 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7421 htab->tls_get_addr_fd->is_func_descriptor = 1;
7422 if (htab->tls_get_addr != NULL)
7423 {
7424 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7425 htab->tls_get_addr->is_func = 1;
7426 }
7427 }
7428 }
7429 }
7430 else
7431 no_tls_get_addr_opt = TRUE;
7432 }
7433 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
33c0ec9d 7434 return _bfd_elf_tls_setup (info->output_bfd, info);
3a71aa26 7435}
8387904d 7436
3a71aa26
AM
7437/* Return TRUE iff REL is a branch reloc with a global symbol matching
7438 HASH1 or HASH2. */
8387904d 7439
3a71aa26
AM
7440static bfd_boolean
7441branch_reloc_hash_match (const bfd *ibfd,
7442 const Elf_Internal_Rela *rel,
7443 const struct ppc_link_hash_entry *hash1,
7444 const struct ppc_link_hash_entry *hash2)
7445{
7446 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7447 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7448 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7449
e054468f 7450 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8387904d 7451 {
3a71aa26
AM
7452 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7453 struct elf_link_hash_entry *h;
8387904d 7454
3a71aa26 7455 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
b31867b6 7456 h = elf_follow_link (h);
3a71aa26
AM
7457 if (h == &hash1->elf || h == &hash2->elf)
7458 return TRUE;
a48ebf4d 7459 }
3a71aa26 7460 return FALSE;
951fd09b 7461}
411e1bfb 7462
951fd09b
AM
7463/* Run through all the TLS relocs looking for optimization
7464 opportunities. The linker has been hacked (see ppc64elf.em) to do
7465 a preliminary section layout so that we know the TLS segment
7466 offsets. We can't optimize earlier because some optimizations need
7467 to know the tp offset, and we need to optimize before allocating
7468 dynamic relocations. */
7469
7470bfd_boolean
33c0ec9d 7471ppc64_elf_tls_optimize (struct bfd_link_info *info)
951fd09b
AM
7472{
7473 bfd *ibfd;
7474 asection *sec;
7475 struct ppc_link_hash_table *htab;
663a1470 7476 unsigned char *toc_ref;
102890f0 7477 int pass;
951fd09b 7478
1d483afe 7479 if (info->relocatable || !info->executable)
411e1bfb
AM
7480 return TRUE;
7481
951fd09b 7482 htab = ppc_hash_table (info);
4dfe6ac6
NC
7483 if (htab == NULL)
7484 return FALSE;
7485
663a1470
AM
7486 /* Make two passes over the relocs. On the first pass, mark toc
7487 entries involved with tls relocs, and check that tls relocs
7488 involved in setting up a tls_get_addr call are indeed followed by
7489 such a call. If they are not, we can't do any tls optimization.
7490 On the second pass twiddle tls_mask flags to notify
7491 relocate_section that optimization can be done, and adjust got
7492 and plt refcounts. */
7493 toc_ref = NULL;
7494 for (pass = 0; pass < 2; ++pass)
7495 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7496 {
7497 Elf_Internal_Sym *locsyms = NULL;
7498 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7499
102890f0
AM
7500 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7501 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
7502 {
7503 Elf_Internal_Rela *relstart, *rel, *relend;
663a1470 7504 bfd_boolean found_tls_get_addr_arg = 0;
411e1bfb 7505
102890f0
AM
7506 /* Read the relocations. */
7507 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7508 info->keep_memory);
7509 if (relstart == NULL)
7510 return FALSE;
411e1bfb 7511
102890f0
AM
7512 relend = relstart + sec->reloc_count;
7513 for (rel = relstart; rel < relend; rel++)
7514 {
7515 enum elf_ppc64_reloc_type r_type;
7516 unsigned long r_symndx;
7517 struct elf_link_hash_entry *h;
7518 Elf_Internal_Sym *sym;
7519 asection *sym_sec;
f961d9dd
AM
7520 unsigned char *tls_mask;
7521 unsigned char tls_set, tls_clear, tls_type = 0;
102890f0
AM
7522 bfd_vma value;
7523 bfd_boolean ok_tprel, is_local;
7524 long toc_ref_index = 0;
7525 int expecting_tls_get_addr = 0;
663a1470 7526 bfd_boolean ret = FALSE;
411e1bfb 7527
102890f0
AM
7528 r_symndx = ELF64_R_SYM (rel->r_info);
7529 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
7530 r_symndx, ibfd))
7531 {
7532 err_free_rel:
7533 if (elf_section_data (sec)->relocs != relstart)
7534 free (relstart);
7535 if (toc_ref != NULL)
7536 free (toc_ref);
7537 if (locsyms != NULL
0ffa91dd 7538 && (elf_symtab_hdr (ibfd).contents
102890f0
AM
7539 != (unsigned char *) locsyms))
7540 free (locsyms);
663a1470 7541 return ret;
102890f0 7542 }
411e1bfb 7543
102890f0
AM
7544 if (h != NULL)
7545 {
766bc656
AM
7546 if (h->root.type == bfd_link_hash_defined
7547 || h->root.type == bfd_link_hash_defweak)
7548 value = h->root.u.def.value;
7549 else if (h->root.type == bfd_link_hash_undefweak)
7550 value = 0;
7551 else
663a1470
AM
7552 {
7553 found_tls_get_addr_arg = 0;
7554 continue;
7555 }
102890f0
AM
7556 }
7557 else
7558 /* Symbols referenced by TLS relocs must be of type
7559 STT_TLS. So no need for .opd local sym adjust. */
7560 value = sym->st_value;
7561
7562 ok_tprel = FALSE;
7563 is_local = FALSE;
7564 if (h == NULL
7565 || !h->def_dynamic)
7566 {
7567 is_local = TRUE;
766bc656
AM
7568 if (h != NULL
7569 && h->root.type == bfd_link_hash_undefweak)
7570 ok_tprel = TRUE;
7571 else
7572 {
7573 value += sym_sec->output_offset;
7574 value += sym_sec->output_section->vma;
7575 value -= htab->elf.tls_sec->vma;
7576 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
7577 < (bfd_vma) 1 << 32);
7578 }
102890f0 7579 }
951fd09b 7580
102890f0 7581 r_type = ELF64_R_TYPE (rel->r_info);
663a1470
AM
7582 /* If this section has old-style __tls_get_addr calls
7583 without marker relocs, then check that each
7584 __tls_get_addr call reloc is preceded by a reloc
7585 that conceivably belongs to the __tls_get_addr arg
7586 setup insn. If we don't find matching arg setup
7587 relocs, don't do any tls optimization. */
7588 if (pass == 0
7589 && sec->has_tls_get_addr_call
7590 && h != NULL
7591 && (h == &htab->tls_get_addr->elf
7592 || h == &htab->tls_get_addr_fd->elf)
7593 && !found_tls_get_addr_arg
7594 && is_branch_reloc (r_type))
7595 {
7596 info->callbacks->minfo (_("%C __tls_get_addr lost arg, "
7597 "TLS optimization disabled\n"),
7598 ibfd, sec, rel->r_offset);
7599 ret = TRUE;
7600 goto err_free_rel;
7601 }
7602
7603 found_tls_get_addr_arg = 0;
102890f0
AM
7604 switch (r_type)
7605 {
7606 case R_PPC64_GOT_TLSLD16:
7607 case R_PPC64_GOT_TLSLD16_LO:
7608 expecting_tls_get_addr = 1;
663a1470 7609 found_tls_get_addr_arg = 1;
102890f0
AM
7610 /* Fall thru */
7611
7612 case R_PPC64_GOT_TLSLD16_HI:
7613 case R_PPC64_GOT_TLSLD16_HA:
7614 /* These relocs should never be against a symbol
7615 defined in a shared lib. Leave them alone if
7616 that turns out to be the case. */
7617 if (!is_local)
7618 continue;
411e1bfb 7619
102890f0 7620 /* LD -> LE */
411e1bfb 7621 tls_set = 0;
102890f0
AM
7622 tls_clear = TLS_LD;
7623 tls_type = TLS_TLS | TLS_LD;
7624 break;
411e1bfb 7625
102890f0
AM
7626 case R_PPC64_GOT_TLSGD16:
7627 case R_PPC64_GOT_TLSGD16_LO:
7628 expecting_tls_get_addr = 1;
663a1470 7629 found_tls_get_addr_arg = 1;
102890f0
AM
7630 /* Fall thru */
7631
7632 case R_PPC64_GOT_TLSGD16_HI:
7633 case R_PPC64_GOT_TLSGD16_HA:
7634 if (ok_tprel)
7635 /* GD -> LE */
411e1bfb 7636 tls_set = 0;
102890f0
AM
7637 else
7638 /* GD -> IE */
7639 tls_set = TLS_TLS | TLS_TPRELGD;
7640 tls_clear = TLS_GD;
7641 tls_type = TLS_TLS | TLS_GD;
7642 break;
7643
7644 case R_PPC64_GOT_TPREL16_DS:
7645 case R_PPC64_GOT_TPREL16_LO_DS:
7646 case R_PPC64_GOT_TPREL16_HI:
7647 case R_PPC64_GOT_TPREL16_HA:
7648 if (ok_tprel)
7649 {
7650 /* IE -> LE */
7651 tls_set = 0;
7652 tls_clear = TLS_TPREL;
7653 tls_type = TLS_TLS | TLS_TPREL;
7654 break;
7655 }
411e1bfb
AM
7656 continue;
7657
727fc41e
AM
7658 case R_PPC64_TLSGD:
7659 case R_PPC64_TLSLD:
663a1470
AM
7660 found_tls_get_addr_arg = 1;
7661 /* Fall thru */
7662
7663 case R_PPC64_TLS:
7664 case R_PPC64_TOC16:
7665 case R_PPC64_TOC16_LO:
102890f0
AM
7666 if (sym_sec == NULL || sym_sec != toc)
7667 continue;
7668
7669 /* Mark this toc entry as referenced by a TLS
7670 code sequence. We can do that now in the
7671 case of R_PPC64_TLS, and after checking for
7672 tls_get_addr for the TOC16 relocs. */
7673 if (toc_ref == NULL)
663a1470
AM
7674 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
7675 if (toc_ref == NULL)
7676 goto err_free_rel;
7677
102890f0
AM
7678 if (h != NULL)
7679 value = h->root.u.def.value;
7680 else
7681 value = sym->st_value;
7682 value += rel->r_addend;
7683 BFD_ASSERT (value < toc->size && value % 8 == 0);
663a1470 7684 toc_ref_index = (value + toc->output_offset) / 8;
727fc41e
AM
7685 if (r_type == R_PPC64_TLS
7686 || r_type == R_PPC64_TLSGD
7687 || r_type == R_PPC64_TLSLD)
102890f0
AM
7688 {
7689 toc_ref[toc_ref_index] = 1;
7690 continue;
7691 }
7692
7693 if (pass != 0 && toc_ref[toc_ref_index] == 0)
7694 continue;
7695
7696 tls_set = 0;
7697 tls_clear = 0;
7698 expecting_tls_get_addr = 2;
7699 break;
7700
7701 case R_PPC64_TPREL64:
7702 if (pass == 0
7703 || sec != toc
7704 || toc_ref == NULL
663a1470 7705 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7706 continue;
7707 if (ok_tprel)
7708 {
7709 /* IE -> LE */
7710 tls_set = TLS_EXPLICIT;
7711 tls_clear = TLS_TPREL;
7712 break;
7713 }
7714 continue;
7715
7716 case R_PPC64_DTPMOD64:
7717 if (pass == 0
7718 || sec != toc
7719 || toc_ref == NULL
663a1470 7720 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
102890f0
AM
7721 continue;
7722 if (rel + 1 < relend
7723 && (rel[1].r_info
7724 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
7725 && rel[1].r_offset == rel->r_offset + 8)
7726 {
7727 if (ok_tprel)
7728 /* GD -> LE */
7729 tls_set = TLS_EXPLICIT | TLS_GD;
7730 else
7731 /* GD -> IE */
7732 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7733 tls_clear = TLS_GD;
7734 }
7735 else
7736 {
7737 if (!is_local)
7738 continue;
7739
7740 /* LD -> LE */
7741 tls_set = TLS_EXPLICIT;
7742 tls_clear = TLS_LD;
7743 }
7744 break;
7745
7746 default:
7747 continue;
7748 }
7749
7750 if (pass == 0)
7751 {
727fc41e
AM
7752 if (!expecting_tls_get_addr
7753 || !sec->has_tls_get_addr_call)
102890f0
AM
7754 continue;
7755
3a71aa26
AM
7756 if (rel + 1 < relend
7757 && branch_reloc_hash_match (ibfd, rel + 1,
7758 htab->tls_get_addr,
7759 htab->tls_get_addr_fd))
102890f0 7760 {
3a71aa26 7761 if (expecting_tls_get_addr == 2)
102890f0 7762 {
3a71aa26 7763 /* Check for toc tls entries. */
f961d9dd 7764 unsigned char *toc_tls;
3a71aa26
AM
7765 int retval;
7766
7767 retval = get_tls_mask (&toc_tls, NULL, NULL,
7768 &locsyms,
7769 rel, ibfd);
7770 if (retval == 0)
7771 goto err_free_rel;
663a1470
AM
7772 if (toc_tls != NULL)
7773 {
7774 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
7775 found_tls_get_addr_arg = 1;
7776 if (retval > 1)
7777 toc_ref[toc_ref_index] = 1;
7778 }
102890f0 7779 }
3a71aa26 7780 continue;
102890f0
AM
7781 }
7782
7783 if (expecting_tls_get_addr != 1)
7784 continue;
7785
7786 /* Uh oh, we didn't find the expected call. We
7787 could just mark this symbol to exclude it
7788 from tls optimization but it's safer to skip
663a1470
AM
7789 the entire optimization. */
7790 info->callbacks->minfo (_("%C arg lost __tls_get_addr, "
7791 "TLS optimization disabled\n"),
7792 ibfd, sec, rel->r_offset);
7793 ret = TRUE;
7794 goto err_free_rel;
102890f0
AM
7795 }
7796
85f7a9cb 7797 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
102890f0
AM
7798 {
7799 struct plt_entry *ent;
7800 for (ent = htab->tls_get_addr->elf.plt.plist;
7801 ent != NULL;
7802 ent = ent->next)
7803 if (ent->addend == 0)
411e1bfb 7804 {
102890f0 7805 if (ent->plt.refcount > 0)
30038c59 7806 {
102890f0
AM
7807 ent->plt.refcount -= 1;
7808 expecting_tls_get_addr = 0;
30038c59 7809 }
102890f0 7810 break;
411e1bfb 7811 }
102890f0 7812 }
411e1bfb 7813
85f7a9cb 7814 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
102890f0
AM
7815 {
7816 struct plt_entry *ent;
7817 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
7818 ent != NULL;
7819 ent = ent->next)
7820 if (ent->addend == 0)
411e1bfb 7821 {
102890f0
AM
7822 if (ent->plt.refcount > 0)
7823 ent->plt.refcount -= 1;
7824 break;
411e1bfb 7825 }
102890f0 7826 }
411e1bfb 7827
102890f0 7828 if (tls_clear == 0)
30038c59
AM
7829 continue;
7830
102890f0
AM
7831 if ((tls_set & TLS_EXPLICIT) == 0)
7832 {
7833 struct got_entry *ent;
411e1bfb 7834
102890f0
AM
7835 /* Adjust got entry for this reloc. */
7836 if (h != NULL)
7837 ent = h->got.glist;
7838 else
7839 ent = elf_local_got_ents (ibfd)[r_symndx];
411e1bfb 7840
102890f0
AM
7841 for (; ent != NULL; ent = ent->next)
7842 if (ent->addend == rel->r_addend
7843 && ent->owner == ibfd
7844 && ent->tls_type == tls_type)
7845 break;
7846 if (ent == NULL)
7847 abort ();
411e1bfb 7848
102890f0
AM
7849 if (tls_set == 0)
7850 {
7851 /* We managed to get rid of a got entry. */
7852 if (ent->got.refcount > 0)
7853 ent->got.refcount -= 1;
7854 }
7855 }
7856 else
7857 {
7858 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7859 we'll lose one or two dyn relocs. */
7860 if (!dec_dynrel_count (rel->r_info, sec, info,
7861 NULL, h, sym_sec))
7862 return FALSE;
411e1bfb 7863
102890f0
AM
7864 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7865 {
7866 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7867 NULL, h, sym_sec))
7868 return FALSE;
7869 }
7870 }
411e1bfb 7871
102890f0
AM
7872 *tls_mask |= tls_set;
7873 *tls_mask &= ~tls_clear;
7874 }
8c1d1bb8 7875
102890f0
AM
7876 if (elf_section_data (sec)->relocs != relstart)
7877 free (relstart);
7878 }
411e1bfb 7879
663a1470
AM
7880 if (locsyms != NULL
7881 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
7882 {
7883 if (!info->keep_memory)
7884 free (locsyms);
7885 else
7886 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
7887 }
7888 }
411e1bfb 7889
663a1470
AM
7890 if (toc_ref != NULL)
7891 free (toc_ref);
b34976b6 7892 return TRUE;
1e2f5b6e 7893}
b34976b6 7894
c5614fa4
AM
7895/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7896 the values of any global symbols in a toc section that has been
7897 edited. Globals in toc sections should be a rarity, so this function
7898 sets a flag if any are found in toc sections other than the one just
7899 edited, so that futher hash table traversals can be avoided. */
7900
7901struct adjust_toc_info
7902{
7903 asection *toc;
7904 unsigned long *skip;
7905 bfd_boolean global_toc_syms;
7906};
7907
ba761f19
AM
7908enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
7909
c5614fa4
AM
7910static bfd_boolean
7911adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7912{
7913 struct ppc_link_hash_entry *eh;
7914 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
854b41e7 7915 unsigned long i;
c5614fa4
AM
7916
7917 if (h->root.type == bfd_link_hash_indirect)
7918 return TRUE;
7919
7920 if (h->root.type == bfd_link_hash_warning)
7921 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7922
7923 if (h->root.type != bfd_link_hash_defined
7924 && h->root.type != bfd_link_hash_defweak)
7925 return TRUE;
7926
7927 eh = (struct ppc_link_hash_entry *) h;
7928 if (eh->adjust_done)
7929 return TRUE;
7930
7931 if (eh->elf.root.u.def.section == toc_inf->toc)
7932 {
854b41e7
AM
7933 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
7934 i = toc_inf->toc->rawsize >> 3;
c5614fa4 7935 else
854b41e7
AM
7936 i = eh->elf.root.u.def.value >> 3;
7937
ba761f19 7938 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
c5614fa4
AM
7939 {
7940 (*_bfd_error_handler)
854b41e7
AM
7941 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
7942 do
7943 ++i;
ba761f19 7944 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
854b41e7 7945 eh->elf.root.u.def.value = (bfd_vma) i << 3;
c5614fa4 7946 }
854b41e7
AM
7947
7948 eh->elf.root.u.def.value -= toc_inf->skip[i];
c5614fa4
AM
7949 eh->adjust_done = 1;
7950 }
7951 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7952 toc_inf->global_toc_syms = TRUE;
7953
7954 return TRUE;
7955}
7956
7957/* Examine all relocs referencing .toc sections in order to remove
7958 unused .toc entries. */
7959
7960bfd_boolean
33c0ec9d 7961ppc64_elf_edit_toc (struct bfd_link_info *info)
c5614fa4
AM
7962{
7963 bfd *ibfd;
7964 struct adjust_toc_info toc_inf;
67f0cbdb 7965 struct ppc_link_hash_table *htab = ppc_hash_table (info);
c5614fa4 7966
67f0cbdb 7967 htab->do_toc_opt = 1;
c5614fa4
AM
7968 toc_inf.global_toc_syms = TRUE;
7969 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7970 {
7971 asection *toc, *sec;
7972 Elf_Internal_Shdr *symtab_hdr;
7973 Elf_Internal_Sym *local_syms;
425b145b 7974 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
c5614fa4
AM
7975 unsigned long *skip, *drop;
7976 unsigned char *used;
7977 unsigned char *keep, last, some_unused;
7978
854b41e7
AM
7979 if (!is_ppc64_elf (ibfd))
7980 continue;
7981
c5614fa4
AM
7982 toc = bfd_get_section_by_name (ibfd, ".toc");
7983 if (toc == NULL
92b7a70f 7984 || toc->size == 0
c5614fa4
AM
7985 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7986 || elf_discarded_section (toc))
7987 continue;
7988
425b145b 7989 toc_relocs = NULL;
c5614fa4 7990 local_syms = NULL;
0ffa91dd 7991 symtab_hdr = &elf_symtab_hdr (ibfd);
c5614fa4
AM
7992
7993 /* Look at sections dropped from the final link. */
7994 skip = NULL;
7995 relstart = NULL;
7996 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7997 {
7998 if (sec->reloc_count == 0
7999 || !elf_discarded_section (sec)
8000 || get_opd_info (sec)
8001 || (sec->flags & SEC_ALLOC) == 0
8002 || (sec->flags & SEC_DEBUGGING) != 0)
8003 continue;
8004
8005 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8006 if (relstart == NULL)
8007 goto error_ret;
8008
8009 /* Run through the relocs to see which toc entries might be
8010 unused. */
8011 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8012 {
8013 enum elf_ppc64_reloc_type r_type;
8014 unsigned long r_symndx;
8015 asection *sym_sec;
8016 struct elf_link_hash_entry *h;
8017 Elf_Internal_Sym *sym;
8018 bfd_vma val;
8019
8020 r_type = ELF64_R_TYPE (rel->r_info);
8021 switch (r_type)
8022 {
8023 default:
8024 continue;
8025
8026 case R_PPC64_TOC16:
8027 case R_PPC64_TOC16_LO:
8028 case R_PPC64_TOC16_HI:
8029 case R_PPC64_TOC16_HA:
8030 case R_PPC64_TOC16_DS:
8031 case R_PPC64_TOC16_LO_DS:
8032 break;
8033 }
8034
8035 r_symndx = ELF64_R_SYM (rel->r_info);
8036 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8037 r_symndx, ibfd))
8038 goto error_ret;
8039
8040 if (sym_sec != toc)
8041 continue;
8042
8043 if (h != NULL)
8044 val = h->root.u.def.value;
8045 else
8046 val = sym->st_value;
8047 val += rel->r_addend;
8048
8049 if (val >= toc->size)
8050 continue;
8051
8052 /* Anything in the toc ought to be aligned to 8 bytes.
8053 If not, don't mark as unused. */
8054 if (val & 7)
8055 continue;
8056
8057 if (skip == NULL)
8058 {
854b41e7 8059 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
c5614fa4
AM
8060 if (skip == NULL)
8061 goto error_ret;
8062 }
8063
ba761f19 8064 skip[val >> 3] = ref_from_discarded;
c5614fa4
AM
8065 }
8066
8067 if (elf_section_data (sec)->relocs != relstart)
8068 free (relstart);
8069 }
8070
ba761f19
AM
8071 /* For largetoc loads of address constants, we can convert
8072 . addis rx,2,addr@got@ha
8073 . ld ry,addr@got@l(rx)
8074 to
8075 . addis rx,2,addr@toc@ha
8076 . addi ry,rx,addr@toc@l
8077 when addr is within 2G of the toc pointer. This then means
8078 that the word storing "addr" in the toc is no longer needed. */
8079
8080 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8081 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8082 && toc->reloc_count != 0)
8083 {
8084 /* Read toc relocs. */
425b145b
AM
8085 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8086 info->keep_memory);
8087 if (toc_relocs == NULL)
ba761f19
AM
8088 goto error_ret;
8089
425b145b 8090 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8091 {
8092 enum elf_ppc64_reloc_type r_type;
8093 unsigned long r_symndx;
8094 asection *sym_sec;
8095 struct elf_link_hash_entry *h;
8096 Elf_Internal_Sym *sym;
8097 bfd_vma val, addr;
8098
8099 r_type = ELF64_R_TYPE (rel->r_info);
8100 if (r_type != R_PPC64_ADDR64)
8101 continue;
8102
8103 r_symndx = ELF64_R_SYM (rel->r_info);
8104 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8105 r_symndx, ibfd))
8106 goto error_ret;
8107
425b145b
AM
8108 if (sym_sec == NULL
8109 || elf_discarded_section (sym_sec))
8110 continue;
8111
726d3ab0 8112 if (!SYMBOL_CALLS_LOCAL (info, h))
ba761f19
AM
8113 continue;
8114
8115 if (h != NULL)
bddc25c9
AM
8116 {
8117 if (h->type == STT_GNU_IFUNC)
8118 continue;
8119 val = h->root.u.def.value;
8120 }
ba761f19 8121 else
bddc25c9
AM
8122 {
8123 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8124 continue;
8125 val = sym->st_value;
8126 }
ba761f19
AM
8127 val += rel->r_addend;
8128 val += sym_sec->output_section->vma + sym_sec->output_offset;
8129
8130 /* We don't yet know the exact toc pointer value, but we
8131 know it will be somewhere in the toc section. Don't
8132 optimize if the difference from any possible toc
8133 pointer is outside [ff..f80008000, 7fff7fff]. */
8134 addr = toc->output_section->vma + TOC_BASE_OFF;
8135 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8136 continue;
8137
8138 addr = toc->output_section->vma + toc->output_section->rawsize;
8139 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8140 continue;
8141
8142 if (skip == NULL)
8143 {
8144 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8145 if (skip == NULL)
8146 goto error_ret;
8147 }
8148
8149 skip[rel->r_offset >> 3]
425b145b 8150 |= can_optimize | ((rel - toc_relocs) << 2);
ba761f19 8151 }
ba761f19
AM
8152 }
8153
c5614fa4
AM
8154 if (skip == NULL)
8155 continue;
8156
8157 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8158 if (used == NULL)
8159 {
8160 error_ret:
8161 if (local_syms != NULL
8162 && symtab_hdr->contents != (unsigned char *) local_syms)
8163 free (local_syms);
8164 if (sec != NULL
8165 && relstart != NULL
8166 && elf_section_data (sec)->relocs != relstart)
8167 free (relstart);
425b145b
AM
8168 if (toc_relocs != NULL
8169 && elf_section_data (toc)->relocs != toc_relocs)
8170 free (toc_relocs);
c5614fa4
AM
8171 if (skip != NULL)
8172 free (skip);
8173 return FALSE;
8174 }
8175
30038c59
AM
8176 /* Now check all kept sections that might reference the toc.
8177 Check the toc itself last. */
8178 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8179 : ibfd->sections);
c5614fa4 8180 sec != NULL;
c5614fa4 8181 sec = (sec == toc ? NULL
c5614fa4 8182 : sec->next == NULL ? toc
30038c59 8183 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
8184 : sec->next))
8185 {
8186 int repeat;
8187
8188 if (sec->reloc_count == 0
8189 || elf_discarded_section (sec)
8190 || get_opd_info (sec)
8191 || (sec->flags & SEC_ALLOC) == 0
8192 || (sec->flags & SEC_DEBUGGING) != 0)
8193 continue;
8194
854b41e7
AM
8195 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8196 info->keep_memory);
c5614fa4
AM
8197 if (relstart == NULL)
8198 goto error_ret;
8199
8200 /* Mark toc entries referenced as used. */
8201 repeat = 0;
8202 do
8203 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8204 {
8205 enum elf_ppc64_reloc_type r_type;
8206 unsigned long r_symndx;
8207 asection *sym_sec;
8208 struct elf_link_hash_entry *h;
8209 Elf_Internal_Sym *sym;
8210 bfd_vma val;
8211
8212 r_type = ELF64_R_TYPE (rel->r_info);
8213 switch (r_type)
8214 {
8215 case R_PPC64_TOC16:
8216 case R_PPC64_TOC16_LO:
8217 case R_PPC64_TOC16_HI:
8218 case R_PPC64_TOC16_HA:
8219 case R_PPC64_TOC16_DS:
8220 case R_PPC64_TOC16_LO_DS:
8221 /* In case we're taking addresses of toc entries. */
8222 case R_PPC64_ADDR64:
8223 break;
8224
8225 default:
8226 continue;
8227 }
8228
8229 r_symndx = ELF64_R_SYM (rel->r_info);
8230 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8231 r_symndx, ibfd))
8232 {
8233 free (used);
8234 goto error_ret;
8235 }
8236
8237 if (sym_sec != toc)
8238 continue;
8239
8240 if (h != NULL)
8241 val = h->root.u.def.value;
8242 else
8243 val = sym->st_value;
8244 val += rel->r_addend;
8245
8246 if (val >= toc->size)
8247 continue;
8248
ba761f19
AM
8249 if ((skip[val >> 3] & can_optimize) != 0)
8250 {
8251 bfd_vma off;
8252 unsigned char opc;
8253
8254 switch (r_type)
8255 {
8256 case R_PPC64_TOC16_HA:
8257 break;
8258
8259 case R_PPC64_TOC16_LO_DS:
8260 off = rel->r_offset + (bfd_big_endian (ibfd) ? -2 : 3);
8261 if (!bfd_get_section_contents (ibfd, sec, &opc, off, 1))
8262 return FALSE;
8263 if ((opc & (0x3f << 2)) == (58u << 2))
8264 break;
8265 /* Fall thru */
8266
8267 default:
8268 /* Wrong sort of reloc, or not a ld. We may
8269 as well clear ref_from_discarded too. */
8270 skip[val >> 3] = 0;
8271 }
8272 }
8273
c5614fa4
AM
8274 /* For the toc section, we only mark as used if
8275 this entry itself isn't unused. */
8276 if (sec == toc
8277 && !used[val >> 3]
8278 && (used[rel->r_offset >> 3]
ba761f19 8279 || !(skip[rel->r_offset >> 3] & ref_from_discarded)))
c5614fa4
AM
8280 /* Do all the relocs again, to catch reference
8281 chains. */
8282 repeat = 1;
8283
8284 used[val >> 3] = 1;
8285 }
8286 while (repeat);
854b41e7
AM
8287
8288 if (elf_section_data (sec)->relocs != relstart)
8289 free (relstart);
c5614fa4
AM
8290 }
8291
8292 /* Merge the used and skip arrays. Assume that TOC
8293 doublewords not appearing as either used or unused belong
8294 to to an entry more than one doubleword in size. */
8295 for (drop = skip, keep = used, last = 0, some_unused = 0;
8296 drop < skip + (toc->size + 7) / 8;
8297 ++drop, ++keep)
8298 {
8299 if (*keep)
8300 {
ba761f19
AM
8301 *drop &= ~ref_from_discarded;
8302 if ((*drop & can_optimize) != 0)
8303 some_unused = 1;
c5614fa4
AM
8304 last = 0;
8305 }
8306 else if (*drop)
8307 {
8308 some_unused = 1;
ba761f19 8309 last = ref_from_discarded;
c5614fa4
AM
8310 }
8311 else
8312 *drop = last;
8313 }
8314
8315 free (used);
8316
8317 if (some_unused)
8318 {
8319 bfd_byte *contents, *src;
8320 unsigned long off;
d62b3684 8321 Elf_Internal_Sym *sym;
ba761f19 8322 bfd_boolean local_toc_syms = FALSE;
c5614fa4
AM
8323
8324 /* Shuffle the toc contents, and at the same time convert the
8325 skip array from booleans into offsets. */
8326 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8327 goto error_ret;
8328
8329 elf_section_data (toc)->this_hdr.contents = contents;
8330
8331 for (src = contents, off = 0, drop = skip;
8332 src < contents + toc->size;
8333 src += 8, ++drop)
8334 {
ba761f19
AM
8335 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8336 off += 8;
c5614fa4
AM
8337 else if (off != 0)
8338 {
8339 *drop = off;
8340 memcpy (src - off, src, 8);
8341 }
8342 }
854b41e7 8343 *drop = off;
c5614fa4
AM
8344 toc->rawsize = toc->size;
8345 toc->size = src - contents - off;
8346
ba761f19
AM
8347 /* Adjust addends for relocs against the toc section sym,
8348 and optimize any accesses we can. */
c5614fa4
AM
8349 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8350 {
8351 if (sec->reloc_count == 0
8352 || elf_discarded_section (sec))
8353 continue;
8354
8355 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
854b41e7 8356 info->keep_memory);
c5614fa4
AM
8357 if (relstart == NULL)
8358 goto error_ret;
8359
8360 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8361 {
8362 enum elf_ppc64_reloc_type r_type;
8363 unsigned long r_symndx;
8364 asection *sym_sec;
8365 struct elf_link_hash_entry *h;
854b41e7 8366 bfd_vma val;
c5614fa4
AM
8367
8368 r_type = ELF64_R_TYPE (rel->r_info);
8369 switch (r_type)
8370 {
8371 default:
8372 continue;
8373
8374 case R_PPC64_TOC16:
8375 case R_PPC64_TOC16_LO:
8376 case R_PPC64_TOC16_HI:
8377 case R_PPC64_TOC16_HA:
8378 case R_PPC64_TOC16_DS:
8379 case R_PPC64_TOC16_LO_DS:
8380 case R_PPC64_ADDR64:
8381 break;
8382 }
8383
8384 r_symndx = ELF64_R_SYM (rel->r_info);
8385 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8386 r_symndx, ibfd))
8387 goto error_ret;
8388
ba761f19 8389 if (sym_sec != toc)
c5614fa4
AM
8390 continue;
8391
ba761f19
AM
8392 if (h != NULL)
8393 val = h->root.u.def.value;
8394 else
8395 {
8396 val = sym->st_value;
8397 if (val != 0)
8398 local_toc_syms = TRUE;
8399 }
8400
8401 val += rel->r_addend;
854b41e7
AM
8402
8403 if (val > toc->rawsize)
8404 val = toc->rawsize;
ba761f19
AM
8405 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8406 continue;
8407 else if ((skip[val >> 3] & can_optimize) != 0)
8408 {
8409 Elf_Internal_Rela *tocrel
425b145b 8410 = toc_relocs + (skip[val >> 3] >> 2);
ba761f19
AM
8411 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
8412
8413 switch (r_type)
8414 {
8415 case R_PPC64_TOC16_HA:
8416 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
8417 break;
8418
8419 case R_PPC64_TOC16_LO_DS:
8420 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
8421 break;
8422
8423 default:
8424 abort ();
8425 }
8426 rel->r_addend = tocrel->r_addend;
8427 elf_section_data (sec)->relocs = relstart;
8428 continue;
8429 }
8430
8431 if (h != NULL || sym->st_value != 0)
8432 continue;
854b41e7
AM
8433
8434 rel->r_addend -= skip[val >> 3];
8435 elf_section_data (sec)->relocs = relstart;
c5614fa4 8436 }
854b41e7
AM
8437
8438 if (elf_section_data (sec)->relocs != relstart)
8439 free (relstart);
c5614fa4
AM
8440 }
8441
8442 /* We shouldn't have local or global symbols defined in the TOC,
8443 but handle them anyway. */
df22d223
AM
8444 if (local_syms != NULL)
8445 for (sym = local_syms;
8446 sym < local_syms + symtab_hdr->sh_info;
8447 ++sym)
8448 if (sym->st_value != 0
8449 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
8450 {
8451 unsigned long i;
854b41e7 8452
df22d223
AM
8453 if (sym->st_value > toc->rawsize)
8454 i = toc->rawsize >> 3;
8455 else
8456 i = sym->st_value >> 3;
854b41e7 8457
df22d223
AM
8458 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
8459 {
8460 if (local_toc_syms)
8461 (*_bfd_error_handler)
8462 (_("%s defined on removed toc entry"),
8463 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
8464 do
8465 ++i;
8466 while ((skip[i] & (ref_from_discarded | can_optimize)));
8467 sym->st_value = (bfd_vma) i << 3;
8468 }
d62b3684 8469
df22d223
AM
8470 sym->st_value -= skip[i];
8471 symtab_hdr->contents = (unsigned char *) local_syms;
8472 }
c5614fa4 8473
854b41e7 8474 /* Adjust any global syms defined in this toc input section. */
c5614fa4
AM
8475 if (toc_inf.global_toc_syms)
8476 {
8477 toc_inf.toc = toc;
8478 toc_inf.skip = skip;
8479 toc_inf.global_toc_syms = FALSE;
8480 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
8481 &toc_inf);
8482 }
854b41e7
AM
8483
8484 if (toc->reloc_count != 0)
8485 {
d4730f92 8486 Elf_Internal_Shdr *rel_hdr;
854b41e7
AM
8487 Elf_Internal_Rela *wrel;
8488 bfd_size_type sz;
8489
854b41e7 8490 /* Remove unused toc relocs, and adjust those we keep. */
28be611c
AM
8491 if (toc_relocs == NULL)
8492 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8493 info->keep_memory);
8494 if (toc_relocs == NULL)
8495 goto error_ret;
8496
425b145b
AM
8497 wrel = toc_relocs;
8498 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
ba761f19
AM
8499 if ((skip[rel->r_offset >> 3]
8500 & (ref_from_discarded | can_optimize)) == 0)
854b41e7
AM
8501 {
8502 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
8503 wrel->r_info = rel->r_info;
8504 wrel->r_addend = rel->r_addend;
8505 ++wrel;
8506 }
8507 else if (!dec_dynrel_count (rel->r_info, toc, info,
8508 &local_syms, NULL, NULL))
8509 goto error_ret;
8510
425b145b
AM
8511 elf_section_data (toc)->relocs = toc_relocs;
8512 toc->reloc_count = wrel - toc_relocs;
d4730f92
BS
8513 rel_hdr = _bfd_elf_single_rel_hdr (toc);
8514 sz = rel_hdr->sh_entsize;
8515 rel_hdr->sh_size = toc->reloc_count * sz;
854b41e7 8516 }
c5614fa4 8517 }
28be611c
AM
8518 else if (toc_relocs != NULL
8519 && elf_section_data (toc)->relocs != toc_relocs)
425b145b 8520 free (toc_relocs);
c5614fa4
AM
8521
8522 if (local_syms != NULL
8523 && symtab_hdr->contents != (unsigned char *) local_syms)
8524 {
8525 if (!info->keep_memory)
8526 free (local_syms);
8527 else
8528 symtab_hdr->contents = (unsigned char *) local_syms;
8529 }
8530 free (skip);
8531 }
8532
8533 return TRUE;
8534}
8535
1bbe0902
AM
8536/* Return true iff input section I references the TOC using
8537 instructions limited to +/-32k offsets. */
8538
8539bfd_boolean
8540ppc64_elf_has_small_toc_reloc (asection *i)
8541{
8542 return (is_ppc64_elf (i->owner)
8543 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
8544}
8545
927be08e
AM
8546/* Allocate space for one GOT entry. */
8547
8548static void
8549allocate_got (struct elf_link_hash_entry *h,
8550 struct bfd_link_info *info,
8551 struct got_entry *gent)
8552{
8553 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8554 bfd_boolean dyn;
8555 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8556 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
8557 ? 16 : 8);
8558 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
8559 ? 2 : 1) * sizeof (Elf64_External_Rela);
8560 asection *got = ppc64_elf_tdata (gent->owner)->got;
8561
8562 gent->got.offset = got->size;
8563 got->size += entsize;
8564
8565 dyn = htab->elf.dynamic_sections_created;
8566 if ((info->shared
8567 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
8568 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
8569 || h->root.type != bfd_link_hash_undefweak))
8570 {
8571 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
8572 relgot->size += rentsize;
8573 }
8574 else if (h->type == STT_GNU_IFUNC)
8575 {
8576 asection *relgot = htab->reliplt;
8577 relgot->size += rentsize;
8578 htab->got_reli_size += rentsize;
8579 }
8580}
8581
7865406b
AM
8582/* This function merges got entries in the same toc group. */
8583
8584static void
8585merge_got_entries (struct got_entry **pent)
8586{
8587 struct got_entry *ent, *ent2;
8588
8589 for (ent = *pent; ent != NULL; ent = ent->next)
8590 if (!ent->is_indirect)
8591 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
8592 if (!ent2->is_indirect
8593 && ent2->addend == ent->addend
8594 && ent2->tls_type == ent->tls_type
8595 && elf_gp (ent2->owner) == elf_gp (ent->owner))
8596 {
8597 ent2->is_indirect = TRUE;
8598 ent2->got.ent = ent;
8599 }
8600}
8601
65f38f15
AM
8602/* Allocate space in .plt, .got and associated reloc sections for
8603 dynamic relocs. */
5bd4f169 8604
b34976b6 8605static bfd_boolean
4ce794b7 8606allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 8607{
65f38f15
AM
8608 struct bfd_link_info *info;
8609 struct ppc_link_hash_table *htab;
5bd4f169 8610 asection *s;
65f38f15
AM
8611 struct ppc_link_hash_entry *eh;
8612 struct ppc_dyn_relocs *p;
0b8bcf0d 8613 struct got_entry **pgent, *gent;
5bd4f169 8614
e92d460e 8615 if (h->root.type == bfd_link_hash_indirect)
b34976b6 8616 return TRUE;
5bd4f169 8617
e92d460e
AM
8618 if (h->root.type == bfd_link_hash_warning)
8619 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8620
65f38f15
AM
8621 info = (struct bfd_link_info *) inf;
8622 htab = ppc_hash_table (info);
4dfe6ac6
NC
8623 if (htab == NULL)
8624 return FALSE;
5bd4f169 8625
e054468f
AM
8626 if ((htab->elf.dynamic_sections_created
8627 && h->dynindx != -1
8628 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
8629 || h->type == STT_GNU_IFUNC)
5bd4f169 8630 {
411e1bfb
AM
8631 struct plt_entry *pent;
8632 bfd_boolean doneone = FALSE;
8633 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
8634 if (pent->plt.refcount > 0)
8635 {
25f23106
AM
8636 if (!htab->elf.dynamic_sections_created
8637 || h->dynindx == -1)
e054468f
AM
8638 {
8639 s = htab->iplt;
8640 pent->plt.offset = s->size;
8641 s->size += PLT_ENTRY_SIZE;
8642 s = htab->reliplt;
8643 }
8644 else
8645 {
8646 /* If this is the first .plt entry, make room for the special
8647 first entry. */
8648 s = htab->plt;
8649 if (s->size == 0)
8650 s->size += PLT_INITIAL_ENTRY_SIZE;
8651
8652 pent->plt.offset = s->size;
8653
8654 /* Make room for this entry. */
8655 s->size += PLT_ENTRY_SIZE;
8656
8657 /* Make room for the .glink code. */
8658 s = htab->glink;
8659 if (s->size == 0)
8660 s->size += GLINK_CALL_STUB_SIZE;
8661 /* We need bigger stubs past index 32767. */
8662 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
8663 s->size += 4;
8664 s->size += 2*4;
8665
8666 /* We also need to make an entry in the .rela.plt section. */
8667 s = htab->relplt;
8668 }
eea6121a 8669 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
8670 doneone = TRUE;
8671 }
8672 else
8673 pent->plt.offset = (bfd_vma) -1;
8674 if (!doneone)
65f38f15 8675 {
411e1bfb 8676 h->plt.plist = NULL;
f5385ebf 8677 h->needs_plt = 0;
65f38f15
AM
8678 }
8679 }
8680 else
8681 {
411e1bfb 8682 h->plt.plist = NULL;
f5385ebf 8683 h->needs_plt = 0;
65f38f15
AM
8684 }
8685
951fd09b
AM
8686 eh = (struct ppc_link_hash_entry *) h;
8687 /* Run through the TLS GD got entries first if we're changing them
8688 to TPREL. */
e7b938ca 8689 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
8690 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8691 if (gent->got.refcount > 0
8692 && (gent->tls_type & TLS_GD) != 0)
8693 {
8694 /* This was a GD entry that has been converted to TPREL. If
8695 there happens to be a TPREL entry we can use that one. */
8696 struct got_entry *ent;
8697 for (ent = h->got.glist; ent != NULL; ent = ent->next)
8698 if (ent->got.refcount > 0
8699 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
8700 && ent->addend == gent->addend
8701 && ent->owner == gent->owner)
951fd09b
AM
8702 {
8703 gent->got.refcount = 0;
8704 break;
8705 }
8706
8707 /* If not, then we'll be using our own TPREL entry. */
8708 if (gent->got.refcount != 0)
8709 gent->tls_type = TLS_TLS | TLS_TPREL;
8710 }
8711
7865406b
AM
8712 /* Remove any list entry that won't generate a word in the GOT before
8713 we call merge_got_entries. Otherwise we risk merging to empty
8714 entries. */
0b8bcf0d
AM
8715 pgent = &h->got.glist;
8716 while ((gent = *pgent) != NULL)
411e1bfb 8717 if (gent->got.refcount > 0)
7865406b
AM
8718 {
8719 if ((gent->tls_type & TLS_LD) != 0
8720 && !h->def_dynamic)
8721 {
8722 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
8723 *pgent = gent->next;
8724 }
8725 else
8726 pgent = &gent->next;
8727 }
8728 else
8729 *pgent = gent->next;
8730
8731 if (!htab->do_multi_toc)
8732 merge_got_entries (&h->got.glist);
8733
8734 for (gent = h->got.glist; gent != NULL; gent = gent->next)
8735 if (!gent->is_indirect)
411e1bfb
AM
8736 {
8737 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
8738 Undefined weak syms won't yet be marked as dynamic,
8739 nor will all TLS symbols. */
411e1bfb 8740 if (h->dynindx == -1
b099ab9f 8741 && !h->forced_local
25f23106 8742 && h->type != STT_GNU_IFUNC
b099ab9f 8743 && htab->elf.dynamic_sections_created)
411e1bfb 8744 {
c152c796 8745 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
8746 return FALSE;
8747 }
65f38f15 8748
0c8d6e5c 8749 if (!is_ppc64_elf (gent->owner))
927be08e 8750 abort ();
0ffa91dd 8751
927be08e 8752 allocate_got (h, info, gent);
411e1bfb 8753 }
65f38f15 8754
b099ab9f 8755 if (eh->dyn_relocs == NULL
25f23106
AM
8756 || (!htab->elf.dynamic_sections_created
8757 && h->type != STT_GNU_IFUNC))
b34976b6 8758 return TRUE;
65f38f15
AM
8759
8760 /* In the shared -Bsymbolic case, discard space allocated for
8761 dynamic pc-relative relocs against symbols which turn out to be
8762 defined in regular objects. For the normal shared case, discard
8763 space for relocs that have become local due to symbol visibility
8764 changes. */
8765
8766 if (info->shared)
8767 {
9c7a29a3 8768 /* Relocs that use pc_count are those that appear on a call insn,
1d483afe 8769 or certain REL relocs (see must_be_dyn_reloc) that can be
9c7a29a3
AM
8770 generated via assembly. We want calls to protected symbols to
8771 resolve directly to the function rather than going via the plt.
8772 If people want function pointer comparisons to work as expected
8773 then they should avoid writing weird assembly. */
09695f56 8774 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15
AM
8775 {
8776 struct ppc_dyn_relocs **pp;
8777
8778 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 8779 {
65f38f15
AM
8780 p->count -= p->pc_count;
8781 p->pc_count = 0;
8782 if (p->count == 0)
8783 *pp = p->next;
8784 else
8785 pp = &p->next;
5bd4f169 8786 }
65f38f15 8787 }
4e795f50
AM
8788
8789 /* Also discard relocs on undefined weak syms with non-default
8790 visibility. */
cab87ef9
AM
8791 if (eh->dyn_relocs != NULL
8792 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
8793 {
8794 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
8795 eh->dyn_relocs = NULL;
8796
8797 /* Make sure this symbol is output as a dynamic symbol.
8798 Undefined weak syms won't yet be marked as dynamic. */
8799 else if (h->dynindx == -1
8800 && !h->forced_local)
8801 {
8802 if (! bfd_elf_link_record_dynamic_symbol (info, h))
8803 return FALSE;
8804 }
8805 }
65f38f15 8806 }
25f23106
AM
8807 else if (h->type == STT_GNU_IFUNC)
8808 {
8809 if (!h->non_got_ref)
8810 eh->dyn_relocs = NULL;
8811 }
f4656909 8812 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
8813 {
8814 /* For the non-shared case, discard space for relocs against
8815 symbols which turn out to need copy relocs or are not
8816 dynamic. */
8817
f5385ebf 8818 if (!h->non_got_ref
f5385ebf 8819 && !h->def_regular)
65f38f15
AM
8820 {
8821 /* Make sure this symbol is output as a dynamic symbol.
8822 Undefined weak syms won't yet be marked as dynamic. */
8823 if (h->dynindx == -1
f5385ebf 8824 && !h->forced_local)
65f38f15 8825 {
c152c796 8826 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 8827 return FALSE;
65f38f15
AM
8828 }
8829
8830 /* If that succeeded, we know we'll be keeping all the
8831 relocs. */
8832 if (h->dynindx != -1)
8833 goto keep;
8834 }
8835
8836 eh->dyn_relocs = NULL;
8837
ec338859 8838 keep: ;
65f38f15
AM
8839 }
8840
8841 /* Finally, allocate space. */
8842 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8843 {
8844 asection *sreloc = elf_section_data (p->sec)->sreloc;
25f23106
AM
8845 if (!htab->elf.dynamic_sections_created)
8846 sreloc = htab->reliplt;
eea6121a 8847 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
8848 }
8849
b34976b6 8850 return TRUE;
65f38f15
AM
8851}
8852
8853/* Find any dynamic relocs that apply to read-only sections. */
8854
b34976b6 8855static bfd_boolean
4ce794b7 8856readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
8857{
8858 struct ppc_link_hash_entry *eh;
8859 struct ppc_dyn_relocs *p;
8860
e92d460e
AM
8861 if (h->root.type == bfd_link_hash_warning)
8862 h = (struct elf_link_hash_entry *) h->root.u.i.link;
8863
65f38f15
AM
8864 eh = (struct ppc_link_hash_entry *) h;
8865 for (p = eh->dyn_relocs; p != NULL; p = p->next)
8866 {
8867 asection *s = p->sec->output_section;
8868
8869 if (s != NULL && (s->flags & SEC_READONLY) != 0)
8870 {
4ce794b7 8871 struct bfd_link_info *info = inf;
65f38f15
AM
8872
8873 info->flags |= DF_TEXTREL;
8874
8875 /* Not an error, just cut short the traversal. */
b34976b6 8876 return FALSE;
65f38f15
AM
8877 }
8878 }
b34976b6 8879 return TRUE;
65f38f15
AM
8880}
8881
8882/* Set the sizes of the dynamic sections. */
8883
b34976b6 8884static bfd_boolean
4ce794b7
AM
8885ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
8886 struct bfd_link_info *info)
65f38f15
AM
8887{
8888 struct ppc_link_hash_table *htab;
8889 bfd *dynobj;
8890 asection *s;
b34976b6 8891 bfd_boolean relocs;
65f38f15 8892 bfd *ibfd;
7865406b 8893 struct got_entry *first_tlsld;
65f38f15
AM
8894
8895 htab = ppc_hash_table (info);
4dfe6ac6
NC
8896 if (htab == NULL)
8897 return FALSE;
8898
65f38f15
AM
8899 dynobj = htab->elf.dynobj;
8900 if (dynobj == NULL)
8901 abort ();
8902
8903 if (htab->elf.dynamic_sections_created)
8904 {
8905 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 8906 if (info->executable)
65f38f15
AM
8907 {
8908 s = bfd_get_section_by_name (dynobj, ".interp");
8909 if (s == NULL)
8910 abort ();
eea6121a 8911 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
8912 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
8913 }
8914 }
8915
8916 /* Set up .got offsets for local syms, and space for local dynamic
8917 relocs. */
8918 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8919 {
411e1bfb
AM
8920 struct got_entry **lgot_ents;
8921 struct got_entry **end_lgot_ents;
e054468f
AM
8922 struct plt_entry **local_plt;
8923 struct plt_entry **end_local_plt;
f961d9dd 8924 unsigned char *lgot_masks;
65f38f15
AM
8925 bfd_size_type locsymcount;
8926 Elf_Internal_Shdr *symtab_hdr;
8927 asection *srel;
8928
0c8d6e5c 8929 if (!is_ppc64_elf (ibfd))
65f38f15
AM
8930 continue;
8931
8932 for (s = ibfd->sections; s != NULL; s = s->next)
8933 {
ec338859 8934 struct ppc_dyn_relocs *p;
65f38f15 8935
6edfbbad 8936 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 8937 {
ec338859
AM
8938 if (!bfd_is_abs_section (p->sec)
8939 && bfd_is_abs_section (p->sec->output_section))
8940 {
8941 /* Input section has been discarded, either because
8942 it is a copy of a linkonce section or due to
8943 linker script /DISCARD/, so we'll be discarding
8944 the relocs too. */
8945 }
248866a8 8946 else if (p->count != 0)
ec338859
AM
8947 {
8948 srel = elf_section_data (p->sec)->sreloc;
25f23106
AM
8949 if (!htab->elf.dynamic_sections_created)
8950 srel = htab->reliplt;
eea6121a 8951 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
8952 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
8953 info->flags |= DF_TEXTREL;
ec338859 8954 }
65f38f15
AM
8955 }
8956 }
8957
411e1bfb
AM
8958 lgot_ents = elf_local_got_ents (ibfd);
8959 if (!lgot_ents)
65f38f15
AM
8960 continue;
8961
0ffa91dd 8962 symtab_hdr = &elf_symtab_hdr (ibfd);
65f38f15 8963 locsymcount = symtab_hdr->sh_info;
411e1bfb 8964 end_lgot_ents = lgot_ents + locsymcount;
e054468f
AM
8965 local_plt = (struct plt_entry **) end_lgot_ents;
8966 end_local_plt = local_plt + locsymcount;
f961d9dd 8967 lgot_masks = (unsigned char *) end_local_plt;
e717da7e
AM
8968 s = ppc64_elf_tdata (ibfd)->got;
8969 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 8970 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 8971 {
0b8bcf0d 8972 struct got_entry **pent, *ent;
411e1bfb 8973
0b8bcf0d
AM
8974 pent = lgot_ents;
8975 while ((ent = *pent) != NULL)
411e1bfb
AM
8976 if (ent->got.refcount > 0)
8977 {
e7b938ca 8978 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 8979 {
927be08e 8980 ppc64_tlsld_got (ibfd)->got.refcount += 1;
0b8bcf0d 8981 *pent = ent->next;
411e1bfb
AM
8982 }
8983 else
8984 {
25f23106 8985 unsigned int num = 1;
eea6121a 8986 ent->got.offset = s->size;
e7b938ca 8987 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
25f23106
AM
8988 num = 2;
8989 s->size += num * 8;
8990 if (info->shared)
8991 srel->size += num * sizeof (Elf64_External_Rela);
8992 else if ((*lgot_masks & PLT_IFUNC) != 0)
927be08e
AM
8993 {
8994 htab->reliplt->size
8995 += num * sizeof (Elf64_External_Rela);
8996 htab->got_reli_size
8997 += num * sizeof (Elf64_External_Rela);
8998 }
0b8bcf0d 8999 pent = &ent->next;
411e1bfb
AM
9000 }
9001 }
9002 else
0b8bcf0d 9003 *pent = ent->next;
65f38f15 9004 }
e054468f
AM
9005
9006 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9007 for (; local_plt < end_local_plt; ++local_plt)
9008 {
9009 struct plt_entry *ent;
9010
9011 for (ent = *local_plt; ent != NULL; ent = ent->next)
9012 if (ent->plt.refcount > 0)
9013 {
91d6fa6a 9014 s = htab->iplt;
e054468f
AM
9015 ent->plt.offset = s->size;
9016 s->size += PLT_ENTRY_SIZE;
9017
25f23106 9018 htab->reliplt->size += sizeof (Elf64_External_Rela);
e054468f
AM
9019 }
9020 else
9021 ent->plt.offset = (bfd_vma) -1;
9022 }
65f38f15
AM
9023 }
9024
9025 /* Allocate global sym .plt and .got entries, and space for global
9026 sym dynamic relocs. */
4ce794b7 9027 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15 9028
7865406b 9029 first_tlsld = NULL;
102890f0
AM
9030 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9031 {
7865406b
AM
9032 struct got_entry *ent;
9033
0c8d6e5c 9034 if (!is_ppc64_elf (ibfd))
102890f0
AM
9035 continue;
9036
7865406b
AM
9037 ent = ppc64_tlsld_got (ibfd);
9038 if (ent->got.refcount > 0)
102890f0 9039 {
7865406b 9040 if (!htab->do_multi_toc && first_tlsld != NULL)
102890f0 9041 {
7865406b
AM
9042 ent->is_indirect = TRUE;
9043 ent->got.ent = first_tlsld;
9044 }
9045 else
9046 {
9047 if (first_tlsld == NULL)
9048 first_tlsld = ent;
9049 s = ppc64_elf_tdata (ibfd)->got;
9050 ent->got.offset = s->size;
9051 ent->owner = ibfd;
9052 s->size += 16;
9053 if (info->shared)
9054 {
9055 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9056 srel->size += sizeof (Elf64_External_Rela);
9057 }
102890f0
AM
9058 }
9059 }
9060 else
7865406b 9061 ent->got.offset = (bfd_vma) -1;
102890f0
AM
9062 }
9063
65f38f15
AM
9064 /* We now have determined the sizes of the various dynamic sections.
9065 Allocate memory for them. */
b34976b6 9066 relocs = FALSE;
65f38f15
AM
9067 for (s = dynobj->sections; s != NULL; s = s->next)
9068 {
9069 if ((s->flags & SEC_LINKER_CREATED) == 0)
9070 continue;
9071
4ce794b7 9072 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
9073 /* These haven't been allocated yet; don't strip. */
9074 continue;
e717da7e
AM
9075 else if (s == htab->got
9076 || s == htab->plt
e054468f 9077 || s == htab->iplt
c456f082
AM
9078 || s == htab->glink
9079 || s == htab->dynbss)
65f38f15
AM
9080 {
9081 /* Strip this section if we don't need it; see the
9082 comment below. */
5bd4f169 9083 }
70cc837d 9084 else if (CONST_STRNEQ (s->name, ".rela"))
5bd4f169 9085 {
c456f082 9086 if (s->size != 0)
5bd4f169 9087 {
4ce794b7 9088 if (s != htab->relplt)
b34976b6 9089 relocs = TRUE;
5bd4f169
AM
9090
9091 /* We use the reloc_count field as a counter if we need
9092 to copy relocs into the output file. */
9093 s->reloc_count = 0;
9094 }
9095 }
65f38f15 9096 else
5bd4f169
AM
9097 {
9098 /* It's not one of our sections, so don't allocate space. */
9099 continue;
9100 }
9101
eea6121a 9102 if (s->size == 0)
5bd4f169 9103 {
c456f082
AM
9104 /* If we don't need this section, strip it from the
9105 output file. This is mostly to handle .rela.bss and
9106 .rela.plt. We must create both sections in
9107 create_dynamic_sections, because they must be created
9108 before the linker maps input sections to output
9109 sections. The linker does that before
9110 adjust_dynamic_symbol is called, and it is that
9111 function which decides whether anything needs to go
9112 into these sections. */
8423293d 9113 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
9114 continue;
9115 }
9116
c456f082 9117 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
9118 continue;
9119
65f38f15
AM
9120 /* Allocate memory for the section contents. We use bfd_zalloc
9121 here in case unused entries are not reclaimed before the
9122 section's contents are written out. This should not happen,
411e1bfb
AM
9123 but this way if it does we get a R_PPC64_NONE reloc in .rela
9124 sections instead of garbage.
9125 We also rely on the section contents being zero when writing
9126 the GOT. */
eea6121a 9127 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 9128 if (s->contents == NULL)
b34976b6 9129 return FALSE;
5bd4f169
AM
9130 }
9131
e717da7e
AM
9132 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9133 {
0c8d6e5c 9134 if (!is_ppc64_elf (ibfd))
7b53ace3
AM
9135 continue;
9136
e717da7e
AM
9137 s = ppc64_elf_tdata (ibfd)->got;
9138 if (s != NULL && s != htab->got)
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 }
9148 }
9149 s = ppc64_elf_tdata (ibfd)->relgot;
9150 if (s != NULL)
9151 {
eea6121a 9152 if (s->size == 0)
8423293d 9153 s->flags |= SEC_EXCLUDE;
e717da7e
AM
9154 else
9155 {
eea6121a 9156 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
9157 if (s->contents == NULL)
9158 return FALSE;
9159 relocs = TRUE;
9160 s->reloc_count = 0;
9161 }
9162 }
9163 }
9164
e86ce104 9165 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
9166 {
9167 /* Add some entries to the .dynamic section. We fill in the
9168 values later, in ppc64_elf_finish_dynamic_sections, but we
9169 must add the entries now so that we get the correct size for
9170 the .dynamic section. The DT_DEBUG entry is filled in by the
9171 dynamic linker and used by the debugger. */
dc810e39 9172#define add_dynamic_entry(TAG, VAL) \
5a580b3a 9173 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 9174
36af4a4e 9175 if (info->executable)
5bd4f169 9176 {
dc810e39 9177 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 9178 return FALSE;
5bd4f169
AM
9179 }
9180
eea6121a 9181 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 9182 {
dc810e39
AM
9183 if (!add_dynamic_entry (DT_PLTGOT, 0)
9184 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9185 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
9186 || !add_dynamic_entry (DT_JMPREL, 0)
9187 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 9188 return FALSE;
5bd4f169
AM
9189 }
9190
19397422
AM
9191 if (NO_OPD_RELOCS)
9192 {
9193 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9194 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 9195 return FALSE;
19397422
AM
9196 }
9197
a7f2871e
AM
9198 if (!htab->no_tls_get_addr_opt
9199 && htab->tls_get_addr_fd != NULL
9200 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9201 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9202 return FALSE;
9203
5bd4f169
AM
9204 if (relocs)
9205 {
dc810e39
AM
9206 if (!add_dynamic_entry (DT_RELA, 0)
9207 || !add_dynamic_entry (DT_RELASZ, 0)
9208 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 9209 return FALSE;
5bd4f169 9210
65f38f15
AM
9211 /* If any dynamic relocs apply to a read-only section,
9212 then we need a DT_TEXTREL entry. */
248866a8 9213 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 9214 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 9215
65f38f15 9216 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 9217 {
65f38f15 9218 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 9219 return FALSE;
5bd4f169 9220 }
5bd4f169 9221 }
5bd4f169 9222 }
65f38f15 9223#undef add_dynamic_entry
5bd4f169 9224
b34976b6 9225 return TRUE;
5bd4f169
AM
9226}
9227
721956f4 9228/* Determine the type of stub needed, if any, for a call. */
5bd4f169 9229
4ce794b7
AM
9230static inline enum ppc_stub_type
9231ppc_type_of_stub (asection *input_sec,
9232 const Elf_Internal_Rela *rel,
9233 struct ppc_link_hash_entry **hash,
e054468f 9234 struct plt_entry **plt_ent,
4ce794b7 9235 bfd_vma destination)
5bd4f169 9236{
721956f4
AM
9237 struct ppc_link_hash_entry *h = *hash;
9238 bfd_vma location;
9239 bfd_vma branch_offset;
9240 bfd_vma max_branch_offset;
4ce794b7 9241 enum elf_ppc64_reloc_type r_type;
5bd4f169 9242
721956f4
AM
9243 if (h != NULL)
9244 {
e054468f 9245 struct plt_entry *ent;
7fe2b9a6 9246 struct ppc_link_hash_entry *fdh = h;
b31867b6
AM
9247 if (h->oh != NULL
9248 && h->oh->is_func_descriptor)
7b8f6675
AM
9249 {
9250 fdh = ppc_follow_link (h->oh);
9251 *hash = fdh;
9252 }
8387904d 9253
e054468f
AM
9254 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9255 if (ent->addend == rel->r_addend
9256 && ent->plt.offset != (bfd_vma) -1)
9257 {
e054468f
AM
9258 *plt_ent = ent;
9259 return ppc_stub_plt_call;
9260 }
5bd4f169 9261
7fe2b9a6
AM
9262 /* Here, we know we don't have a plt entry. If we don't have a
9263 either a defined function descriptor or a defined entry symbol
9264 in a regular object file, then it is pointless trying to make
9265 any other type of stub. */
854b41e7
AM
9266 if (!is_static_defined (&fdh->elf)
9267 && !is_static_defined (&h->elf))
721956f4 9268 return ppc_stub_none;
5d1634d7 9269 }
e054468f
AM
9270 else if (elf_local_got_ents (input_sec->owner) != NULL)
9271 {
9272 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9273 struct plt_entry **local_plt = (struct plt_entry **)
9274 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9275 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9276
9277 if (local_plt[r_symndx] != NULL)
9278 {
9279 struct plt_entry *ent;
9280
9281 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9282 if (ent->addend == rel->r_addend
9283 && ent->plt.offset != (bfd_vma) -1)
9284 {
9285 *plt_ent = ent;
9286 return ppc_stub_plt_call;
9287 }
9288 }
9289 }
5d1634d7 9290
721956f4
AM
9291 /* Determine where the call point is. */
9292 location = (input_sec->output_offset
9293 + input_sec->output_section->vma
9294 + rel->r_offset);
5d1634d7 9295
721956f4
AM
9296 branch_offset = destination - location;
9297 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 9298
721956f4
AM
9299 /* Determine if a long branch stub is needed. */
9300 max_branch_offset = 1 << 25;
4ce794b7 9301 if (r_type != R_PPC64_REL24)
721956f4 9302 max_branch_offset = 1 << 15;
5d1634d7 9303
721956f4
AM
9304 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
9305 /* We need a stub. Figure out whether a long_branch or plt_branch
9306 is needed later. */
9307 return ppc_stub_long_branch;
5d1634d7 9308
721956f4 9309 return ppc_stub_none;
5d1634d7
AM
9310}
9311
9312/* Build a .plt call stub. */
9313
4ce794b7 9314static inline bfd_byte *
176a0d42 9315build_plt_stub (bfd *obfd, bfd_byte *p, int offset, Elf_Internal_Rela *r)
5d1634d7
AM
9316{
9317#define PPC_LO(v) ((v) & 0xffff)
9318#define PPC_HI(v) (((v) >> 16) & 0xffff)
9319#define PPC_HA(v) PPC_HI ((v) + 0x8000)
9320
ac2df442
AM
9321 if (PPC_HA (offset) != 0)
9322 {
176a0d42
AM
9323 if (r != NULL)
9324 {
9325 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9326 r[1].r_offset = r[0].r_offset + 8;
9327 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9328 r[1].r_addend = r[0].r_addend;
9329 if (PPC_HA (offset + 16) != PPC_HA (offset))
9330 {
9331 r[2].r_offset = r[1].r_offset + 4;
9332 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
9333 r[2].r_addend = r[0].r_addend;
9334 }
9335 else
9336 {
9337 r[2].r_offset = r[1].r_offset + 8;
9338 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9339 r[2].r_addend = r[0].r_addend + 8;
9340 r[3].r_offset = r[2].r_offset + 4;
9341 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9342 r[3].r_addend = r[0].r_addend + 16;
9343 }
9344 }
ac2df442
AM
9345 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
9346 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9347 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
9348 if (PPC_HA (offset + 16) != PPC_HA (offset))
9349 {
9350 bfd_put_32 (obfd, ADDI_R12_R12 | PPC_LO (offset), p), p += 4;
9351 offset = 0;
9352 }
9353 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9354 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset + 8), p), p += 4;
9355 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset + 16), p), p += 4;
9356 bfd_put_32 (obfd, BCTR, p), p += 4;
9357 }
9358 else
9359 {
176a0d42
AM
9360 if (r != NULL)
9361 {
9362 r[0].r_offset += 4;
9363 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9364 if (PPC_HA (offset + 16) != PPC_HA (offset))
9365 {
9366 r[1].r_offset = r[0].r_offset + 4;
9367 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
9368 r[1].r_addend = r[0].r_addend;
9369 }
9370 else
9371 {
9372 r[1].r_offset = r[0].r_offset + 8;
9373 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9374 r[1].r_addend = r[0].r_addend + 16;
9375 r[2].r_offset = r[1].r_offset + 4;
9376 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9377 r[2].r_addend = r[0].r_addend + 8;
9378 }
9379 }
ac2df442
AM
9380 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
9381 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset), p), p += 4;
9382 if (PPC_HA (offset + 16) != PPC_HA (offset))
9383 {
9384 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
9385 offset = 0;
9386 }
9387 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
9388 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
9389 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
9390 bfd_put_32 (obfd, BCTR, p), p += 4;
9391 }
5d1634d7
AM
9392 return p;
9393}
9394
a7f2871e
AM
9395/* Build a special .plt call stub for __tls_get_addr. */
9396
9397#define LD_R11_0R3 0xe9630000
9398#define LD_R12_0R3 0xe9830000
9399#define MR_R0_R3 0x7c601b78
9400#define CMPDI_R11_0 0x2c2b0000
9401#define ADD_R3_R12_R13 0x7c6c6a14
9402#define BEQLR 0x4d820020
9403#define MR_R3_R0 0x7c030378
9404#define MFLR_R11 0x7d6802a6
9405#define STD_R11_0R1 0xf9610000
9406#define BCTRL 0x4e800421
9407#define LD_R11_0R1 0xe9610000
9408#define LD_R2_0R1 0xe8410000
9409#define MTLR_R11 0x7d6803a6
9410
9411static inline bfd_byte *
9412build_tls_get_addr_stub (bfd *obfd, bfd_byte *p, int offset,
9413 Elf_Internal_Rela *r)
9414{
9415 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
9416 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
9417 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
9418 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
9419 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
9420 bfd_put_32 (obfd, BEQLR, p), p += 4;
9421 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
9422 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
9423 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
9424
9425 if (r != NULL)
9426 r[0].r_offset += 9 * 4;
9427 p = build_plt_stub (obfd, p, offset, r);
9428 bfd_put_32 (obfd, BCTRL, p - 4);
9429
9430 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
9431 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
9432 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
9433 bfd_put_32 (obfd, BLR, p), p += 4;
9434
9435 return p;
9436}
9437
176a0d42
AM
9438static Elf_Internal_Rela *
9439get_relocs (asection *sec, int count)
9440{
9441 Elf_Internal_Rela *relocs;
9442 struct bfd_elf_section_data *elfsec_data;
9443
9444 elfsec_data = elf_section_data (sec);
9445 relocs = elfsec_data->relocs;
9446 if (relocs == NULL)
9447 {
9448 bfd_size_type relsize;
9449 relsize = sec->reloc_count * sizeof (*relocs);
9450 relocs = bfd_alloc (sec->owner, relsize);
9451 if (relocs == NULL)
9452 return NULL;
9453 elfsec_data->relocs = relocs;
d4730f92
BS
9454 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
9455 sizeof (Elf_Internal_Shdr));
9456 if (elfsec_data->rela.hdr == NULL)
9457 return NULL;
9458 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
9459 * sizeof (Elf64_External_Rela));
9460 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
176a0d42
AM
9461 sec->reloc_count = 0;
9462 }
9463 relocs += sec->reloc_count;
9464 sec->reloc_count += count;
9465 return relocs;
9466}
9467
aa374f67
AM
9468static bfd_vma
9469get_r2off (struct ppc_link_hash_table *htab,
9470 struct ppc_stub_hash_entry *stub_entry)
9471{
9472 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
9473
9474 if (r2off == 0)
9475 {
9476 /* Support linking -R objects. Get the toc pointer from the
9477 opd entry. */
9478 char buf[8];
9479 asection *opd = stub_entry->h->elf.root.u.def.section;
9480 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
9481
9482 if (strcmp (opd->name, ".opd") != 0
9483 || opd->reloc_count != 0)
9484 {
9485 (*_bfd_error_handler) (_("cannot find opd entry toc for %s"),
9486 stub_entry->h->elf.root.root.string);
9487 bfd_set_error (bfd_error_bad_value);
9488 return 0;
9489 }
9490 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
9491 return 0;
9492 r2off = bfd_get_64 (opd->owner, buf);
9493 r2off -= elf_gp (stub_entry->id_sec->output_section->owner);
9494 }
9495 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
9496 return r2off;
9497}
9498
b34976b6 9499static bfd_boolean
4ce794b7 9500ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 9501{
721956f4
AM
9502 struct ppc_stub_hash_entry *stub_entry;
9503 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
9504 struct bfd_link_info *info;
9505 struct ppc_link_hash_table *htab;
721956f4
AM
9506 bfd_byte *loc;
9507 bfd_byte *p;
ee75fd95 9508 bfd_vma dest, off;
721956f4 9509 int size;
176a0d42 9510 Elf_Internal_Rela *r;
e054468f 9511 asection *plt;
5d1634d7 9512
721956f4
AM
9513 /* Massage our args to the form they really have. */
9514 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 9515 info = in_arg;
5d1634d7 9516
5d1634d7 9517 htab = ppc_hash_table (info);
4dfe6ac6
NC
9518 if (htab == NULL)
9519 return FALSE;
5d1634d7 9520
721956f4 9521 /* Make a note of the offset within the stubs for this entry. */
eea6121a 9522 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 9523 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 9524
4ce794b7 9525 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 9526 switch (stub_entry->stub_type)
5d1634d7 9527 {
721956f4 9528 case ppc_stub_long_branch:
ad8e1ba5 9529 case ppc_stub_long_branch_r2off:
721956f4 9530 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
9531 off = dest = (stub_entry->target_value
9532 + stub_entry->target_section->output_offset
9533 + stub_entry->target_section->output_section->vma);
5d1634d7 9534
721956f4
AM
9535 /* And this is where we are coming from. */
9536 off -= (stub_entry->stub_offset
97b639ba
AM
9537 + stub_entry->stub_sec->output_offset
9538 + stub_entry->stub_sec->output_section->vma);
e86ce104 9539
ac2df442
AM
9540 size = 4;
9541 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
ad8e1ba5 9542 {
aa374f67 9543 bfd_vma r2off = get_r2off (htab, stub_entry);
ad8e1ba5 9544
aa374f67
AM
9545 if (r2off == 0)
9546 {
9547 htab->stub_error = TRUE;
9548 return FALSE;
9549 }
97b639ba 9550 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9551 loc += 4;
ac2df442
AM
9552 size = 12;
9553 if (PPC_HA (r2off) != 0)
9554 {
9555 size = 16;
9556 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9557 loc += 4;
9558 }
97b639ba 9559 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5 9560 loc += 4;
ac2df442 9561 off -= size - 4;
ad8e1ba5 9562 }
97b639ba 9563 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 9564
5c3dead3
AM
9565 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9566 {
9567 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
9568 stub_entry->root.string);
9569 htab->stub_error = TRUE;
9570 return FALSE;
9571 }
ee75fd95
AM
9572
9573 if (info->emitrelocations)
9574 {
176a0d42
AM
9575 r = get_relocs (stub_entry->stub_sec, 1);
9576 if (r == NULL)
9577 return FALSE;
ee75fd95
AM
9578 r->r_offset = loc - stub_entry->stub_sec->contents;
9579 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
9580 r->r_addend = dest;
9581 if (stub_entry->h != NULL)
9582 {
9583 struct elf_link_hash_entry **hashes;
9584 unsigned long symndx;
9585 struct ppc_link_hash_entry *h;
9586
9587 hashes = elf_sym_hashes (htab->stub_bfd);
9588 if (hashes == NULL)
9589 {
9590 bfd_size_type hsize;
9591
9592 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
9593 hashes = bfd_zalloc (htab->stub_bfd, hsize);
9594 if (hashes == NULL)
9595 return FALSE;
9596 elf_sym_hashes (htab->stub_bfd) = hashes;
9597 htab->stub_globals = 1;
9598 }
9599 symndx = htab->stub_globals++;
9600 h = stub_entry->h;
9601 hashes[symndx] = &h->elf;
9602 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
9603 if (h->oh != NULL && h->oh->is_func)
b31867b6 9604 h = ppc_follow_link (h->oh);
ee75fd95
AM
9605 if (h->elf.root.u.def.section != stub_entry->target_section)
9606 /* H is an opd symbol. The addend must be zero. */
9607 r->r_addend = 0;
9608 else
9609 {
9610 off = (h->elf.root.u.def.value
9611 + h->elf.root.u.def.section->output_offset
9612 + h->elf.root.u.def.section->output_section->vma);
9613 r->r_addend -= off;
9614 }
9615 }
9616 }
721956f4 9617 break;
e86ce104 9618
721956f4 9619 case ppc_stub_plt_branch:
ad8e1ba5 9620 case ppc_stub_plt_branch_r2off:
721956f4
AM
9621 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9622 stub_entry->root.string + 9,
b34976b6 9623 FALSE, FALSE);
721956f4
AM
9624 if (br_entry == NULL)
9625 {
9626 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
5c3dead3 9627 stub_entry->root.string);
b34976b6
AM
9628 htab->stub_error = TRUE;
9629 return FALSE;
721956f4
AM
9630 }
9631
176a0d42
AM
9632 dest = (stub_entry->target_value
9633 + stub_entry->target_section->output_offset
9634 + stub_entry->target_section->output_section->vma);
721956f4 9635
176a0d42 9636 bfd_put_64 (htab->brlt->owner, dest,
4ce794b7 9637 htab->brlt->contents + br_entry->offset);
721956f4 9638
f94498ff 9639 if (br_entry->iter == htab->stub_iteration)
721956f4 9640 {
f94498ff 9641 br_entry->iter = 0;
84f5d08e 9642
f94498ff 9643 if (htab->relbrlt != NULL)
84f5d08e 9644 {
f94498ff
AM
9645 /* Create a reloc for the branch lookup table entry. */
9646 Elf_Internal_Rela rela;
9647 bfd_byte *rl;
9648
9649 rela.r_offset = (br_entry->offset
9650 + htab->brlt->output_offset
9651 + htab->brlt->output_section->vma);
9652 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9653 rela.r_addend = dest;
f94498ff
AM
9654
9655 rl = htab->relbrlt->contents;
9656 rl += (htab->relbrlt->reloc_count++
9657 * sizeof (Elf64_External_Rela));
9658 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
9659 }
9660 else if (info->emitrelocations)
9661 {
176a0d42
AM
9662 r = get_relocs (htab->brlt, 1);
9663 if (r == NULL)
9664 return FALSE;
9665 /* brlt, being SEC_LINKER_CREATED does not go through the
9666 normal reloc processing. Symbols and offsets are not
9667 translated from input file to output file form, so
9668 set up the offset per the output file. */
f94498ff
AM
9669 r->r_offset = (br_entry->offset
9670 + htab->brlt->output_offset
9671 + htab->brlt->output_section->vma);
9672 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
176a0d42 9673 r->r_addend = dest;
84f5d08e 9674 }
84f5d08e 9675 }
721956f4 9676
176a0d42
AM
9677 dest = (br_entry->offset
9678 + htab->brlt->output_offset
9679 + htab->brlt->output_section->vma);
9680
9681 off = (dest
4ce794b7 9682 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 9683 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9684
ad8e1ba5 9685 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7
AM
9686 {
9687 (*_bfd_error_handler)
e86ce104 9688 (_("linkage table error against `%s'"),
721956f4 9689 stub_entry->root.string);
5d1634d7 9690 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9691 htab->stub_error = TRUE;
9692 return FALSE;
5d1634d7 9693 }
41bd81ab 9694
176a0d42
AM
9695 if (info->emitrelocations)
9696 {
9697 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
9698 if (r == NULL)
9699 return FALSE;
9700 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9701 if (bfd_big_endian (info->output_bfd))
9702 r[0].r_offset += 2;
176a0d42
AM
9703 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
9704 r[0].r_offset += 4;
9705 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
9706 r[0].r_addend = dest;
9707 if (PPC_HA (off) != 0)
9708 {
9709 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
9710 r[1].r_offset = r[0].r_offset + 4;
9711 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
9712 r[1].r_addend = r[0].r_addend;
9713 }
9714 }
9715
ad8e1ba5
AM
9716 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
9717 {
176a0d42 9718 if (PPC_HA (off) != 0)
ac2df442
AM
9719 {
9720 size = 16;
176a0d42 9721 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9722 loc += 4;
176a0d42 9723 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9724 }
9725 else
9726 {
9727 size = 12;
176a0d42 9728 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442 9729 }
ad8e1ba5
AM
9730 }
9731 else
9732 {
aa374f67
AM
9733 bfd_vma r2off = get_r2off (htab, stub_entry);
9734
9735 if (r2off == 0)
9736 {
9737 htab->stub_error = TRUE;
9738 return FALSE;
9739 }
ad8e1ba5 9740
97b639ba 9741 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 9742 loc += 4;
ac2df442 9743 size = 20;
176a0d42 9744 if (PPC_HA (off) != 0)
ac2df442
AM
9745 {
9746 size += 4;
176a0d42 9747 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (off), loc);
ac2df442 9748 loc += 4;
176a0d42 9749 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (off), loc);
ac2df442
AM
9750 loc += 4;
9751 }
9752 else
9753 {
176a0d42 9754 bfd_put_32 (htab->stub_bfd, LD_R11_0R2 | PPC_LO (off), loc);
ac2df442
AM
9755 loc += 4;
9756 }
9757
9758 if (PPC_HA (r2off) != 0)
9759 {
9760 size += 4;
9761 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
9762 loc += 4;
9763 }
97b639ba 9764 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
9765 }
9766 loc += 4;
97b639ba 9767 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 9768 loc += 4;
97b639ba 9769 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 9770 break;
5d1634d7 9771
721956f4 9772 case ppc_stub_plt_call:
e054468f 9773 if (stub_entry->h != NULL
b31867b6
AM
9774 && stub_entry->h->is_func_descriptor
9775 && stub_entry->h->oh != NULL)
c862ae31 9776 {
b31867b6
AM
9777 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
9778
9779 /* If the old-ABI "dot-symbol" is undefined make it weak so
9780 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
9781 FIXME: We used to define the symbol on one of the call
9782 stubs instead, which is why we test symbol section id
9783 against htab->top_id in various places. Likely all
9784 these checks could now disappear. */
9785 if (fh->elf.root.type == bfd_link_hash_undefined)
9786 fh->elf.root.type = bfd_link_hash_undefweak;
c862ae31
AM
9787 }
9788
721956f4 9789 /* Now build the stub. */
e054468f 9790 dest = stub_entry->plt_ent->plt.offset & ~1;
176a0d42 9791 if (dest >= (bfd_vma) -2)
721956f4
AM
9792 abort ();
9793
e054468f 9794 plt = htab->plt;
25f23106
AM
9795 if (!htab->elf.dynamic_sections_created
9796 || stub_entry->h == NULL
9797 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9798 plt = htab->iplt;
9799
9800 dest += plt->output_offset + plt->output_section->vma;
9801
9802 if (stub_entry->h == NULL
9803 && (stub_entry->plt_ent->plt.offset & 1) == 0)
9804 {
9805 Elf_Internal_Rela rela;
9806 bfd_byte *rl;
9807
9808 rela.r_offset = dest;
25f23106 9809 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
9810 rela.r_addend = (stub_entry->target_value
9811 + stub_entry->target_section->output_offset
9812 + stub_entry->target_section->output_section->vma);
9813
25f23106
AM
9814 rl = (htab->reliplt->contents
9815 + (htab->reliplt->reloc_count++
9816 * sizeof (Elf64_External_Rela)));
9817 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
e054468f
AM
9818 stub_entry->plt_ent->plt.offset |= 1;
9819 }
176a0d42
AM
9820
9821 off = (dest
e054468f 9822 - elf_gp (plt->output_section->owner)
176a0d42 9823 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9824
ad8e1ba5 9825 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4
AM
9826 {
9827 (*_bfd_error_handler)
9828 (_("linkage table error against `%s'"),
e054468f
AM
9829 stub_entry->h != NULL
9830 ? stub_entry->h->elf.root.root.string
9831 : "<local sym>");
721956f4 9832 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
9833 htab->stub_error = TRUE;
9834 return FALSE;
721956f4
AM
9835 }
9836
176a0d42
AM
9837 r = NULL;
9838 if (info->emitrelocations)
9839 {
9840 r = get_relocs (stub_entry->stub_sec,
9841 (2 + (PPC_HA (off) != 0)
9842 + (PPC_HA (off + 16) == PPC_HA (off))));
9843 if (r == NULL)
9844 return FALSE;
9845 r[0].r_offset = loc - stub_entry->stub_sec->contents;
7cfbafbc
AM
9846 if (bfd_big_endian (info->output_bfd))
9847 r[0].r_offset += 2;
176a0d42
AM
9848 r[0].r_addend = dest;
9849 }
a7f2871e
AM
9850 if (stub_entry->h != NULL
9851 && (stub_entry->h == htab->tls_get_addr_fd
9852 || stub_entry->h == htab->tls_get_addr)
9853 && !htab->no_tls_get_addr_opt)
9854 p = build_tls_get_addr_stub (htab->stub_bfd, loc, off, r);
9855 else
9856 p = build_plt_stub (htab->stub_bfd, loc, off, r);
721956f4
AM
9857 size = p - loc;
9858 break;
9859
9860 default:
9861 BFD_FAIL ();
b34976b6 9862 return FALSE;
721956f4
AM
9863 }
9864
eea6121a 9865 stub_entry->stub_sec->size += size;
97b639ba 9866
ee75fd95 9867 if (htab->emit_stub_syms)
97b639ba
AM
9868 {
9869 struct elf_link_hash_entry *h;
ee75fd95
AM
9870 size_t len1, len2;
9871 char *name;
9872 const char *const stub_str[] = { "long_branch",
9873 "long_branch_r2off",
9874 "plt_branch",
9875 "plt_branch_r2off",
9876 "plt_call" };
9877
9878 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
9879 len2 = strlen (stub_entry->root.string);
9880 name = bfd_malloc (len1 + len2 + 2);
9881 if (name == NULL)
9882 return FALSE;
9883 memcpy (name, stub_entry->root.string, 9);
9884 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
9885 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
9886 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
9887 if (h == NULL)
9888 return FALSE;
9889 if (h->root.type == bfd_link_hash_new)
9890 {
9891 h->root.type = bfd_link_hash_defined;
9892 h->root.u.def.section = stub_entry->stub_sec;
9893 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
9894 h->ref_regular = 1;
9895 h->def_regular = 1;
9896 h->ref_regular_nonweak = 1;
9897 h->forced_local = 1;
9898 h->non_elf = 0;
97b639ba
AM
9899 }
9900 }
9901
b34976b6 9902 return TRUE;
721956f4
AM
9903}
9904
9905/* As above, but don't actually build the stub. Just bump offset so
9906 we know stub section sizes, and select plt_branch stubs where
9907 long_branch stubs won't do. */
9908
b34976b6 9909static bfd_boolean
4ce794b7 9910ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
9911{
9912 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 9913 struct bfd_link_info *info;
721956f4
AM
9914 struct ppc_link_hash_table *htab;
9915 bfd_vma off;
9916 int size;
9917
9918 /* Massage our args to the form they really have. */
9919 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
9920 info = in_arg;
9921
9922 htab = ppc_hash_table (info);
4dfe6ac6
NC
9923 if (htab == NULL)
9924 return FALSE;
721956f4
AM
9925
9926 if (stub_entry->stub_type == ppc_stub_plt_call)
9927 {
e054468f
AM
9928 asection *plt;
9929 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
58ac9f71 9930 if (off >= (bfd_vma) -2)
411e1bfb 9931 abort ();
e054468f 9932 plt = htab->plt;
25f23106
AM
9933 if (!htab->elf.dynamic_sections_created
9934 || stub_entry->h == NULL
9935 || stub_entry->h->elf.dynindx == -1)
e054468f
AM
9936 plt = htab->iplt;
9937 off += (plt->output_offset
9938 + plt->output_section->vma
9939 - elf_gp (plt->output_section->owner)
ad8e1ba5 9940 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 9941
ad8e1ba5 9942 size = PLT_CALL_STUB_SIZE;
ac2df442
AM
9943 if (PPC_HA (off) == 0)
9944 size -= 4;
4ce794b7 9945 if (PPC_HA (off + 16) != PPC_HA (off))
721956f4 9946 size += 4;
a7f2871e
AM
9947 if (stub_entry->h != NULL
9948 && (stub_entry->h == htab->tls_get_addr_fd
9949 || stub_entry->h == htab->tls_get_addr)
9950 && !htab->no_tls_get_addr_opt)
9951 size += 13 * 4;
176a0d42
AM
9952 if (info->emitrelocations)
9953 {
9954 stub_entry->stub_sec->reloc_count
9955 += 2 + (PPC_HA (off) != 0) + (PPC_HA (off + 16) == PPC_HA (off));
9956 stub_entry->stub_sec->flags |= SEC_RELOC;
9957 }
721956f4
AM
9958 }
9959 else
9960 {
ad8e1ba5
AM
9961 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
9962 variants. */
ac2df442
AM
9963 bfd_vma r2off = 0;
9964
721956f4
AM
9965 off = (stub_entry->target_value
9966 + stub_entry->target_section->output_offset
9967 + stub_entry->target_section->output_section->vma);
eea6121a 9968 off -= (stub_entry->stub_sec->size
721956f4
AM
9969 + stub_entry->stub_sec->output_offset
9970 + stub_entry->stub_sec->output_section->vma);
9971
ad8e1ba5
AM
9972 /* Reset the stub type from the plt variant in case we now
9973 can reach with a shorter stub. */
9974 if (stub_entry->stub_type >= ppc_stub_plt_branch)
9975 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
9976
9977 size = 4;
9978 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
9979 {
aa374f67
AM
9980 r2off = get_r2off (htab, stub_entry);
9981 if (r2off == 0)
9982 {
9983 htab->stub_error = TRUE;
9984 return FALSE;
9985 }
ac2df442
AM
9986 size = 12;
9987 if (PPC_HA (r2off) != 0)
9988 size = 16;
9989 off -= size - 4;
ad8e1ba5
AM
9990 }
9991
9992 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
9993 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
9994 {
9995 struct ppc_branch_hash_entry *br_entry;
9996
9997 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
9998 stub_entry->root.string + 9,
b34976b6 9999 TRUE, FALSE);
721956f4
AM
10000 if (br_entry == NULL)
10001 {
10002 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
5c3dead3 10003 stub_entry->root.string);
b34976b6
AM
10004 htab->stub_error = TRUE;
10005 return FALSE;
721956f4
AM
10006 }
10007
10008 if (br_entry->iter != htab->stub_iteration)
10009 {
10010 br_entry->iter = htab->stub_iteration;
eea6121a
AM
10011 br_entry->offset = htab->brlt->size;
10012 htab->brlt->size += 8;
63bc6f6c 10013
ee75fd95 10014 if (htab->relbrlt != NULL)
eea6121a 10015 htab->relbrlt->size += sizeof (Elf64_External_Rela);
84f5d08e
AM
10016 else if (info->emitrelocations)
10017 {
10018 htab->brlt->reloc_count += 1;
10019 htab->brlt->flags |= SEC_RELOC;
10020 }
721956f4 10021 }
ad8e1ba5
AM
10022
10023 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
ac2df442
AM
10024 off = (br_entry->offset
10025 + htab->brlt->output_offset
10026 + htab->brlt->output_section->vma
10027 - elf_gp (htab->brlt->output_section->owner)
10028 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10029
176a0d42
AM
10030 if (info->emitrelocations)
10031 {
10032 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10033 stub_entry->stub_sec->flags |= SEC_RELOC;
10034 }
10035
ac2df442
AM
10036 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10037 {
10038 size = 12;
176a0d42 10039 if (PPC_HA (off) != 0)
ac2df442
AM
10040 size = 16;
10041 }
10042 else
10043 {
10044 size = 20;
176a0d42 10045 if (PPC_HA (off) != 0)
ac2df442
AM
10046 size += 4;
10047
10048 if (PPC_HA (r2off) != 0)
10049 size += 4;
10050 }
721956f4 10051 }
84f5d08e
AM
10052 else if (info->emitrelocations)
10053 {
10054 stub_entry->stub_sec->reloc_count += 1;
10055 stub_entry->stub_sec->flags |= SEC_RELOC;
10056 }
721956f4
AM
10057 }
10058
eea6121a 10059 stub_entry->stub_sec->size += size;
b34976b6 10060 return TRUE;
721956f4
AM
10061}
10062
10063/* Set up various things so that we can make a list of input sections
10064 for each output section included in the link. Returns -1 on error,
cedb70c5 10065 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
10066
10067int
927be08e
AM
10068ppc64_elf_setup_section_lists
10069 (struct bfd_link_info *info,
10070 asection *(*add_stub_section) (const char *, asection *),
10071 void (*layout_sections_again) (void))
721956f4
AM
10072{
10073 bfd *input_bfd;
734b6cf9 10074 int top_id, top_index, id;
721956f4 10075 asection *section;
734b6cf9 10076 asection **input_list;
721956f4
AM
10077 bfd_size_type amt;
10078 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10079
4dfe6ac6
NC
10080 if (htab == NULL)
10081 return -1;
927be08e
AM
10082 /* Stash our params away. */
10083 htab->add_stub_section = add_stub_section;
10084 htab->layout_sections_again = layout_sections_again;
4c52953f 10085
4ce794b7 10086 if (htab->brlt == NULL)
721956f4
AM
10087 return 0;
10088
1e2f5b6e 10089 /* Find the top input section id. */
3d6f9012 10090 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
10091 input_bfd != NULL;
10092 input_bfd = input_bfd->link_next)
10093 {
721956f4
AM
10094 for (section = input_bfd->sections;
10095 section != NULL;
10096 section = section->next)
10097 {
10098 if (top_id < section->id)
10099 top_id = section->id;
10100 }
10101 }
721956f4 10102
8f3bab57 10103 htab->top_id = top_id;
721956f4 10104 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 10105 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
10106 if (htab->stub_group == NULL)
10107 return -1;
10108
3d6f9012
AM
10109 /* Set toc_off for com, und, abs and ind sections. */
10110 for (id = 0; id < 3; id++)
10111 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 10112
734b6cf9
AM
10113 /* We can't use output_bfd->section_count here to find the top output
10114 section index as some sections may have been removed, and
8423293d 10115 strip_excluded_output_sections doesn't renumber the indices. */
927be08e 10116 for (section = info->output_bfd->sections, top_index = 0;
734b6cf9
AM
10117 section != NULL;
10118 section = section->next)
10119 {
10120 if (top_index < section->index)
10121 top_index = section->index;
10122 }
10123
10124 htab->top_index = top_index;
10125 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 10126 input_list = bfd_zmalloc (amt);
734b6cf9
AM
10127 htab->input_list = input_list;
10128 if (input_list == NULL)
10129 return -1;
10130
721956f4
AM
10131 return 1;
10132}
10133
927be08e
AM
10134/* Set up for first pass at multitoc partitioning. */
10135
10136void
10137ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10138{
10139 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10140
10141 elf_gp (info->output_bfd) = ppc64_elf_toc (info->output_bfd);
10142 htab->toc_curr = elf_gp (info->output_bfd);
10143 htab->toc_bfd = NULL;
10144 htab->toc_first_sec = NULL;
10145}
10146
e717da7e
AM
10147/* The linker repeatedly calls this function for each TOC input section
10148 and linker generated GOT section. Group input bfds such that the toc
927be08e 10149 within a group is less than 64k in size. */
ad8e1ba5 10150
927be08e 10151bfd_boolean
4ce794b7 10152ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
10153{
10154 struct ppc_link_hash_table *htab = ppc_hash_table (info);
d77c8a4b 10155 bfd_vma addr, off, limit;
ad8e1ba5 10156
4dfe6ac6
NC
10157 if (htab == NULL)
10158 return FALSE;
10159
927be08e 10160 if (!htab->second_toc_pass)
4c52953f 10161 {
927be08e 10162 /* Keep track of the first .toc or .got section for this input bfd. */
bf102f86
AM
10163 if (htab->toc_bfd != isec->owner)
10164 {
10165 htab->toc_bfd = isec->owner;
10166 htab->toc_first_sec = isec;
10167 }
927be08e 10168
bf102f86
AM
10169 addr = isec->output_offset + isec->output_section->vma;
10170 off = addr - htab->toc_curr;
d77c8a4b
AM
10171 limit = 0x80008000;
10172 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10173 limit = 0x10000;
10174 if (off + isec->size > limit)
bf102f86
AM
10175 {
10176 addr = (htab->toc_first_sec->output_offset
10177 + htab->toc_first_sec->output_section->vma);
10178 htab->toc_curr = addr;
10179 }
99877b66 10180
927be08e
AM
10181 /* toc_curr is the base address of this toc group. Set elf_gp
10182 for the input section to be the offset relative to the
10183 output toc base plus 0x8000. Making the input elf_gp an
10184 offset allows us to move the toc as a whole without
10185 recalculating input elf_gp. */
10186 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10187 off += TOC_BASE_OFF;
10188
10189 /* Die if someone uses a linker script that doesn't keep input
10190 file .toc and .got together. */
10191 if (elf_gp (isec->owner) != 0
10192 && elf_gp (isec->owner) != off)
10193 return FALSE;
10194
10195 elf_gp (isec->owner) = off;
10196 return TRUE;
4c52953f 10197 }
927be08e
AM
10198
10199 /* During the second pass toc_first_sec points to the start of
10200 a toc group, and toc_curr is used to track the old elf_gp.
10201 We use toc_bfd to ensure we only look at each bfd once. */
10202 if (htab->toc_bfd == isec->owner)
10203 return TRUE;
10204 htab->toc_bfd = isec->owner;
10205
10206 if (htab->toc_first_sec == NULL
10207 || htab->toc_curr != elf_gp (isec->owner))
10208 {
10209 htab->toc_curr = elf_gp (isec->owner);
10210 htab->toc_first_sec = isec;
10211 }
10212 addr = (htab->toc_first_sec->output_offset
10213 + htab->toc_first_sec->output_section->vma);
10214 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10215 elf_gp (isec->owner) = off;
10216
10217 return TRUE;
ad8e1ba5
AM
10218}
10219
927be08e
AM
10220/* Called via elf_link_hash_traverse to merge GOT entries for global
10221 symbol H. */
10222
10223static bfd_boolean
10224merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10225{
10226 if (h->root.type == bfd_link_hash_indirect)
10227 return TRUE;
10228
10229 if (h->root.type == bfd_link_hash_warning)
10230 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10231
10232 merge_got_entries (&h->got.glist);
10233
10234 return TRUE;
10235}
10236
10237/* Called via elf_link_hash_traverse to allocate GOT entries for global
10238 symbol H. */
10239
10240static bfd_boolean
10241reallocate_got (struct elf_link_hash_entry *h, void *inf)
10242{
10243 struct got_entry *gent;
10244
10245 if (h->root.type == bfd_link_hash_indirect)
10246 return TRUE;
10247
10248 if (h->root.type == bfd_link_hash_warning)
10249 h = (struct elf_link_hash_entry *) h->root.u.i.link;
10250
10251 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10252 if (!gent->is_indirect)
10253 allocate_got (h, (struct bfd_link_info *) inf, gent);
10254 return TRUE;
10255}
10256
10257/* Called on the first multitoc pass after the last call to
10258 ppc64_elf_next_toc_section. This function removes duplicate GOT
10259 entries. */
10260
10261bfd_boolean
10262ppc64_elf_layout_multitoc (struct bfd_link_info *info)
ad8e1ba5
AM
10263{
10264 struct ppc_link_hash_table *htab = ppc_hash_table (info);
927be08e
AM
10265 struct bfd *ibfd, *ibfd2;
10266 bfd_boolean done_something;
10267
10268 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
ad8e1ba5 10269
7865406b
AM
10270 if (!htab->do_multi_toc)
10271 return FALSE;
10272
d0fae19d 10273 /* Merge global sym got entries within a toc group. */
927be08e
AM
10274 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
10275
10276 /* And tlsld_got. */
10277 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10278 {
10279 struct got_entry *ent, *ent2;
10280
10281 if (!is_ppc64_elf (ibfd))
10282 continue;
10283
10284 ent = ppc64_tlsld_got (ibfd);
10285 if (!ent->is_indirect
10286 && ent->got.offset != (bfd_vma) -1)
10287 {
10288 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
10289 {
10290 if (!is_ppc64_elf (ibfd2))
10291 continue;
10292
10293 ent2 = ppc64_tlsld_got (ibfd2);
10294 if (!ent2->is_indirect
10295 && ent2->got.offset != (bfd_vma) -1
10296 && elf_gp (ibfd2) == elf_gp (ibfd))
10297 {
10298 ent2->is_indirect = TRUE;
10299 ent2->got.ent = ent;
10300 }
10301 }
10302 }
10303 }
10304
10305 /* Zap sizes of got sections. */
10306 htab->reliplt->rawsize = htab->reliplt->size;
10307 htab->reliplt->size -= htab->got_reli_size;
10308 htab->got_reli_size = 0;
10309
10310 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10311 {
10312 asection *got, *relgot;
10313
10314 if (!is_ppc64_elf (ibfd))
10315 continue;
10316
10317 got = ppc64_elf_tdata (ibfd)->got;
10318 if (got != NULL)
10319 {
10320 got->rawsize = got->size;
10321 got->size = 0;
10322 relgot = ppc64_elf_tdata (ibfd)->relgot;
10323 relgot->rawsize = relgot->size;
10324 relgot->size = 0;
10325 }
10326 }
10327
10328 /* Now reallocate the got, local syms first. We don't need to
10329 allocate section contents again since we never increase size. */
10330 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10331 {
10332 struct got_entry **lgot_ents;
10333 struct got_entry **end_lgot_ents;
10334 struct plt_entry **local_plt;
10335 struct plt_entry **end_local_plt;
f961d9dd 10336 unsigned char *lgot_masks;
927be08e
AM
10337 bfd_size_type locsymcount;
10338 Elf_Internal_Shdr *symtab_hdr;
10339 asection *s, *srel;
10340
10341 if (!is_ppc64_elf (ibfd))
10342 continue;
10343
10344 lgot_ents = elf_local_got_ents (ibfd);
10345 if (!lgot_ents)
10346 continue;
10347
10348 symtab_hdr = &elf_symtab_hdr (ibfd);
10349 locsymcount = symtab_hdr->sh_info;
10350 end_lgot_ents = lgot_ents + locsymcount;
10351 local_plt = (struct plt_entry **) end_lgot_ents;
10352 end_local_plt = local_plt + locsymcount;
f961d9dd 10353 lgot_masks = (unsigned char *) end_local_plt;
927be08e
AM
10354 s = ppc64_elf_tdata (ibfd)->got;
10355 srel = ppc64_elf_tdata (ibfd)->relgot;
10356 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10357 {
10358 struct got_entry *ent;
10359
10360 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
d0fae19d
AM
10361 {
10362 unsigned int num = 1;
10363 ent->got.offset = s->size;
10364 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10365 num = 2;
10366 s->size += num * 8;
10367 if (info->shared)
10368 srel->size += num * sizeof (Elf64_External_Rela);
10369 else if ((*lgot_masks & PLT_IFUNC) != 0)
10370 {
10371 htab->reliplt->size
10372 += num * sizeof (Elf64_External_Rela);
10373 htab->got_reli_size
10374 += num * sizeof (Elf64_External_Rela);
10375 }
10376 }
927be08e
AM
10377 }
10378 }
10379
10380 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
10381
10382 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10383 {
10384 struct got_entry *ent;
10385
10386 if (!is_ppc64_elf (ibfd))
10387 continue;
10388
10389 ent = ppc64_tlsld_got (ibfd);
10390 if (!ent->is_indirect
10391 && ent->got.offset != (bfd_vma) -1)
10392 {
10393 asection *s = ppc64_elf_tdata (ibfd)->got;
10394 ent->got.offset = s->size;
10395 s->size += 16;
10396 if (info->shared)
10397 {
10398 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10399 srel->size += sizeof (Elf64_External_Rela);
10400 }
10401 }
10402 }
10403
10404 done_something = htab->reliplt->rawsize != htab->reliplt->size;
10405 if (!done_something)
10406 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
10407 {
10408 asection *got;
10409
10410 if (!is_ppc64_elf (ibfd))
10411 continue;
10412
10413 got = ppc64_elf_tdata (ibfd)->got;
10414 if (got != NULL)
10415 {
10416 done_something = got->rawsize != got->size;
10417 if (done_something)
10418 break;
10419 }
10420 }
10421
10422 if (done_something)
10423 (*htab->layout_sections_again) ();
10424
10425 /* Set up for second pass over toc sections to recalculate elf_gp
10426 on input sections. */
10427 htab->toc_bfd = NULL;
10428 htab->toc_first_sec = NULL;
10429 htab->second_toc_pass = TRUE;
10430 return done_something;
10431}
10432
10433/* Called after second pass of multitoc partitioning. */
10434
10435void
10436ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
10437{
10438 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10439
10440 /* After the second pass, toc_curr tracks the TOC offset used
10441 for code sections below in ppc64_elf_next_input_section. */
3d6f9012 10442 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
10443}
10444
9b5ecbd0
AM
10445/* No toc references were found in ISEC. If the code in ISEC makes no
10446 calls, then there's no need to use toc adjusting stubs when branching
10447 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
10448 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
10449 needed, and 2 if a cyclical call-graph was found but no other reason
10450 for a stub was detected. If called from the top level, a return of
10451 2 means the same as a return of 0. */
9b5ecbd0
AM
10452
10453static int
4ce794b7 10454toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 10455{
9b5ecbd0 10456 int ret;
70cc837d
AM
10457
10458 /* Mark this section as checked. */
10459 isec->call_check_done = 1;
9b5ecbd0 10460
772119ce
AM
10461 /* We know none of our code bearing sections will need toc stubs. */
10462 if ((isec->flags & SEC_LINKER_CREATED) != 0)
10463 return 0;
10464
eea6121a 10465 if (isec->size == 0)
082c50f8
AM
10466 return 0;
10467
4c52953f
AM
10468 if (isec->output_section == NULL)
10469 return 0;
10470
4c52953f 10471 ret = 0;
70cc837d 10472 if (isec->reloc_count != 0)
9b5ecbd0 10473 {
70cc837d
AM
10474 Elf_Internal_Rela *relstart, *rel;
10475 Elf_Internal_Sym *local_syms;
10476 struct ppc_link_hash_table *htab;
2917689a 10477
70cc837d
AM
10478 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
10479 info->keep_memory);
10480 if (relstart == NULL)
10481 return -1;
90aecf7a 10482
70cc837d
AM
10483 /* Look for branches to outside of this section. */
10484 local_syms = NULL;
10485 htab = ppc_hash_table (info);
10486 if (htab == NULL)
10487 return -1;
4c52953f 10488
70cc837d 10489 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
4c52953f 10490 {
70cc837d
AM
10491 enum elf_ppc64_reloc_type r_type;
10492 unsigned long r_symndx;
10493 struct elf_link_hash_entry *h;
10494 struct ppc_link_hash_entry *eh;
10495 Elf_Internal_Sym *sym;
10496 asection *sym_sec;
10497 struct _opd_sec_data *opd;
10498 bfd_vma sym_value;
10499 bfd_vma dest;
10500
10501 r_type = ELF64_R_TYPE (rel->r_info);
10502 if (r_type != R_PPC64_REL24
10503 && r_type != R_PPC64_REL14
10504 && r_type != R_PPC64_REL14_BRTAKEN
10505 && r_type != R_PPC64_REL14_BRNTAKEN)
10506 continue;
4c52953f 10507
70cc837d
AM
10508 r_symndx = ELF64_R_SYM (rel->r_info);
10509 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
10510 isec->owner))
4c52953f 10511 {
70cc837d
AM
10512 ret = -1;
10513 break;
10514 }
4c52953f 10515
70cc837d
AM
10516 /* Calls to dynamic lib functions go through a plt call stub
10517 that uses r2. */
10518 eh = (struct ppc_link_hash_entry *) h;
10519 if (eh != NULL
10520 && (eh->elf.plt.plist != NULL
10521 || (eh->oh != NULL
10522 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
10523 {
10524 ret = 1;
10525 break;
4c52953f
AM
10526 }
10527
70cc837d
AM
10528 if (sym_sec == NULL)
10529 /* Ignore other undefined symbols. */
4c52953f 10530 continue;
4c52953f 10531
70cc837d
AM
10532 /* Assume branches to other sections not included in the
10533 link need stubs too, to cover -R and absolute syms. */
10534 if (sym_sec->output_section == NULL)
10535 {
10536 ret = 1;
10537 break;
10538 }
4c52953f 10539
70cc837d
AM
10540 if (h == NULL)
10541 sym_value = sym->st_value;
10542 else
10543 {
10544 if (h->root.type != bfd_link_hash_defined
10545 && h->root.type != bfd_link_hash_defweak)
10546 abort ();
10547 sym_value = h->root.u.def.value;
10548 }
10549 sym_value += rel->r_addend;
4c52953f 10550
70cc837d
AM
10551 /* If this branch reloc uses an opd sym, find the code section. */
10552 opd = get_opd_info (sym_sec);
10553 if (opd != NULL)
10554 {
10555 if (h == NULL && opd->adjust != NULL)
10556 {
10557 long adjust;
4c52953f 10558
70cc837d
AM
10559 adjust = opd->adjust[sym->st_value / 8];
10560 if (adjust == -1)
10561 /* Assume deleted functions won't ever be called. */
10562 continue;
10563 sym_value += adjust;
10564 }
4c52953f 10565
70cc837d
AM
10566 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
10567 if (dest == (bfd_vma) -1)
10568 continue;
10569 }
10570 else
10571 dest = (sym_value
10572 + sym_sec->output_offset
10573 + sym_sec->output_section->vma);
4c52953f 10574
70cc837d
AM
10575 /* Ignore branch to self. */
10576 if (sym_sec == isec)
10577 continue;
4c52953f 10578
70cc837d
AM
10579 /* If the called function uses the toc, we need a stub. */
10580 if (sym_sec->has_toc_reloc
10581 || sym_sec->makes_toc_func_call)
4c52953f 10582 {
70cc837d 10583 ret = 1;
4c52953f
AM
10584 break;
10585 }
70cc837d
AM
10586
10587 /* Assume any branch that needs a long branch stub might in fact
10588 need a plt_branch stub. A plt_branch stub uses r2. */
10589 else if (dest - (isec->output_offset
10590 + isec->output_section->vma
10591 + rel->r_offset) + (1 << 25) >= (2 << 25))
4c52953f 10592 {
70cc837d
AM
10593 ret = 1;
10594 break;
10595 }
10596
10597 /* If calling back to a section in the process of being
10598 tested, we can't say for sure that no toc adjusting stubs
10599 are needed, so don't return zero. */
10600 else if (sym_sec->call_check_in_progress)
10601 ret = 2;
10602
10603 /* Branches to another section that itself doesn't have any TOC
10604 references are OK. Recursively call ourselves to check. */
10605 else if (!sym_sec->call_check_done)
10606 {
10607 int recur;
10608
10609 /* Mark current section as indeterminate, so that other
10610 sections that call back to current won't be marked as
10611 known. */
10612 isec->call_check_in_progress = 1;
10613 recur = toc_adjusting_stub_needed (info, sym_sec);
10614 isec->call_check_in_progress = 0;
10615
4c52953f
AM
10616 if (recur != 0)
10617 {
70cc837d
AM
10618 ret = recur;
10619 if (recur != 2)
10620 break;
4c52953f
AM
10621 }
10622 }
4c52953f 10623 }
70cc837d
AM
10624
10625 if (local_syms != NULL
10626 && (elf_symtab_hdr (isec->owner).contents
10627 != (unsigned char *) local_syms))
10628 free (local_syms);
10629 if (elf_section_data (isec)->relocs != relstart)
10630 free (relstart);
9b5ecbd0
AM
10631 }
10632
70cc837d
AM
10633 if ((ret & 1) == 0
10634 && isec->map_head.s != NULL
10635 && (strcmp (isec->output_section->name, ".init") == 0
10636 || strcmp (isec->output_section->name, ".fini") == 0))
10637 {
10638 if (isec->map_head.s->has_toc_reloc
10639 || isec->map_head.s->makes_toc_func_call)
10640 ret = 1;
10641 else if (!isec->map_head.s->call_check_done)
10642 {
10643 int recur;
10644 isec->call_check_in_progress = 1;
10645 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
10646 isec->call_check_in_progress = 0;
10647 if (recur != 0)
10648 ret = recur;
10649 }
10650 }
10651
10652 if (ret == 1)
10653 isec->makes_toc_func_call = 1;
4c52953f 10654
9b5ecbd0
AM
10655 return ret;
10656}
10657
721956f4
AM
10658/* The linker repeatedly calls this function for each input section,
10659 in the order that input sections are linked into output sections.
10660 Build lists of input sections to determine groupings between which
10661 we may insert linker stubs. */
10662
9b5ecbd0 10663bfd_boolean
4ce794b7 10664ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
10665{
10666 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10667
4dfe6ac6
NC
10668 if (htab == NULL)
10669 return FALSE;
10670
734b6cf9
AM
10671 if ((isec->output_section->flags & SEC_CODE) != 0
10672 && isec->output_section->index <= htab->top_index)
721956f4 10673 {
734b6cf9 10674 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 10675 /* Steal the link_sec pointer for our list. */
721956f4 10676#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
10677 /* This happens to make the list in reverse order,
10678 which is what we want. */
734b6cf9
AM
10679 PREV_SEC (isec) = *list;
10680 *list = isec;
721956f4 10681 }
ad8e1ba5 10682
4c52953f 10683 if (htab->multi_toc_needed)
9b5ecbd0 10684 {
4c52953f
AM
10685 /* If a code section has a function that uses the TOC then we need
10686 to use the right TOC (obviously). Also, make sure that .opd gets
10687 the correct TOC value for R_PPC64_TOC relocs that don't have or
f94498ff
AM
10688 can't find their function symbol (shouldn't ever happen now).
10689 Also specially treat .fixup for the linux kernel. .fixup
10690 contains branches, but only back to the function that hit an
10691 exception. */
10692 if (isec->has_toc_reloc
10693 || (isec->flags & SEC_CODE) == 0
10694 || strcmp (isec->name, ".fixup") == 0)
4c52953f
AM
10695 {
10696 if (elf_gp (isec->owner) != 0)
10697 htab->toc_curr = elf_gp (isec->owner);
10698 }
6683a28d
AM
10699 else
10700 {
10701 if (!isec->call_check_done
10702 && toc_adjusting_stub_needed (info, isec) < 0)
10703 return FALSE;
10704 /* If we make a local call from this section, ie. a branch
10705 without a following nop, then we have no place to put a
10706 toc restoring insn. We must use the same toc group as
10707 the callee.
10708 Testing makes_toc_func_call actually tests for *any*
10709 calls to functions that need a good toc pointer. A more
10710 precise test would be better, as this one will set
10711 incorrect values for pasted .init/.fini fragments.
10712 (Fixed later in check_pasted_section.) */
10713 if (isec->makes_toc_func_call
10714 && elf_gp (isec->owner) != 0)
10715 htab->toc_curr = elf_gp (isec->owner);
10716 }
9b5ecbd0 10717 }
ad8e1ba5
AM
10718
10719 /* Functions that don't use the TOC can belong in any TOC group.
6683a28d 10720 Use the last TOC base. */
ad8e1ba5 10721 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 10722 return TRUE;
721956f4
AM
10723}
10724
70cc837d
AM
10725/* Check that all .init and .fini sections use the same toc, if they
10726 have toc relocs. */
10727
10728static bfd_boolean
10729check_pasted_section (struct bfd_link_info *info, const char *name)
10730{
10731 asection *o = bfd_get_section_by_name (info->output_bfd, name);
10732
10733 if (o != NULL)
10734 {
10735 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10736 bfd_vma toc_off = 0;
10737 asection *i;
10738
10739 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10740 if (i->has_toc_reloc)
10741 {
10742 if (toc_off == 0)
10743 toc_off = htab->stub_group[i->id].toc_off;
10744 else if (toc_off != htab->stub_group[i->id].toc_off)
10745 return FALSE;
10746 }
6683a28d
AM
10747
10748 if (toc_off == 0)
10749 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10750 if (i->makes_toc_func_call)
10751 {
10752 toc_off = htab->stub_group[i->id].toc_off;
10753 break;
10754 }
10755
70cc837d
AM
10756 /* Make sure the whole pasted function uses the same toc offset. */
10757 if (toc_off != 0)
10758 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
10759 htab->stub_group[i->id].toc_off = toc_off;
10760 }
10761 return TRUE;
10762}
10763
10764bfd_boolean
10765ppc64_elf_check_init_fini (struct bfd_link_info *info)
10766{
10767 return (check_pasted_section (info, ".init")
10768 & check_pasted_section (info, ".fini"));
10769}
10770
721956f4
AM
10771/* See whether we can group stub sections together. Grouping stub
10772 sections may result in fewer stubs. More importantly, we need to
10773 put all .init* and .fini* stubs at the beginning of the .init or
10774 .fini output sections respectively, because glibc splits the
10775 _init and _fini functions into multiple parts. Putting a stub in
10776 the middle of a function is not a good idea. */
10777
10778static void
4ce794b7
AM
10779group_sections (struct ppc_link_hash_table *htab,
10780 bfd_size_type stub_group_size,
10781 bfd_boolean stubs_always_before_branch)
721956f4 10782{
7c8fe5c4
AM
10783 asection **list;
10784 bfd_size_type stub14_group_size;
10785 bfd_boolean suppress_size_errors;
10786
10787 suppress_size_errors = FALSE;
10788 stub14_group_size = stub_group_size;
10789 if (stub_group_size == 1)
10790 {
10791 /* Default values. */
10792 if (stubs_always_before_branch)
10793 {
10794 stub_group_size = 0x1e00000;
10795 stub14_group_size = 0x7800;
10796 }
10797 else
10798 {
10799 stub_group_size = 0x1c00000;
10800 stub14_group_size = 0x7000;
10801 }
10802 suppress_size_errors = TRUE;
10803 }
10804
10805 list = htab->input_list + htab->top_index;
734b6cf9 10806 do
721956f4 10807 {
734b6cf9
AM
10808 asection *tail = *list;
10809 while (tail != NULL)
721956f4 10810 {
734b6cf9
AM
10811 asection *curr;
10812 asection *prev;
10813 bfd_size_type total;
10814 bfd_boolean big_sec;
10815 bfd_vma curr_toc;
10816
10817 curr = tail;
eea6121a 10818 total = tail->size;
6bee8834
AM
10819 big_sec = total > (ppc64_elf_section_data (tail) != NULL
10820 && ppc64_elf_section_data (tail)->has_14bit_branch
7c8fe5c4
AM
10821 ? stub14_group_size : stub_group_size);
10822 if (big_sec && !suppress_size_errors)
5c3dead3
AM
10823 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
10824 tail->owner, tail);
734b6cf9
AM
10825 curr_toc = htab->stub_group[tail->id].toc_off;
10826
10827 while ((prev = PREV_SEC (curr)) != NULL
10828 && ((total += curr->output_offset - prev->output_offset)
6bee8834
AM
10829 < (ppc64_elf_section_data (prev) != NULL
10830 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10831 ? stub14_group_size : stub_group_size))
ad8e1ba5 10832 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
10833 curr = prev;
10834
10835 /* OK, the size from the start of CURR to the end is less
10836 than stub_group_size and thus can be handled by one stub
10837 section. (or the tail section is itself larger than
10838 stub_group_size, in which case we may be toast.) We
10839 should really be keeping track of the total size of stubs
10840 added here, as stubs contribute to the final output
10841 section size. That's a little tricky, and this way will
10842 only break if stubs added make the total size more than
10843 2^25, ie. for the default stub_group_size, if stubs total
10844 more than 2097152 bytes, or nearly 75000 plt call stubs. */
10845 do
721956f4
AM
10846 {
10847 prev = PREV_SEC (tail);
734b6cf9 10848 /* Set up this stub group. */
721956f4
AM
10849 htab->stub_group[tail->id].link_sec = curr;
10850 }
734b6cf9
AM
10851 while (tail != curr && (tail = prev) != NULL);
10852
10853 /* But wait, there's more! Input sections up to stub_group_size
10854 bytes before the stub section can be handled by it too.
10855 Don't do this if we have a really large section after the
10856 stubs, as adding more stubs increases the chance that
10857 branches may not reach into the stub section. */
10858 if (!stubs_always_before_branch && !big_sec)
10859 {
10860 total = 0;
10861 while (prev != NULL
10862 && ((total += tail->output_offset - prev->output_offset)
6bee8834
AM
10863 < (ppc64_elf_section_data (prev) != NULL
10864 && ppc64_elf_section_data (prev)->has_14bit_branch
7c8fe5c4 10865 ? stub14_group_size : stub_group_size))
734b6cf9
AM
10866 && htab->stub_group[prev->id].toc_off == curr_toc)
10867 {
10868 tail = prev;
10869 prev = PREV_SEC (tail);
10870 htab->stub_group[tail->id].link_sec = curr;
10871 }
10872 }
10873 tail = prev;
721956f4
AM
10874 }
10875 }
734b6cf9
AM
10876 while (list-- != htab->input_list);
10877 free (htab->input_list);
721956f4
AM
10878#undef PREV_SEC
10879}
10880
721956f4
AM
10881/* Determine and set the size of the stub section for a final link.
10882
10883 The basic idea here is to examine all the relocations looking for
10884 PC-relative calls to a target that is unreachable with a "bl"
10885 instruction. */
10886
b34976b6 10887bfd_boolean
927be08e 10888ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size)
721956f4
AM
10889{
10890 bfd_size_type stub_group_size;
b34976b6 10891 bfd_boolean stubs_always_before_branch;
721956f4
AM
10892 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10893
4dfe6ac6
NC
10894 if (htab == NULL)
10895 return FALSE;
10896
721956f4
AM
10897 stubs_always_before_branch = group_size < 0;
10898 if (group_size < 0)
10899 stub_group_size = -group_size;
10900 else
10901 stub_group_size = group_size;
721956f4
AM
10902
10903 group_sections (htab, stub_group_size, stubs_always_before_branch);
10904
721956f4
AM
10905 while (1)
10906 {
10907 bfd *input_bfd;
10908 unsigned int bfd_indx;
10909 asection *stub_sec;
721956f4
AM
10910
10911 htab->stub_iteration += 1;
721956f4
AM
10912
10913 for (input_bfd = info->input_bfds, bfd_indx = 0;
10914 input_bfd != NULL;
10915 input_bfd = input_bfd->link_next, bfd_indx++)
10916 {
10917 Elf_Internal_Shdr *symtab_hdr;
10918 asection *section;
6cdc0ccc 10919 Elf_Internal_Sym *local_syms = NULL;
721956f4 10920
0c8d6e5c 10921 if (!is_ppc64_elf (input_bfd))
67f93c31
AM
10922 continue;
10923
721956f4 10924 /* We'll need the symbol table in a second. */
0ffa91dd 10925 symtab_hdr = &elf_symtab_hdr (input_bfd);
721956f4
AM
10926 if (symtab_hdr->sh_info == 0)
10927 continue;
10928
721956f4
AM
10929 /* Walk over each section attached to the input bfd. */
10930 for (section = input_bfd->sections;
10931 section != NULL;
10932 section = section->next)
10933 {
721956f4 10934 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
10935
10936 /* If there aren't any relocs, then there's nothing more
10937 to do. */
10938 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
10939 || (section->flags & SEC_ALLOC) == 0
10940 || (section->flags & SEC_LOAD) == 0
10941 || (section->flags & SEC_CODE) == 0
721956f4
AM
10942 || section->reloc_count == 0)
10943 continue;
10944
10945 /* If this section is a link-once section that will be
10946 discarded, then don't create any stubs. */
10947 if (section->output_section == NULL
927be08e 10948 || section->output_section->owner != info->output_bfd)
721956f4
AM
10949 continue;
10950
1e2f5b6e
AM
10951 /* Get the relocs. */
10952 internal_relocs
4ce794b7 10953 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 10954 info->keep_memory);
721956f4 10955 if (internal_relocs == NULL)
1e2f5b6e 10956 goto error_ret_free_local;
721956f4
AM
10957
10958 /* Now examine each relocation. */
10959 irela = internal_relocs;
10960 irelaend = irela + section->reloc_count;
10961 for (; irela < irelaend; irela++)
10962 {
4ce794b7
AM
10963 enum elf_ppc64_reloc_type r_type;
10964 unsigned int r_indx;
721956f4
AM
10965 enum ppc_stub_type stub_type;
10966 struct ppc_stub_hash_entry *stub_entry;
8387904d 10967 asection *sym_sec, *code_sec;
e054468f 10968 bfd_vma sym_value, code_value;
721956f4 10969 bfd_vma destination;
8843416a 10970 bfd_boolean ok_dest;
721956f4 10971 struct ppc_link_hash_entry *hash;
8387904d 10972 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
10973 struct elf_link_hash_entry *h;
10974 Elf_Internal_Sym *sym;
721956f4
AM
10975 char *stub_name;
10976 const asection *id_sec;
74f0fb50 10977 struct _opd_sec_data *opd;
e054468f 10978 struct plt_entry *plt_ent;
721956f4
AM
10979
10980 r_type = ELF64_R_TYPE (irela->r_info);
10981 r_indx = ELF64_R_SYM (irela->r_info);
10982
4ce794b7 10983 if (r_type >= R_PPC64_max)
721956f4
AM
10984 {
10985 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 10986 goto error_ret_free_internal;
721956f4
AM
10987 }
10988
10989 /* Only look for stubs on branch instructions. */
4ce794b7
AM
10990 if (r_type != R_PPC64_REL24
10991 && r_type != R_PPC64_REL14
10992 && r_type != R_PPC64_REL14_BRTAKEN
10993 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
10994 continue;
10995
10996 /* Now determine the call target, its name, value,
10997 section. */
411e1bfb
AM
10998 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
10999 r_indx, input_bfd))
11000 goto error_ret_free_internal;
11001 hash = (struct ppc_link_hash_entry *) h;
11002
8843416a 11003 ok_dest = FALSE;
8387904d 11004 fdh = NULL;
7fe2b9a6 11005 sym_value = 0;
411e1bfb 11006 if (hash == NULL)
721956f4 11007 {
411e1bfb 11008 sym_value = sym->st_value;
8843416a 11009 ok_dest = TRUE;
721956f4 11010 }
7fe2b9a6
AM
11011 else if (hash->elf.root.type == bfd_link_hash_defined
11012 || hash->elf.root.type == bfd_link_hash_defweak)
11013 {
11014 sym_value = hash->elf.root.u.def.value;
11015 if (sym_sec->output_section != NULL)
11016 ok_dest = TRUE;
11017 }
11018 else if (hash->elf.root.type == bfd_link_hash_undefweak
11019 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 11020 {
99877b66 11021 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
11022 use the func descriptor sym instead if it is
11023 defined. */
ceb1f1ef 11024 if (hash->elf.root.root.string[0] == '.'
b31867b6 11025 && (fdh = lookup_fdh (hash, htab)) != NULL)
8387904d 11026 {
8387904d
AM
11027 if (fdh->elf.root.type == bfd_link_hash_defined
11028 || fdh->elf.root.type == bfd_link_hash_defweak)
11029 {
11030 sym_sec = fdh->elf.root.u.def.section;
11031 sym_value = fdh->elf.root.u.def.value;
11032 if (sym_sec->output_section != NULL)
11033 ok_dest = TRUE;
11034 }
99877b66
AM
11035 else
11036 fdh = NULL;
8387904d 11037 }
7fe2b9a6
AM
11038 }
11039 else
11040 {
11041 bfd_set_error (bfd_error_bad_value);
11042 goto error_ret_free_internal;
721956f4
AM
11043 }
11044
8843416a
AM
11045 destination = 0;
11046 if (ok_dest)
11047 {
11048 sym_value += irela->r_addend;
11049 destination = (sym_value
11050 + sym_sec->output_offset
11051 + sym_sec->output_section->vma);
11052 }
11053
8387904d 11054 code_sec = sym_sec;
e054468f 11055 code_value = sym_value;
74f0fb50
AM
11056 opd = get_opd_info (sym_sec);
11057 if (opd != NULL)
8387904d
AM
11058 {
11059 bfd_vma dest;
11060
74f0fb50 11061 if (hash == NULL && opd->adjust != NULL)
8387904d 11062 {
74f0fb50 11063 long adjust = opd->adjust[sym_value / 8];
8387904d
AM
11064 if (adjust == -1)
11065 continue;
e054468f 11066 code_value += adjust;
8387904d
AM
11067 sym_value += adjust;
11068 }
11069 dest = opd_entry_value (sym_sec, sym_value,
e054468f 11070 &code_sec, &code_value);
8387904d
AM
11071 if (dest != (bfd_vma) -1)
11072 {
11073 destination = dest;
11074 if (fdh != NULL)
11075 {
11076 /* Fixup old ABI sym to point at code
11077 entry. */
99877b66 11078 hash->elf.root.type = bfd_link_hash_defweak;
8387904d 11079 hash->elf.root.u.def.section = code_sec;
e054468f 11080 hash->elf.root.u.def.value = code_value;
8387904d
AM
11081 }
11082 }
11083 }
11084
721956f4 11085 /* Determine what (if any) linker stub is needed. */
e054468f 11086 plt_ent = NULL;
721956f4 11087 stub_type = ppc_type_of_stub (section, irela, &hash,
e054468f 11088 &plt_ent, destination);
ad8e1ba5
AM
11089
11090 if (stub_type != ppc_stub_plt_call)
11091 {
11092 /* Check whether we need a TOC adjusting stub.
11093 Since the linker pastes together pieces from
11094 different object files when creating the
11095 _init and _fini functions, it may be that a
11096 call to what looks like a local sym is in
11097 fact a call needing a TOC adjustment. */
8387904d
AM
11098 if (code_sec != NULL
11099 && code_sec->output_section != NULL
11100 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 11101 != htab->stub_group[section->id].toc_off)
4c52953f
AM
11102 && (code_sec->has_toc_reloc
11103 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
11104 stub_type = ppc_stub_long_branch_r2off;
11105 }
11106
721956f4
AM
11107 if (stub_type == ppc_stub_none)
11108 continue;
11109
411e1bfb
AM
11110 /* __tls_get_addr calls might be eliminated. */
11111 if (stub_type != ppc_stub_plt_call
11112 && hash != NULL
8387904d
AM
11113 && (hash == htab->tls_get_addr
11114 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
11115 && section->has_tls_reloc
11116 && irela != internal_relocs)
11117 {
11118 /* Get tls info. */
f961d9dd 11119 unsigned char *tls_mask;
411e1bfb 11120
3a71aa26 11121 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
411e1bfb
AM
11122 irela - 1, input_bfd))
11123 goto error_ret_free_internal;
e7b938ca 11124 if (*tls_mask != 0)
411e1bfb
AM
11125 continue;
11126 }
11127
721956f4
AM
11128 /* Support for grouping stub sections. */
11129 id_sec = htab->stub_group[section->id].link_sec;
11130
11131 /* Get the name of this stub. */
11132 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11133 if (!stub_name)
11134 goto error_ret_free_internal;
11135
11136 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 11137 stub_name, FALSE, FALSE);
721956f4
AM
11138 if (stub_entry != NULL)
11139 {
11140 /* The proper stub has already been created. */
11141 free (stub_name);
11142 continue;
11143 }
11144
11145 stub_entry = ppc_add_stub (stub_name, section, htab);
11146 if (stub_entry == NULL)
11147 {
11148 free (stub_name);
6cdc0ccc
AM
11149 error_ret_free_internal:
11150 if (elf_section_data (section)->relocs == NULL)
11151 free (internal_relocs);
11152 error_ret_free_local:
11153 if (local_syms != NULL
11154 && (symtab_hdr->contents
11155 != (unsigned char *) local_syms))
11156 free (local_syms);
b34976b6 11157 return FALSE;
721956f4
AM
11158 }
11159
ad8e1ba5 11160 stub_entry->stub_type = stub_type;
e054468f
AM
11161 if (stub_type != ppc_stub_plt_call)
11162 {
11163 stub_entry->target_value = code_value;
11164 stub_entry->target_section = code_sec;
11165 }
11166 else
11167 {
11168 stub_entry->target_value = sym_value;
11169 stub_entry->target_section = sym_sec;
11170 }
721956f4 11171 stub_entry->h = hash;
e054468f 11172 stub_entry->plt_ent = plt_ent;
411e1bfb 11173 stub_entry->addend = irela->r_addend;
ee75fd95
AM
11174
11175 if (stub_entry->h != NULL)
11176 htab->stub_globals += 1;
721956f4
AM
11177 }
11178
11179 /* We're done with the internal relocs, free them. */
6cdc0ccc 11180 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 11181 free (internal_relocs);
721956f4 11182 }
6cdc0ccc
AM
11183
11184 if (local_syms != NULL
11185 && symtab_hdr->contents != (unsigned char *) local_syms)
11186 {
11187 if (!info->keep_memory)
11188 free (local_syms);
11189 else
11190 symtab_hdr->contents = (unsigned char *) local_syms;
11191 }
721956f4
AM
11192 }
11193
5c3dead3 11194 /* We may have added some stubs. Find out the new size of the
721956f4
AM
11195 stub sections. */
11196 for (stub_sec = htab->stub_bfd->sections;
11197 stub_sec != NULL;
11198 stub_sec = stub_sec->next)
e717da7e 11199 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 11200 {
5c3dead3 11201 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
11202 stub_sec->size = 0;
11203 stub_sec->reloc_count = 0;
84f5d08e 11204 stub_sec->flags &= ~SEC_RELOC;
ee75fd95 11205 }
eea6121a
AM
11206
11207 htab->brlt->size = 0;
84f5d08e
AM
11208 htab->brlt->reloc_count = 0;
11209 htab->brlt->flags &= ~SEC_RELOC;
ee75fd95 11210 if (htab->relbrlt != NULL)
eea6121a 11211 htab->relbrlt->size = 0;
721956f4 11212
63bc6f6c 11213 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 11214
176a0d42
AM
11215 if (info->emitrelocations
11216 && htab->glink != NULL && htab->glink->size != 0)
11217 {
11218 htab->glink->reloc_count = 1;
11219 htab->glink->flags |= SEC_RELOC;
11220 }
11221
5c3dead3
AM
11222 for (stub_sec = htab->stub_bfd->sections;
11223 stub_sec != NULL;
11224 stub_sec = stub_sec->next)
11225 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11226 && stub_sec->rawsize != stub_sec->size)
11227 break;
11228
11229 /* Exit from this loop when no stubs have been added, and no stubs
11230 have changed size. */
11231 if (stub_sec == NULL)
11232 break;
11233
721956f4
AM
11234 /* Ask the linker to do its stuff. */
11235 (*htab->layout_sections_again) ();
11236 }
11237
c456f082 11238 /* It would be nice to strip htab->brlt from the output if the
afbe61cf
AM
11239 section is empty, but it's too late. If we strip sections here,
11240 the dynamic symbol table is corrupted since the section symbol
11241 for the stripped section isn't written. */
721956f4 11242
b34976b6 11243 return TRUE;
721956f4
AM
11244}
11245
11246/* Called after we have determined section placement. If sections
805fc799 11247 move, we'll be called again. Provide a value for TOCstart. */
721956f4 11248
805fc799 11249bfd_vma
4ce794b7 11250ppc64_elf_toc (bfd *obfd)
721956f4 11251{
805fc799
AM
11252 asection *s;
11253 bfd_vma TOCstart;
721956f4 11254
805fc799
AM
11255 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
11256 order. The TOC starts where the first of these sections starts. */
11257 s = bfd_get_section_by_name (obfd, ".got");
e054468f 11258 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11259 s = bfd_get_section_by_name (obfd, ".toc");
e054468f 11260 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11261 s = bfd_get_section_by_name (obfd, ".tocbss");
e054468f 11262 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799 11263 s = bfd_get_section_by_name (obfd, ".plt");
e054468f 11264 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
805fc799
AM
11265 {
11266 /* This may happen for
11267 o references to TOC base (SYM@toc / TOC[tc0]) without a
11268 .toc directive
11269 o bad linker script
11270 o --gc-sections and empty TOC sections
11271
11272 FIXME: Warn user? */
11273
11274 /* Look for a likely section. We probably won't even be
11275 using TOCstart. */
11276 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11277 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
11278 | SEC_EXCLUDE))
805fc799
AM
11279 == (SEC_ALLOC | SEC_SMALL_DATA))
11280 break;
721956f4 11281 if (s == NULL)
805fc799 11282 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11283 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
805fc799
AM
11284 == (SEC_ALLOC | SEC_SMALL_DATA))
11285 break;
721956f4 11286 if (s == NULL)
805fc799 11287 for (s = obfd->sections; s != NULL; s = s->next)
e054468f
AM
11288 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
11289 == SEC_ALLOC)
805fc799 11290 break;
721956f4 11291 if (s == NULL)
805fc799 11292 for (s = obfd->sections; s != NULL; s = s->next)
e054468f 11293 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
805fc799
AM
11294 break;
11295 }
721956f4 11296
805fc799
AM
11297 TOCstart = 0;
11298 if (s != NULL)
11299 TOCstart = s->output_section->vma + s->output_offset;
721956f4 11300
805fc799 11301 return TOCstart;
721956f4
AM
11302}
11303
11304/* Build all the stubs associated with the current output file.
11305 The stubs are kept in a hash table attached to the main linker
11306 hash table. This function is called via gldelf64ppc_finish. */
11307
b34976b6 11308bfd_boolean
4ce794b7
AM
11309ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
11310 struct bfd_link_info *info,
11311 char **stats)
5d1634d7
AM
11312{
11313 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 11314 asection *stub_sec;
5d1634d7 11315 bfd_byte *p;
e717da7e 11316 int stub_sec_count = 0;
5d1634d7 11317
4dfe6ac6
NC
11318 if (htab == NULL)
11319 return FALSE;
11320
ad8e1ba5 11321 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
11322
11323 /* Allocate memory to hold the linker stubs. */
721956f4
AM
11324 for (stub_sec = htab->stub_bfd->sections;
11325 stub_sec != NULL;
11326 stub_sec = stub_sec->next)
eea6121a
AM
11327 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
11328 && stub_sec->size != 0)
e717da7e 11329 {
eea6121a
AM
11330 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
11331 if (stub_sec->contents == NULL)
11332 return FALSE;
11333 /* We want to check that built size is the same as calculated
11334 size. rawsize is a convenient location to use. */
11335 stub_sec->rawsize = stub_sec->size;
11336 stub_sec->size = 0;
e717da7e 11337 }
5d1634d7 11338
23eb7e01 11339 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 11340 {
9f951329 11341 unsigned int indx;
ad8e1ba5 11342 bfd_vma plt0;
9f951329 11343
721956f4 11344 /* Build the .glink plt call stub. */
97b639ba
AM
11345 if (htab->emit_stub_syms)
11346 {
11347 struct elf_link_hash_entry *h;
468392fb
AM
11348 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
11349 TRUE, FALSE, FALSE);
97b639ba
AM
11350 if (h == NULL)
11351 return FALSE;
11352 if (h->root.type == bfd_link_hash_new)
11353 {
11354 h->root.type = bfd_link_hash_defined;
11355 h->root.u.def.section = htab->glink;
ee4bf8d2 11356 h->root.u.def.value = 8;
f5385ebf
AM
11357 h->ref_regular = 1;
11358 h->def_regular = 1;
11359 h->ref_regular_nonweak = 1;
11360 h->forced_local = 1;
11361 h->non_elf = 0;
97b639ba
AM
11362 }
11363 }
176a0d42
AM
11364 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
11365 if (info->emitrelocations)
11366 {
11367 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
11368 if (r == NULL)
11369 return FALSE;
11370 r->r_offset = (htab->glink->output_offset
11371 + htab->glink->output_section->vma);
11372 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
11373 r->r_addend = plt0;
11374 }
4ce794b7 11375 p = htab->glink->contents;
176a0d42 11376 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
ee4bf8d2
AM
11377 bfd_put_64 (htab->glink->owner, plt0, p);
11378 p += 8;
11379 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 11380 p += 4;
ee4bf8d2 11381 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 11382 p += 4;
ee4bf8d2 11383 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 11384 p += 4;
ee4bf8d2 11385 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 11386 p += 4;
ee4bf8d2 11387 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 11388 p += 4;
ee4bf8d2 11389 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 11390 p += 4;
ee4bf8d2 11391 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 11392 p += 4;
4ce794b7 11393 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 11394 p += 4;
4ce794b7 11395 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 11396 p += 4;
4ce794b7 11397 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 11398 p += 4;
4ce794b7 11399 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 11400 p += 4;
ee4bf8d2
AM
11401 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
11402 {
11403 bfd_put_32 (htab->glink->owner, NOP, p);
11404 p += 4;
11405 }
ad8e1ba5 11406
9f951329
AM
11407 /* Build the .glink lazy link call stubs. */
11408 indx = 0;
eea6121a 11409 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
11410 {
11411 if (indx < 0x8000)
11412 {
4ce794b7 11413 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
11414 p += 4;
11415 }
11416 else
11417 {
4ce794b7 11418 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 11419 p += 4;
4ce794b7 11420 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
11421 p += 4;
11422 }
4ce794b7 11423 bfd_put_32 (htab->glink->owner,
ee4bf8d2 11424 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 11425 indx++;
9f951329
AM
11426 p += 4;
11427 }
eea6121a 11428 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 11429 }
5d1634d7 11430
eea6121a 11431 if (htab->brlt->size != 0)
721956f4 11432 {
4ce794b7 11433 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 11434 htab->brlt->size);
4ce794b7 11435 if (htab->brlt->contents == NULL)
b34976b6 11436 return FALSE;
721956f4 11437 }
ee75fd95 11438 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
11439 {
11440 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 11441 htab->relbrlt->size);
63bc6f6c
AM
11442 if (htab->relbrlt->contents == NULL)
11443 return FALSE;
11444 }
5d1634d7 11445
721956f4
AM
11446 /* Build the stubs as directed by the stub hash table. */
11447 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 11448
aa8a7074
AM
11449 if (htab->relbrlt != NULL)
11450 htab->relbrlt->reloc_count = 0;
11451
721956f4
AM
11452 for (stub_sec = htab->stub_bfd->sections;
11453 stub_sec != NULL;
11454 stub_sec = stub_sec->next)
e717da7e
AM
11455 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
11456 {
11457 stub_sec_count += 1;
eea6121a 11458 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
11459 break;
11460 }
5d1634d7 11461
721956f4 11462 if (stub_sec != NULL
eea6121a 11463 || htab->glink->rawsize != htab->glink->size)
5d1634d7 11464 {
b34976b6 11465 htab->stub_error = TRUE;
721956f4 11466 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5d1634d7 11467 }
721956f4 11468
d2a300cf
AM
11469 if (htab->stub_error)
11470 return FALSE;
11471
11472 if (stats != NULL)
11473 {
11474 *stats = bfd_malloc (500);
11475 if (*stats == NULL)
11476 return FALSE;
11477
ee75fd95 11478 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
11479 " branch %lu\n"
11480 " toc adjust %lu\n"
11481 " long branch %lu\n"
11482 " long toc adj %lu\n"
11483 " plt call %lu"),
e717da7e 11484 stub_sec_count,
ee75fd95 11485 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
11486 htab->stub_count[ppc_stub_long_branch - 1],
11487 htab->stub_count[ppc_stub_long_branch_r2off - 1],
11488 htab->stub_count[ppc_stub_plt_branch - 1],
11489 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
11490 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
11491 }
11492 return TRUE;
5bd4f169
AM
11493}
11494
99877b66
AM
11495/* This function undoes the changes made by add_symbol_adjust. */
11496
11497static bfd_boolean
11498undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
11499{
11500 struct ppc_link_hash_entry *eh;
11501
11502 if (h->root.type == bfd_link_hash_indirect)
11503 return TRUE;
11504
11505 if (h->root.type == bfd_link_hash_warning)
11506 h = (struct elf_link_hash_entry *) h->root.u.i.link;
11507
11508 eh = (struct ppc_link_hash_entry *) h;
11509 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
11510 return TRUE;
11511
11512 eh->elf.root.type = bfd_link_hash_undefined;
11513 return TRUE;
11514}
11515
11516void
11517ppc64_elf_restore_symbols (struct bfd_link_info *info)
11518{
11519 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4dfe6ac6
NC
11520
11521 if (htab != NULL)
11522 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
99877b66
AM
11523}
11524
60124e18
AM
11525/* What to do when ld finds relocations against symbols defined in
11526 discarded sections. */
11527
11528static unsigned int
11529ppc64_elf_action_discarded (asection *sec)
11530{
11531 if (strcmp (".opd", sec->name) == 0)
11532 return 0;
11533
11534 if (strcmp (".toc", sec->name) == 0)
11535 return 0;
11536
bce50a28
JJ
11537 if (strcmp (".toc1", sec->name) == 0)
11538 return 0;
11539
60124e18
AM
11540 return _bfd_elf_default_action_discarded (sec);
11541}
11542
ba761f19 11543/* REL points to a low-part reloc on a largetoc instruction sequence.
67f0cbdb 11544 Find the matching high-part reloc instruction and verify that it
d983b7a0
AM
11545 is addis REG,x,imm. If so, set *REG to x and return a pointer to
11546 the high-part reloc. */
67f0cbdb
AM
11547
11548static const Elf_Internal_Rela *
11549ha_reloc_match (const Elf_Internal_Rela *relocs,
11550 const Elf_Internal_Rela *rel,
d983b7a0
AM
11551 unsigned int *reg,
11552 bfd_boolean match_addend,
67f0cbdb
AM
11553 const bfd *input_bfd,
11554 const bfd_byte *contents)
11555{
11556 enum elf_ppc64_reloc_type r_type, r_type_ha;
11557 bfd_vma r_info_ha, r_addend;
11558
11559 r_type = ELF64_R_TYPE (rel->r_info);
11560 switch (r_type)
11561 {
11562 case R_PPC64_GOT_TLSLD16_LO:
11563 case R_PPC64_GOT_TLSGD16_LO:
11564 case R_PPC64_GOT_TPREL16_LO_DS:
11565 case R_PPC64_GOT_DTPREL16_LO_DS:
11566 case R_PPC64_GOT16_LO:
11567 case R_PPC64_TOC16_LO:
11568 r_type_ha = r_type + 2;
11569 break;
11570 case R_PPC64_GOT16_LO_DS:
11571 r_type_ha = R_PPC64_GOT16_HA;
11572 break;
11573 case R_PPC64_TOC16_LO_DS:
11574 r_type_ha = R_PPC64_TOC16_HA;
11575 break;
11576 default:
11577 abort ();
11578 }
11579 r_info_ha = ELF64_R_INFO (ELF64_R_SYM (rel->r_info), r_type_ha);
11580 r_addend = rel->r_addend;
11581
11582 while (--rel >= relocs)
11583 if (rel->r_info == r_info_ha
d983b7a0
AM
11584 && (!match_addend
11585 || rel->r_addend == r_addend))
67f0cbdb
AM
11586 {
11587 const bfd_byte *p = contents + (rel->r_offset & ~3);
11588 unsigned int insn = bfd_get_32 (input_bfd, p);
d983b7a0
AM
11589 if ((insn & (0x3f << 26)) == (15u << 26) /* addis rt,x,imm */
11590 && (insn & (0x1f << 21)) == (*reg << 21))
11591 {
11592 *reg = (insn >> 16) & 0x1f;
11593 return rel;
11594 }
67f0cbdb
AM
11595 break;
11596 }
11597 return NULL;
11598}
11599
5bd4f169
AM
11600/* The RELOCATE_SECTION function is called by the ELF backend linker
11601 to handle the relocations for a section.
11602
11603 The relocs are always passed as Rela structures; if the section
11604 actually uses Rel structures, the r_addend field will always be
11605 zero.
11606
11607 This function is responsible for adjust the section contents as
11608 necessary, and (if using Rela relocs and generating a
1049f94e 11609 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
11610 necessary.
11611
11612 This function does not have to worry about setting the reloc
11613 address or the reloc symbol index.
11614
11615 LOCAL_SYMS is a pointer to the swapped in local symbols.
11616
11617 LOCAL_SECTIONS is an array giving the section in the input file
11618 corresponding to the st_shndx field of each local symbol.
11619
11620 The global hash table entry for the global symbols can be found
11621 via elf_sym_hashes (input_bfd).
11622
1049f94e 11623 When generating relocatable output, this function must handle
5bd4f169
AM
11624 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
11625 going to be the section symbol corresponding to the output
11626 section, which means that the addend must be adjusted
11627 accordingly. */
11628
b34976b6 11629static bfd_boolean
4ce794b7
AM
11630ppc64_elf_relocate_section (bfd *output_bfd,
11631 struct bfd_link_info *info,
11632 bfd *input_bfd,
11633 asection *input_section,
11634 bfd_byte *contents,
11635 Elf_Internal_Rela *relocs,
11636 Elf_Internal_Sym *local_syms,
11637 asection **local_sections)
5bd4f169 11638{
65f38f15 11639 struct ppc_link_hash_table *htab;
5bd4f169
AM
11640 Elf_Internal_Shdr *symtab_hdr;
11641 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
11642 Elf_Internal_Rela *rel;
11643 Elf_Internal_Rela *relend;
411e1bfb
AM
11644 Elf_Internal_Rela outrel;
11645 bfd_byte *loc;
411e1bfb 11646 struct got_entry **local_got_ents;
d983b7a0 11647 unsigned char *ha_opt;
5bd4f169 11648 bfd_vma TOCstart;
d983b7a0 11649 bfd_boolean no_ha_opt;
b34976b6
AM
11650 bfd_boolean ret = TRUE;
11651 bfd_boolean is_opd;
96e0dda4 11652 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 11653 bfd_boolean is_power4 = FALSE;
4fe5ca5b 11654 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 11655
65f38f15 11656 /* Initialize howto table if needed. */
5bd4f169 11657 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
11658 ppc_howto_init ();
11659
65f38f15 11660 htab = ppc_hash_table (info);
4dfe6ac6
NC
11661 if (htab == NULL)
11662 return FALSE;
ee75fd95
AM
11663
11664 /* Don't relocate stub sections. */
11665 if (input_section->owner == htab->stub_bfd)
11666 return TRUE;
11667
0c8d6e5c 11668 BFD_ASSERT (is_ppc64_elf (input_bfd));
0ffa91dd 11669
411e1bfb 11670 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 11671 TOCstart = elf_gp (output_bfd);
0ffa91dd 11672 symtab_hdr = &elf_symtab_hdr (input_bfd);
5bd4f169 11673 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 11674 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
d983b7a0
AM
11675 ha_opt = NULL;
11676 no_ha_opt = FALSE;
65f38f15 11677
5bd4f169
AM
11678 rel = relocs;
11679 relend = relocs + input_section->reloc_count;
11680 for (; rel < relend; rel++)
11681 {
04c9666a 11682 enum elf_ppc64_reloc_type r_type;
4cc603a5 11683 bfd_vma addend, orig_addend;
5bd4f169
AM
11684 bfd_reloc_status_type r;
11685 Elf_Internal_Sym *sym;
11686 asection *sec;
039b3fef
AM
11687 struct elf_link_hash_entry *h_elf;
11688 struct ppc_link_hash_entry *h;
11689 struct ppc_link_hash_entry *fdh;
5bd4f169 11690 const char *sym_name;
0d4792f7 11691 unsigned long r_symndx, toc_symndx;
3a71aa26 11692 bfd_vma toc_addend;
f961d9dd
AM
11693 unsigned char tls_mask, tls_gd, tls_type;
11694 unsigned char sym_type;
5bd4f169 11695 bfd_vma relocation;
b34976b6
AM
11696 bfd_boolean unresolved_reloc;
11697 bfd_boolean warned;
67f0cbdb 11698 unsigned int insn;
e11840f9 11699 unsigned int mask;
721956f4
AM
11700 struct ppc_stub_hash_entry *stub_entry;
11701 bfd_vma max_br_offset;
11702 bfd_vma from;
5bd4f169 11703
4ce794b7 11704 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 11705 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
11706
11707 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
11708 symbol of the previous ADDR64 reloc. The symbol gives us the
11709 proper TOC base to use. */
11710 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
11711 && rel != relocs
11712 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
11713 && is_opd)
11714 r_symndx = ELF64_R_SYM (rel[-1].r_info);
11715
4ce794b7
AM
11716 sym = NULL;
11717 sec = NULL;
039b3fef 11718 h_elf = NULL;
4ce794b7 11719 sym_name = NULL;
b34976b6
AM
11720 unresolved_reloc = FALSE;
11721 warned = FALSE;
4cc603a5 11722 orig_addend = rel->r_addend;
65f38f15 11723
0b13192e 11724 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
11725 {
11726 /* It's a local symbol. */
74f0fb50 11727 struct _opd_sec_data *opd;
4025353c 11728
5bd4f169
AM
11729 sym = local_syms + r_symndx;
11730 sec = local_sections[r_symndx];
26c61ae5 11731 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 11732 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 11733 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
74f0fb50
AM
11734 opd = get_opd_info (sec);
11735 if (opd != NULL && opd->adjust != NULL)
1e2f5b6e 11736 {
74f0fb50 11737 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
11738 if (adjust == -1)
11739 relocation = 0;
11740 else
4cc603a5
AM
11741 {
11742 /* If this is a relocation against the opd section sym
11743 and we have edited .opd, adjust the reloc addend so
11744 that ld -r and ld --emit-relocs output is correct.
11745 If it is a reloc against some other .opd symbol,
11746 then the symbol value will be adjusted later. */
11747 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
11748 rel->r_addend += adjust;
11749 else
11750 relocation += adjust;
11751 }
1e2f5b6e 11752 }
5bd4f169
AM
11753 }
11754 else
11755 {
b2a8e766
AM
11756 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
11757 r_symndx, symtab_hdr, sym_hashes,
039b3fef 11758 h_elf, sec, relocation,
b2a8e766 11759 unresolved_reloc, warned);
039b3fef
AM
11760 sym_name = h_elf->root.root.string;
11761 sym_type = h_elf->type;
5bd4f169 11762 }
039b3fef 11763 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 11764
ab96bf03 11765 if (sec != NULL && elf_discarded_section (sec))
e4067dbb
DJ
11766 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
11767 rel, relend,
11768 ppc64_elf_howto_table[r_type],
11769 contents);
ab96bf03
AM
11770
11771 if (info->relocatable)
11772 continue;
11773
951fd09b
AM
11774 /* TLS optimizations. Replace instruction sequences and relocs
11775 based on information we collected in tls_optimize. We edit
11776 RELOCS so that --emit-relocs will output something sensible
11777 for the final instruction stream. */
11778 tls_mask = 0;
11779 tls_gd = 0;
0d4792f7 11780 toc_symndx = 0;
727fc41e
AM
11781 if (h != NULL)
11782 tls_mask = h->tls_mask;
11783 else if (local_got_ents != NULL)
411e1bfb 11784 {
e054468f
AM
11785 struct plt_entry **local_plt = (struct plt_entry **)
11786 (local_got_ents + symtab_hdr->sh_info);
f961d9dd 11787 unsigned char *lgot_masks = (unsigned char *)
e054468f 11788 (local_plt + symtab_hdr->sh_info);
727fc41e
AM
11789 tls_mask = lgot_masks[r_symndx];
11790 }
11791 if (tls_mask == 0
11792 && (r_type == R_PPC64_TLS
11793 || r_type == R_PPC64_TLSGD
11794 || r_type == R_PPC64_TLSLD))
11795 {
11796 /* Check for toc tls entries. */
f961d9dd 11797 unsigned char *toc_tls;
0d4792f7 11798
727fc41e
AM
11799 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11800 &local_syms, rel, input_bfd))
11801 return FALSE;
0d4792f7 11802
727fc41e
AM
11803 if (toc_tls)
11804 tls_mask = *toc_tls;
0d4792f7
AM
11805 }
11806
11807 /* Check that tls relocs are used with tls syms, and non-tls
11808 relocs are used with non-tls syms. */
cf35638d 11809 if (r_symndx != STN_UNDEF
0d4792f7
AM
11810 && r_type != R_PPC64_NONE
11811 && (h == NULL
039b3fef
AM
11812 || h->elf.root.type == bfd_link_hash_defined
11813 || h->elf.root.type == bfd_link_hash_defweak)
1d483afe
AM
11814 && (IS_PPC64_TLS_RELOC (r_type)
11815 != (sym_type == STT_TLS
11816 || (sym_type == STT_SECTION
11817 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
0d4792f7 11818 {
727fc41e
AM
11819 if (tls_mask != 0
11820 && (r_type == R_PPC64_TLS
11821 || r_type == R_PPC64_TLSGD
11822 || r_type == R_PPC64_TLSLD))
0d4792f7
AM
11823 /* R_PPC64_TLS is OK against a symbol in the TOC. */
11824 ;
11825 else
11826 (*_bfd_error_handler)
1d483afe 11827 (!IS_PPC64_TLS_RELOC (r_type)
d003868e
AM
11828 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
11829 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
11830 input_bfd,
11831 input_section,
0d4792f7
AM
11832 (long) rel->r_offset,
11833 ppc64_elf_howto_table[r_type]->name,
11834 sym_name);
411e1bfb
AM
11835 }
11836
11837 /* Ensure reloc mapping code below stays sane. */
11838 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
11839 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
11840 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
11841 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
11842 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
11843 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
11844 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
11845 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
11846 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
11847 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
11848 abort ();
0d4792f7 11849
411e1bfb
AM
11850 switch (r_type)
11851 {
11852 default:
411e1bfb
AM
11853 break;
11854
ba761f19
AM
11855 case R_PPC64_LO_DS_OPT:
11856 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
11857 if ((insn & (0x3f << 26)) != 58u << 26)
11858 abort ();
11859 insn += (14u << 26) - (58u << 26);
11860 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
11861 r_type = R_PPC64_TOC16_LO;
11862 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11863 break;
11864
411e1bfb
AM
11865 case R_PPC64_TOC16:
11866 case R_PPC64_TOC16_LO:
11867 case R_PPC64_TOC16_DS:
11868 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
11869 {
11870 /* Check for toc tls entries. */
f961d9dd 11871 unsigned char *toc_tls;
951fd09b 11872 int retval;
411e1bfb 11873
3a71aa26
AM
11874 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
11875 &local_syms, rel, input_bfd);
951fd09b 11876 if (retval == 0)
411e1bfb
AM
11877 return FALSE;
11878
11879 if (toc_tls)
11880 {
951fd09b 11881 tls_mask = *toc_tls;
411e1bfb
AM
11882 if (r_type == R_PPC64_TOC16_DS
11883 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
11884 {
11885 if (tls_mask != 0
11886 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
11887 goto toctprel;
11888 }
411e1bfb 11889 else
951fd09b
AM
11890 {
11891 /* If we found a GD reloc pair, then we might be
11892 doing a GD->IE transition. */
11893 if (retval == 2)
11894 {
11895 tls_gd = TLS_TPRELGD;
11896 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 11897 goto tls_ldgd_opt;
951fd09b
AM
11898 }
11899 else if (retval == 3)
11900 {
11901 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
102890f0 11902 goto tls_ldgd_opt;
951fd09b
AM
11903 }
11904 }
411e1bfb
AM
11905 }
11906 }
11907 break;
11908
9d6ded02
AM
11909 case R_PPC64_GOT_TPREL16_HI:
11910 case R_PPC64_GOT_TPREL16_HA:
11911 if (tls_mask != 0
11912 && (tls_mask & TLS_TPREL) == 0)
11913 {
11914 rel->r_offset -= d_offset;
11915 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
11916 r_type = R_PPC64_NONE;
11917 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
11918 }
11919 break;
11920
411e1bfb
AM
11921 case R_PPC64_GOT_TPREL16_DS:
11922 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
11923 if (tls_mask != 0
11924 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11925 {
81407a69 11926 toctprel:
4fe5ca5b 11927 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
11928 insn &= 31 << 21;
11929 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 11930 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 11931 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
11932 if (toc_symndx != 0)
11933 {
11934 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11935 rel->r_addend = toc_addend;
0d4792f7
AM
11936 /* We changed the symbol. Start over in order to
11937 get h, sym, sec etc. right. */
11938 rel--;
11939 continue;
11940 }
11941 else
11942 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11943 }
11944 break;
11945
11946 case R_PPC64_TLS:
951fd09b
AM
11947 if (tls_mask != 0
11948 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 11949 {
411e1bfb 11950 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
2d0f3896
AM
11951 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
11952 if (insn == 0)
411e1bfb 11953 abort ();
411e1bfb 11954 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 11955 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
11956 PPC64_TPREL16_LO which is at low-order half-word. */
11957 rel->r_offset += d_offset;
0d4792f7
AM
11958 r_type = R_PPC64_TPREL16_LO;
11959 if (toc_symndx != 0)
11960 {
11961 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
3a71aa26 11962 rel->r_addend = toc_addend;
0d4792f7
AM
11963 /* We changed the symbol. Start over in order to
11964 get h, sym, sec etc. right. */
11965 rel--;
11966 continue;
11967 }
11968 else
11969 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11970 }
11971 break;
11972
411e1bfb
AM
11973 case R_PPC64_GOT_TLSGD16_HI:
11974 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
11975 tls_gd = TLS_TPRELGD;
11976 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
11977 goto tls_gdld_hi;
11978 break;
11979
411e1bfb
AM
11980 case R_PPC64_GOT_TLSLD16_HI:
11981 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 11982 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 11983 {
951fd09b
AM
11984 tls_gdld_hi:
11985 if ((tls_mask & tls_gd) != 0)
11986 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
11987 + R_PPC64_GOT_TPREL16_DS);
11988 else
411e1bfb 11989 {
4fe5ca5b 11990 rel->r_offset -= d_offset;
727ac201 11991 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
951fd09b 11992 r_type = R_PPC64_NONE;
411e1bfb 11993 }
951fd09b 11994 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
11995 }
11996 break;
11997
951fd09b
AM
11998 case R_PPC64_GOT_TLSGD16:
11999 case R_PPC64_GOT_TLSGD16_LO:
12000 tls_gd = TLS_TPRELGD;
12001 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
102890f0 12002 goto tls_ldgd_opt;
951fd09b 12003 break;
411e1bfb 12004
951fd09b
AM
12005 case R_PPC64_GOT_TLSLD16:
12006 case R_PPC64_GOT_TLSLD16_LO:
12007 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12008 {
3a71aa26 12009 unsigned int insn1, insn2, insn3;
102890f0
AM
12010 bfd_vma offset;
12011
12012 tls_ldgd_opt:
727fc41e
AM
12013 offset = (bfd_vma) -1;
12014 /* If not using the newer R_PPC64_TLSGD/LD to mark
12015 __tls_get_addr calls, we must trust that the call
12016 stays with its arg setup insns, ie. that the next
12017 reloc is the __tls_get_addr call associated with
12018 the current reloc. Edit both insns. */
12019 if (input_section->has_tls_get_addr_call
12020 && rel + 1 < relend
12021 && branch_reloc_hash_match (input_bfd, rel + 1,
12022 htab->tls_get_addr,
12023 htab->tls_get_addr_fd))
12024 offset = rel[1].r_offset;
102890f0 12025 if ((tls_mask & tls_gd) != 0)
411e1bfb 12026 {
102890f0 12027 /* IE */
3a71aa26
AM
12028 insn1 = bfd_get_32 (output_bfd,
12029 contents + rel->r_offset - d_offset);
102890f0
AM
12030 insn1 &= (1 << 26) - (1 << 2);
12031 insn1 |= 58 << 26; /* ld */
12032 insn2 = 0x7c636a14; /* add 3,3,13 */
727fc41e 12033 if (offset != (bfd_vma) -1)
f58d5a2d 12034 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
102890f0
AM
12035 if ((tls_mask & TLS_EXPLICIT) == 0)
12036 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12037 + R_PPC64_GOT_TPREL16_DS);
411e1bfb 12038 else
102890f0
AM
12039 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
12040 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12041 }
12042 else
12043 {
12044 /* LE */
12045 insn1 = 0x3c6d0000; /* addis 3,13,0 */
12046 insn2 = 0x38630000; /* addi 3,3,0 */
12047 if (tls_gd == 0)
951fd09b 12048 {
102890f0 12049 /* Was an LD reloc. */
1d483afe
AM
12050 if (toc_symndx)
12051 sec = local_sections[toc_symndx];
12052 for (r_symndx = 0;
12053 r_symndx < symtab_hdr->sh_info;
12054 r_symndx++)
12055 if (local_sections[r_symndx] == sec)
12056 break;
12057 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12058 r_symndx = STN_UNDEF;
102890f0 12059 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12060 if (r_symndx != STN_UNDEF)
1d483afe
AM
12061 rel->r_addend -= (local_syms[r_symndx].st_value
12062 + sec->output_offset
12063 + sec->output_section->vma);
951fd09b 12064 }
102890f0 12065 else if (toc_symndx != 0)
3a71aa26
AM
12066 {
12067 r_symndx = toc_symndx;
12068 rel->r_addend = toc_addend;
12069 }
102890f0
AM
12070 r_type = R_PPC64_TPREL16_HA;
12071 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
727fc41e
AM
12072 if (offset != (bfd_vma) -1)
12073 {
12074 rel[1].r_info = ELF64_R_INFO (r_symndx,
12075 R_PPC64_TPREL16_LO);
12076 rel[1].r_offset = offset + d_offset;
12077 rel[1].r_addend = rel->r_addend;
12078 }
102890f0 12079 }
3a71aa26
AM
12080 bfd_put_32 (output_bfd, insn1,
12081 contents + rel->r_offset - d_offset);
727fc41e
AM
12082 if (offset != (bfd_vma) -1)
12083 {
12084 insn3 = bfd_get_32 (output_bfd,
12085 contents + offset + 4);
12086 if (insn3 == NOP
12087 || insn3 == CROR_151515 || insn3 == CROR_313131)
12088 {
12089 rel[1].r_offset += 4;
12090 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12091 insn2 = NOP;
12092 }
12093 bfd_put_32 (output_bfd, insn2, contents + offset);
12094 }
12095 if ((tls_mask & tls_gd) == 0
12096 && (tls_gd == 0 || toc_symndx != 0))
12097 {
12098 /* We changed the symbol. Start over in order
12099 to get h, sym, sec etc. right. */
12100 rel--;
12101 continue;
12102 }
12103 }
12104 break;
12105
12106 case R_PPC64_TLSGD:
12107 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12108 {
12109 unsigned int insn2, insn3;
12110 bfd_vma offset = rel->r_offset;
12111
12112 if ((tls_mask & TLS_TPRELGD) != 0)
12113 {
12114 /* IE */
12115 r_type = R_PPC64_NONE;
12116 insn2 = 0x7c636a14; /* add 3,3,13 */
12117 }
12118 else
12119 {
12120 /* LE */
12121 if (toc_symndx != 0)
12122 {
12123 r_symndx = toc_symndx;
12124 rel->r_addend = toc_addend;
12125 }
12126 r_type = R_PPC64_TPREL16_LO;
12127 rel->r_offset = offset + d_offset;
12128 insn2 = 0x38630000; /* addi 3,3,0 */
12129 }
12130 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12131 /* Zap the reloc on the _tls_get_addr call too. */
12132 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12133 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
3a71aa26
AM
12134 insn3 = bfd_get_32 (output_bfd,
12135 contents + offset + 4);
102890f0
AM
12136 if (insn3 == NOP
12137 || insn3 == CROR_151515 || insn3 == CROR_313131)
12138 {
727fc41e 12139 rel->r_offset += 4;
3a71aa26
AM
12140 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12141 insn2 = NOP;
102890f0 12142 }
102890f0 12143 bfd_put_32 (output_bfd, insn2, contents + offset);
727fc41e 12144 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
102890f0 12145 {
102890f0
AM
12146 rel--;
12147 continue;
411e1bfb 12148 }
411e1bfb 12149 }
411e1bfb
AM
12150 break;
12151
727fc41e
AM
12152 case R_PPC64_TLSLD:
12153 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12154 {
12155 unsigned int insn2, insn3;
12156 bfd_vma offset = rel->r_offset;
12157
12158 if (toc_symndx)
12159 sec = local_sections[toc_symndx];
12160 for (r_symndx = 0;
12161 r_symndx < symtab_hdr->sh_info;
12162 r_symndx++)
12163 if (local_sections[r_symndx] == sec)
12164 break;
12165 if (r_symndx >= symtab_hdr->sh_info)
cf35638d 12166 r_symndx = STN_UNDEF;
727fc41e 12167 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
cf35638d 12168 if (r_symndx != STN_UNDEF)
727fc41e
AM
12169 rel->r_addend -= (local_syms[r_symndx].st_value
12170 + sec->output_offset
12171 + sec->output_section->vma);
12172
12173 r_type = R_PPC64_TPREL16_LO;
12174 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12175 rel->r_offset = offset + d_offset;
12176 /* Zap the reloc on the _tls_get_addr call too. */
12177 BFD_ASSERT (offset == rel[1].r_offset);
f58d5a2d 12178 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
727fc41e
AM
12179 insn2 = 0x38630000; /* addi 3,3,0 */
12180 insn3 = bfd_get_32 (output_bfd,
12181 contents + offset + 4);
12182 if (insn3 == NOP
12183 || insn3 == CROR_151515 || insn3 == CROR_313131)
12184 {
12185 rel->r_offset += 4;
12186 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
12187 insn2 = NOP;
12188 }
12189 bfd_put_32 (output_bfd, insn2, contents + offset);
12190 rel--;
12191 continue;
12192 }
12193 break;
12194
411e1bfb 12195 case R_PPC64_DTPMOD64:
951fd09b
AM
12196 if (rel + 1 < relend
12197 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
12198 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 12199 {
951fd09b
AM
12200 if ((tls_mask & TLS_GD) == 0)
12201 {
12202 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
12203 if ((tls_mask & TLS_TPRELGD) != 0)
12204 r_type = R_PPC64_TPREL64;
12205 else
12206 {
4ce794b7 12207 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
12208 r_type = R_PPC64_NONE;
12209 }
12210 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12211 }
12212 }
12213 else
12214 {
12215 if ((tls_mask & TLS_LD) == 0)
411e1bfb 12216 {
4ce794b7 12217 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 12218 r_type = R_PPC64_NONE;
951fd09b 12219 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 12220 }
411e1bfb
AM
12221 }
12222 break;
12223
12224 case R_PPC64_TPREL64:
951fd09b 12225 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
12226 {
12227 r_type = R_PPC64_NONE;
12228 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12229 }
12230 break;
12231 }
12232
12233 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 12234 insn = 0;
b25116a9
AM
12235 max_br_offset = 1 << 25;
12236 addend = rel->r_addend;
65f38f15 12237 switch (r_type)
5bd4f169
AM
12238 {
12239 default:
65f38f15 12240 break;
5bd4f169 12241
65f38f15
AM
12242 /* Branch taken prediction relocations. */
12243 case R_PPC64_ADDR14_BRTAKEN:
12244 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
12245 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
12246 /* Fall thru. */
65f38f15 12247
86c76c7b 12248 /* Branch not taken prediction relocations. */
65f38f15
AM
12249 case R_PPC64_ADDR14_BRNTAKEN:
12250 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
12251 insn |= bfd_get_32 (output_bfd,
12252 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 12253 /* Fall thru. */
86c76c7b 12254
b25116a9
AM
12255 case R_PPC64_REL14:
12256 max_br_offset = 1 << 15;
12257 /* Fall thru. */
5bd4f169 12258
65f38f15 12259 case R_PPC64_REL24:
ad8e1ba5
AM
12260 /* Calls to functions with a different TOC, such as calls to
12261 shared objects, need to alter the TOC pointer. This is
12262 done using a linkage stub. A REL24 branching to these
12263 linkage stubs needs to be followed by a nop, as the nop
12264 will be replaced with an instruction to restore the TOC
12265 base pointer. */
8387904d 12266 fdh = h;
b31867b6
AM
12267 if (h != NULL
12268 && h->oh != NULL
12269 && h->oh->is_func_descriptor)
12270 fdh = ppc_follow_link (h->oh);
6abec6bc
AM
12271 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, rel, htab);
12272 if (stub_entry != NULL
ad8e1ba5
AM
12273 && (stub_entry->stub_type == ppc_stub_plt_call
12274 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
12275 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 12276 {
b25116a9 12277 bfd_boolean can_plt_call = FALSE;
721956f4 12278
eea6121a 12279 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 12280 {
b25116a9
AM
12281 unsigned long nop;
12282 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
12283 if (nop == NOP
12284 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 12285 {
a7f2871e
AM
12286 if (h != NULL
12287 && (h == htab->tls_get_addr_fd
12288 || h == htab->tls_get_addr)
12289 && !htab->no_tls_get_addr_opt)
12290 {
12291 /* Special stub used, leave nop alone. */
12292 }
12293 else
12294 bfd_put_32 (input_bfd, LD_R2_40R1,
12295 contents + rel->r_offset + 4);
b25116a9 12296 can_plt_call = TRUE;
41bd81ab 12297 }
5bd4f169 12298 }
721956f4
AM
12299
12300 if (!can_plt_call)
12301 {
ad8e1ba5
AM
12302 if (stub_entry->stub_type == ppc_stub_plt_call)
12303 {
12304 /* If this is a plain branch rather than a branch
4fa1c237
AM
12305 and link, don't require a nop. However, don't
12306 allow tail calls in a shared library as they
12307 will result in r2 being corrupted. */
b25116a9
AM
12308 unsigned long br;
12309 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 12310 if (info->executable && (br & 1) == 0)
b25116a9 12311 can_plt_call = TRUE;
15b552e9
AM
12312 else
12313 stub_entry = NULL;
ad8e1ba5 12314 }
6ab189d5 12315 else if (h != NULL
039b3fef 12316 && strcmp (h->elf.root.root.string,
6ab189d5
AM
12317 ".__libc_start_main") == 0)
12318 {
12319 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 12320 can_plt_call = TRUE;
6ab189d5 12321 }
ad8e1ba5
AM
12322 else
12323 {
12324 if (strcmp (input_section->output_section->name,
12325 ".init") == 0
12326 || strcmp (input_section->output_section->name,
12327 ".fini") == 0)
12328 (*_bfd_error_handler)
d003868e 12329 (_("%B(%A+0x%lx): automatic multiple TOCs "
ad8e1ba5
AM
12330 "not supported using your crt files; "
12331 "recompile with -mminimal-toc or upgrade gcc"),
d003868e
AM
12332 input_bfd,
12333 input_section,
ad8e1ba5
AM
12334 (long) rel->r_offset);
12335 else
12336 (*_bfd_error_handler)
d003868e 12337 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
ad8e1ba5
AM
12338 "does not allow automatic multiple TOCs; "
12339 "recompile with -mminimal-toc or "
12340 "-fno-optimize-sibling-calls, "
12341 "or make `%s' extern"),
d003868e
AM
12342 input_bfd,
12343 input_section,
ad8e1ba5
AM
12344 (long) rel->r_offset,
12345 sym_name,
12346 sym_name);
12347 bfd_set_error (bfd_error_bad_value);
12348 ret = FALSE;
12349 }
721956f4
AM
12350 }
12351
b25116a9
AM
12352 if (can_plt_call
12353 && stub_entry->stub_type == ppc_stub_plt_call)
12354 unresolved_reloc = FALSE;
12355 }
12356
6abec6bc
AM
12357 if ((stub_entry == NULL
12358 || stub_entry->stub_type == ppc_stub_long_branch
12359 || stub_entry->stub_type == ppc_stub_plt_branch)
8387904d
AM
12360 && get_opd_info (sec) != NULL)
12361 {
12362 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
12363 bfd_vma off = (relocation + addend
12364 - sec->output_section->vma
12365 - sec->output_offset);
8387904d
AM
12366 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
12367 if (dest != (bfd_vma) -1)
12368 {
12369 relocation = dest;
12370 addend = 0;
12371 }
12372 }
12373
b25116a9
AM
12374 /* If the branch is out of reach we ought to have a long
12375 branch stub. */
12376 from = (rel->r_offset
12377 + input_section->output_offset
12378 + input_section->output_section->vma);
12379
6abec6bc
AM
12380 if (stub_entry != NULL
12381 && (stub_entry->stub_type == ppc_stub_long_branch
12382 || stub_entry->stub_type == ppc_stub_plt_branch)
12383 && (r_type == R_PPC64_ADDR14_BRTAKEN
12384 || r_type == R_PPC64_ADDR14_BRNTAKEN
12385 || (relocation + addend - from + max_br_offset
12386 < 2 * max_br_offset)))
12387 /* Don't use the stub if this branch is in range. */
12388 stub_entry = NULL;
b25116a9
AM
12389
12390 if (stub_entry != NULL)
12391 {
12392 /* Munge up the value and addend so that we call the stub
12393 rather than the procedure directly. */
12394 relocation = (stub_entry->stub_offset
12395 + stub_entry->stub_sec->output_offset
12396 + stub_entry->stub_sec->output_section->vma);
12397 addend = 0;
12398 }
12399
12400 if (insn != 0)
12401 {
12402 if (is_power4)
721956f4 12403 {
b25116a9
AM
12404 /* Set 'a' bit. This is 0b00010 in BO field for branch
12405 on CR(BI) insns (BO == 001at or 011at), and 0b01000
12406 for branch on CTR insns (BO == 1a00t or 1a01t). */
12407 if ((insn & (0x14 << 21)) == (0x04 << 21))
12408 insn |= 0x02 << 21;
12409 else if ((insn & (0x14 << 21)) == (0x10 << 21))
12410 insn |= 0x08 << 21;
12411 else
12412 break;
12413 }
12414 else
12415 {
12416 /* Invert 'y' bit if not the default. */
4cc603a5 12417 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 12418 insn ^= 0x01 << 21;
721956f4 12419 }
b25116a9
AM
12420
12421 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 12422 }
e86ce104 12423
06da1e8e
AM
12424 /* NOP out calls to undefined weak functions.
12425 We can thus call a weak function without first
12426 checking whether the function is defined. */
b25116a9 12427 else if (h != NULL
039b3fef 12428 && h->elf.root.type == bfd_link_hash_undefweak
766bc656 12429 && h->elf.dynindx == -1
b25116a9
AM
12430 && r_type == R_PPC64_REL24
12431 && relocation == 0
4cc603a5 12432 && addend == 0)
e86ce104 12433 {
06da1e8e
AM
12434 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12435 continue;
e86ce104 12436 }
65f38f15
AM
12437 break;
12438 }
5bd4f169 12439
65f38f15 12440 /* Set `addend'. */
411e1bfb 12441 tls_type = 0;
65f38f15
AM
12442 switch (r_type)
12443 {
12444 default:
12445 (*_bfd_error_handler)
d003868e
AM
12446 (_("%B: unknown relocation type %d for symbol %s"),
12447 input_bfd, (int) r_type, sym_name);
5bd4f169 12448
65f38f15 12449 bfd_set_error (bfd_error_bad_value);
b34976b6 12450 ret = FALSE;
65f38f15 12451 continue;
5bd4f169 12452
65f38f15 12453 case R_PPC64_NONE:
411e1bfb 12454 case R_PPC64_TLS:
727fc41e
AM
12455 case R_PPC64_TLSGD:
12456 case R_PPC64_TLSLD:
04c9666a
AM
12457 case R_PPC64_GNU_VTINHERIT:
12458 case R_PPC64_GNU_VTENTRY:
65f38f15 12459 continue;
5bd4f169
AM
12460
12461 /* GOT16 relocations. Like an ADDR16 using the symbol's
12462 address in the GOT as relocation value instead of the
411e1bfb 12463 symbol's value itself. Also, create a GOT entry for the
5bd4f169 12464 symbol and put the symbol value there. */
411e1bfb
AM
12465 case R_PPC64_GOT_TLSGD16:
12466 case R_PPC64_GOT_TLSGD16_LO:
12467 case R_PPC64_GOT_TLSGD16_HI:
12468 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 12469 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
12470 goto dogot;
12471
12472 case R_PPC64_GOT_TLSLD16:
12473 case R_PPC64_GOT_TLSLD16_LO:
12474 case R_PPC64_GOT_TLSLD16_HI:
12475 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 12476 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
12477 goto dogot;
12478
12479 case R_PPC64_GOT_TPREL16_DS:
12480 case R_PPC64_GOT_TPREL16_LO_DS:
12481 case R_PPC64_GOT_TPREL16_HI:
12482 case R_PPC64_GOT_TPREL16_HA:
12483 tls_type = TLS_TLS | TLS_TPREL;
12484 goto dogot;
12485
12486 case R_PPC64_GOT_DTPREL16_DS:
12487 case R_PPC64_GOT_DTPREL16_LO_DS:
12488 case R_PPC64_GOT_DTPREL16_HI:
12489 case R_PPC64_GOT_DTPREL16_HA:
12490 tls_type = TLS_TLS | TLS_DTPREL;
12491 goto dogot;
12492
65f38f15
AM
12493 case R_PPC64_GOT16:
12494 case R_PPC64_GOT16_LO:
12495 case R_PPC64_GOT16_HI:
12496 case R_PPC64_GOT16_HA:
12497 case R_PPC64_GOT16_DS:
12498 case R_PPC64_GOT16_LO_DS:
411e1bfb 12499 dogot:
5bd4f169
AM
12500 {
12501 /* Relocation is to the entry for this symbol in the global
12502 offset table. */
e717da7e 12503 asection *got;
d881513a 12504 bfd_vma *offp;
5bd4f169 12505 bfd_vma off;
d881513a 12506 unsigned long indx = 0;
927be08e 12507 struct got_entry *ent;
65f38f15 12508
d881513a
AM
12509 if (tls_type == (TLS_TLS | TLS_LD)
12510 && (h == NULL
f5385ebf 12511 || !h->elf.def_dynamic))
927be08e 12512 ent = ppc64_tlsld_got (input_bfd);
411e1bfb 12513 else
5bd4f169 12514 {
5bd4f169 12515
d881513a
AM
12516 if (h != NULL)
12517 {
12518 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
12519 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
12520 &h->elf)
d881513a 12521 || (info->shared
726d3ab0 12522 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
d881513a
AM
12523 /* This is actually a static link, or it is a
12524 -Bsymbolic link and the symbol is defined
12525 locally, or the symbol was forced to be local
12526 because of a version file. */
12527 ;
12528 else
12529 {
039b3fef 12530 indx = h->elf.dynindx;
d881513a
AM
12531 unresolved_reloc = FALSE;
12532 }
039b3fef 12533 ent = h->elf.got.glist;
d881513a 12534 }
411e1bfb 12535 else
5bd4f169 12536 {
d881513a
AM
12537 if (local_got_ents == NULL)
12538 abort ();
12539 ent = local_got_ents[r_symndx];
5bd4f169 12540 }
d881513a
AM
12541
12542 for (; ent != NULL; ent = ent->next)
4cc603a5 12543 if (ent->addend == orig_addend
e717da7e 12544 && ent->owner == input_bfd
d881513a
AM
12545 && ent->tls_type == tls_type)
12546 break;
5bd4f169 12547 }
411e1bfb 12548
927be08e
AM
12549 if (ent == NULL)
12550 abort ();
12551 if (ent->is_indirect)
12552 ent = ent->got.ent;
12553 offp = &ent->got.offset;
12554 got = ppc64_elf_tdata (ent->owner)->got;
e717da7e
AM
12555 if (got == NULL)
12556 abort ();
12557
411e1bfb
AM
12558 /* The offset must always be a multiple of 8. We use the
12559 least significant bit to record whether we have already
12560 processed this entry. */
d881513a 12561 off = *offp;
411e1bfb
AM
12562 if ((off & 1) != 0)
12563 off &= ~1;
5bd4f169
AM
12564 else
12565 {
411e1bfb
AM
12566 /* Generate relocs for the dynamic linker, except in
12567 the case of TLSLD where we'll use one entry per
12568 module. */
25f23106
AM
12569 asection *relgot;
12570 bfd_boolean ifunc;
e717da7e 12571
d881513a 12572 *offp = off | 1;
25f23106
AM
12573 relgot = NULL;
12574 ifunc = (h != NULL
12575 ? h->elf.type == STT_GNU_IFUNC
12576 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
4e795f50 12577 if ((info->shared || indx != 0)
927be08e
AM
12578 && (h == NULL
12579 || (tls_type == (TLS_TLS | TLS_LD)
12580 && !h->elf.def_dynamic)
039b3fef
AM
12581 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12582 || h->elf.root.type != bfd_link_hash_undefweak))
927be08e 12583 relgot = ppc64_elf_tdata (ent->owner)->relgot;
25f23106
AM
12584 else if (ifunc)
12585 relgot = htab->reliplt;
12586 if (relgot != NULL)
5bd4f169 12587 {
e717da7e
AM
12588 outrel.r_offset = (got->output_section->vma
12589 + got->output_offset
411e1bfb 12590 + off);
4cc603a5 12591 outrel.r_addend = addend;
d881513a 12592 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 12593 {
411e1bfb 12594 outrel.r_addend = 0;
e515b051 12595 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
12596 if (tls_type == (TLS_TLS | TLS_GD))
12597 {
e717da7e
AM
12598 loc = relgot->contents;
12599 loc += (relgot->reloc_count++
d881513a
AM
12600 * sizeof (Elf64_External_Rela));
12601 bfd_elf64_swap_reloca_out (output_bfd,
12602 &outrel, loc);
e515b051 12603 outrel.r_offset += 8;
4cc603a5 12604 outrel.r_addend = addend;
d881513a
AM
12605 outrel.r_info
12606 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 12607 }
411e1bfb 12608 }
951fd09b 12609 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 12610 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 12611 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12612 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
25f23106
AM
12613 else if (indx != 0)
12614 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
12615 else
81407a69 12616 {
25f23106
AM
12617 if (ifunc)
12618 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12619 else
12620 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
12621
12622 /* Write the .got section contents for the sake
12623 of prelink. */
e717da7e 12624 loc = got->contents + off;
23fbd6fa
JJ
12625 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
12626 loc);
81407a69 12627 }
81407a69
AM
12628
12629 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
12630 {
12631 outrel.r_addend += relocation;
12632 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 12633 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 12634 }
e717da7e
AM
12635 loc = relgot->contents;
12636 loc += (relgot->reloc_count++
411e1bfb
AM
12637 * sizeof (Elf64_External_Rela));
12638 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
12639 }
12640
ad8e1ba5 12641 /* Init the .got section contents here if we're not
81407a69 12642 emitting a reloc. */
d881513a 12643 else
411e1bfb 12644 {
4cc603a5 12645 relocation += addend;
7b609f53
AM
12646 if (tls_type == (TLS_TLS | TLS_LD))
12647 relocation = 1;
12648 else if (tls_type != 0)
411e1bfb 12649 {
e1918d23 12650 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 12651 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 12652 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 12653
7b609f53
AM
12654 if (tls_type == (TLS_TLS | TLS_GD))
12655 {
12656 bfd_put_64 (output_bfd, relocation,
e717da7e 12657 got->contents + off + 8);
7b609f53
AM
12658 relocation = 1;
12659 }
411e1bfb 12660 }
7b609f53 12661
411e1bfb 12662 bfd_put_64 (output_bfd, relocation,
e717da7e 12663 got->contents + off);
5bd4f169
AM
12664 }
12665 }
12666
65f38f15
AM
12667 if (off >= (bfd_vma) -2)
12668 abort ();
12669
bf102f86
AM
12670 relocation = got->output_section->vma + got->output_offset + off;
12671 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
5bd4f169 12672 }
65f38f15
AM
12673 break;
12674
12675 case R_PPC64_PLT16_HA:
12676 case R_PPC64_PLT16_HI:
12677 case R_PPC64_PLT16_LO:
12678 case R_PPC64_PLT32:
12679 case R_PPC64_PLT64:
12680 /* Relocation is to the entry for this symbol in the
12681 procedure linkage table. */
12682
12683 /* Resolve a PLT reloc against a local symbol directly,
12684 without using the procedure linkage table. */
12685 if (h == NULL)
12686 break;
12687
411e1bfb
AM
12688 /* It's possible that we didn't make a PLT entry for this
12689 symbol. This happens when statically linking PIC code,
12690 or when using -Bsymbolic. Go find a match if there is a
12691 PLT entry. */
4ce794b7 12692 if (htab->plt != NULL)
65f38f15 12693 {
411e1bfb 12694 struct plt_entry *ent;
039b3fef 12695 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 12696 if (ent->addend == orig_addend
411e1bfb
AM
12697 && ent->plt.offset != (bfd_vma) -1)
12698 {
4ce794b7
AM
12699 relocation = (htab->plt->output_section->vma
12700 + htab->plt->output_offset
411e1bfb
AM
12701 + ent->plt.offset);
12702 unresolved_reloc = FALSE;
12703 }
65f38f15 12704 }
65f38f15 12705 break;
5bd4f169 12706
0b13192e
AM
12707 case R_PPC64_TOC:
12708 /* Relocation value is TOC base. */
12709 relocation = TOCstart;
cf35638d 12710 if (r_symndx == STN_UNDEF)
0b13192e 12711 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
12712 else if (unresolved_reloc)
12713 ;
12714 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
12715 relocation += htab->stub_group[sec->id].toc_off;
12716 else
12717 unresolved_reloc = TRUE;
ab96bf03 12718 goto dodyn;
0b13192e 12719
5bd4f169
AM
12720 /* TOC16 relocs. We want the offset relative to the TOC base,
12721 which is the address of the start of the TOC plus 0x8000.
12722 The TOC consists of sections .got, .toc, .tocbss, and .plt,
12723 in this order. */
65f38f15
AM
12724 case R_PPC64_TOC16:
12725 case R_PPC64_TOC16_LO:
12726 case R_PPC64_TOC16_HI:
12727 case R_PPC64_TOC16_DS:
12728 case R_PPC64_TOC16_LO_DS:
12729 case R_PPC64_TOC16_HA:
ad8e1ba5 12730 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
12731 break;
12732
12733 /* Relocate against the beginning of the section. */
65f38f15
AM
12734 case R_PPC64_SECTOFF:
12735 case R_PPC64_SECTOFF_LO:
12736 case R_PPC64_SECTOFF_HI:
12737 case R_PPC64_SECTOFF_DS:
12738 case R_PPC64_SECTOFF_LO_DS:
12739 case R_PPC64_SECTOFF_HA:
4ce794b7 12740 if (sec != NULL)
65f38f15 12741 addend -= sec->output_section->vma;
5bd4f169
AM
12742 break;
12743
25f23106
AM
12744 case R_PPC64_REL16:
12745 case R_PPC64_REL16_LO:
12746 case R_PPC64_REL16_HI:
12747 case R_PPC64_REL16_HA:
12748 break;
12749
721956f4
AM
12750 case R_PPC64_REL14:
12751 case R_PPC64_REL14_BRNTAKEN:
12752 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
12753 case R_PPC64_REL24:
12754 break;
12755
411e1bfb
AM
12756 case R_PPC64_TPREL16:
12757 case R_PPC64_TPREL16_LO:
12758 case R_PPC64_TPREL16_HI:
12759 case R_PPC64_TPREL16_HA:
12760 case R_PPC64_TPREL16_DS:
12761 case R_PPC64_TPREL16_LO_DS:
12762 case R_PPC64_TPREL16_HIGHER:
12763 case R_PPC64_TPREL16_HIGHERA:
12764 case R_PPC64_TPREL16_HIGHEST:
12765 case R_PPC64_TPREL16_HIGHESTA:
766bc656
AM
12766 if (h != NULL
12767 && h->elf.root.type == bfd_link_hash_undefweak
12768 && h->elf.dynindx == -1)
12769 {
12770 /* Make this relocation against an undefined weak symbol
12771 resolve to zero. This is really just a tweak, since
12772 code using weak externs ought to check that they are
12773 defined before using them. */
12774 bfd_byte *p = contents + rel->r_offset - d_offset;
12775
12776 insn = bfd_get_32 (output_bfd, p);
12777 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
12778 if (insn != 0)
12779 bfd_put_32 (output_bfd, insn, p);
12780 break;
12781 }
e1918d23 12782 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12783 if (info->shared)
12784 /* The TPREL16 relocs shouldn't really be used in shared
12785 libs as they will result in DT_TEXTREL being set, but
12786 support them anyway. */
12787 goto dodyn;
12788 break;
12789
12790 case R_PPC64_DTPREL16:
12791 case R_PPC64_DTPREL16_LO:
12792 case R_PPC64_DTPREL16_HI:
12793 case R_PPC64_DTPREL16_HA:
12794 case R_PPC64_DTPREL16_DS:
12795 case R_PPC64_DTPREL16_LO_DS:
12796 case R_PPC64_DTPREL16_HIGHER:
12797 case R_PPC64_DTPREL16_HIGHERA:
12798 case R_PPC64_DTPREL16_HIGHEST:
12799 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 12800 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12801 break;
12802
e515b051
AM
12803 case R_PPC64_DTPMOD64:
12804 relocation = 1;
12805 addend = 0;
12806 goto dodyn;
12807
411e1bfb 12808 case R_PPC64_TPREL64:
e1918d23 12809 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
12810 goto dodyn;
12811
12812 case R_PPC64_DTPREL64:
e1918d23 12813 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
12814 /* Fall thru */
12815
65f38f15
AM
12816 /* Relocations that may need to be propagated if this is a
12817 dynamic object. */
04c9666a 12818 case R_PPC64_REL30:
65f38f15
AM
12819 case R_PPC64_REL32:
12820 case R_PPC64_REL64:
12821 case R_PPC64_ADDR14:
12822 case R_PPC64_ADDR14_BRNTAKEN:
12823 case R_PPC64_ADDR14_BRTAKEN:
12824 case R_PPC64_ADDR16:
12825 case R_PPC64_ADDR16_DS:
12826 case R_PPC64_ADDR16_HA:
12827 case R_PPC64_ADDR16_HI:
12828 case R_PPC64_ADDR16_HIGHER:
12829 case R_PPC64_ADDR16_HIGHERA:
12830 case R_PPC64_ADDR16_HIGHEST:
12831 case R_PPC64_ADDR16_HIGHESTA:
12832 case R_PPC64_ADDR16_LO:
12833 case R_PPC64_ADDR16_LO_DS:
12834 case R_PPC64_ADDR24:
65f38f15
AM
12835 case R_PPC64_ADDR32:
12836 case R_PPC64_ADDR64:
12837 case R_PPC64_UADDR16:
12838 case R_PPC64_UADDR32:
12839 case R_PPC64_UADDR64:
411e1bfb 12840 dodyn:
5d1634d7 12841 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
12842 break;
12843
41bd81ab
AM
12844 if (NO_OPD_RELOCS && is_opd)
12845 break;
12846
65f38f15 12847 if ((info->shared
4e795f50 12848 && (h == NULL
039b3fef
AM
12849 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
12850 || h->elf.root.type != bfd_link_hash_undefweak)
1d483afe 12851 && (must_be_dyn_reloc (info, r_type)
039b3fef 12852 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
12853 || (ELIMINATE_COPY_RELOCS
12854 && !info->shared
65f38f15 12855 && h != NULL
039b3fef 12856 && h->elf.dynindx != -1
f5385ebf 12857 && !h->elf.non_got_ref
25f23106
AM
12858 && !h->elf.def_regular)
12859 || (!info->shared
12860 && (h != NULL
12861 ? h->elf.type == STT_GNU_IFUNC
12862 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
65f38f15 12863 {
b34976b6 12864 bfd_boolean skip, relocate;
65f38f15 12865 asection *sreloc;
1cf1f670 12866 bfd_vma out_off;
65f38f15
AM
12867
12868 /* When generating a dynamic object, these relocations
12869 are copied into the output file to be resolved at run
12870 time. */
12871
b34976b6
AM
12872 skip = FALSE;
12873 relocate = FALSE;
65f38f15 12874
1cf1f670
AM
12875 out_off = _bfd_elf_section_offset (output_bfd, info,
12876 input_section, rel->r_offset);
12877 if (out_off == (bfd_vma) -1)
b34976b6 12878 skip = TRUE;
1cf1f670 12879 else if (out_off == (bfd_vma) -2)
b34976b6 12880 skip = TRUE, relocate = TRUE;
1cf1f670
AM
12881 out_off += (input_section->output_section->vma
12882 + input_section->output_offset);
12883 outrel.r_offset = out_off;
411e1bfb 12884 outrel.r_addend = rel->r_addend;
65f38f15 12885
1cf1f670
AM
12886 /* Optimize unaligned reloc use. */
12887 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
12888 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
12889 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
12890 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
12891 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
12892 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
12893 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
12894 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
12895 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
12896
65f38f15 12897 if (skip)
0bb2d96a 12898 memset (&outrel, 0, sizeof outrel);
726d3ab0 12899 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
0b13192e
AM
12900 && !is_opd
12901 && r_type != R_PPC64_TOC)
039b3fef 12902 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
12903 else
12904 {
41bd81ab
AM
12905 /* This symbol is local, or marked to become local,
12906 or this is an opd section reloc which must point
12907 at a local function. */
65f38f15 12908 outrel.r_addend += relocation;
e86ce104 12909 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 12910 {
3fad3c7c 12911 if (is_opd && h != NULL)
afbe61cf
AM
12912 {
12913 /* Lie about opd entries. This case occurs
12914 when building shared libraries and we
12915 reference a function in another shared
3fad3c7c
AM
12916 lib. The same thing happens for a weak
12917 definition in an application that's
12918 overridden by a strong definition in a
12919 shared lib. (I believe this is a generic
12920 bug in binutils handling of weak syms.)
12921 In these cases we won't use the opd
1e2f5b6e 12922 entry in this lib. */
b34976b6 12923 unresolved_reloc = FALSE;
afbe61cf 12924 }
25f23106
AM
12925 if (!is_opd
12926 && r_type == R_PPC64_ADDR64
12927 && (h != NULL
12928 ? h->elf.type == STT_GNU_IFUNC
12929 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
12930 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
12931 else
12932 {
12933 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69 12934
25f23106
AM
12935 /* We need to relocate .opd contents for ld.so.
12936 Prelink also wants simple and consistent rules
12937 for relocs. This make all RELATIVE relocs have
12938 *r_offset equal to r_addend. */
12939 relocate = TRUE;
12940 }
65f38f15
AM
12941 }
12942 else
12943 {
12944 long indx = 0;
12945
25f23106
AM
12946 if (h != NULL
12947 ? h->elf.type == STT_GNU_IFUNC
12948 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
12949 {
12950 (*_bfd_error_handler)
12951 (_("%B(%A+0x%lx): relocation %s for indirect "
12952 "function %s unsupported"),
12953 input_bfd,
12954 input_section,
12955 (long) rel->r_offset,
12956 ppc64_elf_howto_table[r_type]->name,
12957 sym_name);
12958 ret = FALSE;
12959 }
cf35638d 12960 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
65f38f15
AM
12961 ;
12962 else if (sec == NULL || sec->owner == NULL)
12963 {
12964 bfd_set_error (bfd_error_bad_value);
b34976b6 12965 return FALSE;
65f38f15
AM
12966 }
12967 else
12968 {
12969 asection *osec;
12970
12971 osec = sec->output_section;
12972 indx = elf_section_data (osec)->dynindx;
12973
74541ad4
AM
12974 if (indx == 0)
12975 {
12976 if ((osec->flags & SEC_READONLY) == 0
12977 && htab->elf.data_index_section != NULL)
12978 osec = htab->elf.data_index_section;
12979 else
12980 osec = htab->elf.text_index_section;
12981 indx = elf_section_data (osec)->dynindx;
12982 }
12983 BFD_ASSERT (indx != 0);
12984
65f38f15
AM
12985 /* We are turning this relocation into one
12986 against a section symbol, so subtract out
12987 the output section's address but not the
12988 offset of the input section in the output
12989 section. */
12990 outrel.r_addend -= osec->vma;
12991 }
12992
12993 outrel.r_info = ELF64_R_INFO (indx, r_type);
12994 }
12995 }
12996
12997 sreloc = elf_section_data (input_section)->sreloc;
25f23106
AM
12998 if (!htab->elf.dynamic_sections_created)
12999 sreloc = htab->reliplt;
65f38f15
AM
13000 if (sreloc == NULL)
13001 abort ();
13002
dfbb6ac9
AM
13003 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
13004 >= sreloc->size)
13005 abort ();
947216bf
AM
13006 loc = sreloc->contents;
13007 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
13008 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13009
13010 /* If this reloc is against an external symbol, it will
13011 be computed at runtime, so there's no need to do
81407a69
AM
13012 anything now. However, for the sake of prelink ensure
13013 that the section contents are a known value. */
65f38f15 13014 if (! relocate)
81407a69
AM
13015 {
13016 unresolved_reloc = FALSE;
13017 /* The value chosen here is quite arbitrary as ld.so
13018 ignores section contents except for the special
13019 case of .opd where the contents might be accessed
13020 before relocation. Choose zero, as that won't
13021 cause reloc overflow. */
13022 relocation = 0;
13023 addend = 0;
13024 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
13025 to improve backward compatibility with older
13026 versions of ld. */
13027 if (r_type == R_PPC64_ADDR64)
13028 addend = outrel.r_addend;
13029 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 13030 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
13031 addend = (input_section->output_section->vma
13032 + input_section->output_offset
13033 + rel->r_offset);
81407a69 13034 }
65f38f15 13035 }
5bd4f169
AM
13036 break;
13037
65f38f15
AM
13038 case R_PPC64_COPY:
13039 case R_PPC64_GLOB_DAT:
13040 case R_PPC64_JMP_SLOT:
25f23106 13041 case R_PPC64_JMP_IREL:
65f38f15
AM
13042 case R_PPC64_RELATIVE:
13043 /* We shouldn't ever see these dynamic relocs in relocatable
13044 files. */
ae9a127f 13045 /* Fall through. */
65f38f15
AM
13046
13047 case R_PPC64_PLTGOT16:
13048 case R_PPC64_PLTGOT16_DS:
13049 case R_PPC64_PLTGOT16_HA:
13050 case R_PPC64_PLTGOT16_HI:
13051 case R_PPC64_PLTGOT16_LO:
13052 case R_PPC64_PLTGOT16_LO_DS:
13053 case R_PPC64_PLTREL32:
13054 case R_PPC64_PLTREL64:
13055 /* These ones haven't been implemented yet. */
13056
13057 (*_bfd_error_handler)
d003868e
AM
13058 (_("%B: relocation %s is not supported for symbol %s."),
13059 input_bfd,
4ce794b7 13060 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
13061
13062 bfd_set_error (bfd_error_invalid_operation);
b34976b6 13063 ret = FALSE;
5bd4f169 13064 continue;
65f38f15 13065 }
5bd4f169 13066
67f0cbdb
AM
13067 /* Multi-instruction sequences that access the TOC can be
13068 optimized, eg. addis ra,r2,0; addi rb,ra,x;
13069 to nop; addi rb,r2,x; */
13070 switch (r_type)
13071 {
13072 default:
13073 break;
13074
13075 case R_PPC64_GOT_TLSLD16_HI:
13076 case R_PPC64_GOT_TLSGD16_HI:
13077 case R_PPC64_GOT_TPREL16_HI:
13078 case R_PPC64_GOT_DTPREL16_HI:
13079 case R_PPC64_GOT16_HI:
13080 case R_PPC64_TOC16_HI:
13081 /* These relocs would only be useful if building up an
13082 offset to later add to r2, perhaps in an indexed
13083 addressing mode instruction. Don't try to optimize.
13084 Unfortunately, the possibility of someone building up an
13085 offset like this or even with the HA relocs, means that
13086 we need to check the high insn when optimizing the low
13087 insn. */
13088 break;
13089
13090 case R_PPC64_GOT_TLSLD16_HA:
13091 case R_PPC64_GOT_TLSGD16_HA:
13092 case R_PPC64_GOT_TPREL16_HA:
13093 case R_PPC64_GOT_DTPREL16_HA:
13094 case R_PPC64_GOT16_HA:
13095 case R_PPC64_TOC16_HA:
d983b7a0 13096 /* nop is done later. */
67f0cbdb
AM
13097 break;
13098
13099 case R_PPC64_GOT_TLSLD16_LO:
13100 case R_PPC64_GOT_TLSGD16_LO:
13101 case R_PPC64_GOT_TPREL16_LO_DS:
13102 case R_PPC64_GOT_DTPREL16_LO_DS:
13103 case R_PPC64_GOT16_LO:
13104 case R_PPC64_GOT16_LO_DS:
13105 case R_PPC64_TOC16_LO:
13106 case R_PPC64_TOC16_LO_DS:
13107 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000)
13108 {
13109 bfd_byte *p = contents + (rel->r_offset & ~3);
13110 insn = bfd_get_32 (input_bfd, p);
13111 if ((insn & (0x3f << 26)) == 14u << 26 /* addi */
13112 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
13113 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
13114 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
13115 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
13116 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
13117 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
13118 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
13119 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
13120 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
13121 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
13122 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
13123 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
13124 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
13125 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
13126 && (insn & 3) != 1)
13127 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
13128 && ((insn & 3) == 0 || (insn & 3) == 3)))
13129 {
13130 unsigned int reg = (insn >> 16) & 0x1f;
d983b7a0
AM
13131 const Elf_Internal_Rela *ha;
13132 bfd_boolean match_addend;
13133
13134 match_addend = (sym != NULL
13135 && ELF_ST_TYPE (sym->st_info) == STT_SECTION);
13136 ha = ha_reloc_match (relocs, rel, &reg, match_addend,
13137 input_bfd, contents);
13138 if (ha != NULL)
67f0cbdb
AM
13139 {
13140 insn &= ~(0x1f << 16);
d983b7a0 13141 insn |= reg << 16;
67f0cbdb 13142 bfd_put_32 (input_bfd, insn, p);
d983b7a0
AM
13143 if (ha_opt == NULL)
13144 {
13145 ha_opt = bfd_zmalloc (input_section->reloc_count);
13146 if (ha_opt == NULL)
13147 return FALSE;
13148 }
13149 ha_opt[ha - relocs] = 1;
67f0cbdb 13150 }
d983b7a0
AM
13151 else
13152 /* If we don't find a matching high part insn,
13153 something is fishy. Refuse to nop any high
13154 part insn in this section. */
13155 no_ha_opt = TRUE;
67f0cbdb
AM
13156 }
13157 }
13158 break;
13159 }
13160
65f38f15
AM
13161 /* Do any further special processing. */
13162 switch (r_type)
13163 {
13164 default:
13165 break;
13166
13167 case R_PPC64_ADDR16_HA:
25f23106 13168 case R_PPC64_REL16_HA:
65f38f15
AM
13169 case R_PPC64_ADDR16_HIGHERA:
13170 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
13171 case R_PPC64_TOC16_HA:
13172 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
13173 case R_PPC64_TPREL16_HA:
13174 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
13175 case R_PPC64_TPREL16_HIGHER:
13176 case R_PPC64_TPREL16_HIGHERA:
13177 case R_PPC64_TPREL16_HIGHEST:
13178 case R_PPC64_TPREL16_HIGHESTA:
13179 case R_PPC64_DTPREL16_HIGHER:
13180 case R_PPC64_DTPREL16_HIGHERA:
13181 case R_PPC64_DTPREL16_HIGHEST:
13182 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
13183 /* It's just possible that this symbol is a weak symbol
13184 that's not actually defined anywhere. In that case,
13185 'sec' would be NULL, and we should leave the symbol
13186 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
13187 if (sec == NULL)
13188 break;
13189 /* Fall thru */
13190
13191 case R_PPC64_GOT16_HA:
13192 case R_PPC64_PLTGOT16_HA:
13193 case R_PPC64_PLT16_HA:
13194 case R_PPC64_GOT_TLSGD16_HA:
13195 case R_PPC64_GOT_TLSLD16_HA:
13196 case R_PPC64_GOT_TPREL16_HA:
13197 case R_PPC64_GOT_DTPREL16_HA:
13198 /* Add 0x10000 if sign bit in 0:15 is set.
13199 Bits 0:15 are not used. */
13200 addend += 0x8000;
65f38f15
AM
13201 break;
13202
13203 case R_PPC64_ADDR16_DS:
13204 case R_PPC64_ADDR16_LO_DS:
13205 case R_PPC64_GOT16_DS:
13206 case R_PPC64_GOT16_LO_DS:
13207 case R_PPC64_PLT16_LO_DS:
13208 case R_PPC64_SECTOFF_DS:
13209 case R_PPC64_SECTOFF_LO_DS:
13210 case R_PPC64_TOC16_DS:
13211 case R_PPC64_TOC16_LO_DS:
13212 case R_PPC64_PLTGOT16_DS:
13213 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
13214 case R_PPC64_GOT_TPREL16_DS:
13215 case R_PPC64_GOT_TPREL16_LO_DS:
13216 case R_PPC64_GOT_DTPREL16_DS:
13217 case R_PPC64_GOT_DTPREL16_LO_DS:
13218 case R_PPC64_TPREL16_DS:
13219 case R_PPC64_TPREL16_LO_DS:
13220 case R_PPC64_DTPREL16_DS:
13221 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
13222 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
13223 mask = 3;
13224 /* If this reloc is against an lq insn, then the value must be
13225 a multiple of 16. This is somewhat of a hack, but the
13226 "correct" way to do this by defining _DQ forms of all the
13227 _DS relocs bloats all reloc switches in this file. It
13228 doesn't seem to make much sense to use any of these relocs
13229 in data, so testing the insn should be safe. */
494dac0c 13230 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
13231 mask = 15;
13232 if (((relocation + addend) & mask) != 0)
65f38f15
AM
13233 {
13234 (*_bfd_error_handler)
e11840f9
AM
13235 (_("%B(%A+0x%lx): error: %s not a multiple of %u"),
13236 input_bfd, input_section, (long) rel->r_offset,
4ce794b7 13237 ppc64_elf_howto_table[r_type]->name,
adadcc0c 13238 mask + 1);
65f38f15 13239 bfd_set_error (bfd_error_bad_value);
b34976b6 13240 ret = FALSE;
65f38f15
AM
13241 continue;
13242 }
13243 break;
5bd4f169
AM
13244 }
13245
239e1f3a
AM
13246 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
13247 because such sections are not SEC_ALLOC and thus ld.so will
13248 not process them. */
65f38f15 13249 if (unresolved_reloc
239e1f3a 13250 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 13251 && h->elf.def_dynamic))
9c07fe7c
AM
13252 {
13253 (*_bfd_error_handler)
d003868e
AM
13254 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
13255 input_bfd,
13256 input_section,
9c07fe7c 13257 (long) rel->r_offset,
7b609f53 13258 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 13259 h->elf.root.root.string);
b34976b6 13260 ret = FALSE;
9c07fe7c 13261 }
5bd4f169 13262
65f38f15 13263 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
13264 input_bfd,
13265 input_section,
13266 contents,
411e1bfb 13267 rel->r_offset,
5bd4f169
AM
13268 relocation,
13269 addend);
13270
ef60b7ff 13271 if (r != bfd_reloc_ok)
5bd4f169 13272 {
cd27b276
AM
13273 if (sym_name == NULL)
13274 sym_name = "(null)";
13275 if (r == bfd_reloc_overflow)
5bd4f169 13276 {
cd27b276
AM
13277 if (warned)
13278 continue;
13279 if (h != NULL
039b3fef 13280 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 13281 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
13282 {
13283 /* Assume this is a call protected by other code that
13284 detects the symbol is undefined. If this is the case,
13285 we can safely ignore the overflow. If not, the
13286 program is hosed anyway, and a little warning isn't
13287 going to help. */
13288
13289 continue;
13290 }
13291
ef60b7ff 13292 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
13293 (info, (h ? &h->elf.root : NULL), sym_name,
13294 ppc64_elf_howto_table[r_type]->name,
4cc603a5 13295 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 13296 return FALSE;
ef60b7ff
AM
13297 }
13298 else
13299 {
13300 (*_bfd_error_handler)
d003868e
AM
13301 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
13302 input_bfd,
13303 input_section,
7b609f53 13304 (long) rel->r_offset,
4ce794b7 13305 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
13306 sym_name,
13307 (int) r);
b34976b6 13308 ret = FALSE;
ef60b7ff 13309 }
5bd4f169 13310 }
5bd4f169
AM
13311 }
13312
d983b7a0
AM
13313 if (ha_opt != NULL)
13314 {
13315 if (!no_ha_opt)
13316 {
13317 unsigned char *opt = ha_opt;
13318 rel = relocs;
13319 relend = relocs + input_section->reloc_count;
13320 for (; rel < relend; opt++, rel++)
13321 if (*opt != 0)
13322 {
13323 bfd_byte *p = contents + (rel->r_offset & ~3);
13324 bfd_put_32 (input_bfd, NOP, p);
13325 }
13326 }
13327 free (ha_opt);
13328 }
13329
645ea6a9
AM
13330 /* If we're emitting relocations, then shortly after this function
13331 returns, reloc offsets and addends for this section will be
13332 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
13333 file rather than the input. Save a copy of the relocs for
13334 opd_entry_value. */
4cc603a5 13335 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
13336 {
13337 bfd_size_type amt;
13338 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
13339 rel = bfd_alloc (input_bfd, amt);
13340 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
13341 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
13342 if (rel == NULL)
13343 return FALSE;
13344 memcpy (rel, relocs, amt);
13345 }
5bd4f169
AM
13346 return ret;
13347}
13348
754021d0
AM
13349/* Adjust the value of any local symbols in opd sections. */
13350
6e0b88f1 13351static int
754021d0
AM
13352ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
13353 const char *name ATTRIBUTE_UNUSED,
13354 Elf_Internal_Sym *elfsym,
13355 asection *input_sec,
13356 struct elf_link_hash_entry *h)
13357{
74f0fb50
AM
13358 struct _opd_sec_data *opd;
13359 long adjust;
754021d0
AM
13360 bfd_vma value;
13361
4025353c 13362 if (h != NULL)
6e0b88f1 13363 return 1;
4025353c 13364
74f0fb50
AM
13365 opd = get_opd_info (input_sec);
13366 if (opd == NULL || opd->adjust == NULL)
6e0b88f1 13367 return 1;
754021d0
AM
13368
13369 value = elfsym->st_value - input_sec->output_offset;
13370 if (!info->relocatable)
13371 value -= input_sec->output_section->vma;
13372
74f0fb50 13373 adjust = opd->adjust[value / 8];
4025353c 13374 if (adjust == -1)
6e0b88f1
AM
13375 return 2;
13376
13377 elfsym->st_value += adjust;
13378 return 1;
754021d0
AM
13379}
13380
5bd4f169
AM
13381/* Finish up dynamic symbol handling. We set the contents of various
13382 dynamic sections here. */
13383
b34976b6 13384static bfd_boolean
4ce794b7
AM
13385ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
13386 struct bfd_link_info *info,
13387 struct elf_link_hash_entry *h,
13388 Elf_Internal_Sym *sym)
5bd4f169 13389{
65f38f15 13390 struct ppc_link_hash_table *htab;
8387904d
AM
13391 struct plt_entry *ent;
13392 Elf_Internal_Rela rela;
13393 bfd_byte *loc;
5bd4f169 13394
65f38f15 13395 htab = ppc_hash_table (info);
4dfe6ac6
NC
13396 if (htab == NULL)
13397 return FALSE;
5bd4f169 13398
8387904d
AM
13399 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13400 if (ent->plt.offset != (bfd_vma) -1)
13401 {
13402 /* This symbol has an entry in the procedure linkage
13403 table. Set it up. */
e054468f
AM
13404 if (!htab->elf.dynamic_sections_created
13405 || h->dynindx == -1)
13406 {
13407 BFD_ASSERT (h->type == STT_GNU_IFUNC
13408 && h->def_regular
13409 && (h->root.type == bfd_link_hash_defined
13410 || h->root.type == bfd_link_hash_defweak));
25f23106
AM
13411 rela.r_offset = (htab->iplt->output_section->vma
13412 + htab->iplt->output_offset
13413 + ent->plt.offset);
13414 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
e054468f
AM
13415 rela.r_addend = (h->root.u.def.value
13416 + h->root.u.def.section->output_offset
13417 + h->root.u.def.section->output_section->vma
13418 + ent->addend);
25f23106
AM
13419 loc = (htab->reliplt->contents
13420 + (htab->reliplt->reloc_count++
13421 * sizeof (Elf64_External_Rela)));
e054468f
AM
13422 }
13423 else
13424 {
25f23106
AM
13425 rela.r_offset = (htab->plt->output_section->vma
13426 + htab->plt->output_offset
13427 + ent->plt.offset);
e054468f
AM
13428 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13429 rela.r_addend = ent->addend;
25f23106
AM
13430 loc = (htab->relplt->contents
13431 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
13432 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
e054468f 13433 }
8387904d
AM
13434 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
13435 }
5bd4f169 13436
f5385ebf 13437 if (h->needs_copy)
5bd4f169 13438 {
65f38f15 13439 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 13440
65f38f15
AM
13441 if (h->dynindx == -1
13442 || (h->root.type != bfd_link_hash_defined
13443 && h->root.type != bfd_link_hash_defweak)
4ce794b7 13444 || htab->relbss == NULL)
65f38f15 13445 abort ();
5bd4f169
AM
13446
13447 rela.r_offset = (h->root.u.def.value
13448 + h->root.u.def.section->output_section->vma
13449 + h->root.u.def.section->output_offset);
13450 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
13451 rela.r_addend = 0;
4ce794b7
AM
13452 loc = htab->relbss->contents;
13453 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 13454 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
13455 }
13456
5bd4f169
AM
13457 /* Mark some specially defined symbols as absolute. */
13458 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
13459 sym->st_shndx = SHN_ABS;
13460
b34976b6 13461 return TRUE;
5bd4f169
AM
13462}
13463
65f38f15
AM
13464/* Used to decide how to sort relocs in an optimal manner for the
13465 dynamic linker, before writing them out. */
13466
13467static enum elf_reloc_type_class
4ce794b7 13468ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 13469{
04c9666a 13470 enum elf_ppc64_reloc_type r_type;
a33d1f77 13471
4ce794b7 13472 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 13473 switch (r_type)
65f38f15
AM
13474 {
13475 case R_PPC64_RELATIVE:
13476 return reloc_class_relative;
13477 case R_PPC64_JMP_SLOT:
13478 return reloc_class_plt;
13479 case R_PPC64_COPY:
13480 return reloc_class_copy;
13481 default:
13482 return reloc_class_normal;
13483 }
13484}
13485
5bd4f169
AM
13486/* Finish up the dynamic sections. */
13487
b34976b6 13488static bfd_boolean
4ce794b7
AM
13489ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
13490 struct bfd_link_info *info)
5bd4f169 13491{
65f38f15
AM
13492 struct ppc_link_hash_table *htab;
13493 bfd *dynobj;
5bd4f169 13494 asection *sdyn;
5bd4f169 13495
65f38f15 13496 htab = ppc_hash_table (info);
4dfe6ac6
NC
13497 if (htab == NULL)
13498 return FALSE;
13499
65f38f15 13500 dynobj = htab->elf.dynobj;
5bd4f169
AM
13501 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
13502
65f38f15 13503 if (htab->elf.dynamic_sections_created)
5bd4f169 13504 {
5bd4f169
AM
13505 Elf64_External_Dyn *dyncon, *dynconend;
13506
4ce794b7 13507 if (sdyn == NULL || htab->got == NULL)
65f38f15 13508 abort ();
5bd4f169
AM
13509
13510 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 13511 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
13512 for (; dyncon < dynconend; dyncon++)
13513 {
13514 Elf_Internal_Dyn dyn;
19397422 13515 asection *s;
5bd4f169
AM
13516
13517 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
13518
13519 switch (dyn.d_tag)
13520 {
65f38f15
AM
13521 default:
13522 continue;
5bd4f169 13523
5d1634d7 13524 case DT_PPC64_GLINK:
4ce794b7 13525 s = htab->glink;
6348e046 13526 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
13527 /* We stupidly defined DT_PPC64_GLINK to be the start
13528 of glink rather than the first entry point, which is
13529 what ld.so needs, and now have a bigger stub to
13530 support automatic multiple TOCs. */
13531 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
13532 break;
13533
19397422
AM
13534 case DT_PPC64_OPD:
13535 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13536 if (s == NULL)
13537 continue;
13538 dyn.d_un.d_ptr = s->vma;
19397422
AM
13539 break;
13540
13541 case DT_PPC64_OPDSZ:
13542 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
13543 if (s == NULL)
13544 continue;
eea6121a 13545 dyn.d_un.d_val = s->size;
19397422
AM
13546 break;
13547
65f38f15 13548 case DT_PLTGOT:
4ce794b7 13549 s = htab->plt;
6348e046 13550 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
13551 break;
13552
13553 case DT_JMPREL:
4ce794b7 13554 s = htab->relplt;
6348e046 13555 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 13556 break;
5bd4f169 13557
65f38f15 13558 case DT_PLTRELSZ:
eea6121a 13559 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
13560 break;
13561
13562 case DT_RELASZ:
13563 /* Don't count procedure linkage table relocs in the
13564 overall reloc count. */
4ce794b7 13565 s = htab->relplt;
6348e046
AM
13566 if (s == NULL)
13567 continue;
eea6121a 13568 dyn.d_un.d_val -= s->size;
6348e046
AM
13569 break;
13570
13571 case DT_RELA:
13572 /* We may not be using the standard ELF linker script.
13573 If .rela.plt is the first .rela section, we adjust
13574 DT_RELA to not include it. */
4ce794b7 13575 s = htab->relplt;
6348e046
AM
13576 if (s == NULL)
13577 continue;
13578 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
13579 continue;
eea6121a 13580 dyn.d_un.d_ptr += s->size;
65f38f15 13581 break;
5bd4f169 13582 }
5bd4f169 13583
65f38f15 13584 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 13585 }
5bd4f169
AM
13586 }
13587
eea6121a 13588 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
13589 {
13590 /* Fill in the first entry in the global offset table.
13591 We use it to hold the link-time TOCbase. */
13592 bfd_put_64 (output_bfd,
60ee0d4a 13593 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 13594 htab->got->contents);
5d1634d7
AM
13595
13596 /* Set .got entry size. */
4ce794b7 13597 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
13598 }
13599
eea6121a 13600 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
13601 {
13602 /* Set .plt entry size. */
4ce794b7 13603 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
13604 = PLT_ENTRY_SIZE;
13605 }
13606
84f5d08e
AM
13607 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
13608 brlt ourselves if emitrelocations. */
13609 if (htab->brlt != NULL
13610 && htab->brlt->reloc_count != 0
13611 && !_bfd_elf_link_output_relocs (output_bfd,
13612 htab->brlt,
d4730f92 13613 elf_section_data (htab->brlt)->rela.hdr,
84f5d08e
AM
13614 elf_section_data (htab->brlt)->relocs,
13615 NULL))
13616 return FALSE;
13617
176a0d42
AM
13618 if (htab->glink != NULL
13619 && htab->glink->reloc_count != 0
13620 && !_bfd_elf_link_output_relocs (output_bfd,
13621 htab->glink,
d4730f92 13622 elf_section_data (htab->glink)->rela.hdr,
176a0d42
AM
13623 elf_section_data (htab->glink)->relocs,
13624 NULL))
13625 return FALSE;
13626
e717da7e 13627 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
13628 since we didn't add them to DYNOBJ. We know dynobj is the first
13629 bfd. */
e717da7e
AM
13630 while ((dynobj = dynobj->link_next) != NULL)
13631 {
13632 asection *s;
7b53ace3 13633
0c8d6e5c 13634 if (!is_ppc64_elf (dynobj))
7b53ace3
AM
13635 continue;
13636
e717da7e
AM
13637 s = ppc64_elf_tdata (dynobj)->got;
13638 if (s != NULL
eea6121a 13639 && s->size != 0
e717da7e
AM
13640 && s->output_section != bfd_abs_section_ptr
13641 && !bfd_set_section_contents (output_bfd, s->output_section,
13642 s->contents, s->output_offset,
eea6121a 13643 s->size))
e717da7e
AM
13644 return FALSE;
13645 s = ppc64_elf_tdata (dynobj)->relgot;
13646 if (s != NULL
eea6121a 13647 && s->size != 0
e717da7e
AM
13648 && s->output_section != bfd_abs_section_ptr
13649 && !bfd_set_section_contents (output_bfd, s->output_section,
13650 s->contents, s->output_offset,
eea6121a 13651 s->size))
e717da7e
AM
13652 return FALSE;
13653 }
f6c52c13 13654
b34976b6 13655 return TRUE;
5bd4f169
AM
13656}
13657
5bd4f169 13658#include "elf64-target.h"
This page took 1.698542 seconds and 4 git commands to generate.