1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Written by Linus Nordberg, Swox AB <info@swox.com>,
5 based on elf32-ppc.c by Ian Lance Taylor.
6 Largely rewritten by Alan Modra.
8 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
13 (at your option) any later version.
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.
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.,
22 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
25 /* The 64-bit PowerPC ELF ABI may be found at
26 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
27 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
35 #include "elf/ppc64.h"
36 #include "elf64-ppc.h"
39 static bfd_reloc_status_type ppc64_elf_ha_reloc
40 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41 static bfd_reloc_status_type ppc64_elf_branch_reloc
42 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
44 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
45 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
46 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
47 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
48 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
49 static bfd_reloc_status_type ppc64_elf_toc_reloc
50 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
51 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
52 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
53 static bfd_reloc_status_type ppc64_elf_toc64_reloc
54 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
55 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
56 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
57 static bfd_vma opd_entry_value
58 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
60 #define TARGET_LITTLE_SYM bfd_elf64_powerpcle_vec
61 #define TARGET_LITTLE_NAME "elf64-powerpcle"
62 #define TARGET_BIG_SYM bfd_elf64_powerpc_vec
63 #define TARGET_BIG_NAME "elf64-powerpc"
64 #define ELF_ARCH bfd_arch_powerpc
65 #define ELF_TARGET_ID PPC64_ELF_DATA
66 #define ELF_MACHINE_CODE EM_PPC64
67 #define ELF_MAXPAGESIZE 0x10000
68 #define ELF_COMMONPAGESIZE 0x1000
69 #define elf_info_to_howto ppc64_elf_info_to_howto
71 #define elf_backend_want_got_sym 0
72 #define elf_backend_want_plt_sym 0
73 #define elf_backend_plt_alignment 3
74 #define elf_backend_plt_not_loaded 1
75 #define elf_backend_got_header_size 8
76 #define elf_backend_can_gc_sections 1
77 #define elf_backend_can_refcount 1
78 #define elf_backend_rela_normal 1
79 #define elf_backend_default_execstack 0
81 #define bfd_elf64_mkobject ppc64_elf_mkobject
82 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
83 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
84 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
85 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
86 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
87 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
88 #define bfd_elf64_bfd_link_hash_table_free ppc64_elf_link_hash_table_free
89 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
90 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
92 #define elf_backend_object_p ppc64_elf_object_p
93 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
94 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
95 #define elf_backend_write_core_note ppc64_elf_write_core_note
96 #define elf_backend_create_dynamic_sections ppc64_elf_create_dynamic_sections
97 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
98 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
99 #define elf_backend_check_directives ppc64_elf_process_dot_syms
100 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
101 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
102 #define elf_backend_check_relocs ppc64_elf_check_relocs
103 #define elf_backend_gc_keep ppc64_elf_gc_keep
104 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
105 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
106 #define elf_backend_gc_sweep_hook ppc64_elf_gc_sweep_hook
107 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
108 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
109 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
110 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
111 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
112 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
113 #define elf_backend_action_discarded ppc64_elf_action_discarded
114 #define elf_backend_relocate_section ppc64_elf_relocate_section
115 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
116 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
117 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
118 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
119 #define elf_backend_special_sections ppc64_elf_special_sections
120 #define elf_backend_post_process_headers _bfd_elf_set_osabi
121 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
123 /* The name of the dynamic interpreter. This is put in the .interp
125 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
127 /* The size in bytes of an entry in the procedure linkage table. */
128 #define PLT_ENTRY_SIZE 24
130 /* The initial size of the plt reserved for the dynamic linker. */
131 #define PLT_INITIAL_ENTRY_SIZE PLT_ENTRY_SIZE
133 /* TOC base pointers offset from start of TOC. */
134 #define TOC_BASE_OFF 0x8000
136 /* Offset of tp and dtp pointers from start of TLS block. */
137 #define TP_OFFSET 0x7000
138 #define DTP_OFFSET 0x8000
140 /* .plt call stub instructions. The normal stub is like this, but
141 sometimes the .plt entry crosses a 64k boundary and we need to
142 insert an addi to adjust r11. */
143 #define PLT_CALL_STUB_SIZE (7*4)
144 #define STD_R2_40R1 0xf8410028 /* std %r2,40(%r1) */
145 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
146 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
147 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
148 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
149 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
150 #define BCTR 0x4e800420 /* bctr */
152 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
153 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
154 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
156 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
157 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
158 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
159 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
160 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
161 #define BNECTR 0x4ca20420 /* bnectr+ */
162 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
164 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
165 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
166 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
168 #define LD_R2_40R1 0xe8410028 /* ld %r2,40(%r1) */
170 /* glink call stub instructions. We enter with the index in R0. */
171 #define GLINK_CALL_STUB_SIZE (16*4)
175 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
176 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
178 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
179 /* ld %2,(0b-1b)(%11) */
180 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
181 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
189 #define NOP 0x60000000
191 /* Some other nops. */
192 #define CROR_151515 0x4def7b82
193 #define CROR_313131 0x4ffffb82
195 /* .glink entries for the first 32k functions are two instructions. */
196 #define LI_R0_0 0x38000000 /* li %r0,0 */
197 #define B_DOT 0x48000000 /* b . */
199 /* After that, we need two instructions to load the index, followed by
201 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
202 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
204 /* Instructions used by the save and restore reg functions. */
205 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
206 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
207 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
208 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
209 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
210 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
211 #define LI_R12_0 0x39800000 /* li %r12,0 */
212 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
213 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
214 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
215 #define BLR 0x4e800020 /* blr */
217 /* Since .opd is an array of descriptors and each entry will end up
218 with identical R_PPC64_RELATIVE relocs, there is really no need to
219 propagate .opd relocs; The dynamic linker should be taught to
220 relocate .opd without reloc entries. */
221 #ifndef NO_OPD_RELOCS
222 #define NO_OPD_RELOCS 0
225 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
227 /* Relocation HOWTO's. */
228 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
230 static reloc_howto_type ppc64_elf_howto_raw[] = {
231 /* This reloc does nothing. */
232 HOWTO (R_PPC64_NONE, /* type */
234 2, /* size (0 = byte, 1 = short, 2 = long) */
236 FALSE, /* pc_relative */
238 complain_overflow_dont, /* complain_on_overflow */
239 bfd_elf_generic_reloc, /* special_function */
240 "R_PPC64_NONE", /* name */
241 FALSE, /* partial_inplace */
244 FALSE), /* pcrel_offset */
246 /* A standard 32 bit relocation. */
247 HOWTO (R_PPC64_ADDR32, /* type */
249 2, /* size (0 = byte, 1 = short, 2 = long) */
251 FALSE, /* pc_relative */
253 complain_overflow_bitfield, /* complain_on_overflow */
254 bfd_elf_generic_reloc, /* special_function */
255 "R_PPC64_ADDR32", /* name */
256 FALSE, /* partial_inplace */
258 0xffffffff, /* dst_mask */
259 FALSE), /* pcrel_offset */
261 /* An absolute 26 bit branch; the lower two bits must be zero.
262 FIXME: we don't check that, we just clear them. */
263 HOWTO (R_PPC64_ADDR24, /* type */
265 2, /* size (0 = byte, 1 = short, 2 = long) */
267 FALSE, /* pc_relative */
269 complain_overflow_bitfield, /* complain_on_overflow */
270 bfd_elf_generic_reloc, /* special_function */
271 "R_PPC64_ADDR24", /* name */
272 FALSE, /* partial_inplace */
274 0x03fffffc, /* dst_mask */
275 FALSE), /* pcrel_offset */
277 /* A standard 16 bit relocation. */
278 HOWTO (R_PPC64_ADDR16, /* type */
280 1, /* size (0 = byte, 1 = short, 2 = long) */
282 FALSE, /* pc_relative */
284 complain_overflow_bitfield, /* complain_on_overflow */
285 bfd_elf_generic_reloc, /* special_function */
286 "R_PPC64_ADDR16", /* name */
287 FALSE, /* partial_inplace */
289 0xffff, /* dst_mask */
290 FALSE), /* pcrel_offset */
292 /* A 16 bit relocation without overflow. */
293 HOWTO (R_PPC64_ADDR16_LO, /* type */
295 1, /* size (0 = byte, 1 = short, 2 = long) */
297 FALSE, /* pc_relative */
299 complain_overflow_dont,/* complain_on_overflow */
300 bfd_elf_generic_reloc, /* special_function */
301 "R_PPC64_ADDR16_LO", /* name */
302 FALSE, /* partial_inplace */
304 0xffff, /* dst_mask */
305 FALSE), /* pcrel_offset */
307 /* Bits 16-31 of an address. */
308 HOWTO (R_PPC64_ADDR16_HI, /* type */
310 1, /* size (0 = byte, 1 = short, 2 = long) */
312 FALSE, /* pc_relative */
314 complain_overflow_signed, /* complain_on_overflow */
315 bfd_elf_generic_reloc, /* special_function */
316 "R_PPC64_ADDR16_HI", /* name */
317 FALSE, /* partial_inplace */
319 0xffff, /* dst_mask */
320 FALSE), /* pcrel_offset */
322 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
323 bits, treated as a signed number, is negative. */
324 HOWTO (R_PPC64_ADDR16_HA, /* type */
326 1, /* size (0 = byte, 1 = short, 2 = long) */
328 FALSE, /* pc_relative */
330 complain_overflow_signed, /* complain_on_overflow */
331 ppc64_elf_ha_reloc, /* special_function */
332 "R_PPC64_ADDR16_HA", /* name */
333 FALSE, /* partial_inplace */
335 0xffff, /* dst_mask */
336 FALSE), /* pcrel_offset */
338 /* An absolute 16 bit branch; the lower two bits must be zero.
339 FIXME: we don't check that, we just clear them. */
340 HOWTO (R_PPC64_ADDR14, /* type */
342 2, /* size (0 = byte, 1 = short, 2 = long) */
344 FALSE, /* pc_relative */
346 complain_overflow_bitfield, /* complain_on_overflow */
347 ppc64_elf_branch_reloc, /* special_function */
348 "R_PPC64_ADDR14", /* name */
349 FALSE, /* partial_inplace */
351 0x0000fffc, /* dst_mask */
352 FALSE), /* pcrel_offset */
354 /* An absolute 16 bit branch, for which bit 10 should be set to
355 indicate that the branch is expected to be taken. The lower two
356 bits must be zero. */
357 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
359 2, /* size (0 = byte, 1 = short, 2 = long) */
361 FALSE, /* pc_relative */
363 complain_overflow_bitfield, /* complain_on_overflow */
364 ppc64_elf_brtaken_reloc, /* special_function */
365 "R_PPC64_ADDR14_BRTAKEN",/* name */
366 FALSE, /* partial_inplace */
368 0x0000fffc, /* dst_mask */
369 FALSE), /* pcrel_offset */
371 /* An absolute 16 bit branch, for which bit 10 should be set to
372 indicate that the branch is not expected to be taken. The lower
373 two bits must be zero. */
374 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
376 2, /* size (0 = byte, 1 = short, 2 = long) */
378 FALSE, /* pc_relative */
380 complain_overflow_bitfield, /* complain_on_overflow */
381 ppc64_elf_brtaken_reloc, /* special_function */
382 "R_PPC64_ADDR14_BRNTAKEN",/* name */
383 FALSE, /* partial_inplace */
385 0x0000fffc, /* dst_mask */
386 FALSE), /* pcrel_offset */
388 /* A relative 26 bit branch; the lower two bits must be zero. */
389 HOWTO (R_PPC64_REL24, /* type */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
393 TRUE, /* pc_relative */
395 complain_overflow_signed, /* complain_on_overflow */
396 ppc64_elf_branch_reloc, /* special_function */
397 "R_PPC64_REL24", /* name */
398 FALSE, /* partial_inplace */
400 0x03fffffc, /* dst_mask */
401 TRUE), /* pcrel_offset */
403 /* A relative 16 bit branch; the lower two bits must be zero. */
404 HOWTO (R_PPC64_REL14, /* type */
406 2, /* size (0 = byte, 1 = short, 2 = long) */
408 TRUE, /* pc_relative */
410 complain_overflow_signed, /* complain_on_overflow */
411 ppc64_elf_branch_reloc, /* special_function */
412 "R_PPC64_REL14", /* name */
413 FALSE, /* partial_inplace */
415 0x0000fffc, /* dst_mask */
416 TRUE), /* pcrel_offset */
418 /* A relative 16 bit branch. Bit 10 should be set to indicate that
419 the branch is expected to be taken. The lower two bits must be
421 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
423 2, /* size (0 = byte, 1 = short, 2 = long) */
425 TRUE, /* pc_relative */
427 complain_overflow_signed, /* complain_on_overflow */
428 ppc64_elf_brtaken_reloc, /* special_function */
429 "R_PPC64_REL14_BRTAKEN", /* name */
430 FALSE, /* partial_inplace */
432 0x0000fffc, /* dst_mask */
433 TRUE), /* pcrel_offset */
435 /* A relative 16 bit branch. Bit 10 should be set to indicate that
436 the branch is not expected to be taken. The lower two bits must
438 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
440 2, /* size (0 = byte, 1 = short, 2 = long) */
442 TRUE, /* pc_relative */
444 complain_overflow_signed, /* complain_on_overflow */
445 ppc64_elf_brtaken_reloc, /* special_function */
446 "R_PPC64_REL14_BRNTAKEN",/* name */
447 FALSE, /* partial_inplace */
449 0x0000fffc, /* dst_mask */
450 TRUE), /* pcrel_offset */
452 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
454 HOWTO (R_PPC64_GOT16, /* type */
456 1, /* size (0 = byte, 1 = short, 2 = long) */
458 FALSE, /* pc_relative */
460 complain_overflow_signed, /* complain_on_overflow */
461 ppc64_elf_unhandled_reloc, /* special_function */
462 "R_PPC64_GOT16", /* name */
463 FALSE, /* partial_inplace */
465 0xffff, /* dst_mask */
466 FALSE), /* pcrel_offset */
468 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
470 HOWTO (R_PPC64_GOT16_LO, /* type */
472 1, /* size (0 = byte, 1 = short, 2 = long) */
474 FALSE, /* pc_relative */
476 complain_overflow_dont, /* complain_on_overflow */
477 ppc64_elf_unhandled_reloc, /* special_function */
478 "R_PPC64_GOT16_LO", /* name */
479 FALSE, /* partial_inplace */
481 0xffff, /* dst_mask */
482 FALSE), /* pcrel_offset */
484 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
486 HOWTO (R_PPC64_GOT16_HI, /* type */
488 1, /* size (0 = byte, 1 = short, 2 = long) */
490 FALSE, /* pc_relative */
492 complain_overflow_signed,/* complain_on_overflow */
493 ppc64_elf_unhandled_reloc, /* special_function */
494 "R_PPC64_GOT16_HI", /* name */
495 FALSE, /* partial_inplace */
497 0xffff, /* dst_mask */
498 FALSE), /* pcrel_offset */
500 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
502 HOWTO (R_PPC64_GOT16_HA, /* type */
504 1, /* size (0 = byte, 1 = short, 2 = long) */
506 FALSE, /* pc_relative */
508 complain_overflow_signed,/* complain_on_overflow */
509 ppc64_elf_unhandled_reloc, /* special_function */
510 "R_PPC64_GOT16_HA", /* name */
511 FALSE, /* partial_inplace */
513 0xffff, /* dst_mask */
514 FALSE), /* pcrel_offset */
516 /* This is used only by the dynamic linker. The symbol should exist
517 both in the object being run and in some shared library. The
518 dynamic linker copies the data addressed by the symbol from the
519 shared library into the object, because the object being
520 run has to have the data at some particular address. */
521 HOWTO (R_PPC64_COPY, /* type */
523 0, /* this one is variable size */
525 FALSE, /* pc_relative */
527 complain_overflow_dont, /* complain_on_overflow */
528 ppc64_elf_unhandled_reloc, /* special_function */
529 "R_PPC64_COPY", /* name */
530 FALSE, /* partial_inplace */
533 FALSE), /* pcrel_offset */
535 /* Like R_PPC64_ADDR64, but used when setting global offset table
537 HOWTO (R_PPC64_GLOB_DAT, /* type */
539 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
541 FALSE, /* pc_relative */
543 complain_overflow_dont, /* complain_on_overflow */
544 ppc64_elf_unhandled_reloc, /* special_function */
545 "R_PPC64_GLOB_DAT", /* name */
546 FALSE, /* partial_inplace */
548 ONES (64), /* dst_mask */
549 FALSE), /* pcrel_offset */
551 /* Created by the link editor. Marks a procedure linkage table
552 entry for a symbol. */
553 HOWTO (R_PPC64_JMP_SLOT, /* type */
555 0, /* size (0 = byte, 1 = short, 2 = long) */
557 FALSE, /* pc_relative */
559 complain_overflow_dont, /* complain_on_overflow */
560 ppc64_elf_unhandled_reloc, /* special_function */
561 "R_PPC64_JMP_SLOT", /* name */
562 FALSE, /* partial_inplace */
565 FALSE), /* pcrel_offset */
567 /* Used only by the dynamic linker. When the object is run, this
568 doubleword64 is set to the load address of the object, plus the
570 HOWTO (R_PPC64_RELATIVE, /* type */
572 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
574 FALSE, /* pc_relative */
576 complain_overflow_dont, /* complain_on_overflow */
577 bfd_elf_generic_reloc, /* special_function */
578 "R_PPC64_RELATIVE", /* name */
579 FALSE, /* partial_inplace */
581 ONES (64), /* dst_mask */
582 FALSE), /* pcrel_offset */
584 /* Like R_PPC64_ADDR32, but may be unaligned. */
585 HOWTO (R_PPC64_UADDR32, /* type */
587 2, /* size (0 = byte, 1 = short, 2 = long) */
589 FALSE, /* pc_relative */
591 complain_overflow_bitfield, /* complain_on_overflow */
592 bfd_elf_generic_reloc, /* special_function */
593 "R_PPC64_UADDR32", /* name */
594 FALSE, /* partial_inplace */
596 0xffffffff, /* dst_mask */
597 FALSE), /* pcrel_offset */
599 /* Like R_PPC64_ADDR16, but may be unaligned. */
600 HOWTO (R_PPC64_UADDR16, /* type */
602 1, /* size (0 = byte, 1 = short, 2 = long) */
604 FALSE, /* pc_relative */
606 complain_overflow_bitfield, /* complain_on_overflow */
607 bfd_elf_generic_reloc, /* special_function */
608 "R_PPC64_UADDR16", /* name */
609 FALSE, /* partial_inplace */
611 0xffff, /* dst_mask */
612 FALSE), /* pcrel_offset */
614 /* 32-bit PC relative. */
615 HOWTO (R_PPC64_REL32, /* type */
617 2, /* size (0 = byte, 1 = short, 2 = long) */
619 TRUE, /* pc_relative */
621 /* FIXME: Verify. Was complain_overflow_bitfield. */
622 complain_overflow_signed, /* complain_on_overflow */
623 bfd_elf_generic_reloc, /* special_function */
624 "R_PPC64_REL32", /* name */
625 FALSE, /* partial_inplace */
627 0xffffffff, /* dst_mask */
628 TRUE), /* pcrel_offset */
630 /* 32-bit relocation to the symbol's procedure linkage table. */
631 HOWTO (R_PPC64_PLT32, /* type */
633 2, /* size (0 = byte, 1 = short, 2 = long) */
635 FALSE, /* pc_relative */
637 complain_overflow_bitfield, /* complain_on_overflow */
638 ppc64_elf_unhandled_reloc, /* special_function */
639 "R_PPC64_PLT32", /* name */
640 FALSE, /* partial_inplace */
642 0xffffffff, /* dst_mask */
643 FALSE), /* pcrel_offset */
645 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
646 FIXME: R_PPC64_PLTREL32 not supported. */
647 HOWTO (R_PPC64_PLTREL32, /* type */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
651 TRUE, /* pc_relative */
653 complain_overflow_signed, /* complain_on_overflow */
654 bfd_elf_generic_reloc, /* special_function */
655 "R_PPC64_PLTREL32", /* name */
656 FALSE, /* partial_inplace */
658 0xffffffff, /* dst_mask */
659 TRUE), /* pcrel_offset */
661 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
663 HOWTO (R_PPC64_PLT16_LO, /* type */
665 1, /* size (0 = byte, 1 = short, 2 = long) */
667 FALSE, /* pc_relative */
669 complain_overflow_dont, /* complain_on_overflow */
670 ppc64_elf_unhandled_reloc, /* special_function */
671 "R_PPC64_PLT16_LO", /* name */
672 FALSE, /* partial_inplace */
674 0xffff, /* dst_mask */
675 FALSE), /* pcrel_offset */
677 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
679 HOWTO (R_PPC64_PLT16_HI, /* type */
681 1, /* size (0 = byte, 1 = short, 2 = long) */
683 FALSE, /* pc_relative */
685 complain_overflow_signed, /* complain_on_overflow */
686 ppc64_elf_unhandled_reloc, /* special_function */
687 "R_PPC64_PLT16_HI", /* name */
688 FALSE, /* partial_inplace */
690 0xffff, /* dst_mask */
691 FALSE), /* pcrel_offset */
693 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
695 HOWTO (R_PPC64_PLT16_HA, /* type */
697 1, /* size (0 = byte, 1 = short, 2 = long) */
699 FALSE, /* pc_relative */
701 complain_overflow_signed, /* complain_on_overflow */
702 ppc64_elf_unhandled_reloc, /* special_function */
703 "R_PPC64_PLT16_HA", /* name */
704 FALSE, /* partial_inplace */
706 0xffff, /* dst_mask */
707 FALSE), /* pcrel_offset */
709 /* 16-bit section relative relocation. */
710 HOWTO (R_PPC64_SECTOFF, /* type */
712 1, /* size (0 = byte, 1 = short, 2 = long) */
714 FALSE, /* pc_relative */
716 complain_overflow_bitfield, /* complain_on_overflow */
717 ppc64_elf_sectoff_reloc, /* special_function */
718 "R_PPC64_SECTOFF", /* name */
719 FALSE, /* partial_inplace */
721 0xffff, /* dst_mask */
722 FALSE), /* pcrel_offset */
724 /* Like R_PPC64_SECTOFF, but no overflow warning. */
725 HOWTO (R_PPC64_SECTOFF_LO, /* type */
727 1, /* size (0 = byte, 1 = short, 2 = long) */
729 FALSE, /* pc_relative */
731 complain_overflow_dont, /* complain_on_overflow */
732 ppc64_elf_sectoff_reloc, /* special_function */
733 "R_PPC64_SECTOFF_LO", /* name */
734 FALSE, /* partial_inplace */
736 0xffff, /* dst_mask */
737 FALSE), /* pcrel_offset */
739 /* 16-bit upper half section relative relocation. */
740 HOWTO (R_PPC64_SECTOFF_HI, /* type */
742 1, /* size (0 = byte, 1 = short, 2 = long) */
744 FALSE, /* pc_relative */
746 complain_overflow_signed, /* complain_on_overflow */
747 ppc64_elf_sectoff_reloc, /* special_function */
748 "R_PPC64_SECTOFF_HI", /* name */
749 FALSE, /* partial_inplace */
751 0xffff, /* dst_mask */
752 FALSE), /* pcrel_offset */
754 /* 16-bit upper half adjusted section relative relocation. */
755 HOWTO (R_PPC64_SECTOFF_HA, /* type */
757 1, /* size (0 = byte, 1 = short, 2 = long) */
759 FALSE, /* pc_relative */
761 complain_overflow_signed, /* complain_on_overflow */
762 ppc64_elf_sectoff_ha_reloc, /* special_function */
763 "R_PPC64_SECTOFF_HA", /* name */
764 FALSE, /* partial_inplace */
766 0xffff, /* dst_mask */
767 FALSE), /* pcrel_offset */
769 /* Like R_PPC64_REL24 without touching the two least significant bits. */
770 HOWTO (R_PPC64_REL30, /* type */
772 2, /* size (0 = byte, 1 = short, 2 = long) */
774 TRUE, /* pc_relative */
776 complain_overflow_dont, /* complain_on_overflow */
777 bfd_elf_generic_reloc, /* special_function */
778 "R_PPC64_REL30", /* name */
779 FALSE, /* partial_inplace */
781 0xfffffffc, /* dst_mask */
782 TRUE), /* pcrel_offset */
784 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
786 /* A standard 64-bit relocation. */
787 HOWTO (R_PPC64_ADDR64, /* type */
789 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
791 FALSE, /* pc_relative */
793 complain_overflow_dont, /* complain_on_overflow */
794 bfd_elf_generic_reloc, /* special_function */
795 "R_PPC64_ADDR64", /* name */
796 FALSE, /* partial_inplace */
798 ONES (64), /* dst_mask */
799 FALSE), /* pcrel_offset */
801 /* The bits 32-47 of an address. */
802 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
804 1, /* size (0 = byte, 1 = short, 2 = long) */
806 FALSE, /* pc_relative */
808 complain_overflow_dont, /* complain_on_overflow */
809 bfd_elf_generic_reloc, /* special_function */
810 "R_PPC64_ADDR16_HIGHER", /* name */
811 FALSE, /* partial_inplace */
813 0xffff, /* dst_mask */
814 FALSE), /* pcrel_offset */
816 /* The bits 32-47 of an address, plus 1 if the contents of the low
817 16 bits, treated as a signed number, is negative. */
818 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
820 1, /* size (0 = byte, 1 = short, 2 = long) */
822 FALSE, /* pc_relative */
824 complain_overflow_dont, /* complain_on_overflow */
825 ppc64_elf_ha_reloc, /* special_function */
826 "R_PPC64_ADDR16_HIGHERA", /* name */
827 FALSE, /* partial_inplace */
829 0xffff, /* dst_mask */
830 FALSE), /* pcrel_offset */
832 /* The bits 48-63 of an address. */
833 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
835 1, /* size (0 = byte, 1 = short, 2 = long) */
837 FALSE, /* pc_relative */
839 complain_overflow_dont, /* complain_on_overflow */
840 bfd_elf_generic_reloc, /* special_function */
841 "R_PPC64_ADDR16_HIGHEST", /* name */
842 FALSE, /* partial_inplace */
844 0xffff, /* dst_mask */
845 FALSE), /* pcrel_offset */
847 /* The bits 48-63 of an address, plus 1 if the contents of the low
848 16 bits, treated as a signed number, is negative. */
849 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
851 1, /* size (0 = byte, 1 = short, 2 = long) */
853 FALSE, /* pc_relative */
855 complain_overflow_dont, /* complain_on_overflow */
856 ppc64_elf_ha_reloc, /* special_function */
857 "R_PPC64_ADDR16_HIGHESTA", /* name */
858 FALSE, /* partial_inplace */
860 0xffff, /* dst_mask */
861 FALSE), /* pcrel_offset */
863 /* Like ADDR64, but may be unaligned. */
864 HOWTO (R_PPC64_UADDR64, /* type */
866 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
868 FALSE, /* pc_relative */
870 complain_overflow_dont, /* complain_on_overflow */
871 bfd_elf_generic_reloc, /* special_function */
872 "R_PPC64_UADDR64", /* name */
873 FALSE, /* partial_inplace */
875 ONES (64), /* dst_mask */
876 FALSE), /* pcrel_offset */
878 /* 64-bit relative relocation. */
879 HOWTO (R_PPC64_REL64, /* type */
881 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
883 TRUE, /* pc_relative */
885 complain_overflow_dont, /* complain_on_overflow */
886 bfd_elf_generic_reloc, /* special_function */
887 "R_PPC64_REL64", /* name */
888 FALSE, /* partial_inplace */
890 ONES (64), /* dst_mask */
891 TRUE), /* pcrel_offset */
893 /* 64-bit relocation to the symbol's procedure linkage table. */
894 HOWTO (R_PPC64_PLT64, /* type */
896 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
898 FALSE, /* pc_relative */
900 complain_overflow_dont, /* complain_on_overflow */
901 ppc64_elf_unhandled_reloc, /* special_function */
902 "R_PPC64_PLT64", /* name */
903 FALSE, /* partial_inplace */
905 ONES (64), /* dst_mask */
906 FALSE), /* pcrel_offset */
908 /* 64-bit PC relative relocation to the symbol's procedure linkage
910 /* FIXME: R_PPC64_PLTREL64 not supported. */
911 HOWTO (R_PPC64_PLTREL64, /* type */
913 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
915 TRUE, /* pc_relative */
917 complain_overflow_dont, /* complain_on_overflow */
918 ppc64_elf_unhandled_reloc, /* special_function */
919 "R_PPC64_PLTREL64", /* name */
920 FALSE, /* partial_inplace */
922 ONES (64), /* dst_mask */
923 TRUE), /* pcrel_offset */
925 /* 16 bit TOC-relative relocation. */
927 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
928 HOWTO (R_PPC64_TOC16, /* type */
930 1, /* size (0 = byte, 1 = short, 2 = long) */
932 FALSE, /* pc_relative */
934 complain_overflow_signed, /* complain_on_overflow */
935 ppc64_elf_toc_reloc, /* special_function */
936 "R_PPC64_TOC16", /* name */
937 FALSE, /* partial_inplace */
939 0xffff, /* dst_mask */
940 FALSE), /* pcrel_offset */
942 /* 16 bit TOC-relative relocation without overflow. */
944 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
945 HOWTO (R_PPC64_TOC16_LO, /* type */
947 1, /* size (0 = byte, 1 = short, 2 = long) */
949 FALSE, /* pc_relative */
951 complain_overflow_dont, /* complain_on_overflow */
952 ppc64_elf_toc_reloc, /* special_function */
953 "R_PPC64_TOC16_LO", /* name */
954 FALSE, /* partial_inplace */
956 0xffff, /* dst_mask */
957 FALSE), /* pcrel_offset */
959 /* 16 bit TOC-relative relocation, high 16 bits. */
961 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
962 HOWTO (R_PPC64_TOC16_HI, /* type */
964 1, /* size (0 = byte, 1 = short, 2 = long) */
966 FALSE, /* pc_relative */
968 complain_overflow_signed, /* complain_on_overflow */
969 ppc64_elf_toc_reloc, /* special_function */
970 "R_PPC64_TOC16_HI", /* name */
971 FALSE, /* partial_inplace */
973 0xffff, /* dst_mask */
974 FALSE), /* pcrel_offset */
976 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
977 contents of the low 16 bits, treated as a signed number, is
980 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
981 HOWTO (R_PPC64_TOC16_HA, /* type */
983 1, /* size (0 = byte, 1 = short, 2 = long) */
985 FALSE, /* pc_relative */
987 complain_overflow_signed, /* complain_on_overflow */
988 ppc64_elf_toc_ha_reloc, /* special_function */
989 "R_PPC64_TOC16_HA", /* name */
990 FALSE, /* partial_inplace */
992 0xffff, /* dst_mask */
993 FALSE), /* pcrel_offset */
995 /* 64-bit relocation; insert value of TOC base (.TOC.). */
997 /* R_PPC64_TOC 51 doubleword64 .TOC. */
998 HOWTO (R_PPC64_TOC, /* type */
1000 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1002 FALSE, /* pc_relative */
1004 complain_overflow_bitfield, /* complain_on_overflow */
1005 ppc64_elf_toc64_reloc, /* special_function */
1006 "R_PPC64_TOC", /* name */
1007 FALSE, /* partial_inplace */
1009 ONES (64), /* dst_mask */
1010 FALSE), /* pcrel_offset */
1012 /* Like R_PPC64_GOT16, but also informs the link editor that the
1013 value to relocate may (!) refer to a PLT entry which the link
1014 editor (a) may replace with the symbol value. If the link editor
1015 is unable to fully resolve the symbol, it may (b) create a PLT
1016 entry and store the address to the new PLT entry in the GOT.
1017 This permits lazy resolution of function symbols at run time.
1018 The link editor may also skip all of this and just (c) emit a
1019 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1020 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1021 HOWTO (R_PPC64_PLTGOT16, /* type */
1023 1, /* size (0 = byte, 1 = short, 2 = long) */
1025 FALSE, /* pc_relative */
1027 complain_overflow_signed, /* complain_on_overflow */
1028 ppc64_elf_unhandled_reloc, /* special_function */
1029 "R_PPC64_PLTGOT16", /* name */
1030 FALSE, /* partial_inplace */
1032 0xffff, /* dst_mask */
1033 FALSE), /* pcrel_offset */
1035 /* Like R_PPC64_PLTGOT16, but without overflow. */
1036 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1037 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1039 1, /* size (0 = byte, 1 = short, 2 = long) */
1041 FALSE, /* pc_relative */
1043 complain_overflow_dont, /* complain_on_overflow */
1044 ppc64_elf_unhandled_reloc, /* special_function */
1045 "R_PPC64_PLTGOT16_LO", /* name */
1046 FALSE, /* partial_inplace */
1048 0xffff, /* dst_mask */
1049 FALSE), /* pcrel_offset */
1051 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1052 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1053 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1054 16, /* rightshift */
1055 1, /* size (0 = byte, 1 = short, 2 = long) */
1057 FALSE, /* pc_relative */
1059 complain_overflow_signed, /* complain_on_overflow */
1060 ppc64_elf_unhandled_reloc, /* special_function */
1061 "R_PPC64_PLTGOT16_HI", /* name */
1062 FALSE, /* partial_inplace */
1064 0xffff, /* dst_mask */
1065 FALSE), /* pcrel_offset */
1067 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1068 1 if the contents of the low 16 bits, treated as a signed number,
1070 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1071 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1072 16, /* rightshift */
1073 1, /* size (0 = byte, 1 = short, 2 = long) */
1075 FALSE, /* pc_relative */
1077 complain_overflow_signed, /* complain_on_overflow */
1078 ppc64_elf_unhandled_reloc, /* special_function */
1079 "R_PPC64_PLTGOT16_HA", /* name */
1080 FALSE, /* partial_inplace */
1082 0xffff, /* dst_mask */
1083 FALSE), /* pcrel_offset */
1085 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1086 HOWTO (R_PPC64_ADDR16_DS, /* type */
1088 1, /* size (0 = byte, 1 = short, 2 = long) */
1090 FALSE, /* pc_relative */
1092 complain_overflow_bitfield, /* complain_on_overflow */
1093 bfd_elf_generic_reloc, /* special_function */
1094 "R_PPC64_ADDR16_DS", /* name */
1095 FALSE, /* partial_inplace */
1097 0xfffc, /* dst_mask */
1098 FALSE), /* pcrel_offset */
1100 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1101 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1103 1, /* size (0 = byte, 1 = short, 2 = long) */
1105 FALSE, /* pc_relative */
1107 complain_overflow_dont,/* complain_on_overflow */
1108 bfd_elf_generic_reloc, /* special_function */
1109 "R_PPC64_ADDR16_LO_DS",/* name */
1110 FALSE, /* partial_inplace */
1112 0xfffc, /* dst_mask */
1113 FALSE), /* pcrel_offset */
1115 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1116 HOWTO (R_PPC64_GOT16_DS, /* type */
1118 1, /* size (0 = byte, 1 = short, 2 = long) */
1120 FALSE, /* pc_relative */
1122 complain_overflow_signed, /* complain_on_overflow */
1123 ppc64_elf_unhandled_reloc, /* special_function */
1124 "R_PPC64_GOT16_DS", /* name */
1125 FALSE, /* partial_inplace */
1127 0xfffc, /* dst_mask */
1128 FALSE), /* pcrel_offset */
1130 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1131 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1133 1, /* size (0 = byte, 1 = short, 2 = long) */
1135 FALSE, /* pc_relative */
1137 complain_overflow_dont, /* complain_on_overflow */
1138 ppc64_elf_unhandled_reloc, /* special_function */
1139 "R_PPC64_GOT16_LO_DS", /* name */
1140 FALSE, /* partial_inplace */
1142 0xfffc, /* dst_mask */
1143 FALSE), /* pcrel_offset */
1145 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1146 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1148 1, /* size (0 = byte, 1 = short, 2 = long) */
1150 FALSE, /* pc_relative */
1152 complain_overflow_dont, /* complain_on_overflow */
1153 ppc64_elf_unhandled_reloc, /* special_function */
1154 "R_PPC64_PLT16_LO_DS", /* name */
1155 FALSE, /* partial_inplace */
1157 0xfffc, /* dst_mask */
1158 FALSE), /* pcrel_offset */
1160 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1161 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1163 1, /* size (0 = byte, 1 = short, 2 = long) */
1165 FALSE, /* pc_relative */
1167 complain_overflow_bitfield, /* complain_on_overflow */
1168 ppc64_elf_sectoff_reloc, /* special_function */
1169 "R_PPC64_SECTOFF_DS", /* name */
1170 FALSE, /* partial_inplace */
1172 0xfffc, /* dst_mask */
1173 FALSE), /* pcrel_offset */
1175 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1176 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1178 1, /* size (0 = byte, 1 = short, 2 = long) */
1180 FALSE, /* pc_relative */
1182 complain_overflow_dont, /* complain_on_overflow */
1183 ppc64_elf_sectoff_reloc, /* special_function */
1184 "R_PPC64_SECTOFF_LO_DS",/* name */
1185 FALSE, /* partial_inplace */
1187 0xfffc, /* dst_mask */
1188 FALSE), /* pcrel_offset */
1190 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1191 HOWTO (R_PPC64_TOC16_DS, /* type */
1193 1, /* size (0 = byte, 1 = short, 2 = long) */
1195 FALSE, /* pc_relative */
1197 complain_overflow_signed, /* complain_on_overflow */
1198 ppc64_elf_toc_reloc, /* special_function */
1199 "R_PPC64_TOC16_DS", /* name */
1200 FALSE, /* partial_inplace */
1202 0xfffc, /* dst_mask */
1203 FALSE), /* pcrel_offset */
1205 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1206 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1208 1, /* size (0 = byte, 1 = short, 2 = long) */
1210 FALSE, /* pc_relative */
1212 complain_overflow_dont, /* complain_on_overflow */
1213 ppc64_elf_toc_reloc, /* special_function */
1214 "R_PPC64_TOC16_LO_DS", /* name */
1215 FALSE, /* partial_inplace */
1217 0xfffc, /* dst_mask */
1218 FALSE), /* pcrel_offset */
1220 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1221 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1222 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1224 1, /* size (0 = byte, 1 = short, 2 = long) */
1226 FALSE, /* pc_relative */
1228 complain_overflow_signed, /* complain_on_overflow */
1229 ppc64_elf_unhandled_reloc, /* special_function */
1230 "R_PPC64_PLTGOT16_DS", /* name */
1231 FALSE, /* partial_inplace */
1233 0xfffc, /* dst_mask */
1234 FALSE), /* pcrel_offset */
1236 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1237 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1238 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1240 1, /* size (0 = byte, 1 = short, 2 = long) */
1242 FALSE, /* pc_relative */
1244 complain_overflow_dont, /* complain_on_overflow */
1245 ppc64_elf_unhandled_reloc, /* special_function */
1246 "R_PPC64_PLTGOT16_LO_DS",/* name */
1247 FALSE, /* partial_inplace */
1249 0xfffc, /* dst_mask */
1250 FALSE), /* pcrel_offset */
1252 /* Marker relocs for TLS. */
1255 2, /* size (0 = byte, 1 = short, 2 = long) */
1257 FALSE, /* pc_relative */
1259 complain_overflow_dont, /* complain_on_overflow */
1260 bfd_elf_generic_reloc, /* special_function */
1261 "R_PPC64_TLS", /* name */
1262 FALSE, /* partial_inplace */
1265 FALSE), /* pcrel_offset */
1267 HOWTO (R_PPC64_TLSGD,
1269 2, /* size (0 = byte, 1 = short, 2 = long) */
1271 FALSE, /* pc_relative */
1273 complain_overflow_dont, /* complain_on_overflow */
1274 bfd_elf_generic_reloc, /* special_function */
1275 "R_PPC64_TLSGD", /* name */
1276 FALSE, /* partial_inplace */
1279 FALSE), /* pcrel_offset */
1281 HOWTO (R_PPC64_TLSLD,
1283 2, /* size (0 = byte, 1 = short, 2 = long) */
1285 FALSE, /* pc_relative */
1287 complain_overflow_dont, /* complain_on_overflow */
1288 bfd_elf_generic_reloc, /* special_function */
1289 "R_PPC64_TLSLD", /* name */
1290 FALSE, /* partial_inplace */
1293 FALSE), /* pcrel_offset */
1295 HOWTO (R_PPC64_TOCSAVE,
1297 2, /* size (0 = byte, 1 = short, 2 = long) */
1299 FALSE, /* pc_relative */
1301 complain_overflow_dont, /* complain_on_overflow */
1302 bfd_elf_generic_reloc, /* special_function */
1303 "R_PPC64_TOCSAVE", /* name */
1304 FALSE, /* partial_inplace */
1307 FALSE), /* pcrel_offset */
1309 /* Computes the load module index of the load module that contains the
1310 definition of its TLS sym. */
1311 HOWTO (R_PPC64_DTPMOD64,
1313 4, /* size (0 = byte, 1 = short, 2 = long) */
1315 FALSE, /* pc_relative */
1317 complain_overflow_dont, /* complain_on_overflow */
1318 ppc64_elf_unhandled_reloc, /* special_function */
1319 "R_PPC64_DTPMOD64", /* name */
1320 FALSE, /* partial_inplace */
1322 ONES (64), /* dst_mask */
1323 FALSE), /* pcrel_offset */
1325 /* Computes a dtv-relative displacement, the difference between the value
1326 of sym+add and the base address of the thread-local storage block that
1327 contains the definition of sym, minus 0x8000. */
1328 HOWTO (R_PPC64_DTPREL64,
1330 4, /* size (0 = byte, 1 = short, 2 = long) */
1332 FALSE, /* pc_relative */
1334 complain_overflow_dont, /* complain_on_overflow */
1335 ppc64_elf_unhandled_reloc, /* special_function */
1336 "R_PPC64_DTPREL64", /* name */
1337 FALSE, /* partial_inplace */
1339 ONES (64), /* dst_mask */
1340 FALSE), /* pcrel_offset */
1342 /* A 16 bit dtprel reloc. */
1343 HOWTO (R_PPC64_DTPREL16,
1345 1, /* size (0 = byte, 1 = short, 2 = long) */
1347 FALSE, /* pc_relative */
1349 complain_overflow_signed, /* complain_on_overflow */
1350 ppc64_elf_unhandled_reloc, /* special_function */
1351 "R_PPC64_DTPREL16", /* name */
1352 FALSE, /* partial_inplace */
1354 0xffff, /* dst_mask */
1355 FALSE), /* pcrel_offset */
1357 /* Like DTPREL16, but no overflow. */
1358 HOWTO (R_PPC64_DTPREL16_LO,
1360 1, /* size (0 = byte, 1 = short, 2 = long) */
1362 FALSE, /* pc_relative */
1364 complain_overflow_dont, /* complain_on_overflow */
1365 ppc64_elf_unhandled_reloc, /* special_function */
1366 "R_PPC64_DTPREL16_LO", /* name */
1367 FALSE, /* partial_inplace */
1369 0xffff, /* dst_mask */
1370 FALSE), /* pcrel_offset */
1372 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1373 HOWTO (R_PPC64_DTPREL16_HI,
1374 16, /* rightshift */
1375 1, /* size (0 = byte, 1 = short, 2 = long) */
1377 FALSE, /* pc_relative */
1379 complain_overflow_signed, /* complain_on_overflow */
1380 ppc64_elf_unhandled_reloc, /* special_function */
1381 "R_PPC64_DTPREL16_HI", /* name */
1382 FALSE, /* partial_inplace */
1384 0xffff, /* dst_mask */
1385 FALSE), /* pcrel_offset */
1387 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1388 HOWTO (R_PPC64_DTPREL16_HA,
1389 16, /* rightshift */
1390 1, /* size (0 = byte, 1 = short, 2 = long) */
1392 FALSE, /* pc_relative */
1394 complain_overflow_signed, /* complain_on_overflow */
1395 ppc64_elf_unhandled_reloc, /* special_function */
1396 "R_PPC64_DTPREL16_HA", /* name */
1397 FALSE, /* partial_inplace */
1399 0xffff, /* dst_mask */
1400 FALSE), /* pcrel_offset */
1402 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1403 HOWTO (R_PPC64_DTPREL16_HIGHER,
1404 32, /* rightshift */
1405 1, /* size (0 = byte, 1 = short, 2 = long) */
1407 FALSE, /* pc_relative */
1409 complain_overflow_dont, /* complain_on_overflow */
1410 ppc64_elf_unhandled_reloc, /* special_function */
1411 "R_PPC64_DTPREL16_HIGHER", /* name */
1412 FALSE, /* partial_inplace */
1414 0xffff, /* dst_mask */
1415 FALSE), /* pcrel_offset */
1417 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1418 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1419 32, /* rightshift */
1420 1, /* size (0 = byte, 1 = short, 2 = long) */
1422 FALSE, /* pc_relative */
1424 complain_overflow_dont, /* complain_on_overflow */
1425 ppc64_elf_unhandled_reloc, /* special_function */
1426 "R_PPC64_DTPREL16_HIGHERA", /* name */
1427 FALSE, /* partial_inplace */
1429 0xffff, /* dst_mask */
1430 FALSE), /* pcrel_offset */
1432 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1433 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1434 48, /* rightshift */
1435 1, /* size (0 = byte, 1 = short, 2 = long) */
1437 FALSE, /* pc_relative */
1439 complain_overflow_dont, /* complain_on_overflow */
1440 ppc64_elf_unhandled_reloc, /* special_function */
1441 "R_PPC64_DTPREL16_HIGHEST", /* name */
1442 FALSE, /* partial_inplace */
1444 0xffff, /* dst_mask */
1445 FALSE), /* pcrel_offset */
1447 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1448 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1449 48, /* rightshift */
1450 1, /* size (0 = byte, 1 = short, 2 = long) */
1452 FALSE, /* pc_relative */
1454 complain_overflow_dont, /* complain_on_overflow */
1455 ppc64_elf_unhandled_reloc, /* special_function */
1456 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1457 FALSE, /* partial_inplace */
1459 0xffff, /* dst_mask */
1460 FALSE), /* pcrel_offset */
1462 /* Like DTPREL16, but for insns with a DS field. */
1463 HOWTO (R_PPC64_DTPREL16_DS,
1465 1, /* size (0 = byte, 1 = short, 2 = long) */
1467 FALSE, /* pc_relative */
1469 complain_overflow_signed, /* complain_on_overflow */
1470 ppc64_elf_unhandled_reloc, /* special_function */
1471 "R_PPC64_DTPREL16_DS", /* name */
1472 FALSE, /* partial_inplace */
1474 0xfffc, /* dst_mask */
1475 FALSE), /* pcrel_offset */
1477 /* Like DTPREL16_DS, but no overflow. */
1478 HOWTO (R_PPC64_DTPREL16_LO_DS,
1480 1, /* size (0 = byte, 1 = short, 2 = long) */
1482 FALSE, /* pc_relative */
1484 complain_overflow_dont, /* complain_on_overflow */
1485 ppc64_elf_unhandled_reloc, /* special_function */
1486 "R_PPC64_DTPREL16_LO_DS", /* name */
1487 FALSE, /* partial_inplace */
1489 0xfffc, /* dst_mask */
1490 FALSE), /* pcrel_offset */
1492 /* Computes a tp-relative displacement, the difference between the value of
1493 sym+add and the value of the thread pointer (r13). */
1494 HOWTO (R_PPC64_TPREL64,
1496 4, /* size (0 = byte, 1 = short, 2 = long) */
1498 FALSE, /* pc_relative */
1500 complain_overflow_dont, /* complain_on_overflow */
1501 ppc64_elf_unhandled_reloc, /* special_function */
1502 "R_PPC64_TPREL64", /* name */
1503 FALSE, /* partial_inplace */
1505 ONES (64), /* dst_mask */
1506 FALSE), /* pcrel_offset */
1508 /* A 16 bit tprel reloc. */
1509 HOWTO (R_PPC64_TPREL16,
1511 1, /* size (0 = byte, 1 = short, 2 = long) */
1513 FALSE, /* pc_relative */
1515 complain_overflow_signed, /* complain_on_overflow */
1516 ppc64_elf_unhandled_reloc, /* special_function */
1517 "R_PPC64_TPREL16", /* name */
1518 FALSE, /* partial_inplace */
1520 0xffff, /* dst_mask */
1521 FALSE), /* pcrel_offset */
1523 /* Like TPREL16, but no overflow. */
1524 HOWTO (R_PPC64_TPREL16_LO,
1526 1, /* size (0 = byte, 1 = short, 2 = long) */
1528 FALSE, /* pc_relative */
1530 complain_overflow_dont, /* complain_on_overflow */
1531 ppc64_elf_unhandled_reloc, /* special_function */
1532 "R_PPC64_TPREL16_LO", /* name */
1533 FALSE, /* partial_inplace */
1535 0xffff, /* dst_mask */
1536 FALSE), /* pcrel_offset */
1538 /* Like TPREL16_LO, but next higher group of 16 bits. */
1539 HOWTO (R_PPC64_TPREL16_HI,
1540 16, /* rightshift */
1541 1, /* size (0 = byte, 1 = short, 2 = long) */
1543 FALSE, /* pc_relative */
1545 complain_overflow_signed, /* complain_on_overflow */
1546 ppc64_elf_unhandled_reloc, /* special_function */
1547 "R_PPC64_TPREL16_HI", /* name */
1548 FALSE, /* partial_inplace */
1550 0xffff, /* dst_mask */
1551 FALSE), /* pcrel_offset */
1553 /* Like TPREL16_HI, but adjust for low 16 bits. */
1554 HOWTO (R_PPC64_TPREL16_HA,
1555 16, /* rightshift */
1556 1, /* size (0 = byte, 1 = short, 2 = long) */
1558 FALSE, /* pc_relative */
1560 complain_overflow_signed, /* complain_on_overflow */
1561 ppc64_elf_unhandled_reloc, /* special_function */
1562 "R_PPC64_TPREL16_HA", /* name */
1563 FALSE, /* partial_inplace */
1565 0xffff, /* dst_mask */
1566 FALSE), /* pcrel_offset */
1568 /* Like TPREL16_HI, but next higher group of 16 bits. */
1569 HOWTO (R_PPC64_TPREL16_HIGHER,
1570 32, /* rightshift */
1571 1, /* size (0 = byte, 1 = short, 2 = long) */
1573 FALSE, /* pc_relative */
1575 complain_overflow_dont, /* complain_on_overflow */
1576 ppc64_elf_unhandled_reloc, /* special_function */
1577 "R_PPC64_TPREL16_HIGHER", /* name */
1578 FALSE, /* partial_inplace */
1580 0xffff, /* dst_mask */
1581 FALSE), /* pcrel_offset */
1583 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1584 HOWTO (R_PPC64_TPREL16_HIGHERA,
1585 32, /* rightshift */
1586 1, /* size (0 = byte, 1 = short, 2 = long) */
1588 FALSE, /* pc_relative */
1590 complain_overflow_dont, /* complain_on_overflow */
1591 ppc64_elf_unhandled_reloc, /* special_function */
1592 "R_PPC64_TPREL16_HIGHERA", /* name */
1593 FALSE, /* partial_inplace */
1595 0xffff, /* dst_mask */
1596 FALSE), /* pcrel_offset */
1598 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1599 HOWTO (R_PPC64_TPREL16_HIGHEST,
1600 48, /* rightshift */
1601 1, /* size (0 = byte, 1 = short, 2 = long) */
1603 FALSE, /* pc_relative */
1605 complain_overflow_dont, /* complain_on_overflow */
1606 ppc64_elf_unhandled_reloc, /* special_function */
1607 "R_PPC64_TPREL16_HIGHEST", /* name */
1608 FALSE, /* partial_inplace */
1610 0xffff, /* dst_mask */
1611 FALSE), /* pcrel_offset */
1613 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1614 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1615 48, /* rightshift */
1616 1, /* size (0 = byte, 1 = short, 2 = long) */
1618 FALSE, /* pc_relative */
1620 complain_overflow_dont, /* complain_on_overflow */
1621 ppc64_elf_unhandled_reloc, /* special_function */
1622 "R_PPC64_TPREL16_HIGHESTA", /* name */
1623 FALSE, /* partial_inplace */
1625 0xffff, /* dst_mask */
1626 FALSE), /* pcrel_offset */
1628 /* Like TPREL16, but for insns with a DS field. */
1629 HOWTO (R_PPC64_TPREL16_DS,
1631 1, /* size (0 = byte, 1 = short, 2 = long) */
1633 FALSE, /* pc_relative */
1635 complain_overflow_signed, /* complain_on_overflow */
1636 ppc64_elf_unhandled_reloc, /* special_function */
1637 "R_PPC64_TPREL16_DS", /* name */
1638 FALSE, /* partial_inplace */
1640 0xfffc, /* dst_mask */
1641 FALSE), /* pcrel_offset */
1643 /* Like TPREL16_DS, but no overflow. */
1644 HOWTO (R_PPC64_TPREL16_LO_DS,
1646 1, /* size (0 = byte, 1 = short, 2 = long) */
1648 FALSE, /* pc_relative */
1650 complain_overflow_dont, /* complain_on_overflow */
1651 ppc64_elf_unhandled_reloc, /* special_function */
1652 "R_PPC64_TPREL16_LO_DS", /* name */
1653 FALSE, /* partial_inplace */
1655 0xfffc, /* dst_mask */
1656 FALSE), /* pcrel_offset */
1658 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1659 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1660 to the first entry relative to the TOC base (r2). */
1661 HOWTO (R_PPC64_GOT_TLSGD16,
1663 1, /* size (0 = byte, 1 = short, 2 = long) */
1665 FALSE, /* pc_relative */
1667 complain_overflow_signed, /* complain_on_overflow */
1668 ppc64_elf_unhandled_reloc, /* special_function */
1669 "R_PPC64_GOT_TLSGD16", /* name */
1670 FALSE, /* partial_inplace */
1672 0xffff, /* dst_mask */
1673 FALSE), /* pcrel_offset */
1675 /* Like GOT_TLSGD16, but no overflow. */
1676 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1678 1, /* size (0 = byte, 1 = short, 2 = long) */
1680 FALSE, /* pc_relative */
1682 complain_overflow_dont, /* complain_on_overflow */
1683 ppc64_elf_unhandled_reloc, /* special_function */
1684 "R_PPC64_GOT_TLSGD16_LO", /* name */
1685 FALSE, /* partial_inplace */
1687 0xffff, /* dst_mask */
1688 FALSE), /* pcrel_offset */
1690 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1691 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1692 16, /* rightshift */
1693 1, /* size (0 = byte, 1 = short, 2 = long) */
1695 FALSE, /* pc_relative */
1697 complain_overflow_signed, /* complain_on_overflow */
1698 ppc64_elf_unhandled_reloc, /* special_function */
1699 "R_PPC64_GOT_TLSGD16_HI", /* name */
1700 FALSE, /* partial_inplace */
1702 0xffff, /* dst_mask */
1703 FALSE), /* pcrel_offset */
1705 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1706 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1707 16, /* rightshift */
1708 1, /* size (0 = byte, 1 = short, 2 = long) */
1710 FALSE, /* pc_relative */
1712 complain_overflow_signed, /* complain_on_overflow */
1713 ppc64_elf_unhandled_reloc, /* special_function */
1714 "R_PPC64_GOT_TLSGD16_HA", /* name */
1715 FALSE, /* partial_inplace */
1717 0xffff, /* dst_mask */
1718 FALSE), /* pcrel_offset */
1720 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1721 with values (sym+add)@dtpmod and zero, and computes the offset to the
1722 first entry relative to the TOC base (r2). */
1723 HOWTO (R_PPC64_GOT_TLSLD16,
1725 1, /* size (0 = byte, 1 = short, 2 = long) */
1727 FALSE, /* pc_relative */
1729 complain_overflow_signed, /* complain_on_overflow */
1730 ppc64_elf_unhandled_reloc, /* special_function */
1731 "R_PPC64_GOT_TLSLD16", /* name */
1732 FALSE, /* partial_inplace */
1734 0xffff, /* dst_mask */
1735 FALSE), /* pcrel_offset */
1737 /* Like GOT_TLSLD16, but no overflow. */
1738 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1740 1, /* size (0 = byte, 1 = short, 2 = long) */
1742 FALSE, /* pc_relative */
1744 complain_overflow_dont, /* complain_on_overflow */
1745 ppc64_elf_unhandled_reloc, /* special_function */
1746 "R_PPC64_GOT_TLSLD16_LO", /* name */
1747 FALSE, /* partial_inplace */
1749 0xffff, /* dst_mask */
1750 FALSE), /* pcrel_offset */
1752 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1753 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1754 16, /* rightshift */
1755 1, /* size (0 = byte, 1 = short, 2 = long) */
1757 FALSE, /* pc_relative */
1759 complain_overflow_signed, /* complain_on_overflow */
1760 ppc64_elf_unhandled_reloc, /* special_function */
1761 "R_PPC64_GOT_TLSLD16_HI", /* name */
1762 FALSE, /* partial_inplace */
1764 0xffff, /* dst_mask */
1765 FALSE), /* pcrel_offset */
1767 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1768 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1769 16, /* rightshift */
1770 1, /* size (0 = byte, 1 = short, 2 = long) */
1772 FALSE, /* pc_relative */
1774 complain_overflow_signed, /* complain_on_overflow */
1775 ppc64_elf_unhandled_reloc, /* special_function */
1776 "R_PPC64_GOT_TLSLD16_HA", /* name */
1777 FALSE, /* partial_inplace */
1779 0xffff, /* dst_mask */
1780 FALSE), /* pcrel_offset */
1782 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1783 the offset to the entry relative to the TOC base (r2). */
1784 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1786 1, /* size (0 = byte, 1 = short, 2 = long) */
1788 FALSE, /* pc_relative */
1790 complain_overflow_signed, /* complain_on_overflow */
1791 ppc64_elf_unhandled_reloc, /* special_function */
1792 "R_PPC64_GOT_DTPREL16_DS", /* name */
1793 FALSE, /* partial_inplace */
1795 0xfffc, /* dst_mask */
1796 FALSE), /* pcrel_offset */
1798 /* Like GOT_DTPREL16_DS, but no overflow. */
1799 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1801 1, /* size (0 = byte, 1 = short, 2 = long) */
1803 FALSE, /* pc_relative */
1805 complain_overflow_dont, /* complain_on_overflow */
1806 ppc64_elf_unhandled_reloc, /* special_function */
1807 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1808 FALSE, /* partial_inplace */
1810 0xfffc, /* dst_mask */
1811 FALSE), /* pcrel_offset */
1813 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1814 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1815 16, /* rightshift */
1816 1, /* size (0 = byte, 1 = short, 2 = long) */
1818 FALSE, /* pc_relative */
1820 complain_overflow_signed, /* complain_on_overflow */
1821 ppc64_elf_unhandled_reloc, /* special_function */
1822 "R_PPC64_GOT_DTPREL16_HI", /* name */
1823 FALSE, /* partial_inplace */
1825 0xffff, /* dst_mask */
1826 FALSE), /* pcrel_offset */
1828 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1829 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1830 16, /* rightshift */
1831 1, /* size (0 = byte, 1 = short, 2 = long) */
1833 FALSE, /* pc_relative */
1835 complain_overflow_signed, /* complain_on_overflow */
1836 ppc64_elf_unhandled_reloc, /* special_function */
1837 "R_PPC64_GOT_DTPREL16_HA", /* name */
1838 FALSE, /* partial_inplace */
1840 0xffff, /* dst_mask */
1841 FALSE), /* pcrel_offset */
1843 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1844 offset to the entry relative to the TOC base (r2). */
1845 HOWTO (R_PPC64_GOT_TPREL16_DS,
1847 1, /* size (0 = byte, 1 = short, 2 = long) */
1849 FALSE, /* pc_relative */
1851 complain_overflow_signed, /* complain_on_overflow */
1852 ppc64_elf_unhandled_reloc, /* special_function */
1853 "R_PPC64_GOT_TPREL16_DS", /* name */
1854 FALSE, /* partial_inplace */
1856 0xfffc, /* dst_mask */
1857 FALSE), /* pcrel_offset */
1859 /* Like GOT_TPREL16_DS, but no overflow. */
1860 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1862 1, /* size (0 = byte, 1 = short, 2 = long) */
1864 FALSE, /* pc_relative */
1866 complain_overflow_dont, /* complain_on_overflow */
1867 ppc64_elf_unhandled_reloc, /* special_function */
1868 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1869 FALSE, /* partial_inplace */
1871 0xfffc, /* dst_mask */
1872 FALSE), /* pcrel_offset */
1874 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1875 HOWTO (R_PPC64_GOT_TPREL16_HI,
1876 16, /* rightshift */
1877 1, /* size (0 = byte, 1 = short, 2 = long) */
1879 FALSE, /* pc_relative */
1881 complain_overflow_signed, /* complain_on_overflow */
1882 ppc64_elf_unhandled_reloc, /* special_function */
1883 "R_PPC64_GOT_TPREL16_HI", /* name */
1884 FALSE, /* partial_inplace */
1886 0xffff, /* dst_mask */
1887 FALSE), /* pcrel_offset */
1889 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1890 HOWTO (R_PPC64_GOT_TPREL16_HA,
1891 16, /* rightshift */
1892 1, /* size (0 = byte, 1 = short, 2 = long) */
1894 FALSE, /* pc_relative */
1896 complain_overflow_signed, /* complain_on_overflow */
1897 ppc64_elf_unhandled_reloc, /* special_function */
1898 "R_PPC64_GOT_TPREL16_HA", /* name */
1899 FALSE, /* partial_inplace */
1901 0xffff, /* dst_mask */
1902 FALSE), /* pcrel_offset */
1904 HOWTO (R_PPC64_JMP_IREL, /* type */
1906 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1908 FALSE, /* pc_relative */
1910 complain_overflow_dont, /* complain_on_overflow */
1911 ppc64_elf_unhandled_reloc, /* special_function */
1912 "R_PPC64_JMP_IREL", /* name */
1913 FALSE, /* partial_inplace */
1916 FALSE), /* pcrel_offset */
1918 HOWTO (R_PPC64_IRELATIVE, /* type */
1920 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1922 FALSE, /* pc_relative */
1924 complain_overflow_dont, /* complain_on_overflow */
1925 bfd_elf_generic_reloc, /* special_function */
1926 "R_PPC64_IRELATIVE", /* name */
1927 FALSE, /* partial_inplace */
1929 ONES (64), /* dst_mask */
1930 FALSE), /* pcrel_offset */
1932 /* A 16 bit relative relocation. */
1933 HOWTO (R_PPC64_REL16, /* type */
1935 1, /* size (0 = byte, 1 = short, 2 = long) */
1937 TRUE, /* pc_relative */
1939 complain_overflow_bitfield, /* complain_on_overflow */
1940 bfd_elf_generic_reloc, /* special_function */
1941 "R_PPC64_REL16", /* name */
1942 FALSE, /* partial_inplace */
1944 0xffff, /* dst_mask */
1945 TRUE), /* pcrel_offset */
1947 /* A 16 bit relative relocation without overflow. */
1948 HOWTO (R_PPC64_REL16_LO, /* type */
1950 1, /* size (0 = byte, 1 = short, 2 = long) */
1952 TRUE, /* pc_relative */
1954 complain_overflow_dont,/* complain_on_overflow */
1955 bfd_elf_generic_reloc, /* special_function */
1956 "R_PPC64_REL16_LO", /* name */
1957 FALSE, /* partial_inplace */
1959 0xffff, /* dst_mask */
1960 TRUE), /* pcrel_offset */
1962 /* The high order 16 bits of a relative address. */
1963 HOWTO (R_PPC64_REL16_HI, /* type */
1964 16, /* rightshift */
1965 1, /* size (0 = byte, 1 = short, 2 = long) */
1967 TRUE, /* pc_relative */
1969 complain_overflow_signed, /* complain_on_overflow */
1970 bfd_elf_generic_reloc, /* special_function */
1971 "R_PPC64_REL16_HI", /* name */
1972 FALSE, /* partial_inplace */
1974 0xffff, /* dst_mask */
1975 TRUE), /* pcrel_offset */
1977 /* The high order 16 bits of a relative address, plus 1 if the contents of
1978 the low 16 bits, treated as a signed number, is negative. */
1979 HOWTO (R_PPC64_REL16_HA, /* type */
1980 16, /* rightshift */
1981 1, /* size (0 = byte, 1 = short, 2 = long) */
1983 TRUE, /* pc_relative */
1985 complain_overflow_signed, /* complain_on_overflow */
1986 ppc64_elf_ha_reloc, /* special_function */
1987 "R_PPC64_REL16_HA", /* name */
1988 FALSE, /* partial_inplace */
1990 0xffff, /* dst_mask */
1991 TRUE), /* pcrel_offset */
1993 /* Like R_PPC64_ADDR16_HI, but no overflow. */
1994 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
1995 16, /* rightshift */
1996 1, /* size (0 = byte, 1 = short, 2 = long) */
1998 FALSE, /* pc_relative */
2000 complain_overflow_dont, /* complain_on_overflow */
2001 bfd_elf_generic_reloc, /* special_function */
2002 "R_PPC64_ADDR16_HIGH", /* name */
2003 FALSE, /* partial_inplace */
2005 0xffff, /* dst_mask */
2006 FALSE), /* pcrel_offset */
2008 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2009 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2010 16, /* rightshift */
2011 1, /* size (0 = byte, 1 = short, 2 = long) */
2013 FALSE, /* pc_relative */
2015 complain_overflow_dont, /* complain_on_overflow */
2016 ppc64_elf_ha_reloc, /* special_function */
2017 "R_PPC64_ADDR16_HIGHA", /* name */
2018 FALSE, /* partial_inplace */
2020 0xffff, /* dst_mask */
2021 FALSE), /* pcrel_offset */
2023 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2024 HOWTO (R_PPC64_DTPREL16_HIGH,
2025 16, /* rightshift */
2026 1, /* size (0 = byte, 1 = short, 2 = long) */
2028 FALSE, /* pc_relative */
2030 complain_overflow_dont, /* complain_on_overflow */
2031 ppc64_elf_unhandled_reloc, /* special_function */
2032 "R_PPC64_DTPREL16_HIGH", /* name */
2033 FALSE, /* partial_inplace */
2035 0xffff, /* dst_mask */
2036 FALSE), /* pcrel_offset */
2038 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2039 HOWTO (R_PPC64_DTPREL16_HIGHA,
2040 16, /* rightshift */
2041 1, /* size (0 = byte, 1 = short, 2 = long) */
2043 FALSE, /* pc_relative */
2045 complain_overflow_dont, /* complain_on_overflow */
2046 ppc64_elf_unhandled_reloc, /* special_function */
2047 "R_PPC64_DTPREL16_HIGHA", /* name */
2048 FALSE, /* partial_inplace */
2050 0xffff, /* dst_mask */
2051 FALSE), /* pcrel_offset */
2053 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2054 HOWTO (R_PPC64_TPREL16_HIGH,
2055 16, /* rightshift */
2056 1, /* size (0 = byte, 1 = short, 2 = long) */
2058 FALSE, /* pc_relative */
2060 complain_overflow_dont, /* complain_on_overflow */
2061 ppc64_elf_unhandled_reloc, /* special_function */
2062 "R_PPC64_TPREL16_HIGH", /* name */
2063 FALSE, /* partial_inplace */
2065 0xffff, /* dst_mask */
2066 FALSE), /* pcrel_offset */
2068 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2069 HOWTO (R_PPC64_TPREL16_HIGHA,
2070 16, /* rightshift */
2071 1, /* size (0 = byte, 1 = short, 2 = long) */
2073 FALSE, /* pc_relative */
2075 complain_overflow_dont, /* complain_on_overflow */
2076 ppc64_elf_unhandled_reloc, /* special_function */
2077 "R_PPC64_TPREL16_HIGHA", /* name */
2078 FALSE, /* partial_inplace */
2080 0xffff, /* dst_mask */
2081 FALSE), /* pcrel_offset */
2083 /* GNU extension to record C++ vtable hierarchy. */
2084 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2086 0, /* size (0 = byte, 1 = short, 2 = long) */
2088 FALSE, /* pc_relative */
2090 complain_overflow_dont, /* complain_on_overflow */
2091 NULL, /* special_function */
2092 "R_PPC64_GNU_VTINHERIT", /* name */
2093 FALSE, /* partial_inplace */
2096 FALSE), /* pcrel_offset */
2098 /* GNU extension to record C++ vtable member usage. */
2099 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2101 0, /* size (0 = byte, 1 = short, 2 = long) */
2103 FALSE, /* pc_relative */
2105 complain_overflow_dont, /* complain_on_overflow */
2106 NULL, /* special_function */
2107 "R_PPC64_GNU_VTENTRY", /* name */
2108 FALSE, /* partial_inplace */
2111 FALSE), /* pcrel_offset */
2115 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2119 ppc_howto_init (void)
2121 unsigned int i, type;
2124 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2127 type = ppc64_elf_howto_raw[i].type;
2128 BFD_ASSERT (type < (sizeof (ppc64_elf_howto_table)
2129 / sizeof (ppc64_elf_howto_table[0])));
2130 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2134 static reloc_howto_type *
2135 ppc64_elf_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2136 bfd_reloc_code_real_type code)
2138 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2140 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2141 /* Initialize howto table if needed. */
2149 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2151 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2153 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2155 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2157 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2159 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2161 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2163 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2165 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2167 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2169 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2171 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2173 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2175 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2177 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2179 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2181 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2183 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2185 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2187 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2189 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2191 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2193 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2195 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2197 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2199 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2201 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2203 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2205 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2207 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2209 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2211 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2213 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2215 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2217 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2219 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2221 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2223 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2225 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2227 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2229 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2231 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2233 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2235 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2237 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2239 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2241 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2243 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2245 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2247 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2249 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2251 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2253 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2255 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2257 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2259 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2261 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2263 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2265 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2267 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2269 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2271 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2273 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2275 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2277 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2279 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2281 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2283 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2285 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2287 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2289 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2291 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2293 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2295 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2297 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2299 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2301 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2303 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2305 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2307 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2309 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2311 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2313 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2315 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2317 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2319 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2321 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2323 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2325 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2327 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2329 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2331 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2333 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2335 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2337 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2339 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2341 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2343 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2345 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2347 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2349 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2351 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2353 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2355 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2357 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2359 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2361 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2363 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2365 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2367 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2369 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2371 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2373 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2377 return ppc64_elf_howto_table[r];
2380 static reloc_howto_type *
2381 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2387 i < sizeof (ppc64_elf_howto_raw) / sizeof (ppc64_elf_howto_raw[0]);
2389 if (ppc64_elf_howto_raw[i].name != NULL
2390 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2391 return &ppc64_elf_howto_raw[i];
2396 /* Set the howto pointer for a PowerPC ELF reloc. */
2399 ppc64_elf_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *cache_ptr,
2400 Elf_Internal_Rela *dst)
2404 /* Initialize howto table if needed. */
2405 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2408 type = ELF64_R_TYPE (dst->r_info);
2409 if (type >= (sizeof (ppc64_elf_howto_table)
2410 / sizeof (ppc64_elf_howto_table[0])))
2412 (*_bfd_error_handler) (_("%B: invalid relocation type %d"),
2414 type = R_PPC64_NONE;
2416 cache_ptr->howto = ppc64_elf_howto_table[type];
2419 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2421 static bfd_reloc_status_type
2422 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2423 void *data, asection *input_section,
2424 bfd *output_bfd, char **error_message)
2426 /* If this is a relocatable link (output_bfd test tells us), just
2427 call the generic function. Any adjustment will be done at final
2429 if (output_bfd != NULL)
2430 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2431 input_section, output_bfd, error_message);
2433 /* Adjust the addend for sign extension of the low 16 bits.
2434 We won't actually be using the low 16 bits, so trashing them
2436 reloc_entry->addend += 0x8000;
2437 return bfd_reloc_continue;
2440 static bfd_reloc_status_type
2441 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2442 void *data, asection *input_section,
2443 bfd *output_bfd, char **error_message)
2445 if (output_bfd != NULL)
2446 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2447 input_section, output_bfd, error_message);
2449 if (strcmp (symbol->section->name, ".opd") == 0
2450 && (symbol->section->owner->flags & DYNAMIC) == 0)
2452 bfd_vma dest = opd_entry_value (symbol->section,
2453 symbol->value + reloc_entry->addend,
2455 if (dest != (bfd_vma) -1)
2456 reloc_entry->addend = dest - (symbol->value
2457 + symbol->section->output_section->vma
2458 + symbol->section->output_offset);
2460 return bfd_reloc_continue;
2463 static bfd_reloc_status_type
2464 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2465 void *data, asection *input_section,
2466 bfd *output_bfd, char **error_message)
2469 enum elf_ppc64_reloc_type r_type;
2470 bfd_size_type octets;
2471 /* Assume 'at' branch hints. */
2472 bfd_boolean is_isa_v2 = TRUE;
2474 /* If this is a relocatable link (output_bfd test tells us), just
2475 call the generic function. Any adjustment will be done at final
2477 if (output_bfd != NULL)
2478 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2479 input_section, output_bfd, error_message);
2481 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2482 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2483 insn &= ~(0x01 << 21);
2484 r_type = reloc_entry->howto->type;
2485 if (r_type == R_PPC64_ADDR14_BRTAKEN
2486 || r_type == R_PPC64_REL14_BRTAKEN)
2487 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2491 /* Set 'a' bit. This is 0b00010 in BO field for branch
2492 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2493 for branch on CTR insns (BO == 1a00t or 1a01t). */
2494 if ((insn & (0x14 << 21)) == (0x04 << 21))
2496 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2506 if (!bfd_is_com_section (symbol->section))
2507 target = symbol->value;
2508 target += symbol->section->output_section->vma;
2509 target += symbol->section->output_offset;
2510 target += reloc_entry->addend;
2512 from = (reloc_entry->address
2513 + input_section->output_offset
2514 + input_section->output_section->vma);
2516 /* Invert 'y' bit if not the default. */
2517 if ((bfd_signed_vma) (target - from) < 0)
2520 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2522 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2523 input_section, output_bfd, error_message);
2526 static bfd_reloc_status_type
2527 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2528 void *data, asection *input_section,
2529 bfd *output_bfd, char **error_message)
2531 /* If this is a relocatable link (output_bfd test tells us), just
2532 call the generic function. Any adjustment will be done at final
2534 if (output_bfd != NULL)
2535 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2536 input_section, output_bfd, error_message);
2538 /* Subtract the symbol section base address. */
2539 reloc_entry->addend -= symbol->section->output_section->vma;
2540 return bfd_reloc_continue;
2543 static bfd_reloc_status_type
2544 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2545 void *data, asection *input_section,
2546 bfd *output_bfd, char **error_message)
2548 /* If this is a relocatable link (output_bfd test tells us), just
2549 call the generic function. Any adjustment will be done at final
2551 if (output_bfd != NULL)
2552 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2553 input_section, output_bfd, error_message);
2555 /* Subtract the symbol section base address. */
2556 reloc_entry->addend -= symbol->section->output_section->vma;
2558 /* Adjust the addend for sign extension of the low 16 bits. */
2559 reloc_entry->addend += 0x8000;
2560 return bfd_reloc_continue;
2563 static bfd_reloc_status_type
2564 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2565 void *data, asection *input_section,
2566 bfd *output_bfd, char **error_message)
2570 /* If this is a relocatable link (output_bfd test tells us), just
2571 call the generic function. Any adjustment will be done at final
2573 if (output_bfd != NULL)
2574 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2575 input_section, output_bfd, error_message);
2577 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2579 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2581 /* Subtract the TOC base address. */
2582 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2583 return bfd_reloc_continue;
2586 static bfd_reloc_status_type
2587 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2588 void *data, asection *input_section,
2589 bfd *output_bfd, char **error_message)
2593 /* If this is a relocatable link (output_bfd test tells us), just
2594 call the generic function. Any adjustment will be done at final
2596 if (output_bfd != NULL)
2597 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2598 input_section, output_bfd, error_message);
2600 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2602 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2604 /* Subtract the TOC base address. */
2605 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2607 /* Adjust the addend for sign extension of the low 16 bits. */
2608 reloc_entry->addend += 0x8000;
2609 return bfd_reloc_continue;
2612 static bfd_reloc_status_type
2613 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2614 void *data, asection *input_section,
2615 bfd *output_bfd, char **error_message)
2618 bfd_size_type octets;
2620 /* If this is a relocatable link (output_bfd test tells us), just
2621 call the generic function. Any adjustment will be done at final
2623 if (output_bfd != NULL)
2624 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2625 input_section, output_bfd, error_message);
2627 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2629 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2631 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2632 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2633 return bfd_reloc_ok;
2636 static bfd_reloc_status_type
2637 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2638 void *data, asection *input_section,
2639 bfd *output_bfd, char **error_message)
2641 /* If this is a relocatable link (output_bfd test tells us), just
2642 call the generic function. Any adjustment will be done at final
2644 if (output_bfd != NULL)
2645 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2646 input_section, output_bfd, error_message);
2648 if (error_message != NULL)
2650 static char buf[60];
2651 sprintf (buf, "generic linker can't handle %s",
2652 reloc_entry->howto->name);
2653 *error_message = buf;
2655 return bfd_reloc_dangerous;
2658 /* Track GOT entries needed for a given symbol. We might need more
2659 than one got entry per symbol. */
2662 struct got_entry *next;
2664 /* The symbol addend that we'll be placing in the GOT. */
2667 /* Unlike other ELF targets, we use separate GOT entries for the same
2668 symbol referenced from different input files. This is to support
2669 automatic multiple TOC/GOT sections, where the TOC base can vary
2670 from one input file to another. After partitioning into TOC groups
2671 we merge entries within the group.
2673 Point to the BFD owning this GOT entry. */
2676 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2677 TLS_TPREL or TLS_DTPREL for tls entries. */
2678 unsigned char tls_type;
2680 /* Non-zero if got.ent points to real entry. */
2681 unsigned char is_indirect;
2683 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2686 bfd_signed_vma refcount;
2688 struct got_entry *ent;
2692 /* The same for PLT. */
2695 struct plt_entry *next;
2701 bfd_signed_vma refcount;
2706 struct ppc64_elf_obj_tdata
2708 struct elf_obj_tdata elf;
2710 /* Shortcuts to dynamic linker sections. */
2714 /* Used during garbage collection. We attach global symbols defined
2715 on removed .opd entries to this section so that the sym is removed. */
2716 asection *deleted_section;
2718 /* TLS local dynamic got entry handling. Support for multiple GOT
2719 sections means we potentially need one of these for each input bfd. */
2720 struct got_entry tlsld_got;
2723 /* A copy of relocs before they are modified for --emit-relocs. */
2724 Elf_Internal_Rela *relocs;
2726 /* Section contents. */
2730 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2731 the reloc to be in the range -32768 to 32767. */
2732 unsigned int has_small_toc_reloc : 1;
2734 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2735 instruction not one we handle. */
2736 unsigned int unexpected_toc_insn : 1;
2739 #define ppc64_elf_tdata(bfd) \
2740 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2742 #define ppc64_tlsld_got(bfd) \
2743 (&ppc64_elf_tdata (bfd)->tlsld_got)
2745 #define is_ppc64_elf(bfd) \
2746 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2747 && elf_object_id (bfd) == PPC64_ELF_DATA)
2749 /* Override the generic function because we store some extras. */
2752 ppc64_elf_mkobject (bfd *abfd)
2754 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2758 /* Fix bad default arch selected for a 64 bit input bfd when the
2759 default is 32 bit. */
2762 ppc64_elf_object_p (bfd *abfd)
2764 if (abfd->arch_info->the_default && abfd->arch_info->bits_per_word == 32)
2766 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
2768 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
2770 /* Relies on arch after 32 bit default being 64 bit default. */
2771 abfd->arch_info = abfd->arch_info->next;
2772 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
2778 /* Support for core dump NOTE sections. */
2781 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2783 size_t offset, size;
2785 if (note->descsz != 504)
2789 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2792 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
2798 /* Make a ".reg/999" section. */
2799 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
2800 size, note->descpos + offset);
2804 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
2806 if (note->descsz != 136)
2809 elf_tdata (abfd)->core->pid
2810 = bfd_get_32 (abfd, note->descdata + 24);
2811 elf_tdata (abfd)->core->program
2812 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
2813 elf_tdata (abfd)->core->command
2814 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
2820 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
2833 va_start (ap, note_type);
2834 memset (data, 0, sizeof (data));
2835 strncpy (data + 40, va_arg (ap, const char *), 16);
2836 strncpy (data + 56, va_arg (ap, const char *), 80);
2838 return elfcore_write_note (abfd, buf, bufsiz,
2839 "CORE", note_type, data, sizeof (data));
2850 va_start (ap, note_type);
2851 memset (data, 0, 112);
2852 pid = va_arg (ap, long);
2853 bfd_put_32 (abfd, pid, data + 32);
2854 cursig = va_arg (ap, int);
2855 bfd_put_16 (abfd, cursig, data + 12);
2856 greg = va_arg (ap, const void *);
2857 memcpy (data + 112, greg, 384);
2858 memset (data + 496, 0, 8);
2860 return elfcore_write_note (abfd, buf, bufsiz,
2861 "CORE", note_type, data, sizeof (data));
2866 /* Add extra PPC sections. */
2868 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
2870 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
2871 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2872 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2873 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2874 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
2875 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
2876 { NULL, 0, 0, 0, 0 }
2879 enum _ppc64_sec_type {
2885 struct _ppc64_elf_section_data
2887 struct bfd_elf_section_data elf;
2891 /* An array with one entry for each opd function descriptor. */
2892 struct _opd_sec_data
2894 /* Points to the function code section for local opd entries. */
2895 asection **func_sec;
2897 /* After editing .opd, adjust references to opd local syms. */
2901 /* An array for toc sections, indexed by offset/8. */
2902 struct _toc_sec_data
2904 /* Specifies the relocation symbol index used at a given toc offset. */
2907 /* And the relocation addend. */
2912 enum _ppc64_sec_type sec_type:2;
2914 /* Flag set when small branches are detected. Used to
2915 select suitable defaults for the stub group size. */
2916 unsigned int has_14bit_branch:1;
2919 #define ppc64_elf_section_data(sec) \
2920 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2923 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2925 if (!sec->used_by_bfd)
2927 struct _ppc64_elf_section_data *sdata;
2928 bfd_size_type amt = sizeof (*sdata);
2930 sdata = bfd_zalloc (abfd, amt);
2933 sec->used_by_bfd = sdata;
2936 return _bfd_elf_new_section_hook (abfd, sec);
2939 static struct _opd_sec_data *
2940 get_opd_info (asection * sec)
2943 && ppc64_elf_section_data (sec) != NULL
2944 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2945 return &ppc64_elf_section_data (sec)->u.opd;
2950 abiversion (bfd *abfd)
2952 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
2956 set_abiversion (bfd *abfd, int ver)
2958 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
2959 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
2962 /* Parameters for the qsort hook. */
2963 static bfd_boolean synthetic_relocatable;
2965 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2968 compare_symbols (const void *ap, const void *bp)
2970 const asymbol *a = * (const asymbol **) ap;
2971 const asymbol *b = * (const asymbol **) bp;
2973 /* Section symbols first. */
2974 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2976 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2979 /* then .opd symbols. */
2980 if (strcmp (a->section->name, ".opd") == 0
2981 && strcmp (b->section->name, ".opd") != 0)
2983 if (strcmp (a->section->name, ".opd") != 0
2984 && strcmp (b->section->name, ".opd") == 0)
2987 /* then other code symbols. */
2988 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2989 == (SEC_CODE | SEC_ALLOC)
2990 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2991 != (SEC_CODE | SEC_ALLOC))
2994 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2995 != (SEC_CODE | SEC_ALLOC)
2996 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2997 == (SEC_CODE | SEC_ALLOC))
3000 if (synthetic_relocatable)
3002 if (a->section->id < b->section->id)
3005 if (a->section->id > b->section->id)
3009 if (a->value + a->section->vma < b->value + b->section->vma)
3012 if (a->value + a->section->vma > b->value + b->section->vma)
3015 /* For syms with the same value, prefer strong dynamic global function
3016 syms over other syms. */
3017 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3020 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3023 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3026 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3029 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3032 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3035 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3038 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3044 /* Search SYMS for a symbol of the given VALUE. */
3047 sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
3055 mid = (lo + hi) >> 1;
3056 if (syms[mid]->value + syms[mid]->section->vma < value)
3058 else if (syms[mid]->value + syms[mid]->section->vma > value)
3068 mid = (lo + hi) >> 1;
3069 if (syms[mid]->section->id < id)
3071 else if (syms[mid]->section->id > id)
3073 else if (syms[mid]->value < value)
3075 else if (syms[mid]->value > value)
3085 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3087 bfd_vma vma = *(bfd_vma *) ptr;
3088 return ((section->flags & SEC_ALLOC) != 0
3089 && section->vma <= vma
3090 && vma < section->vma + section->size);
3093 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3094 entry syms. Also generate @plt symbols for the glink branch table. */
3097 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3098 long static_count, asymbol **static_syms,
3099 long dyn_count, asymbol **dyn_syms,
3106 long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3107 asection *opd = NULL;
3108 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3110 int abi = abiversion (abfd);
3116 opd = bfd_get_section_by_name (abfd, ".opd");
3117 if (opd == NULL && abi == 1)
3121 symcount = static_count;
3123 symcount += dyn_count;
3127 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3131 if (!relocatable && static_count != 0 && dyn_count != 0)
3133 /* Use both symbol tables. */
3134 memcpy (syms, static_syms, static_count * sizeof (*syms));
3135 memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
3137 else if (!relocatable && static_count == 0)
3138 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3140 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3142 synthetic_relocatable = relocatable;
3143 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3145 if (!relocatable && symcount > 1)
3148 /* Trim duplicate syms, since we may have merged the normal and
3149 dynamic symbols. Actually, we only care about syms that have
3150 different values, so trim any with the same value. */
3151 for (i = 1, j = 1; i < symcount; ++i)
3152 if (syms[i - 1]->value + syms[i - 1]->section->vma
3153 != syms[i]->value + syms[i]->section->vma)
3154 syms[j++] = syms[i];
3159 if (strcmp (syms[i]->section->name, ".opd") == 0)
3163 for (; i < symcount; ++i)
3164 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3165 != (SEC_CODE | SEC_ALLOC))
3166 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3170 for (; i < symcount; ++i)
3171 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3175 for (; i < symcount; ++i)
3176 if (strcmp (syms[i]->section->name, ".opd") != 0)
3180 for (; i < symcount; ++i)
3181 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3182 != (SEC_CODE | SEC_ALLOC))
3190 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3195 if (opdsymend == secsymend)
3198 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3199 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3203 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3210 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3214 while (r < opd->relocation + relcount
3215 && r->address < syms[i]->value + opd->vma)
3218 if (r == opd->relocation + relcount)
3221 if (r->address != syms[i]->value + opd->vma)
3224 if (r->howto->type != R_PPC64_ADDR64)
3227 sym = *r->sym_ptr_ptr;
3228 if (!sym_exists_at (syms, opdsymend, symcount,
3229 sym->section->id, sym->value + r->addend))
3232 size += sizeof (asymbol);
3233 size += strlen (syms[i]->name) + 2;
3237 s = *ret = bfd_malloc (size);
3244 names = (char *) (s + count);
3246 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3250 while (r < opd->relocation + relcount
3251 && r->address < syms[i]->value + opd->vma)
3254 if (r == opd->relocation + relcount)
3257 if (r->address != syms[i]->value + opd->vma)
3260 if (r->howto->type != R_PPC64_ADDR64)
3263 sym = *r->sym_ptr_ptr;
3264 if (!sym_exists_at (syms, opdsymend, symcount,
3265 sym->section->id, sym->value + r->addend))
3270 s->flags |= BSF_SYNTHETIC;
3271 s->section = sym->section;
3272 s->value = sym->value + r->addend;
3275 len = strlen (syms[i]->name);
3276 memcpy (names, syms[i]->name, len + 1);
3278 /* Have udata.p point back to the original symbol this
3279 synthetic symbol was derived from. */
3280 s->udata.p = syms[i];
3287 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3288 bfd_byte *contents = NULL;
3291 bfd_vma glink_vma = 0, resolv_vma = 0;
3292 asection *dynamic, *glink = NULL, *relplt = NULL;
3295 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3297 free_contents_and_exit:
3305 for (i = secsymend; i < opdsymend; ++i)
3309 /* Ignore bogus symbols. */
3310 if (syms[i]->value > opd->size - 8)
3313 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3314 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3317 size += sizeof (asymbol);
3318 size += strlen (syms[i]->name) + 2;
3322 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3324 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3326 bfd_byte *dynbuf, *extdyn, *extdynend;
3328 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3330 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3331 goto free_contents_and_exit;
3333 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3334 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3337 extdynend = extdyn + dynamic->size;
3338 for (; extdyn < extdynend; extdyn += extdynsize)
3340 Elf_Internal_Dyn dyn;
3341 (*swap_dyn_in) (abfd, extdyn, &dyn);
3343 if (dyn.d_tag == DT_NULL)
3346 if (dyn.d_tag == DT_PPC64_GLINK)
3348 /* The first glink stub starts at offset 32; see comment in
3349 ppc64_elf_finish_dynamic_sections. */
3350 glink_vma = dyn.d_un.d_val + 32;
3351 /* The .glink section usually does not survive the final
3352 link; search for the section (usually .text) where the
3353 glink stubs now reside. */
3354 glink = bfd_sections_find_if (abfd, section_covers_vma,
3365 /* Determine __glink trampoline by reading the relative branch
3366 from the first glink stub. */
3368 if (bfd_get_section_contents (abfd, glink, buf,
3369 glink_vma + 4 - glink->vma, 4))
3371 unsigned int insn = bfd_get_32 (abfd, buf);
3373 if ((insn & ~0x3fffffc) == 0)
3374 resolv_vma = glink_vma + 4 + (insn ^ 0x2000000) - 0x2000000;
3378 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3380 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3383 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3384 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3385 goto free_contents_and_exit;
3387 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3388 size += plt_count * sizeof (asymbol);
3390 p = relplt->relocation;
3391 for (i = 0; i < plt_count; i++, p++)
3393 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3395 size += sizeof ("+0x") - 1 + 16;
3400 s = *ret = bfd_malloc (size);
3402 goto free_contents_and_exit;
3404 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3406 for (i = secsymend; i < opdsymend; ++i)
3410 if (syms[i]->value > opd->size - 8)
3413 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3414 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3418 asection *sec = abfd->sections;
3425 long mid = (lo + hi) >> 1;
3426 if (syms[mid]->section->vma < ent)
3428 else if (syms[mid]->section->vma > ent)
3432 sec = syms[mid]->section;
3437 if (lo >= hi && lo > codesecsym)
3438 sec = syms[lo - 1]->section;
3440 for (; sec != NULL; sec = sec->next)
3444 /* SEC_LOAD may not be set if SEC is from a separate debug
3446 if ((sec->flags & SEC_ALLOC) == 0)
3448 if ((sec->flags & SEC_CODE) != 0)
3451 s->flags |= BSF_SYNTHETIC;
3452 s->value = ent - s->section->vma;
3455 len = strlen (syms[i]->name);
3456 memcpy (names, syms[i]->name, len + 1);
3458 /* Have udata.p point back to the original symbol this
3459 synthetic symbol was derived from. */
3460 s->udata.p = syms[i];
3466 if (glink != NULL && relplt != NULL)
3470 /* Add a symbol for the main glink trampoline. */
3471 memset (s, 0, sizeof *s);
3473 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3475 s->value = resolv_vma - glink->vma;
3477 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3478 names += sizeof ("__glink_PLTresolve");
3483 /* FIXME: It would be very much nicer to put sym@plt on the
3484 stub rather than on the glink branch table entry. The
3485 objdump disassembler would then use a sensible symbol
3486 name on plt calls. The difficulty in doing so is
3487 a) finding the stubs, and,
3488 b) matching stubs against plt entries, and,
3489 c) there can be multiple stubs for a given plt entry.
3491 Solving (a) could be done by code scanning, but older
3492 ppc64 binaries used different stubs to current code.
3493 (b) is the tricky one since you need to known the toc
3494 pointer for at least one function that uses a pic stub to
3495 be able to calculate the plt address referenced.
3496 (c) means gdb would need to set multiple breakpoints (or
3497 find the glink branch itself) when setting breakpoints
3498 for pending shared library loads. */
3499 p = relplt->relocation;
3500 for (i = 0; i < plt_count; i++, p++)
3504 *s = **p->sym_ptr_ptr;
3505 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3506 we are defining a symbol, ensure one of them is set. */
3507 if ((s->flags & BSF_LOCAL) == 0)
3508 s->flags |= BSF_GLOBAL;
3509 s->flags |= BSF_SYNTHETIC;
3511 s->value = glink_vma - glink->vma;
3514 len = strlen ((*p->sym_ptr_ptr)->name);
3515 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3519 memcpy (names, "+0x", sizeof ("+0x") - 1);
3520 names += sizeof ("+0x") - 1;
3521 bfd_sprintf_vma (abfd, names, p->addend);
3522 names += strlen (names);
3524 memcpy (names, "@plt", sizeof ("@plt"));
3525 names += sizeof ("@plt");
3540 /* The following functions are specific to the ELF linker, while
3541 functions above are used generally. Those named ppc64_elf_* are
3542 called by the main ELF linker code. They appear in this file more
3543 or less in the order in which they are called. eg.
3544 ppc64_elf_check_relocs is called early in the link process,
3545 ppc64_elf_finish_dynamic_sections is one of the last functions
3548 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3549 functions have both a function code symbol and a function descriptor
3550 symbol. A call to foo in a relocatable object file looks like:
3557 The function definition in another object file might be:
3561 . .quad .TOC.@tocbase
3567 When the linker resolves the call during a static link, the branch
3568 unsurprisingly just goes to .foo and the .opd information is unused.
3569 If the function definition is in a shared library, things are a little
3570 different: The call goes via a plt call stub, the opd information gets
3571 copied to the plt, and the linker patches the nop.
3579 . std 2,40(1) # in practice, the call stub
3580 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3581 . addi 11,11,Lfoo@toc@l # this is the general idea
3589 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3591 The "reloc ()" notation is supposed to indicate that the linker emits
3592 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3595 What are the difficulties here? Well, firstly, the relocations
3596 examined by the linker in check_relocs are against the function code
3597 sym .foo, while the dynamic relocation in the plt is emitted against
3598 the function descriptor symbol, foo. Somewhere along the line, we need
3599 to carefully copy dynamic link information from one symbol to the other.
3600 Secondly, the generic part of the elf linker will make .foo a dynamic
3601 symbol as is normal for most other backends. We need foo dynamic
3602 instead, at least for an application final link. However, when
3603 creating a shared library containing foo, we need to have both symbols
3604 dynamic so that references to .foo are satisfied during the early
3605 stages of linking. Otherwise the linker might decide to pull in a
3606 definition from some other object, eg. a static library.
3608 Update: As of August 2004, we support a new convention. Function
3609 calls may use the function descriptor symbol, ie. "bl foo". This
3610 behaves exactly as "bl .foo". */
3612 /* Of those relocs that might be copied as dynamic relocs, this function
3613 selects those that must be copied when linking a shared library,
3614 even when the symbol is local. */
3617 must_be_dyn_reloc (struct bfd_link_info *info,
3618 enum elf_ppc64_reloc_type r_type)
3630 case R_PPC64_TPREL16:
3631 case R_PPC64_TPREL16_LO:
3632 case R_PPC64_TPREL16_HI:
3633 case R_PPC64_TPREL16_HA:
3634 case R_PPC64_TPREL16_DS:
3635 case R_PPC64_TPREL16_LO_DS:
3636 case R_PPC64_TPREL16_HIGH:
3637 case R_PPC64_TPREL16_HIGHA:
3638 case R_PPC64_TPREL16_HIGHER:
3639 case R_PPC64_TPREL16_HIGHERA:
3640 case R_PPC64_TPREL16_HIGHEST:
3641 case R_PPC64_TPREL16_HIGHESTA:
3642 case R_PPC64_TPREL64:
3643 return !info->executable;
3647 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3648 copying dynamic variables from a shared lib into an app's dynbss
3649 section, and instead use a dynamic relocation to point into the
3650 shared lib. With code that gcc generates, it's vital that this be
3651 enabled; In the PowerPC64 ABI, the address of a function is actually
3652 the address of a function descriptor, which resides in the .opd
3653 section. gcc uses the descriptor directly rather than going via the
3654 GOT as some other ABI's do, which means that initialized function
3655 pointers must reference the descriptor. Thus, a function pointer
3656 initialized to the address of a function in a shared library will
3657 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3658 redefines the function descriptor symbol to point to the copy. This
3659 presents a problem as a plt entry for that function is also
3660 initialized from the function descriptor symbol and the copy reloc
3661 may not be initialized first. */
3662 #define ELIMINATE_COPY_RELOCS 1
3664 /* Section name for stubs is the associated section name plus this
3666 #define STUB_SUFFIX ".stub"
3669 ppc_stub_long_branch:
3670 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3671 destination, but a 24 bit branch in a stub section will reach.
3674 ppc_stub_plt_branch:
3675 Similar to the above, but a 24 bit branch in the stub section won't
3676 reach its destination.
3677 . addis %r11,%r2,xxx@toc@ha
3678 . ld %r12,xxx@toc@l(%r11)
3683 Used to call a function in a shared library. If it so happens that
3684 the plt entry referenced crosses a 64k boundary, then an extra
3685 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3687 . addis %r11,%r2,xxx@toc@ha
3688 . ld %r12,xxx+0@toc@l(%r11)
3690 . ld %r2,xxx+8@toc@l(%r11)
3691 . ld %r11,xxx+16@toc@l(%r11)
3694 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3695 code to adjust the value and save r2 to support multiple toc sections.
3696 A ppc_stub_long_branch with an r2 offset looks like:
3698 . addis %r2,%r2,off@ha
3699 . addi %r2,%r2,off@l
3702 A ppc_stub_plt_branch with an r2 offset looks like:
3704 . addis %r11,%r2,xxx@toc@ha
3705 . ld %r12,xxx@toc@l(%r11)
3706 . addis %r2,%r2,off@ha
3707 . addi %r2,%r2,off@l
3711 In cases where the "addis" instruction would add zero, the "addis" is
3712 omitted and following instructions modified slightly in some cases.
3715 enum ppc_stub_type {
3717 ppc_stub_long_branch,
3718 ppc_stub_long_branch_r2off,
3719 ppc_stub_plt_branch,
3720 ppc_stub_plt_branch_r2off,
3722 ppc_stub_plt_call_r2save
3725 struct ppc_stub_hash_entry {
3727 /* Base hash table entry structure. */
3728 struct bfd_hash_entry root;
3730 enum ppc_stub_type stub_type;
3732 /* The stub section. */
3735 /* Offset within stub_sec of the beginning of this stub. */
3736 bfd_vma stub_offset;
3738 /* Given the symbol's value and its section we can determine its final
3739 value when building the stubs (so the stub knows where to jump. */
3740 bfd_vma target_value;
3741 asection *target_section;
3743 /* The symbol table entry, if any, that this was derived from. */
3744 struct ppc_link_hash_entry *h;
3745 struct plt_entry *plt_ent;
3747 /* Where this stub is being called from, or, in the case of combined
3748 stub sections, the first input section in the group. */
3751 /* Symbol st_other. */
3752 unsigned char other;
3755 struct ppc_branch_hash_entry {
3757 /* Base hash table entry structure. */
3758 struct bfd_hash_entry root;
3760 /* Offset within branch lookup table. */
3761 unsigned int offset;
3763 /* Generation marker. */
3767 /* Used to track dynamic relocations for local symbols. */
3768 struct ppc_dyn_relocs
3770 struct ppc_dyn_relocs *next;
3772 /* The input section of the reloc. */
3775 /* Total number of relocs copied for the input section. */
3776 unsigned int count : 31;
3778 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3779 unsigned int ifunc : 1;
3782 struct ppc_link_hash_entry
3784 struct elf_link_hash_entry elf;
3787 /* A pointer to the most recently used stub hash entry against this
3789 struct ppc_stub_hash_entry *stub_cache;
3791 /* A pointer to the next symbol starting with a '.' */
3792 struct ppc_link_hash_entry *next_dot_sym;
3795 /* Track dynamic relocs copied for this symbol. */
3796 struct elf_dyn_relocs *dyn_relocs;
3798 /* Link between function code and descriptor symbols. */
3799 struct ppc_link_hash_entry *oh;
3801 /* Flag function code and descriptor symbols. */
3802 unsigned int is_func:1;
3803 unsigned int is_func_descriptor:1;
3804 unsigned int fake:1;
3806 /* Whether global opd/toc sym has been adjusted or not.
3807 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3808 should be set for all globals defined in any opd/toc section. */
3809 unsigned int adjust_done:1;
3811 /* Set if we twiddled this symbol to weak at some stage. */
3812 unsigned int was_undefined:1;
3814 /* Contexts in which symbol is used in the GOT (or TOC).
3815 TLS_GD .. TLS_EXPLICIT bits are or'd into the mask as the
3816 corresponding relocs are encountered during check_relocs.
3817 tls_optimize clears TLS_GD .. TLS_TPREL when optimizing to
3818 indicate the corresponding GOT entry type is not needed.
3819 tls_optimize may also set TLS_TPRELGD when a GD reloc turns into
3820 a TPREL one. We use a separate flag rather than setting TPREL
3821 just for convenience in distinguishing the two cases. */
3822 #define TLS_GD 1 /* GD reloc. */
3823 #define TLS_LD 2 /* LD reloc. */
3824 #define TLS_TPREL 4 /* TPREL reloc, => IE. */
3825 #define TLS_DTPREL 8 /* DTPREL reloc, => LD. */
3826 #define TLS_TLS 16 /* Any TLS reloc. */
3827 #define TLS_EXPLICIT 32 /* Marks TOC section TLS relocs. */
3828 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
3829 #define PLT_IFUNC 128 /* STT_GNU_IFUNC. */
3830 unsigned char tls_mask;
3833 /* ppc64 ELF linker hash table. */
3835 struct ppc_link_hash_table
3837 struct elf_link_hash_table elf;
3839 /* The stub hash table. */
3840 struct bfd_hash_table stub_hash_table;
3842 /* Another hash table for plt_branch stubs. */
3843 struct bfd_hash_table branch_hash_table;
3845 /* Hash table for function prologue tocsave. */
3846 htab_t tocsave_htab;
3848 /* Linker stub bfd. */
3851 /* Linker call-backs. */
3852 asection * (*add_stub_section) (const char *, asection *);
3853 void (*layout_sections_again) (void);
3855 /* Array to keep track of which stub sections have been created, and
3856 information on stub grouping. */
3858 /* This is the section to which stubs in the group will be attached. */
3860 /* The stub section. */
3862 /* Along with elf_gp, specifies the TOC pointer used in this group. */
3866 /* Temp used when calculating TOC pointers. */
3869 asection *toc_first_sec;
3871 /* Highest input section id. */
3874 /* Highest output section index. */
3877 /* Used when adding symbols. */
3878 struct ppc_link_hash_entry *dot_syms;
3880 /* List of input sections for each output section. */
3881 asection **input_list;
3883 /* Short-cuts to get to dynamic linker sections. */
3895 asection *glink_eh_frame;
3897 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3898 struct ppc_link_hash_entry *tls_get_addr;
3899 struct ppc_link_hash_entry *tls_get_addr_fd;
3901 /* The size of reliplt used by got entry relocs. */
3902 bfd_size_type got_reli_size;
3905 unsigned long stub_count[ppc_stub_plt_call_r2save];
3907 /* Number of stubs against global syms. */
3908 unsigned long stub_globals;
3910 /* Alignment of PLT call stubs. */
3911 unsigned int plt_stub_align:4;
3913 /* Set if we're linking code with function descriptors. */
3914 unsigned int opd_abi:1;
3916 /* Set if PLT call stubs should load r11. */
3917 unsigned int plt_static_chain:1;
3919 /* Set if PLT call stubs need a read-read barrier. */
3920 unsigned int plt_thread_safe:1;
3922 /* Set if we should emit symbols for stubs. */
3923 unsigned int emit_stub_syms:1;
3925 /* Set if __tls_get_addr optimization should not be done. */
3926 unsigned int no_tls_get_addr_opt:1;
3928 /* Support for multiple toc sections. */
3929 unsigned int do_multi_toc:1;
3930 unsigned int multi_toc_needed:1;
3931 unsigned int second_toc_pass:1;
3932 unsigned int do_toc_opt:1;
3935 unsigned int stub_error:1;
3937 /* Temp used by ppc64_elf_process_dot_syms. */
3938 unsigned int twiddled_syms:1;
3940 /* Incremented every time we size stubs. */
3941 unsigned int stub_iteration;
3943 /* Small local sym cache. */
3944 struct sym_cache sym_cache;
3947 /* Rename some of the generic section flags to better document how they
3950 /* Nonzero if this section has TLS related relocations. */
3951 #define has_tls_reloc sec_flg0
3953 /* Nonzero if this section has a call to __tls_get_addr. */
3954 #define has_tls_get_addr_call sec_flg1
3956 /* Nonzero if this section has any toc or got relocs. */
3957 #define has_toc_reloc sec_flg2
3959 /* Nonzero if this section has a call to another section that uses
3961 #define makes_toc_func_call sec_flg3
3963 /* Recursion protection when determining above flag. */
3964 #define call_check_in_progress sec_flg4
3965 #define call_check_done sec_flg5
3967 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3969 #define ppc_hash_table(p) \
3970 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
3971 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
3973 #define ppc_stub_hash_lookup(table, string, create, copy) \
3974 ((struct ppc_stub_hash_entry *) \
3975 bfd_hash_lookup ((table), (string), (create), (copy)))
3977 #define ppc_branch_hash_lookup(table, string, create, copy) \
3978 ((struct ppc_branch_hash_entry *) \
3979 bfd_hash_lookup ((table), (string), (create), (copy)))
3981 /* Create an entry in the stub hash table. */
3983 static struct bfd_hash_entry *
3984 stub_hash_newfunc (struct bfd_hash_entry *entry,
3985 struct bfd_hash_table *table,
3988 /* Allocate the structure if it has not already been allocated by a
3992 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3997 /* Call the allocation method of the superclass. */
3998 entry = bfd_hash_newfunc (entry, table, string);
4001 struct ppc_stub_hash_entry *eh;
4003 /* Initialize the local fields. */
4004 eh = (struct ppc_stub_hash_entry *) entry;
4005 eh->stub_type = ppc_stub_none;
4006 eh->stub_sec = NULL;
4007 eh->stub_offset = 0;
4008 eh->target_value = 0;
4009 eh->target_section = NULL;
4019 /* Create an entry in the branch hash table. */
4021 static struct bfd_hash_entry *
4022 branch_hash_newfunc (struct bfd_hash_entry *entry,
4023 struct bfd_hash_table *table,
4026 /* Allocate the structure if it has not already been allocated by a
4030 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4035 /* Call the allocation method of the superclass. */
4036 entry = bfd_hash_newfunc (entry, table, string);
4039 struct ppc_branch_hash_entry *eh;
4041 /* Initialize the local fields. */
4042 eh = (struct ppc_branch_hash_entry *) entry;
4050 /* Create an entry in a ppc64 ELF linker hash table. */
4052 static struct bfd_hash_entry *
4053 link_hash_newfunc (struct bfd_hash_entry *entry,
4054 struct bfd_hash_table *table,
4057 /* Allocate the structure if it has not already been allocated by a
4061 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4066 /* Call the allocation method of the superclass. */
4067 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4070 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4072 memset (&eh->u.stub_cache, 0,
4073 (sizeof (struct ppc_link_hash_entry)
4074 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4076 /* When making function calls, old ABI code references function entry
4077 points (dot symbols), while new ABI code references the function
4078 descriptor symbol. We need to make any combination of reference and
4079 definition work together, without breaking archive linking.
4081 For a defined function "foo" and an undefined call to "bar":
4082 An old object defines "foo" and ".foo", references ".bar" (possibly
4084 A new object defines "foo" and references "bar".
4086 A new object thus has no problem with its undefined symbols being
4087 satisfied by definitions in an old object. On the other hand, the
4088 old object won't have ".bar" satisfied by a new object.
4090 Keep a list of newly added dot-symbols. */
4092 if (string[0] == '.')
4094 struct ppc_link_hash_table *htab;
4096 htab = (struct ppc_link_hash_table *) table;
4097 eh->u.next_dot_sym = htab->dot_syms;
4098 htab->dot_syms = eh;
4105 struct tocsave_entry {
4111 tocsave_htab_hash (const void *p)
4113 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4114 return ((bfd_vma)(intptr_t) e->sec ^ e->offset) >> 3;
4118 tocsave_htab_eq (const void *p1, const void *p2)
4120 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4121 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4122 return e1->sec == e2->sec && e1->offset == e2->offset;
4125 /* Create a ppc64 ELF linker hash table. */
4127 static struct bfd_link_hash_table *
4128 ppc64_elf_link_hash_table_create (bfd *abfd)
4130 struct ppc_link_hash_table *htab;
4131 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4133 htab = bfd_zmalloc (amt);
4137 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4138 sizeof (struct ppc_link_hash_entry),
4145 /* Init the stub hash table too. */
4146 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4147 sizeof (struct ppc_stub_hash_entry)))
4150 /* And the branch hash table. */
4151 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4152 sizeof (struct ppc_branch_hash_entry)))
4155 htab->tocsave_htab = htab_try_create (1024,
4159 if (htab->tocsave_htab == NULL)
4162 /* Initializing two fields of the union is just cosmetic. We really
4163 only care about glist, but when compiled on a 32-bit host the
4164 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4165 debugger inspection of these fields look nicer. */
4166 htab->elf.init_got_refcount.refcount = 0;
4167 htab->elf.init_got_refcount.glist = NULL;
4168 htab->elf.init_plt_refcount.refcount = 0;
4169 htab->elf.init_plt_refcount.glist = NULL;
4170 htab->elf.init_got_offset.offset = 0;
4171 htab->elf.init_got_offset.glist = NULL;
4172 htab->elf.init_plt_offset.offset = 0;
4173 htab->elf.init_plt_offset.glist = NULL;
4175 return &htab->elf.root;
4178 /* Free the derived linker hash table. */
4181 ppc64_elf_link_hash_table_free (struct bfd_link_hash_table *hash)
4183 struct ppc_link_hash_table *htab = (struct ppc_link_hash_table *) hash;
4185 bfd_hash_table_free (&htab->stub_hash_table);
4186 bfd_hash_table_free (&htab->branch_hash_table);
4187 if (htab->tocsave_htab)
4188 htab_delete (htab->tocsave_htab);
4189 _bfd_elf_link_hash_table_free (hash);
4192 /* Create sections for linker generated code. */
4195 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4197 struct ppc_link_hash_table *htab;
4200 htab = ppc_hash_table (info);
4202 /* Create .sfpr for code to save and restore fp regs. */
4203 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4204 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4205 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4207 if (htab->sfpr == NULL
4208 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4211 /* Create .glink for lazy dynamic linking support. */
4212 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4214 if (htab->glink == NULL
4215 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4218 if (!info->no_ld_generated_unwind_info)
4220 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4221 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4222 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4225 if (htab->glink_eh_frame == NULL
4226 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4230 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4231 htab->iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4232 if (htab->iplt == NULL
4233 || ! bfd_set_section_alignment (dynobj, htab->iplt, 3))
4236 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4237 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4238 htab->reliplt = bfd_make_section_anyway_with_flags (dynobj,
4241 if (htab->reliplt == NULL
4242 || ! bfd_set_section_alignment (dynobj, htab->reliplt, 3))
4245 /* Create branch lookup table for plt_branch stubs. */
4246 flags = (SEC_ALLOC | SEC_LOAD
4247 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4248 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4250 if (htab->brlt == NULL
4251 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4257 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4258 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4259 htab->relbrlt = bfd_make_section_anyway_with_flags (dynobj,
4262 if (htab->relbrlt == NULL
4263 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4269 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4272 ppc64_elf_init_stub_bfd (bfd *abfd, struct bfd_link_info *info)
4274 struct ppc_link_hash_table *htab;
4276 elf_elfheader (abfd)->e_ident[EI_CLASS] = ELFCLASS64;
4278 /* Always hook our dynamic sections into the first bfd, which is the
4279 linker created stub bfd. This ensures that the GOT header is at
4280 the start of the output TOC section. */
4281 htab = ppc_hash_table (info);
4284 htab->stub_bfd = abfd;
4285 htab->elf.dynobj = abfd;
4287 if (info->relocatable)
4290 return create_linkage_sections (htab->elf.dynobj, info);
4293 /* Build a name for an entry in the stub hash table. */
4296 ppc_stub_name (const asection *input_section,
4297 const asection *sym_sec,
4298 const struct ppc_link_hash_entry *h,
4299 const Elf_Internal_Rela *rel)
4304 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4305 offsets from a sym as a branch target? In fact, we could
4306 probably assume the addend is always zero. */
4307 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4311 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4312 stub_name = bfd_malloc (len);
4313 if (stub_name == NULL)
4316 len = sprintf (stub_name, "%08x.%s+%x",
4317 input_section->id & 0xffffffff,
4318 h->elf.root.root.string,
4319 (int) rel->r_addend & 0xffffffff);
4323 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4324 stub_name = bfd_malloc (len);
4325 if (stub_name == NULL)
4328 len = sprintf (stub_name, "%08x.%x:%x+%x",
4329 input_section->id & 0xffffffff,
4330 sym_sec->id & 0xffffffff,
4331 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4332 (int) rel->r_addend & 0xffffffff);
4334 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4335 stub_name[len - 2] = 0;
4339 /* Look up an entry in the stub hash. Stub entries are cached because
4340 creating the stub name takes a bit of time. */
4342 static struct ppc_stub_hash_entry *
4343 ppc_get_stub_entry (const asection *input_section,
4344 const asection *sym_sec,
4345 struct ppc_link_hash_entry *h,
4346 const Elf_Internal_Rela *rel,
4347 struct ppc_link_hash_table *htab)
4349 struct ppc_stub_hash_entry *stub_entry;
4350 const asection *id_sec;
4352 /* If this input section is part of a group of sections sharing one
4353 stub section, then use the id of the first section in the group.
4354 Stub names need to include a section id, as there may well be
4355 more than one stub used to reach say, printf, and we need to
4356 distinguish between them. */
4357 id_sec = htab->stub_group[input_section->id].link_sec;
4359 if (h != NULL && h->u.stub_cache != NULL
4360 && h->u.stub_cache->h == h
4361 && h->u.stub_cache->id_sec == id_sec)
4363 stub_entry = h->u.stub_cache;
4369 stub_name = ppc_stub_name (id_sec, sym_sec, h, rel);
4370 if (stub_name == NULL)
4373 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4374 stub_name, FALSE, FALSE);
4376 h->u.stub_cache = stub_entry;
4384 /* Add a new stub entry to the stub hash. Not all fields of the new
4385 stub entry are initialised. */
4387 static struct ppc_stub_hash_entry *
4388 ppc_add_stub (const char *stub_name,
4390 struct bfd_link_info *info)
4392 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4395 struct ppc_stub_hash_entry *stub_entry;
4397 link_sec = htab->stub_group[section->id].link_sec;
4398 stub_sec = htab->stub_group[section->id].stub_sec;
4399 if (stub_sec == NULL)
4401 stub_sec = htab->stub_group[link_sec->id].stub_sec;
4402 if (stub_sec == NULL)
4408 namelen = strlen (link_sec->name);
4409 len = namelen + sizeof (STUB_SUFFIX);
4410 s_name = bfd_alloc (htab->stub_bfd, len);
4414 memcpy (s_name, link_sec->name, namelen);
4415 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4416 stub_sec = (*htab->add_stub_section) (s_name, link_sec);
4417 if (stub_sec == NULL)
4419 htab->stub_group[link_sec->id].stub_sec = stub_sec;
4421 htab->stub_group[section->id].stub_sec = stub_sec;
4424 /* Enter this entry into the linker stub hash table. */
4425 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4427 if (stub_entry == NULL)
4429 info->callbacks->einfo (_("%P: %B: cannot create stub entry %s\n"),
4430 section->owner, stub_name);
4434 stub_entry->stub_sec = stub_sec;
4435 stub_entry->stub_offset = 0;
4436 stub_entry->id_sec = link_sec;
4440 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4441 not already done. */
4444 create_got_section (bfd *abfd, struct bfd_link_info *info)
4446 asection *got, *relgot;
4448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4450 if (!is_ppc64_elf (abfd))
4457 if (! _bfd_elf_create_got_section (htab->elf.dynobj, info))
4460 htab->got = bfd_get_linker_section (htab->elf.dynobj, ".got");
4465 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4466 | SEC_LINKER_CREATED);
4468 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4470 || !bfd_set_section_alignment (abfd, got, 3))
4473 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4474 flags | SEC_READONLY);
4476 || ! bfd_set_section_alignment (abfd, relgot, 3))
4479 ppc64_elf_tdata (abfd)->got = got;
4480 ppc64_elf_tdata (abfd)->relgot = relgot;
4484 /* Create the dynamic sections, and set up shortcuts. */
4487 ppc64_elf_create_dynamic_sections (bfd *dynobj, struct bfd_link_info *info)
4489 struct ppc_link_hash_table *htab;
4491 if (!_bfd_elf_create_dynamic_sections (dynobj, info))
4494 htab = ppc_hash_table (info);
4499 htab->got = bfd_get_linker_section (dynobj, ".got");
4500 htab->plt = bfd_get_linker_section (dynobj, ".plt");
4501 htab->relplt = bfd_get_linker_section (dynobj, ".rela.plt");
4502 htab->dynbss = bfd_get_linker_section (dynobj, ".dynbss");
4504 htab->relbss = bfd_get_linker_section (dynobj, ".rela.bss");
4506 if (!htab->got || !htab->plt || !htab->relplt || !htab->dynbss
4507 || (!info->shared && !htab->relbss))
4513 /* Follow indirect and warning symbol links. */
4515 static inline struct bfd_link_hash_entry *
4516 follow_link (struct bfd_link_hash_entry *h)
4518 while (h->type == bfd_link_hash_indirect
4519 || h->type == bfd_link_hash_warning)
4524 static inline struct elf_link_hash_entry *
4525 elf_follow_link (struct elf_link_hash_entry *h)
4527 return (struct elf_link_hash_entry *) follow_link (&h->root);
4530 static inline struct ppc_link_hash_entry *
4531 ppc_follow_link (struct ppc_link_hash_entry *h)
4533 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4536 /* Merge PLT info on FROM with that on TO. */
4539 move_plt_plist (struct ppc_link_hash_entry *from,
4540 struct ppc_link_hash_entry *to)
4542 if (from->elf.plt.plist != NULL)
4544 if (to->elf.plt.plist != NULL)
4546 struct plt_entry **entp;
4547 struct plt_entry *ent;
4549 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4551 struct plt_entry *dent;
4553 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4554 if (dent->addend == ent->addend)
4556 dent->plt.refcount += ent->plt.refcount;
4563 *entp = to->elf.plt.plist;
4566 to->elf.plt.plist = from->elf.plt.plist;
4567 from->elf.plt.plist = NULL;
4571 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4574 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4575 struct elf_link_hash_entry *dir,
4576 struct elf_link_hash_entry *ind)
4578 struct ppc_link_hash_entry *edir, *eind;
4580 edir = (struct ppc_link_hash_entry *) dir;
4581 eind = (struct ppc_link_hash_entry *) ind;
4583 edir->is_func |= eind->is_func;
4584 edir->is_func_descriptor |= eind->is_func_descriptor;
4585 edir->tls_mask |= eind->tls_mask;
4586 if (eind->oh != NULL)
4587 edir->oh = ppc_follow_link (eind->oh);
4589 /* If called to transfer flags for a weakdef during processing
4590 of elf_adjust_dynamic_symbol, don't copy NON_GOT_REF.
4591 We clear it ourselves for ELIMINATE_COPY_RELOCS. */
4592 if (!(ELIMINATE_COPY_RELOCS
4593 && eind->elf.root.type != bfd_link_hash_indirect
4594 && edir->elf.dynamic_adjusted))
4595 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4597 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4598 edir->elf.ref_regular |= eind->elf.ref_regular;
4599 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4600 edir->elf.needs_plt |= eind->elf.needs_plt;
4602 /* Copy over any dynamic relocs we may have on the indirect sym. */
4603 if (eind->dyn_relocs != NULL)
4605 if (edir->dyn_relocs != NULL)
4607 struct elf_dyn_relocs **pp;
4608 struct elf_dyn_relocs *p;
4610 /* Add reloc counts against the indirect sym to the direct sym
4611 list. Merge any entries against the same section. */
4612 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
4614 struct elf_dyn_relocs *q;
4616 for (q = edir->dyn_relocs; q != NULL; q = q->next)
4617 if (q->sec == p->sec)
4619 q->pc_count += p->pc_count;
4620 q->count += p->count;
4627 *pp = edir->dyn_relocs;
4630 edir->dyn_relocs = eind->dyn_relocs;
4631 eind->dyn_relocs = NULL;
4634 /* If we were called to copy over info for a weak sym, that's all.
4635 You might think dyn_relocs need not be copied over; After all,
4636 both syms will be dynamic or both non-dynamic so we're just
4637 moving reloc accounting around. However, ELIMINATE_COPY_RELOCS
4638 code in ppc64_elf_adjust_dynamic_symbol needs to check for
4639 dyn_relocs in read-only sections, and it does so on what is the
4641 if (eind->elf.root.type != bfd_link_hash_indirect)
4644 /* Copy over got entries that we may have already seen to the
4645 symbol which just became indirect. */
4646 if (eind->elf.got.glist != NULL)
4648 if (edir->elf.got.glist != NULL)
4650 struct got_entry **entp;
4651 struct got_entry *ent;
4653 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4655 struct got_entry *dent;
4657 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4658 if (dent->addend == ent->addend
4659 && dent->owner == ent->owner
4660 && dent->tls_type == ent->tls_type)
4662 dent->got.refcount += ent->got.refcount;
4669 *entp = edir->elf.got.glist;
4672 edir->elf.got.glist = eind->elf.got.glist;
4673 eind->elf.got.glist = NULL;
4676 /* And plt entries. */
4677 move_plt_plist (eind, edir);
4679 if (eind->elf.dynindx != -1)
4681 if (edir->elf.dynindx != -1)
4682 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4683 edir->elf.dynstr_index);
4684 edir->elf.dynindx = eind->elf.dynindx;
4685 edir->elf.dynstr_index = eind->elf.dynstr_index;
4686 eind->elf.dynindx = -1;
4687 eind->elf.dynstr_index = 0;
4691 /* Find the function descriptor hash entry from the given function code
4692 hash entry FH. Link the entries via their OH fields. */
4694 static struct ppc_link_hash_entry *
4695 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4697 struct ppc_link_hash_entry *fdh = fh->oh;
4701 const char *fd_name = fh->elf.root.root.string + 1;
4703 fdh = (struct ppc_link_hash_entry *)
4704 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
4708 fdh->is_func_descriptor = 1;
4714 return ppc_follow_link (fdh);
4717 /* Make a fake function descriptor sym for the code sym FH. */
4719 static struct ppc_link_hash_entry *
4720 make_fdh (struct bfd_link_info *info,
4721 struct ppc_link_hash_entry *fh)
4725 struct bfd_link_hash_entry *bh;
4726 struct ppc_link_hash_entry *fdh;
4728 abfd = fh->elf.root.u.undef.abfd;
4729 newsym = bfd_make_empty_symbol (abfd);
4730 newsym->name = fh->elf.root.root.string + 1;
4731 newsym->section = bfd_und_section_ptr;
4733 newsym->flags = BSF_WEAK;
4736 if (!_bfd_generic_link_add_one_symbol (info, abfd, newsym->name,
4737 newsym->flags, newsym->section,
4738 newsym->value, NULL, FALSE, FALSE,
4742 fdh = (struct ppc_link_hash_entry *) bh;
4743 fdh->elf.non_elf = 0;
4745 fdh->is_func_descriptor = 1;
4752 /* Fix function descriptor symbols defined in .opd sections to be
4756 ppc64_elf_add_symbol_hook (bfd *ibfd,
4757 struct bfd_link_info *info,
4758 Elf_Internal_Sym *isym,
4760 flagword *flags ATTRIBUTE_UNUSED,
4762 bfd_vma *value ATTRIBUTE_UNUSED)
4764 if ((ibfd->flags & DYNAMIC) == 0
4765 && ELF_ST_BIND (isym->st_info) == STB_GNU_UNIQUE)
4766 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4768 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4770 if ((ibfd->flags & DYNAMIC) == 0)
4771 elf_tdata (info->output_bfd)->has_gnu_symbols = TRUE;
4773 else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC)
4775 else if (*sec != NULL
4776 && strcmp ((*sec)->name, ".opd") == 0)
4777 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4779 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4781 if (abiversion (ibfd) == 0)
4782 set_abiversion (ibfd, 2);
4783 else if (abiversion (ibfd) == 1)
4785 info->callbacks->einfo (_("%P: symbol '%s' has invalid st_other"
4786 " for ABI version 1\n"), name);
4787 bfd_set_error (bfd_error_bad_value);
4795 /* Merge non-visibility st_other attributes: local entry point. */
4798 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4799 const Elf_Internal_Sym *isym,
4800 bfd_boolean definition,
4801 bfd_boolean dynamic)
4803 if (definition && !dynamic)
4804 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
4805 | ELF_ST_VISIBILITY (h->other));
4808 /* This function makes an old ABI object reference to ".bar" cause the
4809 inclusion of a new ABI object archive that defines "bar".
4810 NAME is a symbol defined in an archive. Return a symbol in the hash
4811 table that might be satisfied by the archive symbols. */
4813 static struct elf_link_hash_entry *
4814 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4815 struct bfd_link_info *info,
4818 struct elf_link_hash_entry *h;
4822 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4824 /* Don't return this sym if it is a fake function descriptor
4825 created by add_symbol_adjust. */
4826 && !(h->root.type == bfd_link_hash_undefweak
4827 && ((struct ppc_link_hash_entry *) h)->fake))
4833 len = strlen (name);
4834 dot_name = bfd_alloc (abfd, len + 2);
4835 if (dot_name == NULL)
4836 return (struct elf_link_hash_entry *) 0 - 1;
4838 memcpy (dot_name + 1, name, len + 1);
4839 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4840 bfd_release (abfd, dot_name);
4844 /* This function satisfies all old ABI object references to ".bar" if a
4845 new ABI object defines "bar". Well, at least, undefined dot symbols
4846 are made weak. This stops later archive searches from including an
4847 object if we already have a function descriptor definition. It also
4848 prevents the linker complaining about undefined symbols.
4849 We also check and correct mismatched symbol visibility here. The
4850 most restrictive visibility of the function descriptor and the
4851 function entry symbol is used. */
4854 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4856 struct ppc_link_hash_table *htab;
4857 struct ppc_link_hash_entry *fdh;
4859 if (eh->elf.root.type == bfd_link_hash_indirect)
4862 if (eh->elf.root.type == bfd_link_hash_warning)
4863 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4865 if (eh->elf.root.root.string[0] != '.')
4868 htab = ppc_hash_table (info);
4872 fdh = lookup_fdh (eh, htab);
4875 if (!info->relocatable
4876 && (eh->elf.root.type == bfd_link_hash_undefined
4877 || eh->elf.root.type == bfd_link_hash_undefweak)
4878 && eh->elf.ref_regular)
4880 /* Make an undefweak function descriptor sym, which is enough to
4881 pull in an --as-needed shared lib, but won't cause link
4882 errors. Archives are handled elsewhere. */
4883 fdh = make_fdh (info, eh);
4886 fdh->elf.ref_regular = 1;
4891 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4892 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4893 if (entry_vis < descr_vis)
4894 fdh->elf.other += entry_vis - descr_vis;
4895 else if (entry_vis > descr_vis)
4896 eh->elf.other += descr_vis - entry_vis;
4898 if ((fdh->elf.root.type == bfd_link_hash_defined
4899 || fdh->elf.root.type == bfd_link_hash_defweak)
4900 && eh->elf.root.type == bfd_link_hash_undefined)
4902 eh->elf.root.type = bfd_link_hash_undefweak;
4903 eh->was_undefined = 1;
4904 htab->twiddled_syms = 1;
4911 /* Process list of dot-symbols we made in link_hash_newfunc. */
4914 ppc64_elf_process_dot_syms (bfd *ibfd, struct bfd_link_info *info)
4916 struct ppc_link_hash_table *htab;
4917 struct ppc_link_hash_entry **p, *eh;
4919 if (!is_ppc64_elf (info->output_bfd))
4921 htab = ppc_hash_table (info);
4925 if (is_ppc64_elf (ibfd))
4927 p = &htab->dot_syms;
4928 while ((eh = *p) != NULL)
4931 if (&eh->elf == htab->elf.hgot)
4933 else if (htab->elf.hgot == NULL
4934 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4935 htab->elf.hgot = &eh->elf;
4936 else if (!add_symbol_adjust (eh, info))
4938 p = &eh->u.next_dot_sym;
4942 /* Clear the list for non-ppc64 input files. */
4943 p = &htab->dot_syms;
4944 while ((eh = *p) != NULL)
4947 p = &eh->u.next_dot_sym;
4950 /* We need to fix the undefs list for any syms we have twiddled to
4952 if (htab->twiddled_syms)
4954 bfd_link_repair_undef_list (&htab->elf.root);
4955 htab->twiddled_syms = 0;
4960 /* Undo hash table changes when an --as-needed input file is determined
4961 not to be needed. */
4964 ppc64_elf_notice_as_needed (bfd *ibfd,
4965 struct bfd_link_info *info,
4966 enum notice_asneeded_action act)
4968 if (act == notice_not_needed)
4970 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4975 htab->dot_syms = NULL;
4977 return _bfd_elf_notice_as_needed (ibfd, info, act);
4980 /* If --just-symbols against a final linked binary, then assume we need
4981 toc adjusting stubs when calling functions defined there. */
4984 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4986 if ((sec->flags & SEC_CODE) != 0
4987 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4988 && is_ppc64_elf (sec->owner))
4990 asection *got = bfd_get_section_by_name (sec->owner, ".got");
4992 && got->size >= elf_backend_got_header_size
4993 && bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4994 sec->has_toc_reloc = 1;
4996 _bfd_elf_link_just_syms (sec, info);
4999 static struct plt_entry **
5000 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5001 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5003 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5004 struct plt_entry **local_plt;
5005 unsigned char *local_got_tls_masks;
5007 if (local_got_ents == NULL)
5009 bfd_size_type size = symtab_hdr->sh_info;
5011 size *= (sizeof (*local_got_ents)
5012 + sizeof (*local_plt)
5013 + sizeof (*local_got_tls_masks));
5014 local_got_ents = bfd_zalloc (abfd, size);
5015 if (local_got_ents == NULL)
5017 elf_local_got_ents (abfd) = local_got_ents;
5020 if ((tls_type & (PLT_IFUNC | TLS_EXPLICIT)) == 0)
5022 struct got_entry *ent;
5024 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5025 if (ent->addend == r_addend
5026 && ent->owner == abfd
5027 && ent->tls_type == tls_type)
5031 bfd_size_type amt = sizeof (*ent);
5032 ent = bfd_alloc (abfd, amt);
5035 ent->next = local_got_ents[r_symndx];
5036 ent->addend = r_addend;
5038 ent->tls_type = tls_type;
5039 ent->is_indirect = FALSE;
5040 ent->got.refcount = 0;
5041 local_got_ents[r_symndx] = ent;
5043 ent->got.refcount += 1;
5046 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5047 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5048 local_got_tls_masks[r_symndx] |= tls_type;
5050 return local_plt + r_symndx;
5054 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5056 struct plt_entry *ent;
5058 for (ent = *plist; ent != NULL; ent = ent->next)
5059 if (ent->addend == addend)
5063 bfd_size_type amt = sizeof (*ent);
5064 ent = bfd_alloc (abfd, amt);
5068 ent->addend = addend;
5069 ent->plt.refcount = 0;
5072 ent->plt.refcount += 1;
5077 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5079 return (r_type == R_PPC64_REL24
5080 || r_type == R_PPC64_REL14
5081 || r_type == R_PPC64_REL14_BRTAKEN
5082 || r_type == R_PPC64_REL14_BRNTAKEN
5083 || r_type == R_PPC64_ADDR24
5084 || r_type == R_PPC64_ADDR14
5085 || r_type == R_PPC64_ADDR14_BRTAKEN
5086 || r_type == R_PPC64_ADDR14_BRNTAKEN);
5089 /* Look through the relocs for a section during the first phase, and
5090 calculate needed space in the global offset table, procedure
5091 linkage table, and dynamic reloc sections. */
5094 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5095 asection *sec, const Elf_Internal_Rela *relocs)
5097 struct ppc_link_hash_table *htab;
5098 Elf_Internal_Shdr *symtab_hdr;
5099 struct elf_link_hash_entry **sym_hashes;
5100 const Elf_Internal_Rela *rel;
5101 const Elf_Internal_Rela *rel_end;
5103 asection **opd_sym_map;
5104 struct elf_link_hash_entry *tga, *dottga;
5106 if (info->relocatable)
5109 /* Don't do anything special with non-loaded, non-alloced sections.
5110 In particular, any relocs in such sections should not affect GOT
5111 and PLT reference counting (ie. we don't allow them to create GOT
5112 or PLT entries), there's no possibility or desire to optimize TLS
5113 relocs, and there's not much point in propagating relocs to shared
5114 libs that the dynamic linker won't relocate. */
5115 if ((sec->flags & SEC_ALLOC) == 0)
5118 BFD_ASSERT (is_ppc64_elf (abfd));
5120 htab = ppc_hash_table (info);
5124 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5125 FALSE, FALSE, TRUE);
5126 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5127 FALSE, FALSE, TRUE);
5128 symtab_hdr = &elf_symtab_hdr (abfd);
5129 sym_hashes = elf_sym_hashes (abfd);
5132 if (strcmp (sec->name, ".opd") == 0)
5134 /* Garbage collection needs some extra help with .opd sections.
5135 We don't want to necessarily keep everything referenced by
5136 relocs in .opd, as that would keep all functions. Instead,
5137 if we reference an .opd symbol (a function descriptor), we
5138 want to keep the function code symbol's section. This is
5139 easy for global symbols, but for local syms we need to keep
5140 information about the associated function section. */
5143 if (abiversion (abfd) == 0)
5144 set_abiversion (abfd, 1);
5145 else if (abiversion (abfd) == 2)
5147 info->callbacks->einfo (_("%P: .opd not allowed in ABI version %d\n"),
5149 bfd_set_error (bfd_error_bad_value);
5152 amt = sec->size * sizeof (*opd_sym_map) / 8;
5153 opd_sym_map = bfd_zalloc (abfd, amt);
5154 if (opd_sym_map == NULL)
5156 ppc64_elf_section_data (sec)->u.opd.func_sec = opd_sym_map;
5157 BFD_ASSERT (ppc64_elf_section_data (sec)->sec_type == sec_normal);
5158 ppc64_elf_section_data (sec)->sec_type = sec_opd;
5161 rel_end = relocs + sec->reloc_count;
5162 for (rel = relocs; rel < rel_end; rel++)
5164 unsigned long r_symndx;
5165 struct elf_link_hash_entry *h;
5166 enum elf_ppc64_reloc_type r_type;
5168 struct _ppc64_elf_section_data *ppc64_sec;
5169 struct plt_entry **ifunc;
5171 r_symndx = ELF64_R_SYM (rel->r_info);
5172 if (r_symndx < symtab_hdr->sh_info)
5176 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5177 h = elf_follow_link (h);
5179 /* PR15323, ref flags aren't set for references in the same
5181 h->root.non_ir_ref = 1;
5183 if (h == htab->elf.hgot)
5184 sec->has_toc_reloc = 1;
5191 if (h->type == STT_GNU_IFUNC)
5194 ifunc = &h->plt.plist;
5199 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5204 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5206 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5207 rel->r_addend, PLT_IFUNC);
5212 r_type = ELF64_R_TYPE (rel->r_info);
5213 if (is_branch_reloc (r_type))
5215 if (h != NULL && (h == tga || h == dottga))
5218 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5219 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5220 /* We have a new-style __tls_get_addr call with a marker
5224 /* Mark this section as having an old-style call. */
5225 sec->has_tls_get_addr_call = 1;
5228 /* STT_GNU_IFUNC symbols must have a PLT entry. */
5230 && !update_plt_info (abfd, ifunc, rel->r_addend))
5238 /* These special tls relocs tie a call to __tls_get_addr with
5239 its parameter symbol. */
5242 case R_PPC64_GOT_TLSLD16:
5243 case R_PPC64_GOT_TLSLD16_LO:
5244 case R_PPC64_GOT_TLSLD16_HI:
5245 case R_PPC64_GOT_TLSLD16_HA:
5246 tls_type = TLS_TLS | TLS_LD;
5249 case R_PPC64_GOT_TLSGD16:
5250 case R_PPC64_GOT_TLSGD16_LO:
5251 case R_PPC64_GOT_TLSGD16_HI:
5252 case R_PPC64_GOT_TLSGD16_HA:
5253 tls_type = TLS_TLS | TLS_GD;
5256 case R_PPC64_GOT_TPREL16_DS:
5257 case R_PPC64_GOT_TPREL16_LO_DS:
5258 case R_PPC64_GOT_TPREL16_HI:
5259 case R_PPC64_GOT_TPREL16_HA:
5260 if (!info->executable)
5261 info->flags |= DF_STATIC_TLS;
5262 tls_type = TLS_TLS | TLS_TPREL;
5265 case R_PPC64_GOT_DTPREL16_DS:
5266 case R_PPC64_GOT_DTPREL16_LO_DS:
5267 case R_PPC64_GOT_DTPREL16_HI:
5268 case R_PPC64_GOT_DTPREL16_HA:
5269 tls_type = TLS_TLS | TLS_DTPREL;
5271 sec->has_tls_reloc = 1;
5275 case R_PPC64_GOT16_DS:
5276 case R_PPC64_GOT16_HA:
5277 case R_PPC64_GOT16_HI:
5278 case R_PPC64_GOT16_LO:
5279 case R_PPC64_GOT16_LO_DS:
5280 /* This symbol requires a global offset table entry. */
5281 sec->has_toc_reloc = 1;
5282 if (r_type == R_PPC64_GOT_TLSLD16
5283 || r_type == R_PPC64_GOT_TLSGD16
5284 || r_type == R_PPC64_GOT_TPREL16_DS
5285 || r_type == R_PPC64_GOT_DTPREL16_DS
5286 || r_type == R_PPC64_GOT16
5287 || r_type == R_PPC64_GOT16_DS)
5289 htab->do_multi_toc = 1;
5290 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5293 if (ppc64_elf_tdata (abfd)->got == NULL
5294 && !create_got_section (abfd, info))
5299 struct ppc_link_hash_entry *eh;
5300 struct got_entry *ent;
5302 eh = (struct ppc_link_hash_entry *) h;
5303 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5304 if (ent->addend == rel->r_addend
5305 && ent->owner == abfd
5306 && ent->tls_type == tls_type)
5310 bfd_size_type amt = sizeof (*ent);
5311 ent = bfd_alloc (abfd, amt);
5314 ent->next = eh->elf.got.glist;
5315 ent->addend = rel->r_addend;
5317 ent->tls_type = tls_type;
5318 ent->is_indirect = FALSE;
5319 ent->got.refcount = 0;
5320 eh->elf.got.glist = ent;
5322 ent->got.refcount += 1;
5323 eh->tls_mask |= tls_type;
5326 /* This is a global offset table entry for a local symbol. */
5327 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5328 rel->r_addend, tls_type))
5332 case R_PPC64_PLT16_HA:
5333 case R_PPC64_PLT16_HI:
5334 case R_PPC64_PLT16_LO:
5337 /* This symbol requires a procedure linkage table entry. We
5338 actually build the entry in adjust_dynamic_symbol,
5339 because this might be a case of linking PIC code without
5340 linking in any dynamic objects, in which case we don't
5341 need to generate a procedure linkage table after all. */
5344 /* It does not make sense to have a procedure linkage
5345 table entry for a local symbol. */
5346 bfd_set_error (bfd_error_bad_value);
5351 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5354 if (h->root.root.string[0] == '.'
5355 && h->root.root.string[1] != '\0')
5356 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5360 /* The following relocations don't need to propagate the
5361 relocation if linking a shared object since they are
5362 section relative. */
5363 case R_PPC64_SECTOFF:
5364 case R_PPC64_SECTOFF_LO:
5365 case R_PPC64_SECTOFF_HI:
5366 case R_PPC64_SECTOFF_HA:
5367 case R_PPC64_SECTOFF_DS:
5368 case R_PPC64_SECTOFF_LO_DS:
5369 case R_PPC64_DTPREL16:
5370 case R_PPC64_DTPREL16_LO:
5371 case R_PPC64_DTPREL16_HI:
5372 case R_PPC64_DTPREL16_HA:
5373 case R_PPC64_DTPREL16_DS:
5374 case R_PPC64_DTPREL16_LO_DS:
5375 case R_PPC64_DTPREL16_HIGH:
5376 case R_PPC64_DTPREL16_HIGHA:
5377 case R_PPC64_DTPREL16_HIGHER:
5378 case R_PPC64_DTPREL16_HIGHERA:
5379 case R_PPC64_DTPREL16_HIGHEST:
5380 case R_PPC64_DTPREL16_HIGHESTA:
5385 case R_PPC64_REL16_LO:
5386 case R_PPC64_REL16_HI:
5387 case R_PPC64_REL16_HA:
5391 case R_PPC64_TOC16_DS:
5392 htab->do_multi_toc = 1;
5393 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5394 case R_PPC64_TOC16_LO:
5395 case R_PPC64_TOC16_HI:
5396 case R_PPC64_TOC16_HA:
5397 case R_PPC64_TOC16_LO_DS:
5398 sec->has_toc_reloc = 1;
5401 /* This relocation describes the C++ object vtable hierarchy.
5402 Reconstruct it for later use during GC. */
5403 case R_PPC64_GNU_VTINHERIT:
5404 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5408 /* This relocation describes which C++ vtable entries are actually
5409 used. Record for later use during GC. */
5410 case R_PPC64_GNU_VTENTRY:
5411 BFD_ASSERT (h != NULL);
5413 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5418 case R_PPC64_REL14_BRTAKEN:
5419 case R_PPC64_REL14_BRNTAKEN:
5421 asection *dest = NULL;
5423 /* Heuristic: If jumping outside our section, chances are
5424 we are going to need a stub. */
5427 /* If the sym is weak it may be overridden later, so
5428 don't assume we know where a weak sym lives. */
5429 if (h->root.type == bfd_link_hash_defined)
5430 dest = h->root.u.def.section;
5434 Elf_Internal_Sym *isym;
5436 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5441 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5445 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5450 if (h != NULL && ifunc == NULL)
5452 /* We may need a .plt entry if the function this reloc
5453 refers to is in a shared lib. */
5454 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5457 if (h->root.root.string[0] == '.'
5458 && h->root.root.string[1] != '\0')
5459 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5460 if (h == tga || h == dottga)
5461 sec->has_tls_reloc = 1;
5465 case R_PPC64_TPREL64:
5466 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5467 if (!info->executable)
5468 info->flags |= DF_STATIC_TLS;
5471 case R_PPC64_DTPMOD64:
5472 if (rel + 1 < rel_end
5473 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5474 && rel[1].r_offset == rel->r_offset + 8)
5475 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5477 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5480 case R_PPC64_DTPREL64:
5481 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5483 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5484 && rel[-1].r_offset == rel->r_offset - 8)
5485 /* This is the second reloc of a dtpmod, dtprel pair.
5486 Don't mark with TLS_DTPREL. */
5490 sec->has_tls_reloc = 1;
5493 struct ppc_link_hash_entry *eh;
5494 eh = (struct ppc_link_hash_entry *) h;
5495 eh->tls_mask |= tls_type;
5498 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5499 rel->r_addend, tls_type))
5502 ppc64_sec = ppc64_elf_section_data (sec);
5503 if (ppc64_sec->sec_type != sec_toc)
5507 /* One extra to simplify get_tls_mask. */
5508 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5509 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5510 if (ppc64_sec->u.toc.symndx == NULL)
5512 amt = sec->size * sizeof (bfd_vma) / 8;
5513 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5514 if (ppc64_sec->u.toc.add == NULL)
5516 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5517 ppc64_sec->sec_type = sec_toc;
5519 BFD_ASSERT (rel->r_offset % 8 == 0);
5520 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5521 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5523 /* Mark the second slot of a GD or LD entry.
5524 -1 to indicate GD and -2 to indicate LD. */
5525 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5526 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5527 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5528 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5531 case R_PPC64_TPREL16:
5532 case R_PPC64_TPREL16_LO:
5533 case R_PPC64_TPREL16_HI:
5534 case R_PPC64_TPREL16_HA:
5535 case R_PPC64_TPREL16_DS:
5536 case R_PPC64_TPREL16_LO_DS:
5537 case R_PPC64_TPREL16_HIGH:
5538 case R_PPC64_TPREL16_HIGHA:
5539 case R_PPC64_TPREL16_HIGHER:
5540 case R_PPC64_TPREL16_HIGHERA:
5541 case R_PPC64_TPREL16_HIGHEST:
5542 case R_PPC64_TPREL16_HIGHESTA:
5545 if (!info->executable)
5546 info->flags |= DF_STATIC_TLS;
5551 case R_PPC64_ADDR64:
5552 if (opd_sym_map != NULL
5553 && rel + 1 < rel_end
5554 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5558 if (h->root.root.string[0] == '.'
5559 && h->root.root.string[1] != 0
5560 && lookup_fdh ((struct ppc_link_hash_entry *) h, htab))
5563 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5568 Elf_Internal_Sym *isym;
5570 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5575 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5576 if (s != NULL && s != sec)
5577 opd_sym_map[rel->r_offset / 8] = s;
5585 case R_PPC64_ADDR14:
5586 case R_PPC64_ADDR14_BRNTAKEN:
5587 case R_PPC64_ADDR14_BRTAKEN:
5588 case R_PPC64_ADDR16:
5589 case R_PPC64_ADDR16_DS:
5590 case R_PPC64_ADDR16_HA:
5591 case R_PPC64_ADDR16_HI:
5592 case R_PPC64_ADDR16_HIGH:
5593 case R_PPC64_ADDR16_HIGHA:
5594 case R_PPC64_ADDR16_HIGHER:
5595 case R_PPC64_ADDR16_HIGHERA:
5596 case R_PPC64_ADDR16_HIGHEST:
5597 case R_PPC64_ADDR16_HIGHESTA:
5598 case R_PPC64_ADDR16_LO:
5599 case R_PPC64_ADDR16_LO_DS:
5600 case R_PPC64_ADDR24:
5601 case R_PPC64_ADDR32:
5602 case R_PPC64_UADDR16:
5603 case R_PPC64_UADDR32:
5604 case R_PPC64_UADDR64:
5606 if (h != NULL && !info->shared)
5607 /* We may need a copy reloc. */
5610 /* Don't propagate .opd relocs. */
5611 if (NO_OPD_RELOCS && opd_sym_map != NULL)
5614 /* If we are creating a shared library, and this is a reloc
5615 against a global symbol, or a non PC relative reloc
5616 against a local symbol, then we need to copy the reloc
5617 into the shared library. However, if we are linking with
5618 -Bsymbolic, we do not need to copy a reloc against a
5619 global symbol which is defined in an object we are
5620 including in the link (i.e., DEF_REGULAR is set). At
5621 this point we have not seen all the input files, so it is
5622 possible that DEF_REGULAR is not set now but will be set
5623 later (it is never cleared). In case of a weak definition,
5624 DEF_REGULAR may be cleared later by a strong definition in
5625 a shared library. We account for that possibility below by
5626 storing information in the dyn_relocs field of the hash
5627 table entry. A similar situation occurs when creating
5628 shared libraries and symbol visibility changes render the
5631 If on the other hand, we are creating an executable, we
5632 may need to keep relocations for symbols satisfied by a
5633 dynamic library if we manage to avoid copy relocs for the
5637 && (must_be_dyn_reloc (info, r_type)
5639 && (!SYMBOLIC_BIND (info, h)
5640 || h->root.type == bfd_link_hash_defweak
5641 || !h->def_regular))))
5642 || (ELIMINATE_COPY_RELOCS
5645 && (h->root.type == bfd_link_hash_defweak
5646 || !h->def_regular))
5650 /* We must copy these reloc types into the output file.
5651 Create a reloc section in dynobj and make room for
5655 sreloc = _bfd_elf_make_dynamic_reloc_section
5656 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
5662 /* If this is a global symbol, we count the number of
5663 relocations we need for this symbol. */
5666 struct elf_dyn_relocs *p;
5667 struct elf_dyn_relocs **head;
5669 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
5671 if (p == NULL || p->sec != sec)
5673 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5683 if (!must_be_dyn_reloc (info, r_type))
5688 /* Track dynamic relocs needed for local syms too.
5689 We really need local syms available to do this
5691 struct ppc_dyn_relocs *p;
5692 struct ppc_dyn_relocs **head;
5693 bfd_boolean is_ifunc;
5696 Elf_Internal_Sym *isym;
5698 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5703 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5707 vpp = &elf_section_data (s)->local_dynrel;
5708 head = (struct ppc_dyn_relocs **) vpp;
5709 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5711 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5713 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5715 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5721 p->ifunc = is_ifunc;
5737 /* Merge backend specific data from an object file to the output
5738 object file when linking. */
5741 ppc64_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
5743 unsigned long iflags, oflags;
5745 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5748 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5751 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
5754 iflags = elf_elfheader (ibfd)->e_flags;
5755 oflags = elf_elfheader (obfd)->e_flags;
5757 if (!elf_flags_init (obfd) || oflags == 0)
5759 elf_flags_init (obfd) = TRUE;
5760 elf_elfheader (obfd)->e_flags = iflags;
5762 else if (iflags == oflags || iflags == 0)
5764 else if (iflags & ~EF_PPC64_ABI)
5766 (*_bfd_error_handler)
5767 (_("%B uses unknown e_flags 0x%lx"), ibfd, iflags);
5768 bfd_set_error (bfd_error_bad_value);
5773 (*_bfd_error_handler)
5774 (_("%B: ABI version %ld is not compatible with ABI version %ld output"),
5775 ibfd, iflags, oflags);
5776 bfd_set_error (bfd_error_bad_value);
5780 /* Merge Tag_compatibility attributes and any common GNU ones. */
5781 _bfd_elf_merge_object_attributes (ibfd, obfd);
5787 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5789 /* Print normal ELF private data. */
5790 _bfd_elf_print_private_bfd_data (abfd, ptr);
5792 if (elf_elfheader (abfd)->e_flags != 0)
5796 /* xgettext:c-format */
5797 fprintf (file, _("private flags = 0x%lx:"),
5798 elf_elfheader (abfd)->e_flags);
5800 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5801 fprintf (file, _(" [abiv%ld]"),
5802 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5809 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5810 of the code entry point, and its section. */
5813 opd_entry_value (asection *opd_sec,
5815 asection **code_sec,
5817 bfd_boolean in_code_sec)
5819 bfd *opd_bfd = opd_sec->owner;
5820 Elf_Internal_Rela *relocs;
5821 Elf_Internal_Rela *lo, *hi, *look;
5824 /* No relocs implies we are linking a --just-symbols object, or looking
5825 at a final linked executable with addr2line or somesuch. */
5826 if (opd_sec->reloc_count == 0)
5828 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5830 if (contents == NULL)
5832 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5833 return (bfd_vma) -1;
5834 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5837 val = bfd_get_64 (opd_bfd, contents + offset);
5838 if (code_sec != NULL)
5840 asection *sec, *likely = NULL;
5846 && val < sec->vma + sec->size)
5852 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5854 && (sec->flags & SEC_LOAD) != 0
5855 && (sec->flags & SEC_ALLOC) != 0)
5860 if (code_off != NULL)
5861 *code_off = val - likely->vma;
5867 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5869 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5871 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
5873 /* Go find the opd reloc at the sym address. */
5875 BFD_ASSERT (lo != NULL);
5876 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5880 look = lo + (hi - lo) / 2;
5881 if (look->r_offset < offset)
5883 else if (look->r_offset > offset)
5887 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5889 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5890 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5892 unsigned long symndx = ELF64_R_SYM (look->r_info);
5895 if (symndx < symtab_hdr->sh_info
5896 || elf_sym_hashes (opd_bfd) == NULL)
5898 Elf_Internal_Sym *sym;
5900 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5903 size_t symcnt = symtab_hdr->sh_info;
5904 if (elf_sym_hashes (opd_bfd) == NULL)
5905 symcnt = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
5906 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, symcnt,
5907 0, NULL, NULL, NULL);
5910 symtab_hdr->contents = (bfd_byte *) sym;
5914 val = sym->st_value;
5915 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5916 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5920 struct elf_link_hash_entry **sym_hashes;
5921 struct elf_link_hash_entry *rh;
5923 sym_hashes = elf_sym_hashes (opd_bfd);
5924 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5927 rh = elf_follow_link (rh);
5928 BFD_ASSERT (rh->root.type == bfd_link_hash_defined
5929 || rh->root.type == bfd_link_hash_defweak);
5930 val = rh->root.u.def.value;
5931 sec = rh->root.u.def.section;
5935 /* Handle the odd case where we can be called
5936 during bfd_elf_link_add_symbols before the
5937 symbol hashes have been fully populated. */
5938 Elf_Internal_Sym *sym;
5940 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr, 1,
5941 symndx, NULL, NULL, NULL);
5945 val = sym->st_value;
5946 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5950 val += look->r_addend;
5951 if (code_off != NULL)
5953 if (code_sec != NULL)
5955 if (in_code_sec && *code_sec != sec)
5960 if (sec != NULL && sec->output_section != NULL)
5961 val += sec->output_section->vma + sec->output_offset;
5970 /* If the ELF symbol SYM might be a function in SEC, return the
5971 function size and set *CODE_OFF to the function's entry point,
5972 otherwise return zero. */
5974 static bfd_size_type
5975 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5980 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5981 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5985 if (!(sym->flags & BSF_SYNTHETIC))
5986 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5988 if (strcmp (sym->section->name, ".opd") == 0)
5990 if (opd_entry_value (sym->section, sym->value,
5991 &sec, code_off, TRUE) == (bfd_vma) -1)
5993 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5994 symbol. This size has nothing to do with the code size of the
5995 function, which is what we're supposed to return, but the
5996 code size isn't available without looking up the dot-sym.
5997 However, doing that would be a waste of time particularly
5998 since elf_find_function will look at the dot-sym anyway.
5999 Now, elf_find_function will keep the largest size of any
6000 function sym found at the code address of interest, so return
6001 1 here to avoid it incorrectly caching a larger function size
6002 for a small function. This does mean we return the wrong
6003 size for a new-ABI function of size 24, but all that does is
6004 disable caching for such functions. */
6010 if (sym->section != sec)
6012 *code_off = sym->value;
6019 /* Return true if symbol is defined in a regular object file. */
6022 is_static_defined (struct elf_link_hash_entry *h)
6024 return ((h->root.type == bfd_link_hash_defined
6025 || h->root.type == bfd_link_hash_defweak)
6026 && h->root.u.def.section != NULL
6027 && h->root.u.def.section->output_section != NULL);
6030 /* If FDH is a function descriptor symbol, return the associated code
6031 entry symbol if it is defined. Return NULL otherwise. */
6033 static struct ppc_link_hash_entry *
6034 defined_code_entry (struct ppc_link_hash_entry *fdh)
6036 if (fdh->is_func_descriptor)
6038 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6039 if (fh->elf.root.type == bfd_link_hash_defined
6040 || fh->elf.root.type == bfd_link_hash_defweak)
6046 /* If FH is a function code entry symbol, return the associated
6047 function descriptor symbol if it is defined. Return NULL otherwise. */
6049 static struct ppc_link_hash_entry *
6050 defined_func_desc (struct ppc_link_hash_entry *fh)
6053 && fh->oh->is_func_descriptor)
6055 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6056 if (fdh->elf.root.type == bfd_link_hash_defined
6057 || fdh->elf.root.type == bfd_link_hash_defweak)
6063 /* Mark all our entry sym sections, both opd and code section. */
6066 ppc64_elf_gc_keep (struct bfd_link_info *info)
6068 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6069 struct bfd_sym_chain *sym;
6074 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6076 struct ppc_link_hash_entry *eh, *fh;
6079 eh = (struct ppc_link_hash_entry *)
6080 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6083 if (eh->elf.root.type != bfd_link_hash_defined
6084 && eh->elf.root.type != bfd_link_hash_defweak)
6087 fh = defined_code_entry (eh);
6090 sec = fh->elf.root.u.def.section;
6091 sec->flags |= SEC_KEEP;
6093 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6094 && opd_entry_value (eh->elf.root.u.def.section,
6095 eh->elf.root.u.def.value,
6096 &sec, NULL, FALSE) != (bfd_vma) -1)
6097 sec->flags |= SEC_KEEP;
6099 sec = eh->elf.root.u.def.section;
6100 sec->flags |= SEC_KEEP;
6104 /* Mark sections containing dynamically referenced symbols. When
6105 building shared libraries, we must assume that any visible symbol is
6109 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6111 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6112 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6113 struct ppc_link_hash_entry *fdh;
6115 /* Dynamic linking info is on the func descriptor sym. */
6116 fdh = defined_func_desc (eh);
6120 if ((eh->elf.root.type == bfd_link_hash_defined
6121 || eh->elf.root.type == bfd_link_hash_defweak)
6122 && (eh->elf.ref_dynamic
6123 || (!info->executable
6124 && eh->elf.def_regular
6125 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6126 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6127 && (strchr (eh->elf.root.root.string, ELF_VER_CHR) != NULL
6128 || !bfd_hide_sym_by_version (info->version_info,
6129 eh->elf.root.root.string)))))
6132 struct ppc_link_hash_entry *fh;
6134 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6136 /* Function descriptor syms cause the associated
6137 function code sym section to be marked. */
6138 fh = defined_code_entry (eh);
6141 code_sec = fh->elf.root.u.def.section;
6142 code_sec->flags |= SEC_KEEP;
6144 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6145 && opd_entry_value (eh->elf.root.u.def.section,
6146 eh->elf.root.u.def.value,
6147 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6148 code_sec->flags |= SEC_KEEP;
6154 /* Return the section that should be marked against GC for a given
6158 ppc64_elf_gc_mark_hook (asection *sec,
6159 struct bfd_link_info *info,
6160 Elf_Internal_Rela *rel,
6161 struct elf_link_hash_entry *h,
6162 Elf_Internal_Sym *sym)
6166 /* Syms return NULL if we're marking .opd, so we avoid marking all
6167 function sections, as all functions are referenced in .opd. */
6169 if (get_opd_info (sec) != NULL)
6174 enum elf_ppc64_reloc_type r_type;
6175 struct ppc_link_hash_entry *eh, *fh, *fdh;
6177 r_type = ELF64_R_TYPE (rel->r_info);
6180 case R_PPC64_GNU_VTINHERIT:
6181 case R_PPC64_GNU_VTENTRY:
6185 switch (h->root.type)
6187 case bfd_link_hash_defined:
6188 case bfd_link_hash_defweak:
6189 eh = (struct ppc_link_hash_entry *) h;
6190 fdh = defined_func_desc (eh);
6194 /* Function descriptor syms cause the associated
6195 function code sym section to be marked. */
6196 fh = defined_code_entry (eh);
6199 /* They also mark their opd section. */
6200 eh->elf.root.u.def.section->gc_mark = 1;
6202 rsec = fh->elf.root.u.def.section;
6204 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6205 && opd_entry_value (eh->elf.root.u.def.section,
6206 eh->elf.root.u.def.value,
6207 &rsec, NULL, FALSE) != (bfd_vma) -1)
6208 eh->elf.root.u.def.section->gc_mark = 1;
6210 rsec = h->root.u.def.section;
6213 case bfd_link_hash_common:
6214 rsec = h->root.u.c.p->section;
6218 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6224 struct _opd_sec_data *opd;
6226 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6227 opd = get_opd_info (rsec);
6228 if (opd != NULL && opd->func_sec != NULL)
6232 rsec = opd->func_sec[(sym->st_value + rel->r_addend) / 8];
6239 /* Update the .got, .plt. and dynamic reloc reference counts for the
6240 section being removed. */
6243 ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
6244 asection *sec, const Elf_Internal_Rela *relocs)
6246 struct ppc_link_hash_table *htab;
6247 Elf_Internal_Shdr *symtab_hdr;
6248 struct elf_link_hash_entry **sym_hashes;
6249 struct got_entry **local_got_ents;
6250 const Elf_Internal_Rela *rel, *relend;
6252 if (info->relocatable)
6255 if ((sec->flags & SEC_ALLOC) == 0)
6258 elf_section_data (sec)->local_dynrel = NULL;
6260 htab = ppc_hash_table (info);
6264 symtab_hdr = &elf_symtab_hdr (abfd);
6265 sym_hashes = elf_sym_hashes (abfd);
6266 local_got_ents = elf_local_got_ents (abfd);
6268 relend = relocs + sec->reloc_count;
6269 for (rel = relocs; rel < relend; rel++)
6271 unsigned long r_symndx;
6272 enum elf_ppc64_reloc_type r_type;
6273 struct elf_link_hash_entry *h = NULL;
6274 unsigned char tls_type = 0;
6276 r_symndx = ELF64_R_SYM (rel->r_info);
6277 r_type = ELF64_R_TYPE (rel->r_info);
6278 if (r_symndx >= symtab_hdr->sh_info)
6280 struct ppc_link_hash_entry *eh;
6281 struct elf_dyn_relocs **pp;
6282 struct elf_dyn_relocs *p;
6284 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6285 h = elf_follow_link (h);
6286 eh = (struct ppc_link_hash_entry *) h;
6288 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
6291 /* Everything must go for SEC. */
6297 if (is_branch_reloc (r_type))
6299 struct plt_entry **ifunc = NULL;
6302 if (h->type == STT_GNU_IFUNC)
6303 ifunc = &h->plt.plist;
6305 else if (local_got_ents != NULL)
6307 struct plt_entry **local_plt = (struct plt_entry **)
6308 (local_got_ents + symtab_hdr->sh_info);
6309 unsigned char *local_got_tls_masks = (unsigned char *)
6310 (local_plt + symtab_hdr->sh_info);
6311 if ((local_got_tls_masks[r_symndx] & PLT_IFUNC) != 0)
6312 ifunc = local_plt + r_symndx;
6316 struct plt_entry *ent;
6318 for (ent = *ifunc; ent != NULL; ent = ent->next)
6319 if (ent->addend == rel->r_addend)
6323 if (ent->plt.refcount > 0)
6324 ent->plt.refcount -= 1;
6331 case R_PPC64_GOT_TLSLD16:
6332 case R_PPC64_GOT_TLSLD16_LO:
6333 case R_PPC64_GOT_TLSLD16_HI:
6334 case R_PPC64_GOT_TLSLD16_HA:
6335 tls_type = TLS_TLS | TLS_LD;
6338 case R_PPC64_GOT_TLSGD16:
6339 case R_PPC64_GOT_TLSGD16_LO:
6340 case R_PPC64_GOT_TLSGD16_HI:
6341 case R_PPC64_GOT_TLSGD16_HA:
6342 tls_type = TLS_TLS | TLS_GD;
6345 case R_PPC64_GOT_TPREL16_DS:
6346 case R_PPC64_GOT_TPREL16_LO_DS:
6347 case R_PPC64_GOT_TPREL16_HI:
6348 case R_PPC64_GOT_TPREL16_HA:
6349 tls_type = TLS_TLS | TLS_TPREL;
6352 case R_PPC64_GOT_DTPREL16_DS:
6353 case R_PPC64_GOT_DTPREL16_LO_DS:
6354 case R_PPC64_GOT_DTPREL16_HI:
6355 case R_PPC64_GOT_DTPREL16_HA:
6356 tls_type = TLS_TLS | TLS_DTPREL;
6360 case R_PPC64_GOT16_DS:
6361 case R_PPC64_GOT16_HA:
6362 case R_PPC64_GOT16_HI:
6363 case R_PPC64_GOT16_LO:
6364 case R_PPC64_GOT16_LO_DS:
6367 struct got_entry *ent;
6372 ent = local_got_ents[r_symndx];
6374 for (; ent != NULL; ent = ent->next)
6375 if (ent->addend == rel->r_addend
6376 && ent->owner == abfd
6377 && ent->tls_type == tls_type)
6381 if (ent->got.refcount > 0)
6382 ent->got.refcount -= 1;
6386 case R_PPC64_PLT16_HA:
6387 case R_PPC64_PLT16_HI:
6388 case R_PPC64_PLT16_LO:
6392 case R_PPC64_REL14_BRNTAKEN:
6393 case R_PPC64_REL14_BRTAKEN:
6397 struct plt_entry *ent;
6399 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6400 if (ent->addend == rel->r_addend)
6402 if (ent != NULL && ent->plt.refcount > 0)
6403 ent->plt.refcount -= 1;
6414 /* The maximum size of .sfpr. */
6415 #define SFPR_MAX (218*4)
6417 struct sfpr_def_parms
6419 const char name[12];
6420 unsigned char lo, hi;
6421 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6422 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6425 /* Auto-generate _save*, _rest* functions in .sfpr. */
6428 sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
6430 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6432 size_t len = strlen (parm->name);
6433 bfd_boolean writing = FALSE;
6439 memcpy (sym, parm->name, len);
6442 for (i = parm->lo; i <= parm->hi; i++)
6444 struct elf_link_hash_entry *h;
6446 sym[len + 0] = i / 10 + '0';
6447 sym[len + 1] = i % 10 + '0';
6448 h = elf_link_hash_lookup (&htab->elf, sym, FALSE, FALSE, TRUE);
6452 h->root.type = bfd_link_hash_defined;
6453 h->root.u.def.section = htab->sfpr;
6454 h->root.u.def.value = htab->sfpr->size;
6457 _bfd_elf_link_hash_hide_symbol (info, h, TRUE);
6459 if (htab->sfpr->contents == NULL)
6461 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6462 if (htab->sfpr->contents == NULL)
6468 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6470 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6472 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6473 htab->sfpr->size = p - htab->sfpr->contents;
6481 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6483 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6488 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6490 p = savegpr0 (abfd, p, r);
6491 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6493 bfd_put_32 (abfd, BLR, p);
6498 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6500 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6505 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6507 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6509 p = restgpr0 (abfd, p, r);
6510 bfd_put_32 (abfd, MTLR_R0, p);
6514 p = restgpr0 (abfd, p, 30);
6515 p = restgpr0 (abfd, p, 31);
6517 bfd_put_32 (abfd, BLR, p);
6522 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6524 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6529 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6531 p = savegpr1 (abfd, p, r);
6532 bfd_put_32 (abfd, BLR, p);
6537 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6539 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6544 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6546 p = restgpr1 (abfd, p, r);
6547 bfd_put_32 (abfd, BLR, p);
6552 savefpr (bfd *abfd, bfd_byte *p, int r)
6554 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6559 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6561 p = savefpr (abfd, p, r);
6562 bfd_put_32 (abfd, STD_R0_0R1 + 16, p);
6564 bfd_put_32 (abfd, BLR, p);
6569 restfpr (bfd *abfd, bfd_byte *p, int r)
6571 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6576 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6578 bfd_put_32 (abfd, LD_R0_0R1 + 16, p);
6580 p = restfpr (abfd, p, r);
6581 bfd_put_32 (abfd, MTLR_R0, p);
6585 p = restfpr (abfd, p, 30);
6586 p = restfpr (abfd, p, 31);
6588 bfd_put_32 (abfd, BLR, p);
6593 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6595 p = savefpr (abfd, p, r);
6596 bfd_put_32 (abfd, BLR, p);
6601 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6603 p = restfpr (abfd, p, r);
6604 bfd_put_32 (abfd, BLR, p);
6609 savevr (bfd *abfd, bfd_byte *p, int r)
6611 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6613 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6618 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6620 p = savevr (abfd, p, r);
6621 bfd_put_32 (abfd, BLR, p);
6626 restvr (bfd *abfd, bfd_byte *p, int r)
6628 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6630 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6635 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6637 p = restvr (abfd, p, r);
6638 bfd_put_32 (abfd, BLR, p);
6642 /* Called via elf_link_hash_traverse to transfer dynamic linking
6643 information on function code symbol entries to their corresponding
6644 function descriptor symbol entries. */
6647 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6649 struct bfd_link_info *info;
6650 struct ppc_link_hash_table *htab;
6651 struct plt_entry *ent;
6652 struct ppc_link_hash_entry *fh;
6653 struct ppc_link_hash_entry *fdh;
6654 bfd_boolean force_local;
6656 fh = (struct ppc_link_hash_entry *) h;
6657 if (fh->elf.root.type == bfd_link_hash_indirect)
6661 htab = ppc_hash_table (info);
6665 /* Resolve undefined references to dot-symbols as the value
6666 in the function descriptor, if we have one in a regular object.
6667 This is to satisfy cases like ".quad .foo". Calls to functions
6668 in dynamic objects are handled elsewhere. */
6669 if (fh->elf.root.type == bfd_link_hash_undefweak
6670 && fh->was_undefined
6671 && (fdh = defined_func_desc (fh)) != NULL
6672 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6673 && opd_entry_value (fdh->elf.root.u.def.section,
6674 fdh->elf.root.u.def.value,
6675 &fh->elf.root.u.def.section,
6676 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
6678 fh->elf.root.type = fdh->elf.root.type;
6679 fh->elf.forced_local = 1;
6680 fh->elf.def_regular = fdh->elf.def_regular;
6681 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6684 /* If this is a function code symbol, transfer dynamic linking
6685 information to the function descriptor symbol. */
6689 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6690 if (ent->plt.refcount > 0)
6693 || fh->elf.root.root.string[0] != '.'
6694 || fh->elf.root.root.string[1] == '\0')
6697 /* Find the corresponding function descriptor symbol. Create it
6698 as undefined if necessary. */
6700 fdh = lookup_fdh (fh, htab);
6702 && !info->executable
6703 && (fh->elf.root.type == bfd_link_hash_undefined
6704 || fh->elf.root.type == bfd_link_hash_undefweak))
6706 fdh = make_fdh (info, fh);
6711 /* Fake function descriptors are made undefweak. If the function
6712 code symbol is strong undefined, make the fake sym the same.
6713 If the function code symbol is defined, then force the fake
6714 descriptor local; We can't support overriding of symbols in a
6715 shared library on a fake descriptor. */
6719 && fdh->elf.root.type == bfd_link_hash_undefweak)
6721 if (fh->elf.root.type == bfd_link_hash_undefined)
6723 fdh->elf.root.type = bfd_link_hash_undefined;
6724 bfd_link_add_undef (&htab->elf.root, &fdh->elf.root);
6726 else if (fh->elf.root.type == bfd_link_hash_defined
6727 || fh->elf.root.type == bfd_link_hash_defweak)
6729 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
6734 && !fdh->elf.forced_local
6735 && (!info->executable
6736 || fdh->elf.def_dynamic
6737 || fdh->elf.ref_dynamic
6738 || (fdh->elf.root.type == bfd_link_hash_undefweak
6739 && ELF_ST_VISIBILITY (fdh->elf.other) == STV_DEFAULT)))
6741 if (fdh->elf.dynindx == -1)
6742 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6744 fdh->elf.ref_regular |= fh->elf.ref_regular;
6745 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6746 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6747 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6748 if (ELF_ST_VISIBILITY (fh->elf.other) == STV_DEFAULT)
6750 move_plt_plist (fh, fdh);
6751 fdh->elf.needs_plt = 1;
6753 fdh->is_func_descriptor = 1;
6758 /* Now that the info is on the function descriptor, clear the
6759 function code sym info. Any function code syms for which we
6760 don't have a definition in a regular file, we force local.
6761 This prevents a shared library from exporting syms that have
6762 been imported from another library. Function code syms that
6763 are really in the library we must leave global to prevent the
6764 linker dragging in a definition from a static library. */
6765 force_local = (!fh->elf.def_regular
6767 || !fdh->elf.def_regular
6768 || fdh->elf.forced_local);
6769 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6774 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6775 this hook to a) provide some gcc support functions, and b) transfer
6776 dynamic linking information gathered so far on function code symbol
6777 entries, to their corresponding function descriptor symbol entries. */
6780 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
6781 struct bfd_link_info *info)
6783 struct ppc_link_hash_table *htab;
6785 static const struct sfpr_def_parms funcs[] =
6787 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6788 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6789 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6790 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6791 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6792 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6793 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6794 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6795 { "._savef", 14, 31, savefpr, savefpr1_tail },
6796 { "._restf", 14, 31, restfpr, restfpr1_tail },
6797 { "_savevr_", 20, 31, savevr, savevr_tail },
6798 { "_restvr_", 20, 31, restvr, restvr_tail }
6801 htab = ppc_hash_table (info);
6805 if (!info->relocatable
6806 && htab->elf.hgot != NULL)
6807 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
6809 if (htab->sfpr == NULL)
6810 /* We don't have any relocs. */
6813 /* Provide any missing _save* and _rest* functions. */
6814 htab->sfpr->size = 0;
6815 if (!info->relocatable)
6816 for (i = 0; i < sizeof (funcs) / sizeof (funcs[0]); i++)
6817 if (!sfpr_define (info, &funcs[i]))
6820 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6822 if (htab->sfpr->size == 0)
6823 htab->sfpr->flags |= SEC_EXCLUDE;
6828 /* Adjust a symbol defined by a dynamic object and referenced by a
6829 regular object. The current definition is in some section of the
6830 dynamic object, but we're not including those sections. We have to
6831 change the definition to something the rest of the link can
6835 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6836 struct elf_link_hash_entry *h)
6838 struct ppc_link_hash_table *htab;
6841 htab = ppc_hash_table (info);
6845 /* Deal with function syms. */
6846 if (h->type == STT_FUNC
6847 || h->type == STT_GNU_IFUNC
6850 /* Clear procedure linkage table information for any symbol that
6851 won't need a .plt entry. */
6852 struct plt_entry *ent;
6853 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6854 if (ent->plt.refcount > 0)
6857 || (h->type != STT_GNU_IFUNC
6858 && (SYMBOL_CALLS_LOCAL (info, h)
6859 || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
6860 && h->root.type == bfd_link_hash_undefweak))))
6862 h->plt.plist = NULL;
6867 h->plt.plist = NULL;
6869 /* If this is a weak symbol, and there is a real definition, the
6870 processor independent code will have arranged for us to see the
6871 real definition first, and we can just use the same value. */
6872 if (h->u.weakdef != NULL)
6874 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
6875 || h->u.weakdef->root.type == bfd_link_hash_defweak);
6876 h->root.u.def.section = h->u.weakdef->root.u.def.section;
6877 h->root.u.def.value = h->u.weakdef->root.u.def.value;
6878 if (ELIMINATE_COPY_RELOCS)
6879 h->non_got_ref = h->u.weakdef->non_got_ref;
6883 /* If we are creating a shared library, we must presume that the
6884 only references to the symbol are via the global offset table.
6885 For such cases we need not do anything here; the relocations will
6886 be handled correctly by relocate_section. */
6890 /* If there are no references to this symbol that do not use the
6891 GOT, we don't need to generate a copy reloc. */
6892 if (!h->non_got_ref)
6895 /* Don't generate a copy reloc for symbols defined in the executable. */
6896 if (!h->def_dynamic || !h->ref_regular || h->def_regular)
6899 if (ELIMINATE_COPY_RELOCS)
6901 struct ppc_link_hash_entry * eh;
6902 struct elf_dyn_relocs *p;
6904 eh = (struct ppc_link_hash_entry *) h;
6905 for (p = eh->dyn_relocs; p != NULL; p = p->next)
6907 s = p->sec->output_section;
6908 if (s != NULL && (s->flags & SEC_READONLY) != 0)
6912 /* If we didn't find any dynamic relocs in read-only sections, then
6913 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6921 if (h->plt.plist != NULL)
6923 /* We should never get here, but unfortunately there are versions
6924 of gcc out there that improperly (for this ABI) put initialized
6925 function pointers, vtable refs and suchlike in read-only
6926 sections. Allow them to proceed, but warn that this might
6927 break at runtime. */
6928 info->callbacks->einfo
6929 (_("%P: copy reloc against `%T' requires lazy plt linking; "
6930 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6931 h->root.root.string);
6934 /* This is a reference to a symbol defined by a dynamic object which
6935 is not a function. */
6937 /* We must allocate the symbol in our .dynbss section, which will
6938 become part of the .bss section of the executable. There will be
6939 an entry for this symbol in the .dynsym section. The dynamic
6940 object will contain position independent code, so all references
6941 from the dynamic object to this symbol will go through the global
6942 offset table. The dynamic linker will use the .dynsym entry to
6943 determine the address it must put in the global offset table, so
6944 both the dynamic object and the regular object will refer to the
6945 same memory location for the variable. */
6947 /* We must generate a R_PPC64_COPY reloc to tell the dynamic linker
6948 to copy the initial value out of the dynamic object and into the
6949 runtime process image. We need to remember the offset into the
6950 .rela.bss section we are going to use. */
6951 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6953 htab->relbss->size += sizeof (Elf64_External_Rela);
6959 return _bfd_elf_adjust_dynamic_copy (h, s);
6962 /* If given a function descriptor symbol, hide both the function code
6963 sym and the descriptor. */
6965 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6966 struct elf_link_hash_entry *h,
6967 bfd_boolean force_local)
6969 struct ppc_link_hash_entry *eh;
6970 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6972 eh = (struct ppc_link_hash_entry *) h;
6973 if (eh->is_func_descriptor)
6975 struct ppc_link_hash_entry *fh = eh->oh;
6980 struct ppc_link_hash_table *htab;
6983 /* We aren't supposed to use alloca in BFD because on
6984 systems which do not have alloca the version in libiberty
6985 calls xmalloc, which might cause the program to crash
6986 when it runs out of memory. This function doesn't have a
6987 return status, so there's no way to gracefully return an
6988 error. So cheat. We know that string[-1] can be safely
6989 accessed; It's either a string in an ELF string table,
6990 or allocated in an objalloc structure. */
6992 p = eh->elf.root.root.string - 1;
6995 htab = ppc_hash_table (info);
6999 fh = (struct ppc_link_hash_entry *)
7000 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7003 /* Unfortunately, if it so happens that the string we were
7004 looking for was allocated immediately before this string,
7005 then we overwrote the string terminator. That's the only
7006 reason the lookup should fail. */
7009 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7010 while (q >= eh->elf.root.root.string && *q == *p)
7012 if (q < eh->elf.root.root.string && *p == '.')
7013 fh = (struct ppc_link_hash_entry *)
7014 elf_link_hash_lookup (&htab->elf, p, FALSE, FALSE, FALSE);
7023 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7028 get_sym_h (struct elf_link_hash_entry **hp,
7029 Elf_Internal_Sym **symp,
7031 unsigned char **tls_maskp,
7032 Elf_Internal_Sym **locsymsp,
7033 unsigned long r_symndx,
7036 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7038 if (r_symndx >= symtab_hdr->sh_info)
7040 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7041 struct elf_link_hash_entry *h;
7043 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7044 h = elf_follow_link (h);
7052 if (symsecp != NULL)
7054 asection *symsec = NULL;
7055 if (h->root.type == bfd_link_hash_defined
7056 || h->root.type == bfd_link_hash_defweak)
7057 symsec = h->root.u.def.section;
7061 if (tls_maskp != NULL)
7063 struct ppc_link_hash_entry *eh;
7065 eh = (struct ppc_link_hash_entry *) h;
7066 *tls_maskp = &eh->tls_mask;
7071 Elf_Internal_Sym *sym;
7072 Elf_Internal_Sym *locsyms = *locsymsp;
7074 if (locsyms == NULL)
7076 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7077 if (locsyms == NULL)
7078 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7079 symtab_hdr->sh_info,
7080 0, NULL, NULL, NULL);
7081 if (locsyms == NULL)
7083 *locsymsp = locsyms;
7085 sym = locsyms + r_symndx;
7093 if (symsecp != NULL)
7094 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7096 if (tls_maskp != NULL)
7098 struct got_entry **lgot_ents;
7099 unsigned char *tls_mask;
7102 lgot_ents = elf_local_got_ents (ibfd);
7103 if (lgot_ents != NULL)
7105 struct plt_entry **local_plt = (struct plt_entry **)
7106 (lgot_ents + symtab_hdr->sh_info);
7107 unsigned char *lgot_masks = (unsigned char *)
7108 (local_plt + symtab_hdr->sh_info);
7109 tls_mask = &lgot_masks[r_symndx];
7111 *tls_maskp = tls_mask;
7117 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7118 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7119 type suitable for optimization, and 1 otherwise. */
7122 get_tls_mask (unsigned char **tls_maskp,
7123 unsigned long *toc_symndx,
7124 bfd_vma *toc_addend,
7125 Elf_Internal_Sym **locsymsp,
7126 const Elf_Internal_Rela *rel,
7129 unsigned long r_symndx;
7131 struct elf_link_hash_entry *h;
7132 Elf_Internal_Sym *sym;
7136 r_symndx = ELF64_R_SYM (rel->r_info);
7137 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7140 if ((*tls_maskp != NULL && **tls_maskp != 0)
7142 || ppc64_elf_section_data (sec) == NULL
7143 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7146 /* Look inside a TOC section too. */
7149 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7150 off = h->root.u.def.value;
7153 off = sym->st_value;
7154 off += rel->r_addend;
7155 BFD_ASSERT (off % 8 == 0);
7156 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7157 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7158 if (toc_symndx != NULL)
7159 *toc_symndx = r_symndx;
7160 if (toc_addend != NULL)
7161 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7162 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7164 if ((h == NULL || is_static_defined (h))
7165 && (next_r == -1 || next_r == -2))
7170 /* Find (or create) an entry in the tocsave hash table. */
7172 static struct tocsave_entry *
7173 tocsave_find (struct ppc_link_hash_table *htab,
7174 enum insert_option insert,
7175 Elf_Internal_Sym **local_syms,
7176 const Elf_Internal_Rela *irela,
7179 unsigned long r_indx;
7180 struct elf_link_hash_entry *h;
7181 Elf_Internal_Sym *sym;
7182 struct tocsave_entry ent, *p;
7184 struct tocsave_entry **slot;
7186 r_indx = ELF64_R_SYM (irela->r_info);
7187 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7189 if (ent.sec == NULL || ent.sec->output_section == NULL)
7191 (*_bfd_error_handler)
7192 (_("%B: undefined symbol on R_PPC64_TOCSAVE relocation"));
7197 ent.offset = h->root.u.def.value;
7199 ent.offset = sym->st_value;
7200 ent.offset += irela->r_addend;
7202 hash = tocsave_htab_hash (&ent);
7203 slot = ((struct tocsave_entry **)
7204 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7210 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7219 /* Adjust all global syms defined in opd sections. In gcc generated
7220 code for the old ABI, these will already have been done. */
7223 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7225 struct ppc_link_hash_entry *eh;
7227 struct _opd_sec_data *opd;
7229 if (h->root.type == bfd_link_hash_indirect)
7232 if (h->root.type != bfd_link_hash_defined
7233 && h->root.type != bfd_link_hash_defweak)
7236 eh = (struct ppc_link_hash_entry *) h;
7237 if (eh->adjust_done)
7240 sym_sec = eh->elf.root.u.def.section;
7241 opd = get_opd_info (sym_sec);
7242 if (opd != NULL && opd->adjust != NULL)
7244 long adjust = opd->adjust[eh->elf.root.u.def.value / 8];
7247 /* This entry has been deleted. */
7248 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7251 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7252 if (discarded_section (dsec))
7254 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7258 eh->elf.root.u.def.value = 0;
7259 eh->elf.root.u.def.section = dsec;
7262 eh->elf.root.u.def.value += adjust;
7263 eh->adjust_done = 1;
7268 /* Handles decrementing dynamic reloc counts for the reloc specified by
7269 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7270 have already been determined. */
7273 dec_dynrel_count (bfd_vma r_info,
7275 struct bfd_link_info *info,
7276 Elf_Internal_Sym **local_syms,
7277 struct elf_link_hash_entry *h,
7278 Elf_Internal_Sym *sym)
7280 enum elf_ppc64_reloc_type r_type;
7281 asection *sym_sec = NULL;
7283 /* Can this reloc be dynamic? This switch, and later tests here
7284 should be kept in sync with the code in check_relocs. */
7285 r_type = ELF64_R_TYPE (r_info);
7291 case R_PPC64_TPREL16:
7292 case R_PPC64_TPREL16_LO:
7293 case R_PPC64_TPREL16_HI:
7294 case R_PPC64_TPREL16_HA:
7295 case R_PPC64_TPREL16_DS:
7296 case R_PPC64_TPREL16_LO_DS:
7297 case R_PPC64_TPREL16_HIGH:
7298 case R_PPC64_TPREL16_HIGHA:
7299 case R_PPC64_TPREL16_HIGHER:
7300 case R_PPC64_TPREL16_HIGHERA:
7301 case R_PPC64_TPREL16_HIGHEST:
7302 case R_PPC64_TPREL16_HIGHESTA:
7306 case R_PPC64_TPREL64:
7307 case R_PPC64_DTPMOD64:
7308 case R_PPC64_DTPREL64:
7309 case R_PPC64_ADDR64:
7313 case R_PPC64_ADDR14:
7314 case R_PPC64_ADDR14_BRNTAKEN:
7315 case R_PPC64_ADDR14_BRTAKEN:
7316 case R_PPC64_ADDR16:
7317 case R_PPC64_ADDR16_DS:
7318 case R_PPC64_ADDR16_HA:
7319 case R_PPC64_ADDR16_HI:
7320 case R_PPC64_ADDR16_HIGH:
7321 case R_PPC64_ADDR16_HIGHA:
7322 case R_PPC64_ADDR16_HIGHER:
7323 case R_PPC64_ADDR16_HIGHERA:
7324 case R_PPC64_ADDR16_HIGHEST:
7325 case R_PPC64_ADDR16_HIGHESTA:
7326 case R_PPC64_ADDR16_LO:
7327 case R_PPC64_ADDR16_LO_DS:
7328 case R_PPC64_ADDR24:
7329 case R_PPC64_ADDR32:
7330 case R_PPC64_UADDR16:
7331 case R_PPC64_UADDR32:
7332 case R_PPC64_UADDR64:
7337 if (local_syms != NULL)
7339 unsigned long r_symndx;
7340 bfd *ibfd = sec->owner;
7342 r_symndx = ELF64_R_SYM (r_info);
7343 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7348 && (must_be_dyn_reloc (info, r_type)
7350 && (!SYMBOLIC_BIND (info, h)
7351 || h->root.type == bfd_link_hash_defweak
7352 || !h->def_regular))))
7353 || (ELIMINATE_COPY_RELOCS
7356 && (h->root.type == bfd_link_hash_defweak
7357 || !h->def_regular)))
7364 struct elf_dyn_relocs *p;
7365 struct elf_dyn_relocs **pp;
7366 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7368 /* elf_gc_sweep may have already removed all dyn relocs associated
7369 with local syms for a given section. Also, symbol flags are
7370 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7371 report a dynreloc miscount. */
7372 if (*pp == NULL && info->gc_sections)
7375 while ((p = *pp) != NULL)
7379 if (!must_be_dyn_reloc (info, r_type))
7391 struct ppc_dyn_relocs *p;
7392 struct ppc_dyn_relocs **pp;
7394 bfd_boolean is_ifunc;
7396 if (local_syms == NULL)
7397 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7398 if (sym_sec == NULL)
7401 vpp = &elf_section_data (sym_sec)->local_dynrel;
7402 pp = (struct ppc_dyn_relocs **) vpp;
7404 if (*pp == NULL && info->gc_sections)
7407 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7408 while ((p = *pp) != NULL)
7410 if (p->sec == sec && p->ifunc == is_ifunc)
7421 info->callbacks->einfo (_("%P: dynreloc miscount for %B, section %A\n"),
7423 bfd_set_error (bfd_error_bad_value);
7427 /* Remove unused Official Procedure Descriptor entries. Currently we
7428 only remove those associated with functions in discarded link-once
7429 sections, or weakly defined functions that have been overridden. It
7430 would be possible to remove many more entries for statically linked
7434 ppc64_elf_edit_opd (struct bfd_link_info *info, bfd_boolean non_overlapping)
7437 bfd_boolean some_edited = FALSE;
7438 asection *need_pad = NULL;
7440 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7443 Elf_Internal_Rela *relstart, *rel, *relend;
7444 Elf_Internal_Shdr *symtab_hdr;
7445 Elf_Internal_Sym *local_syms;
7447 struct _opd_sec_data *opd;
7448 bfd_boolean need_edit, add_aux_fields;
7449 bfd_size_type cnt_16b = 0;
7451 if (!is_ppc64_elf (ibfd))
7454 sec = bfd_get_section_by_name (ibfd, ".opd");
7455 if (sec == NULL || sec->size == 0)
7458 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7461 if (sec->output_section == bfd_abs_section_ptr)
7464 /* Look through the section relocs. */
7465 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7469 symtab_hdr = &elf_symtab_hdr (ibfd);
7471 /* Read the relocations. */
7472 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7474 if (relstart == NULL)
7477 /* First run through the relocs to check they are sane, and to
7478 determine whether we need to edit this opd section. */
7482 relend = relstart + sec->reloc_count;
7483 for (rel = relstart; rel < relend; )
7485 enum elf_ppc64_reloc_type r_type;
7486 unsigned long r_symndx;
7488 struct elf_link_hash_entry *h;
7489 Elf_Internal_Sym *sym;
7491 /* .opd contains a regular array of 16 or 24 byte entries. We're
7492 only interested in the reloc pointing to a function entry
7494 if (rel->r_offset != offset
7495 || rel + 1 >= relend
7496 || (rel + 1)->r_offset != offset + 8)
7498 /* If someone messes with .opd alignment then after a
7499 "ld -r" we might have padding in the middle of .opd.
7500 Also, there's nothing to prevent someone putting
7501 something silly in .opd with the assembler. No .opd
7502 optimization for them! */
7504 (*_bfd_error_handler)
7505 (_("%B: .opd is not a regular array of opd entries"), ibfd);
7510 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7511 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7513 (*_bfd_error_handler)
7514 (_("%B: unexpected reloc type %u in .opd section"),
7520 r_symndx = ELF64_R_SYM (rel->r_info);
7521 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7525 if (sym_sec == NULL || sym_sec->owner == NULL)
7527 const char *sym_name;
7529 sym_name = h->root.root.string;
7531 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7534 (*_bfd_error_handler)
7535 (_("%B: undefined sym `%s' in .opd section"),
7541 /* opd entries are always for functions defined in the
7542 current input bfd. If the symbol isn't defined in the
7543 input bfd, then we won't be using the function in this
7544 bfd; It must be defined in a linkonce section in another
7545 bfd, or is weak. It's also possible that we are
7546 discarding the function due to a linker script /DISCARD/,
7547 which we test for via the output_section. */
7548 if (sym_sec->owner != ibfd
7549 || sym_sec->output_section == bfd_abs_section_ptr)
7554 || (rel + 1 == relend && rel->r_offset == offset + 16))
7556 if (sec->size == offset + 24)
7561 if (rel == relend && sec->size == offset + 16)
7569 if (rel->r_offset == offset + 24)
7571 else if (rel->r_offset != offset + 16)
7573 else if (rel + 1 < relend
7574 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7575 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7580 else if (rel + 2 < relend
7581 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_ADDR64
7582 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC)
7591 add_aux_fields = non_overlapping && cnt_16b > 0;
7593 if (need_edit || add_aux_fields)
7595 Elf_Internal_Rela *write_rel;
7596 Elf_Internal_Shdr *rel_hdr;
7597 bfd_byte *rptr, *wptr;
7598 bfd_byte *new_contents;
7603 new_contents = NULL;
7604 amt = sec->size * sizeof (long) / 8;
7605 opd = &ppc64_elf_section_data (sec)->u.opd;
7606 opd->adjust = bfd_zalloc (sec->owner, amt);
7607 if (opd->adjust == NULL)
7609 ppc64_elf_section_data (sec)->sec_type = sec_opd;
7611 /* This seems a waste of time as input .opd sections are all
7612 zeros as generated by gcc, but I suppose there's no reason
7613 this will always be so. We might start putting something in
7614 the third word of .opd entries. */
7615 if ((sec->flags & SEC_IN_MEMORY) == 0)
7618 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7623 if (local_syms != NULL
7624 && symtab_hdr->contents != (unsigned char *) local_syms)
7626 if (elf_section_data (sec)->relocs != relstart)
7630 sec->contents = loc;
7631 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7634 elf_section_data (sec)->relocs = relstart;
7636 new_contents = sec->contents;
7639 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7640 if (new_contents == NULL)
7644 wptr = new_contents;
7645 rptr = sec->contents;
7647 write_rel = relstart;
7651 for (rel = relstart; rel < relend; rel++)
7653 unsigned long r_symndx;
7655 struct elf_link_hash_entry *h;
7656 Elf_Internal_Sym *sym;
7658 r_symndx = ELF64_R_SYM (rel->r_info);
7659 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7663 if (rel->r_offset == offset)
7665 struct ppc_link_hash_entry *fdh = NULL;
7667 /* See if the .opd entry is full 24 byte or
7668 16 byte (with fd_aux entry overlapped with next
7671 if ((rel + 2 == relend && sec->size == offset + 16)
7672 || (rel + 3 < relend
7673 && rel[2].r_offset == offset + 16
7674 && rel[3].r_offset == offset + 24
7675 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_ADDR64
7676 && ELF64_R_TYPE (rel[3].r_info) == R_PPC64_TOC))
7680 && h->root.root.string[0] == '.')
7682 struct ppc_link_hash_table *htab;
7684 htab = ppc_hash_table (info);
7686 fdh = lookup_fdh ((struct ppc_link_hash_entry *) h,
7689 && fdh->elf.root.type != bfd_link_hash_defined
7690 && fdh->elf.root.type != bfd_link_hash_defweak)
7694 skip = (sym_sec->owner != ibfd
7695 || sym_sec->output_section == bfd_abs_section_ptr);
7698 if (fdh != NULL && sym_sec->owner == ibfd)
7700 /* Arrange for the function descriptor sym
7702 fdh->elf.root.u.def.value = 0;
7703 fdh->elf.root.u.def.section = sym_sec;
7705 opd->adjust[rel->r_offset / 8] = -1;
7709 /* We'll be keeping this opd entry. */
7713 /* Redefine the function descriptor symbol to
7714 this location in the opd section. It is
7715 necessary to update the value here rather
7716 than using an array of adjustments as we do
7717 for local symbols, because various places
7718 in the generic ELF code use the value
7719 stored in u.def.value. */
7720 fdh->elf.root.u.def.value = wptr - new_contents;
7721 fdh->adjust_done = 1;
7724 /* Local syms are a bit tricky. We could
7725 tweak them as they can be cached, but
7726 we'd need to look through the local syms
7727 for the function descriptor sym which we
7728 don't have at the moment. So keep an
7729 array of adjustments. */
7730 opd->adjust[rel->r_offset / 8]
7731 = (wptr - new_contents) - (rptr - sec->contents);
7734 memcpy (wptr, rptr, opd_ent_size);
7735 wptr += opd_ent_size;
7736 if (add_aux_fields && opd_ent_size == 16)
7738 memset (wptr, '\0', 8);
7742 rptr += opd_ent_size;
7743 offset += opd_ent_size;
7749 && !info->relocatable
7750 && !dec_dynrel_count (rel->r_info, sec, info,
7756 /* We need to adjust any reloc offsets to point to the
7757 new opd entries. While we're at it, we may as well
7758 remove redundant relocs. */
7759 rel->r_offset += opd->adjust[(offset - opd_ent_size) / 8];
7760 if (write_rel != rel)
7761 memcpy (write_rel, rel, sizeof (*rel));
7766 sec->size = wptr - new_contents;
7767 sec->reloc_count = write_rel - relstart;
7770 free (sec->contents);
7771 sec->contents = new_contents;
7774 /* Fudge the header size too, as this is used later in
7775 elf_bfd_final_link if we are emitting relocs. */
7776 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7777 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7780 else if (elf_section_data (sec)->relocs != relstart)
7783 if (local_syms != NULL
7784 && symtab_hdr->contents != (unsigned char *) local_syms)
7786 if (!info->keep_memory)
7789 symtab_hdr->contents = (unsigned char *) local_syms;
7794 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7796 /* If we are doing a final link and the last .opd entry is just 16 byte
7797 long, add a 8 byte padding after it. */
7798 if (need_pad != NULL && !info->relocatable)
7802 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7804 BFD_ASSERT (need_pad->size > 0);
7806 p = bfd_malloc (need_pad->size + 8);
7810 if (! bfd_get_section_contents (need_pad->owner, need_pad,
7811 p, 0, need_pad->size))
7814 need_pad->contents = p;
7815 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7819 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7823 need_pad->contents = p;
7826 memset (need_pad->contents + need_pad->size, 0, 8);
7827 need_pad->size += 8;
7833 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
7836 ppc64_elf_tls_setup (struct bfd_link_info *info,
7837 int no_tls_get_addr_opt,
7840 struct ppc_link_hash_table *htab;
7842 htab = ppc_hash_table (info);
7846 if (abiversion (info->output_bfd) == 1)
7850 htab->do_multi_toc = 0;
7851 else if (!htab->do_multi_toc)
7854 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
7855 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7856 FALSE, FALSE, TRUE));
7857 /* Move dynamic linking info to the function descriptor sym. */
7858 if (htab->tls_get_addr != NULL)
7859 func_desc_adjust (&htab->tls_get_addr->elf, info);
7860 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
7861 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7862 FALSE, FALSE, TRUE));
7863 if (!no_tls_get_addr_opt)
7865 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
7867 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7868 FALSE, FALSE, TRUE);
7870 func_desc_adjust (opt, info);
7871 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7872 FALSE, FALSE, TRUE);
7874 && (opt_fd->root.type == bfd_link_hash_defined
7875 || opt_fd->root.type == bfd_link_hash_defweak))
7877 /* If glibc supports an optimized __tls_get_addr call stub,
7878 signalled by the presence of __tls_get_addr_opt, and we'll
7879 be calling __tls_get_addr via a plt call stub, then
7880 make __tls_get_addr point to __tls_get_addr_opt. */
7881 tga_fd = &htab->tls_get_addr_fd->elf;
7882 if (htab->elf.dynamic_sections_created
7884 && (tga_fd->type == STT_FUNC
7885 || tga_fd->needs_plt)
7886 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7887 || (ELF_ST_VISIBILITY (tga_fd->other) != STV_DEFAULT
7888 && tga_fd->root.type == bfd_link_hash_undefweak)))
7890 struct plt_entry *ent;
7892 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7893 if (ent->plt.refcount > 0)
7897 tga_fd->root.type = bfd_link_hash_indirect;
7898 tga_fd->root.u.i.link = &opt_fd->root;
7899 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7900 if (opt_fd->dynindx != -1)
7902 /* Use __tls_get_addr_opt in dynamic relocations. */
7903 opt_fd->dynindx = -1;
7904 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7905 opt_fd->dynstr_index);
7906 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7909 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
7910 tga = &htab->tls_get_addr->elf;
7911 if (opt != NULL && tga != NULL)
7913 tga->root.type = bfd_link_hash_indirect;
7914 tga->root.u.i.link = &opt->root;
7915 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7916 _bfd_elf_link_hash_hide_symbol (info, opt,
7918 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
7920 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7921 htab->tls_get_addr_fd->is_func_descriptor = 1;
7922 if (htab->tls_get_addr != NULL)
7924 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7925 htab->tls_get_addr->is_func = 1;
7931 no_tls_get_addr_opt = TRUE;
7933 htab->no_tls_get_addr_opt = no_tls_get_addr_opt;
7934 return _bfd_elf_tls_setup (info->output_bfd, info);
7937 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7941 branch_reloc_hash_match (const bfd *ibfd,
7942 const Elf_Internal_Rela *rel,
7943 const struct ppc_link_hash_entry *hash1,
7944 const struct ppc_link_hash_entry *hash2)
7946 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7947 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7948 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7950 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7952 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7953 struct elf_link_hash_entry *h;
7955 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7956 h = elf_follow_link (h);
7957 if (h == &hash1->elf || h == &hash2->elf)
7963 /* Run through all the TLS relocs looking for optimization
7964 opportunities. The linker has been hacked (see ppc64elf.em) to do
7965 a preliminary section layout so that we know the TLS segment
7966 offsets. We can't optimize earlier because some optimizations need
7967 to know the tp offset, and we need to optimize before allocating
7968 dynamic relocations. */
7971 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7975 struct ppc_link_hash_table *htab;
7976 unsigned char *toc_ref;
7979 if (info->relocatable || !info->executable)
7982 htab = ppc_hash_table (info);
7986 /* Make two passes over the relocs. On the first pass, mark toc
7987 entries involved with tls relocs, and check that tls relocs
7988 involved in setting up a tls_get_addr call are indeed followed by
7989 such a call. If they are not, we can't do any tls optimization.
7990 On the second pass twiddle tls_mask flags to notify
7991 relocate_section that optimization can be done, and adjust got
7992 and plt refcounts. */
7994 for (pass = 0; pass < 2; ++pass)
7995 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
7997 Elf_Internal_Sym *locsyms = NULL;
7998 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8000 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8001 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8003 Elf_Internal_Rela *relstart, *rel, *relend;
8004 bfd_boolean found_tls_get_addr_arg = 0;
8006 /* Read the relocations. */
8007 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8009 if (relstart == NULL)
8012 relend = relstart + sec->reloc_count;
8013 for (rel = relstart; rel < relend; rel++)
8015 enum elf_ppc64_reloc_type r_type;
8016 unsigned long r_symndx;
8017 struct elf_link_hash_entry *h;
8018 Elf_Internal_Sym *sym;
8020 unsigned char *tls_mask;
8021 unsigned char tls_set, tls_clear, tls_type = 0;
8023 bfd_boolean ok_tprel, is_local;
8024 long toc_ref_index = 0;
8025 int expecting_tls_get_addr = 0;
8026 bfd_boolean ret = FALSE;
8028 r_symndx = ELF64_R_SYM (rel->r_info);
8029 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8033 if (elf_section_data (sec)->relocs != relstart)
8035 if (toc_ref != NULL)
8038 && (elf_symtab_hdr (ibfd).contents
8039 != (unsigned char *) locsyms))
8046 if (h->root.type == bfd_link_hash_defined
8047 || h->root.type == bfd_link_hash_defweak)
8048 value = h->root.u.def.value;
8049 else if (h->root.type == bfd_link_hash_undefweak)
8053 found_tls_get_addr_arg = 0;
8058 /* Symbols referenced by TLS relocs must be of type
8059 STT_TLS. So no need for .opd local sym adjust. */
8060 value = sym->st_value;
8069 && h->root.type == bfd_link_hash_undefweak)
8073 value += sym_sec->output_offset;
8074 value += sym_sec->output_section->vma;
8075 value -= htab->elf.tls_sec->vma;
8076 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8077 < (bfd_vma) 1 << 32);
8081 r_type = ELF64_R_TYPE (rel->r_info);
8082 /* If this section has old-style __tls_get_addr calls
8083 without marker relocs, then check that each
8084 __tls_get_addr call reloc is preceded by a reloc
8085 that conceivably belongs to the __tls_get_addr arg
8086 setup insn. If we don't find matching arg setup
8087 relocs, don't do any tls optimization. */
8089 && sec->has_tls_get_addr_call
8091 && (h == &htab->tls_get_addr->elf
8092 || h == &htab->tls_get_addr_fd->elf)
8093 && !found_tls_get_addr_arg
8094 && is_branch_reloc (r_type))
8096 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8097 "TLS optimization disabled\n"),
8098 ibfd, sec, rel->r_offset);
8103 found_tls_get_addr_arg = 0;
8106 case R_PPC64_GOT_TLSLD16:
8107 case R_PPC64_GOT_TLSLD16_LO:
8108 expecting_tls_get_addr = 1;
8109 found_tls_get_addr_arg = 1;
8112 case R_PPC64_GOT_TLSLD16_HI:
8113 case R_PPC64_GOT_TLSLD16_HA:
8114 /* These relocs should never be against a symbol
8115 defined in a shared lib. Leave them alone if
8116 that turns out to be the case. */
8123 tls_type = TLS_TLS | TLS_LD;
8126 case R_PPC64_GOT_TLSGD16:
8127 case R_PPC64_GOT_TLSGD16_LO:
8128 expecting_tls_get_addr = 1;
8129 found_tls_get_addr_arg = 1;
8132 case R_PPC64_GOT_TLSGD16_HI:
8133 case R_PPC64_GOT_TLSGD16_HA:
8139 tls_set = TLS_TLS | TLS_TPRELGD;
8141 tls_type = TLS_TLS | TLS_GD;
8144 case R_PPC64_GOT_TPREL16_DS:
8145 case R_PPC64_GOT_TPREL16_LO_DS:
8146 case R_PPC64_GOT_TPREL16_HI:
8147 case R_PPC64_GOT_TPREL16_HA:
8152 tls_clear = TLS_TPREL;
8153 tls_type = TLS_TLS | TLS_TPREL;
8160 found_tls_get_addr_arg = 1;
8165 case R_PPC64_TOC16_LO:
8166 if (sym_sec == NULL || sym_sec != toc)
8169 /* Mark this toc entry as referenced by a TLS
8170 code sequence. We can do that now in the
8171 case of R_PPC64_TLS, and after checking for
8172 tls_get_addr for the TOC16 relocs. */
8173 if (toc_ref == NULL)
8174 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8175 if (toc_ref == NULL)
8179 value = h->root.u.def.value;
8181 value = sym->st_value;
8182 value += rel->r_addend;
8183 BFD_ASSERT (value < toc->size && value % 8 == 0);
8184 toc_ref_index = (value + toc->output_offset) / 8;
8185 if (r_type == R_PPC64_TLS
8186 || r_type == R_PPC64_TLSGD
8187 || r_type == R_PPC64_TLSLD)
8189 toc_ref[toc_ref_index] = 1;
8193 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8198 expecting_tls_get_addr = 2;
8201 case R_PPC64_TPREL64:
8205 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8210 tls_set = TLS_EXPLICIT;
8211 tls_clear = TLS_TPREL;
8216 case R_PPC64_DTPMOD64:
8220 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8222 if (rel + 1 < relend
8224 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8225 && rel[1].r_offset == rel->r_offset + 8)
8229 tls_set = TLS_EXPLICIT | TLS_GD;
8232 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
8241 tls_set = TLS_EXPLICIT;
8252 if (!expecting_tls_get_addr
8253 || !sec->has_tls_get_addr_call)
8256 if (rel + 1 < relend
8257 && branch_reloc_hash_match (ibfd, rel + 1,
8259 htab->tls_get_addr_fd))
8261 if (expecting_tls_get_addr == 2)
8263 /* Check for toc tls entries. */
8264 unsigned char *toc_tls;
8267 retval = get_tls_mask (&toc_tls, NULL, NULL,
8272 if (toc_tls != NULL)
8274 if ((*toc_tls & (TLS_GD | TLS_LD)) != 0)
8275 found_tls_get_addr_arg = 1;
8277 toc_ref[toc_ref_index] = 1;
8283 if (expecting_tls_get_addr != 1)
8286 /* Uh oh, we didn't find the expected call. We
8287 could just mark this symbol to exclude it
8288 from tls optimization but it's safer to skip
8289 the entire optimization. */
8290 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8291 "TLS optimization disabled\n"),
8292 ibfd, sec, rel->r_offset);
8297 if (expecting_tls_get_addr && htab->tls_get_addr != NULL)
8299 struct plt_entry *ent;
8300 for (ent = htab->tls_get_addr->elf.plt.plist;
8303 if (ent->addend == 0)
8305 if (ent->plt.refcount > 0)
8307 ent->plt.refcount -= 1;
8308 expecting_tls_get_addr = 0;
8314 if (expecting_tls_get_addr && htab->tls_get_addr_fd != NULL)
8316 struct plt_entry *ent;
8317 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8320 if (ent->addend == 0)
8322 if (ent->plt.refcount > 0)
8323 ent->plt.refcount -= 1;
8331 if ((tls_set & TLS_EXPLICIT) == 0)
8333 struct got_entry *ent;
8335 /* Adjust got entry for this reloc. */
8339 ent = elf_local_got_ents (ibfd)[r_symndx];
8341 for (; ent != NULL; ent = ent->next)
8342 if (ent->addend == rel->r_addend
8343 && ent->owner == ibfd
8344 && ent->tls_type == tls_type)
8351 /* We managed to get rid of a got entry. */
8352 if (ent->got.refcount > 0)
8353 ent->got.refcount -= 1;
8358 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8359 we'll lose one or two dyn relocs. */
8360 if (!dec_dynrel_count (rel->r_info, sec, info,
8364 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8366 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8372 *tls_mask |= tls_set;
8373 *tls_mask &= ~tls_clear;
8376 if (elf_section_data (sec)->relocs != relstart)
8381 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8383 if (!info->keep_memory)
8386 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8390 if (toc_ref != NULL)
8395 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8396 the values of any global symbols in a toc section that has been
8397 edited. Globals in toc sections should be a rarity, so this function
8398 sets a flag if any are found in toc sections other than the one just
8399 edited, so that futher hash table traversals can be avoided. */
8401 struct adjust_toc_info
8404 unsigned long *skip;
8405 bfd_boolean global_toc_syms;
8408 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8411 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8413 struct ppc_link_hash_entry *eh;
8414 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8417 if (h->root.type != bfd_link_hash_defined
8418 && h->root.type != bfd_link_hash_defweak)
8421 eh = (struct ppc_link_hash_entry *) h;
8422 if (eh->adjust_done)
8425 if (eh->elf.root.u.def.section == toc_inf->toc)
8427 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8428 i = toc_inf->toc->rawsize >> 3;
8430 i = eh->elf.root.u.def.value >> 3;
8432 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8434 (*_bfd_error_handler)
8435 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8438 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8439 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8442 eh->elf.root.u.def.value -= toc_inf->skip[i];
8443 eh->adjust_done = 1;
8445 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8446 toc_inf->global_toc_syms = TRUE;
8451 /* Return TRUE iff INSN is one we expect on a _LO variety toc/got reloc. */
8454 ok_lo_toc_insn (unsigned int insn)
8456 return ((insn & (0x3f << 26)) == 14u << 26 /* addi */
8457 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
8458 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
8459 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
8460 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
8461 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
8462 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
8463 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
8464 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
8465 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
8466 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
8467 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
8468 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
8469 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
8470 || ((insn & (0x3f << 26)) == 58u << 26 /* lwa,ld,lmd */
8472 || ((insn & (0x3f << 26)) == 62u << 26 /* std, stmd */
8473 && ((insn & 3) == 0 || (insn & 3) == 3))
8474 || (insn & (0x3f << 26)) == 12u << 26 /* addic */);
8477 /* Examine all relocs referencing .toc sections in order to remove
8478 unused .toc entries. */
8481 ppc64_elf_edit_toc (struct bfd_link_info *info)
8484 struct adjust_toc_info toc_inf;
8485 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8487 htab->do_toc_opt = 1;
8488 toc_inf.global_toc_syms = TRUE;
8489 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
8491 asection *toc, *sec;
8492 Elf_Internal_Shdr *symtab_hdr;
8493 Elf_Internal_Sym *local_syms;
8494 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8495 unsigned long *skip, *drop;
8496 unsigned char *used;
8497 unsigned char *keep, last, some_unused;
8499 if (!is_ppc64_elf (ibfd))
8502 toc = bfd_get_section_by_name (ibfd, ".toc");
8505 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8506 || discarded_section (toc))
8511 symtab_hdr = &elf_symtab_hdr (ibfd);
8513 /* Look at sections dropped from the final link. */
8516 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8518 if (sec->reloc_count == 0
8519 || !discarded_section (sec)
8520 || get_opd_info (sec)
8521 || (sec->flags & SEC_ALLOC) == 0
8522 || (sec->flags & SEC_DEBUGGING) != 0)
8525 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
8526 if (relstart == NULL)
8529 /* Run through the relocs to see which toc entries might be
8531 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8533 enum elf_ppc64_reloc_type r_type;
8534 unsigned long r_symndx;
8536 struct elf_link_hash_entry *h;
8537 Elf_Internal_Sym *sym;
8540 r_type = ELF64_R_TYPE (rel->r_info);
8547 case R_PPC64_TOC16_LO:
8548 case R_PPC64_TOC16_HI:
8549 case R_PPC64_TOC16_HA:
8550 case R_PPC64_TOC16_DS:
8551 case R_PPC64_TOC16_LO_DS:
8555 r_symndx = ELF64_R_SYM (rel->r_info);
8556 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8564 val = h->root.u.def.value;
8566 val = sym->st_value;
8567 val += rel->r_addend;
8569 if (val >= toc->size)
8572 /* Anything in the toc ought to be aligned to 8 bytes.
8573 If not, don't mark as unused. */
8579 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8584 skip[val >> 3] = ref_from_discarded;
8587 if (elf_section_data (sec)->relocs != relstart)
8591 /* For largetoc loads of address constants, we can convert
8592 . addis rx,2,addr@got@ha
8593 . ld ry,addr@got@l(rx)
8595 . addis rx,2,addr@toc@ha
8596 . addi ry,rx,addr@toc@l
8597 when addr is within 2G of the toc pointer. This then means
8598 that the word storing "addr" in the toc is no longer needed. */
8600 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8601 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8602 && toc->reloc_count != 0)
8604 /* Read toc relocs. */
8605 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8607 if (toc_relocs == NULL)
8610 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8612 enum elf_ppc64_reloc_type r_type;
8613 unsigned long r_symndx;
8615 struct elf_link_hash_entry *h;
8616 Elf_Internal_Sym *sym;
8619 r_type = ELF64_R_TYPE (rel->r_info);
8620 if (r_type != R_PPC64_ADDR64)
8623 r_symndx = ELF64_R_SYM (rel->r_info);
8624 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8629 || discarded_section (sym_sec))
8632 if (!SYMBOL_CALLS_LOCAL (info, h))
8637 if (h->type == STT_GNU_IFUNC)
8639 val = h->root.u.def.value;
8643 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8645 val = sym->st_value;
8647 val += rel->r_addend;
8648 val += sym_sec->output_section->vma + sym_sec->output_offset;
8650 /* We don't yet know the exact toc pointer value, but we
8651 know it will be somewhere in the toc section. Don't
8652 optimize if the difference from any possible toc
8653 pointer is outside [ff..f80008000, 7fff7fff]. */
8654 addr = toc->output_section->vma + TOC_BASE_OFF;
8655 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8658 addr = toc->output_section->vma + toc->output_section->rawsize;
8659 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8664 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8669 skip[rel->r_offset >> 3]
8670 |= can_optimize | ((rel - toc_relocs) << 2);
8677 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8681 if (local_syms != NULL
8682 && symtab_hdr->contents != (unsigned char *) local_syms)
8686 && elf_section_data (sec)->relocs != relstart)
8688 if (toc_relocs != NULL
8689 && elf_section_data (toc)->relocs != toc_relocs)
8696 /* Now check all kept sections that might reference the toc.
8697 Check the toc itself last. */
8698 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8701 sec = (sec == toc ? NULL
8702 : sec->next == NULL ? toc
8703 : sec->next == toc && toc->next ? toc->next
8708 if (sec->reloc_count == 0
8709 || discarded_section (sec)
8710 || get_opd_info (sec)
8711 || (sec->flags & SEC_ALLOC) == 0
8712 || (sec->flags & SEC_DEBUGGING) != 0)
8715 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8717 if (relstart == NULL)
8720 /* Mark toc entries referenced as used. */
8724 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8726 enum elf_ppc64_reloc_type r_type;
8727 unsigned long r_symndx;
8729 struct elf_link_hash_entry *h;
8730 Elf_Internal_Sym *sym;
8732 enum {no_check, check_lo, check_ha} insn_check;
8734 r_type = ELF64_R_TYPE (rel->r_info);
8738 insn_check = no_check;
8741 case R_PPC64_GOT_TLSLD16_HA:
8742 case R_PPC64_GOT_TLSGD16_HA:
8743 case R_PPC64_GOT_TPREL16_HA:
8744 case R_PPC64_GOT_DTPREL16_HA:
8745 case R_PPC64_GOT16_HA:
8746 case R_PPC64_TOC16_HA:
8747 insn_check = check_ha;
8750 case R_PPC64_GOT_TLSLD16_LO:
8751 case R_PPC64_GOT_TLSGD16_LO:
8752 case R_PPC64_GOT_TPREL16_LO_DS:
8753 case R_PPC64_GOT_DTPREL16_LO_DS:
8754 case R_PPC64_GOT16_LO:
8755 case R_PPC64_GOT16_LO_DS:
8756 case R_PPC64_TOC16_LO:
8757 case R_PPC64_TOC16_LO_DS:
8758 insn_check = check_lo;
8762 if (insn_check != no_check)
8764 bfd_vma off = rel->r_offset & ~3;
8765 unsigned char buf[4];
8768 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
8773 insn = bfd_get_32 (ibfd, buf);
8774 if (insn_check == check_lo
8775 ? !ok_lo_toc_insn (insn)
8776 : ((insn & ((0x3f << 26) | 0x1f << 16))
8777 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
8781 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
8782 sprintf (str, "%#08x", insn);
8783 info->callbacks->einfo
8784 (_("%P: %H: toc optimization is not supported for"
8785 " %s instruction.\n"),
8786 ibfd, sec, rel->r_offset & ~3, str);
8793 case R_PPC64_TOC16_LO:
8794 case R_PPC64_TOC16_HI:
8795 case R_PPC64_TOC16_HA:
8796 case R_PPC64_TOC16_DS:
8797 case R_PPC64_TOC16_LO_DS:
8798 /* In case we're taking addresses of toc entries. */
8799 case R_PPC64_ADDR64:
8806 r_symndx = ELF64_R_SYM (rel->r_info);
8807 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8818 val = h->root.u.def.value;
8820 val = sym->st_value;
8821 val += rel->r_addend;
8823 if (val >= toc->size)
8826 if ((skip[val >> 3] & can_optimize) != 0)
8833 case R_PPC64_TOC16_HA:
8836 case R_PPC64_TOC16_LO_DS:
8837 off = rel->r_offset;
8838 off += (bfd_big_endian (ibfd) ? -2 : 3);
8839 if (!bfd_get_section_contents (ibfd, sec, &opc,
8845 if ((opc & (0x3f << 2)) == (58u << 2))
8850 /* Wrong sort of reloc, or not a ld. We may
8851 as well clear ref_from_discarded too. */
8858 /* For the toc section, we only mark as used if this
8859 entry itself isn't unused. */
8860 else if ((used[rel->r_offset >> 3]
8861 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
8864 /* Do all the relocs again, to catch reference
8873 if (elf_section_data (sec)->relocs != relstart)
8877 /* Merge the used and skip arrays. Assume that TOC
8878 doublewords not appearing as either used or unused belong
8879 to to an entry more than one doubleword in size. */
8880 for (drop = skip, keep = used, last = 0, some_unused = 0;
8881 drop < skip + (toc->size + 7) / 8;
8886 *drop &= ~ref_from_discarded;
8887 if ((*drop & can_optimize) != 0)
8891 else if ((*drop & ref_from_discarded) != 0)
8894 last = ref_from_discarded;
8904 bfd_byte *contents, *src;
8906 Elf_Internal_Sym *sym;
8907 bfd_boolean local_toc_syms = FALSE;
8909 /* Shuffle the toc contents, and at the same time convert the
8910 skip array from booleans into offsets. */
8911 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
8914 elf_section_data (toc)->this_hdr.contents = contents;
8916 for (src = contents, off = 0, drop = skip;
8917 src < contents + toc->size;
8920 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
8925 memcpy (src - off, src, 8);
8929 toc->rawsize = toc->size;
8930 toc->size = src - contents - off;
8932 /* Adjust addends for relocs against the toc section sym,
8933 and optimize any accesses we can. */
8934 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8936 if (sec->reloc_count == 0
8937 || discarded_section (sec))
8940 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8942 if (relstart == NULL)
8945 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8947 enum elf_ppc64_reloc_type r_type;
8948 unsigned long r_symndx;
8950 struct elf_link_hash_entry *h;
8953 r_type = ELF64_R_TYPE (rel->r_info);
8960 case R_PPC64_TOC16_LO:
8961 case R_PPC64_TOC16_HI:
8962 case R_PPC64_TOC16_HA:
8963 case R_PPC64_TOC16_DS:
8964 case R_PPC64_TOC16_LO_DS:
8965 case R_PPC64_ADDR64:
8969 r_symndx = ELF64_R_SYM (rel->r_info);
8970 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8978 val = h->root.u.def.value;
8981 val = sym->st_value;
8983 local_toc_syms = TRUE;
8986 val += rel->r_addend;
8988 if (val > toc->rawsize)
8990 else if ((skip[val >> 3] & ref_from_discarded) != 0)
8992 else if ((skip[val >> 3] & can_optimize) != 0)
8994 Elf_Internal_Rela *tocrel
8995 = toc_relocs + (skip[val >> 3] >> 2);
8996 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9000 case R_PPC64_TOC16_HA:
9001 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9004 case R_PPC64_TOC16_LO_DS:
9005 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9009 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9011 info->callbacks->einfo
9012 (_("%P: %H: %s references "
9013 "optimized away TOC entry\n"),
9014 ibfd, sec, rel->r_offset,
9015 ppc64_elf_howto_table[r_type]->name);
9016 bfd_set_error (bfd_error_bad_value);
9019 rel->r_addend = tocrel->r_addend;
9020 elf_section_data (sec)->relocs = relstart;
9024 if (h != NULL || sym->st_value != 0)
9027 rel->r_addend -= skip[val >> 3];
9028 elf_section_data (sec)->relocs = relstart;
9031 if (elf_section_data (sec)->relocs != relstart)
9035 /* We shouldn't have local or global symbols defined in the TOC,
9036 but handle them anyway. */
9037 if (local_syms != NULL)
9038 for (sym = local_syms;
9039 sym < local_syms + symtab_hdr->sh_info;
9041 if (sym->st_value != 0
9042 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9046 if (sym->st_value > toc->rawsize)
9047 i = toc->rawsize >> 3;
9049 i = sym->st_value >> 3;
9051 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9054 (*_bfd_error_handler)
9055 (_("%s defined on removed toc entry"),
9056 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9059 while ((skip[i] & (ref_from_discarded | can_optimize)));
9060 sym->st_value = (bfd_vma) i << 3;
9063 sym->st_value -= skip[i];
9064 symtab_hdr->contents = (unsigned char *) local_syms;
9067 /* Adjust any global syms defined in this toc input section. */
9068 if (toc_inf.global_toc_syms)
9071 toc_inf.skip = skip;
9072 toc_inf.global_toc_syms = FALSE;
9073 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9077 if (toc->reloc_count != 0)
9079 Elf_Internal_Shdr *rel_hdr;
9080 Elf_Internal_Rela *wrel;
9083 /* Remove unused toc relocs, and adjust those we keep. */
9084 if (toc_relocs == NULL)
9085 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9087 if (toc_relocs == NULL)
9091 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9092 if ((skip[rel->r_offset >> 3]
9093 & (ref_from_discarded | can_optimize)) == 0)
9095 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9096 wrel->r_info = rel->r_info;
9097 wrel->r_addend = rel->r_addend;
9100 else if (!dec_dynrel_count (rel->r_info, toc, info,
9101 &local_syms, NULL, NULL))
9104 elf_section_data (toc)->relocs = toc_relocs;
9105 toc->reloc_count = wrel - toc_relocs;
9106 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9107 sz = rel_hdr->sh_entsize;
9108 rel_hdr->sh_size = toc->reloc_count * sz;
9111 else if (toc_relocs != NULL
9112 && elf_section_data (toc)->relocs != toc_relocs)
9115 if (local_syms != NULL
9116 && symtab_hdr->contents != (unsigned char *) local_syms)
9118 if (!info->keep_memory)
9121 symtab_hdr->contents = (unsigned char *) local_syms;
9129 /* Return true iff input section I references the TOC using
9130 instructions limited to +/-32k offsets. */
9133 ppc64_elf_has_small_toc_reloc (asection *i)
9135 return (is_ppc64_elf (i->owner)
9136 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9139 /* Allocate space for one GOT entry. */
9142 allocate_got (struct elf_link_hash_entry *h,
9143 struct bfd_link_info *info,
9144 struct got_entry *gent)
9146 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9148 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9149 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9151 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9152 ? 2 : 1) * sizeof (Elf64_External_Rela);
9153 asection *got = ppc64_elf_tdata (gent->owner)->got;
9155 gent->got.offset = got->size;
9156 got->size += entsize;
9158 dyn = htab->elf.dynamic_sections_created;
9159 if (h->type == STT_GNU_IFUNC)
9161 htab->reliplt->size += rentsize;
9162 htab->got_reli_size += rentsize;
9164 else if ((info->shared
9165 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h))
9166 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
9167 || h->root.type != bfd_link_hash_undefweak))
9169 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9170 relgot->size += rentsize;
9174 /* This function merges got entries in the same toc group. */
9177 merge_got_entries (struct got_entry **pent)
9179 struct got_entry *ent, *ent2;
9181 for (ent = *pent; ent != NULL; ent = ent->next)
9182 if (!ent->is_indirect)
9183 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9184 if (!ent2->is_indirect
9185 && ent2->addend == ent->addend
9186 && ent2->tls_type == ent->tls_type
9187 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9189 ent2->is_indirect = TRUE;
9190 ent2->got.ent = ent;
9194 /* Allocate space in .plt, .got and associated reloc sections for
9198 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9200 struct bfd_link_info *info;
9201 struct ppc_link_hash_table *htab;
9203 struct ppc_link_hash_entry *eh;
9204 struct elf_dyn_relocs *p;
9205 struct got_entry **pgent, *gent;
9207 if (h->root.type == bfd_link_hash_indirect)
9210 info = (struct bfd_link_info *) inf;
9211 htab = ppc_hash_table (info);
9215 if ((htab->elf.dynamic_sections_created
9217 && WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, h))
9218 || h->type == STT_GNU_IFUNC)
9220 struct plt_entry *pent;
9221 bfd_boolean doneone = FALSE;
9222 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9223 if (pent->plt.refcount > 0)
9225 if (!htab->elf.dynamic_sections_created
9226 || h->dynindx == -1)
9229 pent->plt.offset = s->size;
9230 s->size += PLT_ENTRY_SIZE;
9235 /* If this is the first .plt entry, make room for the special
9239 s->size += PLT_INITIAL_ENTRY_SIZE;
9241 pent->plt.offset = s->size;
9243 /* Make room for this entry. */
9244 s->size += PLT_ENTRY_SIZE;
9246 /* Make room for the .glink code. */
9249 s->size += GLINK_CALL_STUB_SIZE;
9250 /* We need bigger stubs past index 32767. */
9251 if (s->size >= GLINK_CALL_STUB_SIZE + 32768*2*4)
9255 /* We also need to make an entry in the .rela.plt section. */
9258 s->size += sizeof (Elf64_External_Rela);
9262 pent->plt.offset = (bfd_vma) -1;
9265 h->plt.plist = NULL;
9271 h->plt.plist = NULL;
9275 eh = (struct ppc_link_hash_entry *) h;
9276 /* Run through the TLS GD got entries first if we're changing them
9278 if ((eh->tls_mask & TLS_TPRELGD) != 0)
9279 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9280 if (gent->got.refcount > 0
9281 && (gent->tls_type & TLS_GD) != 0)
9283 /* This was a GD entry that has been converted to TPREL. If
9284 there happens to be a TPREL entry we can use that one. */
9285 struct got_entry *ent;
9286 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9287 if (ent->got.refcount > 0
9288 && (ent->tls_type & TLS_TPREL) != 0
9289 && ent->addend == gent->addend
9290 && ent->owner == gent->owner)
9292 gent->got.refcount = 0;
9296 /* If not, then we'll be using our own TPREL entry. */
9297 if (gent->got.refcount != 0)
9298 gent->tls_type = TLS_TLS | TLS_TPREL;
9301 /* Remove any list entry that won't generate a word in the GOT before
9302 we call merge_got_entries. Otherwise we risk merging to empty
9304 pgent = &h->got.glist;
9305 while ((gent = *pgent) != NULL)
9306 if (gent->got.refcount > 0)
9308 if ((gent->tls_type & TLS_LD) != 0
9311 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9312 *pgent = gent->next;
9315 pgent = &gent->next;
9318 *pgent = gent->next;
9320 if (!htab->do_multi_toc)
9321 merge_got_entries (&h->got.glist);
9323 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9324 if (!gent->is_indirect)
9326 /* Make sure this symbol is output as a dynamic symbol.
9327 Undefined weak syms won't yet be marked as dynamic,
9328 nor will all TLS symbols. */
9329 if (h->dynindx == -1
9331 && h->type != STT_GNU_IFUNC
9332 && htab->elf.dynamic_sections_created)
9334 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9338 if (!is_ppc64_elf (gent->owner))
9341 allocate_got (h, info, gent);
9344 if (eh->dyn_relocs == NULL
9345 || (!htab->elf.dynamic_sections_created
9346 && h->type != STT_GNU_IFUNC))
9349 /* In the shared -Bsymbolic case, discard space allocated for
9350 dynamic pc-relative relocs against symbols which turn out to be
9351 defined in regular objects. For the normal shared case, discard
9352 space for relocs that have become local due to symbol visibility
9357 /* Relocs that use pc_count are those that appear on a call insn,
9358 or certain REL relocs (see must_be_dyn_reloc) that can be
9359 generated via assembly. We want calls to protected symbols to
9360 resolve directly to the function rather than going via the plt.
9361 If people want function pointer comparisons to work as expected
9362 then they should avoid writing weird assembly. */
9363 if (SYMBOL_CALLS_LOCAL (info, h))
9365 struct elf_dyn_relocs **pp;
9367 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
9369 p->count -= p->pc_count;
9378 /* Also discard relocs on undefined weak syms with non-default
9380 if (eh->dyn_relocs != NULL
9381 && h->root.type == bfd_link_hash_undefweak)
9383 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9384 eh->dyn_relocs = NULL;
9386 /* Make sure this symbol is output as a dynamic symbol.
9387 Undefined weak syms won't yet be marked as dynamic. */
9388 else if (h->dynindx == -1
9389 && !h->forced_local)
9391 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9396 else if (h->type == STT_GNU_IFUNC)
9398 if (!h->non_got_ref)
9399 eh->dyn_relocs = NULL;
9401 else if (ELIMINATE_COPY_RELOCS)
9403 /* For the non-shared case, discard space for relocs against
9404 symbols which turn out to need copy relocs or are not
9410 /* Make sure this symbol is output as a dynamic symbol.
9411 Undefined weak syms won't yet be marked as dynamic. */
9412 if (h->dynindx == -1
9413 && !h->forced_local)
9415 if (! bfd_elf_link_record_dynamic_symbol (info, h))
9419 /* If that succeeded, we know we'll be keeping all the
9421 if (h->dynindx != -1)
9425 eh->dyn_relocs = NULL;
9430 /* Finally, allocate space. */
9431 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9433 asection *sreloc = elf_section_data (p->sec)->sreloc;
9434 if (eh->elf.type == STT_GNU_IFUNC)
9435 sreloc = htab->reliplt;
9436 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9442 /* Find any dynamic relocs that apply to read-only sections. */
9445 readonly_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9447 struct ppc_link_hash_entry *eh;
9448 struct elf_dyn_relocs *p;
9450 eh = (struct ppc_link_hash_entry *) h;
9451 for (p = eh->dyn_relocs; p != NULL; p = p->next)
9453 asection *s = p->sec->output_section;
9455 if (s != NULL && (s->flags & SEC_READONLY) != 0)
9457 struct bfd_link_info *info = inf;
9459 info->flags |= DF_TEXTREL;
9461 /* Not an error, just cut short the traversal. */
9468 /* Set the sizes of the dynamic sections. */
9471 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9472 struct bfd_link_info *info)
9474 struct ppc_link_hash_table *htab;
9479 struct got_entry *first_tlsld;
9481 htab = ppc_hash_table (info);
9485 dynobj = htab->elf.dynobj;
9489 if (htab->elf.dynamic_sections_created)
9491 /* Set the contents of the .interp section to the interpreter. */
9492 if (info->executable)
9494 s = bfd_get_linker_section (dynobj, ".interp");
9497 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
9498 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
9502 /* Set up .got offsets for local syms, and space for local dynamic
9504 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9506 struct got_entry **lgot_ents;
9507 struct got_entry **end_lgot_ents;
9508 struct plt_entry **local_plt;
9509 struct plt_entry **end_local_plt;
9510 unsigned char *lgot_masks;
9511 bfd_size_type locsymcount;
9512 Elf_Internal_Shdr *symtab_hdr;
9514 if (!is_ppc64_elf (ibfd))
9517 for (s = ibfd->sections; s != NULL; s = s->next)
9519 struct ppc_dyn_relocs *p;
9521 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
9523 if (!bfd_is_abs_section (p->sec)
9524 && bfd_is_abs_section (p->sec->output_section))
9526 /* Input section has been discarded, either because
9527 it is a copy of a linkonce section or due to
9528 linker script /DISCARD/, so we'll be discarding
9531 else if (p->count != 0)
9533 asection *srel = elf_section_data (p->sec)->sreloc;
9535 srel = htab->reliplt;
9536 srel->size += p->count * sizeof (Elf64_External_Rela);
9537 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
9538 info->flags |= DF_TEXTREL;
9543 lgot_ents = elf_local_got_ents (ibfd);
9547 symtab_hdr = &elf_symtab_hdr (ibfd);
9548 locsymcount = symtab_hdr->sh_info;
9549 end_lgot_ents = lgot_ents + locsymcount;
9550 local_plt = (struct plt_entry **) end_lgot_ents;
9551 end_local_plt = local_plt + locsymcount;
9552 lgot_masks = (unsigned char *) end_local_plt;
9553 s = ppc64_elf_tdata (ibfd)->got;
9554 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
9556 struct got_entry **pent, *ent;
9559 while ((ent = *pent) != NULL)
9560 if (ent->got.refcount > 0)
9562 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
9564 ppc64_tlsld_got (ibfd)->got.refcount += 1;
9569 unsigned int ent_size = 8;
9570 unsigned int rel_size = sizeof (Elf64_External_Rela);
9572 ent->got.offset = s->size;
9573 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
9578 s->size += ent_size;
9579 if ((*lgot_masks & PLT_IFUNC) != 0)
9581 htab->reliplt->size += rel_size;
9582 htab->got_reli_size += rel_size;
9584 else if (info->shared)
9586 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9587 srel->size += rel_size;
9596 /* Allocate space for calls to local STT_GNU_IFUNC syms in .iplt. */
9597 for (; local_plt < end_local_plt; ++local_plt)
9599 struct plt_entry *ent;
9601 for (ent = *local_plt; ent != NULL; ent = ent->next)
9602 if (ent->plt.refcount > 0)
9605 ent->plt.offset = s->size;
9606 s->size += PLT_ENTRY_SIZE;
9608 htab->reliplt->size += sizeof (Elf64_External_Rela);
9611 ent->plt.offset = (bfd_vma) -1;
9615 /* Allocate global sym .plt and .got entries, and space for global
9616 sym dynamic relocs. */
9617 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
9620 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9622 struct got_entry *ent;
9624 if (!is_ppc64_elf (ibfd))
9627 ent = ppc64_tlsld_got (ibfd);
9628 if (ent->got.refcount > 0)
9630 if (!htab->do_multi_toc && first_tlsld != NULL)
9632 ent->is_indirect = TRUE;
9633 ent->got.ent = first_tlsld;
9637 if (first_tlsld == NULL)
9639 s = ppc64_elf_tdata (ibfd)->got;
9640 ent->got.offset = s->size;
9645 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
9646 srel->size += sizeof (Elf64_External_Rela);
9651 ent->got.offset = (bfd_vma) -1;
9654 /* We now have determined the sizes of the various dynamic sections.
9655 Allocate memory for them. */
9657 for (s = dynobj->sections; s != NULL; s = s->next)
9659 if ((s->flags & SEC_LINKER_CREATED) == 0)
9662 if (s == htab->brlt || s == htab->relbrlt)
9663 /* These haven't been allocated yet; don't strip. */
9665 else if (s == htab->got
9669 || s == htab->dynbss)
9671 /* Strip this section if we don't need it; see the
9674 else if (s == htab->glink_eh_frame)
9676 if (!bfd_is_abs_section (s->output_section))
9677 /* Not sized yet. */
9680 else if (CONST_STRNEQ (s->name, ".rela"))
9684 if (s != htab->relplt)
9687 /* We use the reloc_count field as a counter if we need
9688 to copy relocs into the output file. */
9694 /* It's not one of our sections, so don't allocate space. */
9700 /* If we don't need this section, strip it from the
9701 output file. This is mostly to handle .rela.bss and
9702 .rela.plt. We must create both sections in
9703 create_dynamic_sections, because they must be created
9704 before the linker maps input sections to output
9705 sections. The linker does that before
9706 adjust_dynamic_symbol is called, and it is that
9707 function which decides whether anything needs to go
9708 into these sections. */
9709 s->flags |= SEC_EXCLUDE;
9713 if ((s->flags & SEC_HAS_CONTENTS) == 0)
9716 /* Allocate memory for the section contents. We use bfd_zalloc
9717 here in case unused entries are not reclaimed before the
9718 section's contents are written out. This should not happen,
9719 but this way if it does we get a R_PPC64_NONE reloc in .rela
9720 sections instead of garbage.
9721 We also rely on the section contents being zero when writing
9723 s->contents = bfd_zalloc (dynobj, s->size);
9724 if (s->contents == NULL)
9728 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
9730 if (!is_ppc64_elf (ibfd))
9733 s = ppc64_elf_tdata (ibfd)->got;
9734 if (s != NULL && s != htab->got)
9737 s->flags |= SEC_EXCLUDE;
9740 s->contents = bfd_zalloc (ibfd, s->size);
9741 if (s->contents == NULL)
9745 s = ppc64_elf_tdata (ibfd)->relgot;
9749 s->flags |= SEC_EXCLUDE;
9752 s->contents = bfd_zalloc (ibfd, s->size);
9753 if (s->contents == NULL)
9761 if (htab->elf.dynamic_sections_created)
9763 /* Add some entries to the .dynamic section. We fill in the
9764 values later, in ppc64_elf_finish_dynamic_sections, but we
9765 must add the entries now so that we get the correct size for
9766 the .dynamic section. The DT_DEBUG entry is filled in by the
9767 dynamic linker and used by the debugger. */
9768 #define add_dynamic_entry(TAG, VAL) \
9769 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
9771 if (info->executable)
9773 if (!add_dynamic_entry (DT_DEBUG, 0))
9777 if (htab->plt != NULL && htab->plt->size != 0)
9779 if (!add_dynamic_entry (DT_PLTGOT, 0)
9780 || !add_dynamic_entry (DT_PLTRELSZ, 0)
9781 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
9782 || !add_dynamic_entry (DT_JMPREL, 0)
9783 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
9787 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
9789 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
9790 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
9794 if (!htab->no_tls_get_addr_opt
9795 && htab->tls_get_addr_fd != NULL
9796 && htab->tls_get_addr_fd->elf.plt.plist != NULL
9797 && !add_dynamic_entry (DT_PPC64_TLSOPT, 0))
9802 if (!add_dynamic_entry (DT_RELA, 0)
9803 || !add_dynamic_entry (DT_RELASZ, 0)
9804 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
9807 /* If any dynamic relocs apply to a read-only section,
9808 then we need a DT_TEXTREL entry. */
9809 if ((info->flags & DF_TEXTREL) == 0)
9810 elf_link_hash_traverse (&htab->elf, readonly_dynrelocs, info);
9812 if ((info->flags & DF_TEXTREL) != 0)
9814 if (!add_dynamic_entry (DT_TEXTREL, 0))
9819 #undef add_dynamic_entry
9824 /* Determine the type of stub needed, if any, for a call. */
9826 static inline enum ppc_stub_type
9827 ppc_type_of_stub (asection *input_sec,
9828 const Elf_Internal_Rela *rel,
9829 struct ppc_link_hash_entry **hash,
9830 struct plt_entry **plt_ent,
9831 bfd_vma destination,
9832 unsigned long local_off)
9834 struct ppc_link_hash_entry *h = *hash;
9836 bfd_vma branch_offset;
9837 bfd_vma max_branch_offset;
9838 enum elf_ppc64_reloc_type r_type;
9842 struct plt_entry *ent;
9843 struct ppc_link_hash_entry *fdh = h;
9845 && h->oh->is_func_descriptor)
9847 fdh = ppc_follow_link (h->oh);
9851 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
9852 if (ent->addend == rel->r_addend
9853 && ent->plt.offset != (bfd_vma) -1)
9856 return ppc_stub_plt_call;
9859 /* Here, we know we don't have a plt entry. If we don't have a
9860 either a defined function descriptor or a defined entry symbol
9861 in a regular object file, then it is pointless trying to make
9862 any other type of stub. */
9863 if (!is_static_defined (&fdh->elf)
9864 && !is_static_defined (&h->elf))
9865 return ppc_stub_none;
9867 else if (elf_local_got_ents (input_sec->owner) != NULL)
9869 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
9870 struct plt_entry **local_plt = (struct plt_entry **)
9871 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
9872 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
9874 if (local_plt[r_symndx] != NULL)
9876 struct plt_entry *ent;
9878 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
9879 if (ent->addend == rel->r_addend
9880 && ent->plt.offset != (bfd_vma) -1)
9883 return ppc_stub_plt_call;
9888 /* Determine where the call point is. */
9889 location = (input_sec->output_offset
9890 + input_sec->output_section->vma
9893 branch_offset = destination - location;
9894 r_type = ELF64_R_TYPE (rel->r_info);
9896 /* Determine if a long branch stub is needed. */
9897 max_branch_offset = 1 << 25;
9898 if (r_type != R_PPC64_REL24)
9899 max_branch_offset = 1 << 15;
9901 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
9902 /* We need a stub. Figure out whether a long_branch or plt_branch
9904 return ppc_stub_long_branch;
9906 return ppc_stub_none;
9909 /* With power7 weakly ordered memory model, it is possible for ld.so
9910 to update a plt entry in one thread and have another thread see a
9911 stale zero toc entry. To avoid this we need some sort of acquire
9912 barrier in the call stub. One solution is to make the load of the
9913 toc word seem to appear to depend on the load of the function entry
9914 word. Another solution is to test for r2 being zero, and branch to
9915 the appropriate glink entry if so.
9917 . fake dep barrier compare
9918 . ld 12,xxx(2) ld 12,xxx(2)
9920 . xor 11,12,12 ld 2,xxx+8(2)
9921 . add 2,2,11 cmpldi 2,0
9922 . ld 2,xxx+8(2) bnectr+
9923 . bctr b <glink_entry>
9925 The solution involving the compare turns out to be faster, so
9926 that's what we use unless the branch won't reach. */
9928 #define ALWAYS_USE_FAKE_DEP 0
9929 #define ALWAYS_EMIT_R2SAVE 0
9931 #define PPC_LO(v) ((v) & 0xffff)
9932 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9933 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9935 static inline unsigned int
9936 plt_stub_size (struct ppc_link_hash_table *htab,
9937 struct ppc_stub_hash_entry *stub_entry,
9940 unsigned size = PLT_CALL_STUB_SIZE;
9942 if (!(ALWAYS_EMIT_R2SAVE
9943 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
9945 if (!htab->plt_static_chain)
9947 if (htab->plt_thread_safe)
9949 if (PPC_HA (off) == 0)
9951 if (PPC_HA (off + 8 + 8 * htab->plt_static_chain) != PPC_HA (off))
9953 if (stub_entry->h != NULL
9954 && (stub_entry->h == htab->tls_get_addr_fd
9955 || stub_entry->h == htab->tls_get_addr)
9956 && !htab->no_tls_get_addr_opt)
9961 /* If this stub would cross fewer 2**plt_stub_align boundaries if we align,
9962 then return the padding needed to do so. */
9963 static inline unsigned int
9964 plt_stub_pad (struct ppc_link_hash_table *htab,
9965 struct ppc_stub_hash_entry *stub_entry,
9968 int stub_align = 1 << htab->plt_stub_align;
9969 unsigned stub_size = plt_stub_size (htab, stub_entry, plt_off);
9970 bfd_vma stub_off = stub_entry->stub_sec->size;
9972 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
9973 > (stub_size & -stub_align))
9974 return stub_align - (stub_off & (stub_align - 1));
9978 /* Build a .plt call stub. */
9980 static inline bfd_byte *
9981 build_plt_stub (struct ppc_link_hash_table *htab,
9982 struct ppc_stub_hash_entry *stub_entry,
9983 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
9985 bfd *obfd = htab->stub_bfd;
9986 bfd_boolean plt_static_chain = htab->plt_static_chain;
9987 bfd_boolean plt_thread_safe = htab->plt_thread_safe;
9988 bfd_boolean use_fake_dep = plt_thread_safe;
9989 bfd_vma cmp_branch_off = 0;
9991 if (!ALWAYS_USE_FAKE_DEP
9993 && !(stub_entry->h != NULL
9994 && (stub_entry->h == htab->tls_get_addr_fd
9995 || stub_entry->h == htab->tls_get_addr)
9996 && !htab->no_tls_get_addr_opt))
9998 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
9999 bfd_vma pltindex = (pltoff - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE;
10000 bfd_vma glinkoff = GLINK_CALL_STUB_SIZE + pltindex * 8;
10003 if (pltindex > 32768)
10004 glinkoff += (pltindex - 32768) * 4;
10006 + htab->glink->output_offset
10007 + htab->glink->output_section->vma);
10008 from = (p - stub_entry->stub_sec->contents
10009 + 4 * (ALWAYS_EMIT_R2SAVE
10010 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10011 + 4 * (PPC_HA (offset) != 0)
10012 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
10013 != PPC_HA (offset))
10014 + 4 * (plt_static_chain != 0)
10016 + stub_entry->stub_sec->output_offset
10017 + stub_entry->stub_sec->output_section->vma);
10018 cmp_branch_off = to - from;
10019 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
10022 if (PPC_HA (offset) != 0)
10026 if (ALWAYS_EMIT_R2SAVE
10027 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10028 r[0].r_offset += 4;
10029 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10030 r[1].r_offset = r[0].r_offset + 4;
10031 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10032 r[1].r_addend = r[0].r_addend;
10033 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10035 r[2].r_offset = r[1].r_offset + 4;
10036 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
10037 r[2].r_addend = r[0].r_addend;
10041 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
10042 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10043 r[2].r_addend = r[0].r_addend + 8;
10044 if (plt_static_chain)
10046 r[3].r_offset = r[2].r_offset + 4;
10047 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10048 r[3].r_addend = r[0].r_addend + 16;
10052 if (ALWAYS_EMIT_R2SAVE
10053 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10054 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
10055 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
10056 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
10057 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10059 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
10062 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10065 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
10066 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
10068 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
10069 if (plt_static_chain)
10070 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
10076 if (ALWAYS_EMIT_R2SAVE
10077 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10078 r[0].r_offset += 4;
10079 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10080 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10082 r[1].r_offset = r[0].r_offset + 4;
10083 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
10084 r[1].r_addend = r[0].r_addend;
10088 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
10089 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10090 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
10091 if (plt_static_chain)
10093 r[2].r_offset = r[1].r_offset + 4;
10094 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10095 r[2].r_addend = r[0].r_addend + 8;
10099 if (ALWAYS_EMIT_R2SAVE
10100 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10101 bfd_put_32 (obfd, STD_R2_40R1, p), p += 4;
10102 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
10103 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
10105 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
10108 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
10111 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
10112 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
10114 if (plt_static_chain)
10115 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
10116 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
10118 if (plt_thread_safe && !use_fake_dep)
10120 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
10121 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
10122 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
10125 bfd_put_32 (obfd, BCTR, p), p += 4;
10129 /* Build a special .plt call stub for __tls_get_addr. */
10131 #define LD_R11_0R3 0xe9630000
10132 #define LD_R12_0R3 0xe9830000
10133 #define MR_R0_R3 0x7c601b78
10134 #define CMPDI_R11_0 0x2c2b0000
10135 #define ADD_R3_R12_R13 0x7c6c6a14
10136 #define BEQLR 0x4d820020
10137 #define MR_R3_R0 0x7c030378
10138 #define MFLR_R11 0x7d6802a6
10139 #define STD_R11_0R1 0xf9610000
10140 #define BCTRL 0x4e800421
10141 #define LD_R11_0R1 0xe9610000
10142 #define LD_R2_0R1 0xe8410000
10143 #define MTLR_R11 0x7d6803a6
10145 static inline bfd_byte *
10146 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
10147 struct ppc_stub_hash_entry *stub_entry,
10148 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10150 bfd *obfd = htab->stub_bfd;
10152 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
10153 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
10154 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
10155 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
10156 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
10157 bfd_put_32 (obfd, BEQLR, p), p += 4;
10158 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
10159 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
10160 bfd_put_32 (obfd, STD_R11_0R1 + 32, p), p += 4;
10163 r[0].r_offset += 9 * 4;
10164 p = build_plt_stub (htab, stub_entry, p, offset, r);
10165 bfd_put_32 (obfd, BCTRL, p - 4);
10167 bfd_put_32 (obfd, LD_R11_0R1 + 32, p), p += 4;
10168 bfd_put_32 (obfd, LD_R2_0R1 + 40, p), p += 4;
10169 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
10170 bfd_put_32 (obfd, BLR, p), p += 4;
10175 static Elf_Internal_Rela *
10176 get_relocs (asection *sec, int count)
10178 Elf_Internal_Rela *relocs;
10179 struct bfd_elf_section_data *elfsec_data;
10181 elfsec_data = elf_section_data (sec);
10182 relocs = elfsec_data->relocs;
10183 if (relocs == NULL)
10185 bfd_size_type relsize;
10186 relsize = sec->reloc_count * sizeof (*relocs);
10187 relocs = bfd_alloc (sec->owner, relsize);
10188 if (relocs == NULL)
10190 elfsec_data->relocs = relocs;
10191 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
10192 sizeof (Elf_Internal_Shdr));
10193 if (elfsec_data->rela.hdr == NULL)
10195 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
10196 * sizeof (Elf64_External_Rela));
10197 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
10198 sec->reloc_count = 0;
10200 relocs += sec->reloc_count;
10201 sec->reloc_count += count;
10206 get_r2off (struct bfd_link_info *info,
10207 struct ppc_stub_hash_entry *stub_entry)
10209 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10210 bfd_vma r2off = htab->stub_group[stub_entry->target_section->id].toc_off;
10214 /* Support linking -R objects. Get the toc pointer from the
10217 asection *opd = stub_entry->h->elf.root.u.def.section;
10218 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
10220 if (strcmp (opd->name, ".opd") != 0
10221 || opd->reloc_count != 0)
10223 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%T'\n"),
10224 stub_entry->h->elf.root.root.string);
10225 bfd_set_error (bfd_error_bad_value);
10228 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
10230 r2off = bfd_get_64 (opd->owner, buf);
10231 r2off -= elf_gp (info->output_bfd);
10233 r2off -= htab->stub_group[stub_entry->id_sec->id].toc_off;
10238 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10240 struct ppc_stub_hash_entry *stub_entry;
10241 struct ppc_branch_hash_entry *br_entry;
10242 struct bfd_link_info *info;
10243 struct ppc_link_hash_table *htab;
10248 Elf_Internal_Rela *r;
10251 /* Massage our args to the form they really have. */
10252 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10255 htab = ppc_hash_table (info);
10259 /* Make a note of the offset within the stubs for this entry. */
10260 stub_entry->stub_offset = stub_entry->stub_sec->size;
10261 loc = stub_entry->stub_sec->contents + stub_entry->stub_offset;
10263 htab->stub_count[stub_entry->stub_type - 1] += 1;
10264 switch (stub_entry->stub_type)
10266 case ppc_stub_long_branch:
10267 case ppc_stub_long_branch_r2off:
10268 /* Branches are relative. This is where we are going to. */
10269 dest = (stub_entry->target_value
10270 + stub_entry->target_section->output_offset
10271 + stub_entry->target_section->output_section->vma);
10272 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10275 /* And this is where we are coming from. */
10276 off -= (stub_entry->stub_offset
10277 + stub_entry->stub_sec->output_offset
10278 + stub_entry->stub_sec->output_section->vma);
10281 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10283 bfd_vma r2off = get_r2off (info, stub_entry);
10287 htab->stub_error = TRUE;
10290 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10293 if (PPC_HA (r2off) != 0)
10296 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10299 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10303 bfd_put_32 (htab->stub_bfd, B_DOT | (off & 0x3fffffc), loc);
10305 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
10307 info->callbacks->einfo
10308 (_("%P: long branch stub `%s' offset overflow\n"),
10309 stub_entry->root.string);
10310 htab->stub_error = TRUE;
10314 if (info->emitrelocations)
10316 r = get_relocs (stub_entry->stub_sec, 1);
10319 r->r_offset = loc - stub_entry->stub_sec->contents;
10320 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
10321 r->r_addend = dest;
10322 if (stub_entry->h != NULL)
10324 struct elf_link_hash_entry **hashes;
10325 unsigned long symndx;
10326 struct ppc_link_hash_entry *h;
10328 hashes = elf_sym_hashes (htab->stub_bfd);
10329 if (hashes == NULL)
10331 bfd_size_type hsize;
10333 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
10334 hashes = bfd_zalloc (htab->stub_bfd, hsize);
10335 if (hashes == NULL)
10337 elf_sym_hashes (htab->stub_bfd) = hashes;
10338 htab->stub_globals = 1;
10340 symndx = htab->stub_globals++;
10342 hashes[symndx] = &h->elf;
10343 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
10344 if (h->oh != NULL && h->oh->is_func)
10345 h = ppc_follow_link (h->oh);
10346 if (h->elf.root.u.def.section != stub_entry->target_section)
10347 /* H is an opd symbol. The addend must be zero. */
10351 off = (h->elf.root.u.def.value
10352 + h->elf.root.u.def.section->output_offset
10353 + h->elf.root.u.def.section->output_section->vma);
10354 r->r_addend -= off;
10360 case ppc_stub_plt_branch:
10361 case ppc_stub_plt_branch_r2off:
10362 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10363 stub_entry->root.string + 9,
10365 if (br_entry == NULL)
10367 info->callbacks->einfo (_("%P: can't find branch stub `%s'\n"),
10368 stub_entry->root.string);
10369 htab->stub_error = TRUE;
10373 dest = (stub_entry->target_value
10374 + stub_entry->target_section->output_offset
10375 + stub_entry->target_section->output_section->vma);
10376 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10377 dest += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10379 bfd_put_64 (htab->brlt->owner, dest,
10380 htab->brlt->contents + br_entry->offset);
10382 if (br_entry->iter == htab->stub_iteration)
10384 br_entry->iter = 0;
10386 if (htab->relbrlt != NULL)
10388 /* Create a reloc for the branch lookup table entry. */
10389 Elf_Internal_Rela rela;
10392 rela.r_offset = (br_entry->offset
10393 + htab->brlt->output_offset
10394 + htab->brlt->output_section->vma);
10395 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10396 rela.r_addend = dest;
10398 rl = htab->relbrlt->contents;
10399 rl += (htab->relbrlt->reloc_count++
10400 * sizeof (Elf64_External_Rela));
10401 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
10403 else if (info->emitrelocations)
10405 r = get_relocs (htab->brlt, 1);
10408 /* brlt, being SEC_LINKER_CREATED does not go through the
10409 normal reloc processing. Symbols and offsets are not
10410 translated from input file to output file form, so
10411 set up the offset per the output file. */
10412 r->r_offset = (br_entry->offset
10413 + htab->brlt->output_offset
10414 + htab->brlt->output_section->vma);
10415 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
10416 r->r_addend = dest;
10420 dest = (br_entry->offset
10421 + htab->brlt->output_offset
10422 + htab->brlt->output_section->vma);
10425 - elf_gp (htab->brlt->output_section->owner)
10426 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10428 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10430 info->callbacks->einfo
10431 (_("%P: linkage table error against `%T'\n"),
10432 stub_entry->root.string);
10433 bfd_set_error (bfd_error_bad_value);
10434 htab->stub_error = TRUE;
10438 if (info->emitrelocations)
10440 r = get_relocs (stub_entry->stub_sec, 1 + (PPC_HA (off) != 0));
10443 r[0].r_offset = loc - stub_entry->stub_sec->contents;
10444 if (bfd_big_endian (info->output_bfd))
10445 r[0].r_offset += 2;
10446 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
10447 r[0].r_offset += 4;
10448 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
10449 r[0].r_addend = dest;
10450 if (PPC_HA (off) != 0)
10452 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
10453 r[1].r_offset = r[0].r_offset + 4;
10454 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
10455 r[1].r_addend = r[0].r_addend;
10459 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10461 if (PPC_HA (off) != 0)
10464 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10466 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10471 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10476 bfd_vma r2off = get_r2off (info, stub_entry);
10480 htab->stub_error = TRUE;
10484 bfd_put_32 (htab->stub_bfd, STD_R2_40R1, loc);
10487 if (PPC_HA (off) != 0)
10490 bfd_put_32 (htab->stub_bfd, ADDIS_R11_R2 | PPC_HA (off), loc);
10492 bfd_put_32 (htab->stub_bfd, LD_R12_0R11 | PPC_LO (off), loc);
10497 bfd_put_32 (htab->stub_bfd, LD_R12_0R2 | PPC_LO (off), loc);
10501 if (PPC_HA (r2off) != 0)
10504 bfd_put_32 (htab->stub_bfd, ADDIS_R2_R2 | PPC_HA (r2off), loc);
10507 bfd_put_32 (htab->stub_bfd, ADDI_R2_R2 | PPC_LO (r2off), loc);
10510 bfd_put_32 (htab->stub_bfd, MTCTR_R12, loc);
10512 bfd_put_32 (htab->stub_bfd, BCTR, loc);
10515 case ppc_stub_plt_call:
10516 case ppc_stub_plt_call_r2save:
10517 if (stub_entry->h != NULL
10518 && stub_entry->h->is_func_descriptor
10519 && stub_entry->h->oh != NULL)
10521 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
10523 /* If the old-ABI "dot-symbol" is undefined make it weak so
10524 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL.
10525 FIXME: We used to define the symbol on one of the call
10526 stubs instead, which is why we test symbol section id
10527 against htab->top_id in various places. Likely all
10528 these checks could now disappear. */
10529 if (fh->elf.root.type == bfd_link_hash_undefined)
10530 fh->elf.root.type = bfd_link_hash_undefweak;
10531 /* Stop undo_symbol_twiddle changing it back to undefined. */
10532 fh->was_undefined = 0;
10535 /* Now build the stub. */
10536 dest = stub_entry->plt_ent->plt.offset & ~1;
10537 if (dest >= (bfd_vma) -2)
10541 if (!htab->elf.dynamic_sections_created
10542 || stub_entry->h == NULL
10543 || stub_entry->h->elf.dynindx == -1)
10546 dest += plt->output_offset + plt->output_section->vma;
10548 if (stub_entry->h == NULL
10549 && (stub_entry->plt_ent->plt.offset & 1) == 0)
10551 Elf_Internal_Rela rela;
10554 rela.r_offset = dest;
10556 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
10558 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
10559 rela.r_addend = (stub_entry->target_value
10560 + stub_entry->target_section->output_offset
10561 + stub_entry->target_section->output_section->vma);
10563 rl = (htab->reliplt->contents
10564 + (htab->reliplt->reloc_count++
10565 * sizeof (Elf64_External_Rela)));
10566 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, rl);
10567 stub_entry->plt_ent->plt.offset |= 1;
10571 - elf_gp (plt->output_section->owner)
10572 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10574 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
10576 info->callbacks->einfo
10577 (_("%P: linkage table error against `%T'\n"),
10578 stub_entry->h != NULL
10579 ? stub_entry->h->elf.root.root.string
10581 bfd_set_error (bfd_error_bad_value);
10582 htab->stub_error = TRUE;
10586 if (htab->plt_stub_align != 0)
10588 unsigned pad = plt_stub_pad (htab, stub_entry, off);
10590 stub_entry->stub_sec->size += pad;
10591 stub_entry->stub_offset = stub_entry->stub_sec->size;
10596 if (info->emitrelocations)
10598 r = get_relocs (stub_entry->stub_sec,
10600 + (PPC_HA (off) != 0)
10601 + (htab->plt_static_chain
10602 && PPC_HA (off + 16) == PPC_HA (off))));
10605 r[0].r_offset = loc - stub_entry->stub_sec->contents;
10606 if (bfd_big_endian (info->output_bfd))
10607 r[0].r_offset += 2;
10608 r[0].r_addend = dest;
10610 if (stub_entry->h != NULL
10611 && (stub_entry->h == htab->tls_get_addr_fd
10612 || stub_entry->h == htab->tls_get_addr)
10613 && !htab->no_tls_get_addr_opt)
10614 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
10616 p = build_plt_stub (htab, stub_entry, loc, off, r);
10625 stub_entry->stub_sec->size += size;
10627 if (htab->emit_stub_syms)
10629 struct elf_link_hash_entry *h;
10632 const char *const stub_str[] = { "long_branch",
10633 "long_branch_r2off",
10635 "plt_branch_r2off",
10639 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
10640 len2 = strlen (stub_entry->root.string);
10641 name = bfd_malloc (len1 + len2 + 2);
10644 memcpy (name, stub_entry->root.string, 9);
10645 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
10646 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
10647 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
10650 if (h->root.type == bfd_link_hash_new)
10652 h->root.type = bfd_link_hash_defined;
10653 h->root.u.def.section = stub_entry->stub_sec;
10654 h->root.u.def.value = stub_entry->stub_offset;
10655 h->ref_regular = 1;
10656 h->def_regular = 1;
10657 h->ref_regular_nonweak = 1;
10658 h->forced_local = 1;
10666 /* As above, but don't actually build the stub. Just bump offset so
10667 we know stub section sizes, and select plt_branch stubs where
10668 long_branch stubs won't do. */
10671 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
10673 struct ppc_stub_hash_entry *stub_entry;
10674 struct bfd_link_info *info;
10675 struct ppc_link_hash_table *htab;
10679 /* Massage our args to the form they really have. */
10680 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
10683 htab = ppc_hash_table (info);
10687 if (stub_entry->stub_type == ppc_stub_plt_call
10688 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10691 off = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
10692 if (off >= (bfd_vma) -2)
10695 if (!htab->elf.dynamic_sections_created
10696 || stub_entry->h == NULL
10697 || stub_entry->h->elf.dynindx == -1)
10699 off += (plt->output_offset
10700 + plt->output_section->vma
10701 - elf_gp (plt->output_section->owner)
10702 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10704 size = plt_stub_size (htab, stub_entry, off);
10705 if (htab->plt_stub_align)
10706 size += plt_stub_pad (htab, stub_entry, off);
10707 if (info->emitrelocations)
10709 stub_entry->stub_sec->reloc_count
10711 + (PPC_HA (off) != 0)
10712 + (htab->plt_static_chain
10713 && PPC_HA (off + 16) == PPC_HA (off)));
10714 stub_entry->stub_sec->flags |= SEC_RELOC;
10719 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
10722 bfd_vma local_off = 0;
10724 off = (stub_entry->target_value
10725 + stub_entry->target_section->output_offset
10726 + stub_entry->target_section->output_section->vma);
10727 off -= (stub_entry->stub_sec->size
10728 + stub_entry->stub_sec->output_offset
10729 + stub_entry->stub_sec->output_section->vma);
10731 /* Reset the stub type from the plt variant in case we now
10732 can reach with a shorter stub. */
10733 if (stub_entry->stub_type >= ppc_stub_plt_branch)
10734 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
10737 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
10739 r2off = get_r2off (info, stub_entry);
10742 htab->stub_error = TRUE;
10746 if (PPC_HA (r2off) != 0)
10751 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
10753 /* If the branch offset if too big, use a ppc_stub_plt_branch. */
10754 if (off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
10756 struct ppc_branch_hash_entry *br_entry;
10758 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
10759 stub_entry->root.string + 9,
10761 if (br_entry == NULL)
10763 info->callbacks->einfo (_("%P: can't build branch stub `%s'\n"),
10764 stub_entry->root.string);
10765 htab->stub_error = TRUE;
10769 if (br_entry->iter != htab->stub_iteration)
10771 br_entry->iter = htab->stub_iteration;
10772 br_entry->offset = htab->brlt->size;
10773 htab->brlt->size += 8;
10775 if (htab->relbrlt != NULL)
10776 htab->relbrlt->size += sizeof (Elf64_External_Rela);
10777 else if (info->emitrelocations)
10779 htab->brlt->reloc_count += 1;
10780 htab->brlt->flags |= SEC_RELOC;
10784 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
10785 off = (br_entry->offset
10786 + htab->brlt->output_offset
10787 + htab->brlt->output_section->vma
10788 - elf_gp (htab->brlt->output_section->owner)
10789 - htab->stub_group[stub_entry->id_sec->id].toc_off);
10791 if (info->emitrelocations)
10793 stub_entry->stub_sec->reloc_count += 1 + (PPC_HA (off) != 0);
10794 stub_entry->stub_sec->flags |= SEC_RELOC;
10797 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
10800 if (PPC_HA (off) != 0)
10806 if (PPC_HA (off) != 0)
10809 if (PPC_HA (r2off) != 0)
10813 else if (info->emitrelocations)
10815 stub_entry->stub_sec->reloc_count += 1;
10816 stub_entry->stub_sec->flags |= SEC_RELOC;
10820 stub_entry->stub_sec->size += size;
10824 /* Set up various things so that we can make a list of input sections
10825 for each output section included in the link. Returns -1 on error,
10826 0 when no stubs will be needed, and 1 on success. */
10829 ppc64_elf_setup_section_lists
10830 (struct bfd_link_info *info,
10831 asection *(*add_stub_section) (const char *, asection *),
10832 void (*layout_sections_again) (void))
10835 int top_id, top_index, id;
10837 asection **input_list;
10839 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10843 /* Stash our params away. */
10844 htab->add_stub_section = add_stub_section;
10845 htab->layout_sections_again = layout_sections_again;
10847 /* Find the top input section id. */
10848 for (input_bfd = info->input_bfds, top_id = 3;
10850 input_bfd = input_bfd->link_next)
10852 for (section = input_bfd->sections;
10854 section = section->next)
10856 if (top_id < section->id)
10857 top_id = section->id;
10861 htab->top_id = top_id;
10862 amt = sizeof (struct map_stub) * (top_id + 1);
10863 htab->stub_group = bfd_zmalloc (amt);
10864 if (htab->stub_group == NULL)
10867 /* Set toc_off for com, und, abs and ind sections. */
10868 for (id = 0; id < 3; id++)
10869 htab->stub_group[id].toc_off = TOC_BASE_OFF;
10871 /* We can't use output_bfd->section_count here to find the top output
10872 section index as some sections may have been removed, and
10873 strip_excluded_output_sections doesn't renumber the indices. */
10874 for (section = info->output_bfd->sections, top_index = 0;
10876 section = section->next)
10878 if (top_index < section->index)
10879 top_index = section->index;
10882 htab->top_index = top_index;
10883 amt = sizeof (asection *) * (top_index + 1);
10884 input_list = bfd_zmalloc (amt);
10885 htab->input_list = input_list;
10886 if (input_list == NULL)
10892 /* Set up for first pass at multitoc partitioning. */
10895 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
10897 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10899 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
10900 htab->toc_bfd = NULL;
10901 htab->toc_first_sec = NULL;
10904 /* The linker repeatedly calls this function for each TOC input section
10905 and linker generated GOT section. Group input bfds such that the toc
10906 within a group is less than 64k in size. */
10909 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
10911 struct ppc_link_hash_table *htab = ppc_hash_table (info);
10912 bfd_vma addr, off, limit;
10917 if (!htab->second_toc_pass)
10919 /* Keep track of the first .toc or .got section for this input bfd. */
10920 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
10924 htab->toc_bfd = isec->owner;
10925 htab->toc_first_sec = isec;
10928 addr = isec->output_offset + isec->output_section->vma;
10929 off = addr - htab->toc_curr;
10930 limit = 0x80008000;
10931 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
10933 if (off + isec->size > limit)
10935 addr = (htab->toc_first_sec->output_offset
10936 + htab->toc_first_sec->output_section->vma);
10937 htab->toc_curr = addr;
10940 /* toc_curr is the base address of this toc group. Set elf_gp
10941 for the input section to be the offset relative to the
10942 output toc base plus 0x8000. Making the input elf_gp an
10943 offset allows us to move the toc as a whole without
10944 recalculating input elf_gp. */
10945 off = htab->toc_curr - elf_gp (isec->output_section->owner);
10946 off += TOC_BASE_OFF;
10948 /* Die if someone uses a linker script that doesn't keep input
10949 file .toc and .got together. */
10951 && elf_gp (isec->owner) != 0
10952 && elf_gp (isec->owner) != off)
10955 elf_gp (isec->owner) = off;
10959 /* During the second pass toc_first_sec points to the start of
10960 a toc group, and toc_curr is used to track the old elf_gp.
10961 We use toc_bfd to ensure we only look at each bfd once. */
10962 if (htab->toc_bfd == isec->owner)
10964 htab->toc_bfd = isec->owner;
10966 if (htab->toc_first_sec == NULL
10967 || htab->toc_curr != elf_gp (isec->owner))
10969 htab->toc_curr = elf_gp (isec->owner);
10970 htab->toc_first_sec = isec;
10972 addr = (htab->toc_first_sec->output_offset
10973 + htab->toc_first_sec->output_section->vma);
10974 off = addr - elf_gp (isec->output_section->owner) + TOC_BASE_OFF;
10975 elf_gp (isec->owner) = off;
10980 /* Called via elf_link_hash_traverse to merge GOT entries for global
10984 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
10986 if (h->root.type == bfd_link_hash_indirect)
10989 merge_got_entries (&h->got.glist);
10994 /* Called via elf_link_hash_traverse to allocate GOT entries for global
10998 reallocate_got (struct elf_link_hash_entry *h, void *inf)
11000 struct got_entry *gent;
11002 if (h->root.type == bfd_link_hash_indirect)
11005 for (gent = h->got.glist; gent != NULL; gent = gent->next)
11006 if (!gent->is_indirect)
11007 allocate_got (h, (struct bfd_link_info *) inf, gent);
11011 /* Called on the first multitoc pass after the last call to
11012 ppc64_elf_next_toc_section. This function removes duplicate GOT
11016 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
11018 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11019 struct bfd *ibfd, *ibfd2;
11020 bfd_boolean done_something;
11022 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
11024 if (!htab->do_multi_toc)
11027 /* Merge global sym got entries within a toc group. */
11028 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
11030 /* And tlsld_got. */
11031 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11033 struct got_entry *ent, *ent2;
11035 if (!is_ppc64_elf (ibfd))
11038 ent = ppc64_tlsld_got (ibfd);
11039 if (!ent->is_indirect
11040 && ent->got.offset != (bfd_vma) -1)
11042 for (ibfd2 = ibfd->link_next; ibfd2 != NULL; ibfd2 = ibfd2->link_next)
11044 if (!is_ppc64_elf (ibfd2))
11047 ent2 = ppc64_tlsld_got (ibfd2);
11048 if (!ent2->is_indirect
11049 && ent2->got.offset != (bfd_vma) -1
11050 && elf_gp (ibfd2) == elf_gp (ibfd))
11052 ent2->is_indirect = TRUE;
11053 ent2->got.ent = ent;
11059 /* Zap sizes of got sections. */
11060 htab->reliplt->rawsize = htab->reliplt->size;
11061 htab->reliplt->size -= htab->got_reli_size;
11062 htab->got_reli_size = 0;
11064 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11066 asection *got, *relgot;
11068 if (!is_ppc64_elf (ibfd))
11071 got = ppc64_elf_tdata (ibfd)->got;
11074 got->rawsize = got->size;
11076 relgot = ppc64_elf_tdata (ibfd)->relgot;
11077 relgot->rawsize = relgot->size;
11082 /* Now reallocate the got, local syms first. We don't need to
11083 allocate section contents again since we never increase size. */
11084 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11086 struct got_entry **lgot_ents;
11087 struct got_entry **end_lgot_ents;
11088 struct plt_entry **local_plt;
11089 struct plt_entry **end_local_plt;
11090 unsigned char *lgot_masks;
11091 bfd_size_type locsymcount;
11092 Elf_Internal_Shdr *symtab_hdr;
11095 if (!is_ppc64_elf (ibfd))
11098 lgot_ents = elf_local_got_ents (ibfd);
11102 symtab_hdr = &elf_symtab_hdr (ibfd);
11103 locsymcount = symtab_hdr->sh_info;
11104 end_lgot_ents = lgot_ents + locsymcount;
11105 local_plt = (struct plt_entry **) end_lgot_ents;
11106 end_local_plt = local_plt + locsymcount;
11107 lgot_masks = (unsigned char *) end_local_plt;
11108 s = ppc64_elf_tdata (ibfd)->got;
11109 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
11111 struct got_entry *ent;
11113 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
11115 unsigned int ent_size = 8;
11116 unsigned int rel_size = sizeof (Elf64_External_Rela);
11118 ent->got.offset = s->size;
11119 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
11124 s->size += ent_size;
11125 if ((*lgot_masks & PLT_IFUNC) != 0)
11127 htab->reliplt->size += rel_size;
11128 htab->got_reli_size += rel_size;
11130 else if (info->shared)
11132 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11133 srel->size += rel_size;
11139 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
11141 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11143 struct got_entry *ent;
11145 if (!is_ppc64_elf (ibfd))
11148 ent = ppc64_tlsld_got (ibfd);
11149 if (!ent->is_indirect
11150 && ent->got.offset != (bfd_vma) -1)
11152 asection *s = ppc64_elf_tdata (ibfd)->got;
11153 ent->got.offset = s->size;
11157 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
11158 srel->size += sizeof (Elf64_External_Rela);
11163 done_something = htab->reliplt->rawsize != htab->reliplt->size;
11164 if (!done_something)
11165 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
11169 if (!is_ppc64_elf (ibfd))
11172 got = ppc64_elf_tdata (ibfd)->got;
11175 done_something = got->rawsize != got->size;
11176 if (done_something)
11181 if (done_something)
11182 (*htab->layout_sections_again) ();
11184 /* Set up for second pass over toc sections to recalculate elf_gp
11185 on input sections. */
11186 htab->toc_bfd = NULL;
11187 htab->toc_first_sec = NULL;
11188 htab->second_toc_pass = TRUE;
11189 return done_something;
11192 /* Called after second pass of multitoc partitioning. */
11195 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
11197 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11199 /* After the second pass, toc_curr tracks the TOC offset used
11200 for code sections below in ppc64_elf_next_input_section. */
11201 htab->toc_curr = TOC_BASE_OFF;
11204 /* No toc references were found in ISEC. If the code in ISEC makes no
11205 calls, then there's no need to use toc adjusting stubs when branching
11206 into ISEC. Actually, indirect calls from ISEC are OK as they will
11207 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
11208 needed, and 2 if a cyclical call-graph was found but no other reason
11209 for a stub was detected. If called from the top level, a return of
11210 2 means the same as a return of 0. */
11213 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
11217 /* Mark this section as checked. */
11218 isec->call_check_done = 1;
11220 /* We know none of our code bearing sections will need toc stubs. */
11221 if ((isec->flags & SEC_LINKER_CREATED) != 0)
11224 if (isec->size == 0)
11227 if (isec->output_section == NULL)
11231 if (isec->reloc_count != 0)
11233 Elf_Internal_Rela *relstart, *rel;
11234 Elf_Internal_Sym *local_syms;
11235 struct ppc_link_hash_table *htab;
11237 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
11238 info->keep_memory);
11239 if (relstart == NULL)
11242 /* Look for branches to outside of this section. */
11244 htab = ppc_hash_table (info);
11248 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
11250 enum elf_ppc64_reloc_type r_type;
11251 unsigned long r_symndx;
11252 struct elf_link_hash_entry *h;
11253 struct ppc_link_hash_entry *eh;
11254 Elf_Internal_Sym *sym;
11256 struct _opd_sec_data *opd;
11260 r_type = ELF64_R_TYPE (rel->r_info);
11261 if (r_type != R_PPC64_REL24
11262 && r_type != R_PPC64_REL14
11263 && r_type != R_PPC64_REL14_BRTAKEN
11264 && r_type != R_PPC64_REL14_BRNTAKEN)
11267 r_symndx = ELF64_R_SYM (rel->r_info);
11268 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
11275 /* Calls to dynamic lib functions go through a plt call stub
11277 eh = (struct ppc_link_hash_entry *) h;
11279 && (eh->elf.plt.plist != NULL
11281 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
11287 if (sym_sec == NULL)
11288 /* Ignore other undefined symbols. */
11291 /* Assume branches to other sections not included in the
11292 link need stubs too, to cover -R and absolute syms. */
11293 if (sym_sec->output_section == NULL)
11300 sym_value = sym->st_value;
11303 if (h->root.type != bfd_link_hash_defined
11304 && h->root.type != bfd_link_hash_defweak)
11306 sym_value = h->root.u.def.value;
11308 sym_value += rel->r_addend;
11310 /* If this branch reloc uses an opd sym, find the code section. */
11311 opd = get_opd_info (sym_sec);
11314 if (h == NULL && opd->adjust != NULL)
11318 adjust = opd->adjust[sym->st_value / 8];
11320 /* Assume deleted functions won't ever be called. */
11322 sym_value += adjust;
11325 dest = opd_entry_value (sym_sec, sym_value,
11326 &sym_sec, NULL, FALSE);
11327 if (dest == (bfd_vma) -1)
11332 + sym_sec->output_offset
11333 + sym_sec->output_section->vma);
11335 /* Ignore branch to self. */
11336 if (sym_sec == isec)
11339 /* If the called function uses the toc, we need a stub. */
11340 if (sym_sec->has_toc_reloc
11341 || sym_sec->makes_toc_func_call)
11347 /* Assume any branch that needs a long branch stub might in fact
11348 need a plt_branch stub. A plt_branch stub uses r2. */
11349 else if (dest - (isec->output_offset
11350 + isec->output_section->vma
11351 + rel->r_offset) + (1 << 25)
11352 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
11360 /* If calling back to a section in the process of being
11361 tested, we can't say for sure that no toc adjusting stubs
11362 are needed, so don't return zero. */
11363 else if (sym_sec->call_check_in_progress)
11366 /* Branches to another section that itself doesn't have any TOC
11367 references are OK. Recursively call ourselves to check. */
11368 else if (!sym_sec->call_check_done)
11372 /* Mark current section as indeterminate, so that other
11373 sections that call back to current won't be marked as
11375 isec->call_check_in_progress = 1;
11376 recur = toc_adjusting_stub_needed (info, sym_sec);
11377 isec->call_check_in_progress = 0;
11388 if (local_syms != NULL
11389 && (elf_symtab_hdr (isec->owner).contents
11390 != (unsigned char *) local_syms))
11392 if (elf_section_data (isec)->relocs != relstart)
11397 && isec->map_head.s != NULL
11398 && (strcmp (isec->output_section->name, ".init") == 0
11399 || strcmp (isec->output_section->name, ".fini") == 0))
11401 if (isec->map_head.s->has_toc_reloc
11402 || isec->map_head.s->makes_toc_func_call)
11404 else if (!isec->map_head.s->call_check_done)
11407 isec->call_check_in_progress = 1;
11408 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
11409 isec->call_check_in_progress = 0;
11416 isec->makes_toc_func_call = 1;
11421 /* The linker repeatedly calls this function for each input section,
11422 in the order that input sections are linked into output sections.
11423 Build lists of input sections to determine groupings between which
11424 we may insert linker stubs. */
11427 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
11429 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11434 if ((isec->output_section->flags & SEC_CODE) != 0
11435 && isec->output_section->index <= htab->top_index)
11437 asection **list = htab->input_list + isec->output_section->index;
11438 /* Steal the link_sec pointer for our list. */
11439 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
11440 /* This happens to make the list in reverse order,
11441 which is what we want. */
11442 PREV_SEC (isec) = *list;
11446 if (htab->multi_toc_needed)
11448 /* If a code section has a function that uses the TOC then we need
11449 to use the right TOC (obviously). Also, make sure that .opd gets
11450 the correct TOC value for R_PPC64_TOC relocs that don't have or
11451 can't find their function symbol (shouldn't ever happen now).
11452 Also specially treat .fixup for the linux kernel. .fixup
11453 contains branches, but only back to the function that hit an
11455 if (isec->has_toc_reloc
11456 || (isec->flags & SEC_CODE) == 0
11457 || strcmp (isec->name, ".fixup") == 0)
11459 if (elf_gp (isec->owner) != 0)
11460 htab->toc_curr = elf_gp (isec->owner);
11464 if (!isec->call_check_done
11465 && toc_adjusting_stub_needed (info, isec) < 0)
11467 /* If we make a local call from this section, ie. a branch
11468 without a following nop, then we have no place to put a
11469 toc restoring insn. We must use the same toc group as
11471 Testing makes_toc_func_call actually tests for *any*
11472 calls to functions that need a good toc pointer. A more
11473 precise test would be better, as this one will set
11474 incorrect values for pasted .init/.fini fragments.
11475 (Fixed later in check_pasted_section.) */
11476 if (isec->makes_toc_func_call
11477 && elf_gp (isec->owner) != 0)
11478 htab->toc_curr = elf_gp (isec->owner);
11482 /* Functions that don't use the TOC can belong in any TOC group.
11483 Use the last TOC base. */
11484 htab->stub_group[isec->id].toc_off = htab->toc_curr;
11488 /* Check that all .init and .fini sections use the same toc, if they
11489 have toc relocs. */
11492 check_pasted_section (struct bfd_link_info *info, const char *name)
11494 asection *o = bfd_get_section_by_name (info->output_bfd, name);
11498 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11499 bfd_vma toc_off = 0;
11502 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11503 if (i->has_toc_reloc)
11506 toc_off = htab->stub_group[i->id].toc_off;
11507 else if (toc_off != htab->stub_group[i->id].toc_off)
11512 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11513 if (i->makes_toc_func_call)
11515 toc_off = htab->stub_group[i->id].toc_off;
11519 /* Make sure the whole pasted function uses the same toc offset. */
11521 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
11522 htab->stub_group[i->id].toc_off = toc_off;
11528 ppc64_elf_check_init_fini (struct bfd_link_info *info)
11530 return (check_pasted_section (info, ".init")
11531 & check_pasted_section (info, ".fini"));
11534 /* See whether we can group stub sections together. Grouping stub
11535 sections may result in fewer stubs. More importantly, we need to
11536 put all .init* and .fini* stubs at the beginning of the .init or
11537 .fini output sections respectively, because glibc splits the
11538 _init and _fini functions into multiple parts. Putting a stub in
11539 the middle of a function is not a good idea. */
11542 group_sections (struct ppc_link_hash_table *htab,
11543 bfd_size_type stub_group_size,
11544 bfd_boolean stubs_always_before_branch)
11547 bfd_size_type stub14_group_size;
11548 bfd_boolean suppress_size_errors;
11550 suppress_size_errors = FALSE;
11551 stub14_group_size = stub_group_size;
11552 if (stub_group_size == 1)
11554 /* Default values. */
11555 if (stubs_always_before_branch)
11557 stub_group_size = 0x1e00000;
11558 stub14_group_size = 0x7800;
11562 stub_group_size = 0x1c00000;
11563 stub14_group_size = 0x7000;
11565 suppress_size_errors = TRUE;
11568 list = htab->input_list + htab->top_index;
11571 asection *tail = *list;
11572 while (tail != NULL)
11576 bfd_size_type total;
11577 bfd_boolean big_sec;
11581 total = tail->size;
11582 big_sec = total > (ppc64_elf_section_data (tail) != NULL
11583 && ppc64_elf_section_data (tail)->has_14bit_branch
11584 ? stub14_group_size : stub_group_size);
11585 if (big_sec && !suppress_size_errors)
11586 (*_bfd_error_handler) (_("%B section %A exceeds stub group size"),
11587 tail->owner, tail);
11588 curr_toc = htab->stub_group[tail->id].toc_off;
11590 while ((prev = PREV_SEC (curr)) != NULL
11591 && ((total += curr->output_offset - prev->output_offset)
11592 < (ppc64_elf_section_data (prev) != NULL
11593 && ppc64_elf_section_data (prev)->has_14bit_branch
11594 ? stub14_group_size : stub_group_size))
11595 && htab->stub_group[prev->id].toc_off == curr_toc)
11598 /* OK, the size from the start of CURR to the end is less
11599 than stub_group_size and thus can be handled by one stub
11600 section. (or the tail section is itself larger than
11601 stub_group_size, in which case we may be toast.) We
11602 should really be keeping track of the total size of stubs
11603 added here, as stubs contribute to the final output
11604 section size. That's a little tricky, and this way will
11605 only break if stubs added make the total size more than
11606 2^25, ie. for the default stub_group_size, if stubs total
11607 more than 2097152 bytes, or nearly 75000 plt call stubs. */
11610 prev = PREV_SEC (tail);
11611 /* Set up this stub group. */
11612 htab->stub_group[tail->id].link_sec = curr;
11614 while (tail != curr && (tail = prev) != NULL);
11616 /* But wait, there's more! Input sections up to stub_group_size
11617 bytes before the stub section can be handled by it too.
11618 Don't do this if we have a really large section after the
11619 stubs, as adding more stubs increases the chance that
11620 branches may not reach into the stub section. */
11621 if (!stubs_always_before_branch && !big_sec)
11624 while (prev != NULL
11625 && ((total += tail->output_offset - prev->output_offset)
11626 < (ppc64_elf_section_data (prev) != NULL
11627 && ppc64_elf_section_data (prev)->has_14bit_branch
11628 ? stub14_group_size : stub_group_size))
11629 && htab->stub_group[prev->id].toc_off == curr_toc)
11632 prev = PREV_SEC (tail);
11633 htab->stub_group[tail->id].link_sec = curr;
11639 while (list-- != htab->input_list);
11640 free (htab->input_list);
11644 static const unsigned char glink_eh_frame_cie[] =
11646 0, 0, 0, 16, /* length. */
11647 0, 0, 0, 0, /* id. */
11648 1, /* CIE version. */
11649 'z', 'R', 0, /* Augmentation string. */
11650 4, /* Code alignment. */
11651 0x78, /* Data alignment. */
11653 1, /* Augmentation size. */
11654 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
11655 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
11658 /* Stripping output sections is normally done before dynamic section
11659 symbols have been allocated. This function is called later, and
11660 handles cases like htab->brlt which is mapped to its own output
11664 maybe_strip_output (struct bfd_link_info *info, asection *isec)
11666 if (isec->size == 0
11667 && isec->output_section->size == 0
11668 && !(isec->output_section->flags & SEC_KEEP)
11669 && !bfd_section_removed_from_list (info->output_bfd,
11670 isec->output_section)
11671 && elf_section_data (isec->output_section)->dynindx == 0)
11673 isec->output_section->flags |= SEC_EXCLUDE;
11674 bfd_section_list_remove (info->output_bfd, isec->output_section);
11675 info->output_bfd->section_count--;
11679 /* Determine and set the size of the stub section for a final link.
11681 The basic idea here is to examine all the relocations looking for
11682 PC-relative calls to a target that is unreachable with a "bl"
11686 ppc64_elf_size_stubs (struct bfd_link_info *info, bfd_signed_vma group_size,
11687 bfd_boolean plt_static_chain, int plt_thread_safe,
11688 int plt_stub_align)
11690 bfd_size_type stub_group_size;
11691 bfd_boolean stubs_always_before_branch;
11692 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11697 htab->plt_static_chain = plt_static_chain;
11698 htab->plt_stub_align = plt_stub_align;
11699 if (plt_thread_safe == -1 && !info->executable)
11700 plt_thread_safe = 1;
11701 if (plt_thread_safe == -1)
11703 static const char *const thread_starter[] =
11707 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
11709 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
11710 "mq_notify", "create_timer",
11714 "GOMP_parallel_start",
11715 "GOMP_parallel_loop_static_start",
11716 "GOMP_parallel_loop_dynamic_start",
11717 "GOMP_parallel_loop_guided_start",
11718 "GOMP_parallel_loop_runtime_start",
11719 "GOMP_parallel_sections_start",
11723 for (i = 0; i < sizeof (thread_starter)/ sizeof (thread_starter[0]); i++)
11725 struct elf_link_hash_entry *h;
11726 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
11727 FALSE, FALSE, TRUE);
11728 plt_thread_safe = h != NULL && h->ref_regular;
11729 if (plt_thread_safe)
11733 htab->plt_thread_safe = plt_thread_safe;
11734 stubs_always_before_branch = group_size < 0;
11735 if (group_size < 0)
11736 stub_group_size = -group_size;
11738 stub_group_size = group_size;
11740 group_sections (htab, stub_group_size, stubs_always_before_branch);
11745 unsigned int bfd_indx;
11746 asection *stub_sec;
11748 htab->stub_iteration += 1;
11750 for (input_bfd = info->input_bfds, bfd_indx = 0;
11752 input_bfd = input_bfd->link_next, bfd_indx++)
11754 Elf_Internal_Shdr *symtab_hdr;
11756 Elf_Internal_Sym *local_syms = NULL;
11758 if (!is_ppc64_elf (input_bfd))
11761 /* We'll need the symbol table in a second. */
11762 symtab_hdr = &elf_symtab_hdr (input_bfd);
11763 if (symtab_hdr->sh_info == 0)
11766 /* Walk over each section attached to the input bfd. */
11767 for (section = input_bfd->sections;
11769 section = section->next)
11771 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
11773 /* If there aren't any relocs, then there's nothing more
11775 if ((section->flags & SEC_RELOC) == 0
11776 || (section->flags & SEC_ALLOC) == 0
11777 || (section->flags & SEC_LOAD) == 0
11778 || (section->flags & SEC_CODE) == 0
11779 || section->reloc_count == 0)
11782 /* If this section is a link-once section that will be
11783 discarded, then don't create any stubs. */
11784 if (section->output_section == NULL
11785 || section->output_section->owner != info->output_bfd)
11788 /* Get the relocs. */
11790 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
11791 info->keep_memory);
11792 if (internal_relocs == NULL)
11793 goto error_ret_free_local;
11795 /* Now examine each relocation. */
11796 irela = internal_relocs;
11797 irelaend = irela + section->reloc_count;
11798 for (; irela < irelaend; irela++)
11800 enum elf_ppc64_reloc_type r_type;
11801 unsigned int r_indx;
11802 enum ppc_stub_type stub_type;
11803 struct ppc_stub_hash_entry *stub_entry;
11804 asection *sym_sec, *code_sec;
11805 bfd_vma sym_value, code_value;
11806 bfd_vma destination;
11807 unsigned long local_off;
11808 bfd_boolean ok_dest;
11809 struct ppc_link_hash_entry *hash;
11810 struct ppc_link_hash_entry *fdh;
11811 struct elf_link_hash_entry *h;
11812 Elf_Internal_Sym *sym;
11814 const asection *id_sec;
11815 struct _opd_sec_data *opd;
11816 struct plt_entry *plt_ent;
11818 r_type = ELF64_R_TYPE (irela->r_info);
11819 r_indx = ELF64_R_SYM (irela->r_info);
11821 if (r_type >= R_PPC64_max)
11823 bfd_set_error (bfd_error_bad_value);
11824 goto error_ret_free_internal;
11827 /* Only look for stubs on branch instructions. */
11828 if (r_type != R_PPC64_REL24
11829 && r_type != R_PPC64_REL14
11830 && r_type != R_PPC64_REL14_BRTAKEN
11831 && r_type != R_PPC64_REL14_BRNTAKEN)
11834 /* Now determine the call target, its name, value,
11836 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
11837 r_indx, input_bfd))
11838 goto error_ret_free_internal;
11839 hash = (struct ppc_link_hash_entry *) h;
11846 sym_value = sym->st_value;
11849 else if (hash->elf.root.type == bfd_link_hash_defined
11850 || hash->elf.root.type == bfd_link_hash_defweak)
11852 sym_value = hash->elf.root.u.def.value;
11853 if (sym_sec->output_section != NULL)
11856 else if (hash->elf.root.type == bfd_link_hash_undefweak
11857 || hash->elf.root.type == bfd_link_hash_undefined)
11859 /* Recognise an old ABI func code entry sym, and
11860 use the func descriptor sym instead if it is
11862 if (hash->elf.root.root.string[0] == '.'
11863 && (fdh = lookup_fdh (hash, htab)) != NULL)
11865 if (fdh->elf.root.type == bfd_link_hash_defined
11866 || fdh->elf.root.type == bfd_link_hash_defweak)
11868 sym_sec = fdh->elf.root.u.def.section;
11869 sym_value = fdh->elf.root.u.def.value;
11870 if (sym_sec->output_section != NULL)
11879 bfd_set_error (bfd_error_bad_value);
11880 goto error_ret_free_internal;
11887 sym_value += irela->r_addend;
11888 destination = (sym_value
11889 + sym_sec->output_offset
11890 + sym_sec->output_section->vma);
11891 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
11896 code_sec = sym_sec;
11897 code_value = sym_value;
11898 opd = get_opd_info (sym_sec);
11903 if (hash == NULL && opd->adjust != NULL)
11905 long adjust = opd->adjust[sym_value / 8];
11908 code_value += adjust;
11909 sym_value += adjust;
11911 dest = opd_entry_value (sym_sec, sym_value,
11912 &code_sec, &code_value, FALSE);
11913 if (dest != (bfd_vma) -1)
11915 destination = dest;
11918 /* Fixup old ABI sym to point at code
11920 hash->elf.root.type = bfd_link_hash_defweak;
11921 hash->elf.root.u.def.section = code_sec;
11922 hash->elf.root.u.def.value = code_value;
11927 /* Determine what (if any) linker stub is needed. */
11929 stub_type = ppc_type_of_stub (section, irela, &hash,
11930 &plt_ent, destination,
11933 if (stub_type != ppc_stub_plt_call)
11935 /* Check whether we need a TOC adjusting stub.
11936 Since the linker pastes together pieces from
11937 different object files when creating the
11938 _init and _fini functions, it may be that a
11939 call to what looks like a local sym is in
11940 fact a call needing a TOC adjustment. */
11941 if (code_sec != NULL
11942 && code_sec->output_section != NULL
11943 && (htab->stub_group[code_sec->id].toc_off
11944 != htab->stub_group[section->id].toc_off)
11945 && (code_sec->has_toc_reloc
11946 || code_sec->makes_toc_func_call))
11947 stub_type = ppc_stub_long_branch_r2off;
11950 if (stub_type == ppc_stub_none)
11953 /* __tls_get_addr calls might be eliminated. */
11954 if (stub_type != ppc_stub_plt_call
11956 && (hash == htab->tls_get_addr
11957 || hash == htab->tls_get_addr_fd)
11958 && section->has_tls_reloc
11959 && irela != internal_relocs)
11961 /* Get tls info. */
11962 unsigned char *tls_mask;
11964 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
11965 irela - 1, input_bfd))
11966 goto error_ret_free_internal;
11967 if (*tls_mask != 0)
11971 if (stub_type == ppc_stub_plt_call
11972 && irela + 1 < irelaend
11973 && irela[1].r_offset == irela->r_offset + 4
11974 && ELF64_R_TYPE (irela[1].r_info) == R_PPC64_TOCSAVE)
11976 if (!tocsave_find (htab, INSERT,
11977 &local_syms, irela + 1, input_bfd))
11978 goto error_ret_free_internal;
11980 else if (stub_type == ppc_stub_plt_call)
11981 stub_type = ppc_stub_plt_call_r2save;
11983 /* Support for grouping stub sections. */
11984 id_sec = htab->stub_group[section->id].link_sec;
11986 /* Get the name of this stub. */
11987 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
11989 goto error_ret_free_internal;
11991 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
11992 stub_name, FALSE, FALSE);
11993 if (stub_entry != NULL)
11995 /* The proper stub has already been created. */
11997 if (stub_type == ppc_stub_plt_call_r2save)
11998 stub_entry->stub_type = stub_type;
12002 stub_entry = ppc_add_stub (stub_name, section, info);
12003 if (stub_entry == NULL)
12006 error_ret_free_internal:
12007 if (elf_section_data (section)->relocs == NULL)
12008 free (internal_relocs);
12009 error_ret_free_local:
12010 if (local_syms != NULL
12011 && (symtab_hdr->contents
12012 != (unsigned char *) local_syms))
12017 stub_entry->stub_type = stub_type;
12018 if (stub_type != ppc_stub_plt_call
12019 && stub_type != ppc_stub_plt_call_r2save)
12021 stub_entry->target_value = code_value;
12022 stub_entry->target_section = code_sec;
12026 stub_entry->target_value = sym_value;
12027 stub_entry->target_section = sym_sec;
12029 stub_entry->h = hash;
12030 stub_entry->plt_ent = plt_ent;
12031 stub_entry->other = hash ? hash->elf.other : sym->st_other;
12033 if (stub_entry->h != NULL)
12034 htab->stub_globals += 1;
12037 /* We're done with the internal relocs, free them. */
12038 if (elf_section_data (section)->relocs != internal_relocs)
12039 free (internal_relocs);
12042 if (local_syms != NULL
12043 && symtab_hdr->contents != (unsigned char *) local_syms)
12045 if (!info->keep_memory)
12048 symtab_hdr->contents = (unsigned char *) local_syms;
12052 /* We may have added some stubs. Find out the new size of the
12054 for (stub_sec = htab->stub_bfd->sections;
12056 stub_sec = stub_sec->next)
12057 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12059 stub_sec->rawsize = stub_sec->size;
12060 stub_sec->size = 0;
12061 stub_sec->reloc_count = 0;
12062 stub_sec->flags &= ~SEC_RELOC;
12065 htab->brlt->size = 0;
12066 htab->brlt->reloc_count = 0;
12067 htab->brlt->flags &= ~SEC_RELOC;
12068 if (htab->relbrlt != NULL)
12069 htab->relbrlt->size = 0;
12071 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
12073 if (info->emitrelocations
12074 && htab->glink != NULL && htab->glink->size != 0)
12076 htab->glink->reloc_count = 1;
12077 htab->glink->flags |= SEC_RELOC;
12080 if (htab->glink_eh_frame != NULL
12081 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
12082 && htab->glink_eh_frame->output_section->size != 0)
12084 size_t size = 0, align;
12086 for (stub_sec = htab->stub_bfd->sections;
12088 stub_sec = stub_sec->next)
12089 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12091 if (htab->glink != NULL && htab->glink->size != 0)
12094 size += sizeof (glink_eh_frame_cie);
12096 align <<= htab->glink_eh_frame->output_section->alignment_power;
12098 size = (size + align) & ~align;
12099 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12100 htab->glink_eh_frame->size = size;
12103 if (htab->plt_stub_align != 0)
12104 for (stub_sec = htab->stub_bfd->sections;
12106 stub_sec = stub_sec->next)
12107 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12108 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12109 & (-1 << htab->plt_stub_align));
12111 for (stub_sec = htab->stub_bfd->sections;
12113 stub_sec = stub_sec->next)
12114 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12115 && stub_sec->rawsize != stub_sec->size)
12118 /* Exit from this loop when no stubs have been added, and no stubs
12119 have changed size. */
12120 if (stub_sec == NULL
12121 && (htab->glink_eh_frame == NULL
12122 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
12125 /* Ask the linker to do its stuff. */
12126 (*htab->layout_sections_again) ();
12129 maybe_strip_output (info, htab->brlt);
12130 if (htab->glink_eh_frame != NULL)
12131 maybe_strip_output (info, htab->glink_eh_frame);
12136 /* Called after we have determined section placement. If sections
12137 move, we'll be called again. Provide a value for TOCstart. */
12140 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
12145 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
12146 order. The TOC starts where the first of these sections starts. */
12147 s = bfd_get_section_by_name (obfd, ".got");
12148 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12149 s = bfd_get_section_by_name (obfd, ".toc");
12150 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12151 s = bfd_get_section_by_name (obfd, ".tocbss");
12152 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12153 s = bfd_get_section_by_name (obfd, ".plt");
12154 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
12156 /* This may happen for
12157 o references to TOC base (SYM@toc / TOC[tc0]) without a
12159 o bad linker script
12160 o --gc-sections and empty TOC sections
12162 FIXME: Warn user? */
12164 /* Look for a likely section. We probably won't even be
12166 for (s = obfd->sections; s != NULL; s = s->next)
12167 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
12169 == (SEC_ALLOC | SEC_SMALL_DATA))
12172 for (s = obfd->sections; s != NULL; s = s->next)
12173 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
12174 == (SEC_ALLOC | SEC_SMALL_DATA))
12177 for (s = obfd->sections; s != NULL; s = s->next)
12178 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
12182 for (s = obfd->sections; s != NULL; s = s->next)
12183 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
12189 TOCstart = s->output_section->vma + s->output_offset;
12191 _bfd_set_gp_value (obfd, TOCstart);
12193 if (info != NULL && s != NULL && is_ppc64_elf (obfd))
12195 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12198 && htab->elf.hgot != NULL)
12200 htab->elf.hgot->type = STT_OBJECT;
12201 htab->elf.hgot->root.type = bfd_link_hash_defined;
12202 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF;
12203 htab->elf.hgot->root.u.def.section = s;
12209 /* Build all the stubs associated with the current output file.
12210 The stubs are kept in a hash table attached to the main linker
12211 hash table. This function is called via gldelf64ppc_finish. */
12214 ppc64_elf_build_stubs (bfd_boolean emit_stub_syms,
12215 struct bfd_link_info *info,
12218 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12219 asection *stub_sec;
12221 int stub_sec_count = 0;
12226 htab->emit_stub_syms = emit_stub_syms;
12228 /* Allocate memory to hold the linker stubs. */
12229 for (stub_sec = htab->stub_bfd->sections;
12231 stub_sec = stub_sec->next)
12232 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0
12233 && stub_sec->size != 0)
12235 stub_sec->contents = bfd_zalloc (htab->stub_bfd, stub_sec->size);
12236 if (stub_sec->contents == NULL)
12238 /* We want to check that built size is the same as calculated
12239 size. rawsize is a convenient location to use. */
12240 stub_sec->rawsize = stub_sec->size;
12241 stub_sec->size = 0;
12244 if (htab->glink != NULL && htab->glink->size != 0)
12249 /* Build the .glink plt call stub. */
12250 if (htab->emit_stub_syms)
12252 struct elf_link_hash_entry *h;
12253 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
12254 TRUE, FALSE, FALSE);
12257 if (h->root.type == bfd_link_hash_new)
12259 h->root.type = bfd_link_hash_defined;
12260 h->root.u.def.section = htab->glink;
12261 h->root.u.def.value = 8;
12262 h->ref_regular = 1;
12263 h->def_regular = 1;
12264 h->ref_regular_nonweak = 1;
12265 h->forced_local = 1;
12269 plt0 = htab->plt->output_section->vma + htab->plt->output_offset - 16;
12270 if (info->emitrelocations)
12272 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
12275 r->r_offset = (htab->glink->output_offset
12276 + htab->glink->output_section->vma);
12277 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
12278 r->r_addend = plt0;
12280 p = htab->glink->contents;
12281 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
12282 bfd_put_64 (htab->glink->owner, plt0, p);
12284 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
12286 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
12288 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
12290 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
12292 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
12294 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
12296 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
12298 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
12300 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
12302 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
12304 bfd_put_32 (htab->glink->owner, BCTR, p);
12306 while (p - htab->glink->contents < GLINK_CALL_STUB_SIZE)
12308 bfd_put_32 (htab->glink->owner, NOP, p);
12312 /* Build the .glink lazy link call stubs. */
12314 while (p < htab->glink->contents + htab->glink->size)
12318 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
12323 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
12325 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx), p);
12328 bfd_put_32 (htab->glink->owner,
12329 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
12333 htab->glink->rawsize = p - htab->glink->contents;
12336 if (htab->brlt->size != 0)
12338 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
12340 if (htab->brlt->contents == NULL)
12343 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
12345 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
12346 htab->relbrlt->size);
12347 if (htab->relbrlt->contents == NULL)
12351 if (htab->glink_eh_frame != NULL
12352 && htab->glink_eh_frame->size != 0)
12355 bfd_byte *last_fde;
12356 size_t last_fde_len, size, align, pad;
12358 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
12361 htab->glink_eh_frame->contents = p;
12364 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
12366 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
12367 /* CIE length (rewrite in case little-endian). */
12368 last_fde_len = sizeof (glink_eh_frame_cie) - 4;
12369 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
12370 p += sizeof (glink_eh_frame_cie);
12372 for (stub_sec = htab->stub_bfd->sections;
12374 stub_sec = stub_sec->next)
12375 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12380 bfd_put_32 (htab->elf.dynobj, 16, p);
12383 val = p - htab->glink_eh_frame->contents;
12384 bfd_put_32 (htab->elf.dynobj, val, p);
12386 /* Offset to stub section. */
12387 val = (stub_sec->output_section->vma
12388 + stub_sec->output_offset);
12389 val -= (htab->glink_eh_frame->output_section->vma
12390 + htab->glink_eh_frame->output_offset);
12391 val -= p - htab->glink_eh_frame->contents;
12392 if (val + 0x80000000 > 0xffffffff)
12394 info->callbacks->einfo
12395 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12399 bfd_put_32 (htab->elf.dynobj, val, p);
12401 /* stub section size. */
12402 bfd_put_32 (htab->elf.dynobj, stub_sec->rawsize, p);
12404 /* Augmentation. */
12409 if (htab->glink != NULL && htab->glink->size != 0)
12414 bfd_put_32 (htab->elf.dynobj, 20, p);
12417 val = p - htab->glink_eh_frame->contents;
12418 bfd_put_32 (htab->elf.dynobj, val, p);
12420 /* Offset to .glink. */
12421 val = (htab->glink->output_section->vma
12422 + htab->glink->output_offset
12424 val -= (htab->glink_eh_frame->output_section->vma
12425 + htab->glink_eh_frame->output_offset);
12426 val -= p - htab->glink_eh_frame->contents;
12427 if (val + 0x80000000 > 0xffffffff)
12429 info->callbacks->einfo
12430 (_("%P: %s offset too large for .eh_frame sdata4 encoding"),
12431 htab->glink->name);
12434 bfd_put_32 (htab->elf.dynobj, val, p);
12437 bfd_put_32 (htab->elf.dynobj, htab->glink->rawsize - 8, p);
12439 /* Augmentation. */
12442 *p++ = DW_CFA_advance_loc + 1;
12443 *p++ = DW_CFA_register;
12446 *p++ = DW_CFA_advance_loc + 4;
12447 *p++ = DW_CFA_restore_extended;
12450 /* Subsume any padding into the last FDE if user .eh_frame
12451 sections are aligned more than glink_eh_frame. Otherwise any
12452 zero padding will be seen as a terminator. */
12453 size = p - htab->glink_eh_frame->contents;
12455 align <<= htab->glink_eh_frame->output_section->alignment_power;
12457 pad = ((size + align) & ~align) - size;
12458 htab->glink_eh_frame->size = size + pad;
12459 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
12462 /* Build the stubs as directed by the stub hash table. */
12463 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
12465 if (htab->relbrlt != NULL)
12466 htab->relbrlt->reloc_count = 0;
12468 if (htab->plt_stub_align != 0)
12469 for (stub_sec = htab->stub_bfd->sections;
12471 stub_sec = stub_sec->next)
12472 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12473 stub_sec->size = ((stub_sec->size + (1 << htab->plt_stub_align) - 1)
12474 & (-1 << htab->plt_stub_align));
12476 for (stub_sec = htab->stub_bfd->sections;
12478 stub_sec = stub_sec->next)
12479 if ((stub_sec->flags & SEC_LINKER_CREATED) == 0)
12481 stub_sec_count += 1;
12482 if (stub_sec->rawsize != stub_sec->size)
12486 if (stub_sec != NULL
12487 || htab->glink->rawsize != htab->glink->size
12488 || (htab->glink_eh_frame != NULL
12489 && htab->glink_eh_frame->rawsize != htab->glink_eh_frame->size))
12491 htab->stub_error = TRUE;
12492 info->callbacks->einfo (_("%P: stubs don't match calculated size\n"));
12495 if (htab->stub_error)
12500 *stats = bfd_malloc (500);
12501 if (*stats == NULL)
12504 sprintf (*stats, _("linker stubs in %u group%s\n"
12506 " toc adjust %lu\n"
12507 " long branch %lu\n"
12508 " long toc adj %lu\n"
12510 " plt call toc %lu"),
12512 stub_sec_count == 1 ? "" : "s",
12513 htab->stub_count[ppc_stub_long_branch - 1],
12514 htab->stub_count[ppc_stub_long_branch_r2off - 1],
12515 htab->stub_count[ppc_stub_plt_branch - 1],
12516 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
12517 htab->stub_count[ppc_stub_plt_call - 1],
12518 htab->stub_count[ppc_stub_plt_call_r2save - 1]);
12523 /* This function undoes the changes made by add_symbol_adjust. */
12526 undo_symbol_twiddle (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12528 struct ppc_link_hash_entry *eh;
12530 if (h->root.type == bfd_link_hash_indirect)
12533 eh = (struct ppc_link_hash_entry *) h;
12534 if (eh->elf.root.type != bfd_link_hash_undefweak || !eh->was_undefined)
12537 eh->elf.root.type = bfd_link_hash_undefined;
12542 ppc64_elf_restore_symbols (struct bfd_link_info *info)
12544 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12547 elf_link_hash_traverse (&htab->elf, undo_symbol_twiddle, info);
12550 /* What to do when ld finds relocations against symbols defined in
12551 discarded sections. */
12553 static unsigned int
12554 ppc64_elf_action_discarded (asection *sec)
12556 if (strcmp (".opd", sec->name) == 0)
12559 if (strcmp (".toc", sec->name) == 0)
12562 if (strcmp (".toc1", sec->name) == 0)
12565 return _bfd_elf_default_action_discarded (sec);
12568 /* The RELOCATE_SECTION function is called by the ELF backend linker
12569 to handle the relocations for a section.
12571 The relocs are always passed as Rela structures; if the section
12572 actually uses Rel structures, the r_addend field will always be
12575 This function is responsible for adjust the section contents as
12576 necessary, and (if using Rela relocs and generating a
12577 relocatable output file) adjusting the reloc addend as
12580 This function does not have to worry about setting the reloc
12581 address or the reloc symbol index.
12583 LOCAL_SYMS is a pointer to the swapped in local symbols.
12585 LOCAL_SECTIONS is an array giving the section in the input file
12586 corresponding to the st_shndx field of each local symbol.
12588 The global hash table entry for the global symbols can be found
12589 via elf_sym_hashes (input_bfd).
12591 When generating relocatable output, this function must handle
12592 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
12593 going to be the section symbol corresponding to the output
12594 section, which means that the addend must be adjusted
12598 ppc64_elf_relocate_section (bfd *output_bfd,
12599 struct bfd_link_info *info,
12601 asection *input_section,
12602 bfd_byte *contents,
12603 Elf_Internal_Rela *relocs,
12604 Elf_Internal_Sym *local_syms,
12605 asection **local_sections)
12607 struct ppc_link_hash_table *htab;
12608 Elf_Internal_Shdr *symtab_hdr;
12609 struct elf_link_hash_entry **sym_hashes;
12610 Elf_Internal_Rela *rel;
12611 Elf_Internal_Rela *relend;
12612 Elf_Internal_Rela outrel;
12614 struct got_entry **local_got_ents;
12616 bfd_boolean ret = TRUE;
12617 bfd_boolean is_opd;
12618 /* Assume 'at' branch hints. */
12619 bfd_boolean is_isa_v2 = TRUE;
12620 bfd_vma d_offset = (bfd_big_endian (output_bfd) ? 2 : 0);
12622 /* Initialize howto table if needed. */
12623 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
12626 htab = ppc_hash_table (info);
12630 /* Don't relocate stub sections. */
12631 if (input_section->owner == htab->stub_bfd)
12634 BFD_ASSERT (is_ppc64_elf (input_bfd));
12636 local_got_ents = elf_local_got_ents (input_bfd);
12637 TOCstart = elf_gp (output_bfd);
12638 symtab_hdr = &elf_symtab_hdr (input_bfd);
12639 sym_hashes = elf_sym_hashes (input_bfd);
12640 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
12643 relend = relocs + input_section->reloc_count;
12644 for (; rel < relend; rel++)
12646 enum elf_ppc64_reloc_type r_type;
12648 bfd_reloc_status_type r;
12649 Elf_Internal_Sym *sym;
12651 struct elf_link_hash_entry *h_elf;
12652 struct ppc_link_hash_entry *h;
12653 struct ppc_link_hash_entry *fdh;
12654 const char *sym_name;
12655 unsigned long r_symndx, toc_symndx;
12656 bfd_vma toc_addend;
12657 unsigned char tls_mask, tls_gd, tls_type;
12658 unsigned char sym_type;
12659 bfd_vma relocation;
12660 bfd_boolean unresolved_reloc;
12661 bfd_boolean warned;
12662 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
12665 struct ppc_stub_hash_entry *stub_entry;
12666 bfd_vma max_br_offset;
12668 const Elf_Internal_Rela orig_rel = *rel;
12670 r_type = ELF64_R_TYPE (rel->r_info);
12671 r_symndx = ELF64_R_SYM (rel->r_info);
12673 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
12674 symbol of the previous ADDR64 reloc. The symbol gives us the
12675 proper TOC base to use. */
12676 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
12678 && ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_ADDR64
12680 r_symndx = ELF64_R_SYM (rel[-1].r_info);
12686 unresolved_reloc = FALSE;
12689 if (r_symndx < symtab_hdr->sh_info)
12691 /* It's a local symbol. */
12692 struct _opd_sec_data *opd;
12694 sym = local_syms + r_symndx;
12695 sec = local_sections[r_symndx];
12696 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
12697 sym_type = ELF64_ST_TYPE (sym->st_info);
12698 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
12699 opd = get_opd_info (sec);
12700 if (opd != NULL && opd->adjust != NULL)
12702 long adjust = opd->adjust[(sym->st_value + rel->r_addend) / 8];
12707 /* If this is a relocation against the opd section sym
12708 and we have edited .opd, adjust the reloc addend so
12709 that ld -r and ld --emit-relocs output is correct.
12710 If it is a reloc against some other .opd symbol,
12711 then the symbol value will be adjusted later. */
12712 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
12713 rel->r_addend += adjust;
12715 relocation += adjust;
12721 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
12722 r_symndx, symtab_hdr, sym_hashes,
12723 h_elf, sec, relocation,
12724 unresolved_reloc, warned);
12725 sym_name = h_elf->root.root.string;
12726 sym_type = h_elf->type;
12728 && sec->owner == output_bfd
12729 && strcmp (sec->name, ".opd") == 0)
12731 /* This is a symbol defined in a linker script. All
12732 such are defined in output sections, even those
12733 defined by simple assignment from a symbol defined in
12734 an input section. Transfer the symbol to an
12735 appropriate input .opd section, so that a branch to
12736 this symbol will be mapped to the location specified
12737 by the opd entry. */
12738 struct bfd_link_order *lo;
12739 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
12740 if (lo->type == bfd_indirect_link_order)
12742 asection *isec = lo->u.indirect.section;
12743 if (h_elf->root.u.def.value >= isec->output_offset
12744 && h_elf->root.u.def.value < (isec->output_offset
12747 h_elf->root.u.def.value -= isec->output_offset;
12748 h_elf->root.u.def.section = isec;
12755 h = (struct ppc_link_hash_entry *) h_elf;
12757 if (sec != NULL && discarded_section (sec))
12758 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
12760 ppc64_elf_howto_table[r_type], 0,
12763 if (info->relocatable)
12766 if (h != NULL && &h->elf == htab->elf.hgot)
12768 relocation = (TOCstart
12769 + htab->stub_group[input_section->id].toc_off);
12770 sec = bfd_abs_section_ptr;
12771 unresolved_reloc = FALSE;
12774 /* TLS optimizations. Replace instruction sequences and relocs
12775 based on information we collected in tls_optimize. We edit
12776 RELOCS so that --emit-relocs will output something sensible
12777 for the final instruction stream. */
12782 tls_mask = h->tls_mask;
12783 else if (local_got_ents != NULL)
12785 struct plt_entry **local_plt = (struct plt_entry **)
12786 (local_got_ents + symtab_hdr->sh_info);
12787 unsigned char *lgot_masks = (unsigned char *)
12788 (local_plt + symtab_hdr->sh_info);
12789 tls_mask = lgot_masks[r_symndx];
12792 && (r_type == R_PPC64_TLS
12793 || r_type == R_PPC64_TLSGD
12794 || r_type == R_PPC64_TLSLD))
12796 /* Check for toc tls entries. */
12797 unsigned char *toc_tls;
12799 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12800 &local_syms, rel, input_bfd))
12804 tls_mask = *toc_tls;
12807 /* Check that tls relocs are used with tls syms, and non-tls
12808 relocs are used with non-tls syms. */
12809 if (r_symndx != STN_UNDEF
12810 && r_type != R_PPC64_NONE
12812 || h->elf.root.type == bfd_link_hash_defined
12813 || h->elf.root.type == bfd_link_hash_defweak)
12814 && (IS_PPC64_TLS_RELOC (r_type)
12815 != (sym_type == STT_TLS
12816 || (sym_type == STT_SECTION
12817 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
12820 && (r_type == R_PPC64_TLS
12821 || r_type == R_PPC64_TLSGD
12822 || r_type == R_PPC64_TLSLD))
12823 /* R_PPC64_TLS is OK against a symbol in the TOC. */
12826 info->callbacks->einfo
12827 (!IS_PPC64_TLS_RELOC (r_type)
12828 ? _("%P: %H: %s used with TLS symbol `%T'\n")
12829 : _("%P: %H: %s used with non-TLS symbol `%T'\n"),
12830 input_bfd, input_section, rel->r_offset,
12831 ppc64_elf_howto_table[r_type]->name,
12835 /* Ensure reloc mapping code below stays sane. */
12836 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
12837 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
12838 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
12839 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
12840 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
12841 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
12842 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
12843 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
12844 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
12845 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
12853 case R_PPC64_LO_DS_OPT:
12854 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12855 if ((insn & (0x3f << 26)) != 58u << 26)
12857 insn += (14u << 26) - (58u << 26);
12858 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12859 r_type = R_PPC64_TOC16_LO;
12860 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12863 case R_PPC64_TOC16:
12864 case R_PPC64_TOC16_LO:
12865 case R_PPC64_TOC16_DS:
12866 case R_PPC64_TOC16_LO_DS:
12868 /* Check for toc tls entries. */
12869 unsigned char *toc_tls;
12872 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
12873 &local_syms, rel, input_bfd);
12879 tls_mask = *toc_tls;
12880 if (r_type == R_PPC64_TOC16_DS
12881 || r_type == R_PPC64_TOC16_LO_DS)
12884 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
12889 /* If we found a GD reloc pair, then we might be
12890 doing a GD->IE transition. */
12893 tls_gd = TLS_TPRELGD;
12894 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12897 else if (retval == 3)
12899 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12907 case R_PPC64_GOT_TPREL16_HI:
12908 case R_PPC64_GOT_TPREL16_HA:
12910 && (tls_mask & TLS_TPREL) == 0)
12912 rel->r_offset -= d_offset;
12913 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12914 r_type = R_PPC64_NONE;
12915 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12919 case R_PPC64_GOT_TPREL16_DS:
12920 case R_PPC64_GOT_TPREL16_LO_DS:
12922 && (tls_mask & TLS_TPREL) == 0)
12925 insn = bfd_get_32 (output_bfd, contents + rel->r_offset - d_offset);
12927 insn |= 0x3c0d0000; /* addis 0,13,0 */
12928 bfd_put_32 (output_bfd, insn, contents + rel->r_offset - d_offset);
12929 r_type = R_PPC64_TPREL16_HA;
12930 if (toc_symndx != 0)
12932 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12933 rel->r_addend = toc_addend;
12934 /* We changed the symbol. Start over in order to
12935 get h, sym, sec etc. right. */
12940 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12946 && (tls_mask & TLS_TPREL) == 0)
12948 insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
12949 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
12952 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
12953 /* Was PPC64_TLS which sits on insn boundary, now
12954 PPC64_TPREL16_LO which is at low-order half-word. */
12955 rel->r_offset += d_offset;
12956 r_type = R_PPC64_TPREL16_LO;
12957 if (toc_symndx != 0)
12959 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
12960 rel->r_addend = toc_addend;
12961 /* We changed the symbol. Start over in order to
12962 get h, sym, sec etc. right. */
12967 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12971 case R_PPC64_GOT_TLSGD16_HI:
12972 case R_PPC64_GOT_TLSGD16_HA:
12973 tls_gd = TLS_TPRELGD;
12974 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
12978 case R_PPC64_GOT_TLSLD16_HI:
12979 case R_PPC64_GOT_TLSLD16_HA:
12980 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
12983 if ((tls_mask & tls_gd) != 0)
12984 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
12985 + R_PPC64_GOT_TPREL16_DS);
12988 rel->r_offset -= d_offset;
12989 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
12990 r_type = R_PPC64_NONE;
12992 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
12996 case R_PPC64_GOT_TLSGD16:
12997 case R_PPC64_GOT_TLSGD16_LO:
12998 tls_gd = TLS_TPRELGD;
12999 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13003 case R_PPC64_GOT_TLSLD16:
13004 case R_PPC64_GOT_TLSLD16_LO:
13005 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13007 unsigned int insn1, insn2, insn3;
13011 offset = (bfd_vma) -1;
13012 /* If not using the newer R_PPC64_TLSGD/LD to mark
13013 __tls_get_addr calls, we must trust that the call
13014 stays with its arg setup insns, ie. that the next
13015 reloc is the __tls_get_addr call associated with
13016 the current reloc. Edit both insns. */
13017 if (input_section->has_tls_get_addr_call
13018 && rel + 1 < relend
13019 && branch_reloc_hash_match (input_bfd, rel + 1,
13020 htab->tls_get_addr,
13021 htab->tls_get_addr_fd))
13022 offset = rel[1].r_offset;
13023 if ((tls_mask & tls_gd) != 0)
13026 insn1 = bfd_get_32 (output_bfd,
13027 contents + rel->r_offset - d_offset);
13028 insn1 &= (1 << 26) - (1 << 2);
13029 insn1 |= 58 << 26; /* ld */
13030 insn2 = 0x7c636a14; /* add 3,3,13 */
13031 if (offset != (bfd_vma) -1)
13032 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13033 if ((tls_mask & TLS_EXPLICIT) == 0)
13034 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
13035 + R_PPC64_GOT_TPREL16_DS);
13037 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
13038 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13043 insn1 = 0x3c6d0000; /* addis 3,13,0 */
13044 insn2 = 0x38630000; /* addi 3,3,0 */
13047 /* Was an LD reloc. */
13049 sec = local_sections[toc_symndx];
13051 r_symndx < symtab_hdr->sh_info;
13053 if (local_sections[r_symndx] == sec)
13055 if (r_symndx >= symtab_hdr->sh_info)
13056 r_symndx = STN_UNDEF;
13057 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13058 if (r_symndx != STN_UNDEF)
13059 rel->r_addend -= (local_syms[r_symndx].st_value
13060 + sec->output_offset
13061 + sec->output_section->vma);
13063 else if (toc_symndx != 0)
13065 r_symndx = toc_symndx;
13066 rel->r_addend = toc_addend;
13068 r_type = R_PPC64_TPREL16_HA;
13069 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13070 if (offset != (bfd_vma) -1)
13072 rel[1].r_info = ELF64_R_INFO (r_symndx,
13073 R_PPC64_TPREL16_LO);
13074 rel[1].r_offset = offset + d_offset;
13075 rel[1].r_addend = rel->r_addend;
13078 bfd_put_32 (output_bfd, insn1,
13079 contents + rel->r_offset - d_offset);
13080 if (offset != (bfd_vma) -1)
13082 insn3 = bfd_get_32 (output_bfd,
13083 contents + offset + 4);
13085 || insn3 == CROR_151515 || insn3 == CROR_313131)
13087 rel[1].r_offset += 4;
13088 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13091 bfd_put_32 (output_bfd, insn2, contents + offset);
13093 if ((tls_mask & tls_gd) == 0
13094 && (tls_gd == 0 || toc_symndx != 0))
13096 /* We changed the symbol. Start over in order
13097 to get h, sym, sec etc. right. */
13104 case R_PPC64_TLSGD:
13105 if (tls_mask != 0 && (tls_mask & TLS_GD) == 0)
13107 unsigned int insn2, insn3;
13108 bfd_vma offset = rel->r_offset;
13110 if ((tls_mask & TLS_TPRELGD) != 0)
13113 r_type = R_PPC64_NONE;
13114 insn2 = 0x7c636a14; /* add 3,3,13 */
13119 if (toc_symndx != 0)
13121 r_symndx = toc_symndx;
13122 rel->r_addend = toc_addend;
13124 r_type = R_PPC64_TPREL16_LO;
13125 rel->r_offset = offset + d_offset;
13126 insn2 = 0x38630000; /* addi 3,3,0 */
13128 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13129 /* Zap the reloc on the _tls_get_addr call too. */
13130 BFD_ASSERT (offset == rel[1].r_offset);
13131 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13132 insn3 = bfd_get_32 (output_bfd,
13133 contents + offset + 4);
13135 || insn3 == CROR_151515 || insn3 == CROR_313131)
13137 rel->r_offset += 4;
13138 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13141 bfd_put_32 (output_bfd, insn2, contents + offset);
13142 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
13150 case R_PPC64_TLSLD:
13151 if (tls_mask != 0 && (tls_mask & TLS_LD) == 0)
13153 unsigned int insn2, insn3;
13154 bfd_vma offset = rel->r_offset;
13157 sec = local_sections[toc_symndx];
13159 r_symndx < symtab_hdr->sh_info;
13161 if (local_sections[r_symndx] == sec)
13163 if (r_symndx >= symtab_hdr->sh_info)
13164 r_symndx = STN_UNDEF;
13165 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
13166 if (r_symndx != STN_UNDEF)
13167 rel->r_addend -= (local_syms[r_symndx].st_value
13168 + sec->output_offset
13169 + sec->output_section->vma);
13171 r_type = R_PPC64_TPREL16_LO;
13172 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13173 rel->r_offset = offset + d_offset;
13174 /* Zap the reloc on the _tls_get_addr call too. */
13175 BFD_ASSERT (offset == rel[1].r_offset);
13176 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
13177 insn2 = 0x38630000; /* addi 3,3,0 */
13178 insn3 = bfd_get_32 (output_bfd,
13179 contents + offset + 4);
13181 || insn3 == CROR_151515 || insn3 == CROR_313131)
13183 rel->r_offset += 4;
13184 bfd_put_32 (output_bfd, insn2, contents + offset + 4);
13187 bfd_put_32 (output_bfd, insn2, contents + offset);
13193 case R_PPC64_DTPMOD64:
13194 if (rel + 1 < relend
13195 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
13196 && rel[1].r_offset == rel->r_offset + 8)
13198 if ((tls_mask & TLS_GD) == 0)
13200 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
13201 if ((tls_mask & TLS_TPRELGD) != 0)
13202 r_type = R_PPC64_TPREL64;
13205 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13206 r_type = R_PPC64_NONE;
13208 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13213 if ((tls_mask & TLS_LD) == 0)
13215 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
13216 r_type = R_PPC64_NONE;
13217 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13222 case R_PPC64_TPREL64:
13223 if ((tls_mask & TLS_TPREL) == 0)
13225 r_type = R_PPC64_NONE;
13226 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
13231 /* Handle other relocations that tweak non-addend part of insn. */
13233 max_br_offset = 1 << 25;
13234 addend = rel->r_addend;
13235 reloc_dest = DEST_NORMAL;
13241 case R_PPC64_TOCSAVE:
13242 if (relocation + addend == (rel->r_offset
13243 + input_section->output_offset
13244 + input_section->output_section->vma)
13245 && tocsave_find (htab, NO_INSERT,
13246 &local_syms, rel, input_bfd))
13248 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
13250 || insn == CROR_151515 || insn == CROR_313131)
13251 bfd_put_32 (input_bfd, STD_R2_40R1,
13252 contents + rel->r_offset);
13256 /* Branch taken prediction relocations. */
13257 case R_PPC64_ADDR14_BRTAKEN:
13258 case R_PPC64_REL14_BRTAKEN:
13259 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
13262 /* Branch not taken prediction relocations. */
13263 case R_PPC64_ADDR14_BRNTAKEN:
13264 case R_PPC64_REL14_BRNTAKEN:
13265 insn |= bfd_get_32 (output_bfd,
13266 contents + rel->r_offset) & ~(0x01 << 21);
13269 case R_PPC64_REL14:
13270 max_br_offset = 1 << 15;
13273 case R_PPC64_REL24:
13274 /* Calls to functions with a different TOC, such as calls to
13275 shared objects, need to alter the TOC pointer. This is
13276 done using a linkage stub. A REL24 branching to these
13277 linkage stubs needs to be followed by a nop, as the nop
13278 will be replaced with an instruction to restore the TOC
13283 && h->oh->is_func_descriptor)
13284 fdh = ppc_follow_link (h->oh);
13285 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
13287 if (stub_entry != NULL
13288 && (stub_entry->stub_type == ppc_stub_plt_call
13289 || stub_entry->stub_type == ppc_stub_plt_call_r2save
13290 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
13291 || stub_entry->stub_type == ppc_stub_long_branch_r2off))
13293 bfd_boolean can_plt_call = FALSE;
13295 /* All of these stubs will modify r2, so there must be a
13296 branch and link followed by a nop. The nop is
13297 replaced by an insn to restore r2. */
13298 if (rel->r_offset + 8 <= input_section->size)
13302 br = bfd_get_32 (input_bfd,
13303 contents + rel->r_offset);
13308 nop = bfd_get_32 (input_bfd,
13309 contents + rel->r_offset + 4);
13311 || nop == CROR_151515 || nop == CROR_313131)
13314 && (h == htab->tls_get_addr_fd
13315 || h == htab->tls_get_addr)
13316 && !htab->no_tls_get_addr_opt)
13318 /* Special stub used, leave nop alone. */
13321 bfd_put_32 (input_bfd, LD_R2_40R1,
13322 contents + rel->r_offset + 4);
13323 can_plt_call = TRUE;
13328 if (!can_plt_call && h != NULL)
13330 const char *name = h->elf.root.root.string;
13335 if (strncmp (name, "__libc_start_main", 17) == 0
13336 && (name[17] == 0 || name[17] == '@'))
13338 /* Allow crt1 branch to go via a toc adjusting
13339 stub. Other calls that never return could do
13340 the same, if we could detect such. */
13341 can_plt_call = TRUE;
13347 /* g++ as of 20130507 emits self-calls without a
13348 following nop. This is arguably wrong since we
13349 have conflicting information. On the one hand a
13350 global symbol and on the other a local call
13351 sequence, but don't error for this special case.
13352 It isn't possible to cheaply verify we have
13353 exactly such a call. Allow all calls to the same
13355 asection *code_sec = sec;
13357 if (get_opd_info (sec) != NULL)
13359 bfd_vma off = (relocation + addend
13360 - sec->output_section->vma
13361 - sec->output_offset);
13363 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
13365 if (code_sec == input_section)
13366 can_plt_call = TRUE;
13371 info->callbacks->einfo
13372 (_("%P: %H: call to `%T' lacks nop, can't restore toc; "
13373 "recompile with -fPIC"),
13374 input_bfd, input_section, rel->r_offset, sym_name);
13376 bfd_set_error (bfd_error_bad_value);
13381 && (stub_entry->stub_type == ppc_stub_plt_call
13382 || stub_entry->stub_type == ppc_stub_plt_call_r2save))
13383 unresolved_reloc = FALSE;
13386 if ((stub_entry == NULL
13387 || stub_entry->stub_type == ppc_stub_long_branch
13388 || stub_entry->stub_type == ppc_stub_plt_branch)
13389 && get_opd_info (sec) != NULL)
13391 /* The branch destination is the value of the opd entry. */
13392 bfd_vma off = (relocation + addend
13393 - sec->output_section->vma
13394 - sec->output_offset);
13395 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
13396 if (dest != (bfd_vma) -1)
13400 reloc_dest = DEST_OPD;
13404 /* If the branch is out of reach we ought to have a long
13406 from = (rel->r_offset
13407 + input_section->output_offset
13408 + input_section->output_section->vma);
13410 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
13414 if (stub_entry != NULL
13415 && (stub_entry->stub_type == ppc_stub_long_branch
13416 || stub_entry->stub_type == ppc_stub_plt_branch)
13417 && (r_type == R_PPC64_ADDR14_BRTAKEN
13418 || r_type == R_PPC64_ADDR14_BRNTAKEN
13419 || (relocation + addend - from + max_br_offset
13420 < 2 * max_br_offset)))
13421 /* Don't use the stub if this branch is in range. */
13424 if (stub_entry != NULL)
13426 /* Munge up the value and addend so that we call the stub
13427 rather than the procedure directly. */
13428 relocation = (stub_entry->stub_offset
13429 + stub_entry->stub_sec->output_offset
13430 + stub_entry->stub_sec->output_section->vma);
13432 reloc_dest = DEST_STUB;
13434 if ((stub_entry->stub_type == ppc_stub_plt_call
13435 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13436 && (ALWAYS_EMIT_R2SAVE
13437 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
13438 && rel + 1 < relend
13439 && rel[1].r_offset == rel->r_offset + 4
13440 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
13448 /* Set 'a' bit. This is 0b00010 in BO field for branch
13449 on CR(BI) insns (BO == 001at or 011at), and 0b01000
13450 for branch on CTR insns (BO == 1a00t or 1a01t). */
13451 if ((insn & (0x14 << 21)) == (0x04 << 21))
13452 insn |= 0x02 << 21;
13453 else if ((insn & (0x14 << 21)) == (0x10 << 21))
13454 insn |= 0x08 << 21;
13460 /* Invert 'y' bit if not the default. */
13461 if ((bfd_signed_vma) (relocation + addend - from) < 0)
13462 insn ^= 0x01 << 21;
13465 bfd_put_32 (output_bfd, insn, contents + rel->r_offset);
13468 /* NOP out calls to undefined weak functions.
13469 We can thus call a weak function without first
13470 checking whether the function is defined. */
13472 && h->elf.root.type == bfd_link_hash_undefweak
13473 && h->elf.dynindx == -1
13474 && r_type == R_PPC64_REL24
13478 bfd_put_32 (output_bfd, NOP, contents + rel->r_offset);
13484 /* Set `addend'. */
13489 info->callbacks->einfo
13490 (_("%P: %B: unknown relocation type %d for `%T'\n"),
13491 input_bfd, (int) r_type, sym_name);
13493 bfd_set_error (bfd_error_bad_value);
13499 case R_PPC64_TLSGD:
13500 case R_PPC64_TLSLD:
13501 case R_PPC64_TOCSAVE:
13502 case R_PPC64_GNU_VTINHERIT:
13503 case R_PPC64_GNU_VTENTRY:
13506 /* GOT16 relocations. Like an ADDR16 using the symbol's
13507 address in the GOT as relocation value instead of the
13508 symbol's value itself. Also, create a GOT entry for the
13509 symbol and put the symbol value there. */
13510 case R_PPC64_GOT_TLSGD16:
13511 case R_PPC64_GOT_TLSGD16_LO:
13512 case R_PPC64_GOT_TLSGD16_HI:
13513 case R_PPC64_GOT_TLSGD16_HA:
13514 tls_type = TLS_TLS | TLS_GD;
13517 case R_PPC64_GOT_TLSLD16:
13518 case R_PPC64_GOT_TLSLD16_LO:
13519 case R_PPC64_GOT_TLSLD16_HI:
13520 case R_PPC64_GOT_TLSLD16_HA:
13521 tls_type = TLS_TLS | TLS_LD;
13524 case R_PPC64_GOT_TPREL16_DS:
13525 case R_PPC64_GOT_TPREL16_LO_DS:
13526 case R_PPC64_GOT_TPREL16_HI:
13527 case R_PPC64_GOT_TPREL16_HA:
13528 tls_type = TLS_TLS | TLS_TPREL;
13531 case R_PPC64_GOT_DTPREL16_DS:
13532 case R_PPC64_GOT_DTPREL16_LO_DS:
13533 case R_PPC64_GOT_DTPREL16_HI:
13534 case R_PPC64_GOT_DTPREL16_HA:
13535 tls_type = TLS_TLS | TLS_DTPREL;
13538 case R_PPC64_GOT16:
13539 case R_PPC64_GOT16_LO:
13540 case R_PPC64_GOT16_HI:
13541 case R_PPC64_GOT16_HA:
13542 case R_PPC64_GOT16_DS:
13543 case R_PPC64_GOT16_LO_DS:
13546 /* Relocation is to the entry for this symbol in the global
13551 unsigned long indx = 0;
13552 struct got_entry *ent;
13554 if (tls_type == (TLS_TLS | TLS_LD)
13556 || !h->elf.def_dynamic))
13557 ent = ppc64_tlsld_got (input_bfd);
13563 bfd_boolean dyn = htab->elf.dynamic_sections_created;
13564 if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared,
13567 && SYMBOL_CALLS_LOCAL (info, &h->elf)))
13568 /* This is actually a static link, or it is a
13569 -Bsymbolic link and the symbol is defined
13570 locally, or the symbol was forced to be local
13571 because of a version file. */
13575 BFD_ASSERT (h->elf.dynindx != -1);
13576 indx = h->elf.dynindx;
13577 unresolved_reloc = FALSE;
13579 ent = h->elf.got.glist;
13583 if (local_got_ents == NULL)
13585 ent = local_got_ents[r_symndx];
13588 for (; ent != NULL; ent = ent->next)
13589 if (ent->addend == orig_rel.r_addend
13590 && ent->owner == input_bfd
13591 && ent->tls_type == tls_type)
13597 if (ent->is_indirect)
13598 ent = ent->got.ent;
13599 offp = &ent->got.offset;
13600 got = ppc64_elf_tdata (ent->owner)->got;
13604 /* The offset must always be a multiple of 8. We use the
13605 least significant bit to record whether we have already
13606 processed this entry. */
13608 if ((off & 1) != 0)
13612 /* Generate relocs for the dynamic linker, except in
13613 the case of TLSLD where we'll use one entry per
13621 ? h->elf.type == STT_GNU_IFUNC
13622 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
13624 relgot = htab->reliplt;
13625 else if ((info->shared || indx != 0)
13627 || (tls_type == (TLS_TLS | TLS_LD)
13628 && !h->elf.def_dynamic)
13629 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13630 || h->elf.root.type != bfd_link_hash_undefweak))
13631 relgot = ppc64_elf_tdata (ent->owner)->relgot;
13632 if (relgot != NULL)
13634 outrel.r_offset = (got->output_section->vma
13635 + got->output_offset
13637 outrel.r_addend = addend;
13638 if (tls_type & (TLS_LD | TLS_GD))
13640 outrel.r_addend = 0;
13641 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
13642 if (tls_type == (TLS_TLS | TLS_GD))
13644 loc = relgot->contents;
13645 loc += (relgot->reloc_count++
13646 * sizeof (Elf64_External_Rela));
13647 bfd_elf64_swap_reloca_out (output_bfd,
13649 outrel.r_offset += 8;
13650 outrel.r_addend = addend;
13652 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13655 else if (tls_type == (TLS_TLS | TLS_DTPREL))
13656 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
13657 else if (tls_type == (TLS_TLS | TLS_TPREL))
13658 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
13659 else if (indx != 0)
13660 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
13664 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13666 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13668 /* Write the .got section contents for the sake
13670 loc = got->contents + off;
13671 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
13675 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
13677 outrel.r_addend += relocation;
13678 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
13679 outrel.r_addend -= htab->elf.tls_sec->vma;
13681 loc = relgot->contents;
13682 loc += (relgot->reloc_count++
13683 * sizeof (Elf64_External_Rela));
13684 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
13687 /* Init the .got section contents here if we're not
13688 emitting a reloc. */
13691 relocation += addend;
13692 if (tls_type == (TLS_TLS | TLS_LD))
13694 else if (tls_type != 0)
13696 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
13697 if (tls_type == (TLS_TLS | TLS_TPREL))
13698 relocation += DTP_OFFSET - TP_OFFSET;
13700 if (tls_type == (TLS_TLS | TLS_GD))
13702 bfd_put_64 (output_bfd, relocation,
13703 got->contents + off + 8);
13708 bfd_put_64 (output_bfd, relocation,
13709 got->contents + off);
13713 if (off >= (bfd_vma) -2)
13716 relocation = got->output_section->vma + got->output_offset + off;
13717 addend = -(TOCstart + htab->stub_group[input_section->id].toc_off);
13721 case R_PPC64_PLT16_HA:
13722 case R_PPC64_PLT16_HI:
13723 case R_PPC64_PLT16_LO:
13724 case R_PPC64_PLT32:
13725 case R_PPC64_PLT64:
13726 /* Relocation is to the entry for this symbol in the
13727 procedure linkage table. */
13729 /* Resolve a PLT reloc against a local symbol directly,
13730 without using the procedure linkage table. */
13734 /* It's possible that we didn't make a PLT entry for this
13735 symbol. This happens when statically linking PIC code,
13736 or when using -Bsymbolic. Go find a match if there is a
13738 if (htab->plt != NULL)
13740 struct plt_entry *ent;
13741 for (ent = h->elf.plt.plist; ent != NULL; ent = ent->next)
13742 if (ent->addend == orig_rel.r_addend
13743 && ent->plt.offset != (bfd_vma) -1)
13745 relocation = (htab->plt->output_section->vma
13746 + htab->plt->output_offset
13747 + ent->plt.offset);
13748 unresolved_reloc = FALSE;
13754 /* Relocation value is TOC base. */
13755 relocation = TOCstart;
13756 if (r_symndx == STN_UNDEF)
13757 relocation += htab->stub_group[input_section->id].toc_off;
13758 else if (unresolved_reloc)
13760 else if (sec != NULL && sec->id <= htab->top_id)
13761 relocation += htab->stub_group[sec->id].toc_off;
13763 unresolved_reloc = TRUE;
13766 /* TOC16 relocs. We want the offset relative to the TOC base,
13767 which is the address of the start of the TOC plus 0x8000.
13768 The TOC consists of sections .got, .toc, .tocbss, and .plt,
13770 case R_PPC64_TOC16:
13771 case R_PPC64_TOC16_LO:
13772 case R_PPC64_TOC16_HI:
13773 case R_PPC64_TOC16_DS:
13774 case R_PPC64_TOC16_LO_DS:
13775 case R_PPC64_TOC16_HA:
13776 addend -= TOCstart + htab->stub_group[input_section->id].toc_off;
13779 /* Relocate against the beginning of the section. */
13780 case R_PPC64_SECTOFF:
13781 case R_PPC64_SECTOFF_LO:
13782 case R_PPC64_SECTOFF_HI:
13783 case R_PPC64_SECTOFF_DS:
13784 case R_PPC64_SECTOFF_LO_DS:
13785 case R_PPC64_SECTOFF_HA:
13787 addend -= sec->output_section->vma;
13790 case R_PPC64_REL16:
13791 case R_PPC64_REL16_LO:
13792 case R_PPC64_REL16_HI:
13793 case R_PPC64_REL16_HA:
13796 case R_PPC64_REL14:
13797 case R_PPC64_REL14_BRNTAKEN:
13798 case R_PPC64_REL14_BRTAKEN:
13799 case R_PPC64_REL24:
13802 case R_PPC64_TPREL16:
13803 case R_PPC64_TPREL16_LO:
13804 case R_PPC64_TPREL16_HI:
13805 case R_PPC64_TPREL16_HA:
13806 case R_PPC64_TPREL16_DS:
13807 case R_PPC64_TPREL16_LO_DS:
13808 case R_PPC64_TPREL16_HIGH:
13809 case R_PPC64_TPREL16_HIGHA:
13810 case R_PPC64_TPREL16_HIGHER:
13811 case R_PPC64_TPREL16_HIGHERA:
13812 case R_PPC64_TPREL16_HIGHEST:
13813 case R_PPC64_TPREL16_HIGHESTA:
13815 && h->elf.root.type == bfd_link_hash_undefweak
13816 && h->elf.dynindx == -1)
13818 /* Make this relocation against an undefined weak symbol
13819 resolve to zero. This is really just a tweak, since
13820 code using weak externs ought to check that they are
13821 defined before using them. */
13822 bfd_byte *p = contents + rel->r_offset - d_offset;
13824 insn = bfd_get_32 (output_bfd, p);
13825 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
13827 bfd_put_32 (output_bfd, insn, p);
13830 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13832 /* The TPREL16 relocs shouldn't really be used in shared
13833 libs as they will result in DT_TEXTREL being set, but
13834 support them anyway. */
13838 case R_PPC64_DTPREL16:
13839 case R_PPC64_DTPREL16_LO:
13840 case R_PPC64_DTPREL16_HI:
13841 case R_PPC64_DTPREL16_HA:
13842 case R_PPC64_DTPREL16_DS:
13843 case R_PPC64_DTPREL16_LO_DS:
13844 case R_PPC64_DTPREL16_HIGH:
13845 case R_PPC64_DTPREL16_HIGHA:
13846 case R_PPC64_DTPREL16_HIGHER:
13847 case R_PPC64_DTPREL16_HIGHERA:
13848 case R_PPC64_DTPREL16_HIGHEST:
13849 case R_PPC64_DTPREL16_HIGHESTA:
13850 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13853 case R_PPC64_DTPMOD64:
13858 case R_PPC64_TPREL64:
13859 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
13862 case R_PPC64_DTPREL64:
13863 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
13866 /* Relocations that may need to be propagated if this is a
13868 case R_PPC64_REL30:
13869 case R_PPC64_REL32:
13870 case R_PPC64_REL64:
13871 case R_PPC64_ADDR14:
13872 case R_PPC64_ADDR14_BRNTAKEN:
13873 case R_PPC64_ADDR14_BRTAKEN:
13874 case R_PPC64_ADDR16:
13875 case R_PPC64_ADDR16_DS:
13876 case R_PPC64_ADDR16_HA:
13877 case R_PPC64_ADDR16_HI:
13878 case R_PPC64_ADDR16_HIGH:
13879 case R_PPC64_ADDR16_HIGHA:
13880 case R_PPC64_ADDR16_HIGHER:
13881 case R_PPC64_ADDR16_HIGHERA:
13882 case R_PPC64_ADDR16_HIGHEST:
13883 case R_PPC64_ADDR16_HIGHESTA:
13884 case R_PPC64_ADDR16_LO:
13885 case R_PPC64_ADDR16_LO_DS:
13886 case R_PPC64_ADDR24:
13887 case R_PPC64_ADDR32:
13888 case R_PPC64_ADDR64:
13889 case R_PPC64_UADDR16:
13890 case R_PPC64_UADDR32:
13891 case R_PPC64_UADDR64:
13893 if ((input_section->flags & SEC_ALLOC) == 0)
13896 if (NO_OPD_RELOCS && is_opd)
13901 || ELF_ST_VISIBILITY (h->elf.other) == STV_DEFAULT
13902 || h->elf.root.type != bfd_link_hash_undefweak)
13903 && (must_be_dyn_reloc (info, r_type)
13904 || !SYMBOL_CALLS_LOCAL (info, &h->elf)))
13905 || (ELIMINATE_COPY_RELOCS
13908 && h->elf.dynindx != -1
13909 && !h->elf.non_got_ref
13910 && !h->elf.def_regular)
13913 ? h->elf.type == STT_GNU_IFUNC
13914 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
13916 bfd_boolean skip, relocate;
13920 /* When generating a dynamic object, these relocations
13921 are copied into the output file to be resolved at run
13927 out_off = _bfd_elf_section_offset (output_bfd, info,
13928 input_section, rel->r_offset);
13929 if (out_off == (bfd_vma) -1)
13931 else if (out_off == (bfd_vma) -2)
13932 skip = TRUE, relocate = TRUE;
13933 out_off += (input_section->output_section->vma
13934 + input_section->output_offset);
13935 outrel.r_offset = out_off;
13936 outrel.r_addend = rel->r_addend;
13938 /* Optimize unaligned reloc use. */
13939 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
13940 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
13941 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
13942 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
13943 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
13944 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
13945 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
13946 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
13947 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
13950 memset (&outrel, 0, sizeof outrel);
13951 else if (!SYMBOL_CALLS_LOCAL (info, &h->elf)
13953 && r_type != R_PPC64_TOC)
13955 BFD_ASSERT (h->elf.dynindx != -1);
13956 outrel.r_info = ELF64_R_INFO (h->elf.dynindx, r_type);
13960 /* This symbol is local, or marked to become local,
13961 or this is an opd section reloc which must point
13962 at a local function. */
13963 outrel.r_addend += relocation;
13964 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13966 if (is_opd && h != NULL)
13968 /* Lie about opd entries. This case occurs
13969 when building shared libraries and we
13970 reference a function in another shared
13971 lib. The same thing happens for a weak
13972 definition in an application that's
13973 overridden by a strong definition in a
13974 shared lib. (I believe this is a generic
13975 bug in binutils handling of weak syms.)
13976 In these cases we won't use the opd
13977 entry in this lib. */
13978 unresolved_reloc = FALSE;
13981 && r_type == R_PPC64_ADDR64
13983 ? h->elf.type == STT_GNU_IFUNC
13984 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
13985 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13988 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13990 /* We need to relocate .opd contents for ld.so.
13991 Prelink also wants simple and consistent rules
13992 for relocs. This make all RELATIVE relocs have
13993 *r_offset equal to r_addend. */
14002 ? h->elf.type == STT_GNU_IFUNC
14003 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14005 info->callbacks->einfo
14006 (_("%P: %H: %s for indirect "
14007 "function `%T' unsupported\n"),
14008 input_bfd, input_section, rel->r_offset,
14009 ppc64_elf_howto_table[r_type]->name,
14013 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
14015 else if (sec == NULL || sec->owner == NULL)
14017 bfd_set_error (bfd_error_bad_value);
14024 osec = sec->output_section;
14025 indx = elf_section_data (osec)->dynindx;
14029 if ((osec->flags & SEC_READONLY) == 0
14030 && htab->elf.data_index_section != NULL)
14031 osec = htab->elf.data_index_section;
14033 osec = htab->elf.text_index_section;
14034 indx = elf_section_data (osec)->dynindx;
14036 BFD_ASSERT (indx != 0);
14038 /* We are turning this relocation into one
14039 against a section symbol, so subtract out
14040 the output section's address but not the
14041 offset of the input section in the output
14043 outrel.r_addend -= osec->vma;
14046 outrel.r_info = ELF64_R_INFO (indx, r_type);
14050 sreloc = elf_section_data (input_section)->sreloc;
14052 ? h->elf.type == STT_GNU_IFUNC
14053 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14054 sreloc = htab->reliplt;
14055 if (sreloc == NULL)
14058 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
14061 loc = sreloc->contents;
14062 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
14063 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
14065 /* If this reloc is against an external symbol, it will
14066 be computed at runtime, so there's no need to do
14067 anything now. However, for the sake of prelink ensure
14068 that the section contents are a known value. */
14071 unresolved_reloc = FALSE;
14072 /* The value chosen here is quite arbitrary as ld.so
14073 ignores section contents except for the special
14074 case of .opd where the contents might be accessed
14075 before relocation. Choose zero, as that won't
14076 cause reloc overflow. */
14079 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
14080 to improve backward compatibility with older
14082 if (r_type == R_PPC64_ADDR64)
14083 addend = outrel.r_addend;
14084 /* Adjust pc_relative relocs to have zero in *r_offset. */
14085 else if (ppc64_elf_howto_table[r_type]->pc_relative)
14086 addend = (input_section->output_section->vma
14087 + input_section->output_offset
14094 case R_PPC64_GLOB_DAT:
14095 case R_PPC64_JMP_SLOT:
14096 case R_PPC64_JMP_IREL:
14097 case R_PPC64_RELATIVE:
14098 /* We shouldn't ever see these dynamic relocs in relocatable
14100 /* Fall through. */
14102 case R_PPC64_PLTGOT16:
14103 case R_PPC64_PLTGOT16_DS:
14104 case R_PPC64_PLTGOT16_HA:
14105 case R_PPC64_PLTGOT16_HI:
14106 case R_PPC64_PLTGOT16_LO:
14107 case R_PPC64_PLTGOT16_LO_DS:
14108 case R_PPC64_PLTREL32:
14109 case R_PPC64_PLTREL64:
14110 /* These ones haven't been implemented yet. */
14112 info->callbacks->einfo
14113 (_("%P: %B: %s is not supported for `%T'\n"),
14115 ppc64_elf_howto_table[r_type]->name, sym_name);
14117 bfd_set_error (bfd_error_invalid_operation);
14122 /* Multi-instruction sequences that access the TOC can be
14123 optimized, eg. addis ra,r2,0; addi rb,ra,x;
14124 to nop; addi rb,r2,x; */
14130 case R_PPC64_GOT_TLSLD16_HI:
14131 case R_PPC64_GOT_TLSGD16_HI:
14132 case R_PPC64_GOT_TPREL16_HI:
14133 case R_PPC64_GOT_DTPREL16_HI:
14134 case R_PPC64_GOT16_HI:
14135 case R_PPC64_TOC16_HI:
14136 /* These relocs would only be useful if building up an
14137 offset to later add to r2, perhaps in an indexed
14138 addressing mode instruction. Don't try to optimize.
14139 Unfortunately, the possibility of someone building up an
14140 offset like this or even with the HA relocs, means that
14141 we need to check the high insn when optimizing the low
14145 case R_PPC64_GOT_TLSLD16_HA:
14146 case R_PPC64_GOT_TLSGD16_HA:
14147 case R_PPC64_GOT_TPREL16_HA:
14148 case R_PPC64_GOT_DTPREL16_HA:
14149 case R_PPC64_GOT16_HA:
14150 case R_PPC64_TOC16_HA:
14151 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14152 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14154 bfd_byte *p = contents + (rel->r_offset & ~3);
14155 bfd_put_32 (input_bfd, NOP, p);
14159 case R_PPC64_GOT_TLSLD16_LO:
14160 case R_PPC64_GOT_TLSGD16_LO:
14161 case R_PPC64_GOT_TPREL16_LO_DS:
14162 case R_PPC64_GOT_DTPREL16_LO_DS:
14163 case R_PPC64_GOT16_LO:
14164 case R_PPC64_GOT16_LO_DS:
14165 case R_PPC64_TOC16_LO:
14166 case R_PPC64_TOC16_LO_DS:
14167 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
14168 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
14170 bfd_byte *p = contents + (rel->r_offset & ~3);
14171 insn = bfd_get_32 (input_bfd, p);
14172 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
14174 /* Transform addic to addi when we change reg. */
14175 insn &= ~((0x3f << 26) | (0x1f << 16));
14176 insn |= (14u << 26) | (2 << 16);
14180 insn &= ~(0x1f << 16);
14183 bfd_put_32 (input_bfd, insn, p);
14188 /* Do any further special processing. */
14194 case R_PPC64_REL16_HA:
14195 case R_PPC64_ADDR16_HA:
14196 case R_PPC64_ADDR16_HIGHA:
14197 case R_PPC64_ADDR16_HIGHERA:
14198 case R_PPC64_ADDR16_HIGHESTA:
14199 case R_PPC64_TOC16_HA:
14200 case R_PPC64_SECTOFF_HA:
14201 case R_PPC64_TPREL16_HA:
14202 case R_PPC64_TPREL16_HIGHA:
14203 case R_PPC64_TPREL16_HIGHERA:
14204 case R_PPC64_TPREL16_HIGHESTA:
14205 case R_PPC64_DTPREL16_HA:
14206 case R_PPC64_DTPREL16_HIGHA:
14207 case R_PPC64_DTPREL16_HIGHERA:
14208 case R_PPC64_DTPREL16_HIGHESTA:
14209 /* It's just possible that this symbol is a weak symbol
14210 that's not actually defined anywhere. In that case,
14211 'sec' would be NULL, and we should leave the symbol
14212 alone (it will be set to zero elsewhere in the link). */
14217 case R_PPC64_GOT16_HA:
14218 case R_PPC64_PLTGOT16_HA:
14219 case R_PPC64_PLT16_HA:
14220 case R_PPC64_GOT_TLSGD16_HA:
14221 case R_PPC64_GOT_TLSLD16_HA:
14222 case R_PPC64_GOT_TPREL16_HA:
14223 case R_PPC64_GOT_DTPREL16_HA:
14224 /* Add 0x10000 if sign bit in 0:15 is set.
14225 Bits 0:15 are not used. */
14229 case R_PPC64_ADDR16_DS:
14230 case R_PPC64_ADDR16_LO_DS:
14231 case R_PPC64_GOT16_DS:
14232 case R_PPC64_GOT16_LO_DS:
14233 case R_PPC64_PLT16_LO_DS:
14234 case R_PPC64_SECTOFF_DS:
14235 case R_PPC64_SECTOFF_LO_DS:
14236 case R_PPC64_TOC16_DS:
14237 case R_PPC64_TOC16_LO_DS:
14238 case R_PPC64_PLTGOT16_DS:
14239 case R_PPC64_PLTGOT16_LO_DS:
14240 case R_PPC64_GOT_TPREL16_DS:
14241 case R_PPC64_GOT_TPREL16_LO_DS:
14242 case R_PPC64_GOT_DTPREL16_DS:
14243 case R_PPC64_GOT_DTPREL16_LO_DS:
14244 case R_PPC64_TPREL16_DS:
14245 case R_PPC64_TPREL16_LO_DS:
14246 case R_PPC64_DTPREL16_DS:
14247 case R_PPC64_DTPREL16_LO_DS:
14248 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
14250 /* If this reloc is against an lq insn, then the value must be
14251 a multiple of 16. This is somewhat of a hack, but the
14252 "correct" way to do this by defining _DQ forms of all the
14253 _DS relocs bloats all reloc switches in this file. It
14254 doesn't seem to make much sense to use any of these relocs
14255 in data, so testing the insn should be safe. */
14256 if ((insn & (0x3f << 26)) == (56u << 26))
14258 if (((relocation + addend) & mask) != 0)
14260 info->callbacks->einfo
14261 (_("%P: %H: error: %s not a multiple of %u\n"),
14262 input_bfd, input_section, rel->r_offset,
14263 ppc64_elf_howto_table[r_type]->name,
14265 bfd_set_error (bfd_error_bad_value);
14272 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
14273 because such sections are not SEC_ALLOC and thus ld.so will
14274 not process them. */
14275 if (unresolved_reloc
14276 && !((input_section->flags & SEC_DEBUGGING) != 0
14277 && h->elf.def_dynamic)
14278 && _bfd_elf_section_offset (output_bfd, info, input_section,
14279 rel->r_offset) != (bfd_vma) -1)
14281 info->callbacks->einfo
14282 (_("%P: %H: unresolvable %s against `%T'\n"),
14283 input_bfd, input_section, rel->r_offset,
14284 ppc64_elf_howto_table[(int) r_type]->name,
14285 h->elf.root.root.string);
14289 r = _bfd_final_link_relocate (ppc64_elf_howto_table[(int) r_type],
14297 if (r != bfd_reloc_ok)
14299 char *more_info = NULL;
14300 const char *reloc_name = ppc64_elf_howto_table[r_type]->name;
14302 if (reloc_dest != DEST_NORMAL)
14304 more_info = bfd_malloc (strlen (reloc_name) + 8);
14305 if (more_info != NULL)
14307 strcpy (more_info, reloc_name);
14308 strcat (more_info, (reloc_dest == DEST_OPD
14309 ? " (OPD)" : " (stub)"));
14310 reloc_name = more_info;
14314 if (r == bfd_reloc_overflow)
14319 && h->elf.root.type == bfd_link_hash_undefweak
14320 && ppc64_elf_howto_table[r_type]->pc_relative)
14322 /* Assume this is a call protected by other code that
14323 detects the symbol is undefined. If this is the case,
14324 we can safely ignore the overflow. If not, the
14325 program is hosed anyway, and a little warning isn't
14331 if (!((*info->callbacks->reloc_overflow)
14332 (info, &h->elf.root, sym_name,
14333 reloc_name, orig_rel.r_addend,
14334 input_bfd, input_section, rel->r_offset)))
14339 info->callbacks->einfo
14340 (_("%P: %H: %s against `%T': error %d\n"),
14341 input_bfd, input_section, rel->r_offset,
14342 reloc_name, sym_name, (int) r);
14345 if (more_info != NULL)
14350 /* If we're emitting relocations, then shortly after this function
14351 returns, reloc offsets and addends for this section will be
14352 adjusted. Worse, reloc symbol indices will be for the output
14353 file rather than the input. Save a copy of the relocs for
14354 opd_entry_value. */
14355 if (is_opd && (info->emitrelocations || info->relocatable))
14358 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
14359 rel = bfd_alloc (input_bfd, amt);
14360 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
14361 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
14364 memcpy (rel, relocs, amt);
14369 /* Adjust the value of any local symbols in opd sections. */
14372 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
14373 const char *name ATTRIBUTE_UNUSED,
14374 Elf_Internal_Sym *elfsym,
14375 asection *input_sec,
14376 struct elf_link_hash_entry *h)
14378 struct _opd_sec_data *opd;
14385 opd = get_opd_info (input_sec);
14386 if (opd == NULL || opd->adjust == NULL)
14389 value = elfsym->st_value - input_sec->output_offset;
14390 if (!info->relocatable)
14391 value -= input_sec->output_section->vma;
14393 adjust = opd->adjust[value / 8];
14397 elfsym->st_value += adjust;
14401 /* Finish up dynamic symbol handling. We set the contents of various
14402 dynamic sections here. */
14405 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
14406 struct bfd_link_info *info,
14407 struct elf_link_hash_entry *h,
14408 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
14410 struct ppc_link_hash_table *htab;
14411 struct plt_entry *ent;
14412 Elf_Internal_Rela rela;
14415 htab = ppc_hash_table (info);
14419 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14420 if (ent->plt.offset != (bfd_vma) -1)
14422 /* This symbol has an entry in the procedure linkage
14423 table. Set it up. */
14424 if (!htab->elf.dynamic_sections_created
14425 || h->dynindx == -1)
14427 BFD_ASSERT (h->type == STT_GNU_IFUNC
14429 && (h->root.type == bfd_link_hash_defined
14430 || h->root.type == bfd_link_hash_defweak));
14431 rela.r_offset = (htab->iplt->output_section->vma
14432 + htab->iplt->output_offset
14433 + ent->plt.offset);
14435 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14437 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14438 rela.r_addend = (h->root.u.def.value
14439 + h->root.u.def.section->output_offset
14440 + h->root.u.def.section->output_section->vma
14442 loc = (htab->reliplt->contents
14443 + (htab->reliplt->reloc_count++
14444 * sizeof (Elf64_External_Rela)));
14448 rela.r_offset = (htab->plt->output_section->vma
14449 + htab->plt->output_offset
14450 + ent->plt.offset);
14451 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14452 rela.r_addend = ent->addend;
14453 loc = (htab->relplt->contents
14454 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE)
14455 / (PLT_ENTRY_SIZE / sizeof (Elf64_External_Rela))));
14457 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14462 /* This symbol needs a copy reloc. Set it up. */
14464 if (h->dynindx == -1
14465 || (h->root.type != bfd_link_hash_defined
14466 && h->root.type != bfd_link_hash_defweak)
14467 || htab->relbss == NULL)
14470 rela.r_offset = (h->root.u.def.value
14471 + h->root.u.def.section->output_section->vma
14472 + h->root.u.def.section->output_offset);
14473 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
14475 loc = htab->relbss->contents;
14476 loc += htab->relbss->reloc_count++ * sizeof (Elf64_External_Rela);
14477 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
14483 /* Used to decide how to sort relocs in an optimal manner for the
14484 dynamic linker, before writing them out. */
14486 static enum elf_reloc_type_class
14487 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
14488 const asection *rel_sec,
14489 const Elf_Internal_Rela *rela)
14491 enum elf_ppc64_reloc_type r_type;
14492 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14494 if (rel_sec == htab->reliplt)
14495 return reloc_class_ifunc;
14497 r_type = ELF64_R_TYPE (rela->r_info);
14500 case R_PPC64_RELATIVE:
14501 return reloc_class_relative;
14502 case R_PPC64_JMP_SLOT:
14503 return reloc_class_plt;
14505 return reloc_class_copy;
14507 return reloc_class_normal;
14511 /* Finish up the dynamic sections. */
14514 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
14515 struct bfd_link_info *info)
14517 struct ppc_link_hash_table *htab;
14521 htab = ppc_hash_table (info);
14525 dynobj = htab->elf.dynobj;
14526 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
14528 if (htab->elf.dynamic_sections_created)
14530 Elf64_External_Dyn *dyncon, *dynconend;
14532 if (sdyn == NULL || htab->got == NULL)
14535 dyncon = (Elf64_External_Dyn *) sdyn->contents;
14536 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
14537 for (; dyncon < dynconend; dyncon++)
14539 Elf_Internal_Dyn dyn;
14542 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
14549 case DT_PPC64_GLINK:
14551 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14552 /* We stupidly defined DT_PPC64_GLINK to be the start
14553 of glink rather than the first entry point, which is
14554 what ld.so needs, and now have a bigger stub to
14555 support automatic multiple TOCs. */
14556 dyn.d_un.d_ptr += GLINK_CALL_STUB_SIZE - 32;
14560 s = bfd_get_section_by_name (output_bfd, ".opd");
14563 dyn.d_un.d_ptr = s->vma;
14566 case DT_PPC64_OPDSZ:
14567 s = bfd_get_section_by_name (output_bfd, ".opd");
14570 dyn.d_un.d_val = s->size;
14575 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14580 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
14584 dyn.d_un.d_val = htab->relplt->size;
14588 /* Don't count procedure linkage table relocs in the
14589 overall reloc count. */
14593 dyn.d_un.d_val -= s->size;
14597 /* We may not be using the standard ELF linker script.
14598 If .rela.plt is the first .rela section, we adjust
14599 DT_RELA to not include it. */
14603 if (dyn.d_un.d_ptr != s->output_section->vma + s->output_offset)
14605 dyn.d_un.d_ptr += s->size;
14609 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
14613 if (htab->got != NULL && htab->got->size != 0)
14615 /* Fill in the first entry in the global offset table.
14616 We use it to hold the link-time TOCbase. */
14617 bfd_put_64 (output_bfd,
14618 elf_gp (output_bfd) + TOC_BASE_OFF,
14619 htab->got->contents);
14621 /* Set .got entry size. */
14622 elf_section_data (htab->got->output_section)->this_hdr.sh_entsize = 8;
14625 if (htab->plt != NULL && htab->plt->size != 0)
14627 /* Set .plt entry size. */
14628 elf_section_data (htab->plt->output_section)->this_hdr.sh_entsize
14632 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
14633 brlt ourselves if emitrelocations. */
14634 if (htab->brlt != NULL
14635 && htab->brlt->reloc_count != 0
14636 && !_bfd_elf_link_output_relocs (output_bfd,
14638 elf_section_data (htab->brlt)->rela.hdr,
14639 elf_section_data (htab->brlt)->relocs,
14643 if (htab->glink != NULL
14644 && htab->glink->reloc_count != 0
14645 && !_bfd_elf_link_output_relocs (output_bfd,
14647 elf_section_data (htab->glink)->rela.hdr,
14648 elf_section_data (htab->glink)->relocs,
14653 if (htab->glink_eh_frame != NULL
14654 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
14655 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
14656 htab->glink_eh_frame,
14657 htab->glink_eh_frame->contents))
14660 /* We need to handle writing out multiple GOT sections ourselves,
14661 since we didn't add them to DYNOBJ. We know dynobj is the first
14663 while ((dynobj = dynobj->link_next) != NULL)
14667 if (!is_ppc64_elf (dynobj))
14670 s = ppc64_elf_tdata (dynobj)->got;
14673 && s->output_section != bfd_abs_section_ptr
14674 && !bfd_set_section_contents (output_bfd, s->output_section,
14675 s->contents, s->output_offset,
14678 s = ppc64_elf_tdata (dynobj)->relgot;
14681 && s->output_section != bfd_abs_section_ptr
14682 && !bfd_set_section_contents (output_bfd, s->output_section,
14683 s->contents, s->output_offset,
14691 #include "elf64-target.h"
14693 /* FreeBSD support */
14695 #undef TARGET_LITTLE_SYM
14696 #undef TARGET_LITTLE_NAME
14698 #undef TARGET_BIG_SYM
14699 #define TARGET_BIG_SYM bfd_elf64_powerpc_freebsd_vec
14700 #undef TARGET_BIG_NAME
14701 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
14704 #define ELF_OSABI ELFOSABI_FREEBSD
14707 #define elf64_bed elf64_powerpc_fbsd_bed
14709 #include "elf64-target.h"