* ld-scripts/default-script.exp: Add "--local-store 0:0" to
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
CommitLineData
5bd4f169 1/* PowerPC64-specific support for 64-bit ELF.
bce50a28 2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
b2a8e766 3 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.
d37c89e5 6 Largely rewritten by Alan Modra <amodra@bigpond.net.au>
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
12 the Free Software Foundation; either version 2 of the License, or
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
4ce794b7
AM
24/* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
5bd4f169
AM
27
28#include "bfd.h"
29#include "sysdep.h"
30#include "bfdlink.h"
31#include "libbfd.h"
32#include "elf-bfd.h"
04c9666a 33#include "elf/ppc64.h"
5d1634d7 34#include "elf64-ppc.h"
5bd4f169 35
805fc799 36static bfd_reloc_status_type ppc64_elf_ha_reloc
4ce794b7 37 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
38static bfd_reloc_status_type ppc64_elf_branch_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 40static bfd_reloc_status_type ppc64_elf_brtaken_reloc
4ce794b7 41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 42static bfd_reloc_status_type ppc64_elf_sectoff_reloc
4ce794b7 43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 44static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
4ce794b7 45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 46static bfd_reloc_status_type ppc64_elf_toc_reloc
4ce794b7 47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 48static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
4ce794b7 49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 50static bfd_reloc_status_type ppc64_elf_toc64_reloc
4ce794b7 51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
805fc799 52static bfd_reloc_status_type ppc64_elf_unhandled_reloc
4ce794b7 53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
2441e016
AM
54static bfd_vma opd_entry_value
55 (asection *, bfd_vma, asection **, bfd_vma *);
5bd4f169 56
ad8e1ba5
AM
57#define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
58#define TARGET_LITTLE_NAME "elf64-powerpcle"
59#define TARGET_BIG_SYM bfd_elf64_powerpc_vec
60#define TARGET_BIG_NAME "elf64-powerpc"
61#define ELF_ARCH bfd_arch_powerpc
62#define ELF_MACHINE_CODE EM_PPC64
63#define ELF_MAXPAGESIZE 0x10000
24718e3b 64#define ELF_COMMONPAGESIZE 0x1000
ad8e1ba5
AM
65#define elf_info_to_howto ppc64_elf_info_to_howto
66
67#define elf_backend_want_got_sym 0
68#define elf_backend_want_plt_sym 0
69#define elf_backend_plt_alignment 3
70#define elf_backend_plt_not_loaded 1
ad8e1ba5 71#define elf_backend_got_header_size 8
ad8e1ba5
AM
72#define elf_backend_can_gc_sections 1
73#define elf_backend_can_refcount 1
74#define elf_backend_rela_normal 1
75
e717da7e 76#define bfd_elf64_mkobject ppc64_elf_mkobject
ad8e1ba5
AM
77#define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
78#define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
79#define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
80#define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
81#define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
90e3cdf2 82#define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
ad8e1ba5
AM
83
84#define elf_backend_object_p ppc64_elf_object_p
d37c89e5
AM
85#define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
86#define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
ad8e1ba5
AM
87#define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
88#define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
555cd476 89#define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
8387904d 90#define elf_backend_check_directives ppc64_elf_check_directives
97fed1c9 91#define elf_backend_as_needed_cleanup ppc64_elf_as_needed_cleanup
8387904d 92#define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
ad8e1ba5 93#define elf_backend_check_relocs ppc64_elf_check_relocs
64d03ab5 94#define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
ad8e1ba5
AM
95#define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
96#define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
97#define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
98#define elf_backend_hide_symbol ppc64_elf_hide_symbol
99#define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
100#define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
74541ad4 101#define elf_backend_init_index_section _bfd_elf_init_2_index_sections
60124e18 102#define elf_backend_action_discarded ppc64_elf_action_discarded
ad8e1ba5
AM
103#define elf_backend_relocate_section ppc64_elf_relocate_section
104#define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
105#define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
106#define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
754021d0 107#define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
29ef7005 108#define elf_backend_special_sections ppc64_elf_special_sections
ad8e1ba5 109
5bd4f169
AM
110/* The name of the dynamic interpreter. This is put in the .interp
111 section. */
112#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
113
114/* The size in bytes of an entry in the procedure linkage table. */
115#define PLT_ENTRY_SIZE 24
116
117/* The initial size of the plt reserved for the dynamic linker. */
5d1634d7 118#define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
5bd4f169
AM
119
120/* TOC base pointers offset from start of TOC. */
411e1bfb
AM
121#define TOC_BASE_OFF 0x8000
122
123/* Offset of tp and dtp pointers from start of TLS block. */
124#define TP_OFFSET 0x7000
125#define DTP_OFFSET 0x8000
5bd4f169 126
ad8e1ba5
AM
127/* .plt call stub instructions. The normal stub is like this, but
128 sometimes the .plt entry crosses a 64k boundary and we need to
129 insert an addis to adjust r12. */
130#define PLT_CALL_STUB_SIZE (7*4)
5d1634d7
AM
131#define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
132#define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
133#define LD_R11_0R12 0xe96c0000 /* ld %r11,xxx+0@l(%r12) */
134#define LD_R2_0R12 0xe84c0000 /* ld %r2,xxx+8@l(%r12) */
135#define MTCTR_R11 0x7d6903a6 /* mtctr %r11 */
136 /* ld %r11,xxx+16@l(%r12) */
137#define BCTR 0x4e800420 /* bctr */
138
5d1634d7 139
ee4bf8d2 140#define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,off@ha */
ad8e1ba5
AM
141#define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
142#define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
143
144#define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
145
ee4bf8d2 146/* glink call stub instructions. We enter with the index in R0. */
ad8e1ba5 147#define GLINK_CALL_STUB_SIZE (16*4)
ee4bf8d2
AM
148 /* 0: */
149 /* .quad plt0-1f */
150 /* __glink: */
151#define MFLR_R12 0x7d8802a6 /* mflr %12 */
152#define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
153 /* 1: */
154#define MFLR_R11 0x7d6802a6 /* mflr %11 */
155#define LD_R2_M16R11 0xe84bfff0 /* ld %2,(0b-1b)(%11) */
156#define MTLR_R12 0x7d8803a6 /* mtlr %12 */
157#define ADD_R12_R2_R11 0x7d825a14 /* add %12,%2,%11 */
158 /* ld %11,0(%12) */
159 /* ld %2,8(%12) */
160 /* mtctr %11 */
161 /* ld %11,16(%12) */
162 /* bctr */
5d1634d7
AM
163
164/* Pad with this. */
165#define NOP 0x60000000
166
721956f4
AM
167/* Some other nops. */
168#define CROR_151515 0x4def7b82
169#define CROR_313131 0x4ffffb82
170
cedb70c5 171/* .glink entries for the first 32k functions are two instructions. */
5d1634d7
AM
172#define LI_R0_0 0x38000000 /* li %r0,0 */
173#define B_DOT 0x48000000 /* b . */
174
175/* After that, we need two instructions to load the index, followed by
176 a branch. */
177#define LIS_R0_0 0x3c000000 /* lis %r0,0 */
10ed1bba 178#define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
41bd81ab 179
deb0e272
AM
180/* Instructions used by the save and restore reg functions. */
181#define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
182#define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
183#define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
184#define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
82bd7b59
AM
185#define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
186#define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
deb0e272
AM
187#define LI_R12_0 0x39800000 /* li %r12,0 */
188#define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
189#define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
190#define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
82bd7b59
AM
191#define BLR 0x4e800020 /* blr */
192
41bd81ab
AM
193/* Since .opd is an array of descriptors and each entry will end up
194 with identical R_PPC64_RELATIVE relocs, there is really no need to
195 propagate .opd relocs; The dynamic linker should be taught to
1e2f5b6e 196 relocate .opd without reloc entries. */
41bd81ab
AM
197#ifndef NO_OPD_RELOCS
198#define NO_OPD_RELOCS 0
199#endif
5bd4f169 200\f
f5e87a1d 201#define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
b34976b6 202
5bd4f169 203/* Relocation HOWTO's. */
04c9666a 204static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
5bd4f169
AM
205
206static reloc_howto_type ppc64_elf_howto_raw[] = {
207 /* This reloc does nothing. */
208 HOWTO (R_PPC64_NONE, /* type */
209 0, /* rightshift */
411e1bfb
AM
210 2, /* size (0 = byte, 1 = short, 2 = long) */
211 32, /* bitsize */
b34976b6 212 FALSE, /* pc_relative */
5bd4f169 213 0, /* bitpos */
f5e87a1d 214 complain_overflow_dont, /* complain_on_overflow */
5bd4f169
AM
215 bfd_elf_generic_reloc, /* special_function */
216 "R_PPC64_NONE", /* name */
b34976b6 217 FALSE, /* partial_inplace */
d006db6c 218 0, /* src_mask */
5bd4f169 219 0, /* dst_mask */
b34976b6 220 FALSE), /* pcrel_offset */
5bd4f169
AM
221
222 /* A standard 32 bit relocation. */
223 HOWTO (R_PPC64_ADDR32, /* type */
224 0, /* rightshift */
225 2, /* size (0 = byte, 1 = short, 2 = long) */
226 32, /* bitsize */
b34976b6 227 FALSE, /* pc_relative */
5bd4f169
AM
228 0, /* bitpos */
229 complain_overflow_bitfield, /* complain_on_overflow */
230 bfd_elf_generic_reloc, /* special_function */
231 "R_PPC64_ADDR32", /* name */
b34976b6 232 FALSE, /* partial_inplace */
5bd4f169
AM
233 0, /* src_mask */
234 0xffffffff, /* dst_mask */
b34976b6 235 FALSE), /* pcrel_offset */
5bd4f169
AM
236
237 /* An absolute 26 bit branch; the lower two bits must be zero.
238 FIXME: we don't check that, we just clear them. */
239 HOWTO (R_PPC64_ADDR24, /* type */
240 0, /* rightshift */
241 2, /* size (0 = byte, 1 = short, 2 = long) */
242 26, /* bitsize */
b34976b6 243 FALSE, /* pc_relative */
5bd4f169
AM
244 0, /* bitpos */
245 complain_overflow_bitfield, /* complain_on_overflow */
246 bfd_elf_generic_reloc, /* special_function */
247 "R_PPC64_ADDR24", /* name */
b34976b6 248 FALSE, /* partial_inplace */
d006db6c 249 0, /* src_mask */
f5e87a1d 250 0x03fffffc, /* dst_mask */
b34976b6 251 FALSE), /* pcrel_offset */
5bd4f169
AM
252
253 /* A standard 16 bit relocation. */
254 HOWTO (R_PPC64_ADDR16, /* type */
255 0, /* rightshift */
256 1, /* size (0 = byte, 1 = short, 2 = long) */
257 16, /* bitsize */
b34976b6 258 FALSE, /* pc_relative */
5bd4f169
AM
259 0, /* bitpos */
260 complain_overflow_bitfield, /* complain_on_overflow */
261 bfd_elf_generic_reloc, /* special_function */
262 "R_PPC64_ADDR16", /* name */
b34976b6 263 FALSE, /* partial_inplace */
5bd4f169
AM
264 0, /* src_mask */
265 0xffff, /* dst_mask */
b34976b6 266 FALSE), /* pcrel_offset */
5bd4f169
AM
267
268 /* A 16 bit relocation without overflow. */
269 HOWTO (R_PPC64_ADDR16_LO, /* type */
270 0, /* rightshift */
271 1, /* size (0 = byte, 1 = short, 2 = long) */
272 16, /* bitsize */
b34976b6 273 FALSE, /* pc_relative */
5bd4f169
AM
274 0, /* bitpos */
275 complain_overflow_dont,/* complain_on_overflow */
276 bfd_elf_generic_reloc, /* special_function */
277 "R_PPC64_ADDR16_LO", /* name */
b34976b6 278 FALSE, /* partial_inplace */
5bd4f169
AM
279 0, /* src_mask */
280 0xffff, /* dst_mask */
b34976b6 281 FALSE), /* pcrel_offset */
5bd4f169
AM
282
283 /* Bits 16-31 of an address. */
284 HOWTO (R_PPC64_ADDR16_HI, /* type */
285 16, /* rightshift */
286 1, /* size (0 = byte, 1 = short, 2 = long) */
287 16, /* bitsize */
b34976b6 288 FALSE, /* pc_relative */
5bd4f169
AM
289 0, /* bitpos */
290 complain_overflow_dont, /* complain_on_overflow */
291 bfd_elf_generic_reloc, /* special_function */
292 "R_PPC64_ADDR16_HI", /* name */
b34976b6 293 FALSE, /* partial_inplace */
5bd4f169
AM
294 0, /* src_mask */
295 0xffff, /* dst_mask */
b34976b6 296 FALSE), /* pcrel_offset */
5bd4f169
AM
297
298 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
299 bits, treated as a signed number, is negative. */
300 HOWTO (R_PPC64_ADDR16_HA, /* type */
301 16, /* rightshift */
302 1, /* size (0 = byte, 1 = short, 2 = long) */
303 16, /* bitsize */
b34976b6 304 FALSE, /* pc_relative */
5bd4f169
AM
305 0, /* bitpos */
306 complain_overflow_dont, /* complain_on_overflow */
805fc799 307 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 308 "R_PPC64_ADDR16_HA", /* name */
b34976b6 309 FALSE, /* partial_inplace */
5bd4f169
AM
310 0, /* src_mask */
311 0xffff, /* dst_mask */
b34976b6 312 FALSE), /* pcrel_offset */
5bd4f169
AM
313
314 /* An absolute 16 bit branch; the lower two bits must be zero.
315 FIXME: we don't check that, we just clear them. */
316 HOWTO (R_PPC64_ADDR14, /* type */
317 0, /* rightshift */
318 2, /* size (0 = byte, 1 = short, 2 = long) */
319 16, /* bitsize */
b34976b6 320 FALSE, /* pc_relative */
5bd4f169
AM
321 0, /* bitpos */
322 complain_overflow_bitfield, /* complain_on_overflow */
2441e016 323 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 324 "R_PPC64_ADDR14", /* name */
b34976b6 325 FALSE, /* partial_inplace */
d006db6c 326 0, /* src_mask */
f5e87a1d 327 0x0000fffc, /* dst_mask */
b34976b6 328 FALSE), /* pcrel_offset */
5bd4f169
AM
329
330 /* An absolute 16 bit branch, for which bit 10 should be set to
331 indicate that the branch is expected to be taken. The lower two
332 bits must be zero. */
333 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
334 0, /* rightshift */
335 2, /* size (0 = byte, 1 = short, 2 = long) */
336 16, /* bitsize */
b34976b6 337 FALSE, /* pc_relative */
5bd4f169
AM
338 0, /* bitpos */
339 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 340 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 341 "R_PPC64_ADDR14_BRTAKEN",/* name */
b34976b6 342 FALSE, /* partial_inplace */
d006db6c 343 0, /* src_mask */
f5e87a1d 344 0x0000fffc, /* dst_mask */
b34976b6 345 FALSE), /* pcrel_offset */
5bd4f169
AM
346
347 /* An absolute 16 bit branch, for which bit 10 should be set to
348 indicate that the branch is not expected to be taken. The lower
349 two bits must be zero. */
350 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
351 0, /* rightshift */
352 2, /* size (0 = byte, 1 = short, 2 = long) */
353 16, /* bitsize */
b34976b6 354 FALSE, /* pc_relative */
5bd4f169
AM
355 0, /* bitpos */
356 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 357 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 358 "R_PPC64_ADDR14_BRNTAKEN",/* name */
b34976b6 359 FALSE, /* partial_inplace */
d006db6c 360 0, /* src_mask */
f5e87a1d 361 0x0000fffc, /* dst_mask */
b34976b6 362 FALSE), /* pcrel_offset */
5bd4f169
AM
363
364 /* A relative 26 bit branch; the lower two bits must be zero. */
365 HOWTO (R_PPC64_REL24, /* type */
366 0, /* rightshift */
367 2, /* size (0 = byte, 1 = short, 2 = long) */
368 26, /* bitsize */
b34976b6 369 TRUE, /* pc_relative */
5bd4f169
AM
370 0, /* bitpos */
371 complain_overflow_signed, /* complain_on_overflow */
2441e016 372 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 373 "R_PPC64_REL24", /* name */
b34976b6 374 FALSE, /* partial_inplace */
d006db6c 375 0, /* src_mask */
f5e87a1d 376 0x03fffffc, /* dst_mask */
b34976b6 377 TRUE), /* pcrel_offset */
5bd4f169
AM
378
379 /* A relative 16 bit branch; the lower two bits must be zero. */
380 HOWTO (R_PPC64_REL14, /* type */
381 0, /* rightshift */
382 2, /* size (0 = byte, 1 = short, 2 = long) */
383 16, /* bitsize */
b34976b6 384 TRUE, /* pc_relative */
5bd4f169
AM
385 0, /* bitpos */
386 complain_overflow_signed, /* complain_on_overflow */
2441e016 387 ppc64_elf_branch_reloc, /* special_function */
5bd4f169 388 "R_PPC64_REL14", /* name */
b34976b6 389 FALSE, /* partial_inplace */
d006db6c 390 0, /* src_mask */
f5e87a1d 391 0x0000fffc, /* dst_mask */
b34976b6 392 TRUE), /* pcrel_offset */
5bd4f169
AM
393
394 /* A relative 16 bit branch. Bit 10 should be set to indicate that
395 the branch is expected to be taken. The lower two bits must be
396 zero. */
397 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 16, /* bitsize */
b34976b6 401 TRUE, /* pc_relative */
5bd4f169
AM
402 0, /* bitpos */
403 complain_overflow_signed, /* complain_on_overflow */
805fc799 404 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 405 "R_PPC64_REL14_BRTAKEN", /* name */
b34976b6 406 FALSE, /* partial_inplace */
d006db6c 407 0, /* src_mask */
f5e87a1d 408 0x0000fffc, /* dst_mask */
b34976b6 409 TRUE), /* pcrel_offset */
5bd4f169
AM
410
411 /* A relative 16 bit branch. Bit 10 should be set to indicate that
412 the branch is not expected to be taken. The lower two bits must
413 be zero. */
414 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
415 0, /* rightshift */
416 2, /* size (0 = byte, 1 = short, 2 = long) */
417 16, /* bitsize */
b34976b6 418 TRUE, /* pc_relative */
5bd4f169
AM
419 0, /* bitpos */
420 complain_overflow_signed, /* complain_on_overflow */
805fc799 421 ppc64_elf_brtaken_reloc, /* special_function */
5bd4f169 422 "R_PPC64_REL14_BRNTAKEN",/* name */
b34976b6 423 FALSE, /* partial_inplace */
d006db6c 424 0, /* src_mask */
f5e87a1d 425 0x0000fffc, /* dst_mask */
b34976b6 426 TRUE), /* pcrel_offset */
5bd4f169
AM
427
428 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
429 symbol. */
430 HOWTO (R_PPC64_GOT16, /* type */
431 0, /* rightshift */
432 1, /* size (0 = byte, 1 = short, 2 = long) */
433 16, /* bitsize */
b34976b6 434 FALSE, /* pc_relative */
5bd4f169
AM
435 0, /* bitpos */
436 complain_overflow_signed, /* complain_on_overflow */
805fc799 437 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 438 "R_PPC64_GOT16", /* name */
b34976b6 439 FALSE, /* partial_inplace */
5bd4f169
AM
440 0, /* src_mask */
441 0xffff, /* dst_mask */
b34976b6 442 FALSE), /* pcrel_offset */
5bd4f169
AM
443
444 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
445 the symbol. */
446 HOWTO (R_PPC64_GOT16_LO, /* type */
447 0, /* rightshift */
448 1, /* size (0 = byte, 1 = short, 2 = long) */
449 16, /* bitsize */
b34976b6 450 FALSE, /* pc_relative */
5bd4f169
AM
451 0, /* bitpos */
452 complain_overflow_dont, /* complain_on_overflow */
805fc799 453 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 454 "R_PPC64_GOT16_LO", /* name */
b34976b6 455 FALSE, /* partial_inplace */
5bd4f169
AM
456 0, /* src_mask */
457 0xffff, /* dst_mask */
b34976b6 458 FALSE), /* pcrel_offset */
5bd4f169
AM
459
460 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
461 the symbol. */
462 HOWTO (R_PPC64_GOT16_HI, /* type */
463 16, /* rightshift */
464 1, /* size (0 = byte, 1 = short, 2 = long) */
465 16, /* bitsize */
b34976b6 466 FALSE, /* pc_relative */
5bd4f169
AM
467 0, /* bitpos */
468 complain_overflow_dont,/* complain_on_overflow */
805fc799 469 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 470 "R_PPC64_GOT16_HI", /* name */
b34976b6 471 FALSE, /* partial_inplace */
5bd4f169
AM
472 0, /* src_mask */
473 0xffff, /* dst_mask */
b34976b6 474 FALSE), /* pcrel_offset */
5bd4f169
AM
475
476 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
477 the symbol. */
478 HOWTO (R_PPC64_GOT16_HA, /* type */
479 16, /* rightshift */
480 1, /* size (0 = byte, 1 = short, 2 = long) */
481 16, /* bitsize */
b34976b6 482 FALSE, /* pc_relative */
5bd4f169
AM
483 0, /* bitpos */
484 complain_overflow_dont,/* complain_on_overflow */
805fc799 485 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 486 "R_PPC64_GOT16_HA", /* name */
b34976b6 487 FALSE, /* partial_inplace */
5bd4f169
AM
488 0, /* src_mask */
489 0xffff, /* dst_mask */
b34976b6 490 FALSE), /* pcrel_offset */
5bd4f169
AM
491
492 /* This is used only by the dynamic linker. The symbol should exist
493 both in the object being run and in some shared library. The
494 dynamic linker copies the data addressed by the symbol from the
495 shared library into the object, because the object being
496 run has to have the data at some particular address. */
497 HOWTO (R_PPC64_COPY, /* type */
498 0, /* rightshift */
f5e87a1d
AM
499 0, /* this one is variable size */
500 0, /* bitsize */
b34976b6 501 FALSE, /* pc_relative */
5bd4f169 502 0, /* bitpos */
f5e87a1d
AM
503 complain_overflow_dont, /* complain_on_overflow */
504 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 505 "R_PPC64_COPY", /* name */
b34976b6 506 FALSE, /* partial_inplace */
5bd4f169
AM
507 0, /* src_mask */
508 0, /* dst_mask */
b34976b6 509 FALSE), /* pcrel_offset */
5bd4f169
AM
510
511 /* Like R_PPC64_ADDR64, but used when setting global offset table
512 entries. */
513 HOWTO (R_PPC64_GLOB_DAT, /* type */
514 0, /* rightshift */
515 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
516 64, /* bitsize */
b34976b6 517 FALSE, /* pc_relative */
5bd4f169
AM
518 0, /* bitpos */
519 complain_overflow_dont, /* complain_on_overflow */
805fc799 520 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 521 "R_PPC64_GLOB_DAT", /* name */
b34976b6 522 FALSE, /* partial_inplace */
5bd4f169 523 0, /* src_mask */
f5e87a1d 524 ONES (64), /* dst_mask */
b34976b6 525 FALSE), /* pcrel_offset */
5bd4f169
AM
526
527 /* Created by the link editor. Marks a procedure linkage table
528 entry for a symbol. */
529 HOWTO (R_PPC64_JMP_SLOT, /* type */
530 0, /* rightshift */
531 0, /* size (0 = byte, 1 = short, 2 = long) */
532 0, /* bitsize */
b34976b6 533 FALSE, /* pc_relative */
5bd4f169
AM
534 0, /* bitpos */
535 complain_overflow_dont, /* complain_on_overflow */
805fc799 536 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 537 "R_PPC64_JMP_SLOT", /* name */
b34976b6 538 FALSE, /* partial_inplace */
5bd4f169
AM
539 0, /* src_mask */
540 0, /* dst_mask */
b34976b6 541 FALSE), /* pcrel_offset */
5bd4f169
AM
542
543 /* Used only by the dynamic linker. When the object is run, this
544 doubleword64 is set to the load address of the object, plus the
545 addend. */
546 HOWTO (R_PPC64_RELATIVE, /* type */
547 0, /* rightshift */
548 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
549 64, /* bitsize */
b34976b6 550 FALSE, /* pc_relative */
5bd4f169
AM
551 0, /* bitpos */
552 complain_overflow_dont, /* complain_on_overflow */
553 bfd_elf_generic_reloc, /* special_function */
554 "R_PPC64_RELATIVE", /* name */
b34976b6 555 FALSE, /* partial_inplace */
5bd4f169 556 0, /* src_mask */
f5e87a1d 557 ONES (64), /* dst_mask */
b34976b6 558 FALSE), /* pcrel_offset */
5bd4f169
AM
559
560 /* Like R_PPC64_ADDR32, but may be unaligned. */
561 HOWTO (R_PPC64_UADDR32, /* type */
562 0, /* rightshift */
563 2, /* size (0 = byte, 1 = short, 2 = long) */
564 32, /* bitsize */
b34976b6 565 FALSE, /* pc_relative */
5bd4f169
AM
566 0, /* bitpos */
567 complain_overflow_bitfield, /* complain_on_overflow */
568 bfd_elf_generic_reloc, /* special_function */
569 "R_PPC64_UADDR32", /* name */
b34976b6 570 FALSE, /* partial_inplace */
5bd4f169
AM
571 0, /* src_mask */
572 0xffffffff, /* dst_mask */
b34976b6 573 FALSE), /* pcrel_offset */
5bd4f169
AM
574
575 /* Like R_PPC64_ADDR16, but may be unaligned. */
576 HOWTO (R_PPC64_UADDR16, /* type */
577 0, /* rightshift */
578 1, /* size (0 = byte, 1 = short, 2 = long) */
579 16, /* bitsize */
b34976b6 580 FALSE, /* pc_relative */
5bd4f169
AM
581 0, /* bitpos */
582 complain_overflow_bitfield, /* complain_on_overflow */
583 bfd_elf_generic_reloc, /* special_function */
584 "R_PPC64_UADDR16", /* name */
b34976b6 585 FALSE, /* partial_inplace */
5bd4f169
AM
586 0, /* src_mask */
587 0xffff, /* dst_mask */
b34976b6 588 FALSE), /* pcrel_offset */
5bd4f169
AM
589
590 /* 32-bit PC relative. */
591 HOWTO (R_PPC64_REL32, /* type */
592 0, /* rightshift */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
594 32, /* bitsize */
b34976b6 595 TRUE, /* pc_relative */
5bd4f169 596 0, /* bitpos */
cedb70c5 597 /* FIXME: Verify. Was complain_overflow_bitfield. */
5bd4f169
AM
598 complain_overflow_signed, /* complain_on_overflow */
599 bfd_elf_generic_reloc, /* special_function */
600 "R_PPC64_REL32", /* name */
b34976b6 601 FALSE, /* partial_inplace */
5bd4f169
AM
602 0, /* src_mask */
603 0xffffffff, /* dst_mask */
b34976b6 604 TRUE), /* pcrel_offset */
5bd4f169 605
10ed1bba 606 /* 32-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
607 HOWTO (R_PPC64_PLT32, /* type */
608 0, /* rightshift */
609 2, /* size (0 = byte, 1 = short, 2 = long) */
610 32, /* bitsize */
b34976b6 611 FALSE, /* pc_relative */
5bd4f169
AM
612 0, /* bitpos */
613 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 614 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 615 "R_PPC64_PLT32", /* name */
b34976b6 616 FALSE, /* partial_inplace */
5bd4f169 617 0, /* src_mask */
f5e87a1d 618 0xffffffff, /* dst_mask */
b34976b6 619 FALSE), /* pcrel_offset */
5bd4f169
AM
620
621 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
622 FIXME: R_PPC64_PLTREL32 not supported. */
623 HOWTO (R_PPC64_PLTREL32, /* type */
624 0, /* rightshift */
625 2, /* size (0 = byte, 1 = short, 2 = long) */
626 32, /* bitsize */
b34976b6 627 TRUE, /* pc_relative */
5bd4f169
AM
628 0, /* bitpos */
629 complain_overflow_signed, /* complain_on_overflow */
630 bfd_elf_generic_reloc, /* special_function */
631 "R_PPC64_PLTREL32", /* name */
b34976b6 632 FALSE, /* partial_inplace */
5bd4f169 633 0, /* src_mask */
f5e87a1d 634 0xffffffff, /* dst_mask */
b34976b6 635 TRUE), /* pcrel_offset */
5bd4f169
AM
636
637 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
638 the symbol. */
639 HOWTO (R_PPC64_PLT16_LO, /* type */
640 0, /* rightshift */
641 1, /* size (0 = byte, 1 = short, 2 = long) */
642 16, /* bitsize */
b34976b6 643 FALSE, /* pc_relative */
5bd4f169
AM
644 0, /* bitpos */
645 complain_overflow_dont, /* complain_on_overflow */
805fc799 646 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 647 "R_PPC64_PLT16_LO", /* name */
b34976b6 648 FALSE, /* partial_inplace */
5bd4f169
AM
649 0, /* src_mask */
650 0xffff, /* dst_mask */
b34976b6 651 FALSE), /* pcrel_offset */
5bd4f169
AM
652
653 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
654 the symbol. */
655 HOWTO (R_PPC64_PLT16_HI, /* type */
656 16, /* rightshift */
657 1, /* size (0 = byte, 1 = short, 2 = long) */
658 16, /* bitsize */
b34976b6 659 FALSE, /* pc_relative */
5bd4f169
AM
660 0, /* bitpos */
661 complain_overflow_dont, /* complain_on_overflow */
805fc799 662 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 663 "R_PPC64_PLT16_HI", /* name */
b34976b6 664 FALSE, /* partial_inplace */
5bd4f169
AM
665 0, /* src_mask */
666 0xffff, /* dst_mask */
b34976b6 667 FALSE), /* pcrel_offset */
5bd4f169
AM
668
669 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
670 the symbol. */
671 HOWTO (R_PPC64_PLT16_HA, /* type */
672 16, /* rightshift */
673 1, /* size (0 = byte, 1 = short, 2 = long) */
674 16, /* bitsize */
b34976b6 675 FALSE, /* pc_relative */
5bd4f169
AM
676 0, /* bitpos */
677 complain_overflow_dont, /* complain_on_overflow */
805fc799 678 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 679 "R_PPC64_PLT16_HA", /* name */
b34976b6 680 FALSE, /* partial_inplace */
5bd4f169
AM
681 0, /* src_mask */
682 0xffff, /* dst_mask */
b34976b6 683 FALSE), /* pcrel_offset */
5bd4f169 684
c061c2d8 685 /* 16-bit section relative relocation. */
5bd4f169
AM
686 HOWTO (R_PPC64_SECTOFF, /* type */
687 0, /* rightshift */
c061c2d8
AM
688 1, /* size (0 = byte, 1 = short, 2 = long) */
689 16, /* bitsize */
b34976b6 690 FALSE, /* pc_relative */
5bd4f169
AM
691 0, /* bitpos */
692 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 693 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 694 "R_PPC64_SECTOFF", /* name */
b34976b6 695 FALSE, /* partial_inplace */
5bd4f169 696 0, /* src_mask */
c061c2d8 697 0xffff, /* dst_mask */
b34976b6 698 FALSE), /* pcrel_offset */
5bd4f169 699
c061c2d8 700 /* Like R_PPC64_SECTOFF, but no overflow warning. */
5bd4f169
AM
701 HOWTO (R_PPC64_SECTOFF_LO, /* type */
702 0, /* rightshift */
703 1, /* size (0 = byte, 1 = short, 2 = long) */
704 16, /* bitsize */
b34976b6 705 FALSE, /* pc_relative */
5bd4f169
AM
706 0, /* bitpos */
707 complain_overflow_dont, /* complain_on_overflow */
805fc799 708 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 709 "R_PPC64_SECTOFF_LO", /* name */
b34976b6 710 FALSE, /* partial_inplace */
5bd4f169
AM
711 0, /* src_mask */
712 0xffff, /* dst_mask */
b34976b6 713 FALSE), /* pcrel_offset */
5bd4f169
AM
714
715 /* 16-bit upper half section relative relocation. */
716 HOWTO (R_PPC64_SECTOFF_HI, /* type */
717 16, /* rightshift */
718 1, /* size (0 = byte, 1 = short, 2 = long) */
719 16, /* bitsize */
b34976b6 720 FALSE, /* pc_relative */
5bd4f169
AM
721 0, /* bitpos */
722 complain_overflow_dont, /* complain_on_overflow */
805fc799 723 ppc64_elf_sectoff_reloc, /* special_function */
5bd4f169 724 "R_PPC64_SECTOFF_HI", /* name */
b34976b6 725 FALSE, /* partial_inplace */
5bd4f169
AM
726 0, /* src_mask */
727 0xffff, /* dst_mask */
b34976b6 728 FALSE), /* pcrel_offset */
5bd4f169
AM
729
730 /* 16-bit upper half adjusted section relative relocation. */
731 HOWTO (R_PPC64_SECTOFF_HA, /* type */
732 16, /* rightshift */
733 1, /* size (0 = byte, 1 = short, 2 = long) */
734 16, /* bitsize */
b34976b6 735 FALSE, /* pc_relative */
5bd4f169
AM
736 0, /* bitpos */
737 complain_overflow_dont, /* complain_on_overflow */
805fc799 738 ppc64_elf_sectoff_ha_reloc, /* special_function */
5bd4f169 739 "R_PPC64_SECTOFF_HA", /* name */
b34976b6 740 FALSE, /* partial_inplace */
5bd4f169
AM
741 0, /* src_mask */
742 0xffff, /* dst_mask */
b34976b6 743 FALSE), /* pcrel_offset */
5bd4f169 744
04c9666a
AM
745 /* Like R_PPC64_REL24 without touching the two least significant bits. */
746 HOWTO (R_PPC64_REL30, /* type */
5bd4f169
AM
747 2, /* rightshift */
748 2, /* size (0 = byte, 1 = short, 2 = long) */
749 30, /* bitsize */
b34976b6 750 TRUE, /* pc_relative */
5bd4f169
AM
751 0, /* bitpos */
752 complain_overflow_dont, /* complain_on_overflow */
753 bfd_elf_generic_reloc, /* special_function */
04c9666a 754 "R_PPC64_REL30", /* name */
b34976b6 755 FALSE, /* partial_inplace */
d006db6c 756 0, /* src_mask */
5bd4f169 757 0xfffffffc, /* dst_mask */
b34976b6 758 TRUE), /* pcrel_offset */
5bd4f169
AM
759
760 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
761
762 /* A standard 64-bit relocation. */
763 HOWTO (R_PPC64_ADDR64, /* type */
764 0, /* rightshift */
765 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
766 64, /* bitsize */
b34976b6 767 FALSE, /* pc_relative */
5bd4f169
AM
768 0, /* bitpos */
769 complain_overflow_dont, /* complain_on_overflow */
770 bfd_elf_generic_reloc, /* special_function */
771 "R_PPC64_ADDR64", /* name */
b34976b6 772 FALSE, /* partial_inplace */
5bd4f169 773 0, /* src_mask */
f5e87a1d 774 ONES (64), /* dst_mask */
b34976b6 775 FALSE), /* pcrel_offset */
5bd4f169
AM
776
777 /* The bits 32-47 of an address. */
778 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
779 32, /* rightshift */
780 1, /* size (0 = byte, 1 = short, 2 = long) */
781 16, /* bitsize */
b34976b6 782 FALSE, /* pc_relative */
5bd4f169
AM
783 0, /* bitpos */
784 complain_overflow_dont, /* complain_on_overflow */
785 bfd_elf_generic_reloc, /* special_function */
786 "R_PPC64_ADDR16_HIGHER", /* name */
b34976b6 787 FALSE, /* partial_inplace */
5bd4f169
AM
788 0, /* src_mask */
789 0xffff, /* dst_mask */
b34976b6 790 FALSE), /* pcrel_offset */
5bd4f169
AM
791
792 /* The bits 32-47 of an address, plus 1 if the contents of the low
793 16 bits, treated as a signed number, is negative. */
794 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
795 32, /* rightshift */
796 1, /* size (0 = byte, 1 = short, 2 = long) */
797 16, /* bitsize */
b34976b6 798 FALSE, /* pc_relative */
5bd4f169
AM
799 0, /* bitpos */
800 complain_overflow_dont, /* complain_on_overflow */
805fc799 801 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 802 "R_PPC64_ADDR16_HIGHERA", /* name */
b34976b6 803 FALSE, /* partial_inplace */
5bd4f169
AM
804 0, /* src_mask */
805 0xffff, /* dst_mask */
b34976b6 806 FALSE), /* pcrel_offset */
5bd4f169
AM
807
808 /* The bits 48-63 of an address. */
809 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
810 48, /* rightshift */
811 1, /* size (0 = byte, 1 = short, 2 = long) */
812 16, /* bitsize */
b34976b6 813 FALSE, /* pc_relative */
5bd4f169
AM
814 0, /* bitpos */
815 complain_overflow_dont, /* complain_on_overflow */
816 bfd_elf_generic_reloc, /* special_function */
817 "R_PPC64_ADDR16_HIGHEST", /* name */
b34976b6 818 FALSE, /* partial_inplace */
5bd4f169
AM
819 0, /* src_mask */
820 0xffff, /* dst_mask */
b34976b6 821 FALSE), /* pcrel_offset */
5bd4f169
AM
822
823 /* The bits 48-63 of an address, plus 1 if the contents of the low
824 16 bits, treated as a signed number, is negative. */
825 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
826 48, /* rightshift */
827 1, /* size (0 = byte, 1 = short, 2 = long) */
828 16, /* bitsize */
b34976b6 829 FALSE, /* pc_relative */
5bd4f169
AM
830 0, /* bitpos */
831 complain_overflow_dont, /* complain_on_overflow */
805fc799 832 ppc64_elf_ha_reloc, /* special_function */
5bd4f169 833 "R_PPC64_ADDR16_HIGHESTA", /* name */
b34976b6 834 FALSE, /* partial_inplace */
5bd4f169
AM
835 0, /* src_mask */
836 0xffff, /* dst_mask */
b34976b6 837 FALSE), /* pcrel_offset */
5bd4f169
AM
838
839 /* Like ADDR64, but may be unaligned. */
840 HOWTO (R_PPC64_UADDR64, /* type */
841 0, /* rightshift */
842 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
843 64, /* bitsize */
b34976b6 844 FALSE, /* pc_relative */
5bd4f169
AM
845 0, /* bitpos */
846 complain_overflow_dont, /* complain_on_overflow */
847 bfd_elf_generic_reloc, /* special_function */
848 "R_PPC64_UADDR64", /* name */
b34976b6 849 FALSE, /* partial_inplace */
5bd4f169 850 0, /* src_mask */
f5e87a1d 851 ONES (64), /* dst_mask */
b34976b6 852 FALSE), /* pcrel_offset */
5bd4f169
AM
853
854 /* 64-bit relative relocation. */
855 HOWTO (R_PPC64_REL64, /* type */
856 0, /* rightshift */
857 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
858 64, /* bitsize */
b34976b6 859 TRUE, /* pc_relative */
5bd4f169
AM
860 0, /* bitpos */
861 complain_overflow_dont, /* complain_on_overflow */
862 bfd_elf_generic_reloc, /* special_function */
863 "R_PPC64_REL64", /* name */
b34976b6 864 FALSE, /* partial_inplace */
5bd4f169 865 0, /* src_mask */
f5e87a1d 866 ONES (64), /* dst_mask */
b34976b6 867 TRUE), /* pcrel_offset */
5bd4f169 868
cedb70c5 869 /* 64-bit relocation to the symbol's procedure linkage table. */
5bd4f169
AM
870 HOWTO (R_PPC64_PLT64, /* type */
871 0, /* rightshift */
872 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
873 64, /* bitsize */
b34976b6 874 FALSE, /* pc_relative */
5bd4f169
AM
875 0, /* bitpos */
876 complain_overflow_dont, /* complain_on_overflow */
805fc799 877 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 878 "R_PPC64_PLT64", /* name */
b34976b6 879 FALSE, /* partial_inplace */
5bd4f169 880 0, /* src_mask */
f5e87a1d 881 ONES (64), /* dst_mask */
b34976b6 882 FALSE), /* pcrel_offset */
5bd4f169
AM
883
884 /* 64-bit PC relative relocation to the symbol's procedure linkage
885 table. */
886 /* FIXME: R_PPC64_PLTREL64 not supported. */
887 HOWTO (R_PPC64_PLTREL64, /* type */
888 0, /* rightshift */
889 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
890 64, /* bitsize */
b34976b6 891 TRUE, /* pc_relative */
5bd4f169
AM
892 0, /* bitpos */
893 complain_overflow_dont, /* complain_on_overflow */
805fc799 894 ppc64_elf_unhandled_reloc, /* special_function */
5bd4f169 895 "R_PPC64_PLTREL64", /* name */
b34976b6 896 FALSE, /* partial_inplace */
5bd4f169 897 0, /* src_mask */
f5e87a1d 898 ONES (64), /* dst_mask */
b34976b6 899 TRUE), /* pcrel_offset */
5bd4f169
AM
900
901 /* 16 bit TOC-relative relocation. */
902
903 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
904 HOWTO (R_PPC64_TOC16, /* type */
905 0, /* rightshift */
906 1, /* size (0 = byte, 1 = short, 2 = long) */
907 16, /* bitsize */
b34976b6 908 FALSE, /* pc_relative */
5bd4f169
AM
909 0, /* bitpos */
910 complain_overflow_signed, /* complain_on_overflow */
805fc799 911 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 912 "R_PPC64_TOC16", /* name */
b34976b6 913 FALSE, /* partial_inplace */
5bd4f169
AM
914 0, /* src_mask */
915 0xffff, /* dst_mask */
b34976b6 916 FALSE), /* pcrel_offset */
5bd4f169
AM
917
918 /* 16 bit TOC-relative relocation without overflow. */
919
920 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
921 HOWTO (R_PPC64_TOC16_LO, /* type */
922 0, /* rightshift */
923 1, /* size (0 = byte, 1 = short, 2 = long) */
924 16, /* bitsize */
b34976b6 925 FALSE, /* pc_relative */
5bd4f169
AM
926 0, /* bitpos */
927 complain_overflow_dont, /* complain_on_overflow */
805fc799 928 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 929 "R_PPC64_TOC16_LO", /* name */
b34976b6 930 FALSE, /* partial_inplace */
5bd4f169
AM
931 0, /* src_mask */
932 0xffff, /* dst_mask */
b34976b6 933 FALSE), /* pcrel_offset */
5bd4f169
AM
934
935 /* 16 bit TOC-relative relocation, high 16 bits. */
936
937 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
938 HOWTO (R_PPC64_TOC16_HI, /* type */
939 16, /* rightshift */
940 1, /* size (0 = byte, 1 = short, 2 = long) */
941 16, /* bitsize */
b34976b6 942 FALSE, /* pc_relative */
5bd4f169
AM
943 0, /* bitpos */
944 complain_overflow_dont, /* complain_on_overflow */
805fc799 945 ppc64_elf_toc_reloc, /* special_function */
5bd4f169 946 "R_PPC64_TOC16_HI", /* name */
b34976b6 947 FALSE, /* partial_inplace */
5bd4f169
AM
948 0, /* src_mask */
949 0xffff, /* dst_mask */
b34976b6 950 FALSE), /* pcrel_offset */
5bd4f169
AM
951
952 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
953 contents of the low 16 bits, treated as a signed number, is
954 negative. */
955
956 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
957 HOWTO (R_PPC64_TOC16_HA, /* type */
958 16, /* rightshift */
959 1, /* size (0 = byte, 1 = short, 2 = long) */
960 16, /* bitsize */
b34976b6 961 FALSE, /* pc_relative */
5bd4f169
AM
962 0, /* bitpos */
963 complain_overflow_dont, /* complain_on_overflow */
805fc799 964 ppc64_elf_toc_ha_reloc, /* special_function */
5bd4f169 965 "R_PPC64_TOC16_HA", /* name */
b34976b6 966 FALSE, /* partial_inplace */
5bd4f169
AM
967 0, /* src_mask */
968 0xffff, /* dst_mask */
b34976b6 969 FALSE), /* pcrel_offset */
5bd4f169
AM
970
971 /* 64-bit relocation; insert value of TOC base (.TOC.). */
972
973 /* R_PPC64_TOC 51 doubleword64 .TOC. */
974 HOWTO (R_PPC64_TOC, /* type */
975 0, /* rightshift */
976 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
977 64, /* bitsize */
b34976b6 978 FALSE, /* pc_relative */
5bd4f169
AM
979 0, /* bitpos */
980 complain_overflow_bitfield, /* complain_on_overflow */
805fc799 981 ppc64_elf_toc64_reloc, /* special_function */
5bd4f169 982 "R_PPC64_TOC", /* name */
b34976b6 983 FALSE, /* partial_inplace */
5bd4f169 984 0, /* src_mask */
f5e87a1d 985 ONES (64), /* dst_mask */
b34976b6 986 FALSE), /* pcrel_offset */
5bd4f169
AM
987
988 /* Like R_PPC64_GOT16, but also informs the link editor that the
989 value to relocate may (!) refer to a PLT entry which the link
990 editor (a) may replace with the symbol value. If the link editor
991 is unable to fully resolve the symbol, it may (b) create a PLT
992 entry and store the address to the new PLT entry in the GOT.
993 This permits lazy resolution of function symbols at run time.
994 The link editor may also skip all of this and just (c) emit a
995 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
996 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
997 HOWTO (R_PPC64_PLTGOT16, /* type */
998 0, /* rightshift */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1000 16, /* bitsize */
b34976b6 1001 FALSE, /* pc_relative */
5bd4f169
AM
1002 0, /* bitpos */
1003 complain_overflow_signed, /* complain_on_overflow */
805fc799 1004 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb
AM
1005 "R_PPC64_PLTGOT16", /* name */
1006 FALSE, /* partial_inplace */
1007 0, /* src_mask */
1008 0xffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1010
1011 /* Like R_PPC64_PLTGOT16, but without overflow. */
1012 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1013 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1014 0, /* rightshift */
1015 1, /* size (0 = byte, 1 = short, 2 = long) */
1016 16, /* bitsize */
1017 FALSE, /* pc_relative */
1018 0, /* bitpos */
1019 complain_overflow_dont, /* complain_on_overflow */
1020 ppc64_elf_unhandled_reloc, /* special_function */
1021 "R_PPC64_PLTGOT16_LO", /* name */
1022 FALSE, /* partial_inplace */
1023 0, /* src_mask */
1024 0xffff, /* dst_mask */
1025 FALSE), /* pcrel_offset */
1026
1027 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1028 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1029 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1030 16, /* rightshift */
1031 1, /* size (0 = byte, 1 = short, 2 = long) */
1032 16, /* bitsize */
1033 FALSE, /* pc_relative */
1034 0, /* bitpos */
1035 complain_overflow_dont, /* complain_on_overflow */
1036 ppc64_elf_unhandled_reloc, /* special_function */
1037 "R_PPC64_PLTGOT16_HI", /* name */
1038 FALSE, /* partial_inplace */
1039 0, /* src_mask */
1040 0xffff, /* dst_mask */
1041 FALSE), /* pcrel_offset */
1042
1043 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1044 1 if the contents of the low 16 bits, treated as a signed number,
1045 is negative. */
1046 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1047 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1048 16, /* rightshift */
1049 1, /* size (0 = byte, 1 = short, 2 = long) */
1050 16, /* bitsize */
1051 FALSE, /* pc_relative */
1052 0, /* bitpos */
1053 complain_overflow_dont,/* complain_on_overflow */
1054 ppc64_elf_unhandled_reloc, /* special_function */
1055 "R_PPC64_PLTGOT16_HA", /* name */
1056 FALSE, /* partial_inplace */
1057 0, /* src_mask */
1058 0xffff, /* dst_mask */
1059 FALSE), /* pcrel_offset */
1060
1061 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1062 HOWTO (R_PPC64_ADDR16_DS, /* type */
1063 0, /* rightshift */
1064 1, /* size (0 = byte, 1 = short, 2 = long) */
1065 16, /* bitsize */
1066 FALSE, /* pc_relative */
1067 0, /* bitpos */
1068 complain_overflow_bitfield, /* complain_on_overflow */
1069 bfd_elf_generic_reloc, /* special_function */
1070 "R_PPC64_ADDR16_DS", /* name */
1071 FALSE, /* partial_inplace */
1072 0, /* src_mask */
1073 0xfffc, /* dst_mask */
1074 FALSE), /* pcrel_offset */
1075
1076 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1077 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1078 0, /* rightshift */
1079 1, /* size (0 = byte, 1 = short, 2 = long) */
1080 16, /* bitsize */
1081 FALSE, /* pc_relative */
1082 0, /* bitpos */
1083 complain_overflow_dont,/* complain_on_overflow */
1084 bfd_elf_generic_reloc, /* special_function */
1085 "R_PPC64_ADDR16_LO_DS",/* name */
1086 FALSE, /* partial_inplace */
1087 0, /* src_mask */
1088 0xfffc, /* dst_mask */
1089 FALSE), /* pcrel_offset */
1090
1091 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1092 HOWTO (R_PPC64_GOT16_DS, /* type */
1093 0, /* rightshift */
1094 1, /* size (0 = byte, 1 = short, 2 = long) */
1095 16, /* bitsize */
1096 FALSE, /* pc_relative */
1097 0, /* bitpos */
1098 complain_overflow_signed, /* complain_on_overflow */
1099 ppc64_elf_unhandled_reloc, /* special_function */
1100 "R_PPC64_GOT16_DS", /* name */
1101 FALSE, /* partial_inplace */
1102 0, /* src_mask */
1103 0xfffc, /* dst_mask */
1104 FALSE), /* pcrel_offset */
1105
1106 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1107 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1108 0, /* rightshift */
1109 1, /* size (0 = byte, 1 = short, 2 = long) */
1110 16, /* bitsize */
1111 FALSE, /* pc_relative */
1112 0, /* bitpos */
1113 complain_overflow_dont, /* complain_on_overflow */
1114 ppc64_elf_unhandled_reloc, /* special_function */
1115 "R_PPC64_GOT16_LO_DS", /* name */
1116 FALSE, /* partial_inplace */
1117 0, /* src_mask */
1118 0xfffc, /* dst_mask */
1119 FALSE), /* pcrel_offset */
1120
1121 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1122 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1123 0, /* rightshift */
1124 1, /* size (0 = byte, 1 = short, 2 = long) */
1125 16, /* bitsize */
1126 FALSE, /* pc_relative */
1127 0, /* bitpos */
1128 complain_overflow_dont, /* complain_on_overflow */
1129 ppc64_elf_unhandled_reloc, /* special_function */
1130 "R_PPC64_PLT16_LO_DS", /* name */
1131 FALSE, /* partial_inplace */
1132 0, /* src_mask */
1133 0xfffc, /* dst_mask */
1134 FALSE), /* pcrel_offset */
1135
1136 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1137 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1138 0, /* rightshift */
1139 1, /* size (0 = byte, 1 = short, 2 = long) */
1140 16, /* bitsize */
1141 FALSE, /* pc_relative */
1142 0, /* bitpos */
1143 complain_overflow_bitfield, /* complain_on_overflow */
1144 ppc64_elf_sectoff_reloc, /* special_function */
1145 "R_PPC64_SECTOFF_DS", /* name */
1146 FALSE, /* partial_inplace */
1147 0, /* src_mask */
1148 0xfffc, /* dst_mask */
1149 FALSE), /* pcrel_offset */
1150
1151 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1152 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1153 0, /* rightshift */
1154 1, /* size (0 = byte, 1 = short, 2 = long) */
1155 16, /* bitsize */
1156 FALSE, /* pc_relative */
1157 0, /* bitpos */
1158 complain_overflow_dont, /* complain_on_overflow */
1159 ppc64_elf_sectoff_reloc, /* special_function */
1160 "R_PPC64_SECTOFF_LO_DS",/* name */
1161 FALSE, /* partial_inplace */
1162 0, /* src_mask */
1163 0xfffc, /* dst_mask */
1164 FALSE), /* pcrel_offset */
1165
1166 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1167 HOWTO (R_PPC64_TOC16_DS, /* type */
1168 0, /* rightshift */
1169 1, /* size (0 = byte, 1 = short, 2 = long) */
1170 16, /* bitsize */
1171 FALSE, /* pc_relative */
1172 0, /* bitpos */
1173 complain_overflow_signed, /* complain_on_overflow */
1174 ppc64_elf_toc_reloc, /* special_function */
1175 "R_PPC64_TOC16_DS", /* name */
1176 FALSE, /* partial_inplace */
1177 0, /* src_mask */
1178 0xfffc, /* dst_mask */
1179 FALSE), /* pcrel_offset */
1180
1181 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1182 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1183 0, /* rightshift */
1184 1, /* size (0 = byte, 1 = short, 2 = long) */
1185 16, /* bitsize */
1186 FALSE, /* pc_relative */
1187 0, /* bitpos */
1188 complain_overflow_dont, /* complain_on_overflow */
1189 ppc64_elf_toc_reloc, /* special_function */
1190 "R_PPC64_TOC16_LO_DS", /* name */
1191 FALSE, /* partial_inplace */
1192 0, /* src_mask */
1193 0xfffc, /* dst_mask */
1194 FALSE), /* pcrel_offset */
1195
1196 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1197 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1198 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1199 0, /* rightshift */
1200 1, /* size (0 = byte, 1 = short, 2 = long) */
1201 16, /* bitsize */
1202 FALSE, /* pc_relative */
1203 0, /* bitpos */
1204 complain_overflow_signed, /* complain_on_overflow */
1205 ppc64_elf_unhandled_reloc, /* special_function */
1206 "R_PPC64_PLTGOT16_DS", /* name */
1207 FALSE, /* partial_inplace */
1208 0, /* src_mask */
1209 0xfffc, /* dst_mask */
1210 FALSE), /* pcrel_offset */
1211
1212 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1213 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1214 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1215 0, /* rightshift */
1216 1, /* size (0 = byte, 1 = short, 2 = long) */
1217 16, /* bitsize */
1218 FALSE, /* pc_relative */
1219 0, /* bitpos */
1220 complain_overflow_dont, /* complain_on_overflow */
1221 ppc64_elf_unhandled_reloc, /* special_function */
1222 "R_PPC64_PLTGOT16_LO_DS",/* name */
1223 FALSE, /* partial_inplace */
1224 0, /* src_mask */
1225 0xfffc, /* dst_mask */
1226 FALSE), /* pcrel_offset */
1227
1228 /* Marker reloc for TLS. */
1229 HOWTO (R_PPC64_TLS,
1230 0, /* rightshift */
1231 2, /* size (0 = byte, 1 = short, 2 = long) */
1232 32, /* bitsize */
1233 FALSE, /* pc_relative */
1234 0, /* bitpos */
1235 complain_overflow_dont, /* complain_on_overflow */
1236 bfd_elf_generic_reloc, /* special_function */
1237 "R_PPC64_TLS", /* name */
1238 FALSE, /* partial_inplace */
1239 0, /* src_mask */
1240 0, /* dst_mask */
1241 FALSE), /* pcrel_offset */
1242
1243 /* Computes the load module index of the load module that contains the
1244 definition of its TLS sym. */
1245 HOWTO (R_PPC64_DTPMOD64,
1246 0, /* rightshift */
1247 4, /* size (0 = byte, 1 = short, 2 = long) */
1248 64, /* bitsize */
1249 FALSE, /* pc_relative */
1250 0, /* bitpos */
1251 complain_overflow_dont, /* complain_on_overflow */
1252 ppc64_elf_unhandled_reloc, /* special_function */
1253 "R_PPC64_DTPMOD64", /* name */
1254 FALSE, /* partial_inplace */
1255 0, /* src_mask */
1256 ONES (64), /* dst_mask */
1257 FALSE), /* pcrel_offset */
1258
1259 /* Computes a dtv-relative displacement, the difference between the value
1260 of sym+add and the base address of the thread-local storage block that
1261 contains the definition of sym, minus 0x8000. */
1262 HOWTO (R_PPC64_DTPREL64,
1263 0, /* rightshift */
1264 4, /* size (0 = byte, 1 = short, 2 = long) */
1265 64, /* bitsize */
1266 FALSE, /* pc_relative */
1267 0, /* bitpos */
1268 complain_overflow_dont, /* complain_on_overflow */
1269 ppc64_elf_unhandled_reloc, /* special_function */
1270 "R_PPC64_DTPREL64", /* name */
1271 FALSE, /* partial_inplace */
1272 0, /* src_mask */
1273 ONES (64), /* dst_mask */
1274 FALSE), /* pcrel_offset */
1275
1276 /* A 16 bit dtprel reloc. */
1277 HOWTO (R_PPC64_DTPREL16,
1278 0, /* rightshift */
1279 1, /* size (0 = byte, 1 = short, 2 = long) */
1280 16, /* bitsize */
1281 FALSE, /* pc_relative */
1282 0, /* bitpos */
1283 complain_overflow_signed, /* complain_on_overflow */
1284 ppc64_elf_unhandled_reloc, /* special_function */
1285 "R_PPC64_DTPREL16", /* name */
1286 FALSE, /* partial_inplace */
1287 0, /* src_mask */
1288 0xffff, /* dst_mask */
1289 FALSE), /* pcrel_offset */
1290
1291 /* Like DTPREL16, but no overflow. */
1292 HOWTO (R_PPC64_DTPREL16_LO,
1293 0, /* rightshift */
1294 1, /* size (0 = byte, 1 = short, 2 = long) */
1295 16, /* bitsize */
1296 FALSE, /* pc_relative */
1297 0, /* bitpos */
1298 complain_overflow_dont, /* complain_on_overflow */
1299 ppc64_elf_unhandled_reloc, /* special_function */
1300 "R_PPC64_DTPREL16_LO", /* name */
1301 FALSE, /* partial_inplace */
1302 0, /* src_mask */
1303 0xffff, /* dst_mask */
1304 FALSE), /* pcrel_offset */
1305
1306 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1307 HOWTO (R_PPC64_DTPREL16_HI,
1308 16, /* rightshift */
1309 1, /* size (0 = byte, 1 = short, 2 = long) */
1310 16, /* bitsize */
1311 FALSE, /* pc_relative */
1312 0, /* bitpos */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 ppc64_elf_unhandled_reloc, /* special_function */
1315 "R_PPC64_DTPREL16_HI", /* name */
1316 FALSE, /* partial_inplace */
1317 0, /* src_mask */
1318 0xffff, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1320
1321 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1322 HOWTO (R_PPC64_DTPREL16_HA,
1323 16, /* rightshift */
1324 1, /* size (0 = byte, 1 = short, 2 = long) */
1325 16, /* bitsize */
1326 FALSE, /* pc_relative */
1327 0, /* bitpos */
1328 complain_overflow_dont, /* complain_on_overflow */
1329 ppc64_elf_unhandled_reloc, /* special_function */
1330 "R_PPC64_DTPREL16_HA", /* name */
1331 FALSE, /* partial_inplace */
1332 0, /* src_mask */
1333 0xffff, /* dst_mask */
1334 FALSE), /* pcrel_offset */
1335
1336 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1337 HOWTO (R_PPC64_DTPREL16_HIGHER,
1338 32, /* rightshift */
1339 1, /* size (0 = byte, 1 = short, 2 = long) */
1340 16, /* bitsize */
1341 FALSE, /* pc_relative */
1342 0, /* bitpos */
1343 complain_overflow_dont, /* complain_on_overflow */
1344 ppc64_elf_unhandled_reloc, /* special_function */
1345 "R_PPC64_DTPREL16_HIGHER", /* name */
1346 FALSE, /* partial_inplace */
1347 0, /* src_mask */
1348 0xffff, /* dst_mask */
1349 FALSE), /* pcrel_offset */
1350
1351 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1352 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1353 32, /* rightshift */
1354 1, /* size (0 = byte, 1 = short, 2 = long) */
1355 16, /* bitsize */
1356 FALSE, /* pc_relative */
1357 0, /* bitpos */
1358 complain_overflow_dont, /* complain_on_overflow */
1359 ppc64_elf_unhandled_reloc, /* special_function */
1360 "R_PPC64_DTPREL16_HIGHERA", /* name */
1361 FALSE, /* partial_inplace */
1362 0, /* src_mask */
1363 0xffff, /* dst_mask */
1364 FALSE), /* pcrel_offset */
1365
1366 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1367 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1368 48, /* rightshift */
1369 1, /* size (0 = byte, 1 = short, 2 = long) */
1370 16, /* bitsize */
1371 FALSE, /* pc_relative */
1372 0, /* bitpos */
1373 complain_overflow_dont, /* complain_on_overflow */
1374 ppc64_elf_unhandled_reloc, /* special_function */
1375 "R_PPC64_DTPREL16_HIGHEST", /* name */
1376 FALSE, /* partial_inplace */
1377 0, /* src_mask */
1378 0xffff, /* dst_mask */
1379 FALSE), /* pcrel_offset */
1380
1381 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1382 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1383 48, /* rightshift */
1384 1, /* size (0 = byte, 1 = short, 2 = long) */
1385 16, /* bitsize */
1386 FALSE, /* pc_relative */
1387 0, /* bitpos */
1388 complain_overflow_dont, /* complain_on_overflow */
1389 ppc64_elf_unhandled_reloc, /* special_function */
1390 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1391 FALSE, /* partial_inplace */
1392 0, /* src_mask */
1393 0xffff, /* dst_mask */
1394 FALSE), /* pcrel_offset */
1395
1396 /* Like DTPREL16, but for insns with a DS field. */
1397 HOWTO (R_PPC64_DTPREL16_DS,
1398 0, /* rightshift */
1399 1, /* size (0 = byte, 1 = short, 2 = long) */
1400 16, /* bitsize */
1401 FALSE, /* pc_relative */
1402 0, /* bitpos */
1403 complain_overflow_signed, /* complain_on_overflow */
1404 ppc64_elf_unhandled_reloc, /* special_function */
1405 "R_PPC64_DTPREL16_DS", /* name */
1406 FALSE, /* partial_inplace */
1407 0, /* src_mask */
1408 0xfffc, /* dst_mask */
1409 FALSE), /* pcrel_offset */
1410
1411 /* Like DTPREL16_DS, but no overflow. */
1412 HOWTO (R_PPC64_DTPREL16_LO_DS,
1413 0, /* rightshift */
1414 1, /* size (0 = byte, 1 = short, 2 = long) */
1415 16, /* bitsize */
1416 FALSE, /* pc_relative */
1417 0, /* bitpos */
1418 complain_overflow_dont, /* complain_on_overflow */
1419 ppc64_elf_unhandled_reloc, /* special_function */
1420 "R_PPC64_DTPREL16_LO_DS", /* name */
1421 FALSE, /* partial_inplace */
1422 0, /* src_mask */
1423 0xfffc, /* dst_mask */
1424 FALSE), /* pcrel_offset */
1425
1426 /* Computes a tp-relative displacement, the difference between the value of
1427 sym+add and the value of the thread pointer (r13). */
1428 HOWTO (R_PPC64_TPREL64,
1429 0, /* rightshift */
1430 4, /* size (0 = byte, 1 = short, 2 = long) */
1431 64, /* bitsize */
1432 FALSE, /* pc_relative */
1433 0, /* bitpos */
1434 complain_overflow_dont, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_TPREL64", /* name */
1437 FALSE, /* partial_inplace */
1438 0, /* src_mask */
1439 ONES (64), /* dst_mask */
1440 FALSE), /* pcrel_offset */
1441
1442 /* A 16 bit tprel reloc. */
1443 HOWTO (R_PPC64_TPREL16,
1444 0, /* rightshift */
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1446 16, /* bitsize */
1447 FALSE, /* pc_relative */
1448 0, /* bitpos */
1449 complain_overflow_signed, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_TPREL16", /* name */
1452 FALSE, /* partial_inplace */
1453 0, /* src_mask */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1456
1457 /* Like TPREL16, but no overflow. */
1458 HOWTO (R_PPC64_TPREL16_LO,
1459 0, /* rightshift */
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1461 16, /* bitsize */
1462 FALSE, /* pc_relative */
1463 0, /* bitpos */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_TPREL16_LO", /* name */
1467 FALSE, /* partial_inplace */
1468 0, /* src_mask */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1471
1472 /* Like TPREL16_LO, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_TPREL16_HI,
1474 16, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1476 16, /* bitsize */
1477 FALSE, /* pc_relative */
1478 0, /* bitpos */
1479 complain_overflow_dont, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_TPREL16_HI", /* name */
1482 FALSE, /* partial_inplace */
1483 0, /* src_mask */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1486
1487 /* Like TPREL16_HI, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_TPREL16_HA,
1489 16, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1491 16, /* bitsize */
1492 FALSE, /* pc_relative */
1493 0, /* bitpos */
1494 complain_overflow_dont, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_TPREL16_HA", /* name */
1497 FALSE, /* partial_inplace */
1498 0, /* src_mask */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1501
1502 /* Like TPREL16_HI, but next higher group of 16 bits. */
1503 HOWTO (R_PPC64_TPREL16_HIGHER,
1504 32, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1506 16, /* bitsize */
1507 FALSE, /* pc_relative */
1508 0, /* bitpos */
1509 complain_overflow_dont, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_TPREL16_HIGHER", /* name */
1512 FALSE, /* partial_inplace */
1513 0, /* src_mask */
1514 0xffff, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1516
1517 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1518 HOWTO (R_PPC64_TPREL16_HIGHERA,
1519 32, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1521 16, /* bitsize */
1522 FALSE, /* pc_relative */
1523 0, /* bitpos */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_TPREL16_HIGHERA", /* name */
1527 FALSE, /* partial_inplace */
1528 0, /* src_mask */
1529 0xffff, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1531
1532 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1533 HOWTO (R_PPC64_TPREL16_HIGHEST,
1534 48, /* rightshift */
1535 1, /* size (0 = byte, 1 = short, 2 = long) */
1536 16, /* bitsize */
1537 FALSE, /* pc_relative */
1538 0, /* bitpos */
1539 complain_overflow_dont, /* complain_on_overflow */
1540 ppc64_elf_unhandled_reloc, /* special_function */
1541 "R_PPC64_TPREL16_HIGHEST", /* name */
1542 FALSE, /* partial_inplace */
1543 0, /* src_mask */
1544 0xffff, /* dst_mask */
1545 FALSE), /* pcrel_offset */
1546
1547 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1548 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1549 48, /* rightshift */
1550 1, /* size (0 = byte, 1 = short, 2 = long) */
1551 16, /* bitsize */
1552 FALSE, /* pc_relative */
1553 0, /* bitpos */
1554 complain_overflow_dont, /* complain_on_overflow */
1555 ppc64_elf_unhandled_reloc, /* special_function */
1556 "R_PPC64_TPREL16_HIGHESTA", /* name */
1557 FALSE, /* partial_inplace */
1558 0, /* src_mask */
1559 0xffff, /* dst_mask */
1560 FALSE), /* pcrel_offset */
1561
1562 /* Like TPREL16, but for insns with a DS field. */
1563 HOWTO (R_PPC64_TPREL16_DS,
1564 0, /* rightshift */
1565 1, /* size (0 = byte, 1 = short, 2 = long) */
1566 16, /* bitsize */
1567 FALSE, /* pc_relative */
1568 0, /* bitpos */
1569 complain_overflow_signed, /* complain_on_overflow */
1570 ppc64_elf_unhandled_reloc, /* special_function */
1571 "R_PPC64_TPREL16_DS", /* name */
1572 FALSE, /* partial_inplace */
1573 0, /* src_mask */
1574 0xfffc, /* dst_mask */
1575 FALSE), /* pcrel_offset */
1576
1577 /* Like TPREL16_DS, but no overflow. */
1578 HOWTO (R_PPC64_TPREL16_LO_DS,
1579 0, /* rightshift */
1580 1, /* size (0 = byte, 1 = short, 2 = long) */
1581 16, /* bitsize */
1582 FALSE, /* pc_relative */
1583 0, /* bitpos */
1584 complain_overflow_dont, /* complain_on_overflow */
1585 ppc64_elf_unhandled_reloc, /* special_function */
1586 "R_PPC64_TPREL16_LO_DS", /* name */
1587 FALSE, /* partial_inplace */
1588 0, /* src_mask */
1589 0xfffc, /* dst_mask */
1590 FALSE), /* pcrel_offset */
1591
1592 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1593 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1594 to the first entry relative to the TOC base (r2). */
1595 HOWTO (R_PPC64_GOT_TLSGD16,
1596 0, /* rightshift */
1597 1, /* size (0 = byte, 1 = short, 2 = long) */
1598 16, /* bitsize */
1599 FALSE, /* pc_relative */
1600 0, /* bitpos */
1601 complain_overflow_signed, /* complain_on_overflow */
1602 ppc64_elf_unhandled_reloc, /* special_function */
1603 "R_PPC64_GOT_TLSGD16", /* name */
b34976b6 1604 FALSE, /* partial_inplace */
5bd4f169
AM
1605 0, /* src_mask */
1606 0xffff, /* dst_mask */
b34976b6 1607 FALSE), /* pcrel_offset */
5bd4f169 1608
411e1bfb
AM
1609 /* Like GOT_TLSGD16, but no overflow. */
1610 HOWTO (R_PPC64_GOT_TLSGD16_LO,
5bd4f169
AM
1611 0, /* rightshift */
1612 1, /* size (0 = byte, 1 = short, 2 = long) */
1613 16, /* bitsize */
b34976b6 1614 FALSE, /* pc_relative */
5bd4f169
AM
1615 0, /* bitpos */
1616 complain_overflow_dont, /* complain_on_overflow */
805fc799 1617 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1618 "R_PPC64_GOT_TLSGD16_LO", /* name */
b34976b6 1619 FALSE, /* partial_inplace */
5bd4f169
AM
1620 0, /* src_mask */
1621 0xffff, /* dst_mask */
b34976b6 1622 FALSE), /* pcrel_offset */
5bd4f169 1623
411e1bfb
AM
1624 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1625 HOWTO (R_PPC64_GOT_TLSGD16_HI,
5bd4f169
AM
1626 16, /* rightshift */
1627 1, /* size (0 = byte, 1 = short, 2 = long) */
1628 16, /* bitsize */
b34976b6 1629 FALSE, /* pc_relative */
5bd4f169
AM
1630 0, /* bitpos */
1631 complain_overflow_dont, /* complain_on_overflow */
805fc799 1632 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1633 "R_PPC64_GOT_TLSGD16_HI", /* name */
b34976b6 1634 FALSE, /* partial_inplace */
5bd4f169
AM
1635 0, /* src_mask */
1636 0xffff, /* dst_mask */
b34976b6 1637 FALSE), /* pcrel_offset */
5bd4f169 1638
411e1bfb
AM
1639 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1640 HOWTO (R_PPC64_GOT_TLSGD16_HA,
5bd4f169
AM
1641 16, /* rightshift */
1642 1, /* size (0 = byte, 1 = short, 2 = long) */
1643 16, /* bitsize */
b34976b6 1644 FALSE, /* pc_relative */
5bd4f169 1645 0, /* bitpos */
411e1bfb 1646 complain_overflow_dont, /* complain_on_overflow */
805fc799 1647 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1648 "R_PPC64_GOT_TLSGD16_HA", /* name */
b34976b6 1649 FALSE, /* partial_inplace */
5bd4f169
AM
1650 0, /* src_mask */
1651 0xffff, /* dst_mask */
b34976b6 1652 FALSE), /* pcrel_offset */
5bd4f169 1653
411e1bfb
AM
1654 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1655 with values (sym+add)@dtpmod and zero, and computes the offset to the
1656 first entry relative to the TOC base (r2). */
1657 HOWTO (R_PPC64_GOT_TLSLD16,
5bd4f169
AM
1658 0, /* rightshift */
1659 1, /* size (0 = byte, 1 = short, 2 = long) */
1660 16, /* bitsize */
b34976b6 1661 FALSE, /* pc_relative */
5bd4f169 1662 0, /* bitpos */
411e1bfb
AM
1663 complain_overflow_signed, /* complain_on_overflow */
1664 ppc64_elf_unhandled_reloc, /* special_function */
1665 "R_PPC64_GOT_TLSLD16", /* name */
b34976b6 1666 FALSE, /* partial_inplace */
d006db6c 1667 0, /* src_mask */
411e1bfb 1668 0xffff, /* dst_mask */
b34976b6 1669 FALSE), /* pcrel_offset */
5bd4f169 1670
411e1bfb
AM
1671 /* Like GOT_TLSLD16, but no overflow. */
1672 HOWTO (R_PPC64_GOT_TLSLD16_LO,
5bd4f169
AM
1673 0, /* rightshift */
1674 1, /* size (0 = byte, 1 = short, 2 = long) */
1675 16, /* bitsize */
b34976b6 1676 FALSE, /* pc_relative */
5bd4f169 1677 0, /* bitpos */
411e1bfb
AM
1678 complain_overflow_dont, /* complain_on_overflow */
1679 ppc64_elf_unhandled_reloc, /* special_function */
1680 "R_PPC64_GOT_TLSLD16_LO", /* name */
b34976b6 1681 FALSE, /* partial_inplace */
d006db6c 1682 0, /* src_mask */
411e1bfb 1683 0xffff, /* dst_mask */
b34976b6 1684 FALSE), /* pcrel_offset */
5bd4f169 1685
411e1bfb
AM
1686 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1687 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1688 16, /* rightshift */
5bd4f169
AM
1689 1, /* size (0 = byte, 1 = short, 2 = long) */
1690 16, /* bitsize */
b34976b6 1691 FALSE, /* pc_relative */
5bd4f169 1692 0, /* bitpos */
411e1bfb 1693 complain_overflow_dont, /* complain_on_overflow */
805fc799 1694 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1695 "R_PPC64_GOT_TLSLD16_HI", /* name */
b34976b6 1696 FALSE, /* partial_inplace */
d006db6c 1697 0, /* src_mask */
411e1bfb 1698 0xffff, /* dst_mask */
b34976b6 1699 FALSE), /* pcrel_offset */
5bd4f169 1700
411e1bfb
AM
1701 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1702 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1703 16, /* rightshift */
5bd4f169
AM
1704 1, /* size (0 = byte, 1 = short, 2 = long) */
1705 16, /* bitsize */
b34976b6 1706 FALSE, /* pc_relative */
5bd4f169
AM
1707 0, /* bitpos */
1708 complain_overflow_dont, /* complain_on_overflow */
805fc799 1709 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1710 "R_PPC64_GOT_TLSLD16_HA", /* name */
b34976b6 1711 FALSE, /* partial_inplace */
d006db6c 1712 0, /* src_mask */
411e1bfb 1713 0xffff, /* dst_mask */
b34976b6 1714 FALSE), /* pcrel_offset */
5bd4f169 1715
411e1bfb
AM
1716 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1717 the offset to the entry relative to the TOC base (r2). */
1718 HOWTO (R_PPC64_GOT_DTPREL16_DS,
5bd4f169
AM
1719 0, /* rightshift */
1720 1, /* size (0 = byte, 1 = short, 2 = long) */
1721 16, /* bitsize */
b34976b6 1722 FALSE, /* pc_relative */
5bd4f169 1723 0, /* bitpos */
411e1bfb 1724 complain_overflow_signed, /* complain_on_overflow */
805fc799 1725 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1726 "R_PPC64_GOT_DTPREL16_DS", /* name */
b34976b6 1727 FALSE, /* partial_inplace */
d006db6c 1728 0, /* src_mask */
5bd4f169 1729 0xfffc, /* dst_mask */
b34976b6 1730 FALSE), /* pcrel_offset */
5bd4f169 1731
411e1bfb
AM
1732 /* Like GOT_DTPREL16_DS, but no overflow. */
1733 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
5bd4f169 1734 0, /* rightshift */
c061c2d8
AM
1735 1, /* size (0 = byte, 1 = short, 2 = long) */
1736 16, /* bitsize */
b34976b6 1737 FALSE, /* pc_relative */
5bd4f169 1738 0, /* bitpos */
411e1bfb
AM
1739 complain_overflow_dont, /* complain_on_overflow */
1740 ppc64_elf_unhandled_reloc, /* special_function */
1741 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
b34976b6 1742 FALSE, /* partial_inplace */
d006db6c 1743 0, /* src_mask */
c061c2d8 1744 0xfffc, /* dst_mask */
b34976b6 1745 FALSE), /* pcrel_offset */
5bd4f169 1746
411e1bfb
AM
1747 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1748 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1749 16, /* rightshift */
5bd4f169
AM
1750 1, /* size (0 = byte, 1 = short, 2 = long) */
1751 16, /* bitsize */
b34976b6 1752 FALSE, /* pc_relative */
5bd4f169
AM
1753 0, /* bitpos */
1754 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1755 ppc64_elf_unhandled_reloc, /* special_function */
1756 "R_PPC64_GOT_DTPREL16_HI", /* name */
b34976b6 1757 FALSE, /* partial_inplace */
d006db6c 1758 0, /* src_mask */
411e1bfb 1759 0xffff, /* dst_mask */
b34976b6 1760 FALSE), /* pcrel_offset */
5bd4f169 1761
411e1bfb
AM
1762 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1763 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1764 16, /* rightshift */
1765 1, /* size (0 = byte, 1 = short, 2 = long) */
1766 16, /* bitsize */
1767 FALSE, /* pc_relative */
1768 0, /* bitpos */
1769 complain_overflow_dont, /* complain_on_overflow */
1770 ppc64_elf_unhandled_reloc, /* special_function */
1771 "R_PPC64_GOT_DTPREL16_HA", /* name */
1772 FALSE, /* partial_inplace */
1773 0, /* src_mask */
1774 0xffff, /* dst_mask */
1775 FALSE), /* pcrel_offset */
1776
1777 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1778 offset to the entry relative to the TOC base (r2). */
1779 HOWTO (R_PPC64_GOT_TPREL16_DS,
5bd4f169
AM
1780 0, /* rightshift */
1781 1, /* size (0 = byte, 1 = short, 2 = long) */
1782 16, /* bitsize */
b34976b6 1783 FALSE, /* pc_relative */
5bd4f169
AM
1784 0, /* bitpos */
1785 complain_overflow_signed, /* complain_on_overflow */
411e1bfb
AM
1786 ppc64_elf_unhandled_reloc, /* special_function */
1787 "R_PPC64_GOT_TPREL16_DS", /* name */
b34976b6 1788 FALSE, /* partial_inplace */
d006db6c 1789 0, /* src_mask */
ad8e1ba5 1790 0xfffc, /* dst_mask */
b34976b6 1791 FALSE), /* pcrel_offset */
5bd4f169 1792
411e1bfb
AM
1793 /* Like GOT_TPREL16_DS, but no overflow. */
1794 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
5bd4f169
AM
1795 0, /* rightshift */
1796 1, /* size (0 = byte, 1 = short, 2 = long) */
1797 16, /* bitsize */
b34976b6 1798 FALSE, /* pc_relative */
5bd4f169
AM
1799 0, /* bitpos */
1800 complain_overflow_dont, /* complain_on_overflow */
411e1bfb
AM
1801 ppc64_elf_unhandled_reloc, /* special_function */
1802 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
b34976b6 1803 FALSE, /* partial_inplace */
d006db6c 1804 0, /* src_mask */
ad8e1ba5 1805 0xfffc, /* dst_mask */
b34976b6 1806 FALSE), /* pcrel_offset */
5bd4f169 1807
411e1bfb
AM
1808 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1809 HOWTO (R_PPC64_GOT_TPREL16_HI,
1810 16, /* rightshift */
5bd4f169
AM
1811 1, /* size (0 = byte, 1 = short, 2 = long) */
1812 16, /* bitsize */
b34976b6 1813 FALSE, /* pc_relative */
5bd4f169 1814 0, /* bitpos */
411e1bfb 1815 complain_overflow_dont, /* complain_on_overflow */
805fc799 1816 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1817 "R_PPC64_GOT_TPREL16_HI", /* name */
b34976b6 1818 FALSE, /* partial_inplace */
d006db6c 1819 0, /* src_mask */
411e1bfb 1820 0xffff, /* dst_mask */
b34976b6 1821 FALSE), /* pcrel_offset */
5bd4f169 1822
411e1bfb
AM
1823 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1824 HOWTO (R_PPC64_GOT_TPREL16_HA,
1825 16, /* rightshift */
5bd4f169
AM
1826 1, /* size (0 = byte, 1 = short, 2 = long) */
1827 16, /* bitsize */
b34976b6 1828 FALSE, /* pc_relative */
5bd4f169
AM
1829 0, /* bitpos */
1830 complain_overflow_dont, /* complain_on_overflow */
805fc799 1831 ppc64_elf_unhandled_reloc, /* special_function */
411e1bfb 1832 "R_PPC64_GOT_TPREL16_HA", /* name */
b34976b6 1833 FALSE, /* partial_inplace */
d006db6c 1834 0, /* src_mask */
411e1bfb 1835 0xffff, /* dst_mask */
b34976b6 1836 FALSE), /* pcrel_offset */
5bd4f169
AM
1837
1838 /* GNU extension to record C++ vtable hierarchy. */
1839 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
1840 0, /* rightshift */
1841 0, /* size (0 = byte, 1 = short, 2 = long) */
1842 0, /* bitsize */
b34976b6 1843 FALSE, /* pc_relative */
5bd4f169
AM
1844 0, /* bitpos */
1845 complain_overflow_dont, /* complain_on_overflow */
1846 NULL, /* special_function */
1847 "R_PPC64_GNU_VTINHERIT", /* name */
b34976b6 1848 FALSE, /* partial_inplace */
5bd4f169
AM
1849 0, /* src_mask */
1850 0, /* dst_mask */
b34976b6 1851 FALSE), /* pcrel_offset */
5bd4f169
AM
1852
1853 /* GNU extension to record C++ vtable member usage. */
1854 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
1855 0, /* rightshift */
1856 0, /* size (0 = byte, 1 = short, 2 = long) */
1857 0, /* bitsize */
b34976b6 1858 FALSE, /* pc_relative */
5bd4f169
AM
1859 0, /* bitpos */
1860 complain_overflow_dont, /* complain_on_overflow */
1861 NULL, /* special_function */
1862 "R_PPC64_GNU_VTENTRY", /* name */
b34976b6 1863 FALSE, /* partial_inplace */
5bd4f169
AM
1864 0, /* src_mask */
1865 0, /* dst_mask */
b34976b6 1866 FALSE), /* pcrel_offset */
5bd4f169
AM
1867};
1868
1869\f
1870/* Initialize the ppc64_elf_howto_table, so that linear accesses can
1871 be done. */
1872
1873static void
4ce794b7 1874ppc_howto_init (void)
5bd4f169
AM
1875{
1876 unsigned int i, type;
1877
1878 for (i = 0;
1879 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
1880 i++)
1881 {
1882 type = ppc64_elf_howto_raw[i].type;
1883 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
1884 / sizeof (ppc64_elf_howto_table[0])));
1885 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1886 }
1887}
1888
1889static reloc_howto_type *
4ce794b7
AM
1890ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1891 bfd_reloc_code_real_type code)
5bd4f169 1892{
411e1bfb 1893 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
5bd4f169
AM
1894
1895 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1896 /* Initialize howto table if needed. */
1897 ppc_howto_init ();
1898
4ce794b7 1899 switch (code)
5bd4f169
AM
1900 {
1901 default:
4ce794b7 1902 return NULL;
5bd4f169 1903
411e1bfb
AM
1904 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1905 break;
1906 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1907 break;
1908 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1909 break;
1910 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1911 break;
1912 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1913 break;
1914 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1915 break;
1916 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
5bd4f169 1917 break;
411e1bfb 1918 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
5bd4f169 1919 break;
411e1bfb 1920 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
5bd4f169 1921 break;
411e1bfb 1922 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
5bd4f169 1923 break;
411e1bfb 1924 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
5bd4f169 1925 break;
411e1bfb 1926 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
5bd4f169 1927 break;
411e1bfb 1928 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
5bd4f169 1929 break;
411e1bfb 1930 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
5bd4f169 1931 break;
411e1bfb 1932 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
5bd4f169 1933 break;
411e1bfb 1934 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
5bd4f169 1935 break;
411e1bfb 1936 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
5bd4f169 1937 break;
411e1bfb 1938 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
5bd4f169 1939 break;
411e1bfb 1940 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
5bd4f169 1941 break;
411e1bfb 1942 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
5bd4f169 1943 break;
411e1bfb 1944 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
5bd4f169 1945 break;
411e1bfb 1946 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
5bd4f169 1947 break;
411e1bfb 1948 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
5bd4f169 1949 break;
411e1bfb 1950 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
5bd4f169 1951 break;
411e1bfb 1952 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
5bd4f169 1953 break;
411e1bfb 1954 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
5bd4f169 1955 break;
411e1bfb 1956 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
5bd4f169 1957 break;
411e1bfb 1958 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
5bd4f169 1959 break;
411e1bfb 1960 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
5bd4f169 1961 break;
411e1bfb 1962 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
5bd4f169 1963 break;
411e1bfb 1964 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
5bd4f169 1965 break;
411e1bfb 1966 case BFD_RELOC_64: r = R_PPC64_ADDR64;
5bd4f169 1967 break;
411e1bfb 1968 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
5bd4f169 1969 break;
411e1bfb 1970 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
5bd4f169 1971 break;
411e1bfb 1972 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
5bd4f169 1973 break;
411e1bfb 1974 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
5bd4f169 1975 break;
411e1bfb 1976 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
5bd4f169 1977 break;
411e1bfb 1978 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
5bd4f169 1979 break;
411e1bfb 1980 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
5bd4f169 1981 break;
411e1bfb 1982 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
5bd4f169 1983 break;
411e1bfb 1984 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
5bd4f169 1985 break;
411e1bfb 1986 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
5bd4f169 1987 break;
411e1bfb 1988 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
5bd4f169 1989 break;
411e1bfb 1990 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
5bd4f169 1991 break;
411e1bfb 1992 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
5bd4f169 1993 break;
411e1bfb 1994 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
5bd4f169 1995 break;
411e1bfb 1996 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
5bd4f169 1997 break;
411e1bfb 1998 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
5bd4f169 1999 break;
411e1bfb 2000 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
5bd4f169 2001 break;
411e1bfb 2002 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
5bd4f169 2003 break;
411e1bfb 2004 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
5bd4f169 2005 break;
411e1bfb 2006 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
5bd4f169 2007 break;
411e1bfb 2008 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
5bd4f169 2009 break;
411e1bfb 2010 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
5bd4f169 2011 break;
411e1bfb 2012 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
5bd4f169 2013 break;
411e1bfb 2014 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
5bd4f169 2015 break;
411e1bfb 2016 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
5bd4f169 2017 break;
411e1bfb 2018 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
5bd4f169 2019 break;
411e1bfb 2020 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
5bd4f169 2021 break;
411e1bfb 2022 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
5bd4f169 2023 break;
411e1bfb 2024 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
5bd4f169 2025 break;
411e1bfb 2026 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
5bd4f169 2027 break;
411e1bfb 2028 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
5bd4f169 2029 break;
411e1bfb 2030 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
5bd4f169 2031 break;
411e1bfb 2032 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
5bd4f169 2033 break;
411e1bfb 2034 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
5bd4f169 2035 break;
411e1bfb
AM
2036 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2037 break;
2038 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2039 break;
2040 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2041 break;
2042 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2043 break;
2044 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2045 break;
2046 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2047 break;
2048 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2049 break;
2050 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2051 break;
2052 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2053 break;
2054 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2055 break;
2056 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2057 break;
2058 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2059 break;
2060 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2061 break;
2062 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2063 break;
2064 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2065 break;
2066 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2067 break;
2068 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2069 break;
2070 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2071 break;
2072 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2073 break;
2074 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2075 break;
2076 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2077 break;
2078 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2079 break;
2080 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2081 break;
2082 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2083 break;
2084 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2085 break;
2086 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2087 break;
2088 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2089 break;
2090 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2091 break;
2092 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2093 break;
2094 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2095 break;
2096 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2097 break;
2098 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2099 break;
2100 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2101 break;
2102 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2103 break;
2104 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
5bd4f169
AM
2105 break;
2106 }
2107
4ce794b7 2108 return ppc64_elf_howto_table[r];
5bd4f169
AM
2109};
2110
2111/* Set the howto pointer for a PowerPC ELF reloc. */
2112
2113static void
4ce794b7
AM
2114ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2115 Elf_Internal_Rela *dst)
5bd4f169 2116{
65f38f15
AM
2117 unsigned int type;
2118
ef60b7ff 2119 /* Initialize howto table if needed. */
5bd4f169 2120 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
2121 ppc_howto_init ();
2122
65f38f15 2123 type = ELF64_R_TYPE (dst->r_info);
d0fb9a8d
JJ
2124 if (type >= (sizeof (ppc64_elf_howto_table)
2125 / sizeof (ppc64_elf_howto_table[0])))
2126 {
2127 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
3ec2b351
NC
2128 abfd, (int) type);
2129 type = R_PPC64_NONE;
d0fb9a8d 2130 }
65f38f15 2131 cache_ptr->howto = ppc64_elf_howto_table[type];
5bd4f169
AM
2132}
2133
04c9666a 2134/* Handle the R_PPC64_ADDR16_HA and similar relocs. */
5bd4f169
AM
2135
2136static bfd_reloc_status_type
4ce794b7
AM
2137ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2138 void *data, asection *input_section,
2139 bfd *output_bfd, char **error_message)
5bd4f169 2140{
805fc799
AM
2141 /* If this is a relocatable link (output_bfd test tells us), just
2142 call the generic function. Any adjustment will be done at final
2143 link time. */
2144 if (output_bfd != NULL)
cedb70c5 2145 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2146 input_section, output_bfd, error_message);
2147
2148 /* Adjust the addend for sign extension of the low 16 bits.
2149 We won't actually be using the low 16 bits, so trashing them
2150 doesn't matter. */
2151 reloc_entry->addend += 0x8000;
2152 return bfd_reloc_continue;
2153}
5bd4f169 2154
2441e016
AM
2155static bfd_reloc_status_type
2156ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2157 void *data, asection *input_section,
2158 bfd *output_bfd, char **error_message)
2159{
2160 if (output_bfd != NULL)
2161 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2162 input_section, output_bfd, error_message);
2163
699733f6
AM
2164 if (strcmp (symbol->section->name, ".opd") == 0
2165 && (symbol->section->owner->flags & DYNAMIC) == 0)
2441e016
AM
2166 {
2167 bfd_vma dest = opd_entry_value (symbol->section,
2168 symbol->value + reloc_entry->addend,
2169 NULL, NULL);
2170 if (dest != (bfd_vma) -1)
2171 reloc_entry->addend = dest - (symbol->value
2172 + symbol->section->output_section->vma
2173 + symbol->section->output_offset);
2174 }
2175 return bfd_reloc_continue;
2176}
2177
805fc799 2178static bfd_reloc_status_type
4ce794b7
AM
2179ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2180 void *data, asection *input_section,
2181 bfd *output_bfd, char **error_message)
805fc799
AM
2182{
2183 long insn;
04c9666a 2184 enum elf_ppc64_reloc_type r_type;
805fc799
AM
2185 bfd_size_type octets;
2186 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 2187 bfd_boolean is_power4 = FALSE;
805fc799
AM
2188
2189 /* If this is a relocatable link (output_bfd test tells us), just
2190 call the generic function. Any adjustment will be done at final
2191 link time. */
5bd4f169 2192 if (output_bfd != NULL)
cedb70c5 2193 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2194 input_section, output_bfd, error_message);
2195
2196 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2197 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2198 insn &= ~(0x01 << 21);
4ce794b7 2199 r_type = reloc_entry->howto->type;
805fc799
AM
2200 if (r_type == R_PPC64_ADDR14_BRTAKEN
2201 || r_type == R_PPC64_REL14_BRTAKEN)
cedb70c5 2202 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
805fc799
AM
2203
2204 if (is_power4)
5bd4f169 2205 {
805fc799
AM
2206 /* Set 'a' bit. This is 0b00010 in BO field for branch
2207 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2208 for branch on CTR insns (BO == 1a00t or 1a01t). */
2209 if ((insn & (0x14 << 21)) == (0x04 << 21))
2210 insn |= 0x02 << 21;
2211 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2212 insn |= 0x08 << 21;
2213 else
2441e016 2214 goto out;
5bd4f169 2215 }
805fc799
AM
2216 else
2217 {
2218 bfd_vma target = 0;
2219 bfd_vma from;
5bd4f169 2220
805fc799
AM
2221 if (!bfd_is_com_section (symbol->section))
2222 target = symbol->value;
2223 target += symbol->section->output_section->vma;
2224 target += symbol->section->output_offset;
2225 target += reloc_entry->addend;
5bd4f169 2226
805fc799
AM
2227 from = (reloc_entry->address
2228 + input_section->output_offset
2229 + input_section->output_section->vma);
5bd4f169 2230
805fc799
AM
2231 /* Invert 'y' bit if not the default. */
2232 if ((bfd_signed_vma) (target - from) < 0)
2233 insn ^= 0x01 << 21;
2234 }
4ce794b7 2235 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2441e016
AM
2236 out:
2237 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2238 input_section, output_bfd, error_message);
805fc799 2239}
5bd4f169 2240
805fc799 2241static bfd_reloc_status_type
4ce794b7
AM
2242ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2243 void *data, asection *input_section,
2244 bfd *output_bfd, char **error_message)
805fc799
AM
2245{
2246 /* If this is a relocatable link (output_bfd test tells us), just
2247 call the generic function. Any adjustment will be done at final
2248 link time. */
2249 if (output_bfd != NULL)
cedb70c5 2250 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799 2251 input_section, output_bfd, error_message);
5bd4f169 2252
805fc799
AM
2253 /* Subtract the symbol section base address. */
2254 reloc_entry->addend -= symbol->section->output_section->vma;
5bd4f169
AM
2255 return bfd_reloc_continue;
2256}
2257
805fc799 2258static bfd_reloc_status_type
4ce794b7
AM
2259ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2260 void *data, asection *input_section,
2261 bfd *output_bfd, char **error_message)
805fc799
AM
2262{
2263 /* If this is a relocatable link (output_bfd test tells us), just
2264 call the generic function. Any adjustment will be done at final
2265 link time. */
2266 if (output_bfd != NULL)
cedb70c5 2267 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2268 input_section, output_bfd, error_message);
2269
2270 /* Subtract the symbol section base address. */
2271 reloc_entry->addend -= symbol->section->output_section->vma;
2272
2273 /* Adjust the addend for sign extension of the low 16 bits. */
2274 reloc_entry->addend += 0x8000;
2275 return bfd_reloc_continue;
2276}
2277
2278static bfd_reloc_status_type
4ce794b7
AM
2279ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2280 void *data, asection *input_section,
2281 bfd *output_bfd, char **error_message)
805fc799
AM
2282{
2283 bfd_vma TOCstart;
2284
2285 /* If this is a relocatable link (output_bfd test tells us), just
2286 call the generic function. Any adjustment will be done at final
2287 link time. */
2288 if (output_bfd != NULL)
cedb70c5 2289 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2290 input_section, output_bfd, error_message);
2291
2292 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2293 if (TOCstart == 0)
2294 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2295
2296 /* Subtract the TOC base address. */
2297 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2298 return bfd_reloc_continue;
2299}
2300
2301static bfd_reloc_status_type
4ce794b7
AM
2302ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2303 void *data, asection *input_section,
2304 bfd *output_bfd, char **error_message)
805fc799
AM
2305{
2306 bfd_vma TOCstart;
2307
2308 /* If this is a relocatable link (output_bfd test tells us), just
2309 call the generic function. Any adjustment will be done at final
2310 link time. */
2311 if (output_bfd != NULL)
cedb70c5 2312 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2313 input_section, output_bfd, error_message);
2314
2315 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2316 if (TOCstart == 0)
2317 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2318
2319 /* Subtract the TOC base address. */
2320 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2321
2322 /* Adjust the addend for sign extension of the low 16 bits. */
2323 reloc_entry->addend += 0x8000;
2324 return bfd_reloc_continue;
2325}
2326
2327static bfd_reloc_status_type
4ce794b7
AM
2328ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2329 void *data, asection *input_section,
2330 bfd *output_bfd, char **error_message)
805fc799
AM
2331{
2332 bfd_vma TOCstart;
2333 bfd_size_type octets;
2334
2335 /* If this is a relocatable link (output_bfd test tells us), just
2336 call the generic function. Any adjustment will be done at final
2337 link time. */
2338 if (output_bfd != NULL)
cedb70c5 2339 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2340 input_section, output_bfd, error_message);
2341
2342 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2343 if (TOCstart == 0)
2344 TOCstart = ppc64_elf_toc (input_section->output_section->owner);
2345
2346 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2347 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2348 return bfd_reloc_ok;
2349}
2350
2351static bfd_reloc_status_type
4ce794b7
AM
2352ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2353 void *data, asection *input_section,
2354 bfd *output_bfd, char **error_message)
805fc799
AM
2355{
2356 /* If this is a relocatable link (output_bfd test tells us), just
2357 call the generic function. Any adjustment will be done at final
2358 link time. */
2359 if (output_bfd != NULL)
cedb70c5 2360 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
805fc799
AM
2361 input_section, output_bfd, error_message);
2362
2363 if (error_message != NULL)
2364 {
2365 static char buf[60];
2366 sprintf (buf, "generic linker can't handle %s",
2367 reloc_entry->howto->name);
2368 *error_message = buf;
2369 }
2370 return bfd_reloc_dangerous;
2371}
2372
e717da7e
AM
2373struct ppc64_elf_obj_tdata
2374{
2375 struct elf_obj_tdata elf;
2376
2377 /* Shortcuts to dynamic linker sections. */
2378 asection *got;
2379 asection *relgot;
2380
b3fac117
AM
2381 /* Used during garbage collection. We attach global symbols defined
2382 on removed .opd entries to this section so that the sym is removed. */
2383 asection *deleted_section;
81688140 2384
e717da7e
AM
2385 /* TLS local dynamic got entry handling. Suppose for multiple GOT
2386 sections means we potentially need one of these for each input bfd. */
2387 union {
2388 bfd_signed_vma refcount;
2389 bfd_vma offset;
2390 } tlsld_got;
8860955f
AM
2391
2392 /* A copy of relocs before they are modified for --emit-relocs. */
2393 Elf_Internal_Rela *opd_relocs;
e717da7e
AM
2394};
2395
2396#define ppc64_elf_tdata(bfd) \
2397 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2398
2399#define ppc64_tlsld_got(bfd) \
2400 (&ppc64_elf_tdata (bfd)->tlsld_got)
2401
2402/* Override the generic function because we store some extras. */
2403
2404static bfd_boolean
2405ppc64_elf_mkobject (bfd *abfd)
2406{
e717da7e 2407 if (abfd->tdata.any == NULL)
62d7a5f6
AM
2408 {
2409 bfd_size_type amt = sizeof (struct ppc64_elf_obj_tdata);
2410 abfd->tdata.any = bfd_zalloc (abfd, amt);
2411 if (abfd->tdata.any == NULL)
2412 return FALSE;
2413 }
2414 return bfd_elf_mkobject (abfd);
e717da7e
AM
2415}
2416
ee75fd95
AM
2417/* Return 1 if target is one of ours. */
2418
7b53ace3 2419static bfd_boolean
ee75fd95 2420is_ppc64_elf_target (const struct bfd_target *targ)
7b53ace3
AM
2421{
2422 extern const bfd_target bfd_elf64_powerpc_vec;
2423 extern const bfd_target bfd_elf64_powerpcle_vec;
2424
2425 return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
2426}
2427
feee612b
AM
2428/* Fix bad default arch selected for a 64 bit input bfd when the
2429 default is 32 bit. */
2430
b34976b6 2431static bfd_boolean
4ce794b7 2432ppc64_elf_object_p (bfd *abfd)
feee612b
AM
2433{
2434 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2435 {
2436 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2437
2438 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2439 {
2440 /* Relies on arch after 32 bit default being 64 bit default. */
2441 abfd->arch_info = abfd->arch_info->next;
2442 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2443 }
2444 }
b34976b6 2445 return TRUE;
feee612b
AM
2446}
2447
d37c89e5
AM
2448/* Support for core dump NOTE sections. */
2449
2450static bfd_boolean
2451ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2452{
eea6121a 2453 size_t offset, size;
d37c89e5
AM
2454
2455 if (note->descsz != 504)
2456 return FALSE;
2457
2458 /* pr_cursig */
2459 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
2460
2461 /* pr_pid */
2462 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 32);
2463
2464 /* pr_reg */
2465 offset = 112;
eea6121a 2466 size = 384;
d37c89e5
AM
2467
2468 /* Make a ".reg/999" section. */
2469 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
eea6121a 2470 size, note->descpos + offset);
d37c89e5
AM
2471}
2472
2473static bfd_boolean
2474ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2475{
2476 if (note->descsz != 136)
2477 return FALSE;
2478
2479 elf_tdata (abfd)->core_program
2480 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2481 elf_tdata (abfd)->core_command
2482 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2483
2484 return TRUE;
2485}
2486
5bd4f169
AM
2487/* Merge backend specific data from an object file to the output
2488 object file when linking. */
2f6d9989 2489
b34976b6 2490static bfd_boolean
4ce794b7 2491ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5bd4f169 2492{
5bd4f169
AM
2493 /* Check if we have the same endianess. */
2494 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
87e226ce 2495 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
5bd4f169
AM
2496 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
2497 {
2498 const char *msg;
2499
2500 if (bfd_big_endian (ibfd))
d003868e 2501 msg = _("%B: compiled for a big endian system "
4ce794b7 2502 "and target is little endian");
5bd4f169 2503 else
d003868e 2504 msg = _("%B: compiled for a little endian system "
4ce794b7 2505 "and target is big endian");
5bd4f169 2506
d003868e 2507 (*_bfd_error_handler) (msg, ibfd);
5bd4f169
AM
2508
2509 bfd_set_error (bfd_error_wrong_format);
b34976b6 2510 return FALSE;
5bd4f169
AM
2511 }
2512
b34976b6 2513 return TRUE;
5bd4f169 2514}
f0abc2a1 2515
5d35169e
AM
2516/* Add extra PPC sections. */
2517
b35d266b 2518static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
7f4d3958 2519{
0112cd26
NC
2520 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2521 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2522 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2523 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2524 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2525 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2526 { NULL, 0, 0, 0, 0 }
5d35169e
AM
2527};
2528
7c8fe5c4
AM
2529enum _ppc64_sec_type {
2530 sec_normal = 0,
2531 sec_opd = 1,
2532 sec_toc = 2
2533};
2534
f0abc2a1
AM
2535struct _ppc64_elf_section_data
2536{
2537 struct bfd_elf_section_data elf;
411e1bfb
AM
2538
2539 /* An array with one entry for each opd function descriptor. */
f0abc2a1
AM
2540 union
2541 {
411e1bfb 2542 /* Points to the function code section for local opd entries. */
7c8fe5c4 2543 asection **opd_func_sec;
411e1bfb 2544 /* After editing .opd, adjust references to opd local syms. */
7c8fe5c4
AM
2545 long *opd_adjust;
2546
2547 /* An array for toc sections, indexed by offset/8.
2548 Specifies the relocation symbol index used at a given toc offset. */
2549 unsigned *t_symndx;
2550 } u;
2551
2552 enum _ppc64_sec_type sec_type:2;
411e1bfb 2553
7c8fe5c4
AM
2554 /* Flag set when small branches are detected. Used to
2555 select suitable defaults for the stub group size. */
2556 unsigned int has_14bit_branch:1;
f0abc2a1
AM
2557};
2558
2559#define ppc64_elf_section_data(sec) \
411e1bfb 2560 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
f0abc2a1
AM
2561
2562static bfd_boolean
4ce794b7 2563ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
f0abc2a1 2564{
f592407e
AM
2565 if (!sec->used_by_bfd)
2566 {
2567 struct _ppc64_elf_section_data *sdata;
2568 bfd_size_type amt = sizeof (*sdata);
f0abc2a1 2569
f592407e
AM
2570 sdata = bfd_zalloc (abfd, amt);
2571 if (sdata == NULL)
2572 return FALSE;
2573 sec->used_by_bfd = sdata;
2574 }
f0abc2a1
AM
2575
2576 return _bfd_elf_new_section_hook (abfd, sec);
2577}
4025353c
AM
2578
2579static void *
2580get_opd_info (asection * sec)
2581{
2582 if (sec != NULL
2583 && ppc64_elf_section_data (sec) != NULL
7c8fe5c4
AM
2584 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2585 return ppc64_elf_section_data (sec)->u.opd_adjust;
4025353c
AM
2586 return NULL;
2587}
90e3cdf2
JJ
2588\f
2589/* Parameters for the qsort hook. */
2590static asection *synthetic_opd;
2591static bfd_boolean synthetic_relocatable;
2592
699733f6 2593/* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
90e3cdf2
JJ
2594
2595static int
2596compare_symbols (const void *ap, const void *bp)
2597{
2598 const asymbol *a = * (const asymbol **) ap;
2599 const asymbol *b = * (const asymbol **) bp;
2600
699733f6
AM
2601 /* Section symbols first. */
2602 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
90e3cdf2 2603 return -1;
699733f6 2604 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
90e3cdf2
JJ
2605 return 1;
2606
699733f6 2607 /* then .opd symbols. */
90e3cdf2
JJ
2608 if (a->section == synthetic_opd && b->section != synthetic_opd)
2609 return -1;
2610 if (a->section != synthetic_opd && b->section == synthetic_opd)
2611 return 1;
2612
699733f6 2613 /* then other code symbols. */
90e3cdf2
JJ
2614 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2615 == (SEC_CODE | SEC_ALLOC)
2616 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2617 != (SEC_CODE | SEC_ALLOC))
2618 return -1;
2619
2620 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2621 != (SEC_CODE | SEC_ALLOC)
2622 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2623 == (SEC_CODE | SEC_ALLOC))
2624 return 1;
2625
2626 if (synthetic_relocatable)
2627 {
2628 if (a->section->id < b->section->id)
2629 return -1;
2630
2631 if (a->section->id > b->section->id)
2632 return 1;
2633 }
2634
2635 if (a->value + a->section->vma < b->value + b->section->vma)
2636 return -1;
2637
2638 if (a->value + a->section->vma > b->value + b->section->vma)
2639 return 1;
2640
4d35a0aa
AM
2641 /* For syms with the same value, prefer strong dynamic global function
2642 syms over other syms. */
2643 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2644 return -1;
2645
2646 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2647 return 1;
2648
2649 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2650 return -1;
2651
2652 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2653 return 1;
2654
2655 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2656 return -1;
2657
2658 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2659 return 1;
2660
2661 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2662 return -1;
2663
2664 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2665 return 1;
2666
90e3cdf2
JJ
2667 return 0;
2668}
2669
699733f6 2670/* Search SYMS for a symbol of the given VALUE. */
90e3cdf2 2671
699733f6
AM
2672static asymbol *
2673sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
90e3cdf2 2674{
699733f6 2675 long mid;
90e3cdf2 2676
699733f6
AM
2677 if (id == -1)
2678 {
2679 while (lo < hi)
2680 {
2681 mid = (lo + hi) >> 1;
2682 if (syms[mid]->value + syms[mid]->section->vma < value)
2683 lo = mid + 1;
2684 else if (syms[mid]->value + syms[mid]->section->vma > value)
2685 hi = mid;
2686 else
2687 return syms[mid];
2688 }
2689 }
2690 else
2691 {
2692 while (lo < hi)
2693 {
2694 mid = (lo + hi) >> 1;
2695 if (syms[mid]->section->id < id)
2696 lo = mid + 1;
2697 else if (syms[mid]->section->id > id)
2698 hi = mid;
2699 else if (syms[mid]->value < value)
2700 lo = mid + 1;
2701 else if (syms[mid]->value > value)
2702 hi = mid;
2703 else
2704 return syms[mid];
2705 }
2706 }
2707 return NULL;
90e3cdf2
JJ
2708}
2709
699733f6
AM
2710/* Create synthetic symbols, effectively restoring "dot-symbol" function
2711 entry syms. */
90e3cdf2
JJ
2712
2713static long
a7535cf3
AM
2714ppc64_elf_get_synthetic_symtab (bfd *abfd,
2715 long static_count, asymbol **static_syms,
2716 long dyn_count, asymbol **dyn_syms,
c9727e01 2717 asymbol **ret)
90e3cdf2
JJ
2718{
2719 asymbol *s;
699733f6
AM
2720 long i;
2721 long count;
90e3cdf2 2722 char *names;
a7535cf3 2723 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
699733f6 2724 asection *opd;
90e3cdf2 2725 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
a7535cf3 2726 asymbol **syms;
90e3cdf2
JJ
2727
2728 *ret = NULL;
2729
2730 opd = bfd_get_section_by_name (abfd, ".opd");
2731 if (opd == NULL)
2732 return 0;
2733
a7535cf3 2734 symcount = static_count;
c9727e01 2735 if (!relocatable)
a7535cf3 2736 symcount += dyn_count;
90e3cdf2 2737 if (symcount == 0)
c9727e01 2738 return 0;
90e3cdf2 2739
a7535cf3
AM
2740 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2741 if (syms == NULL)
7356fed5 2742 return -1;
a7535cf3
AM
2743
2744 if (!relocatable && static_count != 0 && dyn_count != 0)
2745 {
2746 /* Use both symbol tables. */
2747 memcpy (syms, static_syms, static_count * sizeof (*syms));
2748 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
2749 }
2750 else if (!relocatable && static_count == 0)
2751 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2752 else
2753 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2754
90e3cdf2
JJ
2755 synthetic_opd = opd;
2756 synthetic_relocatable = relocatable;
595da8c5 2757 qsort (syms, symcount, sizeof (*syms), compare_symbols);
90e3cdf2 2758
c9727e01
AM
2759 if (!relocatable && symcount > 1)
2760 {
2761 long j;
2762 /* Trim duplicate syms, since we may have merged the normal and
2763 dynamic symbols. Actually, we only care about syms that have
3b36f7e6 2764 different values, so trim any with the same value. */
c9727e01
AM
2765 for (i = 1, j = 1; i < symcount; ++i)
2766 if (syms[i - 1]->value + syms[i - 1]->section->vma
2767 != syms[i]->value + syms[i]->section->vma)
2768 syms[j++] = syms[i];
2769 symcount = j;
2770 }
2771
699733f6
AM
2772 i = 0;
2773 if (syms[i]->section == opd)
2774 ++i;
2775 codesecsym = i;
90e3cdf2 2776
699733f6
AM
2777 for (; i < symcount; ++i)
2778 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2779 != (SEC_CODE | SEC_ALLOC))
2780 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2781 break;
2782 codesecsymend = i;
90e3cdf2 2783
699733f6
AM
2784 for (; i < symcount; ++i)
2785 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2786 break;
2787 secsymend = i;
90e3cdf2 2788
699733f6
AM
2789 for (; i < symcount; ++i)
2790 if (syms[i]->section != opd)
2791 break;
2792 opdsymend = i;
90e3cdf2 2793
699733f6
AM
2794 for (; i < symcount; ++i)
2795 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2796 != (SEC_CODE | SEC_ALLOC))
2797 break;
2798 symcount = i;
2799
c9727e01 2800 count = 0;
699733f6 2801 if (opdsymend == secsymend)
c9727e01 2802 goto done;
90e3cdf2 2803
699733f6 2804 if (relocatable)
90e3cdf2 2805 {
699733f6
AM
2806 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
2807 arelent *r;
2808 size_t size;
2809 long relcount;
90e3cdf2 2810
699733f6 2811 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
90e3cdf2 2812 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
7356fed5 2813 if (relcount == 0)
c9727e01 2814 goto done;
90e3cdf2 2815
7356fed5
AM
2816 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
2817 {
2818 count = -1;
2819 goto done;
2820 }
2821
699733f6 2822 size = 0;
595da8c5 2823 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
699733f6
AM
2824 {
2825 asymbol *sym;
90e3cdf2 2826
595da8c5 2827 while (r < opd->relocation + relcount
699733f6
AM
2828 && r->address < syms[i]->value + opd->vma)
2829 ++r;
90e3cdf2 2830
595da8c5 2831 if (r == opd->relocation + relcount)
699733f6 2832 break;
90e3cdf2 2833
699733f6
AM
2834 if (r->address != syms[i]->value + opd->vma)
2835 continue;
90e3cdf2 2836
699733f6
AM
2837 if (r->howto->type != R_PPC64_ADDR64)
2838 continue;
90e3cdf2 2839
699733f6
AM
2840 sym = *r->sym_ptr_ptr;
2841 if (!sym_exists_at (syms, opdsymend, symcount,
2842 sym->section->id, sym->value + r->addend))
2843 {
2844 ++count;
2845 size += sizeof (asymbol);
2846 size += strlen (syms[i]->name) + 2;
2847 }
2848 }
90e3cdf2 2849
699733f6
AM
2850 s = *ret = bfd_malloc (size);
2851 if (s == NULL)
2852 {
7356fed5 2853 count = -1;
c9727e01 2854 goto done;
699733f6 2855 }
90e3cdf2 2856
699733f6 2857 names = (char *) (s + count);
90e3cdf2 2858
595da8c5 2859 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
90e3cdf2 2860 {
699733f6 2861 asymbol *sym;
90e3cdf2 2862
595da8c5 2863 while (r < opd->relocation + relcount
699733f6
AM
2864 && r->address < syms[i]->value + opd->vma)
2865 ++r;
90e3cdf2 2866
595da8c5 2867 if (r == opd->relocation + relcount)
699733f6
AM
2868 break;
2869
2870 if (r->address != syms[i]->value + opd->vma)
2871 continue;
2872
2873 if (r->howto->type != R_PPC64_ADDR64)
2874 continue;
90e3cdf2 2875
699733f6
AM
2876 sym = *r->sym_ptr_ptr;
2877 if (!sym_exists_at (syms, opdsymend, symcount,
2878 sym->section->id, sym->value + r->addend))
2879 {
2880 size_t len;
2881
2882 *s = *syms[i];
2883 s->section = sym->section;
2884 s->value = sym->value + r->addend;
2885 s->name = names;
2886 *names++ = '.';
2887 len = strlen (syms[i]->name);
2888 memcpy (names, syms[i]->name, len + 1);
2889 names += len + 1;
2890 s++;
2891 }
2892 }
2893 }
2894 else
90e3cdf2 2895 {
699733f6
AM
2896 bfd_byte *contents;
2897 size_t size;
90e3cdf2 2898
699733f6
AM
2899 if (!bfd_malloc_and_get_section (abfd, opd, &contents))
2900 {
2901 if (contents)
c9727e01
AM
2902 {
2903 free_contents_and_exit:
2904 free (contents);
2905 }
7356fed5 2906 count = -1;
c9727e01 2907 goto done;
699733f6 2908 }
90e3cdf2 2909
699733f6
AM
2910 size = 0;
2911 for (i = secsymend; i < opdsymend; ++i)
2912 {
2913 bfd_vma ent;
90e3cdf2 2914
699733f6
AM
2915 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2916 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2917 {
2918 ++count;
2919 size += sizeof (asymbol);
2920 size += strlen (syms[i]->name) + 2;
2921 }
2922 }
90e3cdf2 2923
699733f6
AM
2924 s = *ret = bfd_malloc (size);
2925 if (s == NULL)
7356fed5 2926 goto free_contents_and_exit;
90e3cdf2 2927
699733f6 2928 names = (char *) (s + count);
90e3cdf2 2929
699733f6 2930 for (i = secsymend; i < opdsymend; ++i)
90e3cdf2 2931 {
699733f6 2932 bfd_vma ent;
90e3cdf2 2933
699733f6
AM
2934 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2935 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
90e3cdf2 2936 {
c9727e01 2937 long lo, hi;
699733f6 2938 size_t len;
c9727e01 2939 asection *sec = abfd->sections;
90e3cdf2 2940
699733f6
AM
2941 *s = *syms[i];
2942 lo = codesecsym;
2943 hi = codesecsymend;
2944 while (lo < hi)
2945 {
c9727e01 2946 long mid = (lo + hi) >> 1;
699733f6
AM
2947 if (syms[mid]->section->vma < ent)
2948 lo = mid + 1;
2949 else if (syms[mid]->section->vma > ent)
2950 hi = mid;
2951 else
c9727e01
AM
2952 {
2953 sec = syms[mid]->section;
2954 break;
2955 }
699733f6
AM
2956 }
2957
c9727e01 2958 if (lo >= hi && lo > codesecsym)
699733f6 2959 sec = syms[lo - 1]->section;
699733f6
AM
2960
2961 for (; sec != NULL; sec = sec->next)
2962 {
2963 if (sec->vma > ent)
2964 break;
2965 if ((sec->flags & SEC_ALLOC) == 0
2966 || (sec->flags & SEC_LOAD) == 0)
2967 break;
2968 if ((sec->flags & SEC_CODE) != 0)
2969 s->section = sec;
2970 }
2971 s->value = ent - s->section->vma;
2972 s->name = names;
2973 *names++ = '.';
2974 len = strlen (syms[i]->name);
2975 memcpy (names, syms[i]->name, len + 1);
2976 names += len + 1;
2977 s++;
90e3cdf2 2978 }
90e3cdf2 2979 }
699733f6 2980 free (contents);
90e3cdf2
JJ
2981 }
2982
c9727e01 2983 done:
a7535cf3 2984 free (syms);
90e3cdf2
JJ
2985 return count;
2986}
5bd4f169 2987\f
65f38f15
AM
2988/* The following functions are specific to the ELF linker, while
2989 functions above are used generally. Those named ppc64_elf_* are
2990 called by the main ELF linker code. They appear in this file more
2991 or less in the order in which they are called. eg.
2992 ppc64_elf_check_relocs is called early in the link process,
2993 ppc64_elf_finish_dynamic_sections is one of the last functions
e86ce104
AM
2994 called.
2995
2996 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2997 functions have both a function code symbol and a function descriptor
2998 symbol. A call to foo in a relocatable object file looks like:
2999
3000 . .text
3001 . x:
3002 . bl .foo
3003 . nop
3004
3005 The function definition in another object file might be:
3006
3007 . .section .opd
3008 . foo: .quad .foo
3009 . .quad .TOC.@tocbase
3010 . .quad 0
3011 .
3012 . .text
3013 . .foo: blr
3014
3015 When the linker resolves the call during a static link, the branch
3016 unsurprisingly just goes to .foo and the .opd information is unused.
3017 If the function definition is in a shared library, things are a little
3018 different: The call goes via a plt call stub, the opd information gets
3019 copied to the plt, and the linker patches the nop.
3020
3021 . x:
3022 . bl .foo_stub
3023 . ld 2,40(1)
3024 .
3025 .
3026 . .foo_stub:
3027 . addis 12,2,Lfoo@toc@ha # in practice, the call stub
411e1bfb 3028 . addi 12,12,Lfoo@toc@l # is slightly optimized, but
e86ce104
AM
3029 . std 2,40(1) # this is the general idea
3030 . ld 11,0(12)
3031 . ld 2,8(12)
3032 . mtctr 11
3033 . ld 11,16(12)
3034 . bctr
3035 .
3036 . .section .plt
3037 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3038
3039 The "reloc ()" notation is supposed to indicate that the linker emits
3040 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3041 copying.
3042
3043 What are the difficulties here? Well, firstly, the relocations
3044 examined by the linker in check_relocs are against the function code
3045 sym .foo, while the dynamic relocation in the plt is emitted against
3046 the function descriptor symbol, foo. Somewhere along the line, we need
3047 to carefully copy dynamic link information from one symbol to the other.
3048 Secondly, the generic part of the elf linker will make .foo a dynamic
3049 symbol as is normal for most other backends. We need foo dynamic
3050 instead, at least for an application final link. However, when
3051 creating a shared library containing foo, we need to have both symbols
3052 dynamic so that references to .foo are satisfied during the early
3053 stages of linking. Otherwise the linker might decide to pull in a
8387904d
AM
3054 definition from some other object, eg. a static library.
3055
3056 Update: As of August 2004, we support a new convention. Function
3057 calls may use the function descriptor symbol, ie. "bl foo". This
3058 behaves exactly as "bl .foo". */
65f38f15
AM
3059
3060/* The linker needs to keep track of the number of relocs that it
3061 decides to copy as dynamic relocs in check_relocs for each symbol.
3062 This is so that it can later discard them if they are found to be
3063 unnecessary. We store the information in a field extending the
3064 regular ELF linker hash table. */
3065
3066struct ppc_dyn_relocs
3067{
3068 struct ppc_dyn_relocs *next;
3069
3070 /* The input section of the reloc. */
3071 asection *sec;
3072
3073 /* Total number of relocs copied for the input section. */
3074 bfd_size_type count;
3075
3076 /* Number of pc-relative relocs copied for the input section. */
3077 bfd_size_type pc_count;
3078};
3079
411e1bfb
AM
3080/* Track GOT entries needed for a given symbol. We might need more
3081 than one got entry per symbol. */
3082struct got_entry
3083{
3084 struct got_entry *next;
3085
e7b938ca 3086 /* The symbol addend that we'll be placing in the GOT. */
411e1bfb
AM
3087 bfd_vma addend;
3088
e717da7e
AM
3089 /* Unlike other ELF targets, we use separate GOT entries for the same
3090 symbol referenced from different input files. This is to support
3091 automatic multiple TOC/GOT sections, where the TOC base can vary
3092 from one input file to another.
3093
3094 Point to the BFD owning this GOT entry. */
3095 bfd *owner;
3096
3097 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
3098 TLS_TPREL or TLS_DTPREL for tls entries. */
3099 char tls_type;
3100
e7b938ca 3101 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
411e1bfb
AM
3102 union
3103 {
3104 bfd_signed_vma refcount;
3105 bfd_vma offset;
3106 } got;
411e1bfb
AM
3107};
3108
3109/* The same for PLT. */
3110struct plt_entry
3111{
3112 struct plt_entry *next;
3113
3114 bfd_vma addend;
3115
3116 union
3117 {
3118 bfd_signed_vma refcount;
3119 bfd_vma offset;
3120 } plt;
3121};
3122
65f38f15 3123/* Of those relocs that might be copied as dynamic relocs, this macro
58ac9f71
AM
3124 selects those that must be copied when linking a shared library,
3125 even when the symbol is local. */
65f38f15 3126
411e1bfb 3127#define MUST_BE_DYN_RELOC(RTYPE) \
805fc799
AM
3128 ((RTYPE) != R_PPC64_REL32 \
3129 && (RTYPE) != R_PPC64_REL64 \
04c9666a 3130 && (RTYPE) != R_PPC64_REL30)
65f38f15 3131
f4656909
AM
3132/* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3133 copying dynamic variables from a shared lib into an app's dynbss
3134 section, and instead use a dynamic relocation to point into the
5d35169e
AM
3135 shared lib. With code that gcc generates, it's vital that this be
3136 enabled; In the PowerPC64 ABI, the address of a function is actually
3137 the address of a function descriptor, which resides in the .opd
3138 section. gcc uses the descriptor directly rather than going via the
3139 GOT as some other ABI's do, which means that initialized function
3140 pointers must reference the descriptor. Thus, a function pointer
3141 initialized to the address of a function in a shared library will
3142 either require a copy reloc, or a dynamic reloc. Using a copy reloc
4cc11e76 3143 redefines the function descriptor symbol to point to the copy. This
5d35169e
AM
3144 presents a problem as a plt entry for that function is also
3145 initialized from the function descriptor symbol and the copy reloc
3146 may not be initialized first. */
a23b6845 3147#define ELIMINATE_COPY_RELOCS 1
f4656909 3148
721956f4
AM
3149/* Section name for stubs is the associated section name plus this
3150 string. */
3151#define STUB_SUFFIX ".stub"
3152
3153/* Linker stubs.
3154 ppc_stub_long_branch:
3155 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3156 destination, but a 24 bit branch in a stub section will reach.
3157 . b dest
3158
3159 ppc_stub_plt_branch:
3160 Similar to the above, but a 24 bit branch in the stub section won't
3161 reach its destination.
87e226ce
AM
3162 . addis %r12,%r2,xxx@toc@ha
3163 . ld %r11,xxx@toc@l(%r12)
721956f4
AM
3164 . mtctr %r11
3165 . bctr
3166
3167 ppc_stub_plt_call:
2c66dc6c
AM
3168 Used to call a function in a shared library. If it so happens that
3169 the plt entry referenced crosses a 64k boundary, then an extra
3170 "addis %r12,%r12,1" will be inserted before the load at xxx+8 or
3171 xxx+16 as appropriate.
87e226ce 3172 . addis %r12,%r2,xxx@toc@ha
721956f4 3173 . std %r2,40(%r1)
87e226ce
AM
3174 . ld %r11,xxx+0@toc@l(%r12)
3175 . ld %r2,xxx+8@toc@l(%r12)
721956f4 3176 . mtctr %r11
87e226ce 3177 . ld %r11,xxx+16@toc@l(%r12)
721956f4 3178 . bctr
ad8e1ba5
AM
3179
3180 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3181 code to adjust the value and save r2 to support multiple toc sections.
3182 A ppc_stub_long_branch with an r2 offset looks like:
3183 . std %r2,40(%r1)
3184 . addis %r2,%r2,off@ha
3185 . addi %r2,%r2,off@l
3186 . b dest
3187
3188 A ppc_stub_plt_branch with an r2 offset looks like:
3189 . std %r2,40(%r1)
3190 . addis %r12,%r2,xxx@toc@ha
3191 . ld %r11,xxx@toc@l(%r12)
3192 . addis %r2,%r2,off@ha
3193 . addi %r2,%r2,off@l
3194 . mtctr %r11
3195 . bctr
721956f4
AM
3196*/
3197
3198enum ppc_stub_type {
3199 ppc_stub_none,
3200 ppc_stub_long_branch,
ad8e1ba5 3201 ppc_stub_long_branch_r2off,
721956f4 3202 ppc_stub_plt_branch,
ad8e1ba5 3203 ppc_stub_plt_branch_r2off,
721956f4
AM
3204 ppc_stub_plt_call
3205};
3206
3207struct ppc_stub_hash_entry {
3208
3209 /* Base hash table entry structure. */
3210 struct bfd_hash_entry root;
3211
ad8e1ba5
AM
3212 enum ppc_stub_type stub_type;
3213
721956f4
AM
3214 /* The stub section. */
3215 asection *stub_sec;
3216
3217 /* Offset within stub_sec of the beginning of this stub. */
3218 bfd_vma stub_offset;
3219
3220 /* Given the symbol's value and its section we can determine its final
3221 value when building the stubs (so the stub knows where to jump. */
3222 bfd_vma target_value;
3223 asection *target_section;
3224
721956f4
AM
3225 /* The symbol table entry, if any, that this was derived from. */
3226 struct ppc_link_hash_entry *h;
3227
411e1bfb
AM
3228 /* And the reloc addend that this was derived from. */
3229 bfd_vma addend;
3230
721956f4
AM
3231 /* Where this stub is being called from, or, in the case of combined
3232 stub sections, the first input section in the group. */
3233 asection *id_sec;
3234};
3235
3236struct ppc_branch_hash_entry {
3237
3238 /* Base hash table entry structure. */
3239 struct bfd_hash_entry root;
3240
c456f082 3241 /* Offset within branch lookup table. */
721956f4
AM
3242 unsigned int offset;
3243
3244 /* Generation marker. */
3245 unsigned int iter;
3246};
65f38f15
AM
3247
3248struct ppc_link_hash_entry
3249{
3250 struct elf_link_hash_entry elf;
3251
b3fac117
AM
3252 union {
3253 /* A pointer to the most recently used stub hash entry against this
3254 symbol. */
3255 struct ppc_stub_hash_entry *stub_cache;
3256
3257 /* A pointer to the next symbol starting with a '.' */
3258 struct ppc_link_hash_entry *next_dot_sym;
3259 } u;
721956f4 3260
65f38f15
AM
3261 /* Track dynamic relocs copied for this symbol. */
3262 struct ppc_dyn_relocs *dyn_relocs;
e86ce104 3263
721956f4 3264 /* Link between function code and descriptor symbols. */
34814b9f 3265 struct ppc_link_hash_entry *oh;
721956f4 3266
e86ce104
AM
3267 /* Flag function code and descriptor symbols. */
3268 unsigned int is_func:1;
3269 unsigned int is_func_descriptor:1;
908b32fc 3270 unsigned int fake:1;
411e1bfb 3271
c5614fa4
AM
3272 /* Whether global opd/toc sym has been adjusted or not.
3273 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3274 should be set for all globals defined in any opd/toc section. */
754021d0
AM
3275 unsigned int adjust_done:1;
3276
99877b66
AM
3277 /* Set if we twiddled this symbol to weak at some stage. */
3278 unsigned int was_undefined:1;
3279
411e1bfb 3280 /* Contexts in which symbol is used in the GOT (or TOC).
e7b938ca
AM
3281 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3282 corresponding relocs are encountered during check_relocs.
3283 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3284 indicate the corresponding GOT entry type is not needed.
3285 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3286 a TPREL one. We use a separate flag rather than setting TPREL
3287 just for convenience in distinguishing the two cases. */
3288#define TLS_GD 1 /* GD reloc. */
3289#define TLS_LD 2 /* LD reloc. */
3290#define TLS_TPREL 4 /* TPREL reloc, => IE. */
3291#define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3292#define TLS_TLS 16 /* Any TLS reloc. */
3293#define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3294#define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3295 char tls_mask;
65f38f15
AM
3296};
3297
3298/* ppc64 ELF linker hash table. */
3299
3300struct ppc_link_hash_table
3301{
3302 struct elf_link_hash_table elf;
3303
721956f4
AM
3304 /* The stub hash table. */
3305 struct bfd_hash_table stub_hash_table;
3306
3307 /* Another hash table for plt_branch stubs. */
3308 struct bfd_hash_table branch_hash_table;
3309
3310 /* Linker stub bfd. */
3311 bfd *stub_bfd;
3312
3313 /* Linker call-backs. */
4ce794b7
AM
3314 asection * (*add_stub_section) (const char *, asection *);
3315 void (*layout_sections_again) (void);
721956f4
AM
3316
3317 /* Array to keep track of which stub sections have been created, and
3318 information on stub grouping. */
3319 struct map_stub {
3320 /* This is the section to which stubs in the group will be attached. */
3321 asection *link_sec;
3322 /* The stub section. */
3323 asection *stub_sec;
ad8e1ba5
AM
3324 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3325 bfd_vma toc_off;
721956f4
AM
3326 } *stub_group;
3327
ad8e1ba5
AM
3328 /* Temp used when calculating TOC pointers. */
3329 bfd_vma toc_curr;
3330
8f3bab57
AM
3331 /* Highest input section id. */
3332 int top_id;
3333
734b6cf9
AM
3334 /* Highest output section index. */
3335 int top_index;
3336
b3fac117
AM
3337 /* Used when adding symbols. */
3338 struct ppc_link_hash_entry *dot_syms;
3339
734b6cf9
AM
3340 /* List of input sections for each output section. */
3341 asection **input_list;
721956f4 3342
65f38f15 3343 /* Short-cuts to get to dynamic linker sections. */
4ce794b7 3344 asection *got;
4ce794b7
AM
3345 asection *plt;
3346 asection *relplt;
3347 asection *dynbss;
3348 asection *relbss;
3349 asection *glink;
82bd7b59 3350 asection *sfpr;
4ce794b7
AM
3351 asection *brlt;
3352 asection *relbrlt;
ec338859 3353
8387904d
AM
3354 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3355 struct ppc_link_hash_entry *tls_get_addr;
3356 struct ppc_link_hash_entry *tls_get_addr_fd;
411e1bfb 3357
9b5ecbd0
AM
3358 /* Statistics. */
3359 unsigned long stub_count[ppc_stub_plt_call];
3360
ee75fd95
AM
3361 /* Number of stubs against global syms. */
3362 unsigned long stub_globals;
3363
ad8e1ba5 3364 /* Set if we should emit symbols for stubs. */
99877b66 3365 unsigned int emit_stub_syms:1;
ad8e1ba5 3366
4c52953f
AM
3367 /* Support for multiple toc sections. */
3368 unsigned int no_multi_toc:1;
3369 unsigned int multi_toc_needed:1;
3370
5d1634d7 3371 /* Set on error. */
99877b66 3372 unsigned int stub_error:1;
721956f4 3373
99877b66
AM
3374 /* Temp used by ppc64_elf_check_directives. */
3375 unsigned int twiddled_syms:1;
721956f4
AM
3376
3377 /* Incremented every time we size stubs. */
3378 unsigned int stub_iteration;
5d1634d7 3379
ec338859
AM
3380 /* Small local sym to section mapping cache. */
3381 struct sym_sec_cache sym_sec;
65f38f15
AM
3382};
3383
4c52953f
AM
3384/* Rename some of the generic section flags to better document how they
3385 are used here. */
3386#define has_toc_reloc has_gp_reloc
3387#define makes_toc_func_call need_finalize_relax
3388#define call_check_in_progress reloc_done
3389
65f38f15
AM
3390/* Get the ppc64 ELF linker hash table from a link_info structure. */
3391
3392#define ppc_hash_table(p) \
3393 ((struct ppc_link_hash_table *) ((p)->hash))
3394
721956f4
AM
3395#define ppc_stub_hash_lookup(table, string, create, copy) \
3396 ((struct ppc_stub_hash_entry *) \
3397 bfd_hash_lookup ((table), (string), (create), (copy)))
3398
3399#define ppc_branch_hash_lookup(table, string, create, copy) \
3400 ((struct ppc_branch_hash_entry *) \
3401 bfd_hash_lookup ((table), (string), (create), (copy)))
3402
3403/* Create an entry in the stub hash table. */
3404
3405static struct bfd_hash_entry *
4ce794b7
AM
3406stub_hash_newfunc (struct bfd_hash_entry *entry,
3407 struct bfd_hash_table *table,
3408 const char *string)
721956f4
AM
3409{
3410 /* Allocate the structure if it has not already been allocated by a
3411 subclass. */
3412 if (entry == NULL)
3413 {
3414 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3415 if (entry == NULL)
3416 return entry;
3417 }
3418
3419 /* Call the allocation method of the superclass. */
3420 entry = bfd_hash_newfunc (entry, table, string);
3421 if (entry != NULL)
3422 {
3423 struct ppc_stub_hash_entry *eh;
3424
3425 /* Initialize the local fields. */
3426 eh = (struct ppc_stub_hash_entry *) entry;
ad8e1ba5 3427 eh->stub_type = ppc_stub_none;
721956f4
AM
3428 eh->stub_sec = NULL;
3429 eh->stub_offset = 0;
3430 eh->target_value = 0;
3431 eh->target_section = NULL;
721956f4
AM
3432 eh->h = NULL;
3433 eh->id_sec = NULL;
3434 }
3435
3436 return entry;
3437}
3438
3439/* Create an entry in the branch hash table. */
3440
3441static struct bfd_hash_entry *
4ce794b7
AM
3442branch_hash_newfunc (struct bfd_hash_entry *entry,
3443 struct bfd_hash_table *table,
3444 const char *string)
721956f4
AM
3445{
3446 /* Allocate the structure if it has not already been allocated by a
3447 subclass. */
3448 if (entry == NULL)
3449 {
3450 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3451 if (entry == NULL)
3452 return entry;
3453 }
3454
3455 /* Call the allocation method of the superclass. */
3456 entry = bfd_hash_newfunc (entry, table, string);
3457 if (entry != NULL)
3458 {
3459 struct ppc_branch_hash_entry *eh;
3460
3461 /* Initialize the local fields. */
3462 eh = (struct ppc_branch_hash_entry *) entry;
3463 eh->offset = 0;
3464 eh->iter = 0;
3465 }
3466
3467 return entry;
3468}
3469
65f38f15
AM
3470/* Create an entry in a ppc64 ELF linker hash table. */
3471
3472static struct bfd_hash_entry *
4ce794b7
AM
3473link_hash_newfunc (struct bfd_hash_entry *entry,
3474 struct bfd_hash_table *table,
3475 const char *string)
65f38f15
AM
3476{
3477 /* Allocate the structure if it has not already been allocated by a
3478 subclass. */
3479 if (entry == NULL)
3480 {
3481 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3482 if (entry == NULL)
3483 return entry;
3484 }
3485
3486 /* Call the allocation method of the superclass. */
3487 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3488 if (entry != NULL)
3489 {
3490 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3491
b3fac117 3492 memset (&eh->u.stub_cache, 0,
908b32fc 3493 (sizeof (struct ppc_link_hash_entry)
b3fac117
AM
3494 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3495
3496 /* When making function calls, old ABI code references function entry
3497 points (dot symbols), while new ABI code references the function
3498 descriptor symbol. We need to make any combination of reference and
3499 definition work together, without breaking archive linking.
3500
3501 For a defined function "foo" and an undefined call to "bar":
3502 An old object defines "foo" and ".foo", references ".bar" (possibly
3503 "bar" too).
3504 A new object defines "foo" and references "bar".
3505
3506 A new object thus has no problem with its undefined symbols being
3507 satisfied by definitions in an old object. On the other hand, the
3508 old object won't have ".bar" satisfied by a new object.
3509
3510 Keep a list of newly added dot-symbols. */
3511
3512 if (string[0] == '.')
3513 {
3514 struct ppc_link_hash_table *htab;
3515
3516 htab = (struct ppc_link_hash_table *) table;
3517 eh->u.next_dot_sym = htab->dot_syms;
3518 htab->dot_syms = eh;
3519 }
65f38f15
AM
3520 }
3521
3522 return entry;
3523}
3524
3525/* Create a ppc64 ELF linker hash table. */
3526
3527static struct bfd_link_hash_table *
4ce794b7 3528ppc64_elf_link_hash_table_create (bfd *abfd)
65f38f15
AM
3529{
3530 struct ppc_link_hash_table *htab;
3531 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
3532
4ce794b7 3533 htab = bfd_zmalloc (amt);
65f38f15
AM
3534 if (htab == NULL)
3535 return NULL;
3536
66eb6687
AM
3537 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3538 sizeof (struct ppc_link_hash_entry)))
65f38f15 3539 {
e2d34d7d 3540 free (htab);
65f38f15
AM
3541 return NULL;
3542 }
3543
721956f4 3544 /* Init the stub hash table too. */
66eb6687
AM
3545 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3546 sizeof (struct ppc_stub_hash_entry)))
721956f4
AM
3547 return NULL;
3548
3549 /* And the branch hash table. */
66eb6687
AM
3550 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3551 sizeof (struct ppc_branch_hash_entry)))
721956f4
AM
3552 return NULL;
3553
3254fd24
AM
3554 /* Initializing two fields of the union is just cosmetic. We really
3555 only care about glist, but when compiled on a 32-bit host the
3556 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3557 debugger inspection of these fields look nicer. */
a6aa5195
AM
3558 htab->elf.init_got_refcount.refcount = 0;
3559 htab->elf.init_got_refcount.glist = NULL;
3560 htab->elf.init_plt_refcount.refcount = 0;
3561 htab->elf.init_plt_refcount.glist = NULL;
3562 htab->elf.init_got_offset.offset = 0;
3563 htab->elf.init_got_offset.glist = NULL;
3564 htab->elf.init_plt_offset.offset = 0;
3565 htab->elf.init_plt_offset.glist = NULL;
3254fd24 3566
65f38f15
AM
3567 return &htab->elf.root;
3568}
3569
721956f4
AM
3570/* Free the derived linker hash table. */
3571
3572static void
4ce794b7 3573ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
721956f4
AM
3574{
3575 struct ppc_link_hash_table *ret = (struct ppc_link_hash_table *) hash;
3576
3577 bfd_hash_table_free (&ret->stub_hash_table);
3578 bfd_hash_table_free (&ret->branch_hash_table);
3579 _bfd_generic_link_hash_table_free (hash);
3580}
3581
e717da7e
AM
3582/* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3583
3584void
3585ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
3586{
3587 struct ppc_link_hash_table *htab;
3588
3589 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
3590
3591/* Always hook our dynamic sections into the first bfd, which is the
3592 linker created stub bfd. This ensures that the GOT header is at
3593 the start of the output TOC section. */
3594 htab = ppc_hash_table (info);
3595 htab->stub_bfd = abfd;
3596 htab->elf.dynobj = abfd;
3597}
3598
721956f4
AM
3599/* Build a name for an entry in the stub hash table. */
3600
3601static char *
4ce794b7
AM
3602ppc_stub_name (const asection *input_section,
3603 const asection *sym_sec,
3604 const struct ppc_link_hash_entry *h,
3605 const Elf_Internal_Rela *rel)
721956f4
AM
3606{
3607 char *stub_name;
3608 bfd_size_type len;
3609
3610 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3611 offsets from a sym as a branch target? In fact, we could
3612 probably assume the addend is always zero. */
3613 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3614
3615 if (h)
3616 {
3617 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3618 stub_name = bfd_malloc (len);
46de2a7c
AM
3619 if (stub_name == NULL)
3620 return stub_name;
3621
3622 sprintf (stub_name, "%08x.%s+%x",
3623 input_section->id & 0xffffffff,
3624 h->elf.root.root.string,
3625 (int) rel->r_addend & 0xffffffff);
721956f4
AM
3626 }
3627 else
3628 {
ad8e1ba5 3629 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
721956f4 3630 stub_name = bfd_malloc (len);
46de2a7c
AM
3631 if (stub_name == NULL)
3632 return stub_name;
3633
3634 sprintf (stub_name, "%08x.%x:%x+%x",
3635 input_section->id & 0xffffffff,
3636 sym_sec->id & 0xffffffff,
3637 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3638 (int) rel->r_addend & 0xffffffff);
721956f4 3639 }
ee75fd95
AM
3640 if (stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3641 stub_name[len - 2] = 0;
721956f4
AM
3642 return stub_name;
3643}
3644
3645/* Look up an entry in the stub hash. Stub entries are cached because
3646 creating the stub name takes a bit of time. */
3647
3648static struct ppc_stub_hash_entry *
4ce794b7
AM
3649ppc_get_stub_entry (const asection *input_section,
3650 const asection *sym_sec,
039b3fef 3651 struct ppc_link_hash_entry *h,
4ce794b7
AM
3652 const Elf_Internal_Rela *rel,
3653 struct ppc_link_hash_table *htab)
721956f4
AM
3654{
3655 struct ppc_stub_hash_entry *stub_entry;
721956f4
AM
3656 const asection *id_sec;
3657
3658 /* If this input section is part of a group of sections sharing one
3659 stub section, then use the id of the first section in the group.
3660 Stub names need to include a section id, as there may well be
3661 more than one stub used to reach say, printf, and we need to
3662 distinguish between them. */
3663 id_sec = htab->stub_group[input_section->id].link_sec;
3664
b3fac117
AM
3665 if (h != NULL && h->u.stub_cache != NULL
3666 && h->u.stub_cache->h == h
3667 && h->u.stub_cache->id_sec == id_sec)
721956f4 3668 {
b3fac117 3669 stub_entry = h->u.stub_cache;
721956f4
AM
3670 }
3671 else
3672 {
3673 char *stub_name;
3674
3675 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
3676 if (stub_name == NULL)
3677 return NULL;
3678
3679 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 3680 stub_name, FALSE, FALSE);
721956f4 3681 if (h != NULL)
b3fac117 3682 h->u.stub_cache = stub_entry;
721956f4
AM
3683
3684 free (stub_name);
3685 }
3686
3687 return stub_entry;
3688}
3689
3690/* Add a new stub entry to the stub hash. Not all fields of the new
3691 stub entry are initialised. */
3692
3693static struct ppc_stub_hash_entry *
4ce794b7
AM
3694ppc_add_stub (const char *stub_name,
3695 asection *section,
3696 struct ppc_link_hash_table *htab)
721956f4
AM
3697{
3698 asection *link_sec;
3699 asection *stub_sec;
3700 struct ppc_stub_hash_entry *stub_entry;
3701
3702 link_sec = htab->stub_group[section->id].link_sec;
3703 stub_sec = htab->stub_group[section->id].stub_sec;
3704 if (stub_sec == NULL)
3705 {
3706 stub_sec = htab->stub_group[link_sec->id].stub_sec;
3707 if (stub_sec == NULL)
3708 {
d4c88bbb 3709 size_t namelen;
721956f4
AM
3710 bfd_size_type len;
3711 char *s_name;
3712
d4c88bbb
AM
3713 namelen = strlen (link_sec->name);
3714 len = namelen + sizeof (STUB_SUFFIX);
721956f4
AM
3715 s_name = bfd_alloc (htab->stub_bfd, len);
3716 if (s_name == NULL)
3717 return NULL;
3718
d4c88bbb
AM
3719 memcpy (s_name, link_sec->name, namelen);
3720 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
721956f4
AM
3721 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
3722 if (stub_sec == NULL)
3723 return NULL;
3724 htab->stub_group[link_sec->id].stub_sec = stub_sec;
3725 }
3726 htab->stub_group[section->id].stub_sec = stub_sec;
3727 }
3728
3729 /* Enter this entry into the linker stub hash table. */
3730 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
b34976b6 3731 TRUE, FALSE);
721956f4
AM
3732 if (stub_entry == NULL)
3733 {
d003868e
AM
3734 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
3735 section->owner, stub_name);
721956f4
AM
3736 return NULL;
3737 }
3738
3739 stub_entry->stub_sec = stub_sec;
3740 stub_entry->stub_offset = 0;
3741 stub_entry->id_sec = link_sec;
3742 return stub_entry;
3743}
3744
82bd7b59
AM
3745/* Create sections for linker generated code. */
3746
b34976b6 3747static bfd_boolean
4ce794b7 3748create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
82bd7b59
AM
3749{
3750 struct ppc_link_hash_table *htab;
3751 flagword flags;
3752
3753 htab = ppc_hash_table (info);
3754
3755 /* Create .sfpr for code to save and restore fp regs. */
3756 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3757 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3496cb2a
L
3758 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3759 flags);
82bd7b59 3760 if (htab->sfpr == NULL
82bd7b59 3761 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
b34976b6 3762 return FALSE;
82bd7b59 3763
721956f4 3764 /* Create .glink for lazy dynamic linking support. */
3496cb2a
L
3765 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3766 flags);
4ce794b7 3767 if (htab->glink == NULL
ee4bf8d2 3768 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
b34976b6 3769 return FALSE;
82bd7b59 3770
c456f082
AM
3771 /* Create branch lookup table for plt_branch stubs. */
3772 if (info->shared)
3773 {
3774 flags = (SEC_ALLOC | SEC_LOAD
3775 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3776 htab->brlt
3777 = bfd_make_section_anyway_with_flags (dynobj, ".data.rel.ro.brlt",
3778 flags);
3779 }
3780 else
3781 {
3782 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3783 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3784 htab->brlt
3785 = bfd_make_section_anyway_with_flags (dynobj, ".rodata.brlt", flags);
3786 }
3787
4ce794b7 3788 if (htab->brlt == NULL
4ce794b7 3789 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
b34976b6 3790 return FALSE;
721956f4 3791
c456f082 3792 if (info->shared)
721956f4
AM
3793 {
3794 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3795 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
c456f082
AM
3796 htab->relbrlt
3797 = bfd_make_section_anyway_with_flags (dynobj, ".rela.data.rel.ro.brlt",
3798 flags);
721956f4 3799 }
c456f082
AM
3800 else if (info->emitrelocations)
3801 {
3802 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3803 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3804 htab->relbrlt
3805 = bfd_make_section_anyway_with_flags (dynobj, ".rela.rodata.brlt",
3806 flags);
3807 }
3808 else
3809 return TRUE;
3810
3811 if (!htab->relbrlt
3812 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
3813 return FALSE;
3814
b34976b6 3815 return TRUE;
82bd7b59
AM
3816}
3817
e717da7e
AM
3818/* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3819 not already done. */
65f38f15 3820
b34976b6 3821static bfd_boolean
e717da7e 3822create_got_section (bfd *abfd, struct bfd_link_info *info)
65f38f15 3823{
e717da7e
AM
3824 asection *got, *relgot;
3825 flagword flags;
3826 struct ppc_link_hash_table *htab = ppc_hash_table (info);
65f38f15 3827
4ce794b7 3828 if (!htab->got)
e717da7e
AM
3829 {
3830 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
3831 return FALSE;
3832
3833 htab->got = bfd_get_section_by_name (htab->elf.dynobj, ".got");
3834 if (!htab->got)
3835 abort ();
3836 }
3837
3838 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3839 | SEC_LINKER_CREATED);
3840
c456f082 3841 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
e717da7e 3842 if (!got
e717da7e
AM
3843 || !bfd_set_section_alignment (abfd, got, 3))
3844 return FALSE;
65f38f15 3845
c456f082
AM
3846 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3847 flags | SEC_READONLY);
e717da7e 3848 if (!relgot
e717da7e 3849 || ! bfd_set_section_alignment (abfd, relgot, 3))
b34976b6 3850 return FALSE;
e717da7e
AM
3851
3852 ppc64_elf_tdata (abfd)->got = got;
3853 ppc64_elf_tdata (abfd)->relgot = relgot;
b34976b6 3854 return TRUE;
65f38f15 3855}
5bd4f169 3856
82bd7b59 3857/* Create the dynamic sections, and set up shortcuts. */
5bd4f169 3858
b34976b6 3859static bfd_boolean
4ce794b7 3860ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
5bd4f169 3861{
65f38f15 3862 struct ppc_link_hash_table *htab;
5bd4f169 3863
65f38f15 3864 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
b34976b6 3865 return FALSE;
65f38f15 3866
e717da7e
AM
3867 htab = ppc_hash_table (info);
3868 if (!htab->got)
3869 htab->got = bfd_get_section_by_name (dynobj, ".got");
4ce794b7
AM
3870 htab->plt = bfd_get_section_by_name (dynobj, ".plt");
3871 htab->relplt = bfd_get_section_by_name (dynobj, ".rela.plt");
3872 htab->dynbss = bfd_get_section_by_name (dynobj, ".dynbss");
65f38f15 3873 if (!info->shared)
4ce794b7 3874 htab->relbss = bfd_get_section_by_name (dynobj, ".rela.bss");
65f38f15 3875
e717da7e 3876 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4ce794b7 3877 || (!info->shared && !htab->relbss))
65f38f15
AM
3878 abort ();
3879
b34976b6 3880 return TRUE;
5bd4f169
AM
3881}
3882
40d16e0b
AM
3883/* Merge PLT info on FROM with that on TO. */
3884
3885static void
3886move_plt_plist (struct ppc_link_hash_entry *from,
3887 struct ppc_link_hash_entry *to)
3888{
3889 if (from->elf.plt.plist != NULL)
3890 {
3891 if (to->elf.plt.plist != NULL)
3892 {
3893 struct plt_entry **entp;
3894 struct plt_entry *ent;
3895
3896 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3897 {
3898 struct plt_entry *dent;
3899
3900 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3901 if (dent->addend == ent->addend)
3902 {
3903 dent->plt.refcount += ent->plt.refcount;
3904 *entp = ent->next;
3905 break;
3906 }
3907 if (dent == NULL)
3908 entp = &ent->next;
3909 }
3910 *entp = to->elf.plt.plist;
3911 }
3912
3913 to->elf.plt.plist = from->elf.plt.plist;
3914 from->elf.plt.plist = NULL;
3915 }
3916}
3917
65f38f15
AM
3918/* Copy the extra info we tack onto an elf_link_hash_entry. */
3919
3920static void
fcfa13d2
AM
3921ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3922 struct elf_link_hash_entry *dir,
3923 struct elf_link_hash_entry *ind)
65f38f15
AM
3924{
3925 struct ppc_link_hash_entry *edir, *eind;
3926
3927 edir = (struct ppc_link_hash_entry *) dir;
3928 eind = (struct ppc_link_hash_entry *) ind;
3929
411e1bfb 3930 /* Copy over any dynamic relocs we may have on the indirect sym. */
bbd7ec4a 3931 if (eind->dyn_relocs != NULL)
65f38f15 3932 {
bbd7ec4a
AM
3933 if (edir->dyn_relocs != NULL)
3934 {
3935 struct ppc_dyn_relocs **pp;
3936 struct ppc_dyn_relocs *p;
3937
fcfa13d2 3938 /* Add reloc counts against the indirect sym to the direct sym
bbd7ec4a
AM
3939 list. Merge any entries against the same section. */
3940 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
3941 {
3942 struct ppc_dyn_relocs *q;
3943
3944 for (q = edir->dyn_relocs; q != NULL; q = q->next)
3945 if (q->sec == p->sec)
3946 {
3947 q->pc_count += p->pc_count;
3948 q->count += p->count;
3949 *pp = p->next;
3950 break;
3951 }
3952 if (q == NULL)
3953 pp = &p->next;
3954 }
3955 *pp = edir->dyn_relocs;
3956 }
3957
65f38f15
AM
3958 edir->dyn_relocs = eind->dyn_relocs;
3959 eind->dyn_relocs = NULL;
3960 }
65f38f15 3961
6349e628
AM
3962 edir->is_func |= eind->is_func;
3963 edir->is_func_descriptor |= eind->is_func_descriptor;
58ac9f71 3964 edir->tls_mask |= eind->tls_mask;
6349e628 3965
81848ca0 3966 /* If called to transfer flags for a weakdef during processing
f5385ebf 3967 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
81848ca0 3968 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
f5385ebf
AM
3969 if (!(ELIMINATE_COPY_RELOCS
3970 && eind->elf.root.type != bfd_link_hash_indirect
3971 && edir->elf.dynamic_adjusted))
3972 edir->elf.non_got_ref |= eind->elf.non_got_ref;
81848ca0 3973
f5385ebf
AM
3974 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3975 edir->elf.ref_regular |= eind->elf.ref_regular;
3976 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3977 edir->elf.needs_plt |= eind->elf.needs_plt;
6349e628
AM
3978
3979 /* If we were called to copy over info for a weak sym, that's all. */
3980 if (eind->elf.root.type != bfd_link_hash_indirect)
3981 return;
3982
81848ca0
AM
3983 /* Copy over got entries that we may have already seen to the
3984 symbol which just became indirect. */
411e1bfb
AM
3985 if (eind->elf.got.glist != NULL)
3986 {
3987 if (edir->elf.got.glist != NULL)
3988 {
3989 struct got_entry **entp;
3990 struct got_entry *ent;
3991
3992 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
3993 {
3994 struct got_entry *dent;
3995
3996 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
3997 if (dent->addend == ent->addend
e717da7e 3998 && dent->owner == ent->owner
411e1bfb
AM
3999 && dent->tls_type == ent->tls_type)
4000 {
4001 dent->got.refcount += ent->got.refcount;
4002 *entp = ent->next;
4003 break;
4004 }
4005 if (dent == NULL)
4006 entp = &ent->next;
4007 }
4008 *entp = edir->elf.got.glist;
4009 }
4010
4011 edir->elf.got.glist = eind->elf.got.glist;
4012 eind->elf.got.glist = NULL;
4013 }
4014
4015 /* And plt entries. */
40d16e0b 4016 move_plt_plist (eind, edir);
411e1bfb 4017
fcfa13d2 4018 if (eind->elf.dynindx != -1)
411e1bfb 4019 {
fcfa13d2
AM
4020 if (edir->elf.dynindx != -1)
4021 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4022 edir->elf.dynstr_index);
411e1bfb
AM
4023 edir->elf.dynindx = eind->elf.dynindx;
4024 edir->elf.dynstr_index = eind->elf.dynstr_index;
4025 eind->elf.dynindx = -1;
4026 eind->elf.dynstr_index = 0;
4027 }
411e1bfb
AM
4028}
4029
8387904d
AM
4030/* Find the function descriptor hash entry from the given function code
4031 hash entry FH. Link the entries via their OH fields. */
4032
4033static struct ppc_link_hash_entry *
4034get_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4035{
4036 struct ppc_link_hash_entry *fdh = fh->oh;
4037
4038 if (fdh == NULL)
4039 {
4040 const char *fd_name = fh->elf.root.root.string + 1;
4041
4042 fdh = (struct ppc_link_hash_entry *)
4043 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4044 if (fdh != NULL)
4045 {
4046 fdh->is_func_descriptor = 1;
4047 fdh->oh = fh;
4048 fh->is_func = 1;
4049 fh->oh = fdh;
4050 }
4051 }
4052
4053 return fdh;
4054}
4055
bb700d78
AM
4056/* Make a fake function descriptor sym for the code sym FH. */
4057
4058static struct ppc_link_hash_entry *
4059make_fdh (struct bfd_link_info *info,
908b32fc 4060 struct ppc_link_hash_entry *fh)
bb700d78
AM
4061{
4062 bfd *abfd;
4063 asymbol *newsym;
4064 struct bfd_link_hash_entry *bh;
4065 struct ppc_link_hash_entry *fdh;
4066
4067 abfd = fh->elf.root.u.undef.abfd;
4068 newsym = bfd_make_empty_symbol (abfd);
4069 newsym->name = fh->elf.root.root.string + 1;
4070 newsym->section = bfd_und_section_ptr;
4071 newsym->value = 0;
908b32fc 4072 newsym->flags = BSF_WEAK;
bb700d78
AM
4073
4074 bh = NULL;
4075 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4076 newsym->flags, newsym->section,
4077 newsym->value, NULL, FALSE, FALSE,
4078 &bh))
4079 return NULL;
4080
4081 fdh = (struct ppc_link_hash_entry *) bh;
4082 fdh->elf.non_elf = 0;
908b32fc
AM
4083 fdh->fake = 1;
4084 fdh->is_func_descriptor = 1;
4085 fdh->oh = fh;
4086 fh->is_func = 1;
4087 fh->oh = fdh;
bb700d78
AM
4088 return fdh;
4089}
4090
8387904d
AM
4091/* Fix function descriptor symbols defined in .opd sections to be
4092 function type. */
555cd476
AM
4093
4094static bfd_boolean
b3fac117 4095ppc64_elf_add_symbol_hook (bfd *ibfd ATTRIBUTE_UNUSED,
555cd476
AM
4096 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4097 Elf_Internal_Sym *isym,
b3fac117 4098 const char **name ATTRIBUTE_UNUSED,
555cd476
AM
4099 flagword *flags ATTRIBUTE_UNUSED,
4100 asection **sec,
4101 bfd_vma *value ATTRIBUTE_UNUSED)
4102{
8843416a
AM
4103 if (*sec != NULL
4104 && strcmp (bfd_get_section_name (ibfd, *sec), ".opd") == 0)
555cd476 4105 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
433817dd 4106
555cd476
AM
4107 return TRUE;
4108}
4109
8387904d 4110/* This function makes an old ABI object reference to ".bar" cause the
908b32fc
AM
4111 inclusion of a new ABI object archive that defines "bar".
4112 NAME is a symbol defined in an archive. Return a symbol in the hash
4113 table that might be satisfied by the archive symbols. */
8387904d
AM
4114
4115static struct elf_link_hash_entry *
4116ppc64_elf_archive_symbol_lookup (bfd *abfd,
4117 struct bfd_link_info *info,
4118 const char *name)
4119{
4120 struct elf_link_hash_entry *h;
4121 char *dot_name;
4122 size_t len;
4123
4124 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
908b32fc
AM
4125 if (h != NULL
4126 /* Don't return this sym if it is a fake function descriptor
4127 created by add_symbol_adjust. */
4128 && !(h->root.type == bfd_link_hash_undefweak
4129 && ((struct ppc_link_hash_entry *) h)->fake))
8387904d
AM
4130 return h;
4131
4132 if (name[0] == '.')
4133 return h;
4134
4135 len = strlen (name);
4136 dot_name = bfd_alloc (abfd, len + 2);
4137 if (dot_name == NULL)
4138 return (struct elf_link_hash_entry *) 0 - 1;
4139 dot_name[0] = '.';
4140 memcpy (dot_name + 1, name, len + 1);
4141 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4142 bfd_release (abfd, dot_name);
4143 return h;
4144}
4145
4146/* This function satisfies all old ABI object references to ".bar" if a
99877b66
AM
4147 new ABI object defines "bar". Well, at least, undefined dot symbols
4148 are made weak. This stops later archive searches from including an
4149 object if we already have a function descriptor definition. It also
35b0ce59
AM
4150 prevents the linker complaining about undefined symbols.
4151 We also check and correct mismatched symbol visibility here. The
4152 most restrictive visibility of the function descriptor and the
4153 function entry symbol is used. */
8387904d
AM
4154
4155static bfd_boolean
b3fac117 4156add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
8387904d 4157{
8387904d
AM
4158 struct ppc_link_hash_table *htab;
4159 struct ppc_link_hash_entry *fdh;
4160
b3fac117 4161 if (eh->elf.root.type == bfd_link_hash_indirect)
8387904d
AM
4162 return TRUE;
4163
b3fac117
AM
4164 if (eh->elf.root.type == bfd_link_hash_warning)
4165 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
8387904d 4166
b3fac117
AM
4167 if (eh->elf.root.root.string[0] != '.')
4168 abort ();
8387904d 4169
b3fac117 4170 htab = ppc_hash_table (info);
99877b66 4171 fdh = get_fdh (eh, htab);
bb700d78 4172 if (fdh == NULL
b3fac117 4173 && !info->relocatable
bb700d78
AM
4174 && (eh->elf.root.type == bfd_link_hash_undefined
4175 || eh->elf.root.type == bfd_link_hash_undefweak)
4176 && eh->elf.ref_regular)
4177 {
4178 /* Make an undefweak function descriptor sym, which is enough to
4179 pull in an --as-needed shared lib, but won't cause link
4180 errors. Archives are handled elsewhere. */
b3fac117 4181 fdh = make_fdh (info, eh);
bb700d78 4182 if (fdh == NULL)
b3fac117 4183 return FALSE;
bb700d78
AM
4184 else
4185 fdh->elf.ref_regular = 1;
4186 }
e87d886e 4187 else if (fdh != NULL)
8387904d 4188 {
35b0ce59
AM
4189 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4190 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4191 if (entry_vis < descr_vis)
4192 fdh->elf.other += entry_vis - descr_vis;
4193 else if (entry_vis > descr_vis)
4194 eh->elf.other += descr_vis - entry_vis;
4195
e87d886e
AM
4196 if ((fdh->elf.root.type == bfd_link_hash_defined
4197 || fdh->elf.root.type == bfd_link_hash_defweak)
4198 && eh->elf.root.type == bfd_link_hash_undefined)
35b0ce59
AM
4199 {
4200 eh->elf.root.type = bfd_link_hash_undefweak;
4201 eh->was_undefined = 1;
4202 htab->twiddled_syms = 1;
4203 }
8387904d 4204 }
99877b66 4205
8387904d
AM
4206 return TRUE;
4207}
4208
b3fac117
AM
4209/* Process list of dot-symbols we made in link_hash_newfunc. */
4210
8387904d 4211static bfd_boolean
b3fac117 4212ppc64_elf_check_directives (bfd *ibfd, struct bfd_link_info *info)
8387904d 4213{
99877b66 4214 struct ppc_link_hash_table *htab;
b3fac117 4215 struct ppc_link_hash_entry **p, *eh;
433817dd 4216
99877b66 4217 htab = ppc_hash_table (info);
ee75fd95 4218 if (!is_ppc64_elf_target (htab->elf.root.creator))
35b0ce59
AM
4219 return TRUE;
4220
b3fac117
AM
4221 if (is_ppc64_elf_target (ibfd->xvec))
4222 {
4223 p = &htab->dot_syms;
4224 while ((eh = *p) != NULL)
4225 {
4226 *p = NULL;
4227 if (!add_symbol_adjust (eh, info))
4228 return FALSE;
4229 p = &eh->u.next_dot_sym;
4230 }
4231 }
4232
4233 /* Clear the list for non-ppc64 input files. */
4234 p = &htab->dot_syms;
4235 while ((eh = *p) != NULL)
4236 {
4237 *p = NULL;
4238 p = &eh->u.next_dot_sym;
4239 }
99877b66
AM
4240
4241 /* We need to fix the undefs list for any syms we have twiddled to
4242 undef_weak. */
4243 if (htab->twiddled_syms)
4244 {
77cfaee6 4245 bfd_link_repair_undef_list (&htab->elf.root);
99877b66
AM
4246 htab->twiddled_syms = 0;
4247 }
b3fac117 4248 return TRUE;
8387904d
AM
4249}
4250
97fed1c9
JJ
4251/* Undo hash table changes when an --as-needed input file is determined
4252 not to be needed. */
4253
4254static bfd_boolean
4255ppc64_elf_as_needed_cleanup (bfd *ibfd ATTRIBUTE_UNUSED,
4256 struct bfd_link_info *info)
4257{
4258 ppc_hash_table (info)->dot_syms = NULL;
4259 return TRUE;
4260}
4261
411e1bfb 4262static bfd_boolean
4ce794b7
AM
4263update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4264 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
411e1bfb
AM
4265{
4266 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
e7b938ca 4267 char *local_got_tls_masks;
411e1bfb
AM
4268
4269 if (local_got_ents == NULL)
4270 {
4271 bfd_size_type size = symtab_hdr->sh_info;
4272
e7b938ca 4273 size *= sizeof (*local_got_ents) + sizeof (*local_got_tls_masks);
4ce794b7 4274 local_got_ents = bfd_zalloc (abfd, size);
411e1bfb
AM
4275 if (local_got_ents == NULL)
4276 return FALSE;
4277 elf_local_got_ents (abfd) = local_got_ents;
4278 }
4279
4280 if ((tls_type & TLS_EXPLICIT) == 0)
4281 {
4282 struct got_entry *ent;
4283
4284 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
e717da7e
AM
4285 if (ent->addend == r_addend
4286 && ent->owner == abfd
4287 && ent->tls_type == tls_type)
411e1bfb
AM
4288 break;
4289 if (ent == NULL)
4290 {
4291 bfd_size_type amt = sizeof (*ent);
4ce794b7 4292 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4293 if (ent == NULL)
4294 return FALSE;
4295 ent->next = local_got_ents[r_symndx];
4296 ent->addend = r_addend;
e717da7e 4297 ent->owner = abfd;
411e1bfb
AM
4298 ent->tls_type = tls_type;
4299 ent->got.refcount = 0;
4300 local_got_ents[r_symndx] = ent;
4301 }
4302 ent->got.refcount += 1;
4303 }
4304
e7b938ca
AM
4305 local_got_tls_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
4306 local_got_tls_masks[r_symndx] |= tls_type;
411e1bfb 4307 return TRUE;
65f38f15
AM
4308}
4309
411e1bfb 4310static bfd_boolean
4ce794b7 4311update_plt_info (bfd *abfd, struct ppc_link_hash_entry *eh, bfd_vma addend)
1e2f5b6e 4312{
411e1bfb 4313 struct plt_entry *ent;
1e2f5b6e 4314
411e1bfb
AM
4315 for (ent = eh->elf.plt.plist; ent != NULL; ent = ent->next)
4316 if (ent->addend == addend)
4317 break;
4318 if (ent == NULL)
1e2f5b6e 4319 {
411e1bfb 4320 bfd_size_type amt = sizeof (*ent);
4ce794b7 4321 ent = bfd_alloc (abfd, amt);
411e1bfb
AM
4322 if (ent == NULL)
4323 return FALSE;
4324 ent->next = eh->elf.plt.plist;
4325 ent->addend = addend;
4326 ent->plt.refcount = 0;
4327 eh->elf.plt.plist = ent;
1e2f5b6e 4328 }
411e1bfb 4329 ent->plt.refcount += 1;
f5385ebf 4330 eh->elf.needs_plt = 1;
93f3fa99
AM
4331 if (eh->elf.root.root.string[0] == '.'
4332 && eh->elf.root.root.string[1] != '\0')
4333 eh->is_func = 1;
b34976b6 4334 return TRUE;
1e2f5b6e
AM
4335}
4336
5bd4f169 4337/* Look through the relocs for a section during the first phase, and
65f38f15 4338 calculate needed space in the global offset table, procedure
5d1634d7 4339 linkage table, and dynamic reloc sections. */
5bd4f169 4340
b34976b6 4341static bfd_boolean
4ce794b7
AM
4342ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4343 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 4344{
65f38f15 4345 struct ppc_link_hash_table *htab;
5bd4f169
AM
4346 Elf_Internal_Shdr *symtab_hdr;
4347 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
4348 const Elf_Internal_Rela *rel;
4349 const Elf_Internal_Rela *rel_end;
5bd4f169 4350 asection *sreloc;
1e2f5b6e 4351 asection **opd_sym_map;
5bd4f169 4352
1049f94e 4353 if (info->relocatable)
b34976b6 4354 return TRUE;
5bd4f169 4355
680a3378
AM
4356 /* Don't do anything special with non-loaded, non-alloced sections.
4357 In particular, any relocs in such sections should not affect GOT
4358 and PLT reference counting (ie. we don't allow them to create GOT
4359 or PLT entries), there's no possibility or desire to optimize TLS
4360 relocs, and there's not much point in propagating relocs to shared
4361 libs that the dynamic linker won't relocate. */
4362 if ((sec->flags & SEC_ALLOC) == 0)
4363 return TRUE;
4364
65f38f15 4365 htab = ppc_hash_table (info);
5bd4f169 4366 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5bd4f169
AM
4367
4368 sym_hashes = elf_sym_hashes (abfd);
4369 sym_hashes_end = (sym_hashes
1e2f5b6e
AM
4370 + symtab_hdr->sh_size / sizeof (Elf64_External_Sym)
4371 - symtab_hdr->sh_info);
5bd4f169
AM
4372
4373 sreloc = NULL;
1e2f5b6e
AM
4374 opd_sym_map = NULL;
4375 if (strcmp (bfd_get_section_name (abfd, sec), ".opd") == 0)
4376 {
4377 /* Garbage collection needs some extra help with .opd sections.
4378 We don't want to necessarily keep everything referenced by
4379 relocs in .opd, as that would keep all functions. Instead,
4380 if we reference an .opd symbol (a function descriptor), we
4381 want to keep the function code symbol's section. This is
4382 easy for global symbols, but for local syms we need to keep
4383 information about the associated function section. Later, if
4384 edit_opd deletes entries, we'll use this array to adjust
4385 local syms in .opd. */
4386 union opd_info {
4387 asection *func_section;
4388 long entry_adjust;
4389 };
4390 bfd_size_type amt;
4391
3f764659 4392 amt = sec->size * sizeof (union opd_info) / 8;
4ce794b7 4393 opd_sym_map = bfd_zalloc (abfd, amt);
1e2f5b6e 4394 if (opd_sym_map == NULL)
b34976b6 4395 return FALSE;
7c8fe5c4
AM
4396 ppc64_elf_section_data (sec)->u.opd_func_sec = opd_sym_map;
4397 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
4398 ppc64_elf_section_data (sec)->sec_type = sec_opd;
1e2f5b6e 4399 }
5bd4f169 4400
82bd7b59
AM
4401 if (htab->sfpr == NULL
4402 && !create_linkage_sections (htab->elf.dynobj, info))
b34976b6 4403 return FALSE;
82bd7b59 4404
5bd4f169
AM
4405 rel_end = relocs + sec->reloc_count;
4406 for (rel = relocs; rel < rel_end; rel++)
4407 {
4408 unsigned long r_symndx;
4409 struct elf_link_hash_entry *h;
04c9666a 4410 enum elf_ppc64_reloc_type r_type;
411e1bfb 4411 int tls_type = 0;
7c8fe5c4 4412 struct _ppc64_elf_section_data *ppc64_sec;
5bd4f169
AM
4413
4414 r_symndx = ELF64_R_SYM (rel->r_info);
4415 if (r_symndx < symtab_hdr->sh_info)
4416 h = NULL;
4417 else
973a3492
L
4418 {
4419 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4420 while (h->root.type == bfd_link_hash_indirect
4421 || h->root.type == bfd_link_hash_warning)
4422 h = (struct elf_link_hash_entry *) h->root.u.i.link;
4423 }
5bd4f169 4424
4ce794b7 4425 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 4426 switch (r_type)
5bd4f169 4427 {
411e1bfb
AM
4428 case R_PPC64_GOT_TLSLD16:
4429 case R_PPC64_GOT_TLSLD16_LO:
4430 case R_PPC64_GOT_TLSLD16_HI:
4431 case R_PPC64_GOT_TLSLD16_HA:
e717da7e 4432 ppc64_tlsld_got (abfd)->refcount += 1;
951fd09b 4433 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
4434 goto dogottls;
4435
4436 case R_PPC64_GOT_TLSGD16:
4437 case R_PPC64_GOT_TLSGD16_LO:
4438 case R_PPC64_GOT_TLSGD16_HI:
4439 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 4440 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
4441 goto dogottls;
4442
4443 case R_PPC64_GOT_TPREL16_DS:
4444 case R_PPC64_GOT_TPREL16_LO_DS:
4445 case R_PPC64_GOT_TPREL16_HI:
4446 case R_PPC64_GOT_TPREL16_HA:
4447 if (info->shared)
4448 info->flags |= DF_STATIC_TLS;
4449 tls_type = TLS_TLS | TLS_TPREL;
4450 goto dogottls;
4451
4452 case R_PPC64_GOT_DTPREL16_DS:
4453 case R_PPC64_GOT_DTPREL16_LO_DS:
4454 case R_PPC64_GOT_DTPREL16_HI:
4455 case R_PPC64_GOT_DTPREL16_HA:
4456 tls_type = TLS_TLS | TLS_DTPREL;
4457 dogottls:
4458 sec->has_tls_reloc = 1;
4459 /* Fall thru */
4460
5bd4f169 4461 case R_PPC64_GOT16:
5bd4f169 4462 case R_PPC64_GOT16_DS:
65f38f15
AM
4463 case R_PPC64_GOT16_HA:
4464 case R_PPC64_GOT16_HI:
4465 case R_PPC64_GOT16_LO:
5bd4f169 4466 case R_PPC64_GOT16_LO_DS:
65f38f15 4467 /* This symbol requires a global offset table entry. */
4c52953f 4468 sec->has_toc_reloc = 1;
e717da7e
AM
4469 if (ppc64_elf_tdata (abfd)->got == NULL
4470 && !create_got_section (abfd, info))
b34976b6 4471 return FALSE;
5bd4f169
AM
4472
4473 if (h != NULL)
4474 {
411e1bfb
AM
4475 struct ppc_link_hash_entry *eh;
4476 struct got_entry *ent;
65f38f15 4477
411e1bfb
AM
4478 eh = (struct ppc_link_hash_entry *) h;
4479 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4480 if (ent->addend == rel->r_addend
e717da7e 4481 && ent->owner == abfd
411e1bfb
AM
4482 && ent->tls_type == tls_type)
4483 break;
4484 if (ent == NULL)
5bd4f169 4485 {
411e1bfb 4486 bfd_size_type amt = sizeof (*ent);
4ce794b7 4487 ent = bfd_alloc (abfd, amt);
411e1bfb 4488 if (ent == NULL)
b34976b6 4489 return FALSE;
411e1bfb
AM
4490 ent->next = eh->elf.got.glist;
4491 ent->addend = rel->r_addend;
e717da7e 4492 ent->owner = abfd;
411e1bfb
AM
4493 ent->tls_type = tls_type;
4494 ent->got.refcount = 0;
4495 eh->elf.got.glist = ent;
5bd4f169 4496 }
411e1bfb 4497 ent->got.refcount += 1;
e7b938ca 4498 eh->tls_mask |= tls_type;
5bd4f169 4499 }
411e1bfb
AM
4500 else
4501 /* This is a global offset table entry for a local symbol. */
4502 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4503 rel->r_addend, tls_type))
4504 return FALSE;
5bd4f169
AM
4505 break;
4506
5bd4f169 4507 case R_PPC64_PLT16_HA:
65f38f15
AM
4508 case R_PPC64_PLT16_HI:
4509 case R_PPC64_PLT16_LO:
4510 case R_PPC64_PLT32:
4511 case R_PPC64_PLT64:
5bd4f169 4512 /* This symbol requires a procedure linkage table entry. We
3fad3c7c
AM
4513 actually build the entry in adjust_dynamic_symbol,
4514 because this might be a case of linking PIC code without
4515 linking in any dynamic objects, in which case we don't
4516 need to generate a procedure linkage table after all. */
5bd4f169
AM
4517 if (h == NULL)
4518 {
4519 /* It does not make sense to have a procedure linkage
3fad3c7c 4520 table entry for a local symbol. */
5bd4f169 4521 bfd_set_error (bfd_error_bad_value);
b34976b6 4522 return FALSE;
5bd4f169 4523 }
411e1bfb
AM
4524 else
4525 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4526 rel->r_addend))
4527 return FALSE;
5bd4f169
AM
4528 break;
4529
4530 /* The following relocations don't need to propagate the
4531 relocation if linking a shared object since they are
4532 section relative. */
4533 case R_PPC64_SECTOFF:
4534 case R_PPC64_SECTOFF_LO:
4535 case R_PPC64_SECTOFF_HI:
4536 case R_PPC64_SECTOFF_HA:
4537 case R_PPC64_SECTOFF_DS:
4538 case R_PPC64_SECTOFF_LO_DS:
411e1bfb
AM
4539 case R_PPC64_DTPREL16:
4540 case R_PPC64_DTPREL16_LO:
4541 case R_PPC64_DTPREL16_HI:
4542 case R_PPC64_DTPREL16_HA:
4543 case R_PPC64_DTPREL16_DS:
4544 case R_PPC64_DTPREL16_LO_DS:
4545 case R_PPC64_DTPREL16_HIGHER:
4546 case R_PPC64_DTPREL16_HIGHERA:
4547 case R_PPC64_DTPREL16_HIGHEST:
4548 case R_PPC64_DTPREL16_HIGHESTA:
5bd4f169
AM
4549 break;
4550
ad8e1ba5
AM
4551 /* Nor do these. */
4552 case R_PPC64_TOC16:
4553 case R_PPC64_TOC16_LO:
4554 case R_PPC64_TOC16_HI:
4555 case R_PPC64_TOC16_HA:
4556 case R_PPC64_TOC16_DS:
4557 case R_PPC64_TOC16_LO_DS:
4c52953f 4558 sec->has_toc_reloc = 1;
ad8e1ba5
AM
4559 break;
4560
5bd4f169
AM
4561 /* This relocation describes the C++ object vtable hierarchy.
4562 Reconstruct it for later use during GC. */
4563 case R_PPC64_GNU_VTINHERIT:
c152c796 4564 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
b34976b6 4565 return FALSE;
5bd4f169
AM
4566 break;
4567
4568 /* This relocation describes which C++ vtable entries are actually
4569 used. Record for later use during GC. */
4570 case R_PPC64_GNU_VTENTRY:
c152c796 4571 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
b34976b6 4572 return FALSE;
5bd4f169
AM
4573 break;
4574
721956f4
AM
4575 case R_PPC64_REL14:
4576 case R_PPC64_REL14_BRTAKEN:
4577 case R_PPC64_REL14_BRNTAKEN:
220c76dd
AM
4578 {
4579 asection *dest = NULL;
4580
4581 /* Heuristic: If jumping outside our section, chances are
4582 we are going to need a stub. */
4583 if (h != NULL)
4584 {
4585 /* If the sym is weak it may be overridden later, so
4586 don't assume we know where a weak sym lives. */
4587 if (h->root.type == bfd_link_hash_defined)
4588 dest = h->root.u.def.section;
4589 }
4590 else
4591 dest = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4592 sec, r_symndx);
4593 if (dest != sec)
7c8fe5c4 4594 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
220c76dd 4595 }
721956f4
AM
4596 /* Fall through. */
4597
5d1634d7 4598 case R_PPC64_REL24:
8387904d 4599 if (h != NULL)
5d1634d7
AM
4600 {
4601 /* We may need a .plt entry if the function this reloc
4602 refers to is in a shared lib. */
411e1bfb
AM
4603 if (!update_plt_info (abfd, (struct ppc_link_hash_entry *) h,
4604 rel->r_addend))
4605 return FALSE;
8387904d
AM
4606 if (h == &htab->tls_get_addr->elf
4607 || h == &htab->tls_get_addr_fd->elf)
411e1bfb 4608 sec->has_tls_reloc = 1;
8387904d 4609 else if (htab->tls_get_addr == NULL
0112cd26 4610 && CONST_STRNEQ (h->root.root.string, ".__tls_get_addr")
a48ebf4d
AM
4611 && (h->root.root.string[15] == 0
4612 || h->root.root.string[15] == '@'))
411e1bfb 4613 {
8387904d
AM
4614 htab->tls_get_addr = (struct ppc_link_hash_entry *) h;
4615 sec->has_tls_reloc = 1;
4616 }
4617 else if (htab->tls_get_addr_fd == NULL
0112cd26 4618 && CONST_STRNEQ (h->root.root.string, "__tls_get_addr")
8387904d
AM
4619 && (h->root.root.string[14] == 0
4620 || h->root.root.string[14] == '@'))
4621 {
4622 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) h;
411e1bfb
AM
4623 sec->has_tls_reloc = 1;
4624 }
4625 }
4626 break;
4627
4628 case R_PPC64_TPREL64:
4629 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
4630 if (info->shared)
4631 info->flags |= DF_STATIC_TLS;
4632 goto dotlstoc;
4633
4634 case R_PPC64_DTPMOD64:
4635 if (rel + 1 < rel_end
4636 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
4637 && rel[1].r_offset == rel->r_offset + 8)
951fd09b 4638 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
411e1bfb 4639 else
951fd09b 4640 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
411e1bfb
AM
4641 goto dotlstoc;
4642
4643 case R_PPC64_DTPREL64:
4644 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
4645 if (rel != relocs
4646 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
4647 && rel[-1].r_offset == rel->r_offset - 8)
4648 /* This is the second reloc of a dtpmod, dtprel pair.
4649 Don't mark with TLS_DTPREL. */
4650 goto dodyn;
4651
4652 dotlstoc:
4653 sec->has_tls_reloc = 1;
4654 if (h != NULL)
4655 {
4656 struct ppc_link_hash_entry *eh;
4657 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 4658 eh->tls_mask |= tls_type;
411e1bfb
AM
4659 }
4660 else
4661 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4662 rel->r_addend, tls_type))
4663 return FALSE;
4664
7c8fe5c4
AM
4665 ppc64_sec = ppc64_elf_section_data (sec);
4666 if (ppc64_sec->sec_type != sec_toc)
411e1bfb 4667 {
e7b938ca 4668 /* One extra to simplify get_tls_mask. */
eea6121a 4669 bfd_size_type amt = sec->size * sizeof (unsigned) / 8 + 1;
7c8fe5c4
AM
4670 ppc64_sec->u.t_symndx = bfd_zalloc (abfd, amt);
4671 if (ppc64_sec->u.t_symndx == NULL)
411e1bfb 4672 return FALSE;
7c8fe5c4
AM
4673 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
4674 ppc64_sec->sec_type = sec_toc;
411e1bfb
AM
4675 }
4676 BFD_ASSERT (rel->r_offset % 8 == 0);
7c8fe5c4 4677 ppc64_sec->u.t_symndx[rel->r_offset / 8] = r_symndx;
951fd09b
AM
4678
4679 /* Mark the second slot of a GD or LD entry.
4680 -1 to indicate GD and -2 to indicate LD. */
4681 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
7c8fe5c4 4682 ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -1;
951fd09b 4683 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
7c8fe5c4 4684 ppc64_sec->u.t_symndx[rel->r_offset / 8 + 1] = -2;
411e1bfb
AM
4685 goto dodyn;
4686
4687 case R_PPC64_TPREL16:
4688 case R_PPC64_TPREL16_LO:
4689 case R_PPC64_TPREL16_HI:
4690 case R_PPC64_TPREL16_HA:
4691 case R_PPC64_TPREL16_DS:
4692 case R_PPC64_TPREL16_LO_DS:
4693 case R_PPC64_TPREL16_HIGHER:
4694 case R_PPC64_TPREL16_HIGHERA:
4695 case R_PPC64_TPREL16_HIGHEST:
4696 case R_PPC64_TPREL16_HIGHESTA:
4697 if (info->shared)
4698 {
4699 info->flags |= DF_STATIC_TLS;
4700 goto dodyn;
5d1634d7
AM
4701 }
4702 break;
4703
e86ce104 4704 case R_PPC64_ADDR64:
1e2f5b6e 4705 if (opd_sym_map != NULL
1e2f5b6e 4706 && rel + 1 < rel_end
4ce794b7 4707 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
1e2f5b6e 4708 {
8387904d
AM
4709 if (h != NULL)
4710 {
4711 if (h->root.root.string[0] == '.'
4712 && h->root.root.string[1] != 0
4713 && get_fdh ((struct ppc_link_hash_entry *) h, htab))
4714 ;
4715 else
4716 ((struct ppc_link_hash_entry *) h)->is_func = 1;
4717 }
4718 else
4719 {
4720 asection *s;
1e2f5b6e 4721
8387904d
AM
4722 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec, sec,
4723 r_symndx);
4724 if (s == NULL)
4725 return FALSE;
4726 else if (s != sec)
3f764659 4727 opd_sym_map[rel->r_offset / 8] = s;
8387904d 4728 }
1e2f5b6e 4729 }
e86ce104
AM
4730 /* Fall through. */
4731
04c9666a 4732 case R_PPC64_REL30:
5bd4f169 4733 case R_PPC64_REL32:
04c9666a 4734 case R_PPC64_REL64:
65f38f15
AM
4735 case R_PPC64_ADDR14:
4736 case R_PPC64_ADDR14_BRNTAKEN:
4737 case R_PPC64_ADDR14_BRTAKEN:
4738 case R_PPC64_ADDR16:
4739 case R_PPC64_ADDR16_DS:
4740 case R_PPC64_ADDR16_HA:
4741 case R_PPC64_ADDR16_HI:
4742 case R_PPC64_ADDR16_HIGHER:
4743 case R_PPC64_ADDR16_HIGHERA:
4744 case R_PPC64_ADDR16_HIGHEST:
4745 case R_PPC64_ADDR16_HIGHESTA:
4746 case R_PPC64_ADDR16_LO:
4747 case R_PPC64_ADDR16_LO_DS:
4748 case R_PPC64_ADDR24:
65f38f15 4749 case R_PPC64_ADDR32:
65f38f15
AM
4750 case R_PPC64_UADDR16:
4751 case R_PPC64_UADDR32:
4752 case R_PPC64_UADDR64:
5bd4f169 4753 case R_PPC64_TOC:
81848ca0
AM
4754 if (h != NULL && !info->shared)
4755 /* We may need a copy reloc. */
f5385ebf 4756 h->non_got_ref = 1;
81848ca0 4757
41bd81ab 4758 /* Don't propagate .opd relocs. */
1e2f5b6e 4759 if (NO_OPD_RELOCS && opd_sym_map != NULL)
e86ce104 4760 break;
e86ce104 4761
65f38f15
AM
4762 /* If we are creating a shared library, and this is a reloc
4763 against a global symbol, or a non PC relative reloc
4764 against a local symbol, then we need to copy the reloc
4765 into the shared library. However, if we are linking with
4766 -Bsymbolic, we do not need to copy a reloc against a
4767 global symbol which is defined in an object we are
4768 including in the link (i.e., DEF_REGULAR is set). At
4769 this point we have not seen all the input files, so it is
4770 possible that DEF_REGULAR is not set now but will be set
4771 later (it is never cleared). In case of a weak definition,
4772 DEF_REGULAR may be cleared later by a strong definition in
4773 a shared library. We account for that possibility below by
f4656909 4774 storing information in the dyn_relocs field of the hash
65f38f15
AM
4775 table entry. A similar situation occurs when creating
4776 shared libraries and symbol visibility changes render the
4777 symbol local.
4778
4779 If on the other hand, we are creating an executable, we
4780 may need to keep relocations for symbols satisfied by a
4781 dynamic library if we manage to avoid copy relocs for the
4782 symbol. */
411e1bfb 4783 dodyn:
65f38f15 4784 if ((info->shared
411e1bfb 4785 && (MUST_BE_DYN_RELOC (r_type)
65f38f15
AM
4786 || (h != NULL
4787 && (! info->symbolic
4788 || h->root.type == bfd_link_hash_defweak
f5385ebf 4789 || !h->def_regular))))
f4656909
AM
4790 || (ELIMINATE_COPY_RELOCS
4791 && !info->shared
65f38f15
AM
4792 && h != NULL
4793 && (h->root.type == bfd_link_hash_defweak
f5385ebf 4794 || !h->def_regular)))
5bd4f169 4795 {
ec338859
AM
4796 struct ppc_dyn_relocs *p;
4797 struct ppc_dyn_relocs **head;
4798
65f38f15
AM
4799 /* We must copy these reloc types into the output file.
4800 Create a reloc section in dynobj and make room for
4801 this reloc. */
5bd4f169
AM
4802 if (sreloc == NULL)
4803 {
4804 const char *name;
65f38f15 4805 bfd *dynobj;
5bd4f169
AM
4806
4807 name = (bfd_elf_string_from_elf_section
4808 (abfd,
4809 elf_elfheader (abfd)->e_shstrndx,
4810 elf_section_data (sec)->rel_hdr.sh_name));
4811 if (name == NULL)
b34976b6 4812 return FALSE;
5bd4f169 4813
0112cd26 4814 if (! CONST_STRNEQ (name, ".rela")
65f38f15
AM
4815 || strcmp (bfd_get_section_name (abfd, sec),
4816 name + 5) != 0)
4817 {
4818 (*_bfd_error_handler)
d003868e
AM
4819 (_("%B: bad relocation section name `%s\'"),
4820 abfd, name);
5d1634d7 4821 bfd_set_error (bfd_error_bad_value);
65f38f15
AM
4822 }
4823
65f38f15 4824 dynobj = htab->elf.dynobj;
5bd4f169
AM
4825 sreloc = bfd_get_section_by_name (dynobj, name);
4826 if (sreloc == NULL)
4827 {
4828 flagword flags;
4829
5bd4f169 4830 flags = (SEC_HAS_CONTENTS | SEC_READONLY
77623a34
AM
4831 | SEC_IN_MEMORY | SEC_LINKER_CREATED
4832 | SEC_ALLOC | SEC_LOAD);
3496cb2a
L
4833 sreloc = bfd_make_section_with_flags (dynobj,
4834 name,
4835 flags);
5bd4f169 4836 if (sreloc == NULL
65f38f15 4837 || ! bfd_set_section_alignment (dynobj, sreloc, 3))
b34976b6 4838 return FALSE;
5bd4f169 4839 }
65f38f15 4840 elf_section_data (sec)->sreloc = sreloc;
5bd4f169
AM
4841 }
4842
65f38f15
AM
4843 /* If this is a global symbol, we count the number of
4844 relocations we need for this symbol. */
4845 if (h != NULL)
4846 {
ec338859 4847 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
65f38f15
AM
4848 }
4849 else
4850 {
ec338859
AM
4851 /* Track dynamic relocs needed for local syms too.
4852 We really need local syms available to do this
4853 easily. Oh well. */
4854
4855 asection *s;
6edfbbad
DJ
4856 void *vpp;
4857
ec338859
AM
4858 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
4859 sec, r_symndx);
4860 if (s == NULL)
b34976b6 4861 return FALSE;
ec338859 4862
6edfbbad
DJ
4863 vpp = &elf_section_data (s)->local_dynrel;
4864 head = (struct ppc_dyn_relocs **) vpp;
65f38f15 4865 }
ec338859
AM
4866
4867 p = *head;
4868 if (p == NULL || p->sec != sec)
4869 {
4ce794b7 4870 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
ec338859 4871 if (p == NULL)
b34976b6 4872 return FALSE;
ec338859
AM
4873 p->next = *head;
4874 *head = p;
4875 p->sec = sec;
4876 p->count = 0;
4877 p->pc_count = 0;
4878 }
4879
4880 p->count += 1;
411e1bfb 4881 if (!MUST_BE_DYN_RELOC (r_type))
ec338859 4882 p->pc_count += 1;
65f38f15 4883 }
5bd4f169 4884 break;
65f38f15
AM
4885
4886 default:
96e0dda4 4887 break;
5bd4f169
AM
4888 }
4889 }
4890
b34976b6 4891 return TRUE;
5bd4f169
AM
4892}
4893
8387904d
AM
4894/* OFFSET in OPD_SEC specifies a function descriptor. Return the address
4895 of the code entry point, and its section. */
4896
4897static bfd_vma
4898opd_entry_value (asection *opd_sec,
4899 bfd_vma offset,
4900 asection **code_sec,
4901 bfd_vma *code_off)
4902{
4903 bfd *opd_bfd = opd_sec->owner;
8860955f 4904 Elf_Internal_Rela *relocs;
8387904d 4905 Elf_Internal_Rela *lo, *hi, *look;
645ea6a9 4906 bfd_vma val;
8387904d 4907
4b85d634
AM
4908 /* No relocs implies we are linking a --just-symbols object. */
4909 if (opd_sec->reloc_count == 0)
4910 {
4911 bfd_vma val;
4912
4913 if (!bfd_get_section_contents (opd_bfd, opd_sec, &val, offset, 8))
4914 return (bfd_vma) -1;
3b36f7e6 4915
4b85d634
AM
4916 if (code_sec != NULL)
4917 {
4918 asection *sec, *likely = NULL;
4919 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
4920 if (sec->vma <= val
4921 && (sec->flags & SEC_LOAD) != 0
4922 && (sec->flags & SEC_ALLOC) != 0)
4923 likely = sec;
4924 if (likely != NULL)
4925 {
4926 *code_sec = likely;
4927 if (code_off != NULL)
4928 *code_off = val - likely->vma;
4929 }
4930 }
4931 return val;
4932 }
4933
8860955f
AM
4934 relocs = ppc64_elf_tdata (opd_bfd)->opd_relocs;
4935 if (relocs == NULL)
4936 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
645ea6a9 4937
8387904d 4938 /* Go find the opd reloc at the sym address. */
8860955f 4939 lo = relocs;
8387904d
AM
4940 BFD_ASSERT (lo != NULL);
4941 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
645ea6a9 4942 val = (bfd_vma) -1;
8387904d
AM
4943 while (lo < hi)
4944 {
4945 look = lo + (hi - lo) / 2;
4946 if (look->r_offset < offset)
4947 lo = look + 1;
4948 else if (look->r_offset > offset)
4949 hi = look;
4950 else
4951 {
4952 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (opd_bfd)->symtab_hdr;
4953 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
4954 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
4955 {
4956 unsigned long symndx = ELF64_R_SYM (look->r_info);
8387904d
AM
4957 asection *sec;
4958
4959 if (symndx < symtab_hdr->sh_info)
4960 {
4961 Elf_Internal_Sym *sym;
4962
4963 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
4964 if (sym == NULL)
4965 {
4966 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
4967 symtab_hdr->sh_info,
4968 0, NULL, NULL, NULL);
4969 if (sym == NULL)
645ea6a9 4970 break;
8387904d
AM
4971 symtab_hdr->contents = (bfd_byte *) sym;
4972 }
4973
4974 sym += symndx;
4975 val = sym->st_value;
4976 sec = NULL;
4977 if ((sym->st_shndx != SHN_UNDEF
4978 && sym->st_shndx < SHN_LORESERVE)
4979 || sym->st_shndx > SHN_HIRESERVE)
4980 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
4981 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
4982 }
4983 else
4984 {
4985 struct elf_link_hash_entry **sym_hashes;
4986 struct elf_link_hash_entry *rh;
4987
4988 sym_hashes = elf_sym_hashes (opd_bfd);
4989 rh = sym_hashes[symndx - symtab_hdr->sh_info];
4990 while (rh->root.type == bfd_link_hash_indirect
4991 || rh->root.type == bfd_link_hash_warning)
4992 rh = ((struct elf_link_hash_entry *) rh->root.u.i.link);
4993 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
4994 || rh->root.type == bfd_link_hash_defweak);
4995 val = rh->root.u.def.value;
4996 sec = rh->root.u.def.section;
4997 }
4998 val += look->r_addend;
4999 if (code_off != NULL)
5000 *code_off = val;
5001 if (code_sec != NULL)
5002 *code_sec = sec;
5003 if (sec != NULL && sec->output_section != NULL)
5004 val += sec->output_section->vma + sec->output_offset;
8387904d
AM
5005 }
5006 break;
5007 }
5008 }
645ea6a9 5009
645ea6a9 5010 return val;
8387904d
AM
5011}
5012
64d03ab5
AM
5013/* Mark sections containing dynamically referenced symbols. When
5014 building shared libraries, we must assume that any visible symbol is
5015 referenced. */
5016
5017static bfd_boolean
5018ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5019{
5020 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5021 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
5022
5023 if (eh->elf.root.type == bfd_link_hash_warning)
5024 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5025
5026 /* Dynamic linking info is on the func descriptor sym. */
5027 if (eh->oh != NULL
5028 && eh->oh->is_func_descriptor
5029 && (eh->oh->elf.root.type == bfd_link_hash_defined
5030 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5031 eh = eh->oh;
5032
5033 if ((eh->elf.root.type == bfd_link_hash_defined
5034 || eh->elf.root.type == bfd_link_hash_defweak)
5035 && (eh->elf.ref_dynamic
5036 || (!info->executable
5037 && eh->elf.def_regular
5038 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5039 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN)))
5040 {
5041 asection *code_sec;
5042
5043 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5044
5045 /* Function descriptor syms cause the associated
5046 function code sym section to be marked. */
5047 if (eh->is_func_descriptor
5048 && (eh->oh->elf.root.type == bfd_link_hash_defined
5049 || eh->oh->elf.root.type == bfd_link_hash_defweak))
5050 eh->oh->elf.root.u.def.section->flags |= SEC_KEEP;
5051 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5052 && opd_entry_value (eh->elf.root.u.def.section,
5053 eh->elf.root.u.def.value,
5054 &code_sec, NULL) != (bfd_vma) -1)
5055 code_sec->flags |= SEC_KEEP;
5056 }
5057
5058 return TRUE;
5059}
5060
5bd4f169
AM
5061/* Return the section that should be marked against GC for a given
5062 relocation. */
5063
5064static asection *
4ce794b7 5065ppc64_elf_gc_mark_hook (asection *sec,
ccfa59ea 5066 struct bfd_link_info *info,
4ce794b7
AM
5067 Elf_Internal_Rela *rel,
5068 struct elf_link_hash_entry *h,
5069 Elf_Internal_Sym *sym)
5bd4f169 5070{
ccfa59ea
AM
5071 asection *rsec;
5072
5073 /* First mark all our entry sym sections. */
5074 if (info->gc_sym_list != NULL)
5075 {
5076 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5077 struct bfd_sym_chain *sym = info->gc_sym_list;
5078
5079 info->gc_sym_list = NULL;
b56cbdb9 5080 for (; sym != NULL; sym = sym->next)
ccfa59ea
AM
5081 {
5082 struct ppc_link_hash_entry *eh;
5083
5084 eh = (struct ppc_link_hash_entry *)
5085 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, FALSE);
5086 if (eh == NULL)
5087 continue;
5088 if (eh->elf.root.type != bfd_link_hash_defined
5089 && eh->elf.root.type != bfd_link_hash_defweak)
5090 continue;
5091
c4f68ce3
AM
5092 if (eh->is_func_descriptor
5093 && (eh->oh->elf.root.type == bfd_link_hash_defined
5094 || eh->oh->elf.root.type == bfd_link_hash_defweak))
ccfa59ea 5095 rsec = eh->oh->elf.root.u.def.section;
8387904d
AM
5096 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5097 && opd_entry_value (eh->elf.root.u.def.section,
5098 eh->elf.root.u.def.value,
5099 &rsec, NULL) != (bfd_vma) -1)
5100 ;
ccfa59ea
AM
5101 else
5102 continue;
5103
5104 if (!rsec->gc_mark)
5105 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5106
5107 rsec = eh->elf.root.u.def.section;
5108 if (!rsec->gc_mark)
5109 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
ccfa59ea 5110 }
ccfa59ea
AM
5111 }
5112
5113 /* Syms return NULL if we're marking .opd, so we avoid marking all
5114 function sections, as all functions are referenced in .opd. */
5115 rsec = NULL;
5116 if (get_opd_info (sec) != NULL)
5117 return rsec;
1e2f5b6e 5118
5bd4f169
AM
5119 if (h != NULL)
5120 {
04c9666a 5121 enum elf_ppc64_reloc_type r_type;
ccfa59ea 5122 struct ppc_link_hash_entry *eh;
a33d1f77 5123
4ce794b7 5124 r_type = ELF64_R_TYPE (rel->r_info);
a33d1f77 5125 switch (r_type)
5bd4f169
AM
5126 {
5127 case R_PPC64_GNU_VTINHERIT:
5128 case R_PPC64_GNU_VTENTRY:
5129 break;
5130
5131 default:
5132 switch (h->root.type)
5133 {
5134 case bfd_link_hash_defined:
5135 case bfd_link_hash_defweak:
ccfa59ea 5136 eh = (struct ppc_link_hash_entry *) h;
c4f68ce3
AM
5137 if (eh->oh != NULL
5138 && eh->oh->is_func_descriptor
5139 && (eh->oh->elf.root.type == bfd_link_hash_defined
5140 || eh->oh->elf.root.type == bfd_link_hash_defweak))
ccfa59ea 5141 eh = eh->oh;
1e2f5b6e
AM
5142
5143 /* Function descriptor syms cause the associated
5144 function code sym section to be marked. */
c4f68ce3
AM
5145 if (eh->is_func_descriptor
5146 && (eh->oh->elf.root.type == bfd_link_hash_defined
5147 || eh->oh->elf.root.type == bfd_link_hash_defweak))
ccfa59ea
AM
5148 {
5149 /* They also mark their opd section. */
5150 if (!eh->elf.root.u.def.section->gc_mark)
5151 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5152 ppc64_elf_gc_mark_hook);
5153
5154 rsec = eh->oh->elf.root.u.def.section;
5155 }
8387904d
AM
5156 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5157 && opd_entry_value (eh->elf.root.u.def.section,
5158 eh->elf.root.u.def.value,
5159 &rsec, NULL) != (bfd_vma) -1)
5160 {
5161 if (!eh->elf.root.u.def.section->gc_mark)
5162 _bfd_elf_gc_mark (info, eh->elf.root.u.def.section,
5163 ppc64_elf_gc_mark_hook);
5164 }
ccfa59ea 5165 else
1e2f5b6e
AM
5166 rsec = h->root.u.def.section;
5167 break;
5bd4f169
AM
5168
5169 case bfd_link_hash_common:
1e2f5b6e
AM
5170 rsec = h->root.u.c.p->section;
5171 break;
5bd4f169
AM
5172
5173 default:
5174 break;
5175 }
5176 }
5177 }
5178 else
5179 {
1e2f5b6e
AM
5180 asection **opd_sym_section;
5181
5182 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
ccfa59ea 5183 opd_sym_section = get_opd_info (rsec);
1e2f5b6e 5184 if (opd_sym_section != NULL)
ccfa59ea
AM
5185 {
5186 if (!rsec->gc_mark)
5187 _bfd_elf_gc_mark (info, rsec, ppc64_elf_gc_mark_hook);
5188
b4f4e59f 5189 rsec = opd_sym_section[(sym->st_value + rel->r_addend) / 8];
ccfa59ea 5190 }
5bd4f169
AM
5191 }
5192
1e2f5b6e 5193 return rsec;
5bd4f169
AM
5194}
5195
65f38f15
AM
5196/* Update the .got, .plt. and dynamic reloc reference counts for the
5197 section being removed. */
5bd4f169 5198
b34976b6 5199static bfd_boolean
4ce794b7
AM
5200ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
5201 asection *sec, const Elf_Internal_Rela *relocs)
5bd4f169 5202{
411e1bfb 5203 struct ppc_link_hash_table *htab;
5bd4f169
AM
5204 Elf_Internal_Shdr *symtab_hdr;
5205 struct elf_link_hash_entry **sym_hashes;
411e1bfb 5206 struct got_entry **local_got_ents;
5bd4f169 5207 const Elf_Internal_Rela *rel, *relend;
5bd4f169 5208
680a3378
AM
5209 if ((sec->flags & SEC_ALLOC) == 0)
5210 return TRUE;
5211
ec338859
AM
5212 elf_section_data (sec)->local_dynrel = NULL;
5213
411e1bfb 5214 htab = ppc_hash_table (info);
5bd4f169
AM
5215 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5216 sym_hashes = elf_sym_hashes (abfd);
411e1bfb 5217 local_got_ents = elf_local_got_ents (abfd);
5bd4f169
AM
5218
5219 relend = relocs + sec->reloc_count;
5220 for (rel = relocs; rel < relend; rel++)
a33d1f77
AM
5221 {
5222 unsigned long r_symndx;
04c9666a 5223 enum elf_ppc64_reloc_type r_type;
58ac9f71 5224 struct elf_link_hash_entry *h = NULL;
411e1bfb 5225 char tls_type = 0;
5bd4f169 5226
a33d1f77 5227 r_symndx = ELF64_R_SYM (rel->r_info);
4ce794b7 5228 r_type = ELF64_R_TYPE (rel->r_info);
58ac9f71
AM
5229 if (r_symndx >= symtab_hdr->sh_info)
5230 {
5231 struct ppc_link_hash_entry *eh;
5232 struct ppc_dyn_relocs **pp;
5233 struct ppc_dyn_relocs *p;
5234
5235 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3eb128b2
AM
5236 while (h->root.type == bfd_link_hash_indirect
5237 || h->root.type == bfd_link_hash_warning)
5238 h = (struct elf_link_hash_entry *) h->root.u.i.link;
58ac9f71
AM
5239 eh = (struct ppc_link_hash_entry *) h;
5240
5241 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
5242 if (p->sec == sec)
5243 {
5244 /* Everything must go for SEC. */
5245 *pp = p->next;
5246 break;
5247 }
5248 }
5249
a33d1f77
AM
5250 switch (r_type)
5251 {
411e1bfb
AM
5252 case R_PPC64_GOT_TLSLD16:
5253 case R_PPC64_GOT_TLSLD16_LO:
5254 case R_PPC64_GOT_TLSLD16_HI:
5255 case R_PPC64_GOT_TLSLD16_HA:
e717da7e 5256 ppc64_tlsld_got (abfd)->refcount -= 1;
951fd09b 5257 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
5258 goto dogot;
5259
5260 case R_PPC64_GOT_TLSGD16:
5261 case R_PPC64_GOT_TLSGD16_LO:
5262 case R_PPC64_GOT_TLSGD16_HI:
5263 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 5264 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
5265 goto dogot;
5266
5267 case R_PPC64_GOT_TPREL16_DS:
5268 case R_PPC64_GOT_TPREL16_LO_DS:
5269 case R_PPC64_GOT_TPREL16_HI:
5270 case R_PPC64_GOT_TPREL16_HA:
5271 tls_type = TLS_TLS | TLS_TPREL;
5272 goto dogot;
5273
5274 case R_PPC64_GOT_DTPREL16_DS:
5275 case R_PPC64_GOT_DTPREL16_LO_DS:
5276 case R_PPC64_GOT_DTPREL16_HI:
5277 case R_PPC64_GOT_DTPREL16_HA:
5278 tls_type = TLS_TLS | TLS_DTPREL;
5279 goto dogot;
5280
a33d1f77
AM
5281 case R_PPC64_GOT16:
5282 case R_PPC64_GOT16_DS:
5283 case R_PPC64_GOT16_HA:
5284 case R_PPC64_GOT16_HI:
5285 case R_PPC64_GOT16_LO:
5286 case R_PPC64_GOT16_LO_DS:
411e1bfb
AM
5287 dogot:
5288 {
5289 struct got_entry *ent;
5290
58ac9f71
AM
5291 if (h != NULL)
5292 ent = h->got.glist;
411e1bfb
AM
5293 else
5294 ent = local_got_ents[r_symndx];
5295
5296 for (; ent != NULL; ent = ent->next)
5297 if (ent->addend == rel->r_addend
e717da7e 5298 && ent->owner == abfd
411e1bfb
AM
5299 && ent->tls_type == tls_type)
5300 break;
5301 if (ent == NULL)
5302 abort ();
5303 if (ent->got.refcount > 0)
5304 ent->got.refcount -= 1;
5305 }
a33d1f77 5306 break;
65f38f15 5307
a33d1f77
AM
5308 case R_PPC64_PLT16_HA:
5309 case R_PPC64_PLT16_HI:
5310 case R_PPC64_PLT16_LO:
5311 case R_PPC64_PLT32:
5312 case R_PPC64_PLT64:
721956f4
AM
5313 case R_PPC64_REL14:
5314 case R_PPC64_REL14_BRNTAKEN:
5315 case R_PPC64_REL14_BRTAKEN:
5d1634d7 5316 case R_PPC64_REL24:
58ac9f71 5317 if (h != NULL)
5d1634d7 5318 {
411e1bfb
AM
5319 struct plt_entry *ent;
5320
411e1bfb
AM
5321 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5322 if (ent->addend == rel->r_addend)
5323 break;
5324 if (ent == NULL)
5325 abort ();
5326 if (ent->plt.refcount > 0)
5327 ent->plt.refcount -= 1;
5d1634d7 5328 }
e86ce104 5329 break;
5d1634d7 5330
a33d1f77
AM
5331 default:
5332 break;
5333 }
5334 }
b34976b6 5335 return TRUE;
5bd4f169
AM
5336}
5337
deb0e272
AM
5338/* The maximum size of .sfpr. */
5339#define SFPR_MAX (218*4)
5340
5341struct sfpr_def_parms
5342{
699733f6
AM
5343 const char name[12];
5344 unsigned char lo, hi;
deb0e272
AM
5345 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
5346 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
5347};
5348
5349/* Auto-generate _save*, _rest* functions in .sfpr. */
5350
5351static unsigned int
5352sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
5353{
5354 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5355 unsigned int i;
5356 size_t len = strlen (parm->name);
5357 bfd_boolean writing = FALSE;
699733f6 5358 char sym[16];
deb0e272
AM
5359
5360 memcpy (sym, parm->name, len);
5361 sym[len + 2] = 0;
5362
5363 for (i = parm->lo; i <= parm->hi; i++)
5364 {
5365 struct elf_link_hash_entry *h;
5366
5367 sym[len + 0] = i / 10 + '0';
5368 sym[len + 1] = i % 10 + '0';
5369 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
5370 if (h != NULL
f5385ebf 5371 && !h->def_regular)
deb0e272
AM
5372 {
5373 h->root.type = bfd_link_hash_defined;
5374 h->root.u.def.section = htab->sfpr;
5375 h->root.u.def.value = htab->sfpr->size;
5376 h->type = STT_FUNC;
f5385ebf 5377 h->def_regular = 1;
deb0e272
AM
5378 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
5379 writing = TRUE;
5380 if (htab->sfpr->contents == NULL)
5381 {
5382 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5383 if (htab->sfpr->contents == NULL)
5384 return FALSE;
5385 }
5386 }
5387 if (writing)
5388 {
5389 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5390 if (i != parm->hi)
5391 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5392 else
5393 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5394 htab->sfpr->size = p - htab->sfpr->contents;
5395 }
5396 }
5397
5398 return TRUE;
5399}
5400
5401static bfd_byte *
5402savegpr0 (bfd *abfd, bfd_byte *p, int r)
5403{
5404 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5405 return p + 4;
5406}
5407
5408static bfd_byte *
5409savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5410{
5411 p = savegpr0 (abfd, p, r);
5412 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5413 p = p + 4;
5414 bfd_put_32 (abfd, BLR, p);
5415 return p + 4;
5416}
5417
5418static bfd_byte *
5419restgpr0 (bfd *abfd, bfd_byte *p, int r)
5420{
5421 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5422 return p + 4;
5423}
5424
5425static bfd_byte *
5426restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
5427{
5428 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5429 p = p + 4;
5430 p = restgpr0 (abfd, p, r);
5431 bfd_put_32 (abfd, MTLR_R0, p);
5432 p = p + 4;
5433 if (r == 29)
5434 {
5435 p = restgpr0 (abfd, p, 30);
5436 p = restgpr0 (abfd, p, 31);
5437 }
5438 bfd_put_32 (abfd, BLR, p);
5439 return p + 4;
5440}
5441
5442static bfd_byte *
5443savegpr1 (bfd *abfd, bfd_byte *p, int r)
5444{
5445 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5446 return p + 4;
5447}
5448
5449static bfd_byte *
5450savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
5451{
5452 p = savegpr1 (abfd, p, r);
5453 bfd_put_32 (abfd, BLR, p);
5454 return p + 4;
5455}
5456
5457static bfd_byte *
5458restgpr1 (bfd *abfd, bfd_byte *p, int r)
5459{
5460 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5461 return p + 4;
5462}
5463
5464static bfd_byte *
5465restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
5466{
5467 p = restgpr1 (abfd, p, r);
5468 bfd_put_32 (abfd, BLR, p);
5469 return p + 4;
5470}
5471
5472static bfd_byte *
5473savefpr (bfd *abfd, bfd_byte *p, int r)
5474{
5475 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5476 return p + 4;
5477}
5478
5479static bfd_byte *
5480savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
5481{
5482 p = savefpr (abfd, p, r);
5483 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
5484 p = p + 4;
5485 bfd_put_32 (abfd, BLR, p);
5486 return p + 4;
5487}
5488
5489static bfd_byte *
5490restfpr (bfd *abfd, bfd_byte *p, int r)
5491{
5492 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5493 return p + 4;
5494}
5495
5496static bfd_byte *
5497restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
5498{
5499 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
5500 p = p + 4;
5501 p = restfpr (abfd, p, r);
5502 bfd_put_32 (abfd, MTLR_R0, p);
5503 p = p + 4;
5504 if (r == 29)
5505 {
5506 p = restfpr (abfd, p, 30);
5507 p = restfpr (abfd, p, 31);
5508 }
5509 bfd_put_32 (abfd, BLR, p);
5510 return p + 4;
5511}
5512
5513static bfd_byte *
5514savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
5515{
5516 p = savefpr (abfd, p, r);
5517 bfd_put_32 (abfd, BLR, p);
5518 return p + 4;
5519}
5520
5521static bfd_byte *
5522restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
5523{
5524 p = restfpr (abfd, p, r);
5525 bfd_put_32 (abfd, BLR, p);
5526 return p + 4;
5527}
5528
5529static bfd_byte *
5530savevr (bfd *abfd, bfd_byte *p, int r)
5531{
5532 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5533 p = p + 4;
5534 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
5535 return p + 4;
5536}
5537
5538static bfd_byte *
5539savevr_tail (bfd *abfd, bfd_byte *p, int r)
5540{
5541 p = savevr (abfd, p, r);
5542 bfd_put_32 (abfd, BLR, p);
5543 return p + 4;
5544}
5545
5546static bfd_byte *
5547restvr (bfd *abfd, bfd_byte *p, int r)
5548{
5549 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
5550 p = p + 4;
5551 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
5552 return p + 4;
5553}
5554
5555static bfd_byte *
5556restvr_tail (bfd *abfd, bfd_byte *p, int r)
5557{
5558 p = restvr (abfd, p, r);
5559 bfd_put_32 (abfd, BLR, p);
5560 return p + 4;
5561}
5562
e86ce104
AM
5563/* Called via elf_link_hash_traverse to transfer dynamic linking
5564 information on function code symbol entries to their corresponding
5565 function descriptor symbol entries. */
deb0e272 5566
b34976b6 5567static bfd_boolean
4ce794b7 5568func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
5bd4f169 5569{
e86ce104 5570 struct bfd_link_info *info;
65f38f15 5571 struct ppc_link_hash_table *htab;
411e1bfb 5572 struct plt_entry *ent;
50bc7936
AM
5573 struct ppc_link_hash_entry *fh;
5574 struct ppc_link_hash_entry *fdh;
5575 bfd_boolean force_local;
5bd4f169 5576
50bc7936
AM
5577 fh = (struct ppc_link_hash_entry *) h;
5578 if (fh->elf.root.type == bfd_link_hash_indirect)
b34976b6 5579 return TRUE;
e86ce104 5580
50bc7936
AM
5581 if (fh->elf.root.type == bfd_link_hash_warning)
5582 fh = (struct ppc_link_hash_entry *) fh->elf.root.u.i.link;
e92d460e 5583
4ce794b7 5584 info = inf;
65f38f15 5585 htab = ppc_hash_table (info);
5bd4f169 5586
c09bdfe5
AM
5587 /* Resolve undefined references to dot-symbols as the value
5588 in the function descriptor, if we have one in a regular object.
5589 This is to satisfy cases like ".quad .foo". Calls to functions
5590 in dynamic objects are handled elsewhere. */
5591 if (fh->elf.root.type == bfd_link_hash_undefweak
5592 && fh->was_undefined
5593 && (fh->oh->elf.root.type == bfd_link_hash_defined
5594 || fh->oh->elf.root.type == bfd_link_hash_defweak)
5595 && get_opd_info (fh->oh->elf.root.u.def.section) != NULL
5596 && opd_entry_value (fh->oh->elf.root.u.def.section,
5597 fh->oh->elf.root.u.def.value,
5598 &fh->elf.root.u.def.section,
5599 &fh->elf.root.u.def.value) != (bfd_vma) -1)
5600 {
5601 fh->elf.root.type = fh->oh->elf.root.type;
f5385ebf 5602 fh->elf.forced_local = 1;
c09bdfe5
AM
5603 }
5604
e86ce104
AM
5605 /* If this is a function code symbol, transfer dynamic linking
5606 information to the function descriptor symbol. */
50bc7936 5607 if (!fh->is_func)
b34976b6 5608 return TRUE;
e86ce104 5609
50bc7936 5610 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
5611 if (ent->plt.refcount > 0)
5612 break;
50bc7936
AM
5613 if (ent == NULL
5614 || fh->elf.root.root.string[0] != '.'
5615 || fh->elf.root.root.string[1] == '\0')
5616 return TRUE;
5bd4f169 5617
50bc7936
AM
5618 /* Find the corresponding function descriptor symbol. Create it
5619 as undefined if necessary. */
5bd4f169 5620
50bc7936
AM
5621 fdh = get_fdh (fh, htab);
5622 if (fdh != NULL)
5623 while (fdh->elf.root.type == bfd_link_hash_indirect
5624 || fdh->elf.root.type == bfd_link_hash_warning)
5625 fdh = (struct ppc_link_hash_entry *) fdh->elf.root.u.i.link;
5bd4f169 5626
50bc7936
AM
5627 if (fdh == NULL
5628 && info->shared
5629 && (fh->elf.root.type == bfd_link_hash_undefined
5630 || fh->elf.root.type == bfd_link_hash_undefweak))
5631 {
908b32fc 5632 fdh = make_fdh (info, fh);
bb700d78
AM
5633 if (fdh == NULL)
5634 return FALSE;
50bc7936 5635 }
648cca2c 5636
908b32fc 5637 /* Fake function descriptors are made undefweak. If the function
433817dd
AM
5638 code symbol is strong undefined, make the fake sym the same.
5639 If the function code symbol is defined, then force the fake
5640 descriptor local; We can't support overriding of symbols in a
5641 shared library on a fake descriptor. */
908b32fc
AM
5642
5643 if (fdh != NULL
5644 && fdh->fake
433817dd 5645 && fdh->elf.root.type == bfd_link_hash_undefweak)
908b32fc 5646 {
433817dd
AM
5647 if (fh->elf.root.type == bfd_link_hash_undefined)
5648 {
5649 fdh->elf.root.type = bfd_link_hash_undefined;
5650 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
5651 }
5652 else if (fh->elf.root.type == bfd_link_hash_defined
5653 || fh->elf.root.type == bfd_link_hash_defweak)
5654 {
5655 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
5656 }
908b32fc
AM
5657 }
5658
50bc7936 5659 if (fdh != NULL
f5385ebf 5660 && !fdh->elf.forced_local
50bc7936 5661 && (info->shared
f5385ebf
AM
5662 || fdh->elf.def_dynamic
5663 || fdh->elf.ref_dynamic
50bc7936
AM
5664 || (fdh->elf.root.type == bfd_link_hash_undefweak
5665 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
5666 {
5667 if (fdh->elf.dynindx == -1)
c152c796 5668 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
50bc7936 5669 return FALSE;
f5385ebf
AM
5670 fdh->elf.ref_regular |= fh->elf.ref_regular;
5671 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
5672 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
5673 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
50bc7936 5674 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
e86ce104 5675 {
40d16e0b 5676 move_plt_plist (fh, fdh);
f5385ebf 5677 fdh->elf.needs_plt = 1;
e86ce104 5678 }
50bc7936 5679 fdh->is_func_descriptor = 1;
34814b9f
AM
5680 fdh->oh = fh;
5681 fh->oh = fdh;
e86ce104
AM
5682 }
5683
50bc7936
AM
5684 /* Now that the info is on the function descriptor, clear the
5685 function code sym info. Any function code syms for which we
5686 don't have a definition in a regular file, we force local.
5687 This prevents a shared library from exporting syms that have
5688 been imported from another library. Function code syms that
5689 are really in the library we must leave global to prevent the
5690 linker dragging in a definition from a static library. */
93f3fa99
AM
5691 force_local = (!fh->elf.def_regular
5692 || fdh == NULL
5693 || !fdh->elf.def_regular
5694 || fdh->elf.forced_local);
50bc7936
AM
5695 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
5696
b34976b6 5697 return TRUE;
e86ce104 5698}
40b8271b 5699
e86ce104 5700/* Called near the start of bfd_elf_size_dynamic_sections. We use
82bd7b59
AM
5701 this hook to a) provide some gcc support functions, and b) transfer
5702 dynamic linking information gathered so far on function code symbol
5703 entries, to their corresponding function descriptor symbol entries. */
deb0e272 5704
b34976b6 5705static bfd_boolean
4ce794b7
AM
5706ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
5707 struct bfd_link_info *info)
e86ce104
AM
5708{
5709 struct ppc_link_hash_table *htab;
82bd7b59 5710 unsigned int i;
deb0e272
AM
5711 const struct sfpr_def_parms funcs[] =
5712 {
5713 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
5714 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
5715 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
5716 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
5717 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
5718 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
5719 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
5720 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
5721 { "._savef", 14, 31, savefpr, savefpr1_tail },
5722 { "._restf", 14, 31, restfpr, restfpr1_tail },
5723 { "_savevr_", 20, 31, savevr, savevr_tail },
5724 { "_restvr_", 20, 31, restvr, restvr_tail }
5725 };
e86ce104
AM
5726
5727 htab = ppc_hash_table (info);
82bd7b59
AM
5728 if (htab->sfpr == NULL)
5729 /* We don't have any relocs. */
b34976b6 5730 return TRUE;
82bd7b59 5731
deb0e272
AM
5732 /* Provide any missing _save* and _rest* functions. */
5733 htab->sfpr->size = 0;
5734 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
5735 if (!sfpr_define (info, &funcs[i]))
5736 return FALSE;
82bd7b59 5737
4ce794b7 5738 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
805fc799 5739
eea6121a 5740 if (htab->sfpr->size == 0)
8423293d 5741 htab->sfpr->flags |= SEC_EXCLUDE;
82bd7b59 5742
b34976b6 5743 return TRUE;
e86ce104
AM
5744}
5745
5746/* Adjust a symbol defined by a dynamic object and referenced by a
5747 regular object. The current definition is in some section of the
5748 dynamic object, but we're not including those sections. We have to
5749 change the definition to something the rest of the link can
5750 understand. */
5751
b34976b6 5752static bfd_boolean
4ce794b7
AM
5753ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
5754 struct elf_link_hash_entry *h)
e86ce104
AM
5755{
5756 struct ppc_link_hash_table *htab;
e86ce104
AM
5757 asection *s;
5758 unsigned int power_of_two;
5759
5760 htab = ppc_hash_table (info);
5761
5762 /* Deal with function syms. */
5763 if (h->type == STT_FUNC
f5385ebf 5764 || h->needs_plt)
e86ce104
AM
5765 {
5766 /* Clear procedure linkage table information for any symbol that
5767 won't need a .plt entry. */
411e1bfb
AM
5768 struct plt_entry *ent;
5769 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
5770 if (ent->plt.refcount > 0)
5771 break;
8387904d 5772 if (ent == NULL
9c7a29a3
AM
5773 || SYMBOL_CALLS_LOCAL (info, h)
5774 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
5775 && h->root.type == bfd_link_hash_undefweak))
40b8271b 5776 {
411e1bfb 5777 h->plt.plist = NULL;
f5385ebf 5778 h->needs_plt = 0;
40b8271b 5779 }
5bd4f169 5780 }
bbd7ec4a 5781 else
411e1bfb 5782 h->plt.plist = NULL;
5bd4f169
AM
5783
5784 /* If this is a weak symbol, and there is a real definition, the
5785 processor independent code will have arranged for us to see the
5786 real definition first, and we can just use the same value. */
f6e332e6 5787 if (h->u.weakdef != NULL)
5bd4f169 5788 {
f6e332e6
AM
5789 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
5790 || h->u.weakdef->root.type == bfd_link_hash_defweak);
5791 h->root.u.def.section = h->u.weakdef->root.u.def.section;
5792 h->root.u.def.value = h->u.weakdef->root.u.def.value;
a23b6845 5793 if (ELIMINATE_COPY_RELOCS)
f6e332e6 5794 h->non_got_ref = h->u.weakdef->non_got_ref;
b34976b6 5795 return TRUE;
5bd4f169
AM
5796 }
5797
5bd4f169
AM
5798 /* If we are creating a shared library, we must presume that the
5799 only references to the symbol are via the global offset table.
5800 For such cases we need not do anything here; the relocations will
5801 be handled correctly by relocate_section. */
5802 if (info->shared)
b34976b6 5803 return TRUE;
5bd4f169 5804
65f38f15
AM
5805 /* If there are no references to this symbol that do not use the
5806 GOT, we don't need to generate a copy reloc. */
f5385ebf 5807 if (!h->non_got_ref)
b34976b6 5808 return TRUE;
65f38f15 5809
f4656909 5810 if (ELIMINATE_COPY_RELOCS)
65f38f15 5811 {
f4656909
AM
5812 struct ppc_link_hash_entry * eh;
5813 struct ppc_dyn_relocs *p;
65f38f15 5814
f4656909
AM
5815 eh = (struct ppc_link_hash_entry *) h;
5816 for (p = eh->dyn_relocs; p != NULL; p = p->next)
5817 {
5818 s = p->sec->output_section;
5819 if (s != NULL && (s->flags & SEC_READONLY) != 0)
5820 break;
5821 }
5822
5823 /* If we didn't find any dynamic relocs in read-only sections, then
5824 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
5825 if (p == NULL)
5826 {
f5385ebf 5827 h->non_got_ref = 0;
f4656909
AM
5828 return TRUE;
5829 }
65f38f15
AM
5830 }
5831
5d35169e 5832 if (h->plt.plist != NULL)
97b639ba
AM
5833 {
5834 /* We should never get here, but unfortunately there are versions
5835 of gcc out there that improperly (for this ABI) put initialized
5836 function pointers, vtable refs and suchlike in read-only
5837 sections. Allow them to proceed, but warn that this might
5838 break at runtime. */
5839 (*_bfd_error_handler)
5840 (_("copy reloc against `%s' requires lazy plt linking; "
5841 "avoid setting LD_BIND_NOW=1 or upgrade gcc"),
5842 h->root.root.string);
5843 }
5d35169e
AM
5844
5845 /* This is a reference to a symbol defined by a dynamic object which
5846 is not a function. */
5847
909272ee
AM
5848 if (h->size == 0)
5849 {
5850 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
5851 h->root.root.string);
5852 return TRUE;
5853 }
5854
5bd4f169
AM
5855 /* We must allocate the symbol in our .dynbss section, which will
5856 become part of the .bss section of the executable. There will be
5857 an entry for this symbol in the .dynsym section. The dynamic
5858 object will contain position independent code, so all references
5859 from the dynamic object to this symbol will go through the global
5860 offset table. The dynamic linker will use the .dynsym entry to
5861 determine the address it must put in the global offset table, so
5862 both the dynamic object and the regular object will refer to the
5863 same memory location for the variable. */
5bd4f169 5864
04c9666a
AM
5865 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
5866 to copy the initial value out of the dynamic object and into the
5bd4f169
AM
5867 runtime process image. We need to remember the offset into the
5868 .rela.bss section we are going to use. */
5869 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5870 {
eea6121a 5871 htab->relbss->size += sizeof (Elf64_External_Rela);
f5385ebf 5872 h->needs_copy = 1;
5bd4f169
AM
5873 }
5874
5875 /* We need to figure out the alignment required for this symbol. I
5876 have no idea how ELF linkers handle this. */
5877 power_of_two = bfd_log2 (h->size);
5878 if (power_of_two > 4)
5879 power_of_two = 4;
5880
5881 /* Apply the required alignment. */
4ce794b7 5882 s = htab->dynbss;
eea6121a 5883 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
65f38f15 5884 if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
5bd4f169 5885 {
65f38f15 5886 if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
b34976b6 5887 return FALSE;
5bd4f169
AM
5888 }
5889
5890 /* Define the symbol as being at this point in the section. */
5891 h->root.u.def.section = s;
eea6121a 5892 h->root.u.def.value = s->size;
5bd4f169
AM
5893
5894 /* Increment the section size to make room for the symbol. */
eea6121a 5895 s->size += h->size;
5bd4f169 5896
b34976b6 5897 return TRUE;
5bd4f169
AM
5898}
5899
e86ce104
AM
5900/* If given a function descriptor symbol, hide both the function code
5901 sym and the descriptor. */
5902static void
4ce794b7
AM
5903ppc64_elf_hide_symbol (struct bfd_link_info *info,
5904 struct elf_link_hash_entry *h,
5905 bfd_boolean force_local)
e86ce104 5906{
34814b9f 5907 struct ppc_link_hash_entry *eh;
e86ce104
AM
5908 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
5909
34814b9f
AM
5910 eh = (struct ppc_link_hash_entry *) h;
5911 if (eh->is_func_descriptor)
e86ce104 5912 {
34814b9f 5913 struct ppc_link_hash_entry *fh = eh->oh;
e86ce104 5914
721956f4 5915 if (fh == NULL)
d1329ca3
AM
5916 {
5917 const char *p, *q;
5918 struct ppc_link_hash_table *htab;
5919 char save;
5920
5921 /* We aren't supposed to use alloca in BFD because on
5922 systems which do not have alloca the version in libiberty
5923 calls xmalloc, which might cause the program to crash
5924 when it runs out of memory. This function doesn't have a
5925 return status, so there's no way to gracefully return an
5926 error. So cheat. We know that string[-1] can be safely
34814b9f
AM
5927 accessed; It's either a string in an ELF string table,
5928 or allocated in an objalloc structure. */
d1329ca3 5929
34814b9f 5930 p = eh->elf.root.root.string - 1;
d1329ca3
AM
5931 save = *p;
5932 *(char *) p = '.';
5933 htab = ppc_hash_table (info);
34814b9f
AM
5934 fh = (struct ppc_link_hash_entry *)
5935 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
5936 *(char *) p = save;
5937
5938 /* Unfortunately, if it so happens that the string we were
5939 looking for was allocated immediately before this string,
5940 then we overwrote the string terminator. That's the only
5941 reason the lookup should fail. */
5942 if (fh == NULL)
5943 {
34814b9f
AM
5944 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
5945 while (q >= eh->elf.root.root.string && *q == *p)
d1329ca3 5946 --q, --p;
34814b9f
AM
5947 if (q < eh->elf.root.root.string && *p == '.')
5948 fh = (struct ppc_link_hash_entry *)
5949 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
d1329ca3
AM
5950 }
5951 if (fh != NULL)
5952 {
34814b9f
AM
5953 eh->oh = fh;
5954 fh->oh = eh;
d1329ca3
AM
5955 }
5956 }
e86ce104 5957 if (fh != NULL)
34814b9f 5958 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
e86ce104
AM
5959 }
5960}
5961
411e1bfb 5962static bfd_boolean
8843416a
AM
5963get_sym_h (struct elf_link_hash_entry **hp,
5964 Elf_Internal_Sym **symp,
5965 asection **symsecp,
5966 char **tls_maskp,
5967 Elf_Internal_Sym **locsymsp,
5968 unsigned long r_symndx,
5969 bfd *ibfd)
411e1bfb
AM
5970{
5971 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
5972
5973 if (r_symndx >= symtab_hdr->sh_info)
5974 {
5975 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
5976 struct elf_link_hash_entry *h;
5977
5978 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5979 while (h->root.type == bfd_link_hash_indirect
5980 || h->root.type == bfd_link_hash_warning)
5981 h = (struct elf_link_hash_entry *) h->root.u.i.link;
5982
5983 if (hp != NULL)
5984 *hp = h;
5985
5986 if (symp != NULL)
5987 *symp = NULL;
5988
5989 if (symsecp != NULL)
5990 {
5991 asection *symsec = NULL;
5992 if (h->root.type == bfd_link_hash_defined
5993 || h->root.type == bfd_link_hash_defweak)
5994 symsec = h->root.u.def.section;
5995 *symsecp = symsec;
5996 }
5997
e7b938ca 5998 if (tls_maskp != NULL)
411e1bfb
AM
5999 {
6000 struct ppc_link_hash_entry *eh;
6001
6002 eh = (struct ppc_link_hash_entry *) h;
e7b938ca 6003 *tls_maskp = &eh->tls_mask;
411e1bfb
AM
6004 }
6005 }
6006 else
6007 {
6008 Elf_Internal_Sym *sym;
6009 Elf_Internal_Sym *locsyms = *locsymsp;
6010
6011 if (locsyms == NULL)
6012 {
6013 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6014 if (locsyms == NULL)
6015 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6016 symtab_hdr->sh_info,
6017 0, NULL, NULL, NULL);
6018 if (locsyms == NULL)
6019 return FALSE;
6020 *locsymsp = locsyms;
6021 }
6022 sym = locsyms + r_symndx;
6023
6024 if (hp != NULL)
6025 *hp = NULL;
6026
6027 if (symp != NULL)
6028 *symp = sym;
6029
6030 if (symsecp != NULL)
6031 {
6032 asection *symsec = NULL;
6033 if ((sym->st_shndx != SHN_UNDEF
6034 && sym->st_shndx < SHN_LORESERVE)
6035 || sym->st_shndx > SHN_HIRESERVE)
6036 symsec = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6037 *symsecp = symsec;
6038 }
6039
e7b938ca 6040 if (tls_maskp != NULL)
411e1bfb
AM
6041 {
6042 struct got_entry **lgot_ents;
e7b938ca 6043 char *tls_mask;
411e1bfb 6044
e7b938ca 6045 tls_mask = NULL;
411e1bfb
AM
6046 lgot_ents = elf_local_got_ents (ibfd);
6047 if (lgot_ents != NULL)
6048 {
e7b938ca
AM
6049 char *lgot_masks = (char *) (lgot_ents + symtab_hdr->sh_info);
6050 tls_mask = &lgot_masks[r_symndx];
411e1bfb 6051 }
e7b938ca 6052 *tls_maskp = tls_mask;
411e1bfb
AM
6053 }
6054 }
6055 return TRUE;
6056}
6057
e7b938ca 6058/* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
951fd09b 6059 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
ad8e1ba5 6060 type suitable for optimization, and 1 otherwise. */
951fd09b
AM
6061
6062static int
0d4792f7
AM
6063get_tls_mask (char **tls_maskp, unsigned long *toc_symndx,
6064 Elf_Internal_Sym **locsymsp,
4ce794b7 6065 const Elf_Internal_Rela *rel, bfd *ibfd)
411e1bfb
AM
6066{
6067 unsigned long r_symndx;
0d4792f7 6068 int next_r;
411e1bfb
AM
6069 struct elf_link_hash_entry *h;
6070 Elf_Internal_Sym *sym;
6071 asection *sec;
6072 bfd_vma off;
6073
6074 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6075 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6076 return 0;
411e1bfb 6077
e7b938ca 6078 if ((*tls_maskp != NULL && **tls_maskp != 0)
411e1bfb 6079 || sec == NULL
7c8fe5c4 6080 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
951fd09b 6081 return 1;
411e1bfb
AM
6082
6083 /* Look inside a TOC section too. */
6084 if (h != NULL)
6085 {
6086 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6087 off = h->root.u.def.value;
6088 }
6089 else
6090 off = sym->st_value;
6091 off += rel->r_addend;
6092 BFD_ASSERT (off % 8 == 0);
7c8fe5c4
AM
6093 r_symndx = ppc64_elf_section_data (sec)->u.t_symndx[off / 8];
6094 next_r = ppc64_elf_section_data (sec)->u.t_symndx[off / 8 + 1];
e7b938ca 6095 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
951fd09b 6096 return 0;
0d4792f7
AM
6097 if (toc_symndx != NULL)
6098 *toc_symndx = r_symndx;
6099 if ((h == NULL
6100 || ((h->root.type == bfd_link_hash_defined
6101 || h->root.type == bfd_link_hash_defweak)
f5385ebf 6102 && !h->def_dynamic))
0d4792f7
AM
6103 && (next_r == -1 || next_r == -2))
6104 return 1 - next_r;
951fd09b 6105 return 1;
411e1bfb
AM
6106}
6107
754021d0 6108/* Adjust all global syms defined in opd sections. In gcc generated
8387904d 6109 code for the old ABI, these will already have been done. */
754021d0
AM
6110
6111static bfd_boolean
6112adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6113{
6114 struct ppc_link_hash_entry *eh;
6115 asection *sym_sec;
6116 long *opd_adjust;
6117
6118 if (h->root.type == bfd_link_hash_indirect)
6119 return TRUE;
6120
6121 if (h->root.type == bfd_link_hash_warning)
6122 h = (struct elf_link_hash_entry *) h->root.u.i.link;
6123
6124 if (h->root.type != bfd_link_hash_defined
6125 && h->root.type != bfd_link_hash_defweak)
6126 return TRUE;
6127
6128 eh = (struct ppc_link_hash_entry *) h;
6129 if (eh->adjust_done)
6130 return TRUE;
6131
6132 sym_sec = eh->elf.root.u.def.section;
4025353c
AM
6133 opd_adjust = get_opd_info (sym_sec);
6134 if (opd_adjust != NULL)
754021d0 6135 {
3f764659 6136 long adjust = opd_adjust[eh->elf.root.u.def.value / 8];
4025353c
AM
6137 if (adjust == -1)
6138 {
6139 /* This entry has been deleted. */
b3fac117 6140 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
81688140
AM
6141 if (dsec == NULL)
6142 {
6143 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6144 if (elf_discarded_section (dsec))
6145 {
b3fac117 6146 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
81688140
AM
6147 break;
6148 }
6149 }
4025353c 6150 eh->elf.root.u.def.value = 0;
81688140 6151 eh->elf.root.u.def.section = dsec;
4025353c
AM
6152 }
6153 else
6154 eh->elf.root.u.def.value += adjust;
754021d0
AM
6155 eh->adjust_done = 1;
6156 }
6157 return TRUE;
6158}
6159
8c1d1bb8
AM
6160/* Handles decrementing dynamic reloc counts for the reloc specified by
6161 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM_SEC
6162 have already been determined. */
6163
6164static bfd_boolean
6165dec_dynrel_count (bfd_vma r_info,
6166 asection *sec,
6167 struct bfd_link_info *info,
6168 Elf_Internal_Sym **local_syms,
6169 struct elf_link_hash_entry *h,
6170 asection *sym_sec)
6171{
6172 enum elf_ppc64_reloc_type r_type;
6173 struct ppc_dyn_relocs *p;
6174 struct ppc_dyn_relocs **pp;
6175
6176 /* Can this reloc be dynamic? This switch, and later tests here
6177 should be kept in sync with the code in check_relocs. */
6178 r_type = ELF64_R_TYPE (r_info);
6179 switch (r_type)
6180 {
6181 default:
6182 return TRUE;
6183
6184 case R_PPC64_TPREL16:
6185 case R_PPC64_TPREL16_LO:
6186 case R_PPC64_TPREL16_HI:
6187 case R_PPC64_TPREL16_HA:
6188 case R_PPC64_TPREL16_DS:
6189 case R_PPC64_TPREL16_LO_DS:
6190 case R_PPC64_TPREL16_HIGHER:
6191 case R_PPC64_TPREL16_HIGHERA:
6192 case R_PPC64_TPREL16_HIGHEST:
6193 case R_PPC64_TPREL16_HIGHESTA:
6194 if (!info->shared)
6195 return TRUE;
6196
6197 case R_PPC64_TPREL64:
6198 case R_PPC64_DTPMOD64:
6199 case R_PPC64_DTPREL64:
6200 case R_PPC64_ADDR64:
6201 case R_PPC64_REL30:
6202 case R_PPC64_REL32:
6203 case R_PPC64_REL64:
6204 case R_PPC64_ADDR14:
6205 case R_PPC64_ADDR14_BRNTAKEN:
6206 case R_PPC64_ADDR14_BRTAKEN:
6207 case R_PPC64_ADDR16:
6208 case R_PPC64_ADDR16_DS:
6209 case R_PPC64_ADDR16_HA:
6210 case R_PPC64_ADDR16_HI:
6211 case R_PPC64_ADDR16_HIGHER:
6212 case R_PPC64_ADDR16_HIGHERA:
6213 case R_PPC64_ADDR16_HIGHEST:
6214 case R_PPC64_ADDR16_HIGHESTA:
6215 case R_PPC64_ADDR16_LO:
6216 case R_PPC64_ADDR16_LO_DS:
6217 case R_PPC64_ADDR24:
6218 case R_PPC64_ADDR32:
6219 case R_PPC64_UADDR16:
6220 case R_PPC64_UADDR32:
6221 case R_PPC64_UADDR64:
6222 case R_PPC64_TOC:
6223 break;
6224 }
6225
6226 if (local_syms != NULL)
6227 {
6228 unsigned long r_symndx;
6229 Elf_Internal_Sym *sym;
6230 bfd *ibfd = sec->owner;
6231
6232 r_symndx = ELF64_R_SYM (r_info);
6233 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
6234 return FALSE;
6235 }
6236
6237 if ((info->shared
6238 && (MUST_BE_DYN_RELOC (r_type)
6239 || (h != NULL
6240 && (!info->symbolic
6241 || h->root.type == bfd_link_hash_defweak
6242 || !h->def_regular))))
6243 || (ELIMINATE_COPY_RELOCS
6244 && !info->shared
6245 && h != NULL
6246 && (h->root.type == bfd_link_hash_defweak
6247 || !h->def_regular)))
6248 ;
6249 else
6250 return TRUE;
6251
6252 if (h != NULL)
6253 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
8c1d1bb8 6254 else
6edfbbad 6255 {
60124e18
AM
6256 if (sym_sec != NULL)
6257 {
6258 void *vpp = &elf_section_data (sym_sec)->local_dynrel;
6259 pp = (struct ppc_dyn_relocs **) vpp;
6260 }
6261 else
6262 {
6263 void *vpp = &elf_section_data (sec)->local_dynrel;
6264 pp = (struct ppc_dyn_relocs **) vpp;
6265 }
6266
6267 /* elf_gc_sweep may have already removed all dyn relocs associated
6268 with local syms for a given section. Don't report a dynreloc
6269 miscount. */
6270 if (*pp == NULL)
6271 return TRUE;
6edfbbad 6272 }
8c1d1bb8
AM
6273
6274 while ((p = *pp) != NULL)
6275 {
6276 if (p->sec == sec)
6277 {
6278 if (!MUST_BE_DYN_RELOC (r_type))
6279 p->pc_count -= 1;
6280 p->count -= 1;
6281 if (p->count == 0)
6282 *pp = p->next;
6283 return TRUE;
6284 }
6285 pp = &p->next;
6286 }
6287
6288 (*_bfd_error_handler) (_("dynreloc miscount for %B, section %A"),
6289 sec->owner, sec);
6290 bfd_set_error (bfd_error_bad_value);
6291 return FALSE;
6292}
6293
754021d0
AM
6294/* Remove unused Official Procedure Descriptor entries. Currently we
6295 only remove those associated with functions in discarded link-once
6296 sections, or weakly defined functions that have been overridden. It
6297 would be possible to remove many more entries for statically linked
6298 applications. */
6299
b34976b6 6300bfd_boolean
3f764659 6301ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
b4f4e59f 6302 bfd_boolean no_opd_opt,
3f764659 6303 bfd_boolean non_overlapping)
1e2f5b6e
AM
6304{
6305 bfd *ibfd;
754021d0 6306 bfd_boolean some_edited = FALSE;
3f764659 6307 asection *need_pad = NULL;
1e2f5b6e 6308
411e1bfb 6309 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
1e2f5b6e
AM
6310 {
6311 asection *sec;
6312 Elf_Internal_Rela *relstart, *rel, *relend;
6313 Elf_Internal_Shdr *symtab_hdr;
6cdc0ccc 6314 Elf_Internal_Sym *local_syms;
1e2f5b6e
AM
6315 struct elf_link_hash_entry **sym_hashes;
6316 bfd_vma offset;
d6fe2dc1 6317 bfd_size_type amt;
4025353c 6318 long *opd_adjust;
3f764659
JJ
6319 bfd_boolean need_edit, add_aux_fields;
6320 bfd_size_type cnt_16b = 0;
1e2f5b6e
AM
6321
6322 sec = bfd_get_section_by_name (ibfd, ".opd");
46de2a7c 6323 if (sec == NULL || sec->size == 0)
1e2f5b6e
AM
6324 continue;
6325
3f764659 6326 amt = sec->size * sizeof (long) / 8;
4025353c
AM
6327 opd_adjust = get_opd_info (sec);
6328 if (opd_adjust == NULL)
d6fe2dc1 6329 {
4b85d634
AM
6330 /* check_relocs hasn't been called. Must be a ld -r link
6331 or --just-symbols object. */
b4f4e59f 6332 opd_adjust = bfd_alloc (obfd, amt);
46de2a7c
AM
6333 if (opd_adjust == NULL)
6334 return FALSE;
7c8fe5c4
AM
6335 ppc64_elf_section_data (sec)->u.opd_adjust = opd_adjust;
6336 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
6337 ppc64_elf_section_data (sec)->sec_type = sec_opd;
d6fe2dc1 6338 }
4025353c 6339 memset (opd_adjust, 0, amt);
1e2f5b6e 6340
b4f4e59f
AM
6341 if (no_opd_opt)
6342 continue;
6343
4b85d634
AM
6344 if (sec->sec_info_type == ELF_INFO_TYPE_JUST_SYMS)
6345 continue;
6346
1e2f5b6e
AM
6347 if (sec->output_section == bfd_abs_section_ptr)
6348 continue;
6349
6350 /* Look through the section relocs. */
6351 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
6352 continue;
6353
6cdc0ccc 6354 local_syms = NULL;
1e2f5b6e
AM
6355 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
6356 sym_hashes = elf_sym_hashes (ibfd);
6357
6358 /* Read the relocations. */
4ce794b7 6359 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6360 info->keep_memory);
1e2f5b6e 6361 if (relstart == NULL)
b34976b6 6362 return FALSE;
1e2f5b6e
AM
6363
6364 /* First run through the relocs to check they are sane, and to
6365 determine whether we need to edit this opd section. */
b34976b6 6366 need_edit = FALSE;
3f764659 6367 need_pad = sec;
1e2f5b6e
AM
6368 offset = 0;
6369 relend = relstart + sec->reloc_count;
50bc7936 6370 for (rel = relstart; rel < relend; )
1e2f5b6e 6371 {
04c9666a 6372 enum elf_ppc64_reloc_type r_type;
1e2f5b6e
AM
6373 unsigned long r_symndx;
6374 asection *sym_sec;
6375 struct elf_link_hash_entry *h;
6376 Elf_Internal_Sym *sym;
6377
3f764659 6378 /* .opd contains a regular array of 16 or 24 byte entries. We're
1e2f5b6e
AM
6379 only interested in the reloc pointing to a function entry
6380 point. */
50bc7936
AM
6381 if (rel->r_offset != offset
6382 || rel + 1 >= relend
6383 || (rel + 1)->r_offset != offset + 8)
1e2f5b6e
AM
6384 {
6385 /* If someone messes with .opd alignment then after a
6386 "ld -r" we might have padding in the middle of .opd.
6387 Also, there's nothing to prevent someone putting
6388 something silly in .opd with the assembler. No .opd
b34976b6 6389 optimization for them! */
3f764659 6390 broken_opd:
1e2f5b6e 6391 (*_bfd_error_handler)
d003868e 6392 (_("%B: .opd is not a regular array of opd entries"), ibfd);
b34976b6 6393 need_edit = FALSE;
1e2f5b6e
AM
6394 break;
6395 }
6396
50bc7936
AM
6397 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
6398 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
6399 {
6400 (*_bfd_error_handler)
d003868e
AM
6401 (_("%B: unexpected reloc type %u in .opd section"),
6402 ibfd, r_type);
50bc7936
AM
6403 need_edit = FALSE;
6404 break;
6405 }
6406
1e2f5b6e 6407 r_symndx = ELF64_R_SYM (rel->r_info);
411e1bfb
AM
6408 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
6409 r_symndx, ibfd))
50bc7936 6410 goto error_ret;
1e2f5b6e
AM
6411
6412 if (sym_sec == NULL || sym_sec->owner == NULL)
6413 {
411e1bfb
AM
6414 const char *sym_name;
6415 if (h != NULL)
6416 sym_name = h->root.root.string;
6417 else
26c61ae5
L
6418 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
6419 sym_sec);
411e1bfb 6420
1e2f5b6e 6421 (*_bfd_error_handler)
d003868e
AM
6422 (_("%B: undefined sym `%s' in .opd section"),
6423 ibfd, sym_name);
b34976b6 6424 need_edit = FALSE;
1e2f5b6e
AM
6425 break;
6426 }
6427
51020317
AM
6428 /* opd entries are always for functions defined in the
6429 current input bfd. If the symbol isn't defined in the
6430 input bfd, then we won't be using the function in this
6431 bfd; It must be defined in a linkonce section in another
6432 bfd, or is weak. It's also possible that we are
6433 discarding the function due to a linker script /DISCARD/,
6434 which we test for via the output_section. */
6435 if (sym_sec->owner != ibfd
6436 || sym_sec->output_section == bfd_abs_section_ptr)
b34976b6 6437 need_edit = TRUE;
1e2f5b6e 6438
50bc7936 6439 rel += 2;
3f764659
JJ
6440 if (rel == relend
6441 || (rel + 1 == relend && rel->r_offset == offset + 16))
6442 {
6443 if (sec->size == offset + 24)
6444 {
6445 need_pad = NULL;
6446 break;
6447 }
6448 if (rel == relend && sec->size == offset + 16)
6449 {
6450 cnt_16b++;
6451 break;
6452 }
6453 goto broken_opd;
6454 }
6455
6456 if (rel->r_offset == offset + 24)
6457 offset += 24;
6458 else if (rel->r_offset != offset + 16)
6459 goto broken_opd;
6460 else if (rel + 1 < relend
6461 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
6462 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
6463 {
6464 offset += 16;
6465 cnt_16b++;
6466 }
6467 else if (rel + 2 < relend
6468 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
6469 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
6470 {
6471 offset += 24;
6472 rel += 1;
6473 }
6474 else
6475 goto broken_opd;
1e2f5b6e
AM
6476 }
6477
3f764659
JJ
6478 add_aux_fields = non_overlapping && cnt_16b > 0;
6479
6480 if (need_edit || add_aux_fields)
1e2f5b6e
AM
6481 {
6482 Elf_Internal_Rela *write_rel;
6483 bfd_byte *rptr, *wptr;
3f764659 6484 bfd_byte *new_contents = NULL;
b34976b6 6485 bfd_boolean skip;
3f764659 6486 long opd_ent_size;
1e2f5b6e
AM
6487
6488 /* This seems a waste of time as input .opd sections are all
6489 zeros as generated by gcc, but I suppose there's no reason
6490 this will always be so. We might start putting something in
6491 the third word of .opd entries. */
6492 if ((sec->flags & SEC_IN_MEMORY) == 0)
6493 {
eea6121a
AM
6494 bfd_byte *loc;
6495 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
6cdc0ccc 6496 {
eea6121a
AM
6497 if (loc != NULL)
6498 free (loc);
50bc7936 6499 error_ret:
6cdc0ccc
AM
6500 if (local_syms != NULL
6501 && symtab_hdr->contents != (unsigned char *) local_syms)
6502 free (local_syms);
6cdc0ccc
AM
6503 if (elf_section_data (sec)->relocs != relstart)
6504 free (relstart);
b34976b6 6505 return FALSE;
6cdc0ccc 6506 }
1e2f5b6e
AM
6507 sec->contents = loc;
6508 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6509 }
6510
6511 elf_section_data (sec)->relocs = relstart;
6512
3f764659 6513 new_contents = sec->contents;
3f764659
JJ
6514 if (add_aux_fields)
6515 {
6516 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
6517 if (new_contents == NULL)
6518 return FALSE;
6519 need_pad = FALSE;
3f764659 6520 }
b4f4e59f
AM
6521 wptr = new_contents;
6522 rptr = sec->contents;
3f764659 6523
1e2f5b6e 6524 write_rel = relstart;
b34976b6 6525 skip = FALSE;
1e2f5b6e 6526 offset = 0;
3f764659 6527 opd_ent_size = 0;
1e2f5b6e
AM
6528 for (rel = relstart; rel < relend; rel++)
6529 {
50bc7936
AM
6530 unsigned long r_symndx;
6531 asection *sym_sec;
6532 struct elf_link_hash_entry *h;
6533 Elf_Internal_Sym *sym;
6534
6535 r_symndx = ELF64_R_SYM (rel->r_info);
6536 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
d37c89e5 6537 r_symndx, ibfd))
50bc7936
AM
6538 goto error_ret;
6539
1e2f5b6e
AM
6540 if (rel->r_offset == offset)
6541 {
50bc7936 6542 struct ppc_link_hash_entry *fdh = NULL;
3f764659
JJ
6543
6544 /* See if the .opd entry is full 24 byte or
6545 16 byte (with fd_aux entry overlapped with next
6546 fd_func). */
6547 opd_ent_size = 24;
6548 if ((rel + 2 == relend && sec->size == offset + 16)
6549 || (rel + 3 < relend
6550 && rel[2].r_offset == offset + 16
6551 && rel[3].r_offset == offset + 24
6552 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
6553 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
6554 opd_ent_size = 16;
6555
4025353c
AM
6556 if (h != NULL
6557 && h->root.root.string[0] == '.')
c4f68ce3
AM
6558 {
6559 fdh = get_fdh ((struct ppc_link_hash_entry *) h,
6560 ppc_hash_table (info));
6561 if (fdh != NULL
6562 && fdh->elf.root.type != bfd_link_hash_defined
6563 && fdh->elf.root.type != bfd_link_hash_defweak)
6564 fdh = NULL;
6565 }
1e2f5b6e 6566
51020317
AM
6567 skip = (sym_sec->owner != ibfd
6568 || sym_sec->output_section == bfd_abs_section_ptr);
a4aa0fb7
AM
6569 if (skip)
6570 {
4025353c 6571 if (fdh != NULL && sym_sec->owner == ibfd)
a4aa0fb7
AM
6572 {
6573 /* Arrange for the function descriptor sym
6574 to be dropped. */
d6fe2dc1
AM
6575 fdh->elf.root.u.def.value = 0;
6576 fdh->elf.root.u.def.section = sym_sec;
a4aa0fb7 6577 }
3f764659 6578 opd_adjust[rel->r_offset / 8] = -1;
a4aa0fb7
AM
6579 }
6580 else
1e2f5b6e
AM
6581 {
6582 /* We'll be keeping this opd entry. */
6583
4025353c 6584 if (fdh != NULL)
1e2f5b6e 6585 {
754021d0
AM
6586 /* Redefine the function descriptor symbol to
6587 this location in the opd section. It is
6588 necessary to update the value here rather
6589 than using an array of adjustments as we do
6590 for local symbols, because various places
6591 in the generic ELF code use the value
6592 stored in u.def.value. */
3f764659 6593 fdh->elf.root.u.def.value = wptr - new_contents;
754021d0 6594 fdh->adjust_done = 1;
1e2f5b6e 6595 }
754021d0
AM
6596
6597 /* Local syms are a bit tricky. We could
6598 tweak them as they can be cached, but
6599 we'd need to look through the local syms
6600 for the function descriptor sym which we
6601 don't have at the moment. So keep an
6602 array of adjustments. */
3f764659
JJ
6603 opd_adjust[rel->r_offset / 8]
6604 = (wptr - new_contents) - (rptr - sec->contents);
1e2f5b6e
AM
6605
6606 if (wptr != rptr)
3f764659
JJ
6607 memcpy (wptr, rptr, opd_ent_size);
6608 wptr += opd_ent_size;
6609 if (add_aux_fields && opd_ent_size == 16)
6610 {
6611 memset (wptr, '\0', 8);
6612 wptr += 8;
6613 }
1e2f5b6e 6614 }
3f764659
JJ
6615 rptr += opd_ent_size;
6616 offset += opd_ent_size;
1e2f5b6e
AM
6617 }
6618
50bc7936
AM
6619 if (skip)
6620 {
60124e18
AM
6621 if (!NO_OPD_RELOCS
6622 && !info->relocatable
18d944df
AM
6623 && !dec_dynrel_count (rel->r_info, sec, info,
6624 NULL, h, sym_sec))
8c1d1bb8 6625 goto error_ret;
50bc7936
AM
6626 }
6627 else
1e2f5b6e 6628 {
50bc7936
AM
6629 /* We need to adjust any reloc offsets to point to the
6630 new opd entries. While we're at it, we may as well
6631 remove redundant relocs. */
3f764659 6632 rel->r_offset += opd_adjust[(offset - opd_ent_size) / 8];
1e2f5b6e
AM
6633 if (write_rel != rel)
6634 memcpy (write_rel, rel, sizeof (*rel));
6635 ++write_rel;
6636 }
6637 }
6638
3f764659 6639 sec->size = wptr - new_contents;
1e2f5b6e 6640 sec->reloc_count = write_rel - relstart;
3f764659
JJ
6641 if (add_aux_fields)
6642 {
6643 free (sec->contents);
6644 sec->contents = new_contents;
6645 }
6646
05bf9422 6647 /* Fudge the header size too, as this is used later in
cdcf6e38
AM
6648 elf_bfd_final_link if we are emitting relocs. */
6649 elf_section_data (sec)->rel_hdr.sh_size
6650 = sec->reloc_count * elf_section_data (sec)->rel_hdr.sh_entsize;
6651 BFD_ASSERT (elf_section_data (sec)->rel_hdr2 == NULL);
754021d0 6652 some_edited = TRUE;
1e2f5b6e 6653 }
6cdc0ccc 6654 else if (elf_section_data (sec)->relocs != relstart)
1e2f5b6e 6655 free (relstart);
6cdc0ccc 6656
411e1bfb
AM
6657 if (local_syms != NULL
6658 && symtab_hdr->contents != (unsigned char *) local_syms)
6659 {
6660 if (!info->keep_memory)
6661 free (local_syms);
6662 else
6663 symtab_hdr->contents = (unsigned char *) local_syms;
6664 }
6665 }
6666
754021d0
AM
6667 if (some_edited)
6668 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
6669
3f764659
JJ
6670 /* If we are doing a final link and the last .opd entry is just 16 byte
6671 long, add a 8 byte padding after it. */
6672 if (need_pad != NULL && !info->relocatable)
6673 {
6674 bfd_byte *p;
6675
6676 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
6677 {
6678 BFD_ASSERT (need_pad->size > 0);
6679
6680 p = bfd_malloc (need_pad->size + 8);
6681 if (p == NULL)
6682 return FALSE;
699733f6 6683
3f764659
JJ
6684 if (! bfd_get_section_contents (need_pad->owner, need_pad,
6685 p, 0, need_pad->size))
6686 return FALSE;
6687
6688 need_pad->contents = p;
6689 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
6690 }
6691 else
6692 {
6693 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
6694 if (p == NULL)
6695 return FALSE;
6696
6697 need_pad->contents = p;
6698 }
6699
6700 memset (need_pad->contents + need_pad->size, 0, 8);
6701 need_pad->size += 8;
6702 }
6703
411e1bfb
AM
6704 return TRUE;
6705}
6706
e1918d23 6707/* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
411e1bfb 6708
e1918d23 6709asection *
4ce794b7 6710ppc64_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
411e1bfb 6711{
411e1bfb
AM
6712 struct ppc_link_hash_table *htab;
6713
411e1bfb 6714 htab = ppc_hash_table (info);
a48ebf4d
AM
6715 if (htab->tls_get_addr != NULL)
6716 {
8387904d 6717 struct ppc_link_hash_entry *h = htab->tls_get_addr;
a48ebf4d 6718
8387904d
AM
6719 while (h->elf.root.type == bfd_link_hash_indirect
6720 || h->elf.root.type == bfd_link_hash_warning)
6721 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
a48ebf4d
AM
6722
6723 htab->tls_get_addr = h;
8387904d
AM
6724
6725 if (htab->tls_get_addr_fd == NULL
6726 && h->oh != NULL
c4f68ce3
AM
6727 && h->oh->is_func_descriptor
6728 && (h->oh->elf.root.type == bfd_link_hash_defined
6729 || h->oh->elf.root.type == bfd_link_hash_defweak))
8387904d
AM
6730 htab->tls_get_addr_fd = h->oh;
6731 }
6732
6733 if (htab->tls_get_addr_fd != NULL)
6734 {
6735 struct ppc_link_hash_entry *h = htab->tls_get_addr_fd;
6736
6737 while (h->elf.root.type == bfd_link_hash_indirect
6738 || h->elf.root.type == bfd_link_hash_warning)
6739 h = (struct ppc_link_hash_entry *) h->elf.root.u.i.link;
6740
6741 htab->tls_get_addr_fd = h;
a48ebf4d
AM
6742 }
6743
e1918d23 6744 return _bfd_elf_tls_setup (obfd, info);
951fd09b 6745}
411e1bfb 6746
951fd09b
AM
6747/* Run through all the TLS relocs looking for optimization
6748 opportunities. The linker has been hacked (see ppc64elf.em) to do
6749 a preliminary section layout so that we know the TLS segment
6750 offsets. We can't optimize earlier because some optimizations need
6751 to know the tp offset, and we need to optimize before allocating
6752 dynamic relocations. */
6753
6754bfd_boolean
4ce794b7 6755ppc64_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
951fd09b
AM
6756{
6757 bfd *ibfd;
6758 asection *sec;
6759 struct ppc_link_hash_table *htab;
6760
1049f94e 6761 if (info->relocatable || info->shared)
411e1bfb
AM
6762 return TRUE;
6763
951fd09b 6764 htab = ppc_hash_table (info);
411e1bfb
AM
6765 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
6766 {
6767 Elf_Internal_Sym *locsyms = NULL;
30038c59
AM
6768 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
6769 unsigned char *toc_ref = NULL;
411e1bfb 6770
30038c59
AM
6771 /* Look at all the sections for this file, with TOC last. */
6772 for (sec = (ibfd->sections == toc && toc && toc->next ? toc->next
6773 : ibfd->sections);
6774 sec != NULL;
6775 sec = (sec == toc ? NULL
6776 : sec->next == NULL ? toc
6777 : sec->next == toc && toc->next ? toc->next
6778 : sec->next))
411e1bfb
AM
6779 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
6780 {
6781 Elf_Internal_Rela *relstart, *rel, *relend;
6782 int expecting_tls_get_addr;
30038c59 6783 long toc_ref_index = 0;
411e1bfb
AM
6784
6785 /* Read the relocations. */
4ce794b7 6786 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
45d6a902 6787 info->keep_memory);
411e1bfb
AM
6788 if (relstart == NULL)
6789 return FALSE;
6790
6791 expecting_tls_get_addr = 0;
6792 relend = relstart + sec->reloc_count;
6793 for (rel = relstart; rel < relend; rel++)
6794 {
6795 enum elf_ppc64_reloc_type r_type;
6796 unsigned long r_symndx;
6797 struct elf_link_hash_entry *h;
6798 Elf_Internal_Sym *sym;
6799 asection *sym_sec;
e7b938ca
AM
6800 char *tls_mask;
6801 char tls_set, tls_clear, tls_type = 0;
411e1bfb 6802 bfd_vma value;
951fd09b 6803 bfd_boolean ok_tprel, is_local;
411e1bfb
AM
6804
6805 r_symndx = ELF64_R_SYM (rel->r_info);
e7b938ca 6806 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
411e1bfb
AM
6807 r_symndx, ibfd))
6808 {
6809 err_free_rel:
6810 if (elf_section_data (sec)->relocs != relstart)
6811 free (relstart);
30038c59
AM
6812 if (toc_ref != NULL)
6813 free (toc_ref);
411e1bfb
AM
6814 if (locsyms != NULL
6815 && (elf_tdata (ibfd)->symtab_hdr.contents
6816 != (unsigned char *) locsyms))
6817 free (locsyms);
6818 return FALSE;
6819 }
6820
6821 if (h != NULL)
6822 {
6823 if (h->root.type != bfd_link_hash_defined
6824 && h->root.type != bfd_link_hash_defweak)
6825 continue;
6826 value = h->root.u.def.value;
6827 }
6828 else
4025353c
AM
6829 /* Symbols referenced by TLS relocs must be of type
6830 STT_TLS. So no need for .opd local sym adjust. */
6831 value = sym->st_value;
951fd09b 6832
411e1bfb 6833 ok_tprel = FALSE;
951fd09b
AM
6834 is_local = FALSE;
6835 if (h == NULL
f5385ebf 6836 || !h->def_dynamic)
411e1bfb 6837 {
951fd09b 6838 is_local = TRUE;
411e1bfb
AM
6839 value += sym_sec->output_offset;
6840 value += sym_sec->output_section->vma;
e1918d23 6841 value -= htab->elf.tls_sec->vma;
411e1bfb
AM
6842 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
6843 < (bfd_vma) 1 << 32);
6844 }
6845
4ce794b7 6846 r_type = ELF64_R_TYPE (rel->r_info);
411e1bfb
AM
6847 switch (r_type)
6848 {
6849 case R_PPC64_GOT_TLSLD16:
6850 case R_PPC64_GOT_TLSLD16_LO:
6851 case R_PPC64_GOT_TLSLD16_HI:
6852 case R_PPC64_GOT_TLSLD16_HA:
951fd09b
AM
6853 /* These relocs should never be against a symbol
6854 defined in a shared lib. Leave them alone if
6855 that turns out to be the case. */
e717da7e 6856 ppc64_tlsld_got (ibfd)->refcount -= 1;
951fd09b
AM
6857 if (!is_local)
6858 continue;
6859
951fd09b
AM
6860 /* LD -> LE */
6861 tls_set = 0;
6862 tls_clear = TLS_LD;
e7b938ca 6863 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
6864 expecting_tls_get_addr = 1;
6865 break;
6866
6867 case R_PPC64_GOT_TLSGD16:
6868 case R_PPC64_GOT_TLSGD16_LO:
6869 case R_PPC64_GOT_TLSGD16_HI:
6870 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 6871 if (ok_tprel)
411e1bfb
AM
6872 /* GD -> LE */
6873 tls_set = 0;
6874 else
6875 /* GD -> IE */
951fd09b
AM
6876 tls_set = TLS_TLS | TLS_TPRELGD;
6877 tls_clear = TLS_GD;
e7b938ca 6878 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
6879 expecting_tls_get_addr = 1;
6880 break;
6881
6882 case R_PPC64_GOT_TPREL16_DS:
6883 case R_PPC64_GOT_TPREL16_LO_DS:
6884 case R_PPC64_GOT_TPREL16_HI:
6885 case R_PPC64_GOT_TPREL16_HA:
6886 expecting_tls_get_addr = 0;
6887 if (ok_tprel)
6888 {
6889 /* IE -> LE */
6890 tls_set = 0;
6891 tls_clear = TLS_TPREL;
e7b938ca 6892 tls_type = TLS_TLS | TLS_TPREL;
411e1bfb
AM
6893 break;
6894 }
6895 else
6896 continue;
6897
6898 case R_PPC64_REL14:
6899 case R_PPC64_REL14_BRTAKEN:
6900 case R_PPC64_REL14_BRNTAKEN:
6901 case R_PPC64_REL24:
6902 if (h != NULL
8387904d
AM
6903 && (h == &htab->tls_get_addr->elf
6904 || h == &htab->tls_get_addr_fd->elf))
411e1bfb
AM
6905 {
6906 if (!expecting_tls_get_addr
6907 && rel != relstart
6908 && ((ELF64_R_TYPE (rel[-1].r_info)
6909 == R_PPC64_TOC16)
6910 || (ELF64_R_TYPE (rel[-1].r_info)
6911 == R_PPC64_TOC16_LO)))
6912 {
6913 /* Check for toc tls entries. */
6914 char *toc_tls;
951fd09b 6915 int retval;
411e1bfb 6916
0d4792f7 6917 retval = get_tls_mask (&toc_tls, NULL, &locsyms,
951fd09b
AM
6918 rel - 1, ibfd);
6919 if (retval == 0)
411e1bfb 6920 goto err_free_rel;
30038c59
AM
6921 if (retval > 1 && toc_tls != NULL)
6922 {
6923 expecting_tls_get_addr = 1;
6924 if (toc_ref != NULL)
6925 toc_ref[toc_ref_index] = 1;
6926 }
411e1bfb
AM
6927 }
6928
6929 if (expecting_tls_get_addr)
6930 {
6931 struct plt_entry *ent;
6932 for (ent = h->plt.plist; ent; ent = ent->next)
6933 if (ent->addend == 0)
6934 {
6935 if (ent->plt.refcount > 0)
6936 ent->plt.refcount -= 1;
6937 break;
6938 }
6939 }
6940 }
6941 expecting_tls_get_addr = 0;
6942 continue;
6943
30038c59
AM
6944 case R_PPC64_TOC16:
6945 case R_PPC64_TOC16_LO:
6946 case R_PPC64_TLS:
6947 expecting_tls_get_addr = 0;
6948 if (sym_sec == toc && toc != NULL)
6949 {
6950 /* Mark this toc entry as referenced by a TLS
6951 code sequence. We can do that now in the
6952 case of R_PPC64_TLS, and after checking for
6953 tls_get_addr for the TOC16 relocs. */
6954 if (toc_ref == NULL)
6955 {
6956 toc_ref = bfd_zmalloc (toc->size / 8);
6957 if (toc_ref == NULL)
6958 goto err_free_rel;
6959 }
6960 if (h != NULL)
6961 value = h->root.u.def.value;
6962 else
6963 value = sym->st_value;
6964 value += rel->r_addend;
6965 BFD_ASSERT (value < toc->size && value % 8 == 0);
6966 toc_ref_index = value / 8;
6967 if (r_type == R_PPC64_TLS)
6968 toc_ref[toc_ref_index] = 1;
6969 }
6970 continue;
6971
411e1bfb
AM
6972 case R_PPC64_TPREL64:
6973 expecting_tls_get_addr = 0;
30038c59
AM
6974 if (sec != toc
6975 || toc_ref == NULL
6976 || !toc_ref[rel->r_offset / 8])
6977 continue;
411e1bfb
AM
6978 if (ok_tprel)
6979 {
6980 /* IE -> LE */
6981 tls_set = TLS_EXPLICIT;
6982 tls_clear = TLS_TPREL;
6983 break;
6984 }
6985 else
6986 continue;
6987
6988 case R_PPC64_DTPMOD64:
6989 expecting_tls_get_addr = 0;
30038c59
AM
6990 if (sec != toc
6991 || toc_ref == NULL
6992 || !toc_ref[rel->r_offset / 8])
6993 continue;
951fd09b
AM
6994 if (rel + 1 < relend
6995 && (rel[1].r_info
6996 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
6997 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 6998 {
951fd09b 6999 if (ok_tprel)
411e1bfb 7000 /* GD -> LE */
951fd09b 7001 tls_set = TLS_EXPLICIT | TLS_GD;
411e1bfb
AM
7002 else
7003 /* GD -> IE */
951fd09b
AM
7004 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
7005 tls_clear = TLS_GD;
411e1bfb
AM
7006 }
7007 else
7008 {
951fd09b
AM
7009 if (!is_local)
7010 continue;
7011
7012 /* LD -> LE */
7013 tls_set = TLS_EXPLICIT;
7014 tls_clear = TLS_LD;
411e1bfb
AM
7015 }
7016 break;
7017
7018 default:
7019 expecting_tls_get_addr = 0;
7020 continue;
7021 }
7022
7023 if ((tls_set & TLS_EXPLICIT) == 0)
7024 {
7025 struct got_entry *ent;
7026
7027 /* Adjust got entry for this reloc. */
7028 if (h != NULL)
7029 ent = h->got.glist;
7030 else
7031 ent = elf_local_got_ents (ibfd)[r_symndx];
7032
7033 for (; ent != NULL; ent = ent->next)
7034 if (ent->addend == rel->r_addend
e717da7e 7035 && ent->owner == ibfd
e7b938ca 7036 && ent->tls_type == tls_type)
411e1bfb
AM
7037 break;
7038 if (ent == NULL)
7039 abort ();
7040
7041 if (tls_set == 0)
7042 {
7043 /* We managed to get rid of a got entry. */
7044 if (ent->got.refcount > 0)
7045 ent->got.refcount -= 1;
7046 }
411e1bfb 7047 }
8c1d1bb8 7048 else
411e1bfb 7049 {
8c1d1bb8
AM
7050 /* If we got rid of a DTPMOD/DTPREL reloc pair then
7051 we'll lose one or two dyn relocs. */
7052 if (!dec_dynrel_count (rel->r_info, sec, info,
7053 NULL, h, sym_sec))
7054 return FALSE;
7055
7056 if (tls_set == (TLS_EXPLICIT | TLS_GD))
7057 {
7058 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
7059 NULL, h, sym_sec))
7060 return FALSE;
7061 }
411e1bfb
AM
7062 }
7063
e7b938ca
AM
7064 *tls_mask |= tls_set;
7065 *tls_mask &= ~tls_clear;
411e1bfb
AM
7066 }
7067
7068 if (elf_section_data (sec)->relocs != relstart)
7069 free (relstart);
7070 }
7071
30038c59
AM
7072 if (toc_ref != NULL)
7073 free (toc_ref);
7074
411e1bfb
AM
7075 if (locsyms != NULL
7076 && (elf_tdata (ibfd)->symtab_hdr.contents
7077 != (unsigned char *) locsyms))
6cdc0ccc
AM
7078 {
7079 if (!info->keep_memory)
411e1bfb 7080 free (locsyms);
6cdc0ccc 7081 else
411e1bfb 7082 elf_tdata (ibfd)->symtab_hdr.contents = (unsigned char *) locsyms;
6cdc0ccc 7083 }
1e2f5b6e 7084 }
b34976b6 7085 return TRUE;
1e2f5b6e 7086}
b34976b6 7087
c5614fa4
AM
7088/* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
7089 the values of any global symbols in a toc section that has been
7090 edited. Globals in toc sections should be a rarity, so this function
7091 sets a flag if any are found in toc sections other than the one just
7092 edited, so that futher hash table traversals can be avoided. */
7093
7094struct adjust_toc_info
7095{
7096 asection *toc;
7097 unsigned long *skip;
7098 bfd_boolean global_toc_syms;
7099};
7100
7101static bfd_boolean
7102adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
7103{
7104 struct ppc_link_hash_entry *eh;
7105 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
7106
7107 if (h->root.type == bfd_link_hash_indirect)
7108 return TRUE;
7109
7110 if (h->root.type == bfd_link_hash_warning)
7111 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7112
7113 if (h->root.type != bfd_link_hash_defined
7114 && h->root.type != bfd_link_hash_defweak)
7115 return TRUE;
7116
7117 eh = (struct ppc_link_hash_entry *) h;
7118 if (eh->adjust_done)
7119 return TRUE;
7120
7121 if (eh->elf.root.u.def.section == toc_inf->toc)
7122 {
7123 unsigned long skip = toc_inf->skip[eh->elf.root.u.def.value >> 3];
7124 if (skip != (unsigned long) -1)
7125 eh->elf.root.u.def.value -= skip;
7126 else
7127 {
7128 (*_bfd_error_handler)
7129 (_("%s defined in removed toc entry"), eh->elf.root.root.string);
7130 eh->elf.root.u.def.section = &bfd_abs_section;
7131 eh->elf.root.u.def.value = 0;
7132 }
7133 eh->adjust_done = 1;
7134 }
7135 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
7136 toc_inf->global_toc_syms = TRUE;
7137
7138 return TRUE;
7139}
7140
7141/* Examine all relocs referencing .toc sections in order to remove
7142 unused .toc entries. */
7143
7144bfd_boolean
7145ppc64_elf_edit_toc (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
7146{
7147 bfd *ibfd;
7148 struct adjust_toc_info toc_inf;
7149
7150 toc_inf.global_toc_syms = TRUE;
7151 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7152 {
7153 asection *toc, *sec;
7154 Elf_Internal_Shdr *symtab_hdr;
7155 Elf_Internal_Sym *local_syms;
7156 struct elf_link_hash_entry **sym_hashes;
92b7a70f 7157 Elf_Internal_Rela *relstart, *rel;
c5614fa4
AM
7158 unsigned long *skip, *drop;
7159 unsigned char *used;
7160 unsigned char *keep, last, some_unused;
7161
7162 toc = bfd_get_section_by_name (ibfd, ".toc");
7163 if (toc == NULL
92b7a70f 7164 || toc->size == 0
c5614fa4
AM
7165 || toc->sec_info_type == ELF_INFO_TYPE_JUST_SYMS
7166 || elf_discarded_section (toc))
7167 continue;
7168
7169 local_syms = NULL;
7170 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7171 sym_hashes = elf_sym_hashes (ibfd);
7172
7173 /* Look at sections dropped from the final link. */
7174 skip = NULL;
7175 relstart = NULL;
7176 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7177 {
7178 if (sec->reloc_count == 0
7179 || !elf_discarded_section (sec)
7180 || get_opd_info (sec)
7181 || (sec->flags & SEC_ALLOC) == 0
7182 || (sec->flags & SEC_DEBUGGING) != 0)
7183 continue;
7184
7185 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
7186 if (relstart == NULL)
7187 goto error_ret;
7188
7189 /* Run through the relocs to see which toc entries might be
7190 unused. */
7191 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7192 {
7193 enum elf_ppc64_reloc_type r_type;
7194 unsigned long r_symndx;
7195 asection *sym_sec;
7196 struct elf_link_hash_entry *h;
7197 Elf_Internal_Sym *sym;
7198 bfd_vma val;
7199
7200 r_type = ELF64_R_TYPE (rel->r_info);
7201 switch (r_type)
7202 {
7203 default:
7204 continue;
7205
7206 case R_PPC64_TOC16:
7207 case R_PPC64_TOC16_LO:
7208 case R_PPC64_TOC16_HI:
7209 case R_PPC64_TOC16_HA:
7210 case R_PPC64_TOC16_DS:
7211 case R_PPC64_TOC16_LO_DS:
7212 break;
7213 }
7214
7215 r_symndx = ELF64_R_SYM (rel->r_info);
7216 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7217 r_symndx, ibfd))
7218 goto error_ret;
7219
7220 if (sym_sec != toc)
7221 continue;
7222
7223 if (h != NULL)
7224 val = h->root.u.def.value;
7225 else
7226 val = sym->st_value;
7227 val += rel->r_addend;
7228
7229 if (val >= toc->size)
7230 continue;
7231
7232 /* Anything in the toc ought to be aligned to 8 bytes.
7233 If not, don't mark as unused. */
7234 if (val & 7)
7235 continue;
7236
7237 if (skip == NULL)
7238 {
7239 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 7) / 8);
7240 if (skip == NULL)
7241 goto error_ret;
7242 }
7243
7244 skip[val >> 3] = 1;
7245 }
7246
7247 if (elf_section_data (sec)->relocs != relstart)
7248 free (relstart);
7249 }
7250
7251 if (skip == NULL)
7252 continue;
7253
7254 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
7255 if (used == NULL)
7256 {
7257 error_ret:
7258 if (local_syms != NULL
7259 && symtab_hdr->contents != (unsigned char *) local_syms)
7260 free (local_syms);
7261 if (sec != NULL
7262 && relstart != NULL
7263 && elf_section_data (sec)->relocs != relstart)
7264 free (relstart);
7265 if (skip != NULL)
7266 free (skip);
7267 return FALSE;
7268 }
7269
30038c59
AM
7270 /* Now check all kept sections that might reference the toc.
7271 Check the toc itself last. */
7272 for (sec = (ibfd->sections == toc && toc->next ? toc->next
7273 : ibfd->sections);
c5614fa4 7274 sec != NULL;
c5614fa4 7275 sec = (sec == toc ? NULL
c5614fa4 7276 : sec->next == NULL ? toc
30038c59 7277 : sec->next == toc && toc->next ? toc->next
c5614fa4
AM
7278 : sec->next))
7279 {
7280 int repeat;
7281
7282 if (sec->reloc_count == 0
7283 || elf_discarded_section (sec)
7284 || get_opd_info (sec)
7285 || (sec->flags & SEC_ALLOC) == 0
7286 || (sec->flags & SEC_DEBUGGING) != 0)
7287 continue;
7288
7289 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, TRUE);
7290 if (relstart == NULL)
7291 goto error_ret;
7292
7293 /* Mark toc entries referenced as used. */
7294 repeat = 0;
7295 do
7296 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7297 {
7298 enum elf_ppc64_reloc_type r_type;
7299 unsigned long r_symndx;
7300 asection *sym_sec;
7301 struct elf_link_hash_entry *h;
7302 Elf_Internal_Sym *sym;
7303 bfd_vma val;
7304
7305 r_type = ELF64_R_TYPE (rel->r_info);
7306 switch (r_type)
7307 {
7308 case R_PPC64_TOC16:
7309 case R_PPC64_TOC16_LO:
7310 case R_PPC64_TOC16_HI:
7311 case R_PPC64_TOC16_HA:
7312 case R_PPC64_TOC16_DS:
7313 case R_PPC64_TOC16_LO_DS:
7314 /* In case we're taking addresses of toc entries. */
7315 case R_PPC64_ADDR64:
7316 break;
7317
7318 default:
7319 continue;
7320 }
7321
7322 r_symndx = ELF64_R_SYM (rel->r_info);
7323 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7324 r_symndx, ibfd))
7325 {
7326 free (used);
7327 goto error_ret;
7328 }
7329
7330 if (sym_sec != toc)
7331 continue;
7332
7333 if (h != NULL)
7334 val = h->root.u.def.value;
7335 else
7336 val = sym->st_value;
7337 val += rel->r_addend;
7338
7339 if (val >= toc->size)
7340 continue;
7341
7342 /* For the toc section, we only mark as used if
7343 this entry itself isn't unused. */
7344 if (sec == toc
7345 && !used[val >> 3]
7346 && (used[rel->r_offset >> 3]
7347 || !skip[rel->r_offset >> 3]))
7348 /* Do all the relocs again, to catch reference
7349 chains. */
7350 repeat = 1;
7351
7352 used[val >> 3] = 1;
7353 }
7354 while (repeat);
7355 }
7356
7357 /* Merge the used and skip arrays. Assume that TOC
7358 doublewords not appearing as either used or unused belong
7359 to to an entry more than one doubleword in size. */
7360 for (drop = skip, keep = used, last = 0, some_unused = 0;
7361 drop < skip + (toc->size + 7) / 8;
7362 ++drop, ++keep)
7363 {
7364 if (*keep)
7365 {
7366 *drop = 0;
7367 last = 0;
7368 }
7369 else if (*drop)
7370 {
7371 some_unused = 1;
7372 last = 1;
7373 }
7374 else
7375 *drop = last;
7376 }
7377
7378 free (used);
7379
7380 if (some_unused)
7381 {
7382 bfd_byte *contents, *src;
7383 unsigned long off;
7384
7385 /* Shuffle the toc contents, and at the same time convert the
7386 skip array from booleans into offsets. */
7387 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
7388 goto error_ret;
7389
7390 elf_section_data (toc)->this_hdr.contents = contents;
7391
7392 for (src = contents, off = 0, drop = skip;
7393 src < contents + toc->size;
7394 src += 8, ++drop)
7395 {
7396 if (*drop)
7397 {
7398 *drop = (unsigned long) -1;
7399 off += 8;
7400 }
7401 else if (off != 0)
7402 {
7403 *drop = off;
7404 memcpy (src - off, src, 8);
7405 }
7406 }
7407 toc->rawsize = toc->size;
7408 toc->size = src - contents - off;
7409
92b7a70f
AM
7410 if (toc->reloc_count != 0)
7411 {
7412 Elf_Internal_Rela *wrel;
7413 bfd_size_type sz;
c5614fa4 7414
92b7a70f
AM
7415 /* Read toc relocs. */
7416 relstart = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
7417 TRUE);
7418 if (relstart == NULL)
7419 goto error_ret;
7420
7421 /* Remove unused toc relocs, and adjust those we keep. */
7422 wrel = relstart;
7423 for (rel = relstart; rel < relstart + toc->reloc_count; ++rel)
7424 if (skip[rel->r_offset >> 3] != (unsigned long) -1)
7425 {
7426 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
7427 wrel->r_info = rel->r_info;
7428 wrel->r_addend = rel->r_addend;
7429 ++wrel;
7430 }
8c1d1bb8
AM
7431 else if (!dec_dynrel_count (rel->r_info, toc, info,
7432 &local_syms, NULL, NULL))
7433 goto error_ret;
35090471 7434
92b7a70f
AM
7435 toc->reloc_count = wrel - relstart;
7436 sz = elf_section_data (toc)->rel_hdr.sh_entsize;
7437 elf_section_data (toc)->rel_hdr.sh_size = toc->reloc_count * sz;
7438 BFD_ASSERT (elf_section_data (toc)->rel_hdr2 == NULL);
7439 }
c5614fa4
AM
7440
7441 /* Adjust addends for relocs against the toc section sym. */
7442 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7443 {
7444 if (sec->reloc_count == 0
7445 || elf_discarded_section (sec))
7446 continue;
7447
7448 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7449 TRUE);
7450 if (relstart == NULL)
7451 goto error_ret;
7452
7453 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
7454 {
7455 enum elf_ppc64_reloc_type r_type;
7456 unsigned long r_symndx;
7457 asection *sym_sec;
7458 struct elf_link_hash_entry *h;
7459 Elf_Internal_Sym *sym;
7460
7461 r_type = ELF64_R_TYPE (rel->r_info);
7462 switch (r_type)
7463 {
7464 default:
7465 continue;
7466
7467 case R_PPC64_TOC16:
7468 case R_PPC64_TOC16_LO:
7469 case R_PPC64_TOC16_HI:
7470 case R_PPC64_TOC16_HA:
7471 case R_PPC64_TOC16_DS:
7472 case R_PPC64_TOC16_LO_DS:
7473 case R_PPC64_ADDR64:
7474 break;
7475 }
7476
7477 r_symndx = ELF64_R_SYM (rel->r_info);
7478 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7479 r_symndx, ibfd))
7480 goto error_ret;
7481
7482 if (sym_sec != toc || h != NULL || sym->st_value != 0)
7483 continue;
7484
7485 rel->r_addend -= skip[rel->r_addend >> 3];
7486 }
7487 }
7488
7489 /* We shouldn't have local or global symbols defined in the TOC,
7490 but handle them anyway. */
7491 if (local_syms != NULL)
7492 {
7493 Elf_Internal_Sym *sym;
7494
7495 for (sym = local_syms;
7496 sym < local_syms + symtab_hdr->sh_info;
7497 ++sym)
7498 if (sym->st_shndx != SHN_UNDEF
7499 && (sym->st_shndx < SHN_LORESERVE
7500 || sym->st_shndx > SHN_HIRESERVE)
7501 && sym->st_value != 0
7502 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
7503 {
7504 if (skip[sym->st_value >> 3] != (unsigned long) -1)
7505 sym->st_value -= skip[sym->st_value >> 3];
7506 else
7507 {
7508 (*_bfd_error_handler)
7509 (_("%s defined in removed toc entry"),
26c61ae5
L
7510 bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7511 NULL));
c5614fa4
AM
7512 sym->st_value = 0;
7513 sym->st_shndx = SHN_ABS;
7514 }
7515 symtab_hdr->contents = (unsigned char *) local_syms;
7516 }
7517 }
7518
7519 /* Finally, adjust any global syms defined in the toc. */
7520 if (toc_inf.global_toc_syms)
7521 {
7522 toc_inf.toc = toc;
7523 toc_inf.skip = skip;
7524 toc_inf.global_toc_syms = FALSE;
7525 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
7526 &toc_inf);
7527 }
7528 }
7529
7530 if (local_syms != NULL
7531 && symtab_hdr->contents != (unsigned char *) local_syms)
7532 {
7533 if (!info->keep_memory)
7534 free (local_syms);
7535 else
7536 symtab_hdr->contents = (unsigned char *) local_syms;
7537 }
7538 free (skip);
7539 }
7540
7541 return TRUE;
7542}
7543
65f38f15
AM
7544/* Allocate space in .plt, .got and associated reloc sections for
7545 dynamic relocs. */
5bd4f169 7546
b34976b6 7547static bfd_boolean
4ce794b7 7548allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
5bd4f169 7549{
65f38f15
AM
7550 struct bfd_link_info *info;
7551 struct ppc_link_hash_table *htab;
5bd4f169 7552 asection *s;
65f38f15
AM
7553 struct ppc_link_hash_entry *eh;
7554 struct ppc_dyn_relocs *p;
411e1bfb 7555 struct got_entry *gent;
5bd4f169 7556
e92d460e 7557 if (h->root.type == bfd_link_hash_indirect)
b34976b6 7558 return TRUE;
5bd4f169 7559
e92d460e
AM
7560 if (h->root.type == bfd_link_hash_warning)
7561 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7562
65f38f15
AM
7563 info = (struct bfd_link_info *) inf;
7564 htab = ppc_hash_table (info);
5bd4f169 7565
65f38f15 7566 if (htab->elf.dynamic_sections_created
411e1bfb 7567 && h->dynindx != -1
9c7a29a3 7568 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
5bd4f169 7569 {
411e1bfb
AM
7570 struct plt_entry *pent;
7571 bfd_boolean doneone = FALSE;
7572 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7573 if (pent->plt.refcount > 0)
7574 {
411e1bfb
AM
7575 /* If this is the first .plt entry, make room for the special
7576 first entry. */
4ce794b7 7577 s = htab->plt;
eea6121a
AM
7578 if (s->size == 0)
7579 s->size += PLT_INITIAL_ENTRY_SIZE;
411e1bfb 7580
eea6121a 7581 pent->plt.offset = s->size;
411e1bfb
AM
7582
7583 /* Make room for this entry. */
eea6121a 7584 s->size += PLT_ENTRY_SIZE;
411e1bfb
AM
7585
7586 /* Make room for the .glink code. */
4ce794b7 7587 s = htab->glink;
eea6121a
AM
7588 if (s->size == 0)
7589 s->size += GLINK_CALL_STUB_SIZE;
411e1bfb 7590 /* We need bigger stubs past index 32767. */
eea6121a
AM
7591 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
7592 s->size += 4;
7593 s->size += 2*4;
411e1bfb
AM
7594
7595 /* We also need to make an entry in the .rela.plt section. */
4ce794b7 7596 s = htab->relplt;
eea6121a 7597 s->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
7598 doneone = TRUE;
7599 }
7600 else
7601 pent->plt.offset = (bfd_vma) -1;
7602 if (!doneone)
65f38f15 7603 {
411e1bfb 7604 h->plt.plist = NULL;
f5385ebf 7605 h->needs_plt = 0;
65f38f15
AM
7606 }
7607 }
7608 else
7609 {
411e1bfb 7610 h->plt.plist = NULL;
f5385ebf 7611 h->needs_plt = 0;
65f38f15
AM
7612 }
7613
951fd09b
AM
7614 eh = (struct ppc_link_hash_entry *) h;
7615 /* Run through the TLS GD got entries first if we're changing them
7616 to TPREL. */
e7b938ca 7617 if ((eh->tls_mask & TLS_TPRELGD) != 0)
951fd09b
AM
7618 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7619 if (gent->got.refcount > 0
7620 && (gent->tls_type & TLS_GD) != 0)
7621 {
7622 /* This was a GD entry that has been converted to TPREL. If
7623 there happens to be a TPREL entry we can use that one. */
7624 struct got_entry *ent;
7625 for (ent = h->got.glist; ent != NULL; ent = ent->next)
7626 if (ent->got.refcount > 0
7627 && (ent->tls_type & TLS_TPREL) != 0
e717da7e
AM
7628 && ent->addend == gent->addend
7629 && ent->owner == gent->owner)
951fd09b
AM
7630 {
7631 gent->got.refcount = 0;
7632 break;
7633 }
7634
7635 /* If not, then we'll be using our own TPREL entry. */
7636 if (gent->got.refcount != 0)
7637 gent->tls_type = TLS_TLS | TLS_TPREL;
7638 }
7639
411e1bfb
AM
7640 for (gent = h->got.glist; gent != NULL; gent = gent->next)
7641 if (gent->got.refcount > 0)
7642 {
951fd09b
AM
7643 bfd_boolean dyn;
7644
411e1bfb 7645 /* Make sure this symbol is output as a dynamic symbol.
951fd09b
AM
7646 Undefined weak syms won't yet be marked as dynamic,
7647 nor will all TLS symbols. */
411e1bfb 7648 if (h->dynindx == -1
f5385ebf 7649 && !h->forced_local)
411e1bfb 7650 {
c152c796 7651 if (! bfd_elf_link_record_dynamic_symbol (info, h))
411e1bfb
AM
7652 return FALSE;
7653 }
65f38f15 7654
d881513a 7655 if ((gent->tls_type & TLS_LD) != 0
f5385ebf 7656 && !h->def_dynamic)
411e1bfb 7657 {
e717da7e 7658 gent->got.offset = ppc64_tlsld_got (gent->owner)->offset;
951fd09b 7659 continue;
411e1bfb 7660 }
951fd09b 7661
e717da7e 7662 s = ppc64_elf_tdata (gent->owner)->got;
eea6121a
AM
7663 gent->got.offset = s->size;
7664 s->size
d881513a 7665 += (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)) ? 16 : 8;
951fd09b 7666 dyn = htab->elf.dynamic_sections_created;
4e795f50
AM
7667 if ((info->shared
7668 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
7669 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
7670 || h->root.type != bfd_link_hash_undefweak))
eea6121a 7671 ppc64_elf_tdata (gent->owner)->relgot->size
e7b938ca 7672 += (gent->tls_type & eh->tls_mask & TLS_GD
951fd09b
AM
7673 ? 2 * sizeof (Elf64_External_Rela)
7674 : sizeof (Elf64_External_Rela));
411e1bfb
AM
7675 }
7676 else
7677 gent->got.offset = (bfd_vma) -1;
65f38f15 7678
65f38f15 7679 if (eh->dyn_relocs == NULL)
b34976b6 7680 return TRUE;
65f38f15
AM
7681
7682 /* In the shared -Bsymbolic case, discard space allocated for
7683 dynamic pc-relative relocs against symbols which turn out to be
7684 defined in regular objects. For the normal shared case, discard
7685 space for relocs that have become local due to symbol visibility
7686 changes. */
7687
7688 if (info->shared)
7689 {
9c7a29a3
AM
7690 /* Relocs that use pc_count are those that appear on a call insn,
7691 or certain REL relocs (see MUST_BE_DYN_RELOC) that can be
7692 generated via assembly. We want calls to protected symbols to
7693 resolve directly to the function rather than going via the plt.
7694 If people want function pointer comparisons to work as expected
7695 then they should avoid writing weird assembly. */
09695f56 7696 if (SYMBOL_CALLS_LOCAL (info, h))
65f38f15
AM
7697 {
7698 struct ppc_dyn_relocs **pp;
7699
7700 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
5bd4f169 7701 {
65f38f15
AM
7702 p->count -= p->pc_count;
7703 p->pc_count = 0;
7704 if (p->count == 0)
7705 *pp = p->next;
7706 else
7707 pp = &p->next;
5bd4f169 7708 }
65f38f15 7709 }
4e795f50
AM
7710
7711 /* Also discard relocs on undefined weak syms with non-default
7712 visibility. */
cab87ef9
AM
7713 if (eh->dyn_relocs != NULL
7714 && h->root.type == bfd_link_hash_undefweak)
dfbb6ac9
AM
7715 {
7716 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
7717 eh->dyn_relocs = NULL;
7718
7719 /* Make sure this symbol is output as a dynamic symbol.
7720 Undefined weak syms won't yet be marked as dynamic. */
7721 else if (h->dynindx == -1
7722 && !h->forced_local)
7723 {
7724 if (! bfd_elf_link_record_dynamic_symbol (info, h))
7725 return FALSE;
7726 }
7727 }
65f38f15 7728 }
f4656909 7729 else if (ELIMINATE_COPY_RELOCS)
65f38f15
AM
7730 {
7731 /* For the non-shared case, discard space for relocs against
7732 symbols which turn out to need copy relocs or are not
7733 dynamic. */
7734
f5385ebf
AM
7735 if (!h->non_got_ref
7736 && h->def_dynamic
7737 && !h->def_regular)
65f38f15
AM
7738 {
7739 /* Make sure this symbol is output as a dynamic symbol.
7740 Undefined weak syms won't yet be marked as dynamic. */
7741 if (h->dynindx == -1
f5385ebf 7742 && !h->forced_local)
65f38f15 7743 {
c152c796 7744 if (! bfd_elf_link_record_dynamic_symbol (info, h))
b34976b6 7745 return FALSE;
65f38f15
AM
7746 }
7747
7748 /* If that succeeded, we know we'll be keeping all the
7749 relocs. */
7750 if (h->dynindx != -1)
7751 goto keep;
7752 }
7753
7754 eh->dyn_relocs = NULL;
7755
ec338859 7756 keep: ;
65f38f15
AM
7757 }
7758
7759 /* Finally, allocate space. */
7760 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7761 {
7762 asection *sreloc = elf_section_data (p->sec)->sreloc;
eea6121a 7763 sreloc->size += p->count * sizeof (Elf64_External_Rela);
65f38f15
AM
7764 }
7765
b34976b6 7766 return TRUE;
65f38f15
AM
7767}
7768
7769/* Find any dynamic relocs that apply to read-only sections. */
7770
b34976b6 7771static bfd_boolean
4ce794b7 7772readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
65f38f15
AM
7773{
7774 struct ppc_link_hash_entry *eh;
7775 struct ppc_dyn_relocs *p;
7776
e92d460e
AM
7777 if (h->root.type == bfd_link_hash_warning)
7778 h = (struct elf_link_hash_entry *) h->root.u.i.link;
7779
65f38f15
AM
7780 eh = (struct ppc_link_hash_entry *) h;
7781 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7782 {
7783 asection *s = p->sec->output_section;
7784
7785 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7786 {
4ce794b7 7787 struct bfd_link_info *info = inf;
65f38f15
AM
7788
7789 info->flags |= DF_TEXTREL;
7790
7791 /* Not an error, just cut short the traversal. */
b34976b6 7792 return FALSE;
65f38f15
AM
7793 }
7794 }
b34976b6 7795 return TRUE;
65f38f15
AM
7796}
7797
7798/* Set the sizes of the dynamic sections. */
7799
b34976b6 7800static bfd_boolean
4ce794b7
AM
7801ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
7802 struct bfd_link_info *info)
65f38f15
AM
7803{
7804 struct ppc_link_hash_table *htab;
7805 bfd *dynobj;
7806 asection *s;
b34976b6 7807 bfd_boolean relocs;
65f38f15
AM
7808 bfd *ibfd;
7809
7810 htab = ppc_hash_table (info);
7811 dynobj = htab->elf.dynobj;
7812 if (dynobj == NULL)
7813 abort ();
7814
7815 if (htab->elf.dynamic_sections_created)
7816 {
7817 /* Set the contents of the .interp section to the interpreter. */
36af4a4e 7818 if (info->executable)
65f38f15
AM
7819 {
7820 s = bfd_get_section_by_name (dynobj, ".interp");
7821 if (s == NULL)
7822 abort ();
eea6121a 7823 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
65f38f15
AM
7824 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
7825 }
7826 }
7827
7828 /* Set up .got offsets for local syms, and space for local dynamic
7829 relocs. */
7830 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7831 {
411e1bfb
AM
7832 struct got_entry **lgot_ents;
7833 struct got_entry **end_lgot_ents;
e7b938ca 7834 char *lgot_masks;
65f38f15
AM
7835 bfd_size_type locsymcount;
7836 Elf_Internal_Shdr *symtab_hdr;
7837 asection *srel;
7838
ee75fd95 7839 if (!is_ppc64_elf_target (ibfd->xvec))
65f38f15
AM
7840 continue;
7841
e717da7e
AM
7842 if (ppc64_tlsld_got (ibfd)->refcount > 0)
7843 {
7844 s = ppc64_elf_tdata (ibfd)->got;
eea6121a
AM
7845 ppc64_tlsld_got (ibfd)->offset = s->size;
7846 s->size += 16;
e717da7e
AM
7847 if (info->shared)
7848 {
7849 srel = ppc64_elf_tdata (ibfd)->relgot;
eea6121a 7850 srel->size += sizeof (Elf64_External_Rela);
e717da7e
AM
7851 }
7852 }
7853 else
7854 ppc64_tlsld_got (ibfd)->offset = (bfd_vma) -1;
7855
65f38f15
AM
7856 for (s = ibfd->sections; s != NULL; s = s->next)
7857 {
ec338859 7858 struct ppc_dyn_relocs *p;
65f38f15 7859
6edfbbad 7860 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
65f38f15 7861 {
ec338859
AM
7862 if (!bfd_is_abs_section (p->sec)
7863 && bfd_is_abs_section (p->sec->output_section))
7864 {
7865 /* Input section has been discarded, either because
7866 it is a copy of a linkonce section or due to
7867 linker script /DISCARD/, so we'll be discarding
7868 the relocs too. */
7869 }
248866a8 7870 else if (p->count != 0)
ec338859
AM
7871 {
7872 srel = elf_section_data (p->sec)->sreloc;
eea6121a 7873 srel->size += p->count * sizeof (Elf64_External_Rela);
248866a8
AM
7874 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
7875 info->flags |= DF_TEXTREL;
ec338859 7876 }
65f38f15
AM
7877 }
7878 }
7879
411e1bfb
AM
7880 lgot_ents = elf_local_got_ents (ibfd);
7881 if (!lgot_ents)
65f38f15
AM
7882 continue;
7883
7884 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
7885 locsymcount = symtab_hdr->sh_info;
411e1bfb 7886 end_lgot_ents = lgot_ents + locsymcount;
e7b938ca 7887 lgot_masks = (char *) end_lgot_ents;
e717da7e
AM
7888 s = ppc64_elf_tdata (ibfd)->got;
7889 srel = ppc64_elf_tdata (ibfd)->relgot;
e7b938ca 7890 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
65f38f15 7891 {
411e1bfb
AM
7892 struct got_entry *ent;
7893
7894 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
7895 if (ent->got.refcount > 0)
7896 {
e7b938ca 7897 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
411e1bfb 7898 {
e717da7e 7899 if (ppc64_tlsld_got (ibfd)->offset == (bfd_vma) -1)
411e1bfb 7900 {
eea6121a
AM
7901 ppc64_tlsld_got (ibfd)->offset = s->size;
7902 s->size += 16;
411e1bfb 7903 if (info->shared)
eea6121a 7904 srel->size += sizeof (Elf64_External_Rela);
411e1bfb 7905 }
e717da7e 7906 ent->got.offset = ppc64_tlsld_got (ibfd)->offset;
411e1bfb
AM
7907 }
7908 else
7909 {
eea6121a 7910 ent->got.offset = s->size;
e7b938ca 7911 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
411e1bfb 7912 {
eea6121a 7913 s->size += 16;
411e1bfb 7914 if (info->shared)
eea6121a 7915 srel->size += 2 * sizeof (Elf64_External_Rela);
411e1bfb
AM
7916 }
7917 else
7918 {
eea6121a 7919 s->size += 8;
411e1bfb 7920 if (info->shared)
eea6121a 7921 srel->size += sizeof (Elf64_External_Rela);
411e1bfb
AM
7922 }
7923 }
7924 }
7925 else
7926 ent->got.offset = (bfd_vma) -1;
65f38f15
AM
7927 }
7928 }
7929
7930 /* Allocate global sym .plt and .got entries, and space for global
7931 sym dynamic relocs. */
4ce794b7 7932 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
65f38f15
AM
7933
7934 /* We now have determined the sizes of the various dynamic sections.
7935 Allocate memory for them. */
b34976b6 7936 relocs = FALSE;
65f38f15
AM
7937 for (s = dynobj->sections; s != NULL; s = s->next)
7938 {
7939 if ((s->flags & SEC_LINKER_CREATED) == 0)
7940 continue;
7941
4ce794b7 7942 if (s == htab->brlt || s == htab->relbrlt)
721956f4
AM
7943 /* These haven't been allocated yet; don't strip. */
7944 continue;
e717da7e
AM
7945 else if (s == htab->got
7946 || s == htab->plt
c456f082
AM
7947 || s == htab->glink
7948 || s == htab->dynbss)
65f38f15
AM
7949 {
7950 /* Strip this section if we don't need it; see the
7951 comment below. */
5bd4f169 7952 }
0112cd26 7953 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela"))
5bd4f169 7954 {
c456f082 7955 if (s->size != 0)
5bd4f169 7956 {
4ce794b7 7957 if (s != htab->relplt)
b34976b6 7958 relocs = TRUE;
5bd4f169
AM
7959
7960 /* We use the reloc_count field as a counter if we need
7961 to copy relocs into the output file. */
7962 s->reloc_count = 0;
7963 }
7964 }
65f38f15 7965 else
5bd4f169
AM
7966 {
7967 /* It's not one of our sections, so don't allocate space. */
7968 continue;
7969 }
7970
eea6121a 7971 if (s->size == 0)
5bd4f169 7972 {
c456f082
AM
7973 /* If we don't need this section, strip it from the
7974 output file. This is mostly to handle .rela.bss and
7975 .rela.plt. We must create both sections in
7976 create_dynamic_sections, because they must be created
7977 before the linker maps input sections to output
7978 sections. The linker does that before
7979 adjust_dynamic_symbol is called, and it is that
7980 function which decides whether anything needs to go
7981 into these sections. */
8423293d 7982 s->flags |= SEC_EXCLUDE;
5bd4f169
AM
7983 continue;
7984 }
7985
c456f082 7986 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5f333394
AM
7987 continue;
7988
65f38f15
AM
7989 /* Allocate memory for the section contents. We use bfd_zalloc
7990 here in case unused entries are not reclaimed before the
7991 section's contents are written out. This should not happen,
411e1bfb
AM
7992 but this way if it does we get a R_PPC64_NONE reloc in .rela
7993 sections instead of garbage.
7994 We also rely on the section contents being zero when writing
7995 the GOT. */
eea6121a 7996 s->contents = bfd_zalloc (dynobj, s->size);
65f38f15 7997 if (s->contents == NULL)
b34976b6 7998 return FALSE;
5bd4f169
AM
7999 }
8000
e717da7e
AM
8001 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8002 {
ee75fd95 8003 if (!is_ppc64_elf_target (ibfd->xvec))
7b53ace3
AM
8004 continue;
8005
e717da7e
AM
8006 s = ppc64_elf_tdata (ibfd)->got;
8007 if (s != NULL && s != htab->got)
8008 {
eea6121a 8009 if (s->size == 0)
8423293d 8010 s->flags |= SEC_EXCLUDE;
e717da7e
AM
8011 else
8012 {
eea6121a 8013 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
8014 if (s->contents == NULL)
8015 return FALSE;
8016 }
8017 }
8018 s = ppc64_elf_tdata (ibfd)->relgot;
8019 if (s != NULL)
8020 {
eea6121a 8021 if (s->size == 0)
8423293d 8022 s->flags |= SEC_EXCLUDE;
e717da7e
AM
8023 else
8024 {
eea6121a 8025 s->contents = bfd_zalloc (ibfd, s->size);
e717da7e
AM
8026 if (s->contents == NULL)
8027 return FALSE;
8028 relocs = TRUE;
8029 s->reloc_count = 0;
8030 }
8031 }
8032 }
8033
e86ce104 8034 if (htab->elf.dynamic_sections_created)
5bd4f169
AM
8035 {
8036 /* Add some entries to the .dynamic section. We fill in the
8037 values later, in ppc64_elf_finish_dynamic_sections, but we
8038 must add the entries now so that we get the correct size for
8039 the .dynamic section. The DT_DEBUG entry is filled in by the
8040 dynamic linker and used by the debugger. */
dc810e39 8041#define add_dynamic_entry(TAG, VAL) \
5a580b3a 8042 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
dc810e39 8043
36af4a4e 8044 if (info->executable)
5bd4f169 8045 {
dc810e39 8046 if (!add_dynamic_entry (DT_DEBUG, 0))
b34976b6 8047 return FALSE;
5bd4f169
AM
8048 }
8049
eea6121a 8050 if (htab->plt != NULL && htab->plt->size != 0)
5bd4f169 8051 {
dc810e39
AM
8052 if (!add_dynamic_entry (DT_PLTGOT, 0)
8053 || !add_dynamic_entry (DT_PLTRELSZ, 0)
8054 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
5d1634d7
AM
8055 || !add_dynamic_entry (DT_JMPREL, 0)
8056 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
b34976b6 8057 return FALSE;
5bd4f169
AM
8058 }
8059
19397422
AM
8060 if (NO_OPD_RELOCS)
8061 {
8062 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
8063 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
b34976b6 8064 return FALSE;
19397422
AM
8065 }
8066
5bd4f169
AM
8067 if (relocs)
8068 {
dc810e39
AM
8069 if (!add_dynamic_entry (DT_RELA, 0)
8070 || !add_dynamic_entry (DT_RELASZ, 0)
8071 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
b34976b6 8072 return FALSE;
5bd4f169 8073
65f38f15
AM
8074 /* If any dynamic relocs apply to a read-only section,
8075 then we need a DT_TEXTREL entry. */
248866a8 8076 if ((info->flags & DF_TEXTREL) == 0)
4ce794b7 8077 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
5bd4f169 8078
65f38f15 8079 if ((info->flags & DF_TEXTREL) != 0)
5bd4f169 8080 {
65f38f15 8081 if (!add_dynamic_entry (DT_TEXTREL, 0))
b34976b6 8082 return FALSE;
5bd4f169 8083 }
5bd4f169 8084 }
5bd4f169 8085 }
65f38f15 8086#undef add_dynamic_entry
5bd4f169 8087
b34976b6 8088 return TRUE;
5bd4f169
AM
8089}
8090
721956f4 8091/* Determine the type of stub needed, if any, for a call. */
5bd4f169 8092
4ce794b7
AM
8093static inline enum ppc_stub_type
8094ppc_type_of_stub (asection *input_sec,
8095 const Elf_Internal_Rela *rel,
8096 struct ppc_link_hash_entry **hash,
8097 bfd_vma destination)
5bd4f169 8098{
721956f4
AM
8099 struct ppc_link_hash_entry *h = *hash;
8100 bfd_vma location;
8101 bfd_vma branch_offset;
8102 bfd_vma max_branch_offset;
4ce794b7 8103 enum elf_ppc64_reloc_type r_type;
5bd4f169 8104
721956f4
AM
8105 if (h != NULL)
8106 {
7fe2b9a6
AM
8107 struct ppc_link_hash_entry *fdh = h;
8108 if (fdh->oh != NULL
8109 && fdh->oh->is_func_descriptor)
8110 fdh = fdh->oh;
8387904d 8111
7fe2b9a6 8112 if (fdh->elf.dynindx != -1)
5bd4f169 8113 {
411e1bfb 8114 struct plt_entry *ent;
8387904d 8115
7fe2b9a6 8116 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
411e1bfb
AM
8117 if (ent->addend == rel->r_addend
8118 && ent->plt.offset != (bfd_vma) -1)
8119 {
7fe2b9a6 8120 *hash = fdh;
411e1bfb
AM
8121 return ppc_stub_plt_call;
8122 }
5bd4f169
AM
8123 }
8124
7fe2b9a6
AM
8125 /* Here, we know we don't have a plt entry. If we don't have a
8126 either a defined function descriptor or a defined entry symbol
8127 in a regular object file, then it is pointless trying to make
8128 any other type of stub. */
8129 if (!((fdh->elf.root.type == bfd_link_hash_defined
8130 || fdh->elf.root.type == bfd_link_hash_defweak)
8131 && fdh->elf.root.u.def.section->output_section != NULL)
8132 && !((h->elf.root.type == bfd_link_hash_defined
8133 || h->elf.root.type == bfd_link_hash_defweak)
8134 && h->elf.root.u.def.section->output_section != NULL))
721956f4 8135 return ppc_stub_none;
5d1634d7 8136 }
5d1634d7 8137
721956f4
AM
8138 /* Determine where the call point is. */
8139 location = (input_sec->output_offset
8140 + input_sec->output_section->vma
8141 + rel->r_offset);
5d1634d7 8142
721956f4
AM
8143 branch_offset = destination - location;
8144 r_type = ELF64_R_TYPE (rel->r_info);
5d1634d7 8145
721956f4
AM
8146 /* Determine if a long branch stub is needed. */
8147 max_branch_offset = 1 << 25;
4ce794b7 8148 if (r_type != R_PPC64_REL24)
721956f4 8149 max_branch_offset = 1 << 15;
5d1634d7 8150
721956f4
AM
8151 if (branch_offset + max_branch_offset >= 2 * max_branch_offset)
8152 /* We need a stub. Figure out whether a long_branch or plt_branch
8153 is needed later. */
8154 return ppc_stub_long_branch;
5d1634d7 8155
721956f4 8156 return ppc_stub_none;
5d1634d7
AM
8157}
8158
8159/* Build a .plt call stub. */
8160
4ce794b7
AM
8161static inline bfd_byte *
8162build_plt_stub (bfd *obfd, bfd_byte *p, int offset)
5d1634d7
AM
8163{
8164#define PPC_LO(v) ((v) & 0xffff)
8165#define PPC_HI(v) (((v) >> 16) & 0xffff)
8166#define PPC_HA(v) PPC_HI ((v) + 0x8000)
8167
5d1634d7 8168 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
ad8e1ba5 8169 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
5d1634d7
AM
8170 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8171 if (PPC_HA (offset + 8) != PPC_HA (offset))
ad8e1ba5 8172 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
5d1634d7
AM
8173 offset += 8;
8174 bfd_put_32 (obfd, LD_R2_0R12 | PPC_LO (offset), p), p += 4;
8175 if (PPC_HA (offset + 8) != PPC_HA (offset))
ad8e1ba5 8176 bfd_put_32 (obfd, ADDIS_R12_R12 | 1, p), p += 4;
5d1634d7
AM
8177 offset += 8;
8178 bfd_put_32 (obfd, MTCTR_R11, p), p += 4;
8179 bfd_put_32 (obfd, LD_R11_0R12 | PPC_LO (offset), p), p += 4;
8180 bfd_put_32 (obfd, BCTR, p), p += 4;
8181 return p;
8182}
8183
b34976b6 8184static bfd_boolean
4ce794b7 8185ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
5d1634d7 8186{
721956f4
AM
8187 struct ppc_stub_hash_entry *stub_entry;
8188 struct ppc_branch_hash_entry *br_entry;
5d1634d7
AM
8189 struct bfd_link_info *info;
8190 struct ppc_link_hash_table *htab;
721956f4
AM
8191 bfd_byte *loc;
8192 bfd_byte *p;
8193 unsigned int indx;
411e1bfb 8194 struct plt_entry *ent;
ee75fd95 8195 bfd_vma dest, off;
721956f4 8196 int size;
5d1634d7 8197
721956f4
AM
8198 /* Massage our args to the form they really have. */
8199 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
4ce794b7 8200 info = in_arg;
5d1634d7 8201
5d1634d7
AM
8202 htab = ppc_hash_table (info);
8203
721956f4 8204 /* Make a note of the offset within the stubs for this entry. */
eea6121a 8205 stub_entry->stub_offset = stub_entry->stub_sec->size;
97b639ba 8206 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
721956f4 8207
4ce794b7 8208 htab->stub_count[stub_entry->stub_type - 1] += 1;
721956f4 8209 switch (stub_entry->stub_type)
5d1634d7 8210 {
721956f4 8211 case ppc_stub_long_branch:
ad8e1ba5 8212 case ppc_stub_long_branch_r2off:
721956f4 8213 /* Branches are relative. This is where we are going to. */
ee75fd95
AM
8214 off = dest = (stub_entry->target_value
8215 + stub_entry->target_section->output_offset
8216 + stub_entry->target_section->output_section->vma);
5d1634d7 8217
721956f4
AM
8218 /* And this is where we are coming from. */
8219 off -= (stub_entry->stub_offset
97b639ba
AM
8220 + stub_entry->stub_sec->output_offset
8221 + stub_entry->stub_sec->output_section->vma);
e86ce104 8222
ad8e1ba5
AM
8223 if (stub_entry->stub_type != ppc_stub_long_branch_r2off)
8224 size = 4;
8225 else
8226 {
8227 bfd_vma r2off;
8228
8229 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8230 - htab->stub_group[stub_entry->id_sec->id].toc_off);
97b639ba 8231 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 8232 loc += 4;
97b639ba 8233 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
ad8e1ba5 8234 loc += 4;
97b639ba 8235 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
8236 loc += 4;
8237 off -= 12;
8238 size = 16;
8239 }
97b639ba 8240 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
ad8e1ba5 8241
5c3dead3
AM
8242 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8243 {
8244 (*_bfd_error_handler) (_("long branch stub `%s' offset overflow"),
8245 stub_entry->root.string);
8246 htab->stub_error = TRUE;
8247 return FALSE;
8248 }
ee75fd95
AM
8249
8250 if (info->emitrelocations)
8251 {
8252 Elf_Internal_Rela *relocs, *r;
8253 struct bfd_elf_section_data *elfsec_data;
8254
8255 elfsec_data = elf_section_data (stub_entry->stub_sec);
8256 relocs = elfsec_data->relocs;
8257 if (relocs == NULL)
8258 {
8259 bfd_size_type relsize;
8260 relsize = stub_entry->stub_sec->reloc_count * sizeof (*relocs);
8261 relocs = bfd_alloc (htab->stub_bfd, relsize);
8262 if (relocs == NULL)
8263 return FALSE;
8264 elfsec_data->relocs = relocs;
8265 elfsec_data->rel_hdr.sh_size = relsize;
8266 elfsec_data->rel_hdr.sh_entsize = 24;
8267 stub_entry->stub_sec->reloc_count = 0;
8268 }
8269 r = relocs + stub_entry->stub_sec->reloc_count;
8270 stub_entry->stub_sec->reloc_count += 1;
8271 r->r_offset = loc - stub_entry->stub_sec->contents;
8272 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
8273 r->r_addend = dest;
8274 if (stub_entry->h != NULL)
8275 {
8276 struct elf_link_hash_entry **hashes;
8277 unsigned long symndx;
8278 struct ppc_link_hash_entry *h;
8279
8280 hashes = elf_sym_hashes (htab->stub_bfd);
8281 if (hashes == NULL)
8282 {
8283 bfd_size_type hsize;
8284
8285 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
8286 hashes = bfd_zalloc (htab->stub_bfd, hsize);
8287 if (hashes == NULL)
8288 return FALSE;
8289 elf_sym_hashes (htab->stub_bfd) = hashes;
8290 htab->stub_globals = 1;
8291 }
8292 symndx = htab->stub_globals++;
8293 h = stub_entry->h;
8294 hashes[symndx] = &h->elf;
8295 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
8296 if (h->oh != NULL && h->oh->is_func)
8297 h = h->oh;
8298 if (h->elf.root.u.def.section != stub_entry->target_section)
8299 /* H is an opd symbol. The addend must be zero. */
8300 r->r_addend = 0;
8301 else
8302 {
8303 off = (h->elf.root.u.def.value
8304 + h->elf.root.u.def.section->output_offset
8305 + h->elf.root.u.def.section->output_section->vma);
8306 r->r_addend -= off;
8307 }
8308 }
8309 }
721956f4 8310 break;
e86ce104 8311
721956f4 8312 case ppc_stub_plt_branch:
ad8e1ba5 8313 case ppc_stub_plt_branch_r2off:
721956f4
AM
8314 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8315 stub_entry->root.string + 9,
b34976b6 8316 FALSE, FALSE);
721956f4
AM
8317 if (br_entry == NULL)
8318 {
8319 (*_bfd_error_handler) (_("can't find branch stub `%s'"),
5c3dead3 8320 stub_entry->root.string);
b34976b6
AM
8321 htab->stub_error = TRUE;
8322 return FALSE;
721956f4
AM
8323 }
8324
8325 off = (stub_entry->target_value
8326 + stub_entry->target_section->output_offset
8327 + stub_entry->target_section->output_section->vma);
8328
4ce794b7
AM
8329 bfd_put_64 (htab->brlt->owner, off,
8330 htab->brlt->contents + br_entry->offset);
721956f4 8331
ee75fd95 8332 if (htab->relbrlt != NULL)
721956f4
AM
8333 {
8334 /* Create a reloc for the branch lookup table entry. */
8335 Elf_Internal_Rela rela;
ad8e1ba5 8336 bfd_byte *rl;
5d1634d7 8337
721956f4 8338 rela.r_offset = (br_entry->offset
4ce794b7
AM
8339 + htab->brlt->output_offset
8340 + htab->brlt->output_section->vma);
721956f4
AM
8341 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
8342 rela.r_addend = off;
8343
4ce794b7
AM
8344 rl = htab->relbrlt->contents;
8345 rl += htab->relbrlt->reloc_count++ * sizeof (Elf64_External_Rela);
8346 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
721956f4
AM
8347 }
8348
8349 off = (br_entry->offset
4ce794b7
AM
8350 + htab->brlt->output_offset
8351 + htab->brlt->output_section->vma
8352 - elf_gp (htab->brlt->output_section->owner)
ad8e1ba5 8353 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 8354
ad8e1ba5 8355 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
5d1634d7
AM
8356 {
8357 (*_bfd_error_handler)
e86ce104 8358 (_("linkage table error against `%s'"),
721956f4 8359 stub_entry->root.string);
5d1634d7 8360 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
8361 htab->stub_error = TRUE;
8362 return FALSE;
5d1634d7 8363 }
41bd81ab 8364
721956f4 8365 indx = off;
ad8e1ba5
AM
8366 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
8367 {
97b639ba 8368 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
ad8e1ba5 8369 loc += 4;
97b639ba 8370 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
ad8e1ba5
AM
8371 size = 16;
8372 }
8373 else
8374 {
8375 bfd_vma r2off;
8376
8377 r2off = (htab->stub_group[stub_entry->target_section->id].toc_off
8378 - htab->stub_group[stub_entry->id_sec->id].toc_off);
97b639ba 8379 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
ad8e1ba5 8380 loc += 4;
97b639ba 8381 bfd_put_32 (htab->stub_bfd, ADDIS_R12_R2 | PPC_HA (indx), loc);
ad8e1ba5 8382 loc += 4;
97b639ba 8383 bfd_put_32 (htab->stub_bfd, LD_R11_0R12 | PPC_LO (indx), loc);
ad8e1ba5 8384 loc += 4;
97b639ba 8385 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
ad8e1ba5 8386 loc += 4;
97b639ba 8387 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
ad8e1ba5
AM
8388 size = 28;
8389 }
8390 loc += 4;
97b639ba 8391 bfd_put_32 (htab->stub_bfd, MTCTR_R11, loc);
ad8e1ba5 8392 loc += 4;
97b639ba 8393 bfd_put_32 (htab->stub_bfd, BCTR, loc);
721956f4 8394 break;
5d1634d7 8395
721956f4 8396 case ppc_stub_plt_call:
c862ae31
AM
8397 /* Do the best we can for shared libraries built without
8398 exporting ".foo" for each "foo". This can happen when symbol
8399 versioning scripts strip all bar a subset of symbols. */
8387904d
AM
8400 if (stub_entry->h->oh != NULL
8401 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defined
34814b9f 8402 && stub_entry->h->oh->elf.root.type != bfd_link_hash_defweak)
c862ae31
AM
8403 {
8404 /* Point the symbol at the stub. There may be multiple stubs,
8405 we don't really care; The main thing is to make this sym
8f3bab57
AM
8406 defined somewhere. Maybe defining the symbol in the stub
8407 section is a silly idea. If we didn't do this, htab->top_id
8408 could disappear. */
34814b9f
AM
8409 stub_entry->h->oh->elf.root.type = bfd_link_hash_defined;
8410 stub_entry->h->oh->elf.root.u.def.section = stub_entry->stub_sec;
8411 stub_entry->h->oh->elf.root.u.def.value = stub_entry->stub_offset;
c862ae31
AM
8412 }
8413
721956f4 8414 /* Now build the stub. */
411e1bfb
AM
8415 off = (bfd_vma) -1;
8416 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8417 if (ent->addend == stub_entry->addend)
8418 {
8419 off = ent->plt.offset;
8420 break;
8421 }
721956f4
AM
8422 if (off >= (bfd_vma) -2)
8423 abort ();
8424
8425 off &= ~ (bfd_vma) 1;
4ce794b7
AM
8426 off += (htab->plt->output_offset
8427 + htab->plt->output_section->vma
8428 - elf_gp (htab->plt->output_section->owner)
ad8e1ba5 8429 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 8430
ad8e1ba5 8431 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
721956f4
AM
8432 {
8433 (*_bfd_error_handler)
8434 (_("linkage table error against `%s'"),
8435 stub_entry->h->elf.root.root.string);
8436 bfd_set_error (bfd_error_bad_value);
b34976b6
AM
8437 htab->stub_error = TRUE;
8438 return FALSE;
721956f4
AM
8439 }
8440
97b639ba 8441 p = build_plt_stub (htab->stub_bfd, loc, off);
721956f4
AM
8442 size = p - loc;
8443 break;
8444
8445 default:
8446 BFD_FAIL ();
b34976b6 8447 return FALSE;
721956f4
AM
8448 }
8449
eea6121a 8450 stub_entry->stub_sec->size += size;
97b639ba 8451
ee75fd95 8452 if (htab->emit_stub_syms)
97b639ba
AM
8453 {
8454 struct elf_link_hash_entry *h;
ee75fd95
AM
8455 size_t len1, len2;
8456 char *name;
8457 const char *const stub_str[] = { "long_branch",
8458 "long_branch_r2off",
8459 "plt_branch",
8460 "plt_branch_r2off",
8461 "plt_call" };
8462
8463 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
8464 len2 = strlen (stub_entry->root.string);
8465 name = bfd_malloc (len1 + len2 + 2);
8466 if (name == NULL)
8467 return FALSE;
8468 memcpy (name, stub_entry->root.string, 9);
8469 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
8470 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
8471 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
97b639ba
AM
8472 if (h == NULL)
8473 return FALSE;
8474 if (h->root.type == bfd_link_hash_new)
8475 {
8476 h->root.type = bfd_link_hash_defined;
8477 h->root.u.def.section = stub_entry->stub_sec;
8478 h->root.u.def.value = stub_entry->stub_offset;
f5385ebf
AM
8479 h->ref_regular = 1;
8480 h->def_regular = 1;
8481 h->ref_regular_nonweak = 1;
8482 h->forced_local = 1;
8483 h->non_elf = 0;
97b639ba
AM
8484 }
8485 }
8486
b34976b6 8487 return TRUE;
721956f4
AM
8488}
8489
8490/* As above, but don't actually build the stub. Just bump offset so
8491 we know stub section sizes, and select plt_branch stubs where
8492 long_branch stubs won't do. */
8493
b34976b6 8494static bfd_boolean
4ce794b7 8495ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
721956f4
AM
8496{
8497 struct ppc_stub_hash_entry *stub_entry;
63bc6f6c 8498 struct bfd_link_info *info;
721956f4
AM
8499 struct ppc_link_hash_table *htab;
8500 bfd_vma off;
8501 int size;
8502
8503 /* Massage our args to the form they really have. */
8504 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
63bc6f6c
AM
8505 info = in_arg;
8506
8507 htab = ppc_hash_table (info);
721956f4
AM
8508
8509 if (stub_entry->stub_type == ppc_stub_plt_call)
8510 {
411e1bfb 8511 struct plt_entry *ent;
58ac9f71 8512 off = (bfd_vma) -1;
411e1bfb
AM
8513 for (ent = stub_entry->h->elf.plt.plist; ent != NULL; ent = ent->next)
8514 if (ent->addend == stub_entry->addend)
8515 {
8516 off = ent->plt.offset & ~(bfd_vma) 1;
8517 break;
8518 }
58ac9f71 8519 if (off >= (bfd_vma) -2)
411e1bfb 8520 abort ();
4ce794b7
AM
8521 off += (htab->plt->output_offset
8522 + htab->plt->output_section->vma
8523 - elf_gp (htab->plt->output_section->owner)
ad8e1ba5 8524 - htab->stub_group[stub_entry->id_sec->id].toc_off);
721956f4 8525
ad8e1ba5 8526 size = PLT_CALL_STUB_SIZE;
4ce794b7 8527 if (PPC_HA (off + 16) != PPC_HA (off))
721956f4
AM
8528 size += 4;
8529 }
8530 else
8531 {
ad8e1ba5
AM
8532 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
8533 variants. */
721956f4
AM
8534 off = (stub_entry->target_value
8535 + stub_entry->target_section->output_offset
8536 + stub_entry->target_section->output_section->vma);
eea6121a 8537 off -= (stub_entry->stub_sec->size
721956f4
AM
8538 + stub_entry->stub_sec->output_offset
8539 + stub_entry->stub_sec->output_section->vma);
8540
ad8e1ba5
AM
8541 /* Reset the stub type from the plt variant in case we now
8542 can reach with a shorter stub. */
8543 if (stub_entry->stub_type >= ppc_stub_plt_branch)
8544 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
8545
8546 size = 4;
8547 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
8548 {
8549 off -= 12;
8550 size = 16;
8551 }
8552
8553 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
721956f4
AM
8554 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
8555 {
8556 struct ppc_branch_hash_entry *br_entry;
8557
8558 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
8559 stub_entry->root.string + 9,
b34976b6 8560 TRUE, FALSE);
721956f4
AM
8561 if (br_entry == NULL)
8562 {
8563 (*_bfd_error_handler) (_("can't build branch stub `%s'"),
5c3dead3 8564 stub_entry->root.string);
b34976b6
AM
8565 htab->stub_error = TRUE;
8566 return FALSE;
721956f4
AM
8567 }
8568
8569 if (br_entry->iter != htab->stub_iteration)
8570 {
8571 br_entry->iter = htab->stub_iteration;
eea6121a
AM
8572 br_entry->offset = htab->brlt->size;
8573 htab->brlt->size += 8;
63bc6f6c 8574
ee75fd95 8575 if (htab->relbrlt != NULL)
eea6121a 8576 htab->relbrlt->size += sizeof (Elf64_External_Rela);
721956f4 8577 }
ad8e1ba5
AM
8578
8579 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
721956f4 8580 size = 16;
ad8e1ba5
AM
8581 if (stub_entry->stub_type != ppc_stub_plt_branch)
8582 size = 28;
721956f4 8583 }
ee75fd95
AM
8584
8585 if (info->emitrelocations
8586 && (stub_entry->stub_type == ppc_stub_long_branch
8587 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
8588 stub_entry->stub_sec->reloc_count += 1;
721956f4
AM
8589 }
8590
eea6121a 8591 stub_entry->stub_sec->size += size;
b34976b6 8592 return TRUE;
721956f4
AM
8593}
8594
8595/* Set up various things so that we can make a list of input sections
8596 for each output section included in the link. Returns -1 on error,
cedb70c5 8597 0 when no stubs will be needed, and 1 on success. */
721956f4
AM
8598
8599int
4c52953f
AM
8600ppc64_elf_setup_section_lists (bfd *output_bfd,
8601 struct bfd_link_info *info,
8602 int no_multi_toc)
721956f4
AM
8603{
8604 bfd *input_bfd;
734b6cf9 8605 int top_id, top_index, id;
721956f4 8606 asection *section;
734b6cf9 8607 asection **input_list;
721956f4
AM
8608 bfd_size_type amt;
8609 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8610
4c52953f
AM
8611 htab->no_multi_toc = no_multi_toc;
8612
4ce794b7 8613 if (htab->brlt == NULL)
721956f4
AM
8614 return 0;
8615
1e2f5b6e 8616 /* Find the top input section id. */
3d6f9012 8617 for (input_bfd = info->input_bfds, top_id = 3;
721956f4
AM
8618 input_bfd != NULL;
8619 input_bfd = input_bfd->link_next)
8620 {
721956f4
AM
8621 for (section = input_bfd->sections;
8622 section != NULL;
8623 section = section->next)
8624 {
8625 if (top_id < section->id)
8626 top_id = section->id;
8627 }
8628 }
721956f4 8629
8f3bab57 8630 htab->top_id = top_id;
721956f4 8631 amt = sizeof (struct map_stub) * (top_id + 1);
4ce794b7 8632 htab->stub_group = bfd_zmalloc (amt);
721956f4
AM
8633 if (htab->stub_group == NULL)
8634 return -1;
8635
3d6f9012
AM
8636 /* Set toc_off for com, und, abs and ind sections. */
8637 for (id = 0; id < 3; id++)
8638 htab->stub_group[id].toc_off = TOC_BASE_OFF;
721956f4 8639
3d6f9012 8640 elf_gp (output_bfd) = htab->toc_curr = ppc64_elf_toc (output_bfd);
734b6cf9
AM
8641
8642 /* We can't use output_bfd->section_count here to find the top output
8643 section index as some sections may have been removed, and
8423293d 8644 strip_excluded_output_sections doesn't renumber the indices. */
734b6cf9
AM
8645 for (section = output_bfd->sections, top_index = 0;
8646 section != NULL;
8647 section = section->next)
8648 {
8649 if (top_index < section->index)
8650 top_index = section->index;
8651 }
8652
8653 htab->top_index = top_index;
8654 amt = sizeof (asection *) * (top_index + 1);
4ce794b7 8655 input_list = bfd_zmalloc (amt);
734b6cf9
AM
8656 htab->input_list = input_list;
8657 if (input_list == NULL)
8658 return -1;
8659
721956f4
AM
8660 return 1;
8661}
8662
e717da7e
AM
8663/* The linker repeatedly calls this function for each TOC input section
8664 and linker generated GOT section. Group input bfds such that the toc
8665 within a group is less than 64k in size. Will break with cute linker
8666 scripts that play games with dot in the output toc section. */
ad8e1ba5
AM
8667
8668void
4ce794b7 8669ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
ad8e1ba5
AM
8670{
8671 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8672
4c52953f
AM
8673 if (!htab->no_multi_toc)
8674 {
8675 bfd_vma addr = isec->output_offset + isec->output_section->vma;
8676 bfd_vma off = addr - htab->toc_curr;
8677
8678 if (off + isec->size > 0x10000)
8679 htab->toc_curr = addr;
99877b66 8680
4c52953f
AM
8681 elf_gp (isec->owner) = (htab->toc_curr
8682 - elf_gp (isec->output_section->owner)
8683 + TOC_BASE_OFF);
8684 }
ad8e1ba5
AM
8685}
8686
8687/* Called after the last call to the above function. */
8688
8689void
4c52953f 8690ppc64_elf_reinit_toc (bfd *output_bfd, struct bfd_link_info *info)
ad8e1ba5
AM
8691{
8692 struct ppc_link_hash_table *htab = ppc_hash_table (info);
ad8e1ba5 8693
4c52953f
AM
8694 htab->multi_toc_needed = htab->toc_curr != elf_gp (output_bfd);
8695
ad8e1ba5
AM
8696 /* toc_curr tracks the TOC offset used for code sections below in
8697 ppc64_elf_next_input_section. Start off at 0x8000. */
3d6f9012 8698 htab->toc_curr = TOC_BASE_OFF;
ad8e1ba5
AM
8699}
8700
9b5ecbd0
AM
8701/* No toc references were found in ISEC. If the code in ISEC makes no
8702 calls, then there's no need to use toc adjusting stubs when branching
8703 into ISEC. Actually, indirect calls from ISEC are OK as they will
4c52953f
AM
8704 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
8705 needed, and 2 if a cyclical call-graph was found but no other reason
8706 for a stub was detected. If called from the top level, a return of
8707 2 means the same as a return of 0. */
9b5ecbd0
AM
8708
8709static int
4ce794b7 8710toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
9b5ecbd0 8711{
4c52953f
AM
8712 Elf_Internal_Rela *relstart, *rel;
8713 Elf_Internal_Sym *local_syms;
9b5ecbd0 8714 int ret;
4c52953f 8715 struct ppc_link_hash_table *htab;
9b5ecbd0 8716
772119ce
AM
8717 /* We know none of our code bearing sections will need toc stubs. */
8718 if ((isec->flags & SEC_LINKER_CREATED) != 0)
8719 return 0;
8720
eea6121a 8721 if (isec->size == 0)
082c50f8
AM
8722 return 0;
8723
4c52953f
AM
8724 if (isec->output_section == NULL)
8725 return 0;
8726
9b5ecbd0
AM
8727 /* Hack for linux kernel. .fixup contains branches, but only back to
8728 the function that hit an exception. */
4c52953f
AM
8729 if (strcmp (isec->name, ".fixup") == 0)
8730 return 0;
9b5ecbd0 8731
4c52953f
AM
8732 if (isec->reloc_count == 0)
8733 return 0;
8734
8735 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
8736 info->keep_memory);
8737 if (relstart == NULL)
8738 return -1;
8739
8740 /* Look for branches to outside of this section. */
8741 local_syms = NULL;
8742 ret = 0;
8743 htab = ppc_hash_table (info);
8744 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
9b5ecbd0 8745 {
4c52953f
AM
8746 enum elf_ppc64_reloc_type r_type;
8747 unsigned long r_symndx;
8748 struct elf_link_hash_entry *h;
8749 Elf_Internal_Sym *sym;
8750 asection *sym_sec;
8751 long *opd_adjust;
8752 bfd_vma sym_value;
8753 bfd_vma dest;
8754
8755 r_type = ELF64_R_TYPE (rel->r_info);
8756 if (r_type != R_PPC64_REL24
8757 && r_type != R_PPC64_REL14
8758 && r_type != R_PPC64_REL14_BRTAKEN
8759 && r_type != R_PPC64_REL14_BRNTAKEN)
8760 continue;
8761
8762 r_symndx = ELF64_R_SYM (rel->r_info);
8763 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
8764 isec->owner))
9b5ecbd0 8765 {
4c52953f
AM
8766 ret = -1;
8767 break;
9b5ecbd0 8768 }
9b5ecbd0 8769
2917689a
AM
8770 /* Calls to dynamic lib functions go through a plt call stub
8771 that uses r2. Branches to undefined symbols might be a call
8772 using old-style dot symbols that can be satisfied by a plt
8773 call into a new-style dynamic library. */
4c52953f 8774 if (sym_sec == NULL)
2917689a
AM
8775 {
8776 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
8777 if (eh != NULL
8778 && eh->oh != NULL
8779 && eh->oh->elf.plt.plist != NULL)
8780 {
8781 ret = 1;
8782 break;
8783 }
4c52953f 8784
2917689a
AM
8785 /* Ignore other undefined symbols. */
8786 continue;
8787 }
8788
8789 /* Assume branches to other sections not included in the link need
8790 stubs too, to cover -R and absolute syms. */
4c52953f
AM
8791 if (sym_sec->output_section == NULL)
8792 {
8793 ret = 1;
8794 break;
8795 }
8796
8797 if (h == NULL)
8798 sym_value = sym->st_value;
8799 else
8800 {
8801 if (h->root.type != bfd_link_hash_defined
8802 && h->root.type != bfd_link_hash_defweak)
8803 abort ();
8804 sym_value = h->root.u.def.value;
8805 }
8806 sym_value += rel->r_addend;
8807
8808 /* If this branch reloc uses an opd sym, find the code section. */
8809 opd_adjust = get_opd_info (sym_sec);
8810 if (opd_adjust != NULL)
8811 {
4c52953f
AM
8812 if (h == NULL)
8813 {
8814 long adjust;
8815
8816 adjust = opd_adjust[sym->st_value / 8];
8817 if (adjust == -1)
8818 /* Assume deleted functions won't ever be called. */
8819 continue;
8820 sym_value += adjust;
8821 }
8822
8823 dest = opd_entry_value (sym_sec, sym_value, &sym_sec, NULL);
8824 if (dest == (bfd_vma) -1)
8825 continue;
8826 }
8827 else
8828 dest = (sym_value
8829 + sym_sec->output_offset
8830 + sym_sec->output_section->vma);
8831
8832 /* Ignore branch to self. */
8833 if (sym_sec == isec)
8834 continue;
8835
8836 /* If the called function uses the toc, we need a stub. */
8837 if (sym_sec->has_toc_reloc
8838 || sym_sec->makes_toc_func_call)
8839 {
8840 ret = 1;
8841 break;
8842 }
8843
8844 /* Assume any branch that needs a long branch stub might in fact
8845 need a plt_branch stub. A plt_branch stub uses r2. */
8846 else if (dest - (isec->output_offset
8847 + isec->output_section->vma
8848 + rel->r_offset) + (1 << 25) >= (2 << 25))
9b5ecbd0
AM
8849 {
8850 ret = 1;
8851 break;
8852 }
4c52953f
AM
8853
8854 /* If calling back to a section in the process of being tested, we
8855 can't say for sure that no toc adjusting stubs are needed, so
8856 don't return zero. */
8857 else if (sym_sec->call_check_in_progress)
8858 ret = 2;
8859
8860 /* Branches to another section that itself doesn't have any TOC
8861 references are OK. Recursively call ourselves to check. */
8862 else if (sym_sec->id <= htab->top_id
8863 && htab->stub_group[sym_sec->id].toc_off == 0)
8864 {
8865 int recur;
8866
8867 /* Mark current section as indeterminate, so that other
8868 sections that call back to current won't be marked as
8869 known. */
8870 isec->call_check_in_progress = 1;
8871 recur = toc_adjusting_stub_needed (info, sym_sec);
8872 isec->call_check_in_progress = 0;
8873
8874 if (recur < 0)
8875 {
8876 /* An error. Exit. */
8877 ret = -1;
8878 break;
8879 }
8880 else if (recur <= 1)
8881 {
8882 /* Known result. Mark as checked and set section flag. */
8883 htab->stub_group[sym_sec->id].toc_off = 1;
8884 if (recur != 0)
8885 {
8886 sym_sec->makes_toc_func_call = 1;
8887 ret = 1;
8888 break;
8889 }
8890 }
8891 else
8892 {
8893 /* Unknown result. Continue checking. */
8894 ret = 2;
8895 }
8896 }
9b5ecbd0
AM
8897 }
8898
4c52953f
AM
8899 if (local_syms != NULL
8900 && (elf_tdata (isec->owner)->symtab_hdr.contents
8901 != (unsigned char *) local_syms))
8902 free (local_syms);
8903 if (elf_section_data (isec)->relocs != relstart)
8904 free (relstart);
8905
9b5ecbd0
AM
8906 return ret;
8907}
8908
721956f4
AM
8909/* The linker repeatedly calls this function for each input section,
8910 in the order that input sections are linked into output sections.
8911 Build lists of input sections to determine groupings between which
8912 we may insert linker stubs. */
8913
9b5ecbd0 8914bfd_boolean
4ce794b7 8915ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
721956f4
AM
8916{
8917 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8918
734b6cf9
AM
8919 if ((isec->output_section->flags & SEC_CODE) != 0
8920 && isec->output_section->index <= htab->top_index)
721956f4 8921 {
734b6cf9 8922 asection **list = htab->input_list + isec->output_section->index;
3d6f9012 8923 /* Steal the link_sec pointer for our list. */
721956f4 8924#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
3d6f9012
AM
8925 /* This happens to make the list in reverse order,
8926 which is what we want. */
734b6cf9
AM
8927 PREV_SEC (isec) = *list;
8928 *list = isec;
721956f4 8929 }
ad8e1ba5 8930
4c52953f 8931 if (htab->multi_toc_needed)
9b5ecbd0 8932 {
4c52953f
AM
8933 /* If a code section has a function that uses the TOC then we need
8934 to use the right TOC (obviously). Also, make sure that .opd gets
8935 the correct TOC value for R_PPC64_TOC relocs that don't have or
8936 can't find their function symbol (shouldn't ever happen now). */
8937 if (isec->has_toc_reloc || (isec->flags & SEC_CODE) == 0)
8938 {
8939 if (elf_gp (isec->owner) != 0)
8940 htab->toc_curr = elf_gp (isec->owner);
8941 }
8942 else if (htab->stub_group[isec->id].toc_off == 0)
8943 {
8944 int ret = toc_adjusting_stub_needed (info, isec);
8945 if (ret < 0)
8946 return FALSE;
8947 else
8948 isec->makes_toc_func_call = ret & 1;
8949 }
9b5ecbd0 8950 }
ad8e1ba5
AM
8951
8952 /* Functions that don't use the TOC can belong in any TOC group.
8953 Use the last TOC base. This happens to make _init and _fini
8954 pasting work. */
8955 htab->stub_group[isec->id].toc_off = htab->toc_curr;
9b5ecbd0 8956 return TRUE;
721956f4
AM
8957}
8958
8959/* See whether we can group stub sections together. Grouping stub
8960 sections may result in fewer stubs. More importantly, we need to
8961 put all .init* and .fini* stubs at the beginning of the .init or
8962 .fini output sections respectively, because glibc splits the
8963 _init and _fini functions into multiple parts. Putting a stub in
8964 the middle of a function is not a good idea. */
8965
8966static void
4ce794b7
AM
8967group_sections (struct ppc_link_hash_table *htab,
8968 bfd_size_type stub_group_size,
8969 bfd_boolean stubs_always_before_branch)
721956f4 8970{
7c8fe5c4
AM
8971 asection **list;
8972 bfd_size_type stub14_group_size;
8973 bfd_boolean suppress_size_errors;
8974
8975 suppress_size_errors = FALSE;
8976 stub14_group_size = stub_group_size;
8977 if (stub_group_size == 1)
8978 {
8979 /* Default values. */
8980 if (stubs_always_before_branch)
8981 {
8982 stub_group_size = 0x1e00000;
8983 stub14_group_size = 0x7800;
8984 }
8985 else
8986 {
8987 stub_group_size = 0x1c00000;
8988 stub14_group_size = 0x7000;
8989 }
8990 suppress_size_errors = TRUE;
8991 }
8992
8993 list = htab->input_list + htab->top_index;
734b6cf9 8994 do
721956f4 8995 {
734b6cf9
AM
8996 asection *tail = *list;
8997 while (tail != NULL)
721956f4 8998 {
734b6cf9
AM
8999 asection *curr;
9000 asection *prev;
9001 bfd_size_type total;
9002 bfd_boolean big_sec;
9003 bfd_vma curr_toc;
9004
9005 curr = tail;
eea6121a 9006 total = tail->size;
7c8fe5c4
AM
9007 big_sec = total > (ppc64_elf_section_data (tail)->has_14bit_branch
9008 ? stub14_group_size : stub_group_size);
9009 if (big_sec && !suppress_size_errors)
5c3dead3
AM
9010 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
9011 tail->owner, tail);
734b6cf9
AM
9012 curr_toc = htab->stub_group[tail->id].toc_off;
9013
9014 while ((prev = PREV_SEC (curr)) != NULL
9015 && ((total += curr->output_offset - prev->output_offset)
7c8fe5c4
AM
9016 < (ppc64_elf_section_data (prev)->has_14bit_branch
9017 ? stub14_group_size : stub_group_size))
ad8e1ba5 9018 && htab->stub_group[prev->id].toc_off == curr_toc)
734b6cf9
AM
9019 curr = prev;
9020
9021 /* OK, the size from the start of CURR to the end is less
9022 than stub_group_size and thus can be handled by one stub
9023 section. (or the tail section is itself larger than
9024 stub_group_size, in which case we may be toast.) We
9025 should really be keeping track of the total size of stubs
9026 added here, as stubs contribute to the final output
9027 section size. That's a little tricky, and this way will
9028 only break if stubs added make the total size more than
9029 2^25, ie. for the default stub_group_size, if stubs total
9030 more than 2097152 bytes, or nearly 75000 plt call stubs. */
9031 do
721956f4
AM
9032 {
9033 prev = PREV_SEC (tail);
734b6cf9 9034 /* Set up this stub group. */
721956f4
AM
9035 htab->stub_group[tail->id].link_sec = curr;
9036 }
734b6cf9
AM
9037 while (tail != curr && (tail = prev) != NULL);
9038
9039 /* But wait, there's more! Input sections up to stub_group_size
9040 bytes before the stub section can be handled by it too.
9041 Don't do this if we have a really large section after the
9042 stubs, as adding more stubs increases the chance that
9043 branches may not reach into the stub section. */
9044 if (!stubs_always_before_branch && !big_sec)
9045 {
9046 total = 0;
9047 while (prev != NULL
9048 && ((total += tail->output_offset - prev->output_offset)
7c8fe5c4
AM
9049 < (ppc64_elf_section_data (prev)->has_14bit_branch
9050 ? stub14_group_size : stub_group_size))
734b6cf9
AM
9051 && htab->stub_group[prev->id].toc_off == curr_toc)
9052 {
9053 tail = prev;
9054 prev = PREV_SEC (tail);
9055 htab->stub_group[tail->id].link_sec = curr;
9056 }
9057 }
9058 tail = prev;
721956f4
AM
9059 }
9060 }
734b6cf9
AM
9061 while (list-- != htab->input_list);
9062 free (htab->input_list);
721956f4
AM
9063#undef PREV_SEC
9064}
9065
721956f4
AM
9066/* Determine and set the size of the stub section for a final link.
9067
9068 The basic idea here is to examine all the relocations looking for
9069 PC-relative calls to a target that is unreachable with a "bl"
9070 instruction. */
9071
b34976b6 9072bfd_boolean
4ce794b7 9073ppc64_elf_size_stubs (bfd *output_bfd,
4ce794b7
AM
9074 struct bfd_link_info *info,
9075 bfd_signed_vma group_size,
9076 asection *(*add_stub_section) (const char *, asection *),
9077 void (*layout_sections_again) (void))
721956f4
AM
9078{
9079 bfd_size_type stub_group_size;
b34976b6 9080 bfd_boolean stubs_always_before_branch;
721956f4
AM
9081 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9082
9083 /* Stash our params away. */
721956f4
AM
9084 htab->add_stub_section = add_stub_section;
9085 htab->layout_sections_again = layout_sections_again;
9086 stubs_always_before_branch = group_size < 0;
9087 if (group_size < 0)
9088 stub_group_size = -group_size;
9089 else
9090 stub_group_size = group_size;
721956f4
AM
9091
9092 group_sections (htab, stub_group_size, stubs_always_before_branch);
9093
721956f4
AM
9094 while (1)
9095 {
9096 bfd *input_bfd;
9097 unsigned int bfd_indx;
9098 asection *stub_sec;
721956f4
AM
9099
9100 htab->stub_iteration += 1;
721956f4
AM
9101
9102 for (input_bfd = info->input_bfds, bfd_indx = 0;
9103 input_bfd != NULL;
9104 input_bfd = input_bfd->link_next, bfd_indx++)
9105 {
9106 Elf_Internal_Shdr *symtab_hdr;
9107 asection *section;
6cdc0ccc 9108 Elf_Internal_Sym *local_syms = NULL;
721956f4 9109
67f93c31
AM
9110 if (!is_ppc64_elf_target (input_bfd->xvec))
9111 continue;
9112
721956f4
AM
9113 /* We'll need the symbol table in a second. */
9114 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9115 if (symtab_hdr->sh_info == 0)
9116 continue;
9117
721956f4
AM
9118 /* Walk over each section attached to the input bfd. */
9119 for (section = input_bfd->sections;
9120 section != NULL;
9121 section = section->next)
9122 {
721956f4 9123 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
721956f4
AM
9124
9125 /* If there aren't any relocs, then there's nothing more
9126 to do. */
9127 if ((section->flags & SEC_RELOC) == 0
12c0f757
AM
9128 || (section->flags & SEC_ALLOC) == 0
9129 || (section->flags & SEC_LOAD) == 0
9130 || (section->flags & SEC_CODE) == 0
721956f4
AM
9131 || section->reloc_count == 0)
9132 continue;
9133
9134 /* If this section is a link-once section that will be
9135 discarded, then don't create any stubs. */
9136 if (section->output_section == NULL
9137 || section->output_section->owner != output_bfd)
9138 continue;
9139
1e2f5b6e
AM
9140 /* Get the relocs. */
9141 internal_relocs
4ce794b7 9142 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
45d6a902 9143 info->keep_memory);
721956f4 9144 if (internal_relocs == NULL)
1e2f5b6e 9145 goto error_ret_free_local;
721956f4
AM
9146
9147 /* Now examine each relocation. */
9148 irela = internal_relocs;
9149 irelaend = irela + section->reloc_count;
9150 for (; irela < irelaend; irela++)
9151 {
4ce794b7
AM
9152 enum elf_ppc64_reloc_type r_type;
9153 unsigned int r_indx;
721956f4
AM
9154 enum ppc_stub_type stub_type;
9155 struct ppc_stub_hash_entry *stub_entry;
8387904d 9156 asection *sym_sec, *code_sec;
721956f4
AM
9157 bfd_vma sym_value;
9158 bfd_vma destination;
8843416a 9159 bfd_boolean ok_dest;
721956f4 9160 struct ppc_link_hash_entry *hash;
8387904d 9161 struct ppc_link_hash_entry *fdh;
411e1bfb
AM
9162 struct elf_link_hash_entry *h;
9163 Elf_Internal_Sym *sym;
721956f4
AM
9164 char *stub_name;
9165 const asection *id_sec;
8387904d 9166 long *opd_adjust;
721956f4
AM
9167
9168 r_type = ELF64_R_TYPE (irela->r_info);
9169 r_indx = ELF64_R_SYM (irela->r_info);
9170
4ce794b7 9171 if (r_type >= R_PPC64_max)
721956f4
AM
9172 {
9173 bfd_set_error (bfd_error_bad_value);
6cdc0ccc 9174 goto error_ret_free_internal;
721956f4
AM
9175 }
9176
9177 /* Only look for stubs on branch instructions. */
4ce794b7
AM
9178 if (r_type != R_PPC64_REL24
9179 && r_type != R_PPC64_REL14
9180 && r_type != R_PPC64_REL14_BRTAKEN
9181 && r_type != R_PPC64_REL14_BRNTAKEN)
721956f4
AM
9182 continue;
9183
9184 /* Now determine the call target, its name, value,
9185 section. */
411e1bfb
AM
9186 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9187 r_indx, input_bfd))
9188 goto error_ret_free_internal;
9189 hash = (struct ppc_link_hash_entry *) h;
9190
8843416a 9191 ok_dest = FALSE;
8387904d 9192 fdh = NULL;
7fe2b9a6 9193 sym_value = 0;
411e1bfb 9194 if (hash == NULL)
721956f4 9195 {
411e1bfb 9196 sym_value = sym->st_value;
8843416a 9197 ok_dest = TRUE;
721956f4 9198 }
7fe2b9a6
AM
9199 else if (hash->elf.root.type == bfd_link_hash_defined
9200 || hash->elf.root.type == bfd_link_hash_defweak)
9201 {
9202 sym_value = hash->elf.root.u.def.value;
9203 if (sym_sec->output_section != NULL)
9204 ok_dest = TRUE;
9205 }
9206 else if (hash->elf.root.type == bfd_link_hash_undefweak
9207 || hash->elf.root.type == bfd_link_hash_undefined)
721956f4 9208 {
99877b66 9209 /* Recognise an old ABI func code entry sym, and
7fe2b9a6
AM
9210 use the func descriptor sym instead if it is
9211 defined. */
ceb1f1ef 9212 if (hash->elf.root.root.string[0] == '.'
8387904d
AM
9213 && (fdh = get_fdh (hash, htab)) != NULL)
9214 {
8387904d
AM
9215 if (fdh->elf.root.type == bfd_link_hash_defined
9216 || fdh->elf.root.type == bfd_link_hash_defweak)
9217 {
9218 sym_sec = fdh->elf.root.u.def.section;
9219 sym_value = fdh->elf.root.u.def.value;
9220 if (sym_sec->output_section != NULL)
9221 ok_dest = TRUE;
9222 }
99877b66
AM
9223 else
9224 fdh = NULL;
8387904d 9225 }
7fe2b9a6
AM
9226 }
9227 else
9228 {
9229 bfd_set_error (bfd_error_bad_value);
9230 goto error_ret_free_internal;
721956f4
AM
9231 }
9232
8843416a
AM
9233 destination = 0;
9234 if (ok_dest)
9235 {
9236 sym_value += irela->r_addend;
9237 destination = (sym_value
9238 + sym_sec->output_offset
9239 + sym_sec->output_section->vma);
9240 }
9241
8387904d
AM
9242 code_sec = sym_sec;
9243 opd_adjust = get_opd_info (sym_sec);
9244 if (opd_adjust != NULL)
9245 {
9246 bfd_vma dest;
9247
9248 if (hash == NULL)
9249 {
3f764659 9250 long adjust = opd_adjust[sym_value / 8];
8387904d
AM
9251 if (adjust == -1)
9252 continue;
9253 sym_value += adjust;
9254 }
9255 dest = opd_entry_value (sym_sec, sym_value,
9256 &code_sec, &sym_value);
9257 if (dest != (bfd_vma) -1)
9258 {
9259 destination = dest;
9260 if (fdh != NULL)
9261 {
9262 /* Fixup old ABI sym to point at code
9263 entry. */
99877b66 9264 hash->elf.root.type = bfd_link_hash_defweak;
8387904d
AM
9265 hash->elf.root.u.def.section = code_sec;
9266 hash->elf.root.u.def.value = sym_value;
9267 }
9268 }
9269 }
9270
721956f4
AM
9271 /* Determine what (if any) linker stub is needed. */
9272 stub_type = ppc_type_of_stub (section, irela, &hash,
9273 destination);
ad8e1ba5
AM
9274
9275 if (stub_type != ppc_stub_plt_call)
9276 {
9277 /* Check whether we need a TOC adjusting stub.
9278 Since the linker pastes together pieces from
9279 different object files when creating the
9280 _init and _fini functions, it may be that a
9281 call to what looks like a local sym is in
9282 fact a call needing a TOC adjustment. */
8387904d
AM
9283 if (code_sec != NULL
9284 && code_sec->output_section != NULL
9285 && (htab->stub_group[code_sec->id].toc_off
9b5ecbd0 9286 != htab->stub_group[section->id].toc_off)
4c52953f
AM
9287 && (code_sec->has_toc_reloc
9288 || code_sec->makes_toc_func_call))
ad8e1ba5
AM
9289 stub_type = ppc_stub_long_branch_r2off;
9290 }
9291
721956f4
AM
9292 if (stub_type == ppc_stub_none)
9293 continue;
9294
411e1bfb
AM
9295 /* __tls_get_addr calls might be eliminated. */
9296 if (stub_type != ppc_stub_plt_call
9297 && hash != NULL
8387904d
AM
9298 && (hash == htab->tls_get_addr
9299 || hash == htab->tls_get_addr_fd)
411e1bfb
AM
9300 && section->has_tls_reloc
9301 && irela != internal_relocs)
9302 {
9303 /* Get tls info. */
e7b938ca 9304 char *tls_mask;
411e1bfb 9305
0d4792f7 9306 if (!get_tls_mask (&tls_mask, NULL, &local_syms,
411e1bfb
AM
9307 irela - 1, input_bfd))
9308 goto error_ret_free_internal;
e7b938ca 9309 if (*tls_mask != 0)
411e1bfb
AM
9310 continue;
9311 }
9312
721956f4
AM
9313 /* Support for grouping stub sections. */
9314 id_sec = htab->stub_group[section->id].link_sec;
9315
9316 /* Get the name of this stub. */
9317 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
9318 if (!stub_name)
9319 goto error_ret_free_internal;
9320
9321 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
b34976b6 9322 stub_name, FALSE, FALSE);
721956f4
AM
9323 if (stub_entry != NULL)
9324 {
9325 /* The proper stub has already been created. */
9326 free (stub_name);
9327 continue;
9328 }
9329
9330 stub_entry = ppc_add_stub (stub_name, section, htab);
9331 if (stub_entry == NULL)
9332 {
9333 free (stub_name);
6cdc0ccc
AM
9334 error_ret_free_internal:
9335 if (elf_section_data (section)->relocs == NULL)
9336 free (internal_relocs);
9337 error_ret_free_local:
9338 if (local_syms != NULL
9339 && (symtab_hdr->contents
9340 != (unsigned char *) local_syms))
9341 free (local_syms);
b34976b6 9342 return FALSE;
721956f4
AM
9343 }
9344
ad8e1ba5 9345 stub_entry->stub_type = stub_type;
721956f4 9346 stub_entry->target_value = sym_value;
8387904d 9347 stub_entry->target_section = code_sec;
721956f4 9348 stub_entry->h = hash;
411e1bfb 9349 stub_entry->addend = irela->r_addend;
ee75fd95
AM
9350
9351 if (stub_entry->h != NULL)
9352 htab->stub_globals += 1;
721956f4
AM
9353 }
9354
9355 /* We're done with the internal relocs, free them. */
6cdc0ccc 9356 if (elf_section_data (section)->relocs != internal_relocs)
1e2f5b6e 9357 free (internal_relocs);
721956f4 9358 }
6cdc0ccc
AM
9359
9360 if (local_syms != NULL
9361 && symtab_hdr->contents != (unsigned char *) local_syms)
9362 {
9363 if (!info->keep_memory)
9364 free (local_syms);
9365 else
9366 symtab_hdr->contents = (unsigned char *) local_syms;
9367 }
721956f4
AM
9368 }
9369
5c3dead3 9370 /* We may have added some stubs. Find out the new size of the
721956f4
AM
9371 stub sections. */
9372 for (stub_sec = htab->stub_bfd->sections;
9373 stub_sec != NULL;
9374 stub_sec = stub_sec->next)
e717da7e 9375 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
ee75fd95 9376 {
5c3dead3 9377 stub_sec->rawsize = stub_sec->size;
ee75fd95
AM
9378 stub_sec->size = 0;
9379 stub_sec->reloc_count = 0;
9380 }
eea6121a
AM
9381
9382 htab->brlt->size = 0;
ee75fd95 9383 if (htab->relbrlt != NULL)
eea6121a 9384 htab->relbrlt->size = 0;
721956f4 9385
63bc6f6c 9386 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
721956f4 9387
5c3dead3
AM
9388 for (stub_sec = htab->stub_bfd->sections;
9389 stub_sec != NULL;
9390 stub_sec = stub_sec->next)
9391 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9392 && stub_sec->rawsize != stub_sec->size)
9393 break;
9394
9395 /* Exit from this loop when no stubs have been added, and no stubs
9396 have changed size. */
9397 if (stub_sec == NULL)
9398 break;
9399
721956f4
AM
9400 /* Ask the linker to do its stuff. */
9401 (*htab->layout_sections_again) ();
9402 }
9403
c456f082 9404 /* It would be nice to strip htab->brlt from the output if the
afbe61cf
AM
9405 section is empty, but it's too late. If we strip sections here,
9406 the dynamic symbol table is corrupted since the section symbol
9407 for the stripped section isn't written. */
721956f4 9408
b34976b6 9409 return TRUE;
721956f4
AM
9410}
9411
9412/* Called after we have determined section placement. If sections
805fc799 9413 move, we'll be called again. Provide a value for TOCstart. */
721956f4 9414
805fc799 9415bfd_vma
4ce794b7 9416ppc64_elf_toc (bfd *obfd)
721956f4 9417{
805fc799
AM
9418 asection *s;
9419 bfd_vma TOCstart;
721956f4 9420
805fc799
AM
9421 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
9422 order. The TOC starts where the first of these sections starts. */
9423 s = bfd_get_section_by_name (obfd, ".got");
9424 if (s == NULL)
9425 s = bfd_get_section_by_name (obfd, ".toc");
9426 if (s == NULL)
9427 s = bfd_get_section_by_name (obfd, ".tocbss");
9428 if (s == NULL)
9429 s = bfd_get_section_by_name (obfd, ".plt");
9430 if (s == NULL)
9431 {
9432 /* This may happen for
9433 o references to TOC base (SYM@toc / TOC[tc0]) without a
9434 .toc directive
9435 o bad linker script
9436 o --gc-sections and empty TOC sections
9437
9438 FIXME: Warn user? */
9439
9440 /* Look for a likely section. We probably won't even be
9441 using TOCstart. */
9442 for (s = obfd->sections; s != NULL; s = s->next)
9443 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY))
9444 == (SEC_ALLOC | SEC_SMALL_DATA))
9445 break;
721956f4 9446 if (s == NULL)
805fc799
AM
9447 for (s = obfd->sections; s != NULL; s = s->next)
9448 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA))
9449 == (SEC_ALLOC | SEC_SMALL_DATA))
9450 break;
721956f4 9451 if (s == NULL)
805fc799
AM
9452 for (s = obfd->sections; s != NULL; s = s->next)
9453 if ((s->flags & (SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC)
9454 break;
721956f4 9455 if (s == NULL)
805fc799
AM
9456 for (s = obfd->sections; s != NULL; s = s->next)
9457 if ((s->flags & SEC_ALLOC) == SEC_ALLOC)
9458 break;
9459 }
721956f4 9460
805fc799
AM
9461 TOCstart = 0;
9462 if (s != NULL)
9463 TOCstart = s->output_section->vma + s->output_offset;
721956f4 9464
805fc799 9465 return TOCstart;
721956f4
AM
9466}
9467
9468/* Build all the stubs associated with the current output file.
9469 The stubs are kept in a hash table attached to the main linker
9470 hash table. This function is called via gldelf64ppc_finish. */
9471
b34976b6 9472bfd_boolean
4ce794b7
AM
9473ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
9474 struct bfd_link_info *info,
9475 char **stats)
5d1634d7
AM
9476{
9477 struct ppc_link_hash_table *htab = ppc_hash_table (info);
721956f4 9478 asection *stub_sec;
5d1634d7 9479 bfd_byte *p;
e717da7e 9480 int stub_sec_count = 0;
5d1634d7 9481
ad8e1ba5 9482 htab->emit_stub_syms = emit_stub_syms;
eea6121a
AM
9483
9484 /* Allocate memory to hold the linker stubs. */
721956f4
AM
9485 for (stub_sec = htab->stub_bfd->sections;
9486 stub_sec != NULL;
9487 stub_sec = stub_sec->next)
eea6121a
AM
9488 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
9489 && stub_sec->size != 0)
e717da7e 9490 {
eea6121a
AM
9491 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
9492 if (stub_sec->contents == NULL)
9493 return FALSE;
9494 /* We want to check that built size is the same as calculated
9495 size. rawsize is a convenient location to use. */
9496 stub_sec->rawsize = stub_sec->size;
9497 stub_sec->size = 0;
e717da7e 9498 }
5d1634d7 9499
23eb7e01 9500 if (htab->glink != NULL && htab->glink->size != 0)
5d1634d7 9501 {
9f951329 9502 unsigned int indx;
ad8e1ba5 9503 bfd_vma plt0;
9f951329 9504
721956f4 9505 /* Build the .glink plt call stub. */
97b639ba
AM
9506 if (htab->emit_stub_syms)
9507 {
9508 struct elf_link_hash_entry *h;
9509 h = elf_link_hash_lookup (&htab->elf, "__glink", TRUE, FALSE, FALSE);
9510 if (h == NULL)
9511 return FALSE;
9512 if (h->root.type == bfd_link_hash_new)
9513 {
9514 h->root.type = bfd_link_hash_defined;
9515 h->root.u.def.section = htab->glink;
ee4bf8d2 9516 h->root.u.def.value = 8;
f5385ebf
AM
9517 h->ref_regular = 1;
9518 h->def_regular = 1;
9519 h->ref_regular_nonweak = 1;
9520 h->forced_local = 1;
9521 h->non_elf = 0;
97b639ba
AM
9522 }
9523 }
4ce794b7 9524 p = htab->glink->contents;
ee4bf8d2
AM
9525 plt0 = (htab->plt->output_section->vma
9526 + htab->plt->output_offset
9527 - (htab->glink->output_section->vma
9528 + htab->glink->output_offset
9529 + 16));
9530 bfd_put_64 (htab->glink->owner, plt0, p);
9531 p += 8;
9532 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
ad8e1ba5 9533 p += 4;
ee4bf8d2 9534 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
ad8e1ba5 9535 p += 4;
ee4bf8d2 9536 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
ad8e1ba5 9537 p += 4;
ee4bf8d2 9538 bfd_put_32 (htab->glink->owner, LD_R2_M16R11, p);
ad8e1ba5 9539 p += 4;
ee4bf8d2 9540 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
ad8e1ba5 9541 p += 4;
ee4bf8d2 9542 bfd_put_32 (htab->glink->owner, ADD_R12_R2_R11, p);
ad8e1ba5 9543 p += 4;
ee4bf8d2 9544 bfd_put_32 (htab->glink->owner, LD_R11_0R12, p);
ad8e1ba5 9545 p += 4;
4ce794b7 9546 bfd_put_32 (htab->glink->owner, LD_R2_0R12 | 8, p);
ad8e1ba5 9547 p += 4;
4ce794b7 9548 bfd_put_32 (htab->glink->owner, MTCTR_R11, p);
ad8e1ba5 9549 p += 4;
4ce794b7 9550 bfd_put_32 (htab->glink->owner, LD_R11_0R12 | 16, p);
ad8e1ba5 9551 p += 4;
4ce794b7 9552 bfd_put_32 (htab->glink->owner, BCTR, p);
ad8e1ba5 9553 p += 4;
ee4bf8d2
AM
9554 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
9555 {
9556 bfd_put_32 (htab->glink->owner, NOP, p);
9557 p += 4;
9558 }
ad8e1ba5 9559
9f951329
AM
9560 /* Build the .glink lazy link call stubs. */
9561 indx = 0;
eea6121a 9562 while (p < htab->glink->contents + htab->glink->size)
9f951329
AM
9563 {
9564 if (indx < 0x8000)
9565 {
4ce794b7 9566 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
9f951329
AM
9567 p += 4;
9568 }
9569 else
9570 {
4ce794b7 9571 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
9f951329 9572 p += 4;
4ce794b7 9573 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
9f951329
AM
9574 p += 4;
9575 }
4ce794b7 9576 bfd_put_32 (htab->glink->owner,
ee4bf8d2 9577 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
a16d5acb 9578 indx++;
9f951329
AM
9579 p += 4;
9580 }
eea6121a 9581 htab->glink->rawsize = p - htab->glink->contents;
5d1634d7 9582 }
5d1634d7 9583
eea6121a 9584 if (htab->brlt->size != 0)
721956f4 9585 {
4ce794b7 9586 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
eea6121a 9587 htab->brlt->size);
4ce794b7 9588 if (htab->brlt->contents == NULL)
b34976b6 9589 return FALSE;
721956f4 9590 }
ee75fd95 9591 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
63bc6f6c
AM
9592 {
9593 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
eea6121a 9594 htab->relbrlt->size);
63bc6f6c
AM
9595 if (htab->relbrlt->contents == NULL)
9596 return FALSE;
9597 }
5d1634d7 9598
721956f4
AM
9599 /* Build the stubs as directed by the stub hash table. */
9600 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
5d1634d7 9601
aa8a7074
AM
9602 if (htab->relbrlt != NULL)
9603 htab->relbrlt->reloc_count = 0;
9604
721956f4
AM
9605 for (stub_sec = htab->stub_bfd->sections;
9606 stub_sec != NULL;
9607 stub_sec = stub_sec->next)
e717da7e
AM
9608 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
9609 {
9610 stub_sec_count += 1;
eea6121a 9611 if (stub_sec->rawsize != stub_sec->size)
e717da7e
AM
9612 break;
9613 }
5d1634d7 9614
721956f4 9615 if (stub_sec != NULL
eea6121a 9616 || htab->glink->rawsize != htab->glink->size)
5d1634d7 9617 {
b34976b6 9618 htab->stub_error = TRUE;
721956f4 9619 (*_bfd_error_handler) (_("stubs don't match calculated size"));
5d1634d7 9620 }
721956f4 9621
d2a300cf
AM
9622 if (htab->stub_error)
9623 return FALSE;
9624
9625 if (stats != NULL)
9626 {
9627 *stats = bfd_malloc (500);
9628 if (*stats == NULL)
9629 return FALSE;
9630
ee75fd95 9631 sprintf (*stats, _("linker stubs in %u group%s\n"
d2a300cf
AM
9632 " branch %lu\n"
9633 " toc adjust %lu\n"
9634 " long branch %lu\n"
9635 " long toc adj %lu\n"
9636 " plt call %lu"),
e717da7e 9637 stub_sec_count,
ee75fd95 9638 stub_sec_count == 1 ? "" : "s",
4ce794b7
AM
9639 htab->stub_count[ppc_stub_long_branch - 1],
9640 htab->stub_count[ppc_stub_long_branch_r2off - 1],
9641 htab->stub_count[ppc_stub_plt_branch - 1],
9642 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
9643 htab->stub_count[ppc_stub_plt_call - 1]);
d2a300cf
AM
9644 }
9645 return TRUE;
5bd4f169
AM
9646}
9647
99877b66
AM
9648/* This function undoes the changes made by add_symbol_adjust. */
9649
9650static bfd_boolean
9651undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
9652{
9653 struct ppc_link_hash_entry *eh;
9654
9655 if (h->root.type == bfd_link_hash_indirect)
9656 return TRUE;
9657
9658 if (h->root.type == bfd_link_hash_warning)
9659 h = (struct elf_link_hash_entry *) h->root.u.i.link;
9660
9661 eh = (struct ppc_link_hash_entry *) h;
9662 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
9663 return TRUE;
9664
9665 eh->elf.root.type = bfd_link_hash_undefined;
9666 return TRUE;
9667}
9668
9669void
9670ppc64_elf_restore_symbols (struct bfd_link_info *info)
9671{
9672 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9673 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
9674}
9675
60124e18
AM
9676/* What to do when ld finds relocations against symbols defined in
9677 discarded sections. */
9678
9679static unsigned int
9680ppc64_elf_action_discarded (asection *sec)
9681{
9682 if (strcmp (".opd", sec->name) == 0)
9683 return 0;
9684
9685 if (strcmp (".toc", sec->name) == 0)
9686 return 0;
9687
bce50a28
JJ
9688 if (strcmp (".toc1", sec->name) == 0)
9689 return 0;
9690
60124e18
AM
9691 return _bfd_elf_default_action_discarded (sec);
9692}
9693
5bd4f169
AM
9694/* The RELOCATE_SECTION function is called by the ELF backend linker
9695 to handle the relocations for a section.
9696
9697 The relocs are always passed as Rela structures; if the section
9698 actually uses Rel structures, the r_addend field will always be
9699 zero.
9700
9701 This function is responsible for adjust the section contents as
9702 necessary, and (if using Rela relocs and generating a
1049f94e 9703 relocatable output file) adjusting the reloc addend as
5bd4f169
AM
9704 necessary.
9705
9706 This function does not have to worry about setting the reloc
9707 address or the reloc symbol index.
9708
9709 LOCAL_SYMS is a pointer to the swapped in local symbols.
9710
9711 LOCAL_SECTIONS is an array giving the section in the input file
9712 corresponding to the st_shndx field of each local symbol.
9713
9714 The global hash table entry for the global symbols can be found
9715 via elf_sym_hashes (input_bfd).
9716
1049f94e 9717 When generating relocatable output, this function must handle
5bd4f169
AM
9718 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
9719 going to be the section symbol corresponding to the output
9720 section, which means that the addend must be adjusted
9721 accordingly. */
9722
b34976b6 9723static bfd_boolean
4ce794b7
AM
9724ppc64_elf_relocate_section (bfd *output_bfd,
9725 struct bfd_link_info *info,
9726 bfd *input_bfd,
9727 asection *input_section,
9728 bfd_byte *contents,
9729 Elf_Internal_Rela *relocs,
9730 Elf_Internal_Sym *local_syms,
9731 asection **local_sections)
5bd4f169 9732{
65f38f15 9733 struct ppc_link_hash_table *htab;
5bd4f169
AM
9734 Elf_Internal_Shdr *symtab_hdr;
9735 struct elf_link_hash_entry **sym_hashes;
5bd4f169
AM
9736 Elf_Internal_Rela *rel;
9737 Elf_Internal_Rela *relend;
411e1bfb
AM
9738 Elf_Internal_Rela outrel;
9739 bfd_byte *loc;
411e1bfb 9740 struct got_entry **local_got_ents;
5bd4f169 9741 bfd_vma TOCstart;
b34976b6
AM
9742 bfd_boolean ret = TRUE;
9743 bfd_boolean is_opd;
96e0dda4 9744 /* Disabled until we sort out how ld should choose 'y' vs 'at'. */
b34976b6 9745 bfd_boolean is_power4 = FALSE;
4fe5ca5b 9746 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
5bd4f169 9747
65f38f15 9748 /* Initialize howto table if needed. */
5bd4f169 9749 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5bd4f169
AM
9750 ppc_howto_init ();
9751
65f38f15 9752 htab = ppc_hash_table (info);
ee75fd95
AM
9753
9754 /* Don't relocate stub sections. */
9755 if (input_section->owner == htab->stub_bfd)
9756 return TRUE;
9757
411e1bfb 9758 local_got_ents = elf_local_got_ents (input_bfd);
5bd4f169 9759 TOCstart = elf_gp (output_bfd);
5bd4f169
AM
9760 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
9761 sym_hashes = elf_sym_hashes (input_bfd);
7c8fe5c4 9762 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
65f38f15 9763
5bd4f169
AM
9764 rel = relocs;
9765 relend = relocs + input_section->reloc_count;
9766 for (; rel < relend; rel++)
9767 {
04c9666a 9768 enum elf_ppc64_reloc_type r_type;
4cc603a5 9769 bfd_vma addend, orig_addend;
5bd4f169
AM
9770 bfd_reloc_status_type r;
9771 Elf_Internal_Sym *sym;
9772 asection *sec;
039b3fef
AM
9773 struct elf_link_hash_entry *h_elf;
9774 struct ppc_link_hash_entry *h;
9775 struct ppc_link_hash_entry *fdh;
5bd4f169 9776 const char *sym_name;
0d4792f7 9777 unsigned long r_symndx, toc_symndx;
951fd09b 9778 char tls_mask, tls_gd, tls_type;
0d4792f7 9779 char sym_type;
5bd4f169 9780 bfd_vma relocation;
b34976b6
AM
9781 bfd_boolean unresolved_reloc;
9782 bfd_boolean warned;
50bc7936 9783 unsigned long insn, mask;
721956f4
AM
9784 struct ppc_stub_hash_entry *stub_entry;
9785 bfd_vma max_br_offset;
9786 bfd_vma from;
5bd4f169 9787
4ce794b7 9788 r_type = ELF64_R_TYPE (rel->r_info);
5bd4f169 9789 r_symndx = ELF64_R_SYM (rel->r_info);
ee87f2da
AM
9790
9791 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
9792 symbol of the previous ADDR64 reloc. The symbol gives us the
9793 proper TOC base to use. */
9794 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
9795 && rel != relocs
9796 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
9797 && is_opd)
9798 r_symndx = ELF64_R_SYM (rel[-1].r_info);
9799
4ce794b7
AM
9800 sym = NULL;
9801 sec = NULL;
039b3fef 9802 h_elf = NULL;
4ce794b7 9803 sym_name = NULL;
b34976b6
AM
9804 unresolved_reloc = FALSE;
9805 warned = FALSE;
4cc603a5 9806 orig_addend = rel->r_addend;
65f38f15 9807
0b13192e 9808 if (r_symndx < symtab_hdr->sh_info)
5bd4f169
AM
9809 {
9810 /* It's a local symbol. */
4025353c
AM
9811 long *opd_adjust;
9812
5bd4f169
AM
9813 sym = local_syms + r_symndx;
9814 sec = local_sections[r_symndx];
26c61ae5 9815 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
0d4792f7 9816 sym_type = ELF64_ST_TYPE (sym->st_info);
8517fae7 9817 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
4025353c
AM
9818 opd_adjust = get_opd_info (sec);
9819 if (opd_adjust != NULL)
1e2f5b6e 9820 {
3f764659 9821 long adjust = opd_adjust[(sym->st_value + rel->r_addend) / 8];
4025353c
AM
9822 if (adjust == -1)
9823 relocation = 0;
9824 else
4cc603a5
AM
9825 {
9826 /* If this is a relocation against the opd section sym
9827 and we have edited .opd, adjust the reloc addend so
9828 that ld -r and ld --emit-relocs output is correct.
9829 If it is a reloc against some other .opd symbol,
9830 then the symbol value will be adjusted later. */
9831 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
9832 rel->r_addend += adjust;
9833 else
9834 relocation += adjust;
9835 }
1e2f5b6e 9836 }
4cc603a5
AM
9837 if (info->relocatable)
9838 continue;
5bd4f169
AM
9839 }
9840 else
9841 {
4cc603a5
AM
9842 if (info->relocatable)
9843 continue;
b2a8e766
AM
9844 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
9845 r_symndx, symtab_hdr, sym_hashes,
039b3fef 9846 h_elf, sec, relocation,
b2a8e766 9847 unresolved_reloc, warned);
039b3fef
AM
9848 sym_name = h_elf->root.root.string;
9849 sym_type = h_elf->type;
5bd4f169 9850 }
039b3fef 9851 h = (struct ppc_link_hash_entry *) h_elf;
5bd4f169 9852
951fd09b
AM
9853 /* TLS optimizations. Replace instruction sequences and relocs
9854 based on information we collected in tls_optimize. We edit
9855 RELOCS so that --emit-relocs will output something sensible
9856 for the final instruction stream. */
9857 tls_mask = 0;
9858 tls_gd = 0;
0d4792f7 9859 toc_symndx = 0;
d881513a 9860 if (IS_PPC64_TLS_RELOC (r_type))
411e1bfb
AM
9861 {
9862 if (h != NULL)
039b3fef 9863 tls_mask = h->tls_mask;
411e1bfb
AM
9864 else if (local_got_ents != NULL)
9865 {
e7b938ca
AM
9866 char *lgot_masks;
9867 lgot_masks = (char *) (local_got_ents + symtab_hdr->sh_info);
9868 tls_mask = lgot_masks[r_symndx];
411e1bfb 9869 }
0d4792f7
AM
9870 if (tls_mask == 0 && r_type == R_PPC64_TLS)
9871 {
9872 /* Check for toc tls entries. */
9873 char *toc_tls;
9874
9875 if (!get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9876 rel, input_bfd))
9877 return FALSE;
9878
9879 if (toc_tls)
9880 tls_mask = *toc_tls;
9881 }
9882 }
9883
9884 /* Check that tls relocs are used with tls syms, and non-tls
9885 relocs are used with non-tls syms. */
9886 if (r_symndx != 0
9887 && r_type != R_PPC64_NONE
9888 && (h == NULL
039b3fef
AM
9889 || h->elf.root.type == bfd_link_hash_defined
9890 || h->elf.root.type == bfd_link_hash_defweak)
0d4792f7
AM
9891 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
9892 {
9893 if (r_type == R_PPC64_TLS && tls_mask != 0)
9894 /* R_PPC64_TLS is OK against a symbol in the TOC. */
9895 ;
9896 else
9897 (*_bfd_error_handler)
9898 (sym_type == STT_TLS
d003868e
AM
9899 ? _("%B(%A+0x%lx): %s used with TLS symbol %s")
9900 : _("%B(%A+0x%lx): %s used with non-TLS symbol %s"),
9901 input_bfd,
9902 input_section,
0d4792f7
AM
9903 (long) rel->r_offset,
9904 ppc64_elf_howto_table[r_type]->name,
9905 sym_name);
411e1bfb
AM
9906 }
9907
9908 /* Ensure reloc mapping code below stays sane. */
9909 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
9910 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
9911 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
9912 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
9913 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
9914 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
9915 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
9916 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
9917 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
9918 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
9919 abort ();
0d4792f7 9920
411e1bfb
AM
9921 switch (r_type)
9922 {
9923 default:
411e1bfb
AM
9924 break;
9925
9926 case R_PPC64_TOC16:
9927 case R_PPC64_TOC16_LO:
9928 case R_PPC64_TOC16_DS:
9929 case R_PPC64_TOC16_LO_DS:
411e1bfb
AM
9930 {
9931 /* Check for toc tls entries. */
9932 char *toc_tls;
951fd09b 9933 int retval;
411e1bfb 9934
0d4792f7
AM
9935 retval = get_tls_mask (&toc_tls, &toc_symndx, &local_syms,
9936 rel, input_bfd);
951fd09b 9937 if (retval == 0)
411e1bfb
AM
9938 return FALSE;
9939
9940 if (toc_tls)
9941 {
951fd09b 9942 tls_mask = *toc_tls;
411e1bfb
AM
9943 if (r_type == R_PPC64_TOC16_DS
9944 || r_type == R_PPC64_TOC16_LO_DS)
81407a69
AM
9945 {
9946 if (tls_mask != 0
9947 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
9948 goto toctprel;
9949 }
411e1bfb 9950 else
951fd09b
AM
9951 {
9952 /* If we found a GD reloc pair, then we might be
9953 doing a GD->IE transition. */
9954 if (retval == 2)
9955 {
9956 tls_gd = TLS_TPRELGD;
9957 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
9958 goto tls_get_addr_check;
9959 }
9960 else if (retval == 3)
9961 {
9962 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
9963 goto tls_get_addr_check;
9964 }
9965 }
411e1bfb
AM
9966 }
9967 }
9968 break;
9969
9970 case R_PPC64_GOT_TPREL16_DS:
9971 case R_PPC64_GOT_TPREL16_LO_DS:
951fd09b
AM
9972 if (tls_mask != 0
9973 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 9974 {
81407a69 9975 toctprel:
4fe5ca5b 9976 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
411e1bfb
AM
9977 insn &= 31 << 21;
9978 insn |= 0x3c0d0000; /* addis 0,13,0 */
4fe5ca5b 9979 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
411e1bfb 9980 r_type = R_PPC64_TPREL16_HA;
0d4792f7
AM
9981 if (toc_symndx != 0)
9982 {
9983 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
9984 /* We changed the symbol. Start over in order to
9985 get h, sym, sec etc. right. */
9986 rel--;
9987 continue;
9988 }
9989 else
9990 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
9991 }
9992 break;
9993
9994 case R_PPC64_TLS:
951fd09b
AM
9995 if (tls_mask != 0
9996 && (tls_mask & TLS_TPREL) == 0)
411e1bfb 9997 {
50bc7936 9998 bfd_vma rtra;
411e1bfb 9999 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
772119ce 10000 if ((insn & ((0x3f << 26) | (31 << 11)))
411e1bfb
AM
10001 == ((31 << 26) | (13 << 11)))
10002 rtra = insn & ((1 << 26) - (1 << 16));
772119ce 10003 else if ((insn & ((0x3f << 26) | (31 << 16)))
411e1bfb
AM
10004 == ((31 << 26) | (13 << 16)))
10005 rtra = (insn & (31 << 21)) | ((insn & (31 << 11)) << 5);
10006 else
10007 abort ();
10008 if ((insn & ((1 << 11) - (1 << 1))) == 266 << 1)
10009 /* add -> addi. */
10010 insn = 14 << 26;
10011 else if ((insn & (31 << 1)) == 23 << 1
10012 && ((insn & (31 << 6)) < 14 << 6
10013 || ((insn & (31 << 6)) >= 16 << 6
10014 && (insn & (31 << 6)) < 24 << 6)))
10015 /* load and store indexed -> dform. */
10016 insn = (32 | ((insn >> 6) & 31)) << 26;
10017 else if ((insn & (31 << 1)) == 21 << 1
10018 && (insn & (0x1a << 6)) == 0)
10019 /* ldx, ldux, stdx, stdux -> ld, ldu, std, stdu. */
10020 insn = (((58 | ((insn >> 6) & 4)) << 26)
10021 | ((insn >> 6) & 1));
10022 else if ((insn & (31 << 1)) == 21 << 1
10023 && (insn & ((1 << 11) - (1 << 1))) == 341 << 1)
10024 /* lwax -> lwa. */
10025 insn = (58 << 26) | 2;
10026 else
10027 abort ();
10028 insn |= rtra;
10029 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
411e1bfb 10030 /* Was PPC64_TLS which sits on insn boundary, now
4fe5ca5b
GM
10031 PPC64_TPREL16_LO which is at low-order half-word. */
10032 rel->r_offset += d_offset;
0d4792f7
AM
10033 r_type = R_PPC64_TPREL16_LO;
10034 if (toc_symndx != 0)
10035 {
10036 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
10037 /* We changed the symbol. Start over in order to
10038 get h, sym, sec etc. right. */
10039 rel--;
10040 continue;
10041 }
10042 else
10043 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
10044 }
10045 break;
10046
411e1bfb
AM
10047 case R_PPC64_GOT_TLSGD16_HI:
10048 case R_PPC64_GOT_TLSGD16_HA:
951fd09b
AM
10049 tls_gd = TLS_TPRELGD;
10050 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10051 goto tls_gdld_hi;
10052 break;
10053
411e1bfb
AM
10054 case R_PPC64_GOT_TLSLD16_HI:
10055 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 10056 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
411e1bfb 10057 {
951fd09b
AM
10058 tls_gdld_hi:
10059 if ((tls_mask & tls_gd) != 0)
10060 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10061 + R_PPC64_GOT_TPREL16_DS);
10062 else
411e1bfb 10063 {
951fd09b 10064 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
4fe5ca5b 10065 rel->r_offset -= d_offset;
951fd09b 10066 r_type = R_PPC64_NONE;
411e1bfb 10067 }
951fd09b 10068 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
10069 }
10070 break;
10071
951fd09b
AM
10072 case R_PPC64_GOT_TLSGD16:
10073 case R_PPC64_GOT_TLSGD16_LO:
10074 tls_gd = TLS_TPRELGD;
10075 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
10076 goto tls_get_addr_check;
10077 break;
411e1bfb 10078
951fd09b
AM
10079 case R_PPC64_GOT_TLSLD16:
10080 case R_PPC64_GOT_TLSLD16_LO:
10081 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
10082 {
10083 tls_get_addr_check:
10084 if (rel + 1 < relend)
411e1bfb 10085 {
951fd09b
AM
10086 enum elf_ppc64_reloc_type r_type2;
10087 unsigned long r_symndx2;
10088 struct elf_link_hash_entry *h2;
10089 bfd_vma insn1, insn2, insn3;
10090 bfd_vma offset;
10091
10092 /* The next instruction should be a call to
10093 __tls_get_addr. Peek at the reloc to be sure. */
4ce794b7 10094 r_type2 = ELF64_R_TYPE (rel[1].r_info);
951fd09b
AM
10095 r_symndx2 = ELF64_R_SYM (rel[1].r_info);
10096 if (r_symndx2 < symtab_hdr->sh_info
10097 || (r_type2 != R_PPC64_REL14
10098 && r_type2 != R_PPC64_REL14_BRTAKEN
10099 && r_type2 != R_PPC64_REL14_BRNTAKEN
10100 && r_type2 != R_PPC64_REL24))
10101 break;
10102
10103 h2 = sym_hashes[r_symndx2 - symtab_hdr->sh_info];
10104 while (h2->root.type == bfd_link_hash_indirect
10105 || h2->root.type == bfd_link_hash_warning)
10106 h2 = (struct elf_link_hash_entry *) h2->root.u.i.link;
8387904d
AM
10107 if (h2 == NULL || (h2 != &htab->tls_get_addr->elf
10108 && h2 != &htab->tls_get_addr_fd->elf))
951fd09b
AM
10109 break;
10110
10111 /* OK, it checks out. Replace the call. */
10112 offset = rel[1].r_offset;
10113 insn1 = bfd_get_32 (output_bfd,
4fe5ca5b 10114 contents + rel->r_offset - d_offset);
951fd09b
AM
10115 insn3 = bfd_get_32 (output_bfd,
10116 contents + offset + 4);
10117 if ((tls_mask & tls_gd) != 0)
411e1bfb 10118 {
951fd09b
AM
10119 /* IE */
10120 insn1 &= (1 << 26) - (1 << 2);
10121 insn1 |= 58 << 26; /* ld */
10122 insn2 = 0x7c636a14; /* add 3,3,13 */
10123 rel[1].r_info = ELF64_R_INFO (r_symndx2, R_PPC64_NONE);
10124 if ((tls_mask & TLS_EXPLICIT) == 0)
10125 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
10126 + R_PPC64_GOT_TPREL16_DS);
10127 else
10128 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
10129 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb
AM
10130 }
10131 else
951fd09b
AM
10132 {
10133 /* LE */
10134 insn1 = 0x3c6d0000; /* addis 3,13,0 */
10135 insn2 = 0x38630000; /* addi 3,3,0 */
10136 if (tls_gd == 0)
10137 {
10138 /* Was an LD reloc. */
10139 r_symndx = 0;
e1918d23
AM
10140 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
10141 rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
951fd09b 10142 }
0d4792f7
AM
10143 else if (toc_symndx != 0)
10144 r_symndx = toc_symndx;
951fd09b
AM
10145 r_type = R_PPC64_TPREL16_HA;
10146 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10147 rel[1].r_info = ELF64_R_INFO (r_symndx,
10148 R_PPC64_TPREL16_LO);
4fe5ca5b 10149 rel[1].r_offset += d_offset;
951fd09b
AM
10150 }
10151 if (insn3 == NOP
10152 || insn3 == CROR_151515 || insn3 == CROR_313131)
10153 {
10154 insn3 = insn2;
10155 insn2 = NOP;
10156 rel[1].r_offset += 4;
10157 }
4fe5ca5b 10158 bfd_put_32 (output_bfd, insn1, contents + rel->r_offset - d_offset);
951fd09b
AM
10159 bfd_put_32 (output_bfd, insn2, contents + offset);
10160 bfd_put_32 (output_bfd, insn3, contents + offset + 4);
0d4792f7 10161 if (tls_gd == 0 || toc_symndx != 0)
951fd09b 10162 {
0d4792f7
AM
10163 /* We changed the symbol. Start over in order
10164 to get h, sym, sec etc. right. */
951fd09b
AM
10165 rel--;
10166 continue;
10167 }
411e1bfb 10168 }
411e1bfb 10169 }
411e1bfb
AM
10170 break;
10171
10172 case R_PPC64_DTPMOD64:
951fd09b
AM
10173 if (rel + 1 < relend
10174 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
10175 && rel[1].r_offset == rel->r_offset + 8)
411e1bfb 10176 {
951fd09b
AM
10177 if ((tls_mask & TLS_GD) == 0)
10178 {
10179 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
10180 if ((tls_mask & TLS_TPRELGD) != 0)
10181 r_type = R_PPC64_TPREL64;
10182 else
10183 {
4ce794b7 10184 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
951fd09b
AM
10185 r_type = R_PPC64_NONE;
10186 }
10187 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10188 }
10189 }
10190 else
10191 {
10192 if ((tls_mask & TLS_LD) == 0)
411e1bfb 10193 {
4ce794b7 10194 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
411e1bfb 10195 r_type = R_PPC64_NONE;
951fd09b 10196 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
411e1bfb 10197 }
411e1bfb
AM
10198 }
10199 break;
10200
10201 case R_PPC64_TPREL64:
951fd09b 10202 if ((tls_mask & TLS_TPREL) == 0)
411e1bfb
AM
10203 {
10204 r_type = R_PPC64_NONE;
10205 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
10206 }
10207 break;
10208 }
10209
10210 /* Handle other relocations that tweak non-addend part of insn. */
86c76c7b 10211 insn = 0;
b25116a9
AM
10212 max_br_offset = 1 << 25;
10213 addend = rel->r_addend;
65f38f15 10214 switch (r_type)
5bd4f169
AM
10215 {
10216 default:
65f38f15 10217 break;
5bd4f169 10218
65f38f15
AM
10219 /* Branch taken prediction relocations. */
10220 case R_PPC64_ADDR14_BRTAKEN:
10221 case R_PPC64_REL14_BRTAKEN:
cedb70c5
KH
10222 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
10223 /* Fall thru. */
65f38f15 10224
86c76c7b 10225 /* Branch not taken prediction relocations. */
65f38f15
AM
10226 case R_PPC64_ADDR14_BRNTAKEN:
10227 case R_PPC64_REL14_BRNTAKEN:
411e1bfb
AM
10228 insn |= bfd_get_32 (output_bfd,
10229 contents + rel->r_offset) & ~(0x01 << 21);
b25116a9 10230 /* Fall thru. */
86c76c7b 10231
b25116a9
AM
10232 case R_PPC64_REL14:
10233 max_br_offset = 1 << 15;
10234 /* Fall thru. */
5bd4f169 10235
65f38f15 10236 case R_PPC64_REL24:
ad8e1ba5
AM
10237 /* Calls to functions with a different TOC, such as calls to
10238 shared objects, need to alter the TOC pointer. This is
10239 done using a linkage stub. A REL24 branching to these
10240 linkage stubs needs to be followed by a nop, as the nop
10241 will be replaced with an instruction to restore the TOC
10242 base pointer. */
b25116a9 10243 stub_entry = NULL;
8387904d 10244 fdh = h;
ad8e1ba5 10245 if (((h != NULL
039b3fef
AM
10246 && (((fdh = h->oh) != NULL
10247 && fdh->elf.plt.plist != NULL)
10248 || (fdh = h)->elf.plt.plist != NULL))
8387904d 10249 || (sec != NULL
ad8e1ba5 10250 && sec->output_section != NULL
b25116a9 10251 && sec->id <= htab->top_id
ad8e1ba5
AM
10252 && (htab->stub_group[sec->id].toc_off
10253 != htab->stub_group[input_section->id].toc_off)))
721956f4 10254 && (stub_entry = ppc_get_stub_entry (input_section, sec, fdh,
ad8e1ba5
AM
10255 rel, htab)) != NULL
10256 && (stub_entry->stub_type == ppc_stub_plt_call
10257 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
10258 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
41bd81ab 10259 {
b25116a9 10260 bfd_boolean can_plt_call = FALSE;
721956f4 10261
eea6121a 10262 if (rel->r_offset + 8 <= input_section->size)
41bd81ab 10263 {
b25116a9
AM
10264 unsigned long nop;
10265 nop = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
10266 if (nop == NOP
10267 || nop == CROR_151515 || nop == CROR_313131)
41bd81ab 10268 {
4ce794b7 10269 bfd_put_32 (input_bfd, LD_R2_40R1,
411e1bfb 10270 contents + rel->r_offset + 4);
b25116a9 10271 can_plt_call = TRUE;
41bd81ab 10272 }
5bd4f169 10273 }
721956f4
AM
10274
10275 if (!can_plt_call)
10276 {
ad8e1ba5
AM
10277 if (stub_entry->stub_type == ppc_stub_plt_call)
10278 {
10279 /* If this is a plain branch rather than a branch
4fa1c237
AM
10280 and link, don't require a nop. However, don't
10281 allow tail calls in a shared library as they
10282 will result in r2 being corrupted. */
b25116a9
AM
10283 unsigned long br;
10284 br = bfd_get_32 (input_bfd, contents + rel->r_offset);
4fa1c237 10285 if (info->executable && (br & 1) == 0)
b25116a9 10286 can_plt_call = TRUE;
15b552e9
AM
10287 else
10288 stub_entry = NULL;
ad8e1ba5 10289 }
6ab189d5 10290 else if (h != NULL
039b3fef 10291 && strcmp (h->elf.root.root.string,
6ab189d5
AM
10292 ".__libc_start_main") == 0)
10293 {
10294 /* Allow crt1 branch to go via a toc adjusting stub. */
b25116a9 10295 can_plt_call = TRUE;
6ab189d5 10296 }
ad8e1ba5
AM
10297 else
10298 {
10299 if (strcmp (input_section->output_section->name,
10300 ".init") == 0
10301 || strcmp (input_section->output_section->name,
10302 ".fini") == 0)
10303 (*_bfd_error_handler)
d003868e 10304 (_("%B(%A+0x%lx): automatic multiple TOCs "
ad8e1ba5
AM
10305 "not supported using your crt files; "
10306 "recompile with -mminimal-toc or upgrade gcc"),
d003868e
AM
10307 input_bfd,
10308 input_section,
ad8e1ba5
AM
10309 (long) rel->r_offset);
10310 else
10311 (*_bfd_error_handler)
d003868e 10312 (_("%B(%A+0x%lx): sibling call optimization to `%s' "
ad8e1ba5
AM
10313 "does not allow automatic multiple TOCs; "
10314 "recompile with -mminimal-toc or "
10315 "-fno-optimize-sibling-calls, "
10316 "or make `%s' extern"),
d003868e
AM
10317 input_bfd,
10318 input_section,
ad8e1ba5
AM
10319 (long) rel->r_offset,
10320 sym_name,
10321 sym_name);
10322 bfd_set_error (bfd_error_bad_value);
10323 ret = FALSE;
10324 }
721956f4
AM
10325 }
10326
b25116a9
AM
10327 if (can_plt_call
10328 && stub_entry->stub_type == ppc_stub_plt_call)
10329 unresolved_reloc = FALSE;
10330 }
10331
8387904d
AM
10332 if (stub_entry == NULL
10333 && get_opd_info (sec) != NULL)
10334 {
10335 /* The branch destination is the value of the opd entry. */
4cc603a5
AM
10336 bfd_vma off = (relocation + addend
10337 - sec->output_section->vma
10338 - sec->output_offset);
8387904d
AM
10339 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL);
10340 if (dest != (bfd_vma) -1)
10341 {
10342 relocation = dest;
10343 addend = 0;
10344 }
10345 }
10346
b25116a9
AM
10347 /* If the branch is out of reach we ought to have a long
10348 branch stub. */
10349 from = (rel->r_offset
10350 + input_section->output_offset
10351 + input_section->output_section->vma);
10352
10353 if (stub_entry == NULL
4cc603a5 10354 && (relocation + addend - from + max_br_offset
b25116a9
AM
10355 >= 2 * max_br_offset)
10356 && r_type != R_PPC64_ADDR14_BRTAKEN
10357 && r_type != R_PPC64_ADDR14_BRNTAKEN)
039b3fef
AM
10358 stub_entry = ppc_get_stub_entry (input_section, sec, h, rel,
10359 htab);
b25116a9
AM
10360
10361 if (stub_entry != NULL)
10362 {
10363 /* Munge up the value and addend so that we call the stub
10364 rather than the procedure directly. */
10365 relocation = (stub_entry->stub_offset
10366 + stub_entry->stub_sec->output_offset
10367 + stub_entry->stub_sec->output_section->vma);
10368 addend = 0;
10369 }
10370
10371 if (insn != 0)
10372 {
10373 if (is_power4)
721956f4 10374 {
b25116a9
AM
10375 /* Set 'a' bit. This is 0b00010 in BO field for branch
10376 on CR(BI) insns (BO == 001at or 011at), and 0b01000
10377 for branch on CTR insns (BO == 1a00t or 1a01t). */
10378 if ((insn & (0x14 << 21)) == (0x04 << 21))
10379 insn |= 0x02 << 21;
10380 else if ((insn & (0x14 << 21)) == (0x10 << 21))
10381 insn |= 0x08 << 21;
10382 else
10383 break;
10384 }
10385 else
10386 {
10387 /* Invert 'y' bit if not the default. */
4cc603a5 10388 if ((bfd_signed_vma) (relocation + addend - from) < 0)
b25116a9 10389 insn ^= 0x01 << 21;
721956f4 10390 }
b25116a9
AM
10391
10392 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
5bd4f169 10393 }
e86ce104 10394
06da1e8e
AM
10395 /* NOP out calls to undefined weak functions.
10396 We can thus call a weak function without first
10397 checking whether the function is defined. */
b25116a9 10398 else if (h != NULL
039b3fef 10399 && h->elf.root.type == bfd_link_hash_undefweak
b25116a9
AM
10400 && r_type == R_PPC64_REL24
10401 && relocation == 0
4cc603a5 10402 && addend == 0)
e86ce104 10403 {
06da1e8e
AM
10404 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
10405 continue;
e86ce104 10406 }
65f38f15
AM
10407 break;
10408 }
5bd4f169 10409
65f38f15 10410 /* Set `addend'. */
411e1bfb 10411 tls_type = 0;
65f38f15
AM
10412 switch (r_type)
10413 {
10414 default:
10415 (*_bfd_error_handler)
d003868e
AM
10416 (_("%B: unknown relocation type %d for symbol %s"),
10417 input_bfd, (int) r_type, sym_name);
5bd4f169 10418
65f38f15 10419 bfd_set_error (bfd_error_bad_value);
b34976b6 10420 ret = FALSE;
65f38f15 10421 continue;
5bd4f169 10422
65f38f15 10423 case R_PPC64_NONE:
411e1bfb 10424 case R_PPC64_TLS:
04c9666a
AM
10425 case R_PPC64_GNU_VTINHERIT:
10426 case R_PPC64_GNU_VTENTRY:
65f38f15 10427 continue;
5bd4f169
AM
10428
10429 /* GOT16 relocations. Like an ADDR16 using the symbol's
10430 address in the GOT as relocation value instead of the
411e1bfb 10431 symbol's value itself. Also, create a GOT entry for the
5bd4f169 10432 symbol and put the symbol value there. */
411e1bfb
AM
10433 case R_PPC64_GOT_TLSGD16:
10434 case R_PPC64_GOT_TLSGD16_LO:
10435 case R_PPC64_GOT_TLSGD16_HI:
10436 case R_PPC64_GOT_TLSGD16_HA:
951fd09b 10437 tls_type = TLS_TLS | TLS_GD;
411e1bfb
AM
10438 goto dogot;
10439
10440 case R_PPC64_GOT_TLSLD16:
10441 case R_PPC64_GOT_TLSLD16_LO:
10442 case R_PPC64_GOT_TLSLD16_HI:
10443 case R_PPC64_GOT_TLSLD16_HA:
951fd09b 10444 tls_type = TLS_TLS | TLS_LD;
411e1bfb
AM
10445 goto dogot;
10446
10447 case R_PPC64_GOT_TPREL16_DS:
10448 case R_PPC64_GOT_TPREL16_LO_DS:
10449 case R_PPC64_GOT_TPREL16_HI:
10450 case R_PPC64_GOT_TPREL16_HA:
10451 tls_type = TLS_TLS | TLS_TPREL;
10452 goto dogot;
10453
10454 case R_PPC64_GOT_DTPREL16_DS:
10455 case R_PPC64_GOT_DTPREL16_LO_DS:
10456 case R_PPC64_GOT_DTPREL16_HI:
10457 case R_PPC64_GOT_DTPREL16_HA:
10458 tls_type = TLS_TLS | TLS_DTPREL;
10459 goto dogot;
10460
65f38f15
AM
10461 case R_PPC64_GOT16:
10462 case R_PPC64_GOT16_LO:
10463 case R_PPC64_GOT16_HI:
10464 case R_PPC64_GOT16_HA:
10465 case R_PPC64_GOT16_DS:
10466 case R_PPC64_GOT16_LO_DS:
411e1bfb 10467 dogot:
5bd4f169
AM
10468 {
10469 /* Relocation is to the entry for this symbol in the global
10470 offset table. */
e717da7e 10471 asection *got;
d881513a 10472 bfd_vma *offp;
5bd4f169 10473 bfd_vma off;
d881513a 10474 unsigned long indx = 0;
65f38f15 10475
d881513a
AM
10476 if (tls_type == (TLS_TLS | TLS_LD)
10477 && (h == NULL
f5385ebf 10478 || !h->elf.def_dynamic))
e717da7e 10479 offp = &ppc64_tlsld_got (input_bfd)->offset;
411e1bfb 10480 else
5bd4f169 10481 {
d881513a 10482 struct got_entry *ent;
5bd4f169 10483
d881513a
AM
10484 if (h != NULL)
10485 {
10486 bfd_boolean dyn = htab->elf.dynamic_sections_created;
039b3fef
AM
10487 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
10488 &h->elf)
d881513a 10489 || (info->shared
039b3fef 10490 && SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
d881513a
AM
10491 /* This is actually a static link, or it is a
10492 -Bsymbolic link and the symbol is defined
10493 locally, or the symbol was forced to be local
10494 because of a version file. */
10495 ;
10496 else
10497 {
039b3fef 10498 indx = h->elf.dynindx;
d881513a
AM
10499 unresolved_reloc = FALSE;
10500 }
039b3fef 10501 ent = h->elf.got.glist;
d881513a 10502 }
411e1bfb 10503 else
5bd4f169 10504 {
d881513a
AM
10505 if (local_got_ents == NULL)
10506 abort ();
10507 ent = local_got_ents[r_symndx];
5bd4f169 10508 }
d881513a
AM
10509
10510 for (; ent != NULL; ent = ent->next)
4cc603a5 10511 if (ent->addend == orig_addend
e717da7e 10512 && ent->owner == input_bfd
d881513a
AM
10513 && ent->tls_type == tls_type)
10514 break;
10515 if (ent == NULL)
10516 abort ();
10517 offp = &ent->got.offset;
5bd4f169 10518 }
411e1bfb 10519
e717da7e
AM
10520 got = ppc64_elf_tdata (input_bfd)->got;
10521 if (got == NULL)
10522 abort ();
10523
411e1bfb
AM
10524 /* The offset must always be a multiple of 8. We use the
10525 least significant bit to record whether we have already
10526 processed this entry. */
d881513a 10527 off = *offp;
411e1bfb
AM
10528 if ((off & 1) != 0)
10529 off &= ~1;
5bd4f169
AM
10530 else
10531 {
411e1bfb
AM
10532 /* Generate relocs for the dynamic linker, except in
10533 the case of TLSLD where we'll use one entry per
10534 module. */
e717da7e
AM
10535 asection *relgot = ppc64_elf_tdata (input_bfd)->relgot;
10536
d881513a 10537 *offp = off | 1;
4e795f50
AM
10538 if ((info->shared || indx != 0)
10539 && (h == NULL
039b3fef
AM
10540 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10541 || h->elf.root.type != bfd_link_hash_undefweak))
5bd4f169 10542 {
e717da7e
AM
10543 outrel.r_offset = (got->output_section->vma
10544 + got->output_offset
411e1bfb 10545 + off);
4cc603a5 10546 outrel.r_addend = addend;
d881513a 10547 if (tls_type & (TLS_LD | TLS_GD))
5bd4f169 10548 {
411e1bfb 10549 outrel.r_addend = 0;
e515b051 10550 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
d881513a
AM
10551 if (tls_type == (TLS_TLS | TLS_GD))
10552 {
e717da7e
AM
10553 loc = relgot->contents;
10554 loc += (relgot->reloc_count++
d881513a
AM
10555 * sizeof (Elf64_External_Rela));
10556 bfd_elf64_swap_reloca_out (output_bfd,
10557 &outrel, loc);
e515b051 10558 outrel.r_offset += 8;
4cc603a5 10559 outrel.r_addend = addend;
d881513a
AM
10560 outrel.r_info
10561 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
d881513a 10562 }
411e1bfb 10563 }
951fd09b 10564 else if (tls_type == (TLS_TLS | TLS_DTPREL))
411e1bfb 10565 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
951fd09b 10566 else if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb
AM
10567 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
10568 else if (indx == 0)
81407a69
AM
10569 {
10570 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_RELATIVE);
10571
10572 /* Write the .got section contents for the sake
10573 of prelink. */
e717da7e 10574 loc = got->contents + off;
23fbd6fa
JJ
10575 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
10576 loc);
81407a69 10577 }
411e1bfb
AM
10578 else
10579 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
81407a69
AM
10580
10581 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
e515b051
AM
10582 {
10583 outrel.r_addend += relocation;
10584 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
e1918d23 10585 outrel.r_addend -= htab->elf.tls_sec->vma;
e515b051 10586 }
e717da7e
AM
10587 loc = relgot->contents;
10588 loc += (relgot->reloc_count++
411e1bfb
AM
10589 * sizeof (Elf64_External_Rela));
10590 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10591 }
10592
ad8e1ba5 10593 /* Init the .got section contents here if we're not
81407a69 10594 emitting a reloc. */
d881513a 10595 else
411e1bfb 10596 {
4cc603a5 10597 relocation += addend;
7b609f53
AM
10598 if (tls_type == (TLS_TLS | TLS_LD))
10599 relocation = 1;
10600 else if (tls_type != 0)
411e1bfb 10601 {
e1918d23 10602 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
7b609f53 10603 if (tls_type == (TLS_TLS | TLS_TPREL))
411e1bfb 10604 relocation += DTP_OFFSET - TP_OFFSET;
5bd4f169 10605
7b609f53
AM
10606 if (tls_type == (TLS_TLS | TLS_GD))
10607 {
10608 bfd_put_64 (output_bfd, relocation,
e717da7e 10609 got->contents + off + 8);
7b609f53
AM
10610 relocation = 1;
10611 }
411e1bfb 10612 }
7b609f53 10613
411e1bfb 10614 bfd_put_64 (output_bfd, relocation,
e717da7e 10615 got->contents + off);
5bd4f169
AM
10616 }
10617 }
10618
65f38f15
AM
10619 if (off >= (bfd_vma) -2)
10620 abort ();
10621
e717da7e 10622 relocation = got->output_offset + off;
65f38f15 10623
5bd4f169 10624 /* TOC base (r2) is TOC start plus 0x8000. */
e717da7e 10625 addend = -TOC_BASE_OFF;
5bd4f169 10626 }
65f38f15
AM
10627 break;
10628
10629 case R_PPC64_PLT16_HA:
10630 case R_PPC64_PLT16_HI:
10631 case R_PPC64_PLT16_LO:
10632 case R_PPC64_PLT32:
10633 case R_PPC64_PLT64:
10634 /* Relocation is to the entry for this symbol in the
10635 procedure linkage table. */
10636
10637 /* Resolve a PLT reloc against a local symbol directly,
10638 without using the procedure linkage table. */
10639 if (h == NULL)
10640 break;
10641
411e1bfb
AM
10642 /* It's possible that we didn't make a PLT entry for this
10643 symbol. This happens when statically linking PIC code,
10644 or when using -Bsymbolic. Go find a match if there is a
10645 PLT entry. */
4ce794b7 10646 if (htab->plt != NULL)
65f38f15 10647 {
411e1bfb 10648 struct plt_entry *ent;
039b3fef 10649 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
4cc603a5 10650 if (ent->addend == orig_addend
411e1bfb
AM
10651 && ent->plt.offset != (bfd_vma) -1)
10652 {
4ce794b7
AM
10653 relocation = (htab->plt->output_section->vma
10654 + htab->plt->output_offset
411e1bfb
AM
10655 + ent->plt.offset);
10656 unresolved_reloc = FALSE;
10657 }
65f38f15 10658 }
65f38f15 10659 break;
5bd4f169 10660
0b13192e
AM
10661 case R_PPC64_TOC:
10662 /* Relocation value is TOC base. */
10663 relocation = TOCstart;
10664 if (r_symndx == 0)
10665 relocation += htab->stub_group[input_section->id].toc_off;
8517fae7
AM
10666 else if (unresolved_reloc)
10667 ;
10668 else if (sec != NULL && sec->id <= htab->top_id)
0b13192e
AM
10669 relocation += htab->stub_group[sec->id].toc_off;
10670 else
10671 unresolved_reloc = TRUE;
10672 goto dodyn2;
10673
5bd4f169
AM
10674 /* TOC16 relocs. We want the offset relative to the TOC base,
10675 which is the address of the start of the TOC plus 0x8000.
10676 The TOC consists of sections .got, .toc, .tocbss, and .plt,
10677 in this order. */
65f38f15
AM
10678 case R_PPC64_TOC16:
10679 case R_PPC64_TOC16_LO:
10680 case R_PPC64_TOC16_HI:
10681 case R_PPC64_TOC16_DS:
10682 case R_PPC64_TOC16_LO_DS:
10683 case R_PPC64_TOC16_HA:
ad8e1ba5 10684 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
5bd4f169
AM
10685 break;
10686
10687 /* Relocate against the beginning of the section. */
65f38f15
AM
10688 case R_PPC64_SECTOFF:
10689 case R_PPC64_SECTOFF_LO:
10690 case R_PPC64_SECTOFF_HI:
10691 case R_PPC64_SECTOFF_DS:
10692 case R_PPC64_SECTOFF_LO_DS:
10693 case R_PPC64_SECTOFF_HA:
4ce794b7 10694 if (sec != NULL)
65f38f15 10695 addend -= sec->output_section->vma;
5bd4f169
AM
10696 break;
10697
721956f4
AM
10698 case R_PPC64_REL14:
10699 case R_PPC64_REL14_BRNTAKEN:
10700 case R_PPC64_REL14_BRTAKEN:
5d1634d7
AM
10701 case R_PPC64_REL24:
10702 break;
10703
411e1bfb
AM
10704 case R_PPC64_TPREL16:
10705 case R_PPC64_TPREL16_LO:
10706 case R_PPC64_TPREL16_HI:
10707 case R_PPC64_TPREL16_HA:
10708 case R_PPC64_TPREL16_DS:
10709 case R_PPC64_TPREL16_LO_DS:
10710 case R_PPC64_TPREL16_HIGHER:
10711 case R_PPC64_TPREL16_HIGHERA:
10712 case R_PPC64_TPREL16_HIGHEST:
10713 case R_PPC64_TPREL16_HIGHESTA:
e1918d23 10714 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
10715 if (info->shared)
10716 /* The TPREL16 relocs shouldn't really be used in shared
10717 libs as they will result in DT_TEXTREL being set, but
10718 support them anyway. */
10719 goto dodyn;
10720 break;
10721
10722 case R_PPC64_DTPREL16:
10723 case R_PPC64_DTPREL16_LO:
10724 case R_PPC64_DTPREL16_HI:
10725 case R_PPC64_DTPREL16_HA:
10726 case R_PPC64_DTPREL16_DS:
10727 case R_PPC64_DTPREL16_LO_DS:
10728 case R_PPC64_DTPREL16_HIGHER:
10729 case R_PPC64_DTPREL16_HIGHERA:
10730 case R_PPC64_DTPREL16_HIGHEST:
10731 case R_PPC64_DTPREL16_HIGHESTA:
e1918d23 10732 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
10733 break;
10734
e515b051
AM
10735 case R_PPC64_DTPMOD64:
10736 relocation = 1;
10737 addend = 0;
10738 goto dodyn;
10739
411e1bfb 10740 case R_PPC64_TPREL64:
e1918d23 10741 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
411e1bfb
AM
10742 goto dodyn;
10743
10744 case R_PPC64_DTPREL64:
e1918d23 10745 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
411e1bfb
AM
10746 /* Fall thru */
10747
65f38f15
AM
10748 /* Relocations that may need to be propagated if this is a
10749 dynamic object. */
04c9666a 10750 case R_PPC64_REL30:
65f38f15
AM
10751 case R_PPC64_REL32:
10752 case R_PPC64_REL64:
10753 case R_PPC64_ADDR14:
10754 case R_PPC64_ADDR14_BRNTAKEN:
10755 case R_PPC64_ADDR14_BRTAKEN:
10756 case R_PPC64_ADDR16:
10757 case R_PPC64_ADDR16_DS:
10758 case R_PPC64_ADDR16_HA:
10759 case R_PPC64_ADDR16_HI:
10760 case R_PPC64_ADDR16_HIGHER:
10761 case R_PPC64_ADDR16_HIGHERA:
10762 case R_PPC64_ADDR16_HIGHEST:
10763 case R_PPC64_ADDR16_HIGHESTA:
10764 case R_PPC64_ADDR16_LO:
10765 case R_PPC64_ADDR16_LO_DS:
10766 case R_PPC64_ADDR24:
65f38f15
AM
10767 case R_PPC64_ADDR32:
10768 case R_PPC64_ADDR64:
10769 case R_PPC64_UADDR16:
10770 case R_PPC64_UADDR32:
10771 case R_PPC64_UADDR64:
ec338859
AM
10772 /* r_symndx will be zero only for relocs against symbols
10773 from removed linkonce sections, or sections discarded by
10774 a linker script. */
411e1bfb 10775 dodyn:
5d1634d7 10776 if (r_symndx == 0)
b1e24c02
DJ
10777 {
10778 _bfd_clear_contents (ppc64_elf_howto_table[r_type], input_bfd,
10779 contents + rel->r_offset);
10780 break;
10781 }
5d1634d7
AM
10782 /* Fall thru. */
10783
0b13192e 10784 dodyn2:
5d1634d7 10785 if ((input_section->flags & SEC_ALLOC) == 0)
ec338859
AM
10786 break;
10787
41bd81ab
AM
10788 if (NO_OPD_RELOCS && is_opd)
10789 break;
10790
65f38f15 10791 if ((info->shared
4e795f50 10792 && (h == NULL
039b3fef
AM
10793 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
10794 || h->elf.root.type != bfd_link_hash_undefweak)
411e1bfb 10795 && (MUST_BE_DYN_RELOC (r_type)
039b3fef 10796 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
f4656909
AM
10797 || (ELIMINATE_COPY_RELOCS
10798 && !info->shared
65f38f15 10799 && h != NULL
039b3fef 10800 && h->elf.dynindx != -1
f5385ebf
AM
10801 && !h->elf.non_got_ref
10802 && h->elf.def_dynamic
10803 && !h->elf.def_regular))
65f38f15
AM
10804 {
10805 Elf_Internal_Rela outrel;
b34976b6 10806 bfd_boolean skip, relocate;
65f38f15 10807 asection *sreloc;
947216bf 10808 bfd_byte *loc;
1cf1f670 10809 bfd_vma out_off;
65f38f15
AM
10810
10811 /* When generating a dynamic object, these relocations
10812 are copied into the output file to be resolved at run
10813 time. */
10814
b34976b6
AM
10815 skip = FALSE;
10816 relocate = FALSE;
65f38f15 10817
1cf1f670
AM
10818 out_off = _bfd_elf_section_offset (output_bfd, info,
10819 input_section, rel->r_offset);
10820 if (out_off == (bfd_vma) -1)
b34976b6 10821 skip = TRUE;
1cf1f670 10822 else if (out_off == (bfd_vma) -2)
b34976b6 10823 skip = TRUE, relocate = TRUE;
1cf1f670
AM
10824 out_off += (input_section->output_section->vma
10825 + input_section->output_offset);
10826 outrel.r_offset = out_off;
411e1bfb 10827 outrel.r_addend = rel->r_addend;
65f38f15 10828
1cf1f670
AM
10829 /* Optimize unaligned reloc use. */
10830 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
10831 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
10832 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
10833 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
10834 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
10835 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
10836 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
10837 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
10838 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
10839
65f38f15 10840 if (skip)
0bb2d96a 10841 memset (&outrel, 0, sizeof outrel);
039b3fef 10842 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
0b13192e
AM
10843 && !is_opd
10844 && r_type != R_PPC64_TOC)
039b3fef 10845 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
65f38f15
AM
10846 else
10847 {
41bd81ab
AM
10848 /* This symbol is local, or marked to become local,
10849 or this is an opd section reloc which must point
10850 at a local function. */
65f38f15 10851 outrel.r_addend += relocation;
e86ce104 10852 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
65f38f15 10853 {
3fad3c7c 10854 if (is_opd && h != NULL)
afbe61cf
AM
10855 {
10856 /* Lie about opd entries. This case occurs
10857 when building shared libraries and we
10858 reference a function in another shared
3fad3c7c
AM
10859 lib. The same thing happens for a weak
10860 definition in an application that's
10861 overridden by a strong definition in a
10862 shared lib. (I believe this is a generic
10863 bug in binutils handling of weak syms.)
10864 In these cases we won't use the opd
1e2f5b6e 10865 entry in this lib. */
b34976b6 10866 unresolved_reloc = FALSE;
afbe61cf 10867 }
65f38f15 10868 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
81407a69
AM
10869
10870 /* We need to relocate .opd contents for ld.so.
10871 Prelink also wants simple and consistent rules
10872 for relocs. This make all RELATIVE relocs have
10873 *r_offset equal to r_addend. */
10874 relocate = TRUE;
65f38f15
AM
10875 }
10876 else
10877 {
10878 long indx = 0;
10879
41bd81ab 10880 if (bfd_is_abs_section (sec))
65f38f15
AM
10881 ;
10882 else if (sec == NULL || sec->owner == NULL)
10883 {
10884 bfd_set_error (bfd_error_bad_value);
b34976b6 10885 return FALSE;
65f38f15
AM
10886 }
10887 else
10888 {
10889 asection *osec;
10890
10891 osec = sec->output_section;
10892 indx = elf_section_data (osec)->dynindx;
10893
74541ad4
AM
10894 if (indx == 0)
10895 {
10896 if ((osec->flags & SEC_READONLY) == 0
10897 && htab->elf.data_index_section != NULL)
10898 osec = htab->elf.data_index_section;
10899 else
10900 osec = htab->elf.text_index_section;
10901 indx = elf_section_data (osec)->dynindx;
10902 }
10903 BFD_ASSERT (indx != 0);
10904
65f38f15
AM
10905 /* We are turning this relocation into one
10906 against a section symbol, so subtract out
10907 the output section's address but not the
10908 offset of the input section in the output
10909 section. */
10910 outrel.r_addend -= osec->vma;
10911 }
10912
10913 outrel.r_info = ELF64_R_INFO (indx, r_type);
10914 }
10915 }
10916
10917 sreloc = elf_section_data (input_section)->sreloc;
10918 if (sreloc == NULL)
10919 abort ();
10920
dfbb6ac9
AM
10921 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
10922 >= sreloc->size)
10923 abort ();
947216bf
AM
10924 loc = sreloc->contents;
10925 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15
AM
10926 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
10927
10928 /* If this reloc is against an external symbol, it will
10929 be computed at runtime, so there's no need to do
81407a69
AM
10930 anything now. However, for the sake of prelink ensure
10931 that the section contents are a known value. */
65f38f15 10932 if (! relocate)
81407a69
AM
10933 {
10934 unresolved_reloc = FALSE;
10935 /* The value chosen here is quite arbitrary as ld.so
10936 ignores section contents except for the special
10937 case of .opd where the contents might be accessed
10938 before relocation. Choose zero, as that won't
10939 cause reloc overflow. */
10940 relocation = 0;
10941 addend = 0;
10942 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
10943 to improve backward compatibility with older
10944 versions of ld. */
10945 if (r_type == R_PPC64_ADDR64)
10946 addend = outrel.r_addend;
10947 /* Adjust pc_relative relocs to have zero in *r_offset. */
4ce794b7 10948 else if (ppc64_elf_howto_table[r_type]->pc_relative)
000732f7
AM
10949 addend = (input_section->output_section->vma
10950 + input_section->output_offset
10951 + rel->r_offset);
81407a69 10952 }
65f38f15 10953 }
5bd4f169
AM
10954 break;
10955
65f38f15
AM
10956 case R_PPC64_COPY:
10957 case R_PPC64_GLOB_DAT:
10958 case R_PPC64_JMP_SLOT:
10959 case R_PPC64_RELATIVE:
10960 /* We shouldn't ever see these dynamic relocs in relocatable
10961 files. */
ae9a127f 10962 /* Fall through. */
65f38f15
AM
10963
10964 case R_PPC64_PLTGOT16:
10965 case R_PPC64_PLTGOT16_DS:
10966 case R_PPC64_PLTGOT16_HA:
10967 case R_PPC64_PLTGOT16_HI:
10968 case R_PPC64_PLTGOT16_LO:
10969 case R_PPC64_PLTGOT16_LO_DS:
10970 case R_PPC64_PLTREL32:
10971 case R_PPC64_PLTREL64:
10972 /* These ones haven't been implemented yet. */
10973
10974 (*_bfd_error_handler)
d003868e
AM
10975 (_("%B: relocation %s is not supported for symbol %s."),
10976 input_bfd,
4ce794b7 10977 ppc64_elf_howto_table[r_type]->name, sym_name);
5bd4f169
AM
10978
10979 bfd_set_error (bfd_error_invalid_operation);
b34976b6 10980 ret = FALSE;
5bd4f169 10981 continue;
65f38f15 10982 }
5bd4f169 10983
65f38f15
AM
10984 /* Do any further special processing. */
10985 switch (r_type)
10986 {
10987 default:
10988 break;
10989
10990 case R_PPC64_ADDR16_HA:
10991 case R_PPC64_ADDR16_HIGHERA:
10992 case R_PPC64_ADDR16_HIGHESTA:
65f38f15
AM
10993 case R_PPC64_TOC16_HA:
10994 case R_PPC64_SECTOFF_HA:
411e1bfb
AM
10995 case R_PPC64_TPREL16_HA:
10996 case R_PPC64_DTPREL16_HA:
411e1bfb
AM
10997 case R_PPC64_TPREL16_HIGHER:
10998 case R_PPC64_TPREL16_HIGHERA:
10999 case R_PPC64_TPREL16_HIGHEST:
11000 case R_PPC64_TPREL16_HIGHESTA:
11001 case R_PPC64_DTPREL16_HIGHER:
11002 case R_PPC64_DTPREL16_HIGHERA:
11003 case R_PPC64_DTPREL16_HIGHEST:
11004 case R_PPC64_DTPREL16_HIGHESTA:
65f38f15
AM
11005 /* It's just possible that this symbol is a weak symbol
11006 that's not actually defined anywhere. In that case,
11007 'sec' would be NULL, and we should leave the symbol
11008 alone (it will be set to zero elsewhere in the link). */
5c5f6e17
AM
11009 if (sec == NULL)
11010 break;
11011 /* Fall thru */
11012
11013 case R_PPC64_GOT16_HA:
11014 case R_PPC64_PLTGOT16_HA:
11015 case R_PPC64_PLT16_HA:
11016 case R_PPC64_GOT_TLSGD16_HA:
11017 case R_PPC64_GOT_TLSLD16_HA:
11018 case R_PPC64_GOT_TPREL16_HA:
11019 case R_PPC64_GOT_DTPREL16_HA:
11020 /* Add 0x10000 if sign bit in 0:15 is set.
11021 Bits 0:15 are not used. */
11022 addend += 0x8000;
65f38f15
AM
11023 break;
11024
11025 case R_PPC64_ADDR16_DS:
11026 case R_PPC64_ADDR16_LO_DS:
11027 case R_PPC64_GOT16_DS:
11028 case R_PPC64_GOT16_LO_DS:
11029 case R_PPC64_PLT16_LO_DS:
11030 case R_PPC64_SECTOFF_DS:
11031 case R_PPC64_SECTOFF_LO_DS:
11032 case R_PPC64_TOC16_DS:
11033 case R_PPC64_TOC16_LO_DS:
11034 case R_PPC64_PLTGOT16_DS:
11035 case R_PPC64_PLTGOT16_LO_DS:
411e1bfb
AM
11036 case R_PPC64_GOT_TPREL16_DS:
11037 case R_PPC64_GOT_TPREL16_LO_DS:
11038 case R_PPC64_GOT_DTPREL16_DS:
11039 case R_PPC64_GOT_DTPREL16_LO_DS:
11040 case R_PPC64_TPREL16_DS:
11041 case R_PPC64_TPREL16_LO_DS:
11042 case R_PPC64_DTPREL16_DS:
11043 case R_PPC64_DTPREL16_LO_DS:
adadcc0c
AM
11044 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
11045 mask = 3;
11046 /* If this reloc is against an lq insn, then the value must be
11047 a multiple of 16. This is somewhat of a hack, but the
11048 "correct" way to do this by defining _DQ forms of all the
11049 _DS relocs bloats all reloc switches in this file. It
11050 doesn't seem to make much sense to use any of these relocs
11051 in data, so testing the insn should be safe. */
494dac0c 11052 if ((insn & (0x3f << 26)) == (56u << 26))
adadcc0c
AM
11053 mask = 15;
11054 if (((relocation + addend) & mask) != 0)
65f38f15
AM
11055 {
11056 (*_bfd_error_handler)
d003868e
AM
11057 (_("%B: error: relocation %s not a multiple of %d"),
11058 input_bfd,
4ce794b7 11059 ppc64_elf_howto_table[r_type]->name,
adadcc0c 11060 mask + 1);
65f38f15 11061 bfd_set_error (bfd_error_bad_value);
b34976b6 11062 ret = FALSE;
65f38f15
AM
11063 continue;
11064 }
11065 break;
5bd4f169
AM
11066 }
11067
239e1f3a
AM
11068 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
11069 because such sections are not SEC_ALLOC and thus ld.so will
11070 not process them. */
65f38f15 11071 if (unresolved_reloc
239e1f3a 11072 && !((input_section->flags & SEC_DEBUGGING) != 0
f5385ebf 11073 && h->elf.def_dynamic))
9c07fe7c
AM
11074 {
11075 (*_bfd_error_handler)
d003868e
AM
11076 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
11077 input_bfd,
11078 input_section,
9c07fe7c 11079 (long) rel->r_offset,
7b609f53 11080 ppc64_elf_howto_table[(int) r_type]->name,
039b3fef 11081 h->elf.root.root.string);
b34976b6 11082 ret = FALSE;
9c07fe7c 11083 }
5bd4f169 11084
65f38f15 11085 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
5bd4f169
AM
11086 input_bfd,
11087 input_section,
11088 contents,
411e1bfb 11089 rel->r_offset,
5bd4f169
AM
11090 relocation,
11091 addend);
11092
ef60b7ff 11093 if (r != bfd_reloc_ok)
5bd4f169 11094 {
cd27b276
AM
11095 if (sym_name == NULL)
11096 sym_name = "(null)";
11097 if (r == bfd_reloc_overflow)
5bd4f169 11098 {
cd27b276
AM
11099 if (warned)
11100 continue;
11101 if (h != NULL
039b3fef 11102 && h->elf.root.type == bfd_link_hash_undefweak
4ce794b7 11103 && ppc64_elf_howto_table[r_type]->pc_relative)
5bd4f169
AM
11104 {
11105 /* Assume this is a call protected by other code that
11106 detects the symbol is undefined. If this is the case,
11107 we can safely ignore the overflow. If not, the
11108 program is hosed anyway, and a little warning isn't
11109 going to help. */
11110
11111 continue;
11112 }
11113
ef60b7ff 11114 if (!((*info->callbacks->reloc_overflow)
dfeffb9f
L
11115 (info, (h ? &h->elf.root : NULL), sym_name,
11116 ppc64_elf_howto_table[r_type]->name,
4cc603a5 11117 orig_addend, input_bfd, input_section, rel->r_offset)))
b34976b6 11118 return FALSE;
ef60b7ff
AM
11119 }
11120 else
11121 {
11122 (*_bfd_error_handler)
d003868e
AM
11123 (_("%B(%A+0x%lx): %s reloc against `%s': error %d"),
11124 input_bfd,
11125 input_section,
7b609f53 11126 (long) rel->r_offset,
4ce794b7 11127 ppc64_elf_howto_table[r_type]->name,
7b609f53
AM
11128 sym_name,
11129 (int) r);
b34976b6 11130 ret = FALSE;
ef60b7ff 11131 }
5bd4f169 11132 }
5bd4f169
AM
11133 }
11134
645ea6a9
AM
11135 /* If we're emitting relocations, then shortly after this function
11136 returns, reloc offsets and addends for this section will be
11137 adjusted. Worse, reloc symbol indices will be for the output
8860955f
AM
11138 file rather than the input. Save a copy of the relocs for
11139 opd_entry_value. */
4cc603a5 11140 if (is_opd && (info->emitrelocations || info->relocatable))
8860955f
AM
11141 {
11142 bfd_size_type amt;
11143 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
11144 rel = bfd_alloc (input_bfd, amt);
11145 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd_relocs == NULL);
11146 ppc64_elf_tdata (input_bfd)->opd_relocs = rel;
11147 if (rel == NULL)
11148 return FALSE;
11149 memcpy (rel, relocs, amt);
11150 }
5bd4f169
AM
11151 return ret;
11152}
11153
754021d0
AM
11154/* Adjust the value of any local symbols in opd sections. */
11155
11156static bfd_boolean
11157ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
11158 const char *name ATTRIBUTE_UNUSED,
11159 Elf_Internal_Sym *elfsym,
11160 asection *input_sec,
11161 struct elf_link_hash_entry *h)
11162{
4025353c 11163 long *opd_adjust, adjust;
754021d0
AM
11164 bfd_vma value;
11165
4025353c
AM
11166 if (h != NULL)
11167 return TRUE;
11168
11169 opd_adjust = get_opd_info (input_sec);
11170 if (opd_adjust == NULL)
754021d0
AM
11171 return TRUE;
11172
11173 value = elfsym->st_value - input_sec->output_offset;
11174 if (!info->relocatable)
11175 value -= input_sec->output_section->vma;
11176
3f764659 11177 adjust = opd_adjust[value / 8];
4025353c
AM
11178 if (adjust == -1)
11179 elfsym->st_value = 0;
11180 else
11181 elfsym->st_value += adjust;
754021d0
AM
11182 return TRUE;
11183}
11184
5bd4f169
AM
11185/* Finish up dynamic symbol handling. We set the contents of various
11186 dynamic sections here. */
11187
b34976b6 11188static bfd_boolean
4ce794b7
AM
11189ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
11190 struct bfd_link_info *info,
11191 struct elf_link_hash_entry *h,
11192 Elf_Internal_Sym *sym)
5bd4f169 11193{
65f38f15 11194 struct ppc_link_hash_table *htab;
8387904d
AM
11195 struct plt_entry *ent;
11196 Elf_Internal_Rela rela;
11197 bfd_byte *loc;
5bd4f169 11198
65f38f15 11199 htab = ppc_hash_table (info);
5bd4f169 11200
8387904d
AM
11201 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
11202 if (ent->plt.offset != (bfd_vma) -1)
11203 {
11204 /* This symbol has an entry in the procedure linkage
11205 table. Set it up. */
11206
11207 if (htab->plt == NULL
11208 || htab->relplt == NULL
11209 || htab->glink == NULL)
11210 abort ();
11211
11212 /* Create a JMP_SLOT reloc to inform the dynamic linker to
11213 fill in the PLT entry. */
11214 rela.r_offset = (htab->plt->output_section->vma
11215 + htab->plt->output_offset
11216 + ent->plt.offset);
11217 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
11218 rela.r_addend = ent->addend;
11219
11220 loc = htab->relplt->contents;
11221 loc += ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
11222 * sizeof (Elf64_External_Rela));
11223 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
11224 }
5bd4f169 11225
f5385ebf 11226 if (h->needs_copy)
5bd4f169 11227 {
5bd4f169 11228 Elf_Internal_Rela rela;
947216bf 11229 bfd_byte *loc;
5bd4f169 11230
65f38f15 11231 /* This symbol needs a copy reloc. Set it up. */
5bd4f169 11232
65f38f15
AM
11233 if (h->dynindx == -1
11234 || (h->root.type != bfd_link_hash_defined
11235 && h->root.type != bfd_link_hash_defweak)
4ce794b7 11236 || htab->relbss == NULL)
65f38f15 11237 abort ();
5bd4f169
AM
11238
11239 rela.r_offset = (h->root.u.def.value
11240 + h->root.u.def.section->output_section->vma
11241 + h->root.u.def.section->output_offset);
11242 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
11243 rela.r_addend = 0;
4ce794b7
AM
11244 loc = htab->relbss->contents;
11245 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
65f38f15 11246 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
5bd4f169
AM
11247 }
11248
5bd4f169
AM
11249 /* Mark some specially defined symbols as absolute. */
11250 if (strcmp (h->root.root.string, "_DYNAMIC") == 0)
11251 sym->st_shndx = SHN_ABS;
11252
b34976b6 11253 return TRUE;
5bd4f169
AM
11254}
11255
65f38f15
AM
11256/* Used to decide how to sort relocs in an optimal manner for the
11257 dynamic linker, before writing them out. */
11258
11259static enum elf_reloc_type_class
4ce794b7 11260ppc64_elf_reloc_type_class (const Elf_Internal_Rela *rela)
65f38f15 11261{
04c9666a 11262 enum elf_ppc64_reloc_type r_type;
a33d1f77 11263
4ce794b7 11264 r_type = ELF64_R_TYPE (rela->r_info);
a33d1f77 11265 switch (r_type)
65f38f15
AM
11266 {
11267 case R_PPC64_RELATIVE:
11268 return reloc_class_relative;
11269 case R_PPC64_JMP_SLOT:
11270 return reloc_class_plt;
11271 case R_PPC64_COPY:
11272 return reloc_class_copy;
11273 default:
11274 return reloc_class_normal;
11275 }
11276}
11277
5bd4f169
AM
11278/* Finish up the dynamic sections. */
11279
b34976b6 11280static bfd_boolean
4ce794b7
AM
11281ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
11282 struct bfd_link_info *info)
5bd4f169 11283{
65f38f15
AM
11284 struct ppc_link_hash_table *htab;
11285 bfd *dynobj;
5bd4f169 11286 asection *sdyn;
5bd4f169 11287
65f38f15
AM
11288 htab = ppc_hash_table (info);
11289 dynobj = htab->elf.dynobj;
5bd4f169
AM
11290 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
11291
65f38f15 11292 if (htab->elf.dynamic_sections_created)
5bd4f169 11293 {
5bd4f169
AM
11294 Elf64_External_Dyn *dyncon, *dynconend;
11295
4ce794b7 11296 if (sdyn == NULL || htab->got == NULL)
65f38f15 11297 abort ();
5bd4f169
AM
11298
11299 dyncon = (Elf64_External_Dyn *) sdyn->contents;
eea6121a 11300 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
5bd4f169
AM
11301 for (; dyncon < dynconend; dyncon++)
11302 {
11303 Elf_Internal_Dyn dyn;
19397422 11304 asection *s;
5bd4f169
AM
11305
11306 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
11307
11308 switch (dyn.d_tag)
11309 {
65f38f15
AM
11310 default:
11311 continue;
5bd4f169 11312
5d1634d7 11313 case DT_PPC64_GLINK:
4ce794b7 11314 s = htab->glink;
6348e046 11315 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
ad8e1ba5
AM
11316 /* We stupidly defined DT_PPC64_GLINK to be the start
11317 of glink rather than the first entry point, which is
11318 what ld.so needs, and now have a bigger stub to
11319 support automatic multiple TOCs. */
11320 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
5d1634d7
AM
11321 break;
11322
19397422
AM
11323 case DT_PPC64_OPD:
11324 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
11325 if (s == NULL)
11326 continue;
11327 dyn.d_un.d_ptr = s->vma;
19397422
AM
11328 break;
11329
11330 case DT_PPC64_OPDSZ:
11331 s = bfd_get_section_by_name (output_bfd, ".opd");
6348e046
AM
11332 if (s == NULL)
11333 continue;
eea6121a 11334 dyn.d_un.d_val = s->size;
19397422
AM
11335 break;
11336
65f38f15 11337 case DT_PLTGOT:
4ce794b7 11338 s = htab->plt;
6348e046 11339 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15
AM
11340 break;
11341
11342 case DT_JMPREL:
4ce794b7 11343 s = htab->relplt;
6348e046 11344 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
65f38f15 11345 break;
5bd4f169 11346
65f38f15 11347 case DT_PLTRELSZ:
eea6121a 11348 dyn.d_un.d_val = htab->relplt->size;
5d1634d7
AM
11349 break;
11350
11351 case DT_RELASZ:
11352 /* Don't count procedure linkage table relocs in the
11353 overall reloc count. */
4ce794b7 11354 s = htab->relplt;
6348e046
AM
11355 if (s == NULL)
11356 continue;
eea6121a 11357 dyn.d_un.d_val -= s->size;
6348e046
AM
11358 break;
11359
11360 case DT_RELA:
11361 /* We may not be using the standard ELF linker script.
11362 If .rela.plt is the first .rela section, we adjust
11363 DT_RELA to not include it. */
4ce794b7 11364 s = htab->relplt;
6348e046
AM
11365 if (s == NULL)
11366 continue;
11367 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
11368 continue;
eea6121a 11369 dyn.d_un.d_ptr += s->size;
65f38f15 11370 break;
5bd4f169 11371 }
5bd4f169 11372
65f38f15 11373 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
5bd4f169 11374 }
5bd4f169
AM
11375 }
11376
eea6121a 11377 if (htab->got != NULL && htab->got->size != 0)
5d1634d7
AM
11378 {
11379 /* Fill in the first entry in the global offset table.
11380 We use it to hold the link-time TOCbase. */
11381 bfd_put_64 (output_bfd,
60ee0d4a 11382 elf_gp (output_bfd) + TOC_BASE_OFF,
4ce794b7 11383 htab->got->contents);
5d1634d7
AM
11384
11385 /* Set .got entry size. */
4ce794b7 11386 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
5d1634d7
AM
11387 }
11388
eea6121a 11389 if (htab->plt != NULL && htab->plt->size != 0)
5d1634d7
AM
11390 {
11391 /* Set .plt entry size. */
4ce794b7 11392 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
5d1634d7
AM
11393 = PLT_ENTRY_SIZE;
11394 }
11395
e717da7e 11396 /* We need to handle writing out multiple GOT sections ourselves,
7b53ace3
AM
11397 since we didn't add them to DYNOBJ. We know dynobj is the first
11398 bfd. */
e717da7e
AM
11399 while ((dynobj = dynobj->link_next) != NULL)
11400 {
11401 asection *s;
7b53ace3 11402
ee75fd95 11403 if (!is_ppc64_elf_target (dynobj->xvec))
7b53ace3
AM
11404 continue;
11405
e717da7e
AM
11406 s = ppc64_elf_tdata (dynobj)->got;
11407 if (s != NULL
eea6121a 11408 && s->size != 0
e717da7e
AM
11409 && s->output_section != bfd_abs_section_ptr
11410 && !bfd_set_section_contents (output_bfd, s->output_section,
11411 s->contents, s->output_offset,
eea6121a 11412 s->size))
e717da7e
AM
11413 return FALSE;
11414 s = ppc64_elf_tdata (dynobj)->relgot;
11415 if (s != NULL
eea6121a 11416 && s->size != 0
e717da7e
AM
11417 && s->output_section != bfd_abs_section_ptr
11418 && !bfd_set_section_contents (output_bfd, s->output_section,
11419 s->contents, s->output_offset,
eea6121a 11420 s->size))
e717da7e
AM
11421 return FALSE;
11422 }
f6c52c13 11423
b34976b6 11424 return TRUE;
5bd4f169
AM
11425}
11426
5bd4f169 11427#include "elf64-target.h"
This page took 1.074998 seconds and 4 git commands to generate.