1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2018 Free Software Foundation, Inc.
3 Written by Linus Nordberg, Swox AB <info@swox.com>,
4 based on elf32-ppc.c by Ian Lance Taylor.
5 Largely rewritten by Alan Modra.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
24 /* The 64-bit PowerPC ELF ABI may be found at
25 http://www.linuxbase.org/spec/ELF/ppc64/PPC-elf64abi.txt, and
26 http://www.linuxbase.org/spec/ELF/ppc64/spec/book1.html */
34 #include "elf/ppc64.h"
35 #include "elf64-ppc.h"
38 static bfd_reloc_status_type ppc64_elf_ha_reloc
39 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40 static bfd_reloc_status_type ppc64_elf_branch_reloc
41 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
42 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
43 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
44 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_toc_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_toc64_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_vma opd_entry_value
57 (asection *, bfd_vma, asection **, bfd_vma *, bfd_boolean);
59 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
60 #define TARGET_LITTLE_NAME "elf64-powerpcle"
61 #define TARGET_BIG_SYM powerpc_elf64_vec
62 #define TARGET_BIG_NAME "elf64-powerpc"
63 #define ELF_ARCH bfd_arch_powerpc
64 #define ELF_TARGET_ID PPC64_ELF_DATA
65 #define ELF_MACHINE_CODE EM_PPC64
66 #define ELF_MAXPAGESIZE 0x10000
67 #define ELF_COMMONPAGESIZE 0x1000
68 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
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_want_dynrelro 1
77 #define elf_backend_can_gc_sections 1
78 #define elf_backend_can_refcount 1
79 #define elf_backend_rela_normal 1
80 #define elf_backend_dtrel_excludes_plt 1
81 #define elf_backend_default_execstack 0
83 #define bfd_elf64_mkobject ppc64_elf_mkobject
84 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
85 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
86 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
87 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
88 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
89 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
90 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
91 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
92 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
94 #define elf_backend_object_p ppc64_elf_object_p
95 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
96 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
97 #define elf_backend_write_core_note ppc64_elf_write_core_note
98 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
99 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
100 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
101 #define elf_backend_check_directives ppc64_elf_before_check_relocs
102 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
103 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
104 #define elf_backend_check_relocs ppc64_elf_check_relocs
105 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
106 #define elf_backend_gc_keep ppc64_elf_gc_keep
107 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
108 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
109 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
110 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
111 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
112 #define elf_backend_always_size_sections ppc64_elf_func_desc_adjust
113 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
114 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
115 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
116 #define elf_backend_action_discarded ppc64_elf_action_discarded
117 #define elf_backend_relocate_section ppc64_elf_relocate_section
118 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
119 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
120 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
121 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
122 #define elf_backend_special_sections ppc64_elf_special_sections
123 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
124 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
125 #define elf_backend_get_reloc_section bfd_get_section_by_name
127 /* The name of the dynamic interpreter. This is put in the .interp
129 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
131 /* The size in bytes of an entry in the procedure linkage table. */
132 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
133 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
135 /* The initial size of the plt reserved for the dynamic linker. */
136 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
138 /* Offsets to some stack save slots. */
140 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
141 /* This one is dodgy. ELFv2 does not have a linker word, so use the
142 CR save slot. Used only by optimised __tls_get_addr call stub,
143 relying on __tls_get_addr_opt not saving CR.. */
144 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
146 /* TOC base pointers offset from start of TOC. */
147 #define TOC_BASE_OFF 0x8000
148 /* TOC base alignment. */
149 #define TOC_BASE_ALIGN 256
151 /* Offset of tp and dtp pointers from start of TLS block. */
152 #define TP_OFFSET 0x7000
153 #define DTP_OFFSET 0x8000
155 /* .plt call stub instructions. The normal stub is like this, but
156 sometimes the .plt entry crosses a 64k boundary and we need to
157 insert an addi to adjust r11. */
158 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
159 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
160 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
161 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
162 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
163 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
164 #define BCTR 0x4e800420 /* bctr */
166 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
167 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
168 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
169 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
170 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
172 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
173 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
174 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
175 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
176 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
177 #define BNECTR 0x4ca20420 /* bnectr+ */
178 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
180 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
181 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
182 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
184 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
185 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
186 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
188 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
189 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
190 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
191 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
192 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
193 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
194 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
195 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
196 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
197 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
198 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
199 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
201 /* __glink_PLTresolve stub instructions. We enter with the index in R0. */
202 #define GLINK_PLTRESOLVE_SIZE(htab) \
203 (8u + (htab->opd_abi ? 11 * 4 : 14 * 4))
207 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
208 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
210 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
211 /* ld %2,(0b-1b)(%11) */
212 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
213 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
219 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
220 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
221 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
222 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
223 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
226 #define NOP 0x60000000
228 /* Some other nops. */
229 #define CROR_151515 0x4def7b82
230 #define CROR_313131 0x4ffffb82
232 /* .glink entries for the first 32k functions are two instructions. */
233 #define LI_R0_0 0x38000000 /* li %r0,0 */
234 #define B_DOT 0x48000000 /* b . */
236 /* After that, we need two instructions to load the index, followed by
238 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
239 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
241 /* Instructions used by the save and restore reg functions. */
242 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
243 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
244 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
245 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
246 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
247 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
248 #define LI_R12_0 0x39800000 /* li %r12,0 */
249 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
250 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
251 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
252 #define BLR 0x4e800020 /* blr */
254 /* Since .opd is an array of descriptors and each entry will end up
255 with identical R_PPC64_RELATIVE relocs, there is really no need to
256 propagate .opd relocs; The dynamic linker should be taught to
257 relocate .opd without reloc entries. */
258 #ifndef NO_OPD_RELOCS
259 #define NO_OPD_RELOCS 0
263 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
267 abiversion (bfd *abfd)
269 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
273 set_abiversion (bfd *abfd, int ver)
275 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
276 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
279 #define ONES(n) (((bfd_vma) 1 << ((n) - 1) << 1) - 1)
281 /* Relocation HOWTO's. */
282 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
284 static reloc_howto_type ppc64_elf_howto_raw[] =
286 /* This reloc does nothing. */
287 HOWTO (R_PPC64_NONE, /* type */
289 3, /* size (0 = byte, 1 = short, 2 = long) */
291 FALSE, /* pc_relative */
293 complain_overflow_dont, /* complain_on_overflow */
294 bfd_elf_generic_reloc, /* special_function */
295 "R_PPC64_NONE", /* name */
296 FALSE, /* partial_inplace */
299 FALSE), /* pcrel_offset */
301 /* A standard 32 bit relocation. */
302 HOWTO (R_PPC64_ADDR32, /* type */
304 2, /* size (0 = byte, 1 = short, 2 = long) */
306 FALSE, /* pc_relative */
308 complain_overflow_bitfield, /* complain_on_overflow */
309 bfd_elf_generic_reloc, /* special_function */
310 "R_PPC64_ADDR32", /* name */
311 FALSE, /* partial_inplace */
313 0xffffffff, /* dst_mask */
314 FALSE), /* pcrel_offset */
316 /* An absolute 26 bit branch; the lower two bits must be zero.
317 FIXME: we don't check that, we just clear them. */
318 HOWTO (R_PPC64_ADDR24, /* type */
320 2, /* size (0 = byte, 1 = short, 2 = long) */
322 FALSE, /* pc_relative */
324 complain_overflow_bitfield, /* complain_on_overflow */
325 bfd_elf_generic_reloc, /* special_function */
326 "R_PPC64_ADDR24", /* name */
327 FALSE, /* partial_inplace */
329 0x03fffffc, /* dst_mask */
330 FALSE), /* pcrel_offset */
332 /* A standard 16 bit relocation. */
333 HOWTO (R_PPC64_ADDR16, /* type */
335 1, /* size (0 = byte, 1 = short, 2 = long) */
337 FALSE, /* pc_relative */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_PPC64_ADDR16", /* name */
342 FALSE, /* partial_inplace */
344 0xffff, /* dst_mask */
345 FALSE), /* pcrel_offset */
347 /* A 16 bit relocation without overflow. */
348 HOWTO (R_PPC64_ADDR16_LO, /* type */
350 1, /* size (0 = byte, 1 = short, 2 = long) */
352 FALSE, /* pc_relative */
354 complain_overflow_dont,/* complain_on_overflow */
355 bfd_elf_generic_reloc, /* special_function */
356 "R_PPC64_ADDR16_LO", /* name */
357 FALSE, /* partial_inplace */
359 0xffff, /* dst_mask */
360 FALSE), /* pcrel_offset */
362 /* Bits 16-31 of an address. */
363 HOWTO (R_PPC64_ADDR16_HI, /* type */
365 1, /* size (0 = byte, 1 = short, 2 = long) */
367 FALSE, /* pc_relative */
369 complain_overflow_signed, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_PPC64_ADDR16_HI", /* name */
372 FALSE, /* partial_inplace */
374 0xffff, /* dst_mask */
375 FALSE), /* pcrel_offset */
377 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
378 bits, treated as a signed number, is negative. */
379 HOWTO (R_PPC64_ADDR16_HA, /* type */
381 1, /* size (0 = byte, 1 = short, 2 = long) */
383 FALSE, /* pc_relative */
385 complain_overflow_signed, /* complain_on_overflow */
386 ppc64_elf_ha_reloc, /* special_function */
387 "R_PPC64_ADDR16_HA", /* name */
388 FALSE, /* partial_inplace */
390 0xffff, /* dst_mask */
391 FALSE), /* pcrel_offset */
393 /* An absolute 16 bit branch; the lower two bits must be zero.
394 FIXME: we don't check that, we just clear them. */
395 HOWTO (R_PPC64_ADDR14, /* type */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
399 FALSE, /* pc_relative */
401 complain_overflow_signed, /* complain_on_overflow */
402 ppc64_elf_branch_reloc, /* special_function */
403 "R_PPC64_ADDR14", /* name */
404 FALSE, /* partial_inplace */
406 0x0000fffc, /* dst_mask */
407 FALSE), /* pcrel_offset */
409 /* An absolute 16 bit branch, for which bit 10 should be set to
410 indicate that the branch is expected to be taken. The lower two
411 bits must be zero. */
412 HOWTO (R_PPC64_ADDR14_BRTAKEN, /* type */
414 2, /* size (0 = byte, 1 = short, 2 = long) */
416 FALSE, /* pc_relative */
418 complain_overflow_signed, /* complain_on_overflow */
419 ppc64_elf_brtaken_reloc, /* special_function */
420 "R_PPC64_ADDR14_BRTAKEN",/* name */
421 FALSE, /* partial_inplace */
423 0x0000fffc, /* dst_mask */
424 FALSE), /* pcrel_offset */
426 /* An absolute 16 bit branch, for which bit 10 should be set to
427 indicate that the branch is not expected to be taken. The lower
428 two bits must be zero. */
429 HOWTO (R_PPC64_ADDR14_BRNTAKEN, /* type */
431 2, /* size (0 = byte, 1 = short, 2 = long) */
433 FALSE, /* pc_relative */
435 complain_overflow_signed, /* complain_on_overflow */
436 ppc64_elf_brtaken_reloc, /* special_function */
437 "R_PPC64_ADDR14_BRNTAKEN",/* name */
438 FALSE, /* partial_inplace */
440 0x0000fffc, /* dst_mask */
441 FALSE), /* pcrel_offset */
443 /* A relative 26 bit branch; the lower two bits must be zero. */
444 HOWTO (R_PPC64_REL24, /* type */
446 2, /* size (0 = byte, 1 = short, 2 = long) */
448 TRUE, /* pc_relative */
450 complain_overflow_signed, /* complain_on_overflow */
451 ppc64_elf_branch_reloc, /* special_function */
452 "R_PPC64_REL24", /* name */
453 FALSE, /* partial_inplace */
455 0x03fffffc, /* dst_mask */
456 TRUE), /* pcrel_offset */
458 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
459 HOWTO (R_PPC64_REL24_NOTOC, /* type */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
463 TRUE, /* pc_relative */
465 complain_overflow_signed, /* complain_on_overflow */
466 ppc64_elf_branch_reloc, /* special_function */
467 "R_PPC64_REL24_NOTOC", /* name */
468 FALSE, /* partial_inplace */
470 0x03fffffc, /* dst_mask */
471 TRUE), /* pcrel_offset */
473 /* A relative 16 bit branch; the lower two bits must be zero. */
474 HOWTO (R_PPC64_REL14, /* type */
476 2, /* size (0 = byte, 1 = short, 2 = long) */
478 TRUE, /* pc_relative */
480 complain_overflow_signed, /* complain_on_overflow */
481 ppc64_elf_branch_reloc, /* special_function */
482 "R_PPC64_REL14", /* name */
483 FALSE, /* partial_inplace */
485 0x0000fffc, /* dst_mask */
486 TRUE), /* pcrel_offset */
488 /* A relative 16 bit branch. Bit 10 should be set to indicate that
489 the branch is expected to be taken. The lower two bits must be
491 HOWTO (R_PPC64_REL14_BRTAKEN, /* type */
493 2, /* size (0 = byte, 1 = short, 2 = long) */
495 TRUE, /* pc_relative */
497 complain_overflow_signed, /* complain_on_overflow */
498 ppc64_elf_brtaken_reloc, /* special_function */
499 "R_PPC64_REL14_BRTAKEN", /* name */
500 FALSE, /* partial_inplace */
502 0x0000fffc, /* dst_mask */
503 TRUE), /* pcrel_offset */
505 /* A relative 16 bit branch. Bit 10 should be set to indicate that
506 the branch is not expected to be taken. The lower two bits must
508 HOWTO (R_PPC64_REL14_BRNTAKEN, /* type */
510 2, /* size (0 = byte, 1 = short, 2 = long) */
512 TRUE, /* pc_relative */
514 complain_overflow_signed, /* complain_on_overflow */
515 ppc64_elf_brtaken_reloc, /* special_function */
516 "R_PPC64_REL14_BRNTAKEN",/* name */
517 FALSE, /* partial_inplace */
519 0x0000fffc, /* dst_mask */
520 TRUE), /* pcrel_offset */
522 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
524 HOWTO (R_PPC64_GOT16, /* type */
526 1, /* size (0 = byte, 1 = short, 2 = long) */
528 FALSE, /* pc_relative */
530 complain_overflow_signed, /* complain_on_overflow */
531 ppc64_elf_unhandled_reloc, /* special_function */
532 "R_PPC64_GOT16", /* name */
533 FALSE, /* partial_inplace */
535 0xffff, /* dst_mask */
536 FALSE), /* pcrel_offset */
538 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
540 HOWTO (R_PPC64_GOT16_LO, /* type */
542 1, /* size (0 = byte, 1 = short, 2 = long) */
544 FALSE, /* pc_relative */
546 complain_overflow_dont, /* complain_on_overflow */
547 ppc64_elf_unhandled_reloc, /* special_function */
548 "R_PPC64_GOT16_LO", /* name */
549 FALSE, /* partial_inplace */
551 0xffff, /* dst_mask */
552 FALSE), /* pcrel_offset */
554 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
556 HOWTO (R_PPC64_GOT16_HI, /* type */
558 1, /* size (0 = byte, 1 = short, 2 = long) */
560 FALSE, /* pc_relative */
562 complain_overflow_signed,/* complain_on_overflow */
563 ppc64_elf_unhandled_reloc, /* special_function */
564 "R_PPC64_GOT16_HI", /* name */
565 FALSE, /* partial_inplace */
567 0xffff, /* dst_mask */
568 FALSE), /* pcrel_offset */
570 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
572 HOWTO (R_PPC64_GOT16_HA, /* type */
574 1, /* size (0 = byte, 1 = short, 2 = long) */
576 FALSE, /* pc_relative */
578 complain_overflow_signed,/* complain_on_overflow */
579 ppc64_elf_unhandled_reloc, /* special_function */
580 "R_PPC64_GOT16_HA", /* name */
581 FALSE, /* partial_inplace */
583 0xffff, /* dst_mask */
584 FALSE), /* pcrel_offset */
586 /* This is used only by the dynamic linker. The symbol should exist
587 both in the object being run and in some shared library. The
588 dynamic linker copies the data addressed by the symbol from the
589 shared library into the object, because the object being
590 run has to have the data at some particular address. */
591 HOWTO (R_PPC64_COPY, /* type */
593 0, /* this one is variable size */
595 FALSE, /* pc_relative */
597 complain_overflow_dont, /* complain_on_overflow */
598 ppc64_elf_unhandled_reloc, /* special_function */
599 "R_PPC64_COPY", /* name */
600 FALSE, /* partial_inplace */
603 FALSE), /* pcrel_offset */
605 /* Like R_PPC64_ADDR64, but used when setting global offset table
607 HOWTO (R_PPC64_GLOB_DAT, /* type */
609 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
611 FALSE, /* pc_relative */
613 complain_overflow_dont, /* complain_on_overflow */
614 ppc64_elf_unhandled_reloc, /* special_function */
615 "R_PPC64_GLOB_DAT", /* name */
616 FALSE, /* partial_inplace */
618 ONES (64), /* dst_mask */
619 FALSE), /* pcrel_offset */
621 /* Created by the link editor. Marks a procedure linkage table
622 entry for a symbol. */
623 HOWTO (R_PPC64_JMP_SLOT, /* type */
625 0, /* size (0 = byte, 1 = short, 2 = long) */
627 FALSE, /* pc_relative */
629 complain_overflow_dont, /* complain_on_overflow */
630 ppc64_elf_unhandled_reloc, /* special_function */
631 "R_PPC64_JMP_SLOT", /* name */
632 FALSE, /* partial_inplace */
635 FALSE), /* pcrel_offset */
637 /* Used only by the dynamic linker. When the object is run, this
638 doubleword64 is set to the load address of the object, plus the
640 HOWTO (R_PPC64_RELATIVE, /* type */
642 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
644 FALSE, /* pc_relative */
646 complain_overflow_dont, /* complain_on_overflow */
647 bfd_elf_generic_reloc, /* special_function */
648 "R_PPC64_RELATIVE", /* name */
649 FALSE, /* partial_inplace */
651 ONES (64), /* dst_mask */
652 FALSE), /* pcrel_offset */
654 /* Like R_PPC64_ADDR32, but may be unaligned. */
655 HOWTO (R_PPC64_UADDR32, /* type */
657 2, /* size (0 = byte, 1 = short, 2 = long) */
659 FALSE, /* pc_relative */
661 complain_overflow_bitfield, /* complain_on_overflow */
662 bfd_elf_generic_reloc, /* special_function */
663 "R_PPC64_UADDR32", /* name */
664 FALSE, /* partial_inplace */
666 0xffffffff, /* dst_mask */
667 FALSE), /* pcrel_offset */
669 /* Like R_PPC64_ADDR16, but may be unaligned. */
670 HOWTO (R_PPC64_UADDR16, /* type */
672 1, /* size (0 = byte, 1 = short, 2 = long) */
674 FALSE, /* pc_relative */
676 complain_overflow_bitfield, /* complain_on_overflow */
677 bfd_elf_generic_reloc, /* special_function */
678 "R_PPC64_UADDR16", /* name */
679 FALSE, /* partial_inplace */
681 0xffff, /* dst_mask */
682 FALSE), /* pcrel_offset */
684 /* 32-bit PC relative. */
685 HOWTO (R_PPC64_REL32, /* type */
687 2, /* size (0 = byte, 1 = short, 2 = long) */
689 TRUE, /* pc_relative */
691 complain_overflow_signed, /* complain_on_overflow */
692 bfd_elf_generic_reloc, /* special_function */
693 "R_PPC64_REL32", /* name */
694 FALSE, /* partial_inplace */
696 0xffffffff, /* dst_mask */
697 TRUE), /* pcrel_offset */
699 /* 32-bit relocation to the symbol's procedure linkage table. */
700 HOWTO (R_PPC64_PLT32, /* type */
702 2, /* size (0 = byte, 1 = short, 2 = long) */
704 FALSE, /* pc_relative */
706 complain_overflow_bitfield, /* complain_on_overflow */
707 ppc64_elf_unhandled_reloc, /* special_function */
708 "R_PPC64_PLT32", /* name */
709 FALSE, /* partial_inplace */
711 0xffffffff, /* dst_mask */
712 FALSE), /* pcrel_offset */
714 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
715 FIXME: R_PPC64_PLTREL32 not supported. */
716 HOWTO (R_PPC64_PLTREL32, /* type */
718 2, /* size (0 = byte, 1 = short, 2 = long) */
720 TRUE, /* pc_relative */
722 complain_overflow_signed, /* complain_on_overflow */
723 ppc64_elf_unhandled_reloc, /* special_function */
724 "R_PPC64_PLTREL32", /* name */
725 FALSE, /* partial_inplace */
727 0xffffffff, /* dst_mask */
728 TRUE), /* pcrel_offset */
730 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
732 HOWTO (R_PPC64_PLT16_LO, /* type */
734 1, /* size (0 = byte, 1 = short, 2 = long) */
736 FALSE, /* pc_relative */
738 complain_overflow_dont, /* complain_on_overflow */
739 ppc64_elf_unhandled_reloc, /* special_function */
740 "R_PPC64_PLT16_LO", /* name */
741 FALSE, /* partial_inplace */
743 0xffff, /* dst_mask */
744 FALSE), /* pcrel_offset */
746 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
748 HOWTO (R_PPC64_PLT16_HI, /* type */
750 1, /* size (0 = byte, 1 = short, 2 = long) */
752 FALSE, /* pc_relative */
754 complain_overflow_signed, /* complain_on_overflow */
755 ppc64_elf_unhandled_reloc, /* special_function */
756 "R_PPC64_PLT16_HI", /* name */
757 FALSE, /* partial_inplace */
759 0xffff, /* dst_mask */
760 FALSE), /* pcrel_offset */
762 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
764 HOWTO (R_PPC64_PLT16_HA, /* type */
766 1, /* size (0 = byte, 1 = short, 2 = long) */
768 FALSE, /* pc_relative */
770 complain_overflow_signed, /* complain_on_overflow */
771 ppc64_elf_unhandled_reloc, /* special_function */
772 "R_PPC64_PLT16_HA", /* name */
773 FALSE, /* partial_inplace */
775 0xffff, /* dst_mask */
776 FALSE), /* pcrel_offset */
778 /* 16-bit section relative relocation. */
779 HOWTO (R_PPC64_SECTOFF, /* type */
781 1, /* size (0 = byte, 1 = short, 2 = long) */
783 FALSE, /* pc_relative */
785 complain_overflow_signed, /* complain_on_overflow */
786 ppc64_elf_sectoff_reloc, /* special_function */
787 "R_PPC64_SECTOFF", /* name */
788 FALSE, /* partial_inplace */
790 0xffff, /* dst_mask */
791 FALSE), /* pcrel_offset */
793 /* Like R_PPC64_SECTOFF, but no overflow warning. */
794 HOWTO (R_PPC64_SECTOFF_LO, /* type */
796 1, /* size (0 = byte, 1 = short, 2 = long) */
798 FALSE, /* pc_relative */
800 complain_overflow_dont, /* complain_on_overflow */
801 ppc64_elf_sectoff_reloc, /* special_function */
802 "R_PPC64_SECTOFF_LO", /* name */
803 FALSE, /* partial_inplace */
805 0xffff, /* dst_mask */
806 FALSE), /* pcrel_offset */
808 /* 16-bit upper half section relative relocation. */
809 HOWTO (R_PPC64_SECTOFF_HI, /* type */
811 1, /* size (0 = byte, 1 = short, 2 = long) */
813 FALSE, /* pc_relative */
815 complain_overflow_signed, /* complain_on_overflow */
816 ppc64_elf_sectoff_reloc, /* special_function */
817 "R_PPC64_SECTOFF_HI", /* name */
818 FALSE, /* partial_inplace */
820 0xffff, /* dst_mask */
821 FALSE), /* pcrel_offset */
823 /* 16-bit upper half adjusted section relative relocation. */
824 HOWTO (R_PPC64_SECTOFF_HA, /* type */
826 1, /* size (0 = byte, 1 = short, 2 = long) */
828 FALSE, /* pc_relative */
830 complain_overflow_signed, /* complain_on_overflow */
831 ppc64_elf_sectoff_ha_reloc, /* special_function */
832 "R_PPC64_SECTOFF_HA", /* name */
833 FALSE, /* partial_inplace */
835 0xffff, /* dst_mask */
836 FALSE), /* pcrel_offset */
838 /* Like R_PPC64_REL24 without touching the two least significant bits. */
839 HOWTO (R_PPC64_REL30, /* type */
841 2, /* size (0 = byte, 1 = short, 2 = long) */
843 TRUE, /* pc_relative */
845 complain_overflow_dont, /* complain_on_overflow */
846 bfd_elf_generic_reloc, /* special_function */
847 "R_PPC64_REL30", /* name */
848 FALSE, /* partial_inplace */
850 0xfffffffc, /* dst_mask */
851 TRUE), /* pcrel_offset */
853 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
855 /* A standard 64-bit relocation. */
856 HOWTO (R_PPC64_ADDR64, /* type */
858 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
860 FALSE, /* pc_relative */
862 complain_overflow_dont, /* complain_on_overflow */
863 bfd_elf_generic_reloc, /* special_function */
864 "R_PPC64_ADDR64", /* name */
865 FALSE, /* partial_inplace */
867 ONES (64), /* dst_mask */
868 FALSE), /* pcrel_offset */
870 /* The bits 32-47 of an address. */
871 HOWTO (R_PPC64_ADDR16_HIGHER, /* type */
873 1, /* size (0 = byte, 1 = short, 2 = long) */
875 FALSE, /* pc_relative */
877 complain_overflow_dont, /* complain_on_overflow */
878 bfd_elf_generic_reloc, /* special_function */
879 "R_PPC64_ADDR16_HIGHER", /* name */
880 FALSE, /* partial_inplace */
882 0xffff, /* dst_mask */
883 FALSE), /* pcrel_offset */
885 /* The bits 32-47 of an address, plus 1 if the contents of the low
886 16 bits, treated as a signed number, is negative. */
887 HOWTO (R_PPC64_ADDR16_HIGHERA, /* type */
889 1, /* size (0 = byte, 1 = short, 2 = long) */
891 FALSE, /* pc_relative */
893 complain_overflow_dont, /* complain_on_overflow */
894 ppc64_elf_ha_reloc, /* special_function */
895 "R_PPC64_ADDR16_HIGHERA", /* name */
896 FALSE, /* partial_inplace */
898 0xffff, /* dst_mask */
899 FALSE), /* pcrel_offset */
901 /* The bits 48-63 of an address. */
902 HOWTO (R_PPC64_ADDR16_HIGHEST,/* type */
904 1, /* size (0 = byte, 1 = short, 2 = long) */
906 FALSE, /* pc_relative */
908 complain_overflow_dont, /* complain_on_overflow */
909 bfd_elf_generic_reloc, /* special_function */
910 "R_PPC64_ADDR16_HIGHEST", /* name */
911 FALSE, /* partial_inplace */
913 0xffff, /* dst_mask */
914 FALSE), /* pcrel_offset */
916 /* The bits 48-63 of an address, plus 1 if the contents of the low
917 16 bits, treated as a signed number, is negative. */
918 HOWTO (R_PPC64_ADDR16_HIGHESTA,/* type */
920 1, /* size (0 = byte, 1 = short, 2 = long) */
922 FALSE, /* pc_relative */
924 complain_overflow_dont, /* complain_on_overflow */
925 ppc64_elf_ha_reloc, /* special_function */
926 "R_PPC64_ADDR16_HIGHESTA", /* name */
927 FALSE, /* partial_inplace */
929 0xffff, /* dst_mask */
930 FALSE), /* pcrel_offset */
932 /* Like ADDR64, but may be unaligned. */
933 HOWTO (R_PPC64_UADDR64, /* type */
935 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
937 FALSE, /* pc_relative */
939 complain_overflow_dont, /* complain_on_overflow */
940 bfd_elf_generic_reloc, /* special_function */
941 "R_PPC64_UADDR64", /* name */
942 FALSE, /* partial_inplace */
944 ONES (64), /* dst_mask */
945 FALSE), /* pcrel_offset */
947 /* 64-bit relative relocation. */
948 HOWTO (R_PPC64_REL64, /* type */
950 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
952 TRUE, /* pc_relative */
954 complain_overflow_dont, /* complain_on_overflow */
955 bfd_elf_generic_reloc, /* special_function */
956 "R_PPC64_REL64", /* name */
957 FALSE, /* partial_inplace */
959 ONES (64), /* dst_mask */
960 TRUE), /* pcrel_offset */
962 /* 64-bit relocation to the symbol's procedure linkage table. */
963 HOWTO (R_PPC64_PLT64, /* type */
965 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
967 FALSE, /* pc_relative */
969 complain_overflow_dont, /* complain_on_overflow */
970 ppc64_elf_unhandled_reloc, /* special_function */
971 "R_PPC64_PLT64", /* name */
972 FALSE, /* partial_inplace */
974 ONES (64), /* dst_mask */
975 FALSE), /* pcrel_offset */
977 /* 64-bit PC relative relocation to the symbol's procedure linkage
979 /* FIXME: R_PPC64_PLTREL64 not supported. */
980 HOWTO (R_PPC64_PLTREL64, /* type */
982 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
984 TRUE, /* pc_relative */
986 complain_overflow_dont, /* complain_on_overflow */
987 ppc64_elf_unhandled_reloc, /* special_function */
988 "R_PPC64_PLTREL64", /* name */
989 FALSE, /* partial_inplace */
991 ONES (64), /* dst_mask */
992 TRUE), /* pcrel_offset */
994 /* 16 bit TOC-relative relocation. */
996 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
997 HOWTO (R_PPC64_TOC16, /* type */
999 1, /* size (0 = byte, 1 = short, 2 = long) */
1001 FALSE, /* pc_relative */
1003 complain_overflow_signed, /* complain_on_overflow */
1004 ppc64_elf_toc_reloc, /* special_function */
1005 "R_PPC64_TOC16", /* name */
1006 FALSE, /* partial_inplace */
1008 0xffff, /* dst_mask */
1009 FALSE), /* pcrel_offset */
1011 /* 16 bit TOC-relative relocation without overflow. */
1013 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
1014 HOWTO (R_PPC64_TOC16_LO, /* type */
1016 1, /* size (0 = byte, 1 = short, 2 = long) */
1018 FALSE, /* pc_relative */
1020 complain_overflow_dont, /* complain_on_overflow */
1021 ppc64_elf_toc_reloc, /* special_function */
1022 "R_PPC64_TOC16_LO", /* name */
1023 FALSE, /* partial_inplace */
1025 0xffff, /* dst_mask */
1026 FALSE), /* pcrel_offset */
1028 /* 16 bit TOC-relative relocation, high 16 bits. */
1030 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
1031 HOWTO (R_PPC64_TOC16_HI, /* type */
1032 16, /* rightshift */
1033 1, /* size (0 = byte, 1 = short, 2 = long) */
1035 FALSE, /* pc_relative */
1037 complain_overflow_signed, /* complain_on_overflow */
1038 ppc64_elf_toc_reloc, /* special_function */
1039 "R_PPC64_TOC16_HI", /* name */
1040 FALSE, /* partial_inplace */
1042 0xffff, /* dst_mask */
1043 FALSE), /* pcrel_offset */
1045 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
1046 contents of the low 16 bits, treated as a signed number, is
1049 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
1050 HOWTO (R_PPC64_TOC16_HA, /* type */
1051 16, /* rightshift */
1052 1, /* size (0 = byte, 1 = short, 2 = long) */
1054 FALSE, /* pc_relative */
1056 complain_overflow_signed, /* complain_on_overflow */
1057 ppc64_elf_toc_ha_reloc, /* special_function */
1058 "R_PPC64_TOC16_HA", /* name */
1059 FALSE, /* partial_inplace */
1061 0xffff, /* dst_mask */
1062 FALSE), /* pcrel_offset */
1064 /* 64-bit relocation; insert value of TOC base (.TOC.). */
1066 /* R_PPC64_TOC 51 doubleword64 .TOC. */
1067 HOWTO (R_PPC64_TOC, /* type */
1069 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
1071 FALSE, /* pc_relative */
1073 complain_overflow_dont, /* complain_on_overflow */
1074 ppc64_elf_toc64_reloc, /* special_function */
1075 "R_PPC64_TOC", /* name */
1076 FALSE, /* partial_inplace */
1078 ONES (64), /* dst_mask */
1079 FALSE), /* pcrel_offset */
1081 /* Like R_PPC64_GOT16, but also informs the link editor that the
1082 value to relocate may (!) refer to a PLT entry which the link
1083 editor (a) may replace with the symbol value. If the link editor
1084 is unable to fully resolve the symbol, it may (b) create a PLT
1085 entry and store the address to the new PLT entry in the GOT.
1086 This permits lazy resolution of function symbols at run time.
1087 The link editor may also skip all of this and just (c) emit a
1088 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
1089 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
1090 HOWTO (R_PPC64_PLTGOT16, /* type */
1092 1, /* size (0 = byte, 1 = short, 2 = long) */
1094 FALSE, /* pc_relative */
1096 complain_overflow_signed, /* complain_on_overflow */
1097 ppc64_elf_unhandled_reloc, /* special_function */
1098 "R_PPC64_PLTGOT16", /* name */
1099 FALSE, /* partial_inplace */
1101 0xffff, /* dst_mask */
1102 FALSE), /* pcrel_offset */
1104 /* Like R_PPC64_PLTGOT16, but without overflow. */
1105 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1106 HOWTO (R_PPC64_PLTGOT16_LO, /* type */
1108 1, /* size (0 = byte, 1 = short, 2 = long) */
1110 FALSE, /* pc_relative */
1112 complain_overflow_dont, /* complain_on_overflow */
1113 ppc64_elf_unhandled_reloc, /* special_function */
1114 "R_PPC64_PLTGOT16_LO", /* name */
1115 FALSE, /* partial_inplace */
1117 0xffff, /* dst_mask */
1118 FALSE), /* pcrel_offset */
1120 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
1121 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
1122 HOWTO (R_PPC64_PLTGOT16_HI, /* type */
1123 16, /* rightshift */
1124 1, /* size (0 = byte, 1 = short, 2 = long) */
1126 FALSE, /* pc_relative */
1128 complain_overflow_signed, /* complain_on_overflow */
1129 ppc64_elf_unhandled_reloc, /* special_function */
1130 "R_PPC64_PLTGOT16_HI", /* name */
1131 FALSE, /* partial_inplace */
1133 0xffff, /* dst_mask */
1134 FALSE), /* pcrel_offset */
1136 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
1137 1 if the contents of the low 16 bits, treated as a signed number,
1139 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
1140 HOWTO (R_PPC64_PLTGOT16_HA, /* type */
1141 16, /* rightshift */
1142 1, /* size (0 = byte, 1 = short, 2 = long) */
1144 FALSE, /* pc_relative */
1146 complain_overflow_signed, /* complain_on_overflow */
1147 ppc64_elf_unhandled_reloc, /* special_function */
1148 "R_PPC64_PLTGOT16_HA", /* name */
1149 FALSE, /* partial_inplace */
1151 0xffff, /* dst_mask */
1152 FALSE), /* pcrel_offset */
1154 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
1155 HOWTO (R_PPC64_ADDR16_DS, /* type */
1157 1, /* size (0 = byte, 1 = short, 2 = long) */
1159 FALSE, /* pc_relative */
1161 complain_overflow_signed, /* complain_on_overflow */
1162 bfd_elf_generic_reloc, /* special_function */
1163 "R_PPC64_ADDR16_DS", /* name */
1164 FALSE, /* partial_inplace */
1166 0xfffc, /* dst_mask */
1167 FALSE), /* pcrel_offset */
1169 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
1170 HOWTO (R_PPC64_ADDR16_LO_DS, /* type */
1172 1, /* size (0 = byte, 1 = short, 2 = long) */
1174 FALSE, /* pc_relative */
1176 complain_overflow_dont,/* complain_on_overflow */
1177 bfd_elf_generic_reloc, /* special_function */
1178 "R_PPC64_ADDR16_LO_DS",/* name */
1179 FALSE, /* partial_inplace */
1181 0xfffc, /* dst_mask */
1182 FALSE), /* pcrel_offset */
1184 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
1185 HOWTO (R_PPC64_GOT16_DS, /* type */
1187 1, /* size (0 = byte, 1 = short, 2 = long) */
1189 FALSE, /* pc_relative */
1191 complain_overflow_signed, /* complain_on_overflow */
1192 ppc64_elf_unhandled_reloc, /* special_function */
1193 "R_PPC64_GOT16_DS", /* name */
1194 FALSE, /* partial_inplace */
1196 0xfffc, /* dst_mask */
1197 FALSE), /* pcrel_offset */
1199 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
1200 HOWTO (R_PPC64_GOT16_LO_DS, /* type */
1202 1, /* size (0 = byte, 1 = short, 2 = long) */
1204 FALSE, /* pc_relative */
1206 complain_overflow_dont, /* complain_on_overflow */
1207 ppc64_elf_unhandled_reloc, /* special_function */
1208 "R_PPC64_GOT16_LO_DS", /* name */
1209 FALSE, /* partial_inplace */
1211 0xfffc, /* dst_mask */
1212 FALSE), /* pcrel_offset */
1214 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
1215 HOWTO (R_PPC64_PLT16_LO_DS, /* type */
1217 1, /* size (0 = byte, 1 = short, 2 = long) */
1219 FALSE, /* pc_relative */
1221 complain_overflow_dont, /* complain_on_overflow */
1222 ppc64_elf_unhandled_reloc, /* special_function */
1223 "R_PPC64_PLT16_LO_DS", /* name */
1224 FALSE, /* partial_inplace */
1226 0xfffc, /* dst_mask */
1227 FALSE), /* pcrel_offset */
1229 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
1230 HOWTO (R_PPC64_SECTOFF_DS, /* type */
1232 1, /* size (0 = byte, 1 = short, 2 = long) */
1234 FALSE, /* pc_relative */
1236 complain_overflow_signed, /* complain_on_overflow */
1237 ppc64_elf_sectoff_reloc, /* special_function */
1238 "R_PPC64_SECTOFF_DS", /* name */
1239 FALSE, /* partial_inplace */
1241 0xfffc, /* dst_mask */
1242 FALSE), /* pcrel_offset */
1244 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
1245 HOWTO (R_PPC64_SECTOFF_LO_DS, /* type */
1247 1, /* size (0 = byte, 1 = short, 2 = long) */
1249 FALSE, /* pc_relative */
1251 complain_overflow_dont, /* complain_on_overflow */
1252 ppc64_elf_sectoff_reloc, /* special_function */
1253 "R_PPC64_SECTOFF_LO_DS",/* name */
1254 FALSE, /* partial_inplace */
1256 0xfffc, /* dst_mask */
1257 FALSE), /* pcrel_offset */
1259 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
1260 HOWTO (R_PPC64_TOC16_DS, /* type */
1262 1, /* size (0 = byte, 1 = short, 2 = long) */
1264 FALSE, /* pc_relative */
1266 complain_overflow_signed, /* complain_on_overflow */
1267 ppc64_elf_toc_reloc, /* special_function */
1268 "R_PPC64_TOC16_DS", /* name */
1269 FALSE, /* partial_inplace */
1271 0xfffc, /* dst_mask */
1272 FALSE), /* pcrel_offset */
1274 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
1275 HOWTO (R_PPC64_TOC16_LO_DS, /* type */
1277 1, /* size (0 = byte, 1 = short, 2 = long) */
1279 FALSE, /* pc_relative */
1281 complain_overflow_dont, /* complain_on_overflow */
1282 ppc64_elf_toc_reloc, /* special_function */
1283 "R_PPC64_TOC16_LO_DS", /* name */
1284 FALSE, /* partial_inplace */
1286 0xfffc, /* dst_mask */
1287 FALSE), /* pcrel_offset */
1289 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
1290 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
1291 HOWTO (R_PPC64_PLTGOT16_DS, /* type */
1293 1, /* size (0 = byte, 1 = short, 2 = long) */
1295 FALSE, /* pc_relative */
1297 complain_overflow_signed, /* complain_on_overflow */
1298 ppc64_elf_unhandled_reloc, /* special_function */
1299 "R_PPC64_PLTGOT16_DS", /* name */
1300 FALSE, /* partial_inplace */
1302 0xfffc, /* dst_mask */
1303 FALSE), /* pcrel_offset */
1305 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
1306 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
1307 HOWTO (R_PPC64_PLTGOT16_LO_DS,/* type */
1309 1, /* size (0 = byte, 1 = short, 2 = long) */
1311 FALSE, /* pc_relative */
1313 complain_overflow_dont, /* complain_on_overflow */
1314 ppc64_elf_unhandled_reloc, /* special_function */
1315 "R_PPC64_PLTGOT16_LO_DS",/* name */
1316 FALSE, /* partial_inplace */
1318 0xfffc, /* dst_mask */
1319 FALSE), /* pcrel_offset */
1321 /* Marker relocs for TLS. */
1324 2, /* size (0 = byte, 1 = short, 2 = long) */
1326 FALSE, /* pc_relative */
1328 complain_overflow_dont, /* complain_on_overflow */
1329 bfd_elf_generic_reloc, /* special_function */
1330 "R_PPC64_TLS", /* name */
1331 FALSE, /* partial_inplace */
1334 FALSE), /* pcrel_offset */
1336 HOWTO (R_PPC64_TLSGD,
1338 2, /* size (0 = byte, 1 = short, 2 = long) */
1340 FALSE, /* pc_relative */
1342 complain_overflow_dont, /* complain_on_overflow */
1343 bfd_elf_generic_reloc, /* special_function */
1344 "R_PPC64_TLSGD", /* name */
1345 FALSE, /* partial_inplace */
1348 FALSE), /* pcrel_offset */
1350 HOWTO (R_PPC64_TLSLD,
1352 2, /* size (0 = byte, 1 = short, 2 = long) */
1354 FALSE, /* pc_relative */
1356 complain_overflow_dont, /* complain_on_overflow */
1357 bfd_elf_generic_reloc, /* special_function */
1358 "R_PPC64_TLSLD", /* name */
1359 FALSE, /* partial_inplace */
1362 FALSE), /* pcrel_offset */
1364 /* Marker reloc for optimizing r2 save in prologue rather than on
1365 each plt call stub. */
1366 HOWTO (R_PPC64_TOCSAVE,
1368 2, /* size (0 = byte, 1 = short, 2 = long) */
1370 FALSE, /* pc_relative */
1372 complain_overflow_dont, /* complain_on_overflow */
1373 bfd_elf_generic_reloc, /* special_function */
1374 "R_PPC64_TOCSAVE", /* name */
1375 FALSE, /* partial_inplace */
1378 FALSE), /* pcrel_offset */
1380 /* Marker relocs on inline plt call instructions. */
1381 HOWTO (R_PPC64_PLTSEQ,
1383 2, /* size (0 = byte, 1 = short, 2 = long) */
1385 FALSE, /* pc_relative */
1387 complain_overflow_dont, /* complain_on_overflow */
1388 bfd_elf_generic_reloc, /* special_function */
1389 "R_PPC64_PLTSEQ", /* name */
1390 FALSE, /* partial_inplace */
1393 FALSE), /* pcrel_offset */
1395 HOWTO (R_PPC64_PLTCALL,
1397 2, /* size (0 = byte, 1 = short, 2 = long) */
1399 FALSE, /* pc_relative */
1401 complain_overflow_dont, /* complain_on_overflow */
1402 bfd_elf_generic_reloc, /* special_function */
1403 "R_PPC64_PLTCALL", /* name */
1404 FALSE, /* partial_inplace */
1407 FALSE), /* pcrel_offset */
1409 /* Computes the load module index of the load module that contains the
1410 definition of its TLS sym. */
1411 HOWTO (R_PPC64_DTPMOD64,
1413 4, /* size (0 = byte, 1 = short, 2 = long) */
1415 FALSE, /* pc_relative */
1417 complain_overflow_dont, /* complain_on_overflow */
1418 ppc64_elf_unhandled_reloc, /* special_function */
1419 "R_PPC64_DTPMOD64", /* name */
1420 FALSE, /* partial_inplace */
1422 ONES (64), /* dst_mask */
1423 FALSE), /* pcrel_offset */
1425 /* Computes a dtv-relative displacement, the difference between the value
1426 of sym+add and the base address of the thread-local storage block that
1427 contains the definition of sym, minus 0x8000. */
1428 HOWTO (R_PPC64_DTPREL64,
1430 4, /* size (0 = byte, 1 = short, 2 = long) */
1432 FALSE, /* pc_relative */
1434 complain_overflow_dont, /* complain_on_overflow */
1435 ppc64_elf_unhandled_reloc, /* special_function */
1436 "R_PPC64_DTPREL64", /* name */
1437 FALSE, /* partial_inplace */
1439 ONES (64), /* dst_mask */
1440 FALSE), /* pcrel_offset */
1442 /* A 16 bit dtprel reloc. */
1443 HOWTO (R_PPC64_DTPREL16,
1445 1, /* size (0 = byte, 1 = short, 2 = long) */
1447 FALSE, /* pc_relative */
1449 complain_overflow_signed, /* complain_on_overflow */
1450 ppc64_elf_unhandled_reloc, /* special_function */
1451 "R_PPC64_DTPREL16", /* name */
1452 FALSE, /* partial_inplace */
1454 0xffff, /* dst_mask */
1455 FALSE), /* pcrel_offset */
1457 /* Like DTPREL16, but no overflow. */
1458 HOWTO (R_PPC64_DTPREL16_LO,
1460 1, /* size (0 = byte, 1 = short, 2 = long) */
1462 FALSE, /* pc_relative */
1464 complain_overflow_dont, /* complain_on_overflow */
1465 ppc64_elf_unhandled_reloc, /* special_function */
1466 "R_PPC64_DTPREL16_LO", /* name */
1467 FALSE, /* partial_inplace */
1469 0xffff, /* dst_mask */
1470 FALSE), /* pcrel_offset */
1472 /* Like DTPREL16_LO, but next higher group of 16 bits. */
1473 HOWTO (R_PPC64_DTPREL16_HI,
1474 16, /* rightshift */
1475 1, /* size (0 = byte, 1 = short, 2 = long) */
1477 FALSE, /* pc_relative */
1479 complain_overflow_signed, /* complain_on_overflow */
1480 ppc64_elf_unhandled_reloc, /* special_function */
1481 "R_PPC64_DTPREL16_HI", /* name */
1482 FALSE, /* partial_inplace */
1484 0xffff, /* dst_mask */
1485 FALSE), /* pcrel_offset */
1487 /* Like DTPREL16_HI, but adjust for low 16 bits. */
1488 HOWTO (R_PPC64_DTPREL16_HA,
1489 16, /* rightshift */
1490 1, /* size (0 = byte, 1 = short, 2 = long) */
1492 FALSE, /* pc_relative */
1494 complain_overflow_signed, /* complain_on_overflow */
1495 ppc64_elf_unhandled_reloc, /* special_function */
1496 "R_PPC64_DTPREL16_HA", /* name */
1497 FALSE, /* partial_inplace */
1499 0xffff, /* dst_mask */
1500 FALSE), /* pcrel_offset */
1502 /* Like DTPREL16_HI, but next higher group of 16 bits. */
1503 HOWTO (R_PPC64_DTPREL16_HIGHER,
1504 32, /* rightshift */
1505 1, /* size (0 = byte, 1 = short, 2 = long) */
1507 FALSE, /* pc_relative */
1509 complain_overflow_dont, /* complain_on_overflow */
1510 ppc64_elf_unhandled_reloc, /* special_function */
1511 "R_PPC64_DTPREL16_HIGHER", /* name */
1512 FALSE, /* partial_inplace */
1514 0xffff, /* dst_mask */
1515 FALSE), /* pcrel_offset */
1517 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
1518 HOWTO (R_PPC64_DTPREL16_HIGHERA,
1519 32, /* rightshift */
1520 1, /* size (0 = byte, 1 = short, 2 = long) */
1522 FALSE, /* pc_relative */
1524 complain_overflow_dont, /* complain_on_overflow */
1525 ppc64_elf_unhandled_reloc, /* special_function */
1526 "R_PPC64_DTPREL16_HIGHERA", /* name */
1527 FALSE, /* partial_inplace */
1529 0xffff, /* dst_mask */
1530 FALSE), /* pcrel_offset */
1532 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
1533 HOWTO (R_PPC64_DTPREL16_HIGHEST,
1534 48, /* rightshift */
1535 1, /* size (0 = byte, 1 = short, 2 = long) */
1537 FALSE, /* pc_relative */
1539 complain_overflow_dont, /* complain_on_overflow */
1540 ppc64_elf_unhandled_reloc, /* special_function */
1541 "R_PPC64_DTPREL16_HIGHEST", /* name */
1542 FALSE, /* partial_inplace */
1544 0xffff, /* dst_mask */
1545 FALSE), /* pcrel_offset */
1547 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
1548 HOWTO (R_PPC64_DTPREL16_HIGHESTA,
1549 48, /* rightshift */
1550 1, /* size (0 = byte, 1 = short, 2 = long) */
1552 FALSE, /* pc_relative */
1554 complain_overflow_dont, /* complain_on_overflow */
1555 ppc64_elf_unhandled_reloc, /* special_function */
1556 "R_PPC64_DTPREL16_HIGHESTA", /* name */
1557 FALSE, /* partial_inplace */
1559 0xffff, /* dst_mask */
1560 FALSE), /* pcrel_offset */
1562 /* Like DTPREL16, but for insns with a DS field. */
1563 HOWTO (R_PPC64_DTPREL16_DS,
1565 1, /* size (0 = byte, 1 = short, 2 = long) */
1567 FALSE, /* pc_relative */
1569 complain_overflow_signed, /* complain_on_overflow */
1570 ppc64_elf_unhandled_reloc, /* special_function */
1571 "R_PPC64_DTPREL16_DS", /* name */
1572 FALSE, /* partial_inplace */
1574 0xfffc, /* dst_mask */
1575 FALSE), /* pcrel_offset */
1577 /* Like DTPREL16_DS, but no overflow. */
1578 HOWTO (R_PPC64_DTPREL16_LO_DS,
1580 1, /* size (0 = byte, 1 = short, 2 = long) */
1582 FALSE, /* pc_relative */
1584 complain_overflow_dont, /* complain_on_overflow */
1585 ppc64_elf_unhandled_reloc, /* special_function */
1586 "R_PPC64_DTPREL16_LO_DS", /* name */
1587 FALSE, /* partial_inplace */
1589 0xfffc, /* dst_mask */
1590 FALSE), /* pcrel_offset */
1592 /* Computes a tp-relative displacement, the difference between the value of
1593 sym+add and the value of the thread pointer (r13). */
1594 HOWTO (R_PPC64_TPREL64,
1596 4, /* size (0 = byte, 1 = short, 2 = long) */
1598 FALSE, /* pc_relative */
1600 complain_overflow_dont, /* complain_on_overflow */
1601 ppc64_elf_unhandled_reloc, /* special_function */
1602 "R_PPC64_TPREL64", /* name */
1603 FALSE, /* partial_inplace */
1605 ONES (64), /* dst_mask */
1606 FALSE), /* pcrel_offset */
1608 /* A 16 bit tprel reloc. */
1609 HOWTO (R_PPC64_TPREL16,
1611 1, /* size (0 = byte, 1 = short, 2 = long) */
1613 FALSE, /* pc_relative */
1615 complain_overflow_signed, /* complain_on_overflow */
1616 ppc64_elf_unhandled_reloc, /* special_function */
1617 "R_PPC64_TPREL16", /* name */
1618 FALSE, /* partial_inplace */
1620 0xffff, /* dst_mask */
1621 FALSE), /* pcrel_offset */
1623 /* Like TPREL16, but no overflow. */
1624 HOWTO (R_PPC64_TPREL16_LO,
1626 1, /* size (0 = byte, 1 = short, 2 = long) */
1628 FALSE, /* pc_relative */
1630 complain_overflow_dont, /* complain_on_overflow */
1631 ppc64_elf_unhandled_reloc, /* special_function */
1632 "R_PPC64_TPREL16_LO", /* name */
1633 FALSE, /* partial_inplace */
1635 0xffff, /* dst_mask */
1636 FALSE), /* pcrel_offset */
1638 /* Like TPREL16_LO, but next higher group of 16 bits. */
1639 HOWTO (R_PPC64_TPREL16_HI,
1640 16, /* rightshift */
1641 1, /* size (0 = byte, 1 = short, 2 = long) */
1643 FALSE, /* pc_relative */
1645 complain_overflow_signed, /* complain_on_overflow */
1646 ppc64_elf_unhandled_reloc, /* special_function */
1647 "R_PPC64_TPREL16_HI", /* name */
1648 FALSE, /* partial_inplace */
1650 0xffff, /* dst_mask */
1651 FALSE), /* pcrel_offset */
1653 /* Like TPREL16_HI, but adjust for low 16 bits. */
1654 HOWTO (R_PPC64_TPREL16_HA,
1655 16, /* rightshift */
1656 1, /* size (0 = byte, 1 = short, 2 = long) */
1658 FALSE, /* pc_relative */
1660 complain_overflow_signed, /* complain_on_overflow */
1661 ppc64_elf_unhandled_reloc, /* special_function */
1662 "R_PPC64_TPREL16_HA", /* name */
1663 FALSE, /* partial_inplace */
1665 0xffff, /* dst_mask */
1666 FALSE), /* pcrel_offset */
1668 /* Like TPREL16_HI, but next higher group of 16 bits. */
1669 HOWTO (R_PPC64_TPREL16_HIGHER,
1670 32, /* rightshift */
1671 1, /* size (0 = byte, 1 = short, 2 = long) */
1673 FALSE, /* pc_relative */
1675 complain_overflow_dont, /* complain_on_overflow */
1676 ppc64_elf_unhandled_reloc, /* special_function */
1677 "R_PPC64_TPREL16_HIGHER", /* name */
1678 FALSE, /* partial_inplace */
1680 0xffff, /* dst_mask */
1681 FALSE), /* pcrel_offset */
1683 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
1684 HOWTO (R_PPC64_TPREL16_HIGHERA,
1685 32, /* rightshift */
1686 1, /* size (0 = byte, 1 = short, 2 = long) */
1688 FALSE, /* pc_relative */
1690 complain_overflow_dont, /* complain_on_overflow */
1691 ppc64_elf_unhandled_reloc, /* special_function */
1692 "R_PPC64_TPREL16_HIGHERA", /* name */
1693 FALSE, /* partial_inplace */
1695 0xffff, /* dst_mask */
1696 FALSE), /* pcrel_offset */
1698 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
1699 HOWTO (R_PPC64_TPREL16_HIGHEST,
1700 48, /* rightshift */
1701 1, /* size (0 = byte, 1 = short, 2 = long) */
1703 FALSE, /* pc_relative */
1705 complain_overflow_dont, /* complain_on_overflow */
1706 ppc64_elf_unhandled_reloc, /* special_function */
1707 "R_PPC64_TPREL16_HIGHEST", /* name */
1708 FALSE, /* partial_inplace */
1710 0xffff, /* dst_mask */
1711 FALSE), /* pcrel_offset */
1713 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
1714 HOWTO (R_PPC64_TPREL16_HIGHESTA,
1715 48, /* rightshift */
1716 1, /* size (0 = byte, 1 = short, 2 = long) */
1718 FALSE, /* pc_relative */
1720 complain_overflow_dont, /* complain_on_overflow */
1721 ppc64_elf_unhandled_reloc, /* special_function */
1722 "R_PPC64_TPREL16_HIGHESTA", /* name */
1723 FALSE, /* partial_inplace */
1725 0xffff, /* dst_mask */
1726 FALSE), /* pcrel_offset */
1728 /* Like TPREL16, but for insns with a DS field. */
1729 HOWTO (R_PPC64_TPREL16_DS,
1731 1, /* size (0 = byte, 1 = short, 2 = long) */
1733 FALSE, /* pc_relative */
1735 complain_overflow_signed, /* complain_on_overflow */
1736 ppc64_elf_unhandled_reloc, /* special_function */
1737 "R_PPC64_TPREL16_DS", /* name */
1738 FALSE, /* partial_inplace */
1740 0xfffc, /* dst_mask */
1741 FALSE), /* pcrel_offset */
1743 /* Like TPREL16_DS, but no overflow. */
1744 HOWTO (R_PPC64_TPREL16_LO_DS,
1746 1, /* size (0 = byte, 1 = short, 2 = long) */
1748 FALSE, /* pc_relative */
1750 complain_overflow_dont, /* complain_on_overflow */
1751 ppc64_elf_unhandled_reloc, /* special_function */
1752 "R_PPC64_TPREL16_LO_DS", /* name */
1753 FALSE, /* partial_inplace */
1755 0xfffc, /* dst_mask */
1756 FALSE), /* pcrel_offset */
1758 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1759 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
1760 to the first entry relative to the TOC base (r2). */
1761 HOWTO (R_PPC64_GOT_TLSGD16,
1763 1, /* size (0 = byte, 1 = short, 2 = long) */
1765 FALSE, /* pc_relative */
1767 complain_overflow_signed, /* complain_on_overflow */
1768 ppc64_elf_unhandled_reloc, /* special_function */
1769 "R_PPC64_GOT_TLSGD16", /* name */
1770 FALSE, /* partial_inplace */
1772 0xffff, /* dst_mask */
1773 FALSE), /* pcrel_offset */
1775 /* Like GOT_TLSGD16, but no overflow. */
1776 HOWTO (R_PPC64_GOT_TLSGD16_LO,
1778 1, /* size (0 = byte, 1 = short, 2 = long) */
1780 FALSE, /* pc_relative */
1782 complain_overflow_dont, /* complain_on_overflow */
1783 ppc64_elf_unhandled_reloc, /* special_function */
1784 "R_PPC64_GOT_TLSGD16_LO", /* name */
1785 FALSE, /* partial_inplace */
1787 0xffff, /* dst_mask */
1788 FALSE), /* pcrel_offset */
1790 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
1791 HOWTO (R_PPC64_GOT_TLSGD16_HI,
1792 16, /* rightshift */
1793 1, /* size (0 = byte, 1 = short, 2 = long) */
1795 FALSE, /* pc_relative */
1797 complain_overflow_signed, /* complain_on_overflow */
1798 ppc64_elf_unhandled_reloc, /* special_function */
1799 "R_PPC64_GOT_TLSGD16_HI", /* name */
1800 FALSE, /* partial_inplace */
1802 0xffff, /* dst_mask */
1803 FALSE), /* pcrel_offset */
1805 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
1806 HOWTO (R_PPC64_GOT_TLSGD16_HA,
1807 16, /* rightshift */
1808 1, /* size (0 = byte, 1 = short, 2 = long) */
1810 FALSE, /* pc_relative */
1812 complain_overflow_signed, /* complain_on_overflow */
1813 ppc64_elf_unhandled_reloc, /* special_function */
1814 "R_PPC64_GOT_TLSGD16_HA", /* name */
1815 FALSE, /* partial_inplace */
1817 0xffff, /* dst_mask */
1818 FALSE), /* pcrel_offset */
1820 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
1821 with values (sym+add)@dtpmod and zero, and computes the offset to the
1822 first entry relative to the TOC base (r2). */
1823 HOWTO (R_PPC64_GOT_TLSLD16,
1825 1, /* size (0 = byte, 1 = short, 2 = long) */
1827 FALSE, /* pc_relative */
1829 complain_overflow_signed, /* complain_on_overflow */
1830 ppc64_elf_unhandled_reloc, /* special_function */
1831 "R_PPC64_GOT_TLSLD16", /* name */
1832 FALSE, /* partial_inplace */
1834 0xffff, /* dst_mask */
1835 FALSE), /* pcrel_offset */
1837 /* Like GOT_TLSLD16, but no overflow. */
1838 HOWTO (R_PPC64_GOT_TLSLD16_LO,
1840 1, /* size (0 = byte, 1 = short, 2 = long) */
1842 FALSE, /* pc_relative */
1844 complain_overflow_dont, /* complain_on_overflow */
1845 ppc64_elf_unhandled_reloc, /* special_function */
1846 "R_PPC64_GOT_TLSLD16_LO", /* name */
1847 FALSE, /* partial_inplace */
1849 0xffff, /* dst_mask */
1850 FALSE), /* pcrel_offset */
1852 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
1853 HOWTO (R_PPC64_GOT_TLSLD16_HI,
1854 16, /* rightshift */
1855 1, /* size (0 = byte, 1 = short, 2 = long) */
1857 FALSE, /* pc_relative */
1859 complain_overflow_signed, /* complain_on_overflow */
1860 ppc64_elf_unhandled_reloc, /* special_function */
1861 "R_PPC64_GOT_TLSLD16_HI", /* name */
1862 FALSE, /* partial_inplace */
1864 0xffff, /* dst_mask */
1865 FALSE), /* pcrel_offset */
1867 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
1868 HOWTO (R_PPC64_GOT_TLSLD16_HA,
1869 16, /* rightshift */
1870 1, /* size (0 = byte, 1 = short, 2 = long) */
1872 FALSE, /* pc_relative */
1874 complain_overflow_signed, /* complain_on_overflow */
1875 ppc64_elf_unhandled_reloc, /* special_function */
1876 "R_PPC64_GOT_TLSLD16_HA", /* name */
1877 FALSE, /* partial_inplace */
1879 0xffff, /* dst_mask */
1880 FALSE), /* pcrel_offset */
1882 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
1883 the offset to the entry relative to the TOC base (r2). */
1884 HOWTO (R_PPC64_GOT_DTPREL16_DS,
1886 1, /* size (0 = byte, 1 = short, 2 = long) */
1888 FALSE, /* pc_relative */
1890 complain_overflow_signed, /* complain_on_overflow */
1891 ppc64_elf_unhandled_reloc, /* special_function */
1892 "R_PPC64_GOT_DTPREL16_DS", /* name */
1893 FALSE, /* partial_inplace */
1895 0xfffc, /* dst_mask */
1896 FALSE), /* pcrel_offset */
1898 /* Like GOT_DTPREL16_DS, but no overflow. */
1899 HOWTO (R_PPC64_GOT_DTPREL16_LO_DS,
1901 1, /* size (0 = byte, 1 = short, 2 = long) */
1903 FALSE, /* pc_relative */
1905 complain_overflow_dont, /* complain_on_overflow */
1906 ppc64_elf_unhandled_reloc, /* special_function */
1907 "R_PPC64_GOT_DTPREL16_LO_DS", /* name */
1908 FALSE, /* partial_inplace */
1910 0xfffc, /* dst_mask */
1911 FALSE), /* pcrel_offset */
1913 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
1914 HOWTO (R_PPC64_GOT_DTPREL16_HI,
1915 16, /* rightshift */
1916 1, /* size (0 = byte, 1 = short, 2 = long) */
1918 FALSE, /* pc_relative */
1920 complain_overflow_signed, /* complain_on_overflow */
1921 ppc64_elf_unhandled_reloc, /* special_function */
1922 "R_PPC64_GOT_DTPREL16_HI", /* name */
1923 FALSE, /* partial_inplace */
1925 0xffff, /* dst_mask */
1926 FALSE), /* pcrel_offset */
1928 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
1929 HOWTO (R_PPC64_GOT_DTPREL16_HA,
1930 16, /* rightshift */
1931 1, /* size (0 = byte, 1 = short, 2 = long) */
1933 FALSE, /* pc_relative */
1935 complain_overflow_signed, /* complain_on_overflow */
1936 ppc64_elf_unhandled_reloc, /* special_function */
1937 "R_PPC64_GOT_DTPREL16_HA", /* name */
1938 FALSE, /* partial_inplace */
1940 0xffff, /* dst_mask */
1941 FALSE), /* pcrel_offset */
1943 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
1944 offset to the entry relative to the TOC base (r2). */
1945 HOWTO (R_PPC64_GOT_TPREL16_DS,
1947 1, /* size (0 = byte, 1 = short, 2 = long) */
1949 FALSE, /* pc_relative */
1951 complain_overflow_signed, /* complain_on_overflow */
1952 ppc64_elf_unhandled_reloc, /* special_function */
1953 "R_PPC64_GOT_TPREL16_DS", /* name */
1954 FALSE, /* partial_inplace */
1956 0xfffc, /* dst_mask */
1957 FALSE), /* pcrel_offset */
1959 /* Like GOT_TPREL16_DS, but no overflow. */
1960 HOWTO (R_PPC64_GOT_TPREL16_LO_DS,
1962 1, /* size (0 = byte, 1 = short, 2 = long) */
1964 FALSE, /* pc_relative */
1966 complain_overflow_dont, /* complain_on_overflow */
1967 ppc64_elf_unhandled_reloc, /* special_function */
1968 "R_PPC64_GOT_TPREL16_LO_DS", /* name */
1969 FALSE, /* partial_inplace */
1971 0xfffc, /* dst_mask */
1972 FALSE), /* pcrel_offset */
1974 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
1975 HOWTO (R_PPC64_GOT_TPREL16_HI,
1976 16, /* rightshift */
1977 1, /* size (0 = byte, 1 = short, 2 = long) */
1979 FALSE, /* pc_relative */
1981 complain_overflow_signed, /* complain_on_overflow */
1982 ppc64_elf_unhandled_reloc, /* special_function */
1983 "R_PPC64_GOT_TPREL16_HI", /* name */
1984 FALSE, /* partial_inplace */
1986 0xffff, /* dst_mask */
1987 FALSE), /* pcrel_offset */
1989 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
1990 HOWTO (R_PPC64_GOT_TPREL16_HA,
1991 16, /* rightshift */
1992 1, /* size (0 = byte, 1 = short, 2 = long) */
1994 FALSE, /* pc_relative */
1996 complain_overflow_signed, /* complain_on_overflow */
1997 ppc64_elf_unhandled_reloc, /* special_function */
1998 "R_PPC64_GOT_TPREL16_HA", /* name */
1999 FALSE, /* partial_inplace */
2001 0xffff, /* dst_mask */
2002 FALSE), /* pcrel_offset */
2004 HOWTO (R_PPC64_JMP_IREL, /* type */
2006 0, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2008 FALSE, /* pc_relative */
2010 complain_overflow_dont, /* complain_on_overflow */
2011 ppc64_elf_unhandled_reloc, /* special_function */
2012 "R_PPC64_JMP_IREL", /* name */
2013 FALSE, /* partial_inplace */
2016 FALSE), /* pcrel_offset */
2018 HOWTO (R_PPC64_IRELATIVE, /* type */
2020 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2022 FALSE, /* pc_relative */
2024 complain_overflow_dont, /* complain_on_overflow */
2025 bfd_elf_generic_reloc, /* special_function */
2026 "R_PPC64_IRELATIVE", /* name */
2027 FALSE, /* partial_inplace */
2029 ONES (64), /* dst_mask */
2030 FALSE), /* pcrel_offset */
2032 /* A 16 bit relative relocation. */
2033 HOWTO (R_PPC64_REL16, /* type */
2035 1, /* size (0 = byte, 1 = short, 2 = long) */
2037 TRUE, /* pc_relative */
2039 complain_overflow_signed, /* complain_on_overflow */
2040 bfd_elf_generic_reloc, /* special_function */
2041 "R_PPC64_REL16", /* name */
2042 FALSE, /* partial_inplace */
2044 0xffff, /* dst_mask */
2045 TRUE), /* pcrel_offset */
2047 /* A 16 bit relative relocation without overflow. */
2048 HOWTO (R_PPC64_REL16_LO, /* type */
2050 1, /* size (0 = byte, 1 = short, 2 = long) */
2052 TRUE, /* pc_relative */
2054 complain_overflow_dont,/* complain_on_overflow */
2055 bfd_elf_generic_reloc, /* special_function */
2056 "R_PPC64_REL16_LO", /* name */
2057 FALSE, /* partial_inplace */
2059 0xffff, /* dst_mask */
2060 TRUE), /* pcrel_offset */
2062 /* The high order 16 bits of a relative address. */
2063 HOWTO (R_PPC64_REL16_HI, /* type */
2064 16, /* rightshift */
2065 1, /* size (0 = byte, 1 = short, 2 = long) */
2067 TRUE, /* pc_relative */
2069 complain_overflow_signed, /* complain_on_overflow */
2070 bfd_elf_generic_reloc, /* special_function */
2071 "R_PPC64_REL16_HI", /* name */
2072 FALSE, /* partial_inplace */
2074 0xffff, /* dst_mask */
2075 TRUE), /* pcrel_offset */
2077 /* The high order 16 bits of a relative address, plus 1 if the contents of
2078 the low 16 bits, treated as a signed number, is negative. */
2079 HOWTO (R_PPC64_REL16_HA, /* type */
2080 16, /* rightshift */
2081 1, /* size (0 = byte, 1 = short, 2 = long) */
2083 TRUE, /* pc_relative */
2085 complain_overflow_signed, /* complain_on_overflow */
2086 ppc64_elf_ha_reloc, /* special_function */
2087 "R_PPC64_REL16_HA", /* name */
2088 FALSE, /* partial_inplace */
2090 0xffff, /* dst_mask */
2091 TRUE), /* pcrel_offset */
2093 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
2094 HOWTO (R_PPC64_REL16DX_HA, /* type */
2095 16, /* rightshift */
2096 2, /* size (0 = byte, 1 = short, 2 = long) */
2098 TRUE, /* pc_relative */
2100 complain_overflow_signed, /* complain_on_overflow */
2101 ppc64_elf_ha_reloc, /* special_function */
2102 "R_PPC64_REL16DX_HA", /* name */
2103 FALSE, /* partial_inplace */
2105 0x1fffc1, /* dst_mask */
2106 TRUE), /* pcrel_offset */
2108 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
2109 HOWTO (R_PPC64_16DX_HA, /* type */
2110 16, /* rightshift */
2111 2, /* size (0 = byte, 1 = short, 2 = long) */
2113 FALSE, /* pc_relative */
2115 complain_overflow_signed, /* complain_on_overflow */
2116 ppc64_elf_ha_reloc, /* special_function */
2117 "R_PPC64_16DX_HA", /* name */
2118 FALSE, /* partial_inplace */
2120 0x1fffc1, /* dst_mask */
2121 FALSE), /* pcrel_offset */
2123 /* Like R_PPC64_ADDR16_HI, but no overflow. */
2124 HOWTO (R_PPC64_ADDR16_HIGH, /* type */
2125 16, /* rightshift */
2126 1, /* size (0 = byte, 1 = short, 2 = long) */
2128 FALSE, /* pc_relative */
2130 complain_overflow_dont, /* complain_on_overflow */
2131 bfd_elf_generic_reloc, /* special_function */
2132 "R_PPC64_ADDR16_HIGH", /* name */
2133 FALSE, /* partial_inplace */
2135 0xffff, /* dst_mask */
2136 FALSE), /* pcrel_offset */
2138 /* Like R_PPC64_ADDR16_HA, but no overflow. */
2139 HOWTO (R_PPC64_ADDR16_HIGHA, /* type */
2140 16, /* rightshift */
2141 1, /* size (0 = byte, 1 = short, 2 = long) */
2143 FALSE, /* pc_relative */
2145 complain_overflow_dont, /* complain_on_overflow */
2146 ppc64_elf_ha_reloc, /* special_function */
2147 "R_PPC64_ADDR16_HIGHA", /* name */
2148 FALSE, /* partial_inplace */
2150 0xffff, /* dst_mask */
2151 FALSE), /* pcrel_offset */
2153 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
2154 HOWTO (R_PPC64_DTPREL16_HIGH,
2155 16, /* rightshift */
2156 1, /* size (0 = byte, 1 = short, 2 = long) */
2158 FALSE, /* pc_relative */
2160 complain_overflow_dont, /* complain_on_overflow */
2161 ppc64_elf_unhandled_reloc, /* special_function */
2162 "R_PPC64_DTPREL16_HIGH", /* name */
2163 FALSE, /* partial_inplace */
2165 0xffff, /* dst_mask */
2166 FALSE), /* pcrel_offset */
2168 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
2169 HOWTO (R_PPC64_DTPREL16_HIGHA,
2170 16, /* rightshift */
2171 1, /* size (0 = byte, 1 = short, 2 = long) */
2173 FALSE, /* pc_relative */
2175 complain_overflow_dont, /* complain_on_overflow */
2176 ppc64_elf_unhandled_reloc, /* special_function */
2177 "R_PPC64_DTPREL16_HIGHA", /* name */
2178 FALSE, /* partial_inplace */
2180 0xffff, /* dst_mask */
2181 FALSE), /* pcrel_offset */
2183 /* Like R_PPC64_TPREL16_HI, but no overflow. */
2184 HOWTO (R_PPC64_TPREL16_HIGH,
2185 16, /* rightshift */
2186 1, /* size (0 = byte, 1 = short, 2 = long) */
2188 FALSE, /* pc_relative */
2190 complain_overflow_dont, /* complain_on_overflow */
2191 ppc64_elf_unhandled_reloc, /* special_function */
2192 "R_PPC64_TPREL16_HIGH", /* name */
2193 FALSE, /* partial_inplace */
2195 0xffff, /* dst_mask */
2196 FALSE), /* pcrel_offset */
2198 /* Like R_PPC64_TPREL16_HA, but no overflow. */
2199 HOWTO (R_PPC64_TPREL16_HIGHA,
2200 16, /* rightshift */
2201 1, /* size (0 = byte, 1 = short, 2 = long) */
2203 FALSE, /* pc_relative */
2205 complain_overflow_dont, /* complain_on_overflow */
2206 ppc64_elf_unhandled_reloc, /* special_function */
2207 "R_PPC64_TPREL16_HIGHA", /* name */
2208 FALSE, /* partial_inplace */
2210 0xffff, /* dst_mask */
2211 FALSE), /* pcrel_offset */
2213 /* Marker reloc on ELFv2 large-model function entry. */
2214 HOWTO (R_PPC64_ENTRY,
2216 2, /* size (0 = byte, 1 = short, 2 = long) */
2218 FALSE, /* pc_relative */
2220 complain_overflow_dont, /* complain_on_overflow */
2221 bfd_elf_generic_reloc, /* special_function */
2222 "R_PPC64_ENTRY", /* name */
2223 FALSE, /* partial_inplace */
2226 FALSE), /* pcrel_offset */
2228 /* Like ADDR64, but use local entry point of function. */
2229 HOWTO (R_PPC64_ADDR64_LOCAL, /* type */
2231 4, /* size (0=byte, 1=short, 2=long, 4=64 bits) */
2233 FALSE, /* pc_relative */
2235 complain_overflow_dont, /* complain_on_overflow */
2236 bfd_elf_generic_reloc, /* special_function */
2237 "R_PPC64_ADDR64_LOCAL", /* name */
2238 FALSE, /* partial_inplace */
2240 ONES (64), /* dst_mask */
2241 FALSE), /* pcrel_offset */
2243 /* GNU extension to record C++ vtable hierarchy. */
2244 HOWTO (R_PPC64_GNU_VTINHERIT, /* type */
2246 0, /* size (0 = byte, 1 = short, 2 = long) */
2248 FALSE, /* pc_relative */
2250 complain_overflow_dont, /* complain_on_overflow */
2251 NULL, /* special_function */
2252 "R_PPC64_GNU_VTINHERIT", /* name */
2253 FALSE, /* partial_inplace */
2256 FALSE), /* pcrel_offset */
2258 /* GNU extension to record C++ vtable member usage. */
2259 HOWTO (R_PPC64_GNU_VTENTRY, /* type */
2261 0, /* size (0 = byte, 1 = short, 2 = long) */
2263 FALSE, /* pc_relative */
2265 complain_overflow_dont, /* complain_on_overflow */
2266 NULL, /* special_function */
2267 "R_PPC64_GNU_VTENTRY", /* name */
2268 FALSE, /* partial_inplace */
2271 FALSE), /* pcrel_offset */
2275 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
2279 ppc_howto_init (void)
2281 unsigned int i, type;
2283 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2285 type = ppc64_elf_howto_raw[i].type;
2286 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
2287 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
2291 static reloc_howto_type *
2292 ppc64_elf_reloc_type_lookup (bfd *abfd,
2293 bfd_reloc_code_real_type code)
2295 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
2297 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2298 /* Initialize howto table if needed. */
2304 /* xgettext:c-format */
2305 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, (int) code);
2306 bfd_set_error (bfd_error_bad_value);
2309 case BFD_RELOC_NONE: r = R_PPC64_NONE;
2311 case BFD_RELOC_32: r = R_PPC64_ADDR32;
2313 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
2315 case BFD_RELOC_16: r = R_PPC64_ADDR16;
2317 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
2319 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
2321 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
2323 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
2325 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
2327 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
2329 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
2331 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
2333 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
2335 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
2337 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
2339 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
2341 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
2343 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
2345 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
2347 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
2349 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
2351 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
2353 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
2355 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
2357 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
2359 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
2361 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
2363 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
2365 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
2367 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
2369 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
2371 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
2373 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
2375 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
2377 case BFD_RELOC_64: r = R_PPC64_ADDR64;
2379 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
2381 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
2383 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
2385 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
2387 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
2389 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
2391 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
2393 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
2395 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
2397 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
2399 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
2401 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
2403 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
2405 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
2407 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
2409 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
2411 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
2413 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
2415 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
2417 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
2419 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
2421 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
2423 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
2425 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
2427 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
2429 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
2431 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
2433 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
2435 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
2437 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
2439 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
2441 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
2443 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
2445 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
2447 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
2449 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
2451 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
2453 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
2455 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
2457 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
2459 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
2461 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
2463 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
2465 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
2467 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
2469 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
2471 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
2473 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
2475 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
2477 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
2479 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
2481 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
2483 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
2485 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
2487 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
2489 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
2491 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
2493 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
2495 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
2497 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
2499 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
2501 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
2503 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
2505 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
2507 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
2509 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
2511 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
2513 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
2515 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
2517 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
2519 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
2521 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
2523 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
2525 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
2527 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
2529 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
2531 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
2533 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
2535 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
2537 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
2539 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
2541 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
2543 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
2547 return ppc64_elf_howto_table[r];
2550 static reloc_howto_type *
2551 ppc64_elf_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2556 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
2557 if (ppc64_elf_howto_raw[i].name != NULL
2558 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
2559 return &ppc64_elf_howto_raw[i];
2565 /* Set the howto pointer for a PowerPC ELF reloc. */
2568 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
2569 Elf_Internal_Rela *dst)
2573 /* Initialize howto table if needed. */
2574 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
2577 type = ELF64_R_TYPE (dst->r_info);
2578 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
2580 /* xgettext:c-format */
2581 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2583 bfd_set_error (bfd_error_bad_value);
2586 cache_ptr->howto = ppc64_elf_howto_table[type];
2587 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
2589 /* xgettext:c-format */
2590 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2592 bfd_set_error (bfd_error_bad_value);
2599 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
2601 static bfd_reloc_status_type
2602 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2603 void *data, asection *input_section,
2604 bfd *output_bfd, char **error_message)
2606 enum elf_ppc64_reloc_type r_type;
2608 bfd_size_type octets;
2611 /* If this is a relocatable link (output_bfd test tells us), just
2612 call the generic function. Any adjustment will be done at final
2614 if (output_bfd != NULL)
2615 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2616 input_section, output_bfd, error_message);
2618 /* Adjust the addend for sign extension of the low 16 bits.
2619 We won't actually be using the low 16 bits, so trashing them
2621 reloc_entry->addend += 0x8000;
2622 r_type = reloc_entry->howto->type;
2623 if (r_type != R_PPC64_REL16DX_HA)
2624 return bfd_reloc_continue;
2627 if (!bfd_is_com_section (symbol->section))
2628 value = symbol->value;
2629 value += (reloc_entry->addend
2630 + symbol->section->output_offset
2631 + symbol->section->output_section->vma);
2632 value -= (reloc_entry->address
2633 + input_section->output_offset
2634 + input_section->output_section->vma);
2635 value = (bfd_signed_vma) value >> 16;
2637 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2638 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2640 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
2641 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2642 if (value + 0x8000 > 0xffff)
2643 return bfd_reloc_overflow;
2644 return bfd_reloc_ok;
2647 static bfd_reloc_status_type
2648 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2649 void *data, asection *input_section,
2650 bfd *output_bfd, char **error_message)
2652 if (output_bfd != NULL)
2653 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2654 input_section, output_bfd, error_message);
2656 if (strcmp (symbol->section->name, ".opd") == 0
2657 && (symbol->section->owner->flags & DYNAMIC) == 0)
2659 bfd_vma dest = opd_entry_value (symbol->section,
2660 symbol->value + reloc_entry->addend,
2662 if (dest != (bfd_vma) -1)
2663 reloc_entry->addend = dest - (symbol->value
2664 + symbol->section->output_section->vma
2665 + symbol->section->output_offset);
2669 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
2671 if (symbol->section->owner != abfd
2672 && symbol->section->owner != NULL
2673 && abiversion (symbol->section->owner) >= 2)
2677 for (i = 0; i < symbol->section->owner->symcount; ++i)
2679 asymbol *symdef = symbol->section->owner->outsymbols[i];
2681 if (strcmp (symdef->name, symbol->name) == 0)
2683 elfsym = (elf_symbol_type *) symdef;
2689 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
2691 return bfd_reloc_continue;
2694 static bfd_reloc_status_type
2695 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2696 void *data, asection *input_section,
2697 bfd *output_bfd, char **error_message)
2700 enum elf_ppc64_reloc_type r_type;
2701 bfd_size_type octets;
2702 /* Assume 'at' branch hints. */
2703 bfd_boolean is_isa_v2 = TRUE;
2705 /* If this is a relocatable link (output_bfd test tells us), just
2706 call the generic function. Any adjustment will be done at final
2708 if (output_bfd != NULL)
2709 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2710 input_section, output_bfd, error_message);
2712 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2713 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
2714 insn &= ~(0x01 << 21);
2715 r_type = reloc_entry->howto->type;
2716 if (r_type == R_PPC64_ADDR14_BRTAKEN
2717 || r_type == R_PPC64_REL14_BRTAKEN)
2718 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
2722 /* Set 'a' bit. This is 0b00010 in BO field for branch
2723 on CR(BI) insns (BO == 001at or 011at), and 0b01000
2724 for branch on CTR insns (BO == 1a00t or 1a01t). */
2725 if ((insn & (0x14 << 21)) == (0x04 << 21))
2727 else if ((insn & (0x14 << 21)) == (0x10 << 21))
2737 if (!bfd_is_com_section (symbol->section))
2738 target = symbol->value;
2739 target += symbol->section->output_section->vma;
2740 target += symbol->section->output_offset;
2741 target += reloc_entry->addend;
2743 from = (reloc_entry->address
2744 + input_section->output_offset
2745 + input_section->output_section->vma);
2747 /* Invert 'y' bit if not the default. */
2748 if ((bfd_signed_vma) (target - from) < 0)
2751 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
2753 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
2754 input_section, output_bfd, error_message);
2757 static bfd_reloc_status_type
2758 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2759 void *data, asection *input_section,
2760 bfd *output_bfd, char **error_message)
2762 /* If this is a relocatable link (output_bfd test tells us), just
2763 call the generic function. Any adjustment will be done at final
2765 if (output_bfd != NULL)
2766 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2767 input_section, output_bfd, error_message);
2769 /* Subtract the symbol section base address. */
2770 reloc_entry->addend -= symbol->section->output_section->vma;
2771 return bfd_reloc_continue;
2774 static bfd_reloc_status_type
2775 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2776 void *data, asection *input_section,
2777 bfd *output_bfd, char **error_message)
2779 /* If this is a relocatable link (output_bfd test tells us), just
2780 call the generic function. Any adjustment will be done at final
2782 if (output_bfd != NULL)
2783 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2784 input_section, output_bfd, error_message);
2786 /* Subtract the symbol section base address. */
2787 reloc_entry->addend -= symbol->section->output_section->vma;
2789 /* Adjust the addend for sign extension of the low 16 bits. */
2790 reloc_entry->addend += 0x8000;
2791 return bfd_reloc_continue;
2794 static bfd_reloc_status_type
2795 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2796 void *data, asection *input_section,
2797 bfd *output_bfd, char **error_message)
2801 /* If this is a relocatable link (output_bfd test tells us), just
2802 call the generic function. Any adjustment will be done at final
2804 if (output_bfd != NULL)
2805 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2806 input_section, output_bfd, error_message);
2808 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2810 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2812 /* Subtract the TOC base address. */
2813 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2814 return bfd_reloc_continue;
2817 static bfd_reloc_status_type
2818 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2819 void *data, asection *input_section,
2820 bfd *output_bfd, char **error_message)
2824 /* If this is a relocatable link (output_bfd test tells us), just
2825 call the generic function. Any adjustment will be done at final
2827 if (output_bfd != NULL)
2828 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2829 input_section, output_bfd, error_message);
2831 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2833 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2835 /* Subtract the TOC base address. */
2836 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
2838 /* Adjust the addend for sign extension of the low 16 bits. */
2839 reloc_entry->addend += 0x8000;
2840 return bfd_reloc_continue;
2843 static bfd_reloc_status_type
2844 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2845 void *data, asection *input_section,
2846 bfd *output_bfd, char **error_message)
2849 bfd_size_type octets;
2851 /* If this is a relocatable link (output_bfd test tells us), just
2852 call the generic function. Any adjustment will be done at final
2854 if (output_bfd != NULL)
2855 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2856 input_section, output_bfd, error_message);
2858 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
2860 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
2862 octets = reloc_entry->address * bfd_octets_per_byte (abfd);
2863 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
2864 return bfd_reloc_ok;
2867 static bfd_reloc_status_type
2868 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
2869 void *data, asection *input_section,
2870 bfd *output_bfd, char **error_message)
2872 /* If this is a relocatable link (output_bfd test tells us), just
2873 call the generic function. Any adjustment will be done at final
2875 if (output_bfd != NULL)
2876 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
2877 input_section, output_bfd, error_message);
2879 if (error_message != NULL)
2881 static char buf[60];
2882 sprintf (buf, "generic linker can't handle %s",
2883 reloc_entry->howto->name);
2884 *error_message = buf;
2886 return bfd_reloc_dangerous;
2889 /* Track GOT entries needed for a given symbol. We might need more
2890 than one got entry per symbol. */
2893 struct got_entry *next;
2895 /* The symbol addend that we'll be placing in the GOT. */
2898 /* Unlike other ELF targets, we use separate GOT entries for the same
2899 symbol referenced from different input files. This is to support
2900 automatic multiple TOC/GOT sections, where the TOC base can vary
2901 from one input file to another. After partitioning into TOC groups
2902 we merge entries within the group.
2904 Point to the BFD owning this GOT entry. */
2907 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
2908 TLS_TPREL or TLS_DTPREL for tls entries. */
2909 unsigned char tls_type;
2911 /* Non-zero if got.ent points to real entry. */
2912 unsigned char is_indirect;
2914 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
2917 bfd_signed_vma refcount;
2919 struct got_entry *ent;
2923 /* The same for PLT. */
2926 struct plt_entry *next;
2932 bfd_signed_vma refcount;
2937 struct ppc64_elf_obj_tdata
2939 struct elf_obj_tdata elf;
2941 /* Shortcuts to dynamic linker sections. */
2945 /* Used during garbage collection. We attach global symbols defined
2946 on removed .opd entries to this section so that the sym is removed. */
2947 asection *deleted_section;
2949 /* TLS local dynamic got entry handling. Support for multiple GOT
2950 sections means we potentially need one of these for each input bfd. */
2951 struct got_entry tlsld_got;
2954 /* A copy of relocs before they are modified for --emit-relocs. */
2955 Elf_Internal_Rela *relocs;
2957 /* Section contents. */
2961 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
2962 the reloc to be in the range -32768 to 32767. */
2963 unsigned int has_small_toc_reloc : 1;
2965 /* Set if toc/got ha relocs detected not using r2, or lo reloc
2966 instruction not one we handle. */
2967 unsigned int unexpected_toc_insn : 1;
2970 #define ppc64_elf_tdata(bfd) \
2971 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
2973 #define ppc64_tlsld_got(bfd) \
2974 (&ppc64_elf_tdata (bfd)->tlsld_got)
2976 #define is_ppc64_elf(bfd) \
2977 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2978 && elf_object_id (bfd) == PPC64_ELF_DATA)
2980 /* Override the generic function because we store some extras. */
2983 ppc64_elf_mkobject (bfd *abfd)
2985 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
2989 /* Fix bad default arch selected for a 64 bit input bfd when the
2990 default is 32 bit. Also select arch based on apuinfo. */
2993 ppc64_elf_object_p (bfd *abfd)
2995 if (!abfd->arch_info->the_default)
2998 if (abfd->arch_info->bits_per_word == 32)
3000 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
3002 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
3004 /* Relies on arch after 32 bit default being 64 bit default. */
3005 abfd->arch_info = abfd->arch_info->next;
3006 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
3009 return _bfd_elf_ppc_set_arch (abfd);
3012 /* Support for core dump NOTE sections. */
3015 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3017 size_t offset, size;
3019 if (note->descsz != 504)
3023 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3026 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
3032 /* Make a ".reg/999" section. */
3033 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3034 size, note->descpos + offset);
3038 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3040 if (note->descsz != 136)
3043 elf_tdata (abfd)->core->pid
3044 = bfd_get_32 (abfd, note->descdata + 24);
3045 elf_tdata (abfd)->core->program
3046 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
3047 elf_tdata (abfd)->core->command
3048 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
3054 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
3064 char data[136] ATTRIBUTE_NONSTRING;
3067 va_start (ap, note_type);
3068 memset (data, 0, sizeof (data));
3069 strncpy (data + 40, va_arg (ap, const char *), 16);
3070 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3072 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
3073 -Wstringop-truncation:
3074 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
3076 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
3078 strncpy (data + 56, va_arg (ap, const char *), 80);
3079 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
3083 return elfcore_write_note (abfd, buf, bufsiz,
3084 "CORE", note_type, data, sizeof (data));
3095 va_start (ap, note_type);
3096 memset (data, 0, 112);
3097 pid = va_arg (ap, long);
3098 bfd_put_32 (abfd, pid, data + 32);
3099 cursig = va_arg (ap, int);
3100 bfd_put_16 (abfd, cursig, data + 12);
3101 greg = va_arg (ap, const void *);
3102 memcpy (data + 112, greg, 384);
3103 memset (data + 496, 0, 8);
3105 return elfcore_write_note (abfd, buf, bufsiz,
3106 "CORE", note_type, data, sizeof (data));
3111 /* Add extra PPC sections. */
3113 static const struct bfd_elf_special_section ppc64_elf_special_sections[]=
3115 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
3116 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3117 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3118 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3119 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
3120 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
3121 { NULL, 0, 0, 0, 0 }
3124 enum _ppc64_sec_type {
3130 struct _ppc64_elf_section_data
3132 struct bfd_elf_section_data elf;
3136 /* An array with one entry for each opd function descriptor,
3137 and some spares since opd entries may be either 16 or 24 bytes. */
3138 #define OPD_NDX(OFF) ((OFF) >> 4)
3139 struct _opd_sec_data
3141 /* Points to the function code section for local opd entries. */
3142 asection **func_sec;
3144 /* After editing .opd, adjust references to opd local syms. */
3148 /* An array for toc sections, indexed by offset/8. */
3149 struct _toc_sec_data
3151 /* Specifies the relocation symbol index used at a given toc offset. */
3154 /* And the relocation addend. */
3159 enum _ppc64_sec_type sec_type:2;
3161 /* Flag set when small branches are detected. Used to
3162 select suitable defaults for the stub group size. */
3163 unsigned int has_14bit_branch:1;
3165 /* Flag set when PLTCALL relocs are detected. */
3166 unsigned int has_pltcall:1;
3169 #define ppc64_elf_section_data(sec) \
3170 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
3173 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
3175 if (!sec->used_by_bfd)
3177 struct _ppc64_elf_section_data *sdata;
3178 bfd_size_type amt = sizeof (*sdata);
3180 sdata = bfd_zalloc (abfd, amt);
3183 sec->used_by_bfd = sdata;
3186 return _bfd_elf_new_section_hook (abfd, sec);
3189 static struct _opd_sec_data *
3190 get_opd_info (asection * sec)
3193 && ppc64_elf_section_data (sec) != NULL
3194 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
3195 return &ppc64_elf_section_data (sec)->u.opd;
3199 /* Parameters for the qsort hook. */
3200 static bfd_boolean synthetic_relocatable;
3201 static asection *synthetic_opd;
3203 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
3206 compare_symbols (const void *ap, const void *bp)
3208 const asymbol *a = * (const asymbol **) ap;
3209 const asymbol *b = * (const asymbol **) bp;
3211 /* Section symbols first. */
3212 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
3214 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
3217 /* then .opd symbols. */
3218 if (synthetic_opd != NULL)
3220 if (strcmp (a->section->name, ".opd") == 0
3221 && strcmp (b->section->name, ".opd") != 0)
3223 if (strcmp (a->section->name, ".opd") != 0
3224 && strcmp (b->section->name, ".opd") == 0)
3228 /* then other code symbols. */
3229 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3230 == (SEC_CODE | SEC_ALLOC)
3231 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3232 != (SEC_CODE | SEC_ALLOC))
3235 if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3236 != (SEC_CODE | SEC_ALLOC)
3237 && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3238 == (SEC_CODE | SEC_ALLOC))
3241 if (synthetic_relocatable)
3243 if (a->section->id < b->section->id)
3246 if (a->section->id > b->section->id)
3250 if (a->value + a->section->vma < b->value + b->section->vma)
3253 if (a->value + a->section->vma > b->value + b->section->vma)
3256 /* For syms with the same value, prefer strong dynamic global function
3257 syms over other syms. */
3258 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
3261 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
3264 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
3267 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
3270 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
3273 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
3276 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
3279 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
3285 /* Search SYMS for a symbol of the given VALUE. */
3288 sym_exists_at (asymbol **syms, long lo, long hi, unsigned int id, bfd_vma value)
3292 if (id == (unsigned) -1)
3296 mid = (lo + hi) >> 1;
3297 if (syms[mid]->value + syms[mid]->section->vma < value)
3299 else if (syms[mid]->value + syms[mid]->section->vma > value)
3309 mid = (lo + hi) >> 1;
3310 if (syms[mid]->section->id < id)
3312 else if (syms[mid]->section->id > id)
3314 else if (syms[mid]->value < value)
3316 else if (syms[mid]->value > value)
3326 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
3328 bfd_vma vma = *(bfd_vma *) ptr;
3329 return ((section->flags & SEC_ALLOC) != 0
3330 && section->vma <= vma
3331 && vma < section->vma + section->size);
3334 /* Create synthetic symbols, effectively restoring "dot-symbol" function
3335 entry syms. Also generate @plt symbols for the glink branch table.
3336 Returns count of synthetic symbols in RET or -1 on error. */
3339 ppc64_elf_get_synthetic_symtab (bfd *abfd,
3340 long static_count, asymbol **static_syms,
3341 long dyn_count, asymbol **dyn_syms,
3347 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
3348 asection *opd = NULL;
3349 bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
3351 int abi = abiversion (abfd);
3357 opd = bfd_get_section_by_name (abfd, ".opd");
3358 if (opd == NULL && abi == 1)
3370 symcount = static_count;
3372 symcount += dyn_count;
3376 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
3380 if (!relocatable && static_count != 0 && dyn_count != 0)
3382 /* Use both symbol tables. */
3383 memcpy (syms, static_syms, static_count * sizeof (*syms));
3384 memcpy (syms + static_count, dyn_syms,
3385 (dyn_count + 1) * sizeof (*syms));
3387 else if (!relocatable && static_count == 0)
3388 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
3390 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
3392 /* Trim uninteresting symbols. Interesting symbols are section,
3393 function, and notype symbols. */
3394 for (i = 0, j = 0; i < symcount; ++i)
3395 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
3396 | BSF_RELC | BSF_SRELC)) == 0)
3397 syms[j++] = syms[i];
3400 synthetic_relocatable = relocatable;
3401 synthetic_opd = opd;
3402 qsort (syms, symcount, sizeof (*syms), compare_symbols);
3404 if (!relocatable && symcount > 1)
3406 /* Trim duplicate syms, since we may have merged the normal
3407 and dynamic symbols. Actually, we only care about syms
3408 that have different values, so trim any with the same
3409 value. Don't consider ifunc and ifunc resolver symbols
3410 duplicates however, because GDB wants to know whether a
3411 text symbol is an ifunc resolver. */
3412 for (i = 1, j = 1; i < symcount; ++i)
3414 const asymbol *s0 = syms[i - 1];
3415 const asymbol *s1 = syms[i];
3417 if ((s0->value + s0->section->vma
3418 != s1->value + s1->section->vma)
3419 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
3420 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
3421 syms[j++] = syms[i];
3427 /* Note that here and in compare_symbols we can't compare opd and
3428 sym->section directly. With separate debug info files, the
3429 symbols will be extracted from the debug file while abfd passed
3430 to this function is the real binary. */
3431 if (strcmp (syms[i]->section->name, ".opd") == 0)
3435 for (; i < symcount; ++i)
3436 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
3437 | SEC_THREAD_LOCAL))
3438 != (SEC_CODE | SEC_ALLOC))
3439 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
3443 for (; i < symcount; ++i)
3444 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
3448 for (; i < symcount; ++i)
3449 if (strcmp (syms[i]->section->name, ".opd") != 0)
3453 for (; i < symcount; ++i)
3454 if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
3455 != (SEC_CODE | SEC_ALLOC))
3463 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3468 if (opdsymend == secsymend)
3471 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3472 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
3476 if (!(*slurp_relocs) (abfd, opd, static_syms, FALSE))
3483 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3487 while (r < opd->relocation + relcount
3488 && r->address < syms[i]->value + opd->vma)
3491 if (r == opd->relocation + relcount)
3494 if (r->address != syms[i]->value + opd->vma)
3497 if (r->howto->type != R_PPC64_ADDR64)
3500 sym = *r->sym_ptr_ptr;
3501 if (!sym_exists_at (syms, opdsymend, symcount,
3502 sym->section->id, sym->value + r->addend))
3505 size += sizeof (asymbol);
3506 size += strlen (syms[i]->name) + 2;
3512 s = *ret = bfd_malloc (size);
3519 names = (char *) (s + count);
3521 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
3525 while (r < opd->relocation + relcount
3526 && r->address < syms[i]->value + opd->vma)
3529 if (r == opd->relocation + relcount)
3532 if (r->address != syms[i]->value + opd->vma)
3535 if (r->howto->type != R_PPC64_ADDR64)
3538 sym = *r->sym_ptr_ptr;
3539 if (!sym_exists_at (syms, opdsymend, symcount,
3540 sym->section->id, sym->value + r->addend))
3545 s->flags |= BSF_SYNTHETIC;
3546 s->section = sym->section;
3547 s->value = sym->value + r->addend;
3550 len = strlen (syms[i]->name);
3551 memcpy (names, syms[i]->name, len + 1);
3553 /* Have udata.p point back to the original symbol this
3554 synthetic symbol was derived from. */
3555 s->udata.p = syms[i];
3562 bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
3563 bfd_byte *contents = NULL;
3565 size_t plt_count = 0;
3566 bfd_vma glink_vma = 0, resolv_vma = 0;
3567 asection *dynamic, *glink = NULL, *relplt = NULL;
3570 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
3572 free_contents_and_exit_err:
3574 free_contents_and_exit:
3581 for (i = secsymend; i < opdsymend; ++i)
3585 /* Ignore bogus symbols. */
3586 if (syms[i]->value > opd->size - 8)
3589 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3590 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3593 size += sizeof (asymbol);
3594 size += strlen (syms[i]->name) + 2;
3598 /* Get start of .glink stubs from DT_PPC64_GLINK. */
3600 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
3602 bfd_byte *dynbuf, *extdyn, *extdynend;
3604 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
3606 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
3607 goto free_contents_and_exit_err;
3609 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
3610 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
3613 extdynend = extdyn + dynamic->size;
3614 for (; extdyn < extdynend; extdyn += extdynsize)
3616 Elf_Internal_Dyn dyn;
3617 (*swap_dyn_in) (abfd, extdyn, &dyn);
3619 if (dyn.d_tag == DT_NULL)
3622 if (dyn.d_tag == DT_PPC64_GLINK)
3624 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
3625 See comment in ppc64_elf_finish_dynamic_sections. */
3626 glink_vma = dyn.d_un.d_val + 8 * 4;
3627 /* The .glink section usually does not survive the final
3628 link; search for the section (usually .text) where the
3629 glink stubs now reside. */
3630 glink = bfd_sections_find_if (abfd, section_covers_vma,
3641 /* Determine __glink trampoline by reading the relative branch
3642 from the first glink stub. */
3644 unsigned int off = 0;
3646 while (bfd_get_section_contents (abfd, glink, buf,
3647 glink_vma + off - glink->vma, 4))
3649 unsigned int insn = bfd_get_32 (abfd, buf);
3651 if ((insn & ~0x3fffffc) == 0)
3653 resolv_vma = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
3662 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
3664 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
3667 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
3668 if (! (*slurp_relocs) (abfd, relplt, dyn_syms, TRUE))
3669 goto free_contents_and_exit_err;
3671 plt_count = relplt->size / sizeof (Elf64_External_Rela);
3672 size += plt_count * sizeof (asymbol);
3674 p = relplt->relocation;
3675 for (i = 0; i < plt_count; i++, p++)
3677 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
3679 size += sizeof ("+0x") - 1 + 16;
3685 goto free_contents_and_exit;
3686 s = *ret = bfd_malloc (size);
3688 goto free_contents_and_exit_err;
3690 names = (char *) (s + count + plt_count + (resolv_vma != 0));
3692 for (i = secsymend; i < opdsymend; ++i)
3696 if (syms[i]->value > opd->size - 8)
3699 ent = bfd_get_64 (abfd, contents + syms[i]->value);
3700 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
3704 asection *sec = abfd->sections;
3711 size_t mid = (lo + hi) >> 1;
3712 if (syms[mid]->section->vma < ent)
3714 else if (syms[mid]->section->vma > ent)
3718 sec = syms[mid]->section;
3723 if (lo >= hi && lo > codesecsym)
3724 sec = syms[lo - 1]->section;
3726 for (; sec != NULL; sec = sec->next)
3730 /* SEC_LOAD may not be set if SEC is from a separate debug
3732 if ((sec->flags & SEC_ALLOC) == 0)
3734 if ((sec->flags & SEC_CODE) != 0)
3737 s->flags |= BSF_SYNTHETIC;
3738 s->value = ent - s->section->vma;
3741 len = strlen (syms[i]->name);
3742 memcpy (names, syms[i]->name, len + 1);
3744 /* Have udata.p point back to the original symbol this
3745 synthetic symbol was derived from. */
3746 s->udata.p = syms[i];
3752 if (glink != NULL && relplt != NULL)
3756 /* Add a symbol for the main glink trampoline. */
3757 memset (s, 0, sizeof *s);
3759 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
3761 s->value = resolv_vma - glink->vma;
3763 memcpy (names, "__glink_PLTresolve", sizeof ("__glink_PLTresolve"));
3764 names += sizeof ("__glink_PLTresolve");
3769 /* FIXME: It would be very much nicer to put sym@plt on the
3770 stub rather than on the glink branch table entry. The
3771 objdump disassembler would then use a sensible symbol
3772 name on plt calls. The difficulty in doing so is
3773 a) finding the stubs, and,
3774 b) matching stubs against plt entries, and,
3775 c) there can be multiple stubs for a given plt entry.
3777 Solving (a) could be done by code scanning, but older
3778 ppc64 binaries used different stubs to current code.
3779 (b) is the tricky one since you need to known the toc
3780 pointer for at least one function that uses a pic stub to
3781 be able to calculate the plt address referenced.
3782 (c) means gdb would need to set multiple breakpoints (or
3783 find the glink branch itself) when setting breakpoints
3784 for pending shared library loads. */
3785 p = relplt->relocation;
3786 for (i = 0; i < plt_count; i++, p++)
3790 *s = **p->sym_ptr_ptr;
3791 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
3792 we are defining a symbol, ensure one of them is set. */
3793 if ((s->flags & BSF_LOCAL) == 0)
3794 s->flags |= BSF_GLOBAL;
3795 s->flags |= BSF_SYNTHETIC;
3797 s->value = glink_vma - glink->vma;
3800 len = strlen ((*p->sym_ptr_ptr)->name);
3801 memcpy (names, (*p->sym_ptr_ptr)->name, len);
3805 memcpy (names, "+0x", sizeof ("+0x") - 1);
3806 names += sizeof ("+0x") - 1;
3807 bfd_sprintf_vma (abfd, names, p->addend);
3808 names += strlen (names);
3810 memcpy (names, "@plt", sizeof ("@plt"));
3811 names += sizeof ("@plt");
3831 /* The following functions are specific to the ELF linker, while
3832 functions above are used generally. Those named ppc64_elf_* are
3833 called by the main ELF linker code. They appear in this file more
3834 or less in the order in which they are called. eg.
3835 ppc64_elf_check_relocs is called early in the link process,
3836 ppc64_elf_finish_dynamic_sections is one of the last functions
3839 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
3840 functions have both a function code symbol and a function descriptor
3841 symbol. A call to foo in a relocatable object file looks like:
3848 The function definition in another object file might be:
3852 . .quad .TOC.@tocbase
3858 When the linker resolves the call during a static link, the branch
3859 unsurprisingly just goes to .foo and the .opd information is unused.
3860 If the function definition is in a shared library, things are a little
3861 different: The call goes via a plt call stub, the opd information gets
3862 copied to the plt, and the linker patches the nop.
3870 . std 2,40(1) # in practice, the call stub
3871 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
3872 . addi 11,11,Lfoo@toc@l # this is the general idea
3880 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
3882 The "reloc ()" notation is supposed to indicate that the linker emits
3883 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
3886 What are the difficulties here? Well, firstly, the relocations
3887 examined by the linker in check_relocs are against the function code
3888 sym .foo, while the dynamic relocation in the plt is emitted against
3889 the function descriptor symbol, foo. Somewhere along the line, we need
3890 to carefully copy dynamic link information from one symbol to the other.
3891 Secondly, the generic part of the elf linker will make .foo a dynamic
3892 symbol as is normal for most other backends. We need foo dynamic
3893 instead, at least for an application final link. However, when
3894 creating a shared library containing foo, we need to have both symbols
3895 dynamic so that references to .foo are satisfied during the early
3896 stages of linking. Otherwise the linker might decide to pull in a
3897 definition from some other object, eg. a static library.
3899 Update: As of August 2004, we support a new convention. Function
3900 calls may use the function descriptor symbol, ie. "bl foo". This
3901 behaves exactly as "bl .foo". */
3903 /* Of those relocs that might be copied as dynamic relocs, this
3904 function selects those that must be copied when linking a shared
3905 library or PIE, even when the symbol is local. */
3908 must_be_dyn_reloc (struct bfd_link_info *info,
3909 enum elf_ppc64_reloc_type r_type)
3914 /* Only relative relocs can be resolved when the object load
3915 address isn't fixed. DTPREL64 is excluded because the
3916 dynamic linker needs to differentiate global dynamic from
3917 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
3925 case R_PPC64_TPREL16:
3926 case R_PPC64_TPREL16_LO:
3927 case R_PPC64_TPREL16_HI:
3928 case R_PPC64_TPREL16_HA:
3929 case R_PPC64_TPREL16_DS:
3930 case R_PPC64_TPREL16_LO_DS:
3931 case R_PPC64_TPREL16_HIGH:
3932 case R_PPC64_TPREL16_HIGHA:
3933 case R_PPC64_TPREL16_HIGHER:
3934 case R_PPC64_TPREL16_HIGHERA:
3935 case R_PPC64_TPREL16_HIGHEST:
3936 case R_PPC64_TPREL16_HIGHESTA:
3937 case R_PPC64_TPREL64:
3938 /* These relocations are relative but in a shared library the
3939 linker doesn't know the thread pointer base. */
3940 return bfd_link_dll (info);
3944 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
3945 copying dynamic variables from a shared lib into an app's dynbss
3946 section, and instead use a dynamic relocation to point into the
3947 shared lib. With code that gcc generates, it's vital that this be
3948 enabled; In the PowerPC64 ABI, the address of a function is actually
3949 the address of a function descriptor, which resides in the .opd
3950 section. gcc uses the descriptor directly rather than going via the
3951 GOT as some other ABI's do, which means that initialized function
3952 pointers must reference the descriptor. Thus, a function pointer
3953 initialized to the address of a function in a shared library will
3954 either require a copy reloc, or a dynamic reloc. Using a copy reloc
3955 redefines the function descriptor symbol to point to the copy. This
3956 presents a problem as a plt entry for that function is also
3957 initialized from the function descriptor symbol and the copy reloc
3958 may not be initialized first. */
3959 #define ELIMINATE_COPY_RELOCS 1
3961 /* Section name for stubs is the associated section name plus this
3963 #define STUB_SUFFIX ".stub"
3966 ppc_stub_long_branch:
3967 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
3968 destination, but a 24 bit branch in a stub section will reach.
3971 ppc_stub_plt_branch:
3972 Similar to the above, but a 24 bit branch in the stub section won't
3973 reach its destination.
3974 . addis %r11,%r2,xxx@toc@ha
3975 . ld %r12,xxx@toc@l(%r11)
3980 Used to call a function in a shared library. If it so happens that
3981 the plt entry referenced crosses a 64k boundary, then an extra
3982 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
3983 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
3984 . addis %r11,%r2,xxx@toc@ha
3985 . ld %r12,xxx+0@toc@l(%r11)
3987 . ld %r2,xxx+8@toc@l(%r11)
3988 . ld %r11,xxx+16@toc@l(%r11)
3991 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
3992 code to adjust the value and save r2 to support multiple toc sections.
3993 A ppc_stub_long_branch with an r2 offset looks like:
3995 . addis %r2,%r2,off@ha
3996 . addi %r2,%r2,off@l
3999 A ppc_stub_plt_branch with an r2 offset looks like:
4001 . addis %r11,%r2,xxx@toc@ha
4002 . ld %r12,xxx@toc@l(%r11)
4003 . addis %r2,%r2,off@ha
4004 . addi %r2,%r2,off@l
4008 All of the above stubs are shown as their ELFv1 variants. ELFv2
4009 variants exist too, simpler for plt calls since a new toc pointer
4010 and static chain are not loaded by the stub. In addition, ELFv2
4011 has some more complex stubs to handle calls marked with NOTOC
4012 relocs from functions where r2 is not a valid toc pointer. These
4013 come in two flavours, the ones shown below, and _both variants that
4014 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
4015 one call is from a function where r2 is used as the toc pointer but
4016 needs a toc adjusting stub for small-model multi-toc, and another
4017 call is from a function where r2 is not valid.
4018 ppc_stub_long_branch_notoc:
4024 . lis %r12,xxx-1b@highest
4025 . ori %r12,xxx-1b@higher
4027 . oris %r12,%r12,xxx-1b@hi
4028 . ori %r12,%r12,xxx-1b@l
4029 . add %r12,%r11,%r12
4032 ppc_stub_plt_branch_notoc:
4038 . lis %r12,xxx-1b@highest
4039 . ori %r12,xxx-1b@higher
4041 . oris %r12,%r12,xxx-1b@hi
4042 . ori %r12,%r12,xxx-1b@l
4043 . add %r12,%r11,%r12
4047 ppc_stub_plt_call_notoc:
4053 . lis %r12,xxx-1b@highest
4054 . ori %r12,xxx-1b@higher
4056 . oris %r12,%r12,xxx-1b@hi
4057 . ori %r12,%r12,xxx-1b@l
4058 . ldx %r12,%r11,%r12
4062 In cases where the high instructions would add zero, they are
4063 omitted and following instructions modified in some cases.
4065 For a given stub group (a set of sections all using the same toc
4066 pointer value) there will be just one stub type used for any
4067 particular function symbol. For example, if printf is called from
4068 code with the tocsave optimization (ie. r2 saved in function
4069 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
4070 and from other code without the tocsave optimization requiring a
4071 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
4072 type will be created. Calls with the tocsave optimization will
4073 enter this stub after the instruction saving r2. A similar
4074 situation exists when calls are marked with R_PPC64_REL24_NOTOC
4075 relocations. These require a ppc_stub_plt_call_notoc linkage stub
4076 to call an external function like printf. If other calls to printf
4077 require a ppc_stub_plt_call linkage stub then a single
4078 ppc_stub_plt_call_notoc linkage stub will be used for both types of
4079 call. If other calls to printf require a ppc_stub_plt_call_r2save
4080 linkage stub then a single ppc_stub_plt_call_both linkage stub will
4081 be created and calls not requiring r2 to be saved will enter the
4082 stub after the r2 save instruction. There is an analogous
4083 hierarchy of long branch and plt branch stubs for local call
4086 enum ppc_stub_type {
4088 ppc_stub_long_branch,
4089 ppc_stub_long_branch_r2off,
4090 ppc_stub_long_branch_notoc,
4091 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
4092 ppc_stub_plt_branch,
4093 ppc_stub_plt_branch_r2off,
4094 ppc_stub_plt_branch_notoc,
4095 ppc_stub_plt_branch_both,
4097 ppc_stub_plt_call_r2save,
4098 ppc_stub_plt_call_notoc,
4099 ppc_stub_plt_call_both,
4100 ppc_stub_global_entry,
4104 /* Information on stub grouping. */
4107 /* The stub section. */
4109 /* This is the section to which stubs in the group will be attached. */
4112 struct map_stub *next;
4113 /* Whether to emit a copy of register save/restore functions in this
4116 /* The offset of the __tls_get_addr_opt plt stub bctrl in this group,
4117 or -1u if no such stub with bctrl exists. */
4118 unsigned int tls_get_addr_opt_bctrl;
4121 struct ppc_stub_hash_entry {
4123 /* Base hash table entry structure. */
4124 struct bfd_hash_entry root;
4126 enum ppc_stub_type stub_type;
4128 /* Group information. */
4129 struct map_stub *group;
4131 /* Offset within stub_sec of the beginning of this stub. */
4132 bfd_vma stub_offset;
4134 /* Given the symbol's value and its section we can determine its final
4135 value when building the stubs (so the stub knows where to jump. */
4136 bfd_vma target_value;
4137 asection *target_section;
4139 /* The symbol table entry, if any, that this was derived from. */
4140 struct ppc_link_hash_entry *h;
4141 struct plt_entry *plt_ent;
4144 unsigned char symtype;
4146 /* Symbol st_other. */
4147 unsigned char other;
4150 struct ppc_branch_hash_entry {
4152 /* Base hash table entry structure. */
4153 struct bfd_hash_entry root;
4155 /* Offset within branch lookup table. */
4156 unsigned int offset;
4158 /* Generation marker. */
4162 /* Used to track dynamic relocations for local symbols. */
4163 struct ppc_dyn_relocs
4165 struct ppc_dyn_relocs *next;
4167 /* The input section of the reloc. */
4170 /* Total number of relocs copied for the input section. */
4171 unsigned int count : 31;
4173 /* Whether this entry is for STT_GNU_IFUNC symbols. */
4174 unsigned int ifunc : 1;
4177 struct ppc_link_hash_entry
4179 struct elf_link_hash_entry elf;
4182 /* A pointer to the most recently used stub hash entry against this
4184 struct ppc_stub_hash_entry *stub_cache;
4186 /* A pointer to the next symbol starting with a '.' */
4187 struct ppc_link_hash_entry *next_dot_sym;
4190 /* Track dynamic relocs copied for this symbol. */
4191 struct elf_dyn_relocs *dyn_relocs;
4193 /* Link between function code and descriptor symbols. */
4194 struct ppc_link_hash_entry *oh;
4196 /* Flag function code and descriptor symbols. */
4197 unsigned int is_func:1;
4198 unsigned int is_func_descriptor:1;
4199 unsigned int fake:1;
4201 /* Whether global opd/toc sym has been adjusted or not.
4202 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
4203 should be set for all globals defined in any opd/toc section. */
4204 unsigned int adjust_done:1;
4206 /* Set if this is an out-of-line register save/restore function,
4207 with non-standard calling convention. */
4208 unsigned int save_res:1;
4210 /* Set if a duplicate symbol with non-zero localentry is detected,
4211 even when the duplicate symbol does not provide a definition. */
4212 unsigned int non_zero_localentry:1;
4214 /* Contexts in which symbol is used in the GOT (or TOC).
4215 Bits are or'd into the mask as the corresponding relocs are
4216 encountered during check_relocs, with TLS_TLS being set when any
4217 of the other TLS bits are set. tls_optimize clears bits when
4218 optimizing to indicate the corresponding GOT entry type is not
4219 needed. If set, TLS_TLS is never cleared. tls_optimize may also
4220 set TLS_TPRELGD when a GD reloc turns into a TPREL one. We use a
4221 separate flag rather than setting TPREL just for convenience in
4222 distinguishing the two cases.
4223 These flags are also kept for local symbols. */
4224 #define TLS_TLS 1 /* Any TLS reloc. */
4225 #define TLS_GD 2 /* GD reloc. */
4226 #define TLS_LD 4 /* LD reloc. */
4227 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
4228 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
4229 #define TLS_MARK 32 /* __tls_get_addr call marked. */
4230 #define TLS_TPRELGD 64 /* TPREL reloc resulting from GD->IE. */
4231 #define TLS_EXPLICIT 128 /* Marks TOC section TLS relocs. */
4232 unsigned char tls_mask;
4234 /* The above field is also used to mark function symbols. In which
4235 case TLS_TLS will be 0. */
4236 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
4237 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
4238 #define NON_GOT 256 /* local symbol plt, not stored. */
4241 /* ppc64 ELF linker hash table. */
4243 struct ppc_link_hash_table
4245 struct elf_link_hash_table elf;
4247 /* The stub hash table. */
4248 struct bfd_hash_table stub_hash_table;
4250 /* Another hash table for plt_branch stubs. */
4251 struct bfd_hash_table branch_hash_table;
4253 /* Hash table for function prologue tocsave. */
4254 htab_t tocsave_htab;
4256 /* Various options and other info passed from the linker. */
4257 struct ppc64_elf_params *params;
4259 /* The size of sec_info below. */
4260 unsigned int sec_info_arr_size;
4262 /* Per-section array of extra section info. Done this way rather
4263 than as part of ppc64_elf_section_data so we have the info for
4264 non-ppc64 sections. */
4267 /* Along with elf_gp, specifies the TOC pointer used by this section. */
4272 /* The section group that this section belongs to. */
4273 struct map_stub *group;
4274 /* A temp section list pointer. */
4279 /* Linked list of groups. */
4280 struct map_stub *group;
4282 /* Temp used when calculating TOC pointers. */
4285 asection *toc_first_sec;
4287 /* Used when adding symbols. */
4288 struct ppc_link_hash_entry *dot_syms;
4290 /* Shortcuts to get to dynamic linker sections. */
4292 asection *global_entry;
4295 asection *relpltlocal;
4298 asection *glink_eh_frame;
4300 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
4301 struct ppc_link_hash_entry *tls_get_addr;
4302 struct ppc_link_hash_entry *tls_get_addr_fd;
4304 /* The size of reliplt used by got entry relocs. */
4305 bfd_size_type got_reli_size;
4308 unsigned long stub_count[ppc_stub_global_entry];
4310 /* Number of stubs against global syms. */
4311 unsigned long stub_globals;
4313 /* Set if we're linking code with function descriptors. */
4314 unsigned int opd_abi:1;
4316 /* Support for multiple toc sections. */
4317 unsigned int do_multi_toc:1;
4318 unsigned int multi_toc_needed:1;
4319 unsigned int second_toc_pass:1;
4320 unsigned int do_toc_opt:1;
4322 /* Set if tls optimization is enabled. */
4323 unsigned int do_tls_opt:1;
4325 /* Set if inline plt calls should be converted to direct calls. */
4326 unsigned int can_convert_all_inline_plt:1;
4329 unsigned int stub_error:1;
4331 /* Whether func_desc_adjust needs to be run over symbols. */
4332 unsigned int need_func_desc_adj:1;
4334 /* Whether there exist local gnu indirect function resolvers,
4335 referenced by dynamic relocations. */
4336 unsigned int local_ifunc_resolver:1;
4337 unsigned int maybe_local_ifunc_resolver:1;
4339 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
4340 unsigned int has_plt_localentry0:1;
4342 /* Incremented every time we size stubs. */
4343 unsigned int stub_iteration;
4345 /* Small local sym cache. */
4346 struct sym_cache sym_cache;
4349 /* Rename some of the generic section flags to better document how they
4352 /* Nonzero if this section has TLS related relocations. */
4353 #define has_tls_reloc sec_flg0
4355 /* Nonzero if this section has an old-style call to __tls_get_addr. */
4356 #define has_tls_get_addr_call sec_flg1
4358 /* Nonzero if this section has any toc or got relocs. */
4359 #define has_toc_reloc sec_flg2
4361 /* Nonzero if this section has a call to another section that uses
4363 #define makes_toc_func_call sec_flg3
4365 /* Recursion protection when determining above flag. */
4366 #define call_check_in_progress sec_flg4
4367 #define call_check_done sec_flg5
4369 /* Get the ppc64 ELF linker hash table from a link_info structure. */
4371 #define ppc_hash_table(p) \
4372 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
4373 == PPC64_ELF_DATA ? ((struct ppc_link_hash_table *) ((p)->hash)) : NULL)
4375 #define ppc_stub_hash_lookup(table, string, create, copy) \
4376 ((struct ppc_stub_hash_entry *) \
4377 bfd_hash_lookup ((table), (string), (create), (copy)))
4379 #define ppc_branch_hash_lookup(table, string, create, copy) \
4380 ((struct ppc_branch_hash_entry *) \
4381 bfd_hash_lookup ((table), (string), (create), (copy)))
4383 /* Create an entry in the stub hash table. */
4385 static struct bfd_hash_entry *
4386 stub_hash_newfunc (struct bfd_hash_entry *entry,
4387 struct bfd_hash_table *table,
4390 /* Allocate the structure if it has not already been allocated by a
4394 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
4399 /* Call the allocation method of the superclass. */
4400 entry = bfd_hash_newfunc (entry, table, string);
4403 struct ppc_stub_hash_entry *eh;
4405 /* Initialize the local fields. */
4406 eh = (struct ppc_stub_hash_entry *) entry;
4407 eh->stub_type = ppc_stub_none;
4409 eh->stub_offset = 0;
4410 eh->target_value = 0;
4411 eh->target_section = NULL;
4420 /* Create an entry in the branch hash table. */
4422 static struct bfd_hash_entry *
4423 branch_hash_newfunc (struct bfd_hash_entry *entry,
4424 struct bfd_hash_table *table,
4427 /* Allocate the structure if it has not already been allocated by a
4431 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
4436 /* Call the allocation method of the superclass. */
4437 entry = bfd_hash_newfunc (entry, table, string);
4440 struct ppc_branch_hash_entry *eh;
4442 /* Initialize the local fields. */
4443 eh = (struct ppc_branch_hash_entry *) entry;
4451 /* Create an entry in a ppc64 ELF linker hash table. */
4453 static struct bfd_hash_entry *
4454 link_hash_newfunc (struct bfd_hash_entry *entry,
4455 struct bfd_hash_table *table,
4458 /* Allocate the structure if it has not already been allocated by a
4462 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
4467 /* Call the allocation method of the superclass. */
4468 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
4471 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
4473 memset (&eh->u.stub_cache, 0,
4474 (sizeof (struct ppc_link_hash_entry)
4475 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
4477 /* When making function calls, old ABI code references function entry
4478 points (dot symbols), while new ABI code references the function
4479 descriptor symbol. We need to make any combination of reference and
4480 definition work together, without breaking archive linking.
4482 For a defined function "foo" and an undefined call to "bar":
4483 An old object defines "foo" and ".foo", references ".bar" (possibly
4485 A new object defines "foo" and references "bar".
4487 A new object thus has no problem with its undefined symbols being
4488 satisfied by definitions in an old object. On the other hand, the
4489 old object won't have ".bar" satisfied by a new object.
4491 Keep a list of newly added dot-symbols. */
4493 if (string[0] == '.')
4495 struct ppc_link_hash_table *htab;
4497 htab = (struct ppc_link_hash_table *) table;
4498 eh->u.next_dot_sym = htab->dot_syms;
4499 htab->dot_syms = eh;
4506 struct tocsave_entry {
4512 tocsave_htab_hash (const void *p)
4514 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
4515 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
4519 tocsave_htab_eq (const void *p1, const void *p2)
4521 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
4522 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
4523 return e1->sec == e2->sec && e1->offset == e2->offset;
4526 /* Destroy a ppc64 ELF linker hash table. */
4529 ppc64_elf_link_hash_table_free (bfd *obfd)
4531 struct ppc_link_hash_table *htab;
4533 htab = (struct ppc_link_hash_table *) obfd->link.hash;
4534 if (htab->tocsave_htab)
4535 htab_delete (htab->tocsave_htab);
4536 bfd_hash_table_free (&htab->branch_hash_table);
4537 bfd_hash_table_free (&htab->stub_hash_table);
4538 _bfd_elf_link_hash_table_free (obfd);
4541 /* Create a ppc64 ELF linker hash table. */
4543 static struct bfd_link_hash_table *
4544 ppc64_elf_link_hash_table_create (bfd *abfd)
4546 struct ppc_link_hash_table *htab;
4547 bfd_size_type amt = sizeof (struct ppc_link_hash_table);
4549 htab = bfd_zmalloc (amt);
4553 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
4554 sizeof (struct ppc_link_hash_entry),
4561 /* Init the stub hash table too. */
4562 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
4563 sizeof (struct ppc_stub_hash_entry)))
4565 _bfd_elf_link_hash_table_free (abfd);
4569 /* And the branch hash table. */
4570 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
4571 sizeof (struct ppc_branch_hash_entry)))
4573 bfd_hash_table_free (&htab->stub_hash_table);
4574 _bfd_elf_link_hash_table_free (abfd);
4578 htab->tocsave_htab = htab_try_create (1024,
4582 if (htab->tocsave_htab == NULL)
4584 ppc64_elf_link_hash_table_free (abfd);
4587 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
4589 /* Initializing two fields of the union is just cosmetic. We really
4590 only care about glist, but when compiled on a 32-bit host the
4591 bfd_vma fields are larger. Setting the bfd_vma to zero makes
4592 debugger inspection of these fields look nicer. */
4593 htab->elf.init_got_refcount.refcount = 0;
4594 htab->elf.init_got_refcount.glist = NULL;
4595 htab->elf.init_plt_refcount.refcount = 0;
4596 htab->elf.init_plt_refcount.glist = NULL;
4597 htab->elf.init_got_offset.offset = 0;
4598 htab->elf.init_got_offset.glist = NULL;
4599 htab->elf.init_plt_offset.offset = 0;
4600 htab->elf.init_plt_offset.glist = NULL;
4602 return &htab->elf.root;
4605 /* Create sections for linker generated code. */
4608 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
4610 struct ppc_link_hash_table *htab;
4613 htab = ppc_hash_table (info);
4615 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
4616 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4617 if (htab->params->save_restore_funcs)
4619 /* Create .sfpr for code to save and restore fp regs. */
4620 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
4622 if (htab->sfpr == NULL
4623 || ! bfd_set_section_alignment (dynobj, htab->sfpr, 2))
4627 if (bfd_link_relocatable (info))
4630 /* Create .glink for lazy dynamic linking support. */
4631 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4633 if (htab->glink == NULL
4634 || ! bfd_set_section_alignment (dynobj, htab->glink, 3))
4637 /* The part of .glink used by global entry stubs, separate so that
4638 it can be aligned appropriately without affecting htab->glink. */
4639 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
4641 if (htab->global_entry == NULL
4642 || ! bfd_set_section_alignment (dynobj, htab->global_entry, 2))
4645 if (!info->no_ld_generated_unwind_info)
4647 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
4648 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4649 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
4652 if (htab->glink_eh_frame == NULL
4653 || !bfd_set_section_alignment (dynobj, htab->glink_eh_frame, 2))
4657 flags = SEC_ALLOC | SEC_LINKER_CREATED;
4658 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
4659 if (htab->elf.iplt == NULL
4660 || ! bfd_set_section_alignment (dynobj, htab->elf.iplt, 3))
4663 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4664 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4666 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
4667 if (htab->elf.irelplt == NULL
4668 || ! bfd_set_section_alignment (dynobj, htab->elf.irelplt, 3))
4671 /* Create branch lookup table for plt_branch stubs. */
4672 flags = (SEC_ALLOC | SEC_LOAD
4673 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4674 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4676 if (htab->brlt == NULL
4677 || ! bfd_set_section_alignment (dynobj, htab->brlt, 3))
4680 /* Local plt entries, put in .branch_lt but a separate section for
4682 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
4684 if (htab->pltlocal == NULL
4685 || ! bfd_set_section_alignment (dynobj, htab->pltlocal, 3))
4688 if (!bfd_link_pic (info))
4691 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
4692 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
4694 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4695 if (htab->relbrlt == NULL
4696 || ! bfd_set_section_alignment (dynobj, htab->relbrlt, 3))
4700 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
4701 if (htab->relpltlocal == NULL
4702 || ! bfd_set_section_alignment (dynobj, htab->relpltlocal, 3))
4708 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
4711 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
4712 struct ppc64_elf_params *params)
4714 struct ppc_link_hash_table *htab;
4716 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
4718 /* Always hook our dynamic sections into the first bfd, which is the
4719 linker created stub bfd. This ensures that the GOT header is at
4720 the start of the output TOC section. */
4721 htab = ppc_hash_table (info);
4722 htab->elf.dynobj = params->stub_bfd;
4723 htab->params = params;
4725 return create_linkage_sections (htab->elf.dynobj, info);
4728 /* Build a name for an entry in the stub hash table. */
4731 ppc_stub_name (const asection *input_section,
4732 const asection *sym_sec,
4733 const struct ppc_link_hash_entry *h,
4734 const Elf_Internal_Rela *rel)
4739 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
4740 offsets from a sym as a branch target? In fact, we could
4741 probably assume the addend is always zero. */
4742 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
4746 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
4747 stub_name = bfd_malloc (len);
4748 if (stub_name == NULL)
4751 len = sprintf (stub_name, "%08x.%s+%x",
4752 input_section->id & 0xffffffff,
4753 h->elf.root.root.string,
4754 (int) rel->r_addend & 0xffffffff);
4758 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
4759 stub_name = bfd_malloc (len);
4760 if (stub_name == NULL)
4763 len = sprintf (stub_name, "%08x.%x:%x+%x",
4764 input_section->id & 0xffffffff,
4765 sym_sec->id & 0xffffffff,
4766 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
4767 (int) rel->r_addend & 0xffffffff);
4769 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
4770 stub_name[len - 2] = 0;
4774 /* Look up an entry in the stub hash. Stub entries are cached because
4775 creating the stub name takes a bit of time. */
4777 static struct ppc_stub_hash_entry *
4778 ppc_get_stub_entry (const asection *input_section,
4779 const asection *sym_sec,
4780 struct ppc_link_hash_entry *h,
4781 const Elf_Internal_Rela *rel,
4782 struct ppc_link_hash_table *htab)
4784 struct ppc_stub_hash_entry *stub_entry;
4785 struct map_stub *group;
4787 /* If this input section is part of a group of sections sharing one
4788 stub section, then use the id of the first section in the group.
4789 Stub names need to include a section id, as there may well be
4790 more than one stub used to reach say, printf, and we need to
4791 distinguish between them. */
4792 group = htab->sec_info[input_section->id].u.group;
4796 if (h != NULL && h->u.stub_cache != NULL
4797 && h->u.stub_cache->h == h
4798 && h->u.stub_cache->group == group)
4800 stub_entry = h->u.stub_cache;
4806 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
4807 if (stub_name == NULL)
4810 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
4811 stub_name, FALSE, FALSE);
4813 h->u.stub_cache = stub_entry;
4821 /* Add a new stub entry to the stub hash. Not all fields of the new
4822 stub entry are initialised. */
4824 static struct ppc_stub_hash_entry *
4825 ppc_add_stub (const char *stub_name,
4827 struct bfd_link_info *info)
4829 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4830 struct map_stub *group;
4833 struct ppc_stub_hash_entry *stub_entry;
4835 group = htab->sec_info[section->id].u.group;
4836 link_sec = group->link_sec;
4837 stub_sec = group->stub_sec;
4838 if (stub_sec == NULL)
4844 namelen = strlen (link_sec->name);
4845 len = namelen + sizeof (STUB_SUFFIX);
4846 s_name = bfd_alloc (htab->params->stub_bfd, len);
4850 memcpy (s_name, link_sec->name, namelen);
4851 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
4852 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
4853 if (stub_sec == NULL)
4855 group->stub_sec = stub_sec;
4858 /* Enter this entry into the linker stub hash table. */
4859 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
4861 if (stub_entry == NULL)
4863 /* xgettext:c-format */
4864 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
4865 section->owner, stub_name);
4869 stub_entry->group = group;
4870 stub_entry->stub_offset = 0;
4874 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
4875 not already done. */
4878 create_got_section (bfd *abfd, struct bfd_link_info *info)
4880 asection *got, *relgot;
4882 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4884 if (!is_ppc64_elf (abfd))
4890 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
4893 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4894 | SEC_LINKER_CREATED);
4896 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4898 || !bfd_set_section_alignment (abfd, got, 3))
4901 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4902 flags | SEC_READONLY);
4904 || ! bfd_set_section_alignment (abfd, relgot, 3))
4907 ppc64_elf_tdata (abfd)->got = got;
4908 ppc64_elf_tdata (abfd)->relgot = relgot;
4912 /* Follow indirect and warning symbol links. */
4914 static inline struct bfd_link_hash_entry *
4915 follow_link (struct bfd_link_hash_entry *h)
4917 while (h->type == bfd_link_hash_indirect
4918 || h->type == bfd_link_hash_warning)
4923 static inline struct elf_link_hash_entry *
4924 elf_follow_link (struct elf_link_hash_entry *h)
4926 return (struct elf_link_hash_entry *) follow_link (&h->root);
4929 static inline struct ppc_link_hash_entry *
4930 ppc_follow_link (struct ppc_link_hash_entry *h)
4932 return (struct ppc_link_hash_entry *) follow_link (&h->elf.root);
4935 /* Merge PLT info on FROM with that on TO. */
4938 move_plt_plist (struct ppc_link_hash_entry *from,
4939 struct ppc_link_hash_entry *to)
4941 if (from->elf.plt.plist != NULL)
4943 if (to->elf.plt.plist != NULL)
4945 struct plt_entry **entp;
4946 struct plt_entry *ent;
4948 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4950 struct plt_entry *dent;
4952 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4953 if (dent->addend == ent->addend)
4955 dent->plt.refcount += ent->plt.refcount;
4962 *entp = to->elf.plt.plist;
4965 to->elf.plt.plist = from->elf.plt.plist;
4966 from->elf.plt.plist = NULL;
4970 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4973 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4974 struct elf_link_hash_entry *dir,
4975 struct elf_link_hash_entry *ind)
4977 struct ppc_link_hash_entry *edir, *eind;
4979 edir = (struct ppc_link_hash_entry *) dir;
4980 eind = (struct ppc_link_hash_entry *) ind;
4982 edir->is_func |= eind->is_func;
4983 edir->is_func_descriptor |= eind->is_func_descriptor;
4984 edir->tls_mask |= eind->tls_mask;
4985 if (eind->oh != NULL)
4986 edir->oh = ppc_follow_link (eind->oh);
4988 if (edir->elf.versioned != versioned_hidden)
4989 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4990 edir->elf.ref_regular |= eind->elf.ref_regular;
4991 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4992 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4993 edir->elf.needs_plt |= eind->elf.needs_plt;
4994 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4996 /* If we were called to copy over info for a weak sym, don't copy
4997 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4998 in order to simplify readonly_dynrelocs and save a field in the
4999 symbol hash entry, but that means dyn_relocs can't be used in any
5000 tests about a specific symbol, or affect other symbol flags which
5002 if (eind->elf.root.type != bfd_link_hash_indirect)
5005 /* Copy over any dynamic relocs we may have on the indirect sym. */
5006 if (eind->dyn_relocs != NULL)
5008 if (edir->dyn_relocs != NULL)
5010 struct elf_dyn_relocs **pp;
5011 struct elf_dyn_relocs *p;
5013 /* Add reloc counts against the indirect sym to the direct sym
5014 list. Merge any entries against the same section. */
5015 for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
5017 struct elf_dyn_relocs *q;
5019 for (q = edir->dyn_relocs; q != NULL; q = q->next)
5020 if (q->sec == p->sec)
5022 q->pc_count += p->pc_count;
5023 q->count += p->count;
5030 *pp = edir->dyn_relocs;
5033 edir->dyn_relocs = eind->dyn_relocs;
5034 eind->dyn_relocs = NULL;
5037 /* Copy over got entries that we may have already seen to the
5038 symbol which just became indirect. */
5039 if (eind->elf.got.glist != NULL)
5041 if (edir->elf.got.glist != NULL)
5043 struct got_entry **entp;
5044 struct got_entry *ent;
5046 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
5048 struct got_entry *dent;
5050 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
5051 if (dent->addend == ent->addend
5052 && dent->owner == ent->owner
5053 && dent->tls_type == ent->tls_type)
5055 dent->got.refcount += ent->got.refcount;
5062 *entp = edir->elf.got.glist;
5065 edir->elf.got.glist = eind->elf.got.glist;
5066 eind->elf.got.glist = NULL;
5069 /* And plt entries. */
5070 move_plt_plist (eind, edir);
5072 if (eind->elf.dynindx != -1)
5074 if (edir->elf.dynindx != -1)
5075 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
5076 edir->elf.dynstr_index);
5077 edir->elf.dynindx = eind->elf.dynindx;
5078 edir->elf.dynstr_index = eind->elf.dynstr_index;
5079 eind->elf.dynindx = -1;
5080 eind->elf.dynstr_index = 0;
5084 /* Find the function descriptor hash entry from the given function code
5085 hash entry FH. Link the entries via their OH fields. */
5087 static struct ppc_link_hash_entry *
5088 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
5090 struct ppc_link_hash_entry *fdh = fh->oh;
5094 const char *fd_name = fh->elf.root.root.string + 1;
5096 fdh = (struct ppc_link_hash_entry *)
5097 elf_link_hash_lookup (&htab->elf, fd_name, FALSE, FALSE, FALSE);
5101 fdh->is_func_descriptor = 1;
5107 fdh = ppc_follow_link (fdh);
5108 fdh->is_func_descriptor = 1;
5113 /* Make a fake function descriptor sym for the undefined code sym FH. */
5115 static struct ppc_link_hash_entry *
5116 make_fdh (struct bfd_link_info *info,
5117 struct ppc_link_hash_entry *fh)
5119 bfd *abfd = fh->elf.root.u.undef.abfd;
5120 struct bfd_link_hash_entry *bh = NULL;
5121 struct ppc_link_hash_entry *fdh;
5122 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
5126 if (!_bfd_generic_link_add_one_symbol (info, abfd,
5127 fh->elf.root.root.string + 1,
5128 flags, bfd_und_section_ptr, 0,
5129 NULL, FALSE, FALSE, &bh))
5132 fdh = (struct ppc_link_hash_entry *) bh;
5133 fdh->elf.non_elf = 0;
5135 fdh->is_func_descriptor = 1;
5142 /* Fix function descriptor symbols defined in .opd sections to be
5146 ppc64_elf_add_symbol_hook (bfd *ibfd,
5147 struct bfd_link_info *info,
5148 Elf_Internal_Sym *isym,
5150 flagword *flags ATTRIBUTE_UNUSED,
5155 && strcmp ((*sec)->name, ".opd") == 0)
5159 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
5160 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
5161 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
5163 /* If the symbol is a function defined in .opd, and the function
5164 code is in a discarded group, let it appear to be undefined. */
5165 if (!bfd_link_relocatable (info)
5166 && (*sec)->reloc_count != 0
5167 && opd_entry_value (*sec, *value, &code_sec, NULL,
5168 FALSE) != (bfd_vma) -1
5169 && discarded_section (code_sec))
5171 *sec = bfd_und_section_ptr;
5172 isym->st_shndx = SHN_UNDEF;
5175 else if (*sec != NULL
5176 && strcmp ((*sec)->name, ".toc") == 0
5177 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
5179 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5181 htab->params->object_in_toc = 1;
5184 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5186 if (abiversion (ibfd) == 0)
5187 set_abiversion (ibfd, 2);
5188 else if (abiversion (ibfd) == 1)
5190 _bfd_error_handler (_("symbol '%s' has invalid st_other"
5191 " for ABI version 1"), *name);
5192 bfd_set_error (bfd_error_bad_value);
5200 /* Merge non-visibility st_other attributes: local entry point. */
5203 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
5204 const Elf_Internal_Sym *isym,
5205 bfd_boolean definition,
5206 bfd_boolean dynamic)
5208 if (definition && (!dynamic || !h->def_regular))
5209 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
5210 | ELF_ST_VISIBILITY (h->other));
5213 /* Hook called on merging a symbol. We use this to clear "fake" since
5214 we now have a real symbol. */
5217 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
5218 const Elf_Internal_Sym *isym,
5219 asection **psec ATTRIBUTE_UNUSED,
5220 bfd_boolean newdef ATTRIBUTE_UNUSED,
5221 bfd_boolean olddef ATTRIBUTE_UNUSED,
5222 bfd *oldbfd ATTRIBUTE_UNUSED,
5223 const asection *oldsec ATTRIBUTE_UNUSED)
5225 ((struct ppc_link_hash_entry *) h)->fake = 0;
5226 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
5227 ((struct ppc_link_hash_entry *) h)->non_zero_localentry = 1;
5231 /* This function makes an old ABI object reference to ".bar" cause the
5232 inclusion of a new ABI object archive that defines "bar".
5233 NAME is a symbol defined in an archive. Return a symbol in the hash
5234 table that might be satisfied by the archive symbols. */
5236 static struct elf_link_hash_entry *
5237 ppc64_elf_archive_symbol_lookup (bfd *abfd,
5238 struct bfd_link_info *info,
5241 struct elf_link_hash_entry *h;
5245 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
5247 /* Don't return this sym if it is a fake function descriptor
5248 created by add_symbol_adjust. */
5249 && !((struct ppc_link_hash_entry *) h)->fake)
5255 len = strlen (name);
5256 dot_name = bfd_alloc (abfd, len + 2);
5257 if (dot_name == NULL)
5258 return (struct elf_link_hash_entry *) -1;
5260 memcpy (dot_name + 1, name, len + 1);
5261 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
5262 bfd_release (abfd, dot_name);
5266 /* This function satisfies all old ABI object references to ".bar" if a
5267 new ABI object defines "bar". Well, at least, undefined dot symbols
5268 are made weak. This stops later archive searches from including an
5269 object if we already have a function descriptor definition. It also
5270 prevents the linker complaining about undefined symbols.
5271 We also check and correct mismatched symbol visibility here. The
5272 most restrictive visibility of the function descriptor and the
5273 function entry symbol is used. */
5276 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
5278 struct ppc_link_hash_table *htab;
5279 struct ppc_link_hash_entry *fdh;
5281 if (eh->elf.root.type == bfd_link_hash_warning)
5282 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
5284 if (eh->elf.root.type == bfd_link_hash_indirect)
5287 if (eh->elf.root.root.string[0] != '.')
5290 htab = ppc_hash_table (info);
5294 fdh = lookup_fdh (eh, htab);
5296 && !bfd_link_relocatable (info)
5297 && (eh->elf.root.type == bfd_link_hash_undefined
5298 || eh->elf.root.type == bfd_link_hash_undefweak)
5299 && eh->elf.ref_regular)
5301 /* Make an undefined function descriptor sym, in order to
5302 pull in an --as-needed shared lib. Archives are handled
5304 fdh = make_fdh (info, eh);
5311 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
5312 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
5314 /* Make both descriptor and entry symbol have the most
5315 constraining visibility of either symbol. */
5316 if (entry_vis < descr_vis)
5317 fdh->elf.other += entry_vis - descr_vis;
5318 else if (entry_vis > descr_vis)
5319 eh->elf.other += descr_vis - entry_vis;
5321 /* Propagate reference flags from entry symbol to function
5322 descriptor symbol. */
5323 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
5324 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
5325 fdh->elf.ref_regular |= eh->elf.ref_regular;
5326 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
5328 if (!fdh->elf.forced_local
5329 && fdh->elf.dynindx == -1
5330 && fdh->elf.versioned != versioned_hidden
5331 && (bfd_link_dll (info)
5332 || fdh->elf.def_dynamic
5333 || fdh->elf.ref_dynamic)
5334 && (eh->elf.ref_regular
5335 || eh->elf.def_regular))
5337 if (! bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
5345 /* Set up opd section info and abiversion for IBFD, and process list
5346 of dot-symbols we made in link_hash_newfunc. */
5349 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
5351 struct ppc_link_hash_table *htab;
5352 struct ppc_link_hash_entry **p, *eh;
5353 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
5355 if (opd != NULL && opd->size != 0)
5357 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
5358 ppc64_elf_section_data (opd)->sec_type = sec_opd;
5360 if (abiversion (ibfd) == 0)
5361 set_abiversion (ibfd, 1);
5362 else if (abiversion (ibfd) >= 2)
5364 /* xgettext:c-format */
5365 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
5366 ibfd, abiversion (ibfd));
5367 bfd_set_error (bfd_error_bad_value);
5372 if (is_ppc64_elf (info->output_bfd))
5374 /* For input files without an explicit abiversion in e_flags
5375 we should have flagged any with symbol st_other bits set
5376 as ELFv1 and above flagged those with .opd as ELFv2.
5377 Set the output abiversion if not yet set, and for any input
5378 still ambiguous, take its abiversion from the output.
5379 Differences in ABI are reported later. */
5380 if (abiversion (info->output_bfd) == 0)
5381 set_abiversion (info->output_bfd, abiversion (ibfd));
5382 else if (abiversion (ibfd) == 0)
5383 set_abiversion (ibfd, abiversion (info->output_bfd));
5386 htab = ppc_hash_table (info);
5390 if (opd != NULL && opd->size != 0
5391 && (ibfd->flags & DYNAMIC) == 0
5392 && (opd->flags & SEC_RELOC) != 0
5393 && opd->reloc_count != 0
5394 && !bfd_is_abs_section (opd->output_section)
5395 && info->gc_sections)
5397 /* Garbage collection needs some extra help with .opd sections.
5398 We don't want to necessarily keep everything referenced by
5399 relocs in .opd, as that would keep all functions. Instead,
5400 if we reference an .opd symbol (a function descriptor), we
5401 want to keep the function code symbol's section. This is
5402 easy for global symbols, but for local syms we need to keep
5403 information about the associated function section. */
5405 asection **opd_sym_map;
5406 Elf_Internal_Shdr *symtab_hdr;
5407 Elf_Internal_Rela *relocs, *rel_end, *rel;
5409 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
5410 opd_sym_map = bfd_zalloc (ibfd, amt);
5411 if (opd_sym_map == NULL)
5413 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
5414 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
5418 symtab_hdr = &elf_symtab_hdr (ibfd);
5419 rel_end = relocs + opd->reloc_count - 1;
5420 for (rel = relocs; rel < rel_end; rel++)
5422 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
5423 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
5425 if (r_type == R_PPC64_ADDR64
5426 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
5427 && r_symndx < symtab_hdr->sh_info)
5429 Elf_Internal_Sym *isym;
5432 isym = bfd_sym_from_r_symndx (&htab->sym_cache, ibfd, r_symndx);
5435 if (elf_section_data (opd)->relocs != relocs)
5440 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
5441 if (s != NULL && s != opd)
5442 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
5445 if (elf_section_data (opd)->relocs != relocs)
5449 p = &htab->dot_syms;
5450 while ((eh = *p) != NULL)
5453 if (&eh->elf == htab->elf.hgot)
5455 else if (htab->elf.hgot == NULL
5456 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
5457 htab->elf.hgot = &eh->elf;
5458 else if (abiversion (ibfd) <= 1)
5460 htab->need_func_desc_adj = 1;
5461 if (!add_symbol_adjust (eh, info))
5464 p = &eh->u.next_dot_sym;
5469 /* Undo hash table changes when an --as-needed input file is determined
5470 not to be needed. */
5473 ppc64_elf_notice_as_needed (bfd *ibfd,
5474 struct bfd_link_info *info,
5475 enum notice_asneeded_action act)
5477 if (act == notice_not_needed)
5479 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5484 htab->dot_syms = NULL;
5486 return _bfd_elf_notice_as_needed (ibfd, info, act);
5489 /* If --just-symbols against a final linked binary, then assume we need
5490 toc adjusting stubs when calling functions defined there. */
5493 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
5495 if ((sec->flags & SEC_CODE) != 0
5496 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
5497 && is_ppc64_elf (sec->owner))
5499 if (abiversion (sec->owner) >= 2
5500 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
5501 sec->has_toc_reloc = 1;
5503 _bfd_elf_link_just_syms (sec, info);
5506 static struct plt_entry **
5507 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
5508 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
5510 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
5511 struct plt_entry **local_plt;
5512 unsigned char *local_got_tls_masks;
5514 if (local_got_ents == NULL)
5516 bfd_size_type size = symtab_hdr->sh_info;
5518 size *= (sizeof (*local_got_ents)
5519 + sizeof (*local_plt)
5520 + sizeof (*local_got_tls_masks));
5521 local_got_ents = bfd_zalloc (abfd, size);
5522 if (local_got_ents == NULL)
5524 elf_local_got_ents (abfd) = local_got_ents;
5527 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
5529 struct got_entry *ent;
5531 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
5532 if (ent->addend == r_addend
5533 && ent->owner == abfd
5534 && ent->tls_type == tls_type)
5538 bfd_size_type amt = sizeof (*ent);
5539 ent = bfd_alloc (abfd, amt);
5542 ent->next = local_got_ents[r_symndx];
5543 ent->addend = r_addend;
5545 ent->tls_type = tls_type;
5546 ent->is_indirect = FALSE;
5547 ent->got.refcount = 0;
5548 local_got_ents[r_symndx] = ent;
5550 ent->got.refcount += 1;
5553 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
5554 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
5555 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
5557 return local_plt + r_symndx;
5561 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
5563 struct plt_entry *ent;
5565 for (ent = *plist; ent != NULL; ent = ent->next)
5566 if (ent->addend == addend)
5570 bfd_size_type amt = sizeof (*ent);
5571 ent = bfd_alloc (abfd, amt);
5575 ent->addend = addend;
5576 ent->plt.refcount = 0;
5579 ent->plt.refcount += 1;
5584 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
5586 return (r_type == R_PPC64_REL24
5587 || r_type == R_PPC64_REL24_NOTOC
5588 || r_type == R_PPC64_REL14
5589 || r_type == R_PPC64_REL14_BRTAKEN
5590 || r_type == R_PPC64_REL14_BRNTAKEN
5591 || r_type == R_PPC64_ADDR24
5592 || r_type == R_PPC64_ADDR14
5593 || r_type == R_PPC64_ADDR14_BRTAKEN
5594 || r_type == R_PPC64_ADDR14_BRNTAKEN
5595 || r_type == R_PPC64_PLTCALL);
5598 /* Relocs on inline plt call sequence insns prior to the call. */
5601 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
5603 return (r_type == R_PPC64_PLT16_HA
5604 || r_type == R_PPC64_PLT16_HI
5605 || r_type == R_PPC64_PLT16_LO
5606 || r_type == R_PPC64_PLT16_LO_DS
5607 || r_type == R_PPC64_PLTSEQ);
5610 /* Look through the relocs for a section during the first phase, and
5611 calculate needed space in the global offset table, procedure
5612 linkage table, and dynamic reloc sections. */
5615 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
5616 asection *sec, const Elf_Internal_Rela *relocs)
5618 struct ppc_link_hash_table *htab;
5619 Elf_Internal_Shdr *symtab_hdr;
5620 struct elf_link_hash_entry **sym_hashes;
5621 const Elf_Internal_Rela *rel;
5622 const Elf_Internal_Rela *rel_end;
5624 struct elf_link_hash_entry *tga, *dottga;
5627 if (bfd_link_relocatable (info))
5630 /* Don't do anything special with non-loaded, non-alloced sections.
5631 In particular, any relocs in such sections should not affect GOT
5632 and PLT reference counting (ie. we don't allow them to create GOT
5633 or PLT entries), there's no possibility or desire to optimize TLS
5634 relocs, and there's not much point in propagating relocs to shared
5635 libs that the dynamic linker won't relocate. */
5636 if ((sec->flags & SEC_ALLOC) == 0)
5639 BFD_ASSERT (is_ppc64_elf (abfd));
5641 htab = ppc_hash_table (info);
5645 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
5646 FALSE, FALSE, TRUE);
5647 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
5648 FALSE, FALSE, TRUE);
5649 symtab_hdr = &elf_symtab_hdr (abfd);
5650 sym_hashes = elf_sym_hashes (abfd);
5652 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
5653 rel_end = relocs + sec->reloc_count;
5654 for (rel = relocs; rel < rel_end; rel++)
5656 unsigned long r_symndx;
5657 struct elf_link_hash_entry *h;
5658 enum elf_ppc64_reloc_type r_type;
5660 struct _ppc64_elf_section_data *ppc64_sec;
5661 struct plt_entry **ifunc, **plt_list;
5663 r_symndx = ELF64_R_SYM (rel->r_info);
5664 if (r_symndx < symtab_hdr->sh_info)
5668 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5669 h = elf_follow_link (h);
5671 if (h == htab->elf.hgot)
5672 sec->has_toc_reloc = 1;
5679 if (h->type == STT_GNU_IFUNC)
5682 ifunc = &h->plt.plist;
5687 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5692 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
5694 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5696 NON_GOT | PLT_IFUNC);
5702 r_type = ELF64_R_TYPE (rel->r_info);
5707 /* These special tls relocs tie a call to __tls_get_addr with
5708 its parameter symbol. */
5710 ((struct ppc_link_hash_entry *) h)->tls_mask |= TLS_TLS | TLS_MARK;
5712 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5714 NON_GOT | TLS_TLS | TLS_MARK))
5716 sec->has_tls_reloc = 1;
5719 case R_PPC64_GOT_TLSLD16:
5720 case R_PPC64_GOT_TLSLD16_LO:
5721 case R_PPC64_GOT_TLSLD16_HI:
5722 case R_PPC64_GOT_TLSLD16_HA:
5723 tls_type = TLS_TLS | TLS_LD;
5726 case R_PPC64_GOT_TLSGD16:
5727 case R_PPC64_GOT_TLSGD16_LO:
5728 case R_PPC64_GOT_TLSGD16_HI:
5729 case R_PPC64_GOT_TLSGD16_HA:
5730 tls_type = TLS_TLS | TLS_GD;
5733 case R_PPC64_GOT_TPREL16_DS:
5734 case R_PPC64_GOT_TPREL16_LO_DS:
5735 case R_PPC64_GOT_TPREL16_HI:
5736 case R_PPC64_GOT_TPREL16_HA:
5737 if (bfd_link_dll (info))
5738 info->flags |= DF_STATIC_TLS;
5739 tls_type = TLS_TLS | TLS_TPREL;
5742 case R_PPC64_GOT_DTPREL16_DS:
5743 case R_PPC64_GOT_DTPREL16_LO_DS:
5744 case R_PPC64_GOT_DTPREL16_HI:
5745 case R_PPC64_GOT_DTPREL16_HA:
5746 tls_type = TLS_TLS | TLS_DTPREL;
5748 sec->has_tls_reloc = 1;
5752 case R_PPC64_GOT16_DS:
5753 case R_PPC64_GOT16_HA:
5754 case R_PPC64_GOT16_HI:
5755 case R_PPC64_GOT16_LO:
5756 case R_PPC64_GOT16_LO_DS:
5757 /* This symbol requires a global offset table entry. */
5758 sec->has_toc_reloc = 1;
5759 if (r_type == R_PPC64_GOT_TLSLD16
5760 || r_type == R_PPC64_GOT_TLSGD16
5761 || r_type == R_PPC64_GOT_TPREL16_DS
5762 || r_type == R_PPC64_GOT_DTPREL16_DS
5763 || r_type == R_PPC64_GOT16
5764 || r_type == R_PPC64_GOT16_DS)
5766 htab->do_multi_toc = 1;
5767 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5770 if (ppc64_elf_tdata (abfd)->got == NULL
5771 && !create_got_section (abfd, info))
5776 struct ppc_link_hash_entry *eh;
5777 struct got_entry *ent;
5779 eh = (struct ppc_link_hash_entry *) h;
5780 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
5781 if (ent->addend == rel->r_addend
5782 && ent->owner == abfd
5783 && ent->tls_type == tls_type)
5787 bfd_size_type amt = sizeof (*ent);
5788 ent = bfd_alloc (abfd, amt);
5791 ent->next = eh->elf.got.glist;
5792 ent->addend = rel->r_addend;
5794 ent->tls_type = tls_type;
5795 ent->is_indirect = FALSE;
5796 ent->got.refcount = 0;
5797 eh->elf.got.glist = ent;
5799 ent->got.refcount += 1;
5800 eh->tls_mask |= tls_type;
5803 /* This is a global offset table entry for a local symbol. */
5804 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5805 rel->r_addend, tls_type))
5808 /* We may also need a plt entry if the symbol turns out to be
5810 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1)
5812 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
5817 case R_PPC64_PLT16_HA:
5818 case R_PPC64_PLT16_HI:
5819 case R_PPC64_PLT16_LO:
5820 case R_PPC64_PLT16_LO_DS:
5823 /* This symbol requires a procedure linkage table entry. */
5828 if (h->root.root.string[0] == '.'
5829 && h->root.root.string[1] != '\0')
5830 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5831 ((struct ppc_link_hash_entry *) h)->tls_mask |= PLT_KEEP;
5832 plt_list = &h->plt.plist;
5834 if (plt_list == NULL)
5835 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5837 NON_GOT | PLT_KEEP);
5838 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5842 /* The following relocations don't need to propagate the
5843 relocation if linking a shared object since they are
5844 section relative. */
5845 case R_PPC64_SECTOFF:
5846 case R_PPC64_SECTOFF_LO:
5847 case R_PPC64_SECTOFF_HI:
5848 case R_PPC64_SECTOFF_HA:
5849 case R_PPC64_SECTOFF_DS:
5850 case R_PPC64_SECTOFF_LO_DS:
5851 case R_PPC64_DTPREL16:
5852 case R_PPC64_DTPREL16_LO:
5853 case R_PPC64_DTPREL16_HI:
5854 case R_PPC64_DTPREL16_HA:
5855 case R_PPC64_DTPREL16_DS:
5856 case R_PPC64_DTPREL16_LO_DS:
5857 case R_PPC64_DTPREL16_HIGH:
5858 case R_PPC64_DTPREL16_HIGHA:
5859 case R_PPC64_DTPREL16_HIGHER:
5860 case R_PPC64_DTPREL16_HIGHERA:
5861 case R_PPC64_DTPREL16_HIGHEST:
5862 case R_PPC64_DTPREL16_HIGHESTA:
5867 case R_PPC64_REL16_LO:
5868 case R_PPC64_REL16_HI:
5869 case R_PPC64_REL16_HA:
5870 case R_PPC64_REL16DX_HA:
5873 /* Not supported as a dynamic relocation. */
5874 case R_PPC64_ADDR64_LOCAL:
5875 if (bfd_link_pic (info))
5877 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5879 /* xgettext:c-format */
5880 info->callbacks->einfo (_("%H: %s reloc unsupported "
5881 "in shared libraries and PIEs\n"),
5882 abfd, sec, rel->r_offset,
5883 ppc64_elf_howto_table[r_type]->name);
5884 bfd_set_error (bfd_error_bad_value);
5890 case R_PPC64_TOC16_DS:
5891 htab->do_multi_toc = 1;
5892 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5894 case R_PPC64_TOC16_LO:
5895 case R_PPC64_TOC16_HI:
5896 case R_PPC64_TOC16_HA:
5897 case R_PPC64_TOC16_LO_DS:
5898 sec->has_toc_reloc = 1;
5905 /* This relocation describes the C++ object vtable hierarchy.
5906 Reconstruct it for later use during GC. */
5907 case R_PPC64_GNU_VTINHERIT:
5908 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5912 /* This relocation describes which C++ vtable entries are actually
5913 used. Record for later use during GC. */
5914 case R_PPC64_GNU_VTENTRY:
5915 BFD_ASSERT (h != NULL);
5917 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5922 case R_PPC64_REL14_BRTAKEN:
5923 case R_PPC64_REL14_BRNTAKEN:
5925 asection *dest = NULL;
5927 /* Heuristic: If jumping outside our section, chances are
5928 we are going to need a stub. */
5931 /* If the sym is weak it may be overridden later, so
5932 don't assume we know where a weak sym lives. */
5933 if (h->root.type == bfd_link_hash_defined)
5934 dest = h->root.u.def.section;
5938 Elf_Internal_Sym *isym;
5940 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
5945 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5949 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5953 case R_PPC64_PLTCALL:
5954 ppc64_elf_section_data (sec)->has_pltcall = 1;
5958 case R_PPC64_REL24_NOTOC:
5964 if (h->root.root.string[0] == '.'
5965 && h->root.root.string[1] != '\0')
5966 ((struct ppc_link_hash_entry *) h)->is_func = 1;
5968 if (h == tga || h == dottga)
5970 sec->has_tls_reloc = 1;
5972 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5973 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5974 /* We have a new-style __tls_get_addr call with
5978 /* Mark this section as having an old-style call. */
5979 sec->has_tls_get_addr_call = 1;
5981 plt_list = &h->plt.plist;
5984 /* We may need a .plt entry if the function this reloc
5985 refers to is in a shared lib. */
5987 && !update_plt_info (abfd, plt_list, rel->r_addend))
5991 case R_PPC64_ADDR14:
5992 case R_PPC64_ADDR14_BRNTAKEN:
5993 case R_PPC64_ADDR14_BRTAKEN:
5994 case R_PPC64_ADDR24:
5997 case R_PPC64_TPREL64:
5998 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5999 if (bfd_link_dll (info))
6000 info->flags |= DF_STATIC_TLS;
6003 case R_PPC64_DTPMOD64:
6004 if (rel + 1 < rel_end
6005 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
6006 && rel[1].r_offset == rel->r_offset + 8)
6007 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
6009 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
6012 case R_PPC64_DTPREL64:
6013 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
6015 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
6016 && rel[-1].r_offset == rel->r_offset - 8)
6017 /* This is the second reloc of a dtpmod, dtprel pair.
6018 Don't mark with TLS_DTPREL. */
6022 sec->has_tls_reloc = 1;
6025 struct ppc_link_hash_entry *eh;
6026 eh = (struct ppc_link_hash_entry *) h;
6027 eh->tls_mask |= tls_type;
6030 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
6031 rel->r_addend, tls_type))
6034 ppc64_sec = ppc64_elf_section_data (sec);
6035 if (ppc64_sec->sec_type != sec_toc)
6039 /* One extra to simplify get_tls_mask. */
6040 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
6041 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
6042 if (ppc64_sec->u.toc.symndx == NULL)
6044 amt = sec->size * sizeof (bfd_vma) / 8;
6045 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
6046 if (ppc64_sec->u.toc.add == NULL)
6048 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
6049 ppc64_sec->sec_type = sec_toc;
6051 BFD_ASSERT (rel->r_offset % 8 == 0);
6052 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
6053 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
6055 /* Mark the second slot of a GD or LD entry.
6056 -1 to indicate GD and -2 to indicate LD. */
6057 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
6058 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
6059 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
6060 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
6063 case R_PPC64_TPREL16:
6064 case R_PPC64_TPREL16_LO:
6065 case R_PPC64_TPREL16_HI:
6066 case R_PPC64_TPREL16_HA:
6067 case R_PPC64_TPREL16_DS:
6068 case R_PPC64_TPREL16_LO_DS:
6069 case R_PPC64_TPREL16_HIGH:
6070 case R_PPC64_TPREL16_HIGHA:
6071 case R_PPC64_TPREL16_HIGHER:
6072 case R_PPC64_TPREL16_HIGHERA:
6073 case R_PPC64_TPREL16_HIGHEST:
6074 case R_PPC64_TPREL16_HIGHESTA:
6075 if (bfd_link_dll (info))
6076 info->flags |= DF_STATIC_TLS;
6079 case R_PPC64_ADDR64:
6081 && rel + 1 < rel_end
6082 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
6085 ((struct ppc_link_hash_entry *) h)->is_func = 1;
6089 case R_PPC64_ADDR16:
6090 case R_PPC64_ADDR16_DS:
6091 case R_PPC64_ADDR16_HA:
6092 case R_PPC64_ADDR16_HI:
6093 case R_PPC64_ADDR16_HIGH:
6094 case R_PPC64_ADDR16_HIGHA:
6095 case R_PPC64_ADDR16_HIGHER:
6096 case R_PPC64_ADDR16_HIGHERA:
6097 case R_PPC64_ADDR16_HIGHEST:
6098 case R_PPC64_ADDR16_HIGHESTA:
6099 case R_PPC64_ADDR16_LO:
6100 case R_PPC64_ADDR16_LO_DS:
6101 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
6102 && rel->r_addend == 0)
6104 /* We may need a .plt entry if this reloc refers to a
6105 function in a shared lib. */
6106 if (!update_plt_info (abfd, &h->plt.plist, rel->r_addend))
6108 h->pointer_equality_needed = 1;
6115 case R_PPC64_ADDR32:
6116 case R_PPC64_UADDR16:
6117 case R_PPC64_UADDR32:
6118 case R_PPC64_UADDR64:
6120 if (h != NULL && !bfd_link_pic (info))
6121 /* We may need a copy reloc. */
6124 /* Don't propagate .opd relocs. */
6125 if (NO_OPD_RELOCS && is_opd)
6128 /* If we are creating a shared library, and this is a reloc
6129 against a global symbol, or a non PC relative reloc
6130 against a local symbol, then we need to copy the reloc
6131 into the shared library. However, if we are linking with
6132 -Bsymbolic, we do not need to copy a reloc against a
6133 global symbol which is defined in an object we are
6134 including in the link (i.e., DEF_REGULAR is set). At
6135 this point we have not seen all the input files, so it is
6136 possible that DEF_REGULAR is not set now but will be set
6137 later (it is never cleared). In case of a weak definition,
6138 DEF_REGULAR may be cleared later by a strong definition in
6139 a shared library. We account for that possibility below by
6140 storing information in the dyn_relocs field of the hash
6141 table entry. A similar situation occurs when creating
6142 shared libraries and symbol visibility changes render the
6145 If on the other hand, we are creating an executable, we
6146 may need to keep relocations for symbols satisfied by a
6147 dynamic library if we manage to avoid copy relocs for the
6150 if ((bfd_link_pic (info)
6151 && (must_be_dyn_reloc (info, r_type)
6153 && (!SYMBOLIC_BIND (info, h)
6154 || h->root.type == bfd_link_hash_defweak
6155 || !h->def_regular))))
6156 || (ELIMINATE_COPY_RELOCS
6157 && !bfd_link_pic (info)
6159 && (h->root.type == bfd_link_hash_defweak
6160 || !h->def_regular))
6161 || (!bfd_link_pic (info)
6164 /* We must copy these reloc types into the output file.
6165 Create a reloc section in dynobj and make room for
6169 sreloc = _bfd_elf_make_dynamic_reloc_section
6170 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ TRUE);
6176 /* If this is a global symbol, we count the number of
6177 relocations we need for this symbol. */
6180 struct elf_dyn_relocs *p;
6181 struct elf_dyn_relocs **head;
6183 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
6185 if (p == NULL || p->sec != sec)
6187 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6197 if (!must_be_dyn_reloc (info, r_type))
6202 /* Track dynamic relocs needed for local syms too.
6203 We really need local syms available to do this
6205 struct ppc_dyn_relocs *p;
6206 struct ppc_dyn_relocs **head;
6207 bfd_boolean is_ifunc;
6210 Elf_Internal_Sym *isym;
6212 isym = bfd_sym_from_r_symndx (&htab->sym_cache,
6217 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
6221 vpp = &elf_section_data (s)->local_dynrel;
6222 head = (struct ppc_dyn_relocs **) vpp;
6223 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
6225 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
6227 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
6229 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
6235 p->ifunc = is_ifunc;
6251 /* Merge backend specific data from an object file to the output
6252 object file when linking. */
6255 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6257 bfd *obfd = info->output_bfd;
6258 unsigned long iflags, oflags;
6260 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
6263 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
6266 if (!_bfd_generic_verify_endian_match (ibfd, info))
6269 iflags = elf_elfheader (ibfd)->e_flags;
6270 oflags = elf_elfheader (obfd)->e_flags;
6272 if (iflags & ~EF_PPC64_ABI)
6275 /* xgettext:c-format */
6276 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
6277 bfd_set_error (bfd_error_bad_value);
6280 else if (iflags != oflags && iflags != 0)
6283 /* xgettext:c-format */
6284 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
6285 ibfd, iflags, oflags);
6286 bfd_set_error (bfd_error_bad_value);
6290 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
6293 /* Merge Tag_compatibility attributes and any common GNU ones. */
6294 return _bfd_elf_merge_object_attributes (ibfd, info);
6298 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6300 /* Print normal ELF private data. */
6301 _bfd_elf_print_private_bfd_data (abfd, ptr);
6303 if (elf_elfheader (abfd)->e_flags != 0)
6307 fprintf (file, _("private flags = 0x%lx:"),
6308 elf_elfheader (abfd)->e_flags);
6310 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
6311 fprintf (file, _(" [abiv%ld]"),
6312 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
6319 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
6320 of the code entry point, and its section, which must be in the same
6321 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
6324 opd_entry_value (asection *opd_sec,
6326 asection **code_sec,
6328 bfd_boolean in_code_sec)
6330 bfd *opd_bfd = opd_sec->owner;
6331 Elf_Internal_Rela *relocs;
6332 Elf_Internal_Rela *lo, *hi, *look;
6335 /* No relocs implies we are linking a --just-symbols object, or looking
6336 at a final linked executable with addr2line or somesuch. */
6337 if (opd_sec->reloc_count == 0)
6339 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
6341 if (contents == NULL)
6343 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
6344 return (bfd_vma) -1;
6345 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
6348 /* PR 17512: file: 64b9dfbb. */
6349 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
6350 return (bfd_vma) -1;
6352 val = bfd_get_64 (opd_bfd, contents + offset);
6353 if (code_sec != NULL)
6355 asection *sec, *likely = NULL;
6361 && val < sec->vma + sec->size)
6367 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
6369 && (sec->flags & SEC_LOAD) != 0
6370 && (sec->flags & SEC_ALLOC) != 0)
6375 if (code_off != NULL)
6376 *code_off = val - likely->vma;
6382 BFD_ASSERT (is_ppc64_elf (opd_bfd));
6384 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
6386 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, TRUE);
6387 /* PR 17512: file: df8e1fd6. */
6389 return (bfd_vma) -1;
6391 /* Go find the opd reloc at the sym address. */
6393 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
6397 look = lo + (hi - lo) / 2;
6398 if (look->r_offset < offset)
6400 else if (look->r_offset > offset)
6404 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
6406 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
6407 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
6409 unsigned long symndx = ELF64_R_SYM (look->r_info);
6410 asection *sec = NULL;
6412 if (symndx >= symtab_hdr->sh_info
6413 && elf_sym_hashes (opd_bfd) != NULL)
6415 struct elf_link_hash_entry **sym_hashes;
6416 struct elf_link_hash_entry *rh;
6418 sym_hashes = elf_sym_hashes (opd_bfd);
6419 rh = sym_hashes[symndx - symtab_hdr->sh_info];
6422 rh = elf_follow_link (rh);
6423 if (rh->root.type != bfd_link_hash_defined
6424 && rh->root.type != bfd_link_hash_defweak)
6426 if (rh->root.u.def.section->owner == opd_bfd)
6428 val = rh->root.u.def.value;
6429 sec = rh->root.u.def.section;
6436 Elf_Internal_Sym *sym;
6438 if (symndx < symtab_hdr->sh_info)
6440 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
6443 size_t symcnt = symtab_hdr->sh_info;
6444 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6449 symtab_hdr->contents = (bfd_byte *) sym;
6455 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
6461 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
6464 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
6465 val = sym->st_value;
6468 val += look->r_addend;
6469 if (code_off != NULL)
6471 if (code_sec != NULL)
6473 if (in_code_sec && *code_sec != sec)
6478 if (sec->output_section != NULL)
6479 val += sec->output_section->vma + sec->output_offset;
6488 /* If the ELF symbol SYM might be a function in SEC, return the
6489 function size and set *CODE_OFF to the function's entry point,
6490 otherwise return zero. */
6492 static bfd_size_type
6493 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
6498 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
6499 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
6503 if (!(sym->flags & BSF_SYNTHETIC))
6504 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
6506 if (strcmp (sym->section->name, ".opd") == 0)
6508 struct _opd_sec_data *opd = get_opd_info (sym->section);
6509 bfd_vma symval = sym->value;
6512 && opd->adjust != NULL
6513 && elf_section_data (sym->section)->relocs != NULL)
6515 /* opd_entry_value will use cached relocs that have been
6516 adjusted, but with raw symbols. That means both local
6517 and global symbols need adjusting. */
6518 long adjust = opd->adjust[OPD_NDX (symval)];
6524 if (opd_entry_value (sym->section, symval,
6525 &sec, code_off, TRUE) == (bfd_vma) -1)
6527 /* An old ABI binary with dot-syms has a size of 24 on the .opd
6528 symbol. This size has nothing to do with the code size of the
6529 function, which is what we're supposed to return, but the
6530 code size isn't available without looking up the dot-sym.
6531 However, doing that would be a waste of time particularly
6532 since elf_find_function will look at the dot-sym anyway.
6533 Now, elf_find_function will keep the largest size of any
6534 function sym found at the code address of interest, so return
6535 1 here to avoid it incorrectly caching a larger function size
6536 for a small function. This does mean we return the wrong
6537 size for a new-ABI function of size 24, but all that does is
6538 disable caching for such functions. */
6544 if (sym->section != sec)
6546 *code_off = sym->value;
6553 /* Return true if symbol is a strong function defined in an ELFv2
6554 object with st_other localentry bits of zero, ie. its local entry
6555 point coincides with its global entry point. */
6558 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
6561 && h->type == STT_FUNC
6562 && h->root.type == bfd_link_hash_defined
6563 && (STO_PPC64_LOCAL_MASK & h->other) == 0
6564 && !((struct ppc_link_hash_entry *) h)->non_zero_localentry
6565 && is_ppc64_elf (h->root.u.def.section->owner)
6566 && abiversion (h->root.u.def.section->owner) >= 2);
6569 /* Return true if symbol is defined in a regular object file. */
6572 is_static_defined (struct elf_link_hash_entry *h)
6574 return ((h->root.type == bfd_link_hash_defined
6575 || h->root.type == bfd_link_hash_defweak)
6576 && h->root.u.def.section != NULL
6577 && h->root.u.def.section->output_section != NULL);
6580 /* If FDH is a function descriptor symbol, return the associated code
6581 entry symbol if it is defined. Return NULL otherwise. */
6583 static struct ppc_link_hash_entry *
6584 defined_code_entry (struct ppc_link_hash_entry *fdh)
6586 if (fdh->is_func_descriptor)
6588 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
6589 if (fh->elf.root.type == bfd_link_hash_defined
6590 || fh->elf.root.type == bfd_link_hash_defweak)
6596 /* If FH is a function code entry symbol, return the associated
6597 function descriptor symbol if it is defined. Return NULL otherwise. */
6599 static struct ppc_link_hash_entry *
6600 defined_func_desc (struct ppc_link_hash_entry *fh)
6603 && fh->oh->is_func_descriptor)
6605 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
6606 if (fdh->elf.root.type == bfd_link_hash_defined
6607 || fdh->elf.root.type == bfd_link_hash_defweak)
6613 static bfd_boolean func_desc_adjust (struct elf_link_hash_entry *, void *);
6615 /* Garbage collect sections, after first dealing with dot-symbols. */
6618 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
6620 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6622 if (htab != NULL && htab->need_func_desc_adj)
6624 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6625 htab->need_func_desc_adj = 0;
6627 return bfd_elf_gc_sections (abfd, info);
6630 /* Mark all our entry sym sections, both opd and code section. */
6633 ppc64_elf_gc_keep (struct bfd_link_info *info)
6635 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6636 struct bfd_sym_chain *sym;
6641 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
6643 struct ppc_link_hash_entry *eh, *fh;
6646 eh = (struct ppc_link_hash_entry *)
6647 elf_link_hash_lookup (&htab->elf, sym->name, FALSE, FALSE, TRUE);
6650 if (eh->elf.root.type != bfd_link_hash_defined
6651 && eh->elf.root.type != bfd_link_hash_defweak)
6654 fh = defined_code_entry (eh);
6657 sec = fh->elf.root.u.def.section;
6658 sec->flags |= SEC_KEEP;
6660 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6661 && opd_entry_value (eh->elf.root.u.def.section,
6662 eh->elf.root.u.def.value,
6663 &sec, NULL, FALSE) != (bfd_vma) -1)
6664 sec->flags |= SEC_KEEP;
6666 sec = eh->elf.root.u.def.section;
6667 sec->flags |= SEC_KEEP;
6671 /* Mark sections containing dynamically referenced symbols. When
6672 building shared libraries, we must assume that any visible symbol is
6676 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
6678 struct bfd_link_info *info = (struct bfd_link_info *) inf;
6679 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
6680 struct ppc_link_hash_entry *fdh;
6681 struct bfd_elf_dynamic_list *d = info->dynamic_list;
6683 /* Dynamic linking info is on the func descriptor sym. */
6684 fdh = defined_func_desc (eh);
6688 if ((eh->elf.root.type == bfd_link_hash_defined
6689 || eh->elf.root.type == bfd_link_hash_defweak)
6690 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
6691 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
6692 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
6693 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
6694 && (!bfd_link_executable (info)
6695 || info->gc_keep_exported
6696 || info->export_dynamic
6699 && (*d->match) (&d->head, NULL, eh->elf.root.root.string)))
6700 && (eh->elf.versioned >= versioned
6701 || !bfd_hide_sym_by_version (info->version_info,
6702 eh->elf.root.root.string)))))
6705 struct ppc_link_hash_entry *fh;
6707 eh->elf.root.u.def.section->flags |= SEC_KEEP;
6709 /* Function descriptor syms cause the associated
6710 function code sym section to be marked. */
6711 fh = defined_code_entry (eh);
6714 code_sec = fh->elf.root.u.def.section;
6715 code_sec->flags |= SEC_KEEP;
6717 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6718 && opd_entry_value (eh->elf.root.u.def.section,
6719 eh->elf.root.u.def.value,
6720 &code_sec, NULL, FALSE) != (bfd_vma) -1)
6721 code_sec->flags |= SEC_KEEP;
6727 /* Return the section that should be marked against GC for a given
6731 ppc64_elf_gc_mark_hook (asection *sec,
6732 struct bfd_link_info *info,
6733 Elf_Internal_Rela *rel,
6734 struct elf_link_hash_entry *h,
6735 Elf_Internal_Sym *sym)
6739 /* Syms return NULL if we're marking .opd, so we avoid marking all
6740 function sections, as all functions are referenced in .opd. */
6742 if (get_opd_info (sec) != NULL)
6747 enum elf_ppc64_reloc_type r_type;
6748 struct ppc_link_hash_entry *eh, *fh, *fdh;
6750 r_type = ELF64_R_TYPE (rel->r_info);
6753 case R_PPC64_GNU_VTINHERIT:
6754 case R_PPC64_GNU_VTENTRY:
6758 switch (h->root.type)
6760 case bfd_link_hash_defined:
6761 case bfd_link_hash_defweak:
6762 eh = (struct ppc_link_hash_entry *) h;
6763 fdh = defined_func_desc (eh);
6766 /* -mcall-aixdesc code references the dot-symbol on
6767 a call reloc. Mark the function descriptor too
6768 against garbage collection. */
6770 if (fdh->elf.is_weakalias)
6771 weakdef (&fdh->elf)->mark = 1;
6775 /* Function descriptor syms cause the associated
6776 function code sym section to be marked. */
6777 fh = defined_code_entry (eh);
6780 /* They also mark their opd section. */
6781 eh->elf.root.u.def.section->gc_mark = 1;
6783 rsec = fh->elf.root.u.def.section;
6785 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6786 && opd_entry_value (eh->elf.root.u.def.section,
6787 eh->elf.root.u.def.value,
6788 &rsec, NULL, FALSE) != (bfd_vma) -1)
6789 eh->elf.root.u.def.section->gc_mark = 1;
6791 rsec = h->root.u.def.section;
6794 case bfd_link_hash_common:
6795 rsec = h->root.u.c.p->section;
6799 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6805 struct _opd_sec_data *opd;
6807 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6808 opd = get_opd_info (rsec);
6809 if (opd != NULL && opd->func_sec != NULL)
6813 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6820 /* The maximum size of .sfpr. */
6821 #define SFPR_MAX (218*4)
6823 struct sfpr_def_parms
6825 const char name[12];
6826 unsigned char lo, hi;
6827 bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
6828 bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
6831 /* Auto-generate _save*, _rest* functions in .sfpr.
6832 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6836 sfpr_define (struct bfd_link_info *info,
6837 const struct sfpr_def_parms *parm,
6840 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6842 size_t len = strlen (parm->name);
6843 bfd_boolean writing = FALSE;
6849 memcpy (sym, parm->name, len);
6852 for (i = parm->lo; i <= parm->hi; i++)
6854 struct ppc_link_hash_entry *h;
6856 sym[len + 0] = i / 10 + '0';
6857 sym[len + 1] = i % 10 + '0';
6858 h = (struct ppc_link_hash_entry *)
6859 elf_link_hash_lookup (&htab->elf, sym, writing, TRUE, TRUE);
6860 if (stub_sec != NULL)
6863 && h->elf.root.type == bfd_link_hash_defined
6864 && h->elf.root.u.def.section == htab->sfpr)
6866 struct elf_link_hash_entry *s;
6868 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6869 s = elf_link_hash_lookup (&htab->elf, buf, TRUE, TRUE, FALSE);
6872 if (s->root.type == bfd_link_hash_new
6873 || (s->root.type = bfd_link_hash_defined
6874 && s->root.u.def.section == stub_sec))
6876 s->root.type = bfd_link_hash_defined;
6877 s->root.u.def.section = stub_sec;
6878 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6879 + h->elf.root.u.def.value);
6882 s->ref_regular_nonweak = 1;
6883 s->forced_local = 1;
6885 s->root.linker_def = 1;
6893 if (!h->elf.def_regular)
6895 h->elf.root.type = bfd_link_hash_defined;
6896 h->elf.root.u.def.section = htab->sfpr;
6897 h->elf.root.u.def.value = htab->sfpr->size;
6898 h->elf.type = STT_FUNC;
6899 h->elf.def_regular = 1;
6901 _bfd_elf_link_hash_hide_symbol (info, &h->elf, TRUE);
6903 if (htab->sfpr->contents == NULL)
6905 htab->sfpr->contents = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6906 if (htab->sfpr->contents == NULL)
6913 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6915 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6917 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6918 htab->sfpr->size = p - htab->sfpr->contents;
6926 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6928 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6933 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6935 p = savegpr0 (abfd, p, r);
6936 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6938 bfd_put_32 (abfd, BLR, p);
6943 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6945 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6950 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6952 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6954 p = restgpr0 (abfd, p, r);
6955 bfd_put_32 (abfd, MTLR_R0, p);
6959 p = restgpr0 (abfd, p, 30);
6960 p = restgpr0 (abfd, p, 31);
6962 bfd_put_32 (abfd, BLR, p);
6967 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6969 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6974 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6976 p = savegpr1 (abfd, p, r);
6977 bfd_put_32 (abfd, BLR, p);
6982 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6984 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6989 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6991 p = restgpr1 (abfd, p, r);
6992 bfd_put_32 (abfd, BLR, p);
6997 savefpr (bfd *abfd, bfd_byte *p, int r)
6999 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7004 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
7006 p = savefpr (abfd, p, r);
7007 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
7009 bfd_put_32 (abfd, BLR, p);
7014 restfpr (bfd *abfd, bfd_byte *p, int r)
7016 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
7021 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
7023 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
7025 p = restfpr (abfd, p, r);
7026 bfd_put_32 (abfd, MTLR_R0, p);
7030 p = restfpr (abfd, p, 30);
7031 p = restfpr (abfd, p, 31);
7033 bfd_put_32 (abfd, BLR, p);
7038 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
7040 p = savefpr (abfd, p, r);
7041 bfd_put_32 (abfd, BLR, p);
7046 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
7048 p = restfpr (abfd, p, r);
7049 bfd_put_32 (abfd, BLR, p);
7054 savevr (bfd *abfd, bfd_byte *p, int r)
7056 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7058 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
7063 savevr_tail (bfd *abfd, bfd_byte *p, int r)
7065 p = savevr (abfd, p, r);
7066 bfd_put_32 (abfd, BLR, p);
7071 restvr (bfd *abfd, bfd_byte *p, int r)
7073 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
7075 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
7080 restvr_tail (bfd *abfd, bfd_byte *p, int r)
7082 p = restvr (abfd, p, r);
7083 bfd_put_32 (abfd, BLR, p);
7087 /* Called via elf_link_hash_traverse to transfer dynamic linking
7088 information on function code symbol entries to their corresponding
7089 function descriptor symbol entries. */
7092 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
7094 struct bfd_link_info *info;
7095 struct ppc_link_hash_table *htab;
7096 struct ppc_link_hash_entry *fh;
7097 struct ppc_link_hash_entry *fdh;
7098 bfd_boolean force_local;
7100 fh = (struct ppc_link_hash_entry *) h;
7101 if (fh->elf.root.type == bfd_link_hash_indirect)
7107 if (fh->elf.root.root.string[0] != '.'
7108 || fh->elf.root.root.string[1] == '\0')
7112 htab = ppc_hash_table (info);
7116 /* Find the corresponding function descriptor symbol. */
7117 fdh = lookup_fdh (fh, htab);
7119 /* Resolve undefined references to dot-symbols as the value
7120 in the function descriptor, if we have one in a regular object.
7121 This is to satisfy cases like ".quad .foo". Calls to functions
7122 in dynamic objects are handled elsewhere. */
7123 if ((fh->elf.root.type == bfd_link_hash_undefined
7124 || fh->elf.root.type == bfd_link_hash_undefweak)
7125 && (fdh->elf.root.type == bfd_link_hash_defined
7126 || fdh->elf.root.type == bfd_link_hash_defweak)
7127 && get_opd_info (fdh->elf.root.u.def.section) != NULL
7128 && opd_entry_value (fdh->elf.root.u.def.section,
7129 fdh->elf.root.u.def.value,
7130 &fh->elf.root.u.def.section,
7131 &fh->elf.root.u.def.value, FALSE) != (bfd_vma) -1)
7133 fh->elf.root.type = fdh->elf.root.type;
7134 fh->elf.forced_local = 1;
7135 fh->elf.def_regular = fdh->elf.def_regular;
7136 fh->elf.def_dynamic = fdh->elf.def_dynamic;
7139 if (!fh->elf.dynamic)
7141 struct plt_entry *ent;
7143 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
7144 if (ent->plt.refcount > 0)
7150 /* Create a descriptor as undefined if necessary. */
7152 && !bfd_link_executable (info)
7153 && (fh->elf.root.type == bfd_link_hash_undefined
7154 || fh->elf.root.type == bfd_link_hash_undefweak))
7156 fdh = make_fdh (info, fh);
7161 /* We can't support overriding of symbols on a fake descriptor. */
7164 && (fh->elf.root.type == bfd_link_hash_defined
7165 || fh->elf.root.type == bfd_link_hash_defweak))
7166 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, TRUE);
7168 /* Transfer dynamic linking information to the function descriptor. */
7171 fdh->elf.ref_regular |= fh->elf.ref_regular;
7172 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
7173 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
7174 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
7175 fdh->elf.dynamic |= fh->elf.dynamic;
7176 fdh->elf.needs_plt |= (fh->elf.needs_plt
7177 || fh->elf.type == STT_FUNC
7178 || fh->elf.type == STT_GNU_IFUNC);
7179 move_plt_plist (fh, fdh);
7181 if (!fdh->elf.forced_local
7182 && fh->elf.dynindx != -1)
7183 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
7187 /* Now that the info is on the function descriptor, clear the
7188 function code sym info. Any function code syms for which we
7189 don't have a definition in a regular file, we force local.
7190 This prevents a shared library from exporting syms that have
7191 been imported from another library. Function code syms that
7192 are really in the library we must leave global to prevent the
7193 linker dragging in a definition from a static library. */
7194 force_local = (!fh->elf.def_regular
7196 || !fdh->elf.def_regular
7197 || fdh->elf.forced_local);
7198 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7203 static const struct sfpr_def_parms save_res_funcs[] =
7205 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
7206 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
7207 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
7208 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
7209 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
7210 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
7211 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
7212 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
7213 { "._savef", 14, 31, savefpr, savefpr1_tail },
7214 { "._restf", 14, 31, restfpr, restfpr1_tail },
7215 { "_savevr_", 20, 31, savevr, savevr_tail },
7216 { "_restvr_", 20, 31, restvr, restvr_tail }
7219 /* Called near the start of bfd_elf_size_dynamic_sections. We use
7220 this hook to a) provide some gcc support functions, and b) transfer
7221 dynamic linking information gathered so far on function code symbol
7222 entries, to their corresponding function descriptor symbol entries. */
7225 ppc64_elf_func_desc_adjust (bfd *obfd ATTRIBUTE_UNUSED,
7226 struct bfd_link_info *info)
7228 struct ppc_link_hash_table *htab;
7230 htab = ppc_hash_table (info);
7234 /* Provide any missing _save* and _rest* functions. */
7235 if (htab->sfpr != NULL)
7239 htab->sfpr->size = 0;
7240 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
7241 if (!sfpr_define (info, &save_res_funcs[i], NULL))
7243 if (htab->sfpr->size == 0)
7244 htab->sfpr->flags |= SEC_EXCLUDE;
7247 if (bfd_link_relocatable (info))
7250 if (htab->elf.hgot != NULL)
7252 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, TRUE);
7253 /* Make .TOC. defined so as to prevent it being made dynamic.
7254 The wrong value here is fixed later in ppc64_elf_set_toc. */
7255 if (!htab->elf.hgot->def_regular
7256 || htab->elf.hgot->root.type != bfd_link_hash_defined)
7258 htab->elf.hgot->root.type = bfd_link_hash_defined;
7259 htab->elf.hgot->root.u.def.value = 0;
7260 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
7261 htab->elf.hgot->def_regular = 1;
7262 htab->elf.hgot->root.linker_def = 1;
7264 htab->elf.hgot->type = STT_OBJECT;
7265 htab->elf.hgot->other = ((htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1))
7269 if (htab->need_func_desc_adj)
7271 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7272 htab->need_func_desc_adj = 0;
7278 /* Find dynamic relocs for H that apply to read-only sections. */
7281 readonly_dynrelocs (struct elf_link_hash_entry *h)
7283 struct ppc_link_hash_entry *eh;
7284 struct elf_dyn_relocs *p;
7286 eh = (struct ppc_link_hash_entry *) h;
7287 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7289 asection *s = p->sec->output_section;
7291 if (s != NULL && (s->flags & SEC_READONLY) != 0)
7297 /* Return true if we have dynamic relocs against H or any of its weak
7298 aliases, that apply to read-only sections. Cannot be used after
7299 size_dynamic_sections. */
7302 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
7304 struct ppc_link_hash_entry *eh;
7306 eh = (struct ppc_link_hash_entry *) h;
7309 if (readonly_dynrelocs (&eh->elf))
7311 eh = (struct ppc_link_hash_entry *) eh->elf.u.alias;
7312 } while (eh != NULL && &eh->elf != h);
7317 /* Return whether EH has pc-relative dynamic relocs. */
7320 pc_dynrelocs (struct ppc_link_hash_entry *eh)
7322 struct elf_dyn_relocs *p;
7324 for (p = eh->dyn_relocs; p != NULL; p = p->next)
7325 if (p->pc_count != 0)
7330 /* Return true if a global entry stub will be created for H. Valid
7331 for ELFv2 before plt entries have been allocated. */
7334 global_entry_stub (struct elf_link_hash_entry *h)
7336 struct plt_entry *pent;
7338 if (!h->pointer_equality_needed
7342 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
7343 if (pent->plt.refcount > 0
7344 && pent->addend == 0)
7350 /* Adjust a symbol defined by a dynamic object and referenced by a
7351 regular object. The current definition is in some section of the
7352 dynamic object, but we're not including those sections. We have to
7353 change the definition to something the rest of the link can
7357 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
7358 struct elf_link_hash_entry *h)
7360 struct ppc_link_hash_table *htab;
7363 htab = ppc_hash_table (info);
7367 /* Deal with function syms. */
7368 if (h->type == STT_FUNC
7369 || h->type == STT_GNU_IFUNC
7372 bfd_boolean local = (((struct ppc_link_hash_entry *) h)->save_res
7373 || SYMBOL_CALLS_LOCAL (info, h)
7374 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
7375 /* Discard dyn_relocs when non-pic if we've decided that a
7376 function symbol is local and not an ifunc. We keep dynamic
7377 relocs for ifuncs when local rather than always emitting a
7378 plt call stub for them and defining the symbol on the call
7379 stub. We can't do that for ELFv1 anyway (a function symbol
7380 is defined on a descriptor, not code) and it can be faster at
7381 run-time due to not needing to bounce through a stub. The
7382 dyn_relocs for ifuncs will be applied even in a static
7384 if (!bfd_link_pic (info)
7385 && h->type != STT_GNU_IFUNC
7387 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7389 /* Clear procedure linkage table information for any symbol that
7390 won't need a .plt entry. */
7391 struct plt_entry *ent;
7392 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
7393 if (ent->plt.refcount > 0)
7396 || (h->type != STT_GNU_IFUNC
7398 && (htab->can_convert_all_inline_plt
7399 || (((struct ppc_link_hash_entry *) h)->tls_mask
7400 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
7402 h->plt.plist = NULL;
7404 h->pointer_equality_needed = 0;
7406 else if (abiversion (info->output_bfd) >= 2)
7408 /* Taking a function's address in a read/write section
7409 doesn't require us to define the function symbol in the
7410 executable on a global entry stub. A dynamic reloc can
7411 be used instead. The reason we prefer a few more dynamic
7412 relocs is that calling via a global entry stub costs a
7413 few more instructions, and pointer_equality_needed causes
7414 extra work in ld.so when resolving these symbols. */
7415 if (global_entry_stub (h))
7417 if (!readonly_dynrelocs (h))
7419 h->pointer_equality_needed = 0;
7420 /* If we haven't seen a branch reloc and the symbol
7421 isn't an ifunc then we don't need a plt entry. */
7423 h->plt.plist = NULL;
7425 else if (!bfd_link_pic (info))
7426 /* We are going to be defining the function symbol on the
7427 plt stub, so no dyn_relocs needed when non-pic. */
7428 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7431 /* ELFv2 function symbols can't have copy relocs. */
7434 else if (!h->needs_plt
7435 && !readonly_dynrelocs (h))
7437 /* If we haven't seen a branch reloc and the symbol isn't an
7438 ifunc then we don't need a plt entry. */
7439 h->plt.plist = NULL;
7440 h->pointer_equality_needed = 0;
7445 h->plt.plist = NULL;
7447 /* If this is a weak symbol, and there is a real definition, the
7448 processor independent code will have arranged for us to see the
7449 real definition first, and we can just use the same value. */
7450 if (h->is_weakalias)
7452 struct elf_link_hash_entry *def = weakdef (h);
7453 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
7454 h->root.u.def.section = def->root.u.def.section;
7455 h->root.u.def.value = def->root.u.def.value;
7456 if (def->root.u.def.section == htab->elf.sdynbss
7457 || def->root.u.def.section == htab->elf.sdynrelro)
7458 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7462 /* If we are creating a shared library, we must presume that the
7463 only references to the symbol are via the global offset table.
7464 For such cases we need not do anything here; the relocations will
7465 be handled correctly by relocate_section. */
7466 if (bfd_link_pic (info))
7469 /* If there are no references to this symbol that do not use the
7470 GOT, we don't need to generate a copy reloc. */
7471 if (!h->non_got_ref)
7474 /* Don't generate a copy reloc for symbols defined in the executable. */
7475 if (!h->def_dynamic || !h->ref_regular || h->def_regular
7477 /* If -z nocopyreloc was given, don't generate them either. */
7478 || info->nocopyreloc
7480 /* If we don't find any dynamic relocs in read-only sections, then
7481 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
7482 || (ELIMINATE_COPY_RELOCS && !alias_readonly_dynrelocs (h))
7484 /* Protected variables do not work with .dynbss. The copy in
7485 .dynbss won't be used by the shared library with the protected
7486 definition for the variable. Text relocations are preferable
7487 to an incorrect program. */
7488 || h->protected_def)
7491 if (h->plt.plist != NULL)
7493 /* We should never get here, but unfortunately there are versions
7494 of gcc out there that improperly (for this ABI) put initialized
7495 function pointers, vtable refs and suchlike in read-only
7496 sections. Allow them to proceed, but warn that this might
7497 break at runtime. */
7498 info->callbacks->einfo
7499 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
7500 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
7501 h->root.root.string);
7504 /* This is a reference to a symbol defined by a dynamic object which
7505 is not a function. */
7507 /* We must allocate the symbol in our .dynbss section, which will
7508 become part of the .bss section of the executable. There will be
7509 an entry for this symbol in the .dynsym section. The dynamic
7510 object will contain position independent code, so all references
7511 from the dynamic object to this symbol will go through the global
7512 offset table. The dynamic linker will use the .dynsym entry to
7513 determine the address it must put in the global offset table, so
7514 both the dynamic object and the regular object will refer to the
7515 same memory location for the variable. */
7516 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
7518 s = htab->elf.sdynrelro;
7519 srel = htab->elf.sreldynrelro;
7523 s = htab->elf.sdynbss;
7524 srel = htab->elf.srelbss;
7526 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
7528 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
7529 linker to copy the initial value out of the dynamic object
7530 and into the runtime process image. */
7531 srel->size += sizeof (Elf64_External_Rela);
7535 /* We no longer want dyn_relocs. */
7536 ((struct ppc_link_hash_entry *) h)->dyn_relocs = NULL;
7537 return _bfd_elf_adjust_dynamic_copy (info, h, s);
7540 /* If given a function descriptor symbol, hide both the function code
7541 sym and the descriptor. */
7543 ppc64_elf_hide_symbol (struct bfd_link_info *info,
7544 struct elf_link_hash_entry *h,
7545 bfd_boolean force_local)
7547 struct ppc_link_hash_entry *eh;
7548 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
7550 eh = (struct ppc_link_hash_entry *) h;
7551 if (eh->is_func_descriptor)
7553 struct ppc_link_hash_entry *fh = eh->oh;
7558 struct elf_link_hash_table *htab = elf_hash_table (info);
7561 /* We aren't supposed to use alloca in BFD because on
7562 systems which do not have alloca the version in libiberty
7563 calls xmalloc, which might cause the program to crash
7564 when it runs out of memory. This function doesn't have a
7565 return status, so there's no way to gracefully return an
7566 error. So cheat. We know that string[-1] can be safely
7567 accessed; It's either a string in an ELF string table,
7568 or allocated in an objalloc structure. */
7570 p = eh->elf.root.root.string - 1;
7573 fh = (struct ppc_link_hash_entry *)
7574 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7577 /* Unfortunately, if it so happens that the string we were
7578 looking for was allocated immediately before this string,
7579 then we overwrote the string terminator. That's the only
7580 reason the lookup should fail. */
7583 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
7584 while (q >= eh->elf.root.root.string && *q == *p)
7586 if (q < eh->elf.root.root.string && *p == '.')
7587 fh = (struct ppc_link_hash_entry *)
7588 elf_link_hash_lookup (htab, p, FALSE, FALSE, FALSE);
7597 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
7602 get_sym_h (struct elf_link_hash_entry **hp,
7603 Elf_Internal_Sym **symp,
7605 unsigned char **tls_maskp,
7606 Elf_Internal_Sym **locsymsp,
7607 unsigned long r_symndx,
7610 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7612 if (r_symndx >= symtab_hdr->sh_info)
7614 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7615 struct elf_link_hash_entry *h;
7617 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7618 h = elf_follow_link (h);
7626 if (symsecp != NULL)
7628 asection *symsec = NULL;
7629 if (h->root.type == bfd_link_hash_defined
7630 || h->root.type == bfd_link_hash_defweak)
7631 symsec = h->root.u.def.section;
7635 if (tls_maskp != NULL)
7637 struct ppc_link_hash_entry *eh;
7639 eh = (struct ppc_link_hash_entry *) h;
7640 *tls_maskp = &eh->tls_mask;
7645 Elf_Internal_Sym *sym;
7646 Elf_Internal_Sym *locsyms = *locsymsp;
7648 if (locsyms == NULL)
7650 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
7651 if (locsyms == NULL)
7652 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
7653 symtab_hdr->sh_info,
7654 0, NULL, NULL, NULL);
7655 if (locsyms == NULL)
7657 *locsymsp = locsyms;
7659 sym = locsyms + r_symndx;
7667 if (symsecp != NULL)
7668 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
7670 if (tls_maskp != NULL)
7672 struct got_entry **lgot_ents;
7673 unsigned char *tls_mask;
7676 lgot_ents = elf_local_got_ents (ibfd);
7677 if (lgot_ents != NULL)
7679 struct plt_entry **local_plt = (struct plt_entry **)
7680 (lgot_ents + symtab_hdr->sh_info);
7681 unsigned char *lgot_masks = (unsigned char *)
7682 (local_plt + symtab_hdr->sh_info);
7683 tls_mask = &lgot_masks[r_symndx];
7685 *tls_maskp = tls_mask;
7691 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
7692 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7693 type suitable for optimization, and 1 otherwise. */
7696 get_tls_mask (unsigned char **tls_maskp,
7697 unsigned long *toc_symndx,
7698 bfd_vma *toc_addend,
7699 Elf_Internal_Sym **locsymsp,
7700 const Elf_Internal_Rela *rel,
7703 unsigned long r_symndx;
7705 struct elf_link_hash_entry *h;
7706 Elf_Internal_Sym *sym;
7710 r_symndx = ELF64_R_SYM (rel->r_info);
7711 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7714 if ((*tls_maskp != NULL
7715 && (**tls_maskp & TLS_TLS) != 0
7716 && **tls_maskp != (TLS_TLS | TLS_MARK))
7718 || ppc64_elf_section_data (sec) == NULL
7719 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7722 /* Look inside a TOC section too. */
7725 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7726 off = h->root.u.def.value;
7729 off = sym->st_value;
7730 off += rel->r_addend;
7731 BFD_ASSERT (off % 8 == 0);
7732 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7733 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7734 if (toc_symndx != NULL)
7735 *toc_symndx = r_symndx;
7736 if (toc_addend != NULL)
7737 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7738 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7740 if ((h == NULL || is_static_defined (h))
7741 && (next_r == -1 || next_r == -2))
7746 /* Find (or create) an entry in the tocsave hash table. */
7748 static struct tocsave_entry *
7749 tocsave_find (struct ppc_link_hash_table *htab,
7750 enum insert_option insert,
7751 Elf_Internal_Sym **local_syms,
7752 const Elf_Internal_Rela *irela,
7755 unsigned long r_indx;
7756 struct elf_link_hash_entry *h;
7757 Elf_Internal_Sym *sym;
7758 struct tocsave_entry ent, *p;
7760 struct tocsave_entry **slot;
7762 r_indx = ELF64_R_SYM (irela->r_info);
7763 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7765 if (ent.sec == NULL || ent.sec->output_section == NULL)
7768 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7773 ent.offset = h->root.u.def.value;
7775 ent.offset = sym->st_value;
7776 ent.offset += irela->r_addend;
7778 hash = tocsave_htab_hash (&ent);
7779 slot = ((struct tocsave_entry **)
7780 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7786 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7795 /* Adjust all global syms defined in opd sections. In gcc generated
7796 code for the old ABI, these will already have been done. */
7799 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7801 struct ppc_link_hash_entry *eh;
7803 struct _opd_sec_data *opd;
7805 if (h->root.type == bfd_link_hash_indirect)
7808 if (h->root.type != bfd_link_hash_defined
7809 && h->root.type != bfd_link_hash_defweak)
7812 eh = (struct ppc_link_hash_entry *) h;
7813 if (eh->adjust_done)
7816 sym_sec = eh->elf.root.u.def.section;
7817 opd = get_opd_info (sym_sec);
7818 if (opd != NULL && opd->adjust != NULL)
7820 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7823 /* This entry has been deleted. */
7824 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7827 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7828 if (discarded_section (dsec))
7830 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7834 eh->elf.root.u.def.value = 0;
7835 eh->elf.root.u.def.section = dsec;
7838 eh->elf.root.u.def.value += adjust;
7839 eh->adjust_done = 1;
7844 /* Handles decrementing dynamic reloc counts for the reloc specified by
7845 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7846 have already been determined. */
7849 dec_dynrel_count (bfd_vma r_info,
7851 struct bfd_link_info *info,
7852 Elf_Internal_Sym **local_syms,
7853 struct elf_link_hash_entry *h,
7854 Elf_Internal_Sym *sym)
7856 enum elf_ppc64_reloc_type r_type;
7857 asection *sym_sec = NULL;
7859 /* Can this reloc be dynamic? This switch, and later tests here
7860 should be kept in sync with the code in check_relocs. */
7861 r_type = ELF64_R_TYPE (r_info);
7867 case R_PPC64_TPREL16:
7868 case R_PPC64_TPREL16_LO:
7869 case R_PPC64_TPREL16_HI:
7870 case R_PPC64_TPREL16_HA:
7871 case R_PPC64_TPREL16_DS:
7872 case R_PPC64_TPREL16_LO_DS:
7873 case R_PPC64_TPREL16_HIGH:
7874 case R_PPC64_TPREL16_HIGHA:
7875 case R_PPC64_TPREL16_HIGHER:
7876 case R_PPC64_TPREL16_HIGHERA:
7877 case R_PPC64_TPREL16_HIGHEST:
7878 case R_PPC64_TPREL16_HIGHESTA:
7879 case R_PPC64_TPREL64:
7880 case R_PPC64_DTPMOD64:
7881 case R_PPC64_DTPREL64:
7882 case R_PPC64_ADDR64:
7886 case R_PPC64_ADDR14:
7887 case R_PPC64_ADDR14_BRNTAKEN:
7888 case R_PPC64_ADDR14_BRTAKEN:
7889 case R_PPC64_ADDR16:
7890 case R_PPC64_ADDR16_DS:
7891 case R_PPC64_ADDR16_HA:
7892 case R_PPC64_ADDR16_HI:
7893 case R_PPC64_ADDR16_HIGH:
7894 case R_PPC64_ADDR16_HIGHA:
7895 case R_PPC64_ADDR16_HIGHER:
7896 case R_PPC64_ADDR16_HIGHERA:
7897 case R_PPC64_ADDR16_HIGHEST:
7898 case R_PPC64_ADDR16_HIGHESTA:
7899 case R_PPC64_ADDR16_LO:
7900 case R_PPC64_ADDR16_LO_DS:
7901 case R_PPC64_ADDR24:
7902 case R_PPC64_ADDR32:
7903 case R_PPC64_UADDR16:
7904 case R_PPC64_UADDR32:
7905 case R_PPC64_UADDR64:
7910 if (local_syms != NULL)
7912 unsigned long r_symndx;
7913 bfd *ibfd = sec->owner;
7915 r_symndx = ELF64_R_SYM (r_info);
7916 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7920 if ((bfd_link_pic (info)
7921 && (must_be_dyn_reloc (info, r_type)
7923 && (!SYMBOLIC_BIND (info, h)
7924 || h->root.type == bfd_link_hash_defweak
7925 || !h->def_regular))))
7926 || (ELIMINATE_COPY_RELOCS
7927 && !bfd_link_pic (info)
7929 && (h->root.type == bfd_link_hash_defweak
7930 || !h->def_regular)))
7937 struct elf_dyn_relocs *p;
7938 struct elf_dyn_relocs **pp;
7939 pp = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
7941 /* elf_gc_sweep may have already removed all dyn relocs associated
7942 with local syms for a given section. Also, symbol flags are
7943 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7944 report a dynreloc miscount. */
7945 if (*pp == NULL && info->gc_sections)
7948 while ((p = *pp) != NULL)
7952 if (!must_be_dyn_reloc (info, r_type))
7964 struct ppc_dyn_relocs *p;
7965 struct ppc_dyn_relocs **pp;
7967 bfd_boolean is_ifunc;
7969 if (local_syms == NULL)
7970 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7971 if (sym_sec == NULL)
7974 vpp = &elf_section_data (sym_sec)->local_dynrel;
7975 pp = (struct ppc_dyn_relocs **) vpp;
7977 if (*pp == NULL && info->gc_sections)
7980 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7981 while ((p = *pp) != NULL)
7983 if (p->sec == sec && p->ifunc == is_ifunc)
7994 /* xgettext:c-format */
7995 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7997 bfd_set_error (bfd_error_bad_value);
8001 /* Remove unused Official Procedure Descriptor entries. Currently we
8002 only remove those associated with functions in discarded link-once
8003 sections, or weakly defined functions that have been overridden. It
8004 would be possible to remove many more entries for statically linked
8008 ppc64_elf_edit_opd (struct bfd_link_info *info)
8011 bfd_boolean some_edited = FALSE;
8012 asection *need_pad = NULL;
8013 struct ppc_link_hash_table *htab;
8015 htab = ppc_hash_table (info);
8019 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8022 Elf_Internal_Rela *relstart, *rel, *relend;
8023 Elf_Internal_Shdr *symtab_hdr;
8024 Elf_Internal_Sym *local_syms;
8025 struct _opd_sec_data *opd;
8026 bfd_boolean need_edit, add_aux_fields, broken;
8027 bfd_size_type cnt_16b = 0;
8029 if (!is_ppc64_elf (ibfd))
8032 sec = bfd_get_section_by_name (ibfd, ".opd");
8033 if (sec == NULL || sec->size == 0)
8036 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
8039 if (sec->output_section == bfd_abs_section_ptr)
8042 /* Look through the section relocs. */
8043 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
8047 symtab_hdr = &elf_symtab_hdr (ibfd);
8049 /* Read the relocations. */
8050 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8052 if (relstart == NULL)
8055 /* First run through the relocs to check they are sane, and to
8056 determine whether we need to edit this opd section. */
8060 relend = relstart + sec->reloc_count;
8061 for (rel = relstart; rel < relend; )
8063 enum elf_ppc64_reloc_type r_type;
8064 unsigned long r_symndx;
8066 struct elf_link_hash_entry *h;
8067 Elf_Internal_Sym *sym;
8070 /* .opd contains an array of 16 or 24 byte entries. We're
8071 only interested in the reloc pointing to a function entry
8073 offset = rel->r_offset;
8074 if (rel + 1 == relend
8075 || rel[1].r_offset != offset + 8)
8077 /* If someone messes with .opd alignment then after a
8078 "ld -r" we might have padding in the middle of .opd.
8079 Also, there's nothing to prevent someone putting
8080 something silly in .opd with the assembler. No .opd
8081 optimization for them! */
8084 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
8089 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
8090 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
8093 /* xgettext:c-format */
8094 (_("%pB: unexpected reloc type %u in .opd section"),
8100 r_symndx = ELF64_R_SYM (rel->r_info);
8101 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8105 if (sym_sec == NULL || sym_sec->owner == NULL)
8107 const char *sym_name;
8109 sym_name = h->root.root.string;
8111 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
8115 /* xgettext:c-format */
8116 (_("%pB: undefined sym `%s' in .opd section"),
8122 /* opd entries are always for functions defined in the
8123 current input bfd. If the symbol isn't defined in the
8124 input bfd, then we won't be using the function in this
8125 bfd; It must be defined in a linkonce section in another
8126 bfd, or is weak. It's also possible that we are
8127 discarding the function due to a linker script /DISCARD/,
8128 which we test for via the output_section. */
8129 if (sym_sec->owner != ibfd
8130 || sym_sec->output_section == bfd_abs_section_ptr)
8134 if (rel + 1 == relend
8135 || (rel + 2 < relend
8136 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
8141 if (sec->size == offset + 24)
8146 if (sec->size == offset + 16)
8153 else if (rel + 1 < relend
8154 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
8155 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
8157 if (rel[0].r_offset == offset + 16)
8159 else if (rel[0].r_offset != offset + 24)
8166 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
8168 if (!broken && (need_edit || add_aux_fields))
8170 Elf_Internal_Rela *write_rel;
8171 Elf_Internal_Shdr *rel_hdr;
8172 bfd_byte *rptr, *wptr;
8173 bfd_byte *new_contents;
8176 new_contents = NULL;
8177 amt = OPD_NDX (sec->size) * sizeof (long);
8178 opd = &ppc64_elf_section_data (sec)->u.opd;
8179 opd->adjust = bfd_zalloc (sec->owner, amt);
8180 if (opd->adjust == NULL)
8183 /* This seems a waste of time as input .opd sections are all
8184 zeros as generated by gcc, but I suppose there's no reason
8185 this will always be so. We might start putting something in
8186 the third word of .opd entries. */
8187 if ((sec->flags & SEC_IN_MEMORY) == 0)
8190 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
8195 if (local_syms != NULL
8196 && symtab_hdr->contents != (unsigned char *) local_syms)
8198 if (elf_section_data (sec)->relocs != relstart)
8202 sec->contents = loc;
8203 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8206 elf_section_data (sec)->relocs = relstart;
8208 new_contents = sec->contents;
8211 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
8212 if (new_contents == NULL)
8216 wptr = new_contents;
8217 rptr = sec->contents;
8218 write_rel = relstart;
8219 for (rel = relstart; rel < relend; )
8221 unsigned long r_symndx;
8223 struct elf_link_hash_entry *h;
8224 struct ppc_link_hash_entry *fdh = NULL;
8225 Elf_Internal_Sym *sym;
8227 Elf_Internal_Rela *next_rel;
8230 r_symndx = ELF64_R_SYM (rel->r_info);
8231 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8236 if (next_rel + 1 == relend
8237 || (next_rel + 2 < relend
8238 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
8241 /* See if the .opd entry is full 24 byte or
8242 16 byte (with fd_aux entry overlapped with next
8245 if (next_rel == relend)
8247 if (sec->size == rel->r_offset + 16)
8250 else if (next_rel->r_offset == rel->r_offset + 16)
8254 && h->root.root.string[0] == '.')
8256 fdh = ((struct ppc_link_hash_entry *) h)->oh;
8259 fdh = ppc_follow_link (fdh);
8260 if (fdh->elf.root.type != bfd_link_hash_defined
8261 && fdh->elf.root.type != bfd_link_hash_defweak)
8266 skip = (sym_sec->owner != ibfd
8267 || sym_sec->output_section == bfd_abs_section_ptr);
8270 if (fdh != NULL && sym_sec->owner == ibfd)
8272 /* Arrange for the function descriptor sym
8274 fdh->elf.root.u.def.value = 0;
8275 fdh->elf.root.u.def.section = sym_sec;
8277 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
8279 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
8284 if (!dec_dynrel_count (rel->r_info, sec, info,
8288 if (++rel == next_rel)
8291 r_symndx = ELF64_R_SYM (rel->r_info);
8292 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8299 /* We'll be keeping this opd entry. */
8304 /* Redefine the function descriptor symbol to
8305 this location in the opd section. It is
8306 necessary to update the value here rather
8307 than using an array of adjustments as we do
8308 for local symbols, because various places
8309 in the generic ELF code use the value
8310 stored in u.def.value. */
8311 fdh->elf.root.u.def.value = wptr - new_contents;
8312 fdh->adjust_done = 1;
8315 /* Local syms are a bit tricky. We could
8316 tweak them as they can be cached, but
8317 we'd need to look through the local syms
8318 for the function descriptor sym which we
8319 don't have at the moment. So keep an
8320 array of adjustments. */
8321 adjust = (wptr - new_contents) - (rptr - sec->contents);
8322 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
8325 memcpy (wptr, rptr, opd_ent_size);
8326 wptr += opd_ent_size;
8327 if (add_aux_fields && opd_ent_size == 16)
8329 memset (wptr, '\0', 8);
8333 /* We need to adjust any reloc offsets to point to the
8335 for ( ; rel != next_rel; ++rel)
8337 rel->r_offset += adjust;
8338 if (write_rel != rel)
8339 memcpy (write_rel, rel, sizeof (*rel));
8344 rptr += opd_ent_size;
8347 sec->size = wptr - new_contents;
8348 sec->reloc_count = write_rel - relstart;
8351 free (sec->contents);
8352 sec->contents = new_contents;
8355 /* Fudge the header size too, as this is used later in
8356 elf_bfd_final_link if we are emitting relocs. */
8357 rel_hdr = _bfd_elf_single_rel_hdr (sec);
8358 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
8361 else if (elf_section_data (sec)->relocs != relstart)
8364 if (local_syms != NULL
8365 && symtab_hdr->contents != (unsigned char *) local_syms)
8367 if (!info->keep_memory)
8370 symtab_hdr->contents = (unsigned char *) local_syms;
8375 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
8377 /* If we are doing a final link and the last .opd entry is just 16 byte
8378 long, add a 8 byte padding after it. */
8379 if (need_pad != NULL && !bfd_link_relocatable (info))
8383 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
8385 BFD_ASSERT (need_pad->size > 0);
8387 p = bfd_malloc (need_pad->size + 8);
8391 if (! bfd_get_section_contents (need_pad->owner, need_pad,
8392 p, 0, need_pad->size))
8395 need_pad->contents = p;
8396 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
8400 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
8404 need_pad->contents = p;
8407 memset (need_pad->contents + need_pad->size, 0, 8);
8408 need_pad->size += 8;
8414 /* Analyze inline PLT call relocations to see whether calls to locally
8415 defined functions can be converted to direct calls. */
8418 ppc64_elf_inline_plt (struct bfd_link_info *info)
8420 struct ppc_link_hash_table *htab;
8423 bfd_vma low_vma, high_vma, limit;
8425 htab = ppc_hash_table (info);
8429 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
8430 reduced somewhat to cater for possible stubs that might be added
8431 between the call and its destination. */
8432 if (htab->params->group_size < 0)
8434 limit = -htab->params->group_size;
8440 limit = htab->params->group_size;
8447 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
8448 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
8450 if (low_vma > sec->vma)
8452 if (high_vma < sec->vma + sec->size)
8453 high_vma = sec->vma + sec->size;
8456 /* If a "bl" can reach anywhere in local code sections, then we can
8457 convert all inline PLT sequences to direct calls when the symbol
8459 if (high_vma - low_vma < limit)
8461 htab->can_convert_all_inline_plt = 1;
8465 /* Otherwise, go looking through relocs for cases where a direct
8466 call won't reach. Mark the symbol on any such reloc to disable
8467 the optimization and keep the PLT entry as it seems likely that
8468 this will be better than creating trampolines. Note that this
8469 will disable the optimization for all inline PLT calls to a
8470 particular symbol, not just those that won't reach. The
8471 difficulty in doing a more precise optimization is that the
8472 linker needs to make a decision depending on whether a
8473 particular R_PPC64_PLTCALL insn can be turned into a direct
8474 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
8475 the sequence, and there is nothing that ties those relocs
8476 together except their symbol. */
8478 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8480 Elf_Internal_Shdr *symtab_hdr;
8481 Elf_Internal_Sym *local_syms;
8483 if (!is_ppc64_elf (ibfd))
8487 symtab_hdr = &elf_symtab_hdr (ibfd);
8489 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8490 if (ppc64_elf_section_data (sec)->has_pltcall
8491 && !bfd_is_abs_section (sec->output_section))
8493 Elf_Internal_Rela *relstart, *rel, *relend;
8495 /* Read the relocations. */
8496 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8498 if (relstart == NULL)
8501 relend = relstart + sec->reloc_count;
8502 for (rel = relstart; rel < relend; )
8504 enum elf_ppc64_reloc_type r_type;
8505 unsigned long r_symndx;
8507 struct elf_link_hash_entry *h;
8508 Elf_Internal_Sym *sym;
8509 unsigned char *tls_maskp;
8511 r_type = ELF64_R_TYPE (rel->r_info);
8512 if (r_type != R_PPC64_PLTCALL)
8515 r_symndx = ELF64_R_SYM (rel->r_info);
8516 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
8519 if (elf_section_data (sec)->relocs != relstart)
8521 if (local_syms != NULL
8522 && symtab_hdr->contents != (unsigned char *) local_syms)
8527 if (sym_sec != NULL && sym_sec->output_section != NULL)
8531 to = h->root.u.def.value;
8534 to += (rel->r_addend
8535 + sym_sec->output_offset
8536 + sym_sec->output_section->vma);
8537 from = (rel->r_offset
8538 + sec->output_offset
8539 + sec->output_section->vma);
8540 if (to - from + limit < 2 * limit)
8541 *tls_maskp &= ~PLT_KEEP;
8544 if (elf_section_data (sec)->relocs != relstart)
8548 if (local_syms != NULL
8549 && symtab_hdr->contents != (unsigned char *) local_syms)
8551 if (!info->keep_memory)
8554 symtab_hdr->contents = (unsigned char *) local_syms;
8561 /* Set htab->tls_get_addr and call the generic ELF tls_setup function. */
8564 ppc64_elf_tls_setup (struct bfd_link_info *info)
8566 struct ppc_link_hash_table *htab;
8568 htab = ppc_hash_table (info);
8572 if (abiversion (info->output_bfd) == 1)
8575 if (htab->params->no_multi_toc)
8576 htab->do_multi_toc = 0;
8577 else if (!htab->do_multi_toc)
8578 htab->params->no_multi_toc = 1;
8580 /* Default to --no-plt-localentry, as this option can cause problems
8581 with symbol interposition. For example, glibc libpthread.so and
8582 libc.so duplicate many pthread symbols, with a fallback
8583 implementation in libc.so. In some cases the fallback does more
8584 work than the pthread implementation. __pthread_condattr_destroy
8585 is one such symbol: the libpthread.so implementation is
8586 localentry:0 while the libc.so implementation is localentry:8.
8587 An app that "cleverly" uses dlopen to only load necessary
8588 libraries at runtime may omit loading libpthread.so when not
8589 running multi-threaded, which then results in the libc.so
8590 fallback symbols being used and ld.so complaining. Now there
8591 are workarounds in ld (see non_zero_localentry) to detect the
8592 pthread situation, but that may not be the only case where
8593 --plt-localentry can cause trouble. */
8594 if (htab->params->plt_localentry0 < 0)
8595 htab->params->plt_localentry0 = 0;
8596 if (htab->params->plt_localentry0
8597 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
8598 FALSE, FALSE, FALSE) == NULL)
8600 (_("warning: --plt-localentry is especially dangerous without "
8601 "ld.so support to detect ABI violations"));
8603 htab->tls_get_addr = ((struct ppc_link_hash_entry *)
8604 elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
8605 FALSE, FALSE, TRUE));
8606 /* Move dynamic linking info to the function descriptor sym. */
8607 if (htab->tls_get_addr != NULL)
8608 func_desc_adjust (&htab->tls_get_addr->elf, info);
8609 htab->tls_get_addr_fd = ((struct ppc_link_hash_entry *)
8610 elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
8611 FALSE, FALSE, TRUE));
8612 if (htab->params->tls_get_addr_opt)
8614 struct elf_link_hash_entry *opt, *opt_fd, *tga, *tga_fd;
8616 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
8617 FALSE, FALSE, TRUE);
8619 func_desc_adjust (opt, info);
8620 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
8621 FALSE, FALSE, TRUE);
8623 && (opt_fd->root.type == bfd_link_hash_defined
8624 || opt_fd->root.type == bfd_link_hash_defweak))
8626 /* If glibc supports an optimized __tls_get_addr call stub,
8627 signalled by the presence of __tls_get_addr_opt, and we'll
8628 be calling __tls_get_addr via a plt call stub, then
8629 make __tls_get_addr point to __tls_get_addr_opt. */
8630 tga_fd = &htab->tls_get_addr_fd->elf;
8631 if (htab->elf.dynamic_sections_created
8633 && (tga_fd->type == STT_FUNC
8634 || tga_fd->needs_plt)
8635 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8636 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd)))
8638 struct plt_entry *ent;
8640 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8641 if (ent->plt.refcount > 0)
8645 tga_fd->root.type = bfd_link_hash_indirect;
8646 tga_fd->root.u.i.link = &opt_fd->root;
8647 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8649 if (opt_fd->dynindx != -1)
8651 /* Use __tls_get_addr_opt in dynamic relocations. */
8652 opt_fd->dynindx = -1;
8653 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8654 opt_fd->dynstr_index);
8655 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8658 htab->tls_get_addr_fd = (struct ppc_link_hash_entry *) opt_fd;
8659 tga = &htab->tls_get_addr->elf;
8660 if (opt != NULL && tga != NULL)
8662 tga->root.type = bfd_link_hash_indirect;
8663 tga->root.u.i.link = &opt->root;
8664 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8666 _bfd_elf_link_hash_hide_symbol (info, opt,
8668 htab->tls_get_addr = (struct ppc_link_hash_entry *) opt;
8670 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8671 htab->tls_get_addr_fd->is_func_descriptor = 1;
8672 if (htab->tls_get_addr != NULL)
8674 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8675 htab->tls_get_addr->is_func = 1;
8680 else if (htab->params->tls_get_addr_opt < 0)
8681 htab->params->tls_get_addr_opt = 0;
8683 return _bfd_elf_tls_setup (info->output_bfd, info);
8686 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8690 branch_reloc_hash_match (const bfd *ibfd,
8691 const Elf_Internal_Rela *rel,
8692 const struct ppc_link_hash_entry *hash1,
8693 const struct ppc_link_hash_entry *hash2)
8695 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8696 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8697 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8699 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8701 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8702 struct elf_link_hash_entry *h;
8704 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8705 h = elf_follow_link (h);
8706 if (h == &hash1->elf || h == &hash2->elf)
8712 /* Run through all the TLS relocs looking for optimization
8713 opportunities. The linker has been hacked (see ppc64elf.em) to do
8714 a preliminary section layout so that we know the TLS segment
8715 offsets. We can't optimize earlier because some optimizations need
8716 to know the tp offset, and we need to optimize before allocating
8717 dynamic relocations. */
8720 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8724 struct ppc_link_hash_table *htab;
8725 unsigned char *toc_ref;
8728 if (!bfd_link_executable (info))
8731 htab = ppc_hash_table (info);
8735 /* Make two passes over the relocs. On the first pass, mark toc
8736 entries involved with tls relocs, and check that tls relocs
8737 involved in setting up a tls_get_addr call are indeed followed by
8738 such a call. If they are not, we can't do any tls optimization.
8739 On the second pass twiddle tls_mask flags to notify
8740 relocate_section that optimization can be done, and adjust got
8741 and plt refcounts. */
8743 for (pass = 0; pass < 2; ++pass)
8744 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8746 Elf_Internal_Sym *locsyms = NULL;
8747 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8749 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8750 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8752 Elf_Internal_Rela *relstart, *rel, *relend;
8753 bfd_boolean found_tls_get_addr_arg = 0;
8755 /* Read the relocations. */
8756 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8758 if (relstart == NULL)
8764 relend = relstart + sec->reloc_count;
8765 for (rel = relstart; rel < relend; rel++)
8767 enum elf_ppc64_reloc_type r_type;
8768 unsigned long r_symndx;
8769 struct elf_link_hash_entry *h;
8770 Elf_Internal_Sym *sym;
8772 unsigned char *tls_mask;
8773 unsigned char tls_set, tls_clear, tls_type = 0;
8775 bfd_boolean ok_tprel, is_local;
8776 long toc_ref_index = 0;
8777 int expecting_tls_get_addr = 0;
8778 bfd_boolean ret = FALSE;
8780 r_symndx = ELF64_R_SYM (rel->r_info);
8781 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8785 if (elf_section_data (sec)->relocs != relstart)
8787 if (toc_ref != NULL)
8790 && (elf_symtab_hdr (ibfd).contents
8791 != (unsigned char *) locsyms))
8798 if (h->root.type == bfd_link_hash_defined
8799 || h->root.type == bfd_link_hash_defweak)
8800 value = h->root.u.def.value;
8801 else if (h->root.type == bfd_link_hash_undefweak)
8805 found_tls_get_addr_arg = 0;
8810 /* Symbols referenced by TLS relocs must be of type
8811 STT_TLS. So no need for .opd local sym adjust. */
8812 value = sym->st_value;
8821 && h->root.type == bfd_link_hash_undefweak)
8823 else if (sym_sec != NULL
8824 && sym_sec->output_section != NULL)
8826 value += sym_sec->output_offset;
8827 value += sym_sec->output_section->vma;
8828 value -= htab->elf.tls_sec->vma;
8829 ok_tprel = (value + TP_OFFSET + ((bfd_vma) 1 << 31)
8830 < (bfd_vma) 1 << 32);
8834 r_type = ELF64_R_TYPE (rel->r_info);
8835 /* If this section has old-style __tls_get_addr calls
8836 without marker relocs, then check that each
8837 __tls_get_addr call reloc is preceded by a reloc
8838 that conceivably belongs to the __tls_get_addr arg
8839 setup insn. If we don't find matching arg setup
8840 relocs, don't do any tls optimization. */
8842 && sec->has_tls_get_addr_call
8844 && (h == &htab->tls_get_addr->elf
8845 || h == &htab->tls_get_addr_fd->elf)
8846 && !found_tls_get_addr_arg
8847 && is_branch_reloc (r_type))
8849 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8850 "TLS optimization disabled\n"),
8851 ibfd, sec, rel->r_offset);
8856 found_tls_get_addr_arg = 0;
8859 case R_PPC64_GOT_TLSLD16:
8860 case R_PPC64_GOT_TLSLD16_LO:
8861 expecting_tls_get_addr = 1;
8862 found_tls_get_addr_arg = 1;
8865 case R_PPC64_GOT_TLSLD16_HI:
8866 case R_PPC64_GOT_TLSLD16_HA:
8867 /* These relocs should never be against a symbol
8868 defined in a shared lib. Leave them alone if
8869 that turns out to be the case. */
8876 tls_type = TLS_TLS | TLS_LD;
8879 case R_PPC64_GOT_TLSGD16:
8880 case R_PPC64_GOT_TLSGD16_LO:
8881 expecting_tls_get_addr = 1;
8882 found_tls_get_addr_arg = 1;
8885 case R_PPC64_GOT_TLSGD16_HI:
8886 case R_PPC64_GOT_TLSGD16_HA:
8892 tls_set = TLS_TLS | TLS_TPRELGD;
8894 tls_type = TLS_TLS | TLS_GD;
8897 case R_PPC64_GOT_TPREL16_DS:
8898 case R_PPC64_GOT_TPREL16_LO_DS:
8899 case R_PPC64_GOT_TPREL16_HI:
8900 case R_PPC64_GOT_TPREL16_HA:
8905 tls_clear = TLS_TPREL;
8906 tls_type = TLS_TLS | TLS_TPREL;
8913 if (rel + 1 < relend
8914 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8917 && ELF64_R_TYPE (rel[1].r_info) != R_PPC64_PLTSEQ)
8919 r_symndx = ELF64_R_SYM (rel[1].r_info);
8920 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8925 struct plt_entry *ent = NULL;
8927 for (ent = h->plt.plist;
8930 if (ent->addend == rel[1].r_addend)
8934 && ent->plt.refcount > 0)
8935 ent->plt.refcount -= 1;
8940 found_tls_get_addr_arg = 1;
8945 case R_PPC64_TOC16_LO:
8946 if (sym_sec == NULL || sym_sec != toc)
8949 /* Mark this toc entry as referenced by a TLS
8950 code sequence. We can do that now in the
8951 case of R_PPC64_TLS, and after checking for
8952 tls_get_addr for the TOC16 relocs. */
8953 if (toc_ref == NULL)
8954 toc_ref = bfd_zmalloc (toc->output_section->rawsize / 8);
8955 if (toc_ref == NULL)
8959 value = h->root.u.def.value;
8961 value = sym->st_value;
8962 value += rel->r_addend;
8965 BFD_ASSERT (value < toc->size
8966 && toc->output_offset % 8 == 0);
8967 toc_ref_index = (value + toc->output_offset) / 8;
8968 if (r_type == R_PPC64_TLS
8969 || r_type == R_PPC64_TLSGD
8970 || r_type == R_PPC64_TLSLD)
8972 toc_ref[toc_ref_index] = 1;
8976 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8981 expecting_tls_get_addr = 2;
8984 case R_PPC64_TPREL64:
8988 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8993 tls_set = TLS_EXPLICIT;
8994 tls_clear = TLS_TPREL;
8999 case R_PPC64_DTPMOD64:
9003 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
9005 if (rel + 1 < relend
9007 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
9008 && rel[1].r_offset == rel->r_offset + 8)
9012 tls_set = TLS_EXPLICIT | TLS_GD;
9015 tls_set = TLS_EXPLICIT | TLS_GD | TLS_TPRELGD;
9024 tls_set = TLS_EXPLICIT;
9035 if (!expecting_tls_get_addr
9036 || !sec->has_tls_get_addr_call)
9039 if (rel + 1 < relend
9040 && branch_reloc_hash_match (ibfd, rel + 1,
9042 htab->tls_get_addr_fd))
9044 if (expecting_tls_get_addr == 2)
9046 /* Check for toc tls entries. */
9047 unsigned char *toc_tls;
9050 retval = get_tls_mask (&toc_tls, NULL, NULL,
9055 if (toc_tls != NULL)
9057 if ((*toc_tls & TLS_TLS) != 0
9058 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
9059 found_tls_get_addr_arg = 1;
9061 toc_ref[toc_ref_index] = 1;
9067 /* Uh oh, we didn't find the expected call. We
9068 could just mark this symbol to exclude it
9069 from tls optimization but it's safer to skip
9070 the entire optimization. */
9071 /* xgettext:c-format */
9072 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
9073 "TLS optimization disabled\n"),
9074 ibfd, sec, rel->r_offset);
9079 /* If we don't have old-style __tls_get_addr calls
9080 without TLSGD/TLSLD marker relocs, and we haven't
9081 found a new-style __tls_get_addr call with a
9082 marker for this symbol, then we either have a
9083 broken object file or an -mlongcall style
9084 indirect call to __tls_get_addr without a marker.
9085 Disable optimization in this case. */
9086 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
9087 && (tls_set & TLS_EXPLICIT) == 0
9088 && !sec->has_tls_get_addr_call
9089 && ((*tls_mask & (TLS_TLS | TLS_MARK))
9090 != (TLS_TLS | TLS_MARK)))
9093 if (expecting_tls_get_addr)
9095 struct plt_entry *ent = NULL;
9097 if (htab->tls_get_addr != NULL)
9098 for (ent = htab->tls_get_addr->elf.plt.plist;
9101 if (ent->addend == 0)
9104 if (ent == NULL && htab->tls_get_addr_fd != NULL)
9105 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
9108 if (ent->addend == 0)
9112 && ent->plt.refcount > 0)
9113 ent->plt.refcount -= 1;
9119 if ((tls_set & TLS_EXPLICIT) == 0)
9121 struct got_entry *ent;
9123 /* Adjust got entry for this reloc. */
9127 ent = elf_local_got_ents (ibfd)[r_symndx];
9129 for (; ent != NULL; ent = ent->next)
9130 if (ent->addend == rel->r_addend
9131 && ent->owner == ibfd
9132 && ent->tls_type == tls_type)
9139 /* We managed to get rid of a got entry. */
9140 if (ent->got.refcount > 0)
9141 ent->got.refcount -= 1;
9146 /* If we got rid of a DTPMOD/DTPREL reloc pair then
9147 we'll lose one or two dyn relocs. */
9148 if (!dec_dynrel_count (rel->r_info, sec, info,
9152 if (tls_set == (TLS_EXPLICIT | TLS_GD))
9154 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
9160 *tls_mask |= tls_set;
9161 *tls_mask &= ~tls_clear;
9164 if (elf_section_data (sec)->relocs != relstart)
9169 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
9171 if (!info->keep_memory)
9174 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
9178 if (toc_ref != NULL)
9180 htab->do_tls_opt = 1;
9184 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
9185 the values of any global symbols in a toc section that has been
9186 edited. Globals in toc sections should be a rarity, so this function
9187 sets a flag if any are found in toc sections other than the one just
9188 edited, so that further hash table traversals can be avoided. */
9190 struct adjust_toc_info
9193 unsigned long *skip;
9194 bfd_boolean global_toc_syms;
9197 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
9200 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
9202 struct ppc_link_hash_entry *eh;
9203 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
9206 if (h->root.type != bfd_link_hash_defined
9207 && h->root.type != bfd_link_hash_defweak)
9210 eh = (struct ppc_link_hash_entry *) h;
9211 if (eh->adjust_done)
9214 if (eh->elf.root.u.def.section == toc_inf->toc)
9216 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
9217 i = toc_inf->toc->rawsize >> 3;
9219 i = eh->elf.root.u.def.value >> 3;
9221 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
9224 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
9227 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
9228 eh->elf.root.u.def.value = (bfd_vma) i << 3;
9231 eh->elf.root.u.def.value -= toc_inf->skip[i];
9232 eh->adjust_done = 1;
9234 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
9235 toc_inf->global_toc_syms = TRUE;
9240 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
9241 on a _LO variety toc/got reloc. */
9244 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
9246 return ((insn & (0x3f << 26)) == 12u << 26 /* addic */
9247 || (insn & (0x3f << 26)) == 14u << 26 /* addi */
9248 || (insn & (0x3f << 26)) == 32u << 26 /* lwz */
9249 || (insn & (0x3f << 26)) == 34u << 26 /* lbz */
9250 || (insn & (0x3f << 26)) == 36u << 26 /* stw */
9251 || (insn & (0x3f << 26)) == 38u << 26 /* stb */
9252 || (insn & (0x3f << 26)) == 40u << 26 /* lhz */
9253 || (insn & (0x3f << 26)) == 42u << 26 /* lha */
9254 || (insn & (0x3f << 26)) == 44u << 26 /* sth */
9255 || (insn & (0x3f << 26)) == 46u << 26 /* lmw */
9256 || (insn & (0x3f << 26)) == 47u << 26 /* stmw */
9257 || (insn & (0x3f << 26)) == 48u << 26 /* lfs */
9258 || (insn & (0x3f << 26)) == 50u << 26 /* lfd */
9259 || (insn & (0x3f << 26)) == 52u << 26 /* stfs */
9260 || (insn & (0x3f << 26)) == 54u << 26 /* stfd */
9261 || (insn & (0x3f << 26)) == 56u << 26 /* lq,lfq */
9262 || ((insn & (0x3f << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
9263 /* Exclude lfqu by testing reloc. If relocs are ever
9264 defined for the reduced D field in psq_lu then those
9265 will need testing too. */
9266 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9267 || ((insn & (0x3f << 26)) == 58u << 26 /* ld,lwa */
9269 || (insn & (0x3f << 26)) == 60u << 26 /* stfq */
9270 || ((insn & (0x3f << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
9271 /* Exclude stfqu. psq_stu as above for psq_lu. */
9272 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
9273 || ((insn & (0x3f << 26)) == 62u << 26 /* std,stq */
9274 && (insn & 1) == 0));
9277 /* Examine all relocs referencing .toc sections in order to remove
9278 unused .toc entries. */
9281 ppc64_elf_edit_toc (struct bfd_link_info *info)
9284 struct adjust_toc_info toc_inf;
9285 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9287 htab->do_toc_opt = 1;
9288 toc_inf.global_toc_syms = TRUE;
9289 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9291 asection *toc, *sec;
9292 Elf_Internal_Shdr *symtab_hdr;
9293 Elf_Internal_Sym *local_syms;
9294 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
9295 unsigned long *skip, *drop;
9296 unsigned char *used;
9297 unsigned char *keep, last, some_unused;
9299 if (!is_ppc64_elf (ibfd))
9302 toc = bfd_get_section_by_name (ibfd, ".toc");
9305 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
9306 || discarded_section (toc))
9311 symtab_hdr = &elf_symtab_hdr (ibfd);
9313 /* Look at sections dropped from the final link. */
9316 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9318 if (sec->reloc_count == 0
9319 || !discarded_section (sec)
9320 || get_opd_info (sec)
9321 || (sec->flags & SEC_ALLOC) == 0
9322 || (sec->flags & SEC_DEBUGGING) != 0)
9325 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, FALSE);
9326 if (relstart == NULL)
9329 /* Run through the relocs to see which toc entries might be
9331 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9333 enum elf_ppc64_reloc_type r_type;
9334 unsigned long r_symndx;
9336 struct elf_link_hash_entry *h;
9337 Elf_Internal_Sym *sym;
9340 r_type = ELF64_R_TYPE (rel->r_info);
9347 case R_PPC64_TOC16_LO:
9348 case R_PPC64_TOC16_HI:
9349 case R_PPC64_TOC16_HA:
9350 case R_PPC64_TOC16_DS:
9351 case R_PPC64_TOC16_LO_DS:
9355 r_symndx = ELF64_R_SYM (rel->r_info);
9356 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9364 val = h->root.u.def.value;
9366 val = sym->st_value;
9367 val += rel->r_addend;
9369 if (val >= toc->size)
9372 /* Anything in the toc ought to be aligned to 8 bytes.
9373 If not, don't mark as unused. */
9379 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9384 skip[val >> 3] = ref_from_discarded;
9387 if (elf_section_data (sec)->relocs != relstart)
9391 /* For largetoc loads of address constants, we can convert
9392 . addis rx,2,addr@got@ha
9393 . ld ry,addr@got@l(rx)
9395 . addis rx,2,addr@toc@ha
9396 . addi ry,rx,addr@toc@l
9397 when addr is within 2G of the toc pointer. This then means
9398 that the word storing "addr" in the toc is no longer needed. */
9400 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9401 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9402 && toc->reloc_count != 0)
9404 /* Read toc relocs. */
9405 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9407 if (toc_relocs == NULL)
9410 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9412 enum elf_ppc64_reloc_type r_type;
9413 unsigned long r_symndx;
9415 struct elf_link_hash_entry *h;
9416 Elf_Internal_Sym *sym;
9419 r_type = ELF64_R_TYPE (rel->r_info);
9420 if (r_type != R_PPC64_ADDR64)
9423 r_symndx = ELF64_R_SYM (rel->r_info);
9424 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9429 || sym_sec->output_section == NULL
9430 || discarded_section (sym_sec))
9433 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9438 if (h->type == STT_GNU_IFUNC)
9440 val = h->root.u.def.value;
9444 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9446 val = sym->st_value;
9448 val += rel->r_addend;
9449 val += sym_sec->output_section->vma + sym_sec->output_offset;
9451 /* We don't yet know the exact toc pointer value, but we
9452 know it will be somewhere in the toc section. Don't
9453 optimize if the difference from any possible toc
9454 pointer is outside [ff..f80008000, 7fff7fff]. */
9455 addr = toc->output_section->vma + TOC_BASE_OFF;
9456 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9459 addr = toc->output_section->vma + toc->output_section->rawsize;
9460 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9465 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9470 skip[rel->r_offset >> 3]
9471 |= can_optimize | ((rel - toc_relocs) << 2);
9478 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9482 if (local_syms != NULL
9483 && symtab_hdr->contents != (unsigned char *) local_syms)
9487 && elf_section_data (sec)->relocs != relstart)
9489 if (toc_relocs != NULL
9490 && elf_section_data (toc)->relocs != toc_relocs)
9497 /* Now check all kept sections that might reference the toc.
9498 Check the toc itself last. */
9499 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9502 sec = (sec == toc ? NULL
9503 : sec->next == NULL ? toc
9504 : sec->next == toc && toc->next ? toc->next
9509 if (sec->reloc_count == 0
9510 || discarded_section (sec)
9511 || get_opd_info (sec)
9512 || (sec->flags & SEC_ALLOC) == 0
9513 || (sec->flags & SEC_DEBUGGING) != 0)
9516 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9518 if (relstart == NULL)
9524 /* Mark toc entries referenced as used. */
9528 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9530 enum elf_ppc64_reloc_type r_type;
9531 unsigned long r_symndx;
9533 struct elf_link_hash_entry *h;
9534 Elf_Internal_Sym *sym;
9536 enum {no_check, check_lo, check_ha} insn_check;
9538 r_type = ELF64_R_TYPE (rel->r_info);
9542 insn_check = no_check;
9545 case R_PPC64_GOT_TLSLD16_HA:
9546 case R_PPC64_GOT_TLSGD16_HA:
9547 case R_PPC64_GOT_TPREL16_HA:
9548 case R_PPC64_GOT_DTPREL16_HA:
9549 case R_PPC64_GOT16_HA:
9550 case R_PPC64_TOC16_HA:
9551 insn_check = check_ha;
9554 case R_PPC64_GOT_TLSLD16_LO:
9555 case R_PPC64_GOT_TLSGD16_LO:
9556 case R_PPC64_GOT_TPREL16_LO_DS:
9557 case R_PPC64_GOT_DTPREL16_LO_DS:
9558 case R_PPC64_GOT16_LO:
9559 case R_PPC64_GOT16_LO_DS:
9560 case R_PPC64_TOC16_LO:
9561 case R_PPC64_TOC16_LO_DS:
9562 insn_check = check_lo;
9566 if (insn_check != no_check)
9568 bfd_vma off = rel->r_offset & ~3;
9569 unsigned char buf[4];
9572 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9577 insn = bfd_get_32 (ibfd, buf);
9578 if (insn_check == check_lo
9579 ? !ok_lo_toc_insn (insn, r_type)
9580 : ((insn & ((0x3f << 26) | 0x1f << 16))
9581 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9585 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9586 sprintf (str, "%#08x", insn);
9587 info->callbacks->einfo
9588 /* xgettext:c-format */
9589 (_("%H: toc optimization is not supported for"
9590 " %s instruction\n"),
9591 ibfd, sec, rel->r_offset & ~3, str);
9598 case R_PPC64_TOC16_LO:
9599 case R_PPC64_TOC16_HI:
9600 case R_PPC64_TOC16_HA:
9601 case R_PPC64_TOC16_DS:
9602 case R_PPC64_TOC16_LO_DS:
9603 /* In case we're taking addresses of toc entries. */
9604 case R_PPC64_ADDR64:
9611 r_symndx = ELF64_R_SYM (rel->r_info);
9612 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9623 val = h->root.u.def.value;
9625 val = sym->st_value;
9626 val += rel->r_addend;
9628 if (val >= toc->size)
9631 if ((skip[val >> 3] & can_optimize) != 0)
9638 case R_PPC64_TOC16_HA:
9641 case R_PPC64_TOC16_LO_DS:
9642 off = rel->r_offset;
9643 off += (bfd_big_endian (ibfd) ? -2 : 3);
9644 if (!bfd_get_section_contents (ibfd, sec, &opc,
9650 if ((opc & (0x3f << 2)) == (58u << 2))
9655 /* Wrong sort of reloc, or not a ld. We may
9656 as well clear ref_from_discarded too. */
9663 /* For the toc section, we only mark as used if this
9664 entry itself isn't unused. */
9665 else if ((used[rel->r_offset >> 3]
9666 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9669 /* Do all the relocs again, to catch reference
9678 if (elf_section_data (sec)->relocs != relstart)
9682 /* Merge the used and skip arrays. Assume that TOC
9683 doublewords not appearing as either used or unused belong
9684 to an entry more than one doubleword in size. */
9685 for (drop = skip, keep = used, last = 0, some_unused = 0;
9686 drop < skip + (toc->size + 7) / 8;
9691 *drop &= ~ref_from_discarded;
9692 if ((*drop & can_optimize) != 0)
9696 else if ((*drop & ref_from_discarded) != 0)
9699 last = ref_from_discarded;
9709 bfd_byte *contents, *src;
9711 Elf_Internal_Sym *sym;
9712 bfd_boolean local_toc_syms = FALSE;
9714 /* Shuffle the toc contents, and at the same time convert the
9715 skip array from booleans into offsets. */
9716 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9719 elf_section_data (toc)->this_hdr.contents = contents;
9721 for (src = contents, off = 0, drop = skip;
9722 src < contents + toc->size;
9725 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9730 memcpy (src - off, src, 8);
9734 toc->rawsize = toc->size;
9735 toc->size = src - contents - off;
9737 /* Adjust addends for relocs against the toc section sym,
9738 and optimize any accesses we can. */
9739 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9741 if (sec->reloc_count == 0
9742 || discarded_section (sec))
9745 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9747 if (relstart == NULL)
9750 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9752 enum elf_ppc64_reloc_type r_type;
9753 unsigned long r_symndx;
9755 struct elf_link_hash_entry *h;
9758 r_type = ELF64_R_TYPE (rel->r_info);
9765 case R_PPC64_TOC16_LO:
9766 case R_PPC64_TOC16_HI:
9767 case R_PPC64_TOC16_HA:
9768 case R_PPC64_TOC16_DS:
9769 case R_PPC64_TOC16_LO_DS:
9770 case R_PPC64_ADDR64:
9774 r_symndx = ELF64_R_SYM (rel->r_info);
9775 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9783 val = h->root.u.def.value;
9786 val = sym->st_value;
9788 local_toc_syms = TRUE;
9791 val += rel->r_addend;
9793 if (val > toc->rawsize)
9795 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9797 else if ((skip[val >> 3] & can_optimize) != 0)
9799 Elf_Internal_Rela *tocrel
9800 = toc_relocs + (skip[val >> 3] >> 2);
9801 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9805 case R_PPC64_TOC16_HA:
9806 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9809 case R_PPC64_TOC16_LO_DS:
9810 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9814 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9816 info->callbacks->einfo
9817 /* xgettext:c-format */
9818 (_("%H: %s references "
9819 "optimized away TOC entry\n"),
9820 ibfd, sec, rel->r_offset,
9821 ppc64_elf_howto_table[r_type]->name);
9822 bfd_set_error (bfd_error_bad_value);
9825 rel->r_addend = tocrel->r_addend;
9826 elf_section_data (sec)->relocs = relstart;
9830 if (h != NULL || sym->st_value != 0)
9833 rel->r_addend -= skip[val >> 3];
9834 elf_section_data (sec)->relocs = relstart;
9837 if (elf_section_data (sec)->relocs != relstart)
9841 /* We shouldn't have local or global symbols defined in the TOC,
9842 but handle them anyway. */
9843 if (local_syms != NULL)
9844 for (sym = local_syms;
9845 sym < local_syms + symtab_hdr->sh_info;
9847 if (sym->st_value != 0
9848 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9852 if (sym->st_value > toc->rawsize)
9853 i = toc->rawsize >> 3;
9855 i = sym->st_value >> 3;
9857 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9861 (_("%s defined on removed toc entry"),
9862 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9865 while ((skip[i] & (ref_from_discarded | can_optimize)));
9866 sym->st_value = (bfd_vma) i << 3;
9869 sym->st_value -= skip[i];
9870 symtab_hdr->contents = (unsigned char *) local_syms;
9873 /* Adjust any global syms defined in this toc input section. */
9874 if (toc_inf.global_toc_syms)
9877 toc_inf.skip = skip;
9878 toc_inf.global_toc_syms = FALSE;
9879 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9883 if (toc->reloc_count != 0)
9885 Elf_Internal_Shdr *rel_hdr;
9886 Elf_Internal_Rela *wrel;
9889 /* Remove unused toc relocs, and adjust those we keep. */
9890 if (toc_relocs == NULL)
9891 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9893 if (toc_relocs == NULL)
9897 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9898 if ((skip[rel->r_offset >> 3]
9899 & (ref_from_discarded | can_optimize)) == 0)
9901 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9902 wrel->r_info = rel->r_info;
9903 wrel->r_addend = rel->r_addend;
9906 else if (!dec_dynrel_count (rel->r_info, toc, info,
9907 &local_syms, NULL, NULL))
9910 elf_section_data (toc)->relocs = toc_relocs;
9911 toc->reloc_count = wrel - toc_relocs;
9912 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9913 sz = rel_hdr->sh_entsize;
9914 rel_hdr->sh_size = toc->reloc_count * sz;
9917 else if (toc_relocs != NULL
9918 && elf_section_data (toc)->relocs != toc_relocs)
9921 if (local_syms != NULL
9922 && symtab_hdr->contents != (unsigned char *) local_syms)
9924 if (!info->keep_memory)
9927 symtab_hdr->contents = (unsigned char *) local_syms;
9935 /* Return true iff input section I references the TOC using
9936 instructions limited to +/-32k offsets. */
9939 ppc64_elf_has_small_toc_reloc (asection *i)
9941 return (is_ppc64_elf (i->owner)
9942 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9945 /* Allocate space for one GOT entry. */
9948 allocate_got (struct elf_link_hash_entry *h,
9949 struct bfd_link_info *info,
9950 struct got_entry *gent)
9952 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9953 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) h;
9954 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9956 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9957 ? 2 : 1) * sizeof (Elf64_External_Rela);
9958 asection *got = ppc64_elf_tdata (gent->owner)->got;
9960 gent->got.offset = got->size;
9961 got->size += entsize;
9963 if (h->type == STT_GNU_IFUNC)
9965 htab->elf.irelplt->size += rentsize;
9966 htab->got_reli_size += rentsize;
9968 else if (((bfd_link_pic (info)
9969 && !((gent->tls_type & TLS_TPREL) != 0
9970 && bfd_link_executable (info)
9971 && SYMBOL_REFERENCES_LOCAL (info, h)))
9972 || (htab->elf.dynamic_sections_created
9974 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9975 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9977 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9978 relgot->size += rentsize;
9982 /* This function merges got entries in the same toc group. */
9985 merge_got_entries (struct got_entry **pent)
9987 struct got_entry *ent, *ent2;
9989 for (ent = *pent; ent != NULL; ent = ent->next)
9990 if (!ent->is_indirect)
9991 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9992 if (!ent2->is_indirect
9993 && ent2->addend == ent->addend
9994 && ent2->tls_type == ent->tls_type
9995 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9997 ent2->is_indirect = TRUE;
9998 ent2->got.ent = ent;
10002 /* If H is undefined, make it dynamic if that makes sense. */
10005 ensure_undef_dynamic (struct bfd_link_info *info,
10006 struct elf_link_hash_entry *h)
10008 struct elf_link_hash_table *htab = elf_hash_table (info);
10010 if (htab->dynamic_sections_created
10011 && ((info->dynamic_undefined_weak != 0
10012 && h->root.type == bfd_link_hash_undefweak)
10013 || h->root.type == bfd_link_hash_undefined)
10014 && h->dynindx == -1
10015 && !h->forced_local
10016 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
10017 return bfd_elf_link_record_dynamic_symbol (info, h);
10021 /* Allocate space in .plt, .got and associated reloc sections for
10025 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
10027 struct bfd_link_info *info;
10028 struct ppc_link_hash_table *htab;
10030 struct ppc_link_hash_entry *eh;
10031 struct got_entry **pgent, *gent;
10033 if (h->root.type == bfd_link_hash_indirect)
10036 info = (struct bfd_link_info *) inf;
10037 htab = ppc_hash_table (info);
10041 eh = (struct ppc_link_hash_entry *) h;
10042 /* Run through the TLS GD got entries first if we're changing them
10044 if ((eh->tls_mask & (TLS_TLS | TLS_TPRELGD)) == (TLS_TLS | TLS_TPRELGD))
10045 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10046 if (gent->got.refcount > 0
10047 && (gent->tls_type & TLS_GD) != 0)
10049 /* This was a GD entry that has been converted to TPREL. If
10050 there happens to be a TPREL entry we can use that one. */
10051 struct got_entry *ent;
10052 for (ent = h->got.glist; ent != NULL; ent = ent->next)
10053 if (ent->got.refcount > 0
10054 && (ent->tls_type & TLS_TPREL) != 0
10055 && ent->addend == gent->addend
10056 && ent->owner == gent->owner)
10058 gent->got.refcount = 0;
10062 /* If not, then we'll be using our own TPREL entry. */
10063 if (gent->got.refcount != 0)
10064 gent->tls_type = TLS_TLS | TLS_TPREL;
10067 /* Remove any list entry that won't generate a word in the GOT before
10068 we call merge_got_entries. Otherwise we risk merging to empty
10070 pgent = &h->got.glist;
10071 while ((gent = *pgent) != NULL)
10072 if (gent->got.refcount > 0)
10074 if ((gent->tls_type & TLS_LD) != 0
10075 && !h->def_dynamic)
10077 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
10078 *pgent = gent->next;
10081 pgent = &gent->next;
10084 *pgent = gent->next;
10086 if (!htab->do_multi_toc)
10087 merge_got_entries (&h->got.glist);
10089 for (gent = h->got.glist; gent != NULL; gent = gent->next)
10090 if (!gent->is_indirect)
10092 /* Make sure this symbol is output as a dynamic symbol. */
10093 if (!ensure_undef_dynamic (info, h))
10096 if (!is_ppc64_elf (gent->owner))
10099 allocate_got (h, info, gent);
10102 /* If no dynamic sections we can't have dynamic relocs, except for
10103 IFUNCs which are handled even in static executables. */
10104 if (!htab->elf.dynamic_sections_created
10105 && h->type != STT_GNU_IFUNC)
10106 eh->dyn_relocs = NULL;
10108 /* Discard relocs on undefined symbols that must be local. */
10109 else if (h->root.type == bfd_link_hash_undefined
10110 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
10111 eh->dyn_relocs = NULL;
10113 /* Also discard relocs on undefined weak syms with non-default
10114 visibility, or when dynamic_undefined_weak says so. */
10115 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
10116 eh->dyn_relocs = NULL;
10118 if (eh->dyn_relocs != NULL)
10120 struct elf_dyn_relocs *p, **pp;
10122 /* In the shared -Bsymbolic case, discard space allocated for
10123 dynamic pc-relative relocs against symbols which turn out to
10124 be defined in regular objects. For the normal shared case,
10125 discard space for relocs that have become local due to symbol
10126 visibility changes. */
10128 if (bfd_link_pic (info))
10130 /* Relocs that use pc_count are those that appear on a call
10131 insn, or certain REL relocs (see must_be_dyn_reloc) that
10132 can be generated via assembly. We want calls to
10133 protected symbols to resolve directly to the function
10134 rather than going via the plt. If people want function
10135 pointer comparisons to work as expected then they should
10136 avoid writing weird assembly. */
10137 if (SYMBOL_CALLS_LOCAL (info, h))
10139 for (pp = &eh->dyn_relocs; (p = *pp) != NULL; )
10141 p->count -= p->pc_count;
10150 if (eh->dyn_relocs != NULL)
10152 /* Make sure this symbol is output as a dynamic symbol. */
10153 if (!ensure_undef_dynamic (info, h))
10157 else if (ELIMINATE_COPY_RELOCS && h->type != STT_GNU_IFUNC)
10159 /* For the non-pic case, discard space for relocs against
10160 symbols which turn out to need copy relocs or are not
10162 if (h->dynamic_adjusted
10164 && !ELF_COMMON_DEF_P (h))
10166 /* Make sure this symbol is output as a dynamic symbol. */
10167 if (!ensure_undef_dynamic (info, h))
10170 if (h->dynindx == -1)
10171 eh->dyn_relocs = NULL;
10174 eh->dyn_relocs = NULL;
10177 /* Finally, allocate space. */
10178 for (p = eh->dyn_relocs; p != NULL; p = p->next)
10180 asection *sreloc = elf_section_data (p->sec)->sreloc;
10181 if (eh->elf.type == STT_GNU_IFUNC)
10182 sreloc = htab->elf.irelplt;
10183 sreloc->size += p->count * sizeof (Elf64_External_Rela);
10187 /* We might need a PLT entry when the symbol
10190 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10191 d) has plt16 relocs and we are linking statically. */
10192 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10193 || h->type == STT_GNU_IFUNC
10194 || (h->needs_plt && h->dynamic_adjusted)
10197 && !htab->elf.dynamic_sections_created
10198 && !htab->can_convert_all_inline_plt
10199 && (((struct ppc_link_hash_entry *) h)->tls_mask
10200 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10202 struct plt_entry *pent;
10203 bfd_boolean doneone = FALSE;
10204 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10205 if (pent->plt.refcount > 0)
10207 if (!htab->elf.dynamic_sections_created
10208 || h->dynindx == -1)
10210 if (h->type == STT_GNU_IFUNC)
10212 s = htab->elf.iplt;
10213 pent->plt.offset = s->size;
10214 s->size += PLT_ENTRY_SIZE (htab);
10215 s = htab->elf.irelplt;
10219 s = htab->pltlocal;
10220 pent->plt.offset = s->size;
10221 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10222 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
10227 /* If this is the first .plt entry, make room for the special
10229 s = htab->elf.splt;
10231 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10233 pent->plt.offset = s->size;
10235 /* Make room for this entry. */
10236 s->size += PLT_ENTRY_SIZE (htab);
10238 /* Make room for the .glink code. */
10241 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10244 /* We need bigger stubs past index 32767. */
10245 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10252 /* We also need to make an entry in the .rela.plt section. */
10253 s = htab->elf.srelplt;
10256 s->size += sizeof (Elf64_External_Rela);
10260 pent->plt.offset = (bfd_vma) -1;
10263 h->plt.plist = NULL;
10269 h->plt.plist = NULL;
10276 #define PPC_LO(v) ((v) & 0xffff)
10277 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10278 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10280 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10281 to set up space for global entry stubs. These are put in glink,
10282 after the branch table. */
10285 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10287 struct bfd_link_info *info;
10288 struct ppc_link_hash_table *htab;
10289 struct plt_entry *pent;
10292 if (h->root.type == bfd_link_hash_indirect)
10295 if (!h->pointer_equality_needed)
10298 if (h->def_regular)
10302 htab = ppc_hash_table (info);
10306 s = htab->global_entry;
10307 plt = htab->elf.splt;
10308 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10309 if (pent->plt.offset != (bfd_vma) -1
10310 && pent->addend == 0)
10312 /* For ELFv2, if this symbol is not defined in a regular file
10313 and we are not generating a shared library or pie, then we
10314 need to define the symbol in the executable on a call stub.
10315 This is to avoid text relocations. */
10316 bfd_vma off, stub_align, stub_off, stub_size;
10317 unsigned int align_power;
10320 stub_off = s->size;
10321 if (htab->params->plt_stub_align >= 0)
10322 align_power = htab->params->plt_stub_align;
10324 align_power = -htab->params->plt_stub_align;
10325 /* Setting section alignment is delayed until we know it is
10326 non-empty. Otherwise the .text output section will be
10327 aligned at least to plt_stub_align even when no global
10328 entry stubs are needed. */
10329 if (s->alignment_power < align_power)
10330 s->alignment_power = align_power;
10331 stub_align = (bfd_vma) 1 << align_power;
10332 if (htab->params->plt_stub_align >= 0
10333 || ((((stub_off + stub_size - 1) & -stub_align)
10334 - (stub_off & -stub_align))
10335 > ((stub_size - 1) & -stub_align)))
10336 stub_off = (stub_off + stub_align - 1) & -stub_align;
10337 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10338 off -= stub_off + s->output_offset + s->output_section->vma;
10339 /* Note that for --plt-stub-align negative we have a possible
10340 dependency between stub offset and size. Break that
10341 dependency by assuming the max stub size when calculating
10342 the stub offset. */
10343 if (PPC_HA (off) == 0)
10345 h->root.type = bfd_link_hash_defined;
10346 h->root.u.def.section = s;
10347 h->root.u.def.value = stub_off;
10348 s->size = stub_off + stub_size;
10354 /* Set DF_TEXTREL if we find any dynamic relocs that apply to
10355 read-only sections. */
10358 maybe_set_textrel (struct elf_link_hash_entry *h, void *inf)
10362 if (h->root.type == bfd_link_hash_indirect)
10365 sec = readonly_dynrelocs (h);
10368 struct bfd_link_info *info = (struct bfd_link_info *) inf;
10370 info->flags |= DF_TEXTREL;
10371 info->callbacks->minfo
10372 (_("%pB: dynamic relocation against `%pT' in read-only section `%pA'\n"),
10373 sec->owner, h->root.root.string, sec);
10375 /* Not an error, just cut short the traversal. */
10381 /* Set the sizes of the dynamic sections. */
10384 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10385 struct bfd_link_info *info)
10387 struct ppc_link_hash_table *htab;
10390 bfd_boolean relocs;
10392 struct got_entry *first_tlsld;
10394 htab = ppc_hash_table (info);
10398 dynobj = htab->elf.dynobj;
10399 if (dynobj == NULL)
10402 if (htab->elf.dynamic_sections_created)
10404 /* Set the contents of the .interp section to the interpreter. */
10405 if (bfd_link_executable (info) && !info->nointerp)
10407 s = bfd_get_linker_section (dynobj, ".interp");
10410 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10411 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10415 /* Set up .got offsets for local syms, and space for local dynamic
10417 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10419 struct got_entry **lgot_ents;
10420 struct got_entry **end_lgot_ents;
10421 struct plt_entry **local_plt;
10422 struct plt_entry **end_local_plt;
10423 unsigned char *lgot_masks;
10424 bfd_size_type locsymcount;
10425 Elf_Internal_Shdr *symtab_hdr;
10427 if (!is_ppc64_elf (ibfd))
10430 for (s = ibfd->sections; s != NULL; s = s->next)
10432 struct ppc_dyn_relocs *p;
10434 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10436 if (!bfd_is_abs_section (p->sec)
10437 && bfd_is_abs_section (p->sec->output_section))
10439 /* Input section has been discarded, either because
10440 it is a copy of a linkonce section or due to
10441 linker script /DISCARD/, so we'll be discarding
10444 else if (p->count != 0)
10446 asection *srel = elf_section_data (p->sec)->sreloc;
10448 srel = htab->elf.irelplt;
10449 srel->size += p->count * sizeof (Elf64_External_Rela);
10450 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10451 info->flags |= DF_TEXTREL;
10456 lgot_ents = elf_local_got_ents (ibfd);
10460 symtab_hdr = &elf_symtab_hdr (ibfd);
10461 locsymcount = symtab_hdr->sh_info;
10462 end_lgot_ents = lgot_ents + locsymcount;
10463 local_plt = (struct plt_entry **) end_lgot_ents;
10464 end_local_plt = local_plt + locsymcount;
10465 lgot_masks = (unsigned char *) end_local_plt;
10466 s = ppc64_elf_tdata (ibfd)->got;
10467 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10469 struct got_entry **pent, *ent;
10472 while ((ent = *pent) != NULL)
10473 if (ent->got.refcount > 0)
10475 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10477 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10482 unsigned int ent_size = 8;
10483 unsigned int rel_size = sizeof (Elf64_External_Rela);
10485 ent->got.offset = s->size;
10486 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10491 s->size += ent_size;
10492 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10494 htab->elf.irelplt->size += rel_size;
10495 htab->got_reli_size += rel_size;
10497 else if (bfd_link_pic (info)
10498 && !((ent->tls_type & TLS_TPREL) != 0
10499 && bfd_link_executable (info)))
10501 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10502 srel->size += rel_size;
10511 /* Allocate space for plt calls to local syms. */
10512 lgot_masks = (unsigned char *) end_local_plt;
10513 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10515 struct plt_entry *ent;
10517 for (ent = *local_plt; ent != NULL; ent = ent->next)
10518 if (ent->plt.refcount > 0)
10520 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10522 s = htab->elf.iplt;
10523 ent->plt.offset = s->size;
10524 s->size += PLT_ENTRY_SIZE (htab);
10525 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10527 else if (htab->can_convert_all_inline_plt
10528 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10529 ent->plt.offset = (bfd_vma) -1;
10532 s = htab->pltlocal;
10533 ent->plt.offset = s->size;
10534 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10535 if (bfd_link_pic (info))
10536 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10540 ent->plt.offset = (bfd_vma) -1;
10544 /* Allocate global sym .plt and .got entries, and space for global
10545 sym dynamic relocs. */
10546 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10548 if (!htab->opd_abi && !bfd_link_pic (info))
10549 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10551 first_tlsld = NULL;
10552 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10554 struct got_entry *ent;
10556 if (!is_ppc64_elf (ibfd))
10559 ent = ppc64_tlsld_got (ibfd);
10560 if (ent->got.refcount > 0)
10562 if (!htab->do_multi_toc && first_tlsld != NULL)
10564 ent->is_indirect = TRUE;
10565 ent->got.ent = first_tlsld;
10569 if (first_tlsld == NULL)
10571 s = ppc64_elf_tdata (ibfd)->got;
10572 ent->got.offset = s->size;
10575 if (bfd_link_pic (info))
10577 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10578 srel->size += sizeof (Elf64_External_Rela);
10583 ent->got.offset = (bfd_vma) -1;
10586 /* We now have determined the sizes of the various dynamic sections.
10587 Allocate memory for them. */
10589 for (s = dynobj->sections; s != NULL; s = s->next)
10591 if ((s->flags & SEC_LINKER_CREATED) == 0)
10594 if (s == htab->brlt || s == htab->relbrlt)
10595 /* These haven't been allocated yet; don't strip. */
10597 else if (s == htab->elf.sgot
10598 || s == htab->elf.splt
10599 || s == htab->elf.iplt
10600 || s == htab->pltlocal
10601 || s == htab->glink
10602 || s == htab->global_entry
10603 || s == htab->elf.sdynbss
10604 || s == htab->elf.sdynrelro)
10606 /* Strip this section if we don't need it; see the
10609 else if (s == htab->glink_eh_frame)
10611 if (!bfd_is_abs_section (s->output_section))
10612 /* Not sized yet. */
10615 else if (CONST_STRNEQ (s->name, ".rela"))
10619 if (s != htab->elf.srelplt)
10622 /* We use the reloc_count field as a counter if we need
10623 to copy relocs into the output file. */
10624 s->reloc_count = 0;
10629 /* It's not one of our sections, so don't allocate space. */
10635 /* If we don't need this section, strip it from the
10636 output file. This is mostly to handle .rela.bss and
10637 .rela.plt. We must create both sections in
10638 create_dynamic_sections, because they must be created
10639 before the linker maps input sections to output
10640 sections. The linker does that before
10641 adjust_dynamic_symbol is called, and it is that
10642 function which decides whether anything needs to go
10643 into these sections. */
10644 s->flags |= SEC_EXCLUDE;
10648 if (bfd_is_abs_section (s->output_section))
10649 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10652 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10655 /* Allocate memory for the section contents. We use bfd_zalloc
10656 here in case unused entries are not reclaimed before the
10657 section's contents are written out. This should not happen,
10658 but this way if it does we get a R_PPC64_NONE reloc in .rela
10659 sections instead of garbage.
10660 We also rely on the section contents being zero when writing
10661 the GOT and .dynrelro. */
10662 s->contents = bfd_zalloc (dynobj, s->size);
10663 if (s->contents == NULL)
10667 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10669 if (!is_ppc64_elf (ibfd))
10672 s = ppc64_elf_tdata (ibfd)->got;
10673 if (s != NULL && s != htab->elf.sgot)
10676 s->flags |= SEC_EXCLUDE;
10679 s->contents = bfd_zalloc (ibfd, s->size);
10680 if (s->contents == NULL)
10684 s = ppc64_elf_tdata (ibfd)->relgot;
10688 s->flags |= SEC_EXCLUDE;
10691 s->contents = bfd_zalloc (ibfd, s->size);
10692 if (s->contents == NULL)
10695 s->reloc_count = 0;
10700 if (htab->elf.dynamic_sections_created)
10702 bfd_boolean tls_opt;
10704 /* Add some entries to the .dynamic section. We fill in the
10705 values later, in ppc64_elf_finish_dynamic_sections, but we
10706 must add the entries now so that we get the correct size for
10707 the .dynamic section. The DT_DEBUG entry is filled in by the
10708 dynamic linker and used by the debugger. */
10709 #define add_dynamic_entry(TAG, VAL) \
10710 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10712 if (bfd_link_executable (info))
10714 if (!add_dynamic_entry (DT_DEBUG, 0))
10718 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10720 if (!add_dynamic_entry (DT_PLTGOT, 0)
10721 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10722 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10723 || !add_dynamic_entry (DT_JMPREL, 0)
10724 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10728 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10730 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10731 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10735 tls_opt = (htab->params->tls_get_addr_opt
10736 && htab->tls_get_addr_fd != NULL
10737 && htab->tls_get_addr_fd->elf.plt.plist != NULL);
10738 if (tls_opt || !htab->opd_abi)
10740 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10746 if (!add_dynamic_entry (DT_RELA, 0)
10747 || !add_dynamic_entry (DT_RELASZ, 0)
10748 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10751 /* If any dynamic relocs apply to a read-only section,
10752 then we need a DT_TEXTREL entry. */
10753 if ((info->flags & DF_TEXTREL) == 0)
10754 elf_link_hash_traverse (&htab->elf, maybe_set_textrel, info);
10756 if ((info->flags & DF_TEXTREL) != 0)
10758 if (!add_dynamic_entry (DT_TEXTREL, 0))
10763 #undef add_dynamic_entry
10768 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10771 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10773 if (h->plt.plist != NULL
10775 && !h->pointer_equality_needed)
10778 return _bfd_elf_hash_symbol (h);
10781 /* Determine the type of stub needed, if any, for a call. */
10783 static inline enum ppc_stub_type
10784 ppc_type_of_stub (asection *input_sec,
10785 const Elf_Internal_Rela *rel,
10786 struct ppc_link_hash_entry **hash,
10787 struct plt_entry **plt_ent,
10788 bfd_vma destination,
10789 unsigned long local_off)
10791 struct ppc_link_hash_entry *h = *hash;
10793 bfd_vma branch_offset;
10794 bfd_vma max_branch_offset;
10795 enum elf_ppc64_reloc_type r_type;
10799 struct plt_entry *ent;
10800 struct ppc_link_hash_entry *fdh = h;
10802 && h->oh->is_func_descriptor)
10804 fdh = ppc_follow_link (h->oh);
10808 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10809 if (ent->addend == rel->r_addend
10810 && ent->plt.offset != (bfd_vma) -1)
10813 return ppc_stub_plt_call;
10816 /* Here, we know we don't have a plt entry. If we don't have a
10817 either a defined function descriptor or a defined entry symbol
10818 in a regular object file, then it is pointless trying to make
10819 any other type of stub. */
10820 if (!is_static_defined (&fdh->elf)
10821 && !is_static_defined (&h->elf))
10822 return ppc_stub_none;
10824 else if (elf_local_got_ents (input_sec->owner) != NULL)
10826 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10827 struct plt_entry **local_plt = (struct plt_entry **)
10828 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10829 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10831 if (local_plt[r_symndx] != NULL)
10833 struct plt_entry *ent;
10835 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10836 if (ent->addend == rel->r_addend
10837 && ent->plt.offset != (bfd_vma) -1)
10840 return ppc_stub_plt_call;
10845 /* Determine where the call point is. */
10846 location = (input_sec->output_offset
10847 + input_sec->output_section->vma
10850 branch_offset = destination - location;
10851 r_type = ELF64_R_TYPE (rel->r_info);
10853 /* Determine if a long branch stub is needed. */
10854 max_branch_offset = 1 << 25;
10855 if (r_type == R_PPC64_REL14
10856 || r_type == R_PPC64_REL14_BRTAKEN
10857 || r_type == R_PPC64_REL14_BRNTAKEN)
10858 max_branch_offset = 1 << 15;
10860 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10861 /* We need a stub. Figure out whether a long_branch or plt_branch
10862 is needed later. */
10863 return ppc_stub_long_branch;
10865 return ppc_stub_none;
10868 /* Builds a 64-bit offset in r12 then adds it to r11 (LOAD false) or
10869 loads r12 from r11+r12 (LOAD true).
10870 . lis %r12,xxx-1b@highest
10871 . ori %r12,xxx-1b@higher
10872 . sldi %r12,%r12,32
10873 . oris %r12,%r12,xxx-1b@hi
10874 . ori %r12,%r12,xxx-1b@l
10875 . add %r12,%r11,%r12 */
10878 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bfd_boolean load)
10880 if (off + 0x8000 < 0x10000)
10883 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10885 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10888 else if (off + 0x80008000ULL < 0x100000000ULL)
10890 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10893 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10895 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10900 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10902 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10907 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10909 if (((off >> 32) & 0xffff) != 0)
10911 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10915 if (((off >> 32) & 0xffffffffULL) != 0)
10917 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10920 if (PPC_HI (off) != 0)
10922 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10925 if (PPC_LO (off) != 0)
10927 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10931 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10933 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10939 static unsigned int
10940 size_offset (bfd_vma off)
10943 if (off + 0x8000 < 0x10000)
10945 else if (off + 0x80008000ULL < 0x100000000ULL)
10949 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10954 if (((off >> 32) & 0xffff) != 0)
10957 if (((off >> 32) & 0xffffffffULL) != 0)
10959 if (PPC_HI (off) != 0)
10961 if (PPC_LO (off) != 0)
10968 /* With power7 weakly ordered memory model, it is possible for ld.so
10969 to update a plt entry in one thread and have another thread see a
10970 stale zero toc entry. To avoid this we need some sort of acquire
10971 barrier in the call stub. One solution is to make the load of the
10972 toc word seem to appear to depend on the load of the function entry
10973 word. Another solution is to test for r2 being zero, and branch to
10974 the appropriate glink entry if so.
10976 . fake dep barrier compare
10977 . ld 12,xxx(2) ld 12,xxx(2)
10978 . mtctr 12 mtctr 12
10979 . xor 11,12,12 ld 2,xxx+8(2)
10980 . add 2,2,11 cmpldi 2,0
10981 . ld 2,xxx+8(2) bnectr+
10982 . bctr b <glink_entry>
10984 The solution involving the compare turns out to be faster, so
10985 that's what we use unless the branch won't reach. */
10987 #define ALWAYS_USE_FAKE_DEP 0
10988 #define ALWAYS_EMIT_R2SAVE 0
10990 static inline unsigned int
10991 plt_stub_size (struct ppc_link_hash_table *htab,
10992 struct ppc_stub_hash_entry *stub_entry,
10997 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10999 size = 24 + size_offset (off);
11000 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
11006 if (ALWAYS_EMIT_R2SAVE
11007 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11009 if (PPC_HA (off) != 0)
11014 if (htab->params->plt_static_chain)
11016 if (htab->params->plt_thread_safe
11017 && htab->elf.dynamic_sections_created
11018 && stub_entry->h != NULL
11019 && stub_entry->h->elf.dynindx != -1)
11021 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain) != PPC_HA (off))
11024 if (stub_entry->h != NULL
11025 && (stub_entry->h == htab->tls_get_addr_fd
11026 || stub_entry->h == htab->tls_get_addr)
11027 && htab->params->tls_get_addr_opt)
11030 if (stub_entry->stub_type == ppc_stub_plt_call_r2save)
11036 /* Depending on the sign of plt_stub_align:
11037 If positive, return the padding to align to a 2**plt_stub_align
11039 If negative, if this stub would cross fewer 2**plt_stub_align
11040 boundaries if we align, then return the padding needed to do so. */
11042 static inline unsigned int
11043 plt_stub_pad (struct ppc_link_hash_table *htab,
11044 struct ppc_stub_hash_entry *stub_entry,
11048 unsigned stub_size;
11049 bfd_vma stub_off = stub_entry->group->stub_sec->size;
11051 if (htab->params->plt_stub_align >= 0)
11053 stub_align = 1 << htab->params->plt_stub_align;
11054 if ((stub_off & (stub_align - 1)) != 0)
11055 return stub_align - (stub_off & (stub_align - 1));
11059 stub_align = 1 << -htab->params->plt_stub_align;
11060 stub_size = plt_stub_size (htab, stub_entry, plt_off);
11061 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11062 > ((stub_size - 1) & -stub_align))
11063 return stub_align - (stub_off & (stub_align - 1));
11067 /* Build a .plt call stub. */
11069 static inline bfd_byte *
11070 build_plt_stub (struct ppc_link_hash_table *htab,
11071 struct ppc_stub_hash_entry *stub_entry,
11072 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11074 bfd *obfd = htab->params->stub_bfd;
11075 bfd_boolean plt_load_toc = htab->opd_abi;
11076 bfd_boolean plt_static_chain = htab->params->plt_static_chain;
11077 bfd_boolean plt_thread_safe = (htab->params->plt_thread_safe
11078 && htab->elf.dynamic_sections_created
11079 && stub_entry->h != NULL
11080 && stub_entry->h->elf.dynindx != -1);
11081 bfd_boolean use_fake_dep = plt_thread_safe;
11082 bfd_vma cmp_branch_off = 0;
11084 if (!ALWAYS_USE_FAKE_DEP
11087 && !((stub_entry->h == htab->tls_get_addr_fd
11088 || stub_entry->h == htab->tls_get_addr)
11089 && htab->params->tls_get_addr_opt))
11091 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11092 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11093 / PLT_ENTRY_SIZE (htab));
11094 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11097 if (pltindex > 32768)
11098 glinkoff += (pltindex - 32768) * 4;
11100 + htab->glink->output_offset
11101 + htab->glink->output_section->vma);
11102 from = (p - stub_entry->group->stub_sec->contents
11103 + 4 * (ALWAYS_EMIT_R2SAVE
11104 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11105 + 4 * (PPC_HA (offset) != 0)
11106 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11107 != PPC_HA (offset))
11108 + 4 * (plt_static_chain != 0)
11110 + stub_entry->group->stub_sec->output_offset
11111 + stub_entry->group->stub_sec->output_section->vma);
11112 cmp_branch_off = to - from;
11113 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11116 if (PPC_HA (offset) != 0)
11120 if (ALWAYS_EMIT_R2SAVE
11121 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11122 r[0].r_offset += 4;
11123 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11124 r[1].r_offset = r[0].r_offset + 4;
11125 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11126 r[1].r_addend = r[0].r_addend;
11129 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11131 r[2].r_offset = r[1].r_offset + 4;
11132 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11133 r[2].r_addend = r[0].r_addend;
11137 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11138 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11139 r[2].r_addend = r[0].r_addend + 8;
11140 if (plt_static_chain)
11142 r[3].r_offset = r[2].r_offset + 4;
11143 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11144 r[3].r_addend = r[0].r_addend + 16;
11149 if (ALWAYS_EMIT_R2SAVE
11150 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11151 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11154 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11155 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11159 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11160 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11163 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11165 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11168 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11173 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11174 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11176 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11177 if (plt_static_chain)
11178 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11185 if (ALWAYS_EMIT_R2SAVE
11186 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11187 r[0].r_offset += 4;
11188 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11191 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11193 r[1].r_offset = r[0].r_offset + 4;
11194 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11195 r[1].r_addend = r[0].r_addend;
11199 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11200 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11201 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11202 if (plt_static_chain)
11204 r[2].r_offset = r[1].r_offset + 4;
11205 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11206 r[2].r_addend = r[0].r_addend + 8;
11211 if (ALWAYS_EMIT_R2SAVE
11212 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11213 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11214 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11216 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11218 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11221 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11226 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11227 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11229 if (plt_static_chain)
11230 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11231 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11234 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11236 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11237 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11238 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11241 bfd_put_32 (obfd, BCTR, p), p += 4;
11245 /* Build a special .plt call stub for __tls_get_addr. */
11247 #define LD_R11_0R3 0xe9630000
11248 #define LD_R12_0R3 0xe9830000
11249 #define MR_R0_R3 0x7c601b78
11250 #define CMPDI_R11_0 0x2c2b0000
11251 #define ADD_R3_R12_R13 0x7c6c6a14
11252 #define BEQLR 0x4d820020
11253 #define MR_R3_R0 0x7c030378
11254 #define STD_R11_0R1 0xf9610000
11255 #define BCTRL 0x4e800421
11256 #define LD_R11_0R1 0xe9610000
11257 #define MTLR_R11 0x7d6803a6
11259 static inline bfd_byte *
11260 build_tls_get_addr_stub (struct ppc_link_hash_table *htab,
11261 struct ppc_stub_hash_entry *stub_entry,
11262 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11264 bfd *obfd = htab->params->stub_bfd;
11266 bfd_put_32 (obfd, LD_R11_0R3 + 0, p), p += 4;
11267 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11268 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11269 bfd_put_32 (obfd, CMPDI_R11_0, p), p += 4;
11270 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11271 bfd_put_32 (obfd, BEQLR, p), p += 4;
11272 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11274 r[0].r_offset += 7 * 4;
11275 if (stub_entry->stub_type != ppc_stub_plt_call_r2save)
11276 return build_plt_stub (htab, stub_entry, p, offset, r);
11278 bfd_put_32 (obfd, MFLR_R11, p), p += 4;
11279 bfd_put_32 (obfd, STD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11282 r[0].r_offset += 2 * 4;
11283 p = build_plt_stub (htab, stub_entry, p, offset, r);
11284 bfd_put_32 (obfd, BCTRL, p - 4);
11286 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p), p += 4;
11287 bfd_put_32 (obfd, LD_R11_0R1 + STK_LINKER (htab), p), p += 4;
11288 bfd_put_32 (obfd, MTLR_R11, p), p += 4;
11289 bfd_put_32 (obfd, BLR, p), p += 4;
11294 static Elf_Internal_Rela *
11295 get_relocs (asection *sec, int count)
11297 Elf_Internal_Rela *relocs;
11298 struct bfd_elf_section_data *elfsec_data;
11300 elfsec_data = elf_section_data (sec);
11301 relocs = elfsec_data->relocs;
11302 if (relocs == NULL)
11304 bfd_size_type relsize;
11305 relsize = sec->reloc_count * sizeof (*relocs);
11306 relocs = bfd_alloc (sec->owner, relsize);
11307 if (relocs == NULL)
11309 elfsec_data->relocs = relocs;
11310 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11311 sizeof (Elf_Internal_Shdr));
11312 if (elfsec_data->rela.hdr == NULL)
11314 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11315 * sizeof (Elf64_External_Rela));
11316 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11317 sec->reloc_count = 0;
11319 relocs += sec->reloc_count;
11320 sec->reloc_count += count;
11325 get_r2off (struct bfd_link_info *info,
11326 struct ppc_stub_hash_entry *stub_entry)
11328 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11329 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11333 /* Support linking -R objects. Get the toc pointer from the
11336 if (!htab->opd_abi)
11338 asection *opd = stub_entry->h->elf.root.u.def.section;
11339 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11341 if (strcmp (opd->name, ".opd") != 0
11342 || opd->reloc_count != 0)
11344 info->callbacks->einfo (_("%P: cannot find opd entry toc for `%pT'\n"),
11345 stub_entry->h->elf.root.root.string);
11346 bfd_set_error (bfd_error_bad_value);
11347 return (bfd_vma) -1;
11349 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11350 return (bfd_vma) -1;
11351 r2off = bfd_get_64 (opd->owner, buf);
11352 r2off -= elf_gp (info->output_bfd);
11354 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11359 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11361 struct ppc_stub_hash_entry *stub_entry;
11362 struct ppc_branch_hash_entry *br_entry;
11363 struct bfd_link_info *info;
11364 struct ppc_link_hash_table *htab;
11368 Elf_Internal_Rela *r;
11371 /* Massage our args to the form they really have. */
11372 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11375 htab = ppc_hash_table (info);
11379 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11380 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11382 htab->stub_count[stub_entry->stub_type - 1] += 1;
11383 switch (stub_entry->stub_type)
11385 case ppc_stub_long_branch:
11386 case ppc_stub_long_branch_r2off:
11387 /* Branches are relative. This is where we are going to. */
11388 targ = (stub_entry->target_value
11389 + stub_entry->target_section->output_offset
11390 + stub_entry->target_section->output_section->vma);
11391 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11393 /* And this is where we are coming from. */
11394 off = (stub_entry->stub_offset
11395 + stub_entry->group->stub_sec->output_offset
11396 + stub_entry->group->stub_sec->output_section->vma);
11400 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11402 bfd_vma r2off = get_r2off (info, stub_entry);
11404 if (r2off == (bfd_vma) -1)
11406 htab->stub_error = TRUE;
11409 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11411 if (PPC_HA (r2off) != 0)
11413 bfd_put_32 (htab->params->stub_bfd,
11414 ADDIS_R2_R2 | PPC_HA (r2off), p);
11417 if (PPC_LO (r2off) != 0)
11419 bfd_put_32 (htab->params->stub_bfd,
11420 ADDI_R2_R2 | PPC_LO (r2off), p);
11425 bfd_put_32 (htab->params->stub_bfd, B_DOT | (off & 0x3fffffc), p);
11428 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11431 (_("long branch stub `%s' offset overflow"),
11432 stub_entry->root.string);
11433 htab->stub_error = TRUE;
11437 if (info->emitrelocations)
11439 r = get_relocs (stub_entry->group->stub_sec, 1);
11442 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11443 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11444 r->r_addend = targ;
11445 if (stub_entry->h != NULL)
11447 struct elf_link_hash_entry **hashes;
11448 unsigned long symndx;
11449 struct ppc_link_hash_entry *h;
11451 hashes = elf_sym_hashes (htab->params->stub_bfd);
11452 if (hashes == NULL)
11454 bfd_size_type hsize;
11456 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11457 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11458 if (hashes == NULL)
11460 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11461 htab->stub_globals = 1;
11463 symndx = htab->stub_globals++;
11465 hashes[symndx] = &h->elf;
11466 r->r_info = ELF64_R_INFO (symndx, R_PPC64_REL24);
11467 if (h->oh != NULL && h->oh->is_func)
11468 h = ppc_follow_link (h->oh);
11469 if (h->elf.root.u.def.section != stub_entry->target_section)
11470 /* H is an opd symbol. The addend must be zero. */
11474 off = (h->elf.root.u.def.value
11475 + h->elf.root.u.def.section->output_offset
11476 + h->elf.root.u.def.section->output_section->vma);
11477 r->r_addend -= off;
11483 case ppc_stub_plt_branch:
11484 case ppc_stub_plt_branch_r2off:
11485 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11486 stub_entry->root.string + 9,
11488 if (br_entry == NULL)
11490 _bfd_error_handler (_("can't find branch stub `%s'"),
11491 stub_entry->root.string);
11492 htab->stub_error = TRUE;
11496 targ = (stub_entry->target_value
11497 + stub_entry->target_section->output_offset
11498 + stub_entry->target_section->output_section->vma);
11499 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11500 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11502 bfd_put_64 (htab->brlt->owner, targ,
11503 htab->brlt->contents + br_entry->offset);
11505 if (br_entry->iter == htab->stub_iteration)
11507 br_entry->iter = 0;
11509 if (htab->relbrlt != NULL)
11511 /* Create a reloc for the branch lookup table entry. */
11512 Elf_Internal_Rela rela;
11515 rela.r_offset = (br_entry->offset
11516 + htab->brlt->output_offset
11517 + htab->brlt->output_section->vma);
11518 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11519 rela.r_addend = targ;
11521 rl = htab->relbrlt->contents;
11522 rl += (htab->relbrlt->reloc_count++
11523 * sizeof (Elf64_External_Rela));
11524 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11526 else if (info->emitrelocations)
11528 r = get_relocs (htab->brlt, 1);
11531 /* brlt, being SEC_LINKER_CREATED does not go through the
11532 normal reloc processing. Symbols and offsets are not
11533 translated from input file to output file form, so
11534 set up the offset per the output file. */
11535 r->r_offset = (br_entry->offset
11536 + htab->brlt->output_offset
11537 + htab->brlt->output_section->vma);
11538 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11539 r->r_addend = targ;
11543 targ = (br_entry->offset
11544 + htab->brlt->output_offset
11545 + htab->brlt->output_section->vma);
11547 off = (elf_gp (info->output_bfd)
11548 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11551 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11553 info->callbacks->einfo
11554 (_("%P: linkage table error against `%pT'\n"),
11555 stub_entry->root.string);
11556 bfd_set_error (bfd_error_bad_value);
11557 htab->stub_error = TRUE;
11561 if (info->emitrelocations)
11563 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11566 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11567 if (bfd_big_endian (info->output_bfd))
11568 r[0].r_offset += 2;
11569 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11570 r[0].r_offset += 4;
11571 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11572 r[0].r_addend = targ;
11573 if (PPC_HA (off) != 0)
11575 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11576 r[1].r_offset = r[0].r_offset + 4;
11577 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11578 r[1].r_addend = r[0].r_addend;
11583 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11585 if (PPC_HA (off) != 0)
11587 bfd_put_32 (htab->params->stub_bfd,
11588 ADDIS_R12_R2 | PPC_HA (off), p);
11590 bfd_put_32 (htab->params->stub_bfd,
11591 LD_R12_0R12 | PPC_LO (off), p);
11594 bfd_put_32 (htab->params->stub_bfd,
11595 LD_R12_0R2 | PPC_LO (off), p);
11599 bfd_vma r2off = get_r2off (info, stub_entry);
11601 if (r2off == (bfd_vma) -1)
11603 htab->stub_error = TRUE;
11607 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11609 if (PPC_HA (off) != 0)
11611 bfd_put_32 (htab->params->stub_bfd,
11612 ADDIS_R12_R2 | PPC_HA (off), p);
11614 bfd_put_32 (htab->params->stub_bfd,
11615 LD_R12_0R12 | PPC_LO (off), p);
11618 bfd_put_32 (htab->params->stub_bfd, LD_R12_0R2 | PPC_LO (off), p);
11620 if (PPC_HA (r2off) != 0)
11623 bfd_put_32 (htab->params->stub_bfd,
11624 ADDIS_R2_R2 | PPC_HA (r2off), p);
11626 if (PPC_LO (r2off) != 0)
11629 bfd_put_32 (htab->params->stub_bfd,
11630 ADDI_R2_R2 | PPC_LO (r2off), p);
11634 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11636 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11640 case ppc_stub_long_branch_notoc:
11641 case ppc_stub_long_branch_both:
11642 case ppc_stub_plt_branch_notoc:
11643 case ppc_stub_plt_branch_both:
11644 case ppc_stub_plt_call_notoc:
11645 case ppc_stub_plt_call_both:
11647 off = (8 + stub_entry->stub_offset
11648 + stub_entry->group->stub_sec->output_offset
11649 + stub_entry->group->stub_sec->output_section->vma);
11650 if (stub_entry->stub_type == ppc_stub_long_branch_both
11651 || stub_entry->stub_type == ppc_stub_plt_branch_both
11652 || stub_entry->stub_type == ppc_stub_plt_call_both)
11655 bfd_put_32 (htab->params->stub_bfd, STD_R2_0R1 + STK_TOC (htab), p);
11658 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11660 targ = stub_entry->plt_ent->plt.offset & ~1;
11661 if (targ >= (bfd_vma) -2)
11664 plt = htab->elf.splt;
11665 if (!htab->elf.dynamic_sections_created
11666 || stub_entry->h == NULL
11667 || stub_entry->h->elf.dynindx == -1)
11669 if (stub_entry->symtype == STT_GNU_IFUNC)
11670 plt = htab->elf.iplt;
11672 plt = htab->pltlocal;
11674 targ += plt->output_offset + plt->output_section->vma;
11677 targ = (stub_entry->target_value
11678 + stub_entry->target_section->output_offset
11679 + stub_entry->target_section->output_section->vma);
11681 bfd_put_32 (htab->params->stub_bfd, MFLR_R12, p);
11683 bfd_put_32 (htab->params->stub_bfd, BCL_20_31, p);
11685 bfd_put_32 (htab->params->stub_bfd, MFLR_R11, p);
11687 bfd_put_32 (htab->params->stub_bfd, MTLR_R12, p);
11689 p = build_offset (htab->params->stub_bfd, p, off,
11690 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11691 if (stub_entry->stub_type == ppc_stub_long_branch_notoc)
11694 bfd_put_32 (htab->params->stub_bfd,
11695 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11697 else if (stub_entry->stub_type == ppc_stub_long_branch_both)
11700 bfd_put_32 (htab->params->stub_bfd,
11701 B_DOT | ((off - (p - loc)) & 0x3fffffc), p);
11705 bfd_put_32 (htab->params->stub_bfd, MTCTR_R12, p);
11707 bfd_put_32 (htab->params->stub_bfd, BCTR, p);
11712 case ppc_stub_plt_call:
11713 case ppc_stub_plt_call_r2save:
11714 if (stub_entry->h != NULL
11715 && stub_entry->h->is_func_descriptor
11716 && stub_entry->h->oh != NULL)
11718 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11720 /* If the old-ABI "dot-symbol" is undefined make it weak so
11721 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11722 if (fh->elf.root.type == bfd_link_hash_undefined
11723 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11724 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11725 fh->elf.root.type = bfd_link_hash_undefweak;
11728 /* Now build the stub. */
11729 targ = stub_entry->plt_ent->plt.offset & ~1;
11730 if (targ >= (bfd_vma) -2)
11733 plt = htab->elf.splt;
11734 if (!htab->elf.dynamic_sections_created
11735 || stub_entry->h == NULL
11736 || stub_entry->h->elf.dynindx == -1)
11738 if (stub_entry->symtype == STT_GNU_IFUNC)
11739 plt = htab->elf.iplt;
11741 plt = htab->pltlocal;
11743 targ += plt->output_offset + plt->output_section->vma;
11745 off = (elf_gp (info->output_bfd)
11746 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11749 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11751 info->callbacks->einfo
11752 /* xgettext:c-format */
11753 (_("%P: linkage table error against `%pT'\n"),
11754 stub_entry->h != NULL
11755 ? stub_entry->h->elf.root.root.string
11757 bfd_set_error (bfd_error_bad_value);
11758 htab->stub_error = TRUE;
11763 if (info->emitrelocations)
11765 r = get_relocs (stub_entry->group->stub_sec,
11766 ((PPC_HA (off) != 0)
11768 ? 2 + (htab->params->plt_static_chain
11769 && PPC_HA (off + 16) == PPC_HA (off))
11773 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11774 if (bfd_big_endian (info->output_bfd))
11775 r[0].r_offset += 2;
11776 r[0].r_addend = targ;
11778 if (stub_entry->h != NULL
11779 && (stub_entry->h == htab->tls_get_addr_fd
11780 || stub_entry->h == htab->tls_get_addr)
11781 && htab->params->tls_get_addr_opt)
11782 p = build_tls_get_addr_stub (htab, stub_entry, loc, off, r);
11784 p = build_plt_stub (htab, stub_entry, loc, off, r);
11787 case ppc_stub_save_res:
11795 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11797 if (htab->params->emit_stub_syms)
11799 struct elf_link_hash_entry *h;
11802 const char *const stub_str[] = { "long_branch",
11815 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11816 len2 = strlen (stub_entry->root.string);
11817 name = bfd_malloc (len1 + len2 + 2);
11820 memcpy (name, stub_entry->root.string, 9);
11821 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11822 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11823 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
11826 if (h->root.type == bfd_link_hash_new)
11828 h->root.type = bfd_link_hash_defined;
11829 h->root.u.def.section = stub_entry->group->stub_sec;
11830 h->root.u.def.value = stub_entry->stub_offset;
11831 h->ref_regular = 1;
11832 h->def_regular = 1;
11833 h->ref_regular_nonweak = 1;
11834 h->forced_local = 1;
11836 h->root.linker_def = 1;
11843 /* As above, but don't actually build the stub. Just bump offset so
11844 we know stub section sizes, and select plt_branch stubs where
11845 long_branch stubs won't do. */
11848 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11850 struct ppc_stub_hash_entry *stub_entry;
11851 struct bfd_link_info *info;
11852 struct ppc_link_hash_table *htab;
11856 /* Massage our args to the form they really have. */
11857 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11860 htab = ppc_hash_table (info);
11864 /* Make a note of the offset within the stubs for this entry. */
11865 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11867 if (stub_entry->h != NULL
11868 && stub_entry->h->save_res
11869 && stub_entry->h->elf.root.type == bfd_link_hash_defined
11870 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
11872 /* Don't make stubs to out-of-line register save/restore
11873 functions. Instead, emit copies of the functions. */
11874 stub_entry->group->needs_save_res = 1;
11875 stub_entry->stub_type = ppc_stub_save_res;
11879 if (stub_entry->stub_type >= ppc_stub_plt_call
11880 && stub_entry->stub_type <= ppc_stub_plt_call_both)
11883 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
11884 if (targ >= (bfd_vma) -2)
11886 plt = htab->elf.splt;
11887 if (!htab->elf.dynamic_sections_created
11888 || stub_entry->h == NULL
11889 || stub_entry->h->elf.dynindx == -1)
11891 if (stub_entry->symtype == STT_GNU_IFUNC)
11892 plt = htab->elf.iplt;
11894 plt = htab->pltlocal;
11896 targ += plt->output_offset + plt->output_section->vma;
11898 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11900 off = (8 + stub_entry->stub_offset
11901 + stub_entry->group->stub_sec->output_offset
11902 + stub_entry->group->stub_sec->output_section->vma);
11903 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
11907 off = (elf_gp (info->output_bfd)
11908 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11910 if (htab->params->plt_stub_align != 0)
11912 unsigned pad = plt_stub_pad (htab, stub_entry, targ - off);
11914 stub_entry->group->stub_sec->size += pad;
11915 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
11916 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11921 size = plt_stub_size (htab, stub_entry, off);
11923 if (stub_entry->stub_type < ppc_stub_plt_call_notoc)
11925 if (stub_entry->h != NULL
11926 && (stub_entry->h == htab->tls_get_addr_fd
11927 || stub_entry->h == htab->tls_get_addr)
11928 && htab->params->tls_get_addr_opt
11929 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
11930 stub_entry->group->tls_get_addr_opt_bctrl
11931 = stub_entry->stub_offset + size - 5 * 4;
11933 if (info->emitrelocations)
11935 stub_entry->group->stub_sec->reloc_count
11936 += ((PPC_HA (off) != 0)
11938 ? 2 + (htab->params->plt_static_chain
11939 && PPC_HA (off + 16) == PPC_HA (off))
11941 stub_entry->group->stub_sec->flags |= SEC_RELOC;
11947 /* ppc_stub_long_branch or ppc_stub_plt_branch, or their r2off
11950 bfd_vma local_off = 0;
11952 targ = (stub_entry->target_value
11953 + stub_entry->target_section->output_offset
11954 + stub_entry->target_section->output_section->vma);
11955 off = (stub_entry->stub_offset
11956 + stub_entry->group->stub_sec->output_offset
11957 + stub_entry->group->stub_sec->output_section->vma);
11959 /* Reset the stub type from the plt variant in case we now
11960 can reach with a shorter stub. */
11961 if (stub_entry->stub_type >= ppc_stub_plt_branch)
11962 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
11965 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11967 r2off = get_r2off (info, stub_entry);
11968 if (r2off == (bfd_vma) -1)
11970 htab->stub_error = TRUE;
11974 if (PPC_HA (r2off) != 0)
11976 if (PPC_LO (r2off) != 0)
11980 else if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
11982 size = 20 + size_offset (targ - (off + 8));
11983 if (stub_entry->stub_type > ppc_stub_long_branch_notoc)
11989 if (stub_entry->stub_type >= ppc_stub_long_branch_notoc)
11991 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11993 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
11994 - ppc_stub_long_branch_notoc);
12000 local_off = PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12002 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12003 Do the same for -R objects without function descriptors. */
12004 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12006 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12007 || off + (1 << 25) >= (bfd_vma) (1 << 26) - local_off)
12009 struct ppc_branch_hash_entry *br_entry;
12011 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12012 stub_entry->root.string + 9,
12014 if (br_entry == NULL)
12016 _bfd_error_handler (_("can't build branch stub `%s'"),
12017 stub_entry->root.string);
12018 htab->stub_error = TRUE;
12022 if (br_entry->iter != htab->stub_iteration)
12024 br_entry->iter = htab->stub_iteration;
12025 br_entry->offset = htab->brlt->size;
12026 htab->brlt->size += 8;
12028 if (htab->relbrlt != NULL)
12029 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12030 else if (info->emitrelocations)
12032 htab->brlt->reloc_count += 1;
12033 htab->brlt->flags |= SEC_RELOC;
12037 targ = (br_entry->offset
12038 + htab->brlt->output_offset
12039 + htab->brlt->output_section->vma);
12040 off = (elf_gp (info->output_bfd)
12041 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12044 if (info->emitrelocations)
12046 stub_entry->group->stub_sec->reloc_count
12047 += 1 + (PPC_HA (off) != 0);
12048 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12051 stub_entry->stub_type
12052 += ppc_stub_plt_branch - ppc_stub_long_branch;
12053 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12056 if (PPC_HA (off) != 0)
12062 if (PPC_HA (off) != 0)
12065 if (PPC_HA (r2off) != 0)
12067 if (PPC_LO (r2off) != 0)
12071 else if (info->emitrelocations)
12073 stub_entry->group->stub_sec->reloc_count += 1;
12074 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12079 stub_entry->group->stub_sec->size += size;
12083 /* Set up various things so that we can make a list of input sections
12084 for each output section included in the link. Returns -1 on error,
12085 0 when no stubs will be needed, and 1 on success. */
12088 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12092 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12097 htab->sec_info_arr_size = _bfd_section_id;
12098 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12099 htab->sec_info = bfd_zmalloc (amt);
12100 if (htab->sec_info == NULL)
12103 /* Set toc_off for com, und, abs and ind sections. */
12104 for (id = 0; id < 3; id++)
12105 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12110 /* Set up for first pass at multitoc partitioning. */
12113 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12115 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12117 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12118 htab->toc_bfd = NULL;
12119 htab->toc_first_sec = NULL;
12122 /* The linker repeatedly calls this function for each TOC input section
12123 and linker generated GOT section. Group input bfds such that the toc
12124 within a group is less than 64k in size. */
12127 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12129 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12130 bfd_vma addr, off, limit;
12135 if (!htab->second_toc_pass)
12137 /* Keep track of the first .toc or .got section for this input bfd. */
12138 bfd_boolean new_bfd = htab->toc_bfd != isec->owner;
12142 htab->toc_bfd = isec->owner;
12143 htab->toc_first_sec = isec;
12146 addr = isec->output_offset + isec->output_section->vma;
12147 off = addr - htab->toc_curr;
12148 limit = 0x80008000;
12149 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12151 if (off + isec->size > limit)
12153 addr = (htab->toc_first_sec->output_offset
12154 + htab->toc_first_sec->output_section->vma);
12155 htab->toc_curr = addr;
12156 htab->toc_curr &= -TOC_BASE_ALIGN;
12159 /* toc_curr is the base address of this toc group. Set elf_gp
12160 for the input section to be the offset relative to the
12161 output toc base plus 0x8000. Making the input elf_gp an
12162 offset allows us to move the toc as a whole without
12163 recalculating input elf_gp. */
12164 off = htab->toc_curr - elf_gp (info->output_bfd);
12165 off += TOC_BASE_OFF;
12167 /* Die if someone uses a linker script that doesn't keep input
12168 file .toc and .got together. */
12170 && elf_gp (isec->owner) != 0
12171 && elf_gp (isec->owner) != off)
12174 elf_gp (isec->owner) = off;
12178 /* During the second pass toc_first_sec points to the start of
12179 a toc group, and toc_curr is used to track the old elf_gp.
12180 We use toc_bfd to ensure we only look at each bfd once. */
12181 if (htab->toc_bfd == isec->owner)
12183 htab->toc_bfd = isec->owner;
12185 if (htab->toc_first_sec == NULL
12186 || htab->toc_curr != elf_gp (isec->owner))
12188 htab->toc_curr = elf_gp (isec->owner);
12189 htab->toc_first_sec = isec;
12191 addr = (htab->toc_first_sec->output_offset
12192 + htab->toc_first_sec->output_section->vma);
12193 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12194 elf_gp (isec->owner) = off;
12199 /* Called via elf_link_hash_traverse to merge GOT entries for global
12203 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12205 if (h->root.type == bfd_link_hash_indirect)
12208 merge_got_entries (&h->got.glist);
12213 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12217 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12219 struct got_entry *gent;
12221 if (h->root.type == bfd_link_hash_indirect)
12224 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12225 if (!gent->is_indirect)
12226 allocate_got (h, (struct bfd_link_info *) inf, gent);
12230 /* Called on the first multitoc pass after the last call to
12231 ppc64_elf_next_toc_section. This function removes duplicate GOT
12235 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12237 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12238 struct bfd *ibfd, *ibfd2;
12239 bfd_boolean done_something;
12241 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12243 if (!htab->do_multi_toc)
12246 /* Merge global sym got entries within a toc group. */
12247 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12249 /* And tlsld_got. */
12250 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12252 struct got_entry *ent, *ent2;
12254 if (!is_ppc64_elf (ibfd))
12257 ent = ppc64_tlsld_got (ibfd);
12258 if (!ent->is_indirect
12259 && ent->got.offset != (bfd_vma) -1)
12261 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12263 if (!is_ppc64_elf (ibfd2))
12266 ent2 = ppc64_tlsld_got (ibfd2);
12267 if (!ent2->is_indirect
12268 && ent2->got.offset != (bfd_vma) -1
12269 && elf_gp (ibfd2) == elf_gp (ibfd))
12271 ent2->is_indirect = TRUE;
12272 ent2->got.ent = ent;
12278 /* Zap sizes of got sections. */
12279 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12280 htab->elf.irelplt->size -= htab->got_reli_size;
12281 htab->got_reli_size = 0;
12283 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12285 asection *got, *relgot;
12287 if (!is_ppc64_elf (ibfd))
12290 got = ppc64_elf_tdata (ibfd)->got;
12293 got->rawsize = got->size;
12295 relgot = ppc64_elf_tdata (ibfd)->relgot;
12296 relgot->rawsize = relgot->size;
12301 /* Now reallocate the got, local syms first. We don't need to
12302 allocate section contents again since we never increase size. */
12303 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12305 struct got_entry **lgot_ents;
12306 struct got_entry **end_lgot_ents;
12307 struct plt_entry **local_plt;
12308 struct plt_entry **end_local_plt;
12309 unsigned char *lgot_masks;
12310 bfd_size_type locsymcount;
12311 Elf_Internal_Shdr *symtab_hdr;
12314 if (!is_ppc64_elf (ibfd))
12317 lgot_ents = elf_local_got_ents (ibfd);
12321 symtab_hdr = &elf_symtab_hdr (ibfd);
12322 locsymcount = symtab_hdr->sh_info;
12323 end_lgot_ents = lgot_ents + locsymcount;
12324 local_plt = (struct plt_entry **) end_lgot_ents;
12325 end_local_plt = local_plt + locsymcount;
12326 lgot_masks = (unsigned char *) end_local_plt;
12327 s = ppc64_elf_tdata (ibfd)->got;
12328 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12330 struct got_entry *ent;
12332 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12334 unsigned int ent_size = 8;
12335 unsigned int rel_size = sizeof (Elf64_External_Rela);
12337 ent->got.offset = s->size;
12338 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12343 s->size += ent_size;
12344 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12346 htab->elf.irelplt->size += rel_size;
12347 htab->got_reli_size += rel_size;
12349 else if (bfd_link_pic (info)
12350 && !((ent->tls_type & TLS_TPREL) != 0
12351 && bfd_link_executable (info)))
12353 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12354 srel->size += rel_size;
12360 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12362 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12364 struct got_entry *ent;
12366 if (!is_ppc64_elf (ibfd))
12369 ent = ppc64_tlsld_got (ibfd);
12370 if (!ent->is_indirect
12371 && ent->got.offset != (bfd_vma) -1)
12373 asection *s = ppc64_elf_tdata (ibfd)->got;
12374 ent->got.offset = s->size;
12376 if (bfd_link_pic (info))
12378 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12379 srel->size += sizeof (Elf64_External_Rela);
12384 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12385 if (!done_something)
12386 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12390 if (!is_ppc64_elf (ibfd))
12393 got = ppc64_elf_tdata (ibfd)->got;
12396 done_something = got->rawsize != got->size;
12397 if (done_something)
12402 if (done_something)
12403 (*htab->params->layout_sections_again) ();
12405 /* Set up for second pass over toc sections to recalculate elf_gp
12406 on input sections. */
12407 htab->toc_bfd = NULL;
12408 htab->toc_first_sec = NULL;
12409 htab->second_toc_pass = TRUE;
12410 return done_something;
12413 /* Called after second pass of multitoc partitioning. */
12416 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12418 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12420 /* After the second pass, toc_curr tracks the TOC offset used
12421 for code sections below in ppc64_elf_next_input_section. */
12422 htab->toc_curr = TOC_BASE_OFF;
12425 /* No toc references were found in ISEC. If the code in ISEC makes no
12426 calls, then there's no need to use toc adjusting stubs when branching
12427 into ISEC. Actually, indirect calls from ISEC are OK as they will
12428 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12429 needed, and 2 if a cyclical call-graph was found but no other reason
12430 for a stub was detected. If called from the top level, a return of
12431 2 means the same as a return of 0. */
12434 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12438 /* Mark this section as checked. */
12439 isec->call_check_done = 1;
12441 /* We know none of our code bearing sections will need toc stubs. */
12442 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12445 if (isec->size == 0)
12448 if (isec->output_section == NULL)
12452 if (isec->reloc_count != 0)
12454 Elf_Internal_Rela *relstart, *rel;
12455 Elf_Internal_Sym *local_syms;
12456 struct ppc_link_hash_table *htab;
12458 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12459 info->keep_memory);
12460 if (relstart == NULL)
12463 /* Look for branches to outside of this section. */
12465 htab = ppc_hash_table (info);
12469 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12471 enum elf_ppc64_reloc_type r_type;
12472 unsigned long r_symndx;
12473 struct elf_link_hash_entry *h;
12474 struct ppc_link_hash_entry *eh;
12475 Elf_Internal_Sym *sym;
12477 struct _opd_sec_data *opd;
12481 r_type = ELF64_R_TYPE (rel->r_info);
12482 if (r_type != R_PPC64_REL24
12483 && r_type != R_PPC64_REL24_NOTOC
12484 && r_type != R_PPC64_REL14
12485 && r_type != R_PPC64_REL14_BRTAKEN
12486 && r_type != R_PPC64_REL14_BRNTAKEN
12487 && r_type != R_PPC64_PLTCALL)
12490 r_symndx = ELF64_R_SYM (rel->r_info);
12491 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12498 /* Calls to dynamic lib functions go through a plt call stub
12500 eh = (struct ppc_link_hash_entry *) h;
12502 && (eh->elf.plt.plist != NULL
12504 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12510 if (sym_sec == NULL)
12511 /* Ignore other undefined symbols. */
12514 /* Assume branches to other sections not included in the
12515 link need stubs too, to cover -R and absolute syms. */
12516 if (sym_sec->output_section == NULL)
12523 sym_value = sym->st_value;
12526 if (h->root.type != bfd_link_hash_defined
12527 && h->root.type != bfd_link_hash_defweak)
12529 sym_value = h->root.u.def.value;
12531 sym_value += rel->r_addend;
12533 /* If this branch reloc uses an opd sym, find the code section. */
12534 opd = get_opd_info (sym_sec);
12537 if (h == NULL && opd->adjust != NULL)
12541 adjust = opd->adjust[OPD_NDX (sym_value)];
12543 /* Assume deleted functions won't ever be called. */
12545 sym_value += adjust;
12548 dest = opd_entry_value (sym_sec, sym_value,
12549 &sym_sec, NULL, FALSE);
12550 if (dest == (bfd_vma) -1)
12555 + sym_sec->output_offset
12556 + sym_sec->output_section->vma);
12558 /* Ignore branch to self. */
12559 if (sym_sec == isec)
12562 /* If the called function uses the toc, we need a stub. */
12563 if (sym_sec->has_toc_reloc
12564 || sym_sec->makes_toc_func_call)
12570 /* Assume any branch that needs a long branch stub might in fact
12571 need a plt_branch stub. A plt_branch stub uses r2. */
12572 else if (dest - (isec->output_offset
12573 + isec->output_section->vma
12574 + rel->r_offset) + (1 << 25)
12575 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12583 /* If calling back to a section in the process of being
12584 tested, we can't say for sure that no toc adjusting stubs
12585 are needed, so don't return zero. */
12586 else if (sym_sec->call_check_in_progress)
12589 /* Branches to another section that itself doesn't have any TOC
12590 references are OK. Recursively call ourselves to check. */
12591 else if (!sym_sec->call_check_done)
12595 /* Mark current section as indeterminate, so that other
12596 sections that call back to current won't be marked as
12598 isec->call_check_in_progress = 1;
12599 recur = toc_adjusting_stub_needed (info, sym_sec);
12600 isec->call_check_in_progress = 0;
12611 if (local_syms != NULL
12612 && (elf_symtab_hdr (isec->owner).contents
12613 != (unsigned char *) local_syms))
12615 if (elf_section_data (isec)->relocs != relstart)
12620 && isec->map_head.s != NULL
12621 && (strcmp (isec->output_section->name, ".init") == 0
12622 || strcmp (isec->output_section->name, ".fini") == 0))
12624 if (isec->map_head.s->has_toc_reloc
12625 || isec->map_head.s->makes_toc_func_call)
12627 else if (!isec->map_head.s->call_check_done)
12630 isec->call_check_in_progress = 1;
12631 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12632 isec->call_check_in_progress = 0;
12639 isec->makes_toc_func_call = 1;
12644 /* The linker repeatedly calls this function for each input section,
12645 in the order that input sections are linked into output sections.
12646 Build lists of input sections to determine groupings between which
12647 we may insert linker stubs. */
12650 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12652 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12657 if ((isec->output_section->flags & SEC_CODE) != 0
12658 && isec->output_section->id < htab->sec_info_arr_size)
12660 /* This happens to make the list in reverse order,
12661 which is what we want. */
12662 htab->sec_info[isec->id].u.list
12663 = htab->sec_info[isec->output_section->id].u.list;
12664 htab->sec_info[isec->output_section->id].u.list = isec;
12667 if (htab->multi_toc_needed)
12669 /* Analyse sections that aren't already flagged as needing a
12670 valid toc pointer. Exclude .fixup for the linux kernel.
12671 .fixup contains branches, but only back to the function that
12672 hit an exception. */
12673 if (!(isec->has_toc_reloc
12674 || (isec->flags & SEC_CODE) == 0
12675 || strcmp (isec->name, ".fixup") == 0
12676 || isec->call_check_done))
12678 if (toc_adjusting_stub_needed (info, isec) < 0)
12681 /* Make all sections use the TOC assigned for this object file.
12682 This will be wrong for pasted sections; We fix that in
12683 check_pasted_section(). */
12684 if (elf_gp (isec->owner) != 0)
12685 htab->toc_curr = elf_gp (isec->owner);
12688 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12692 /* Check that all .init and .fini sections use the same toc, if they
12693 have toc relocs. */
12696 check_pasted_section (struct bfd_link_info *info, const char *name)
12698 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12702 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12703 bfd_vma toc_off = 0;
12706 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12707 if (i->has_toc_reloc)
12710 toc_off = htab->sec_info[i->id].toc_off;
12711 else if (toc_off != htab->sec_info[i->id].toc_off)
12716 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12717 if (i->makes_toc_func_call)
12719 toc_off = htab->sec_info[i->id].toc_off;
12723 /* Make sure the whole pasted function uses the same toc offset. */
12725 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
12726 htab->sec_info[i->id].toc_off = toc_off;
12732 ppc64_elf_check_init_fini (struct bfd_link_info *info)
12734 return (check_pasted_section (info, ".init")
12735 & check_pasted_section (info, ".fini"));
12738 /* See whether we can group stub sections together. Grouping stub
12739 sections may result in fewer stubs. More importantly, we need to
12740 put all .init* and .fini* stubs at the beginning of the .init or
12741 .fini output sections respectively, because glibc splits the
12742 _init and _fini functions into multiple parts. Putting a stub in
12743 the middle of a function is not a good idea. */
12746 group_sections (struct bfd_link_info *info,
12747 bfd_size_type stub_group_size,
12748 bfd_boolean stubs_always_before_branch)
12750 struct ppc_link_hash_table *htab;
12752 bfd_boolean suppress_size_errors;
12754 htab = ppc_hash_table (info);
12758 suppress_size_errors = FALSE;
12759 if (stub_group_size == 1)
12761 /* Default values. */
12762 if (stubs_always_before_branch)
12763 stub_group_size = 0x1e00000;
12765 stub_group_size = 0x1c00000;
12766 suppress_size_errors = TRUE;
12769 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
12773 if (osec->id >= htab->sec_info_arr_size)
12776 tail = htab->sec_info[osec->id].u.list;
12777 while (tail != NULL)
12781 bfd_size_type total;
12782 bfd_boolean big_sec;
12784 struct map_stub *group;
12785 bfd_size_type group_size;
12788 total = tail->size;
12789 group_size = (ppc64_elf_section_data (tail) != NULL
12790 && ppc64_elf_section_data (tail)->has_14bit_branch
12791 ? stub_group_size >> 10 : stub_group_size);
12793 big_sec = total > group_size;
12794 if (big_sec && !suppress_size_errors)
12795 /* xgettext:c-format */
12796 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
12797 tail->owner, tail);
12798 curr_toc = htab->sec_info[tail->id].toc_off;
12800 while ((prev = htab->sec_info[curr->id].u.list) != NULL
12801 && ((total += curr->output_offset - prev->output_offset)
12802 < (ppc64_elf_section_data (prev) != NULL
12803 && ppc64_elf_section_data (prev)->has_14bit_branch
12804 ? (group_size = stub_group_size >> 10) : group_size))
12805 && htab->sec_info[prev->id].toc_off == curr_toc)
12808 /* OK, the size from the start of CURR to the end is less
12809 than group_size and thus can be handled by one stub
12810 section. (or the tail section is itself larger than
12811 group_size, in which case we may be toast.) We should
12812 really be keeping track of the total size of stubs added
12813 here, as stubs contribute to the final output section
12814 size. That's a little tricky, and this way will only
12815 break if stubs added make the total size more than 2^25,
12816 ie. for the default stub_group_size, if stubs total more
12817 than 2097152 bytes, or nearly 75000 plt call stubs. */
12818 group = bfd_alloc (curr->owner, sizeof (*group));
12821 group->link_sec = curr;
12822 group->stub_sec = NULL;
12823 group->needs_save_res = 0;
12824 group->tls_get_addr_opt_bctrl = -1u;
12825 group->next = htab->group;
12826 htab->group = group;
12829 prev = htab->sec_info[tail->id].u.list;
12830 /* Set up this stub group. */
12831 htab->sec_info[tail->id].u.group = group;
12833 while (tail != curr && (tail = prev) != NULL);
12835 /* But wait, there's more! Input sections up to group_size
12836 bytes before the stub section can be handled by it too.
12837 Don't do this if we have a really large section after the
12838 stubs, as adding more stubs increases the chance that
12839 branches may not reach into the stub section. */
12840 if (!stubs_always_before_branch && !big_sec)
12843 while (prev != NULL
12844 && ((total += tail->output_offset - prev->output_offset)
12845 < (ppc64_elf_section_data (prev) != NULL
12846 && ppc64_elf_section_data (prev)->has_14bit_branch
12847 ? (group_size = stub_group_size >> 10) : group_size))
12848 && htab->sec_info[prev->id].toc_off == curr_toc)
12851 prev = htab->sec_info[tail->id].u.list;
12852 htab->sec_info[tail->id].u.group = group;
12861 static const unsigned char glink_eh_frame_cie[] =
12863 0, 0, 0, 16, /* length. */
12864 0, 0, 0, 0, /* id. */
12865 1, /* CIE version. */
12866 'z', 'R', 0, /* Augmentation string. */
12867 4, /* Code alignment. */
12868 0x78, /* Data alignment. */
12870 1, /* Augmentation size. */
12871 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
12872 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
12876 stub_eh_frame_size (struct map_stub *group, size_t align)
12878 size_t this_size = 17;
12879 if (group->tls_get_addr_opt_bctrl != -1u)
12881 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
12884 else if (to_bctrl < 256)
12886 else if (to_bctrl < 65536)
12892 this_size = (this_size + align - 1) & -align;
12896 /* Stripping output sections is normally done before dynamic section
12897 symbols have been allocated. This function is called later, and
12898 handles cases like htab->brlt which is mapped to its own output
12902 maybe_strip_output (struct bfd_link_info *info, asection *isec)
12904 if (isec->size == 0
12905 && isec->output_section->size == 0
12906 && !(isec->output_section->flags & SEC_KEEP)
12907 && !bfd_section_removed_from_list (info->output_bfd,
12908 isec->output_section)
12909 && elf_section_data (isec->output_section)->dynindx == 0)
12911 isec->output_section->flags |= SEC_EXCLUDE;
12912 bfd_section_list_remove (info->output_bfd, isec->output_section);
12913 info->output_bfd->section_count--;
12917 /* Determine and set the size of the stub section for a final link.
12919 The basic idea here is to examine all the relocations looking for
12920 PC-relative calls to a target that is unreachable with a "bl"
12924 ppc64_elf_size_stubs (struct bfd_link_info *info)
12926 bfd_size_type stub_group_size;
12927 bfd_boolean stubs_always_before_branch;
12928 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12933 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
12934 htab->params->plt_thread_safe = 1;
12935 if (!htab->opd_abi)
12936 htab->params->plt_thread_safe = 0;
12937 else if (htab->params->plt_thread_safe == -1)
12939 static const char *const thread_starter[] =
12943 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
12945 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
12946 "mq_notify", "create_timer",
12951 "GOMP_parallel_start",
12952 "GOMP_parallel_loop_static",
12953 "GOMP_parallel_loop_static_start",
12954 "GOMP_parallel_loop_dynamic",
12955 "GOMP_parallel_loop_dynamic_start",
12956 "GOMP_parallel_loop_guided",
12957 "GOMP_parallel_loop_guided_start",
12958 "GOMP_parallel_loop_runtime",
12959 "GOMP_parallel_loop_runtime_start",
12960 "GOMP_parallel_sections",
12961 "GOMP_parallel_sections_start",
12967 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
12969 struct elf_link_hash_entry *h;
12970 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
12971 FALSE, FALSE, TRUE);
12972 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
12973 if (htab->params->plt_thread_safe)
12977 stubs_always_before_branch = htab->params->group_size < 0;
12978 if (htab->params->group_size < 0)
12979 stub_group_size = -htab->params->group_size;
12981 stub_group_size = htab->params->group_size;
12983 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
12986 #define STUB_SHRINK_ITER 20
12987 /* Loop until no stubs added. After iteration 20 of this loop we may
12988 exit on a stub section shrinking. This is to break out of a
12989 pathological case where adding stubs on one iteration decreases
12990 section gaps (perhaps due to alignment), which then requires
12991 fewer or smaller stubs on the next iteration. */
12996 unsigned int bfd_indx;
12997 struct map_stub *group;
12999 htab->stub_iteration += 1;
13001 for (input_bfd = info->input_bfds, bfd_indx = 0;
13003 input_bfd = input_bfd->link.next, bfd_indx++)
13005 Elf_Internal_Shdr *symtab_hdr;
13007 Elf_Internal_Sym *local_syms = NULL;
13009 if (!is_ppc64_elf (input_bfd))
13012 /* We'll need the symbol table in a second. */
13013 symtab_hdr = &elf_symtab_hdr (input_bfd);
13014 if (symtab_hdr->sh_info == 0)
13017 /* Walk over each section attached to the input bfd. */
13018 for (section = input_bfd->sections;
13020 section = section->next)
13022 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13024 /* If there aren't any relocs, then there's nothing more
13026 if ((section->flags & SEC_RELOC) == 0
13027 || (section->flags & SEC_ALLOC) == 0
13028 || (section->flags & SEC_LOAD) == 0
13029 || (section->flags & SEC_CODE) == 0
13030 || section->reloc_count == 0)
13033 /* If this section is a link-once section that will be
13034 discarded, then don't create any stubs. */
13035 if (section->output_section == NULL
13036 || section->output_section->owner != info->output_bfd)
13039 /* Get the relocs. */
13041 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13042 info->keep_memory);
13043 if (internal_relocs == NULL)
13044 goto error_ret_free_local;
13046 /* Now examine each relocation. */
13047 irela = internal_relocs;
13048 irelaend = irela + section->reloc_count;
13049 for (; irela < irelaend; irela++)
13051 enum elf_ppc64_reloc_type r_type;
13052 unsigned int r_indx;
13053 enum ppc_stub_type stub_type;
13054 struct ppc_stub_hash_entry *stub_entry;
13055 asection *sym_sec, *code_sec;
13056 bfd_vma sym_value, code_value;
13057 bfd_vma destination;
13058 unsigned long local_off;
13059 bfd_boolean ok_dest;
13060 struct ppc_link_hash_entry *hash;
13061 struct ppc_link_hash_entry *fdh;
13062 struct elf_link_hash_entry *h;
13063 Elf_Internal_Sym *sym;
13065 const asection *id_sec;
13066 struct _opd_sec_data *opd;
13067 struct plt_entry *plt_ent;
13069 r_type = ELF64_R_TYPE (irela->r_info);
13070 r_indx = ELF64_R_SYM (irela->r_info);
13072 if (r_type >= R_PPC64_max)
13074 bfd_set_error (bfd_error_bad_value);
13075 goto error_ret_free_internal;
13078 /* Only look for stubs on branch instructions. */
13079 if (r_type != R_PPC64_REL24
13080 && r_type != R_PPC64_REL24_NOTOC
13081 && r_type != R_PPC64_REL14
13082 && r_type != R_PPC64_REL14_BRTAKEN
13083 && r_type != R_PPC64_REL14_BRNTAKEN)
13086 /* Now determine the call target, its name, value,
13088 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13089 r_indx, input_bfd))
13090 goto error_ret_free_internal;
13091 hash = (struct ppc_link_hash_entry *) h;
13098 sym_value = sym->st_value;
13099 if (sym_sec != NULL
13100 && sym_sec->output_section != NULL)
13103 else if (hash->elf.root.type == bfd_link_hash_defined
13104 || hash->elf.root.type == bfd_link_hash_defweak)
13106 sym_value = hash->elf.root.u.def.value;
13107 if (sym_sec->output_section != NULL)
13110 else if (hash->elf.root.type == bfd_link_hash_undefweak
13111 || hash->elf.root.type == bfd_link_hash_undefined)
13113 /* Recognise an old ABI func code entry sym, and
13114 use the func descriptor sym instead if it is
13116 if (hash->elf.root.root.string[0] == '.'
13117 && hash->oh != NULL)
13119 fdh = ppc_follow_link (hash->oh);
13120 if (fdh->elf.root.type == bfd_link_hash_defined
13121 || fdh->elf.root.type == bfd_link_hash_defweak)
13123 sym_sec = fdh->elf.root.u.def.section;
13124 sym_value = fdh->elf.root.u.def.value;
13125 if (sym_sec->output_section != NULL)
13134 bfd_set_error (bfd_error_bad_value);
13135 goto error_ret_free_internal;
13142 sym_value += irela->r_addend;
13143 destination = (sym_value
13144 + sym_sec->output_offset
13145 + sym_sec->output_section->vma);
13146 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13151 code_sec = sym_sec;
13152 code_value = sym_value;
13153 opd = get_opd_info (sym_sec);
13158 if (hash == NULL && opd->adjust != NULL)
13160 long adjust = opd->adjust[OPD_NDX (sym_value)];
13163 code_value += adjust;
13164 sym_value += adjust;
13166 dest = opd_entry_value (sym_sec, sym_value,
13167 &code_sec, &code_value, FALSE);
13168 if (dest != (bfd_vma) -1)
13170 destination = dest;
13173 /* Fixup old ABI sym to point at code
13175 hash->elf.root.type = bfd_link_hash_defweak;
13176 hash->elf.root.u.def.section = code_sec;
13177 hash->elf.root.u.def.value = code_value;
13182 /* Determine what (if any) linker stub is needed. */
13184 stub_type = ppc_type_of_stub (section, irela, &hash,
13185 &plt_ent, destination,
13188 if (r_type == R_PPC64_REL24_NOTOC)
13190 if (stub_type == ppc_stub_plt_call)
13191 stub_type = ppc_stub_plt_call_notoc;
13192 else if (stub_type == ppc_stub_long_branch
13193 || (code_sec != NULL
13194 && code_sec->output_section != NULL
13195 && (((hash ? hash->elf.other : sym->st_other)
13196 & STO_PPC64_LOCAL_MASK)
13197 != 1 << STO_PPC64_LOCAL_BIT)))
13198 stub_type = ppc_stub_long_branch_notoc;
13200 else if (stub_type != ppc_stub_plt_call)
13202 /* Check whether we need a TOC adjusting stub.
13203 Since the linker pastes together pieces from
13204 different object files when creating the
13205 _init and _fini functions, it may be that a
13206 call to what looks like a local sym is in
13207 fact a call needing a TOC adjustment. */
13208 if ((code_sec != NULL
13209 && code_sec->output_section != NULL
13210 && (htab->sec_info[code_sec->id].toc_off
13211 != htab->sec_info[section->id].toc_off)
13212 && (code_sec->has_toc_reloc
13213 || code_sec->makes_toc_func_call))
13214 || (((hash ? hash->elf.other : sym->st_other)
13215 & STO_PPC64_LOCAL_MASK)
13216 == 1 << STO_PPC64_LOCAL_BIT))
13217 stub_type = ppc_stub_long_branch_r2off;
13220 if (stub_type == ppc_stub_none)
13223 /* __tls_get_addr calls might be eliminated. */
13224 if (stub_type != ppc_stub_plt_call
13225 && stub_type != ppc_stub_plt_call_notoc
13227 && (hash == htab->tls_get_addr
13228 || hash == htab->tls_get_addr_fd)
13229 && section->has_tls_reloc
13230 && irela != internal_relocs)
13232 /* Get tls info. */
13233 unsigned char *tls_mask;
13235 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13236 irela - 1, input_bfd))
13237 goto error_ret_free_internal;
13238 if ((*tls_mask & TLS_TLS) != 0)
13242 if (stub_type == ppc_stub_plt_call)
13245 && htab->params->plt_localentry0 != 0
13246 && is_elfv2_localentry0 (&hash->elf))
13247 htab->has_plt_localentry0 = 1;
13248 else if (irela + 1 < irelaend
13249 && irela[1].r_offset == irela->r_offset + 4
13250 && (ELF64_R_TYPE (irela[1].r_info)
13251 == R_PPC64_TOCSAVE))
13253 if (!tocsave_find (htab, INSERT,
13254 &local_syms, irela + 1, input_bfd))
13255 goto error_ret_free_internal;
13258 stub_type = ppc_stub_plt_call_r2save;
13261 /* Support for grouping stub sections. */
13262 id_sec = htab->sec_info[section->id].u.group->link_sec;
13264 /* Get the name of this stub. */
13265 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13267 goto error_ret_free_internal;
13269 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13270 stub_name, FALSE, FALSE);
13271 if (stub_entry != NULL)
13273 enum ppc_stub_type old_type;
13274 /* A stub has already been created, but it may
13275 not be the required type. We shouldn't be
13276 transitioning from plt_call to long_branch
13277 stubs or vice versa, but we might be
13278 upgrading from plt_call to plt_call_r2save or
13279 from long_branch to long_branch_r2off. */
13281 old_type = stub_entry->stub_type;
13287 case ppc_stub_save_res:
13290 case ppc_stub_plt_call:
13291 case ppc_stub_plt_call_r2save:
13292 case ppc_stub_plt_call_notoc:
13293 case ppc_stub_plt_call_both:
13294 if (stub_type == ppc_stub_plt_call)
13296 else if (stub_type == ppc_stub_plt_call_r2save)
13298 if (old_type == ppc_stub_plt_call_notoc)
13299 stub_type = ppc_stub_plt_call_both;
13301 else if (stub_type == ppc_stub_plt_call_notoc)
13303 if (old_type == ppc_stub_plt_call_r2save)
13304 stub_type = ppc_stub_plt_call_both;
13310 case ppc_stub_plt_branch:
13311 case ppc_stub_plt_branch_r2off:
13312 case ppc_stub_plt_branch_notoc:
13313 case ppc_stub_plt_branch_both:
13314 old_type += (ppc_stub_long_branch
13315 - ppc_stub_plt_branch);
13316 /* Fall through. */
13317 case ppc_stub_long_branch:
13318 case ppc_stub_long_branch_r2off:
13319 case ppc_stub_long_branch_notoc:
13320 case ppc_stub_long_branch_both:
13321 if (stub_type == ppc_stub_long_branch)
13323 else if (stub_type == ppc_stub_long_branch_r2off)
13325 if (old_type == ppc_stub_long_branch_notoc)
13326 stub_type = ppc_stub_long_branch_both;
13328 else if (stub_type == ppc_stub_long_branch_notoc)
13330 if (old_type == ppc_stub_long_branch_r2off)
13331 stub_type = ppc_stub_long_branch_both;
13337 if (old_type < stub_type)
13338 stub_entry->stub_type = stub_type;
13342 stub_entry = ppc_add_stub (stub_name, section, info);
13343 if (stub_entry == NULL)
13346 error_ret_free_internal:
13347 if (elf_section_data (section)->relocs == NULL)
13348 free (internal_relocs);
13349 error_ret_free_local:
13350 if (local_syms != NULL
13351 && (symtab_hdr->contents
13352 != (unsigned char *) local_syms))
13357 stub_entry->stub_type = stub_type;
13358 if (stub_type >= ppc_stub_plt_call
13359 && stub_type <= ppc_stub_plt_call_both)
13361 stub_entry->target_value = sym_value;
13362 stub_entry->target_section = sym_sec;
13366 stub_entry->target_value = code_value;
13367 stub_entry->target_section = code_sec;
13369 stub_entry->h = hash;
13370 stub_entry->plt_ent = plt_ent;
13371 stub_entry->symtype
13372 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13373 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13375 if (stub_entry->h != NULL)
13376 htab->stub_globals += 1;
13379 /* We're done with the internal relocs, free them. */
13380 if (elf_section_data (section)->relocs != internal_relocs)
13381 free (internal_relocs);
13384 if (local_syms != NULL
13385 && symtab_hdr->contents != (unsigned char *) local_syms)
13387 if (!info->keep_memory)
13390 symtab_hdr->contents = (unsigned char *) local_syms;
13394 /* We may have added some stubs. Find out the new size of the
13396 for (group = htab->group; group != NULL; group = group->next)
13397 if (group->stub_sec != NULL)
13399 asection *stub_sec = group->stub_sec;
13401 if (htab->stub_iteration <= STUB_SHRINK_ITER
13402 || stub_sec->rawsize < stub_sec->size)
13403 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13404 stub_sec->rawsize = stub_sec->size;
13405 stub_sec->size = 0;
13406 stub_sec->reloc_count = 0;
13407 stub_sec->flags &= ~SEC_RELOC;
13410 if (htab->stub_iteration <= STUB_SHRINK_ITER
13411 || htab->brlt->rawsize < htab->brlt->size)
13412 htab->brlt->rawsize = htab->brlt->size;
13413 htab->brlt->size = 0;
13414 htab->brlt->reloc_count = 0;
13415 htab->brlt->flags &= ~SEC_RELOC;
13416 if (htab->relbrlt != NULL)
13417 htab->relbrlt->size = 0;
13419 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13421 for (group = htab->group; group != NULL; group = group->next)
13422 if (group->needs_save_res)
13423 group->stub_sec->size += htab->sfpr->size;
13425 if (info->emitrelocations
13426 && htab->glink != NULL && htab->glink->size != 0)
13428 htab->glink->reloc_count = 1;
13429 htab->glink->flags |= SEC_RELOC;
13432 if (htab->glink_eh_frame != NULL
13433 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13434 && htab->glink_eh_frame->output_section->size > 8)
13436 size_t size = 0, align = 4;
13438 for (group = htab->group; group != NULL; group = group->next)
13439 if (group->stub_sec != NULL)
13440 size += stub_eh_frame_size (group, align);
13441 if (htab->glink != NULL && htab->glink->size != 0)
13442 size += (24 + align - 1) & -align;
13444 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13445 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13446 size = (size + align - 1) & -align;
13447 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13448 htab->glink_eh_frame->size = size;
13451 if (htab->params->plt_stub_align != 0)
13452 for (group = htab->group; group != NULL; group = group->next)
13453 if (group->stub_sec != NULL)
13455 int align = abs (htab->params->plt_stub_align);
13456 group->stub_sec->size
13457 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13460 for (group = htab->group; group != NULL; group = group->next)
13461 if (group->stub_sec != NULL
13462 && group->stub_sec->rawsize != group->stub_sec->size
13463 && (htab->stub_iteration <= STUB_SHRINK_ITER
13464 || group->stub_sec->rawsize < group->stub_sec->size))
13468 && (htab->brlt->rawsize == htab->brlt->size
13469 || (htab->stub_iteration > STUB_SHRINK_ITER
13470 && htab->brlt->rawsize > htab->brlt->size))
13471 && (htab->glink_eh_frame == NULL
13472 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size))
13475 /* Ask the linker to do its stuff. */
13476 (*htab->params->layout_sections_again) ();
13479 if (htab->glink_eh_frame != NULL
13480 && htab->glink_eh_frame->size != 0)
13483 bfd_byte *p, *last_fde;
13484 size_t last_fde_len, size, align, pad;
13485 struct map_stub *group;
13487 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13490 htab->glink_eh_frame->contents = p;
13494 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13495 /* CIE length (rewrite in case little-endian). */
13496 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13497 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13498 p += last_fde_len + 4;
13500 for (group = htab->group; group != NULL; group = group->next)
13501 if (group->stub_sec != NULL)
13504 last_fde_len = stub_eh_frame_size (group, align) - 4;
13506 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13509 val = p - htab->glink_eh_frame->contents;
13510 bfd_put_32 (htab->elf.dynobj, val, p);
13512 /* Offset to stub section, written later. */
13514 /* stub section size. */
13515 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13517 /* Augmentation. */
13519 if (group->tls_get_addr_opt_bctrl != -1u)
13521 unsigned int to_bctrl = group->tls_get_addr_opt_bctrl / 4;
13523 /* This FDE needs more than just the default.
13524 Describe __tls_get_addr_opt stub LR. */
13526 *p++ = DW_CFA_advance_loc + to_bctrl;
13527 else if (to_bctrl < 256)
13529 *p++ = DW_CFA_advance_loc1;
13532 else if (to_bctrl < 65536)
13534 *p++ = DW_CFA_advance_loc2;
13535 bfd_put_16 (htab->elf.dynobj, to_bctrl, p);
13540 *p++ = DW_CFA_advance_loc4;
13541 bfd_put_32 (htab->elf.dynobj, to_bctrl, p);
13544 *p++ = DW_CFA_offset_extended_sf;
13546 *p++ = -(STK_LINKER (htab) / 8) & 0x7f;
13547 *p++ = DW_CFA_advance_loc + 4;
13548 *p++ = DW_CFA_restore_extended;
13552 p = last_fde + last_fde_len + 4;
13554 if (htab->glink != NULL && htab->glink->size != 0)
13557 last_fde_len = ((24 + align - 1) & -align) - 4;
13559 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13562 val = p - htab->glink_eh_frame->contents;
13563 bfd_put_32 (htab->elf.dynobj, val, p);
13565 /* Offset to .glink, written later. */
13568 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13570 /* Augmentation. */
13573 *p++ = DW_CFA_advance_loc + 1;
13574 *p++ = DW_CFA_register;
13576 *p++ = htab->opd_abi ? 12 : 0;
13577 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 5 : 7);
13578 *p++ = DW_CFA_restore_extended;
13580 p += ((24 + align - 1) & -align) - 24;
13582 /* Subsume any padding into the last FDE if user .eh_frame
13583 sections are aligned more than glink_eh_frame. Otherwise any
13584 zero padding will be seen as a terminator. */
13585 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13586 size = p - htab->glink_eh_frame->contents;
13587 pad = ((size + align - 1) & -align) - size;
13588 htab->glink_eh_frame->size = size + pad;
13589 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13592 maybe_strip_output (info, htab->brlt);
13593 if (htab->glink_eh_frame != NULL)
13594 maybe_strip_output (info, htab->glink_eh_frame);
13599 /* Called after we have determined section placement. If sections
13600 move, we'll be called again. Provide a value for TOCstart. */
13603 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13606 bfd_vma TOCstart, adjust;
13610 struct elf_link_hash_entry *h;
13611 struct elf_link_hash_table *htab = elf_hash_table (info);
13613 if (is_elf_hash_table (htab)
13614 && htab->hgot != NULL)
13618 h = elf_link_hash_lookup (htab, ".TOC.", FALSE, FALSE, TRUE);
13619 if (is_elf_hash_table (htab))
13623 && h->root.type == bfd_link_hash_defined
13624 && !h->root.linker_def
13625 && (!is_elf_hash_table (htab)
13626 || h->def_regular))
13628 TOCstart = (h->root.u.def.value - TOC_BASE_OFF
13629 + h->root.u.def.section->output_offset
13630 + h->root.u.def.section->output_section->vma);
13631 _bfd_set_gp_value (obfd, TOCstart);
13636 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13637 order. The TOC starts where the first of these sections starts. */
13638 s = bfd_get_section_by_name (obfd, ".got");
13639 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13640 s = bfd_get_section_by_name (obfd, ".toc");
13641 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13642 s = bfd_get_section_by_name (obfd, ".tocbss");
13643 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13644 s = bfd_get_section_by_name (obfd, ".plt");
13645 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13647 /* This may happen for
13648 o references to TOC base (SYM@toc / TOC[tc0]) without a
13650 o bad linker script
13651 o --gc-sections and empty TOC sections
13653 FIXME: Warn user? */
13655 /* Look for a likely section. We probably won't even be
13657 for (s = obfd->sections; s != NULL; s = s->next)
13658 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
13660 == (SEC_ALLOC | SEC_SMALL_DATA))
13663 for (s = obfd->sections; s != NULL; s = s->next)
13664 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
13665 == (SEC_ALLOC | SEC_SMALL_DATA))
13668 for (s = obfd->sections; s != NULL; s = s->next)
13669 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
13673 for (s = obfd->sections; s != NULL; s = s->next)
13674 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
13680 TOCstart = s->output_section->vma + s->output_offset;
13682 /* Force alignment. */
13683 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
13684 TOCstart -= adjust;
13685 _bfd_set_gp_value (obfd, TOCstart);
13687 if (info != NULL && s != NULL)
13689 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13693 if (htab->elf.hgot != NULL)
13695 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
13696 htab->elf.hgot->root.u.def.section = s;
13701 struct bfd_link_hash_entry *bh = NULL;
13702 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
13703 s, TOC_BASE_OFF - adjust,
13704 NULL, FALSE, FALSE, &bh);
13710 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
13711 write out any global entry stubs, and PLT relocations. */
13714 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
13716 struct bfd_link_info *info;
13717 struct ppc_link_hash_table *htab;
13718 struct plt_entry *ent;
13721 if (h->root.type == bfd_link_hash_indirect)
13725 htab = ppc_hash_table (info);
13729 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13730 if (ent->plt.offset != (bfd_vma) -1)
13732 /* This symbol has an entry in the procedure linkage
13733 table. Set it up. */
13734 Elf_Internal_Rela rela;
13735 asection *plt, *relplt;
13738 if (!htab->elf.dynamic_sections_created
13739 || h->dynindx == -1)
13741 if (!(h->def_regular
13742 && (h->root.type == bfd_link_hash_defined
13743 || h->root.type == bfd_link_hash_defweak)))
13745 if (h->type == STT_GNU_IFUNC)
13747 plt = htab->elf.iplt;
13748 relplt = htab->elf.irelplt;
13749 htab->local_ifunc_resolver = 1;
13751 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13753 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13757 plt = htab->pltlocal;
13758 if (bfd_link_pic (info))
13760 relplt = htab->relpltlocal;
13762 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13764 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13769 rela.r_addend = (h->root.u.def.value
13770 + h->root.u.def.section->output_offset
13771 + h->root.u.def.section->output_section->vma
13774 if (relplt == NULL)
13776 loc = plt->contents + ent->plt.offset;
13777 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
13780 bfd_vma toc = elf_gp (info->output_bfd);
13781 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
13782 bfd_put_64 (info->output_bfd, toc, loc + 8);
13787 rela.r_offset = (plt->output_section->vma
13788 + plt->output_offset
13789 + ent->plt.offset);
13790 loc = relplt->contents + (relplt->reloc_count++
13791 * sizeof (Elf64_External_Rela));
13792 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13797 rela.r_offset = (htab->elf.splt->output_section->vma
13798 + htab->elf.splt->output_offset
13799 + ent->plt.offset);
13800 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
13801 rela.r_addend = ent->addend;
13802 loc = (htab->elf.srelplt->contents
13803 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
13804 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
13805 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
13806 htab->maybe_local_ifunc_resolver = 1;
13807 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13811 if (!h->pointer_equality_needed)
13814 if (h->def_regular)
13817 s = htab->global_entry;
13818 if (s == NULL || s->size == 0)
13821 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
13822 if (ent->plt.offset != (bfd_vma) -1
13823 && ent->addend == 0)
13829 p = s->contents + h->root.u.def.value;
13830 plt = htab->elf.splt;
13831 if (!htab->elf.dynamic_sections_created
13832 || h->dynindx == -1)
13834 if (h->type == STT_GNU_IFUNC)
13835 plt = htab->elf.iplt;
13837 plt = htab->pltlocal;
13839 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
13840 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
13842 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
13844 info->callbacks->einfo
13845 (_("%P: linkage table error against `%pT'\n"),
13846 h->root.root.string);
13847 bfd_set_error (bfd_error_bad_value);
13848 htab->stub_error = TRUE;
13851 htab->stub_count[ppc_stub_global_entry - 1] += 1;
13852 if (htab->params->emit_stub_syms)
13854 size_t len = strlen (h->root.root.string);
13855 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
13860 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
13861 h = elf_link_hash_lookup (&htab->elf, name, TRUE, FALSE, FALSE);
13864 if (h->root.type == bfd_link_hash_new)
13866 h->root.type = bfd_link_hash_defined;
13867 h->root.u.def.section = s;
13868 h->root.u.def.value = p - s->contents;
13869 h->ref_regular = 1;
13870 h->def_regular = 1;
13871 h->ref_regular_nonweak = 1;
13872 h->forced_local = 1;
13874 h->root.linker_def = 1;
13878 if (PPC_HA (off) != 0)
13880 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
13883 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
13885 bfd_put_32 (s->owner, MTCTR_R12, p);
13887 bfd_put_32 (s->owner, BCTR, p);
13893 /* Write PLT relocs for locals. */
13896 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
13898 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13901 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13903 struct got_entry **lgot_ents, **end_lgot_ents;
13904 struct plt_entry **local_plt, **lplt, **end_local_plt;
13905 Elf_Internal_Shdr *symtab_hdr;
13906 bfd_size_type locsymcount;
13907 Elf_Internal_Sym *local_syms = NULL;
13908 struct plt_entry *ent;
13910 if (!is_ppc64_elf (ibfd))
13913 lgot_ents = elf_local_got_ents (ibfd);
13917 symtab_hdr = &elf_symtab_hdr (ibfd);
13918 locsymcount = symtab_hdr->sh_info;
13919 end_lgot_ents = lgot_ents + locsymcount;
13920 local_plt = (struct plt_entry **) end_lgot_ents;
13921 end_local_plt = local_plt + locsymcount;
13922 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
13923 for (ent = *lplt; ent != NULL; ent = ent->next)
13924 if (ent->plt.offset != (bfd_vma) -1)
13926 Elf_Internal_Sym *sym;
13928 asection *plt, *relplt;
13932 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
13933 lplt - local_plt, ibfd))
13935 if (local_syms != NULL
13936 && symtab_hdr->contents != (unsigned char *) local_syms)
13941 val = sym->st_value + ent->addend;
13942 val += PPC64_LOCAL_ENTRY_OFFSET (sym->st_other);
13943 if (sym_sec != NULL && sym_sec->output_section != NULL)
13944 val += sym_sec->output_offset + sym_sec->output_section->vma;
13946 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13948 htab->local_ifunc_resolver = 1;
13949 plt = htab->elf.iplt;
13950 relplt = htab->elf.irelplt;
13954 plt = htab->pltlocal;
13955 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
13958 if (relplt == NULL)
13960 loc = plt->contents + ent->plt.offset;
13961 bfd_put_64 (info->output_bfd, val, loc);
13964 bfd_vma toc = elf_gp (ibfd);
13965 bfd_put_64 (info->output_bfd, toc, loc + 8);
13970 Elf_Internal_Rela rela;
13971 rela.r_offset = (ent->plt.offset
13972 + plt->output_offset
13973 + plt->output_section->vma);
13974 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13977 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
13979 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
13984 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
13986 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
13988 rela.r_addend = val;
13989 loc = relplt->contents + (relplt->reloc_count++
13990 * sizeof (Elf64_External_Rela));
13991 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
13995 if (local_syms != NULL
13996 && symtab_hdr->contents != (unsigned char *) local_syms)
13998 if (!info->keep_memory)
14001 symtab_hdr->contents = (unsigned char *) local_syms;
14007 /* Build all the stubs associated with the current output file.
14008 The stubs are kept in a hash table attached to the main linker
14009 hash table. This function is called via gldelf64ppc_finish. */
14012 ppc64_elf_build_stubs (struct bfd_link_info *info,
14015 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14016 struct map_stub *group;
14017 asection *stub_sec;
14019 int stub_sec_count = 0;
14024 /* Allocate memory to hold the linker stubs. */
14025 for (group = htab->group; group != NULL; group = group->next)
14026 if ((stub_sec = group->stub_sec) != NULL
14027 && stub_sec->size != 0)
14029 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd, stub_sec->size);
14030 if (stub_sec->contents == NULL)
14032 stub_sec->size = 0;
14035 if (htab->glink != NULL && htab->glink->size != 0)
14040 /* Build the .glink plt call stub. */
14041 if (htab->params->emit_stub_syms)
14043 struct elf_link_hash_entry *h;
14044 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14045 TRUE, FALSE, FALSE);
14048 if (h->root.type == bfd_link_hash_new)
14050 h->root.type = bfd_link_hash_defined;
14051 h->root.u.def.section = htab->glink;
14052 h->root.u.def.value = 8;
14053 h->ref_regular = 1;
14054 h->def_regular = 1;
14055 h->ref_regular_nonweak = 1;
14056 h->forced_local = 1;
14058 h->root.linker_def = 1;
14061 plt0 = (htab->elf.splt->output_section->vma
14062 + htab->elf.splt->output_offset
14064 if (info->emitrelocations)
14066 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14069 r->r_offset = (htab->glink->output_offset
14070 + htab->glink->output_section->vma);
14071 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14072 r->r_addend = plt0;
14074 p = htab->glink->contents;
14075 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14076 bfd_put_64 (htab->glink->owner, plt0, p);
14080 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14082 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14084 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14086 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14088 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14090 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14092 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14094 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14096 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14098 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14103 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14105 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14107 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14109 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14111 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14113 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14115 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14117 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14119 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-48 & 0xffff), p);
14121 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14123 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14125 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14127 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14130 bfd_put_32 (htab->glink->owner, BCTR, p);
14132 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14134 /* Build the .glink lazy link call stubs. */
14136 while (p < htab->glink->contents + htab->glink->size)
14142 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14147 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14149 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14154 bfd_put_32 (htab->glink->owner,
14155 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14161 /* Build .glink global entry stubs, and PLT relocs for globals. */
14162 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14164 if (!write_plt_relocs_for_local_syms (info))
14167 if (htab->brlt != NULL && htab->brlt->size != 0)
14169 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
14171 if (htab->brlt->contents == NULL)
14174 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
14176 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
14177 htab->relbrlt->size);
14178 if (htab->relbrlt->contents == NULL)
14182 /* Build the stubs as directed by the stub hash table. */
14183 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14185 for (group = htab->group; group != NULL; group = group->next)
14186 if (group->needs_save_res)
14187 group->stub_sec->size += htab->sfpr->size;
14189 if (htab->relbrlt != NULL)
14190 htab->relbrlt->reloc_count = 0;
14192 if (htab->params->plt_stub_align != 0)
14193 for (group = htab->group; group != NULL; group = group->next)
14194 if ((stub_sec = group->stub_sec) != NULL)
14196 int align = abs (htab->params->plt_stub_align);
14197 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14200 for (group = htab->group; group != NULL; group = group->next)
14201 if (group->needs_save_res)
14203 stub_sec = group->stub_sec;
14204 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14205 htab->sfpr->contents, htab->sfpr->size);
14206 if (htab->params->emit_stub_syms)
14210 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14211 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14216 for (group = htab->group; group != NULL; group = group->next)
14217 if ((stub_sec = group->stub_sec) != NULL)
14219 stub_sec_count += 1;
14220 if (stub_sec->rawsize != stub_sec->size
14221 && (htab->stub_iteration <= STUB_SHRINK_ITER
14222 || stub_sec->rawsize < stub_sec->size))
14228 htab->stub_error = TRUE;
14229 _bfd_error_handler (_("stubs don't match calculated size"));
14232 if (htab->stub_error)
14238 *stats = bfd_malloc (500);
14239 if (*stats == NULL)
14242 len = sprintf (*stats,
14243 ngettext ("linker stubs in %u group\n",
14244 "linker stubs in %u groups\n",
14247 sprintf (*stats + len, _(" branch %lu\n"
14248 " branch toc adj %lu\n"
14249 " branch notoc %lu\n"
14250 " branch both %lu\n"
14251 " long branch %lu\n"
14252 " long toc adj %lu\n"
14253 " long notoc %lu\n"
14256 " plt call save %lu\n"
14257 " plt call notoc %lu\n"
14258 " plt call both %lu\n"
14259 " global entry %lu"),
14260 htab->stub_count[ppc_stub_long_branch - 1],
14261 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14262 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14263 htab->stub_count[ppc_stub_long_branch_both - 1],
14264 htab->stub_count[ppc_stub_plt_branch - 1],
14265 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14266 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14267 htab->stub_count[ppc_stub_plt_branch_both - 1],
14268 htab->stub_count[ppc_stub_plt_call - 1],
14269 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14270 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14271 htab->stub_count[ppc_stub_plt_call_both - 1],
14272 htab->stub_count[ppc_stub_global_entry - 1]);
14277 /* What to do when ld finds relocations against symbols defined in
14278 discarded sections. */
14280 static unsigned int
14281 ppc64_elf_action_discarded (asection *sec)
14283 if (strcmp (".opd", sec->name) == 0)
14286 if (strcmp (".toc", sec->name) == 0)
14289 if (strcmp (".toc1", sec->name) == 0)
14292 return _bfd_elf_default_action_discarded (sec);
14295 /* The RELOCATE_SECTION function is called by the ELF backend linker
14296 to handle the relocations for a section.
14298 The relocs are always passed as Rela structures; if the section
14299 actually uses Rel structures, the r_addend field will always be
14302 This function is responsible for adjust the section contents as
14303 necessary, and (if using Rela relocs and generating a
14304 relocatable output file) adjusting the reloc addend as
14307 This function does not have to worry about setting the reloc
14308 address or the reloc symbol index.
14310 LOCAL_SYMS is a pointer to the swapped in local symbols.
14312 LOCAL_SECTIONS is an array giving the section in the input file
14313 corresponding to the st_shndx field of each local symbol.
14315 The global hash table entry for the global symbols can be found
14316 via elf_sym_hashes (input_bfd).
14318 When generating relocatable output, this function must handle
14319 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14320 going to be the section symbol corresponding to the output
14321 section, which means that the addend must be adjusted
14325 ppc64_elf_relocate_section (bfd *output_bfd,
14326 struct bfd_link_info *info,
14328 asection *input_section,
14329 bfd_byte *contents,
14330 Elf_Internal_Rela *relocs,
14331 Elf_Internal_Sym *local_syms,
14332 asection **local_sections)
14334 struct ppc_link_hash_table *htab;
14335 Elf_Internal_Shdr *symtab_hdr;
14336 struct elf_link_hash_entry **sym_hashes;
14337 Elf_Internal_Rela *rel;
14338 Elf_Internal_Rela *wrel;
14339 Elf_Internal_Rela *relend;
14340 Elf_Internal_Rela outrel;
14342 struct got_entry **local_got_ents;
14344 bfd_boolean ret = TRUE;
14345 bfd_boolean is_opd;
14346 /* Assume 'at' branch hints. */
14347 bfd_boolean is_isa_v2 = TRUE;
14348 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14350 /* Initialize howto table if needed. */
14351 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14354 htab = ppc_hash_table (info);
14358 /* Don't relocate stub sections. */
14359 if (input_section->owner == htab->params->stub_bfd)
14362 BFD_ASSERT (is_ppc64_elf (input_bfd));
14364 local_got_ents = elf_local_got_ents (input_bfd);
14365 TOCstart = elf_gp (output_bfd);
14366 symtab_hdr = &elf_symtab_hdr (input_bfd);
14367 sym_hashes = elf_sym_hashes (input_bfd);
14368 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14370 rel = wrel = relocs;
14371 relend = relocs + input_section->reloc_count;
14372 for (; rel < relend; wrel++, rel++)
14374 enum elf_ppc64_reloc_type r_type;
14376 bfd_reloc_status_type r;
14377 Elf_Internal_Sym *sym;
14379 struct elf_link_hash_entry *h_elf;
14380 struct ppc_link_hash_entry *h;
14381 struct ppc_link_hash_entry *fdh;
14382 const char *sym_name;
14383 unsigned long r_symndx, toc_symndx;
14384 bfd_vma toc_addend;
14385 unsigned char tls_mask, tls_gd, tls_type;
14386 unsigned char sym_type;
14387 bfd_vma relocation;
14388 bfd_boolean unresolved_reloc, save_unresolved_reloc;
14389 bfd_boolean warned;
14390 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14393 struct ppc_stub_hash_entry *stub_entry;
14394 bfd_vma max_br_offset;
14396 Elf_Internal_Rela orig_rel;
14397 reloc_howto_type *howto;
14398 struct reloc_howto_struct alt_howto;
14403 r_type = ELF64_R_TYPE (rel->r_info);
14404 r_symndx = ELF64_R_SYM (rel->r_info);
14406 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14407 symbol of the previous ADDR64 reloc. The symbol gives us the
14408 proper TOC base to use. */
14409 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14411 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14413 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14419 unresolved_reloc = FALSE;
14422 if (r_symndx < symtab_hdr->sh_info)
14424 /* It's a local symbol. */
14425 struct _opd_sec_data *opd;
14427 sym = local_syms + r_symndx;
14428 sec = local_sections[r_symndx];
14429 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
14430 sym_type = ELF64_ST_TYPE (sym->st_info);
14431 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
14432 opd = get_opd_info (sec);
14433 if (opd != NULL && opd->adjust != NULL)
14435 long adjust = opd->adjust[OPD_NDX (sym->st_value
14441 /* If this is a relocation against the opd section sym
14442 and we have edited .opd, adjust the reloc addend so
14443 that ld -r and ld --emit-relocs output is correct.
14444 If it is a reloc against some other .opd symbol,
14445 then the symbol value will be adjusted later. */
14446 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
14447 rel->r_addend += adjust;
14449 relocation += adjust;
14455 bfd_boolean ignored;
14457 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
14458 r_symndx, symtab_hdr, sym_hashes,
14459 h_elf, sec, relocation,
14460 unresolved_reloc, warned, ignored);
14461 sym_name = h_elf->root.root.string;
14462 sym_type = h_elf->type;
14464 && sec->owner == output_bfd
14465 && strcmp (sec->name, ".opd") == 0)
14467 /* This is a symbol defined in a linker script. All
14468 such are defined in output sections, even those
14469 defined by simple assignment from a symbol defined in
14470 an input section. Transfer the symbol to an
14471 appropriate input .opd section, so that a branch to
14472 this symbol will be mapped to the location specified
14473 by the opd entry. */
14474 struct bfd_link_order *lo;
14475 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
14476 if (lo->type == bfd_indirect_link_order)
14478 asection *isec = lo->u.indirect.section;
14479 if (h_elf->root.u.def.value >= isec->output_offset
14480 && h_elf->root.u.def.value < (isec->output_offset
14483 h_elf->root.u.def.value -= isec->output_offset;
14484 h_elf->root.u.def.section = isec;
14491 h = (struct ppc_link_hash_entry *) h_elf;
14493 if (sec != NULL && discarded_section (sec))
14495 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
14496 input_bfd, input_section,
14497 contents + rel->r_offset);
14498 wrel->r_offset = rel->r_offset;
14500 wrel->r_addend = 0;
14502 /* For ld -r, remove relocations in debug sections against
14503 symbols defined in discarded sections. Not done for
14504 non-debug to preserve relocs in .eh_frame which the
14505 eh_frame editing code expects to be present. */
14506 if (bfd_link_relocatable (info)
14507 && (input_section->flags & SEC_DEBUGGING))
14513 if (bfd_link_relocatable (info))
14516 if (h != NULL && &h->elf == htab->elf.hgot)
14518 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14519 sec = bfd_abs_section_ptr;
14520 unresolved_reloc = FALSE;
14523 /* TLS optimizations. Replace instruction sequences and relocs
14524 based on information we collected in tls_optimize. We edit
14525 RELOCS so that --emit-relocs will output something sensible
14526 for the final instruction stream. */
14531 tls_mask = h->tls_mask;
14532 else if (local_got_ents != NULL)
14534 struct plt_entry **local_plt = (struct plt_entry **)
14535 (local_got_ents + symtab_hdr->sh_info);
14536 unsigned char *lgot_masks = (unsigned char *)
14537 (local_plt + symtab_hdr->sh_info);
14538 tls_mask = lgot_masks[r_symndx];
14540 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
14541 && (r_type == R_PPC64_TLS
14542 || r_type == R_PPC64_TLSGD
14543 || r_type == R_PPC64_TLSLD))
14545 /* Check for toc tls entries. */
14546 unsigned char *toc_tls;
14548 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14549 &local_syms, rel, input_bfd))
14553 tls_mask = *toc_tls;
14556 /* Check that tls relocs are used with tls syms, and non-tls
14557 relocs are used with non-tls syms. */
14558 if (r_symndx != STN_UNDEF
14559 && r_type != R_PPC64_NONE
14561 || h->elf.root.type == bfd_link_hash_defined
14562 || h->elf.root.type == bfd_link_hash_defweak)
14563 && (IS_PPC64_TLS_RELOC (r_type)
14564 != (sym_type == STT_TLS
14565 || (sym_type == STT_SECTION
14566 && (sec->flags & SEC_THREAD_LOCAL) != 0))))
14568 if ((tls_mask & TLS_TLS) != 0
14569 && (r_type == R_PPC64_TLS
14570 || r_type == R_PPC64_TLSGD
14571 || r_type == R_PPC64_TLSLD))
14572 /* R_PPC64_TLS is OK against a symbol in the TOC. */
14575 info->callbacks->einfo
14576 (!IS_PPC64_TLS_RELOC (r_type)
14577 /* xgettext:c-format */
14578 ? _("%H: %s used with TLS symbol `%pT'\n")
14579 /* xgettext:c-format */
14580 : _("%H: %s used with non-TLS symbol `%pT'\n"),
14581 input_bfd, input_section, rel->r_offset,
14582 ppc64_elf_howto_table[r_type]->name,
14586 /* Ensure reloc mapping code below stays sane. */
14587 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
14588 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
14589 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
14590 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
14591 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
14592 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
14593 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
14594 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
14595 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
14596 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
14604 case R_PPC64_LO_DS_OPT:
14605 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
14606 if ((insn & (0x3f << 26)) != 58u << 26)
14608 insn += (14u << 26) - (58u << 26);
14609 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
14610 r_type = R_PPC64_TOC16_LO;
14611 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14614 case R_PPC64_TOC16:
14615 case R_PPC64_TOC16_LO:
14616 case R_PPC64_TOC16_DS:
14617 case R_PPC64_TOC16_LO_DS:
14619 /* Check for toc tls entries. */
14620 unsigned char *toc_tls;
14623 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
14624 &local_syms, rel, input_bfd);
14630 tls_mask = *toc_tls;
14631 if (r_type == R_PPC64_TOC16_DS
14632 || r_type == R_PPC64_TOC16_LO_DS)
14634 if ((tls_mask & TLS_TLS) != 0
14635 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
14640 /* If we found a GD reloc pair, then we might be
14641 doing a GD->IE transition. */
14644 tls_gd = TLS_TPRELGD;
14645 if ((tls_mask & TLS_TLS) != 0
14646 && (tls_mask & TLS_GD) == 0)
14649 else if (retval == 3)
14651 if ((tls_mask & TLS_TLS) != 0
14652 && (tls_mask & TLS_LD) == 0)
14660 case R_PPC64_GOT_TPREL16_HI:
14661 case R_PPC64_GOT_TPREL16_HA:
14662 if ((tls_mask & TLS_TLS) != 0
14663 && (tls_mask & TLS_TPREL) == 0)
14665 rel->r_offset -= d_offset;
14666 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14667 r_type = R_PPC64_NONE;
14668 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14672 case R_PPC64_GOT_TPREL16_DS:
14673 case R_PPC64_GOT_TPREL16_LO_DS:
14674 if ((tls_mask & TLS_TLS) != 0
14675 && (tls_mask & TLS_TPREL) == 0)
14678 insn = bfd_get_32 (input_bfd,
14679 contents + rel->r_offset - d_offset);
14681 insn |= 0x3c0d0000; /* addis 0,13,0 */
14682 bfd_put_32 (input_bfd, insn,
14683 contents + rel->r_offset - d_offset);
14684 r_type = R_PPC64_TPREL16_HA;
14685 if (toc_symndx != 0)
14687 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14688 rel->r_addend = toc_addend;
14689 /* We changed the symbol. Start over in order to
14690 get h, sym, sec etc. right. */
14694 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14699 if ((tls_mask & TLS_TLS) != 0
14700 && (tls_mask & TLS_TPREL) == 0)
14702 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
14703 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
14706 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
14707 /* Was PPC64_TLS which sits on insn boundary, now
14708 PPC64_TPREL16_LO which is at low-order half-word. */
14709 rel->r_offset += d_offset;
14710 r_type = R_PPC64_TPREL16_LO;
14711 if (toc_symndx != 0)
14713 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
14714 rel->r_addend = toc_addend;
14715 /* We changed the symbol. Start over in order to
14716 get h, sym, sec etc. right. */
14720 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14724 case R_PPC64_GOT_TLSGD16_HI:
14725 case R_PPC64_GOT_TLSGD16_HA:
14726 tls_gd = TLS_TPRELGD;
14727 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14731 case R_PPC64_GOT_TLSLD16_HI:
14732 case R_PPC64_GOT_TLSLD16_HA:
14733 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14736 if ((tls_mask & tls_gd) != 0)
14737 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14738 + R_PPC64_GOT_TPREL16_DS);
14741 rel->r_offset -= d_offset;
14742 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
14743 r_type = R_PPC64_NONE;
14745 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14749 case R_PPC64_GOT_TLSGD16:
14750 case R_PPC64_GOT_TLSGD16_LO:
14751 tls_gd = TLS_TPRELGD;
14752 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
14756 case R_PPC64_GOT_TLSLD16:
14757 case R_PPC64_GOT_TLSLD16_LO:
14758 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
14760 unsigned int insn1, insn2;
14764 offset = (bfd_vma) -1;
14765 /* If not using the newer R_PPC64_TLSGD/LD to mark
14766 __tls_get_addr calls, we must trust that the call
14767 stays with its arg setup insns, ie. that the next
14768 reloc is the __tls_get_addr call associated with
14769 the current reloc. Edit both insns. */
14770 if (input_section->has_tls_get_addr_call
14771 && rel + 1 < relend
14772 && branch_reloc_hash_match (input_bfd, rel + 1,
14773 htab->tls_get_addr,
14774 htab->tls_get_addr_fd))
14775 offset = rel[1].r_offset;
14776 /* We read the low GOT_TLS (or TOC16) insn because we
14777 need to keep the destination reg. It may be
14778 something other than the usual r3, and moved to r3
14779 before the call by intervening code. */
14780 insn1 = bfd_get_32 (input_bfd,
14781 contents + rel->r_offset - d_offset);
14782 if ((tls_mask & tls_gd) != 0)
14785 insn1 &= (0x1f << 21) | (0x1f << 16);
14786 insn1 |= 58 << 26; /* ld */
14787 insn2 = 0x7c636a14; /* add 3,3,13 */
14788 if (offset != (bfd_vma) -1)
14789 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14790 if ((tls_mask & TLS_EXPLICIT) == 0)
14791 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
14792 + R_PPC64_GOT_TPREL16_DS);
14794 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
14795 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14800 insn1 &= 0x1f << 21;
14801 insn1 |= 0x3c0d0000; /* addis r,13,0 */
14802 insn2 = 0x38630000; /* addi 3,3,0 */
14805 /* Was an LD reloc. */
14807 sec = local_sections[toc_symndx];
14809 r_symndx < symtab_hdr->sh_info;
14811 if (local_sections[r_symndx] == sec)
14813 if (r_symndx >= symtab_hdr->sh_info)
14814 r_symndx = STN_UNDEF;
14815 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14816 if (r_symndx != STN_UNDEF)
14817 rel->r_addend -= (local_syms[r_symndx].st_value
14818 + sec->output_offset
14819 + sec->output_section->vma);
14821 else if (toc_symndx != 0)
14823 r_symndx = toc_symndx;
14824 rel->r_addend = toc_addend;
14826 r_type = R_PPC64_TPREL16_HA;
14827 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14828 if (offset != (bfd_vma) -1)
14830 rel[1].r_info = ELF64_R_INFO (r_symndx,
14831 R_PPC64_TPREL16_LO);
14832 rel[1].r_offset = offset + d_offset;
14833 rel[1].r_addend = rel->r_addend;
14836 bfd_put_32 (input_bfd, insn1,
14837 contents + rel->r_offset - d_offset);
14838 if (offset != (bfd_vma) -1)
14839 bfd_put_32 (input_bfd, insn2, contents + offset);
14840 if ((tls_mask & tls_gd) == 0
14841 && (tls_gd == 0 || toc_symndx != 0))
14843 /* We changed the symbol. Start over in order
14844 to get h, sym, sec etc. right. */
14850 case R_PPC64_TLSGD:
14851 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
14852 && rel + 1 < relend)
14854 unsigned int insn2;
14855 bfd_vma offset = rel->r_offset;
14857 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14859 bfd_put_32 (output_bfd, NOP, contents + offset);
14860 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14864 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14865 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14867 if ((tls_mask & TLS_TPRELGD) != 0)
14870 r_type = R_PPC64_NONE;
14871 insn2 = 0x7c636a14; /* add 3,3,13 */
14876 if (toc_symndx != 0)
14878 r_symndx = toc_symndx;
14879 rel->r_addend = toc_addend;
14881 r_type = R_PPC64_TPREL16_LO;
14882 rel->r_offset = offset + d_offset;
14883 insn2 = 0x38630000; /* addi 3,3,0 */
14885 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14886 /* Zap the reloc on the _tls_get_addr call too. */
14887 BFD_ASSERT (offset == rel[1].r_offset);
14888 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14889 bfd_put_32 (input_bfd, insn2, contents + offset);
14890 if ((tls_mask & TLS_TPRELGD) == 0 && toc_symndx != 0)
14895 case R_PPC64_TLSLD:
14896 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
14897 && rel + 1 < relend)
14899 unsigned int insn2;
14900 bfd_vma offset = rel->r_offset;
14902 if (is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
14904 bfd_put_32 (output_bfd, NOP, contents + offset);
14905 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14909 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
14910 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
14913 sec = local_sections[toc_symndx];
14915 r_symndx < symtab_hdr->sh_info;
14917 if (local_sections[r_symndx] == sec)
14919 if (r_symndx >= symtab_hdr->sh_info)
14920 r_symndx = STN_UNDEF;
14921 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
14922 if (r_symndx != STN_UNDEF)
14923 rel->r_addend -= (local_syms[r_symndx].st_value
14924 + sec->output_offset
14925 + sec->output_section->vma);
14927 r_type = R_PPC64_TPREL16_LO;
14928 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14929 rel->r_offset = offset + d_offset;
14930 /* Zap the reloc on the _tls_get_addr call too. */
14931 BFD_ASSERT (offset == rel[1].r_offset);
14932 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
14933 insn2 = 0x38630000; /* addi 3,3,0 */
14934 bfd_put_32 (input_bfd, insn2, contents + offset);
14939 case R_PPC64_DTPMOD64:
14940 if (rel + 1 < relend
14941 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
14942 && rel[1].r_offset == rel->r_offset + 8)
14944 if ((tls_mask & TLS_GD) == 0)
14946 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
14947 if ((tls_mask & TLS_TPRELGD) != 0)
14948 r_type = R_PPC64_TPREL64;
14951 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14952 r_type = R_PPC64_NONE;
14954 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14959 if ((tls_mask & TLS_LD) == 0)
14961 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
14962 r_type = R_PPC64_NONE;
14963 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14968 case R_PPC64_TPREL64:
14969 if ((tls_mask & TLS_TPREL) == 0)
14971 r_type = R_PPC64_NONE;
14972 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
14976 case R_PPC64_ENTRY:
14977 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
14978 if (!bfd_link_pic (info)
14979 && !info->traditional_format
14980 && relocation + 0x80008000 <= 0xffffffff)
14982 unsigned int insn1, insn2;
14984 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
14985 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
14986 if ((insn1 & ~0xfffc) == LD_R2_0R12
14987 && insn2 == ADD_R2_R2_R12)
14989 bfd_put_32 (input_bfd,
14990 LIS_R2 + PPC_HA (relocation),
14991 contents + rel->r_offset);
14992 bfd_put_32 (input_bfd,
14993 ADDI_R2_R2 + PPC_LO (relocation),
14994 contents + rel->r_offset + 4);
14999 relocation -= (rel->r_offset
15000 + input_section->output_offset
15001 + input_section->output_section->vma);
15002 if (relocation + 0x80008000 <= 0xffffffff)
15004 unsigned int insn1, insn2;
15006 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15007 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15008 if ((insn1 & ~0xfffc) == LD_R2_0R12
15009 && insn2 == ADD_R2_R2_R12)
15011 bfd_put_32 (input_bfd,
15012 ADDIS_R2_R12 + PPC_HA (relocation),
15013 contents + rel->r_offset);
15014 bfd_put_32 (input_bfd,
15015 ADDI_R2_R2 + PPC_LO (relocation),
15016 contents + rel->r_offset + 4);
15022 case R_PPC64_REL16_HA:
15023 /* If we are generating a non-PIC executable, edit
15024 . 0: addis 2,12,.TOC.-0b@ha
15025 . addi 2,2,.TOC.-0b@l
15026 used by ELFv2 global entry points to set up r2, to
15029 if .TOC. is in range. */
15030 if (!bfd_link_pic (info)
15031 && !info->traditional_format
15033 && rel->r_addend == d_offset
15034 && h != NULL && &h->elf == htab->elf.hgot
15035 && rel + 1 < relend
15036 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15037 && rel[1].r_offset == rel->r_offset + 4
15038 && rel[1].r_addend == rel->r_addend + 4
15039 && relocation + 0x80008000 <= 0xffffffff)
15041 unsigned int insn1, insn2;
15042 bfd_vma offset = rel->r_offset - d_offset;
15043 insn1 = bfd_get_32 (input_bfd, contents + offset);
15044 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15045 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15046 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15048 r_type = R_PPC64_ADDR16_HA;
15049 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15050 rel->r_addend -= d_offset;
15051 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15052 rel[1].r_addend -= d_offset + 4;
15053 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15059 /* Handle other relocations that tweak non-addend part of insn. */
15061 max_br_offset = 1 << 25;
15062 addend = rel->r_addend;
15063 reloc_dest = DEST_NORMAL;
15069 case R_PPC64_TOCSAVE:
15070 if (relocation + addend == (rel->r_offset
15071 + input_section->output_offset
15072 + input_section->output_section->vma)
15073 && tocsave_find (htab, NO_INSERT,
15074 &local_syms, rel, input_bfd))
15076 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15078 || insn == CROR_151515 || insn == CROR_313131)
15079 bfd_put_32 (input_bfd,
15080 STD_R2_0R1 + STK_TOC (htab),
15081 contents + rel->r_offset);
15085 /* Branch taken prediction relocations. */
15086 case R_PPC64_ADDR14_BRTAKEN:
15087 case R_PPC64_REL14_BRTAKEN:
15088 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15089 /* Fall through. */
15091 /* Branch not taken prediction relocations. */
15092 case R_PPC64_ADDR14_BRNTAKEN:
15093 case R_PPC64_REL14_BRNTAKEN:
15094 insn |= bfd_get_32 (input_bfd,
15095 contents + rel->r_offset) & ~(0x01 << 21);
15096 /* Fall through. */
15098 case R_PPC64_REL14:
15099 max_br_offset = 1 << 15;
15100 /* Fall through. */
15102 case R_PPC64_REL24:
15103 case R_PPC64_REL24_NOTOC:
15104 case R_PPC64_PLTCALL:
15105 /* Calls to functions with a different TOC, such as calls to
15106 shared objects, need to alter the TOC pointer. This is
15107 done using a linkage stub. A REL24 branching to these
15108 linkage stubs needs to be followed by a nop, as the nop
15109 will be replaced with an instruction to restore the TOC
15114 && h->oh->is_func_descriptor)
15115 fdh = ppc_follow_link (h->oh);
15116 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15118 if (r_type == R_PPC64_PLTCALL
15119 && stub_entry != NULL
15120 && stub_entry->stub_type >= ppc_stub_plt_call
15121 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15124 if (stub_entry != NULL
15125 && (stub_entry->stub_type == ppc_stub_plt_call
15126 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15127 || stub_entry->stub_type == ppc_stub_plt_call_both
15128 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15129 || stub_entry->stub_type == ppc_stub_plt_branch_both
15130 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15131 || stub_entry->stub_type == ppc_stub_long_branch_both))
15133 bfd_boolean can_plt_call = FALSE;
15135 if (stub_entry->stub_type == ppc_stub_plt_call
15137 && htab->params->plt_localentry0 != 0
15138 && is_elfv2_localentry0 (&h->elf))
15140 /* The function doesn't use or change r2. */
15141 can_plt_call = TRUE;
15143 else if (r_type == R_PPC64_REL24_NOTOC)
15145 /* NOTOC calls don't need to restore r2. */
15146 can_plt_call = TRUE;
15149 /* All of these stubs may modify r2, so there must be a
15150 branch and link followed by a nop. The nop is
15151 replaced by an insn to restore r2. */
15152 else if (rel->r_offset + 8 <= input_section->size)
15156 br = bfd_get_32 (input_bfd,
15157 contents + rel->r_offset);
15162 nop = bfd_get_32 (input_bfd,
15163 contents + rel->r_offset + 4);
15164 if (nop == LD_R2_0R1 + STK_TOC (htab))
15165 can_plt_call = TRUE;
15166 else if (nop == NOP
15167 || nop == CROR_151515
15168 || nop == CROR_313131)
15171 && (h == htab->tls_get_addr_fd
15172 || h == htab->tls_get_addr)
15173 && htab->params->tls_get_addr_opt)
15175 /* Special stub used, leave nop alone. */
15178 bfd_put_32 (input_bfd,
15179 LD_R2_0R1 + STK_TOC (htab),
15180 contents + rel->r_offset + 4);
15181 can_plt_call = TRUE;
15186 if (!can_plt_call && h != NULL)
15188 const char *name = h->elf.root.root.string;
15193 if (strncmp (name, "__libc_start_main", 17) == 0
15194 && (name[17] == 0 || name[17] == '@'))
15196 /* Allow crt1 branch to go via a toc adjusting
15197 stub. Other calls that never return could do
15198 the same, if we could detect such. */
15199 can_plt_call = TRUE;
15205 /* g++ as of 20130507 emits self-calls without a
15206 following nop. This is arguably wrong since we
15207 have conflicting information. On the one hand a
15208 global symbol and on the other a local call
15209 sequence, but don't error for this special case.
15210 It isn't possible to cheaply verify we have
15211 exactly such a call. Allow all calls to the same
15213 asection *code_sec = sec;
15215 if (get_opd_info (sec) != NULL)
15217 bfd_vma off = (relocation + addend
15218 - sec->output_section->vma
15219 - sec->output_offset);
15221 opd_entry_value (sec, off, &code_sec, NULL, FALSE);
15223 if (code_sec == input_section)
15224 can_plt_call = TRUE;
15229 if (stub_entry->stub_type >= ppc_stub_plt_call
15230 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15231 info->callbacks->einfo
15232 /* xgettext:c-format */
15233 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15234 "recompile with -fPIC\n"),
15235 input_bfd, input_section, rel->r_offset, sym_name);
15237 info->callbacks->einfo
15238 /* xgettext:c-format */
15239 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15240 "(-mcmodel=small toc adjust stub)\n"),
15241 input_bfd, input_section, rel->r_offset, sym_name);
15243 bfd_set_error (bfd_error_bad_value);
15248 && stub_entry->stub_type >= ppc_stub_plt_call
15249 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15250 unresolved_reloc = FALSE;
15253 if ((stub_entry == NULL
15254 || stub_entry->stub_type == ppc_stub_long_branch
15255 || stub_entry->stub_type == ppc_stub_plt_branch)
15256 && get_opd_info (sec) != NULL)
15258 /* The branch destination is the value of the opd entry. */
15259 bfd_vma off = (relocation + addend
15260 - sec->output_section->vma
15261 - sec->output_offset);
15262 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, FALSE);
15263 if (dest != (bfd_vma) -1)
15267 reloc_dest = DEST_OPD;
15271 /* If the branch is out of reach we ought to have a long
15273 from = (rel->r_offset
15274 + input_section->output_offset
15275 + input_section->output_section->vma);
15277 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15281 if (stub_entry != NULL
15282 && (stub_entry->stub_type == ppc_stub_long_branch
15283 || stub_entry->stub_type == ppc_stub_plt_branch)
15284 && (r_type == R_PPC64_ADDR14_BRTAKEN
15285 || r_type == R_PPC64_ADDR14_BRNTAKEN
15286 || (relocation + addend - from + max_br_offset
15287 < 2 * max_br_offset)))
15288 /* Don't use the stub if this branch is in range. */
15291 if (stub_entry != NULL
15292 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15293 || stub_entry->stub_type == ppc_stub_long_branch_both
15294 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15295 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15296 && (r_type != R_PPC64_REL24_NOTOC
15297 || ((fdh ? fdh->elf.other : sym->st_other)
15298 & STO_PPC64_LOCAL_MASK) == 1 << STO_PPC64_LOCAL_BIT)
15299 && (relocation + addend - from + max_br_offset
15300 < 2 * max_br_offset))
15303 if (stub_entry != NULL
15304 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15305 || stub_entry->stub_type == ppc_stub_long_branch_both
15306 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15307 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15308 && r_type == R_PPC64_REL24_NOTOC
15309 && (relocation + addend - from + max_br_offset
15310 < 2 * max_br_offset))
15313 if (stub_entry != NULL)
15315 /* Munge up the value and addend so that we call the stub
15316 rather than the procedure directly. */
15317 asection *stub_sec = stub_entry->group->stub_sec;
15319 if (stub_entry->stub_type == ppc_stub_save_res)
15320 relocation += (stub_sec->output_offset
15321 + stub_sec->output_section->vma
15322 + stub_sec->size - htab->sfpr->size
15323 - htab->sfpr->output_offset
15324 - htab->sfpr->output_section->vma);
15326 relocation = (stub_entry->stub_offset
15327 + stub_sec->output_offset
15328 + stub_sec->output_section->vma);
15330 reloc_dest = DEST_STUB;
15332 if (((stub_entry->stub_type == ppc_stub_plt_call
15333 && ALWAYS_EMIT_R2SAVE)
15334 || stub_entry->stub_type == ppc_stub_plt_call_r2save
15335 || stub_entry->stub_type == ppc_stub_plt_call_both)
15337 && (h == htab->tls_get_addr_fd
15338 || h == htab->tls_get_addr)
15339 && htab->params->tls_get_addr_opt)
15340 && rel + 1 < relend
15341 && rel[1].r_offset == rel->r_offset + 4
15342 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
15344 else if ((stub_entry->stub_type == ppc_stub_long_branch_both
15345 || stub_entry->stub_type == ppc_stub_plt_branch_both
15346 || stub_entry->stub_type == ppc_stub_plt_call_both)
15347 && r_type == R_PPC64_REL24_NOTOC)
15355 /* Set 'a' bit. This is 0b00010 in BO field for branch
15356 on CR(BI) insns (BO == 001at or 011at), and 0b01000
15357 for branch on CTR insns (BO == 1a00t or 1a01t). */
15358 if ((insn & (0x14 << 21)) == (0x04 << 21))
15359 insn |= 0x02 << 21;
15360 else if ((insn & (0x14 << 21)) == (0x10 << 21))
15361 insn |= 0x08 << 21;
15367 /* Invert 'y' bit if not the default. */
15368 if ((bfd_signed_vma) (relocation + addend - from) < 0)
15369 insn ^= 0x01 << 21;
15372 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15375 /* NOP out calls to undefined weak functions.
15376 We can thus call a weak function without first
15377 checking whether the function is defined. */
15379 && h->elf.root.type == bfd_link_hash_undefweak
15380 && h->elf.dynindx == -1
15381 && (r_type == R_PPC64_REL24
15382 || r_type == R_PPC64_REL24_NOTOC)
15386 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15392 /* Set `addend'. */
15394 save_unresolved_reloc = unresolved_reloc;
15398 /* xgettext:c-format */
15399 _bfd_error_handler (_("%pB: %s unsupported"),
15400 input_bfd, ppc64_elf_howto_table[r_type]->name);
15402 bfd_set_error (bfd_error_bad_value);
15408 case R_PPC64_TLSGD:
15409 case R_PPC64_TLSLD:
15410 case R_PPC64_TOCSAVE:
15411 case R_PPC64_GNU_VTINHERIT:
15412 case R_PPC64_GNU_VTENTRY:
15413 case R_PPC64_ENTRY:
15416 /* GOT16 relocations. Like an ADDR16 using the symbol's
15417 address in the GOT as relocation value instead of the
15418 symbol's value itself. Also, create a GOT entry for the
15419 symbol and put the symbol value there. */
15420 case R_PPC64_GOT_TLSGD16:
15421 case R_PPC64_GOT_TLSGD16_LO:
15422 case R_PPC64_GOT_TLSGD16_HI:
15423 case R_PPC64_GOT_TLSGD16_HA:
15424 tls_type = TLS_TLS | TLS_GD;
15427 case R_PPC64_GOT_TLSLD16:
15428 case R_PPC64_GOT_TLSLD16_LO:
15429 case R_PPC64_GOT_TLSLD16_HI:
15430 case R_PPC64_GOT_TLSLD16_HA:
15431 tls_type = TLS_TLS | TLS_LD;
15434 case R_PPC64_GOT_TPREL16_DS:
15435 case R_PPC64_GOT_TPREL16_LO_DS:
15436 case R_PPC64_GOT_TPREL16_HI:
15437 case R_PPC64_GOT_TPREL16_HA:
15438 tls_type = TLS_TLS | TLS_TPREL;
15441 case R_PPC64_GOT_DTPREL16_DS:
15442 case R_PPC64_GOT_DTPREL16_LO_DS:
15443 case R_PPC64_GOT_DTPREL16_HI:
15444 case R_PPC64_GOT_DTPREL16_HA:
15445 tls_type = TLS_TLS | TLS_DTPREL;
15448 case R_PPC64_GOT16:
15449 case R_PPC64_GOT16_LO:
15450 case R_PPC64_GOT16_HI:
15451 case R_PPC64_GOT16_HA:
15452 case R_PPC64_GOT16_DS:
15453 case R_PPC64_GOT16_LO_DS:
15456 /* Relocation is to the entry for this symbol in the global
15461 unsigned long indx = 0;
15462 struct got_entry *ent;
15464 if (tls_type == (TLS_TLS | TLS_LD)
15466 || !h->elf.def_dynamic))
15467 ent = ppc64_tlsld_got (input_bfd);
15472 if (!htab->elf.dynamic_sections_created
15473 || h->elf.dynindx == -1
15474 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15475 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
15476 /* This is actually a static link, or it is a
15477 -Bsymbolic link and the symbol is defined
15478 locally, or the symbol was forced to be local
15479 because of a version file. */
15483 indx = h->elf.dynindx;
15484 unresolved_reloc = FALSE;
15486 ent = h->elf.got.glist;
15490 if (local_got_ents == NULL)
15492 ent = local_got_ents[r_symndx];
15495 for (; ent != NULL; ent = ent->next)
15496 if (ent->addend == orig_rel.r_addend
15497 && ent->owner == input_bfd
15498 && ent->tls_type == tls_type)
15504 if (ent->is_indirect)
15505 ent = ent->got.ent;
15506 offp = &ent->got.offset;
15507 got = ppc64_elf_tdata (ent->owner)->got;
15511 /* The offset must always be a multiple of 8. We use the
15512 least significant bit to record whether we have already
15513 processed this entry. */
15515 if ((off & 1) != 0)
15519 /* Generate relocs for the dynamic linker, except in
15520 the case of TLSLD where we'll use one entry per
15528 ? h->elf.type == STT_GNU_IFUNC
15529 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
15532 relgot = htab->elf.irelplt;
15534 htab->local_ifunc_resolver = 1;
15535 else if (is_static_defined (&h->elf))
15536 htab->maybe_local_ifunc_resolver = 1;
15539 || (bfd_link_pic (info)
15541 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf)
15542 || (tls_type == (TLS_TLS | TLS_LD)
15543 && !h->elf.def_dynamic))
15544 && !(tls_type == (TLS_TLS | TLS_TPREL)
15545 && bfd_link_executable (info)
15546 && SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
15547 relgot = ppc64_elf_tdata (ent->owner)->relgot;
15548 if (relgot != NULL)
15550 outrel.r_offset = (got->output_section->vma
15551 + got->output_offset
15553 outrel.r_addend = addend;
15554 if (tls_type & (TLS_LD | TLS_GD))
15556 outrel.r_addend = 0;
15557 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
15558 if (tls_type == (TLS_TLS | TLS_GD))
15560 loc = relgot->contents;
15561 loc += (relgot->reloc_count++
15562 * sizeof (Elf64_External_Rela));
15563 bfd_elf64_swap_reloca_out (output_bfd,
15565 outrel.r_offset += 8;
15566 outrel.r_addend = addend;
15568 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15571 else if (tls_type == (TLS_TLS | TLS_DTPREL))
15572 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
15573 else if (tls_type == (TLS_TLS | TLS_TPREL))
15574 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
15575 else if (indx != 0)
15576 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
15580 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15582 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15584 /* Write the .got section contents for the sake
15586 loc = got->contents + off;
15587 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
15591 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
15593 outrel.r_addend += relocation;
15594 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
15596 if (htab->elf.tls_sec == NULL)
15597 outrel.r_addend = 0;
15599 outrel.r_addend -= htab->elf.tls_sec->vma;
15602 loc = relgot->contents;
15603 loc += (relgot->reloc_count++
15604 * sizeof (Elf64_External_Rela));
15605 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
15608 /* Init the .got section contents here if we're not
15609 emitting a reloc. */
15612 relocation += addend;
15615 if (htab->elf.tls_sec == NULL)
15619 if (tls_type & TLS_LD)
15622 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
15623 if (tls_type & TLS_TPREL)
15624 relocation += DTP_OFFSET - TP_OFFSET;
15627 if (tls_type & (TLS_GD | TLS_LD))
15629 bfd_put_64 (output_bfd, relocation,
15630 got->contents + off + 8);
15634 bfd_put_64 (output_bfd, relocation,
15635 got->contents + off);
15639 if (off >= (bfd_vma) -2)
15642 relocation = got->output_section->vma + got->output_offset + off;
15643 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
15647 case R_PPC64_PLT16_HA:
15648 case R_PPC64_PLT16_HI:
15649 case R_PPC64_PLT16_LO:
15650 case R_PPC64_PLT16_LO_DS:
15651 case R_PPC64_PLT32:
15652 case R_PPC64_PLT64:
15653 case R_PPC64_PLTSEQ:
15654 case R_PPC64_PLTCALL:
15655 /* Relocation is to the entry for this symbol in the
15656 procedure linkage table. */
15657 unresolved_reloc = TRUE;
15659 struct plt_entry **plt_list = NULL;
15661 plt_list = &h->elf.plt.plist;
15662 else if (local_got_ents != NULL)
15664 struct plt_entry **local_plt = (struct plt_entry **)
15665 (local_got_ents + symtab_hdr->sh_info);
15666 plt_list = local_plt + r_symndx;
15670 struct plt_entry *ent;
15672 for (ent = *plt_list; ent != NULL; ent = ent->next)
15673 if (ent->plt.offset != (bfd_vma) -1
15674 && ent->addend == orig_rel.r_addend)
15679 plt = htab->elf.splt;
15680 if (!htab->elf.dynamic_sections_created
15682 || h->elf.dynindx == -1)
15685 ? h->elf.type == STT_GNU_IFUNC
15686 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15687 plt = htab->elf.iplt;
15689 plt = htab->pltlocal;
15691 relocation = (plt->output_section->vma
15692 + plt->output_offset
15693 + ent->plt.offset);
15694 if (r_type == R_PPC64_PLT16_HA
15695 || r_type ==R_PPC64_PLT16_HI
15696 || r_type ==R_PPC64_PLT16_LO
15697 || r_type ==R_PPC64_PLT16_LO_DS)
15699 got = (elf_gp (output_bfd)
15700 + htab->sec_info[input_section->id].toc_off);
15704 unresolved_reloc = FALSE;
15712 /* Relocation value is TOC base. */
15713 relocation = TOCstart;
15714 if (r_symndx == STN_UNDEF)
15715 relocation += htab->sec_info[input_section->id].toc_off;
15716 else if (unresolved_reloc)
15718 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
15719 relocation += htab->sec_info[sec->id].toc_off;
15721 unresolved_reloc = TRUE;
15724 /* TOC16 relocs. We want the offset relative to the TOC base,
15725 which is the address of the start of the TOC plus 0x8000.
15726 The TOC consists of sections .got, .toc, .tocbss, and .plt,
15728 case R_PPC64_TOC16:
15729 case R_PPC64_TOC16_LO:
15730 case R_PPC64_TOC16_HI:
15731 case R_PPC64_TOC16_DS:
15732 case R_PPC64_TOC16_LO_DS:
15733 case R_PPC64_TOC16_HA:
15734 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
15737 /* Relocate against the beginning of the section. */
15738 case R_PPC64_SECTOFF:
15739 case R_PPC64_SECTOFF_LO:
15740 case R_PPC64_SECTOFF_HI:
15741 case R_PPC64_SECTOFF_DS:
15742 case R_PPC64_SECTOFF_LO_DS:
15743 case R_PPC64_SECTOFF_HA:
15745 addend -= sec->output_section->vma;
15748 case R_PPC64_REL16:
15749 case R_PPC64_REL16_LO:
15750 case R_PPC64_REL16_HI:
15751 case R_PPC64_REL16_HA:
15752 case R_PPC64_REL16DX_HA:
15755 case R_PPC64_REL14:
15756 case R_PPC64_REL14_BRNTAKEN:
15757 case R_PPC64_REL14_BRTAKEN:
15758 case R_PPC64_REL24:
15759 case R_PPC64_REL24_NOTOC:
15762 case R_PPC64_TPREL16:
15763 case R_PPC64_TPREL16_LO:
15764 case R_PPC64_TPREL16_HI:
15765 case R_PPC64_TPREL16_HA:
15766 case R_PPC64_TPREL16_DS:
15767 case R_PPC64_TPREL16_LO_DS:
15768 case R_PPC64_TPREL16_HIGH:
15769 case R_PPC64_TPREL16_HIGHA:
15770 case R_PPC64_TPREL16_HIGHER:
15771 case R_PPC64_TPREL16_HIGHERA:
15772 case R_PPC64_TPREL16_HIGHEST:
15773 case R_PPC64_TPREL16_HIGHESTA:
15775 && h->elf.root.type == bfd_link_hash_undefweak
15776 && h->elf.dynindx == -1)
15778 /* Make this relocation against an undefined weak symbol
15779 resolve to zero. This is really just a tweak, since
15780 code using weak externs ought to check that they are
15781 defined before using them. */
15782 bfd_byte *p = contents + rel->r_offset - d_offset;
15784 insn = bfd_get_32 (input_bfd, p);
15785 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
15787 bfd_put_32 (input_bfd, insn, p);
15790 if (htab->elf.tls_sec != NULL)
15791 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15792 /* The TPREL16 relocs shouldn't really be used in shared
15793 libs or with non-local symbols as that will result in
15794 DT_TEXTREL being set, but support them anyway. */
15797 case R_PPC64_DTPREL16:
15798 case R_PPC64_DTPREL16_LO:
15799 case R_PPC64_DTPREL16_HI:
15800 case R_PPC64_DTPREL16_HA:
15801 case R_PPC64_DTPREL16_DS:
15802 case R_PPC64_DTPREL16_LO_DS:
15803 case R_PPC64_DTPREL16_HIGH:
15804 case R_PPC64_DTPREL16_HIGHA:
15805 case R_PPC64_DTPREL16_HIGHER:
15806 case R_PPC64_DTPREL16_HIGHERA:
15807 case R_PPC64_DTPREL16_HIGHEST:
15808 case R_PPC64_DTPREL16_HIGHESTA:
15809 if (htab->elf.tls_sec != NULL)
15810 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15813 case R_PPC64_ADDR64_LOCAL:
15814 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
15819 case R_PPC64_DTPMOD64:
15824 case R_PPC64_TPREL64:
15825 if (htab->elf.tls_sec != NULL)
15826 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
15829 case R_PPC64_DTPREL64:
15830 if (htab->elf.tls_sec != NULL)
15831 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
15832 /* Fall through. */
15834 /* Relocations that may need to be propagated if this is a
15836 case R_PPC64_REL30:
15837 case R_PPC64_REL32:
15838 case R_PPC64_REL64:
15839 case R_PPC64_ADDR14:
15840 case R_PPC64_ADDR14_BRNTAKEN:
15841 case R_PPC64_ADDR14_BRTAKEN:
15842 case R_PPC64_ADDR16:
15843 case R_PPC64_ADDR16_DS:
15844 case R_PPC64_ADDR16_HA:
15845 case R_PPC64_ADDR16_HI:
15846 case R_PPC64_ADDR16_HIGH:
15847 case R_PPC64_ADDR16_HIGHA:
15848 case R_PPC64_ADDR16_HIGHER:
15849 case R_PPC64_ADDR16_HIGHERA:
15850 case R_PPC64_ADDR16_HIGHEST:
15851 case R_PPC64_ADDR16_HIGHESTA:
15852 case R_PPC64_ADDR16_LO:
15853 case R_PPC64_ADDR16_LO_DS:
15854 case R_PPC64_ADDR24:
15855 case R_PPC64_ADDR32:
15856 case R_PPC64_ADDR64:
15857 case R_PPC64_UADDR16:
15858 case R_PPC64_UADDR32:
15859 case R_PPC64_UADDR64:
15861 if ((input_section->flags & SEC_ALLOC) == 0)
15864 if (NO_OPD_RELOCS && is_opd)
15867 if (bfd_link_pic (info)
15869 || h->dyn_relocs != NULL)
15870 && ((h != NULL && pc_dynrelocs (h))
15871 || must_be_dyn_reloc (info, r_type)))
15873 ? h->dyn_relocs != NULL
15874 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15876 bfd_boolean skip, relocate;
15881 /* When generating a dynamic object, these relocations
15882 are copied into the output file to be resolved at run
15888 out_off = _bfd_elf_section_offset (output_bfd, info,
15889 input_section, rel->r_offset);
15890 if (out_off == (bfd_vma) -1)
15892 else if (out_off == (bfd_vma) -2)
15893 skip = TRUE, relocate = TRUE;
15894 out_off += (input_section->output_section->vma
15895 + input_section->output_offset);
15896 outrel.r_offset = out_off;
15897 outrel.r_addend = rel->r_addend;
15899 /* Optimize unaligned reloc use. */
15900 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
15901 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
15902 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
15903 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
15904 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
15905 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
15906 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
15907 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
15908 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
15911 memset (&outrel, 0, sizeof outrel);
15912 else if (!SYMBOL_REFERENCES_LOCAL (info, &h->elf)
15914 && r_type != R_PPC64_TOC)
15916 indx = h->elf.dynindx;
15917 BFD_ASSERT (indx != -1);
15918 outrel.r_info = ELF64_R_INFO (indx, r_type);
15922 /* This symbol is local, or marked to become local,
15923 or this is an opd section reloc which must point
15924 at a local function. */
15925 outrel.r_addend += relocation;
15926 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
15928 if (is_opd && h != NULL)
15930 /* Lie about opd entries. This case occurs
15931 when building shared libraries and we
15932 reference a function in another shared
15933 lib. The same thing happens for a weak
15934 definition in an application that's
15935 overridden by a strong definition in a
15936 shared lib. (I believe this is a generic
15937 bug in binutils handling of weak syms.)
15938 In these cases we won't use the opd
15939 entry in this lib. */
15940 unresolved_reloc = FALSE;
15943 && r_type == R_PPC64_ADDR64
15945 ? h->elf.type == STT_GNU_IFUNC
15946 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
15947 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
15950 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
15952 /* We need to relocate .opd contents for ld.so.
15953 Prelink also wants simple and consistent rules
15954 for relocs. This make all RELATIVE relocs have
15955 *r_offset equal to r_addend. */
15962 ? h->elf.type == STT_GNU_IFUNC
15963 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
15965 info->callbacks->einfo
15966 /* xgettext:c-format */
15967 (_("%H: %s for indirect "
15968 "function `%pT' unsupported\n"),
15969 input_bfd, input_section, rel->r_offset,
15970 ppc64_elf_howto_table[r_type]->name,
15974 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
15976 else if (sec == NULL || sec->owner == NULL)
15978 bfd_set_error (bfd_error_bad_value);
15985 osec = sec->output_section;
15986 indx = elf_section_data (osec)->dynindx;
15990 if ((osec->flags & SEC_READONLY) == 0
15991 && htab->elf.data_index_section != NULL)
15992 osec = htab->elf.data_index_section;
15994 osec = htab->elf.text_index_section;
15995 indx = elf_section_data (osec)->dynindx;
15997 BFD_ASSERT (indx != 0);
15999 /* We are turning this relocation into one
16000 against a section symbol, so subtract out
16001 the output section's address but not the
16002 offset of the input section in the output
16004 outrel.r_addend -= osec->vma;
16007 outrel.r_info = ELF64_R_INFO (indx, r_type);
16011 sreloc = elf_section_data (input_section)->sreloc;
16013 ? h->elf.type == STT_GNU_IFUNC
16014 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16016 sreloc = htab->elf.irelplt;
16018 htab->local_ifunc_resolver = 1;
16019 else if (is_static_defined (&h->elf))
16020 htab->maybe_local_ifunc_resolver = 1;
16022 if (sreloc == NULL)
16025 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16028 loc = sreloc->contents;
16029 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16030 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16032 /* If this reloc is against an external symbol, it will
16033 be computed at runtime, so there's no need to do
16034 anything now. However, for the sake of prelink ensure
16035 that the section contents are a known value. */
16038 unresolved_reloc = FALSE;
16039 /* The value chosen here is quite arbitrary as ld.so
16040 ignores section contents except for the special
16041 case of .opd where the contents might be accessed
16042 before relocation. Choose zero, as that won't
16043 cause reloc overflow. */
16046 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16047 to improve backward compatibility with older
16049 if (r_type == R_PPC64_ADDR64)
16050 addend = outrel.r_addend;
16051 /* Adjust pc_relative relocs to have zero in *r_offset. */
16052 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16053 addend = outrel.r_offset;
16059 case R_PPC64_GLOB_DAT:
16060 case R_PPC64_JMP_SLOT:
16061 case R_PPC64_JMP_IREL:
16062 case R_PPC64_RELATIVE:
16063 /* We shouldn't ever see these dynamic relocs in relocatable
16065 /* Fall through. */
16067 case R_PPC64_PLTGOT16:
16068 case R_PPC64_PLTGOT16_DS:
16069 case R_PPC64_PLTGOT16_HA:
16070 case R_PPC64_PLTGOT16_HI:
16071 case R_PPC64_PLTGOT16_LO:
16072 case R_PPC64_PLTGOT16_LO_DS:
16073 case R_PPC64_PLTREL32:
16074 case R_PPC64_PLTREL64:
16075 /* These ones haven't been implemented yet. */
16077 info->callbacks->einfo
16078 /* xgettext:c-format */
16079 (_("%P: %pB: %s is not supported for `%pT'\n"),
16081 ppc64_elf_howto_table[r_type]->name, sym_name);
16083 bfd_set_error (bfd_error_invalid_operation);
16088 /* Multi-instruction sequences that access the TOC can be
16089 optimized, eg. addis ra,r2,0; addi rb,ra,x;
16090 to nop; addi rb,r2,x; */
16096 case R_PPC64_GOT_TLSLD16_HI:
16097 case R_PPC64_GOT_TLSGD16_HI:
16098 case R_PPC64_GOT_TPREL16_HI:
16099 case R_PPC64_GOT_DTPREL16_HI:
16100 case R_PPC64_GOT16_HI:
16101 case R_PPC64_TOC16_HI:
16102 /* These relocs would only be useful if building up an
16103 offset to later add to r2, perhaps in an indexed
16104 addressing mode instruction. Don't try to optimize.
16105 Unfortunately, the possibility of someone building up an
16106 offset like this or even with the HA relocs, means that
16107 we need to check the high insn when optimizing the low
16111 case R_PPC64_PLTCALL:
16112 if (unresolved_reloc)
16114 /* No plt entry. Make this into a direct call. */
16115 bfd_byte *p = contents + rel->r_offset;
16116 insn = bfd_get_32 (input_bfd, p);
16118 bfd_put_32 (input_bfd, B_DOT | insn, p);
16119 bfd_put_32 (input_bfd, NOP, p + 4);
16120 unresolved_reloc = save_unresolved_reloc;
16121 r_type = R_PPC64_REL24;
16125 case R_PPC64_PLTSEQ:
16126 if (unresolved_reloc)
16128 unresolved_reloc = FALSE;
16133 case R_PPC64_PLT16_HA:
16134 if (unresolved_reloc)
16136 unresolved_reloc = FALSE;
16139 /* Fall through. */
16140 case R_PPC64_GOT_TLSLD16_HA:
16141 case R_PPC64_GOT_TLSGD16_HA:
16142 case R_PPC64_GOT_TPREL16_HA:
16143 case R_PPC64_GOT_DTPREL16_HA:
16144 case R_PPC64_GOT16_HA:
16145 case R_PPC64_TOC16_HA:
16146 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16147 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16151 p = contents + (rel->r_offset & ~3);
16152 bfd_put_32 (input_bfd, NOP, p);
16157 case R_PPC64_PLT16_LO:
16158 case R_PPC64_PLT16_LO_DS:
16159 if (unresolved_reloc)
16161 unresolved_reloc = FALSE;
16164 /* Fall through. */
16165 case R_PPC64_GOT_TLSLD16_LO:
16166 case R_PPC64_GOT_TLSGD16_LO:
16167 case R_PPC64_GOT_TPREL16_LO_DS:
16168 case R_PPC64_GOT_DTPREL16_LO_DS:
16169 case R_PPC64_GOT16_LO:
16170 case R_PPC64_GOT16_LO_DS:
16171 case R_PPC64_TOC16_LO:
16172 case R_PPC64_TOC16_LO_DS:
16173 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
16174 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
16176 bfd_byte *p = contents + (rel->r_offset & ~3);
16177 insn = bfd_get_32 (input_bfd, p);
16178 if ((insn & (0x3f << 26)) == 12u << 26 /* addic */)
16180 /* Transform addic to addi when we change reg. */
16181 insn &= ~((0x3f << 26) | (0x1f << 16));
16182 insn |= (14u << 26) | (2 << 16);
16186 insn &= ~(0x1f << 16);
16189 bfd_put_32 (input_bfd, insn, p);
16193 case R_PPC64_TPREL16_HA:
16194 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16196 bfd_byte *p = contents + (rel->r_offset & ~3);
16197 insn = bfd_get_32 (input_bfd, p);
16198 if ((insn & ((0x3f << 26) | 0x1f << 16))
16199 != ((15u << 26) | (13 << 16)) /* addis rt,13,imm */)
16200 /* xgettext:c-format */
16201 info->callbacks->minfo
16202 (_("%H: warning: %s unexpected insn %#x.\n"),
16203 input_bfd, input_section, rel->r_offset,
16204 ppc64_elf_howto_table[r_type]->name, insn);
16207 bfd_put_32 (input_bfd, NOP, p);
16213 case R_PPC64_TPREL16_LO:
16214 case R_PPC64_TPREL16_LO_DS:
16215 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
16217 bfd_byte *p = contents + (rel->r_offset & ~3);
16218 insn = bfd_get_32 (input_bfd, p);
16219 insn &= ~(0x1f << 16);
16221 bfd_put_32 (input_bfd, insn, p);
16226 /* Do any further special processing. */
16232 case R_PPC64_REL16_HA:
16233 case R_PPC64_REL16DX_HA:
16234 case R_PPC64_ADDR16_HA:
16235 case R_PPC64_ADDR16_HIGHA:
16236 case R_PPC64_ADDR16_HIGHERA:
16237 case R_PPC64_ADDR16_HIGHESTA:
16238 case R_PPC64_TOC16_HA:
16239 case R_PPC64_SECTOFF_HA:
16240 case R_PPC64_TPREL16_HA:
16241 case R_PPC64_TPREL16_HIGHA:
16242 case R_PPC64_TPREL16_HIGHERA:
16243 case R_PPC64_TPREL16_HIGHESTA:
16244 case R_PPC64_DTPREL16_HA:
16245 case R_PPC64_DTPREL16_HIGHA:
16246 case R_PPC64_DTPREL16_HIGHERA:
16247 case R_PPC64_DTPREL16_HIGHESTA:
16248 /* It's just possible that this symbol is a weak symbol
16249 that's not actually defined anywhere. In that case,
16250 'sec' would be NULL, and we should leave the symbol
16251 alone (it will be set to zero elsewhere in the link). */
16254 /* Fall through. */
16256 case R_PPC64_GOT16_HA:
16257 case R_PPC64_PLTGOT16_HA:
16258 case R_PPC64_PLT16_HA:
16259 case R_PPC64_GOT_TLSGD16_HA:
16260 case R_PPC64_GOT_TLSLD16_HA:
16261 case R_PPC64_GOT_TPREL16_HA:
16262 case R_PPC64_GOT_DTPREL16_HA:
16263 /* Add 0x10000 if sign bit in 0:15 is set.
16264 Bits 0:15 are not used. */
16268 case R_PPC64_ADDR16_DS:
16269 case R_PPC64_ADDR16_LO_DS:
16270 case R_PPC64_GOT16_DS:
16271 case R_PPC64_GOT16_LO_DS:
16272 case R_PPC64_PLT16_LO_DS:
16273 case R_PPC64_SECTOFF_DS:
16274 case R_PPC64_SECTOFF_LO_DS:
16275 case R_PPC64_TOC16_DS:
16276 case R_PPC64_TOC16_LO_DS:
16277 case R_PPC64_PLTGOT16_DS:
16278 case R_PPC64_PLTGOT16_LO_DS:
16279 case R_PPC64_GOT_TPREL16_DS:
16280 case R_PPC64_GOT_TPREL16_LO_DS:
16281 case R_PPC64_GOT_DTPREL16_DS:
16282 case R_PPC64_GOT_DTPREL16_LO_DS:
16283 case R_PPC64_TPREL16_DS:
16284 case R_PPC64_TPREL16_LO_DS:
16285 case R_PPC64_DTPREL16_DS:
16286 case R_PPC64_DTPREL16_LO_DS:
16287 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16289 /* If this reloc is against an lq, lxv, or stxv insn, then
16290 the value must be a multiple of 16. This is somewhat of
16291 a hack, but the "correct" way to do this by defining _DQ
16292 forms of all the _DS relocs bloats all reloc switches in
16293 this file. It doesn't make much sense to use these
16294 relocs in data, so testing the insn should be safe. */
16295 if ((insn & (0x3f << 26)) == (56u << 26)
16296 || ((insn & (0x3f << 26)) == (61u << 26) && (insn & 3) == 1))
16298 relocation += addend;
16299 addend = insn & (mask ^ 3);
16300 if ((relocation & mask) != 0)
16302 relocation ^= relocation & mask;
16303 info->callbacks->einfo
16304 /* xgettext:c-format */
16305 (_("%H: error: %s not a multiple of %u\n"),
16306 input_bfd, input_section, rel->r_offset,
16307 ppc64_elf_howto_table[r_type]->name,
16309 bfd_set_error (bfd_error_bad_value);
16316 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
16317 because such sections are not SEC_ALLOC and thus ld.so will
16318 not process them. */
16319 howto = ppc64_elf_howto_table[(int) r_type];
16320 if (unresolved_reloc
16321 && !((input_section->flags & SEC_DEBUGGING) != 0
16322 && h->elf.def_dynamic)
16323 && _bfd_elf_section_offset (output_bfd, info, input_section,
16324 rel->r_offset) != (bfd_vma) -1)
16326 info->callbacks->einfo
16327 /* xgettext:c-format */
16328 (_("%H: unresolvable %s against `%pT'\n"),
16329 input_bfd, input_section, rel->r_offset,
16331 h->elf.root.root.string);
16335 /* 16-bit fields in insns mostly have signed values, but a
16336 few insns have 16-bit unsigned values. Really, we should
16337 have different reloc types. */
16338 if (howto->complain_on_overflow != complain_overflow_dont
16339 && howto->dst_mask == 0xffff
16340 && (input_section->flags & SEC_CODE) != 0)
16342 enum complain_overflow complain = complain_overflow_signed;
16344 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16345 if ((insn & (0x3f << 26)) == 10u << 26 /* cmpli */)
16346 complain = complain_overflow_bitfield;
16347 else if (howto->rightshift == 0
16348 ? ((insn & (0x3f << 26)) == 28u << 26 /* andi */
16349 || (insn & (0x3f << 26)) == 24u << 26 /* ori */
16350 || (insn & (0x3f << 26)) == 26u << 26 /* xori */)
16351 : ((insn & (0x3f << 26)) == 29u << 26 /* andis */
16352 || (insn & (0x3f << 26)) == 25u << 26 /* oris */
16353 || (insn & (0x3f << 26)) == 27u << 26 /* xoris */))
16354 complain = complain_overflow_unsigned;
16355 if (howto->complain_on_overflow != complain)
16357 alt_howto = *howto;
16358 alt_howto.complain_on_overflow = complain;
16359 howto = &alt_howto;
16363 if (r_type == R_PPC64_REL16DX_HA)
16365 /* Split field reloc isn't handled by _bfd_final_link_relocate. */
16366 if (rel->r_offset + 4 > input_section->size)
16367 r = bfd_reloc_outofrange;
16370 relocation += addend;
16371 relocation -= (rel->r_offset
16372 + input_section->output_offset
16373 + input_section->output_section->vma);
16374 relocation = (bfd_signed_vma) relocation >> 16;
16375 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16377 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
16378 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16380 if (relocation + 0x8000 > 0xffff)
16381 r = bfd_reloc_overflow;
16385 r = _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
16386 rel->r_offset, relocation, addend);
16388 if (r != bfd_reloc_ok)
16390 char *more_info = NULL;
16391 const char *reloc_name = howto->name;
16393 if (reloc_dest != DEST_NORMAL)
16395 more_info = bfd_malloc (strlen (reloc_name) + 8);
16396 if (more_info != NULL)
16398 strcpy (more_info, reloc_name);
16399 strcat (more_info, (reloc_dest == DEST_OPD
16400 ? " (OPD)" : " (stub)"));
16401 reloc_name = more_info;
16405 if (r == bfd_reloc_overflow)
16407 /* On code like "if (foo) foo();" don't report overflow
16408 on a branch to zero when foo is undefined. */
16410 && (reloc_dest == DEST_STUB
16412 && (h->elf.root.type == bfd_link_hash_undefweak
16413 || h->elf.root.type == bfd_link_hash_undefined)
16414 && is_branch_reloc (r_type))))
16415 info->callbacks->reloc_overflow (info, &h->elf.root,
16416 sym_name, reloc_name,
16418 input_bfd, input_section,
16423 info->callbacks->einfo
16424 /* xgettext:c-format */
16425 (_("%H: %s against `%pT': error %d\n"),
16426 input_bfd, input_section, rel->r_offset,
16427 reloc_name, sym_name, (int) r);
16430 if (more_info != NULL)
16440 Elf_Internal_Shdr *rel_hdr;
16441 size_t deleted = rel - wrel;
16443 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
16444 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16445 if (rel_hdr->sh_size == 0)
16447 /* It is too late to remove an empty reloc section. Leave
16449 ??? What is wrong with an empty section??? */
16450 rel_hdr->sh_size = rel_hdr->sh_entsize;
16453 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
16454 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
16455 input_section->reloc_count -= deleted;
16458 /* If we're emitting relocations, then shortly after this function
16459 returns, reloc offsets and addends for this section will be
16460 adjusted. Worse, reloc symbol indices will be for the output
16461 file rather than the input. Save a copy of the relocs for
16462 opd_entry_value. */
16463 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
16466 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
16467 rel = bfd_alloc (input_bfd, amt);
16468 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
16469 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
16472 memcpy (rel, relocs, amt);
16477 /* Adjust the value of any local symbols in opd sections. */
16480 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
16481 const char *name ATTRIBUTE_UNUSED,
16482 Elf_Internal_Sym *elfsym,
16483 asection *input_sec,
16484 struct elf_link_hash_entry *h)
16486 struct _opd_sec_data *opd;
16493 opd = get_opd_info (input_sec);
16494 if (opd == NULL || opd->adjust == NULL)
16497 value = elfsym->st_value - input_sec->output_offset;
16498 if (!bfd_link_relocatable (info))
16499 value -= input_sec->output_section->vma;
16501 adjust = opd->adjust[OPD_NDX (value)];
16505 elfsym->st_value += adjust;
16509 /* Finish up dynamic symbol handling. We set the contents of various
16510 dynamic sections here. */
16513 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
16514 struct bfd_link_info *info,
16515 struct elf_link_hash_entry *h,
16516 Elf_Internal_Sym *sym)
16518 struct ppc_link_hash_table *htab;
16519 struct plt_entry *ent;
16521 htab = ppc_hash_table (info);
16525 if (!htab->opd_abi && !h->def_regular)
16526 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
16527 if (ent->plt.offset != (bfd_vma) -1)
16529 /* Mark the symbol as undefined, rather than as
16530 defined in glink. Leave the value if there were
16531 any relocations where pointer equality matters
16532 (this is a clue for the dynamic linker, to make
16533 function pointer comparisons work between an
16534 application and shared library), otherwise set it
16536 sym->st_shndx = SHN_UNDEF;
16537 if (!h->pointer_equality_needed)
16539 else if (!h->ref_regular_nonweak)
16541 /* This breaks function pointer comparisons, but
16542 that is better than breaking tests for a NULL
16543 function pointer. */
16551 /* This symbol needs a copy reloc. Set it up. */
16552 Elf_Internal_Rela rela;
16556 if (h->dynindx == -1
16557 || (h->root.type != bfd_link_hash_defined
16558 && h->root.type != bfd_link_hash_defweak)
16559 || htab->elf.srelbss == NULL
16560 || htab->elf.sreldynrelro == NULL)
16563 rela.r_offset = (h->root.u.def.value
16564 + h->root.u.def.section->output_section->vma
16565 + h->root.u.def.section->output_offset);
16566 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
16568 if (h->root.u.def.section == htab->elf.sdynrelro)
16569 srel = htab->elf.sreldynrelro;
16571 srel = htab->elf.srelbss;
16572 loc = srel->contents;
16573 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
16574 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
16580 /* Used to decide how to sort relocs in an optimal manner for the
16581 dynamic linker, before writing them out. */
16583 static enum elf_reloc_type_class
16584 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
16585 const asection *rel_sec,
16586 const Elf_Internal_Rela *rela)
16588 enum elf_ppc64_reloc_type r_type;
16589 struct ppc_link_hash_table *htab = ppc_hash_table (info);
16591 if (rel_sec == htab->elf.irelplt)
16592 return reloc_class_ifunc;
16594 r_type = ELF64_R_TYPE (rela->r_info);
16597 case R_PPC64_RELATIVE:
16598 return reloc_class_relative;
16599 case R_PPC64_JMP_SLOT:
16600 return reloc_class_plt;
16602 return reloc_class_copy;
16604 return reloc_class_normal;
16608 /* Finish up the dynamic sections. */
16611 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
16612 struct bfd_link_info *info)
16614 struct ppc_link_hash_table *htab;
16618 htab = ppc_hash_table (info);
16622 dynobj = htab->elf.dynobj;
16623 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
16625 if (htab->elf.dynamic_sections_created)
16627 Elf64_External_Dyn *dyncon, *dynconend;
16629 if (sdyn == NULL || htab->elf.sgot == NULL)
16632 dyncon = (Elf64_External_Dyn *) sdyn->contents;
16633 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
16634 for (; dyncon < dynconend; dyncon++)
16636 Elf_Internal_Dyn dyn;
16639 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
16646 case DT_PPC64_GLINK:
16648 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16649 /* We stupidly defined DT_PPC64_GLINK to be the start
16650 of glink rather than the first entry point, which is
16651 what ld.so needs, and now have a bigger stub to
16652 support automatic multiple TOCs. */
16653 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
16657 s = bfd_get_section_by_name (output_bfd, ".opd");
16660 dyn.d_un.d_ptr = s->vma;
16664 if (htab->do_multi_toc && htab->multi_toc_needed)
16665 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
16666 if (htab->has_plt_localentry0)
16667 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
16670 case DT_PPC64_OPDSZ:
16671 s = bfd_get_section_by_name (output_bfd, ".opd");
16674 dyn.d_un.d_val = s->size;
16678 s = htab->elf.splt;
16679 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16683 s = htab->elf.srelplt;
16684 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
16688 dyn.d_un.d_val = htab->elf.srelplt->size;
16692 if (htab->local_ifunc_resolver)
16693 info->callbacks->einfo
16694 (_("%X%P: text relocations and GNU indirect "
16695 "functions will result in a segfault at runtime\n"));
16696 else if (htab->maybe_local_ifunc_resolver)
16697 info->callbacks->einfo
16698 (_("%P: warning: text relocations and GNU indirect "
16699 "functions may result in a segfault at runtime\n"));
16703 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
16707 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
16708 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
16710 /* Fill in the first entry in the global offset table.
16711 We use it to hold the link-time TOCbase. */
16712 bfd_put_64 (output_bfd,
16713 elf_gp (output_bfd) + TOC_BASE_OFF,
16714 htab->elf.sgot->contents);
16716 /* Set .got entry size. */
16717 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize = 8;
16720 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
16721 && htab->elf.splt->output_section != bfd_abs_section_ptr)
16723 /* Set .plt entry size. */
16724 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
16725 = PLT_ENTRY_SIZE (htab);
16728 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
16729 brlt ourselves if emitrelocations. */
16730 if (htab->brlt != NULL
16731 && htab->brlt->reloc_count != 0
16732 && !_bfd_elf_link_output_relocs (output_bfd,
16734 elf_section_data (htab->brlt)->rela.hdr,
16735 elf_section_data (htab->brlt)->relocs,
16739 if (htab->glink != NULL
16740 && htab->glink->reloc_count != 0
16741 && !_bfd_elf_link_output_relocs (output_bfd,
16743 elf_section_data (htab->glink)->rela.hdr,
16744 elf_section_data (htab->glink)->relocs,
16748 if (htab->glink_eh_frame != NULL
16749 && htab->glink_eh_frame->size != 0)
16753 struct map_stub *group;
16756 p = htab->glink_eh_frame->contents;
16757 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
16759 for (group = htab->group; group != NULL; group = group->next)
16760 if (group->stub_sec != NULL)
16762 /* Offset to stub section. */
16763 val = (group->stub_sec->output_section->vma
16764 + group->stub_sec->output_offset);
16765 val -= (htab->glink_eh_frame->output_section->vma
16766 + htab->glink_eh_frame->output_offset
16767 + (p + 8 - htab->glink_eh_frame->contents));
16768 if (val + 0x80000000 > 0xffffffff)
16771 (_("%s offset too large for .eh_frame sdata4 encoding"),
16772 group->stub_sec->name);
16775 bfd_put_32 (dynobj, val, p + 8);
16776 p += stub_eh_frame_size (group, align);
16778 if (htab->glink != NULL && htab->glink->size != 0)
16780 /* Offset to .glink. */
16781 val = (htab->glink->output_section->vma
16782 + htab->glink->output_offset
16784 val -= (htab->glink_eh_frame->output_section->vma
16785 + htab->glink_eh_frame->output_offset
16786 + (p + 8 - htab->glink_eh_frame->contents));
16787 if (val + 0x80000000 > 0xffffffff)
16790 (_("%s offset too large for .eh_frame sdata4 encoding"),
16791 htab->glink->name);
16794 bfd_put_32 (dynobj, val, p + 8);
16795 p += (24 + align - 1) & -align;
16798 if (htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
16799 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
16800 htab->glink_eh_frame,
16801 htab->glink_eh_frame->contents))
16805 /* We need to handle writing out multiple GOT sections ourselves,
16806 since we didn't add them to DYNOBJ. We know dynobj is the first
16808 while ((dynobj = dynobj->link.next) != NULL)
16812 if (!is_ppc64_elf (dynobj))
16815 s = ppc64_elf_tdata (dynobj)->got;
16818 && s->output_section != bfd_abs_section_ptr
16819 && !bfd_set_section_contents (output_bfd, s->output_section,
16820 s->contents, s->output_offset,
16823 s = ppc64_elf_tdata (dynobj)->relgot;
16826 && s->output_section != bfd_abs_section_ptr
16827 && !bfd_set_section_contents (output_bfd, s->output_section,
16828 s->contents, s->output_offset,
16836 #include "elf64-target.h"
16838 /* FreeBSD support */
16840 #undef TARGET_LITTLE_SYM
16841 #undef TARGET_LITTLE_NAME
16843 #undef TARGET_BIG_SYM
16844 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
16845 #undef TARGET_BIG_NAME
16846 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
16849 #define ELF_OSABI ELFOSABI_FREEBSD
16852 #define elf64_bed elf64_powerpc_fbsd_bed
16854 #include "elf64-target.h"