Implement completion for Ada attributes
[binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2022 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.
6
7 This file is part of BFD, the Binary File Descriptor library.
8
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.
13
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.
18
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. */
22
23
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 */
27
28 /* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
32
33 #include "sysdep.h"
34 #include <stdarg.h>
35 #include "bfd.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #include "elf-bfd.h"
39 #include "elf/ppc64.h"
40 #include "elf64-ppc.h"
41 #include "dwarf2.h"
42
43 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44 #define OCTETS_PER_BYTE(ABFD, SEC) 1
45
46 static bfd_reloc_status_type ppc64_elf_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_toc_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type ppc64_elf_toc64_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_vma opd_entry_value
67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
68
69 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
70 #define TARGET_LITTLE_NAME "elf64-powerpcle"
71 #define TARGET_BIG_SYM powerpc_elf64_vec
72 #define TARGET_BIG_NAME "elf64-powerpc"
73 #define ELF_ARCH bfd_arch_powerpc
74 #define ELF_TARGET_ID PPC64_ELF_DATA
75 #define ELF_MACHINE_CODE EM_PPC64
76 #define ELF_MAXPAGESIZE 0x10000
77 #define ELF_COMMONPAGESIZE 0x1000
78 #define elf_info_to_howto ppc64_elf_info_to_howto
79
80 #define elf_backend_want_got_sym 0
81 #define elf_backend_want_plt_sym 0
82 #define elf_backend_plt_alignment 3
83 #define elf_backend_plt_not_loaded 1
84 #define elf_backend_got_header_size 8
85 #define elf_backend_want_dynrelro 1
86 #define elf_backend_can_gc_sections 1
87 #define elf_backend_can_refcount 1
88 #define elf_backend_rela_normal 1
89 #define elf_backend_dtrel_excludes_plt 1
90 #define elf_backend_default_execstack 0
91
92 #define bfd_elf64_mkobject ppc64_elf_mkobject
93 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
94 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
95 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
96 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
97 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
98 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
99 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
100 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
101 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
102
103 #define elf_backend_object_p ppc64_elf_object_p
104 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
105 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
106 #define elf_backend_write_core_note ppc64_elf_write_core_note
107 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
108 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
109 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
110 #define elf_backend_check_directives ppc64_elf_before_check_relocs
111 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
112 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
113 #define elf_backend_check_relocs ppc64_elf_check_relocs
114 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
115 #define elf_backend_gc_keep ppc64_elf_gc_keep
116 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
117 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
118 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
119 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
120 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
121 #define elf_backend_always_size_sections ppc64_elf_edit
122 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
123 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
124 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
125 #define elf_backend_action_discarded ppc64_elf_action_discarded
126 #define elf_backend_relocate_section ppc64_elf_relocate_section
127 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
128 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
129 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
130 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
131 #define elf_backend_special_sections ppc64_elf_special_sections
132 #define elf_backend_section_flags ppc64_elf_section_flags
133 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
134 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
135 #define elf_backend_get_reloc_section bfd_get_section_by_name
136
137 /* The name of the dynamic interpreter. This is put in the .interp
138 section. */
139 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
140
141 /* The size in bytes of an entry in the procedure linkage table. */
142 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
143 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
144
145 /* The initial size of the plt reserved for the dynamic linker. */
146 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
147
148 /* Offsets to some stack save slots. */
149 #define STK_LR 16
150 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
151 /* This one is dodgy. ELFv2 does not have a linker word, so use the
152 CR save slot. Used only by optimised __tls_get_addr call stub,
153 relying on __tls_get_addr_opt not saving CR.. */
154 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
155
156 /* TOC base pointers offset from start of TOC. */
157 #define TOC_BASE_OFF 0x8000
158 /* TOC base alignment. */
159 #define TOC_BASE_ALIGN 256
160
161 /* Offset of tp and dtp pointers from start of TLS block. */
162 #define TP_OFFSET 0x7000
163 #define DTP_OFFSET 0x8000
164
165 /* .plt call stub instructions. The normal stub is like this, but
166 sometimes the .plt entry crosses a 64k boundary and we need to
167 insert an addi to adjust r11. */
168 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
169 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
170 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
171 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
172 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
173 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
174 #define BCTR 0x4e800420 /* bctr */
175
176 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
177 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
178 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
179 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
180 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
181
182 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
183 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
184 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
185 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
186 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
187 #define BNECTR 0x4ca20420 /* bnectr+ */
188 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
189
190 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
191 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
192 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
193
194 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
195 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
196 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
197
198 #define LI_R11_0 0x39600000 /* li %r11,0 */
199 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
200 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
201 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
202 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
203 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
204 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
205 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
206 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
207 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
208 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
209 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
210 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
211 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
212 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
213 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
214 #define PADDI_R12_PC 0x0610000039800000ULL
215 #define PLD_R12_PC 0x04100000e5800000ULL
216 #define PNOP 0x0700000000000000ULL
217
218 /* __glink_PLTresolve stub instructions. We enter with the index in
219 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
220 #define GLINK_PLTRESOLVE_SIZE(htab) \
221 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
222 /* 0: */
223 /* .quad plt0-1f */
224 /* __glink: */
225 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
226 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
227 /* 1: */
228 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
229 /* ld %2,(0b-1b)(%11) */
230 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
231 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
232 /* ld %12,0(%11) */
233 /* ld %2,8(%11) */
234 /* mtctr %12 */
235 /* ld %11,16(%11) */
236 /* bctr */
237
238 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
239 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
240 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
241 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
242 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
243 #define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
244 #define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
245
246 /* Pad with this. */
247 #define NOP 0x60000000
248
249 /* Some other nops. */
250 #define CROR_151515 0x4def7b82
251 #define CROR_313131 0x4ffffb82
252
253 /* .glink entries for the first 32k functions are two instructions. */
254 #define LI_R0_0 0x38000000 /* li %r0,0 */
255 #define B_DOT 0x48000000 /* b . */
256
257 /* After that, we need two instructions to load the index, followed by
258 a branch. */
259 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
260 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
261
262 /* Instructions used by the save and restore reg functions. */
263 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
264 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
265 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
266 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
267 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
268 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
269 #define LI_R12_0 0x39800000 /* li %r12,0 */
270 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
271 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
272 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
273 #define BLR 0x4e800020 /* blr */
274
275 /* Since .opd is an array of descriptors and each entry will end up
276 with identical R_PPC64_RELATIVE relocs, there is really no need to
277 propagate .opd relocs; The dynamic linker should be taught to
278 relocate .opd without reloc entries. */
279 #ifndef NO_OPD_RELOCS
280 #define NO_OPD_RELOCS 0
281 #endif
282
283 #ifndef ARRAY_SIZE
284 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
285 #endif
286
287 static inline int
288 abiversion (bfd *abfd)
289 {
290 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
291 }
292
293 static inline void
294 set_abiversion (bfd *abfd, int ver)
295 {
296 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
297 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
298 }
299 \f
300 /* Relocation HOWTO's. */
301 /* Like other ELF RELA targets that don't apply multiple
302 field-altering relocations to the same localation, src_mask is
303 always zero and pcrel_offset is the same as pc_relative.
304 PowerPC can always use a zero bitpos, even when the field is not at
305 the LSB. For example, a REL24 could use rightshift=2, bisize=24
306 and bitpos=2 which matches the ABI description, or as we do here,
307 rightshift=0, bitsize=26 and bitpos=0. */
308 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
309 complain, special_func) \
310 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
311 complain_overflow_ ## complain, special_func, \
312 #type, false, 0, mask, pc_relative)
313
314 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
315
316 static reloc_howto_type ppc64_elf_howto_raw[] =
317 {
318 /* This reloc does nothing. */
319 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
320 bfd_elf_generic_reloc),
321
322 /* A standard 32 bit relocation. */
323 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
324 bfd_elf_generic_reloc),
325
326 /* An absolute 26 bit branch; the lower two bits must be zero.
327 FIXME: we don't check that, we just clear them. */
328 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
329 bfd_elf_generic_reloc),
330
331 /* A standard 16 bit relocation. */
332 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
333 bfd_elf_generic_reloc),
334
335 /* A 16 bit relocation without overflow. */
336 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
337 bfd_elf_generic_reloc),
338
339 /* Bits 16-31 of an address. */
340 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
341 bfd_elf_generic_reloc),
342
343 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
344 bits, treated as a signed number, is negative. */
345 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
346 ppc64_elf_ha_reloc),
347
348 /* An absolute 16 bit branch; the lower two bits must be zero.
349 FIXME: we don't check that, we just clear them. */
350 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
351 ppc64_elf_branch_reloc),
352
353 /* An absolute 16 bit branch, for which bit 10 should be set to
354 indicate that the branch is expected to be taken. The lower two
355 bits must be zero. */
356 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
357 ppc64_elf_brtaken_reloc),
358
359 /* An absolute 16 bit branch, for which bit 10 should be set to
360 indicate that the branch is not expected to be taken. The lower
361 two bits must be zero. */
362 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
363 ppc64_elf_brtaken_reloc),
364
365 /* A relative 26 bit branch; the lower two bits must be zero. */
366 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
367 ppc64_elf_branch_reloc),
368
369 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
370 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
371 ppc64_elf_branch_reloc),
372
373 /* Another variant, when p10 insns can't be used on stubs. */
374 HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
375 ppc64_elf_branch_reloc),
376
377 /* A relative 16 bit branch; the lower two bits must be zero. */
378 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
379 ppc64_elf_branch_reloc),
380
381 /* A relative 16 bit branch. Bit 10 should be set to indicate that
382 the branch is expected to be taken. The lower two bits must be
383 zero. */
384 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
385 ppc64_elf_brtaken_reloc),
386
387 /* A relative 16 bit branch. Bit 10 should be set to indicate that
388 the branch is not expected to be taken. The lower two bits must
389 be zero. */
390 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
391 ppc64_elf_brtaken_reloc),
392
393 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
394 symbol. */
395 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
396 ppc64_elf_unhandled_reloc),
397
398 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
399 the symbol. */
400 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
401 ppc64_elf_unhandled_reloc),
402
403 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
404 the symbol. */
405 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
406 ppc64_elf_unhandled_reloc),
407
408 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
409 the symbol. */
410 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
411 ppc64_elf_unhandled_reloc),
412
413 /* This is used only by the dynamic linker. The symbol should exist
414 both in the object being run and in some shared library. The
415 dynamic linker copies the data addressed by the symbol from the
416 shared library into the object, because the object being
417 run has to have the data at some particular address. */
418 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
419 ppc64_elf_unhandled_reloc),
420
421 /* Like R_PPC64_ADDR64, but used when setting global offset table
422 entries. */
423 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
424 ppc64_elf_unhandled_reloc),
425
426 /* Created by the link editor. Marks a procedure linkage table
427 entry for a symbol. */
428 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
429 ppc64_elf_unhandled_reloc),
430
431 /* Used only by the dynamic linker. When the object is run, this
432 doubleword64 is set to the load address of the object, plus the
433 addend. */
434 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
435 bfd_elf_generic_reloc),
436
437 /* Like R_PPC64_ADDR32, but may be unaligned. */
438 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
439 bfd_elf_generic_reloc),
440
441 /* Like R_PPC64_ADDR16, but may be unaligned. */
442 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
443 bfd_elf_generic_reloc),
444
445 /* 32-bit PC relative. */
446 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
447 bfd_elf_generic_reloc),
448
449 /* 32-bit relocation to the symbol's procedure linkage table. */
450 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
451 ppc64_elf_unhandled_reloc),
452
453 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
454 FIXME: R_PPC64_PLTREL32 not supported. */
455 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
456 ppc64_elf_unhandled_reloc),
457
458 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
459 the symbol. */
460 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
461 ppc64_elf_unhandled_reloc),
462
463 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
464 the symbol. */
465 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
466 ppc64_elf_unhandled_reloc),
467
468 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
469 the symbol. */
470 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
471 ppc64_elf_unhandled_reloc),
472
473 /* 16-bit section relative relocation. */
474 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
475 ppc64_elf_sectoff_reloc),
476
477 /* Like R_PPC64_SECTOFF, but no overflow warning. */
478 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
479 ppc64_elf_sectoff_reloc),
480
481 /* 16-bit upper half section relative relocation. */
482 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
483 ppc64_elf_sectoff_reloc),
484
485 /* 16-bit upper half adjusted section relative relocation. */
486 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
487 ppc64_elf_sectoff_ha_reloc),
488
489 /* Like R_PPC64_REL24 without touching the two least significant bits. */
490 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
491 bfd_elf_generic_reloc),
492
493 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
494
495 /* A standard 64-bit relocation. */
496 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
497 bfd_elf_generic_reloc),
498
499 /* The bits 32-47 of an address. */
500 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
501 bfd_elf_generic_reloc),
502
503 /* The bits 32-47 of an address, plus 1 if the contents of the low
504 16 bits, treated as a signed number, is negative. */
505 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
506 ppc64_elf_ha_reloc),
507
508 /* The bits 48-63 of an address. */
509 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
510 bfd_elf_generic_reloc),
511
512 /* The bits 48-63 of an address, plus 1 if the contents of the low
513 16 bits, treated as a signed number, is negative. */
514 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
515 ppc64_elf_ha_reloc),
516
517 /* Like ADDR64, but may be unaligned. */
518 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
519 bfd_elf_generic_reloc),
520
521 /* 64-bit relative relocation. */
522 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
523 bfd_elf_generic_reloc),
524
525 /* 64-bit relocation to the symbol's procedure linkage table. */
526 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
527 ppc64_elf_unhandled_reloc),
528
529 /* 64-bit PC relative relocation to the symbol's procedure linkage
530 table. */
531 /* FIXME: R_PPC64_PLTREL64 not supported. */
532 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
533 ppc64_elf_unhandled_reloc),
534
535 /* 16 bit TOC-relative relocation. */
536 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
537 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
538 ppc64_elf_toc_reloc),
539
540 /* 16 bit TOC-relative relocation without overflow. */
541 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
542 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
543 ppc64_elf_toc_reloc),
544
545 /* 16 bit TOC-relative relocation, high 16 bits. */
546 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
547 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
548 ppc64_elf_toc_reloc),
549
550 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
551 contents of the low 16 bits, treated as a signed number, is
552 negative. */
553 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
554 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
555 ppc64_elf_toc_ha_reloc),
556
557 /* 64-bit relocation; insert value of TOC base (.TOC.). */
558 /* R_PPC64_TOC 51 doubleword64 .TOC. */
559 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
560 ppc64_elf_toc64_reloc),
561
562 /* Like R_PPC64_GOT16, but also informs the link editor that the
563 value to relocate may (!) refer to a PLT entry which the link
564 editor (a) may replace with the symbol value. If the link editor
565 is unable to fully resolve the symbol, it may (b) create a PLT
566 entry and store the address to the new PLT entry in the GOT.
567 This permits lazy resolution of function symbols at run time.
568 The link editor may also skip all of this and just (c) emit a
569 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
570 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
571 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
572 ppc64_elf_unhandled_reloc),
573
574 /* Like R_PPC64_PLTGOT16, but without overflow. */
575 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
576 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
577 ppc64_elf_unhandled_reloc),
578
579 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
580 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
581 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
582 ppc64_elf_unhandled_reloc),
583
584 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
585 1 if the contents of the low 16 bits, treated as a signed number,
586 is negative. */
587 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
588 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
589 ppc64_elf_unhandled_reloc),
590
591 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
592 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
593 bfd_elf_generic_reloc),
594
595 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
596 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
597 bfd_elf_generic_reloc),
598
599 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
600 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
601 ppc64_elf_unhandled_reloc),
602
603 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
604 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
605 ppc64_elf_unhandled_reloc),
606
607 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
608 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
609 ppc64_elf_unhandled_reloc),
610
611 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
612 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
613 ppc64_elf_sectoff_reloc),
614
615 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
616 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
617 ppc64_elf_sectoff_reloc),
618
619 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
620 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
621 ppc64_elf_toc_reloc),
622
623 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
624 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
625 ppc64_elf_toc_reloc),
626
627 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
628 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
629 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
630 ppc64_elf_unhandled_reloc),
631
632 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
633 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
634 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
635 ppc64_elf_unhandled_reloc),
636
637 /* Marker relocs for TLS. */
638 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
639 bfd_elf_generic_reloc),
640
641 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
642 bfd_elf_generic_reloc),
643
644 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
645 bfd_elf_generic_reloc),
646
647 /* Marker reloc for optimizing r2 save in prologue rather than on
648 each plt call stub. */
649 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
650 bfd_elf_generic_reloc),
651
652 /* Marker relocs on inline plt call instructions. */
653 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
654 bfd_elf_generic_reloc),
655
656 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
657 bfd_elf_generic_reloc),
658
659 /* Computes the load module index of the load module that contains the
660 definition of its TLS sym. */
661 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
662 ppc64_elf_unhandled_reloc),
663
664 /* Computes a dtv-relative displacement, the difference between the value
665 of sym+add and the base address of the thread-local storage block that
666 contains the definition of sym, minus 0x8000. */
667 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
668 ppc64_elf_unhandled_reloc),
669
670 /* A 16 bit dtprel reloc. */
671 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
672 ppc64_elf_unhandled_reloc),
673
674 /* Like DTPREL16, but no overflow. */
675 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
676 ppc64_elf_unhandled_reloc),
677
678 /* Like DTPREL16_LO, but next higher group of 16 bits. */
679 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
680 ppc64_elf_unhandled_reloc),
681
682 /* Like DTPREL16_HI, but adjust for low 16 bits. */
683 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
684 ppc64_elf_unhandled_reloc),
685
686 /* Like DTPREL16_HI, but next higher group of 16 bits. */
687 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
688 ppc64_elf_unhandled_reloc),
689
690 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
691 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
692 ppc64_elf_unhandled_reloc),
693
694 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
695 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
696 ppc64_elf_unhandled_reloc),
697
698 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
699 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
700 ppc64_elf_unhandled_reloc),
701
702 /* Like DTPREL16, but for insns with a DS field. */
703 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
704 ppc64_elf_unhandled_reloc),
705
706 /* Like DTPREL16_DS, but no overflow. */
707 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
708 ppc64_elf_unhandled_reloc),
709
710 /* Computes a tp-relative displacement, the difference between the value of
711 sym+add and the value of the thread pointer (r13). */
712 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
713 ppc64_elf_unhandled_reloc),
714
715 /* A 16 bit tprel reloc. */
716 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
717 ppc64_elf_unhandled_reloc),
718
719 /* Like TPREL16, but no overflow. */
720 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
721 ppc64_elf_unhandled_reloc),
722
723 /* Like TPREL16_LO, but next higher group of 16 bits. */
724 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
725 ppc64_elf_unhandled_reloc),
726
727 /* Like TPREL16_HI, but adjust for low 16 bits. */
728 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
729 ppc64_elf_unhandled_reloc),
730
731 /* Like TPREL16_HI, but next higher group of 16 bits. */
732 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
733 ppc64_elf_unhandled_reloc),
734
735 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
736 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
737 ppc64_elf_unhandled_reloc),
738
739 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
740 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
741 ppc64_elf_unhandled_reloc),
742
743 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
744 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
745 ppc64_elf_unhandled_reloc),
746
747 /* Like TPREL16, but for insns with a DS field. */
748 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
749 ppc64_elf_unhandled_reloc),
750
751 /* Like TPREL16_DS, but no overflow. */
752 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
753 ppc64_elf_unhandled_reloc),
754
755 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
756 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
757 to the first entry relative to the TOC base (r2). */
758 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
759 ppc64_elf_unhandled_reloc),
760
761 /* Like GOT_TLSGD16, but no overflow. */
762 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
763 ppc64_elf_unhandled_reloc),
764
765 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
766 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
767 ppc64_elf_unhandled_reloc),
768
769 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
770 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
771 ppc64_elf_unhandled_reloc),
772
773 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
774 with values (sym+add)@dtpmod and zero, and computes the offset to the
775 first entry relative to the TOC base (r2). */
776 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
777 ppc64_elf_unhandled_reloc),
778
779 /* Like GOT_TLSLD16, but no overflow. */
780 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
781 ppc64_elf_unhandled_reloc),
782
783 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
784 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
785 ppc64_elf_unhandled_reloc),
786
787 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
788 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
789 ppc64_elf_unhandled_reloc),
790
791 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
792 the offset to the entry relative to the TOC base (r2). */
793 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
794 ppc64_elf_unhandled_reloc),
795
796 /* Like GOT_DTPREL16_DS, but no overflow. */
797 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
798 ppc64_elf_unhandled_reloc),
799
800 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
801 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
802 ppc64_elf_unhandled_reloc),
803
804 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
805 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
806 ppc64_elf_unhandled_reloc),
807
808 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
809 offset to the entry relative to the TOC base (r2). */
810 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
811 ppc64_elf_unhandled_reloc),
812
813 /* Like GOT_TPREL16_DS, but no overflow. */
814 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
815 ppc64_elf_unhandled_reloc),
816
817 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
818 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
819 ppc64_elf_unhandled_reloc),
820
821 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
822 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
823 ppc64_elf_unhandled_reloc),
824
825 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
826 ppc64_elf_unhandled_reloc),
827
828 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
829 bfd_elf_generic_reloc),
830
831 /* A 16 bit relative relocation. */
832 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
833 bfd_elf_generic_reloc),
834
835 /* A 16 bit relative relocation without overflow. */
836 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
837 bfd_elf_generic_reloc),
838
839 /* The high order 16 bits of a relative address. */
840 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
841 bfd_elf_generic_reloc),
842
843 /* The high order 16 bits of a relative address, plus 1 if the contents of
844 the low 16 bits, treated as a signed number, is negative. */
845 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
846 ppc64_elf_ha_reloc),
847
848 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
849 bfd_elf_generic_reloc),
850
851 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
852 ppc64_elf_ha_reloc),
853
854 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
855 bfd_elf_generic_reloc),
856
857 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
858 ppc64_elf_ha_reloc),
859
860 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
861 bfd_elf_generic_reloc),
862
863 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
864 ppc64_elf_ha_reloc),
865
866 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
867 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
868 ppc64_elf_ha_reloc),
869
870 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
871 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
872 ppc64_elf_ha_reloc),
873
874 /* Like R_PPC64_ADDR16_HI, but no overflow. */
875 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
876 bfd_elf_generic_reloc),
877
878 /* Like R_PPC64_ADDR16_HA, but no overflow. */
879 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
880 ppc64_elf_ha_reloc),
881
882 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
883 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
884 ppc64_elf_unhandled_reloc),
885
886 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
887 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
888 ppc64_elf_unhandled_reloc),
889
890 /* Like R_PPC64_TPREL16_HI, but no overflow. */
891 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
892 ppc64_elf_unhandled_reloc),
893
894 /* Like R_PPC64_TPREL16_HA, but no overflow. */
895 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
896 ppc64_elf_unhandled_reloc),
897
898 /* Marker reloc on ELFv2 large-model function entry. */
899 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
900 bfd_elf_generic_reloc),
901
902 /* Like ADDR64, but use local entry point of function. */
903 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
904 bfd_elf_generic_reloc),
905
906 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
907 bfd_elf_generic_reloc),
908
909 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
910 bfd_elf_generic_reloc),
911
912 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
913 bfd_elf_generic_reloc),
914
915 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
916 ppc64_elf_prefix_reloc),
917
918 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
919 ppc64_elf_prefix_reloc),
920
921 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
922 ppc64_elf_prefix_reloc),
923
924 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
925 ppc64_elf_prefix_reloc),
926
927 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
928 ppc64_elf_prefix_reloc),
929
930 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
931 ppc64_elf_unhandled_reloc),
932
933 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
934 ppc64_elf_unhandled_reloc),
935
936 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
937 ppc64_elf_unhandled_reloc),
938
939 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
940 ppc64_elf_unhandled_reloc),
941
942 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
943 ppc64_elf_unhandled_reloc),
944
945 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
946 ppc64_elf_unhandled_reloc),
947
948 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
949 ppc64_elf_unhandled_reloc),
950
951 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
952 ppc64_elf_unhandled_reloc),
953
954 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
955 ppc64_elf_unhandled_reloc),
956
957 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
958 bfd_elf_generic_reloc),
959
960 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
961 ppc64_elf_ha_reloc),
962
963 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
964 bfd_elf_generic_reloc),
965
966 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
967 ppc64_elf_ha_reloc),
968
969 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
970 bfd_elf_generic_reloc),
971
972 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
973 ppc64_elf_ha_reloc),
974
975 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
976 bfd_elf_generic_reloc),
977
978 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
979 ppc64_elf_ha_reloc),
980
981 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
982 ppc64_elf_prefix_reloc),
983
984 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
985 ppc64_elf_prefix_reloc),
986
987 /* GNU extension to record C++ vtable hierarchy. */
988 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
989 NULL),
990
991 /* GNU extension to record C++ vtable member usage. */
992 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
993 NULL),
994 };
995
996 \f
997 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
998 be done. */
999
1000 static void
1001 ppc_howto_init (void)
1002 {
1003 unsigned int i, type;
1004
1005 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1006 {
1007 type = ppc64_elf_howto_raw[i].type;
1008 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1009 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1010 }
1011 }
1012
1013 static reloc_howto_type *
1014 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1015 {
1016 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1017
1018 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1019 /* Initialize howto table if needed. */
1020 ppc_howto_init ();
1021
1022 switch (code)
1023 {
1024 default:
1025 /* xgettext:c-format */
1026 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1027 (int) code);
1028 bfd_set_error (bfd_error_bad_value);
1029 return NULL;
1030
1031 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1032 break;
1033 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1034 break;
1035 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1036 break;
1037 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1038 break;
1039 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1040 break;
1041 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1042 break;
1043 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1044 break;
1045 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1046 break;
1047 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1048 break;
1049 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1050 break;
1051 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1052 break;
1053 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1054 break;
1055 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1056 break;
1057 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1058 break;
1059 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1060 break;
1061 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1062 break;
1063 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1064 break;
1065 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1066 break;
1067 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1068 break;
1069 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1070 break;
1071 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1072 break;
1073 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1074 break;
1075 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1076 break;
1077 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1078 break;
1079 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1080 break;
1081 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1082 break;
1083 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1084 break;
1085 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1086 break;
1087 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1088 break;
1089 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1090 break;
1091 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1092 break;
1093 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1094 break;
1095 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1096 break;
1097 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1098 break;
1099 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1100 break;
1101 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1102 break;
1103 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1104 break;
1105 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1106 break;
1107 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1108 break;
1109 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1110 break;
1111 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1112 break;
1113 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1114 break;
1115 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1116 break;
1117 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1118 break;
1119 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1120 break;
1121 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1122 break;
1123 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1124 break;
1125 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1126 break;
1127 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1128 break;
1129 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1130 break;
1131 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1132 break;
1133 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1134 break;
1135 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1136 break;
1137 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1138 break;
1139 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1140 break;
1141 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1142 break;
1143 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1144 break;
1145 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1146 break;
1147 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1148 break;
1149 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1150 break;
1151 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1152 break;
1153 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1154 break;
1155 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1156 break;
1157 case BFD_RELOC_PPC64_TLS_PCREL:
1158 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1159 break;
1160 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1161 break;
1162 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1163 break;
1164 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1165 break;
1166 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1167 break;
1168 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1169 break;
1170 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1171 break;
1172 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1173 break;
1174 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1175 break;
1176 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1177 break;
1178 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1179 break;
1180 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1181 break;
1182 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1183 break;
1184 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1185 break;
1186 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1187 break;
1188 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1189 break;
1190 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1191 break;
1192 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1193 break;
1194 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1195 break;
1196 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1197 break;
1198 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1199 break;
1200 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1201 break;
1202 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1203 break;
1204 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1205 break;
1206 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1207 break;
1208 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1209 break;
1210 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1211 break;
1212 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1213 break;
1214 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1215 break;
1216 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1217 break;
1218 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1219 break;
1220 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1221 break;
1222 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1223 break;
1224 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1225 break;
1226 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1227 break;
1228 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1229 break;
1230 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1231 break;
1232 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1233 break;
1234 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1235 break;
1236 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1237 break;
1238 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1239 break;
1240 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1241 break;
1242 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1243 break;
1244 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1245 break;
1246 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1247 break;
1248 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1249 break;
1250 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1251 break;
1252 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1253 break;
1254 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1255 break;
1256 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1257 break;
1258 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1259 break;
1260 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1261 break;
1262 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1263 break;
1264 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1265 break;
1266 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1267 break;
1268 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1269 break;
1270 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1271 break;
1272 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1273 break;
1274 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1275 break;
1276 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1277 break;
1278 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1279 break;
1280 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1281 break;
1282 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1283 break;
1284 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1285 break;
1286 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1287 break;
1288 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1289 break;
1290 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1291 break;
1292 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1293 break;
1294 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1295 break;
1296 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1297 break;
1298 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1299 break;
1300 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1301 break;
1302 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1303 break;
1304 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1305 break;
1306 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1307 break;
1308 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1309 break;
1310 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1311 break;
1312 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1313 break;
1314 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1315 break;
1316 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1317 break;
1318 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1319 break;
1320 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1321 break;
1322 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1323 break;
1324 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1325 break;
1326 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1327 break;
1328 }
1329
1330 return ppc64_elf_howto_table[r];
1331 };
1332
1333 static reloc_howto_type *
1334 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1335 {
1336 unsigned int i;
1337 static char *compat_map[][2] = {
1338 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1339 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1340 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1341 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1342 };
1343
1344 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1345 if (ppc64_elf_howto_raw[i].name != NULL
1346 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1347 return &ppc64_elf_howto_raw[i];
1348
1349 /* Handle old names of relocations in case they were used by
1350 .reloc directives.
1351 FIXME: Remove this soon. Mapping the reloc names is very likely
1352 completely unnecessary. */
1353 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1354 if (strcasecmp (compat_map[i][0], r_name) == 0)
1355 {
1356 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1357 compat_map[i][1], compat_map[i][0]);
1358 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1359 }
1360
1361 return NULL;
1362 }
1363
1364 /* Set the howto pointer for a PowerPC ELF reloc. */
1365
1366 static bool
1367 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1368 Elf_Internal_Rela *dst)
1369 {
1370 unsigned int type;
1371
1372 /* Initialize howto table if needed. */
1373 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1374 ppc_howto_init ();
1375
1376 type = ELF64_R_TYPE (dst->r_info);
1377 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1378 {
1379 /* xgettext:c-format */
1380 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1381 abfd, type);
1382 bfd_set_error (bfd_error_bad_value);
1383 return false;
1384 }
1385 cache_ptr->howto = ppc64_elf_howto_table[type];
1386 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1387 {
1388 /* xgettext:c-format */
1389 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1390 abfd, type);
1391 bfd_set_error (bfd_error_bad_value);
1392 return false;
1393 }
1394
1395 return true;
1396 }
1397
1398 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1399
1400 static bfd_reloc_status_type
1401 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1402 void *data, asection *input_section,
1403 bfd *output_bfd, char **error_message)
1404 {
1405 enum elf_ppc64_reloc_type r_type;
1406 long insn;
1407 bfd_size_type octets;
1408 bfd_vma value;
1409
1410 /* If this is a relocatable link (output_bfd test tells us), just
1411 call the generic function. Any adjustment will be done at final
1412 link time. */
1413 if (output_bfd != NULL)
1414 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1415 input_section, output_bfd, error_message);
1416
1417 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1418 We won't actually be using the low bits, so trashing them
1419 doesn't matter. */
1420 r_type = reloc_entry->howto->type;
1421 if (r_type == R_PPC64_ADDR16_HIGHERA34
1422 || r_type == R_PPC64_ADDR16_HIGHESTA34
1423 || r_type == R_PPC64_REL16_HIGHERA34
1424 || r_type == R_PPC64_REL16_HIGHESTA34)
1425 reloc_entry->addend += 1ULL << 33;
1426 else
1427 reloc_entry->addend += 1U << 15;
1428 if (r_type != R_PPC64_REL16DX_HA)
1429 return bfd_reloc_continue;
1430
1431 value = 0;
1432 if (!bfd_is_com_section (symbol->section))
1433 value = symbol->value;
1434 value += (reloc_entry->addend
1435 + symbol->section->output_offset
1436 + symbol->section->output_section->vma);
1437 value -= (reloc_entry->address
1438 + input_section->output_offset
1439 + input_section->output_section->vma);
1440 value = (bfd_signed_vma) value >> 16;
1441
1442 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1443 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1444 input_section, octets))
1445 return bfd_reloc_outofrange;
1446
1447 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1448 insn &= ~0x1fffc1;
1449 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1450 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1451 if (value + 0x8000 > 0xffff)
1452 return bfd_reloc_overflow;
1453 return bfd_reloc_ok;
1454 }
1455
1456 static bfd_reloc_status_type
1457 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1458 void *data, asection *input_section,
1459 bfd *output_bfd, char **error_message)
1460 {
1461 if (output_bfd != NULL)
1462 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1463 input_section, output_bfd, error_message);
1464
1465 if (strcmp (symbol->section->name, ".opd") == 0
1466 && (symbol->section->owner->flags & DYNAMIC) == 0)
1467 {
1468 bfd_vma dest = opd_entry_value (symbol->section,
1469 symbol->value + reloc_entry->addend,
1470 NULL, NULL, false);
1471 if (dest != (bfd_vma) -1)
1472 reloc_entry->addend = dest - (symbol->value
1473 + symbol->section->output_section->vma
1474 + symbol->section->output_offset);
1475 }
1476 else
1477 {
1478 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1479
1480 if (symbol->section->owner != abfd
1481 && symbol->section->owner != NULL
1482 && abiversion (symbol->section->owner) >= 2)
1483 {
1484 unsigned int i;
1485
1486 for (i = 0; i < symbol->section->owner->symcount; ++i)
1487 {
1488 asymbol *symdef = symbol->section->owner->outsymbols[i];
1489
1490 if (strcmp (symdef->name, symbol->name) == 0)
1491 {
1492 elfsym = (elf_symbol_type *) symdef;
1493 break;
1494 }
1495 }
1496 }
1497 reloc_entry->addend
1498 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1499 }
1500 return bfd_reloc_continue;
1501 }
1502
1503 static bfd_reloc_status_type
1504 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1505 void *data, asection *input_section,
1506 bfd *output_bfd, char **error_message)
1507 {
1508 long insn;
1509 enum elf_ppc64_reloc_type r_type;
1510 bfd_size_type octets;
1511 /* Assume 'at' branch hints. */
1512 bool is_isa_v2 = true;
1513
1514 /* If this is a relocatable link (output_bfd test tells us), just
1515 call the generic function. Any adjustment will be done at final
1516 link time. */
1517 if (output_bfd != NULL)
1518 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1519 input_section, output_bfd, error_message);
1520
1521 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1522 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1523 input_section, octets))
1524 return bfd_reloc_outofrange;
1525
1526 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1527 insn &= ~(0x01 << 21);
1528 r_type = reloc_entry->howto->type;
1529 if (r_type == R_PPC64_ADDR14_BRTAKEN
1530 || r_type == R_PPC64_REL14_BRTAKEN)
1531 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1532
1533 if (is_isa_v2)
1534 {
1535 /* Set 'a' bit. This is 0b00010 in BO field for branch
1536 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1537 for branch on CTR insns (BO == 1a00t or 1a01t). */
1538 if ((insn & (0x14 << 21)) == (0x04 << 21))
1539 insn |= 0x02 << 21;
1540 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1541 insn |= 0x08 << 21;
1542 else
1543 goto out;
1544 }
1545 else
1546 {
1547 bfd_vma target = 0;
1548 bfd_vma from;
1549
1550 if (!bfd_is_com_section (symbol->section))
1551 target = symbol->value;
1552 target += symbol->section->output_section->vma;
1553 target += symbol->section->output_offset;
1554 target += reloc_entry->addend;
1555
1556 from = (reloc_entry->address
1557 + input_section->output_offset
1558 + input_section->output_section->vma);
1559
1560 /* Invert 'y' bit if not the default. */
1561 if ((bfd_signed_vma) (target - from) < 0)
1562 insn ^= 0x01 << 21;
1563 }
1564 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1565 out:
1566 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1567 input_section, output_bfd, error_message);
1568 }
1569
1570 static bfd_reloc_status_type
1571 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1572 void *data, asection *input_section,
1573 bfd *output_bfd, char **error_message)
1574 {
1575 /* If this is a relocatable link (output_bfd test tells us), just
1576 call the generic function. Any adjustment will be done at final
1577 link time. */
1578 if (output_bfd != NULL)
1579 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1580 input_section, output_bfd, error_message);
1581
1582 /* Subtract the symbol section base address. */
1583 reloc_entry->addend -= symbol->section->output_section->vma;
1584 return bfd_reloc_continue;
1585 }
1586
1587 static bfd_reloc_status_type
1588 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1589 void *data, asection *input_section,
1590 bfd *output_bfd, char **error_message)
1591 {
1592 /* If this is a relocatable link (output_bfd test tells us), just
1593 call the generic function. Any adjustment will be done at final
1594 link time. */
1595 if (output_bfd != NULL)
1596 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1597 input_section, output_bfd, error_message);
1598
1599 /* Subtract the symbol section base address. */
1600 reloc_entry->addend -= symbol->section->output_section->vma;
1601
1602 /* Adjust the addend for sign extension of the low 16 bits. */
1603 reloc_entry->addend += 0x8000;
1604 return bfd_reloc_continue;
1605 }
1606
1607 static bfd_reloc_status_type
1608 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1609 void *data, asection *input_section,
1610 bfd *output_bfd, char **error_message)
1611 {
1612 bfd_vma TOCstart;
1613
1614 /* If this is a relocatable link (output_bfd test tells us), just
1615 call the generic function. Any adjustment will be done at final
1616 link time. */
1617 if (output_bfd != NULL)
1618 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1619 input_section, output_bfd, error_message);
1620
1621 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1622 if (TOCstart == 0)
1623 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1624
1625 /* Subtract the TOC base address. */
1626 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1627 return bfd_reloc_continue;
1628 }
1629
1630 static bfd_reloc_status_type
1631 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1632 void *data, asection *input_section,
1633 bfd *output_bfd, char **error_message)
1634 {
1635 bfd_vma TOCstart;
1636
1637 /* If this is a relocatable link (output_bfd test tells us), just
1638 call the generic function. Any adjustment will be done at final
1639 link time. */
1640 if (output_bfd != NULL)
1641 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1642 input_section, output_bfd, error_message);
1643
1644 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1645 if (TOCstart == 0)
1646 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1647
1648 /* Subtract the TOC base address. */
1649 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1650
1651 /* Adjust the addend for sign extension of the low 16 bits. */
1652 reloc_entry->addend += 0x8000;
1653 return bfd_reloc_continue;
1654 }
1655
1656 static bfd_reloc_status_type
1657 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1658 void *data, asection *input_section,
1659 bfd *output_bfd, char **error_message)
1660 {
1661 bfd_vma TOCstart;
1662 bfd_size_type octets;
1663
1664 /* If this is a relocatable link (output_bfd test tells us), just
1665 call the generic function. Any adjustment will be done at final
1666 link time. */
1667 if (output_bfd != NULL)
1668 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1669 input_section, output_bfd, error_message);
1670
1671 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1672 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1673 input_section, octets))
1674 return bfd_reloc_outofrange;
1675
1676 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1677 if (TOCstart == 0)
1678 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1679
1680 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1681 return bfd_reloc_ok;
1682 }
1683
1684 static bfd_reloc_status_type
1685 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1686 void *data, asection *input_section,
1687 bfd *output_bfd, char **error_message)
1688 {
1689 uint64_t insn;
1690 bfd_vma targ;
1691 bfd_size_type octets;
1692
1693 if (output_bfd != NULL)
1694 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1695 input_section, output_bfd, error_message);
1696
1697 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1698 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1699 input_section, octets))
1700 return bfd_reloc_outofrange;
1701
1702 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1703 insn <<= 32;
1704 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1705
1706 targ = (symbol->section->output_section->vma
1707 + symbol->section->output_offset
1708 + reloc_entry->addend);
1709 if (!bfd_is_com_section (symbol->section))
1710 targ += symbol->value;
1711 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1712 targ += 1ULL << 33;
1713 if (reloc_entry->howto->pc_relative)
1714 {
1715 bfd_vma from = (reloc_entry->address
1716 + input_section->output_offset
1717 + input_section->output_section->vma);
1718 targ -=from;
1719 }
1720 targ >>= reloc_entry->howto->rightshift;
1721 insn &= ~reloc_entry->howto->dst_mask;
1722 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1723 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1724 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1725 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1726 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1727 >= 1ULL << reloc_entry->howto->bitsize))
1728 return bfd_reloc_overflow;
1729 return bfd_reloc_ok;
1730 }
1731
1732 static bfd_reloc_status_type
1733 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1734 void *data, asection *input_section,
1735 bfd *output_bfd, char **error_message)
1736 {
1737 /* If this is a relocatable link (output_bfd test tells us), just
1738 call the generic function. Any adjustment will be done at final
1739 link time. */
1740 if (output_bfd != NULL)
1741 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1742 input_section, output_bfd, error_message);
1743
1744 if (error_message != NULL)
1745 {
1746 static char *message;
1747 free (message);
1748 if (asprintf (&message, _("generic linker can't handle %s"),
1749 reloc_entry->howto->name) < 0)
1750 message = NULL;
1751 *error_message = message;
1752 }
1753 return bfd_reloc_dangerous;
1754 }
1755
1756 /* Track GOT entries needed for a given symbol. We might need more
1757 than one got entry per symbol. */
1758 struct got_entry
1759 {
1760 struct got_entry *next;
1761
1762 /* The symbol addend that we'll be placing in the GOT. */
1763 bfd_vma addend;
1764
1765 /* Unlike other ELF targets, we use separate GOT entries for the same
1766 symbol referenced from different input files. This is to support
1767 automatic multiple TOC/GOT sections, where the TOC base can vary
1768 from one input file to another. After partitioning into TOC groups
1769 we merge entries within the group.
1770
1771 Point to the BFD owning this GOT entry. */
1772 bfd *owner;
1773
1774 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1775 TLS_TPREL or TLS_DTPREL for tls entries. */
1776 unsigned char tls_type;
1777
1778 /* Non-zero if got.ent points to real entry. */
1779 unsigned char is_indirect;
1780
1781 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1782 union
1783 {
1784 bfd_signed_vma refcount;
1785 bfd_vma offset;
1786 struct got_entry *ent;
1787 } got;
1788 };
1789
1790 /* The same for PLT. */
1791 struct plt_entry
1792 {
1793 struct plt_entry *next;
1794
1795 bfd_vma addend;
1796
1797 union
1798 {
1799 bfd_signed_vma refcount;
1800 bfd_vma offset;
1801 } plt;
1802 };
1803
1804 struct ppc64_elf_obj_tdata
1805 {
1806 struct elf_obj_tdata elf;
1807
1808 /* Shortcuts to dynamic linker sections. */
1809 asection *got;
1810 asection *relgot;
1811
1812 /* Used during garbage collection. We attach global symbols defined
1813 on removed .opd entries to this section so that the sym is removed. */
1814 asection *deleted_section;
1815
1816 /* TLS local dynamic got entry handling. Support for multiple GOT
1817 sections means we potentially need one of these for each input bfd. */
1818 struct got_entry tlsld_got;
1819
1820 union
1821 {
1822 /* A copy of relocs before they are modified for --emit-relocs. */
1823 Elf_Internal_Rela *relocs;
1824
1825 /* Section contents. */
1826 bfd_byte *contents;
1827 } opd;
1828
1829 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1830 the reloc to be in the range -32768 to 32767. */
1831 unsigned int has_small_toc_reloc : 1;
1832
1833 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1834 instruction not one we handle. */
1835 unsigned int unexpected_toc_insn : 1;
1836
1837 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1838 this file. */
1839 unsigned int has_optrel : 1;
1840 };
1841
1842 #define ppc64_elf_tdata(bfd) \
1843 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1844
1845 #define ppc64_tlsld_got(bfd) \
1846 (&ppc64_elf_tdata (bfd)->tlsld_got)
1847
1848 #define is_ppc64_elf(bfd) \
1849 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1850 && elf_object_id (bfd) == PPC64_ELF_DATA)
1851
1852 /* Override the generic function because we store some extras. */
1853
1854 static bool
1855 ppc64_elf_mkobject (bfd *abfd)
1856 {
1857 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1858 PPC64_ELF_DATA);
1859 }
1860
1861 /* Fix bad default arch selected for a 64 bit input bfd when the
1862 default is 32 bit. Also select arch based on apuinfo. */
1863
1864 static bool
1865 ppc64_elf_object_p (bfd *abfd)
1866 {
1867 if (!abfd->arch_info->the_default)
1868 return true;
1869
1870 if (abfd->arch_info->bits_per_word == 32)
1871 {
1872 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1873
1874 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1875 {
1876 /* Relies on arch after 32 bit default being 64 bit default. */
1877 abfd->arch_info = abfd->arch_info->next;
1878 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1879 }
1880 }
1881 return _bfd_elf_ppc_set_arch (abfd);
1882 }
1883
1884 /* Support for core dump NOTE sections. */
1885
1886 static bool
1887 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1888 {
1889 size_t offset, size;
1890
1891 if (note->descsz != 504)
1892 return false;
1893
1894 /* pr_cursig */
1895 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1896
1897 /* pr_pid */
1898 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1899
1900 /* pr_reg */
1901 offset = 112;
1902 size = 384;
1903
1904 /* Make a ".reg/999" section. */
1905 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1906 size, note->descpos + offset);
1907 }
1908
1909 static bool
1910 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1911 {
1912 if (note->descsz != 136)
1913 return false;
1914
1915 elf_tdata (abfd)->core->pid
1916 = bfd_get_32 (abfd, note->descdata + 24);
1917 elf_tdata (abfd)->core->program
1918 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1919 elf_tdata (abfd)->core->command
1920 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1921
1922 return true;
1923 }
1924
1925 static char *
1926 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1927 ...)
1928 {
1929 switch (note_type)
1930 {
1931 default:
1932 return NULL;
1933
1934 case NT_PRPSINFO:
1935 {
1936 char data[136] ATTRIBUTE_NONSTRING;
1937 va_list ap;
1938
1939 va_start (ap, note_type);
1940 memset (data, 0, sizeof (data));
1941 strncpy (data + 40, va_arg (ap, const char *), 16);
1942 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1943 DIAGNOSTIC_PUSH;
1944 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1945 -Wstringop-truncation:
1946 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1947 */
1948 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1949 #endif
1950 strncpy (data + 56, va_arg (ap, const char *), 80);
1951 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1952 DIAGNOSTIC_POP;
1953 #endif
1954 va_end (ap);
1955 return elfcore_write_note (abfd, buf, bufsiz,
1956 "CORE", note_type, data, sizeof (data));
1957 }
1958
1959 case NT_PRSTATUS:
1960 {
1961 char data[504];
1962 va_list ap;
1963 long pid;
1964 int cursig;
1965 const void *greg;
1966
1967 va_start (ap, note_type);
1968 memset (data, 0, 112);
1969 pid = va_arg (ap, long);
1970 bfd_put_32 (abfd, pid, data + 32);
1971 cursig = va_arg (ap, int);
1972 bfd_put_16 (abfd, cursig, data + 12);
1973 greg = va_arg (ap, const void *);
1974 memcpy (data + 112, greg, 384);
1975 memset (data + 496, 0, 8);
1976 va_end (ap);
1977 return elfcore_write_note (abfd, buf, bufsiz,
1978 "CORE", note_type, data, sizeof (data));
1979 }
1980 }
1981 }
1982
1983 /* Add extra PPC sections. */
1984
1985 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1986 {
1987 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1988 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1989 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1990 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1991 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1993 { NULL, 0, 0, 0, 0 }
1994 };
1995
1996 enum _ppc64_sec_type {
1997 sec_normal = 0,
1998 sec_opd = 1,
1999 sec_toc = 2
2000 };
2001
2002 struct _ppc64_elf_section_data
2003 {
2004 struct bfd_elf_section_data elf;
2005
2006 union
2007 {
2008 /* An array with one entry for each opd function descriptor,
2009 and some spares since opd entries may be either 16 or 24 bytes. */
2010 #define OPD_NDX(OFF) ((OFF) >> 4)
2011 struct _opd_sec_data
2012 {
2013 /* Points to the function code section for local opd entries. */
2014 asection **func_sec;
2015
2016 /* After editing .opd, adjust references to opd local syms. */
2017 long *adjust;
2018 } opd;
2019
2020 /* An array for toc sections, indexed by offset/8. */
2021 struct _toc_sec_data
2022 {
2023 /* Specifies the relocation symbol index used at a given toc offset. */
2024 unsigned *symndx;
2025
2026 /* And the relocation addend. */
2027 bfd_vma *add;
2028 } toc;
2029 } u;
2030
2031 enum _ppc64_sec_type sec_type:2;
2032
2033 /* Flag set when small branches are detected. Used to
2034 select suitable defaults for the stub group size. */
2035 unsigned int has_14bit_branch:1;
2036
2037 /* Flag set when PLTCALL relocs are detected. */
2038 unsigned int has_pltcall:1;
2039
2040 /* Flag set when section has PLT/GOT/TOC relocations that can be
2041 optimised. */
2042 unsigned int has_optrel:1;
2043 };
2044
2045 #define ppc64_elf_section_data(sec) \
2046 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2047
2048 static bool
2049 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2050 {
2051 if (!sec->used_by_bfd)
2052 {
2053 struct _ppc64_elf_section_data *sdata;
2054 size_t amt = sizeof (*sdata);
2055
2056 sdata = bfd_zalloc (abfd, amt);
2057 if (sdata == NULL)
2058 return false;
2059 sec->used_by_bfd = sdata;
2060 }
2061
2062 return _bfd_elf_new_section_hook (abfd, sec);
2063 }
2064
2065 static bool
2066 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2067 {
2068 const char *name = hdr->bfd_section->name;
2069
2070 if (startswith (name, ".sbss")
2071 || startswith (name, ".sdata"))
2072 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2073
2074 return true;
2075 }
2076
2077 static struct _opd_sec_data *
2078 get_opd_info (asection * sec)
2079 {
2080 if (sec != NULL
2081 && ppc64_elf_section_data (sec) != NULL
2082 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2083 return &ppc64_elf_section_data (sec)->u.opd;
2084 return NULL;
2085 }
2086 \f
2087 /* Parameters for the qsort hook. */
2088 static bool synthetic_relocatable;
2089 static const asection *synthetic_opd;
2090
2091 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2092
2093 static int
2094 compare_symbols (const void *ap, const void *bp)
2095 {
2096 const asymbol *a = *(const asymbol **) ap;
2097 const asymbol *b = *(const asymbol **) bp;
2098
2099 /* Section symbols first. */
2100 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2101 return -1;
2102 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2103 return 1;
2104
2105 /* then .opd symbols. */
2106 if (synthetic_opd != NULL)
2107 {
2108 if (strcmp (a->section->name, ".opd") == 0
2109 && strcmp (b->section->name, ".opd") != 0)
2110 return -1;
2111 if (strcmp (a->section->name, ".opd") != 0
2112 && strcmp (b->section->name, ".opd") == 0)
2113 return 1;
2114 }
2115
2116 /* then other code symbols. */
2117 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2118 == (SEC_CODE | SEC_ALLOC))
2119 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2120 != (SEC_CODE | SEC_ALLOC)))
2121 return -1;
2122
2123 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2124 != (SEC_CODE | SEC_ALLOC))
2125 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2126 == (SEC_CODE | SEC_ALLOC)))
2127 return 1;
2128
2129 if (synthetic_relocatable)
2130 {
2131 if (a->section->id < b->section->id)
2132 return -1;
2133
2134 if (a->section->id > b->section->id)
2135 return 1;
2136 }
2137
2138 if (a->value + a->section->vma < b->value + b->section->vma)
2139 return -1;
2140
2141 if (a->value + a->section->vma > b->value + b->section->vma)
2142 return 1;
2143
2144 /* For syms with the same value, prefer strong dynamic global function
2145 syms over other syms. */
2146 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2147 return -1;
2148
2149 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2150 return 1;
2151
2152 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2153 return -1;
2154
2155 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2156 return 1;
2157
2158 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2159 return -1;
2160
2161 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2162 return 1;
2163
2164 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2165 return -1;
2166
2167 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2168 return 1;
2169
2170 /* Finally, sort on where the symbol is in memory. The symbols will
2171 be in at most two malloc'd blocks, one for static syms, one for
2172 dynamic syms, and we distinguish the two blocks above by testing
2173 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2174 originally in the same order as the symbols (and we're not
2175 sorting the symbols themselves), this ensures a stable sort. */
2176 if (a < b)
2177 return -1;
2178 if (a > b)
2179 return 1;
2180 return 0;
2181 }
2182
2183 /* Search SYMS for a symbol of the given VALUE. */
2184
2185 static asymbol *
2186 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2187 bfd_vma value)
2188 {
2189 size_t mid;
2190
2191 if (id == (unsigned) -1)
2192 {
2193 while (lo < hi)
2194 {
2195 mid = (lo + hi) >> 1;
2196 if (syms[mid]->value + syms[mid]->section->vma < value)
2197 lo = mid + 1;
2198 else if (syms[mid]->value + syms[mid]->section->vma > value)
2199 hi = mid;
2200 else
2201 return syms[mid];
2202 }
2203 }
2204 else
2205 {
2206 while (lo < hi)
2207 {
2208 mid = (lo + hi) >> 1;
2209 if (syms[mid]->section->id < id)
2210 lo = mid + 1;
2211 else if (syms[mid]->section->id > id)
2212 hi = mid;
2213 else if (syms[mid]->value < value)
2214 lo = mid + 1;
2215 else if (syms[mid]->value > value)
2216 hi = mid;
2217 else
2218 return syms[mid];
2219 }
2220 }
2221 return NULL;
2222 }
2223
2224 static bool
2225 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2226 {
2227 bfd_vma vma = *(bfd_vma *) ptr;
2228 return ((section->flags & SEC_ALLOC) != 0
2229 && section->vma <= vma
2230 && vma < section->vma + section->size);
2231 }
2232
2233 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2234 entry syms. Also generate @plt symbols for the glink branch table.
2235 Returns count of synthetic symbols in RET or -1 on error. */
2236
2237 static long
2238 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2239 long static_count, asymbol **static_syms,
2240 long dyn_count, asymbol **dyn_syms,
2241 asymbol **ret)
2242 {
2243 asymbol *s;
2244 size_t i, j, count;
2245 char *names;
2246 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2247 asection *opd = NULL;
2248 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2249 asymbol **syms;
2250 int abi = abiversion (abfd);
2251
2252 *ret = NULL;
2253
2254 if (abi < 2)
2255 {
2256 opd = bfd_get_section_by_name (abfd, ".opd");
2257 if (opd == NULL && abi == 1)
2258 return 0;
2259 }
2260
2261 syms = NULL;
2262 codesecsym = 0;
2263 codesecsymend = 0;
2264 secsymend = 0;
2265 opdsymend = 0;
2266 symcount = 0;
2267 if (opd != NULL)
2268 {
2269 symcount = static_count;
2270 if (!relocatable)
2271 symcount += dyn_count;
2272 if (symcount == 0)
2273 return 0;
2274
2275 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2276 if (syms == NULL)
2277 return -1;
2278
2279 if (!relocatable && static_count != 0 && dyn_count != 0)
2280 {
2281 /* Use both symbol tables. */
2282 memcpy (syms, static_syms, static_count * sizeof (*syms));
2283 memcpy (syms + static_count, dyn_syms,
2284 (dyn_count + 1) * sizeof (*syms));
2285 }
2286 else if (!relocatable && static_count == 0)
2287 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2288 else
2289 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2290
2291 /* Trim uninteresting symbols. Interesting symbols are section,
2292 function, and notype symbols. */
2293 for (i = 0, j = 0; i < symcount; ++i)
2294 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2295 | BSF_RELC | BSF_SRELC)) == 0)
2296 syms[j++] = syms[i];
2297 symcount = j;
2298
2299 synthetic_relocatable = relocatable;
2300 synthetic_opd = opd;
2301 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2302
2303 if (!relocatable && symcount > 1)
2304 {
2305 /* Trim duplicate syms, since we may have merged the normal
2306 and dynamic symbols. Actually, we only care about syms
2307 that have different values, so trim any with the same
2308 value. Don't consider ifunc and ifunc resolver symbols
2309 duplicates however, because GDB wants to know whether a
2310 text symbol is an ifunc resolver. */
2311 for (i = 1, j = 1; i < symcount; ++i)
2312 {
2313 const asymbol *s0 = syms[i - 1];
2314 const asymbol *s1 = syms[i];
2315
2316 if ((s0->value + s0->section->vma
2317 != s1->value + s1->section->vma)
2318 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2319 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2320 syms[j++] = syms[i];
2321 }
2322 symcount = j;
2323 }
2324
2325 i = 0;
2326 /* Note that here and in compare_symbols we can't compare opd and
2327 sym->section directly. With separate debug info files, the
2328 symbols will be extracted from the debug file while abfd passed
2329 to this function is the real binary. */
2330 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2331 && strcmp (syms[i]->section->name, ".opd") == 0)
2332 ++i;
2333 codesecsym = i;
2334
2335 for (; i < symcount; ++i)
2336 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2337 | SEC_THREAD_LOCAL))
2338 != (SEC_CODE | SEC_ALLOC))
2339 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2340 break;
2341 codesecsymend = i;
2342
2343 for (; i < symcount; ++i)
2344 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2345 break;
2346 secsymend = i;
2347
2348 for (; i < symcount; ++i)
2349 if (strcmp (syms[i]->section->name, ".opd") != 0)
2350 break;
2351 opdsymend = i;
2352
2353 for (; i < symcount; ++i)
2354 if (((syms[i]->section->flags
2355 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2356 != (SEC_CODE | SEC_ALLOC))
2357 break;
2358 symcount = i;
2359 }
2360 count = 0;
2361
2362 if (relocatable)
2363 {
2364 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2365 arelent *r;
2366 size_t size;
2367 size_t relcount;
2368
2369 if (opdsymend == secsymend)
2370 goto done;
2371
2372 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2373 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2374 if (relcount == 0)
2375 goto done;
2376
2377 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2378 {
2379 count = -1;
2380 goto done;
2381 }
2382
2383 size = 0;
2384 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2385 {
2386 asymbol *sym;
2387
2388 while (r < opd->relocation + relcount
2389 && r->address < syms[i]->value + opd->vma)
2390 ++r;
2391
2392 if (r == opd->relocation + relcount)
2393 break;
2394
2395 if (r->address != syms[i]->value + opd->vma)
2396 continue;
2397
2398 if (r->howto->type != R_PPC64_ADDR64)
2399 continue;
2400
2401 sym = *r->sym_ptr_ptr;
2402 if (!sym_exists_at (syms, opdsymend, symcount,
2403 sym->section->id, sym->value + r->addend))
2404 {
2405 ++count;
2406 size += sizeof (asymbol);
2407 size += strlen (syms[i]->name) + 2;
2408 }
2409 }
2410
2411 if (size == 0)
2412 goto done;
2413 s = *ret = bfd_malloc (size);
2414 if (s == NULL)
2415 {
2416 count = -1;
2417 goto done;
2418 }
2419
2420 names = (char *) (s + count);
2421
2422 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2423 {
2424 asymbol *sym;
2425
2426 while (r < opd->relocation + relcount
2427 && r->address < syms[i]->value + opd->vma)
2428 ++r;
2429
2430 if (r == opd->relocation + relcount)
2431 break;
2432
2433 if (r->address != syms[i]->value + opd->vma)
2434 continue;
2435
2436 if (r->howto->type != R_PPC64_ADDR64)
2437 continue;
2438
2439 sym = *r->sym_ptr_ptr;
2440 if (!sym_exists_at (syms, opdsymend, symcount,
2441 sym->section->id, sym->value + r->addend))
2442 {
2443 size_t len;
2444
2445 *s = *syms[i];
2446 s->flags |= BSF_SYNTHETIC;
2447 s->section = sym->section;
2448 s->value = sym->value + r->addend;
2449 s->name = names;
2450 *names++ = '.';
2451 len = strlen (syms[i]->name);
2452 memcpy (names, syms[i]->name, len + 1);
2453 names += len + 1;
2454 /* Have udata.p point back to the original symbol this
2455 synthetic symbol was derived from. */
2456 s->udata.p = syms[i];
2457 s++;
2458 }
2459 }
2460 }
2461 else
2462 {
2463 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2464 bfd_byte *contents = NULL;
2465 size_t size;
2466 size_t plt_count = 0;
2467 bfd_vma glink_vma = 0, resolv_vma = 0;
2468 asection *dynamic, *glink = NULL, *relplt = NULL;
2469 arelent *p;
2470
2471 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2472 {
2473 free_contents_and_exit_err:
2474 count = -1;
2475 free_contents_and_exit:
2476 free (contents);
2477 goto done;
2478 }
2479
2480 size = 0;
2481 for (i = secsymend; i < opdsymend; ++i)
2482 {
2483 bfd_vma ent;
2484
2485 /* Ignore bogus symbols. */
2486 if (syms[i]->value > opd->size - 8)
2487 continue;
2488
2489 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2490 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2491 {
2492 ++count;
2493 size += sizeof (asymbol);
2494 size += strlen (syms[i]->name) + 2;
2495 }
2496 }
2497
2498 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2499 if (dyn_count != 0
2500 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2501 {
2502 bfd_byte *dynbuf, *extdyn, *extdynend;
2503 size_t extdynsize;
2504 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2505
2506 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2507 goto free_contents_and_exit_err;
2508
2509 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2510 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2511
2512 extdyn = dynbuf;
2513 extdynend = extdyn + dynamic->size;
2514 for (; extdyn < extdynend; extdyn += extdynsize)
2515 {
2516 Elf_Internal_Dyn dyn;
2517 (*swap_dyn_in) (abfd, extdyn, &dyn);
2518
2519 if (dyn.d_tag == DT_NULL)
2520 break;
2521
2522 if (dyn.d_tag == DT_PPC64_GLINK)
2523 {
2524 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2525 See comment in ppc64_elf_finish_dynamic_sections. */
2526 glink_vma = dyn.d_un.d_val + 8 * 4;
2527 /* The .glink section usually does not survive the final
2528 link; search for the section (usually .text) where the
2529 glink stubs now reside. */
2530 glink = bfd_sections_find_if (abfd, section_covers_vma,
2531 &glink_vma);
2532 break;
2533 }
2534 }
2535
2536 free (dynbuf);
2537 }
2538
2539 if (glink != NULL)
2540 {
2541 /* Determine __glink trampoline by reading the relative branch
2542 from the first glink stub. */
2543 bfd_byte buf[4];
2544 unsigned int off = 0;
2545
2546 while (bfd_get_section_contents (abfd, glink, buf,
2547 glink_vma + off - glink->vma, 4))
2548 {
2549 unsigned int insn = bfd_get_32 (abfd, buf);
2550 insn ^= B_DOT;
2551 if ((insn & ~0x3fffffc) == 0)
2552 {
2553 resolv_vma
2554 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2555 break;
2556 }
2557 off += 4;
2558 if (off > 4)
2559 break;
2560 }
2561
2562 if (resolv_vma)
2563 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2564
2565 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2566 if (relplt != NULL)
2567 {
2568 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2569 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2570 goto free_contents_and_exit_err;
2571
2572 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2573 size += plt_count * sizeof (asymbol);
2574
2575 p = relplt->relocation;
2576 for (i = 0; i < plt_count; i++, p++)
2577 {
2578 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2579 if (p->addend != 0)
2580 size += sizeof ("+0x") - 1 + 16;
2581 }
2582 }
2583 }
2584
2585 if (size == 0)
2586 goto free_contents_and_exit;
2587 s = *ret = bfd_malloc (size);
2588 if (s == NULL)
2589 goto free_contents_and_exit_err;
2590
2591 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2592
2593 for (i = secsymend; i < opdsymend; ++i)
2594 {
2595 bfd_vma ent;
2596
2597 if (syms[i]->value > opd->size - 8)
2598 continue;
2599
2600 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2601 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2602 {
2603 size_t lo, hi;
2604 size_t len;
2605 asection *sec = abfd->sections;
2606
2607 *s = *syms[i];
2608 lo = codesecsym;
2609 hi = codesecsymend;
2610 while (lo < hi)
2611 {
2612 size_t mid = (lo + hi) >> 1;
2613 if (syms[mid]->section->vma < ent)
2614 lo = mid + 1;
2615 else if (syms[mid]->section->vma > ent)
2616 hi = mid;
2617 else
2618 {
2619 sec = syms[mid]->section;
2620 break;
2621 }
2622 }
2623
2624 if (lo >= hi && lo > codesecsym)
2625 sec = syms[lo - 1]->section;
2626
2627 for (; sec != NULL; sec = sec->next)
2628 {
2629 if (sec->vma > ent)
2630 break;
2631 /* SEC_LOAD may not be set if SEC is from a separate debug
2632 info file. */
2633 if ((sec->flags & SEC_ALLOC) == 0)
2634 break;
2635 if ((sec->flags & SEC_CODE) != 0)
2636 s->section = sec;
2637 }
2638 s->flags |= BSF_SYNTHETIC;
2639 s->value = ent - s->section->vma;
2640 s->name = names;
2641 *names++ = '.';
2642 len = strlen (syms[i]->name);
2643 memcpy (names, syms[i]->name, len + 1);
2644 names += len + 1;
2645 /* Have udata.p point back to the original symbol this
2646 synthetic symbol was derived from. */
2647 s->udata.p = syms[i];
2648 s++;
2649 }
2650 }
2651 free (contents);
2652
2653 if (glink != NULL && relplt != NULL)
2654 {
2655 if (resolv_vma)
2656 {
2657 /* Add a symbol for the main glink trampoline. */
2658 memset (s, 0, sizeof *s);
2659 s->the_bfd = abfd;
2660 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2661 s->section = glink;
2662 s->value = resolv_vma - glink->vma;
2663 s->name = names;
2664 memcpy (names, "__glink_PLTresolve",
2665 sizeof ("__glink_PLTresolve"));
2666 names += sizeof ("__glink_PLTresolve");
2667 s++;
2668 count++;
2669 }
2670
2671 /* FIXME: It would be very much nicer to put sym@plt on the
2672 stub rather than on the glink branch table entry. The
2673 objdump disassembler would then use a sensible symbol
2674 name on plt calls. The difficulty in doing so is
2675 a) finding the stubs, and,
2676 b) matching stubs against plt entries, and,
2677 c) there can be multiple stubs for a given plt entry.
2678
2679 Solving (a) could be done by code scanning, but older
2680 ppc64 binaries used different stubs to current code.
2681 (b) is the tricky one since you need to known the toc
2682 pointer for at least one function that uses a pic stub to
2683 be able to calculate the plt address referenced.
2684 (c) means gdb would need to set multiple breakpoints (or
2685 find the glink branch itself) when setting breakpoints
2686 for pending shared library loads. */
2687 p = relplt->relocation;
2688 for (i = 0; i < plt_count; i++, p++)
2689 {
2690 size_t len;
2691
2692 *s = **p->sym_ptr_ptr;
2693 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2694 we are defining a symbol, ensure one of them is set. */
2695 if ((s->flags & BSF_LOCAL) == 0)
2696 s->flags |= BSF_GLOBAL;
2697 s->flags |= BSF_SYNTHETIC;
2698 s->section = glink;
2699 s->value = glink_vma - glink->vma;
2700 s->name = names;
2701 s->udata.p = NULL;
2702 len = strlen ((*p->sym_ptr_ptr)->name);
2703 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2704 names += len;
2705 if (p->addend != 0)
2706 {
2707 memcpy (names, "+0x", sizeof ("+0x") - 1);
2708 names += sizeof ("+0x") - 1;
2709 bfd_sprintf_vma (abfd, names, p->addend);
2710 names += strlen (names);
2711 }
2712 memcpy (names, "@plt", sizeof ("@plt"));
2713 names += sizeof ("@plt");
2714 s++;
2715 if (abi < 2)
2716 {
2717 glink_vma += 8;
2718 if (i >= 0x8000)
2719 glink_vma += 4;
2720 }
2721 else
2722 glink_vma += 4;
2723 }
2724 count += plt_count;
2725 }
2726 }
2727
2728 done:
2729 free (syms);
2730 return count;
2731 }
2732 \f
2733 /* The following functions are specific to the ELF linker, while
2734 functions above are used generally. Those named ppc64_elf_* are
2735 called by the main ELF linker code. They appear in this file more
2736 or less in the order in which they are called. eg.
2737 ppc64_elf_check_relocs is called early in the link process,
2738 ppc64_elf_finish_dynamic_sections is one of the last functions
2739 called.
2740
2741 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2742 functions have both a function code symbol and a function descriptor
2743 symbol. A call to foo in a relocatable object file looks like:
2744
2745 . .text
2746 . x:
2747 . bl .foo
2748 . nop
2749
2750 The function definition in another object file might be:
2751
2752 . .section .opd
2753 . foo: .quad .foo
2754 . .quad .TOC.@tocbase
2755 . .quad 0
2756 .
2757 . .text
2758 . .foo: blr
2759
2760 When the linker resolves the call during a static link, the branch
2761 unsurprisingly just goes to .foo and the .opd information is unused.
2762 If the function definition is in a shared library, things are a little
2763 different: The call goes via a plt call stub, the opd information gets
2764 copied to the plt, and the linker patches the nop.
2765
2766 . x:
2767 . bl .foo_stub
2768 . ld 2,40(1)
2769 .
2770 .
2771 . .foo_stub:
2772 . std 2,40(1) # in practice, the call stub
2773 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2774 . addi 11,11,Lfoo@toc@l # this is the general idea
2775 . ld 12,0(11)
2776 . ld 2,8(11)
2777 . mtctr 12
2778 . ld 11,16(11)
2779 . bctr
2780 .
2781 . .section .plt
2782 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2783
2784 The "reloc ()" notation is supposed to indicate that the linker emits
2785 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2786 copying.
2787
2788 What are the difficulties here? Well, firstly, the relocations
2789 examined by the linker in check_relocs are against the function code
2790 sym .foo, while the dynamic relocation in the plt is emitted against
2791 the function descriptor symbol, foo. Somewhere along the line, we need
2792 to carefully copy dynamic link information from one symbol to the other.
2793 Secondly, the generic part of the elf linker will make .foo a dynamic
2794 symbol as is normal for most other backends. We need foo dynamic
2795 instead, at least for an application final link. However, when
2796 creating a shared library containing foo, we need to have both symbols
2797 dynamic so that references to .foo are satisfied during the early
2798 stages of linking. Otherwise the linker might decide to pull in a
2799 definition from some other object, eg. a static library.
2800
2801 Update: As of August 2004, we support a new convention. Function
2802 calls may use the function descriptor symbol, ie. "bl foo". This
2803 behaves exactly as "bl .foo". */
2804
2805 /* Of those relocs that might be copied as dynamic relocs, this
2806 function selects those that must be copied when linking a shared
2807 library or PIE, even when the symbol is local. */
2808
2809 static int
2810 must_be_dyn_reloc (struct bfd_link_info *info,
2811 enum elf_ppc64_reloc_type r_type)
2812 {
2813 switch (r_type)
2814 {
2815 default:
2816 /* Only relative relocs can be resolved when the object load
2817 address isn't fixed. DTPREL64 is excluded because the
2818 dynamic linker needs to differentiate global dynamic from
2819 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2820 return 1;
2821
2822 case R_PPC64_REL32:
2823 case R_PPC64_REL64:
2824 case R_PPC64_REL30:
2825 case R_PPC64_TOC16:
2826 case R_PPC64_TOC16_DS:
2827 case R_PPC64_TOC16_LO:
2828 case R_PPC64_TOC16_HI:
2829 case R_PPC64_TOC16_HA:
2830 case R_PPC64_TOC16_LO_DS:
2831 return 0;
2832
2833 case R_PPC64_TPREL16:
2834 case R_PPC64_TPREL16_LO:
2835 case R_PPC64_TPREL16_HI:
2836 case R_PPC64_TPREL16_HA:
2837 case R_PPC64_TPREL16_DS:
2838 case R_PPC64_TPREL16_LO_DS:
2839 case R_PPC64_TPREL16_HIGH:
2840 case R_PPC64_TPREL16_HIGHA:
2841 case R_PPC64_TPREL16_HIGHER:
2842 case R_PPC64_TPREL16_HIGHERA:
2843 case R_PPC64_TPREL16_HIGHEST:
2844 case R_PPC64_TPREL16_HIGHESTA:
2845 case R_PPC64_TPREL64:
2846 case R_PPC64_TPREL34:
2847 /* These relocations are relative but in a shared library the
2848 linker doesn't know the thread pointer base. */
2849 return bfd_link_dll (info);
2850 }
2851 }
2852
2853 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2854 copying dynamic variables from a shared lib into an app's .dynbss
2855 section, and instead use a dynamic relocation to point into the
2856 shared lib. With code that gcc generates it is vital that this be
2857 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2858 actually the address of a function descriptor which resides in the
2859 .opd section. gcc uses the descriptor directly rather than going
2860 via the GOT as some other ABIs do, which means that initialized
2861 function pointers reference the descriptor. Thus, a function
2862 pointer initialized to the address of a function in a shared
2863 library will either require a .dynbss copy and a copy reloc, or a
2864 dynamic reloc. Using a .dynbss copy redefines the function
2865 descriptor symbol to point to the copy. This presents a problem as
2866 a PLT entry for that function is also initialized from the function
2867 descriptor symbol and the copy may not be initialized first. */
2868 #define ELIMINATE_COPY_RELOCS 1
2869
2870 /* Section name for stubs is the associated section name plus this
2871 string. */
2872 #define STUB_SUFFIX ".stub"
2873
2874 /* Linker stubs.
2875 ppc_stub_long_branch:
2876 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2877 destination, but a 24 bit branch in a stub section will reach.
2878 . b dest
2879
2880 ppc_stub_plt_branch:
2881 Similar to the above, but a 24 bit branch in the stub section won't
2882 reach its destination.
2883 . addis %r12,%r2,xxx@toc@ha
2884 . ld %r12,xxx@toc@l(%r12)
2885 . mtctr %r12
2886 . bctr
2887
2888 ppc_stub_plt_call:
2889 Used to call a function in a shared library. If it so happens that
2890 the plt entry referenced crosses a 64k boundary, then an extra
2891 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2892 An r2save variant starts with "std %r2,40(%r1)".
2893 . addis %r11,%r2,xxx@toc@ha
2894 . ld %r12,xxx+0@toc@l(%r11)
2895 . mtctr %r12
2896 . ld %r2,xxx+8@toc@l(%r11)
2897 . ld %r11,xxx+16@toc@l(%r11)
2898 . bctr
2899
2900 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2901 code to adjust the value and save r2 to support multiple toc sections.
2902 A ppc_stub_long_branch with an r2 offset looks like:
2903 . std %r2,40(%r1)
2904 . addis %r2,%r2,off@ha
2905 . addi %r2,%r2,off@l
2906 . b dest
2907
2908 A ppc_stub_plt_branch with an r2 offset looks like:
2909 . std %r2,40(%r1)
2910 . addis %r12,%r2,xxx@toc@ha
2911 . ld %r12,xxx@toc@l(%r12)
2912 . addis %r2,%r2,off@ha
2913 . addi %r2,%r2,off@l
2914 . mtctr %r12
2915 . bctr
2916
2917 All of the above stubs are shown as their ELFv1 variants. ELFv2
2918 variants exist too, simpler for plt calls since a new toc pointer
2919 and static chain are not loaded by the stub. In addition, ELFv2
2920 has some more complex stubs to handle calls marked with NOTOC
2921 relocs from functions where r2 is not a valid toc pointer.
2922 ppc_stub_long_branch_p9notoc:
2923 . mflr %r12
2924 . bcl 20,31,1f
2925 . 1:
2926 . mflr %r11
2927 . mtlr %r12
2928 . addis %r12,%r11,dest-1b@ha
2929 . addi %r12,%r12,dest-1b@l
2930 . b dest
2931
2932 ppc_stub_plt_branch_p9notoc:
2933 . mflr %r12
2934 . bcl 20,31,1f
2935 . 1:
2936 . mflr %r11
2937 . mtlr %r12
2938 . lis %r12,xxx-1b@highest
2939 . ori %r12,%r12,xxx-1b@higher
2940 . sldi %r12,%r12,32
2941 . oris %r12,%r12,xxx-1b@high
2942 . ori %r12,%r12,xxx-1b@l
2943 . add %r12,%r11,%r12
2944 . mtctr %r12
2945 . bctr
2946
2947 ppc_stub_plt_call_p9notoc:
2948 . mflr %r12
2949 . bcl 20,31,1f
2950 . 1:
2951 . mflr %r11
2952 . mtlr %r12
2953 . lis %r12,xxx-1b@highest
2954 . ori %r12,%r12,xxx-1b@higher
2955 . sldi %r12,%r12,32
2956 . oris %r12,%r12,xxx-1b@high
2957 . ori %r12,%r12,xxx-1b@l
2958 . ldx %r12,%r11,%r12
2959 . mtctr %r12
2960 . bctr
2961
2962 There are also ELFv1 power10 variants of these stubs.
2963 ppc_stub_long_branch_notoc:
2964 . pla %r12,dest@pcrel
2965 . b dest
2966 ppc_stub_plt_branch_notoc:
2967 . lis %r11,(dest-1f)@highesta34
2968 . ori %r11,%r11,(dest-1f)@highera34
2969 . sldi %r11,%r11,34
2970 . 1: pla %r12,dest@pcrel
2971 . add %r12,%r11,%r12
2972 . mtctr %r12
2973 . bctr
2974 ppc_stub_plt_call_notoc:
2975 . lis %r11,(xxx-1f)@highesta34
2976 . ori %r11,%r11,(xxx-1f)@highera34
2977 . sldi %r11,%r11,34
2978 . 1: pla %r12,xxx@pcrel
2979 . ldx %r12,%r11,%r12
2980 . mtctr %r12
2981 . bctr
2982
2983 In cases where the high instructions would add zero, they are
2984 omitted and following instructions modified in some cases.
2985 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2986 to
2987 . pld %r12,xxx@pcrel
2988 . mtctr %r12
2989 . bctr
2990
2991 Stub variants may be merged. For example, if printf is called from
2992 code with the tocsave optimization (ie. r2 saved in function
2993 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2994 and from other code without the tocsave optimization requiring a
2995 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2996 type will be created. Calls with the tocsave optimization will
2997 enter this stub after the instruction saving r2. A similar
2998 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2999 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3000 to call an external function like printf. If other calls to printf
3001 require a ppc_stub_plt_call linkage stub then a single
3002 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3003 call. */
3004
3005 enum ppc_stub_main_type
3006 {
3007 ppc_stub_none,
3008 ppc_stub_long_branch,
3009 ppc_stub_plt_branch,
3010 ppc_stub_plt_call,
3011 ppc_stub_global_entry,
3012 ppc_stub_save_res
3013 };
3014
3015 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3016 these variations. */
3017
3018 enum ppc_stub_sub_type
3019 {
3020 ppc_stub_toc,
3021 ppc_stub_notoc,
3022 ppc_stub_p9notoc
3023 };
3024
3025 struct ppc_stub_type
3026 {
3027 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3028 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3029 unsigned int r2save : 1;
3030 };
3031
3032 /* Information on stub grouping. */
3033 struct map_stub
3034 {
3035 /* The stub section. */
3036 asection *stub_sec;
3037 /* This is the section to which stubs in the group will be attached. */
3038 asection *link_sec;
3039 /* Next group. */
3040 struct map_stub *next;
3041 /* Whether to emit a copy of register save/restore functions in this
3042 group. */
3043 int needs_save_res;
3044 /* Current offset within stubs after the insn restoring lr in a
3045 _notoc or _both stub using bcl for pc-relative addressing, or
3046 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3047 unsigned int lr_restore;
3048 /* Accumulated size of EH info emitted to describe return address
3049 if stubs modify lr. Does not include 17 byte FDE header. */
3050 unsigned int eh_size;
3051 /* Offset in glink_eh_frame to the start of EH info for this group. */
3052 unsigned int eh_base;
3053 };
3054
3055 struct ppc_stub_hash_entry
3056 {
3057 /* Base hash table entry structure. */
3058 struct bfd_hash_entry root;
3059
3060 struct ppc_stub_type type;
3061
3062 /* Group information. */
3063 struct map_stub *group;
3064
3065 /* Offset within stub_sec of the beginning of this stub. */
3066 bfd_vma stub_offset;
3067
3068 /* Given the symbol's value and its section we can determine its final
3069 value when building the stubs (so the stub knows where to jump. */
3070 bfd_vma target_value;
3071 asection *target_section;
3072
3073 /* The symbol table entry, if any, that this was derived from. */
3074 struct ppc_link_hash_entry *h;
3075 struct plt_entry *plt_ent;
3076
3077 /* Symbol type. */
3078 unsigned char symtype;
3079
3080 /* Symbol st_other. */
3081 unsigned char other;
3082 };
3083
3084 struct ppc_branch_hash_entry
3085 {
3086 /* Base hash table entry structure. */
3087 struct bfd_hash_entry root;
3088
3089 /* Offset within branch lookup table. */
3090 unsigned int offset;
3091
3092 /* Generation marker. */
3093 unsigned int iter;
3094 };
3095
3096 /* Used to track dynamic relocations. */
3097 struct ppc_dyn_relocs
3098 {
3099 struct ppc_dyn_relocs *next;
3100
3101 /* The input section of the reloc. */
3102 asection *sec;
3103
3104 /* Total number of relocs copied for the input section. */
3105 unsigned int count;
3106
3107 /* Number of pc-relative relocs copied for the input section. */
3108 unsigned int pc_count;
3109
3110 /* Number of relocs that might become R_PPC64_RELATIVE. */
3111 unsigned int rel_count;
3112 };
3113
3114 struct ppc_local_dyn_relocs
3115 {
3116 struct ppc_local_dyn_relocs *next;
3117
3118 /* The input section of the reloc. */
3119 asection *sec;
3120
3121 /* Total number of relocs copied for the input section. */
3122 unsigned int count;
3123
3124 /* Number of relocs that might become R_PPC64_RELATIVE. */
3125 unsigned int rel_count : 31;
3126
3127 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3128 unsigned int ifunc : 1;
3129 };
3130
3131 struct ppc_link_hash_entry
3132 {
3133 struct elf_link_hash_entry elf;
3134
3135 union
3136 {
3137 /* A pointer to the most recently used stub hash entry against this
3138 symbol. */
3139 struct ppc_stub_hash_entry *stub_cache;
3140
3141 /* A pointer to the next symbol starting with a '.' */
3142 struct ppc_link_hash_entry *next_dot_sym;
3143 } u;
3144
3145 /* Link between function code and descriptor symbols. */
3146 struct ppc_link_hash_entry *oh;
3147
3148 /* Flag function code and descriptor symbols. */
3149 unsigned int is_func:1;
3150 unsigned int is_func_descriptor:1;
3151 unsigned int fake:1;
3152
3153 /* Whether global opd/toc sym has been adjusted or not.
3154 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3155 should be set for all globals defined in any opd/toc section. */
3156 unsigned int adjust_done:1;
3157
3158 /* Set if this is an out-of-line register save/restore function,
3159 with non-standard calling convention. */
3160 unsigned int save_res:1;
3161
3162 /* Set if a duplicate symbol with non-zero localentry is detected,
3163 even when the duplicate symbol does not provide a definition. */
3164 unsigned int non_zero_localentry:1;
3165
3166 /* Contexts in which symbol is used in the GOT (or TOC).
3167 Bits are or'd into the mask as the corresponding relocs are
3168 encountered during check_relocs, with TLS_TLS being set when any
3169 of the other TLS bits are set. tls_optimize clears bits when
3170 optimizing to indicate the corresponding GOT entry type is not
3171 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3172 set TLS_GDIE when a GD reloc turns into an IE one.
3173 These flags are also kept for local symbols. */
3174 #define TLS_TLS 1 /* Any TLS reloc. */
3175 #define TLS_GD 2 /* GD reloc. */
3176 #define TLS_LD 4 /* LD reloc. */
3177 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3178 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3179 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3180 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3181 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3182 unsigned char tls_mask;
3183
3184 /* The above field is also used to mark function symbols. In which
3185 case TLS_TLS will be 0. */
3186 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3187 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3188 #define NON_GOT 256 /* local symbol plt, not stored. */
3189 };
3190
3191 static inline struct ppc_link_hash_entry *
3192 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3193 {
3194 return (struct ppc_link_hash_entry *) ent;
3195 }
3196
3197 static inline struct elf_link_hash_entry *
3198 elf_hash_entry (struct ppc_link_hash_entry *ent)
3199 {
3200 return (struct elf_link_hash_entry *) ent;
3201 }
3202
3203 /* ppc64 ELF linker hash table. */
3204
3205 struct ppc_link_hash_table
3206 {
3207 struct elf_link_hash_table elf;
3208
3209 /* The stub hash table. */
3210 struct bfd_hash_table stub_hash_table;
3211
3212 /* Another hash table for plt_branch stubs. */
3213 struct bfd_hash_table branch_hash_table;
3214
3215 /* Hash table for function prologue tocsave. */
3216 htab_t tocsave_htab;
3217
3218 /* Various options and other info passed from the linker. */
3219 struct ppc64_elf_params *params;
3220
3221 /* The size of sec_info below. */
3222 unsigned int sec_info_arr_size;
3223
3224 /* Per-section array of extra section info. Done this way rather
3225 than as part of ppc64_elf_section_data so we have the info for
3226 non-ppc64 sections. */
3227 struct
3228 {
3229 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3230 bfd_vma toc_off;
3231
3232 union
3233 {
3234 /* The section group that this section belongs to. */
3235 struct map_stub *group;
3236 /* A temp section list pointer. */
3237 asection *list;
3238 } u;
3239 } *sec_info;
3240
3241 /* Linked list of groups. */
3242 struct map_stub *group;
3243
3244 /* Temp used when calculating TOC pointers. */
3245 bfd_vma toc_curr;
3246 bfd *toc_bfd;
3247 asection *toc_first_sec;
3248
3249 /* Used when adding symbols. */
3250 struct ppc_link_hash_entry *dot_syms;
3251
3252 /* Shortcuts to get to dynamic linker sections. */
3253 asection *glink;
3254 asection *global_entry;
3255 asection *sfpr;
3256 asection *pltlocal;
3257 asection *relpltlocal;
3258 asection *brlt;
3259 asection *relbrlt;
3260 asection *glink_eh_frame;
3261
3262 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3263 struct ppc_link_hash_entry *tls_get_addr;
3264 struct ppc_link_hash_entry *tls_get_addr_fd;
3265 struct ppc_link_hash_entry *tga_desc;
3266 struct ppc_link_hash_entry *tga_desc_fd;
3267 struct map_stub *tga_group;
3268
3269 /* The size of reliplt used by got entry relocs. */
3270 bfd_size_type got_reli_size;
3271
3272 /* DT_RELR array of section/r_offset. */
3273 size_t relr_alloc;
3274 size_t relr_count;
3275 struct
3276 {
3277 asection *sec;
3278 bfd_vma off;
3279 } *relr;
3280
3281 /* Statistics. */
3282 unsigned long stub_count[ppc_stub_save_res];
3283
3284 /* Number of stubs against global syms. */
3285 unsigned long stub_globals;
3286
3287 /* Set if we're linking code with function descriptors. */
3288 unsigned int opd_abi:1;
3289
3290 /* Support for multiple toc sections. */
3291 unsigned int do_multi_toc:1;
3292 unsigned int multi_toc_needed:1;
3293 unsigned int second_toc_pass:1;
3294 unsigned int do_toc_opt:1;
3295
3296 /* Set if tls optimization is enabled. */
3297 unsigned int do_tls_opt:1;
3298
3299 /* Set if inline plt calls should be converted to direct calls. */
3300 unsigned int can_convert_all_inline_plt:1;
3301
3302 /* Set if a stub_offset changed. */
3303 unsigned int stub_changed:1;
3304
3305 /* Set on error. */
3306 unsigned int stub_error:1;
3307
3308 /* Whether func_desc_adjust needs to be run over symbols. */
3309 unsigned int need_func_desc_adj:1;
3310
3311 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3312 unsigned int has_plt_localentry0:1;
3313
3314 /* Whether calls are made via the PLT from NOTOC functions. */
3315 unsigned int notoc_plt:1;
3316
3317 /* Whether any code linked seems to be Power10. */
3318 unsigned int has_power10_relocs:1;
3319
3320 /* Incremented every time we size stubs. */
3321 unsigned int stub_iteration;
3322
3323 /* After 20 iterations of stub sizing we no longer allow stubs to
3324 shrink. This is to break out of a pathological case where adding
3325 stubs or increasing their size on one iteration decreases section
3326 gaps (perhaps due to alignment), which then results in smaller
3327 stubs on the next iteration. */
3328 #define STUB_SHRINK_ITER 20
3329 };
3330
3331 /* Rename some of the generic section flags to better document how they
3332 are used here. */
3333
3334 /* Nonzero if this section has TLS related relocations. */
3335 #define has_tls_reloc sec_flg0
3336
3337 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3338 relocations. */
3339 #define nomark_tls_get_addr sec_flg1
3340
3341 /* Nonzero if this section has any toc or got relocs. */
3342 #define has_toc_reloc sec_flg2
3343
3344 /* Nonzero if this section has a call to another section that uses
3345 the toc or got. */
3346 #define makes_toc_func_call sec_flg3
3347
3348 /* Recursion protection when determining above flag. */
3349 #define call_check_in_progress sec_flg4
3350 #define call_check_done sec_flg5
3351
3352 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3353
3354 #define ppc_hash_table(p) \
3355 ((is_elf_hash_table ((p)->hash) \
3356 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3357 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3358
3359 #define ppc_stub_hash_lookup(table, string, create, copy) \
3360 ((struct ppc_stub_hash_entry *) \
3361 bfd_hash_lookup ((table), (string), (create), (copy)))
3362
3363 #define ppc_branch_hash_lookup(table, string, create, copy) \
3364 ((struct ppc_branch_hash_entry *) \
3365 bfd_hash_lookup ((table), (string), (create), (copy)))
3366
3367 /* Create an entry in the stub hash table. */
3368
3369 static struct bfd_hash_entry *
3370 stub_hash_newfunc (struct bfd_hash_entry *entry,
3371 struct bfd_hash_table *table,
3372 const char *string)
3373 {
3374 /* Allocate the structure if it has not already been allocated by a
3375 subclass. */
3376 if (entry == NULL)
3377 {
3378 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3379 if (entry == NULL)
3380 return entry;
3381 }
3382
3383 /* Call the allocation method of the superclass. */
3384 entry = bfd_hash_newfunc (entry, table, string);
3385 if (entry != NULL)
3386 {
3387 struct ppc_stub_hash_entry *eh;
3388
3389 /* Initialize the local fields. */
3390 eh = (struct ppc_stub_hash_entry *) entry;
3391 eh->type.main = ppc_stub_none;
3392 eh->type.sub = ppc_stub_toc;
3393 eh->type.r2save = 0;
3394 eh->group = NULL;
3395 eh->stub_offset = 0;
3396 eh->target_value = 0;
3397 eh->target_section = NULL;
3398 eh->h = NULL;
3399 eh->plt_ent = NULL;
3400 eh->other = 0;
3401 }
3402
3403 return entry;
3404 }
3405
3406 /* Create an entry in the branch hash table. */
3407
3408 static struct bfd_hash_entry *
3409 branch_hash_newfunc (struct bfd_hash_entry *entry,
3410 struct bfd_hash_table *table,
3411 const char *string)
3412 {
3413 /* Allocate the structure if it has not already been allocated by a
3414 subclass. */
3415 if (entry == NULL)
3416 {
3417 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3418 if (entry == NULL)
3419 return entry;
3420 }
3421
3422 /* Call the allocation method of the superclass. */
3423 entry = bfd_hash_newfunc (entry, table, string);
3424 if (entry != NULL)
3425 {
3426 struct ppc_branch_hash_entry *eh;
3427
3428 /* Initialize the local fields. */
3429 eh = (struct ppc_branch_hash_entry *) entry;
3430 eh->offset = 0;
3431 eh->iter = 0;
3432 }
3433
3434 return entry;
3435 }
3436
3437 /* Create an entry in a ppc64 ELF linker hash table. */
3438
3439 static struct bfd_hash_entry *
3440 link_hash_newfunc (struct bfd_hash_entry *entry,
3441 struct bfd_hash_table *table,
3442 const char *string)
3443 {
3444 /* Allocate the structure if it has not already been allocated by a
3445 subclass. */
3446 if (entry == NULL)
3447 {
3448 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3449 if (entry == NULL)
3450 return entry;
3451 }
3452
3453 /* Call the allocation method of the superclass. */
3454 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3455 if (entry != NULL)
3456 {
3457 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3458
3459 memset (&eh->u.stub_cache, 0,
3460 (sizeof (struct ppc_link_hash_entry)
3461 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3462
3463 /* When making function calls, old ABI code references function entry
3464 points (dot symbols), while new ABI code references the function
3465 descriptor symbol. We need to make any combination of reference and
3466 definition work together, without breaking archive linking.
3467
3468 For a defined function "foo" and an undefined call to "bar":
3469 An old object defines "foo" and ".foo", references ".bar" (possibly
3470 "bar" too).
3471 A new object defines "foo" and references "bar".
3472
3473 A new object thus has no problem with its undefined symbols being
3474 satisfied by definitions in an old object. On the other hand, the
3475 old object won't have ".bar" satisfied by a new object.
3476
3477 Keep a list of newly added dot-symbols. */
3478
3479 if (string[0] == '.')
3480 {
3481 struct ppc_link_hash_table *htab;
3482
3483 htab = (struct ppc_link_hash_table *) table;
3484 eh->u.next_dot_sym = htab->dot_syms;
3485 htab->dot_syms = eh;
3486 }
3487 }
3488
3489 return entry;
3490 }
3491
3492 struct tocsave_entry
3493 {
3494 asection *sec;
3495 bfd_vma offset;
3496 };
3497
3498 static hashval_t
3499 tocsave_htab_hash (const void *p)
3500 {
3501 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3502 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3503 }
3504
3505 static int
3506 tocsave_htab_eq (const void *p1, const void *p2)
3507 {
3508 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3509 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3510 return e1->sec == e2->sec && e1->offset == e2->offset;
3511 }
3512
3513 /* Destroy a ppc64 ELF linker hash table. */
3514
3515 static void
3516 ppc64_elf_link_hash_table_free (bfd *obfd)
3517 {
3518 struct ppc_link_hash_table *htab;
3519
3520 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3521 if (htab->tocsave_htab)
3522 htab_delete (htab->tocsave_htab);
3523 bfd_hash_table_free (&htab->branch_hash_table);
3524 bfd_hash_table_free (&htab->stub_hash_table);
3525 _bfd_elf_link_hash_table_free (obfd);
3526 }
3527
3528 /* Create a ppc64 ELF linker hash table. */
3529
3530 static struct bfd_link_hash_table *
3531 ppc64_elf_link_hash_table_create (bfd *abfd)
3532 {
3533 struct ppc_link_hash_table *htab;
3534 size_t amt = sizeof (struct ppc_link_hash_table);
3535
3536 htab = bfd_zmalloc (amt);
3537 if (htab == NULL)
3538 return NULL;
3539
3540 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3541 sizeof (struct ppc_link_hash_entry),
3542 PPC64_ELF_DATA))
3543 {
3544 free (htab);
3545 return NULL;
3546 }
3547
3548 /* Init the stub hash table too. */
3549 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3550 sizeof (struct ppc_stub_hash_entry)))
3551 {
3552 _bfd_elf_link_hash_table_free (abfd);
3553 return NULL;
3554 }
3555
3556 /* And the branch hash table. */
3557 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3558 sizeof (struct ppc_branch_hash_entry)))
3559 {
3560 bfd_hash_table_free (&htab->stub_hash_table);
3561 _bfd_elf_link_hash_table_free (abfd);
3562 return NULL;
3563 }
3564
3565 htab->tocsave_htab = htab_try_create (1024,
3566 tocsave_htab_hash,
3567 tocsave_htab_eq,
3568 NULL);
3569 if (htab->tocsave_htab == NULL)
3570 {
3571 ppc64_elf_link_hash_table_free (abfd);
3572 return NULL;
3573 }
3574 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3575
3576 /* Initializing two fields of the union is just cosmetic. We really
3577 only care about glist, but when compiled on a 32-bit host the
3578 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3579 debugger inspection of these fields look nicer. */
3580 htab->elf.init_got_refcount.refcount = 0;
3581 htab->elf.init_got_refcount.glist = NULL;
3582 htab->elf.init_plt_refcount.refcount = 0;
3583 htab->elf.init_plt_refcount.glist = NULL;
3584 htab->elf.init_got_offset.offset = 0;
3585 htab->elf.init_got_offset.glist = NULL;
3586 htab->elf.init_plt_offset.offset = 0;
3587 htab->elf.init_plt_offset.glist = NULL;
3588
3589 return &htab->elf.root;
3590 }
3591
3592 /* Create sections for linker generated code. */
3593
3594 static bool
3595 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3596 {
3597 struct ppc_link_hash_table *htab;
3598 flagword flags;
3599
3600 htab = ppc_hash_table (info);
3601
3602 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3603 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3604 if (htab->params->save_restore_funcs)
3605 {
3606 /* Create .sfpr for code to save and restore fp regs. */
3607 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3608 flags);
3609 if (htab->sfpr == NULL
3610 || !bfd_set_section_alignment (htab->sfpr, 2))
3611 return false;
3612 }
3613
3614 if (bfd_link_relocatable (info))
3615 return true;
3616
3617 /* Create .glink for lazy dynamic linking support. */
3618 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3619 flags);
3620 if (htab->glink == NULL
3621 || !bfd_set_section_alignment (htab->glink, 3))
3622 return false;
3623
3624 /* The part of .glink used by global entry stubs, separate so that
3625 it can be aligned appropriately without affecting htab->glink. */
3626 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3627 flags);
3628 if (htab->global_entry == NULL
3629 || !bfd_set_section_alignment (htab->global_entry, 2))
3630 return false;
3631
3632 if (!info->no_ld_generated_unwind_info)
3633 {
3634 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3635 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3636 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3637 ".eh_frame",
3638 flags);
3639 if (htab->glink_eh_frame == NULL
3640 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3641 return false;
3642 }
3643
3644 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3645 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3646 if (htab->elf.iplt == NULL
3647 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3648 return false;
3649
3650 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3651 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3652 htab->elf.irelplt
3653 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3654 if (htab->elf.irelplt == NULL
3655 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3656 return false;
3657
3658 /* Create branch lookup table for plt_branch stubs. */
3659 flags = (SEC_ALLOC | SEC_LOAD
3660 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3661 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3662 flags);
3663 if (htab->brlt == NULL
3664 || !bfd_set_section_alignment (htab->brlt, 3))
3665 return false;
3666
3667 /* Local plt entries, put in .branch_lt but a separate section for
3668 convenience. */
3669 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3670 flags);
3671 if (htab->pltlocal == NULL
3672 || !bfd_set_section_alignment (htab->pltlocal, 3))
3673 return false;
3674
3675 if (!bfd_link_pic (info))
3676 return true;
3677
3678 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3679 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3680 htab->relbrlt
3681 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3682 if (htab->relbrlt == NULL
3683 || !bfd_set_section_alignment (htab->relbrlt, 3))
3684 return false;
3685
3686 htab->relpltlocal
3687 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3688 if (htab->relpltlocal == NULL
3689 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3690 return false;
3691
3692 return true;
3693 }
3694
3695 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3696
3697 bool
3698 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3699 struct ppc64_elf_params *params)
3700 {
3701 struct ppc_link_hash_table *htab;
3702
3703 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3704
3705 /* Always hook our dynamic sections into the first bfd, which is the
3706 linker created stub bfd. This ensures that the GOT header is at
3707 the start of the output TOC section. */
3708 htab = ppc_hash_table (info);
3709 htab->elf.dynobj = params->stub_bfd;
3710 htab->params = params;
3711
3712 return create_linkage_sections (htab->elf.dynobj, info);
3713 }
3714
3715 /* Build a name for an entry in the stub hash table. */
3716
3717 static char *
3718 ppc_stub_name (const asection *input_section,
3719 const asection *sym_sec,
3720 const struct ppc_link_hash_entry *h,
3721 const Elf_Internal_Rela *rel)
3722 {
3723 char *stub_name;
3724 ssize_t len;
3725
3726 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3727 offsets from a sym as a branch target? In fact, we could
3728 probably assume the addend is always zero. */
3729 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3730
3731 if (h)
3732 {
3733 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3734 stub_name = bfd_malloc (len);
3735 if (stub_name == NULL)
3736 return stub_name;
3737
3738 len = sprintf (stub_name, "%08x.%s+%x",
3739 input_section->id & 0xffffffff,
3740 h->elf.root.root.string,
3741 (int) rel->r_addend & 0xffffffff);
3742 }
3743 else
3744 {
3745 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3746 stub_name = bfd_malloc (len);
3747 if (stub_name == NULL)
3748 return stub_name;
3749
3750 len = sprintf (stub_name, "%08x.%x:%x+%x",
3751 input_section->id & 0xffffffff,
3752 sym_sec->id & 0xffffffff,
3753 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3754 (int) rel->r_addend & 0xffffffff);
3755 }
3756 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3757 stub_name[len - 2] = 0;
3758 return stub_name;
3759 }
3760
3761 /* If mixing power10 with non-power10 code and --power10-stubs is not
3762 specified (or is auto) then there may be multiple stub types for any
3763 given symbol. Up to three classes of stubs are stored in separate
3764 stub_hash_table entries having the same key string. The entries
3765 will always be adjacent on entry->root.next chain, even if hash
3766 table resizing occurs. This function selects the correct entry to
3767 use. */
3768
3769 static struct ppc_stub_hash_entry *
3770 select_alt_stub (struct ppc_stub_hash_entry *entry,
3771 enum elf_ppc64_reloc_type r_type)
3772 {
3773 enum ppc_stub_sub_type subt;
3774
3775 switch (r_type)
3776 {
3777 case R_PPC64_REL24_NOTOC:
3778 subt = ppc_stub_notoc;
3779 break;
3780 case R_PPC64_REL24_P9NOTOC:
3781 subt = ppc_stub_p9notoc;
3782 break;
3783 default:
3784 subt = ppc_stub_toc;
3785 break;
3786 }
3787
3788 while (entry != NULL && entry->type.sub != subt)
3789 {
3790 const char *stub_name = entry->root.string;
3791
3792 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3793 if (entry != NULL
3794 && entry->root.string != stub_name)
3795 entry = NULL;
3796 }
3797
3798 return entry;
3799 }
3800
3801 /* Look up an entry in the stub hash. Stub entries are cached because
3802 creating the stub name takes a bit of time. */
3803
3804 static struct ppc_stub_hash_entry *
3805 ppc_get_stub_entry (const asection *input_section,
3806 const asection *sym_sec,
3807 struct ppc_link_hash_entry *h,
3808 const Elf_Internal_Rela *rel,
3809 struct ppc_link_hash_table *htab)
3810 {
3811 struct ppc_stub_hash_entry *stub_entry;
3812 struct map_stub *group;
3813
3814 /* If this input section is part of a group of sections sharing one
3815 stub section, then use the id of the first section in the group.
3816 Stub names need to include a section id, as there may well be
3817 more than one stub used to reach say, printf, and we need to
3818 distinguish between them. */
3819 group = htab->sec_info[input_section->id].u.group;
3820 if (group == NULL)
3821 return NULL;
3822
3823 if (h != NULL && h->u.stub_cache != NULL
3824 && h->u.stub_cache->h == h
3825 && h->u.stub_cache->group == group)
3826 {
3827 stub_entry = h->u.stub_cache;
3828 }
3829 else
3830 {
3831 char *stub_name;
3832
3833 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3834 if (stub_name == NULL)
3835 return NULL;
3836
3837 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3838 stub_name, false, false);
3839 if (h != NULL)
3840 h->u.stub_cache = stub_entry;
3841
3842 free (stub_name);
3843 }
3844
3845 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3846 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3847
3848 return stub_entry;
3849 }
3850
3851 /* Add a new stub entry to the stub hash. Not all fields of the new
3852 stub entry are initialised. */
3853
3854 static struct ppc_stub_hash_entry *
3855 ppc_add_stub (const char *stub_name,
3856 asection *section,
3857 struct bfd_link_info *info)
3858 {
3859 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3860 struct map_stub *group;
3861 asection *link_sec;
3862 asection *stub_sec;
3863 struct ppc_stub_hash_entry *stub_entry;
3864
3865 group = htab->sec_info[section->id].u.group;
3866 link_sec = group->link_sec;
3867 stub_sec = group->stub_sec;
3868 if (stub_sec == NULL)
3869 {
3870 size_t namelen;
3871 bfd_size_type len;
3872 char *s_name;
3873
3874 namelen = strlen (link_sec->name);
3875 len = namelen + sizeof (STUB_SUFFIX);
3876 s_name = bfd_alloc (htab->params->stub_bfd, len);
3877 if (s_name == NULL)
3878 return NULL;
3879
3880 memcpy (s_name, link_sec->name, namelen);
3881 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3882 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3883 if (stub_sec == NULL)
3884 return NULL;
3885 group->stub_sec = stub_sec;
3886 }
3887
3888 /* Enter this entry into the linker stub hash table. */
3889 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3890 true, false);
3891 if (stub_entry == NULL)
3892 {
3893 /* xgettext:c-format */
3894 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3895 section->owner, stub_name);
3896 return NULL;
3897 }
3898
3899 stub_entry->group = group;
3900 stub_entry->stub_offset = 0;
3901 return stub_entry;
3902 }
3903
3904 /* A stub has already been created, but it may not be the required
3905 type. We shouldn't be transitioning from plt_call to long_branch
3906 stubs or vice versa, but we might be upgrading from plt_call to
3907 plt_call with r2save for example. */
3908
3909 static bool
3910 ppc_merge_stub (struct ppc_link_hash_table *htab,
3911 struct ppc_stub_hash_entry *stub_entry,
3912 struct ppc_stub_type stub_type,
3913 enum elf_ppc64_reloc_type r_type)
3914 {
3915 struct ppc_stub_type old_type = stub_entry->type;
3916
3917 if (old_type.main == ppc_stub_save_res)
3918 return true;
3919
3920 if (htab->params->power10_stubs == -1)
3921 {
3922 /* For --power10-stubs=auto, don't merge _notoc and other
3923 varieties of stubs. */
3924 struct ppc_stub_hash_entry *alt_stub;
3925
3926 alt_stub = select_alt_stub (stub_entry, r_type);
3927 if (alt_stub == NULL)
3928 {
3929 alt_stub = ((struct ppc_stub_hash_entry *)
3930 stub_hash_newfunc (NULL,
3931 &htab->stub_hash_table,
3932 stub_entry->root.string));
3933 if (alt_stub == NULL)
3934 return false;
3935
3936 *alt_stub = *stub_entry;
3937 stub_entry->root.next = &alt_stub->root;
3938
3939 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3940 Not that it matters, this just puts smaller stubs first. */
3941 if (stub_type.sub == ppc_stub_notoc)
3942 alt_stub = stub_entry;
3943 else if (stub_type.sub == ppc_stub_p9notoc
3944 && alt_stub->root.next
3945 && alt_stub->root.next->string == alt_stub->root.string)
3946 {
3947 struct ppc_stub_hash_entry *next
3948 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3949 alt_stub->type = next->type;
3950 alt_stub = next;
3951 }
3952 alt_stub->type = stub_type;
3953 return true;
3954 }
3955 stub_entry = alt_stub;
3956 }
3957
3958 old_type = stub_entry->type;
3959 if (old_type.main == ppc_stub_plt_branch)
3960 old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
3961
3962 if (old_type.main != stub_type.main
3963 || (old_type.sub != stub_type.sub
3964 && old_type.sub != ppc_stub_toc
3965 && stub_type.sub != ppc_stub_toc))
3966 abort ();
3967
3968 stub_entry->type.sub |= stub_type.sub;
3969 stub_entry->type.r2save |= stub_type.r2save;
3970 return true;
3971 }
3972
3973 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3974 not already done. */
3975
3976 static bool
3977 create_got_section (bfd *abfd, struct bfd_link_info *info)
3978 {
3979 asection *got, *relgot;
3980 flagword flags;
3981 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3982
3983 if (!is_ppc64_elf (abfd))
3984 return false;
3985 if (htab == NULL)
3986 return false;
3987
3988 if (!htab->elf.sgot
3989 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3990 return false;
3991
3992 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3993 | SEC_LINKER_CREATED);
3994
3995 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3996 if (!got
3997 || !bfd_set_section_alignment (got, 3))
3998 return false;
3999
4000 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
4001 flags | SEC_READONLY);
4002 if (!relgot
4003 || !bfd_set_section_alignment (relgot, 3))
4004 return false;
4005
4006 ppc64_elf_tdata (abfd)->got = got;
4007 ppc64_elf_tdata (abfd)->relgot = relgot;
4008 return true;
4009 }
4010
4011 /* Follow indirect and warning symbol links. */
4012
4013 static inline struct bfd_link_hash_entry *
4014 follow_link (struct bfd_link_hash_entry *h)
4015 {
4016 while (h->type == bfd_link_hash_indirect
4017 || h->type == bfd_link_hash_warning)
4018 h = h->u.i.link;
4019 return h;
4020 }
4021
4022 static inline struct elf_link_hash_entry *
4023 elf_follow_link (struct elf_link_hash_entry *h)
4024 {
4025 return (struct elf_link_hash_entry *) follow_link (&h->root);
4026 }
4027
4028 static inline struct ppc_link_hash_entry *
4029 ppc_follow_link (struct ppc_link_hash_entry *h)
4030 {
4031 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
4032 }
4033
4034 /* Merge PLT info on FROM with that on TO. */
4035
4036 static void
4037 move_plt_plist (struct ppc_link_hash_entry *from,
4038 struct ppc_link_hash_entry *to)
4039 {
4040 if (from->elf.plt.plist != NULL)
4041 {
4042 if (to->elf.plt.plist != NULL)
4043 {
4044 struct plt_entry **entp;
4045 struct plt_entry *ent;
4046
4047 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4048 {
4049 struct plt_entry *dent;
4050
4051 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4052 if (dent->addend == ent->addend)
4053 {
4054 dent->plt.refcount += ent->plt.refcount;
4055 *entp = ent->next;
4056 break;
4057 }
4058 if (dent == NULL)
4059 entp = &ent->next;
4060 }
4061 *entp = to->elf.plt.plist;
4062 }
4063
4064 to->elf.plt.plist = from->elf.plt.plist;
4065 from->elf.plt.plist = NULL;
4066 }
4067 }
4068
4069 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4070
4071 static void
4072 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4073 struct elf_link_hash_entry *dir,
4074 struct elf_link_hash_entry *ind)
4075 {
4076 struct ppc_link_hash_entry *edir, *eind;
4077
4078 edir = ppc_elf_hash_entry (dir);
4079 eind = ppc_elf_hash_entry (ind);
4080
4081 edir->is_func |= eind->is_func;
4082 edir->is_func_descriptor |= eind->is_func_descriptor;
4083 edir->tls_mask |= eind->tls_mask;
4084 if (eind->oh != NULL)
4085 edir->oh = ppc_follow_link (eind->oh);
4086
4087 if (edir->elf.versioned != versioned_hidden)
4088 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4089 edir->elf.ref_regular |= eind->elf.ref_regular;
4090 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4091 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4092 edir->elf.needs_plt |= eind->elf.needs_plt;
4093 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4094
4095 /* If we were called to copy over info for a weak sym, don't copy
4096 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4097 in order to simplify readonly_dynrelocs and save a field in the
4098 symbol hash entry, but that means dyn_relocs can't be used in any
4099 tests about a specific symbol, or affect other symbol flags which
4100 are then tested. */
4101 if (eind->elf.root.type != bfd_link_hash_indirect)
4102 return;
4103
4104 /* Copy over any dynamic relocs we may have on the indirect sym. */
4105 if (ind->dyn_relocs != NULL)
4106 {
4107 if (dir->dyn_relocs != NULL)
4108 {
4109 struct ppc_dyn_relocs **pp;
4110 struct ppc_dyn_relocs *p;
4111
4112 /* Add reloc counts against the indirect sym to the direct sym
4113 list. Merge any entries against the same section. */
4114 for (pp = (struct ppc_dyn_relocs **) &ind->dyn_relocs;
4115 (p = *pp) != NULL;
4116 )
4117 {
4118 struct ppc_dyn_relocs *q;
4119
4120 for (q = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4121 q != NULL;
4122 q = q->next)
4123 if (q->sec == p->sec)
4124 {
4125 q->count += p->count;
4126 q->pc_count += p->pc_count;
4127 q->rel_count += p->rel_count;
4128 *pp = p->next;
4129 break;
4130 }
4131 if (q == NULL)
4132 pp = &p->next;
4133 }
4134 *pp = (struct ppc_dyn_relocs *) dir->dyn_relocs;
4135 }
4136
4137 dir->dyn_relocs = ind->dyn_relocs;
4138 ind->dyn_relocs = NULL;
4139 }
4140
4141 /* Copy over got entries that we may have already seen to the
4142 symbol which just became indirect. */
4143 if (eind->elf.got.glist != NULL)
4144 {
4145 if (edir->elf.got.glist != NULL)
4146 {
4147 struct got_entry **entp;
4148 struct got_entry *ent;
4149
4150 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4151 {
4152 struct got_entry *dent;
4153
4154 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4155 if (dent->addend == ent->addend
4156 && dent->owner == ent->owner
4157 && dent->tls_type == ent->tls_type)
4158 {
4159 dent->got.refcount += ent->got.refcount;
4160 *entp = ent->next;
4161 break;
4162 }
4163 if (dent == NULL)
4164 entp = &ent->next;
4165 }
4166 *entp = edir->elf.got.glist;
4167 }
4168
4169 edir->elf.got.glist = eind->elf.got.glist;
4170 eind->elf.got.glist = NULL;
4171 }
4172
4173 /* And plt entries. */
4174 move_plt_plist (eind, edir);
4175
4176 if (eind->elf.dynindx != -1)
4177 {
4178 if (edir->elf.dynindx != -1)
4179 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4180 edir->elf.dynstr_index);
4181 edir->elf.dynindx = eind->elf.dynindx;
4182 edir->elf.dynstr_index = eind->elf.dynstr_index;
4183 eind->elf.dynindx = -1;
4184 eind->elf.dynstr_index = 0;
4185 }
4186 }
4187
4188 /* Find the function descriptor hash entry from the given function code
4189 hash entry FH. Link the entries via their OH fields. */
4190
4191 static struct ppc_link_hash_entry *
4192 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4193 {
4194 struct ppc_link_hash_entry *fdh = fh->oh;
4195
4196 if (fdh == NULL)
4197 {
4198 const char *fd_name = fh->elf.root.root.string + 1;
4199
4200 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4201 false, false, false));
4202 if (fdh == NULL)
4203 return fdh;
4204
4205 fdh->is_func_descriptor = 1;
4206 fdh->oh = fh;
4207 fh->is_func = 1;
4208 fh->oh = fdh;
4209 }
4210
4211 fdh = ppc_follow_link (fdh);
4212 fdh->is_func_descriptor = 1;
4213 fdh->oh = fh;
4214 return fdh;
4215 }
4216
4217 /* Make a fake function descriptor sym for the undefined code sym FH. */
4218
4219 static struct ppc_link_hash_entry *
4220 make_fdh (struct bfd_link_info *info,
4221 struct ppc_link_hash_entry *fh)
4222 {
4223 bfd *abfd = fh->elf.root.u.undef.abfd;
4224 struct bfd_link_hash_entry *bh = NULL;
4225 struct ppc_link_hash_entry *fdh;
4226 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4227 ? BSF_WEAK
4228 : BSF_GLOBAL);
4229
4230 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4231 fh->elf.root.root.string + 1,
4232 flags, bfd_und_section_ptr, 0,
4233 NULL, false, false, &bh))
4234 return NULL;
4235
4236 fdh = (struct ppc_link_hash_entry *) bh;
4237 fdh->elf.non_elf = 0;
4238 fdh->fake = 1;
4239 fdh->is_func_descriptor = 1;
4240 fdh->oh = fh;
4241 fh->is_func = 1;
4242 fh->oh = fdh;
4243 return fdh;
4244 }
4245
4246 /* Fix function descriptor symbols defined in .opd sections to be
4247 function type. */
4248
4249 static bool
4250 ppc64_elf_add_symbol_hook (bfd *ibfd,
4251 struct bfd_link_info *info,
4252 Elf_Internal_Sym *isym,
4253 const char **name,
4254 flagword *flags ATTRIBUTE_UNUSED,
4255 asection **sec,
4256 bfd_vma *value)
4257 {
4258 if (*sec != NULL
4259 && strcmp ((*sec)->name, ".opd") == 0)
4260 {
4261 asection *code_sec;
4262
4263 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4264 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4265 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4266
4267 /* If the symbol is a function defined in .opd, and the function
4268 code is in a discarded group, let it appear to be undefined. */
4269 if (!bfd_link_relocatable (info)
4270 && (*sec)->reloc_count != 0
4271 && opd_entry_value (*sec, *value, &code_sec, NULL,
4272 false) != (bfd_vma) -1
4273 && discarded_section (code_sec))
4274 {
4275 *sec = bfd_und_section_ptr;
4276 isym->st_shndx = SHN_UNDEF;
4277 }
4278 }
4279 else if (*sec != NULL
4280 && strcmp ((*sec)->name, ".toc") == 0
4281 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4282 {
4283 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4284 if (htab != NULL)
4285 htab->params->object_in_toc = 1;
4286 }
4287
4288 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4289 {
4290 if (abiversion (ibfd) == 0)
4291 set_abiversion (ibfd, 2);
4292 else if (abiversion (ibfd) == 1)
4293 {
4294 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4295 " for ABI version 1"), *name);
4296 bfd_set_error (bfd_error_bad_value);
4297 return false;
4298 }
4299 }
4300
4301 return true;
4302 }
4303
4304 /* Merge non-visibility st_other attributes: local entry point. */
4305
4306 static void
4307 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4308 unsigned int st_other,
4309 bool definition,
4310 bool dynamic)
4311 {
4312 if (definition && (!dynamic || !h->def_regular))
4313 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4314 | ELF_ST_VISIBILITY (h->other));
4315 }
4316
4317 /* Hook called on merging a symbol. We use this to clear "fake" since
4318 we now have a real symbol. */
4319
4320 static bool
4321 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4322 const Elf_Internal_Sym *isym,
4323 asection **psec ATTRIBUTE_UNUSED,
4324 bool newdef ATTRIBUTE_UNUSED,
4325 bool olddef ATTRIBUTE_UNUSED,
4326 bfd *oldbfd ATTRIBUTE_UNUSED,
4327 const asection *oldsec ATTRIBUTE_UNUSED)
4328 {
4329 ppc_elf_hash_entry (h)->fake = 0;
4330 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4331 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4332 return true;
4333 }
4334
4335 /* This function makes an old ABI object reference to ".bar" cause the
4336 inclusion of a new ABI object archive that defines "bar".
4337 NAME is a symbol defined in an archive. Return a symbol in the hash
4338 table that might be satisfied by the archive symbols. */
4339
4340 static struct bfd_link_hash_entry *
4341 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4342 struct bfd_link_info *info,
4343 const char *name)
4344 {
4345 struct bfd_link_hash_entry *h;
4346 char *dot_name;
4347 size_t len;
4348
4349 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4350 if (h != NULL
4351 && ppc_hash_table (info) != NULL
4352 /* Don't return this sym if it is a fake function descriptor
4353 created by add_symbol_adjust. */
4354 && !((struct ppc_link_hash_entry *) h)->fake)
4355 return h;
4356
4357 if (name[0] == '.')
4358 return h;
4359
4360 len = strlen (name);
4361 dot_name = bfd_alloc (abfd, len + 2);
4362 if (dot_name == NULL)
4363 return (struct bfd_link_hash_entry *) -1;
4364 dot_name[0] = '.';
4365 memcpy (dot_name + 1, name, len + 1);
4366 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4367 bfd_release (abfd, dot_name);
4368 if (h != NULL)
4369 return h;
4370
4371 if (strcmp (name, "__tls_get_addr_opt") == 0)
4372 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4373 return h;
4374 }
4375
4376 /* This function satisfies all old ABI object references to ".bar" if a
4377 new ABI object defines "bar". Well, at least, undefined dot symbols
4378 are made weak. This stops later archive searches from including an
4379 object if we already have a function descriptor definition. It also
4380 prevents the linker complaining about undefined symbols.
4381 We also check and correct mismatched symbol visibility here. The
4382 most restrictive visibility of the function descriptor and the
4383 function entry symbol is used. */
4384
4385 static bool
4386 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4387 {
4388 struct ppc_link_hash_table *htab;
4389 struct ppc_link_hash_entry *fdh;
4390
4391 if (eh->elf.root.type == bfd_link_hash_warning)
4392 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4393
4394 if (eh->elf.root.type == bfd_link_hash_indirect)
4395 return true;
4396
4397 if (eh->elf.root.root.string[0] != '.')
4398 abort ();
4399
4400 htab = ppc_hash_table (info);
4401 if (htab == NULL)
4402 return false;
4403
4404 fdh = lookup_fdh (eh, htab);
4405 if (fdh == NULL
4406 && !bfd_link_relocatable (info)
4407 && (eh->elf.root.type == bfd_link_hash_undefined
4408 || eh->elf.root.type == bfd_link_hash_undefweak)
4409 && eh->elf.ref_regular)
4410 {
4411 /* Make an undefined function descriptor sym, in order to
4412 pull in an --as-needed shared lib. Archives are handled
4413 elsewhere. */
4414 fdh = make_fdh (info, eh);
4415 if (fdh == NULL)
4416 return false;
4417 }
4418
4419 if (fdh != NULL)
4420 {
4421 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4422 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4423
4424 /* Make both descriptor and entry symbol have the most
4425 constraining visibility of either symbol. */
4426 if (entry_vis < descr_vis)
4427 fdh->elf.other += entry_vis - descr_vis;
4428 else if (entry_vis > descr_vis)
4429 eh->elf.other += descr_vis - entry_vis;
4430
4431 /* Propagate reference flags from entry symbol to function
4432 descriptor symbol. */
4433 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4434 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4435 fdh->elf.ref_regular |= eh->elf.ref_regular;
4436 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4437
4438 if (!fdh->elf.forced_local
4439 && fdh->elf.dynindx == -1
4440 && fdh->elf.versioned != versioned_hidden
4441 && (bfd_link_dll (info)
4442 || fdh->elf.def_dynamic
4443 || fdh->elf.ref_dynamic)
4444 && (eh->elf.ref_regular
4445 || eh->elf.def_regular))
4446 {
4447 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4448 return false;
4449 }
4450 }
4451
4452 return true;
4453 }
4454
4455 /* Set up opd section info and abiversion for IBFD, and process list
4456 of dot-symbols we made in link_hash_newfunc. */
4457
4458 static bool
4459 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4460 {
4461 struct ppc_link_hash_table *htab;
4462 struct ppc_link_hash_entry **p, *eh;
4463 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4464
4465 if (opd != NULL && opd->size != 0)
4466 {
4467 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4468 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4469
4470 if (abiversion (ibfd) == 0)
4471 set_abiversion (ibfd, 1);
4472 else if (abiversion (ibfd) >= 2)
4473 {
4474 /* xgettext:c-format */
4475 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4476 ibfd, abiversion (ibfd));
4477 bfd_set_error (bfd_error_bad_value);
4478 return false;
4479 }
4480 }
4481
4482 if (is_ppc64_elf (info->output_bfd))
4483 {
4484 /* For input files without an explicit abiversion in e_flags
4485 we should have flagged any with symbol st_other bits set
4486 as ELFv1 and above flagged those with .opd as ELFv2.
4487 Set the output abiversion if not yet set, and for any input
4488 still ambiguous, take its abiversion from the output.
4489 Differences in ABI are reported later. */
4490 if (abiversion (info->output_bfd) == 0)
4491 set_abiversion (info->output_bfd, abiversion (ibfd));
4492 else if (abiversion (ibfd) == 0)
4493 set_abiversion (ibfd, abiversion (info->output_bfd));
4494 }
4495
4496 htab = ppc_hash_table (info);
4497 if (htab == NULL)
4498 return true;
4499
4500 if (opd != NULL && opd->size != 0
4501 && (ibfd->flags & DYNAMIC) == 0
4502 && (opd->flags & SEC_RELOC) != 0
4503 && opd->reloc_count != 0
4504 && !bfd_is_abs_section (opd->output_section)
4505 && info->gc_sections)
4506 {
4507 /* Garbage collection needs some extra help with .opd sections.
4508 We don't want to necessarily keep everything referenced by
4509 relocs in .opd, as that would keep all functions. Instead,
4510 if we reference an .opd symbol (a function descriptor), we
4511 want to keep the function code symbol's section. This is
4512 easy for global symbols, but for local syms we need to keep
4513 information about the associated function section. */
4514 bfd_size_type amt;
4515 asection **opd_sym_map;
4516 Elf_Internal_Shdr *symtab_hdr;
4517 Elf_Internal_Rela *relocs, *rel_end, *rel;
4518
4519 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4520 opd_sym_map = bfd_zalloc (ibfd, amt);
4521 if (opd_sym_map == NULL)
4522 return false;
4523 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4524 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4525 info->keep_memory);
4526 if (relocs == NULL)
4527 return false;
4528 symtab_hdr = &elf_symtab_hdr (ibfd);
4529 rel_end = relocs + opd->reloc_count - 1;
4530 for (rel = relocs; rel < rel_end; rel++)
4531 {
4532 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4533 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4534
4535 if (r_type == R_PPC64_ADDR64
4536 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4537 && r_symndx < symtab_hdr->sh_info)
4538 {
4539 Elf_Internal_Sym *isym;
4540 asection *s;
4541
4542 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4543 r_symndx);
4544 if (isym == NULL)
4545 {
4546 if (elf_section_data (opd)->relocs != relocs)
4547 free (relocs);
4548 return false;
4549 }
4550
4551 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4552 if (s != NULL && s != opd)
4553 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4554 }
4555 }
4556 if (elf_section_data (opd)->relocs != relocs)
4557 free (relocs);
4558 }
4559
4560 p = &htab->dot_syms;
4561 while ((eh = *p) != NULL)
4562 {
4563 *p = NULL;
4564 if (&eh->elf == htab->elf.hgot)
4565 ;
4566 else if (htab->elf.hgot == NULL
4567 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4568 htab->elf.hgot = &eh->elf;
4569 else if (abiversion (ibfd) <= 1)
4570 {
4571 htab->need_func_desc_adj = 1;
4572 if (!add_symbol_adjust (eh, info))
4573 return false;
4574 }
4575 p = &eh->u.next_dot_sym;
4576 }
4577 return true;
4578 }
4579
4580 /* Undo hash table changes when an --as-needed input file is determined
4581 not to be needed. */
4582
4583 static bool
4584 ppc64_elf_notice_as_needed (bfd *ibfd,
4585 struct bfd_link_info *info,
4586 enum notice_asneeded_action act)
4587 {
4588 if (act == notice_not_needed)
4589 {
4590 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4591
4592 if (htab == NULL)
4593 return false;
4594
4595 htab->dot_syms = NULL;
4596 }
4597 return _bfd_elf_notice_as_needed (ibfd, info, act);
4598 }
4599
4600 /* If --just-symbols against a final linked binary, then assume we need
4601 toc adjusting stubs when calling functions defined there. */
4602
4603 static void
4604 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4605 {
4606 if ((sec->flags & SEC_CODE) != 0
4607 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4608 && is_ppc64_elf (sec->owner))
4609 {
4610 if (abiversion (sec->owner) >= 2
4611 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4612 sec->has_toc_reloc = 1;
4613 }
4614 _bfd_elf_link_just_syms (sec, info);
4615 }
4616
4617 static struct plt_entry **
4618 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4619 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4620 {
4621 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4622 struct plt_entry **local_plt;
4623 unsigned char *local_got_tls_masks;
4624
4625 if (local_got_ents == NULL)
4626 {
4627 bfd_size_type size = symtab_hdr->sh_info;
4628
4629 size *= (sizeof (*local_got_ents)
4630 + sizeof (*local_plt)
4631 + sizeof (*local_got_tls_masks));
4632 local_got_ents = bfd_zalloc (abfd, size);
4633 if (local_got_ents == NULL)
4634 return NULL;
4635 elf_local_got_ents (abfd) = local_got_ents;
4636 }
4637
4638 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4639 {
4640 struct got_entry *ent;
4641
4642 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4643 if (ent->addend == r_addend
4644 && ent->owner == abfd
4645 && ent->tls_type == tls_type)
4646 break;
4647 if (ent == NULL)
4648 {
4649 size_t amt = sizeof (*ent);
4650 ent = bfd_alloc (abfd, amt);
4651 if (ent == NULL)
4652 return false;
4653 ent->next = local_got_ents[r_symndx];
4654 ent->addend = r_addend;
4655 ent->owner = abfd;
4656 ent->tls_type = tls_type;
4657 ent->is_indirect = false;
4658 ent->got.refcount = 0;
4659 local_got_ents[r_symndx] = ent;
4660 }
4661 ent->got.refcount += 1;
4662 }
4663
4664 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4665 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4666 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4667
4668 return local_plt + r_symndx;
4669 }
4670
4671 static bool
4672 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4673 {
4674 struct plt_entry *ent;
4675
4676 for (ent = *plist; ent != NULL; ent = ent->next)
4677 if (ent->addend == addend)
4678 break;
4679 if (ent == NULL)
4680 {
4681 size_t amt = sizeof (*ent);
4682 ent = bfd_alloc (abfd, amt);
4683 if (ent == NULL)
4684 return false;
4685 ent->next = *plist;
4686 ent->addend = addend;
4687 ent->plt.refcount = 0;
4688 *plist = ent;
4689 }
4690 ent->plt.refcount += 1;
4691 return true;
4692 }
4693
4694 static bool
4695 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4696 {
4697 return (r_type == R_PPC64_REL24
4698 || r_type == R_PPC64_REL24_NOTOC
4699 || r_type == R_PPC64_REL24_P9NOTOC
4700 || r_type == R_PPC64_REL14
4701 || r_type == R_PPC64_REL14_BRTAKEN
4702 || r_type == R_PPC64_REL14_BRNTAKEN
4703 || r_type == R_PPC64_ADDR24
4704 || r_type == R_PPC64_ADDR14
4705 || r_type == R_PPC64_ADDR14_BRTAKEN
4706 || r_type == R_PPC64_ADDR14_BRNTAKEN
4707 || r_type == R_PPC64_PLTCALL
4708 || r_type == R_PPC64_PLTCALL_NOTOC);
4709 }
4710
4711 /* Relocs on inline plt call sequence insns prior to the call. */
4712
4713 static bool
4714 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4715 {
4716 return (r_type == R_PPC64_PLT16_HA
4717 || r_type == R_PPC64_PLT16_HI
4718 || r_type == R_PPC64_PLT16_LO
4719 || r_type == R_PPC64_PLT16_LO_DS
4720 || r_type == R_PPC64_PLT_PCREL34
4721 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4722 || r_type == R_PPC64_PLTSEQ
4723 || r_type == R_PPC64_PLTSEQ_NOTOC);
4724 }
4725
4726 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4727 relocs, return true for those that operate on a dword. */
4728
4729 static bool
4730 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4731 {
4732 return (r_type == R_PPC64_PLT_PCREL34
4733 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4734 || r_type == R_PPC64_PLTCALL);
4735 }
4736
4737 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4738 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4739
4740 static bool
4741 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4742 {
4743 return offset <= sec->size && size <= sec->size - offset;
4744 }
4745
4746 /* Look through the relocs for a section during the first phase, and
4747 calculate needed space in the global offset table, procedure
4748 linkage table, and dynamic reloc sections. */
4749
4750 static bool
4751 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4752 asection *sec, const Elf_Internal_Rela *relocs)
4753 {
4754 struct ppc_link_hash_table *htab;
4755 Elf_Internal_Shdr *symtab_hdr;
4756 struct elf_link_hash_entry **sym_hashes;
4757 const Elf_Internal_Rela *rel;
4758 const Elf_Internal_Rela *rel_end;
4759 asection *sreloc;
4760 struct elf_link_hash_entry *tga, *dottga;
4761 bool is_opd;
4762
4763 if (bfd_link_relocatable (info))
4764 return true;
4765
4766 BFD_ASSERT (is_ppc64_elf (abfd));
4767
4768 htab = ppc_hash_table (info);
4769 if (htab == NULL)
4770 return false;
4771
4772 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4773 false, false, true);
4774 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4775 false, false, true);
4776 symtab_hdr = &elf_symtab_hdr (abfd);
4777 sym_hashes = elf_sym_hashes (abfd);
4778 sreloc = NULL;
4779 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4780 rel_end = relocs + sec->reloc_count;
4781 for (rel = relocs; rel < rel_end; rel++)
4782 {
4783 unsigned long r_symndx;
4784 struct elf_link_hash_entry *h;
4785 Elf_Internal_Sym *isym;
4786 enum elf_ppc64_reloc_type r_type;
4787 int tls_type;
4788 struct _ppc64_elf_section_data *ppc64_sec;
4789 struct plt_entry **ifunc, **plt_list;
4790
4791 r_symndx = ELF64_R_SYM (rel->r_info);
4792 if (r_symndx < symtab_hdr->sh_info)
4793 {
4794 h = NULL;
4795 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, abfd, r_symndx);
4796 if (isym == NULL)
4797 return false;
4798 }
4799 else
4800 {
4801 isym = NULL;
4802 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4803 h = elf_follow_link (h);
4804
4805 if (h == htab->elf.hgot)
4806 sec->has_toc_reloc = 1;
4807 }
4808
4809 r_type = ELF64_R_TYPE (rel->r_info);
4810 switch (r_type)
4811 {
4812 case R_PPC64_D34:
4813 case R_PPC64_D34_LO:
4814 case R_PPC64_D34_HI30:
4815 case R_PPC64_D34_HA30:
4816 case R_PPC64_D28:
4817 case R_PPC64_TPREL34:
4818 case R_PPC64_DTPREL34:
4819 case R_PPC64_PCREL34:
4820 case R_PPC64_GOT_PCREL34:
4821 case R_PPC64_GOT_TLSGD_PCREL34:
4822 case R_PPC64_GOT_TLSLD_PCREL34:
4823 case R_PPC64_GOT_TPREL_PCREL34:
4824 case R_PPC64_GOT_DTPREL_PCREL34:
4825 case R_PPC64_PLT_PCREL34:
4826 case R_PPC64_PLT_PCREL34_NOTOC:
4827 case R_PPC64_PCREL28:
4828 htab->has_power10_relocs = 1;
4829 break;
4830 default:
4831 break;
4832 }
4833
4834 switch (r_type)
4835 {
4836 case R_PPC64_PLT16_HA:
4837 case R_PPC64_GOT_TLSLD16_HA:
4838 case R_PPC64_GOT_TLSGD16_HA:
4839 case R_PPC64_GOT_TPREL16_HA:
4840 case R_PPC64_GOT_DTPREL16_HA:
4841 case R_PPC64_GOT16_HA:
4842 case R_PPC64_TOC16_HA:
4843 case R_PPC64_PLT16_LO:
4844 case R_PPC64_PLT16_LO_DS:
4845 case R_PPC64_GOT_TLSLD16_LO:
4846 case R_PPC64_GOT_TLSGD16_LO:
4847 case R_PPC64_GOT_TPREL16_LO_DS:
4848 case R_PPC64_GOT_DTPREL16_LO_DS:
4849 case R_PPC64_GOT16_LO:
4850 case R_PPC64_GOT16_LO_DS:
4851 case R_PPC64_TOC16_LO:
4852 case R_PPC64_TOC16_LO_DS:
4853 case R_PPC64_GOT_PCREL34:
4854 ppc64_elf_tdata (abfd)->has_optrel = 1;
4855 ppc64_elf_section_data (sec)->has_optrel = 1;
4856 break;
4857 default:
4858 break;
4859 }
4860
4861 ifunc = NULL;
4862 if (h != NULL)
4863 {
4864 if (h->type == STT_GNU_IFUNC)
4865 {
4866 h->needs_plt = 1;
4867 ifunc = &h->plt.plist;
4868 }
4869 }
4870 else
4871 {
4872 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4873 {
4874 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4875 rel->r_addend,
4876 NON_GOT | PLT_IFUNC);
4877 if (ifunc == NULL)
4878 return false;
4879 }
4880 }
4881
4882 tls_type = 0;
4883 switch (r_type)
4884 {
4885 case R_PPC64_TLSGD:
4886 case R_PPC64_TLSLD:
4887 /* These special tls relocs tie a call to __tls_get_addr with
4888 its parameter symbol. */
4889 if (h != NULL)
4890 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4891 else
4892 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4893 rel->r_addend,
4894 NON_GOT | TLS_TLS | TLS_MARK))
4895 return false;
4896 sec->has_tls_reloc = 1;
4897 break;
4898
4899 case R_PPC64_GOT_TLSLD16:
4900 case R_PPC64_GOT_TLSLD16_LO:
4901 case R_PPC64_GOT_TLSLD16_HI:
4902 case R_PPC64_GOT_TLSLD16_HA:
4903 case R_PPC64_GOT_TLSLD_PCREL34:
4904 tls_type = TLS_TLS | TLS_LD;
4905 goto dogottls;
4906
4907 case R_PPC64_GOT_TLSGD16:
4908 case R_PPC64_GOT_TLSGD16_LO:
4909 case R_PPC64_GOT_TLSGD16_HI:
4910 case R_PPC64_GOT_TLSGD16_HA:
4911 case R_PPC64_GOT_TLSGD_PCREL34:
4912 tls_type = TLS_TLS | TLS_GD;
4913 goto dogottls;
4914
4915 case R_PPC64_GOT_TPREL16_DS:
4916 case R_PPC64_GOT_TPREL16_LO_DS:
4917 case R_PPC64_GOT_TPREL16_HI:
4918 case R_PPC64_GOT_TPREL16_HA:
4919 case R_PPC64_GOT_TPREL_PCREL34:
4920 if (bfd_link_dll (info))
4921 info->flags |= DF_STATIC_TLS;
4922 tls_type = TLS_TLS | TLS_TPREL;
4923 goto dogottls;
4924
4925 case R_PPC64_GOT_DTPREL16_DS:
4926 case R_PPC64_GOT_DTPREL16_LO_DS:
4927 case R_PPC64_GOT_DTPREL16_HI:
4928 case R_PPC64_GOT_DTPREL16_HA:
4929 case R_PPC64_GOT_DTPREL_PCREL34:
4930 tls_type = TLS_TLS | TLS_DTPREL;
4931 dogottls:
4932 sec->has_tls_reloc = 1;
4933 goto dogot;
4934
4935 case R_PPC64_GOT16:
4936 case R_PPC64_GOT16_LO:
4937 case R_PPC64_GOT16_HI:
4938 case R_PPC64_GOT16_HA:
4939 case R_PPC64_GOT16_DS:
4940 case R_PPC64_GOT16_LO_DS:
4941 case R_PPC64_GOT_PCREL34:
4942 dogot:
4943 /* This symbol requires a global offset table entry. */
4944 sec->has_toc_reloc = 1;
4945 if (r_type == R_PPC64_GOT_TLSLD16
4946 || r_type == R_PPC64_GOT_TLSGD16
4947 || r_type == R_PPC64_GOT_TPREL16_DS
4948 || r_type == R_PPC64_GOT_DTPREL16_DS
4949 || r_type == R_PPC64_GOT16
4950 || r_type == R_PPC64_GOT16_DS)
4951 {
4952 htab->do_multi_toc = 1;
4953 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4954 }
4955
4956 if (ppc64_elf_tdata (abfd)->got == NULL
4957 && !create_got_section (abfd, info))
4958 return false;
4959
4960 if (h != NULL)
4961 {
4962 struct ppc_link_hash_entry *eh;
4963 struct got_entry *ent;
4964
4965 eh = ppc_elf_hash_entry (h);
4966 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4967 if (ent->addend == rel->r_addend
4968 && ent->owner == abfd
4969 && ent->tls_type == tls_type)
4970 break;
4971 if (ent == NULL)
4972 {
4973 size_t amt = sizeof (*ent);
4974 ent = bfd_alloc (abfd, amt);
4975 if (ent == NULL)
4976 return false;
4977 ent->next = eh->elf.got.glist;
4978 ent->addend = rel->r_addend;
4979 ent->owner = abfd;
4980 ent->tls_type = tls_type;
4981 ent->is_indirect = false;
4982 ent->got.refcount = 0;
4983 eh->elf.got.glist = ent;
4984 }
4985 ent->got.refcount += 1;
4986 eh->tls_mask |= tls_type;
4987 }
4988 else
4989 /* This is a global offset table entry for a local symbol. */
4990 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4991 rel->r_addend, tls_type))
4992 return false;
4993 break;
4994
4995 case R_PPC64_PLT16_HA:
4996 case R_PPC64_PLT16_HI:
4997 case R_PPC64_PLT16_LO:
4998 case R_PPC64_PLT16_LO_DS:
4999 case R_PPC64_PLT_PCREL34:
5000 case R_PPC64_PLT_PCREL34_NOTOC:
5001 case R_PPC64_PLT32:
5002 case R_PPC64_PLT64:
5003 /* This symbol requires a procedure linkage table entry. */
5004 plt_list = ifunc;
5005 if (h != NULL)
5006 {
5007 h->needs_plt = 1;
5008 if (h->root.root.string[0] == '.'
5009 && h->root.root.string[1] != '\0')
5010 ppc_elf_hash_entry (h)->is_func = 1;
5011 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
5012 plt_list = &h->plt.plist;
5013 }
5014 if (plt_list == NULL)
5015 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
5016 rel->r_addend,
5017 NON_GOT | PLT_KEEP);
5018 if (!update_plt_info (abfd, plt_list, rel->r_addend))
5019 return false;
5020 break;
5021
5022 /* The following relocations don't need to propagate the
5023 relocation if linking a shared object since they are
5024 section relative. */
5025 case R_PPC64_SECTOFF:
5026 case R_PPC64_SECTOFF_LO:
5027 case R_PPC64_SECTOFF_HI:
5028 case R_PPC64_SECTOFF_HA:
5029 case R_PPC64_SECTOFF_DS:
5030 case R_PPC64_SECTOFF_LO_DS:
5031 case R_PPC64_DTPREL16:
5032 case R_PPC64_DTPREL16_LO:
5033 case R_PPC64_DTPREL16_HI:
5034 case R_PPC64_DTPREL16_HA:
5035 case R_PPC64_DTPREL16_DS:
5036 case R_PPC64_DTPREL16_LO_DS:
5037 case R_PPC64_DTPREL16_HIGH:
5038 case R_PPC64_DTPREL16_HIGHA:
5039 case R_PPC64_DTPREL16_HIGHER:
5040 case R_PPC64_DTPREL16_HIGHERA:
5041 case R_PPC64_DTPREL16_HIGHEST:
5042 case R_PPC64_DTPREL16_HIGHESTA:
5043 break;
5044
5045 /* Nor do these. */
5046 case R_PPC64_REL16:
5047 case R_PPC64_REL16_LO:
5048 case R_PPC64_REL16_HI:
5049 case R_PPC64_REL16_HA:
5050 case R_PPC64_REL16_HIGH:
5051 case R_PPC64_REL16_HIGHA:
5052 case R_PPC64_REL16_HIGHER:
5053 case R_PPC64_REL16_HIGHERA:
5054 case R_PPC64_REL16_HIGHEST:
5055 case R_PPC64_REL16_HIGHESTA:
5056 case R_PPC64_REL16_HIGHER34:
5057 case R_PPC64_REL16_HIGHERA34:
5058 case R_PPC64_REL16_HIGHEST34:
5059 case R_PPC64_REL16_HIGHESTA34:
5060 case R_PPC64_REL16DX_HA:
5061 break;
5062
5063 /* Not supported as a dynamic relocation. */
5064 case R_PPC64_ADDR64_LOCAL:
5065 if (bfd_link_pic (info))
5066 {
5067 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5068 ppc_howto_init ();
5069 /* xgettext:c-format */
5070 info->callbacks->einfo (_("%H: %s reloc unsupported "
5071 "in shared libraries and PIEs\n"),
5072 abfd, sec, rel->r_offset,
5073 ppc64_elf_howto_table[r_type]->name);
5074 bfd_set_error (bfd_error_bad_value);
5075 return false;
5076 }
5077 break;
5078
5079 case R_PPC64_TOC16:
5080 case R_PPC64_TOC16_DS:
5081 htab->do_multi_toc = 1;
5082 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5083 /* Fall through. */
5084 case R_PPC64_TOC16_LO:
5085 case R_PPC64_TOC16_HI:
5086 case R_PPC64_TOC16_HA:
5087 case R_PPC64_TOC16_LO_DS:
5088 sec->has_toc_reloc = 1;
5089 if (h != NULL && bfd_link_executable (info))
5090 {
5091 /* We may need a copy reloc. */
5092 h->non_got_ref = 1;
5093 /* Strongly prefer a copy reloc over a dynamic reloc.
5094 glibc ld.so as of 2019-08 will error out if one of
5095 these relocations is emitted. */
5096 h->needs_copy = 1;
5097 goto dodyn;
5098 }
5099 break;
5100
5101 /* Marker reloc. */
5102 case R_PPC64_ENTRY:
5103 break;
5104
5105 /* This relocation describes the C++ object vtable hierarchy.
5106 Reconstruct it for later use during GC. */
5107 case R_PPC64_GNU_VTINHERIT:
5108 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5109 return false;
5110 break;
5111
5112 /* This relocation describes which C++ vtable entries are actually
5113 used. Record for later use during GC. */
5114 case R_PPC64_GNU_VTENTRY:
5115 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5116 return false;
5117 break;
5118
5119 case R_PPC64_REL14:
5120 case R_PPC64_REL14_BRTAKEN:
5121 case R_PPC64_REL14_BRNTAKEN:
5122 {
5123 asection *dest = NULL;
5124
5125 /* Heuristic: If jumping outside our section, chances are
5126 we are going to need a stub. */
5127 if (h != NULL)
5128 {
5129 /* If the sym is weak it may be overridden later, so
5130 don't assume we know where a weak sym lives. */
5131 if (h->root.type == bfd_link_hash_defined)
5132 dest = h->root.u.def.section;
5133 }
5134 else
5135 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5136
5137 if (dest != sec)
5138 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5139 }
5140 goto rel24;
5141
5142 case R_PPC64_PLTCALL:
5143 case R_PPC64_PLTCALL_NOTOC:
5144 ppc64_elf_section_data (sec)->has_pltcall = 1;
5145 /* Fall through. */
5146
5147 case R_PPC64_REL24:
5148 case R_PPC64_REL24_NOTOC:
5149 case R_PPC64_REL24_P9NOTOC:
5150 rel24:
5151 plt_list = ifunc;
5152 if (h != NULL)
5153 {
5154 h->needs_plt = 1;
5155 if (h->root.root.string[0] == '.'
5156 && h->root.root.string[1] != '\0')
5157 ppc_elf_hash_entry (h)->is_func = 1;
5158
5159 if (h == tga || h == dottga)
5160 {
5161 sec->has_tls_reloc = 1;
5162 if (rel != relocs
5163 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5164 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5165 /* We have a new-style __tls_get_addr call with
5166 a marker reloc. */
5167 ;
5168 else
5169 /* Mark this section as having an old-style call. */
5170 sec->nomark_tls_get_addr = 1;
5171 }
5172 plt_list = &h->plt.plist;
5173 }
5174
5175 /* We may need a .plt entry if the function this reloc
5176 refers to is in a shared lib. */
5177 if (plt_list
5178 && !update_plt_info (abfd, plt_list, rel->r_addend))
5179 return false;
5180 break;
5181
5182 case R_PPC64_ADDR14:
5183 case R_PPC64_ADDR14_BRNTAKEN:
5184 case R_PPC64_ADDR14_BRTAKEN:
5185 case R_PPC64_ADDR24:
5186 goto dodyn;
5187
5188 case R_PPC64_TPREL64:
5189 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5190 if (bfd_link_dll (info))
5191 info->flags |= DF_STATIC_TLS;
5192 goto dotlstoc;
5193
5194 case R_PPC64_DTPMOD64:
5195 if (rel + 1 < rel_end
5196 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5197 && rel[1].r_offset == rel->r_offset + 8)
5198 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5199 else
5200 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5201 goto dotlstoc;
5202
5203 case R_PPC64_DTPREL64:
5204 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5205 if (rel != relocs
5206 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5207 && rel[-1].r_offset == rel->r_offset - 8)
5208 /* This is the second reloc of a dtpmod, dtprel pair.
5209 Don't mark with TLS_DTPREL. */
5210 goto dodyn;
5211
5212 dotlstoc:
5213 sec->has_tls_reloc = 1;
5214 if (h != NULL)
5215 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5216 else
5217 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5218 rel->r_addend, tls_type))
5219 return false;
5220
5221 ppc64_sec = ppc64_elf_section_data (sec);
5222 if (ppc64_sec->sec_type != sec_toc)
5223 {
5224 bfd_size_type amt;
5225
5226 /* One extra to simplify get_tls_mask. */
5227 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5228 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5229 if (ppc64_sec->u.toc.symndx == NULL)
5230 return false;
5231 amt = sec->size * sizeof (bfd_vma) / 8;
5232 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5233 if (ppc64_sec->u.toc.add == NULL)
5234 return false;
5235 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5236 ppc64_sec->sec_type = sec_toc;
5237 }
5238 BFD_ASSERT (rel->r_offset % 8 == 0);
5239 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5240 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5241
5242 /* Mark the second slot of a GD or LD entry.
5243 -1 to indicate GD and -2 to indicate LD. */
5244 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5245 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5246 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5247 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5248 goto dodyn;
5249
5250 case R_PPC64_TPREL16_HI:
5251 case R_PPC64_TPREL16_HA:
5252 case R_PPC64_TPREL16_HIGH:
5253 case R_PPC64_TPREL16_HIGHA:
5254 case R_PPC64_TPREL16_HIGHER:
5255 case R_PPC64_TPREL16_HIGHERA:
5256 case R_PPC64_TPREL16_HIGHEST:
5257 case R_PPC64_TPREL16_HIGHESTA:
5258 sec->has_tls_reloc = 1;
5259 /* Fall through. */
5260 case R_PPC64_TPREL34:
5261 case R_PPC64_TPREL16:
5262 case R_PPC64_TPREL16_DS:
5263 case R_PPC64_TPREL16_LO:
5264 case R_PPC64_TPREL16_LO_DS:
5265 if (bfd_link_dll (info))
5266 info->flags |= DF_STATIC_TLS;
5267 goto dodyn;
5268
5269 case R_PPC64_ADDR64:
5270 if (is_opd
5271 && rel + 1 < rel_end
5272 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5273 {
5274 if (h != NULL)
5275 ppc_elf_hash_entry (h)->is_func = 1;
5276 }
5277 /* Fall through. */
5278
5279 case R_PPC64_ADDR16:
5280 case R_PPC64_ADDR16_DS:
5281 case R_PPC64_ADDR16_HA:
5282 case R_PPC64_ADDR16_HI:
5283 case R_PPC64_ADDR16_HIGH:
5284 case R_PPC64_ADDR16_HIGHA:
5285 case R_PPC64_ADDR16_HIGHER:
5286 case R_PPC64_ADDR16_HIGHERA:
5287 case R_PPC64_ADDR16_HIGHEST:
5288 case R_PPC64_ADDR16_HIGHESTA:
5289 case R_PPC64_ADDR16_LO:
5290 case R_PPC64_ADDR16_LO_DS:
5291 case R_PPC64_D34:
5292 case R_PPC64_D34_LO:
5293 case R_PPC64_D34_HI30:
5294 case R_PPC64_D34_HA30:
5295 case R_PPC64_ADDR16_HIGHER34:
5296 case R_PPC64_ADDR16_HIGHERA34:
5297 case R_PPC64_ADDR16_HIGHEST34:
5298 case R_PPC64_ADDR16_HIGHESTA34:
5299 case R_PPC64_D28:
5300 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5301 && rel->r_addend == 0)
5302 {
5303 /* We may need a .plt entry if this reloc refers to a
5304 function in a shared lib. */
5305 if (!update_plt_info (abfd, &h->plt.plist, 0))
5306 return false;
5307 h->pointer_equality_needed = 1;
5308 }
5309 /* Fall through. */
5310
5311 case R_PPC64_REL30:
5312 case R_PPC64_REL32:
5313 case R_PPC64_REL64:
5314 case R_PPC64_ADDR32:
5315 case R_PPC64_UADDR16:
5316 case R_PPC64_UADDR32:
5317 case R_PPC64_UADDR64:
5318 case R_PPC64_TOC:
5319 if (h != NULL && bfd_link_executable (info))
5320 /* We may need a copy reloc. */
5321 h->non_got_ref = 1;
5322
5323 /* Don't propagate .opd relocs. */
5324 if (NO_OPD_RELOCS && is_opd)
5325 break;
5326
5327 /* If we are creating a shared library, and this is a reloc
5328 against a global symbol, or a non PC relative reloc
5329 against a local symbol, then we need to copy the reloc
5330 into the shared library. However, if we are linking with
5331 -Bsymbolic, we do not need to copy a reloc against a
5332 global symbol which is defined in an object we are
5333 including in the link (i.e., DEF_REGULAR is set). At
5334 this point we have not seen all the input files, so it is
5335 possible that DEF_REGULAR is not set now but will be set
5336 later (it is never cleared). In case of a weak definition,
5337 DEF_REGULAR may be cleared later by a strong definition in
5338 a shared library. We account for that possibility below by
5339 storing information in the dyn_relocs field of the hash
5340 table entry. A similar situation occurs when creating
5341 shared libraries and symbol visibility changes render the
5342 symbol local.
5343
5344 If on the other hand, we are creating an executable, we
5345 may need to keep relocations for symbols satisfied by a
5346 dynamic library if we manage to avoid copy relocs for the
5347 symbol. */
5348 dodyn:
5349 if ((h != NULL
5350 && (h->root.type == bfd_link_hash_defweak
5351 || (!h->def_regular && !h->root.ldscript_def)))
5352 || (h != NULL
5353 && !SYMBOL_REFERENCES_LOCAL (info, h))
5354 || (bfd_link_pic (info)
5355 && (h != NULL
5356 ? !bfd_is_abs_symbol (&h->root)
5357 : isym->st_shndx != SHN_ABS)
5358 && must_be_dyn_reloc (info, r_type))
5359 || (!bfd_link_pic (info)
5360 && ifunc != NULL))
5361 {
5362 /* We must copy these reloc types into the output file.
5363 Create a reloc section in dynobj and make room for
5364 this reloc. */
5365 if (sreloc == NULL)
5366 {
5367 sreloc = _bfd_elf_make_dynamic_reloc_section
5368 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5369
5370 if (sreloc == NULL)
5371 return false;
5372 }
5373
5374 /* If this is a global symbol, we count the number of
5375 relocations we need for this symbol. */
5376 if (h != NULL)
5377 {
5378 struct ppc_dyn_relocs *p;
5379 struct ppc_dyn_relocs **head;
5380
5381 head = (struct ppc_dyn_relocs **) &h->dyn_relocs;
5382 p = *head;
5383 if (p == NULL || p->sec != sec)
5384 {
5385 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5386 if (p == NULL)
5387 return false;
5388 p->next = *head;
5389 *head = p;
5390 p->sec = sec;
5391 p->count = 0;
5392 p->pc_count = 0;
5393 p->rel_count = 0;
5394 }
5395 p->count += 1;
5396 if (!must_be_dyn_reloc (info, r_type))
5397 p->pc_count += 1;
5398 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5399 && rel->r_offset % 2 == 0
5400 && sec->alignment_power != 0)
5401 p->rel_count += 1;
5402 }
5403 else
5404 {
5405 /* Track dynamic relocs needed for local syms too. */
5406 struct ppc_local_dyn_relocs *p;
5407 struct ppc_local_dyn_relocs **head;
5408 bool is_ifunc;
5409 asection *s;
5410 void *vpp;
5411
5412 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5413 if (s == NULL)
5414 s = sec;
5415
5416 vpp = &elf_section_data (s)->local_dynrel;
5417 head = (struct ppc_local_dyn_relocs **) vpp;
5418 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5419 p = *head;
5420 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5421 p = p->next;
5422 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5423 {
5424 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5425 if (p == NULL)
5426 return false;
5427 p->next = *head;
5428 *head = p;
5429 p->sec = sec;
5430 p->count = 0;
5431 p->rel_count = 0;
5432 p->ifunc = is_ifunc;
5433 }
5434 p->count += 1;
5435 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
5436 && rel->r_offset % 2 == 0
5437 && sec->alignment_power != 0)
5438 p->rel_count += 1;
5439 }
5440 }
5441 break;
5442
5443 default:
5444 break;
5445 }
5446 }
5447
5448 return true;
5449 }
5450
5451 /* Merge backend specific data from an object file to the output
5452 object file when linking. */
5453
5454 static bool
5455 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5456 {
5457 bfd *obfd = info->output_bfd;
5458 unsigned long iflags, oflags;
5459
5460 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5461 return true;
5462
5463 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5464 return true;
5465
5466 if (!_bfd_generic_verify_endian_match (ibfd, info))
5467 return false;
5468
5469 iflags = elf_elfheader (ibfd)->e_flags;
5470 oflags = elf_elfheader (obfd)->e_flags;
5471
5472 if (iflags & ~EF_PPC64_ABI)
5473 {
5474 _bfd_error_handler
5475 /* xgettext:c-format */
5476 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5477 bfd_set_error (bfd_error_bad_value);
5478 return false;
5479 }
5480 else if (iflags != oflags && iflags != 0)
5481 {
5482 _bfd_error_handler
5483 /* xgettext:c-format */
5484 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5485 ibfd, iflags, oflags);
5486 bfd_set_error (bfd_error_bad_value);
5487 return false;
5488 }
5489
5490 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5491 return false;
5492
5493 /* Merge Tag_compatibility attributes and any common GNU ones. */
5494 return _bfd_elf_merge_object_attributes (ibfd, info);
5495 }
5496
5497 static bool
5498 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5499 {
5500 /* Print normal ELF private data. */
5501 _bfd_elf_print_private_bfd_data (abfd, ptr);
5502
5503 if (elf_elfheader (abfd)->e_flags != 0)
5504 {
5505 FILE *file = ptr;
5506
5507 fprintf (file, _("private flags = 0x%lx:"),
5508 elf_elfheader (abfd)->e_flags);
5509
5510 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5511 fprintf (file, _(" [abiv%ld]"),
5512 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5513 fputc ('\n', file);
5514 }
5515
5516 return true;
5517 }
5518
5519 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5520 of the code entry point, and its section, which must be in the same
5521 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5522
5523 static bfd_vma
5524 opd_entry_value (asection *opd_sec,
5525 bfd_vma offset,
5526 asection **code_sec,
5527 bfd_vma *code_off,
5528 bool in_code_sec)
5529 {
5530 bfd *opd_bfd = opd_sec->owner;
5531 Elf_Internal_Rela *relocs;
5532 Elf_Internal_Rela *lo, *hi, *look;
5533 bfd_vma val;
5534
5535 /* No relocs implies we are linking a --just-symbols object, or looking
5536 at a final linked executable with addr2line or somesuch. */
5537 if (opd_sec->reloc_count == 0)
5538 {
5539 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5540
5541 if (contents == NULL)
5542 {
5543 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5544 return (bfd_vma) -1;
5545 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5546 }
5547
5548 /* PR 17512: file: 64b9dfbb. */
5549 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5550 return (bfd_vma) -1;
5551
5552 val = bfd_get_64 (opd_bfd, contents + offset);
5553 if (code_sec != NULL)
5554 {
5555 asection *sec, *likely = NULL;
5556
5557 if (in_code_sec)
5558 {
5559 sec = *code_sec;
5560 if (sec->vma <= val
5561 && val < sec->vma + sec->size)
5562 likely = sec;
5563 else
5564 val = -1;
5565 }
5566 else
5567 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5568 if (sec->vma <= val
5569 && (sec->flags & SEC_LOAD) != 0
5570 && (sec->flags & SEC_ALLOC) != 0)
5571 likely = sec;
5572 if (likely != NULL)
5573 {
5574 *code_sec = likely;
5575 if (code_off != NULL)
5576 *code_off = val - likely->vma;
5577 }
5578 }
5579 return val;
5580 }
5581
5582 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5583
5584 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5585 if (relocs == NULL)
5586 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5587 /* PR 17512: file: df8e1fd6. */
5588 if (relocs == NULL)
5589 return (bfd_vma) -1;
5590
5591 /* Go find the opd reloc at the sym address. */
5592 lo = relocs;
5593 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5594 val = (bfd_vma) -1;
5595 while (lo < hi)
5596 {
5597 look = lo + (hi - lo) / 2;
5598 if (look->r_offset < offset)
5599 lo = look + 1;
5600 else if (look->r_offset > offset)
5601 hi = look;
5602 else
5603 {
5604 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5605
5606 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5607 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5608 {
5609 unsigned long symndx = ELF64_R_SYM (look->r_info);
5610 asection *sec = NULL;
5611
5612 if (symndx >= symtab_hdr->sh_info
5613 && elf_sym_hashes (opd_bfd) != NULL)
5614 {
5615 struct elf_link_hash_entry **sym_hashes;
5616 struct elf_link_hash_entry *rh;
5617
5618 sym_hashes = elf_sym_hashes (opd_bfd);
5619 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5620 if (rh != NULL)
5621 {
5622 rh = elf_follow_link (rh);
5623 if (rh->root.type != bfd_link_hash_defined
5624 && rh->root.type != bfd_link_hash_defweak)
5625 break;
5626 if (rh->root.u.def.section->owner == opd_bfd)
5627 {
5628 val = rh->root.u.def.value;
5629 sec = rh->root.u.def.section;
5630 }
5631 }
5632 }
5633
5634 if (sec == NULL)
5635 {
5636 Elf_Internal_Sym *sym;
5637
5638 if (symndx < symtab_hdr->sh_info)
5639 {
5640 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5641 if (sym == NULL)
5642 {
5643 size_t symcnt = symtab_hdr->sh_info;
5644 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5645 symcnt, 0,
5646 NULL, NULL, NULL);
5647 if (sym == NULL)
5648 break;
5649 symtab_hdr->contents = (bfd_byte *) sym;
5650 }
5651 sym += symndx;
5652 }
5653 else
5654 {
5655 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5656 1, symndx,
5657 NULL, NULL, NULL);
5658 if (sym == NULL)
5659 break;
5660 }
5661 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5662 if (sec == NULL)
5663 break;
5664 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5665 val = sym->st_value;
5666 }
5667
5668 val += look->r_addend;
5669 if (code_off != NULL)
5670 *code_off = val;
5671 if (code_sec != NULL)
5672 {
5673 if (in_code_sec && *code_sec != sec)
5674 return -1;
5675 else
5676 *code_sec = sec;
5677 }
5678 if (sec->output_section != NULL)
5679 val += sec->output_section->vma + sec->output_offset;
5680 }
5681 break;
5682 }
5683 }
5684
5685 return val;
5686 }
5687
5688 /* If the ELF symbol SYM might be a function in SEC, return the
5689 function size and set *CODE_OFF to the function's entry point,
5690 otherwise return zero. */
5691
5692 static bfd_size_type
5693 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5694 bfd_vma *code_off)
5695 {
5696 bfd_size_type size;
5697 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5698
5699 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5700 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5701 return 0;
5702
5703 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5704
5705 /* In theory we should check that the symbol's type satisfies
5706 _bfd_elf_is_function_type(), but there are some function-like
5707 symbols which would fail this test. (eg _start). Instead
5708 we check for hidden, local, notype symbols with zero size.
5709 This type of symbol is generated by the annobin plugin for gcc
5710 and clang, and should not be considered to be a function symbol. */
5711 if (size == 0
5712 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5713 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5714 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5715 return 0;
5716
5717 if (strcmp (sym->section->name, ".opd") == 0)
5718 {
5719 struct _opd_sec_data *opd = get_opd_info (sym->section);
5720 bfd_vma symval = sym->value;
5721
5722 if (opd != NULL
5723 && opd->adjust != NULL
5724 && elf_section_data (sym->section)->relocs != NULL)
5725 {
5726 /* opd_entry_value will use cached relocs that have been
5727 adjusted, but with raw symbols. That means both local
5728 and global symbols need adjusting. */
5729 long adjust = opd->adjust[OPD_NDX (symval)];
5730 if (adjust == -1)
5731 return 0;
5732 symval += adjust;
5733 }
5734
5735 if (opd_entry_value (sym->section, symval,
5736 &sec, code_off, true) == (bfd_vma) -1)
5737 return 0;
5738 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5739 symbol. This size has nothing to do with the code size of the
5740 function, which is what we're supposed to return, but the
5741 code size isn't available without looking up the dot-sym.
5742 However, doing that would be a waste of time particularly
5743 since elf_find_function will look at the dot-sym anyway.
5744 Now, elf_find_function will keep the largest size of any
5745 function sym found at the code address of interest, so return
5746 1 here to avoid it incorrectly caching a larger function size
5747 for a small function. This does mean we return the wrong
5748 size for a new-ABI function of size 24, but all that does is
5749 disable caching for such functions. */
5750 if (size == 24)
5751 size = 1;
5752 }
5753 else
5754 {
5755 if (sym->section != sec)
5756 return 0;
5757 *code_off = sym->value;
5758 }
5759
5760 /* Do not return 0 for the function's size. */
5761 return size ? size : 1;
5762 }
5763
5764 /* Return true if symbol is a strong function defined in an ELFv2
5765 object with st_other localentry bits of zero, ie. its local entry
5766 point coincides with its global entry point. */
5767
5768 static bool
5769 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5770 {
5771 return (h != NULL
5772 && h->type == STT_FUNC
5773 && h->root.type == bfd_link_hash_defined
5774 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5775 && !ppc_elf_hash_entry (h)->non_zero_localentry
5776 && is_ppc64_elf (h->root.u.def.section->owner)
5777 && abiversion (h->root.u.def.section->owner) >= 2);
5778 }
5779
5780 /* Return true if symbol is defined in a regular object file. */
5781
5782 static bool
5783 is_static_defined (struct elf_link_hash_entry *h)
5784 {
5785 return ((h->root.type == bfd_link_hash_defined
5786 || h->root.type == bfd_link_hash_defweak)
5787 && h->root.u.def.section != NULL
5788 && h->root.u.def.section->output_section != NULL);
5789 }
5790
5791 /* If FDH is a function descriptor symbol, return the associated code
5792 entry symbol if it is defined. Return NULL otherwise. */
5793
5794 static struct ppc_link_hash_entry *
5795 defined_code_entry (struct ppc_link_hash_entry *fdh)
5796 {
5797 if (fdh->is_func_descriptor)
5798 {
5799 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5800 if (fh->elf.root.type == bfd_link_hash_defined
5801 || fh->elf.root.type == bfd_link_hash_defweak)
5802 return fh;
5803 }
5804 return NULL;
5805 }
5806
5807 /* If FH is a function code entry symbol, return the associated
5808 function descriptor symbol if it is defined. Return NULL otherwise. */
5809
5810 static struct ppc_link_hash_entry *
5811 defined_func_desc (struct ppc_link_hash_entry *fh)
5812 {
5813 if (fh->oh != NULL
5814 && fh->oh->is_func_descriptor)
5815 {
5816 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5817 if (fdh->elf.root.type == bfd_link_hash_defined
5818 || fdh->elf.root.type == bfd_link_hash_defweak)
5819 return fdh;
5820 }
5821 return NULL;
5822 }
5823
5824 /* Given H is a symbol that satisfies is_static_defined, return the
5825 value in the output file. */
5826
5827 static bfd_vma
5828 defined_sym_val (struct elf_link_hash_entry *h)
5829 {
5830 return (h->root.u.def.section->output_section->vma
5831 + h->root.u.def.section->output_offset
5832 + h->root.u.def.value);
5833 }
5834
5835 /* Return true if H matches __tls_get_addr or one of its variants. */
5836
5837 static bool
5838 is_tls_get_addr (struct elf_link_hash_entry *h,
5839 struct ppc_link_hash_table *htab)
5840 {
5841 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5842 || h == elf_hash_entry (htab->tga_desc_fd)
5843 || h == elf_hash_entry (htab->tls_get_addr)
5844 || h == elf_hash_entry (htab->tga_desc));
5845 }
5846
5847 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5848
5849 /* Garbage collect sections, after first dealing with dot-symbols. */
5850
5851 static bool
5852 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5853 {
5854 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5855
5856 if (htab != NULL && htab->need_func_desc_adj)
5857 {
5858 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5859 htab->need_func_desc_adj = 0;
5860 }
5861 return bfd_elf_gc_sections (abfd, info);
5862 }
5863
5864 /* Mark all our entry sym sections, both opd and code section. */
5865
5866 static void
5867 ppc64_elf_gc_keep (struct bfd_link_info *info)
5868 {
5869 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5870 struct bfd_sym_chain *sym;
5871
5872 if (htab == NULL)
5873 return;
5874
5875 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5876 {
5877 struct ppc_link_hash_entry *eh, *fh;
5878 asection *sec;
5879
5880 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5881 false, false, true));
5882 if (eh == NULL)
5883 continue;
5884 if (eh->elf.root.type != bfd_link_hash_defined
5885 && eh->elf.root.type != bfd_link_hash_defweak)
5886 continue;
5887
5888 fh = defined_code_entry (eh);
5889 if (fh != NULL)
5890 {
5891 sec = fh->elf.root.u.def.section;
5892 sec->flags |= SEC_KEEP;
5893 }
5894 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5895 && opd_entry_value (eh->elf.root.u.def.section,
5896 eh->elf.root.u.def.value,
5897 &sec, NULL, false) != (bfd_vma) -1)
5898 sec->flags |= SEC_KEEP;
5899
5900 sec = eh->elf.root.u.def.section;
5901 sec->flags |= SEC_KEEP;
5902 }
5903 }
5904
5905 /* Mark sections containing dynamically referenced symbols. When
5906 building shared libraries, we must assume that any visible symbol is
5907 referenced. */
5908
5909 static bool
5910 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5911 {
5912 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5913 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5914 struct ppc_link_hash_entry *fdh;
5915 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5916
5917 /* Dynamic linking info is on the func descriptor sym. */
5918 fdh = defined_func_desc (eh);
5919 if (fdh != NULL)
5920 eh = fdh;
5921
5922 if ((eh->elf.root.type == bfd_link_hash_defined
5923 || eh->elf.root.type == bfd_link_hash_defweak)
5924 && (!eh->elf.start_stop
5925 || eh->elf.root.ldscript_def
5926 || !info->start_stop_gc)
5927 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5928 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5929 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5930 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5931 && (!bfd_link_executable (info)
5932 || info->gc_keep_exported
5933 || info->export_dynamic
5934 || (eh->elf.dynamic
5935 && d != NULL
5936 && (*d->match) (&d->head, NULL,
5937 eh->elf.root.root.string)))
5938 && (eh->elf.versioned >= versioned
5939 || !bfd_hide_sym_by_version (info->version_info,
5940 eh->elf.root.root.string)))))
5941 {
5942 asection *code_sec;
5943 struct ppc_link_hash_entry *fh;
5944
5945 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5946
5947 /* Function descriptor syms cause the associated
5948 function code sym section to be marked. */
5949 fh = defined_code_entry (eh);
5950 if (fh != NULL)
5951 {
5952 code_sec = fh->elf.root.u.def.section;
5953 code_sec->flags |= SEC_KEEP;
5954 }
5955 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5956 && opd_entry_value (eh->elf.root.u.def.section,
5957 eh->elf.root.u.def.value,
5958 &code_sec, NULL, false) != (bfd_vma) -1)
5959 code_sec->flags |= SEC_KEEP;
5960 }
5961
5962 return true;
5963 }
5964
5965 /* Return the section that should be marked against GC for a given
5966 relocation. */
5967
5968 static asection *
5969 ppc64_elf_gc_mark_hook (asection *sec,
5970 struct bfd_link_info *info,
5971 Elf_Internal_Rela *rel,
5972 struct elf_link_hash_entry *h,
5973 Elf_Internal_Sym *sym)
5974 {
5975 asection *rsec;
5976
5977 /* Syms return NULL if we're marking .opd, so we avoid marking all
5978 function sections, as all functions are referenced in .opd. */
5979 rsec = NULL;
5980 if (get_opd_info (sec) != NULL)
5981 return rsec;
5982
5983 if (h != NULL)
5984 {
5985 enum elf_ppc64_reloc_type r_type;
5986 struct ppc_link_hash_entry *eh, *fh, *fdh;
5987
5988 r_type = ELF64_R_TYPE (rel->r_info);
5989 switch (r_type)
5990 {
5991 case R_PPC64_GNU_VTINHERIT:
5992 case R_PPC64_GNU_VTENTRY:
5993 break;
5994
5995 default:
5996 switch (h->root.type)
5997 {
5998 case bfd_link_hash_defined:
5999 case bfd_link_hash_defweak:
6000 eh = ppc_elf_hash_entry (h);
6001 fdh = defined_func_desc (eh);
6002 if (fdh != NULL)
6003 {
6004 /* -mcall-aixdesc code references the dot-symbol on
6005 a call reloc. Mark the function descriptor too
6006 against garbage collection. */
6007 fdh->elf.mark = 1;
6008 if (fdh->elf.is_weakalias)
6009 weakdef (&fdh->elf)->mark = 1;
6010 eh = fdh;
6011 }
6012
6013 /* Function descriptor syms cause the associated
6014 function code sym section to be marked. */
6015 fh = defined_code_entry (eh);
6016 if (fh != NULL)
6017 {
6018 /* They also mark their opd section. */
6019 eh->elf.root.u.def.section->gc_mark = 1;
6020
6021 rsec = fh->elf.root.u.def.section;
6022 }
6023 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
6024 && opd_entry_value (eh->elf.root.u.def.section,
6025 eh->elf.root.u.def.value,
6026 &rsec, NULL, false) != (bfd_vma) -1)
6027 eh->elf.root.u.def.section->gc_mark = 1;
6028 else
6029 rsec = h->root.u.def.section;
6030 break;
6031
6032 case bfd_link_hash_common:
6033 rsec = h->root.u.c.p->section;
6034 break;
6035
6036 default:
6037 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6038 }
6039 }
6040 }
6041 else
6042 {
6043 struct _opd_sec_data *opd;
6044
6045 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6046 opd = get_opd_info (rsec);
6047 if (opd != NULL && opd->func_sec != NULL)
6048 {
6049 rsec->gc_mark = 1;
6050
6051 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6052 }
6053 }
6054
6055 return rsec;
6056 }
6057
6058 /* The maximum size of .sfpr. */
6059 #define SFPR_MAX (218*4)
6060
6061 struct sfpr_def_parms
6062 {
6063 const char name[12];
6064 unsigned char lo, hi;
6065 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6066 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6067 };
6068
6069 /* Auto-generate _save*, _rest* functions in .sfpr.
6070 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6071 instead. */
6072
6073 static bool
6074 sfpr_define (struct bfd_link_info *info,
6075 const struct sfpr_def_parms *parm,
6076 asection *stub_sec)
6077 {
6078 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6079 unsigned int i;
6080 size_t len = strlen (parm->name);
6081 bool writing = false;
6082 char sym[16];
6083
6084 if (htab == NULL)
6085 return false;
6086
6087 memcpy (sym, parm->name, len);
6088 sym[len + 2] = 0;
6089
6090 for (i = parm->lo; i <= parm->hi; i++)
6091 {
6092 struct ppc_link_hash_entry *h;
6093
6094 sym[len + 0] = i / 10 + '0';
6095 sym[len + 1] = i % 10 + '0';
6096 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6097 writing, true, true));
6098 if (stub_sec != NULL)
6099 {
6100 if (h != NULL
6101 && h->elf.root.type == bfd_link_hash_defined
6102 && h->elf.root.u.def.section == htab->sfpr)
6103 {
6104 struct elf_link_hash_entry *s;
6105 char buf[32];
6106 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6107 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6108 if (s == NULL)
6109 return false;
6110 if (s->root.type == bfd_link_hash_new)
6111 {
6112 s->root.type = bfd_link_hash_defined;
6113 s->root.u.def.section = stub_sec;
6114 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6115 + h->elf.root.u.def.value);
6116 s->ref_regular = 1;
6117 s->def_regular = 1;
6118 s->ref_regular_nonweak = 1;
6119 s->forced_local = 1;
6120 s->non_elf = 0;
6121 s->root.linker_def = 1;
6122 }
6123 }
6124 continue;
6125 }
6126 if (h != NULL)
6127 {
6128 h->save_res = 1;
6129 if (!h->elf.def_regular)
6130 {
6131 h->elf.root.type = bfd_link_hash_defined;
6132 h->elf.root.u.def.section = htab->sfpr;
6133 h->elf.root.u.def.value = htab->sfpr->size;
6134 h->elf.type = STT_FUNC;
6135 h->elf.def_regular = 1;
6136 h->elf.non_elf = 0;
6137 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6138 writing = true;
6139 if (htab->sfpr->contents == NULL)
6140 {
6141 htab->sfpr->contents
6142 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6143 if (htab->sfpr->contents == NULL)
6144 return false;
6145 }
6146 }
6147 }
6148 if (writing)
6149 {
6150 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6151 if (i != parm->hi)
6152 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6153 else
6154 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6155 htab->sfpr->size = p - htab->sfpr->contents;
6156 }
6157 }
6158
6159 return true;
6160 }
6161
6162 static bfd_byte *
6163 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6164 {
6165 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6166 return p + 4;
6167 }
6168
6169 static bfd_byte *
6170 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6171 {
6172 p = savegpr0 (abfd, p, r);
6173 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6174 p = p + 4;
6175 bfd_put_32 (abfd, BLR, p);
6176 return p + 4;
6177 }
6178
6179 static bfd_byte *
6180 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6181 {
6182 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6183 return p + 4;
6184 }
6185
6186 static bfd_byte *
6187 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6188 {
6189 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6190 p = p + 4;
6191 p = restgpr0 (abfd, p, r);
6192 bfd_put_32 (abfd, MTLR_R0, p);
6193 p = p + 4;
6194 if (r == 29)
6195 {
6196 p = restgpr0 (abfd, p, 30);
6197 p = restgpr0 (abfd, p, 31);
6198 }
6199 bfd_put_32 (abfd, BLR, p);
6200 return p + 4;
6201 }
6202
6203 static bfd_byte *
6204 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6205 {
6206 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6207 return p + 4;
6208 }
6209
6210 static bfd_byte *
6211 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6212 {
6213 p = savegpr1 (abfd, p, r);
6214 bfd_put_32 (abfd, BLR, p);
6215 return p + 4;
6216 }
6217
6218 static bfd_byte *
6219 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6220 {
6221 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6222 return p + 4;
6223 }
6224
6225 static bfd_byte *
6226 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6227 {
6228 p = restgpr1 (abfd, p, r);
6229 bfd_put_32 (abfd, BLR, p);
6230 return p + 4;
6231 }
6232
6233 static bfd_byte *
6234 savefpr (bfd *abfd, bfd_byte *p, int r)
6235 {
6236 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6237 return p + 4;
6238 }
6239
6240 static bfd_byte *
6241 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6242 {
6243 p = savefpr (abfd, p, r);
6244 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6245 p = p + 4;
6246 bfd_put_32 (abfd, BLR, p);
6247 return p + 4;
6248 }
6249
6250 static bfd_byte *
6251 restfpr (bfd *abfd, bfd_byte *p, int r)
6252 {
6253 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6254 return p + 4;
6255 }
6256
6257 static bfd_byte *
6258 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6259 {
6260 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6261 p = p + 4;
6262 p = restfpr (abfd, p, r);
6263 bfd_put_32 (abfd, MTLR_R0, p);
6264 p = p + 4;
6265 if (r == 29)
6266 {
6267 p = restfpr (abfd, p, 30);
6268 p = restfpr (abfd, p, 31);
6269 }
6270 bfd_put_32 (abfd, BLR, p);
6271 return p + 4;
6272 }
6273
6274 static bfd_byte *
6275 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6276 {
6277 p = savefpr (abfd, p, r);
6278 bfd_put_32 (abfd, BLR, p);
6279 return p + 4;
6280 }
6281
6282 static bfd_byte *
6283 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6284 {
6285 p = restfpr (abfd, p, r);
6286 bfd_put_32 (abfd, BLR, p);
6287 return p + 4;
6288 }
6289
6290 static bfd_byte *
6291 savevr (bfd *abfd, bfd_byte *p, int r)
6292 {
6293 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6294 p = p + 4;
6295 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6296 return p + 4;
6297 }
6298
6299 static bfd_byte *
6300 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6301 {
6302 p = savevr (abfd, p, r);
6303 bfd_put_32 (abfd, BLR, p);
6304 return p + 4;
6305 }
6306
6307 static bfd_byte *
6308 restvr (bfd *abfd, bfd_byte *p, int r)
6309 {
6310 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6311 p = p + 4;
6312 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6313 return p + 4;
6314 }
6315
6316 static bfd_byte *
6317 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6318 {
6319 p = restvr (abfd, p, r);
6320 bfd_put_32 (abfd, BLR, p);
6321 return p + 4;
6322 }
6323
6324 #define STDU_R1_0R1 0xf8210001
6325 #define ADDI_R1_R1 0x38210000
6326
6327 /* Emit prologue of wrapper preserving regs around a call to
6328 __tls_get_addr_opt. */
6329
6330 static bfd_byte *
6331 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6332 {
6333 unsigned int i;
6334
6335 bfd_put_32 (obfd, MFLR_R0, p);
6336 p += 4;
6337 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6338 p += 4;
6339
6340 if (htab->opd_abi)
6341 {
6342 for (i = 4; i < 12; i++)
6343 {
6344 bfd_put_32 (obfd,
6345 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6346 p += 4;
6347 }
6348 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6349 p += 4;
6350 }
6351 else
6352 {
6353 for (i = 4; i < 12; i++)
6354 {
6355 bfd_put_32 (obfd,
6356 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6357 p += 4;
6358 }
6359 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6360 p += 4;
6361 }
6362 return p;
6363 }
6364
6365 /* Emit epilogue of wrapper preserving regs around a call to
6366 __tls_get_addr_opt. */
6367
6368 static bfd_byte *
6369 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6370 {
6371 unsigned int i;
6372
6373 if (htab->opd_abi)
6374 {
6375 for (i = 4; i < 12; i++)
6376 {
6377 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6378 p += 4;
6379 }
6380 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6381 p += 4;
6382 }
6383 else
6384 {
6385 for (i = 4; i < 12; i++)
6386 {
6387 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6388 p += 4;
6389 }
6390 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6391 p += 4;
6392 }
6393 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6394 p += 4;
6395 bfd_put_32 (obfd, MTLR_R0, p);
6396 p += 4;
6397 bfd_put_32 (obfd, BLR, p);
6398 p += 4;
6399 return p;
6400 }
6401
6402 /* Called via elf_link_hash_traverse to transfer dynamic linking
6403 information on function code symbol entries to their corresponding
6404 function descriptor symbol entries. Must not be called twice for
6405 any given code symbol. */
6406
6407 static bool
6408 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6409 {
6410 struct bfd_link_info *info;
6411 struct ppc_link_hash_table *htab;
6412 struct ppc_link_hash_entry *fh;
6413 struct ppc_link_hash_entry *fdh;
6414 bool force_local;
6415
6416 fh = ppc_elf_hash_entry (h);
6417 if (fh->elf.root.type == bfd_link_hash_indirect)
6418 return true;
6419
6420 if (!fh->is_func)
6421 return true;
6422
6423 if (fh->elf.root.root.string[0] != '.'
6424 || fh->elf.root.root.string[1] == '\0')
6425 return true;
6426
6427 info = inf;
6428 htab = ppc_hash_table (info);
6429 if (htab == NULL)
6430 return false;
6431
6432 /* Find the corresponding function descriptor symbol. */
6433 fdh = lookup_fdh (fh, htab);
6434
6435 /* Resolve undefined references to dot-symbols as the value
6436 in the function descriptor, if we have one in a regular object.
6437 This is to satisfy cases like ".quad .foo". Calls to functions
6438 in dynamic objects are handled elsewhere. */
6439 if ((fh->elf.root.type == bfd_link_hash_undefined
6440 || fh->elf.root.type == bfd_link_hash_undefweak)
6441 && (fdh->elf.root.type == bfd_link_hash_defined
6442 || fdh->elf.root.type == bfd_link_hash_defweak)
6443 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6444 && opd_entry_value (fdh->elf.root.u.def.section,
6445 fdh->elf.root.u.def.value,
6446 &fh->elf.root.u.def.section,
6447 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6448 {
6449 fh->elf.root.type = fdh->elf.root.type;
6450 fh->elf.forced_local = 1;
6451 fh->elf.def_regular = fdh->elf.def_regular;
6452 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6453 }
6454
6455 if (!fh->elf.dynamic)
6456 {
6457 struct plt_entry *ent;
6458
6459 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6460 if (ent->plt.refcount > 0)
6461 break;
6462 if (ent == NULL)
6463 {
6464 if (fdh != NULL && fdh->fake)
6465 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6466 return true;
6467 }
6468 }
6469
6470 /* Create a descriptor as undefined if necessary. */
6471 if (fdh == NULL
6472 && !bfd_link_executable (info)
6473 && (fh->elf.root.type == bfd_link_hash_undefined
6474 || fh->elf.root.type == bfd_link_hash_undefweak))
6475 {
6476 fdh = make_fdh (info, fh);
6477 if (fdh == NULL)
6478 return false;
6479 }
6480
6481 /* We can't support overriding of symbols on a fake descriptor. */
6482 if (fdh != NULL
6483 && fdh->fake
6484 && (fh->elf.root.type == bfd_link_hash_defined
6485 || fh->elf.root.type == bfd_link_hash_defweak))
6486 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6487
6488 /* Transfer dynamic linking information to the function descriptor. */
6489 if (fdh != NULL)
6490 {
6491 fdh->elf.ref_regular |= fh->elf.ref_regular;
6492 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6493 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6494 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6495 fdh->elf.dynamic |= fh->elf.dynamic;
6496 fdh->elf.needs_plt |= (fh->elf.needs_plt
6497 || fh->elf.type == STT_FUNC
6498 || fh->elf.type == STT_GNU_IFUNC);
6499 move_plt_plist (fh, fdh);
6500
6501 if (!fdh->elf.forced_local
6502 && fh->elf.dynindx != -1)
6503 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6504 return false;
6505 }
6506
6507 /* Now that the info is on the function descriptor, clear the
6508 function code sym info. Any function code syms for which we
6509 don't have a definition in a regular file, we force local.
6510 This prevents a shared library from exporting syms that have
6511 been imported from another library. Function code syms that
6512 are really in the library we must leave global to prevent the
6513 linker dragging in a definition from a static library. */
6514 force_local = (!fh->elf.def_regular
6515 || fdh == NULL
6516 || !fdh->elf.def_regular
6517 || fdh->elf.forced_local);
6518 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6519
6520 return true;
6521 }
6522
6523 static const struct sfpr_def_parms save_res_funcs[] =
6524 {
6525 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6526 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6527 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6528 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6529 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6530 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6531 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6532 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6533 { "._savef", 14, 31, savefpr, savefpr1_tail },
6534 { "._restf", 14, 31, restfpr, restfpr1_tail },
6535 { "_savevr_", 20, 31, savevr, savevr_tail },
6536 { "_restvr_", 20, 31, restvr, restvr_tail }
6537 };
6538
6539 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6540 this hook to a) run the edit functions in this file, b) provide
6541 some gcc support functions, and c) transfer dynamic linking
6542 information gathered so far on function code symbol entries, to
6543 their corresponding function descriptor symbol entries. */
6544
6545 static bool
6546 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6547 {
6548 struct ppc_link_hash_table *htab;
6549
6550 htab = ppc_hash_table (info);
6551 if (htab == NULL)
6552 return false;
6553
6554 /* Call back into the linker, which then runs the edit functions. */
6555 htab->params->edit ();
6556
6557 /* Provide any missing _save* and _rest* functions. */
6558 if (htab->sfpr != NULL)
6559 {
6560 unsigned int i;
6561
6562 htab->sfpr->size = 0;
6563 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6564 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6565 return false;
6566 if (htab->sfpr->size == 0)
6567 htab->sfpr->flags |= SEC_EXCLUDE;
6568 }
6569
6570 if (bfd_link_relocatable (info))
6571 return true;
6572
6573 if (htab->elf.hgot != NULL)
6574 {
6575 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6576 /* Make .TOC. defined so as to prevent it being made dynamic.
6577 The wrong value here is fixed later in ppc64_elf_set_toc. */
6578 if (!htab->elf.hgot->def_regular
6579 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6580 {
6581 htab->elf.hgot->root.type = bfd_link_hash_defined;
6582 htab->elf.hgot->root.u.def.value = 0;
6583 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6584 htab->elf.hgot->def_regular = 1;
6585 htab->elf.hgot->root.linker_def = 1;
6586 }
6587 htab->elf.hgot->type = STT_OBJECT;
6588 htab->elf.hgot->other
6589 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6590 }
6591
6592 return true;
6593 }
6594
6595 /* Return true if we have dynamic relocs against H or any of its weak
6596 aliases, that apply to read-only sections. Cannot be used after
6597 size_dynamic_sections. */
6598
6599 static bool
6600 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6601 {
6602 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6603 do
6604 {
6605 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6606 return true;
6607 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6608 }
6609 while (eh != NULL && &eh->elf != h);
6610
6611 return false;
6612 }
6613
6614 /* Return whether EH has pc-relative dynamic relocs. */
6615
6616 static bool
6617 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6618 {
6619 struct ppc_dyn_relocs *p;
6620
6621 for (p = (struct ppc_dyn_relocs *) eh->elf.dyn_relocs; p != NULL; p = p->next)
6622 if (p->pc_count != 0)
6623 return true;
6624 return false;
6625 }
6626
6627 /* Return true if a global entry stub will be created for H. Valid
6628 for ELFv2 before plt entries have been allocated. */
6629
6630 static bool
6631 global_entry_stub (struct elf_link_hash_entry *h)
6632 {
6633 struct plt_entry *pent;
6634
6635 if (!h->pointer_equality_needed
6636 || h->def_regular)
6637 return false;
6638
6639 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6640 if (pent->plt.refcount > 0
6641 && pent->addend == 0)
6642 return true;
6643
6644 return false;
6645 }
6646
6647 /* Adjust a symbol defined by a dynamic object and referenced by a
6648 regular object. The current definition is in some section of the
6649 dynamic object, but we're not including those sections. We have to
6650 change the definition to something the rest of the link can
6651 understand. */
6652
6653 static bool
6654 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6655 struct elf_link_hash_entry *h)
6656 {
6657 struct ppc_link_hash_table *htab;
6658 asection *s, *srel;
6659
6660 htab = ppc_hash_table (info);
6661 if (htab == NULL)
6662 return false;
6663
6664 /* Deal with function syms. */
6665 if (h->type == STT_FUNC
6666 || h->type == STT_GNU_IFUNC
6667 || h->needs_plt)
6668 {
6669 bool local = (ppc_elf_hash_entry (h)->save_res
6670 || SYMBOL_CALLS_LOCAL (info, h)
6671 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6672 /* Discard dyn_relocs when non-pic if we've decided that a
6673 function symbol is local and not an ifunc. We keep dynamic
6674 relocs for ifuncs when local rather than always emitting a
6675 plt call stub for them and defining the symbol on the call
6676 stub. We can't do that for ELFv1 anyway (a function symbol
6677 is defined on a descriptor, not code) and it can be faster at
6678 run-time due to not needing to bounce through a stub. The
6679 dyn_relocs for ifuncs will be applied even in a static
6680 executable. */
6681 if (!bfd_link_pic (info)
6682 && h->type != STT_GNU_IFUNC
6683 && local)
6684 h->dyn_relocs = NULL;
6685
6686 /* Clear procedure linkage table information for any symbol that
6687 won't need a .plt entry. */
6688 struct plt_entry *ent;
6689 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6690 if (ent->plt.refcount > 0)
6691 break;
6692 if (ent == NULL
6693 || (h->type != STT_GNU_IFUNC
6694 && local
6695 && (htab->can_convert_all_inline_plt
6696 || (ppc_elf_hash_entry (h)->tls_mask
6697 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6698 {
6699 h->plt.plist = NULL;
6700 h->needs_plt = 0;
6701 h->pointer_equality_needed = 0;
6702 }
6703 else if (abiversion (info->output_bfd) >= 2)
6704 {
6705 /* Taking a function's address in a read/write section
6706 doesn't require us to define the function symbol in the
6707 executable on a global entry stub. A dynamic reloc can
6708 be used instead. The reason we prefer a few more dynamic
6709 relocs is that calling via a global entry stub costs a
6710 few more instructions, and pointer_equality_needed causes
6711 extra work in ld.so when resolving these symbols. */
6712 if (global_entry_stub (h))
6713 {
6714 if (!_bfd_elf_readonly_dynrelocs (h))
6715 {
6716 h->pointer_equality_needed = 0;
6717 /* If we haven't seen a branch reloc and the symbol
6718 isn't an ifunc then we don't need a plt entry. */
6719 if (!h->needs_plt)
6720 h->plt.plist = NULL;
6721 }
6722 else if (!bfd_link_pic (info))
6723 /* We are going to be defining the function symbol on the
6724 plt stub, so no dyn_relocs needed when non-pic. */
6725 h->dyn_relocs = NULL;
6726 }
6727
6728 /* ELFv2 function symbols can't have copy relocs. */
6729 return true;
6730 }
6731 else if (!h->needs_plt
6732 && !_bfd_elf_readonly_dynrelocs (h))
6733 {
6734 /* If we haven't seen a branch reloc and the symbol isn't an
6735 ifunc then we don't need a plt entry. */
6736 h->plt.plist = NULL;
6737 h->pointer_equality_needed = 0;
6738 return true;
6739 }
6740 }
6741 else
6742 h->plt.plist = NULL;
6743
6744 /* If this is a weak symbol, and there is a real definition, the
6745 processor independent code will have arranged for us to see the
6746 real definition first, and we can just use the same value. */
6747 if (h->is_weakalias)
6748 {
6749 struct elf_link_hash_entry *def = weakdef (h);
6750 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6751 h->root.u.def.section = def->root.u.def.section;
6752 h->root.u.def.value = def->root.u.def.value;
6753 if (def->root.u.def.section == htab->elf.sdynbss
6754 || def->root.u.def.section == htab->elf.sdynrelro)
6755 h->dyn_relocs = NULL;
6756 return true;
6757 }
6758
6759 /* If we are creating a shared library, we must presume that the
6760 only references to the symbol are via the global offset table.
6761 For such cases we need not do anything here; the relocations will
6762 be handled correctly by relocate_section. */
6763 if (!bfd_link_executable (info))
6764 return true;
6765
6766 /* If there are no references to this symbol that do not use the
6767 GOT, we don't need to generate a copy reloc. */
6768 if (!h->non_got_ref)
6769 return true;
6770
6771 /* Don't generate a copy reloc for symbols defined in the executable. */
6772 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6773
6774 /* If -z nocopyreloc was given, don't generate them either. */
6775 || info->nocopyreloc
6776
6777 /* If we don't find any dynamic relocs in read-only sections, then
6778 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6779 || (ELIMINATE_COPY_RELOCS
6780 && !h->needs_copy
6781 && !alias_readonly_dynrelocs (h))
6782
6783 /* Protected variables do not work with .dynbss. The copy in
6784 .dynbss won't be used by the shared library with the protected
6785 definition for the variable. Text relocations are preferable
6786 to an incorrect program. */
6787 || h->protected_def)
6788 return true;
6789
6790 if (h->type == STT_FUNC
6791 || h->type == STT_GNU_IFUNC)
6792 {
6793 /* .dynbss copies of function symbols only work if we have
6794 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6795 use dot-symbols and set the function symbol size to the text
6796 size of the function rather than the size of the descriptor.
6797 That's wrong for copying a descriptor. */
6798 if (ppc_elf_hash_entry (h)->oh == NULL
6799 || !(h->size == 24 || h->size == 16))
6800 return true;
6801
6802 /* We should never get here, but unfortunately there are old
6803 versions of gcc (circa gcc-3.2) that improperly for the
6804 ELFv1 ABI put initialized function pointers, vtable refs and
6805 suchlike in read-only sections. Allow them to proceed, but
6806 warn that this might break at runtime. */
6807 info->callbacks->einfo
6808 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6809 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6810 h->root.root.string);
6811 }
6812
6813 /* This is a reference to a symbol defined by a dynamic object which
6814 is not a function. */
6815
6816 /* We must allocate the symbol in our .dynbss section, which will
6817 become part of the .bss section of the executable. There will be
6818 an entry for this symbol in the .dynsym section. The dynamic
6819 object will contain position independent code, so all references
6820 from the dynamic object to this symbol will go through the global
6821 offset table. The dynamic linker will use the .dynsym entry to
6822 determine the address it must put in the global offset table, so
6823 both the dynamic object and the regular object will refer to the
6824 same memory location for the variable. */
6825 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6826 {
6827 s = htab->elf.sdynrelro;
6828 srel = htab->elf.sreldynrelro;
6829 }
6830 else
6831 {
6832 s = htab->elf.sdynbss;
6833 srel = htab->elf.srelbss;
6834 }
6835 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6836 {
6837 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6838 linker to copy the initial value out of the dynamic object
6839 and into the runtime process image. */
6840 srel->size += sizeof (Elf64_External_Rela);
6841 h->needs_copy = 1;
6842 }
6843
6844 /* We no longer want dyn_relocs. */
6845 h->dyn_relocs = NULL;
6846 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6847 }
6848
6849 /* If given a function descriptor symbol, hide both the function code
6850 sym and the descriptor. */
6851 static void
6852 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6853 struct elf_link_hash_entry *h,
6854 bool force_local)
6855 {
6856 struct ppc_link_hash_entry *eh;
6857 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6858
6859 if (ppc_hash_table (info) == NULL)
6860 return;
6861
6862 eh = ppc_elf_hash_entry (h);
6863 if (eh->is_func_descriptor)
6864 {
6865 struct ppc_link_hash_entry *fh = eh->oh;
6866
6867 if (fh == NULL)
6868 {
6869 const char *p, *q;
6870 struct elf_link_hash_table *htab = elf_hash_table (info);
6871 char save;
6872
6873 /* We aren't supposed to use alloca in BFD because on
6874 systems which do not have alloca the version in libiberty
6875 calls xmalloc, which might cause the program to crash
6876 when it runs out of memory. This function doesn't have a
6877 return status, so there's no way to gracefully return an
6878 error. So cheat. We know that string[-1] can be safely
6879 accessed; It's either a string in an ELF string table,
6880 or allocated in an objalloc structure. */
6881
6882 p = eh->elf.root.root.string - 1;
6883 save = *p;
6884 *(char *) p = '.';
6885 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6886 false, false));
6887 *(char *) p = save;
6888
6889 /* Unfortunately, if it so happens that the string we were
6890 looking for was allocated immediately before this string,
6891 then we overwrote the string terminator. That's the only
6892 reason the lookup should fail. */
6893 if (fh == NULL)
6894 {
6895 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6896 while (q >= eh->elf.root.root.string && *q == *p)
6897 --q, --p;
6898 if (q < eh->elf.root.root.string && *p == '.')
6899 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6900 false, false));
6901 }
6902 if (fh != NULL)
6903 {
6904 eh->oh = fh;
6905 fh->oh = eh;
6906 }
6907 }
6908 if (fh != NULL)
6909 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6910 }
6911 }
6912
6913 static bool
6914 get_sym_h (struct elf_link_hash_entry **hp,
6915 Elf_Internal_Sym **symp,
6916 asection **symsecp,
6917 unsigned char **tls_maskp,
6918 Elf_Internal_Sym **locsymsp,
6919 unsigned long r_symndx,
6920 bfd *ibfd)
6921 {
6922 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6923
6924 if (r_symndx >= symtab_hdr->sh_info)
6925 {
6926 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6927 struct elf_link_hash_entry *h;
6928
6929 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6930 h = elf_follow_link (h);
6931
6932 if (hp != NULL)
6933 *hp = h;
6934
6935 if (symp != NULL)
6936 *symp = NULL;
6937
6938 if (symsecp != NULL)
6939 {
6940 asection *symsec = NULL;
6941 if (h->root.type == bfd_link_hash_defined
6942 || h->root.type == bfd_link_hash_defweak)
6943 symsec = h->root.u.def.section;
6944 *symsecp = symsec;
6945 }
6946
6947 if (tls_maskp != NULL)
6948 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6949 }
6950 else
6951 {
6952 Elf_Internal_Sym *sym;
6953 Elf_Internal_Sym *locsyms = *locsymsp;
6954
6955 if (locsyms == NULL)
6956 {
6957 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6958 if (locsyms == NULL)
6959 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6960 symtab_hdr->sh_info,
6961 0, NULL, NULL, NULL);
6962 if (locsyms == NULL)
6963 return false;
6964 *locsymsp = locsyms;
6965 }
6966 sym = locsyms + r_symndx;
6967
6968 if (hp != NULL)
6969 *hp = NULL;
6970
6971 if (symp != NULL)
6972 *symp = sym;
6973
6974 if (symsecp != NULL)
6975 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6976
6977 if (tls_maskp != NULL)
6978 {
6979 struct got_entry **lgot_ents;
6980 unsigned char *tls_mask;
6981
6982 tls_mask = NULL;
6983 lgot_ents = elf_local_got_ents (ibfd);
6984 if (lgot_ents != NULL)
6985 {
6986 struct plt_entry **local_plt = (struct plt_entry **)
6987 (lgot_ents + symtab_hdr->sh_info);
6988 unsigned char *lgot_masks = (unsigned char *)
6989 (local_plt + symtab_hdr->sh_info);
6990 tls_mask = &lgot_masks[r_symndx];
6991 }
6992 *tls_maskp = tls_mask;
6993 }
6994 }
6995 return true;
6996 }
6997
6998 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6999 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
7000 type suitable for optimization, and 1 otherwise. */
7001
7002 static int
7003 get_tls_mask (unsigned char **tls_maskp,
7004 unsigned long *toc_symndx,
7005 bfd_vma *toc_addend,
7006 Elf_Internal_Sym **locsymsp,
7007 const Elf_Internal_Rela *rel,
7008 bfd *ibfd)
7009 {
7010 unsigned long r_symndx;
7011 int next_r;
7012 struct elf_link_hash_entry *h;
7013 Elf_Internal_Sym *sym;
7014 asection *sec;
7015 bfd_vma off;
7016
7017 r_symndx = ELF64_R_SYM (rel->r_info);
7018 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7019 return 0;
7020
7021 if ((*tls_maskp != NULL
7022 && (**tls_maskp & TLS_TLS) != 0
7023 && **tls_maskp != (TLS_TLS | TLS_MARK))
7024 || sec == NULL
7025 || ppc64_elf_section_data (sec) == NULL
7026 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
7027 return 1;
7028
7029 /* Look inside a TOC section too. */
7030 if (h != NULL)
7031 {
7032 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
7033 off = h->root.u.def.value;
7034 }
7035 else
7036 off = sym->st_value;
7037 off += rel->r_addend;
7038 BFD_ASSERT (off % 8 == 0);
7039 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7040 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7041 if (toc_symndx != NULL)
7042 *toc_symndx = r_symndx;
7043 if (toc_addend != NULL)
7044 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7045 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7046 return 0;
7047 if ((h == NULL || is_static_defined (h))
7048 && (next_r == -1 || next_r == -2))
7049 return 1 - next_r;
7050 return 1;
7051 }
7052
7053 /* Find (or create) an entry in the tocsave hash table. */
7054
7055 static struct tocsave_entry *
7056 tocsave_find (struct ppc_link_hash_table *htab,
7057 enum insert_option insert,
7058 Elf_Internal_Sym **local_syms,
7059 const Elf_Internal_Rela *irela,
7060 bfd *ibfd)
7061 {
7062 unsigned long r_indx;
7063 struct elf_link_hash_entry *h;
7064 Elf_Internal_Sym *sym;
7065 struct tocsave_entry ent, *p;
7066 hashval_t hash;
7067 struct tocsave_entry **slot;
7068
7069 r_indx = ELF64_R_SYM (irela->r_info);
7070 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7071 return NULL;
7072 if (ent.sec == NULL || ent.sec->output_section == NULL)
7073 {
7074 _bfd_error_handler
7075 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7076 return NULL;
7077 }
7078
7079 if (h != NULL)
7080 ent.offset = h->root.u.def.value;
7081 else
7082 ent.offset = sym->st_value;
7083 ent.offset += irela->r_addend;
7084
7085 hash = tocsave_htab_hash (&ent);
7086 slot = ((struct tocsave_entry **)
7087 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7088 if (slot == NULL)
7089 return NULL;
7090
7091 if (*slot == NULL)
7092 {
7093 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7094 if (p == NULL)
7095 return NULL;
7096 *p = ent;
7097 *slot = p;
7098 }
7099 return *slot;
7100 }
7101
7102 /* Adjust all global syms defined in opd sections. In gcc generated
7103 code for the old ABI, these will already have been done. */
7104
7105 static bool
7106 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7107 {
7108 struct ppc_link_hash_entry *eh;
7109 asection *sym_sec;
7110 struct _opd_sec_data *opd;
7111
7112 if (h->root.type == bfd_link_hash_indirect)
7113 return true;
7114
7115 if (h->root.type != bfd_link_hash_defined
7116 && h->root.type != bfd_link_hash_defweak)
7117 return true;
7118
7119 eh = ppc_elf_hash_entry (h);
7120 if (eh->adjust_done)
7121 return true;
7122
7123 sym_sec = eh->elf.root.u.def.section;
7124 opd = get_opd_info (sym_sec);
7125 if (opd != NULL && opd->adjust != NULL)
7126 {
7127 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7128 if (adjust == -1)
7129 {
7130 /* This entry has been deleted. */
7131 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7132 if (dsec == NULL)
7133 {
7134 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7135 if (discarded_section (dsec))
7136 {
7137 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7138 break;
7139 }
7140 }
7141 eh->elf.root.u.def.value = 0;
7142 eh->elf.root.u.def.section = dsec;
7143 }
7144 else
7145 eh->elf.root.u.def.value += adjust;
7146 eh->adjust_done = 1;
7147 }
7148 return true;
7149 }
7150
7151 /* Handles decrementing dynamic reloc counts for the reloc specified by
7152 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7153 have already been determined. */
7154
7155 static bool
7156 dec_dynrel_count (const Elf_Internal_Rela *rel,
7157 asection *sec,
7158 struct bfd_link_info *info,
7159 Elf_Internal_Sym **local_syms,
7160 struct elf_link_hash_entry *h,
7161 Elf_Internal_Sym *sym)
7162 {
7163 enum elf_ppc64_reloc_type r_type;
7164 asection *sym_sec = NULL;
7165
7166 /* Can this reloc be dynamic? This switch, and later tests here
7167 should be kept in sync with the code in check_relocs. */
7168 r_type = ELF64_R_TYPE (rel->r_info);
7169 switch (r_type)
7170 {
7171 default:
7172 return true;
7173
7174 case R_PPC64_TOC16:
7175 case R_PPC64_TOC16_DS:
7176 case R_PPC64_TOC16_LO:
7177 case R_PPC64_TOC16_HI:
7178 case R_PPC64_TOC16_HA:
7179 case R_PPC64_TOC16_LO_DS:
7180 if (h == NULL)
7181 return true;
7182 break;
7183
7184 case R_PPC64_TPREL16:
7185 case R_PPC64_TPREL16_LO:
7186 case R_PPC64_TPREL16_HI:
7187 case R_PPC64_TPREL16_HA:
7188 case R_PPC64_TPREL16_DS:
7189 case R_PPC64_TPREL16_LO_DS:
7190 case R_PPC64_TPREL16_HIGH:
7191 case R_PPC64_TPREL16_HIGHA:
7192 case R_PPC64_TPREL16_HIGHER:
7193 case R_PPC64_TPREL16_HIGHERA:
7194 case R_PPC64_TPREL16_HIGHEST:
7195 case R_PPC64_TPREL16_HIGHESTA:
7196 case R_PPC64_TPREL64:
7197 case R_PPC64_TPREL34:
7198 case R_PPC64_DTPMOD64:
7199 case R_PPC64_DTPREL64:
7200 case R_PPC64_ADDR64:
7201 case R_PPC64_REL30:
7202 case R_PPC64_REL32:
7203 case R_PPC64_REL64:
7204 case R_PPC64_ADDR14:
7205 case R_PPC64_ADDR14_BRNTAKEN:
7206 case R_PPC64_ADDR14_BRTAKEN:
7207 case R_PPC64_ADDR16:
7208 case R_PPC64_ADDR16_DS:
7209 case R_PPC64_ADDR16_HA:
7210 case R_PPC64_ADDR16_HI:
7211 case R_PPC64_ADDR16_HIGH:
7212 case R_PPC64_ADDR16_HIGHA:
7213 case R_PPC64_ADDR16_HIGHER:
7214 case R_PPC64_ADDR16_HIGHERA:
7215 case R_PPC64_ADDR16_HIGHEST:
7216 case R_PPC64_ADDR16_HIGHESTA:
7217 case R_PPC64_ADDR16_LO:
7218 case R_PPC64_ADDR16_LO_DS:
7219 case R_PPC64_ADDR24:
7220 case R_PPC64_ADDR32:
7221 case R_PPC64_UADDR16:
7222 case R_PPC64_UADDR32:
7223 case R_PPC64_UADDR64:
7224 case R_PPC64_TOC:
7225 case R_PPC64_D34:
7226 case R_PPC64_D34_LO:
7227 case R_PPC64_D34_HI30:
7228 case R_PPC64_D34_HA30:
7229 case R_PPC64_ADDR16_HIGHER34:
7230 case R_PPC64_ADDR16_HIGHERA34:
7231 case R_PPC64_ADDR16_HIGHEST34:
7232 case R_PPC64_ADDR16_HIGHESTA34:
7233 case R_PPC64_D28:
7234 break;
7235 }
7236
7237 if (local_syms != NULL)
7238 {
7239 unsigned long r_symndx;
7240 bfd *ibfd = sec->owner;
7241
7242 r_symndx = ELF64_R_SYM (rel->r_info);
7243 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7244 return false;
7245 }
7246
7247 if ((h != NULL
7248 && (h->root.type == bfd_link_hash_defweak
7249 || (!h->def_regular && !h->root.ldscript_def)))
7250 || (h != NULL
7251 && !SYMBOL_REFERENCES_LOCAL (info, h))
7252 || (bfd_link_pic (info)
7253 && (h != NULL
7254 ? !bfd_is_abs_symbol (&h->root)
7255 : sym_sec != bfd_abs_section_ptr)
7256 && must_be_dyn_reloc (info, r_type))
7257 || (!bfd_link_pic (info)
7258 && (h != NULL
7259 ? h->type == STT_GNU_IFUNC
7260 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7261 ;
7262 else
7263 return true;
7264
7265 if (h != NULL)
7266 {
7267 struct ppc_dyn_relocs *p;
7268 struct ppc_dyn_relocs **pp;
7269 pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
7270
7271 /* elf_gc_sweep may have already removed all dyn relocs associated
7272 with local syms for a given section. Also, symbol flags are
7273 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7274 report a dynreloc miscount. */
7275 if (*pp == NULL && info->gc_sections)
7276 return true;
7277
7278 while ((p = *pp) != NULL)
7279 {
7280 if (p->sec == sec)
7281 {
7282 if (!must_be_dyn_reloc (info, r_type))
7283 p->pc_count -= 1;
7284 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7285 && rel->r_offset % 2 == 0
7286 && sec->alignment_power != 0)
7287 p->rel_count -= 1;
7288 p->count -= 1;
7289 if (p->count == 0)
7290 *pp = p->next;
7291 return true;
7292 }
7293 pp = &p->next;
7294 }
7295 }
7296 else
7297 {
7298 struct ppc_local_dyn_relocs *p;
7299 struct ppc_local_dyn_relocs **pp;
7300 void *vpp;
7301 bool is_ifunc;
7302
7303 if (local_syms == NULL)
7304 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7305 if (sym_sec == NULL)
7306 sym_sec = sec;
7307
7308 vpp = &elf_section_data (sym_sec)->local_dynrel;
7309 pp = (struct ppc_local_dyn_relocs **) vpp;
7310
7311 if (*pp == NULL && info->gc_sections)
7312 return true;
7313
7314 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7315 while ((p = *pp) != NULL)
7316 {
7317 if (p->sec == sec && p->ifunc == is_ifunc)
7318 {
7319 if ((r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
7320 && rel->r_offset % 2 == 0
7321 && sec->alignment_power != 0)
7322 p->rel_count -= 1;
7323 p->count -= 1;
7324 if (p->count == 0)
7325 *pp = p->next;
7326 return true;
7327 }
7328 pp = &p->next;
7329 }
7330 }
7331
7332 /* xgettext:c-format */
7333 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7334 sec->owner, sec);
7335 bfd_set_error (bfd_error_bad_value);
7336 return false;
7337 }
7338
7339 /* Remove unused Official Procedure Descriptor entries. Currently we
7340 only remove those associated with functions in discarded link-once
7341 sections, or weakly defined functions that have been overridden. It
7342 would be possible to remove many more entries for statically linked
7343 applications. */
7344
7345 bool
7346 ppc64_elf_edit_opd (struct bfd_link_info *info)
7347 {
7348 bfd *ibfd;
7349 bool some_edited = false;
7350 asection *need_pad = NULL;
7351 struct ppc_link_hash_table *htab;
7352
7353 htab = ppc_hash_table (info);
7354 if (htab == NULL)
7355 return false;
7356
7357 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7358 {
7359 asection *sec;
7360 Elf_Internal_Rela *relstart, *rel, *relend;
7361 Elf_Internal_Shdr *symtab_hdr;
7362 Elf_Internal_Sym *local_syms;
7363 struct _opd_sec_data *opd;
7364 bool need_edit, add_aux_fields, broken;
7365 bfd_size_type cnt_16b = 0;
7366
7367 if (!is_ppc64_elf (ibfd))
7368 continue;
7369
7370 sec = bfd_get_section_by_name (ibfd, ".opd");
7371 if (sec == NULL || sec->size == 0)
7372 continue;
7373
7374 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7375 continue;
7376
7377 if (sec->output_section == bfd_abs_section_ptr)
7378 continue;
7379
7380 /* Look through the section relocs. */
7381 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7382 continue;
7383
7384 local_syms = NULL;
7385 symtab_hdr = &elf_symtab_hdr (ibfd);
7386
7387 /* Read the relocations. */
7388 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7389 info->keep_memory);
7390 if (relstart == NULL)
7391 return false;
7392
7393 /* First run through the relocs to check they are sane, and to
7394 determine whether we need to edit this opd section. */
7395 need_edit = false;
7396 broken = false;
7397 need_pad = sec;
7398 relend = relstart + sec->reloc_count;
7399 for (rel = relstart; rel < relend; )
7400 {
7401 enum elf_ppc64_reloc_type r_type;
7402 unsigned long r_symndx;
7403 asection *sym_sec;
7404 struct elf_link_hash_entry *h;
7405 Elf_Internal_Sym *sym;
7406 bfd_vma offset;
7407
7408 /* .opd contains an array of 16 or 24 byte entries. We're
7409 only interested in the reloc pointing to a function entry
7410 point. */
7411 offset = rel->r_offset;
7412 if (rel + 1 == relend
7413 || rel[1].r_offset != offset + 8)
7414 {
7415 /* If someone messes with .opd alignment then after a
7416 "ld -r" we might have padding in the middle of .opd.
7417 Also, there's nothing to prevent someone putting
7418 something silly in .opd with the assembler. No .opd
7419 optimization for them! */
7420 broken_opd:
7421 _bfd_error_handler
7422 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7423 broken = true;
7424 break;
7425 }
7426
7427 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7428 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7429 {
7430 _bfd_error_handler
7431 /* xgettext:c-format */
7432 (_("%pB: unexpected reloc type %u in .opd section"),
7433 ibfd, r_type);
7434 broken = true;
7435 break;
7436 }
7437
7438 r_symndx = ELF64_R_SYM (rel->r_info);
7439 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7440 r_symndx, ibfd))
7441 goto error_ret;
7442
7443 if (sym_sec == NULL || sym_sec->owner == NULL)
7444 {
7445 const char *sym_name;
7446 if (h != NULL)
7447 sym_name = h->root.root.string;
7448 else
7449 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7450 sym_sec);
7451
7452 _bfd_error_handler
7453 /* xgettext:c-format */
7454 (_("%pB: undefined sym `%s' in .opd section"),
7455 ibfd, sym_name);
7456 broken = true;
7457 break;
7458 }
7459
7460 /* opd entries are always for functions defined in the
7461 current input bfd. If the symbol isn't defined in the
7462 input bfd, then we won't be using the function in this
7463 bfd; It must be defined in a linkonce section in another
7464 bfd, or is weak. It's also possible that we are
7465 discarding the function due to a linker script /DISCARD/,
7466 which we test for via the output_section. */
7467 if (sym_sec->owner != ibfd
7468 || sym_sec->output_section == bfd_abs_section_ptr)
7469 need_edit = true;
7470
7471 rel += 2;
7472 if (rel + 1 == relend
7473 || (rel + 2 < relend
7474 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7475 ++rel;
7476
7477 if (rel == relend)
7478 {
7479 if (sec->size == offset + 24)
7480 {
7481 need_pad = NULL;
7482 break;
7483 }
7484 if (sec->size == offset + 16)
7485 {
7486 cnt_16b++;
7487 break;
7488 }
7489 goto broken_opd;
7490 }
7491 else if (rel + 1 < relend
7492 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7493 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7494 {
7495 if (rel[0].r_offset == offset + 16)
7496 cnt_16b++;
7497 else if (rel[0].r_offset != offset + 24)
7498 goto broken_opd;
7499 }
7500 else
7501 goto broken_opd;
7502 }
7503
7504 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7505
7506 if (!broken && (need_edit || add_aux_fields))
7507 {
7508 Elf_Internal_Rela *write_rel;
7509 Elf_Internal_Shdr *rel_hdr;
7510 bfd_byte *rptr, *wptr;
7511 bfd_byte *new_contents;
7512 bfd_size_type amt;
7513
7514 new_contents = NULL;
7515 amt = OPD_NDX (sec->size) * sizeof (long);
7516 opd = &ppc64_elf_section_data (sec)->u.opd;
7517 opd->adjust = bfd_zalloc (sec->owner, amt);
7518 if (opd->adjust == NULL)
7519 return false;
7520
7521 /* This seems a waste of time as input .opd sections are all
7522 zeros as generated by gcc, but I suppose there's no reason
7523 this will always be so. We might start putting something in
7524 the third word of .opd entries. */
7525 if ((sec->flags & SEC_IN_MEMORY) == 0)
7526 {
7527 bfd_byte *loc;
7528 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7529 {
7530 free (loc);
7531 error_ret:
7532 if (symtab_hdr->contents != (unsigned char *) local_syms)
7533 free (local_syms);
7534 if (elf_section_data (sec)->relocs != relstart)
7535 free (relstart);
7536 return false;
7537 }
7538 sec->contents = loc;
7539 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7540 }
7541
7542 elf_section_data (sec)->relocs = relstart;
7543
7544 new_contents = sec->contents;
7545 if (add_aux_fields)
7546 {
7547 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7548 if (new_contents == NULL)
7549 return false;
7550 need_pad = NULL;
7551 }
7552 wptr = new_contents;
7553 rptr = sec->contents;
7554 write_rel = relstart;
7555 for (rel = relstart; rel < relend; )
7556 {
7557 unsigned long r_symndx;
7558 asection *sym_sec;
7559 struct elf_link_hash_entry *h;
7560 struct ppc_link_hash_entry *fdh = NULL;
7561 Elf_Internal_Sym *sym;
7562 long opd_ent_size;
7563 Elf_Internal_Rela *next_rel;
7564 bool skip;
7565
7566 r_symndx = ELF64_R_SYM (rel->r_info);
7567 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7568 r_symndx, ibfd))
7569 goto error_ret;
7570
7571 next_rel = rel + 2;
7572 if (next_rel + 1 == relend
7573 || (next_rel + 2 < relend
7574 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7575 ++next_rel;
7576
7577 /* See if the .opd entry is full 24 byte or
7578 16 byte (with fd_aux entry overlapped with next
7579 fd_func). */
7580 opd_ent_size = 24;
7581 if (next_rel == relend)
7582 {
7583 if (sec->size == rel->r_offset + 16)
7584 opd_ent_size = 16;
7585 }
7586 else if (next_rel->r_offset == rel->r_offset + 16)
7587 opd_ent_size = 16;
7588
7589 if (h != NULL
7590 && h->root.root.string[0] == '.')
7591 {
7592 fdh = ppc_elf_hash_entry (h)->oh;
7593 if (fdh != NULL)
7594 {
7595 fdh = ppc_follow_link (fdh);
7596 if (fdh->elf.root.type != bfd_link_hash_defined
7597 && fdh->elf.root.type != bfd_link_hash_defweak)
7598 fdh = NULL;
7599 }
7600 }
7601
7602 skip = (sym_sec->owner != ibfd
7603 || sym_sec->output_section == bfd_abs_section_ptr);
7604 if (skip)
7605 {
7606 if (fdh != NULL && sym_sec->owner == ibfd)
7607 {
7608 /* Arrange for the function descriptor sym
7609 to be dropped. */
7610 fdh->elf.root.u.def.value = 0;
7611 fdh->elf.root.u.def.section = sym_sec;
7612 }
7613 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7614
7615 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7616 rel = next_rel;
7617 else
7618 while (1)
7619 {
7620 if (!dec_dynrel_count (rel, sec, info,
7621 NULL, h, sym))
7622 goto error_ret;
7623
7624 if (++rel == next_rel)
7625 break;
7626
7627 r_symndx = ELF64_R_SYM (rel->r_info);
7628 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7629 r_symndx, ibfd))
7630 goto error_ret;
7631 }
7632 }
7633 else
7634 {
7635 /* We'll be keeping this opd entry. */
7636 long adjust;
7637
7638 if (fdh != NULL)
7639 {
7640 /* Redefine the function descriptor symbol to
7641 this location in the opd section. It is
7642 necessary to update the value here rather
7643 than using an array of adjustments as we do
7644 for local symbols, because various places
7645 in the generic ELF code use the value
7646 stored in u.def.value. */
7647 fdh->elf.root.u.def.value = wptr - new_contents;
7648 fdh->adjust_done = 1;
7649 }
7650
7651 /* Local syms are a bit tricky. We could
7652 tweak them as they can be cached, but
7653 we'd need to look through the local syms
7654 for the function descriptor sym which we
7655 don't have at the moment. So keep an
7656 array of adjustments. */
7657 adjust = (wptr - new_contents) - (rptr - sec->contents);
7658 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7659
7660 if (wptr != rptr)
7661 memcpy (wptr, rptr, opd_ent_size);
7662 wptr += opd_ent_size;
7663 if (add_aux_fields && opd_ent_size == 16)
7664 {
7665 memset (wptr, '\0', 8);
7666 wptr += 8;
7667 }
7668
7669 /* We need to adjust any reloc offsets to point to the
7670 new opd entries. */
7671 for ( ; rel != next_rel; ++rel)
7672 {
7673 rel->r_offset += adjust;
7674 if (write_rel != rel)
7675 memcpy (write_rel, rel, sizeof (*rel));
7676 ++write_rel;
7677 }
7678 }
7679
7680 rptr += opd_ent_size;
7681 }
7682
7683 sec->size = wptr - new_contents;
7684 sec->reloc_count = write_rel - relstart;
7685 if (add_aux_fields)
7686 {
7687 free (sec->contents);
7688 sec->contents = new_contents;
7689 }
7690
7691 /* Fudge the header size too, as this is used later in
7692 elf_bfd_final_link if we are emitting relocs. */
7693 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7694 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7695 some_edited = true;
7696 }
7697 else if (elf_section_data (sec)->relocs != relstart)
7698 free (relstart);
7699
7700 if (local_syms != NULL
7701 && symtab_hdr->contents != (unsigned char *) local_syms)
7702 {
7703 if (!info->keep_memory)
7704 free (local_syms);
7705 else
7706 symtab_hdr->contents = (unsigned char *) local_syms;
7707 }
7708 }
7709
7710 if (some_edited)
7711 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7712
7713 /* If we are doing a final link and the last .opd entry is just 16 byte
7714 long, add a 8 byte padding after it. */
7715 if (need_pad != NULL && !bfd_link_relocatable (info))
7716 {
7717 bfd_byte *p;
7718
7719 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7720 {
7721 BFD_ASSERT (need_pad->size > 0);
7722
7723 p = bfd_malloc (need_pad->size + 8);
7724 if (p == NULL)
7725 return false;
7726
7727 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7728 p, 0, need_pad->size))
7729 return false;
7730
7731 need_pad->contents = p;
7732 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7733 }
7734 else
7735 {
7736 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7737 if (p == NULL)
7738 return false;
7739
7740 need_pad->contents = p;
7741 }
7742
7743 memset (need_pad->contents + need_pad->size, 0, 8);
7744 need_pad->size += 8;
7745 }
7746
7747 return true;
7748 }
7749
7750 /* Analyze inline PLT call relocations to see whether calls to locally
7751 defined functions can be converted to direct calls. */
7752
7753 bool
7754 ppc64_elf_inline_plt (struct bfd_link_info *info)
7755 {
7756 struct ppc_link_hash_table *htab;
7757 bfd *ibfd;
7758 asection *sec;
7759 bfd_vma low_vma, high_vma, limit;
7760
7761 htab = ppc_hash_table (info);
7762 if (htab == NULL)
7763 return false;
7764
7765 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7766 reduced somewhat to cater for possible stubs that might be added
7767 between the call and its destination. */
7768 if (htab->params->group_size < 0)
7769 {
7770 limit = -htab->params->group_size;
7771 if (limit == 1)
7772 limit = 0x1e00000;
7773 }
7774 else
7775 {
7776 limit = htab->params->group_size;
7777 if (limit == 1)
7778 limit = 0x1c00000;
7779 }
7780
7781 low_vma = -1;
7782 high_vma = 0;
7783 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7784 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7785 {
7786 if (low_vma > sec->vma)
7787 low_vma = sec->vma;
7788 if (high_vma < sec->vma + sec->size)
7789 high_vma = sec->vma + sec->size;
7790 }
7791
7792 /* If a "bl" can reach anywhere in local code sections, then we can
7793 convert all inline PLT sequences to direct calls when the symbol
7794 is local. */
7795 if (high_vma - low_vma < limit)
7796 {
7797 htab->can_convert_all_inline_plt = 1;
7798 return true;
7799 }
7800
7801 /* Otherwise, go looking through relocs for cases where a direct
7802 call won't reach. Mark the symbol on any such reloc to disable
7803 the optimization and keep the PLT entry as it seems likely that
7804 this will be better than creating trampolines. Note that this
7805 will disable the optimization for all inline PLT calls to a
7806 particular symbol, not just those that won't reach. The
7807 difficulty in doing a more precise optimization is that the
7808 linker needs to make a decision depending on whether a
7809 particular R_PPC64_PLTCALL insn can be turned into a direct
7810 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7811 the sequence, and there is nothing that ties those relocs
7812 together except their symbol. */
7813
7814 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7815 {
7816 Elf_Internal_Shdr *symtab_hdr;
7817 Elf_Internal_Sym *local_syms;
7818
7819 if (!is_ppc64_elf (ibfd))
7820 continue;
7821
7822 local_syms = NULL;
7823 symtab_hdr = &elf_symtab_hdr (ibfd);
7824
7825 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7826 if (ppc64_elf_section_data (sec)->has_pltcall
7827 && !bfd_is_abs_section (sec->output_section))
7828 {
7829 Elf_Internal_Rela *relstart, *rel, *relend;
7830
7831 /* Read the relocations. */
7832 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7833 info->keep_memory);
7834 if (relstart == NULL)
7835 return false;
7836
7837 relend = relstart + sec->reloc_count;
7838 for (rel = relstart; rel < relend; rel++)
7839 {
7840 enum elf_ppc64_reloc_type r_type;
7841 unsigned long r_symndx;
7842 asection *sym_sec;
7843 struct elf_link_hash_entry *h;
7844 Elf_Internal_Sym *sym;
7845 unsigned char *tls_maskp;
7846
7847 r_type = ELF64_R_TYPE (rel->r_info);
7848 if (r_type != R_PPC64_PLTCALL
7849 && r_type != R_PPC64_PLTCALL_NOTOC)
7850 continue;
7851
7852 r_symndx = ELF64_R_SYM (rel->r_info);
7853 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7854 r_symndx, ibfd))
7855 {
7856 if (elf_section_data (sec)->relocs != relstart)
7857 free (relstart);
7858 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7859 free (local_syms);
7860 return false;
7861 }
7862
7863 if (sym_sec != NULL && sym_sec->output_section != NULL)
7864 {
7865 bfd_vma from, to;
7866 if (h != NULL)
7867 to = h->root.u.def.value;
7868 else
7869 to = sym->st_value;
7870 to += (rel->r_addend
7871 + sym_sec->output_offset
7872 + sym_sec->output_section->vma);
7873 from = (rel->r_offset
7874 + sec->output_offset
7875 + sec->output_section->vma);
7876 if (to - from + limit < 2 * limit
7877 && !(r_type == R_PPC64_PLTCALL_NOTOC
7878 && (((h ? h->other : sym->st_other)
7879 & STO_PPC64_LOCAL_MASK)
7880 > 1 << STO_PPC64_LOCAL_BIT)))
7881 *tls_maskp &= ~PLT_KEEP;
7882 }
7883 }
7884 if (elf_section_data (sec)->relocs != relstart)
7885 free (relstart);
7886 }
7887
7888 if (local_syms != NULL
7889 && symtab_hdr->contents != (unsigned char *) local_syms)
7890 {
7891 if (!info->keep_memory)
7892 free (local_syms);
7893 else
7894 symtab_hdr->contents = (unsigned char *) local_syms;
7895 }
7896 }
7897
7898 return true;
7899 }
7900
7901 /* Set htab->tls_get_addr and various other info specific to TLS.
7902 This needs to run before dynamic symbols are processed in
7903 bfd_elf_size_dynamic_sections. */
7904
7905 bool
7906 ppc64_elf_tls_setup (struct bfd_link_info *info)
7907 {
7908 struct ppc_link_hash_table *htab;
7909 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7910
7911 htab = ppc_hash_table (info);
7912 if (htab == NULL)
7913 return false;
7914
7915 /* Move dynamic linking info to the function descriptor sym. */
7916 if (htab->need_func_desc_adj)
7917 {
7918 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7919 htab->need_func_desc_adj = 0;
7920 }
7921
7922 if (abiversion (info->output_bfd) == 1)
7923 htab->opd_abi = 1;
7924
7925 if (htab->params->no_multi_toc)
7926 htab->do_multi_toc = 0;
7927 else if (!htab->do_multi_toc)
7928 htab->params->no_multi_toc = 1;
7929
7930 /* Default to --no-plt-localentry, as this option can cause problems
7931 with symbol interposition. For example, glibc libpthread.so and
7932 libc.so duplicate many pthread symbols, with a fallback
7933 implementation in libc.so. In some cases the fallback does more
7934 work than the pthread implementation. __pthread_condattr_destroy
7935 is one such symbol: the libpthread.so implementation is
7936 localentry:0 while the libc.so implementation is localentry:8.
7937 An app that "cleverly" uses dlopen to only load necessary
7938 libraries at runtime may omit loading libpthread.so when not
7939 running multi-threaded, which then results in the libc.so
7940 fallback symbols being used and ld.so complaining. Now there
7941 are workarounds in ld (see non_zero_localentry) to detect the
7942 pthread situation, but that may not be the only case where
7943 --plt-localentry can cause trouble. */
7944 if (htab->params->plt_localentry0 < 0)
7945 htab->params->plt_localentry0 = 0;
7946 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7947 {
7948 /* The issue is that __glink_PLTresolve saves r2, which is done
7949 because glibc ld.so _dl_runtime_resolve restores r2 to support
7950 a glibc plt call optimisation where global entry code is
7951 skipped on calls that resolve to the same binary. The
7952 __glink_PLTresolve save of r2 is incompatible with code
7953 making tail calls, because the tail call might go via the
7954 resolver and thus overwrite the proper saved r2. */
7955 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7956 "power10 pc-relative code"));
7957 htab->params->plt_localentry0 = 0;
7958 }
7959 if (htab->params->plt_localentry0
7960 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7961 false, false, false) == NULL)
7962 _bfd_error_handler
7963 (_("warning: --plt-localentry is especially dangerous without "
7964 "ld.so support to detect ABI violations"));
7965
7966 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7967 false, false, true);
7968 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7969 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7970 false, false, true);
7971 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7972
7973 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7974 false, false, true);
7975 htab->tga_desc = ppc_elf_hash_entry (desc);
7976 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7977 false, false, true);
7978 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7979
7980 if (htab->params->tls_get_addr_opt)
7981 {
7982 struct elf_link_hash_entry *opt, *opt_fd;
7983
7984 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7985 false, false, true);
7986 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7987 false, false, true);
7988 if (opt_fd != NULL
7989 && (opt_fd->root.type == bfd_link_hash_defined
7990 || opt_fd->root.type == bfd_link_hash_defweak))
7991 {
7992 /* If glibc supports an optimized __tls_get_addr call stub,
7993 signalled by the presence of __tls_get_addr_opt, and we'll
7994 be calling __tls_get_addr via a plt call stub, then
7995 make __tls_get_addr point to __tls_get_addr_opt. */
7996 if (!(htab->elf.dynamic_sections_created
7997 && tga_fd != NULL
7998 && (tga_fd->type == STT_FUNC
7999 || tga_fd->needs_plt)
8000 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
8001 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
8002 tga_fd = NULL;
8003 if (!(htab->elf.dynamic_sections_created
8004 && desc_fd != NULL
8005 && (desc_fd->type == STT_FUNC
8006 || desc_fd->needs_plt)
8007 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
8008 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
8009 desc_fd = NULL;
8010
8011 if (tga_fd != NULL || desc_fd != NULL)
8012 {
8013 struct plt_entry *ent = NULL;
8014
8015 if (tga_fd != NULL)
8016 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
8017 if (ent->plt.refcount > 0)
8018 break;
8019 if (ent == NULL && desc_fd != NULL)
8020 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
8021 if (ent->plt.refcount > 0)
8022 break;
8023 if (ent != NULL)
8024 {
8025 if (tga_fd != NULL)
8026 {
8027 tga_fd->root.type = bfd_link_hash_indirect;
8028 tga_fd->root.u.i.link = &opt_fd->root;
8029 tga_fd->root.u.i.warning = NULL;
8030 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
8031 }
8032 if (desc_fd != NULL)
8033 {
8034 desc_fd->root.type = bfd_link_hash_indirect;
8035 desc_fd->root.u.i.link = &opt_fd->root;
8036 desc_fd->root.u.i.warning = NULL;
8037 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
8038 }
8039 opt_fd->mark = 1;
8040 if (opt_fd->dynindx != -1)
8041 {
8042 /* Use __tls_get_addr_opt in dynamic relocations. */
8043 opt_fd->dynindx = -1;
8044 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
8045 opt_fd->dynstr_index);
8046 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
8047 return false;
8048 }
8049 if (tga_fd != NULL)
8050 {
8051 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8052 tga = elf_hash_entry (htab->tls_get_addr);
8053 if (opt != NULL && tga != NULL)
8054 {
8055 tga->root.type = bfd_link_hash_indirect;
8056 tga->root.u.i.link = &opt->root;
8057 tga->root.u.i.warning = NULL;
8058 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8059 opt->mark = 1;
8060 _bfd_elf_link_hash_hide_symbol (info, opt,
8061 tga->forced_local);
8062 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8063 }
8064 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8065 htab->tls_get_addr_fd->is_func_descriptor = 1;
8066 if (htab->tls_get_addr != NULL)
8067 {
8068 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8069 htab->tls_get_addr->is_func = 1;
8070 }
8071 }
8072 if (desc_fd != NULL)
8073 {
8074 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8075 if (opt != NULL && desc != NULL)
8076 {
8077 desc->root.type = bfd_link_hash_indirect;
8078 desc->root.u.i.link = &opt->root;
8079 desc->root.u.i.warning = NULL;
8080 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8081 opt->mark = 1;
8082 _bfd_elf_link_hash_hide_symbol (info, opt,
8083 desc->forced_local);
8084 htab->tga_desc = ppc_elf_hash_entry (opt);
8085 }
8086 htab->tga_desc_fd->oh = htab->tga_desc;
8087 htab->tga_desc_fd->is_func_descriptor = 1;
8088 if (htab->tga_desc != NULL)
8089 {
8090 htab->tga_desc->oh = htab->tga_desc_fd;
8091 htab->tga_desc->is_func = 1;
8092 }
8093 }
8094 }
8095 }
8096 }
8097 else if (htab->params->tls_get_addr_opt < 0)
8098 htab->params->tls_get_addr_opt = 0;
8099 }
8100
8101 if (htab->tga_desc_fd != NULL
8102 && htab->params->tls_get_addr_opt
8103 && htab->params->no_tls_get_addr_regsave == -1)
8104 htab->params->no_tls_get_addr_regsave = 0;
8105
8106 return true;
8107 }
8108
8109 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8110 any of HASH1, HASH2, HASH3, or HASH4. */
8111
8112 static bool
8113 branch_reloc_hash_match (bfd *ibfd,
8114 Elf_Internal_Rela *rel,
8115 struct ppc_link_hash_entry *hash1,
8116 struct ppc_link_hash_entry *hash2,
8117 struct ppc_link_hash_entry *hash3,
8118 struct ppc_link_hash_entry *hash4)
8119 {
8120 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8121 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8122 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8123
8124 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8125 {
8126 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8127 struct elf_link_hash_entry *h;
8128
8129 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8130 h = elf_follow_link (h);
8131 if (h == elf_hash_entry (hash1)
8132 || h == elf_hash_entry (hash2)
8133 || h == elf_hash_entry (hash3)
8134 || h == elf_hash_entry (hash4))
8135 return true;
8136 }
8137 return false;
8138 }
8139
8140 /* Run through all the TLS relocs looking for optimization
8141 opportunities. The linker has been hacked (see ppc64elf.em) to do
8142 a preliminary section layout so that we know the TLS segment
8143 offsets. We can't optimize earlier because some optimizations need
8144 to know the tp offset, and we need to optimize before allocating
8145 dynamic relocations. */
8146
8147 bool
8148 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8149 {
8150 bfd *ibfd;
8151 asection *sec;
8152 struct ppc_link_hash_table *htab;
8153 unsigned char *toc_ref;
8154 int pass;
8155
8156 if (!bfd_link_executable (info))
8157 return true;
8158
8159 htab = ppc_hash_table (info);
8160 if (htab == NULL)
8161 return false;
8162
8163 htab->do_tls_opt = 1;
8164
8165 /* Make two passes over the relocs. On the first pass, mark toc
8166 entries involved with tls relocs, and check that tls relocs
8167 involved in setting up a tls_get_addr call are indeed followed by
8168 such a call. If they are not, we can't do any tls optimization.
8169 On the second pass twiddle tls_mask flags to notify
8170 relocate_section that optimization can be done, and adjust got
8171 and plt refcounts. */
8172 toc_ref = NULL;
8173 for (pass = 0; pass < 2; ++pass)
8174 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8175 {
8176 Elf_Internal_Sym *locsyms = NULL;
8177 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8178
8179 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8180 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8181 {
8182 Elf_Internal_Rela *relstart, *rel, *relend;
8183 bool found_tls_get_addr_arg = 0;
8184
8185 /* Read the relocations. */
8186 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8187 info->keep_memory);
8188 if (relstart == NULL)
8189 {
8190 free (toc_ref);
8191 return false;
8192 }
8193
8194 relend = relstart + sec->reloc_count;
8195 for (rel = relstart; rel < relend; rel++)
8196 {
8197 enum elf_ppc64_reloc_type r_type;
8198 unsigned long r_symndx;
8199 struct elf_link_hash_entry *h;
8200 Elf_Internal_Sym *sym;
8201 asection *sym_sec;
8202 unsigned char *tls_mask;
8203 unsigned int tls_set, tls_clear, tls_type = 0;
8204 bfd_vma value;
8205 bool ok_tprel, is_local;
8206 long toc_ref_index = 0;
8207 int expecting_tls_get_addr = 0;
8208 bool ret = false;
8209
8210 r_symndx = ELF64_R_SYM (rel->r_info);
8211 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8212 r_symndx, ibfd))
8213 {
8214 err_free_rel:
8215 if (elf_section_data (sec)->relocs != relstart)
8216 free (relstart);
8217 free (toc_ref);
8218 if (elf_symtab_hdr (ibfd).contents
8219 != (unsigned char *) locsyms)
8220 free (locsyms);
8221 return ret;
8222 }
8223
8224 if (h != NULL)
8225 {
8226 if (h->root.type == bfd_link_hash_defined
8227 || h->root.type == bfd_link_hash_defweak)
8228 value = h->root.u.def.value;
8229 else if (h->root.type == bfd_link_hash_undefweak)
8230 value = 0;
8231 else
8232 {
8233 found_tls_get_addr_arg = 0;
8234 continue;
8235 }
8236 }
8237 else
8238 /* Symbols referenced by TLS relocs must be of type
8239 STT_TLS. So no need for .opd local sym adjust. */
8240 value = sym->st_value;
8241
8242 ok_tprel = false;
8243 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8244 if (is_local)
8245 {
8246 if (h != NULL
8247 && h->root.type == bfd_link_hash_undefweak)
8248 ok_tprel = true;
8249 else if (sym_sec != NULL
8250 && sym_sec->output_section != NULL)
8251 {
8252 value += sym_sec->output_offset;
8253 value += sym_sec->output_section->vma;
8254 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8255 /* Note that even though the prefix insns
8256 allow a 1<<33 offset we use the same test
8257 as for addis;addi. There may be a mix of
8258 pcrel and non-pcrel code and the decision
8259 to optimise is per symbol, not per TLS
8260 sequence. */
8261 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8262 }
8263 }
8264
8265 r_type = ELF64_R_TYPE (rel->r_info);
8266 /* If this section has old-style __tls_get_addr calls
8267 without marker relocs, then check that each
8268 __tls_get_addr call reloc is preceded by a reloc
8269 that conceivably belongs to the __tls_get_addr arg
8270 setup insn. If we don't find matching arg setup
8271 relocs, don't do any tls optimization. */
8272 if (pass == 0
8273 && sec->nomark_tls_get_addr
8274 && h != NULL
8275 && is_tls_get_addr (h, htab)
8276 && !found_tls_get_addr_arg
8277 && is_branch_reloc (r_type))
8278 {
8279 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8280 "TLS optimization disabled\n"),
8281 ibfd, sec, rel->r_offset);
8282 ret = true;
8283 goto err_free_rel;
8284 }
8285
8286 found_tls_get_addr_arg = 0;
8287 switch (r_type)
8288 {
8289 case R_PPC64_GOT_TLSLD16:
8290 case R_PPC64_GOT_TLSLD16_LO:
8291 case R_PPC64_GOT_TLSLD_PCREL34:
8292 expecting_tls_get_addr = 1;
8293 found_tls_get_addr_arg = 1;
8294 /* Fall through. */
8295
8296 case R_PPC64_GOT_TLSLD16_HI:
8297 case R_PPC64_GOT_TLSLD16_HA:
8298 /* These relocs should never be against a symbol
8299 defined in a shared lib. Leave them alone if
8300 that turns out to be the case. */
8301 if (!is_local)
8302 continue;
8303
8304 /* LD -> LE */
8305 tls_set = 0;
8306 tls_clear = TLS_LD;
8307 tls_type = TLS_TLS | TLS_LD;
8308 break;
8309
8310 case R_PPC64_GOT_TLSGD16:
8311 case R_PPC64_GOT_TLSGD16_LO:
8312 case R_PPC64_GOT_TLSGD_PCREL34:
8313 expecting_tls_get_addr = 1;
8314 found_tls_get_addr_arg = 1;
8315 /* Fall through. */
8316
8317 case R_PPC64_GOT_TLSGD16_HI:
8318 case R_PPC64_GOT_TLSGD16_HA:
8319 if (ok_tprel)
8320 /* GD -> LE */
8321 tls_set = 0;
8322 else
8323 /* GD -> IE */
8324 tls_set = TLS_TLS | TLS_GDIE;
8325 tls_clear = TLS_GD;
8326 tls_type = TLS_TLS | TLS_GD;
8327 break;
8328
8329 case R_PPC64_GOT_TPREL_PCREL34:
8330 case R_PPC64_GOT_TPREL16_DS:
8331 case R_PPC64_GOT_TPREL16_LO_DS:
8332 case R_PPC64_GOT_TPREL16_HI:
8333 case R_PPC64_GOT_TPREL16_HA:
8334 if (ok_tprel)
8335 {
8336 /* IE -> LE */
8337 tls_set = 0;
8338 tls_clear = TLS_TPREL;
8339 tls_type = TLS_TLS | TLS_TPREL;
8340 break;
8341 }
8342 continue;
8343
8344 case R_PPC64_TLSLD:
8345 if (!is_local)
8346 continue;
8347 /* Fall through. */
8348 case R_PPC64_TLSGD:
8349 if (rel + 1 < relend
8350 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8351 {
8352 if (pass != 0
8353 && (ELF64_R_TYPE (rel[1].r_info)
8354 != R_PPC64_PLTSEQ)
8355 && (ELF64_R_TYPE (rel[1].r_info)
8356 != R_PPC64_PLTSEQ_NOTOC))
8357 {
8358 r_symndx = ELF64_R_SYM (rel[1].r_info);
8359 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8360 r_symndx, ibfd))
8361 goto err_free_rel;
8362 if (h != NULL)
8363 {
8364 struct plt_entry *ent = NULL;
8365
8366 for (ent = h->plt.plist;
8367 ent != NULL;
8368 ent = ent->next)
8369 if (ent->addend == rel[1].r_addend)
8370 break;
8371
8372 if (ent != NULL
8373 && ent->plt.refcount > 0)
8374 ent->plt.refcount -= 1;
8375 }
8376 }
8377 continue;
8378 }
8379 found_tls_get_addr_arg = 1;
8380 /* Fall through. */
8381
8382 case R_PPC64_TLS:
8383 case R_PPC64_TOC16:
8384 case R_PPC64_TOC16_LO:
8385 if (sym_sec == NULL || sym_sec != toc)
8386 continue;
8387
8388 /* Mark this toc entry as referenced by a TLS
8389 code sequence. We can do that now in the
8390 case of R_PPC64_TLS, and after checking for
8391 tls_get_addr for the TOC16 relocs. */
8392 if (toc_ref == NULL)
8393 toc_ref
8394 = bfd_zmalloc (toc->output_section->rawsize / 8);
8395 if (toc_ref == NULL)
8396 goto err_free_rel;
8397
8398 if (h != NULL)
8399 value = h->root.u.def.value;
8400 else
8401 value = sym->st_value;
8402 value += rel->r_addend;
8403 if (value % 8 != 0)
8404 continue;
8405 BFD_ASSERT (value < toc->size
8406 && toc->output_offset % 8 == 0);
8407 toc_ref_index = (value + toc->output_offset) / 8;
8408 if (r_type == R_PPC64_TLS
8409 || r_type == R_PPC64_TLSGD
8410 || r_type == R_PPC64_TLSLD)
8411 {
8412 toc_ref[toc_ref_index] = 1;
8413 continue;
8414 }
8415
8416 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8417 continue;
8418
8419 tls_set = 0;
8420 tls_clear = 0;
8421 expecting_tls_get_addr = 2;
8422 break;
8423
8424 case R_PPC64_TPREL64:
8425 if (pass == 0
8426 || sec != toc
8427 || toc_ref == NULL
8428 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8429 continue;
8430 if (ok_tprel)
8431 {
8432 /* IE -> LE */
8433 tls_set = TLS_EXPLICIT;
8434 tls_clear = TLS_TPREL;
8435 break;
8436 }
8437 continue;
8438
8439 case R_PPC64_DTPMOD64:
8440 if (pass == 0
8441 || sec != toc
8442 || toc_ref == NULL
8443 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8444 continue;
8445 if (rel + 1 < relend
8446 && (rel[1].r_info
8447 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8448 && rel[1].r_offset == rel->r_offset + 8)
8449 {
8450 if (ok_tprel)
8451 /* GD -> LE */
8452 tls_set = TLS_EXPLICIT | TLS_GD;
8453 else
8454 /* GD -> IE */
8455 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8456 tls_clear = TLS_GD;
8457 }
8458 else
8459 {
8460 if (!is_local)
8461 continue;
8462
8463 /* LD -> LE */
8464 tls_set = TLS_EXPLICIT;
8465 tls_clear = TLS_LD;
8466 }
8467 break;
8468
8469 case R_PPC64_TPREL16_HA:
8470 if (pass == 0)
8471 {
8472 unsigned char buf[4];
8473 unsigned int insn;
8474 bfd_vma off = rel->r_offset & ~3;
8475 if (!bfd_get_section_contents (ibfd, sec, buf,
8476 off, 4))
8477 goto err_free_rel;
8478 insn = bfd_get_32 (ibfd, buf);
8479 /* addis rt,13,imm */
8480 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8481 != ((15u << 26) | (13 << 16)))
8482 {
8483 /* xgettext:c-format */
8484 info->callbacks->minfo
8485 (_("%H: warning: %s unexpected insn %#x.\n"),
8486 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8487 htab->do_tls_opt = 0;
8488 }
8489 }
8490 continue;
8491
8492 case R_PPC64_TPREL16_HI:
8493 case R_PPC64_TPREL16_HIGH:
8494 case R_PPC64_TPREL16_HIGHA:
8495 case R_PPC64_TPREL16_HIGHER:
8496 case R_PPC64_TPREL16_HIGHERA:
8497 case R_PPC64_TPREL16_HIGHEST:
8498 case R_PPC64_TPREL16_HIGHESTA:
8499 /* These can all be used in sequences along with
8500 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8501 able to verify easily. */
8502 htab->do_tls_opt = 0;
8503 continue;
8504
8505 default:
8506 continue;
8507 }
8508
8509 if (pass == 0)
8510 {
8511 if (!expecting_tls_get_addr
8512 || !sec->nomark_tls_get_addr)
8513 continue;
8514
8515 if (rel + 1 < relend
8516 && branch_reloc_hash_match (ibfd, rel + 1,
8517 htab->tls_get_addr_fd,
8518 htab->tga_desc_fd,
8519 htab->tls_get_addr,
8520 htab->tga_desc))
8521 {
8522 if (expecting_tls_get_addr == 2)
8523 {
8524 /* Check for toc tls entries. */
8525 unsigned char *toc_tls;
8526 int retval;
8527
8528 retval = get_tls_mask (&toc_tls, NULL, NULL,
8529 &locsyms,
8530 rel, ibfd);
8531 if (retval == 0)
8532 goto err_free_rel;
8533 if (toc_tls != NULL)
8534 {
8535 if ((*toc_tls & TLS_TLS) != 0
8536 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8537 found_tls_get_addr_arg = 1;
8538 if (retval > 1)
8539 toc_ref[toc_ref_index] = 1;
8540 }
8541 }
8542 continue;
8543 }
8544
8545 /* Uh oh, we didn't find the expected call. We
8546 could just mark this symbol to exclude it
8547 from tls optimization but it's safer to skip
8548 the entire optimization. */
8549 /* xgettext:c-format */
8550 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8551 "TLS optimization disabled\n"),
8552 ibfd, sec, rel->r_offset);
8553 ret = true;
8554 goto err_free_rel;
8555 }
8556
8557 /* If we don't have old-style __tls_get_addr calls
8558 without TLSGD/TLSLD marker relocs, and we haven't
8559 found a new-style __tls_get_addr call with a
8560 marker for this symbol, then we either have a
8561 broken object file or an -mlongcall style
8562 indirect call to __tls_get_addr without a marker.
8563 Disable optimization in this case. */
8564 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8565 && (tls_set & TLS_EXPLICIT) == 0
8566 && !sec->nomark_tls_get_addr
8567 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8568 != (TLS_TLS | TLS_MARK)))
8569 continue;
8570
8571 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8572 {
8573 struct plt_entry *ent = NULL;
8574
8575 if (htab->tls_get_addr_fd != NULL)
8576 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8577 ent != NULL;
8578 ent = ent->next)
8579 if (ent->addend == 0)
8580 break;
8581
8582 if (ent == NULL && htab->tga_desc_fd != NULL)
8583 for (ent = htab->tga_desc_fd->elf.plt.plist;
8584 ent != NULL;
8585 ent = ent->next)
8586 if (ent->addend == 0)
8587 break;
8588
8589 if (ent == NULL && htab->tls_get_addr != NULL)
8590 for (ent = htab->tls_get_addr->elf.plt.plist;
8591 ent != NULL;
8592 ent = ent->next)
8593 if (ent->addend == 0)
8594 break;
8595
8596 if (ent == NULL && htab->tga_desc != NULL)
8597 for (ent = htab->tga_desc->elf.plt.plist;
8598 ent != NULL;
8599 ent = ent->next)
8600 if (ent->addend == 0)
8601 break;
8602
8603 if (ent != NULL
8604 && ent->plt.refcount > 0)
8605 ent->plt.refcount -= 1;
8606 }
8607
8608 if (tls_clear == 0)
8609 continue;
8610
8611 if ((tls_set & TLS_EXPLICIT) == 0)
8612 {
8613 struct got_entry *ent;
8614
8615 /* Adjust got entry for this reloc. */
8616 if (h != NULL)
8617 ent = h->got.glist;
8618 else
8619 ent = elf_local_got_ents (ibfd)[r_symndx];
8620
8621 for (; ent != NULL; ent = ent->next)
8622 if (ent->addend == rel->r_addend
8623 && ent->owner == ibfd
8624 && ent->tls_type == tls_type)
8625 break;
8626 if (ent == NULL)
8627 abort ();
8628
8629 if (tls_set == 0)
8630 {
8631 /* We managed to get rid of a got entry. */
8632 if (ent->got.refcount > 0)
8633 ent->got.refcount -= 1;
8634 }
8635 }
8636 else
8637 {
8638 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8639 we'll lose one or two dyn relocs. */
8640 if (!dec_dynrel_count (rel, sec, info,
8641 NULL, h, sym))
8642 return false;
8643
8644 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8645 {
8646 if (!dec_dynrel_count (rel + 1, sec, info,
8647 NULL, h, sym))
8648 return false;
8649 }
8650 }
8651
8652 *tls_mask |= tls_set & 0xff;
8653 *tls_mask &= ~tls_clear;
8654 }
8655
8656 if (elf_section_data (sec)->relocs != relstart)
8657 free (relstart);
8658 }
8659
8660 if (locsyms != NULL
8661 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8662 {
8663 if (!info->keep_memory)
8664 free (locsyms);
8665 else
8666 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8667 }
8668 }
8669
8670 free (toc_ref);
8671 return true;
8672 }
8673
8674 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8675 the values of any global symbols in a toc section that has been
8676 edited. Globals in toc sections should be a rarity, so this function
8677 sets a flag if any are found in toc sections other than the one just
8678 edited, so that further hash table traversals can be avoided. */
8679
8680 struct adjust_toc_info
8681 {
8682 asection *toc;
8683 unsigned long *skip;
8684 bool global_toc_syms;
8685 };
8686
8687 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8688
8689 static bool
8690 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8691 {
8692 struct ppc_link_hash_entry *eh;
8693 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8694 unsigned long i;
8695
8696 if (h->root.type != bfd_link_hash_defined
8697 && h->root.type != bfd_link_hash_defweak)
8698 return true;
8699
8700 eh = ppc_elf_hash_entry (h);
8701 if (eh->adjust_done)
8702 return true;
8703
8704 if (eh->elf.root.u.def.section == toc_inf->toc)
8705 {
8706 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8707 i = toc_inf->toc->rawsize >> 3;
8708 else
8709 i = eh->elf.root.u.def.value >> 3;
8710
8711 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8712 {
8713 _bfd_error_handler
8714 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8715 do
8716 ++i;
8717 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8718 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8719 }
8720
8721 eh->elf.root.u.def.value -= toc_inf->skip[i];
8722 eh->adjust_done = 1;
8723 }
8724 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8725 toc_inf->global_toc_syms = true;
8726
8727 return true;
8728 }
8729
8730 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8731 on a _LO variety toc/got reloc. */
8732
8733 static bool
8734 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8735 {
8736 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8737 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8738 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8739 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8740 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8741 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8742 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8743 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8744 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8745 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8746 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8747 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8748 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8749 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8750 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8751 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8752 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8753 /* Exclude lfqu by testing reloc. If relocs are ever
8754 defined for the reduced D field in psq_lu then those
8755 will need testing too. */
8756 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8757 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8758 && (insn & 1) == 0)
8759 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8760 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8761 /* Exclude stfqu. psq_stu as above for psq_lu. */
8762 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8763 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8764 && (insn & 1) == 0));
8765 }
8766
8767 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8768 pld ra,symbol@got@pcrel
8769 load/store rt,off(ra)
8770 or
8771 pla ra,symbol@pcrel
8772 load/store rt,off(ra)
8773 may be translated to
8774 pload/pstore rt,symbol+off@pcrel
8775 nop.
8776 This function returns true if the optimization is possible, placing
8777 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8778
8779 On entry to this function, the linker has already determined that
8780 the pld can be replaced with pla: *PINSN1 is that pla insn,
8781 while *PINSN2 is the second instruction. */
8782
8783 static bool
8784 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8785 {
8786 uint64_t insn1 = *pinsn1;
8787 uint64_t insn2 = *pinsn2;
8788 bfd_signed_vma off;
8789
8790 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8791 {
8792 /* Check that regs match. */
8793 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8794 return false;
8795
8796 /* P8LS or PMLS form, non-pcrel. */
8797 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8798 return false;
8799
8800 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8801 *pinsn2 = PNOP;
8802 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8803 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8804 return true;
8805 }
8806
8807 insn2 >>= 32;
8808
8809 /* Check that regs match. */
8810 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8811 return false;
8812
8813 switch ((insn2 >> 26) & 63)
8814 {
8815 default:
8816 return false;
8817
8818 case 32: /* lwz */
8819 case 34: /* lbz */
8820 case 36: /* stw */
8821 case 38: /* stb */
8822 case 40: /* lhz */
8823 case 42: /* lha */
8824 case 44: /* sth */
8825 case 48: /* lfs */
8826 case 50: /* lfd */
8827 case 52: /* stfs */
8828 case 54: /* stfd */
8829 /* These are the PMLS cases, where we just need to tack a prefix
8830 on the insn. */
8831 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8832 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8833 off = insn2 & 0xffff;
8834 break;
8835
8836 case 58: /* lwa, ld */
8837 if ((insn2 & 1) != 0)
8838 return false;
8839 insn1 = ((1ULL << 58) | (1ULL << 52)
8840 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8841 | (insn2 & (31ULL << 21)));
8842 off = insn2 & 0xfffc;
8843 break;
8844
8845 case 57: /* lxsd, lxssp */
8846 if ((insn2 & 3) < 2)
8847 return false;
8848 insn1 = ((1ULL << 58) | (1ULL << 52)
8849 | ((40ULL | (insn2 & 3)) << 26)
8850 | (insn2 & (31ULL << 21)));
8851 off = insn2 & 0xfffc;
8852 break;
8853
8854 case 61: /* stxsd, stxssp, lxv, stxv */
8855 if ((insn2 & 3) == 0)
8856 return false;
8857 else if ((insn2 & 3) >= 2)
8858 {
8859 insn1 = ((1ULL << 58) | (1ULL << 52)
8860 | ((44ULL | (insn2 & 3)) << 26)
8861 | (insn2 & (31ULL << 21)));
8862 off = insn2 & 0xfffc;
8863 }
8864 else
8865 {
8866 insn1 = ((1ULL << 58) | (1ULL << 52)
8867 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8868 | (insn2 & (31ULL << 21)));
8869 off = insn2 & 0xfff0;
8870 }
8871 break;
8872
8873 case 56: /* lq */
8874 insn1 = ((1ULL << 58) | (1ULL << 52)
8875 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8876 off = insn2 & 0xffff;
8877 break;
8878
8879 case 6: /* lxvp, stxvp */
8880 if ((insn2 & 0xe) != 0)
8881 return false;
8882 insn1 = ((1ULL << 58) | (1ULL << 52)
8883 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8884 | (insn2 & (31ULL << 21)));
8885 off = insn2 & 0xfff0;
8886 break;
8887
8888 case 62: /* std, stq */
8889 if ((insn2 & 1) != 0)
8890 return false;
8891 insn1 = ((1ULL << 58) | (1ULL << 52)
8892 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8893 | (insn2 & (31ULL << 21)));
8894 off = insn2 & 0xfffc;
8895 break;
8896 }
8897
8898 *pinsn1 = insn1;
8899 *pinsn2 = (uint64_t) NOP << 32;
8900 *poff = (off ^ 0x8000) - 0x8000;
8901 return true;
8902 }
8903
8904 /* Examine all relocs referencing .toc sections in order to remove
8905 unused .toc entries. */
8906
8907 bool
8908 ppc64_elf_edit_toc (struct bfd_link_info *info)
8909 {
8910 bfd *ibfd;
8911 struct adjust_toc_info toc_inf;
8912 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8913
8914 htab->do_toc_opt = 1;
8915 toc_inf.global_toc_syms = true;
8916 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8917 {
8918 asection *toc, *sec;
8919 Elf_Internal_Shdr *symtab_hdr;
8920 Elf_Internal_Sym *local_syms;
8921 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8922 unsigned long *skip, *drop;
8923 unsigned char *used;
8924 unsigned char *keep, last, some_unused;
8925
8926 if (!is_ppc64_elf (ibfd))
8927 continue;
8928
8929 toc = bfd_get_section_by_name (ibfd, ".toc");
8930 if (toc == NULL
8931 || toc->size == 0
8932 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8933 || discarded_section (toc))
8934 continue;
8935
8936 toc_relocs = NULL;
8937 local_syms = NULL;
8938 symtab_hdr = &elf_symtab_hdr (ibfd);
8939
8940 /* Look at sections dropped from the final link. */
8941 skip = NULL;
8942 relstart = NULL;
8943 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8944 {
8945 if (sec->reloc_count == 0
8946 || !discarded_section (sec)
8947 || get_opd_info (sec)
8948 || (sec->flags & SEC_ALLOC) == 0
8949 || (sec->flags & SEC_DEBUGGING) != 0)
8950 continue;
8951
8952 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8953 if (relstart == NULL)
8954 goto error_ret;
8955
8956 /* Run through the relocs to see which toc entries might be
8957 unused. */
8958 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8959 {
8960 enum elf_ppc64_reloc_type r_type;
8961 unsigned long r_symndx;
8962 asection *sym_sec;
8963 struct elf_link_hash_entry *h;
8964 Elf_Internal_Sym *sym;
8965 bfd_vma val;
8966
8967 r_type = ELF64_R_TYPE (rel->r_info);
8968 switch (r_type)
8969 {
8970 default:
8971 continue;
8972
8973 case R_PPC64_TOC16:
8974 case R_PPC64_TOC16_LO:
8975 case R_PPC64_TOC16_HI:
8976 case R_PPC64_TOC16_HA:
8977 case R_PPC64_TOC16_DS:
8978 case R_PPC64_TOC16_LO_DS:
8979 break;
8980 }
8981
8982 r_symndx = ELF64_R_SYM (rel->r_info);
8983 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8984 r_symndx, ibfd))
8985 goto error_ret;
8986
8987 if (sym_sec != toc)
8988 continue;
8989
8990 if (h != NULL)
8991 val = h->root.u.def.value;
8992 else
8993 val = sym->st_value;
8994 val += rel->r_addend;
8995
8996 if (val >= toc->size)
8997 continue;
8998
8999 /* Anything in the toc ought to be aligned to 8 bytes.
9000 If not, don't mark as unused. */
9001 if (val & 7)
9002 continue;
9003
9004 if (skip == NULL)
9005 {
9006 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9007 if (skip == NULL)
9008 goto error_ret;
9009 }
9010
9011 skip[val >> 3] = ref_from_discarded;
9012 }
9013
9014 if (elf_section_data (sec)->relocs != relstart)
9015 free (relstart);
9016 }
9017
9018 /* For largetoc loads of address constants, we can convert
9019 . addis rx,2,addr@got@ha
9020 . ld ry,addr@got@l(rx)
9021 to
9022 . addis rx,2,addr@toc@ha
9023 . addi ry,rx,addr@toc@l
9024 when addr is within 2G of the toc pointer. This then means
9025 that the word storing "addr" in the toc is no longer needed. */
9026
9027 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
9028 && toc->output_section->rawsize < (bfd_vma) 1 << 31
9029 && toc->reloc_count != 0)
9030 {
9031 /* Read toc relocs. */
9032 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9033 info->keep_memory);
9034 if (toc_relocs == NULL)
9035 goto error_ret;
9036
9037 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9038 {
9039 enum elf_ppc64_reloc_type r_type;
9040 unsigned long r_symndx;
9041 asection *sym_sec;
9042 struct elf_link_hash_entry *h;
9043 Elf_Internal_Sym *sym;
9044 bfd_vma val, addr;
9045
9046 r_type = ELF64_R_TYPE (rel->r_info);
9047 if (r_type != R_PPC64_ADDR64)
9048 continue;
9049
9050 r_symndx = ELF64_R_SYM (rel->r_info);
9051 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9052 r_symndx, ibfd))
9053 goto error_ret;
9054
9055 if (sym_sec == NULL
9056 || sym_sec->output_section == NULL
9057 || discarded_section (sym_sec))
9058 continue;
9059
9060 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9061 || (bfd_link_pic (info)
9062 && sym_sec == bfd_abs_section_ptr))
9063 continue;
9064
9065 if (h != NULL)
9066 {
9067 if (h->type == STT_GNU_IFUNC)
9068 continue;
9069 val = h->root.u.def.value;
9070 }
9071 else
9072 {
9073 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9074 continue;
9075 val = sym->st_value;
9076 }
9077 val += rel->r_addend;
9078 val += sym_sec->output_section->vma + sym_sec->output_offset;
9079
9080 /* We don't yet know the exact toc pointer value, but we
9081 know it will be somewhere in the toc section. Don't
9082 optimize if the difference from any possible toc
9083 pointer is outside [ff..f80008000, 7fff7fff]. */
9084 addr = toc->output_section->vma + TOC_BASE_OFF;
9085 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9086 continue;
9087
9088 addr = toc->output_section->vma + toc->output_section->rawsize;
9089 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9090 continue;
9091
9092 if (skip == NULL)
9093 {
9094 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9095 if (skip == NULL)
9096 goto error_ret;
9097 }
9098
9099 skip[rel->r_offset >> 3]
9100 |= can_optimize | ((rel - toc_relocs) << 2);
9101 }
9102 }
9103
9104 if (skip == NULL)
9105 continue;
9106
9107 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9108 if (used == NULL)
9109 {
9110 error_ret:
9111 if (symtab_hdr->contents != (unsigned char *) local_syms)
9112 free (local_syms);
9113 if (sec != NULL
9114 && elf_section_data (sec)->relocs != relstart)
9115 free (relstart);
9116 if (elf_section_data (toc)->relocs != toc_relocs)
9117 free (toc_relocs);
9118 free (skip);
9119 return false;
9120 }
9121
9122 /* Now check all kept sections that might reference the toc.
9123 Check the toc itself last. */
9124 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9125 : ibfd->sections);
9126 sec != NULL;
9127 sec = (sec == toc ? NULL
9128 : sec->next == NULL ? toc
9129 : sec->next == toc && toc->next ? toc->next
9130 : sec->next))
9131 {
9132 int repeat;
9133
9134 if (sec->reloc_count == 0
9135 || discarded_section (sec)
9136 || get_opd_info (sec)
9137 || (sec->flags & SEC_ALLOC) == 0
9138 || (sec->flags & SEC_DEBUGGING) != 0)
9139 continue;
9140
9141 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9142 info->keep_memory);
9143 if (relstart == NULL)
9144 {
9145 free (used);
9146 goto error_ret;
9147 }
9148
9149 /* Mark toc entries referenced as used. */
9150 do
9151 {
9152 repeat = 0;
9153 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9154 {
9155 enum elf_ppc64_reloc_type r_type;
9156 unsigned long r_symndx;
9157 asection *sym_sec;
9158 struct elf_link_hash_entry *h;
9159 Elf_Internal_Sym *sym;
9160 bfd_vma val;
9161
9162 r_type = ELF64_R_TYPE (rel->r_info);
9163 switch (r_type)
9164 {
9165 case R_PPC64_TOC16:
9166 case R_PPC64_TOC16_LO:
9167 case R_PPC64_TOC16_HI:
9168 case R_PPC64_TOC16_HA:
9169 case R_PPC64_TOC16_DS:
9170 case R_PPC64_TOC16_LO_DS:
9171 /* In case we're taking addresses of toc entries. */
9172 case R_PPC64_ADDR64:
9173 break;
9174
9175 default:
9176 continue;
9177 }
9178
9179 r_symndx = ELF64_R_SYM (rel->r_info);
9180 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9181 r_symndx, ibfd))
9182 {
9183 free (used);
9184 goto error_ret;
9185 }
9186
9187 if (sym_sec != toc)
9188 continue;
9189
9190 if (h != NULL)
9191 val = h->root.u.def.value;
9192 else
9193 val = sym->st_value;
9194 val += rel->r_addend;
9195
9196 if (val >= toc->size)
9197 continue;
9198
9199 if ((skip[val >> 3] & can_optimize) != 0)
9200 {
9201 bfd_vma off;
9202 unsigned char opc;
9203
9204 switch (r_type)
9205 {
9206 case R_PPC64_TOC16_HA:
9207 break;
9208
9209 case R_PPC64_TOC16_LO_DS:
9210 off = rel->r_offset;
9211 off += (bfd_big_endian (ibfd) ? -2 : 3);
9212 if (!bfd_get_section_contents (ibfd, sec, &opc,
9213 off, 1))
9214 {
9215 free (used);
9216 goto error_ret;
9217 }
9218 if ((opc & (0x3f << 2)) == (58u << 2))
9219 break;
9220 /* Fall through. */
9221
9222 default:
9223 /* Wrong sort of reloc, or not a ld. We may
9224 as well clear ref_from_discarded too. */
9225 skip[val >> 3] = 0;
9226 }
9227 }
9228
9229 if (sec != toc)
9230 used[val >> 3] = 1;
9231 /* For the toc section, we only mark as used if this
9232 entry itself isn't unused. */
9233 else if ((used[rel->r_offset >> 3]
9234 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9235 && !used[val >> 3])
9236 {
9237 /* Do all the relocs again, to catch reference
9238 chains. */
9239 repeat = 1;
9240 used[val >> 3] = 1;
9241 }
9242 }
9243 }
9244 while (repeat);
9245
9246 if (elf_section_data (sec)->relocs != relstart)
9247 free (relstart);
9248 }
9249
9250 /* Merge the used and skip arrays. Assume that TOC
9251 doublewords not appearing as either used or unused belong
9252 to an entry more than one doubleword in size. */
9253 for (drop = skip, keep = used, last = 0, some_unused = 0;
9254 drop < skip + (toc->size + 7) / 8;
9255 ++drop, ++keep)
9256 {
9257 if (*keep)
9258 {
9259 *drop &= ~ref_from_discarded;
9260 if ((*drop & can_optimize) != 0)
9261 some_unused = 1;
9262 last = 0;
9263 }
9264 else if ((*drop & ref_from_discarded) != 0)
9265 {
9266 some_unused = 1;
9267 last = ref_from_discarded;
9268 }
9269 else
9270 *drop = last;
9271 }
9272
9273 free (used);
9274
9275 if (some_unused)
9276 {
9277 bfd_byte *contents, *src;
9278 unsigned long off;
9279 Elf_Internal_Sym *sym;
9280 bool local_toc_syms = false;
9281
9282 /* Shuffle the toc contents, and at the same time convert the
9283 skip array from booleans into offsets. */
9284 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9285 goto error_ret;
9286
9287 elf_section_data (toc)->this_hdr.contents = contents;
9288
9289 for (src = contents, off = 0, drop = skip;
9290 src < contents + toc->size;
9291 src += 8, ++drop)
9292 {
9293 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9294 off += 8;
9295 else if (off != 0)
9296 {
9297 *drop = off;
9298 memcpy (src - off, src, 8);
9299 }
9300 }
9301 *drop = off;
9302 toc->rawsize = toc->size;
9303 toc->size = src - contents - off;
9304
9305 /* Adjust addends for relocs against the toc section sym,
9306 and optimize any accesses we can. */
9307 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9308 {
9309 if (sec->reloc_count == 0
9310 || discarded_section (sec))
9311 continue;
9312
9313 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9314 info->keep_memory);
9315 if (relstart == NULL)
9316 goto error_ret;
9317
9318 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9319 {
9320 enum elf_ppc64_reloc_type r_type;
9321 unsigned long r_symndx;
9322 asection *sym_sec;
9323 struct elf_link_hash_entry *h;
9324 bfd_vma val;
9325
9326 r_type = ELF64_R_TYPE (rel->r_info);
9327 switch (r_type)
9328 {
9329 default:
9330 continue;
9331
9332 case R_PPC64_TOC16:
9333 case R_PPC64_TOC16_LO:
9334 case R_PPC64_TOC16_HI:
9335 case R_PPC64_TOC16_HA:
9336 case R_PPC64_TOC16_DS:
9337 case R_PPC64_TOC16_LO_DS:
9338 case R_PPC64_ADDR64:
9339 break;
9340 }
9341
9342 r_symndx = ELF64_R_SYM (rel->r_info);
9343 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9344 r_symndx, ibfd))
9345 goto error_ret;
9346
9347 if (sym_sec != toc)
9348 continue;
9349
9350 if (h != NULL)
9351 val = h->root.u.def.value;
9352 else
9353 {
9354 val = sym->st_value;
9355 if (val != 0)
9356 local_toc_syms = true;
9357 }
9358
9359 val += rel->r_addend;
9360
9361 if (val > toc->rawsize)
9362 val = toc->rawsize;
9363 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9364 continue;
9365 else if ((skip[val >> 3] & can_optimize) != 0)
9366 {
9367 Elf_Internal_Rela *tocrel
9368 = toc_relocs + (skip[val >> 3] >> 2);
9369 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9370
9371 switch (r_type)
9372 {
9373 case R_PPC64_TOC16_HA:
9374 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9375 break;
9376
9377 case R_PPC64_TOC16_LO_DS:
9378 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9379 break;
9380
9381 default:
9382 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9383 ppc_howto_init ();
9384 info->callbacks->einfo
9385 /* xgettext:c-format */
9386 (_("%H: %s references "
9387 "optimized away TOC entry\n"),
9388 ibfd, sec, rel->r_offset,
9389 ppc64_elf_howto_table[r_type]->name);
9390 bfd_set_error (bfd_error_bad_value);
9391 goto error_ret;
9392 }
9393 rel->r_addend = tocrel->r_addend;
9394 elf_section_data (sec)->relocs = relstart;
9395 continue;
9396 }
9397
9398 if (h != NULL || sym->st_value != 0)
9399 continue;
9400
9401 rel->r_addend -= skip[val >> 3];
9402 elf_section_data (sec)->relocs = relstart;
9403 }
9404
9405 if (elf_section_data (sec)->relocs != relstart)
9406 free (relstart);
9407 }
9408
9409 /* We shouldn't have local or global symbols defined in the TOC,
9410 but handle them anyway. */
9411 if (local_syms != NULL)
9412 for (sym = local_syms;
9413 sym < local_syms + symtab_hdr->sh_info;
9414 ++sym)
9415 if (sym->st_value != 0
9416 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9417 {
9418 unsigned long i;
9419
9420 if (sym->st_value > toc->rawsize)
9421 i = toc->rawsize >> 3;
9422 else
9423 i = sym->st_value >> 3;
9424
9425 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9426 {
9427 if (local_toc_syms)
9428 _bfd_error_handler
9429 (_("%s defined on removed toc entry"),
9430 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9431 do
9432 ++i;
9433 while ((skip[i] & (ref_from_discarded | can_optimize)));
9434 sym->st_value = (bfd_vma) i << 3;
9435 }
9436
9437 sym->st_value -= skip[i];
9438 symtab_hdr->contents = (unsigned char *) local_syms;
9439 }
9440
9441 /* Adjust any global syms defined in this toc input section. */
9442 if (toc_inf.global_toc_syms)
9443 {
9444 toc_inf.toc = toc;
9445 toc_inf.skip = skip;
9446 toc_inf.global_toc_syms = false;
9447 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9448 &toc_inf);
9449 }
9450
9451 if (toc->reloc_count != 0)
9452 {
9453 Elf_Internal_Shdr *rel_hdr;
9454 Elf_Internal_Rela *wrel;
9455 bfd_size_type sz;
9456
9457 /* Remove unused toc relocs, and adjust those we keep. */
9458 if (toc_relocs == NULL)
9459 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9460 info->keep_memory);
9461 if (toc_relocs == NULL)
9462 goto error_ret;
9463
9464 wrel = toc_relocs;
9465 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9466 if ((skip[rel->r_offset >> 3]
9467 & (ref_from_discarded | can_optimize)) == 0)
9468 {
9469 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9470 wrel->r_info = rel->r_info;
9471 wrel->r_addend = rel->r_addend;
9472 ++wrel;
9473 }
9474 else if (!dec_dynrel_count (rel, toc, info,
9475 &local_syms, NULL, NULL))
9476 goto error_ret;
9477
9478 elf_section_data (toc)->relocs = toc_relocs;
9479 toc->reloc_count = wrel - toc_relocs;
9480 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9481 sz = rel_hdr->sh_entsize;
9482 rel_hdr->sh_size = toc->reloc_count * sz;
9483 }
9484 }
9485 else if (elf_section_data (toc)->relocs != toc_relocs)
9486 free (toc_relocs);
9487
9488 if (local_syms != NULL
9489 && symtab_hdr->contents != (unsigned char *) local_syms)
9490 {
9491 if (!info->keep_memory)
9492 free (local_syms);
9493 else
9494 symtab_hdr->contents = (unsigned char *) local_syms;
9495 }
9496 free (skip);
9497 }
9498
9499 /* Look for cases where we can change an indirect GOT access to
9500 a GOT relative or PC relative access, possibly reducing the
9501 number of GOT entries. */
9502 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9503 {
9504 asection *sec;
9505 Elf_Internal_Shdr *symtab_hdr;
9506 Elf_Internal_Sym *local_syms;
9507 Elf_Internal_Rela *relstart, *rel;
9508 bfd_vma got;
9509
9510 if (!is_ppc64_elf (ibfd))
9511 continue;
9512
9513 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9514 continue;
9515
9516 sec = ppc64_elf_tdata (ibfd)->got;
9517 got = 0;
9518 if (sec != NULL)
9519 got = sec->output_section->vma + sec->output_offset + 0x8000;
9520
9521 local_syms = NULL;
9522 symtab_hdr = &elf_symtab_hdr (ibfd);
9523
9524 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9525 {
9526 if (sec->reloc_count == 0
9527 || !ppc64_elf_section_data (sec)->has_optrel
9528 || discarded_section (sec))
9529 continue;
9530
9531 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9532 info->keep_memory);
9533 if (relstart == NULL)
9534 {
9535 got_error_ret:
9536 if (symtab_hdr->contents != (unsigned char *) local_syms)
9537 free (local_syms);
9538 if (sec != NULL
9539 && elf_section_data (sec)->relocs != relstart)
9540 free (relstart);
9541 return false;
9542 }
9543
9544 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9545 {
9546 enum elf_ppc64_reloc_type r_type;
9547 unsigned long r_symndx;
9548 Elf_Internal_Sym *sym;
9549 asection *sym_sec;
9550 struct elf_link_hash_entry *h;
9551 struct got_entry *ent;
9552 bfd_vma val, pc;
9553 unsigned char buf[8];
9554 unsigned int insn;
9555 enum {no_check, check_lo, check_ha} insn_check;
9556
9557 r_type = ELF64_R_TYPE (rel->r_info);
9558 switch (r_type)
9559 {
9560 default:
9561 insn_check = no_check;
9562 break;
9563
9564 case R_PPC64_PLT16_HA:
9565 case R_PPC64_GOT_TLSLD16_HA:
9566 case R_PPC64_GOT_TLSGD16_HA:
9567 case R_PPC64_GOT_TPREL16_HA:
9568 case R_PPC64_GOT_DTPREL16_HA:
9569 case R_PPC64_GOT16_HA:
9570 case R_PPC64_TOC16_HA:
9571 insn_check = check_ha;
9572 break;
9573
9574 case R_PPC64_PLT16_LO:
9575 case R_PPC64_PLT16_LO_DS:
9576 case R_PPC64_GOT_TLSLD16_LO:
9577 case R_PPC64_GOT_TLSGD16_LO:
9578 case R_PPC64_GOT_TPREL16_LO_DS:
9579 case R_PPC64_GOT_DTPREL16_LO_DS:
9580 case R_PPC64_GOT16_LO:
9581 case R_PPC64_GOT16_LO_DS:
9582 case R_PPC64_TOC16_LO:
9583 case R_PPC64_TOC16_LO_DS:
9584 insn_check = check_lo;
9585 break;
9586 }
9587
9588 if (insn_check != no_check)
9589 {
9590 bfd_vma off = rel->r_offset & ~3;
9591
9592 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9593 goto got_error_ret;
9594
9595 insn = bfd_get_32 (ibfd, buf);
9596 if (insn_check == check_lo
9597 ? !ok_lo_toc_insn (insn, r_type)
9598 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9599 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9600 {
9601 char str[12];
9602
9603 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9604 sprintf (str, "%#08x", insn);
9605 info->callbacks->einfo
9606 /* xgettext:c-format */
9607 (_("%H: got/toc optimization is not supported for"
9608 " %s instruction\n"),
9609 ibfd, sec, rel->r_offset & ~3, str);
9610 continue;
9611 }
9612 }
9613
9614 switch (r_type)
9615 {
9616 /* Note that we don't delete GOT entries for
9617 R_PPC64_GOT16_DS since we'd need a lot more
9618 analysis. For starters, the preliminary layout is
9619 before the GOT, PLT, dynamic sections and stubs are
9620 laid out. Then we'd need to allow for changes in
9621 distance between sections caused by alignment. */
9622 default:
9623 continue;
9624
9625 case R_PPC64_GOT16_HA:
9626 case R_PPC64_GOT16_LO_DS:
9627 case R_PPC64_GOT_PCREL34:
9628 break;
9629 }
9630
9631 r_symndx = ELF64_R_SYM (rel->r_info);
9632 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9633 r_symndx, ibfd))
9634 goto got_error_ret;
9635
9636 if (sym_sec == NULL
9637 || sym_sec->output_section == NULL
9638 || discarded_section (sym_sec))
9639 continue;
9640
9641 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9642 continue;
9643
9644 if (!SYMBOL_REFERENCES_LOCAL (info, h)
9645 || (bfd_link_pic (info)
9646 && sym_sec == bfd_abs_section_ptr))
9647 continue;
9648
9649 if (h != NULL)
9650 val = h->root.u.def.value;
9651 else
9652 val = sym->st_value;
9653 val += rel->r_addend;
9654 val += sym_sec->output_section->vma + sym_sec->output_offset;
9655
9656 /* Fudge factor to allow for the fact that the preliminary layout
9657 isn't exact. Reduce limits by this factor. */
9658 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9659
9660 switch (r_type)
9661 {
9662 default:
9663 continue;
9664
9665 case R_PPC64_GOT16_HA:
9666 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9667 >= LIMIT_ADJUST (0x100000000ULL))
9668 continue;
9669
9670 if (!bfd_get_section_contents (ibfd, sec, buf,
9671 rel->r_offset & ~3, 4))
9672 goto got_error_ret;
9673 insn = bfd_get_32 (ibfd, buf);
9674 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9675 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9676 continue;
9677 break;
9678
9679 case R_PPC64_GOT16_LO_DS:
9680 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9681 >= LIMIT_ADJUST (0x100000000ULL))
9682 continue;
9683 if (!bfd_get_section_contents (ibfd, sec, buf,
9684 rel->r_offset & ~3, 4))
9685 goto got_error_ret;
9686 insn = bfd_get_32 (ibfd, buf);
9687 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9688 continue;
9689 break;
9690
9691 case R_PPC64_GOT_PCREL34:
9692 pc = rel->r_offset;
9693 pc += sec->output_section->vma + sec->output_offset;
9694 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9695 >= LIMIT_ADJUST (1ULL << 34))
9696 continue;
9697 if (!bfd_get_section_contents (ibfd, sec, buf,
9698 rel->r_offset & ~3, 8))
9699 goto got_error_ret;
9700 insn = bfd_get_32 (ibfd, buf);
9701 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9702 continue;
9703 insn = bfd_get_32 (ibfd, buf + 4);
9704 if ((insn & (0x3fu << 26)) != 57u << 26)
9705 continue;
9706 break;
9707 }
9708 #undef LIMIT_ADJUST
9709
9710 if (h != NULL)
9711 ent = h->got.glist;
9712 else
9713 {
9714 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9715 ent = local_got_ents[r_symndx];
9716 }
9717 for (; ent != NULL; ent = ent->next)
9718 if (ent->addend == rel->r_addend
9719 && ent->owner == ibfd
9720 && ent->tls_type == 0)
9721 break;
9722 BFD_ASSERT (ent && ent->got.refcount > 0);
9723 ent->got.refcount -= 1;
9724 }
9725
9726 if (elf_section_data (sec)->relocs != relstart)
9727 free (relstart);
9728 }
9729
9730 if (local_syms != NULL
9731 && symtab_hdr->contents != (unsigned char *) local_syms)
9732 {
9733 if (!info->keep_memory)
9734 free (local_syms);
9735 else
9736 symtab_hdr->contents = (unsigned char *) local_syms;
9737 }
9738 }
9739
9740 return true;
9741 }
9742
9743 /* Return true iff input section I references the TOC using
9744 instructions limited to +/-32k offsets. */
9745
9746 bool
9747 ppc64_elf_has_small_toc_reloc (asection *i)
9748 {
9749 return (is_ppc64_elf (i->owner)
9750 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9751 }
9752
9753 /* Allocate space for one GOT entry. */
9754
9755 static void
9756 allocate_got (struct elf_link_hash_entry *h,
9757 struct bfd_link_info *info,
9758 struct got_entry *gent)
9759 {
9760 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9761 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9762 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9763 ? 16 : 8);
9764 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9765 ? 2 : 1) * sizeof (Elf64_External_Rela);
9766 asection *got = ppc64_elf_tdata (gent->owner)->got;
9767
9768 gent->got.offset = got->size;
9769 got->size += entsize;
9770
9771 if (h->type == STT_GNU_IFUNC)
9772 {
9773 htab->elf.irelplt->size += rentsize;
9774 htab->got_reli_size += rentsize;
9775 }
9776 else if (((bfd_link_pic (info)
9777 && (gent->tls_type == 0
9778 ? !info->enable_dt_relr
9779 : !(bfd_link_executable (info)
9780 && SYMBOL_REFERENCES_LOCAL (info, h)))
9781 && !bfd_is_abs_symbol (&h->root))
9782 || (htab->elf.dynamic_sections_created
9783 && h->dynindx != -1
9784 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9785 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9786 {
9787 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9788 relgot->size += rentsize;
9789 }
9790 }
9791
9792 /* This function merges got entries in the same toc group. */
9793
9794 static void
9795 merge_got_entries (struct got_entry **pent)
9796 {
9797 struct got_entry *ent, *ent2;
9798
9799 for (ent = *pent; ent != NULL; ent = ent->next)
9800 if (!ent->is_indirect)
9801 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9802 if (!ent2->is_indirect
9803 && ent2->addend == ent->addend
9804 && ent2->tls_type == ent->tls_type
9805 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9806 {
9807 ent2->is_indirect = true;
9808 ent2->got.ent = ent;
9809 }
9810 }
9811
9812 /* If H is undefined, make it dynamic if that makes sense. */
9813
9814 static bool
9815 ensure_undef_dynamic (struct bfd_link_info *info,
9816 struct elf_link_hash_entry *h)
9817 {
9818 struct elf_link_hash_table *htab = elf_hash_table (info);
9819
9820 if (htab->dynamic_sections_created
9821 && ((info->dynamic_undefined_weak != 0
9822 && h->root.type == bfd_link_hash_undefweak)
9823 || h->root.type == bfd_link_hash_undefined)
9824 && h->dynindx == -1
9825 && !h->forced_local
9826 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9827 return bfd_elf_link_record_dynamic_symbol (info, h);
9828 return true;
9829 }
9830
9831 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9832 usual htab->elf.splt section for a PLT entry. */
9833
9834 static inline
9835 bool use_local_plt (struct bfd_link_info *info,
9836 struct elf_link_hash_entry *h)
9837 {
9838 return (h == NULL
9839 || h->dynindx == -1
9840 || !elf_hash_table (info)->dynamic_sections_created);
9841 }
9842
9843 /* Allocate space in .plt, .got and associated reloc sections for
9844 dynamic relocs. */
9845
9846 static bool
9847 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9848 {
9849 struct bfd_link_info *info;
9850 struct ppc_link_hash_table *htab;
9851 asection *s;
9852 struct ppc_link_hash_entry *eh;
9853 struct got_entry **pgent, *gent;
9854
9855 if (h->root.type == bfd_link_hash_indirect)
9856 return true;
9857
9858 info = (struct bfd_link_info *) inf;
9859 htab = ppc_hash_table (info);
9860 if (htab == NULL)
9861 return false;
9862
9863 eh = ppc_elf_hash_entry (h);
9864 /* Run through the TLS GD got entries first if we're changing them
9865 to TPREL. */
9866 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9867 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9868 if (gent->got.refcount > 0
9869 && (gent->tls_type & TLS_GD) != 0)
9870 {
9871 /* This was a GD entry that has been converted to TPREL. If
9872 there happens to be a TPREL entry we can use that one. */
9873 struct got_entry *ent;
9874 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9875 if (ent->got.refcount > 0
9876 && (ent->tls_type & TLS_TPREL) != 0
9877 && ent->addend == gent->addend
9878 && ent->owner == gent->owner)
9879 {
9880 gent->got.refcount = 0;
9881 break;
9882 }
9883
9884 /* If not, then we'll be using our own TPREL entry. */
9885 if (gent->got.refcount != 0)
9886 gent->tls_type = TLS_TLS | TLS_TPREL;
9887 }
9888
9889 /* Remove any list entry that won't generate a word in the GOT before
9890 we call merge_got_entries. Otherwise we risk merging to empty
9891 entries. */
9892 pgent = &h->got.glist;
9893 while ((gent = *pgent) != NULL)
9894 if (gent->got.refcount > 0)
9895 {
9896 if ((gent->tls_type & TLS_LD) != 0
9897 && SYMBOL_REFERENCES_LOCAL (info, h))
9898 {
9899 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9900 *pgent = gent->next;
9901 }
9902 else
9903 pgent = &gent->next;
9904 }
9905 else
9906 *pgent = gent->next;
9907
9908 if (!htab->do_multi_toc)
9909 merge_got_entries (&h->got.glist);
9910
9911 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9912 if (!gent->is_indirect)
9913 {
9914 /* Ensure we catch all the cases where this symbol should
9915 be made dynamic. */
9916 if (!ensure_undef_dynamic (info, h))
9917 return false;
9918
9919 if (!is_ppc64_elf (gent->owner))
9920 abort ();
9921
9922 allocate_got (h, info, gent);
9923 }
9924
9925 /* If no dynamic sections we can't have dynamic relocs, except for
9926 IFUNCs which are handled even in static executables. */
9927 if (!htab->elf.dynamic_sections_created
9928 && h->type != STT_GNU_IFUNC)
9929 h->dyn_relocs = NULL;
9930
9931 /* Discard relocs on undefined symbols that must be local. */
9932 else if (h->root.type == bfd_link_hash_undefined
9933 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9934 h->dyn_relocs = NULL;
9935
9936 /* Also discard relocs on undefined weak syms with non-default
9937 visibility, or when dynamic_undefined_weak says so. */
9938 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9939 h->dyn_relocs = NULL;
9940
9941 if (h->dyn_relocs != NULL)
9942 {
9943 struct ppc_dyn_relocs *p, **pp;
9944
9945 /* In the shared -Bsymbolic case, discard space allocated for
9946 dynamic pc-relative relocs against symbols which turn out to
9947 be defined in regular objects. For the normal shared case,
9948 discard space for relocs that have become local due to symbol
9949 visibility changes. */
9950 if (bfd_link_pic (info))
9951 {
9952 /* Relocs that use pc_count are those that appear on a call
9953 insn, or certain REL relocs (see must_be_dyn_reloc) that
9954 can be generated via assembly. We want calls to
9955 protected symbols to resolve directly to the function
9956 rather than going via the plt. If people want function
9957 pointer comparisons to work as expected then they should
9958 avoid writing weird assembly. */
9959 if (SYMBOL_CALLS_LOCAL (info, h))
9960 {
9961 for (pp = (struct ppc_dyn_relocs **) &h->dyn_relocs;
9962 (p = *pp) != NULL;
9963 )
9964 {
9965 p->count -= p->pc_count;
9966 p->pc_count = 0;
9967 if (p->count == 0)
9968 *pp = p->next;
9969 else
9970 pp = &p->next;
9971 }
9972 }
9973
9974 if (h->dyn_relocs != NULL)
9975 {
9976 /* Ensure we catch all the cases where this symbol
9977 should be made dynamic. */
9978 if (!ensure_undef_dynamic (info, h))
9979 return false;
9980 }
9981 }
9982
9983 /* For a fixed position executable, discard space for
9984 relocs against symbols which are not dynamic. */
9985 else if (h->type != STT_GNU_IFUNC)
9986 {
9987 if ((h->dynamic_adjusted
9988 || (h->ref_regular
9989 && h->root.type == bfd_link_hash_undefweak
9990 && (info->dynamic_undefined_weak > 0
9991 || !_bfd_elf_readonly_dynrelocs (h))))
9992 && !h->def_regular
9993 && !ELF_COMMON_DEF_P (h))
9994 {
9995 /* Ensure we catch all the cases where this symbol
9996 should be made dynamic. */
9997 if (!ensure_undef_dynamic (info, h))
9998 return false;
9999
10000 /* But if that didn't work out, discard dynamic relocs. */
10001 if (h->dynindx == -1)
10002 h->dyn_relocs = NULL;
10003 }
10004 else
10005 h->dyn_relocs = NULL;
10006 }
10007
10008 /* Finally, allocate space. */
10009 for (p = (struct ppc_dyn_relocs *) h->dyn_relocs; p != NULL; p = p->next)
10010 {
10011 unsigned int count;
10012 asection *sreloc = elf_section_data (p->sec)->sreloc;
10013 if (eh->elf.type == STT_GNU_IFUNC)
10014 sreloc = htab->elf.irelplt;
10015 count = p->count;
10016 if (info->enable_dt_relr
10017 && ((!NO_OPD_RELOCS
10018 && ppc64_elf_section_data (p->sec)->sec_type == sec_opd)
10019 || (eh->elf.type != STT_GNU_IFUNC
10020 && SYMBOL_REFERENCES_LOCAL (info, h))))
10021 count -= p->rel_count;
10022 sreloc->size += count * sizeof (Elf64_External_Rela);
10023 }
10024 }
10025
10026 /* We might need a PLT entry when the symbol
10027 a) is dynamic, or
10028 b) is an ifunc, or
10029 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
10030 d) has plt16 relocs and we are linking statically. */
10031 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
10032 || h->type == STT_GNU_IFUNC
10033 || (h->needs_plt && h->dynamic_adjusted)
10034 || (h->needs_plt
10035 && h->def_regular
10036 && !htab->elf.dynamic_sections_created
10037 && !htab->can_convert_all_inline_plt
10038 && (ppc_elf_hash_entry (h)->tls_mask
10039 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
10040 {
10041 struct plt_entry *pent;
10042 bool doneone = false;
10043 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10044 if (pent->plt.refcount > 0)
10045 {
10046 if (!ensure_undef_dynamic (info, h))
10047 return false;
10048
10049 if (use_local_plt (info, h))
10050 {
10051 if (h->type == STT_GNU_IFUNC)
10052 {
10053 s = htab->elf.iplt;
10054 pent->plt.offset = s->size;
10055 s->size += PLT_ENTRY_SIZE (htab);
10056 s = htab->elf.irelplt;
10057 }
10058 else
10059 {
10060 s = htab->pltlocal;
10061 pent->plt.offset = s->size;
10062 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10063 s = NULL;
10064 if (bfd_link_pic (info)
10065 && !(info->enable_dt_relr && !htab->opd_abi))
10066 s = htab->relpltlocal;
10067 }
10068 }
10069 else
10070 {
10071 /* If this is the first .plt entry, make room for the special
10072 first entry. */
10073 s = htab->elf.splt;
10074 if (s->size == 0)
10075 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10076
10077 pent->plt.offset = s->size;
10078
10079 /* Make room for this entry. */
10080 s->size += PLT_ENTRY_SIZE (htab);
10081
10082 /* Make room for the .glink code. */
10083 s = htab->glink;
10084 if (s->size == 0)
10085 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10086 if (htab->opd_abi)
10087 {
10088 /* We need bigger stubs past index 32767. */
10089 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10090 s->size += 4;
10091 s->size += 2*4;
10092 }
10093 else
10094 s->size += 4;
10095
10096 /* We also need to make an entry in the .rela.plt section. */
10097 s = htab->elf.srelplt;
10098 }
10099 if (s != NULL)
10100 s->size += sizeof (Elf64_External_Rela);
10101 doneone = true;
10102 }
10103 else
10104 pent->plt.offset = (bfd_vma) -1;
10105 if (!doneone)
10106 {
10107 h->plt.plist = NULL;
10108 h->needs_plt = 0;
10109 }
10110 }
10111 else
10112 {
10113 h->plt.plist = NULL;
10114 h->needs_plt = 0;
10115 }
10116
10117 return true;
10118 }
10119
10120 #define PPC_LO(v) ((v) & 0xffff)
10121 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10122 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10123 #define D34(v) \
10124 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10125 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10126
10127 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10128 to set up space for global entry stubs. These are put in glink,
10129 after the branch table. */
10130
10131 static bool
10132 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10133 {
10134 struct bfd_link_info *info;
10135 struct ppc_link_hash_table *htab;
10136 struct plt_entry *pent;
10137 asection *s, *plt;
10138
10139 if (h->root.type == bfd_link_hash_indirect)
10140 return true;
10141
10142 if (!h->pointer_equality_needed)
10143 return true;
10144
10145 if (h->def_regular)
10146 return true;
10147
10148 info = inf;
10149 htab = ppc_hash_table (info);
10150 if (htab == NULL)
10151 return false;
10152
10153 s = htab->global_entry;
10154 plt = htab->elf.splt;
10155 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10156 if (pent->plt.offset != (bfd_vma) -1
10157 && pent->addend == 0)
10158 {
10159 /* For ELFv2, if this symbol is not defined in a regular file
10160 and we are not generating a shared library or pie, then we
10161 need to define the symbol in the executable on a call stub.
10162 This is to avoid text relocations. */
10163 bfd_vma off, stub_align, stub_off, stub_size;
10164 unsigned int align_power;
10165
10166 stub_size = 16;
10167 stub_off = s->size;
10168 if (htab->params->plt_stub_align >= 0)
10169 align_power = htab->params->plt_stub_align;
10170 else
10171 align_power = -htab->params->plt_stub_align;
10172 /* Setting section alignment is delayed until we know it is
10173 non-empty. Otherwise the .text output section will be
10174 aligned at least to plt_stub_align even when no global
10175 entry stubs are needed. */
10176 if (s->alignment_power < align_power)
10177 s->alignment_power = align_power;
10178 stub_align = (bfd_vma) 1 << align_power;
10179 if (htab->params->plt_stub_align >= 0
10180 || ((((stub_off + stub_size - 1) & -stub_align)
10181 - (stub_off & -stub_align))
10182 > ((stub_size - 1) & -stub_align)))
10183 stub_off = (stub_off + stub_align - 1) & -stub_align;
10184 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10185 off -= stub_off + s->output_offset + s->output_section->vma;
10186 /* Note that for --plt-stub-align negative we have a possible
10187 dependency between stub offset and size. Break that
10188 dependency by assuming the max stub size when calculating
10189 the stub offset. */
10190 if (PPC_HA (off) == 0)
10191 stub_size -= 4;
10192 h->root.type = bfd_link_hash_defined;
10193 h->root.u.def.section = s;
10194 h->root.u.def.value = stub_off;
10195 s->size = stub_off + stub_size;
10196 break;
10197 }
10198 return true;
10199 }
10200
10201 /* Set the sizes of the dynamic sections. */
10202
10203 static bool
10204 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10205 struct bfd_link_info *info)
10206 {
10207 struct ppc_link_hash_table *htab;
10208 bfd *dynobj;
10209 asection *s;
10210 bool relocs;
10211 bfd *ibfd;
10212 struct got_entry *first_tlsld;
10213
10214 htab = ppc_hash_table (info);
10215 if (htab == NULL)
10216 return false;
10217
10218 dynobj = htab->elf.dynobj;
10219 if (dynobj == NULL)
10220 abort ();
10221
10222 if (htab->elf.dynamic_sections_created)
10223 {
10224 /* Set the contents of the .interp section to the interpreter. */
10225 if (bfd_link_executable (info) && !info->nointerp)
10226 {
10227 s = bfd_get_linker_section (dynobj, ".interp");
10228 if (s == NULL)
10229 abort ();
10230 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10231 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10232 }
10233 }
10234
10235 /* Set up .got offsets for local syms, and space for local dynamic
10236 relocs. */
10237 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10238 {
10239 struct got_entry **lgot_ents;
10240 struct got_entry **end_lgot_ents;
10241 struct plt_entry **local_plt;
10242 struct plt_entry **end_local_plt;
10243 unsigned char *lgot_masks;
10244 bfd_size_type locsymcount;
10245 Elf_Internal_Shdr *symtab_hdr;
10246 Elf_Internal_Sym *local_syms;
10247 Elf_Internal_Sym *isym;
10248
10249 if (!is_ppc64_elf (ibfd))
10250 continue;
10251
10252 for (s = ibfd->sections; s != NULL; s = s->next)
10253 {
10254 struct ppc_local_dyn_relocs *p;
10255
10256 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10257 {
10258 if (!bfd_is_abs_section (p->sec)
10259 && bfd_is_abs_section (p->sec->output_section))
10260 {
10261 /* Input section has been discarded, either because
10262 it is a copy of a linkonce section or due to
10263 linker script /DISCARD/, so we'll be discarding
10264 the relocs too. */
10265 }
10266 else if (p->count != 0)
10267 {
10268 unsigned int count;
10269 asection *srel;
10270
10271 count = p->count;
10272 if (info->enable_dt_relr
10273 && ((!NO_OPD_RELOCS
10274 && (ppc64_elf_section_data (p->sec)->sec_type
10275 == sec_opd))
10276 || !p->ifunc))
10277 count -= p->rel_count;
10278 srel = elf_section_data (p->sec)->sreloc;
10279 if (p->ifunc)
10280 srel = htab->elf.irelplt;
10281 srel->size += count * sizeof (Elf64_External_Rela);
10282 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10283 info->flags |= DF_TEXTREL;
10284 }
10285 }
10286 }
10287
10288 lgot_ents = elf_local_got_ents (ibfd);
10289 if (!lgot_ents)
10290 continue;
10291
10292 symtab_hdr = &elf_symtab_hdr (ibfd);
10293 locsymcount = symtab_hdr->sh_info;
10294 end_lgot_ents = lgot_ents + locsymcount;
10295 local_plt = (struct plt_entry **) end_lgot_ents;
10296 end_local_plt = local_plt + locsymcount;
10297 lgot_masks = (unsigned char *) end_local_plt;
10298 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
10299 if (local_syms == NULL && locsymcount != 0)
10300 {
10301 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
10302 0, NULL, NULL, NULL);
10303 if (local_syms == NULL)
10304 return false;
10305 }
10306 s = ppc64_elf_tdata (ibfd)->got;
10307 for (isym = local_syms;
10308 lgot_ents < end_lgot_ents;
10309 ++lgot_ents, ++lgot_masks, isym++)
10310 {
10311 struct got_entry **pent, *ent;
10312
10313 pent = lgot_ents;
10314 while ((ent = *pent) != NULL)
10315 if (ent->got.refcount > 0)
10316 {
10317 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10318 {
10319 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10320 *pent = ent->next;
10321 }
10322 else
10323 {
10324 unsigned int ent_size = 8;
10325 unsigned int rel_size = sizeof (Elf64_External_Rela);
10326
10327 ent->got.offset = s->size;
10328 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10329 {
10330 ent_size *= 2;
10331 rel_size *= 2;
10332 }
10333 s->size += ent_size;
10334 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10335 {
10336 htab->elf.irelplt->size += rel_size;
10337 htab->got_reli_size += rel_size;
10338 }
10339 else if (bfd_link_pic (info)
10340 && (ent->tls_type == 0
10341 ? !info->enable_dt_relr
10342 : !bfd_link_executable (info))
10343 && isym->st_shndx != SHN_ABS)
10344 {
10345 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10346 srel->size += rel_size;
10347 }
10348 pent = &ent->next;
10349 }
10350 }
10351 else
10352 *pent = ent->next;
10353 }
10354 if (local_syms != NULL
10355 && symtab_hdr->contents != (unsigned char *) local_syms)
10356 {
10357 if (!info->keep_memory)
10358 free (local_syms);
10359 else
10360 symtab_hdr->contents = (unsigned char *) local_syms;
10361 }
10362
10363 /* Allocate space for plt calls to local syms. */
10364 lgot_masks = (unsigned char *) end_local_plt;
10365 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10366 {
10367 struct plt_entry *ent;
10368
10369 for (ent = *local_plt; ent != NULL; ent = ent->next)
10370 if (ent->plt.refcount > 0)
10371 {
10372 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10373 {
10374 s = htab->elf.iplt;
10375 ent->plt.offset = s->size;
10376 s->size += PLT_ENTRY_SIZE (htab);
10377 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10378 }
10379 else if (htab->can_convert_all_inline_plt
10380 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10381 ent->plt.offset = (bfd_vma) -1;
10382 else
10383 {
10384 s = htab->pltlocal;
10385 ent->plt.offset = s->size;
10386 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10387 if (bfd_link_pic (info)
10388 && !(info->enable_dt_relr && !htab->opd_abi))
10389 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10390 }
10391 }
10392 else
10393 ent->plt.offset = (bfd_vma) -1;
10394 }
10395 }
10396
10397 /* Allocate global sym .plt and .got entries, and space for global
10398 sym dynamic relocs. */
10399 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10400
10401 if (!htab->opd_abi && !bfd_link_pic (info))
10402 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10403
10404 first_tlsld = NULL;
10405 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10406 {
10407 struct got_entry *ent;
10408
10409 if (!is_ppc64_elf (ibfd))
10410 continue;
10411
10412 ent = ppc64_tlsld_got (ibfd);
10413 if (ent->got.refcount > 0)
10414 {
10415 if (!htab->do_multi_toc && first_tlsld != NULL)
10416 {
10417 ent->is_indirect = true;
10418 ent->got.ent = first_tlsld;
10419 }
10420 else
10421 {
10422 if (first_tlsld == NULL)
10423 first_tlsld = ent;
10424 s = ppc64_elf_tdata (ibfd)->got;
10425 ent->got.offset = s->size;
10426 ent->owner = ibfd;
10427 s->size += 16;
10428 if (bfd_link_dll (info))
10429 {
10430 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10431 srel->size += sizeof (Elf64_External_Rela);
10432 }
10433 }
10434 }
10435 else
10436 ent->got.offset = (bfd_vma) -1;
10437 }
10438
10439 /* We now have determined the sizes of the various dynamic sections.
10440 Allocate memory for them. */
10441 relocs = false;
10442 for (s = dynobj->sections; s != NULL; s = s->next)
10443 {
10444 if ((s->flags & SEC_LINKER_CREATED) == 0)
10445 continue;
10446
10447 if (s == htab->brlt || s == htab->relbrlt || s == htab->elf.srelrdyn)
10448 /* These haven't been allocated yet; don't strip. */
10449 continue;
10450 else if (s == htab->elf.sgot
10451 || s == htab->elf.splt
10452 || s == htab->elf.iplt
10453 || s == htab->pltlocal
10454 || s == htab->glink
10455 || s == htab->global_entry
10456 || s == htab->elf.sdynbss
10457 || s == htab->elf.sdynrelro)
10458 {
10459 /* Strip this section if we don't need it; see the
10460 comment below. */
10461 }
10462 else if (s == htab->glink_eh_frame)
10463 {
10464 if (!bfd_is_abs_section (s->output_section))
10465 /* Not sized yet. */
10466 continue;
10467 }
10468 else if (startswith (s->name, ".rela"))
10469 {
10470 if (s->size != 0)
10471 {
10472 if (s != htab->elf.srelplt)
10473 relocs = true;
10474
10475 /* We use the reloc_count field as a counter if we need
10476 to copy relocs into the output file. */
10477 s->reloc_count = 0;
10478 }
10479 }
10480 else
10481 {
10482 /* It's not one of our sections, so don't allocate space. */
10483 continue;
10484 }
10485
10486 if (s->size == 0)
10487 {
10488 /* If we don't need this section, strip it from the
10489 output file. This is mostly to handle .rela.bss and
10490 .rela.plt. We must create both sections in
10491 create_dynamic_sections, because they must be created
10492 before the linker maps input sections to output
10493 sections. The linker does that before
10494 adjust_dynamic_symbol is called, and it is that
10495 function which decides whether anything needs to go
10496 into these sections. */
10497 s->flags |= SEC_EXCLUDE;
10498 continue;
10499 }
10500
10501 if (bfd_is_abs_section (s->output_section))
10502 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10503 s->name);
10504
10505 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10506 continue;
10507
10508 /* Allocate memory for the section contents. We use bfd_zalloc
10509 here in case unused entries are not reclaimed before the
10510 section's contents are written out. This should not happen,
10511 but this way if it does we get a R_PPC64_NONE reloc in .rela
10512 sections instead of garbage.
10513 We also rely on the section contents being zero when writing
10514 the GOT and .dynrelro. */
10515 s->contents = bfd_zalloc (dynobj, s->size);
10516 if (s->contents == NULL)
10517 return false;
10518 }
10519
10520 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10521 {
10522 if (!is_ppc64_elf (ibfd))
10523 continue;
10524
10525 s = ppc64_elf_tdata (ibfd)->got;
10526 if (s != NULL && s != htab->elf.sgot)
10527 {
10528 if (s->size == 0)
10529 s->flags |= SEC_EXCLUDE;
10530 else
10531 {
10532 s->contents = bfd_zalloc (ibfd, s->size);
10533 if (s->contents == NULL)
10534 return false;
10535 }
10536 }
10537 s = ppc64_elf_tdata (ibfd)->relgot;
10538 if (s != NULL)
10539 {
10540 if (s->size == 0)
10541 s->flags |= SEC_EXCLUDE;
10542 else
10543 {
10544 s->contents = bfd_zalloc (ibfd, s->size);
10545 if (s->contents == NULL)
10546 return false;
10547 relocs = true;
10548 s->reloc_count = 0;
10549 }
10550 }
10551 }
10552
10553 if (htab->elf.dynamic_sections_created)
10554 {
10555 bool tls_opt;
10556
10557 /* Add some entries to the .dynamic section. We fill in the
10558 values later, in ppc64_elf_finish_dynamic_sections, but we
10559 must add the entries now so that we get the correct size for
10560 the .dynamic section. The DT_DEBUG entry is filled in by the
10561 dynamic linker and used by the debugger. */
10562 #define add_dynamic_entry(TAG, VAL) \
10563 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10564
10565 if (bfd_link_executable (info))
10566 {
10567 if (!add_dynamic_entry (DT_DEBUG, 0))
10568 return false;
10569 }
10570
10571 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10572 {
10573 if (!add_dynamic_entry (DT_PLTGOT, 0)
10574 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10575 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10576 || !add_dynamic_entry (DT_JMPREL, 0)
10577 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10578 return false;
10579 }
10580
10581 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10582 {
10583 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10584 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10585 return false;
10586 }
10587
10588 tls_opt = (htab->params->tls_get_addr_opt
10589 && ((htab->tls_get_addr_fd != NULL
10590 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10591 || (htab->tga_desc_fd != NULL
10592 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10593 if (tls_opt || !htab->opd_abi)
10594 {
10595 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10596 return false;
10597 }
10598
10599 if (relocs)
10600 {
10601 if (!add_dynamic_entry (DT_RELA, 0)
10602 || !add_dynamic_entry (DT_RELASZ, 0)
10603 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10604 return false;
10605
10606 /* If any dynamic relocs apply to a read-only section,
10607 then we need a DT_TEXTREL entry. */
10608 if ((info->flags & DF_TEXTREL) == 0)
10609 elf_link_hash_traverse (&htab->elf,
10610 _bfd_elf_maybe_set_textrel, info);
10611
10612 if ((info->flags & DF_TEXTREL) != 0)
10613 {
10614 if (!add_dynamic_entry (DT_TEXTREL, 0))
10615 return false;
10616 }
10617 }
10618 }
10619 #undef add_dynamic_entry
10620
10621 return true;
10622 }
10623
10624 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10625
10626 static bool
10627 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10628 {
10629 if (h->plt.plist != NULL
10630 && !h->def_regular
10631 && !h->pointer_equality_needed)
10632 return false;
10633
10634 return _bfd_elf_hash_symbol (h);
10635 }
10636
10637 /* Determine the type of stub needed, if any, for a call. */
10638
10639 static inline enum ppc_stub_main_type
10640 ppc_type_of_stub (asection *input_sec,
10641 const Elf_Internal_Rela *rel,
10642 struct ppc_link_hash_entry **hash,
10643 struct plt_entry **plt_ent,
10644 bfd_vma destination,
10645 unsigned long local_off)
10646 {
10647 struct ppc_link_hash_entry *h = *hash;
10648 bfd_vma location;
10649 bfd_vma branch_offset;
10650 bfd_vma max_branch_offset;
10651 enum elf_ppc64_reloc_type r_type;
10652
10653 if (h != NULL)
10654 {
10655 struct plt_entry *ent;
10656 struct ppc_link_hash_entry *fdh = h;
10657 if (h->oh != NULL
10658 && h->oh->is_func_descriptor)
10659 {
10660 fdh = ppc_follow_link (h->oh);
10661 *hash = fdh;
10662 }
10663
10664 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10665 if (ent->addend == rel->r_addend
10666 && ent->plt.offset != (bfd_vma) -1)
10667 {
10668 *plt_ent = ent;
10669 return ppc_stub_plt_call;
10670 }
10671
10672 /* Here, we know we don't have a plt entry. If we don't have a
10673 either a defined function descriptor or a defined entry symbol
10674 in a regular object file, then it is pointless trying to make
10675 any other type of stub. */
10676 if (!is_static_defined (&fdh->elf)
10677 && !is_static_defined (&h->elf))
10678 return ppc_stub_none;
10679 }
10680 else if (elf_local_got_ents (input_sec->owner) != NULL)
10681 {
10682 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10683 struct plt_entry **local_plt = (struct plt_entry **)
10684 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10685 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10686
10687 if (local_plt[r_symndx] != NULL)
10688 {
10689 struct plt_entry *ent;
10690
10691 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10692 if (ent->addend == rel->r_addend
10693 && ent->plt.offset != (bfd_vma) -1)
10694 {
10695 *plt_ent = ent;
10696 return ppc_stub_plt_call;
10697 }
10698 }
10699 }
10700
10701 /* Determine where the call point is. */
10702 location = (input_sec->output_offset
10703 + input_sec->output_section->vma
10704 + rel->r_offset);
10705
10706 branch_offset = destination - location;
10707 r_type = ELF64_R_TYPE (rel->r_info);
10708
10709 /* Determine if a long branch stub is needed. */
10710 max_branch_offset = 1 << 25;
10711 if (r_type == R_PPC64_REL14
10712 || r_type == R_PPC64_REL14_BRTAKEN
10713 || r_type == R_PPC64_REL14_BRNTAKEN)
10714 max_branch_offset = 1 << 15;
10715
10716 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10717 /* We need a stub. Figure out whether a long_branch or plt_branch
10718 is needed later. */
10719 return ppc_stub_long_branch;
10720
10721 return ppc_stub_none;
10722 }
10723
10724 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10725 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10726 . mflr %r12
10727 . bcl 20,31,1f
10728 .1: mflr %r11
10729 . mtlr %r12
10730 . lis %r12,xxx-1b@highest
10731 . ori %r12,%r12,xxx-1b@higher
10732 . sldi %r12,%r12,32
10733 . oris %r12,%r12,xxx-1b@high
10734 . ori %r12,%r12,xxx-1b@l
10735 . add/ldx %r12,%r11,%r12 */
10736
10737 static bfd_byte *
10738 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10739 {
10740 bfd_put_32 (abfd, MFLR_R12, p);
10741 p += 4;
10742 bfd_put_32 (abfd, BCL_20_31, p);
10743 p += 4;
10744 bfd_put_32 (abfd, MFLR_R11, p);
10745 p += 4;
10746 bfd_put_32 (abfd, MTLR_R12, p);
10747 p += 4;
10748 if (off + 0x8000 < 0x10000)
10749 {
10750 if (load)
10751 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10752 else
10753 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10754 p += 4;
10755 }
10756 else if (off + 0x80008000ULL < 0x100000000ULL)
10757 {
10758 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10759 p += 4;
10760 if (load)
10761 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10762 else
10763 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10764 p += 4;
10765 }
10766 else
10767 {
10768 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10769 {
10770 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10771 p += 4;
10772 }
10773 else
10774 {
10775 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10776 p += 4;
10777 if (((off >> 32) & 0xffff) != 0)
10778 {
10779 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10780 p += 4;
10781 }
10782 }
10783 if (((off >> 32) & 0xffffffffULL) != 0)
10784 {
10785 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10786 p += 4;
10787 }
10788 if (PPC_HI (off) != 0)
10789 {
10790 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10791 p += 4;
10792 }
10793 if (PPC_LO (off) != 0)
10794 {
10795 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10796 p += 4;
10797 }
10798 if (load)
10799 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10800 else
10801 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10802 p += 4;
10803 }
10804 return p;
10805 }
10806
10807 static unsigned int
10808 size_offset (bfd_vma off)
10809 {
10810 unsigned int size;
10811 if (off + 0x8000 < 0x10000)
10812 size = 4;
10813 else if (off + 0x80008000ULL < 0x100000000ULL)
10814 size = 8;
10815 else
10816 {
10817 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10818 size = 4;
10819 else
10820 {
10821 size = 4;
10822 if (((off >> 32) & 0xffff) != 0)
10823 size += 4;
10824 }
10825 if (((off >> 32) & 0xffffffffULL) != 0)
10826 size += 4;
10827 if (PPC_HI (off) != 0)
10828 size += 4;
10829 if (PPC_LO (off) != 0)
10830 size += 4;
10831 size += 4;
10832 }
10833 return size + 16;
10834 }
10835
10836 static unsigned int
10837 num_relocs_for_offset (bfd_vma off)
10838 {
10839 unsigned int num_rel;
10840 if (off + 0x8000 < 0x10000)
10841 num_rel = 1;
10842 else if (off + 0x80008000ULL < 0x100000000ULL)
10843 num_rel = 2;
10844 else
10845 {
10846 num_rel = 1;
10847 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10848 && ((off >> 32) & 0xffff) != 0)
10849 num_rel += 1;
10850 if (PPC_HI (off) != 0)
10851 num_rel += 1;
10852 if (PPC_LO (off) != 0)
10853 num_rel += 1;
10854 }
10855 return num_rel;
10856 }
10857
10858 static Elf_Internal_Rela *
10859 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10860 bfd_vma roff, bfd_vma targ, bfd_vma off)
10861 {
10862 bfd_vma relative_targ = targ - (roff - 8);
10863 if (bfd_big_endian (info->output_bfd))
10864 roff += 2;
10865 r->r_offset = roff;
10866 r->r_addend = relative_targ + roff;
10867 if (off + 0x8000 < 0x10000)
10868 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10869 else if (off + 0x80008000ULL < 0x100000000ULL)
10870 {
10871 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10872 ++r;
10873 roff += 4;
10874 r->r_offset = roff;
10875 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10876 r->r_addend = relative_targ + roff;
10877 }
10878 else
10879 {
10880 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10881 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10882 else
10883 {
10884 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10885 if (((off >> 32) & 0xffff) != 0)
10886 {
10887 ++r;
10888 roff += 4;
10889 r->r_offset = roff;
10890 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10891 r->r_addend = relative_targ + roff;
10892 }
10893 }
10894 if (((off >> 32) & 0xffffffffULL) != 0)
10895 roff += 4;
10896 if (PPC_HI (off) != 0)
10897 {
10898 ++r;
10899 roff += 4;
10900 r->r_offset = roff;
10901 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10902 r->r_addend = relative_targ + roff;
10903 }
10904 if (PPC_LO (off) != 0)
10905 {
10906 ++r;
10907 roff += 4;
10908 r->r_offset = roff;
10909 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10910 r->r_addend = relative_targ + roff;
10911 }
10912 }
10913 return r;
10914 }
10915
10916 static bfd_byte *
10917 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10918 bool load)
10919 {
10920 uint64_t insn;
10921 if (off - odd + (1ULL << 33) < 1ULL << 34)
10922 {
10923 off -= odd;
10924 if (odd)
10925 {
10926 bfd_put_32 (abfd, NOP, p);
10927 p += 4;
10928 }
10929 if (load)
10930 insn = PLD_R12_PC;
10931 else
10932 insn = PADDI_R12_PC;
10933 insn |= D34 (off);
10934 bfd_put_32 (abfd, insn >> 32, p);
10935 p += 4;
10936 bfd_put_32 (abfd, insn, p);
10937 }
10938 /* The minimum value for paddi is -0x200000000. The minimum value
10939 for li is -0x8000, which when shifted by 34 and added gives a
10940 minimum value of -0x2000200000000. The maximum value is
10941 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10942 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10943 {
10944 off -= 8 - odd;
10945 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10946 p += 4;
10947 if (!odd)
10948 {
10949 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10950 p += 4;
10951 }
10952 insn = PADDI_R12_PC | D34 (off);
10953 bfd_put_32 (abfd, insn >> 32, p);
10954 p += 4;
10955 bfd_put_32 (abfd, insn, p);
10956 p += 4;
10957 if (odd)
10958 {
10959 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10960 p += 4;
10961 }
10962 if (load)
10963 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10964 else
10965 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10966 }
10967 else
10968 {
10969 off -= odd + 8;
10970 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10971 p += 4;
10972 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10973 p += 4;
10974 if (odd)
10975 {
10976 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10977 p += 4;
10978 }
10979 insn = PADDI_R12_PC | D34 (off);
10980 bfd_put_32 (abfd, insn >> 32, p);
10981 p += 4;
10982 bfd_put_32 (abfd, insn, p);
10983 p += 4;
10984 if (!odd)
10985 {
10986 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10987 p += 4;
10988 }
10989 if (load)
10990 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10991 else
10992 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10993 }
10994 p += 4;
10995 return p;
10996 }
10997
10998 static unsigned int
10999 size_power10_offset (bfd_vma off, int odd)
11000 {
11001 if (off - odd + (1ULL << 33) < 1ULL << 34)
11002 return odd + 8;
11003 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11004 return 20;
11005 else
11006 return 24;
11007 }
11008
11009 static unsigned int
11010 num_relocs_for_power10_offset (bfd_vma off, int odd)
11011 {
11012 if (off - odd + (1ULL << 33) < 1ULL << 34)
11013 return 1;
11014 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11015 return 2;
11016 else
11017 return 3;
11018 }
11019
11020 static Elf_Internal_Rela *
11021 emit_relocs_for_power10_offset (struct bfd_link_info *info,
11022 Elf_Internal_Rela *r, bfd_vma roff,
11023 bfd_vma targ, bfd_vma off, int odd)
11024 {
11025 if (off - odd + (1ULL << 33) < 1ULL << 34)
11026 roff += odd;
11027 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
11028 {
11029 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11030 r->r_offset = roff + d_offset;
11031 r->r_addend = targ + 8 - odd - d_offset;
11032 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11033 ++r;
11034 roff += 8 - odd;
11035 }
11036 else
11037 {
11038 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
11039 r->r_offset = roff + d_offset;
11040 r->r_addend = targ + 8 + odd - d_offset;
11041 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
11042 ++r;
11043 roff += 4;
11044 r->r_offset = roff + d_offset;
11045 r->r_addend = targ + 4 + odd - d_offset;
11046 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
11047 ++r;
11048 roff += 4 + odd;
11049 }
11050 r->r_offset = roff;
11051 r->r_addend = targ;
11052 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
11053 return r;
11054 }
11055
11056 /* Emit .eh_frame opcode to advance pc by DELTA. */
11057
11058 static bfd_byte *
11059 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
11060 {
11061 delta /= 4;
11062 if (delta < 64)
11063 *eh++ = DW_CFA_advance_loc + delta;
11064 else if (delta < 256)
11065 {
11066 *eh++ = DW_CFA_advance_loc1;
11067 *eh++ = delta;
11068 }
11069 else if (delta < 65536)
11070 {
11071 *eh++ = DW_CFA_advance_loc2;
11072 bfd_put_16 (abfd, delta, eh);
11073 eh += 2;
11074 }
11075 else
11076 {
11077 *eh++ = DW_CFA_advance_loc4;
11078 bfd_put_32 (abfd, delta, eh);
11079 eh += 4;
11080 }
11081 return eh;
11082 }
11083
11084 /* Size of required .eh_frame opcode to advance pc by DELTA. */
11085
11086 static unsigned int
11087 eh_advance_size (unsigned int delta)
11088 {
11089 if (delta < 64 * 4)
11090 /* DW_CFA_advance_loc+[1..63]. */
11091 return 1;
11092 if (delta < 256 * 4)
11093 /* DW_CFA_advance_loc1, byte. */
11094 return 2;
11095 if (delta < 65536 * 4)
11096 /* DW_CFA_advance_loc2, 2 bytes. */
11097 return 3;
11098 /* DW_CFA_advance_loc4, 4 bytes. */
11099 return 5;
11100 }
11101
11102 /* With power7 weakly ordered memory model, it is possible for ld.so
11103 to update a plt entry in one thread and have another thread see a
11104 stale zero toc entry. To avoid this we need some sort of acquire
11105 barrier in the call stub. One solution is to make the load of the
11106 toc word seem to appear to depend on the load of the function entry
11107 word. Another solution is to test for r2 being zero, and branch to
11108 the appropriate glink entry if so.
11109
11110 . fake dep barrier compare
11111 . ld 12,xxx(2) ld 12,xxx(2)
11112 . mtctr 12 mtctr 12
11113 . xor 11,12,12 ld 2,xxx+8(2)
11114 . add 2,2,11 cmpldi 2,0
11115 . ld 2,xxx+8(2) bnectr+
11116 . bctr b <glink_entry>
11117
11118 The solution involving the compare turns out to be faster, so
11119 that's what we use unless the branch won't reach. */
11120
11121 #define ALWAYS_USE_FAKE_DEP 0
11122 #define ALWAYS_EMIT_R2SAVE 0
11123
11124 static inline unsigned int
11125 plt_stub_size (struct ppc_link_hash_table *htab,
11126 struct ppc_stub_hash_entry *stub_entry,
11127 bfd_vma off,
11128 unsigned int odd)
11129 {
11130 unsigned size;
11131
11132 if (stub_entry->type.sub == ppc_stub_notoc)
11133 {
11134 size = 8 + size_power10_offset (off, odd);
11135 if (stub_entry->type.r2save)
11136 size += 4;
11137 }
11138 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11139 {
11140 size = 8 + size_offset (off - 8);
11141 if (stub_entry->type.r2save)
11142 size += 4;
11143 }
11144 else
11145 {
11146 size = 12;
11147 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11148 size += 4;
11149 if (PPC_HA (off) != 0)
11150 size += 4;
11151 if (htab->opd_abi)
11152 {
11153 size += 4;
11154 if (htab->params->plt_static_chain)
11155 size += 4;
11156 if (htab->params->plt_thread_safe
11157 && htab->elf.dynamic_sections_created
11158 && stub_entry->h != NULL
11159 && stub_entry->h->elf.dynindx != -1)
11160 size += 8;
11161 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11162 != PPC_HA (off))
11163 size += 4;
11164 }
11165 }
11166 if (stub_entry->h != NULL
11167 && is_tls_get_addr (&stub_entry->h->elf, htab)
11168 && htab->params->tls_get_addr_opt)
11169 {
11170 if (!htab->params->no_tls_get_addr_regsave)
11171 {
11172 size += 30 * 4;
11173 if (stub_entry->type.r2save)
11174 size += 4;
11175 }
11176 else
11177 {
11178 size += 7 * 4;
11179 if (stub_entry->type.r2save)
11180 size += 6 * 4;
11181 }
11182 }
11183 return size;
11184 }
11185
11186 /* Depending on the sign of plt_stub_align:
11187 If positive, return the padding to align to a 2**plt_stub_align
11188 boundary.
11189 If negative, if this stub would cross fewer 2**plt_stub_align
11190 boundaries if we align, then return the padding needed to do so. */
11191
11192 static inline unsigned int
11193 plt_stub_pad (struct ppc_link_hash_table *htab,
11194 struct ppc_stub_hash_entry *stub_entry,
11195 bfd_vma stub_off,
11196 bfd_vma plt_off,
11197 unsigned int odd)
11198 {
11199 int stub_align;
11200 unsigned stub_size;
11201
11202 if (htab->params->plt_stub_align >= 0)
11203 {
11204 stub_align = 1 << htab->params->plt_stub_align;
11205 if ((stub_off & (stub_align - 1)) != 0)
11206 return stub_align - (stub_off & (stub_align - 1));
11207 return 0;
11208 }
11209
11210 stub_align = 1 << -htab->params->plt_stub_align;
11211 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
11212 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11213 > ((stub_size - 1) & -stub_align))
11214 return stub_align - (stub_off & (stub_align - 1));
11215 return 0;
11216 }
11217
11218 /* Build a toc using .plt call stub. */
11219
11220 static inline bfd_byte *
11221 build_plt_stub (struct ppc_link_hash_table *htab,
11222 struct ppc_stub_hash_entry *stub_entry,
11223 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11224 {
11225 bfd *obfd = htab->params->stub_bfd;
11226 bool plt_load_toc = htab->opd_abi;
11227 bool plt_static_chain = htab->params->plt_static_chain;
11228 bool plt_thread_safe = (htab->params->plt_thread_safe
11229 && htab->elf.dynamic_sections_created
11230 && stub_entry->h != NULL
11231 && stub_entry->h->elf.dynindx != -1);
11232 bool use_fake_dep = plt_thread_safe;
11233 bfd_vma cmp_branch_off = 0;
11234
11235 if (!ALWAYS_USE_FAKE_DEP
11236 && plt_load_toc
11237 && plt_thread_safe
11238 && !(stub_entry->h != NULL
11239 && is_tls_get_addr (&stub_entry->h->elf, htab)
11240 && htab->params->tls_get_addr_opt))
11241 {
11242 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11243 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11244 / PLT_ENTRY_SIZE (htab));
11245 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11246 bfd_vma to, from;
11247
11248 if (pltindex > 32768)
11249 glinkoff += (pltindex - 32768) * 4;
11250 to = (glinkoff
11251 + htab->glink->output_offset
11252 + htab->glink->output_section->vma);
11253 from = (p - stub_entry->group->stub_sec->contents
11254 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11255 + 4 * (PPC_HA (offset) != 0)
11256 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11257 != PPC_HA (offset))
11258 + 4 * (plt_static_chain != 0)
11259 + 20
11260 + stub_entry->group->stub_sec->output_offset
11261 + stub_entry->group->stub_sec->output_section->vma);
11262 cmp_branch_off = to - from;
11263 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11264 }
11265
11266 if (PPC_HA (offset) != 0)
11267 {
11268 if (r != NULL)
11269 {
11270 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11271 r[0].r_offset += 4;
11272 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11273 r[1].r_offset = r[0].r_offset + 4;
11274 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11275 r[1].r_addend = r[0].r_addend;
11276 if (plt_load_toc)
11277 {
11278 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11279 {
11280 r[2].r_offset = r[1].r_offset + 4;
11281 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11282 r[2].r_addend = r[0].r_addend;
11283 }
11284 else
11285 {
11286 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11287 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11288 r[2].r_addend = r[0].r_addend + 8;
11289 if (plt_static_chain)
11290 {
11291 r[3].r_offset = r[2].r_offset + 4;
11292 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11293 r[3].r_addend = r[0].r_addend + 16;
11294 }
11295 }
11296 }
11297 }
11298 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11299 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11300 if (plt_load_toc)
11301 {
11302 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11303 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11304 }
11305 else
11306 {
11307 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11308 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11309 }
11310 if (plt_load_toc
11311 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11312 {
11313 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11314 offset = 0;
11315 }
11316 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11317 if (plt_load_toc)
11318 {
11319 if (use_fake_dep)
11320 {
11321 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11322 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11323 }
11324 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11325 if (plt_static_chain)
11326 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11327 }
11328 }
11329 else
11330 {
11331 if (r != NULL)
11332 {
11333 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11334 r[0].r_offset += 4;
11335 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11336 if (plt_load_toc)
11337 {
11338 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11339 {
11340 r[1].r_offset = r[0].r_offset + 4;
11341 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11342 r[1].r_addend = r[0].r_addend;
11343 }
11344 else
11345 {
11346 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11347 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11348 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11349 if (plt_static_chain)
11350 {
11351 r[2].r_offset = r[1].r_offset + 4;
11352 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11353 r[2].r_addend = r[0].r_addend + 8;
11354 }
11355 }
11356 }
11357 }
11358 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11359 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11360 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11361 if (plt_load_toc
11362 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11363 {
11364 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11365 offset = 0;
11366 }
11367 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11368 if (plt_load_toc)
11369 {
11370 if (use_fake_dep)
11371 {
11372 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11373 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11374 }
11375 if (plt_static_chain)
11376 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11377 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11378 }
11379 }
11380 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11381 {
11382 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11383 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11384 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11385 }
11386 else
11387 bfd_put_32 (obfd, BCTR, p), p += 4;
11388 return p;
11389 }
11390
11391 /* Build a special .plt call stub for __tls_get_addr. */
11392
11393 #define LD_R0_0R3 0xe8030000
11394 #define LD_R12_0R3 0xe9830000
11395 #define MR_R0_R3 0x7c601b78
11396 #define CMPDI_R0_0 0x2c200000
11397 #define ADD_R3_R12_R13 0x7c6c6a14
11398 #define BEQLR 0x4d820020
11399 #define MR_R3_R0 0x7c030378
11400 #define BCTRL 0x4e800421
11401
11402 static bfd_byte *
11403 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11404 struct ppc_stub_hash_entry *stub_entry,
11405 bfd_byte *p)
11406 {
11407 bfd *obfd = htab->params->stub_bfd;
11408
11409 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11410 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11411 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11412 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11413 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11414 bfd_put_32 (obfd, BEQLR, p), p += 4;
11415 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11416
11417 if (!htab->params->no_tls_get_addr_regsave)
11418 p = tls_get_addr_prologue (obfd, p, htab);
11419 else if (stub_entry->type.r2save)
11420 {
11421 bfd_put_32 (obfd, MFLR_R0, p);
11422 p += 4;
11423 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11424 p += 4;
11425 }
11426 return p;
11427 }
11428
11429 static bfd_byte *
11430 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11431 struct ppc_stub_hash_entry *stub_entry,
11432 bfd_byte *p,
11433 bfd_byte *loc)
11434 {
11435 bfd *obfd = htab->params->stub_bfd;
11436
11437 if (!htab->params->no_tls_get_addr_regsave)
11438 {
11439 bfd_put_32 (obfd, BCTRL, p - 4);
11440
11441 if (stub_entry->type.r2save)
11442 {
11443 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11444 p += 4;
11445 }
11446 p = tls_get_addr_epilogue (obfd, p, htab);
11447 }
11448 else if (stub_entry->type.r2save)
11449 {
11450 bfd_put_32 (obfd, BCTRL, p - 4);
11451
11452 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11453 p += 4;
11454 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11455 p += 4;
11456 bfd_put_32 (obfd, MTLR_R0, p);
11457 p += 4;
11458 bfd_put_32 (obfd, BLR, p);
11459 p += 4;
11460 }
11461
11462 if (htab->glink_eh_frame != NULL
11463 && htab->glink_eh_frame->size != 0)
11464 {
11465 bfd_byte *base, *eh;
11466
11467 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11468 eh = base + stub_entry->group->eh_size;
11469
11470 if (!htab->params->no_tls_get_addr_regsave)
11471 {
11472 unsigned int cfa_updt, delta, i;
11473
11474 /* After the bctrl, lr has been modified so we need to emit
11475 .eh_frame info saying the return address is on the stack. In
11476 fact we must put the EH info at or before the call rather
11477 than after it, because the EH info for a call needs to be
11478 specified by that point.
11479 See libgcc/unwind-dw2.c execute_cfa_program.
11480 Any stack pointer update must be described immediately after
11481 the instruction making the change, and since the stdu occurs
11482 after saving regs we put all the reg saves and the cfa
11483 change there. */
11484 cfa_updt = stub_entry->stub_offset + 18 * 4;
11485 delta = cfa_updt - stub_entry->group->lr_restore;
11486 stub_entry->group->lr_restore
11487 = stub_entry->stub_offset + (p - loc) - 4;
11488 eh = eh_advance (htab->elf.dynobj, eh, delta);
11489 *eh++ = DW_CFA_def_cfa_offset;
11490 if (htab->opd_abi)
11491 {
11492 *eh++ = 128;
11493 *eh++ = 1;
11494 }
11495 else
11496 *eh++ = 96;
11497 *eh++ = DW_CFA_offset_extended_sf;
11498 *eh++ = 65;
11499 *eh++ = (-16 / 8) & 0x7f;
11500 for (i = 4; i < 12; i++)
11501 {
11502 *eh++ = DW_CFA_offset + i;
11503 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11504 }
11505 *eh++ = (DW_CFA_advance_loc
11506 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11507 *eh++ = DW_CFA_def_cfa_offset;
11508 *eh++ = 0;
11509 for (i = 4; i < 12; i++)
11510 *eh++ = DW_CFA_restore + i;
11511 *eh++ = DW_CFA_advance_loc + 2;
11512 *eh++ = DW_CFA_restore_extended;
11513 *eh++ = 65;
11514 stub_entry->group->eh_size = eh - base;
11515 }
11516 else if (stub_entry->type.r2save)
11517 {
11518 unsigned int lr_used, delta;
11519
11520 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11521 delta = lr_used - stub_entry->group->lr_restore;
11522 stub_entry->group->lr_restore = lr_used + 16;
11523 eh = eh_advance (htab->elf.dynobj, eh, delta);
11524 *eh++ = DW_CFA_offset_extended_sf;
11525 *eh++ = 65;
11526 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11527 *eh++ = DW_CFA_advance_loc + 4;
11528 *eh++ = DW_CFA_restore_extended;
11529 *eh++ = 65;
11530 stub_entry->group->eh_size = eh - base;
11531 }
11532 }
11533 return p;
11534 }
11535
11536 static Elf_Internal_Rela *
11537 get_relocs (asection *sec, int count)
11538 {
11539 Elf_Internal_Rela *relocs;
11540 struct bfd_elf_section_data *elfsec_data;
11541
11542 elfsec_data = elf_section_data (sec);
11543 relocs = elfsec_data->relocs;
11544 if (relocs == NULL)
11545 {
11546 bfd_size_type relsize;
11547 relsize = sec->reloc_count * sizeof (*relocs);
11548 relocs = bfd_alloc (sec->owner, relsize);
11549 if (relocs == NULL)
11550 return NULL;
11551 elfsec_data->relocs = relocs;
11552 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11553 sizeof (Elf_Internal_Shdr));
11554 if (elfsec_data->rela.hdr == NULL)
11555 return NULL;
11556 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11557 * sizeof (Elf64_External_Rela));
11558 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11559 sec->reloc_count = 0;
11560 }
11561 relocs += sec->reloc_count;
11562 sec->reloc_count += count;
11563 return relocs;
11564 }
11565
11566 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11567 forms, to the equivalent relocs against the global symbol given by
11568 STUB_ENTRY->H. */
11569
11570 static bool
11571 use_global_in_relocs (struct ppc_link_hash_table *htab,
11572 struct ppc_stub_hash_entry *stub_entry,
11573 Elf_Internal_Rela *r, unsigned int num_rel)
11574 {
11575 struct elf_link_hash_entry **hashes;
11576 unsigned long symndx;
11577 struct ppc_link_hash_entry *h;
11578 bfd_vma symval;
11579
11580 /* Relocs are always against symbols in their own object file. Fake
11581 up global sym hashes for the stub bfd (which has no symbols). */
11582 hashes = elf_sym_hashes (htab->params->stub_bfd);
11583 if (hashes == NULL)
11584 {
11585 bfd_size_type hsize;
11586
11587 /* When called the first time, stub_globals will contain the
11588 total number of symbols seen during stub sizing. After
11589 allocating, stub_globals is used as an index to fill the
11590 hashes array. */
11591 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11592 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11593 if (hashes == NULL)
11594 return false;
11595 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11596 htab->stub_globals = 1;
11597 }
11598 symndx = htab->stub_globals++;
11599 h = stub_entry->h;
11600 hashes[symndx] = &h->elf;
11601 if (h->oh != NULL && h->oh->is_func)
11602 h = ppc_follow_link (h->oh);
11603 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11604 || h->elf.root.type == bfd_link_hash_defweak);
11605 symval = defined_sym_val (&h->elf);
11606 while (num_rel-- != 0)
11607 {
11608 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11609 if (h->elf.root.u.def.section != stub_entry->target_section)
11610 {
11611 /* H is an opd symbol. The addend must be zero, and the
11612 branch reloc is the only one we can convert. */
11613 r->r_addend = 0;
11614 break;
11615 }
11616 else
11617 r->r_addend -= symval;
11618 --r;
11619 }
11620 return true;
11621 }
11622
11623 static bfd_vma
11624 get_r2off (struct bfd_link_info *info,
11625 struct ppc_stub_hash_entry *stub_entry)
11626 {
11627 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11628 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11629
11630 if (r2off == 0)
11631 {
11632 /* Support linking -R objects. Get the toc pointer from the
11633 opd entry. */
11634 char buf[8];
11635 if (!htab->opd_abi)
11636 return r2off;
11637 asection *opd = stub_entry->h->elf.root.u.def.section;
11638 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11639
11640 if (strcmp (opd->name, ".opd") != 0
11641 || opd->reloc_count != 0)
11642 {
11643 info->callbacks->einfo
11644 (_("%P: cannot find opd entry toc for `%pT'\n"),
11645 stub_entry->h->elf.root.root.string);
11646 bfd_set_error (bfd_error_bad_value);
11647 return (bfd_vma) -1;
11648 }
11649 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11650 return (bfd_vma) -1;
11651 r2off = bfd_get_64 (opd->owner, buf);
11652 r2off -= elf_gp (info->output_bfd);
11653 }
11654 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11655 return r2off;
11656 }
11657
11658 static bool
11659 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11660 {
11661 struct ppc_stub_hash_entry *stub_entry;
11662 struct ppc_branch_hash_entry *br_entry;
11663 struct bfd_link_info *info;
11664 struct ppc_link_hash_table *htab;
11665 bfd *obfd;
11666 bfd_byte *loc;
11667 bfd_byte *p, *relp;
11668 bfd_vma targ, off;
11669 Elf_Internal_Rela *r;
11670 asection *plt;
11671 int num_rel;
11672 int odd;
11673 bool is_tga;
11674
11675 /* Massage our args to the form they really have. */
11676 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11677 info = in_arg;
11678
11679 /* Fail if the target section could not be assigned to an output
11680 section. The user should fix his linker script. */
11681 if (stub_entry->target_section != NULL
11682 && stub_entry->target_section->output_section == NULL
11683 && info->non_contiguous_regions)
11684 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11685 "Retry without --enable-non-contiguous-regions.\n"),
11686 stub_entry->target_section);
11687
11688 /* Same for the group. */
11689 if (stub_entry->group->stub_sec != NULL
11690 && stub_entry->group->stub_sec->output_section == NULL
11691 && info->non_contiguous_regions)
11692 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11693 "output section. Retry without "
11694 "--enable-non-contiguous-regions.\n"),
11695 stub_entry->group->stub_sec,
11696 stub_entry->target_section);
11697
11698 htab = ppc_hash_table (info);
11699 if (htab == NULL)
11700 return false;
11701
11702 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11703 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11704
11705 htab->stub_count[stub_entry->type.main - 1] += 1;
11706 if (stub_entry->type.main == ppc_stub_long_branch
11707 && stub_entry->type.sub == ppc_stub_toc)
11708 {
11709 /* Branches are relative. This is where we are going to. */
11710 targ = (stub_entry->target_value
11711 + stub_entry->target_section->output_offset
11712 + stub_entry->target_section->output_section->vma);
11713 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11714
11715 /* And this is where we are coming from. */
11716 off = (stub_entry->stub_offset
11717 + stub_entry->group->stub_sec->output_offset
11718 + stub_entry->group->stub_sec->output_section->vma);
11719 off = targ - off;
11720
11721 p = loc;
11722 obfd = htab->params->stub_bfd;
11723 if (stub_entry->type.r2save)
11724 {
11725 bfd_vma r2off = get_r2off (info, stub_entry);
11726
11727 if (r2off == (bfd_vma) -1)
11728 {
11729 htab->stub_error = true;
11730 return false;
11731 }
11732 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11733 p += 4;
11734 if (PPC_HA (r2off) != 0)
11735 {
11736 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11737 p += 4;
11738 }
11739 if (PPC_LO (r2off) != 0)
11740 {
11741 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11742 p += 4;
11743 }
11744 off -= p - loc;
11745 }
11746 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11747 p += 4;
11748
11749 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11750 {
11751 _bfd_error_handler
11752 (_("long branch stub `%s' offset overflow"),
11753 stub_entry->root.string);
11754 htab->stub_error = true;
11755 return false;
11756 }
11757
11758 if (info->emitrelocations)
11759 {
11760 r = get_relocs (stub_entry->group->stub_sec, 1);
11761 if (r == NULL)
11762 return false;
11763 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11764 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11765 r->r_addend = targ;
11766 if (stub_entry->h != NULL
11767 && !use_global_in_relocs (htab, stub_entry, r, 1))
11768 return false;
11769 }
11770 }
11771 else if (stub_entry->type.main == ppc_stub_plt_branch
11772 && stub_entry->type.sub == ppc_stub_toc)
11773 {
11774 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11775 stub_entry->root.string + 9,
11776 false, false);
11777 if (br_entry == NULL)
11778 {
11779 _bfd_error_handler (_("can't find branch stub `%s'"),
11780 stub_entry->root.string);
11781 htab->stub_error = true;
11782 return false;
11783 }
11784
11785 targ = (stub_entry->target_value
11786 + stub_entry->target_section->output_offset
11787 + stub_entry->target_section->output_section->vma);
11788 if (!stub_entry->type.r2save)
11789 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11790
11791 bfd_put_64 (htab->brlt->owner, targ,
11792 htab->brlt->contents + br_entry->offset);
11793
11794 if (br_entry->iter == htab->stub_iteration)
11795 {
11796 br_entry->iter = 0;
11797
11798 if (htab->relbrlt != NULL && !info->enable_dt_relr)
11799 {
11800 /* Create a reloc for the branch lookup table entry. */
11801 Elf_Internal_Rela rela;
11802 bfd_byte *rl;
11803
11804 rela.r_offset = (br_entry->offset
11805 + htab->brlt->output_offset
11806 + htab->brlt->output_section->vma);
11807 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11808 rela.r_addend = targ;
11809
11810 rl = htab->relbrlt->contents;
11811 rl += (htab->relbrlt->reloc_count++
11812 * sizeof (Elf64_External_Rela));
11813 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11814 }
11815 else if (info->emitrelocations)
11816 {
11817 r = get_relocs (htab->brlt, 1);
11818 if (r == NULL)
11819 return false;
11820 /* brlt, being SEC_LINKER_CREATED does not go through the
11821 normal reloc processing. Symbols and offsets are not
11822 translated from input file to output file form, so
11823 set up the offset per the output file. */
11824 r->r_offset = (br_entry->offset
11825 + htab->brlt->output_offset
11826 + htab->brlt->output_section->vma);
11827 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11828 r->r_addend = targ;
11829 }
11830 }
11831
11832 targ = (br_entry->offset
11833 + htab->brlt->output_offset
11834 + htab->brlt->output_section->vma);
11835
11836 off = (elf_gp (info->output_bfd)
11837 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11838 off = targ - off;
11839
11840 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11841 {
11842 info->callbacks->einfo
11843 (_("%P: linkage table error against `%pT'\n"),
11844 stub_entry->root.string);
11845 bfd_set_error (bfd_error_bad_value);
11846 htab->stub_error = true;
11847 return false;
11848 }
11849
11850 if (info->emitrelocations)
11851 {
11852 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11853 if (r == NULL)
11854 return false;
11855 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11856 if (bfd_big_endian (info->output_bfd))
11857 r[0].r_offset += 2;
11858 if (stub_entry->type.r2save)
11859 r[0].r_offset += 4;
11860 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11861 r[0].r_addend = targ;
11862 if (PPC_HA (off) != 0)
11863 {
11864 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11865 r[1].r_offset = r[0].r_offset + 4;
11866 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11867 r[1].r_addend = r[0].r_addend;
11868 }
11869 }
11870
11871 p = loc;
11872 obfd = htab->params->stub_bfd;
11873 if (!stub_entry->type.r2save)
11874 {
11875 if (PPC_HA (off) != 0)
11876 {
11877 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11878 p += 4;
11879 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11880 }
11881 else
11882 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11883 }
11884 else
11885 {
11886 bfd_vma r2off = get_r2off (info, stub_entry);
11887
11888 if (r2off == (bfd_vma) -1)
11889 {
11890 htab->stub_error = true;
11891 return false;
11892 }
11893
11894 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11895 p += 4;
11896 if (PPC_HA (off) != 0)
11897 {
11898 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11899 p += 4;
11900 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11901 }
11902 else
11903 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11904
11905 if (PPC_HA (r2off) != 0)
11906 {
11907 p += 4;
11908 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11909 }
11910 if (PPC_LO (r2off) != 0)
11911 {
11912 p += 4;
11913 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11914 }
11915 }
11916 p += 4;
11917 bfd_put_32 (obfd, MTCTR_R12, p);
11918 p += 4;
11919 bfd_put_32 (obfd, BCTR, p);
11920 p += 4;
11921 }
11922 else if (stub_entry->type.sub >= ppc_stub_notoc)
11923 {
11924 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
11925 p = loc;
11926 off = (stub_entry->stub_offset
11927 + stub_entry->group->stub_sec->output_offset
11928 + stub_entry->group->stub_sec->output_section->vma);
11929 obfd = htab->params->stub_bfd;
11930 is_tga = (is_plt
11931 && stub_entry->h != NULL
11932 && is_tls_get_addr (&stub_entry->h->elf, htab)
11933 && htab->params->tls_get_addr_opt);
11934 if (is_tga)
11935 {
11936 p = build_tls_get_addr_head (htab, stub_entry, p);
11937 off += p - loc;
11938 }
11939 if (stub_entry->type.r2save)
11940 {
11941 off += 4;
11942 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11943 p += 4;
11944 }
11945 if (is_plt)
11946 {
11947 targ = stub_entry->plt_ent->plt.offset & ~1;
11948 if (targ >= (bfd_vma) -2)
11949 abort ();
11950
11951 plt = htab->elf.splt;
11952 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11953 {
11954 if (stub_entry->symtype == STT_GNU_IFUNC)
11955 plt = htab->elf.iplt;
11956 else
11957 plt = htab->pltlocal;
11958 }
11959 targ += plt->output_offset + plt->output_section->vma;
11960 }
11961 else
11962 targ = (stub_entry->target_value
11963 + stub_entry->target_section->output_offset
11964 + stub_entry->target_section->output_section->vma);
11965 odd = off & 4;
11966 off = targ - off;
11967
11968 relp = p;
11969 num_rel = 0;
11970 if (stub_entry->type.sub == ppc_stub_notoc)
11971 p = build_power10_offset (obfd, p, off, odd, is_plt);
11972 else
11973 {
11974 if (htab->glink_eh_frame != NULL
11975 && htab->glink_eh_frame->size != 0)
11976 {
11977 bfd_byte *base, *eh;
11978 unsigned int lr_used, delta;
11979
11980 base = (htab->glink_eh_frame->contents
11981 + stub_entry->group->eh_base + 17);
11982 eh = base + stub_entry->group->eh_size;
11983 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11984 delta = lr_used - stub_entry->group->lr_restore;
11985 stub_entry->group->lr_restore = lr_used + 8;
11986 eh = eh_advance (htab->elf.dynobj, eh, delta);
11987 *eh++ = DW_CFA_register;
11988 *eh++ = 65;
11989 *eh++ = 12;
11990 *eh++ = DW_CFA_advance_loc + 2;
11991 *eh++ = DW_CFA_restore_extended;
11992 *eh++ = 65;
11993 stub_entry->group->eh_size = eh - base;
11994 }
11995
11996 /* The notoc stubs calculate their target (either a PLT entry or
11997 the global entry point of a function) relative to the PC
11998 returned by the "bcl" two instructions past the start of the
11999 sequence emitted by build_offset. The offset is therefore 8
12000 less than calculated from the start of the sequence. */
12001 off -= 8;
12002 p = build_offset (obfd, p, off, is_plt);
12003 }
12004
12005 if (stub_entry->type.main == ppc_stub_long_branch)
12006 {
12007 bfd_vma from;
12008 num_rel = 1;
12009 from = (stub_entry->stub_offset
12010 + stub_entry->group->stub_sec->output_offset
12011 + stub_entry->group->stub_sec->output_section->vma
12012 + (p - loc));
12013 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
12014 }
12015 else
12016 {
12017 bfd_put_32 (obfd, MTCTR_R12, p);
12018 p += 4;
12019 bfd_put_32 (obfd, BCTR, p);
12020 }
12021 p += 4;
12022
12023 if (is_tga)
12024 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12025
12026 if (info->emitrelocations)
12027 {
12028 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
12029 if (stub_entry->type.sub == ppc_stub_notoc)
12030 num_rel += num_relocs_for_power10_offset (off, odd);
12031 else
12032 {
12033 num_rel += num_relocs_for_offset (off);
12034 roff += 16;
12035 }
12036 r = get_relocs (stub_entry->group->stub_sec, num_rel);
12037 if (r == NULL)
12038 return false;
12039 if (stub_entry->type.sub == ppc_stub_notoc)
12040 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
12041 else
12042 r = emit_relocs_for_offset (info, r, roff, targ, off);
12043 if (stub_entry->type.main == ppc_stub_long_branch)
12044 {
12045 ++r;
12046 roff = p - 4 - stub_entry->group->stub_sec->contents;
12047 r->r_offset = roff;
12048 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
12049 r->r_addend = targ;
12050 if (stub_entry->h != NULL
12051 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
12052 return false;
12053 }
12054 }
12055 }
12056 else if (stub_entry->type.main == ppc_stub_plt_call)
12057 {
12058 if (stub_entry->h != NULL
12059 && stub_entry->h->is_func_descriptor
12060 && stub_entry->h->oh != NULL)
12061 {
12062 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
12063
12064 /* If the old-ABI "dot-symbol" is undefined make it weak so
12065 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
12066 if (fh->elf.root.type == bfd_link_hash_undefined
12067 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
12068 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
12069 fh->elf.root.type = bfd_link_hash_undefweak;
12070 }
12071
12072 /* Now build the stub. */
12073 targ = stub_entry->plt_ent->plt.offset & ~1;
12074 if (targ >= (bfd_vma) -2)
12075 abort ();
12076
12077 plt = htab->elf.splt;
12078 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12079 {
12080 if (stub_entry->symtype == STT_GNU_IFUNC)
12081 plt = htab->elf.iplt;
12082 else
12083 plt = htab->pltlocal;
12084 }
12085 targ += plt->output_offset + plt->output_section->vma;
12086
12087 off = (elf_gp (info->output_bfd)
12088 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12089 off = targ - off;
12090
12091 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
12092 {
12093 info->callbacks->einfo
12094 /* xgettext:c-format */
12095 (_("%P: linkage table error against `%pT'\n"),
12096 stub_entry->h != NULL
12097 ? stub_entry->h->elf.root.root.string
12098 : "<local sym>");
12099 bfd_set_error (bfd_error_bad_value);
12100 htab->stub_error = true;
12101 return false;
12102 }
12103
12104 r = NULL;
12105 if (info->emitrelocations)
12106 {
12107 r = get_relocs (stub_entry->group->stub_sec,
12108 ((PPC_HA (off) != 0)
12109 + (htab->opd_abi
12110 ? 2 + (htab->params->plt_static_chain
12111 && PPC_HA (off + 16) == PPC_HA (off))
12112 : 1)));
12113 if (r == NULL)
12114 return false;
12115 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12116 if (bfd_big_endian (info->output_bfd))
12117 r[0].r_offset += 2;
12118 r[0].r_addend = targ;
12119 }
12120 p = loc;
12121 obfd = htab->params->stub_bfd;
12122 is_tga = (stub_entry->h != NULL
12123 && is_tls_get_addr (&stub_entry->h->elf, htab)
12124 && htab->params->tls_get_addr_opt);
12125 if (is_tga)
12126 {
12127 p = build_tls_get_addr_head (htab, stub_entry, p);
12128 if (r != NULL)
12129 r[0].r_offset += p - loc;
12130 }
12131 p = build_plt_stub (htab, stub_entry, p, off, r);
12132 if (is_tga)
12133 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12134 }
12135 else if (stub_entry->type.main == ppc_stub_save_res)
12136 return true;
12137 else
12138 {
12139 BFD_FAIL ();
12140 return false;
12141 }
12142
12143 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12144
12145 if (htab->params->emit_stub_syms)
12146 {
12147 struct elf_link_hash_entry *h;
12148 size_t len1, len2;
12149 char *name;
12150 const char *const stub_str[] = { "long_branch",
12151 "plt_branch",
12152 "plt_call" };
12153
12154 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12155 len2 = strlen (stub_entry->root.string);
12156 name = bfd_malloc (len1 + len2 + 2);
12157 if (name == NULL)
12158 return false;
12159 memcpy (name, stub_entry->root.string, 9);
12160 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12161 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12162 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12163 if (h == NULL)
12164 return false;
12165 if (h->root.type == bfd_link_hash_new)
12166 {
12167 h->root.type = bfd_link_hash_defined;
12168 h->root.u.def.section = stub_entry->group->stub_sec;
12169 h->root.u.def.value = stub_entry->stub_offset;
12170 h->ref_regular = 1;
12171 h->def_regular = 1;
12172 h->ref_regular_nonweak = 1;
12173 h->forced_local = 1;
12174 h->non_elf = 0;
12175 h->root.linker_def = 1;
12176 }
12177 }
12178
12179 return true;
12180 }
12181
12182 /* As above, but don't actually build the stub. Just bump offset so
12183 we know stub section sizes, and select plt_branch stubs where
12184 long_branch stubs won't do. */
12185
12186 static bool
12187 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12188 {
12189 struct ppc_stub_hash_entry *stub_entry;
12190 struct bfd_link_info *info;
12191 struct ppc_link_hash_table *htab;
12192 asection *plt;
12193 bfd_vma targ, off, r2off;
12194 unsigned int size, extra, lr_used, delta, odd;
12195 bfd_vma stub_offset;
12196
12197 /* Massage our args to the form they really have. */
12198 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12199 info = in_arg;
12200
12201 htab = ppc_hash_table (info);
12202 if (htab == NULL)
12203 return false;
12204
12205 /* Fail if the target section could not be assigned to an output
12206 section. The user should fix his linker script. */
12207 if (stub_entry->target_section != NULL
12208 && stub_entry->target_section->output_section == NULL
12209 && info->non_contiguous_regions)
12210 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12211 "Retry without --enable-non-contiguous-regions.\n"),
12212 stub_entry->target_section);
12213
12214 /* Same for the group. */
12215 if (stub_entry->group->stub_sec != NULL
12216 && stub_entry->group->stub_sec->output_section == NULL
12217 && info->non_contiguous_regions)
12218 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12219 "output section. Retry without "
12220 "--enable-non-contiguous-regions.\n"),
12221 stub_entry->group->stub_sec,
12222 stub_entry->target_section);
12223
12224 /* Make a note of the offset within the stubs for this entry. */
12225 stub_offset = stub_entry->group->stub_sec->size;
12226 if (htab->stub_iteration > STUB_SHRINK_ITER
12227 && stub_entry->stub_offset > stub_offset)
12228 stub_offset = stub_entry->stub_offset;
12229
12230 if (stub_entry->h != NULL
12231 && stub_entry->h->save_res
12232 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12233 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12234 {
12235 /* Don't make stubs to out-of-line register save/restore
12236 functions. Instead, emit copies of the functions. */
12237 stub_entry->group->needs_save_res = 1;
12238 stub_entry->type.main = ppc_stub_save_res;
12239 stub_entry->type.sub = ppc_stub_toc;
12240 stub_entry->type.r2save = 0;
12241 return true;
12242 }
12243
12244 if (stub_entry->type.main == ppc_stub_plt_branch)
12245 {
12246 /* Reset the stub type from the plt branch variant in case we now
12247 can reach with a shorter stub. */
12248 stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
12249 }
12250
12251 if (stub_entry->type.main == ppc_stub_long_branch
12252 && stub_entry->type.sub == ppc_stub_toc)
12253 {
12254 targ = (stub_entry->target_value
12255 + stub_entry->target_section->output_offset
12256 + stub_entry->target_section->output_section->vma);
12257 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12258 off = (stub_offset
12259 + stub_entry->group->stub_sec->output_offset
12260 + stub_entry->group->stub_sec->output_section->vma);
12261
12262 size = 4;
12263 r2off = 0;
12264 if (stub_entry->type.r2save)
12265 {
12266 r2off = get_r2off (info, stub_entry);
12267 if (r2off == (bfd_vma) -1)
12268 {
12269 htab->stub_error = true;
12270 return false;
12271 }
12272 size = 8;
12273 if (PPC_HA (r2off) != 0)
12274 size += 4;
12275 if (PPC_LO (r2off) != 0)
12276 size += 4;
12277 off += size - 4;
12278 }
12279 off = targ - off;
12280
12281 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12282 Do the same for -R objects without function descriptors. */
12283 if ((stub_entry->type.r2save
12284 && r2off == 0
12285 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12286 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12287 {
12288 struct ppc_branch_hash_entry *br_entry;
12289
12290 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12291 stub_entry->root.string + 9,
12292 true, false);
12293 if (br_entry == NULL)
12294 {
12295 _bfd_error_handler (_("can't build branch stub `%s'"),
12296 stub_entry->root.string);
12297 htab->stub_error = true;
12298 return false;
12299 }
12300
12301 if (br_entry->iter != htab->stub_iteration)
12302 {
12303 br_entry->iter = htab->stub_iteration;
12304 br_entry->offset = htab->brlt->size;
12305 htab->brlt->size += 8;
12306
12307 if (htab->relbrlt != NULL && !info->enable_dt_relr)
12308 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12309 else if (info->emitrelocations)
12310 {
12311 htab->brlt->reloc_count += 1;
12312 htab->brlt->flags |= SEC_RELOC;
12313 }
12314 }
12315
12316 targ = (br_entry->offset
12317 + htab->brlt->output_offset
12318 + htab->brlt->output_section->vma);
12319 off = (elf_gp (info->output_bfd)
12320 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12321 off = targ - off;
12322
12323 if (info->emitrelocations)
12324 {
12325 stub_entry->group->stub_sec->reloc_count
12326 += 1 + (PPC_HA (off) != 0);
12327 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12328 }
12329
12330 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12331 if (!stub_entry->type.r2save)
12332 {
12333 size = 12;
12334 if (PPC_HA (off) != 0)
12335 size = 16;
12336 }
12337 else
12338 {
12339 size = 16;
12340 if (PPC_HA (off) != 0)
12341 size += 4;
12342
12343 if (PPC_HA (r2off) != 0)
12344 size += 4;
12345 if (PPC_LO (r2off) != 0)
12346 size += 4;
12347 }
12348 }
12349 else if (info->emitrelocations)
12350 {
12351 stub_entry->group->stub_sec->reloc_count += 1;
12352 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12353 }
12354 }
12355 else if (stub_entry->type.main == ppc_stub_long_branch)
12356 {
12357 off = (stub_offset
12358 + stub_entry->group->stub_sec->output_offset
12359 + stub_entry->group->stub_sec->output_section->vma);
12360 size = 0;
12361 if (stub_entry->type.r2save)
12362 size = 4;
12363 off += size;
12364 targ = (stub_entry->target_value
12365 + stub_entry->target_section->output_offset
12366 + stub_entry->target_section->output_section->vma);
12367 odd = off & 4;
12368 off = targ - off;
12369
12370 if (info->emitrelocations)
12371 {
12372 unsigned int num_rel;
12373 if (stub_entry->type.sub == ppc_stub_notoc)
12374 num_rel = num_relocs_for_power10_offset (off, odd);
12375 else
12376 num_rel = num_relocs_for_offset (off - 8);
12377 stub_entry->group->stub_sec->reloc_count += num_rel;
12378 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12379 }
12380
12381 if (stub_entry->type.sub == ppc_stub_notoc)
12382 extra = size_power10_offset (off, odd);
12383 else
12384 extra = size_offset (off - 8);
12385 /* Include branch insn plus those in the offset sequence. */
12386 size += 4 + extra;
12387 /* The branch insn is at the end, or "extra" bytes along. So
12388 its offset will be "extra" bytes less that that already
12389 calculated. */
12390 off -= extra;
12391
12392 if (stub_entry->type.sub != ppc_stub_notoc)
12393 {
12394 /* After the bcl, lr has been modified so we need to emit
12395 .eh_frame info saying the return address is in r12. */
12396 lr_used = stub_offset + 8;
12397 if (stub_entry->type.r2save)
12398 lr_used += 4;
12399 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12400 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12401 DW_CFA_restore_extended 65. */
12402 delta = lr_used - stub_entry->group->lr_restore;
12403 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12404 stub_entry->group->lr_restore = lr_used + 8;
12405 }
12406
12407 /* If the branch can't reach, use a plt_branch. */
12408 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12409 {
12410 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12411 size += 4;
12412 }
12413 else if (info->emitrelocations)
12414 stub_entry->group->stub_sec->reloc_count +=1;
12415 }
12416 else if (stub_entry->type.sub >= ppc_stub_notoc)
12417 {
12418 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12419 lr_used = 0;
12420 if (stub_entry->h != NULL
12421 && is_tls_get_addr (&stub_entry->h->elf, htab)
12422 && htab->params->tls_get_addr_opt)
12423 {
12424 lr_used += 7 * 4;
12425 if (!htab->params->no_tls_get_addr_regsave)
12426 lr_used += 11 * 4;
12427 else if (stub_entry->type.r2save)
12428 lr_used += 2 * 4;
12429 }
12430 if (stub_entry->type.r2save)
12431 lr_used += 4;
12432 targ = stub_entry->plt_ent->plt.offset & ~1;
12433 if (targ >= (bfd_vma) -2)
12434 abort ();
12435
12436 plt = htab->elf.splt;
12437 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12438 {
12439 if (stub_entry->symtype == STT_GNU_IFUNC)
12440 plt = htab->elf.iplt;
12441 else
12442 plt = htab->pltlocal;
12443 }
12444 targ += plt->output_offset + plt->output_section->vma;
12445 off = (stub_offset
12446 + stub_entry->group->stub_sec->output_offset
12447 + stub_entry->group->stub_sec->output_section->vma
12448 + lr_used);
12449 odd = off & 4;
12450 off = targ - off;
12451
12452 if (htab->params->plt_stub_align != 0)
12453 {
12454 unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, odd);
12455
12456 stub_offset += pad;
12457 off -= pad;
12458 odd ^= pad & 4;
12459 }
12460
12461 if (info->emitrelocations)
12462 {
12463 unsigned int num_rel;
12464 if (stub_entry->type.sub == ppc_stub_notoc)
12465 num_rel = num_relocs_for_power10_offset (off, odd);
12466 else
12467 num_rel = num_relocs_for_offset (off - 8);
12468 stub_entry->group->stub_sec->reloc_count += num_rel;
12469 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12470 }
12471
12472 size = plt_stub_size (htab, stub_entry, off, odd);
12473
12474 if (stub_entry->type.sub != ppc_stub_notoc)
12475 {
12476 /* After the bcl, lr has been modified so we need to emit
12477 .eh_frame info saying the return address is in r12. */
12478 lr_used += stub_offset + 8;
12479 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12480 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12481 DW_CFA_restore_extended 65. */
12482 delta = lr_used - stub_entry->group->lr_restore;
12483 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12484 stub_entry->group->lr_restore = lr_used + 8;
12485 }
12486 if (stub_entry->h != NULL
12487 && is_tls_get_addr (&stub_entry->h->elf, htab)
12488 && htab->params->tls_get_addr_opt)
12489 {
12490 if (!htab->params->no_tls_get_addr_regsave)
12491 {
12492 unsigned int cfa_updt = stub_offset + 18 * 4;
12493 delta = cfa_updt - stub_entry->group->lr_restore;
12494 stub_entry->group->eh_size += eh_advance_size (delta);
12495 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12496 stub_entry->group->lr_restore = stub_offset + size - 4;
12497 }
12498 else if (stub_entry->type.r2save)
12499 {
12500 lr_used = stub_offset + size - 20;
12501 delta = lr_used - stub_entry->group->lr_restore;
12502 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12503 stub_entry->group->lr_restore = stub_offset + size - 4;
12504 }
12505 }
12506 }
12507 else if (stub_entry->type.main == ppc_stub_plt_call)
12508 {
12509 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12510 if (targ >= (bfd_vma) -2)
12511 abort ();
12512 plt = htab->elf.splt;
12513 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12514 {
12515 if (stub_entry->symtype == STT_GNU_IFUNC)
12516 plt = htab->elf.iplt;
12517 else
12518 plt = htab->pltlocal;
12519 }
12520 targ += plt->output_offset + plt->output_section->vma;
12521
12522 off = (elf_gp (info->output_bfd)
12523 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12524 off = targ - off;
12525
12526 if (htab->params->plt_stub_align != 0)
12527 {
12528 unsigned pad = plt_stub_pad (htab, stub_entry, stub_offset, off, 0);
12529
12530 stub_offset += pad;
12531 }
12532
12533 if (info->emitrelocations)
12534 {
12535 stub_entry->group->stub_sec->reloc_count
12536 += ((PPC_HA (off) != 0)
12537 + (htab->opd_abi
12538 ? 2 + (htab->params->plt_static_chain
12539 && PPC_HA (off + 16) == PPC_HA (off))
12540 : 1));
12541 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12542 }
12543
12544 size = plt_stub_size (htab, stub_entry, off, 0);
12545
12546 if (stub_entry->h != NULL
12547 && is_tls_get_addr (&stub_entry->h->elf, htab)
12548 && htab->params->tls_get_addr_opt
12549 && stub_entry->type.r2save)
12550 {
12551 if (!htab->params->no_tls_get_addr_regsave)
12552 {
12553 /* Adjustments to r1 need to be described. */
12554 unsigned int cfa_updt = stub_offset + 18 * 4;
12555 delta = cfa_updt - stub_entry->group->lr_restore;
12556 stub_entry->group->eh_size += eh_advance_size (delta);
12557 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12558 }
12559 else
12560 {
12561 lr_used = stub_offset + size - 20;
12562 /* The eh_frame info will consist of a DW_CFA_advance_loc
12563 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12564 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12565 delta = lr_used - stub_entry->group->lr_restore;
12566 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12567 }
12568 stub_entry->group->lr_restore = stub_offset + size - 4;
12569 }
12570 }
12571 else
12572 {
12573 BFD_FAIL ();
12574 return false;
12575 }
12576
12577 if (stub_entry->stub_offset != stub_offset)
12578 htab->stub_changed = true;
12579 stub_entry->stub_offset = stub_offset;
12580 stub_entry->group->stub_sec->size = stub_offset + size;
12581 return true;
12582 }
12583
12584 /* Set up various things so that we can make a list of input sections
12585 for each output section included in the link. Returns -1 on error,
12586 0 when no stubs will be needed, and 1 on success. */
12587
12588 int
12589 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12590 {
12591 unsigned int id;
12592 size_t amt;
12593 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12594
12595 if (htab == NULL)
12596 return -1;
12597
12598 htab->sec_info_arr_size = _bfd_section_id;
12599 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12600 htab->sec_info = bfd_zmalloc (amt);
12601 if (htab->sec_info == NULL)
12602 return -1;
12603
12604 /* Set toc_off for com, und, abs and ind sections. */
12605 for (id = 0; id < 3; id++)
12606 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12607
12608 return 1;
12609 }
12610
12611 /* Set up for first pass at multitoc partitioning. */
12612
12613 void
12614 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12615 {
12616 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12617
12618 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12619 htab->toc_bfd = NULL;
12620 htab->toc_first_sec = NULL;
12621 }
12622
12623 /* The linker repeatedly calls this function for each TOC input section
12624 and linker generated GOT section. Group input bfds such that the toc
12625 within a group is less than 64k in size. */
12626
12627 bool
12628 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12629 {
12630 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12631 bfd_vma addr, off, limit;
12632
12633 if (htab == NULL)
12634 return false;
12635
12636 if (!htab->second_toc_pass)
12637 {
12638 /* Keep track of the first .toc or .got section for this input bfd. */
12639 bool new_bfd = htab->toc_bfd != isec->owner;
12640
12641 if (new_bfd)
12642 {
12643 htab->toc_bfd = isec->owner;
12644 htab->toc_first_sec = isec;
12645 }
12646
12647 addr = isec->output_offset + isec->output_section->vma;
12648 off = addr - htab->toc_curr;
12649 limit = 0x80008000;
12650 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12651 limit = 0x10000;
12652 if (off + isec->size > limit)
12653 {
12654 addr = (htab->toc_first_sec->output_offset
12655 + htab->toc_first_sec->output_section->vma);
12656 htab->toc_curr = addr;
12657 htab->toc_curr &= -TOC_BASE_ALIGN;
12658 }
12659
12660 /* toc_curr is the base address of this toc group. Set elf_gp
12661 for the input section to be the offset relative to the
12662 output toc base plus 0x8000. Making the input elf_gp an
12663 offset allows us to move the toc as a whole without
12664 recalculating input elf_gp. */
12665 off = htab->toc_curr - elf_gp (info->output_bfd);
12666 off += TOC_BASE_OFF;
12667
12668 /* Die if someone uses a linker script that doesn't keep input
12669 file .toc and .got together. */
12670 if (new_bfd
12671 && elf_gp (isec->owner) != 0
12672 && elf_gp (isec->owner) != off)
12673 return false;
12674
12675 elf_gp (isec->owner) = off;
12676 return true;
12677 }
12678
12679 /* During the second pass toc_first_sec points to the start of
12680 a toc group, and toc_curr is used to track the old elf_gp.
12681 We use toc_bfd to ensure we only look at each bfd once. */
12682 if (htab->toc_bfd == isec->owner)
12683 return true;
12684 htab->toc_bfd = isec->owner;
12685
12686 if (htab->toc_first_sec == NULL
12687 || htab->toc_curr != elf_gp (isec->owner))
12688 {
12689 htab->toc_curr = elf_gp (isec->owner);
12690 htab->toc_first_sec = isec;
12691 }
12692 addr = (htab->toc_first_sec->output_offset
12693 + htab->toc_first_sec->output_section->vma);
12694 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12695 elf_gp (isec->owner) = off;
12696
12697 return true;
12698 }
12699
12700 /* Called via elf_link_hash_traverse to merge GOT entries for global
12701 symbol H. */
12702
12703 static bool
12704 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12705 {
12706 if (h->root.type == bfd_link_hash_indirect)
12707 return true;
12708
12709 merge_got_entries (&h->got.glist);
12710
12711 return true;
12712 }
12713
12714 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12715 symbol H. */
12716
12717 static bool
12718 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12719 {
12720 struct got_entry *gent;
12721
12722 if (h->root.type == bfd_link_hash_indirect)
12723 return true;
12724
12725 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12726 if (!gent->is_indirect)
12727 allocate_got (h, (struct bfd_link_info *) inf, gent);
12728 return true;
12729 }
12730
12731 /* Called on the first multitoc pass after the last call to
12732 ppc64_elf_next_toc_section. This function removes duplicate GOT
12733 entries. */
12734
12735 bool
12736 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12737 {
12738 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12739 struct bfd *ibfd, *ibfd2;
12740 bool done_something;
12741
12742 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12743
12744 if (!htab->do_multi_toc)
12745 return false;
12746
12747 /* Merge global sym got entries within a toc group. */
12748 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12749
12750 /* And tlsld_got. */
12751 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12752 {
12753 struct got_entry *ent, *ent2;
12754
12755 if (!is_ppc64_elf (ibfd))
12756 continue;
12757
12758 ent = ppc64_tlsld_got (ibfd);
12759 if (!ent->is_indirect
12760 && ent->got.offset != (bfd_vma) -1)
12761 {
12762 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12763 {
12764 if (!is_ppc64_elf (ibfd2))
12765 continue;
12766
12767 ent2 = ppc64_tlsld_got (ibfd2);
12768 if (!ent2->is_indirect
12769 && ent2->got.offset != (bfd_vma) -1
12770 && elf_gp (ibfd2) == elf_gp (ibfd))
12771 {
12772 ent2->is_indirect = true;
12773 ent2->got.ent = ent;
12774 }
12775 }
12776 }
12777 }
12778
12779 /* Zap sizes of got sections. */
12780 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12781 htab->elf.irelplt->size -= htab->got_reli_size;
12782 htab->got_reli_size = 0;
12783
12784 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12785 {
12786 asection *got, *relgot;
12787
12788 if (!is_ppc64_elf (ibfd))
12789 continue;
12790
12791 got = ppc64_elf_tdata (ibfd)->got;
12792 if (got != NULL)
12793 {
12794 got->rawsize = got->size;
12795 got->size = 0;
12796 relgot = ppc64_elf_tdata (ibfd)->relgot;
12797 relgot->rawsize = relgot->size;
12798 relgot->size = 0;
12799 }
12800 }
12801
12802 /* Now reallocate the got, local syms first. We don't need to
12803 allocate section contents again since we never increase size. */
12804 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12805 {
12806 struct got_entry **lgot_ents;
12807 struct got_entry **end_lgot_ents;
12808 struct plt_entry **local_plt;
12809 struct plt_entry **end_local_plt;
12810 unsigned char *lgot_masks;
12811 bfd_size_type locsymcount;
12812 Elf_Internal_Shdr *symtab_hdr;
12813 asection *s;
12814 Elf_Internal_Sym *local_syms;
12815 Elf_Internal_Sym *isym;
12816
12817 if (!is_ppc64_elf (ibfd))
12818 continue;
12819
12820 lgot_ents = elf_local_got_ents (ibfd);
12821 if (!lgot_ents)
12822 continue;
12823
12824 symtab_hdr = &elf_symtab_hdr (ibfd);
12825 locsymcount = symtab_hdr->sh_info;
12826 end_lgot_ents = lgot_ents + locsymcount;
12827 local_plt = (struct plt_entry **) end_lgot_ents;
12828 end_local_plt = local_plt + locsymcount;
12829 lgot_masks = (unsigned char *) end_local_plt;
12830 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
12831 if (local_syms == NULL && locsymcount != 0)
12832 {
12833 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
12834 0, NULL, NULL, NULL);
12835 if (local_syms == NULL)
12836 return false;
12837 }
12838 s = ppc64_elf_tdata (ibfd)->got;
12839 for (isym = local_syms;
12840 lgot_ents < end_lgot_ents;
12841 ++lgot_ents, ++lgot_masks, isym++)
12842 {
12843 struct got_entry *ent;
12844
12845 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12846 {
12847 unsigned int ent_size = 8;
12848 unsigned int rel_size = sizeof (Elf64_External_Rela);
12849
12850 ent->got.offset = s->size;
12851 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12852 {
12853 ent_size *= 2;
12854 rel_size *= 2;
12855 }
12856 s->size += ent_size;
12857 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12858 {
12859 htab->elf.irelplt->size += rel_size;
12860 htab->got_reli_size += rel_size;
12861 }
12862 else if (bfd_link_pic (info)
12863 && (ent->tls_type == 0
12864 ? !info->enable_dt_relr
12865 : !bfd_link_executable (info))
12866 && isym->st_shndx != SHN_ABS)
12867 {
12868 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12869 srel->size += rel_size;
12870 }
12871 }
12872 }
12873 }
12874
12875 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12876
12877 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12878 {
12879 struct got_entry *ent;
12880
12881 if (!is_ppc64_elf (ibfd))
12882 continue;
12883
12884 ent = ppc64_tlsld_got (ibfd);
12885 if (!ent->is_indirect
12886 && ent->got.offset != (bfd_vma) -1)
12887 {
12888 asection *s = ppc64_elf_tdata (ibfd)->got;
12889 ent->got.offset = s->size;
12890 s->size += 16;
12891 if (bfd_link_dll (info))
12892 {
12893 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12894 srel->size += sizeof (Elf64_External_Rela);
12895 }
12896 }
12897 }
12898
12899 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12900 if (!done_something)
12901 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12902 {
12903 asection *got;
12904
12905 if (!is_ppc64_elf (ibfd))
12906 continue;
12907
12908 got = ppc64_elf_tdata (ibfd)->got;
12909 if (got != NULL)
12910 {
12911 done_something = got->rawsize != got->size;
12912 if (done_something)
12913 break;
12914 }
12915 }
12916
12917 if (done_something)
12918 (*htab->params->layout_sections_again) ();
12919
12920 /* Set up for second pass over toc sections to recalculate elf_gp
12921 on input sections. */
12922 htab->toc_bfd = NULL;
12923 htab->toc_first_sec = NULL;
12924 htab->second_toc_pass = true;
12925 return done_something;
12926 }
12927
12928 /* Called after second pass of multitoc partitioning. */
12929
12930 void
12931 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12932 {
12933 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12934
12935 /* After the second pass, toc_curr tracks the TOC offset used
12936 for code sections below in ppc64_elf_next_input_section. */
12937 htab->toc_curr = TOC_BASE_OFF;
12938 }
12939
12940 /* No toc references were found in ISEC. If the code in ISEC makes no
12941 calls, then there's no need to use toc adjusting stubs when branching
12942 into ISEC. Actually, indirect calls from ISEC are OK as they will
12943 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12944 needed, and 2 if a cyclical call-graph was found but no other reason
12945 for a stub was detected. If called from the top level, a return of
12946 2 means the same as a return of 0. */
12947
12948 static int
12949 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12950 {
12951 int ret;
12952
12953 /* Mark this section as checked. */
12954 isec->call_check_done = 1;
12955
12956 /* We know none of our code bearing sections will need toc stubs. */
12957 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12958 return 0;
12959
12960 if (isec->size == 0)
12961 return 0;
12962
12963 if (isec->output_section == NULL)
12964 return 0;
12965
12966 ret = 0;
12967 if (isec->reloc_count != 0)
12968 {
12969 Elf_Internal_Rela *relstart, *rel;
12970 Elf_Internal_Sym *local_syms;
12971 struct ppc_link_hash_table *htab;
12972
12973 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12974 info->keep_memory);
12975 if (relstart == NULL)
12976 return -1;
12977
12978 /* Look for branches to outside of this section. */
12979 local_syms = NULL;
12980 htab = ppc_hash_table (info);
12981 if (htab == NULL)
12982 return -1;
12983
12984 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12985 {
12986 enum elf_ppc64_reloc_type r_type;
12987 unsigned long r_symndx;
12988 struct elf_link_hash_entry *h;
12989 struct ppc_link_hash_entry *eh;
12990 Elf_Internal_Sym *sym;
12991 asection *sym_sec;
12992 struct _opd_sec_data *opd;
12993 bfd_vma sym_value;
12994 bfd_vma dest;
12995
12996 r_type = ELF64_R_TYPE (rel->r_info);
12997 if (r_type != R_PPC64_REL24
12998 && r_type != R_PPC64_REL24_NOTOC
12999 && r_type != R_PPC64_REL24_P9NOTOC
13000 && r_type != R_PPC64_REL14
13001 && r_type != R_PPC64_REL14_BRTAKEN
13002 && r_type != R_PPC64_REL14_BRNTAKEN
13003 && r_type != R_PPC64_PLTCALL
13004 && r_type != R_PPC64_PLTCALL_NOTOC)
13005 continue;
13006
13007 r_symndx = ELF64_R_SYM (rel->r_info);
13008 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
13009 isec->owner))
13010 {
13011 ret = -1;
13012 break;
13013 }
13014
13015 /* Calls to dynamic lib functions go through a plt call stub
13016 that uses r2. */
13017 eh = ppc_elf_hash_entry (h);
13018 if (eh != NULL
13019 && (eh->elf.plt.plist != NULL
13020 || (eh->oh != NULL
13021 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
13022 {
13023 ret = 1;
13024 break;
13025 }
13026
13027 if (sym_sec == NULL)
13028 /* Ignore other undefined symbols. */
13029 continue;
13030
13031 /* Assume branches to other sections not included in the
13032 link need stubs too, to cover -R and absolute syms. */
13033 if (sym_sec->output_section == NULL)
13034 {
13035 ret = 1;
13036 break;
13037 }
13038
13039 if (h == NULL)
13040 sym_value = sym->st_value;
13041 else
13042 {
13043 if (h->root.type != bfd_link_hash_defined
13044 && h->root.type != bfd_link_hash_defweak)
13045 abort ();
13046 sym_value = h->root.u.def.value;
13047 }
13048 sym_value += rel->r_addend;
13049
13050 /* If this branch reloc uses an opd sym, find the code section. */
13051 opd = get_opd_info (sym_sec);
13052 if (opd != NULL)
13053 {
13054 if (h == NULL && opd->adjust != NULL)
13055 {
13056 long adjust;
13057
13058 adjust = opd->adjust[OPD_NDX (sym_value)];
13059 if (adjust == -1)
13060 /* Assume deleted functions won't ever be called. */
13061 continue;
13062 sym_value += adjust;
13063 }
13064
13065 dest = opd_entry_value (sym_sec, sym_value,
13066 &sym_sec, NULL, false);
13067 if (dest == (bfd_vma) -1)
13068 continue;
13069 }
13070 else
13071 dest = (sym_value
13072 + sym_sec->output_offset
13073 + sym_sec->output_section->vma);
13074
13075 /* Ignore branch to self. */
13076 if (sym_sec == isec)
13077 continue;
13078
13079 /* If the called function uses the toc, we need a stub. */
13080 if (sym_sec->has_toc_reloc
13081 || sym_sec->makes_toc_func_call)
13082 {
13083 ret = 1;
13084 break;
13085 }
13086
13087 /* Assume any branch that needs a long branch stub might in fact
13088 need a plt_branch stub. A plt_branch stub uses r2. */
13089 else if (dest - (isec->output_offset
13090 + isec->output_section->vma
13091 + rel->r_offset) + (1 << 25)
13092 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
13093 ? h->other
13094 : sym->st_other))
13095 {
13096 ret = 1;
13097 break;
13098 }
13099
13100 /* If calling back to a section in the process of being
13101 tested, we can't say for sure that no toc adjusting stubs
13102 are needed, so don't return zero. */
13103 else if (sym_sec->call_check_in_progress)
13104 ret = 2;
13105
13106 /* Branches to another section that itself doesn't have any TOC
13107 references are OK. Recursively call ourselves to check. */
13108 else if (!sym_sec->call_check_done)
13109 {
13110 int recur;
13111
13112 /* Mark current section as indeterminate, so that other
13113 sections that call back to current won't be marked as
13114 known. */
13115 isec->call_check_in_progress = 1;
13116 recur = toc_adjusting_stub_needed (info, sym_sec);
13117 isec->call_check_in_progress = 0;
13118
13119 if (recur != 0)
13120 {
13121 ret = recur;
13122 if (recur != 2)
13123 break;
13124 }
13125 }
13126 }
13127
13128 if (elf_symtab_hdr (isec->owner).contents
13129 != (unsigned char *) local_syms)
13130 free (local_syms);
13131 if (elf_section_data (isec)->relocs != relstart)
13132 free (relstart);
13133 }
13134
13135 if ((ret & 1) == 0
13136 && isec->map_head.s != NULL
13137 && (strcmp (isec->output_section->name, ".init") == 0
13138 || strcmp (isec->output_section->name, ".fini") == 0))
13139 {
13140 if (isec->map_head.s->has_toc_reloc
13141 || isec->map_head.s->makes_toc_func_call)
13142 ret = 1;
13143 else if (!isec->map_head.s->call_check_done)
13144 {
13145 int recur;
13146 isec->call_check_in_progress = 1;
13147 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13148 isec->call_check_in_progress = 0;
13149 if (recur != 0)
13150 ret = recur;
13151 }
13152 }
13153
13154 if (ret == 1)
13155 isec->makes_toc_func_call = 1;
13156
13157 return ret;
13158 }
13159
13160 /* The linker repeatedly calls this function for each input section,
13161 in the order that input sections are linked into output sections.
13162 Build lists of input sections to determine groupings between which
13163 we may insert linker stubs. */
13164
13165 bool
13166 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13167 {
13168 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13169
13170 if (htab == NULL)
13171 return false;
13172
13173 if ((isec->output_section->flags & SEC_CODE) != 0
13174 && isec->output_section->id < htab->sec_info_arr_size)
13175 {
13176 /* This happens to make the list in reverse order,
13177 which is what we want. */
13178 htab->sec_info[isec->id].u.list
13179 = htab->sec_info[isec->output_section->id].u.list;
13180 htab->sec_info[isec->output_section->id].u.list = isec;
13181 }
13182
13183 if (htab->multi_toc_needed)
13184 {
13185 /* Analyse sections that aren't already flagged as needing a
13186 valid toc pointer. Exclude .fixup for the linux kernel.
13187 .fixup contains branches, but only back to the function that
13188 hit an exception. */
13189 if (!(isec->has_toc_reloc
13190 || (isec->flags & SEC_CODE) == 0
13191 || strcmp (isec->name, ".fixup") == 0
13192 || isec->call_check_done))
13193 {
13194 if (toc_adjusting_stub_needed (info, isec) < 0)
13195 return false;
13196 }
13197 /* Make all sections use the TOC assigned for this object file.
13198 This will be wrong for pasted sections; We fix that in
13199 check_pasted_section(). */
13200 if (elf_gp (isec->owner) != 0)
13201 htab->toc_curr = elf_gp (isec->owner);
13202 }
13203
13204 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13205 return true;
13206 }
13207
13208 /* Check that all .init and .fini sections use the same toc, if they
13209 have toc relocs. */
13210
13211 static bool
13212 check_pasted_section (struct bfd_link_info *info, const char *name)
13213 {
13214 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13215
13216 if (o != NULL)
13217 {
13218 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13219 bfd_vma toc_off = 0;
13220 asection *i;
13221
13222 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13223 if (i->has_toc_reloc)
13224 {
13225 if (toc_off == 0)
13226 toc_off = htab->sec_info[i->id].toc_off;
13227 else if (toc_off != htab->sec_info[i->id].toc_off)
13228 return false;
13229 }
13230
13231 if (toc_off == 0)
13232 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13233 if (i->makes_toc_func_call)
13234 {
13235 toc_off = htab->sec_info[i->id].toc_off;
13236 break;
13237 }
13238
13239 /* Make sure the whole pasted function uses the same toc offset. */
13240 if (toc_off != 0)
13241 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13242 htab->sec_info[i->id].toc_off = toc_off;
13243 }
13244 return true;
13245 }
13246
13247 bool
13248 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13249 {
13250 bool ret1 = check_pasted_section (info, ".init");
13251 bool ret2 = check_pasted_section (info, ".fini");
13252
13253 return ret1 && ret2;
13254 }
13255
13256 /* See whether we can group stub sections together. Grouping stub
13257 sections may result in fewer stubs. More importantly, we need to
13258 put all .init* and .fini* stubs at the beginning of the .init or
13259 .fini output sections respectively, because glibc splits the
13260 _init and _fini functions into multiple parts. Putting a stub in
13261 the middle of a function is not a good idea. */
13262
13263 static bool
13264 group_sections (struct bfd_link_info *info,
13265 bfd_size_type stub_group_size,
13266 bool stubs_always_before_branch)
13267 {
13268 struct ppc_link_hash_table *htab;
13269 asection *osec;
13270 bool suppress_size_errors;
13271
13272 htab = ppc_hash_table (info);
13273 if (htab == NULL)
13274 return false;
13275
13276 suppress_size_errors = false;
13277 if (stub_group_size == 1)
13278 {
13279 /* Default values. */
13280 if (stubs_always_before_branch)
13281 stub_group_size = 0x1e00000;
13282 else
13283 stub_group_size = 0x1c00000;
13284 suppress_size_errors = true;
13285 }
13286
13287 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13288 {
13289 asection *tail;
13290
13291 if (osec->id >= htab->sec_info_arr_size)
13292 continue;
13293
13294 tail = htab->sec_info[osec->id].u.list;
13295 while (tail != NULL)
13296 {
13297 asection *curr;
13298 asection *prev;
13299 bfd_size_type total;
13300 bool big_sec;
13301 bfd_vma curr_toc;
13302 struct map_stub *group;
13303 bfd_size_type group_size;
13304
13305 curr = tail;
13306 total = tail->size;
13307 group_size = (ppc64_elf_section_data (tail) != NULL
13308 && ppc64_elf_section_data (tail)->has_14bit_branch
13309 ? stub_group_size >> 10 : stub_group_size);
13310
13311 big_sec = total > group_size;
13312 if (big_sec && !suppress_size_errors)
13313 /* xgettext:c-format */
13314 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13315 tail->owner, tail);
13316 curr_toc = htab->sec_info[tail->id].toc_off;
13317
13318 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13319 && ((total += curr->output_offset - prev->output_offset)
13320 < (ppc64_elf_section_data (prev) != NULL
13321 && ppc64_elf_section_data (prev)->has_14bit_branch
13322 ? (group_size = stub_group_size >> 10) : group_size))
13323 && htab->sec_info[prev->id].toc_off == curr_toc)
13324 curr = prev;
13325
13326 /* OK, the size from the start of CURR to the end is less
13327 than group_size and thus can be handled by one stub
13328 section. (or the tail section is itself larger than
13329 group_size, in which case we may be toast.) We should
13330 really be keeping track of the total size of stubs added
13331 here, as stubs contribute to the final output section
13332 size. That's a little tricky, and this way will only
13333 break if stubs added make the total size more than 2^25,
13334 ie. for the default stub_group_size, if stubs total more
13335 than 2097152 bytes, or nearly 75000 plt call stubs. */
13336 group = bfd_alloc (curr->owner, sizeof (*group));
13337 if (group == NULL)
13338 return false;
13339 group->link_sec = curr;
13340 group->stub_sec = NULL;
13341 group->needs_save_res = 0;
13342 group->lr_restore = 0;
13343 group->eh_size = 0;
13344 group->eh_base = 0;
13345 group->next = htab->group;
13346 htab->group = group;
13347 do
13348 {
13349 prev = htab->sec_info[tail->id].u.list;
13350 /* Set up this stub group. */
13351 htab->sec_info[tail->id].u.group = group;
13352 }
13353 while (tail != curr && (tail = prev) != NULL);
13354
13355 /* But wait, there's more! Input sections up to group_size
13356 bytes before the stub section can be handled by it too.
13357 Don't do this if we have a really large section after the
13358 stubs, as adding more stubs increases the chance that
13359 branches may not reach into the stub section. */
13360 if (!stubs_always_before_branch && !big_sec)
13361 {
13362 total = 0;
13363 while (prev != NULL
13364 && ((total += tail->output_offset - prev->output_offset)
13365 < (ppc64_elf_section_data (prev) != NULL
13366 && ppc64_elf_section_data (prev)->has_14bit_branch
13367 ? (group_size = stub_group_size >> 10)
13368 : group_size))
13369 && htab->sec_info[prev->id].toc_off == curr_toc)
13370 {
13371 tail = prev;
13372 prev = htab->sec_info[tail->id].u.list;
13373 htab->sec_info[tail->id].u.group = group;
13374 }
13375 }
13376 tail = prev;
13377 }
13378 }
13379 return true;
13380 }
13381
13382 static const unsigned char glink_eh_frame_cie[] =
13383 {
13384 0, 0, 0, 16, /* length. */
13385 0, 0, 0, 0, /* id. */
13386 1, /* CIE version. */
13387 'z', 'R', 0, /* Augmentation string. */
13388 4, /* Code alignment. */
13389 0x78, /* Data alignment. */
13390 65, /* RA reg. */
13391 1, /* Augmentation size. */
13392 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13393 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13394 };
13395
13396 /* Stripping output sections is normally done before dynamic section
13397 symbols have been allocated. This function is called later, and
13398 handles cases like htab->brlt which is mapped to its own output
13399 section. */
13400
13401 static void
13402 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13403 {
13404 if (isec->size == 0
13405 && isec->output_section->size == 0
13406 && !(isec->output_section->flags & SEC_KEEP)
13407 && !bfd_section_removed_from_list (info->output_bfd,
13408 isec->output_section)
13409 && elf_section_data (isec->output_section)->dynindx == 0)
13410 {
13411 isec->output_section->flags |= SEC_EXCLUDE;
13412 bfd_section_list_remove (info->output_bfd, isec->output_section);
13413 info->output_bfd->section_count--;
13414 }
13415 }
13416
13417 /* Stash R_PPC64_RELATIVE reloc at input section SEC, r_offset OFF to
13418 the array of such relocs. */
13419
13420 static bool
13421 append_relr_off (struct ppc_link_hash_table *htab, asection *sec, bfd_vma off)
13422 {
13423 if (htab->relr_count >= htab->relr_alloc)
13424 {
13425 if (htab->relr_alloc == 0)
13426 htab->relr_alloc = 4096;
13427 else
13428 htab->relr_alloc *= 2;
13429 htab->relr = bfd_realloc (htab->relr,
13430 htab->relr_alloc * sizeof (*htab->relr));
13431 if (htab->relr == NULL)
13432 return false;
13433 }
13434 htab->relr[htab->relr_count].sec = sec;
13435 htab->relr[htab->relr_count].off = off;
13436 htab->relr_count++;
13437 return true;
13438 }
13439
13440 /* qsort comparator for bfd_vma args. */
13441
13442 static int
13443 compare_relr_address (const void *arg1, const void *arg2)
13444 {
13445 bfd_vma a = *(bfd_vma *) arg1;
13446 bfd_vma b = *(bfd_vma *) arg2;
13447 return a < b ? -1 : a > b ? 1 : 0;
13448 }
13449
13450 /* Produce a malloc'd sorted array of reloc addresses from the info
13451 stored by append_relr_off. */
13452
13453 static bfd_vma *
13454 sort_relr (struct ppc_link_hash_table *htab)
13455 {
13456 bfd_vma *addr = bfd_malloc (htab->relr_count * sizeof (*addr));
13457 if (addr == NULL)
13458 return NULL;
13459
13460 for (size_t i = 0; i < htab->relr_count; i++)
13461 addr[i] = (htab->relr[i].sec->output_section->vma
13462 + htab->relr[i].sec->output_offset
13463 + htab->relr[i].off);
13464
13465 if (htab->relr_count > 1)
13466 qsort (addr, htab->relr_count, sizeof (*addr), compare_relr_address);
13467
13468 return addr;
13469 }
13470
13471 /* Look over GOT and PLT entries saved on elf_local_got_ents for all
13472 input files, stashing info about needed relative relocs. */
13473
13474 static bool
13475 got_and_plt_relr_for_local_syms (struct bfd_link_info *info)
13476 {
13477 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13478 bfd *ibfd;
13479
13480 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
13481 {
13482 struct got_entry **lgot_ents, **lgot, **end_lgot_ents;
13483 struct plt_entry **local_plt, **lplt, **end_local_plt;
13484 Elf_Internal_Shdr *symtab_hdr;
13485 bfd_size_type locsymcount;
13486 Elf_Internal_Sym *local_syms;
13487 Elf_Internal_Sym *isym;
13488 struct plt_entry *pent;
13489 struct got_entry *gent;
13490
13491 if (!is_ppc64_elf (ibfd))
13492 continue;
13493
13494 lgot_ents = elf_local_got_ents (ibfd);
13495 if (!lgot_ents)
13496 continue;
13497
13498 symtab_hdr = &elf_symtab_hdr (ibfd);
13499 locsymcount = symtab_hdr->sh_info;
13500 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
13501 if (local_syms == NULL && locsymcount != 0)
13502 {
13503 local_syms = bfd_elf_get_elf_syms (ibfd, symtab_hdr, locsymcount,
13504 0, NULL, NULL, NULL);
13505 if (local_syms == NULL)
13506 return false;
13507 }
13508 end_lgot_ents = lgot_ents + locsymcount;
13509 local_plt = (struct plt_entry **) end_lgot_ents;
13510 end_local_plt = local_plt + locsymcount;
13511 for (lgot = lgot_ents, isym = local_syms;
13512 lgot < end_lgot_ents;
13513 ++lgot, ++isym)
13514 for (gent = *lgot; gent != NULL; gent = gent->next)
13515 if (!gent->is_indirect
13516 && gent->tls_type == 0
13517 && gent->got.offset != (bfd_vma) -1
13518 && isym->st_shndx != SHN_ABS)
13519 {
13520 asection *got = ppc64_elf_tdata (gent->owner)->got;
13521 if (!append_relr_off (htab, got, gent->got.offset))
13522 {
13523 htab->stub_error = true;
13524 return false;
13525 }
13526 }
13527
13528 if (!htab->opd_abi)
13529 for (lplt = local_plt, isym = local_syms;
13530 lplt < end_local_plt;
13531 ++lplt, ++isym)
13532 for (pent = *lplt; pent != NULL; pent = pent->next)
13533 if (pent->plt.offset != (bfd_vma) -1
13534 && ELF_ST_TYPE (isym->st_info) != STT_GNU_IFUNC)
13535 {
13536 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13537 {
13538 if (symtab_hdr->contents != (unsigned char *) local_syms)
13539 free (local_syms);
13540 return false;
13541 }
13542 }
13543
13544 if (local_syms != NULL
13545 && symtab_hdr->contents != (unsigned char *) local_syms)
13546 {
13547 if (!info->keep_memory)
13548 free (local_syms);
13549 else
13550 symtab_hdr->contents = (unsigned char *) local_syms;
13551 }
13552 }
13553 return true;
13554 }
13555
13556 /* Stash info about needed GOT and PLT entry relative relocs for
13557 global symbol H. */
13558
13559 static bool
13560 got_and_plt_relr (struct elf_link_hash_entry *h, void *inf)
13561 {
13562 struct bfd_link_info *info;
13563 struct ppc_link_hash_table *htab;
13564 struct plt_entry *pent;
13565 struct got_entry *gent;
13566
13567 if (h->root.type == bfd_link_hash_indirect)
13568 return true;
13569
13570 info = (struct bfd_link_info *) inf;
13571 htab = ppc_hash_table (info);
13572 if (htab == NULL)
13573 return false;
13574
13575 if (h->type != STT_GNU_IFUNC
13576 && h->def_regular
13577 && (h->root.type == bfd_link_hash_defined
13578 || h->root.type == bfd_link_hash_defweak))
13579 {
13580 if ((!htab->elf.dynamic_sections_created
13581 || h->dynindx == -1
13582 || SYMBOL_REFERENCES_LOCAL (info, h))
13583 && !bfd_is_abs_symbol (&h->root))
13584 for (gent = h->got.glist; gent != NULL; gent = gent->next)
13585 if (!gent->is_indirect
13586 && gent->tls_type == 0
13587 && gent->got.offset != (bfd_vma) -1)
13588 {
13589 asection *got = ppc64_elf_tdata (gent->owner)->got;
13590 if (!append_relr_off (htab, got, gent->got.offset))
13591 {
13592 htab->stub_error = true;
13593 return false;
13594 }
13595 }
13596
13597 if (!htab->opd_abi
13598 && use_local_plt (info, h))
13599 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
13600 if (pent->plt.offset != (bfd_vma) -1)
13601 {
13602 if (!append_relr_off (htab, htab->pltlocal, pent->plt.offset))
13603 {
13604 htab->stub_error = true;
13605 return false;
13606 }
13607 }
13608 }
13609 return true;
13610 }
13611
13612 /* Determine and set the size of the stub section for a final link.
13613
13614 The basic idea here is to examine all the relocations looking for
13615 PC-relative calls to a target that is unreachable with a "bl"
13616 instruction. */
13617
13618 bool
13619 ppc64_elf_size_stubs (struct bfd_link_info *info)
13620 {
13621 bfd_size_type stub_group_size;
13622 bool stubs_always_before_branch;
13623 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13624
13625 if (htab == NULL)
13626 return false;
13627
13628 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13629 htab->params->power10_stubs = 0;
13630
13631 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13632 htab->params->plt_thread_safe = 1;
13633 if (!htab->opd_abi)
13634 htab->params->plt_thread_safe = 0;
13635 else if (htab->params->plt_thread_safe == -1)
13636 {
13637 static const char *const thread_starter[] =
13638 {
13639 "pthread_create",
13640 /* libstdc++ */
13641 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13642 /* librt */
13643 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13644 "mq_notify", "create_timer",
13645 /* libanl */
13646 "getaddrinfo_a",
13647 /* libgomp */
13648 "GOMP_parallel",
13649 "GOMP_parallel_start",
13650 "GOMP_parallel_loop_static",
13651 "GOMP_parallel_loop_static_start",
13652 "GOMP_parallel_loop_dynamic",
13653 "GOMP_parallel_loop_dynamic_start",
13654 "GOMP_parallel_loop_guided",
13655 "GOMP_parallel_loop_guided_start",
13656 "GOMP_parallel_loop_runtime",
13657 "GOMP_parallel_loop_runtime_start",
13658 "GOMP_parallel_sections",
13659 "GOMP_parallel_sections_start",
13660 /* libgo */
13661 "__go_go",
13662 };
13663 unsigned i;
13664
13665 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13666 {
13667 struct elf_link_hash_entry *h;
13668 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13669 false, false, true);
13670 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13671 if (htab->params->plt_thread_safe)
13672 break;
13673 }
13674 }
13675 stubs_always_before_branch = htab->params->group_size < 0;
13676 if (htab->params->group_size < 0)
13677 stub_group_size = -htab->params->group_size;
13678 else
13679 stub_group_size = htab->params->group_size;
13680
13681 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13682 return false;
13683
13684 htab->tga_group = NULL;
13685 if (!htab->params->no_tls_get_addr_regsave
13686 && htab->tga_desc_fd != NULL
13687 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13688 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13689 && htab->tls_get_addr_fd != NULL
13690 && is_static_defined (&htab->tls_get_addr_fd->elf))
13691 {
13692 asection *sym_sec, *code_sec, *stub_sec;
13693 bfd_vma sym_value;
13694 struct _opd_sec_data *opd;
13695
13696 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13697 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13698 code_sec = sym_sec;
13699 opd = get_opd_info (sym_sec);
13700 if (opd != NULL)
13701 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13702 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13703 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13704 htab->tga_group->link_sec);
13705 if (stub_sec == NULL)
13706 return false;
13707 htab->tga_group->stub_sec = stub_sec;
13708
13709 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13710 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13711 htab->tga_desc_fd->elf.root.u.def.value = 0;
13712 htab->tga_desc_fd->elf.type = STT_FUNC;
13713 htab->tga_desc_fd->elf.def_regular = 1;
13714 htab->tga_desc_fd->elf.non_elf = 0;
13715 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13716 }
13717
13718 /* Loop until no stubs added. After iteration 20 of this loop we may
13719 exit on a stub section shrinking. */
13720
13721 while (1)
13722 {
13723 bfd *input_bfd;
13724 unsigned int bfd_indx;
13725 struct map_stub *group;
13726
13727 htab->stub_iteration += 1;
13728 htab->relr_count = 0;
13729
13730 for (input_bfd = info->input_bfds, bfd_indx = 0;
13731 input_bfd != NULL;
13732 input_bfd = input_bfd->link.next, bfd_indx++)
13733 {
13734 Elf_Internal_Shdr *symtab_hdr;
13735 asection *section;
13736 Elf_Internal_Sym *local_syms = NULL;
13737
13738 if (!is_ppc64_elf (input_bfd))
13739 continue;
13740
13741 /* We'll need the symbol table in a second. */
13742 symtab_hdr = &elf_symtab_hdr (input_bfd);
13743 if (symtab_hdr->sh_info == 0)
13744 continue;
13745
13746 /* Walk over each section attached to the input bfd. */
13747 for (section = input_bfd->sections;
13748 section != NULL;
13749 section = section->next)
13750 {
13751 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13752 bool is_opd;
13753
13754 /* If there aren't any relocs, then there's nothing more
13755 to do. */
13756 if ((section->flags & SEC_RELOC) == 0
13757 || (section->flags & SEC_ALLOC) == 0
13758 || (section->flags & SEC_LOAD) == 0
13759 || section->reloc_count == 0)
13760 continue;
13761
13762 if (!info->enable_dt_relr
13763 && (section->flags & SEC_CODE) == 0)
13764 continue;
13765
13766 /* If this section is a link-once section that will be
13767 discarded, then don't create any stubs. */
13768 if (section->output_section == NULL
13769 || section->output_section->owner != info->output_bfd)
13770 continue;
13771
13772 /* Get the relocs. */
13773 internal_relocs
13774 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13775 info->keep_memory);
13776 if (internal_relocs == NULL)
13777 goto error_ret_free_local;
13778
13779 is_opd = ppc64_elf_section_data (section)->sec_type == sec_opd;
13780
13781 /* Now examine each relocation. */
13782 irela = internal_relocs;
13783 irelaend = irela + section->reloc_count;
13784 for (; irela < irelaend; irela++)
13785 {
13786 enum elf_ppc64_reloc_type r_type;
13787 unsigned int r_indx;
13788 struct ppc_stub_type stub_type;
13789 struct ppc_stub_hash_entry *stub_entry;
13790 asection *sym_sec, *code_sec;
13791 bfd_vma sym_value, code_value;
13792 bfd_vma destination;
13793 unsigned long local_off;
13794 bool ok_dest;
13795 struct ppc_link_hash_entry *hash;
13796 struct ppc_link_hash_entry *fdh;
13797 struct elf_link_hash_entry *h;
13798 Elf_Internal_Sym *sym;
13799 char *stub_name;
13800 const asection *id_sec;
13801 struct _opd_sec_data *opd;
13802 struct plt_entry *plt_ent;
13803
13804 r_type = ELF64_R_TYPE (irela->r_info);
13805 r_indx = ELF64_R_SYM (irela->r_info);
13806
13807 if (r_type >= R_PPC64_max)
13808 {
13809 bfd_set_error (bfd_error_bad_value);
13810 goto error_ret_free_internal;
13811 }
13812
13813 /* Only look for stubs on branch instructions. */
13814 switch (r_type)
13815 {
13816 default:
13817 continue;
13818
13819 case R_PPC64_REL24:
13820 case R_PPC64_REL24_NOTOC:
13821 case R_PPC64_REL24_P9NOTOC:
13822 case R_PPC64_REL14:
13823 case R_PPC64_REL14_BRTAKEN:
13824 case R_PPC64_REL14_BRNTAKEN:
13825 if ((section->flags & SEC_CODE) != 0)
13826 break;
13827 continue;
13828
13829 case R_PPC64_ADDR64:
13830 case R_PPC64_TOC:
13831 if (info->enable_dt_relr
13832 && irela->r_offset % 2 == 0
13833 && section->alignment_power != 0)
13834 break;
13835 continue;
13836 }
13837
13838 /* Now determine the call target, its name, value,
13839 section. */
13840 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13841 r_indx, input_bfd))
13842 goto error_ret_free_internal;
13843
13844 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
13845 {
13846 /* Only locally defined symbols can possibly use
13847 relative relocations. */
13848 bfd_vma r_offset;
13849 if ((sym_sec == NULL
13850 || sym_sec->output_section == NULL)
13851 /* No symbol is OK too. */
13852 && !(sym != NULL && sym->st_shndx == 0)
13853 /* Hack for __ehdr_start, which is undefined
13854 at this point. */
13855 && !(h != NULL && h->root.linker_def))
13856 continue;
13857 if (NO_OPD_RELOCS && is_opd)
13858 continue;
13859 if (!is_opd
13860 && r_type == R_PPC64_ADDR64)
13861 {
13862 if (h != NULL
13863 ? h->type == STT_GNU_IFUNC
13864 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
13865 continue;
13866 if (h != NULL
13867 ? bfd_is_abs_symbol (&h->root)
13868 : sym->st_shndx == SHN_ABS)
13869 continue;
13870 if (h != NULL
13871 && !SYMBOL_REFERENCES_LOCAL (info, h))
13872 continue;
13873 }
13874 r_offset = _bfd_elf_section_offset (info->output_bfd,
13875 info,
13876 section,
13877 irela->r_offset);
13878 if (r_offset >= (bfd_vma) -2)
13879 continue;
13880 if (!append_relr_off (htab, section, r_offset))
13881 goto error_ret_free_internal;
13882 continue;
13883 }
13884
13885 hash = ppc_elf_hash_entry (h);
13886 ok_dest = false;
13887 fdh = NULL;
13888 sym_value = 0;
13889 if (hash == NULL)
13890 {
13891 sym_value = sym->st_value;
13892 if (sym_sec != NULL
13893 && sym_sec->output_section != NULL)
13894 ok_dest = true;
13895 }
13896 else if (hash->elf.root.type == bfd_link_hash_defined
13897 || hash->elf.root.type == bfd_link_hash_defweak)
13898 {
13899 sym_value = hash->elf.root.u.def.value;
13900 if (sym_sec->output_section != NULL)
13901 ok_dest = true;
13902 }
13903 else if (hash->elf.root.type == bfd_link_hash_undefweak
13904 || hash->elf.root.type == bfd_link_hash_undefined)
13905 {
13906 /* Recognise an old ABI func code entry sym, and
13907 use the func descriptor sym instead if it is
13908 defined. */
13909 if (hash->elf.root.root.string[0] == '.'
13910 && hash->oh != NULL)
13911 {
13912 fdh = ppc_follow_link (hash->oh);
13913 if (fdh->elf.root.type == bfd_link_hash_defined
13914 || fdh->elf.root.type == bfd_link_hash_defweak)
13915 {
13916 sym_sec = fdh->elf.root.u.def.section;
13917 sym_value = fdh->elf.root.u.def.value;
13918 if (sym_sec->output_section != NULL)
13919 ok_dest = true;
13920 }
13921 else
13922 fdh = NULL;
13923 }
13924 }
13925 else
13926 {
13927 bfd_set_error (bfd_error_bad_value);
13928 goto error_ret_free_internal;
13929 }
13930
13931 destination = 0;
13932 local_off = 0;
13933 if (ok_dest)
13934 {
13935 sym_value += irela->r_addend;
13936 destination = (sym_value
13937 + sym_sec->output_offset
13938 + sym_sec->output_section->vma);
13939 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13940 ? hash->elf.other
13941 : sym->st_other);
13942 }
13943
13944 code_sec = sym_sec;
13945 code_value = sym_value;
13946 opd = get_opd_info (sym_sec);
13947 if (opd != NULL)
13948 {
13949 bfd_vma dest;
13950
13951 if (hash == NULL && opd->adjust != NULL)
13952 {
13953 long adjust = opd->adjust[OPD_NDX (sym_value)];
13954 if (adjust == -1)
13955 continue;
13956 code_value += adjust;
13957 sym_value += adjust;
13958 }
13959 dest = opd_entry_value (sym_sec, sym_value,
13960 &code_sec, &code_value, false);
13961 if (dest != (bfd_vma) -1)
13962 {
13963 destination = dest;
13964 if (fdh != NULL)
13965 {
13966 /* Fixup old ABI sym to point at code
13967 entry. */
13968 hash->elf.root.type = bfd_link_hash_defweak;
13969 hash->elf.root.u.def.section = code_sec;
13970 hash->elf.root.u.def.value = code_value;
13971 }
13972 }
13973 }
13974
13975 /* Determine what (if any) linker stub is needed. */
13976 plt_ent = NULL;
13977 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13978 &plt_ent, destination,
13979 local_off);
13980 stub_type.sub = ppc_stub_toc;
13981 stub_type.r2save = 0;
13982
13983 if (r_type == R_PPC64_REL24_NOTOC
13984 || r_type == R_PPC64_REL24_P9NOTOC)
13985 {
13986 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13987 if (htab->params->power10_stubs == 0
13988 || (r_type == R_PPC64_REL24_P9NOTOC
13989 && htab->params->power10_stubs != 1))
13990 notoc = ppc_stub_p9notoc;
13991 if (stub_type.main == ppc_stub_plt_call)
13992 stub_type.sub = notoc;
13993 else if (stub_type.main == ppc_stub_long_branch
13994 || (code_sec != NULL
13995 && code_sec->output_section != NULL
13996 && (((hash ? hash->elf.other : sym->st_other)
13997 & STO_PPC64_LOCAL_MASK)
13998 > 1 << STO_PPC64_LOCAL_BIT)))
13999 {
14000 stub_type.main = ppc_stub_long_branch;
14001 stub_type.sub = notoc;
14002 stub_type.r2save = 0;
14003 }
14004 }
14005 else if (stub_type.main != ppc_stub_plt_call)
14006 {
14007 /* Check whether we need a TOC adjusting stub.
14008 Since the linker pastes together pieces from
14009 different object files when creating the
14010 _init and _fini functions, it may be that a
14011 call to what looks like a local sym is in
14012 fact a call needing a TOC adjustment. */
14013 if ((code_sec != NULL
14014 && code_sec->output_section != NULL
14015 && (code_sec->has_toc_reloc
14016 || code_sec->makes_toc_func_call)
14017 && (htab->sec_info[code_sec->id].toc_off
14018 != htab->sec_info[section->id].toc_off))
14019 || (((hash ? hash->elf.other : sym->st_other)
14020 & STO_PPC64_LOCAL_MASK)
14021 == 1 << STO_PPC64_LOCAL_BIT))
14022 {
14023 stub_type.main = ppc_stub_long_branch;
14024 stub_type.sub = ppc_stub_toc;
14025 stub_type.r2save = 1;
14026 }
14027 }
14028
14029 if (stub_type.main == ppc_stub_none)
14030 continue;
14031
14032 /* __tls_get_addr calls might be eliminated. */
14033 if (stub_type.main != ppc_stub_plt_call
14034 && hash != NULL
14035 && is_tls_get_addr (&hash->elf, htab)
14036 && section->has_tls_reloc
14037 && irela != internal_relocs)
14038 {
14039 /* Get tls info. */
14040 unsigned char *tls_mask;
14041
14042 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
14043 irela - 1, input_bfd))
14044 goto error_ret_free_internal;
14045 if ((*tls_mask & TLS_TLS) != 0
14046 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
14047 continue;
14048 }
14049
14050 if (stub_type.main == ppc_stub_plt_call
14051 && stub_type.sub == ppc_stub_toc)
14052 {
14053 if (!htab->opd_abi
14054 && htab->params->plt_localentry0 != 0
14055 && is_elfv2_localentry0 (&hash->elf))
14056 htab->has_plt_localentry0 = 1;
14057 else if (irela + 1 < irelaend
14058 && irela[1].r_offset == irela->r_offset + 4
14059 && (ELF64_R_TYPE (irela[1].r_info)
14060 == R_PPC64_TOCSAVE))
14061 {
14062 if (!tocsave_find (htab, INSERT,
14063 &local_syms, irela + 1, input_bfd))
14064 goto error_ret_free_internal;
14065 }
14066 else
14067 stub_type.r2save = 1;
14068 }
14069
14070 /* Support for grouping stub sections. */
14071 id_sec = htab->sec_info[section->id].u.group->link_sec;
14072
14073 /* Get the name of this stub. */
14074 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
14075 if (!stub_name)
14076 goto error_ret_free_internal;
14077
14078 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
14079 stub_name, false, false);
14080 if (stub_entry != NULL)
14081 {
14082 free (stub_name);
14083 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
14084 {
14085 /* xgettext:c-format */
14086 _bfd_error_handler
14087 (_("%pB: cannot create stub entry %s"),
14088 section->owner, stub_entry->root.string);
14089 goto error_ret_free_internal;
14090 }
14091 continue;
14092 }
14093
14094 stub_entry = ppc_add_stub (stub_name, section, info);
14095 if (stub_entry == NULL)
14096 {
14097 free (stub_name);
14098 error_ret_free_internal:
14099 if (elf_section_data (section)->relocs == NULL)
14100 free (internal_relocs);
14101 error_ret_free_local:
14102 if (symtab_hdr->contents
14103 != (unsigned char *) local_syms)
14104 free (local_syms);
14105 return false;
14106 }
14107
14108 stub_entry->type = stub_type;
14109 if (stub_type.main == ppc_stub_plt_call)
14110 {
14111 stub_entry->target_value = sym_value;
14112 stub_entry->target_section = sym_sec;
14113 }
14114 else
14115 {
14116 stub_entry->target_value = code_value;
14117 stub_entry->target_section = code_sec;
14118 }
14119 stub_entry->h = hash;
14120 stub_entry->plt_ent = plt_ent;
14121 stub_entry->symtype
14122 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
14123 stub_entry->other = hash ? hash->elf.other : sym->st_other;
14124
14125 if (hash != NULL
14126 && (hash->elf.root.type == bfd_link_hash_defined
14127 || hash->elf.root.type == bfd_link_hash_defweak))
14128 htab->stub_globals += 1;
14129 }
14130
14131 /* We're done with the internal relocs, free them. */
14132 if (elf_section_data (section)->relocs != internal_relocs)
14133 free (internal_relocs);
14134 }
14135
14136 if (local_syms != NULL
14137 && symtab_hdr->contents != (unsigned char *) local_syms)
14138 {
14139 if (!info->keep_memory)
14140 free (local_syms);
14141 else
14142 symtab_hdr->contents = (unsigned char *) local_syms;
14143 }
14144 }
14145
14146 /* We may have added some stubs. Find out the new size of the
14147 stub sections. */
14148 for (group = htab->group; group != NULL; group = group->next)
14149 {
14150 group->lr_restore = 0;
14151 group->eh_size = 0;
14152 if (group->stub_sec != NULL)
14153 {
14154 asection *stub_sec = group->stub_sec;
14155
14156 stub_sec->rawsize = stub_sec->size;
14157 stub_sec->size = 0;
14158 stub_sec->reloc_count = 0;
14159 stub_sec->flags &= ~SEC_RELOC;
14160 }
14161 }
14162 if (htab->tga_group != NULL)
14163 {
14164 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
14165 htab->tga_group->eh_size
14166 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
14167 htab->tga_group->lr_restore = 23 * 4;
14168 htab->tga_group->stub_sec->size = 24 * 4;
14169 }
14170
14171 htab->brlt->rawsize = htab->brlt->size;
14172 htab->brlt->size = 0;
14173 htab->brlt->reloc_count = 0;
14174 htab->brlt->flags &= ~SEC_RELOC;
14175 if (htab->relbrlt != NULL)
14176 htab->relbrlt->size = 0;
14177
14178 if (htab->elf.srelrdyn != NULL)
14179 {
14180 htab->elf.srelrdyn->rawsize = htab->elf.srelrdyn->size;
14181 htab->elf.srelrdyn->size = 0;
14182 }
14183
14184 htab->stub_changed = false;
14185 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
14186
14187 for (group = htab->group; group != NULL; group = group->next)
14188 if (group->needs_save_res)
14189 group->stub_sec->size += htab->sfpr->size;
14190
14191 if (info->emitrelocations
14192 && htab->glink != NULL && htab->glink->size != 0)
14193 {
14194 htab->glink->reloc_count = 1;
14195 htab->glink->flags |= SEC_RELOC;
14196 }
14197
14198 if (htab->glink_eh_frame != NULL
14199 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
14200 && htab->glink_eh_frame->output_section->size > 8)
14201 {
14202 size_t size = 0, align = 4;
14203
14204 for (group = htab->group; group != NULL; group = group->next)
14205 if (group->eh_size != 0)
14206 size += (group->eh_size + 17 + align - 1) & -align;
14207 if (htab->glink != NULL && htab->glink->size != 0)
14208 size += (24 + align - 1) & -align;
14209 if (size != 0)
14210 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14211 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14212 size = (size + align - 1) & -align;
14213 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
14214 htab->glink_eh_frame->size = size;
14215 }
14216
14217 if (htab->params->plt_stub_align != 0)
14218 for (group = htab->group; group != NULL; group = group->next)
14219 if (group->stub_sec != NULL)
14220 {
14221 int align = abs (htab->params->plt_stub_align);
14222 group->stub_sec->size
14223 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
14224 }
14225
14226 if (htab->elf.srelrdyn != NULL)
14227 {
14228 bfd_vma r_offset;
14229
14230 for (r_offset = 0; r_offset < htab->brlt->size; r_offset += 8)
14231 if (!append_relr_off (htab, htab->brlt, r_offset))
14232 return false;
14233
14234 if (!got_and_plt_relr_for_local_syms (info))
14235 return false;
14236 elf_link_hash_traverse (&htab->elf, got_and_plt_relr, info);
14237 if (htab->stub_error)
14238 return false;
14239
14240 bfd_vma *relr_addr = sort_relr (htab);
14241 if (htab->relr_count != 0 && relr_addr == NULL)
14242 return false;
14243
14244 size_t i = 0;
14245 while (i < htab->relr_count)
14246 {
14247 bfd_vma base = relr_addr[i];
14248 htab->elf.srelrdyn->size += 8;
14249 i++;
14250 /* Handle possible duplicate address. This can happen
14251 as sections increase in size when adding stubs. */
14252 while (i < htab->relr_count
14253 && relr_addr[i] == base)
14254 i++;
14255 base += 8;
14256 while (1)
14257 {
14258 size_t start_i = i;
14259 while (i < htab->relr_count
14260 && relr_addr[i] - base < 63 * 8
14261 && (relr_addr[i] - base) % 8 == 0)
14262 i++;
14263 if (i == start_i)
14264 break;
14265 htab->elf.srelrdyn->size += 8;
14266 base += 63 * 8;
14267 }
14268 }
14269 free (relr_addr);
14270 }
14271
14272 for (group = htab->group; group != NULL; group = group->next)
14273 if (group->stub_sec != NULL
14274 && group->stub_sec->rawsize != group->stub_sec->size
14275 && (htab->stub_iteration <= STUB_SHRINK_ITER
14276 || group->stub_sec->rawsize < group->stub_sec->size))
14277 break;
14278
14279 if (group == NULL
14280 && (!htab->stub_changed
14281 || htab->stub_iteration > STUB_SHRINK_ITER)
14282 && (htab->brlt->rawsize == htab->brlt->size
14283 || (htab->stub_iteration > STUB_SHRINK_ITER
14284 && htab->brlt->rawsize > htab->brlt->size))
14285 && (htab->elf.srelrdyn == NULL
14286 || htab->elf.srelrdyn->rawsize == htab->elf.srelrdyn->size
14287 || (htab->stub_iteration > STUB_SHRINK_ITER
14288 && htab->elf.srelrdyn->rawsize > htab->elf.srelrdyn->size))
14289 && (htab->glink_eh_frame == NULL
14290 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
14291 && (htab->tga_group == NULL
14292 || htab->stub_iteration > 1))
14293 break;
14294
14295 if (htab->stub_iteration > STUB_SHRINK_ITER)
14296 {
14297 for (group = htab->group; group != NULL; group = group->next)
14298 if (group->stub_sec != NULL
14299 && group->stub_sec->size < group->stub_sec->rawsize)
14300 group->stub_sec->size = group->stub_sec->rawsize;
14301
14302 if (htab->brlt->size < htab->brlt->rawsize)
14303 htab->brlt->size = htab->brlt->rawsize;
14304
14305 if (htab->elf.srelrdyn != NULL
14306 && htab->elf.srelrdyn->size < htab->elf.srelrdyn->rawsize)
14307 htab->elf.srelrdyn->size = htab->elf.srelrdyn->rawsize;
14308 }
14309
14310 /* Ask the linker to do its stuff. */
14311 (*htab->params->layout_sections_again) ();
14312 }
14313
14314 if (htab->glink_eh_frame != NULL
14315 && htab->glink_eh_frame->size != 0)
14316 {
14317 bfd_vma val;
14318 bfd_byte *p, *last_fde;
14319 size_t last_fde_len, size, align, pad;
14320 struct map_stub *group;
14321
14322 /* It is necessary to at least have a rough outline of the
14323 linker generated CIEs and FDEs written before
14324 bfd_elf_discard_info is run, in order for these FDEs to be
14325 indexed in .eh_frame_hdr. */
14326 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
14327 if (p == NULL)
14328 return false;
14329 htab->glink_eh_frame->contents = p;
14330 last_fde = p;
14331 align = 4;
14332
14333 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
14334 /* CIE length (rewrite in case little-endian). */
14335 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
14336 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14337 p += last_fde_len + 4;
14338
14339 for (group = htab->group; group != NULL; group = group->next)
14340 if (group->eh_size != 0)
14341 {
14342 group->eh_base = p - htab->glink_eh_frame->contents;
14343 last_fde = p;
14344 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
14345 /* FDE length. */
14346 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14347 p += 4;
14348 /* CIE pointer. */
14349 val = p - htab->glink_eh_frame->contents;
14350 bfd_put_32 (htab->elf.dynobj, val, p);
14351 p += 4;
14352 /* Offset to stub section, written later. */
14353 p += 4;
14354 /* stub section size. */
14355 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
14356 p += 4;
14357 /* Augmentation. */
14358 p += 1;
14359 /* Make sure we don't have all nops. This is enough for
14360 elf-eh-frame.c to detect the last non-nop opcode. */
14361 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
14362 p = last_fde + last_fde_len + 4;
14363 }
14364 if (htab->glink != NULL && htab->glink->size != 0)
14365 {
14366 last_fde = p;
14367 last_fde_len = ((24 + align - 1) & -align) - 4;
14368 /* FDE length. */
14369 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
14370 p += 4;
14371 /* CIE pointer. */
14372 val = p - htab->glink_eh_frame->contents;
14373 bfd_put_32 (htab->elf.dynobj, val, p);
14374 p += 4;
14375 /* Offset to .glink, written later. */
14376 p += 4;
14377 /* .glink size. */
14378 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
14379 p += 4;
14380 /* Augmentation. */
14381 p += 1;
14382
14383 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
14384 *p++ = DW_CFA_register;
14385 *p++ = 65;
14386 *p++ = htab->opd_abi ? 12 : 0;
14387 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
14388 *p++ = DW_CFA_restore_extended;
14389 *p++ = 65;
14390 p += ((24 + align - 1) & -align) - 24;
14391 }
14392 /* Subsume any padding into the last FDE if user .eh_frame
14393 sections are aligned more than glink_eh_frame. Otherwise any
14394 zero padding will be seen as a terminator. */
14395 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
14396 size = p - htab->glink_eh_frame->contents;
14397 pad = ((size + align - 1) & -align) - size;
14398 htab->glink_eh_frame->size = size + pad;
14399 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
14400 }
14401
14402 maybe_strip_output (info, htab->brlt);
14403 if (htab->relbrlt != NULL)
14404 maybe_strip_output (info, htab->relbrlt);
14405 if (htab->glink_eh_frame != NULL)
14406 maybe_strip_output (info, htab->glink_eh_frame);
14407 if (htab->elf.srelrdyn != NULL)
14408 maybe_strip_output (info, htab->elf.srelrdyn);
14409
14410 return true;
14411 }
14412
14413 /* Called after we have determined section placement. If sections
14414 move, we'll be called again. Provide a value for TOCstart. */
14415
14416 bfd_vma
14417 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
14418 {
14419 asection *s;
14420 bfd_vma TOCstart, adjust;
14421
14422 if (info != NULL)
14423 {
14424 struct elf_link_hash_entry *h;
14425 struct elf_link_hash_table *htab = elf_hash_table (info);
14426
14427 if (is_elf_hash_table (&htab->root)
14428 && htab->hgot != NULL)
14429 h = htab->hgot;
14430 else
14431 {
14432 h = (struct elf_link_hash_entry *)
14433 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
14434 if (is_elf_hash_table (&htab->root))
14435 htab->hgot = h;
14436 }
14437 if (h != NULL
14438 && h->root.type == bfd_link_hash_defined
14439 && !h->root.linker_def
14440 && (!is_elf_hash_table (&htab->root)
14441 || h->def_regular))
14442 {
14443 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
14444 _bfd_set_gp_value (obfd, TOCstart);
14445 return TOCstart;
14446 }
14447 }
14448
14449 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14450 order. The TOC starts where the first of these sections starts. */
14451 s = bfd_get_section_by_name (obfd, ".got");
14452 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14453 s = bfd_get_section_by_name (obfd, ".toc");
14454 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14455 s = bfd_get_section_by_name (obfd, ".tocbss");
14456 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14457 s = bfd_get_section_by_name (obfd, ".plt");
14458 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14459 {
14460 /* This may happen for
14461 o references to TOC base (SYM@toc / TOC[tc0]) without a
14462 .toc directive
14463 o bad linker script
14464 o --gc-sections and empty TOC sections
14465
14466 FIXME: Warn user? */
14467
14468 /* Look for a likely section. We probably won't even be
14469 using TOCstart. */
14470 for (s = obfd->sections; s != NULL; s = s->next)
14471 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14472 | SEC_EXCLUDE))
14473 == (SEC_ALLOC | SEC_SMALL_DATA))
14474 break;
14475 if (s == NULL)
14476 for (s = obfd->sections; s != NULL; s = s->next)
14477 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14478 == (SEC_ALLOC | SEC_SMALL_DATA))
14479 break;
14480 if (s == NULL)
14481 for (s = obfd->sections; s != NULL; s = s->next)
14482 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14483 == SEC_ALLOC)
14484 break;
14485 if (s == NULL)
14486 for (s = obfd->sections; s != NULL; s = s->next)
14487 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14488 break;
14489 }
14490
14491 TOCstart = 0;
14492 if (s != NULL)
14493 TOCstart = s->output_section->vma + s->output_offset;
14494
14495 /* Force alignment. */
14496 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14497 TOCstart -= adjust;
14498 _bfd_set_gp_value (obfd, TOCstart);
14499
14500 if (info != NULL && s != NULL)
14501 {
14502 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14503
14504 if (htab != NULL)
14505 {
14506 if (htab->elf.hgot != NULL)
14507 {
14508 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14509 htab->elf.hgot->root.u.def.section = s;
14510 }
14511 }
14512 else
14513 {
14514 struct bfd_link_hash_entry *bh = NULL;
14515 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14516 s, TOC_BASE_OFF - adjust,
14517 NULL, false, false, &bh);
14518 }
14519 }
14520 return TOCstart;
14521 }
14522
14523 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14524 write out any global entry stubs, and PLT relocations. */
14525
14526 static bool
14527 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14528 {
14529 struct bfd_link_info *info;
14530 struct ppc_link_hash_table *htab;
14531 struct plt_entry *ent;
14532 asection *s;
14533
14534 if (h->root.type == bfd_link_hash_indirect)
14535 return true;
14536
14537 info = inf;
14538 htab = ppc_hash_table (info);
14539 if (htab == NULL)
14540 return false;
14541
14542 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14543 if (ent->plt.offset != (bfd_vma) -1)
14544 {
14545 /* This symbol has an entry in the procedure linkage
14546 table. Set it up. */
14547 Elf_Internal_Rela rela;
14548 asection *plt, *relplt;
14549 bfd_byte *loc;
14550
14551 if (use_local_plt (info, h))
14552 {
14553 if (!(h->def_regular
14554 && (h->root.type == bfd_link_hash_defined
14555 || h->root.type == bfd_link_hash_defweak)))
14556 continue;
14557 if (h->type == STT_GNU_IFUNC)
14558 {
14559 plt = htab->elf.iplt;
14560 relplt = htab->elf.irelplt;
14561 htab->elf.ifunc_resolvers = true;
14562 if (htab->opd_abi)
14563 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14564 else
14565 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14566 }
14567 else
14568 {
14569 plt = htab->pltlocal;
14570 relplt = NULL;
14571 if (bfd_link_pic (info)
14572 && !(info->enable_dt_relr && !htab->opd_abi))
14573 {
14574 relplt = htab->relpltlocal;
14575 if (htab->opd_abi)
14576 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14577 else
14578 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14579 }
14580 }
14581 rela.r_addend = defined_sym_val (h) + ent->addend;
14582
14583 if (relplt == NULL)
14584 {
14585 loc = plt->contents + ent->plt.offset;
14586 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14587 if (htab->opd_abi)
14588 {
14589 bfd_vma toc = elf_gp (info->output_bfd);
14590 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14591 bfd_put_64 (info->output_bfd, toc, loc + 8);
14592 }
14593 }
14594 else
14595 {
14596 rela.r_offset = (plt->output_section->vma
14597 + plt->output_offset
14598 + ent->plt.offset);
14599 loc = relplt->contents + (relplt->reloc_count++
14600 * sizeof (Elf64_External_Rela));
14601 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14602 }
14603 }
14604 else
14605 {
14606 rela.r_offset = (htab->elf.splt->output_section->vma
14607 + htab->elf.splt->output_offset
14608 + ent->plt.offset);
14609 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14610 rela.r_addend = ent->addend;
14611 loc = (htab->elf.srelplt->contents
14612 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14613 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14614 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14615 htab->elf.ifunc_resolvers = true;
14616 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14617 }
14618 }
14619
14620 if (!h->pointer_equality_needed)
14621 return true;
14622
14623 if (h->def_regular)
14624 return true;
14625
14626 s = htab->global_entry;
14627 if (s == NULL || s->size == 0)
14628 return true;
14629
14630 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14631 if (ent->plt.offset != (bfd_vma) -1
14632 && ent->addend == 0)
14633 {
14634 bfd_byte *p;
14635 asection *plt;
14636 bfd_vma off;
14637
14638 p = s->contents + h->root.u.def.value;
14639 plt = htab->elf.splt;
14640 if (use_local_plt (info, h))
14641 {
14642 if (h->type == STT_GNU_IFUNC)
14643 plt = htab->elf.iplt;
14644 else
14645 plt = htab->pltlocal;
14646 }
14647 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14648 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14649
14650 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14651 {
14652 info->callbacks->einfo
14653 (_("%P: linkage table error against `%pT'\n"),
14654 h->root.root.string);
14655 bfd_set_error (bfd_error_bad_value);
14656 htab->stub_error = true;
14657 }
14658
14659 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14660 if (htab->params->emit_stub_syms)
14661 {
14662 size_t len = strlen (h->root.root.string);
14663 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14664
14665 if (name == NULL)
14666 return false;
14667
14668 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14669 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14670 if (h == NULL)
14671 return false;
14672 if (h->root.type == bfd_link_hash_new)
14673 {
14674 h->root.type = bfd_link_hash_defined;
14675 h->root.u.def.section = s;
14676 h->root.u.def.value = p - s->contents;
14677 h->ref_regular = 1;
14678 h->def_regular = 1;
14679 h->ref_regular_nonweak = 1;
14680 h->forced_local = 1;
14681 h->non_elf = 0;
14682 h->root.linker_def = 1;
14683 }
14684 }
14685
14686 if (PPC_HA (off) != 0)
14687 {
14688 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14689 p += 4;
14690 }
14691 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14692 p += 4;
14693 bfd_put_32 (s->owner, MTCTR_R12, p);
14694 p += 4;
14695 bfd_put_32 (s->owner, BCTR, p);
14696 break;
14697 }
14698 return true;
14699 }
14700
14701 /* Write PLT relocs for locals. */
14702
14703 static bool
14704 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14705 {
14706 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14707 bfd *ibfd;
14708
14709 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14710 {
14711 struct got_entry **lgot_ents, **end_lgot_ents;
14712 struct plt_entry **local_plt, **lplt, **end_local_plt;
14713 Elf_Internal_Shdr *symtab_hdr;
14714 bfd_size_type locsymcount;
14715 Elf_Internal_Sym *local_syms = NULL;
14716 struct plt_entry *ent;
14717
14718 if (!is_ppc64_elf (ibfd))
14719 continue;
14720
14721 lgot_ents = elf_local_got_ents (ibfd);
14722 if (!lgot_ents)
14723 continue;
14724
14725 symtab_hdr = &elf_symtab_hdr (ibfd);
14726 locsymcount = symtab_hdr->sh_info;
14727 end_lgot_ents = lgot_ents + locsymcount;
14728 local_plt = (struct plt_entry **) end_lgot_ents;
14729 end_local_plt = local_plt + locsymcount;
14730 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14731 for (ent = *lplt; ent != NULL; ent = ent->next)
14732 if (ent->plt.offset != (bfd_vma) -1)
14733 {
14734 Elf_Internal_Sym *sym;
14735 asection *sym_sec;
14736 asection *plt, *relplt;
14737 bfd_byte *loc;
14738 bfd_vma val;
14739
14740 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14741 lplt - local_plt, ibfd))
14742 {
14743 if (symtab_hdr->contents != (unsigned char *) local_syms)
14744 free (local_syms);
14745 return false;
14746 }
14747
14748 val = sym->st_value + ent->addend;
14749 if (sym_sec != NULL && sym_sec->output_section != NULL)
14750 val += sym_sec->output_offset + sym_sec->output_section->vma;
14751
14752 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14753 {
14754 htab->elf.ifunc_resolvers = true;
14755 plt = htab->elf.iplt;
14756 relplt = htab->elf.irelplt;
14757 }
14758 else
14759 {
14760 plt = htab->pltlocal;
14761 relplt = NULL;
14762 if (bfd_link_pic (info)
14763 && !(info->enable_dt_relr && !htab->opd_abi))
14764 relplt = htab->relpltlocal;
14765 }
14766
14767 if (relplt == NULL)
14768 {
14769 loc = plt->contents + ent->plt.offset;
14770 bfd_put_64 (info->output_bfd, val, loc);
14771 if (htab->opd_abi)
14772 {
14773 bfd_vma toc = elf_gp (ibfd);
14774 bfd_put_64 (info->output_bfd, toc, loc + 8);
14775 }
14776 }
14777 else
14778 {
14779 Elf_Internal_Rela rela;
14780 rela.r_offset = (ent->plt.offset
14781 + plt->output_offset
14782 + plt->output_section->vma);
14783 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14784 {
14785 if (htab->opd_abi)
14786 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14787 else
14788 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14789 }
14790 else
14791 {
14792 if (htab->opd_abi)
14793 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14794 else
14795 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14796 }
14797 rela.r_addend = val;
14798 loc = relplt->contents + (relplt->reloc_count++
14799 * sizeof (Elf64_External_Rela));
14800 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14801 }
14802 }
14803
14804 if (local_syms != NULL
14805 && symtab_hdr->contents != (unsigned char *) local_syms)
14806 {
14807 if (!info->keep_memory)
14808 free (local_syms);
14809 else
14810 symtab_hdr->contents = (unsigned char *) local_syms;
14811 }
14812 }
14813 return true;
14814 }
14815
14816 /* Emit the static wrapper function preserving registers around a
14817 __tls_get_addr_opt call. */
14818
14819 static bool
14820 emit_tga_desc (struct ppc_link_hash_table *htab)
14821 {
14822 asection *stub_sec = htab->tga_group->stub_sec;
14823 unsigned int cfa_updt = 11 * 4;
14824 bfd_byte *p;
14825 bfd_vma to, from, delta;
14826
14827 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14828 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14829 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14830 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14831 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14832 delta = to - from;
14833 if (delta + (1 << 25) >= 1 << 26)
14834 {
14835 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14836 htab->stub_error = true;
14837 return false;
14838 }
14839
14840 p = stub_sec->contents;
14841 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14842 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14843 p += 4;
14844 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14845 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14846 }
14847
14848 /* Emit eh_frame describing the static wrapper function. */
14849
14850 static bfd_byte *
14851 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14852 {
14853 unsigned int cfa_updt = 11 * 4;
14854 unsigned int i;
14855
14856 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14857 *p++ = DW_CFA_def_cfa_offset;
14858 if (htab->opd_abi)
14859 {
14860 *p++ = 128;
14861 *p++ = 1;
14862 }
14863 else
14864 *p++ = 96;
14865 *p++ = DW_CFA_offset_extended_sf;
14866 *p++ = 65;
14867 *p++ = (-16 / 8) & 0x7f;
14868 for (i = 4; i < 12; i++)
14869 {
14870 *p++ = DW_CFA_offset + i;
14871 *p++ = (htab->opd_abi ? 13 : 12) - i;
14872 }
14873 *p++ = DW_CFA_advance_loc + 10;
14874 *p++ = DW_CFA_def_cfa_offset;
14875 *p++ = 0;
14876 for (i = 4; i < 12; i++)
14877 *p++ = DW_CFA_restore + i;
14878 *p++ = DW_CFA_advance_loc + 2;
14879 *p++ = DW_CFA_restore_extended;
14880 *p++ = 65;
14881 return p;
14882 }
14883
14884 /* Build all the stubs associated with the current output file.
14885 The stubs are kept in a hash table attached to the main linker
14886 hash table. This function is called via gldelf64ppc_finish. */
14887
14888 bool
14889 ppc64_elf_build_stubs (struct bfd_link_info *info,
14890 char **stats)
14891 {
14892 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14893 struct map_stub *group;
14894 asection *stub_sec;
14895 bfd_byte *p;
14896 int stub_sec_count = 0;
14897
14898 if (htab == NULL)
14899 return false;
14900
14901 /* Allocate memory to hold the linker stubs. */
14902 for (group = htab->group; group != NULL; group = group->next)
14903 {
14904 group->eh_size = 0;
14905 group->lr_restore = 0;
14906 if ((stub_sec = group->stub_sec) != NULL
14907 && stub_sec->size != 0)
14908 {
14909 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14910 stub_sec->size);
14911 if (stub_sec->contents == NULL)
14912 return false;
14913 stub_sec->size = 0;
14914 }
14915 }
14916
14917 if (htab->glink != NULL && htab->glink->size != 0)
14918 {
14919 unsigned int indx;
14920 bfd_vma plt0;
14921
14922 /* Build the .glink plt call stub. */
14923 if (htab->params->emit_stub_syms)
14924 {
14925 struct elf_link_hash_entry *h;
14926 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14927 true, false, false);
14928 if (h == NULL)
14929 return false;
14930 if (h->root.type == bfd_link_hash_new)
14931 {
14932 h->root.type = bfd_link_hash_defined;
14933 h->root.u.def.section = htab->glink;
14934 h->root.u.def.value = 8;
14935 h->ref_regular = 1;
14936 h->def_regular = 1;
14937 h->ref_regular_nonweak = 1;
14938 h->forced_local = 1;
14939 h->non_elf = 0;
14940 h->root.linker_def = 1;
14941 }
14942 }
14943 plt0 = (htab->elf.splt->output_section->vma
14944 + htab->elf.splt->output_offset
14945 - 16);
14946 if (info->emitrelocations)
14947 {
14948 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14949 if (r == NULL)
14950 return false;
14951 r->r_offset = (htab->glink->output_offset
14952 + htab->glink->output_section->vma);
14953 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14954 r->r_addend = plt0;
14955 }
14956 p = htab->glink->contents;
14957 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14958 bfd_put_64 (htab->glink->owner, plt0, p);
14959 p += 8;
14960 if (htab->opd_abi)
14961 {
14962 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14963 p += 4;
14964 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14965 p += 4;
14966 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14967 p += 4;
14968 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14969 p += 4;
14970 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14971 p += 4;
14972 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14973 p += 4;
14974 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14975 p += 4;
14976 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14977 p += 4;
14978 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14979 p += 4;
14980 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14981 p += 4;
14982 }
14983 else
14984 {
14985 unsigned int insn;
14986
14987 /* 0:
14988 . .quad plt0-1f # plt0 entry relative to 1:
14989 #
14990 # We get here with r12 initially @ a glink branch
14991 # Load the address of _dl_runtime_resolve from plt0 and
14992 # jump to it, with r0 set to the index of the PLT entry
14993 # to be resolved and r11 the link map.
14994 __glink_PLTresolve:
14995 . std %r2,24(%r1) # optional
14996 . mflr %r0
14997 . bcl 20,31,1f
14998 1:
14999 . mflr %r11
15000 . mtlr %r0
15001 . ld %r0,(0b-1b)(%r11)
15002 . sub %r12,%r12,%r11
15003 . add %r11,%r0,%r11
15004 . addi %r0,%r12,1b-2f
15005 . ld %r12,0(%r11)
15006 . srdi %r0,%r0,2
15007 . mtctr %r12
15008 . ld %r11,8(%r11)
15009 . bctr
15010 2:
15011 . b __glink_PLTresolve
15012 . ...
15013 . b __glink_PLTresolve */
15014
15015 if (htab->has_plt_localentry0)
15016 {
15017 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
15018 p += 4;
15019 }
15020 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
15021 p += 4;
15022 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
15023 p += 4;
15024 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
15025 p += 4;
15026 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
15027 p += 4;
15028 if (htab->has_plt_localentry0)
15029 insn = LD_R0_0R11 | (-20 & 0xfffc);
15030 else
15031 insn = LD_R0_0R11 | (-16 & 0xfffc);
15032 bfd_put_32 (htab->glink->owner, insn, p);
15033 p += 4;
15034 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
15035 p += 4;
15036 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
15037 p += 4;
15038 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
15039 p += 4;
15040 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
15041 p += 4;
15042 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
15043 p += 4;
15044 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
15045 p += 4;
15046 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
15047 p += 4;
15048 }
15049 bfd_put_32 (htab->glink->owner, BCTR, p);
15050 p += 4;
15051 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
15052
15053 /* Build the .glink lazy link call stubs. */
15054 indx = 0;
15055 while (p < htab->glink->contents + htab->glink->size)
15056 {
15057 if (htab->opd_abi)
15058 {
15059 if (indx < 0x8000)
15060 {
15061 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
15062 p += 4;
15063 }
15064 else
15065 {
15066 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
15067 p += 4;
15068 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
15069 p);
15070 p += 4;
15071 }
15072 }
15073 bfd_put_32 (htab->glink->owner,
15074 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
15075 indx++;
15076 p += 4;
15077 }
15078 }
15079
15080 if (htab->tga_group != NULL)
15081 {
15082 htab->tga_group->lr_restore = 23 * 4;
15083 htab->tga_group->stub_sec->size = 24 * 4;
15084 if (!emit_tga_desc (htab))
15085 return false;
15086 if (htab->glink_eh_frame != NULL
15087 && htab->glink_eh_frame->size != 0)
15088 {
15089 size_t align = 4;
15090
15091 p = htab->glink_eh_frame->contents;
15092 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15093 p += 17;
15094 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
15095 }
15096 }
15097
15098 /* Build .glink global entry stubs, and PLT relocs for globals. */
15099 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
15100
15101 if (!write_plt_relocs_for_local_syms (info))
15102 return false;
15103
15104 if (htab->brlt != NULL && htab->brlt->size != 0)
15105 {
15106 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
15107 htab->brlt->size);
15108 if (htab->brlt->contents == NULL)
15109 return false;
15110 }
15111 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
15112 {
15113 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
15114 htab->relbrlt->size);
15115 if (htab->relbrlt->contents == NULL)
15116 return false;
15117 }
15118
15119 /* Build the stubs as directed by the stub hash table. */
15120 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
15121
15122 for (group = htab->group; group != NULL; group = group->next)
15123 if (group->needs_save_res)
15124 group->stub_sec->size += htab->sfpr->size;
15125
15126 if (htab->relbrlt != NULL)
15127 htab->relbrlt->reloc_count = 0;
15128
15129 if (htab->params->plt_stub_align != 0)
15130 for (group = htab->group; group != NULL; group = group->next)
15131 if ((stub_sec = group->stub_sec) != NULL)
15132 {
15133 int align = abs (htab->params->plt_stub_align);
15134 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
15135 }
15136
15137 for (group = htab->group; group != NULL; group = group->next)
15138 if (group->needs_save_res)
15139 {
15140 stub_sec = group->stub_sec;
15141 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
15142 htab->sfpr->contents, htab->sfpr->size);
15143 if (htab->params->emit_stub_syms)
15144 {
15145 unsigned int i;
15146
15147 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
15148 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
15149 return false;
15150 }
15151 }
15152
15153 if (htab->glink_eh_frame != NULL
15154 && htab->glink_eh_frame->size != 0)
15155 {
15156 bfd_vma val;
15157 size_t align = 4;
15158
15159 p = htab->glink_eh_frame->contents;
15160 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
15161
15162 for (group = htab->group; group != NULL; group = group->next)
15163 if (group->eh_size != 0)
15164 {
15165 /* Offset to stub section. */
15166 val = (group->stub_sec->output_section->vma
15167 + group->stub_sec->output_offset);
15168 val -= (htab->glink_eh_frame->output_section->vma
15169 + htab->glink_eh_frame->output_offset
15170 + (p + 8 - htab->glink_eh_frame->contents));
15171 if (val + 0x80000000 > 0xffffffff)
15172 {
15173 _bfd_error_handler
15174 (_("%s offset too large for .eh_frame sdata4 encoding"),
15175 group->stub_sec->name);
15176 return false;
15177 }
15178 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15179 p += (group->eh_size + 17 + 3) & -4;
15180 }
15181 if (htab->glink != NULL && htab->glink->size != 0)
15182 {
15183 /* Offset to .glink. */
15184 val = (htab->glink->output_section->vma
15185 + htab->glink->output_offset
15186 + 8);
15187 val -= (htab->glink_eh_frame->output_section->vma
15188 + htab->glink_eh_frame->output_offset
15189 + (p + 8 - htab->glink_eh_frame->contents));
15190 if (val + 0x80000000 > 0xffffffff)
15191 {
15192 _bfd_error_handler
15193 (_("%s offset too large for .eh_frame sdata4 encoding"),
15194 htab->glink->name);
15195 return false;
15196 }
15197 bfd_put_32 (htab->elf.dynobj, val, p + 8);
15198 p += (24 + align - 1) & -align;
15199 }
15200 }
15201
15202 if (htab->elf.srelrdyn != NULL && htab->elf.srelrdyn->size != 0)
15203 {
15204 htab->elf.srelrdyn->contents
15205 = bfd_alloc (htab->elf.dynobj, htab->elf.srelrdyn->size);
15206 if (htab->elf.srelrdyn->contents == NULL)
15207 return false;
15208
15209 bfd_vma *relr_addr = sort_relr (htab);
15210 if (htab->relr_count != 0 && relr_addr == NULL)
15211 return false;
15212
15213 size_t i = 0;
15214 bfd_byte *loc = htab->elf.srelrdyn->contents;
15215 while (i < htab->relr_count)
15216 {
15217 bfd_vma base = relr_addr[i];
15218 BFD_ASSERT (base % 2 == 0);
15219 bfd_put_64 (htab->elf.dynobj, base, loc);
15220 loc += 8;
15221 i++;
15222 while (i < htab->relr_count
15223 && relr_addr[i] == base)
15224 {
15225 htab->stub_error = true;
15226 i++;
15227 }
15228 base += 8;
15229 while (1)
15230 {
15231 bfd_vma bits = 0;
15232 while (i < htab->relr_count
15233 && relr_addr[i] - base < 63 * 8
15234 && (relr_addr[i] - base) % 8 == 0)
15235 {
15236 bits |= (bfd_vma) 1 << ((relr_addr[i] - base) / 8);
15237 i++;
15238 }
15239 if (bits == 0)
15240 break;
15241 bfd_put_64 (htab->elf.dynobj, (bits << 1) | 1, loc);
15242 loc += 8;
15243 base += 63 * 8;
15244 }
15245 }
15246 free (relr_addr);
15247 /* Pad any excess with 1's, a do-nothing encoding. */
15248 while ((size_t) (loc - htab->elf.srelrdyn->contents)
15249 < htab->elf.srelrdyn->size)
15250 {
15251 bfd_put_64 (htab->elf.dynobj, 1, loc);
15252 loc += 8;
15253 }
15254 }
15255
15256 for (group = htab->group; group != NULL; group = group->next)
15257 if ((stub_sec = group->stub_sec) != NULL)
15258 {
15259 stub_sec_count += 1;
15260 if (stub_sec->rawsize != stub_sec->size
15261 && (htab->stub_iteration <= STUB_SHRINK_ITER
15262 || stub_sec->rawsize < stub_sec->size))
15263 break;
15264 }
15265
15266 if (group != NULL)
15267 htab->stub_error = true;
15268
15269 if (htab->stub_error)
15270 {
15271 _bfd_error_handler (_("stubs don't match calculated size"));
15272 return false;
15273 }
15274
15275 if (stats != NULL)
15276 {
15277 char *groupmsg;
15278 if (asprintf (&groupmsg,
15279 ngettext ("linker stubs in %u group\n",
15280 "linker stubs in %u groups\n",
15281 stub_sec_count),
15282 stub_sec_count) < 0)
15283 *stats = NULL;
15284 else
15285 {
15286 if (asprintf (stats, _("%s"
15287 " branch %lu\n"
15288 " long branch %lu\n"
15289 " plt call %lu\n"
15290 " global entry %lu"),
15291 groupmsg,
15292 htab->stub_count[ppc_stub_long_branch - 1],
15293 htab->stub_count[ppc_stub_plt_branch - 1],
15294 htab->stub_count[ppc_stub_plt_call - 1],
15295 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
15296 *stats = NULL;
15297 free (groupmsg);
15298 }
15299 }
15300 return true;
15301 }
15302
15303 /* What to do when ld finds relocations against symbols defined in
15304 discarded sections. */
15305
15306 static unsigned int
15307 ppc64_elf_action_discarded (asection *sec)
15308 {
15309 if (strcmp (".opd", sec->name) == 0)
15310 return 0;
15311
15312 if (strcmp (".toc", sec->name) == 0)
15313 return 0;
15314
15315 if (strcmp (".toc1", sec->name) == 0)
15316 return 0;
15317
15318 return _bfd_elf_default_action_discarded (sec);
15319 }
15320
15321 /* These are the dynamic relocations supported by glibc. */
15322
15323 static bool
15324 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
15325 {
15326 switch (r_type)
15327 {
15328 case R_PPC64_RELATIVE:
15329 case R_PPC64_NONE:
15330 case R_PPC64_ADDR64:
15331 case R_PPC64_GLOB_DAT:
15332 case R_PPC64_IRELATIVE:
15333 case R_PPC64_JMP_IREL:
15334 case R_PPC64_JMP_SLOT:
15335 case R_PPC64_DTPMOD64:
15336 case R_PPC64_DTPREL64:
15337 case R_PPC64_TPREL64:
15338 case R_PPC64_TPREL16_LO_DS:
15339 case R_PPC64_TPREL16_DS:
15340 case R_PPC64_TPREL16:
15341 case R_PPC64_TPREL16_LO:
15342 case R_PPC64_TPREL16_HI:
15343 case R_PPC64_TPREL16_HIGH:
15344 case R_PPC64_TPREL16_HA:
15345 case R_PPC64_TPREL16_HIGHA:
15346 case R_PPC64_TPREL16_HIGHER:
15347 case R_PPC64_TPREL16_HIGHEST:
15348 case R_PPC64_TPREL16_HIGHERA:
15349 case R_PPC64_TPREL16_HIGHESTA:
15350 case R_PPC64_ADDR16_LO_DS:
15351 case R_PPC64_ADDR16_LO:
15352 case R_PPC64_ADDR16_HI:
15353 case R_PPC64_ADDR16_HIGH:
15354 case R_PPC64_ADDR16_HA:
15355 case R_PPC64_ADDR16_HIGHA:
15356 case R_PPC64_REL30:
15357 case R_PPC64_COPY:
15358 case R_PPC64_UADDR64:
15359 case R_PPC64_UADDR32:
15360 case R_PPC64_ADDR32:
15361 case R_PPC64_ADDR24:
15362 case R_PPC64_ADDR16:
15363 case R_PPC64_UADDR16:
15364 case R_PPC64_ADDR16_DS:
15365 case R_PPC64_ADDR16_HIGHER:
15366 case R_PPC64_ADDR16_HIGHEST:
15367 case R_PPC64_ADDR16_HIGHERA:
15368 case R_PPC64_ADDR16_HIGHESTA:
15369 case R_PPC64_ADDR14:
15370 case R_PPC64_ADDR14_BRTAKEN:
15371 case R_PPC64_ADDR14_BRNTAKEN:
15372 case R_PPC64_REL32:
15373 case R_PPC64_REL64:
15374 return true;
15375
15376 default:
15377 return false;
15378 }
15379 }
15380
15381 /* The RELOCATE_SECTION function is called by the ELF backend linker
15382 to handle the relocations for a section.
15383
15384 The relocs are always passed as Rela structures; if the section
15385 actually uses Rel structures, the r_addend field will always be
15386 zero.
15387
15388 This function is responsible for adjust the section contents as
15389 necessary, and (if using Rela relocs and generating a
15390 relocatable output file) adjusting the reloc addend as
15391 necessary.
15392
15393 This function does not have to worry about setting the reloc
15394 address or the reloc symbol index.
15395
15396 LOCAL_SYMS is a pointer to the swapped in local symbols.
15397
15398 LOCAL_SECTIONS is an array giving the section in the input file
15399 corresponding to the st_shndx field of each local symbol.
15400
15401 The global hash table entry for the global symbols can be found
15402 via elf_sym_hashes (input_bfd).
15403
15404 When generating relocatable output, this function must handle
15405 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
15406 going to be the section symbol corresponding to the output
15407 section, which means that the addend must be adjusted
15408 accordingly. */
15409
15410 static int
15411 ppc64_elf_relocate_section (bfd *output_bfd,
15412 struct bfd_link_info *info,
15413 bfd *input_bfd,
15414 asection *input_section,
15415 bfd_byte *contents,
15416 Elf_Internal_Rela *relocs,
15417 Elf_Internal_Sym *local_syms,
15418 asection **local_sections)
15419 {
15420 struct ppc_link_hash_table *htab;
15421 Elf_Internal_Shdr *symtab_hdr;
15422 struct elf_link_hash_entry **sym_hashes;
15423 Elf_Internal_Rela *rel;
15424 Elf_Internal_Rela *wrel;
15425 Elf_Internal_Rela *relend;
15426 Elf_Internal_Rela outrel;
15427 bfd_byte *loc;
15428 struct got_entry **local_got_ents;
15429 bfd_vma TOCstart;
15430 bool ret = true;
15431 bool is_opd;
15432 /* Assume 'at' branch hints. */
15433 bool is_isa_v2 = true;
15434 bool warned_dynamic = false;
15435 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
15436
15437 /* Initialize howto table if needed. */
15438 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
15439 ppc_howto_init ();
15440
15441 htab = ppc_hash_table (info);
15442 if (htab == NULL)
15443 return false;
15444
15445 /* Don't relocate stub sections. */
15446 if (input_section->owner == htab->params->stub_bfd)
15447 return true;
15448
15449 if (!is_ppc64_elf (input_bfd))
15450 {
15451 bfd_set_error (bfd_error_wrong_format);
15452 return false;
15453 }
15454
15455 local_got_ents = elf_local_got_ents (input_bfd);
15456 TOCstart = elf_gp (output_bfd);
15457 symtab_hdr = &elf_symtab_hdr (input_bfd);
15458 sym_hashes = elf_sym_hashes (input_bfd);
15459 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
15460
15461 rel = wrel = relocs;
15462 relend = relocs + input_section->reloc_count;
15463 for (; rel < relend; wrel++, rel++)
15464 {
15465 enum elf_ppc64_reloc_type r_type;
15466 bfd_vma addend;
15467 bfd_reloc_status_type r;
15468 Elf_Internal_Sym *sym;
15469 asection *sec;
15470 struct elf_link_hash_entry *h_elf;
15471 struct ppc_link_hash_entry *h;
15472 struct ppc_link_hash_entry *fdh;
15473 const char *sym_name;
15474 unsigned long r_symndx, toc_symndx;
15475 bfd_vma toc_addend;
15476 unsigned char tls_mask, tls_gd, tls_type;
15477 unsigned char sym_type;
15478 bfd_vma relocation;
15479 bool unresolved_reloc, save_unresolved_reloc;
15480 bool warned;
15481 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
15482 unsigned int insn;
15483 unsigned int mask;
15484 struct ppc_stub_hash_entry *stub_entry;
15485 bfd_vma max_br_offset;
15486 bfd_vma from;
15487 Elf_Internal_Rela orig_rel;
15488 reloc_howto_type *howto;
15489 struct reloc_howto_struct alt_howto;
15490 uint64_t pinsn;
15491 bfd_vma offset;
15492
15493 again:
15494 orig_rel = *rel;
15495
15496 r_type = ELF64_R_TYPE (rel->r_info);
15497 r_symndx = ELF64_R_SYM (rel->r_info);
15498
15499 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
15500 symbol of the previous ADDR64 reloc. The symbol gives us the
15501 proper TOC base to use. */
15502 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
15503 && wrel != relocs
15504 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15505 && is_opd)
15506 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15507
15508 sym = NULL;
15509 sec = NULL;
15510 h_elf = NULL;
15511 sym_name = NULL;
15512 unresolved_reloc = false;
15513 warned = false;
15514
15515 if (r_symndx < symtab_hdr->sh_info)
15516 {
15517 /* It's a local symbol. */
15518 struct _opd_sec_data *opd;
15519
15520 sym = local_syms + r_symndx;
15521 sec = local_sections[r_symndx];
15522 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15523 sym_type = ELF64_ST_TYPE (sym->st_info);
15524 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15525 opd = get_opd_info (sec);
15526 if (opd != NULL && opd->adjust != NULL)
15527 {
15528 long adjust = opd->adjust[OPD_NDX (sym->st_value
15529 + rel->r_addend)];
15530 if (adjust == -1)
15531 relocation = 0;
15532 else
15533 {
15534 /* If this is a relocation against the opd section sym
15535 and we have edited .opd, adjust the reloc addend so
15536 that ld -r and ld --emit-relocs output is correct.
15537 If it is a reloc against some other .opd symbol,
15538 then the symbol value will be adjusted later. */
15539 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15540 rel->r_addend += adjust;
15541 else
15542 relocation += adjust;
15543 }
15544 }
15545 }
15546 else
15547 {
15548 bool ignored;
15549
15550 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15551 r_symndx, symtab_hdr, sym_hashes,
15552 h_elf, sec, relocation,
15553 unresolved_reloc, warned, ignored);
15554 sym_name = h_elf->root.root.string;
15555 sym_type = h_elf->type;
15556 if (sec != NULL
15557 && sec->owner == output_bfd
15558 && strcmp (sec->name, ".opd") == 0)
15559 {
15560 /* This is a symbol defined in a linker script. All
15561 such are defined in output sections, even those
15562 defined by simple assignment from a symbol defined in
15563 an input section. Transfer the symbol to an
15564 appropriate input .opd section, so that a branch to
15565 this symbol will be mapped to the location specified
15566 by the opd entry. */
15567 struct bfd_link_order *lo;
15568 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15569 if (lo->type == bfd_indirect_link_order)
15570 {
15571 asection *isec = lo->u.indirect.section;
15572 if (h_elf->root.u.def.value >= isec->output_offset
15573 && h_elf->root.u.def.value < (isec->output_offset
15574 + isec->size))
15575 {
15576 h_elf->root.u.def.value -= isec->output_offset;
15577 h_elf->root.u.def.section = isec;
15578 sec = isec;
15579 break;
15580 }
15581 }
15582 }
15583 }
15584 h = ppc_elf_hash_entry (h_elf);
15585
15586 if (sec != NULL && discarded_section (sec))
15587 {
15588 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15589 input_bfd, input_section,
15590 contents, rel->r_offset);
15591 wrel->r_offset = rel->r_offset;
15592 wrel->r_info = 0;
15593 wrel->r_addend = 0;
15594
15595 /* For ld -r, remove relocations in debug sections against
15596 symbols defined in discarded sections. Not done for
15597 non-debug to preserve relocs in .eh_frame which the
15598 eh_frame editing code expects to be present. */
15599 if (bfd_link_relocatable (info)
15600 && (input_section->flags & SEC_DEBUGGING))
15601 wrel--;
15602
15603 continue;
15604 }
15605
15606 if (bfd_link_relocatable (info))
15607 goto copy_reloc;
15608
15609 if (h != NULL && &h->elf == htab->elf.hgot)
15610 {
15611 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15612 sec = bfd_abs_section_ptr;
15613 unresolved_reloc = false;
15614 }
15615
15616 /* TLS optimizations. Replace instruction sequences and relocs
15617 based on information we collected in tls_optimize. We edit
15618 RELOCS so that --emit-relocs will output something sensible
15619 for the final instruction stream. */
15620 tls_mask = 0;
15621 tls_gd = 0;
15622 toc_symndx = 0;
15623 if (h != NULL)
15624 tls_mask = h->tls_mask;
15625 else if (local_got_ents != NULL)
15626 {
15627 struct plt_entry **local_plt = (struct plt_entry **)
15628 (local_got_ents + symtab_hdr->sh_info);
15629 unsigned char *lgot_masks = (unsigned char *)
15630 (local_plt + symtab_hdr->sh_info);
15631 tls_mask = lgot_masks[r_symndx];
15632 }
15633 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15634 && (r_type == R_PPC64_TLS
15635 || r_type == R_PPC64_TLSGD
15636 || r_type == R_PPC64_TLSLD))
15637 {
15638 /* Check for toc tls entries. */
15639 unsigned char *toc_tls;
15640
15641 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15642 &local_syms, rel, input_bfd))
15643 return false;
15644
15645 if (toc_tls)
15646 tls_mask = *toc_tls;
15647 }
15648
15649 /* Check that tls relocs are used with tls syms, and non-tls
15650 relocs are used with non-tls syms. */
15651 if (r_symndx != STN_UNDEF
15652 && r_type != R_PPC64_NONE
15653 && (h == NULL
15654 || h->elf.root.type == bfd_link_hash_defined
15655 || h->elf.root.type == bfd_link_hash_defweak)
15656 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15657 {
15658 if ((tls_mask & TLS_TLS) != 0
15659 && (r_type == R_PPC64_TLS
15660 || r_type == R_PPC64_TLSGD
15661 || r_type == R_PPC64_TLSLD))
15662 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15663 ;
15664 else
15665 info->callbacks->einfo
15666 (!IS_PPC64_TLS_RELOC (r_type)
15667 /* xgettext:c-format */
15668 ? _("%H: %s used with TLS symbol `%pT'\n")
15669 /* xgettext:c-format */
15670 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15671 input_bfd, input_section, rel->r_offset,
15672 ppc64_elf_howto_table[r_type]->name,
15673 sym_name);
15674 }
15675
15676 /* Ensure reloc mapping code below stays sane. */
15677 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15678 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15679 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15680 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15681 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15682 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15683 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15684 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15685 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15686 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15687 abort ();
15688
15689 switch (r_type)
15690 {
15691 default:
15692 break;
15693
15694 case R_PPC64_LO_DS_OPT:
15695 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15696 {
15697 insn = bfd_get_32 (input_bfd,
15698 contents + rel->r_offset - d_offset);
15699 if ((insn & (0x3fu << 26)) != 58u << 26)
15700 abort ();
15701 insn += (14u << 26) - (58u << 26);
15702 bfd_put_32 (input_bfd, insn,
15703 contents + rel->r_offset - d_offset);
15704 r_type = R_PPC64_TOC16_LO;
15705 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15706 }
15707 break;
15708
15709 case R_PPC64_TOC16:
15710 case R_PPC64_TOC16_LO:
15711 case R_PPC64_TOC16_DS:
15712 case R_PPC64_TOC16_LO_DS:
15713 {
15714 /* Check for toc tls entries. */
15715 unsigned char *toc_tls;
15716 int retval;
15717
15718 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15719 &local_syms, rel, input_bfd);
15720 if (retval == 0)
15721 return false;
15722
15723 if (toc_tls)
15724 {
15725 tls_mask = *toc_tls;
15726 if (r_type == R_PPC64_TOC16_DS
15727 || r_type == R_PPC64_TOC16_LO_DS)
15728 {
15729 if ((tls_mask & TLS_TLS) != 0
15730 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15731 goto toctprel;
15732 }
15733 else
15734 {
15735 /* If we found a GD reloc pair, then we might be
15736 doing a GD->IE transition. */
15737 if (retval == 2)
15738 {
15739 tls_gd = TLS_GDIE;
15740 if ((tls_mask & TLS_TLS) != 0
15741 && (tls_mask & TLS_GD) == 0)
15742 goto tls_ldgd_opt;
15743 }
15744 else if (retval == 3)
15745 {
15746 if ((tls_mask & TLS_TLS) != 0
15747 && (tls_mask & TLS_LD) == 0)
15748 goto tls_ldgd_opt;
15749 }
15750 }
15751 }
15752 }
15753 break;
15754
15755 case R_PPC64_GOT_TPREL16_HI:
15756 case R_PPC64_GOT_TPREL16_HA:
15757 if ((tls_mask & TLS_TLS) != 0
15758 && (tls_mask & TLS_TPREL) == 0
15759 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15760 {
15761 rel->r_offset -= d_offset;
15762 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15763 r_type = R_PPC64_NONE;
15764 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15765 }
15766 break;
15767
15768 case R_PPC64_GOT_TPREL16_DS:
15769 case R_PPC64_GOT_TPREL16_LO_DS:
15770 if ((tls_mask & TLS_TLS) != 0
15771 && (tls_mask & TLS_TPREL) == 0
15772 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15773 {
15774 toctprel:
15775 insn = bfd_get_32 (input_bfd,
15776 contents + rel->r_offset - d_offset);
15777 insn &= 31 << 21;
15778 insn |= 0x3c0d0000; /* addis 0,13,0 */
15779 bfd_put_32 (input_bfd, insn,
15780 contents + rel->r_offset - d_offset);
15781 r_type = R_PPC64_TPREL16_HA;
15782 if (toc_symndx != 0)
15783 {
15784 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15785 rel->r_addend = toc_addend;
15786 /* We changed the symbol. Start over in order to
15787 get h, sym, sec etc. right. */
15788 goto again;
15789 }
15790 else
15791 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15792 }
15793 break;
15794
15795 case R_PPC64_GOT_TPREL_PCREL34:
15796 if ((tls_mask & TLS_TLS) != 0
15797 && (tls_mask & TLS_TPREL) == 0
15798 && offset_in_range (input_section, rel->r_offset, 8))
15799 {
15800 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15801 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15802 pinsn <<= 32;
15803 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15804 pinsn += ((2ULL << 56) + (-1ULL << 52)
15805 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15806 bfd_put_32 (input_bfd, pinsn >> 32,
15807 contents + rel->r_offset);
15808 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15809 contents + rel->r_offset + 4);
15810 r_type = R_PPC64_TPREL34;
15811 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15812 }
15813 break;
15814
15815 case R_PPC64_TLS:
15816 if ((tls_mask & TLS_TLS) != 0
15817 && (tls_mask & TLS_TPREL) == 0
15818 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15819 {
15820 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15821 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15822 if (insn == 0)
15823 break;
15824 if ((rel->r_offset & 3) == 0)
15825 {
15826 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15827 /* Was PPC64_TLS which sits on insn boundary, now
15828 PPC64_TPREL16_LO which is at low-order half-word. */
15829 rel->r_offset += d_offset;
15830 r_type = R_PPC64_TPREL16_LO;
15831 if (toc_symndx != 0)
15832 {
15833 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15834 rel->r_addend = toc_addend;
15835 /* We changed the symbol. Start over in order to
15836 get h, sym, sec etc. right. */
15837 goto again;
15838 }
15839 else
15840 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15841 }
15842 else if ((rel->r_offset & 3) == 1)
15843 {
15844 /* For pcrel IE to LE we already have the full
15845 offset and thus don't need an addi here. A nop
15846 or mr will do. */
15847 if ((insn & (0x3fu << 26)) == 14 << 26)
15848 {
15849 /* Extract regs from addi rt,ra,si. */
15850 unsigned int rt = (insn >> 21) & 0x1f;
15851 unsigned int ra = (insn >> 16) & 0x1f;
15852 if (rt == ra)
15853 insn = NOP;
15854 else
15855 {
15856 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15857 insn = (rt << 16) | (ra << 21) | (ra << 11);
15858 insn |= (31u << 26) | (444u << 1);
15859 }
15860 }
15861 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15862 }
15863 }
15864 break;
15865
15866 case R_PPC64_GOT_TLSGD16_HI:
15867 case R_PPC64_GOT_TLSGD16_HA:
15868 tls_gd = TLS_GDIE;
15869 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15870 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15871 goto tls_gdld_hi;
15872 break;
15873
15874 case R_PPC64_GOT_TLSLD16_HI:
15875 case R_PPC64_GOT_TLSLD16_HA:
15876 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15877 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15878 {
15879 tls_gdld_hi:
15880 if ((tls_mask & tls_gd) != 0)
15881 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15882 + R_PPC64_GOT_TPREL16_DS);
15883 else
15884 {
15885 rel->r_offset -= d_offset;
15886 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15887 r_type = R_PPC64_NONE;
15888 }
15889 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15890 }
15891 break;
15892
15893 case R_PPC64_GOT_TLSGD16:
15894 case R_PPC64_GOT_TLSGD16_LO:
15895 tls_gd = TLS_GDIE;
15896 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15897 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15898 goto tls_ldgd_opt;
15899 break;
15900
15901 case R_PPC64_GOT_TLSLD16:
15902 case R_PPC64_GOT_TLSLD16_LO:
15903 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15904 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15905 {
15906 unsigned int insn1, insn2;
15907
15908 tls_ldgd_opt:
15909 offset = (bfd_vma) -1;
15910 /* If not using the newer R_PPC64_TLSGD/LD to mark
15911 __tls_get_addr calls, we must trust that the call
15912 stays with its arg setup insns, ie. that the next
15913 reloc is the __tls_get_addr call associated with
15914 the current reloc. Edit both insns. */
15915 if (input_section->nomark_tls_get_addr
15916 && rel + 1 < relend
15917 && branch_reloc_hash_match (input_bfd, rel + 1,
15918 htab->tls_get_addr_fd,
15919 htab->tga_desc_fd,
15920 htab->tls_get_addr,
15921 htab->tga_desc))
15922 offset = rel[1].r_offset;
15923 /* We read the low GOT_TLS (or TOC16) insn because we
15924 need to keep the destination reg. It may be
15925 something other than the usual r3, and moved to r3
15926 before the call by intervening code. */
15927 insn1 = bfd_get_32 (input_bfd,
15928 contents + rel->r_offset - d_offset);
15929 if ((tls_mask & tls_gd) != 0)
15930 {
15931 /* IE */
15932 insn1 &= (0x1f << 21) | (0x1f << 16);
15933 insn1 |= 58u << 26; /* ld */
15934 insn2 = 0x7c636a14; /* add 3,3,13 */
15935 if (offset != (bfd_vma) -1)
15936 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15937 if (r_type == R_PPC64_TOC16
15938 || r_type == R_PPC64_TOC16_LO)
15939 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15940 else
15941 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15942 + R_PPC64_GOT_TPREL16_DS);
15943 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15944 }
15945 else
15946 {
15947 /* LE */
15948 insn1 &= 0x1f << 21;
15949 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15950 insn2 = 0x38630000; /* addi 3,3,0 */
15951 if (tls_gd == 0)
15952 {
15953 /* Was an LD reloc. */
15954 r_symndx = STN_UNDEF;
15955 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15956 }
15957 else if (toc_symndx != 0)
15958 {
15959 r_symndx = toc_symndx;
15960 rel->r_addend = toc_addend;
15961 }
15962 r_type = R_PPC64_TPREL16_HA;
15963 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15964 if (offset != (bfd_vma) -1)
15965 {
15966 rel[1].r_info = ELF64_R_INFO (r_symndx,
15967 R_PPC64_TPREL16_LO);
15968 rel[1].r_offset = offset + d_offset;
15969 rel[1].r_addend = rel->r_addend;
15970 }
15971 }
15972 bfd_put_32 (input_bfd, insn1,
15973 contents + rel->r_offset - d_offset);
15974 if (offset != (bfd_vma) -1
15975 && offset_in_range (input_section, offset, 4))
15976 {
15977 bfd_put_32 (input_bfd, insn2, contents + offset);
15978 if (offset_in_range (input_section, offset + 4, 4))
15979 {
15980 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15981 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15982 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15983 }
15984 }
15985 if ((tls_mask & tls_gd) == 0
15986 && (tls_gd == 0 || toc_symndx != 0))
15987 {
15988 /* We changed the symbol. Start over in order
15989 to get h, sym, sec etc. right. */
15990 goto again;
15991 }
15992 }
15993 break;
15994
15995 case R_PPC64_GOT_TLSGD_PCREL34:
15996 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15997 && offset_in_range (input_section, rel->r_offset, 8))
15998 {
15999 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16000 pinsn <<= 32;
16001 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16002 if ((tls_mask & TLS_GDIE) != 0)
16003 {
16004 /* IE, pla -> pld */
16005 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
16006 r_type = R_PPC64_GOT_TPREL_PCREL34;
16007 }
16008 else
16009 {
16010 /* LE, pla pcrel -> paddi r13 */
16011 pinsn += (-1ULL << 52) + (13ULL << 16);
16012 r_type = R_PPC64_TPREL34;
16013 }
16014 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16015 bfd_put_32 (input_bfd, pinsn >> 32,
16016 contents + rel->r_offset);
16017 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16018 contents + rel->r_offset + 4);
16019 }
16020 break;
16021
16022 case R_PPC64_GOT_TLSLD_PCREL34:
16023 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16024 && offset_in_range (input_section, rel->r_offset, 8))
16025 {
16026 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16027 pinsn <<= 32;
16028 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16029 pinsn += (-1ULL << 52) + (13ULL << 16);
16030 bfd_put_32 (input_bfd, pinsn >> 32,
16031 contents + rel->r_offset);
16032 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
16033 contents + rel->r_offset + 4);
16034 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16035 r_symndx = STN_UNDEF;
16036 r_type = R_PPC64_TPREL34;
16037 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16038 goto again;
16039 }
16040 break;
16041
16042 case R_PPC64_TLSGD:
16043 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
16044 && rel + 1 < relend
16045 && offset_in_range (input_section, rel->r_offset,
16046 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16047 ? 8 : 4))
16048 {
16049 unsigned int insn2;
16050 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16051
16052 offset = rel->r_offset;
16053 if (is_plt_seq_reloc (r_type1))
16054 {
16055 bfd_put_32 (output_bfd, NOP, contents + offset);
16056 if (r_type1 == R_PPC64_PLT_PCREL34
16057 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16058 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16059 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16060 break;
16061 }
16062
16063 if (r_type1 == R_PPC64_PLTCALL)
16064 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16065
16066 if ((tls_mask & TLS_GDIE) != 0)
16067 {
16068 /* IE */
16069 r_type = R_PPC64_NONE;
16070 insn2 = 0x7c636a14; /* add 3,3,13 */
16071 }
16072 else
16073 {
16074 /* LE */
16075 if (toc_symndx != 0)
16076 {
16077 r_symndx = toc_symndx;
16078 rel->r_addend = toc_addend;
16079 }
16080 if (r_type1 == R_PPC64_REL24_NOTOC
16081 || r_type1 == R_PPC64_REL24_P9NOTOC
16082 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16083 {
16084 r_type = R_PPC64_NONE;
16085 insn2 = NOP;
16086 }
16087 else
16088 {
16089 rel->r_offset = offset + d_offset;
16090 r_type = R_PPC64_TPREL16_LO;
16091 insn2 = 0x38630000; /* addi 3,3,0 */
16092 }
16093 }
16094 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16095 /* Zap the reloc on the _tls_get_addr call too. */
16096 BFD_ASSERT (offset == rel[1].r_offset);
16097 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16098 bfd_put_32 (input_bfd, insn2, contents + offset);
16099 if ((tls_mask & TLS_GDIE) == 0
16100 && toc_symndx != 0
16101 && r_type != R_PPC64_NONE)
16102 goto again;
16103 }
16104 break;
16105
16106 case R_PPC64_TLSLD:
16107 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
16108 && rel + 1 < relend
16109 && offset_in_range (input_section, rel->r_offset,
16110 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
16111 ? 8 : 4))
16112 {
16113 unsigned int insn2;
16114 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
16115
16116 offset = rel->r_offset;
16117 if (is_plt_seq_reloc (r_type1))
16118 {
16119 bfd_put_32 (output_bfd, NOP, contents + offset);
16120 if (r_type1 == R_PPC64_PLT_PCREL34
16121 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
16122 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16123 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16124 break;
16125 }
16126
16127 if (r_type1 == R_PPC64_PLTCALL)
16128 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
16129
16130 if (r_type1 == R_PPC64_REL24_NOTOC
16131 || r_type1 == R_PPC64_REL24_P9NOTOC
16132 || r_type1 == R_PPC64_PLTCALL_NOTOC)
16133 {
16134 r_type = R_PPC64_NONE;
16135 insn2 = NOP;
16136 }
16137 else
16138 {
16139 rel->r_offset = offset + d_offset;
16140 r_symndx = STN_UNDEF;
16141 r_type = R_PPC64_TPREL16_LO;
16142 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
16143 insn2 = 0x38630000; /* addi 3,3,0 */
16144 }
16145 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16146 /* Zap the reloc on the _tls_get_addr call too. */
16147 BFD_ASSERT (offset == rel[1].r_offset);
16148 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
16149 bfd_put_32 (input_bfd, insn2, contents + offset);
16150 if (r_type != R_PPC64_NONE)
16151 goto again;
16152 }
16153 break;
16154
16155 case R_PPC64_DTPMOD64:
16156 if (rel + 1 < relend
16157 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
16158 && rel[1].r_offset == rel->r_offset + 8)
16159 {
16160 if ((tls_mask & TLS_GD) == 0
16161 && offset_in_range (input_section, rel->r_offset, 8))
16162 {
16163 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
16164 if ((tls_mask & TLS_GDIE) != 0)
16165 r_type = R_PPC64_TPREL64;
16166 else
16167 {
16168 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16169 r_type = R_PPC64_NONE;
16170 }
16171 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16172 }
16173 }
16174 else
16175 {
16176 if ((tls_mask & TLS_LD) == 0
16177 && offset_in_range (input_section, rel->r_offset, 8))
16178 {
16179 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
16180 r_type = R_PPC64_NONE;
16181 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16182 }
16183 }
16184 break;
16185
16186 case R_PPC64_TPREL64:
16187 if ((tls_mask & TLS_TPREL) == 0)
16188 {
16189 r_type = R_PPC64_NONE;
16190 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16191 }
16192 break;
16193
16194 case R_PPC64_ENTRY:
16195 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
16196 if (!bfd_link_pic (info)
16197 && !info->traditional_format
16198 && relocation + 0x80008000 <= 0xffffffff
16199 && offset_in_range (input_section, rel->r_offset, 8))
16200 {
16201 unsigned int insn1, insn2;
16202
16203 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16204 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16205 if ((insn1 & ~0xfffc) == LD_R2_0R12
16206 && insn2 == ADD_R2_R2_R12)
16207 {
16208 bfd_put_32 (input_bfd,
16209 LIS_R2 + PPC_HA (relocation),
16210 contents + rel->r_offset);
16211 bfd_put_32 (input_bfd,
16212 ADDI_R2_R2 + PPC_LO (relocation),
16213 contents + rel->r_offset + 4);
16214 }
16215 }
16216 else
16217 {
16218 relocation -= (rel->r_offset
16219 + input_section->output_offset
16220 + input_section->output_section->vma);
16221 if (relocation + 0x80008000 <= 0xffffffff
16222 && offset_in_range (input_section, rel->r_offset, 8))
16223 {
16224 unsigned int insn1, insn2;
16225
16226 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
16227 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
16228 if ((insn1 & ~0xfffc) == LD_R2_0R12
16229 && insn2 == ADD_R2_R2_R12)
16230 {
16231 bfd_put_32 (input_bfd,
16232 ADDIS_R2_R12 + PPC_HA (relocation),
16233 contents + rel->r_offset);
16234 bfd_put_32 (input_bfd,
16235 ADDI_R2_R2 + PPC_LO (relocation),
16236 contents + rel->r_offset + 4);
16237 }
16238 }
16239 }
16240 break;
16241
16242 case R_PPC64_REL16_HA:
16243 /* If we are generating a non-PIC executable, edit
16244 . 0: addis 2,12,.TOC.-0b@ha
16245 . addi 2,2,.TOC.-0b@l
16246 used by ELFv2 global entry points to set up r2, to
16247 . lis 2,.TOC.@ha
16248 . addi 2,2,.TOC.@l
16249 if .TOC. is in range. */
16250 if (!bfd_link_pic (info)
16251 && !info->traditional_format
16252 && !htab->opd_abi
16253 && rel->r_addend == d_offset
16254 && h != NULL && &h->elf == htab->elf.hgot
16255 && rel + 1 < relend
16256 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
16257 && rel[1].r_offset == rel->r_offset + 4
16258 && rel[1].r_addend == rel->r_addend + 4
16259 && relocation + 0x80008000 <= 0xffffffff
16260 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
16261 {
16262 unsigned int insn1, insn2;
16263 offset = rel->r_offset - d_offset;
16264 insn1 = bfd_get_32 (input_bfd, contents + offset);
16265 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
16266 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
16267 && (insn2 & 0xffff0000) == ADDI_R2_R2)
16268 {
16269 r_type = R_PPC64_ADDR16_HA;
16270 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16271 rel->r_addend -= d_offset;
16272 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
16273 rel[1].r_addend -= d_offset + 4;
16274 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
16275 }
16276 }
16277 break;
16278 }
16279
16280 /* Handle other relocations that tweak non-addend part of insn. */
16281 insn = 0;
16282 max_br_offset = 1 << 25;
16283 addend = rel->r_addend;
16284 reloc_dest = DEST_NORMAL;
16285 switch (r_type)
16286 {
16287 default:
16288 break;
16289
16290 case R_PPC64_TOCSAVE:
16291 if (relocation + addend == (rel->r_offset
16292 + input_section->output_offset
16293 + input_section->output_section->vma)
16294 && tocsave_find (htab, NO_INSERT,
16295 &local_syms, rel, input_bfd)
16296 && offset_in_range (input_section, rel->r_offset, 4))
16297 {
16298 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
16299 if (insn == NOP
16300 || insn == CROR_151515 || insn == CROR_313131)
16301 bfd_put_32 (input_bfd,
16302 STD_R2_0R1 + STK_TOC (htab),
16303 contents + rel->r_offset);
16304 }
16305 break;
16306
16307 /* Branch taken prediction relocations. */
16308 case R_PPC64_ADDR14_BRTAKEN:
16309 case R_PPC64_REL14_BRTAKEN:
16310 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
16311 /* Fall through. */
16312
16313 /* Branch not taken prediction relocations. */
16314 case R_PPC64_ADDR14_BRNTAKEN:
16315 case R_PPC64_REL14_BRNTAKEN:
16316 if (!offset_in_range (input_section, rel->r_offset, 4))
16317 break;
16318 insn |= bfd_get_32 (input_bfd,
16319 contents + rel->r_offset) & ~(0x01 << 21);
16320 /* Fall through. */
16321
16322 case R_PPC64_REL14:
16323 max_br_offset = 1 << 15;
16324 /* Fall through. */
16325
16326 case R_PPC64_REL24:
16327 case R_PPC64_REL24_NOTOC:
16328 case R_PPC64_REL24_P9NOTOC:
16329 case R_PPC64_PLTCALL:
16330 case R_PPC64_PLTCALL_NOTOC:
16331 /* Calls to functions with a different TOC, such as calls to
16332 shared objects, need to alter the TOC pointer. This is
16333 done using a linkage stub. A REL24 branching to these
16334 linkage stubs needs to be followed by a nop, as the nop
16335 will be replaced with an instruction to restore the TOC
16336 base pointer. */
16337 fdh = h;
16338 if (h != NULL
16339 && h->oh != NULL
16340 && h->oh->is_func_descriptor)
16341 fdh = ppc_follow_link (h->oh);
16342 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
16343 htab);
16344 if ((r_type == R_PPC64_PLTCALL
16345 || r_type == R_PPC64_PLTCALL_NOTOC)
16346 && stub_entry != NULL
16347 && stub_entry->type.main == ppc_stub_plt_call)
16348 stub_entry = NULL;
16349
16350 if (stub_entry != NULL
16351 && (stub_entry->type.main == ppc_stub_plt_call
16352 || stub_entry->type.r2save))
16353 {
16354 bool can_plt_call = false;
16355
16356 if (r_type == R_PPC64_REL24_NOTOC
16357 || r_type == R_PPC64_REL24_P9NOTOC)
16358 {
16359 /* NOTOC calls don't need to restore r2. */
16360 can_plt_call = true;
16361 }
16362 else if (stub_entry->type.main == ppc_stub_plt_call
16363 && !htab->opd_abi
16364 && htab->params->plt_localentry0 != 0
16365 && h != NULL
16366 && is_elfv2_localentry0 (&h->elf))
16367 {
16368 /* The function doesn't use or change r2. */
16369 can_plt_call = true;
16370 }
16371
16372 /* All of these stubs may modify r2, so there must be a
16373 branch and link followed by a nop. The nop is
16374 replaced by an insn to restore r2. */
16375 else if (offset_in_range (input_section, rel->r_offset, 8))
16376 {
16377 unsigned long br;
16378
16379 br = bfd_get_32 (input_bfd,
16380 contents + rel->r_offset);
16381 if ((br & 1) != 0)
16382 {
16383 unsigned long nop;
16384
16385 nop = bfd_get_32 (input_bfd,
16386 contents + rel->r_offset + 4);
16387 if (nop == LD_R2_0R1 + STK_TOC (htab))
16388 can_plt_call = true;
16389 else if (nop == NOP
16390 || nop == CROR_151515
16391 || nop == CROR_313131)
16392 {
16393 if (h != NULL
16394 && is_tls_get_addr (&h->elf, htab)
16395 && htab->params->tls_get_addr_opt)
16396 {
16397 /* Special stub used, leave nop alone. */
16398 }
16399 else
16400 bfd_put_32 (input_bfd,
16401 LD_R2_0R1 + STK_TOC (htab),
16402 contents + rel->r_offset + 4);
16403 can_plt_call = true;
16404 }
16405 }
16406 }
16407
16408 if (!can_plt_call && h != NULL)
16409 {
16410 const char *name = h->elf.root.root.string;
16411
16412 if (*name == '.')
16413 ++name;
16414
16415 if (startswith (name, "__libc_start_main")
16416 && (name[17] == 0 || name[17] == '@'))
16417 {
16418 /* Allow crt1 branch to go via a toc adjusting
16419 stub. Other calls that never return could do
16420 the same, if we could detect such. */
16421 can_plt_call = true;
16422 }
16423 }
16424
16425 if (!can_plt_call)
16426 {
16427 /* g++ as of 20130507 emits self-calls without a
16428 following nop. This is arguably wrong since we
16429 have conflicting information. On the one hand a
16430 global symbol and on the other a local call
16431 sequence, but don't error for this special case.
16432 It isn't possible to cheaply verify we have
16433 exactly such a call. Allow all calls to the same
16434 section. */
16435 asection *code_sec = sec;
16436
16437 if (get_opd_info (sec) != NULL)
16438 {
16439 bfd_vma off = (relocation + addend
16440 - sec->output_section->vma
16441 - sec->output_offset);
16442
16443 opd_entry_value (sec, off, &code_sec, NULL, false);
16444 }
16445 if (code_sec == input_section)
16446 can_plt_call = true;
16447 }
16448
16449 if (!can_plt_call)
16450 {
16451 if (stub_entry->type.main == ppc_stub_plt_call)
16452 info->callbacks->einfo
16453 /* xgettext:c-format */
16454 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16455 "(plt call stub)\n"),
16456 input_bfd, input_section, rel->r_offset, sym_name);
16457 else
16458 info->callbacks->einfo
16459 /* xgettext:c-format */
16460 (_("%H: call to `%pT' lacks nop, can't restore toc; "
16461 "(toc save/adjust stub)\n"),
16462 input_bfd, input_section, rel->r_offset, sym_name);
16463
16464 bfd_set_error (bfd_error_bad_value);
16465 ret = false;
16466 }
16467
16468 if (can_plt_call
16469 && stub_entry->type.main == ppc_stub_plt_call)
16470 unresolved_reloc = false;
16471 }
16472
16473 if ((stub_entry == NULL
16474 || stub_entry->type.main == ppc_stub_long_branch
16475 || stub_entry->type.main == ppc_stub_plt_branch)
16476 && get_opd_info (sec) != NULL)
16477 {
16478 /* The branch destination is the value of the opd entry. */
16479 bfd_vma off = (relocation + addend
16480 - sec->output_section->vma
16481 - sec->output_offset);
16482 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
16483 if (dest != (bfd_vma) -1)
16484 {
16485 relocation = dest;
16486 addend = 0;
16487 reloc_dest = DEST_OPD;
16488 }
16489 }
16490
16491 /* If the branch is out of reach we ought to have a long
16492 branch stub. */
16493 from = (rel->r_offset
16494 + input_section->output_offset
16495 + input_section->output_section->vma);
16496
16497 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
16498 ? fdh->elf.other
16499 : sym->st_other);
16500
16501 if (stub_entry != NULL
16502 && (stub_entry->type.main == ppc_stub_long_branch
16503 || stub_entry->type.main == ppc_stub_plt_branch))
16504 {
16505 if (stub_entry->type.sub == ppc_stub_toc
16506 && !stub_entry->type.r2save
16507 && (r_type == R_PPC64_ADDR14_BRTAKEN
16508 || r_type == R_PPC64_ADDR14_BRNTAKEN
16509 || (relocation + addend - from + max_br_offset
16510 < 2 * max_br_offset)))
16511 /* Don't use the stub if this branch is in range. */
16512 stub_entry = NULL;
16513
16514 if (stub_entry != NULL
16515 && stub_entry->type.sub >= ppc_stub_notoc
16516 && ((r_type != R_PPC64_REL24_NOTOC
16517 && r_type != R_PPC64_REL24_P9NOTOC)
16518 || ((fdh ? fdh->elf.other : sym->st_other)
16519 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16520 && (relocation + addend - from + max_br_offset
16521 < 2 * max_br_offset))
16522 stub_entry = NULL;
16523
16524 if (stub_entry != NULL
16525 && stub_entry->type.r2save
16526 && (r_type == R_PPC64_REL24_NOTOC
16527 || r_type == R_PPC64_REL24_P9NOTOC)
16528 && (relocation + addend - from + max_br_offset
16529 < 2 * max_br_offset))
16530 stub_entry = NULL;
16531 }
16532
16533 if (stub_entry != NULL)
16534 {
16535 /* Munge up the value and addend so that we call the stub
16536 rather than the procedure directly. */
16537 asection *stub_sec = stub_entry->group->stub_sec;
16538
16539 if (stub_entry->type.main == ppc_stub_save_res)
16540 relocation += (stub_sec->output_offset
16541 + stub_sec->output_section->vma
16542 + stub_sec->size - htab->sfpr->size
16543 - htab->sfpr->output_offset
16544 - htab->sfpr->output_section->vma);
16545 else
16546 relocation = (stub_entry->stub_offset
16547 + stub_sec->output_offset
16548 + stub_sec->output_section->vma);
16549 addend = 0;
16550 reloc_dest = DEST_STUB;
16551
16552 if (((stub_entry->type.r2save
16553 && (r_type == R_PPC64_REL24_NOTOC
16554 || r_type == R_PPC64_REL24_P9NOTOC))
16555 || ((stub_entry->type.main == ppc_stub_plt_call
16556 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16557 && rel + 1 < relend
16558 && rel[1].r_offset == rel->r_offset + 4
16559 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16560 && !(stub_entry->type.main == ppc_stub_plt_call
16561 && htab->params->tls_get_addr_opt
16562 && h != NULL
16563 && is_tls_get_addr (&h->elf, htab)))
16564 {
16565 /* Skip over the r2 store at the start of the stub. */
16566 relocation += 4;
16567 }
16568
16569 if ((r_type == R_PPC64_REL24_NOTOC
16570 || r_type == R_PPC64_REL24_P9NOTOC)
16571 && stub_entry->type.main == ppc_stub_plt_call
16572 && stub_entry->type.sub >= ppc_stub_notoc)
16573 htab->notoc_plt = 1;
16574 }
16575
16576 if (insn != 0)
16577 {
16578 if (is_isa_v2)
16579 {
16580 /* Set 'a' bit. This is 0b00010 in BO field for branch
16581 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16582 for branch on CTR insns (BO == 1a00t or 1a01t). */
16583 if ((insn & (0x14 << 21)) == (0x04 << 21))
16584 insn |= 0x02 << 21;
16585 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16586 insn |= 0x08 << 21;
16587 else
16588 break;
16589 }
16590 else
16591 {
16592 /* Invert 'y' bit if not the default. */
16593 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16594 insn ^= 0x01 << 21;
16595 }
16596
16597 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16598 }
16599
16600 /* NOP out calls to undefined weak functions.
16601 We can thus call a weak function without first
16602 checking whether the function is defined. */
16603 else if (h != NULL
16604 && h->elf.root.type == bfd_link_hash_undefweak
16605 && h->elf.dynindx == -1
16606 && (r_type == R_PPC64_REL24
16607 || r_type == R_PPC64_REL24_NOTOC
16608 || r_type == R_PPC64_REL24_P9NOTOC)
16609 && relocation == 0
16610 && addend == 0
16611 && offset_in_range (input_section, rel->r_offset, 4))
16612 {
16613 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16614 goto copy_reloc;
16615 }
16616 break;
16617
16618 case R_PPC64_GOT16_DS:
16619 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16620 || (bfd_link_pic (info)
16621 && sec == bfd_abs_section_ptr)
16622 || !htab->do_toc_opt)
16623 break;
16624 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16625 if (relocation + addend - from + 0x8000 < 0x10000
16626 && sec != NULL
16627 && sec->output_section != NULL
16628 && !discarded_section (sec)
16629 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16630 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16631 {
16632 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16633 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16634 {
16635 insn += (14u << 26) - (58u << 26);
16636 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16637 r_type = R_PPC64_TOC16;
16638 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16639 }
16640 }
16641 break;
16642
16643 case R_PPC64_GOT16_LO_DS:
16644 case R_PPC64_GOT16_HA:
16645 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16646 || (bfd_link_pic (info)
16647 && sec == bfd_abs_section_ptr)
16648 || !htab->do_toc_opt)
16649 break;
16650 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16651 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16652 && sec != NULL
16653 && sec->output_section != NULL
16654 && !discarded_section (sec)
16655 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16656 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16657 {
16658 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16659 if (r_type == R_PPC64_GOT16_LO_DS
16660 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16661 {
16662 insn += (14u << 26) - (58u << 26);
16663 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16664 r_type = R_PPC64_TOC16_LO;
16665 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16666 }
16667 else if (r_type == R_PPC64_GOT16_HA
16668 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16669 {
16670 r_type = R_PPC64_TOC16_HA;
16671 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16672 }
16673 }
16674 break;
16675
16676 case R_PPC64_GOT_PCREL34:
16677 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16678 || (bfd_link_pic (info)
16679 && sec == bfd_abs_section_ptr)
16680 || !htab->do_toc_opt)
16681 break;
16682 from = (rel->r_offset
16683 + input_section->output_section->vma
16684 + input_section->output_offset);
16685 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16686 && sec != NULL
16687 && sec->output_section != NULL
16688 && !discarded_section (sec)
16689 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16690 && offset_in_range (input_section, rel->r_offset, 8)))
16691 break;
16692
16693 offset = rel->r_offset;
16694 pinsn = bfd_get_32 (input_bfd, contents + offset);
16695 pinsn <<= 32;
16696 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16697 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16698 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16699 break;
16700
16701 /* Replace with paddi. */
16702 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16703 r_type = R_PPC64_PCREL34;
16704 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16705 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16706 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16707 /* Fall through. */
16708
16709 case R_PPC64_PCREL34:
16710 if (!htab->params->no_pcrel_opt
16711 && rel + 1 < relend
16712 && rel[1].r_offset == rel->r_offset
16713 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16714 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16715 && offset_in_range (input_section, rel->r_offset, 8))
16716 {
16717 offset = rel->r_offset;
16718 pinsn = bfd_get_32 (input_bfd, contents + offset);
16719 pinsn <<= 32;
16720 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16721 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16722 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16723 | (14ULL << 26) /* paddi */))
16724 {
16725 bfd_vma off2 = rel[1].r_addend;
16726 if (off2 == 0)
16727 /* zero means next insn. */
16728 off2 = 8;
16729 off2 += offset;
16730 if (offset_in_range (input_section, off2, 4))
16731 {
16732 uint64_t pinsn2;
16733 bfd_signed_vma addend_off;
16734 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16735 pinsn2 <<= 32;
16736 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16737 {
16738 if (!offset_in_range (input_section, off2, 8))
16739 break;
16740 pinsn2 |= bfd_get_32 (input_bfd,
16741 contents + off2 + 4);
16742 }
16743 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16744 {
16745 addend += addend_off;
16746 rel->r_addend = addend;
16747 bfd_put_32 (input_bfd, pinsn >> 32,
16748 contents + offset);
16749 bfd_put_32 (input_bfd, pinsn,
16750 contents + offset + 4);
16751 bfd_put_32 (input_bfd, pinsn2 >> 32,
16752 contents + off2);
16753 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16754 bfd_put_32 (input_bfd, pinsn2,
16755 contents + off2 + 4);
16756 }
16757 }
16758 }
16759 }
16760 break;
16761 }
16762
16763 tls_type = 0;
16764 save_unresolved_reloc = unresolved_reloc;
16765 switch (r_type)
16766 {
16767 default:
16768 /* xgettext:c-format */
16769 _bfd_error_handler (_("%pB: %s unsupported"),
16770 input_bfd, ppc64_elf_howto_table[r_type]->name);
16771
16772 bfd_set_error (bfd_error_bad_value);
16773 ret = false;
16774 goto copy_reloc;
16775
16776 case R_PPC64_NONE:
16777 case R_PPC64_TLS:
16778 case R_PPC64_TLSGD:
16779 case R_PPC64_TLSLD:
16780 case R_PPC64_TOCSAVE:
16781 case R_PPC64_GNU_VTINHERIT:
16782 case R_PPC64_GNU_VTENTRY:
16783 case R_PPC64_ENTRY:
16784 case R_PPC64_PCREL_OPT:
16785 goto copy_reloc;
16786
16787 /* GOT16 relocations. Like an ADDR16 using the symbol's
16788 address in the GOT as relocation value instead of the
16789 symbol's value itself. Also, create a GOT entry for the
16790 symbol and put the symbol value there. */
16791 case R_PPC64_GOT_TLSGD16:
16792 case R_PPC64_GOT_TLSGD16_LO:
16793 case R_PPC64_GOT_TLSGD16_HI:
16794 case R_PPC64_GOT_TLSGD16_HA:
16795 case R_PPC64_GOT_TLSGD_PCREL34:
16796 tls_type = TLS_TLS | TLS_GD;
16797 goto dogot;
16798
16799 case R_PPC64_GOT_TLSLD16:
16800 case R_PPC64_GOT_TLSLD16_LO:
16801 case R_PPC64_GOT_TLSLD16_HI:
16802 case R_PPC64_GOT_TLSLD16_HA:
16803 case R_PPC64_GOT_TLSLD_PCREL34:
16804 tls_type = TLS_TLS | TLS_LD;
16805 goto dogot;
16806
16807 case R_PPC64_GOT_TPREL16_DS:
16808 case R_PPC64_GOT_TPREL16_LO_DS:
16809 case R_PPC64_GOT_TPREL16_HI:
16810 case R_PPC64_GOT_TPREL16_HA:
16811 case R_PPC64_GOT_TPREL_PCREL34:
16812 tls_type = TLS_TLS | TLS_TPREL;
16813 goto dogot;
16814
16815 case R_PPC64_GOT_DTPREL16_DS:
16816 case R_PPC64_GOT_DTPREL16_LO_DS:
16817 case R_PPC64_GOT_DTPREL16_HI:
16818 case R_PPC64_GOT_DTPREL16_HA:
16819 case R_PPC64_GOT_DTPREL_PCREL34:
16820 tls_type = TLS_TLS | TLS_DTPREL;
16821 goto dogot;
16822
16823 case R_PPC64_GOT16:
16824 case R_PPC64_GOT16_LO:
16825 case R_PPC64_GOT16_HI:
16826 case R_PPC64_GOT16_HA:
16827 case R_PPC64_GOT16_DS:
16828 case R_PPC64_GOT16_LO_DS:
16829 case R_PPC64_GOT_PCREL34:
16830 dogot:
16831 {
16832 /* Relocation is to the entry for this symbol in the global
16833 offset table. */
16834 asection *got;
16835 bfd_vma *offp;
16836 bfd_vma off;
16837 unsigned long indx = 0;
16838 struct got_entry *ent;
16839
16840 if (tls_type == (TLS_TLS | TLS_LD)
16841 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16842 ent = ppc64_tlsld_got (input_bfd);
16843 else
16844 {
16845 if (h != NULL)
16846 {
16847 if (!htab->elf.dynamic_sections_created
16848 || h->elf.dynindx == -1
16849 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16850 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16851 /* This is actually a static link, or it is a
16852 -Bsymbolic link and the symbol is defined
16853 locally, or the symbol was forced to be local
16854 because of a version file. */
16855 ;
16856 else
16857 {
16858 indx = h->elf.dynindx;
16859 unresolved_reloc = false;
16860 }
16861 ent = h->elf.got.glist;
16862 }
16863 else
16864 {
16865 if (local_got_ents == NULL)
16866 abort ();
16867 ent = local_got_ents[r_symndx];
16868 }
16869
16870 for (; ent != NULL; ent = ent->next)
16871 if (ent->addend == orig_rel.r_addend
16872 && ent->owner == input_bfd
16873 && ent->tls_type == tls_type)
16874 break;
16875 }
16876
16877 if (ent == NULL)
16878 abort ();
16879 if (ent->is_indirect)
16880 ent = ent->got.ent;
16881 offp = &ent->got.offset;
16882 got = ppc64_elf_tdata (ent->owner)->got;
16883 if (got == NULL)
16884 abort ();
16885
16886 /* The offset must always be a multiple of 8. We use the
16887 least significant bit to record whether we have already
16888 processed this entry. */
16889 off = *offp;
16890 if ((off & 1) != 0)
16891 off &= ~1;
16892 else
16893 {
16894 /* Generate relocs for the dynamic linker, except in
16895 the case of TLSLD where we'll use one entry per
16896 module. */
16897 asection *relgot;
16898 bool ifunc;
16899
16900 *offp = off | 1;
16901 relgot = NULL;
16902 ifunc = (h != NULL
16903 ? h->elf.type == STT_GNU_IFUNC
16904 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16905 if (ifunc)
16906 {
16907 relgot = htab->elf.irelplt;
16908 if (indx == 0 || is_static_defined (&h->elf))
16909 htab->elf.ifunc_resolvers = true;
16910 }
16911 else if (indx != 0
16912 || (bfd_link_pic (info)
16913 && (h == NULL
16914 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16915 && !(tls_type != 0
16916 && bfd_link_executable (info)
16917 && (h == NULL
16918 || SYMBOL_REFERENCES_LOCAL (info,
16919 &h->elf)))
16920 && (h != NULL
16921 ? !bfd_is_abs_symbol (&h->elf.root)
16922 : sym->st_shndx != SHN_ABS)))
16923
16924 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16925 if (relgot != NULL)
16926 {
16927 outrel.r_offset = (got->output_section->vma
16928 + got->output_offset
16929 + off);
16930 outrel.r_addend = orig_rel.r_addend;
16931 if (tls_type & (TLS_LD | TLS_GD))
16932 {
16933 outrel.r_addend = 0;
16934 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16935 if (tls_type == (TLS_TLS | TLS_GD))
16936 {
16937 loc = relgot->contents;
16938 loc += (relgot->reloc_count++
16939 * sizeof (Elf64_External_Rela));
16940 bfd_elf64_swap_reloca_out (output_bfd,
16941 &outrel, loc);
16942 outrel.r_offset += 8;
16943 outrel.r_addend = orig_rel.r_addend;
16944 outrel.r_info
16945 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16946 }
16947 }
16948 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16949 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16950 else if (tls_type == (TLS_TLS | TLS_TPREL))
16951 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16952 else if (indx != 0)
16953 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16954 else
16955 {
16956 if (ifunc)
16957 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16958 else
16959 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16960
16961 /* Write the .got section contents for the sake
16962 of prelink. */
16963 loc = got->contents + off;
16964 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16965 loc);
16966 }
16967
16968 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16969 {
16970 outrel.r_addend += relocation;
16971 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16972 {
16973 if (htab->elf.tls_sec == NULL)
16974 outrel.r_addend = 0;
16975 else
16976 outrel.r_addend -= htab->elf.tls_sec->vma;
16977 }
16978 }
16979 if (!(info->enable_dt_relr
16980 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE))
16981 {
16982 loc = relgot->contents;
16983 loc += (relgot->reloc_count++
16984 * sizeof (Elf64_External_Rela));
16985 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16986 }
16987 }
16988
16989 /* Init the .got section contents here if we're not
16990 emitting a reloc. */
16991 else
16992 {
16993 relocation += orig_rel.r_addend;
16994 if (tls_type != 0)
16995 {
16996 if (htab->elf.tls_sec == NULL)
16997 relocation = 0;
16998 else
16999 {
17000 if (tls_type & TLS_LD)
17001 relocation = 0;
17002 else
17003 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
17004 if (tls_type & TLS_TPREL)
17005 relocation += DTP_OFFSET - TP_OFFSET;
17006 }
17007
17008 if (tls_type & (TLS_GD | TLS_LD))
17009 {
17010 bfd_put_64 (output_bfd, relocation,
17011 got->contents + off + 8);
17012 relocation = 1;
17013 }
17014 }
17015 bfd_put_64 (output_bfd, relocation,
17016 got->contents + off);
17017 }
17018 }
17019
17020 if (off >= (bfd_vma) -2)
17021 abort ();
17022
17023 relocation = got->output_section->vma + got->output_offset + off;
17024 addend = 0;
17025 if (!(r_type == R_PPC64_GOT_PCREL34
17026 || r_type == R_PPC64_GOT_TLSGD_PCREL34
17027 || r_type == R_PPC64_GOT_TLSLD_PCREL34
17028 || r_type == R_PPC64_GOT_TPREL_PCREL34
17029 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
17030 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
17031 }
17032 break;
17033
17034 case R_PPC64_PLT16_HA:
17035 case R_PPC64_PLT16_HI:
17036 case R_PPC64_PLT16_LO:
17037 case R_PPC64_PLT16_LO_DS:
17038 case R_PPC64_PLT_PCREL34:
17039 case R_PPC64_PLT_PCREL34_NOTOC:
17040 case R_PPC64_PLT32:
17041 case R_PPC64_PLT64:
17042 case R_PPC64_PLTSEQ:
17043 case R_PPC64_PLTSEQ_NOTOC:
17044 case R_PPC64_PLTCALL:
17045 case R_PPC64_PLTCALL_NOTOC:
17046 /* Relocation is to the entry for this symbol in the
17047 procedure linkage table. */
17048 unresolved_reloc = true;
17049 {
17050 struct plt_entry **plt_list = NULL;
17051 if (h != NULL)
17052 plt_list = &h->elf.plt.plist;
17053 else if (local_got_ents != NULL)
17054 {
17055 struct plt_entry **local_plt = (struct plt_entry **)
17056 (local_got_ents + symtab_hdr->sh_info);
17057 plt_list = local_plt + r_symndx;
17058 }
17059 if (plt_list)
17060 {
17061 struct plt_entry *ent;
17062
17063 for (ent = *plt_list; ent != NULL; ent = ent->next)
17064 if (ent->plt.offset != (bfd_vma) -1
17065 && ent->addend == orig_rel.r_addend)
17066 {
17067 asection *plt;
17068 bfd_vma got;
17069
17070 plt = htab->elf.splt;
17071 if (use_local_plt (info, elf_hash_entry (h)))
17072 {
17073 if (h != NULL
17074 ? h->elf.type == STT_GNU_IFUNC
17075 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17076 plt = htab->elf.iplt;
17077 else
17078 plt = htab->pltlocal;
17079 }
17080 relocation = (plt->output_section->vma
17081 + plt->output_offset
17082 + ent->plt.offset);
17083 if (r_type == R_PPC64_PLT16_HA
17084 || r_type == R_PPC64_PLT16_HI
17085 || r_type == R_PPC64_PLT16_LO
17086 || r_type == R_PPC64_PLT16_LO_DS)
17087 {
17088 got = (elf_gp (output_bfd)
17089 + htab->sec_info[input_section->id].toc_off);
17090 relocation -= got;
17091 }
17092 addend = 0;
17093 unresolved_reloc = false;
17094 break;
17095 }
17096 }
17097 }
17098 break;
17099
17100 case R_PPC64_TOC:
17101 /* Relocation value is TOC base. */
17102 relocation = TOCstart;
17103 if (r_symndx == STN_UNDEF)
17104 relocation += htab->sec_info[input_section->id].toc_off;
17105 else if (unresolved_reloc)
17106 ;
17107 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
17108 relocation += htab->sec_info[sec->id].toc_off;
17109 else
17110 unresolved_reloc = true;
17111 if (unresolved_reloc
17112 || (!is_opd
17113 && h != NULL
17114 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
17115 info->callbacks->einfo
17116 /* xgettext:c-format */
17117 (_("%H: %s against %pT is not supported\n"),
17118 input_bfd, input_section, rel->r_offset,
17119 ppc64_elf_howto_table[r_type]->name, sym_name);
17120 goto dodyn;
17121
17122 /* TOC16 relocs. We want the offset relative to the TOC base,
17123 which is the address of the start of the TOC plus 0x8000.
17124 The TOC consists of sections .got, .toc, .tocbss, and .plt,
17125 in this order. */
17126 case R_PPC64_TOC16:
17127 case R_PPC64_TOC16_LO:
17128 case R_PPC64_TOC16_HI:
17129 case R_PPC64_TOC16_DS:
17130 case R_PPC64_TOC16_LO_DS:
17131 case R_PPC64_TOC16_HA:
17132 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
17133 if (h != NULL)
17134 goto dodyn;
17135 break;
17136
17137 /* Relocate against the beginning of the section. */
17138 case R_PPC64_SECTOFF:
17139 case R_PPC64_SECTOFF_LO:
17140 case R_PPC64_SECTOFF_HI:
17141 case R_PPC64_SECTOFF_DS:
17142 case R_PPC64_SECTOFF_LO_DS:
17143 case R_PPC64_SECTOFF_HA:
17144 if (sec != NULL)
17145 addend -= sec->output_section->vma;
17146 break;
17147
17148 case R_PPC64_REL16:
17149 case R_PPC64_REL16_LO:
17150 case R_PPC64_REL16_HI:
17151 case R_PPC64_REL16_HA:
17152 case R_PPC64_REL16_HIGH:
17153 case R_PPC64_REL16_HIGHA:
17154 case R_PPC64_REL16_HIGHER:
17155 case R_PPC64_REL16_HIGHERA:
17156 case R_PPC64_REL16_HIGHEST:
17157 case R_PPC64_REL16_HIGHESTA:
17158 case R_PPC64_REL16_HIGHER34:
17159 case R_PPC64_REL16_HIGHERA34:
17160 case R_PPC64_REL16_HIGHEST34:
17161 case R_PPC64_REL16_HIGHESTA34:
17162 case R_PPC64_REL16DX_HA:
17163 case R_PPC64_REL14:
17164 case R_PPC64_REL14_BRNTAKEN:
17165 case R_PPC64_REL14_BRTAKEN:
17166 case R_PPC64_REL24:
17167 case R_PPC64_REL24_NOTOC:
17168 case R_PPC64_REL24_P9NOTOC:
17169 case R_PPC64_PCREL34:
17170 case R_PPC64_PCREL28:
17171 break;
17172
17173 case R_PPC64_TPREL16:
17174 case R_PPC64_TPREL16_LO:
17175 case R_PPC64_TPREL16_HI:
17176 case R_PPC64_TPREL16_HA:
17177 case R_PPC64_TPREL16_DS:
17178 case R_PPC64_TPREL16_LO_DS:
17179 case R_PPC64_TPREL16_HIGH:
17180 case R_PPC64_TPREL16_HIGHA:
17181 case R_PPC64_TPREL16_HIGHER:
17182 case R_PPC64_TPREL16_HIGHERA:
17183 case R_PPC64_TPREL16_HIGHEST:
17184 case R_PPC64_TPREL16_HIGHESTA:
17185 if (h != NULL
17186 && h->elf.root.type == bfd_link_hash_undefweak
17187 && h->elf.dynindx == -1
17188 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
17189 {
17190 /* Make this relocation against an undefined weak symbol
17191 resolve to zero. This is really just a tweak, since
17192 code using weak externs ought to check that they are
17193 defined before using them. */
17194 bfd_byte *p = contents + rel->r_offset - d_offset;
17195
17196 insn = bfd_get_32 (input_bfd, p);
17197 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
17198 if (insn != 0)
17199 bfd_put_32 (input_bfd, insn, p);
17200 break;
17201 }
17202 /* Fall through. */
17203
17204 case R_PPC64_TPREL34:
17205 if (htab->elf.tls_sec != NULL)
17206 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17207 /* The TPREL16 relocs shouldn't really be used in shared
17208 libs or with non-local symbols as that will result in
17209 DT_TEXTREL being set, but support them anyway. */
17210 goto dodyn;
17211
17212 case R_PPC64_DTPREL16:
17213 case R_PPC64_DTPREL16_LO:
17214 case R_PPC64_DTPREL16_HI:
17215 case R_PPC64_DTPREL16_HA:
17216 case R_PPC64_DTPREL16_DS:
17217 case R_PPC64_DTPREL16_LO_DS:
17218 case R_PPC64_DTPREL16_HIGH:
17219 case R_PPC64_DTPREL16_HIGHA:
17220 case R_PPC64_DTPREL16_HIGHER:
17221 case R_PPC64_DTPREL16_HIGHERA:
17222 case R_PPC64_DTPREL16_HIGHEST:
17223 case R_PPC64_DTPREL16_HIGHESTA:
17224 case R_PPC64_DTPREL34:
17225 if (htab->elf.tls_sec != NULL)
17226 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17227 break;
17228
17229 case R_PPC64_ADDR64_LOCAL:
17230 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
17231 ? h->elf.other
17232 : sym->st_other);
17233 break;
17234
17235 case R_PPC64_DTPMOD64:
17236 relocation = 1;
17237 addend = 0;
17238 goto dodyn;
17239
17240 case R_PPC64_TPREL64:
17241 if (htab->elf.tls_sec != NULL)
17242 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
17243 goto dodyn;
17244
17245 case R_PPC64_DTPREL64:
17246 if (htab->elf.tls_sec != NULL)
17247 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
17248 /* Fall through. */
17249
17250 /* Relocations that may need to be propagated if this is a
17251 dynamic object. */
17252 case R_PPC64_REL30:
17253 case R_PPC64_REL32:
17254 case R_PPC64_REL64:
17255 case R_PPC64_ADDR14:
17256 case R_PPC64_ADDR14_BRNTAKEN:
17257 case R_PPC64_ADDR14_BRTAKEN:
17258 case R_PPC64_ADDR16:
17259 case R_PPC64_ADDR16_DS:
17260 case R_PPC64_ADDR16_HA:
17261 case R_PPC64_ADDR16_HI:
17262 case R_PPC64_ADDR16_HIGH:
17263 case R_PPC64_ADDR16_HIGHA:
17264 case R_PPC64_ADDR16_HIGHER:
17265 case R_PPC64_ADDR16_HIGHERA:
17266 case R_PPC64_ADDR16_HIGHEST:
17267 case R_PPC64_ADDR16_HIGHESTA:
17268 case R_PPC64_ADDR16_LO:
17269 case R_PPC64_ADDR16_LO_DS:
17270 case R_PPC64_ADDR16_HIGHER34:
17271 case R_PPC64_ADDR16_HIGHERA34:
17272 case R_PPC64_ADDR16_HIGHEST34:
17273 case R_PPC64_ADDR16_HIGHESTA34:
17274 case R_PPC64_ADDR24:
17275 case R_PPC64_ADDR32:
17276 case R_PPC64_ADDR64:
17277 case R_PPC64_UADDR16:
17278 case R_PPC64_UADDR32:
17279 case R_PPC64_UADDR64:
17280 case R_PPC64_D34:
17281 case R_PPC64_D34_LO:
17282 case R_PPC64_D34_HI30:
17283 case R_PPC64_D34_HA30:
17284 case R_PPC64_D28:
17285 dodyn:
17286 if ((input_section->flags & SEC_ALLOC) == 0)
17287 break;
17288
17289 if (NO_OPD_RELOCS && is_opd)
17290 break;
17291
17292 if (bfd_link_pic (info)
17293 ? ((h == NULL
17294 || h->elf.dyn_relocs != NULL)
17295 && ((h != NULL && pc_dynrelocs (h))
17296 || must_be_dyn_reloc (info, r_type)))
17297 : (h != NULL
17298 ? h->elf.dyn_relocs != NULL
17299 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17300 {
17301 bool skip, relocate;
17302 asection *sreloc;
17303 bfd_vma out_off;
17304 long indx = 0;
17305
17306 /* When generating a dynamic object, these relocations
17307 are copied into the output file to be resolved at run
17308 time. */
17309
17310 skip = false;
17311 relocate = false;
17312
17313 out_off = _bfd_elf_section_offset (output_bfd, info,
17314 input_section, rel->r_offset);
17315 if (out_off == (bfd_vma) -1)
17316 skip = true;
17317 else if (out_off == (bfd_vma) -2)
17318 skip = true, relocate = true;
17319 out_off += (input_section->output_section->vma
17320 + input_section->output_offset);
17321 outrel.r_offset = out_off;
17322 outrel.r_addend = rel->r_addend;
17323
17324 /* Optimize unaligned reloc use. */
17325 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
17326 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
17327 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
17328 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
17329 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
17330 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
17331 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
17332 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
17333 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
17334
17335 if (skip)
17336 memset (&outrel, 0, sizeof outrel);
17337 else if (h != NULL
17338 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
17339 && !is_opd
17340 && r_type != R_PPC64_TOC)
17341 {
17342 indx = h->elf.dynindx;
17343 BFD_ASSERT (indx != -1);
17344 outrel.r_info = ELF64_R_INFO (indx, r_type);
17345 }
17346 else
17347 {
17348 /* This symbol is local, or marked to become local,
17349 or this is an opd section reloc which must point
17350 at a local function. */
17351 outrel.r_addend += relocation;
17352 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
17353 {
17354 if (is_opd && h != NULL)
17355 {
17356 /* Lie about opd entries. This case occurs
17357 when building shared libraries and we
17358 reference a function in another shared
17359 lib. The same thing happens for a weak
17360 definition in an application that's
17361 overridden by a strong definition in a
17362 shared lib. (I believe this is a generic
17363 bug in binutils handling of weak syms.)
17364 In these cases we won't use the opd
17365 entry in this lib. */
17366 unresolved_reloc = false;
17367 }
17368 if (!is_opd
17369 && r_type == R_PPC64_ADDR64
17370 && (h != NULL
17371 ? h->elf.type == STT_GNU_IFUNC
17372 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
17373 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
17374 else
17375 {
17376 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
17377
17378 /* We need to relocate .opd contents for ld.so.
17379 Prelink also wants simple and consistent rules
17380 for relocs. This make all RELATIVE relocs have
17381 *r_offset equal to r_addend. */
17382 relocate = true;
17383 }
17384 }
17385 else
17386 {
17387 if (h != NULL
17388 ? h->elf.type == STT_GNU_IFUNC
17389 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17390 {
17391 info->callbacks->einfo
17392 /* xgettext:c-format */
17393 (_("%H: %s for indirect "
17394 "function `%pT' unsupported\n"),
17395 input_bfd, input_section, rel->r_offset,
17396 ppc64_elf_howto_table[r_type]->name,
17397 sym_name);
17398 ret = false;
17399 }
17400 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
17401 ;
17402 else if (sec == NULL || sec->owner == NULL)
17403 {
17404 bfd_set_error (bfd_error_bad_value);
17405 return false;
17406 }
17407 else
17408 {
17409 asection *osec = sec->output_section;
17410
17411 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
17412 {
17413 /* TLS symbol values are relative to the
17414 TLS segment. Dynamic relocations for
17415 local TLS symbols therefore can't be
17416 reduced to a relocation against their
17417 section symbol because it holds the
17418 address of the section, not a value
17419 relative to the TLS segment. We could
17420 change the .tdata dynamic section symbol
17421 to be zero value but STN_UNDEF works
17422 and is used elsewhere, eg. for TPREL64
17423 GOT relocs against local TLS symbols. */
17424 osec = htab->elf.tls_sec;
17425 indx = 0;
17426 }
17427 else
17428 {
17429 indx = elf_section_data (osec)->dynindx;
17430 if (indx == 0)
17431 {
17432 if ((osec->flags & SEC_READONLY) == 0
17433 && htab->elf.data_index_section != NULL)
17434 osec = htab->elf.data_index_section;
17435 else
17436 osec = htab->elf.text_index_section;
17437 indx = elf_section_data (osec)->dynindx;
17438 }
17439 BFD_ASSERT (indx != 0);
17440 }
17441
17442 /* We are turning this relocation into one
17443 against a section symbol, so subtract out
17444 the output section's address but not the
17445 offset of the input section in the output
17446 section. */
17447 outrel.r_addend -= osec->vma;
17448 }
17449
17450 outrel.r_info = ELF64_R_INFO (indx, r_type);
17451 }
17452 }
17453
17454 if (!(info->enable_dt_relr
17455 && ELF64_R_TYPE (outrel.r_info) == R_PPC64_RELATIVE
17456 && rel->r_offset % 2 == 0
17457 && input_section->alignment_power != 0
17458 && ELF64_R_TYPE (orig_rel.r_info) != R_PPC64_UADDR64))
17459 {
17460 sreloc = elf_section_data (input_section)->sreloc;
17461 if (h != NULL
17462 ? h->elf.type == STT_GNU_IFUNC
17463 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
17464 {
17465 sreloc = htab->elf.irelplt;
17466 if (indx == 0 || is_static_defined (&h->elf))
17467 htab->elf.ifunc_resolvers = true;
17468 }
17469 if (sreloc == NULL)
17470 abort ();
17471
17472 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
17473 >= sreloc->size)
17474 abort ();
17475 loc = sreloc->contents;
17476 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
17477 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
17478 }
17479
17480 if (!warned_dynamic
17481 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
17482 {
17483 info->callbacks->einfo
17484 /* xgettext:c-format */
17485 (_("%X%P: %pB: %s against %pT "
17486 "is not supported by glibc as a dynamic relocation\n"),
17487 input_bfd,
17488 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
17489 sym_name);
17490 warned_dynamic = true;
17491 }
17492
17493 /* If this reloc is against an external symbol, it will
17494 be computed at runtime, so there's no need to do
17495 anything now. However, for the sake of prelink ensure
17496 that the section contents are a known value. */
17497 if (!relocate)
17498 {
17499 unresolved_reloc = false;
17500 /* The value chosen here is quite arbitrary as ld.so
17501 ignores section contents except for the special
17502 case of .opd where the contents might be accessed
17503 before relocation. Choose zero, as that won't
17504 cause reloc overflow. */
17505 relocation = 0;
17506 addend = 0;
17507 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
17508 to improve backward compatibility with older
17509 versions of ld. */
17510 if (r_type == R_PPC64_ADDR64)
17511 addend = outrel.r_addend;
17512 /* Adjust pc_relative relocs to have zero in *r_offset. */
17513 else if (ppc64_elf_howto_table[r_type]->pc_relative)
17514 addend = outrel.r_offset;
17515 }
17516 }
17517 break;
17518
17519 case R_PPC64_COPY:
17520 case R_PPC64_GLOB_DAT:
17521 case R_PPC64_JMP_SLOT:
17522 case R_PPC64_JMP_IREL:
17523 case R_PPC64_RELATIVE:
17524 /* We shouldn't ever see these dynamic relocs in relocatable
17525 files. */
17526 /* Fall through. */
17527
17528 case R_PPC64_PLTGOT16:
17529 case R_PPC64_PLTGOT16_DS:
17530 case R_PPC64_PLTGOT16_HA:
17531 case R_PPC64_PLTGOT16_HI:
17532 case R_PPC64_PLTGOT16_LO:
17533 case R_PPC64_PLTGOT16_LO_DS:
17534 case R_PPC64_PLTREL32:
17535 case R_PPC64_PLTREL64:
17536 /* These ones haven't been implemented yet. */
17537
17538 info->callbacks->einfo
17539 /* xgettext:c-format */
17540 (_("%P: %pB: %s is not supported for `%pT'\n"),
17541 input_bfd,
17542 ppc64_elf_howto_table[r_type]->name, sym_name);
17543
17544 bfd_set_error (bfd_error_invalid_operation);
17545 ret = false;
17546 goto copy_reloc;
17547 }
17548
17549 /* Multi-instruction sequences that access the TOC can be
17550 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17551 to nop; addi rb,r2,x; */
17552 switch (r_type)
17553 {
17554 default:
17555 break;
17556
17557 case R_PPC64_GOT_TLSLD16_HI:
17558 case R_PPC64_GOT_TLSGD16_HI:
17559 case R_PPC64_GOT_TPREL16_HI:
17560 case R_PPC64_GOT_DTPREL16_HI:
17561 case R_PPC64_GOT16_HI:
17562 case R_PPC64_TOC16_HI:
17563 /* These relocs would only be useful if building up an
17564 offset to later add to r2, perhaps in an indexed
17565 addressing mode instruction. Don't try to optimize.
17566 Unfortunately, the possibility of someone building up an
17567 offset like this or even with the HA relocs, means that
17568 we need to check the high insn when optimizing the low
17569 insn. */
17570 break;
17571
17572 case R_PPC64_PLTCALL_NOTOC:
17573 if (!unresolved_reloc)
17574 htab->notoc_plt = 1;
17575 /* Fall through. */
17576 case R_PPC64_PLTCALL:
17577 if (unresolved_reloc
17578 && offset_in_range (input_section, rel->r_offset,
17579 r_type == R_PPC64_PLTCALL ? 8 : 4))
17580 {
17581 /* No plt entry. Make this into a direct call. */
17582 bfd_byte *p = contents + rel->r_offset;
17583 insn = bfd_get_32 (input_bfd, p);
17584 insn &= 1;
17585 bfd_put_32 (input_bfd, B_DOT | insn, p);
17586 if (r_type == R_PPC64_PLTCALL)
17587 bfd_put_32 (input_bfd, NOP, p + 4);
17588 unresolved_reloc = save_unresolved_reloc;
17589 r_type = R_PPC64_REL24;
17590 }
17591 break;
17592
17593 case R_PPC64_PLTSEQ_NOTOC:
17594 case R_PPC64_PLTSEQ:
17595 if (unresolved_reloc)
17596 {
17597 unresolved_reloc = false;
17598 goto nop_it;
17599 }
17600 break;
17601
17602 case R_PPC64_PLT_PCREL34_NOTOC:
17603 if (!unresolved_reloc)
17604 htab->notoc_plt = 1;
17605 /* Fall through. */
17606 case R_PPC64_PLT_PCREL34:
17607 if (unresolved_reloc
17608 && offset_in_range (input_section, rel->r_offset, 8))
17609 {
17610 bfd_byte *p = contents + rel->r_offset;
17611 bfd_put_32 (input_bfd, PNOP >> 32, p);
17612 bfd_put_32 (input_bfd, PNOP, p + 4);
17613 unresolved_reloc = false;
17614 goto copy_reloc;
17615 }
17616 break;
17617
17618 case R_PPC64_PLT16_HA:
17619 if (unresolved_reloc)
17620 {
17621 unresolved_reloc = false;
17622 goto nop_it;
17623 }
17624 /* Fall through. */
17625 case R_PPC64_GOT_TLSLD16_HA:
17626 case R_PPC64_GOT_TLSGD16_HA:
17627 case R_PPC64_GOT_TPREL16_HA:
17628 case R_PPC64_GOT_DTPREL16_HA:
17629 case R_PPC64_GOT16_HA:
17630 case R_PPC64_TOC16_HA:
17631 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17632 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17633 && !(bfd_link_pic (info)
17634 && (h != NULL
17635 ? bfd_is_abs_symbol (&h->elf.root)
17636 : sec == bfd_abs_section_ptr)))
17637 {
17638 bfd_byte *p;
17639 nop_it:
17640 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17641 {
17642 p = contents + (rel->r_offset & ~3);
17643 bfd_put_32 (input_bfd, NOP, p);
17644 goto copy_reloc;
17645 }
17646 }
17647 break;
17648
17649 case R_PPC64_PLT16_LO:
17650 case R_PPC64_PLT16_LO_DS:
17651 if (unresolved_reloc)
17652 {
17653 unresolved_reloc = false;
17654 goto nop_it;
17655 }
17656 /* Fall through. */
17657 case R_PPC64_GOT_TLSLD16_LO:
17658 case R_PPC64_GOT_TLSGD16_LO:
17659 case R_PPC64_GOT_TPREL16_LO_DS:
17660 case R_PPC64_GOT_DTPREL16_LO_DS:
17661 case R_PPC64_GOT16_LO:
17662 case R_PPC64_GOT16_LO_DS:
17663 case R_PPC64_TOC16_LO:
17664 case R_PPC64_TOC16_LO_DS:
17665 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17666 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17667 && !(bfd_link_pic (info)
17668 && (h != NULL
17669 ? bfd_is_abs_symbol (&h->elf.root)
17670 : sec == bfd_abs_section_ptr))
17671 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17672 {
17673 bfd_byte *p = contents + (rel->r_offset & ~3);
17674 insn = bfd_get_32 (input_bfd, p);
17675 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17676 {
17677 /* Transform addic to addi when we change reg. */
17678 insn &= ~((0x3fu << 26) | (0x1f << 16));
17679 insn |= (14u << 26) | (2 << 16);
17680 }
17681 else
17682 {
17683 insn &= ~(0x1f << 16);
17684 insn |= 2 << 16;
17685 }
17686 bfd_put_32 (input_bfd, insn, p);
17687 }
17688 break;
17689
17690 case R_PPC64_TPREL16_HA:
17691 if (htab->do_tls_opt
17692 && relocation + addend + 0x8000 < 0x10000
17693 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17694 {
17695 bfd_byte *p = contents + (rel->r_offset & ~3);
17696 bfd_put_32 (input_bfd, NOP, p);
17697 goto copy_reloc;
17698 }
17699 break;
17700
17701 case R_PPC64_TPREL16_LO:
17702 case R_PPC64_TPREL16_LO_DS:
17703 if (htab->do_tls_opt
17704 && relocation + addend + 0x8000 < 0x10000
17705 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17706 {
17707 bfd_byte *p = contents + (rel->r_offset & ~3);
17708 insn = bfd_get_32 (input_bfd, p);
17709 insn &= ~(0x1f << 16);
17710 insn |= 13 << 16;
17711 bfd_put_32 (input_bfd, insn, p);
17712 }
17713 break;
17714 }
17715
17716 /* Do any further special processing. */
17717 switch (r_type)
17718 {
17719 default:
17720 break;
17721
17722 case R_PPC64_REL16_HA:
17723 case R_PPC64_REL16_HIGHA:
17724 case R_PPC64_REL16_HIGHERA:
17725 case R_PPC64_REL16_HIGHESTA:
17726 case R_PPC64_REL16DX_HA:
17727 case R_PPC64_ADDR16_HA:
17728 case R_PPC64_ADDR16_HIGHA:
17729 case R_PPC64_ADDR16_HIGHERA:
17730 case R_PPC64_ADDR16_HIGHESTA:
17731 case R_PPC64_TOC16_HA:
17732 case R_PPC64_SECTOFF_HA:
17733 case R_PPC64_TPREL16_HA:
17734 case R_PPC64_TPREL16_HIGHA:
17735 case R_PPC64_TPREL16_HIGHERA:
17736 case R_PPC64_TPREL16_HIGHESTA:
17737 case R_PPC64_DTPREL16_HA:
17738 case R_PPC64_DTPREL16_HIGHA:
17739 case R_PPC64_DTPREL16_HIGHERA:
17740 case R_PPC64_DTPREL16_HIGHESTA:
17741 /* It's just possible that this symbol is a weak symbol
17742 that's not actually defined anywhere. In that case,
17743 'sec' would be NULL, and we should leave the symbol
17744 alone (it will be set to zero elsewhere in the link). */
17745 if (sec == NULL)
17746 break;
17747 /* Fall through. */
17748
17749 case R_PPC64_GOT16_HA:
17750 case R_PPC64_PLTGOT16_HA:
17751 case R_PPC64_PLT16_HA:
17752 case R_PPC64_GOT_TLSGD16_HA:
17753 case R_PPC64_GOT_TLSLD16_HA:
17754 case R_PPC64_GOT_TPREL16_HA:
17755 case R_PPC64_GOT_DTPREL16_HA:
17756 /* Add 0x10000 if sign bit in 0:15 is set.
17757 Bits 0:15 are not used. */
17758 addend += 0x8000;
17759 break;
17760
17761 case R_PPC64_D34_HA30:
17762 case R_PPC64_ADDR16_HIGHERA34:
17763 case R_PPC64_ADDR16_HIGHESTA34:
17764 case R_PPC64_REL16_HIGHERA34:
17765 case R_PPC64_REL16_HIGHESTA34:
17766 if (sec != NULL)
17767 addend += 1ULL << 33;
17768 break;
17769
17770 case R_PPC64_ADDR16_DS:
17771 case R_PPC64_ADDR16_LO_DS:
17772 case R_PPC64_GOT16_DS:
17773 case R_PPC64_GOT16_LO_DS:
17774 case R_PPC64_PLT16_LO_DS:
17775 case R_PPC64_SECTOFF_DS:
17776 case R_PPC64_SECTOFF_LO_DS:
17777 case R_PPC64_TOC16_DS:
17778 case R_PPC64_TOC16_LO_DS:
17779 case R_PPC64_PLTGOT16_DS:
17780 case R_PPC64_PLTGOT16_LO_DS:
17781 case R_PPC64_GOT_TPREL16_DS:
17782 case R_PPC64_GOT_TPREL16_LO_DS:
17783 case R_PPC64_GOT_DTPREL16_DS:
17784 case R_PPC64_GOT_DTPREL16_LO_DS:
17785 case R_PPC64_TPREL16_DS:
17786 case R_PPC64_TPREL16_LO_DS:
17787 case R_PPC64_DTPREL16_DS:
17788 case R_PPC64_DTPREL16_LO_DS:
17789 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17790 break;
17791 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17792 mask = 3;
17793 /* If this reloc is against an lq, lxv, or stxv insn, then
17794 the value must be a multiple of 16. This is somewhat of
17795 a hack, but the "correct" way to do this by defining _DQ
17796 forms of all the _DS relocs bloats all reloc switches in
17797 this file. It doesn't make much sense to use these
17798 relocs in data, so testing the insn should be safe. */
17799 if ((insn & (0x3fu << 26)) == (56u << 26)
17800 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17801 mask = 15;
17802 relocation += addend;
17803 addend = insn & (mask ^ 3);
17804 if ((relocation & mask) != 0)
17805 {
17806 relocation ^= relocation & mask;
17807 info->callbacks->einfo
17808 /* xgettext:c-format */
17809 (_("%H: error: %s not a multiple of %u\n"),
17810 input_bfd, input_section, rel->r_offset,
17811 ppc64_elf_howto_table[r_type]->name,
17812 mask + 1);
17813 bfd_set_error (bfd_error_bad_value);
17814 ret = false;
17815 goto copy_reloc;
17816 }
17817 break;
17818 }
17819
17820 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17821 because such sections are not SEC_ALLOC and thus ld.so will
17822 not process them. */
17823 howto = ppc64_elf_howto_table[(int) r_type];
17824 if (unresolved_reloc
17825 && !((input_section->flags & SEC_DEBUGGING) != 0
17826 && h->elf.def_dynamic)
17827 && _bfd_elf_section_offset (output_bfd, info, input_section,
17828 rel->r_offset) != (bfd_vma) -1)
17829 {
17830 info->callbacks->einfo
17831 /* xgettext:c-format */
17832 (_("%H: unresolvable %s against `%pT'\n"),
17833 input_bfd, input_section, rel->r_offset,
17834 howto->name,
17835 h->elf.root.root.string);
17836 ret = false;
17837 }
17838
17839 /* 16-bit fields in insns mostly have signed values, but a
17840 few insns have 16-bit unsigned values. Really, we should
17841 have different reloc types. */
17842 if (howto->complain_on_overflow != complain_overflow_dont
17843 && howto->dst_mask == 0xffff
17844 && (input_section->flags & SEC_CODE) != 0
17845 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17846 {
17847 enum complain_overflow complain = complain_overflow_signed;
17848
17849 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17850 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17851 complain = complain_overflow_bitfield;
17852 else if (howto->rightshift == 0
17853 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17854 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17855 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17856 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17857 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17858 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17859 complain = complain_overflow_unsigned;
17860 if (howto->complain_on_overflow != complain)
17861 {
17862 alt_howto = *howto;
17863 alt_howto.complain_on_overflow = complain;
17864 howto = &alt_howto;
17865 }
17866 }
17867
17868 switch (r_type)
17869 {
17870 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17871 case R_PPC64_D34:
17872 case R_PPC64_D34_LO:
17873 case R_PPC64_D34_HI30:
17874 case R_PPC64_D34_HA30:
17875 case R_PPC64_PCREL34:
17876 case R_PPC64_GOT_PCREL34:
17877 case R_PPC64_TPREL34:
17878 case R_PPC64_DTPREL34:
17879 case R_PPC64_GOT_TLSGD_PCREL34:
17880 case R_PPC64_GOT_TLSLD_PCREL34:
17881 case R_PPC64_GOT_TPREL_PCREL34:
17882 case R_PPC64_GOT_DTPREL_PCREL34:
17883 case R_PPC64_PLT_PCREL34:
17884 case R_PPC64_PLT_PCREL34_NOTOC:
17885 case R_PPC64_D28:
17886 case R_PPC64_PCREL28:
17887 if (!offset_in_range (input_section, rel->r_offset, 8))
17888 r = bfd_reloc_outofrange;
17889 else
17890 {
17891 relocation += addend;
17892 if (howto->pc_relative)
17893 relocation -= (rel->r_offset
17894 + input_section->output_offset
17895 + input_section->output_section->vma);
17896 relocation >>= howto->rightshift;
17897
17898 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17899 pinsn <<= 32;
17900 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17901
17902 pinsn &= ~howto->dst_mask;
17903 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17904 & howto->dst_mask);
17905 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17906 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17907 r = bfd_reloc_ok;
17908 if (howto->complain_on_overflow == complain_overflow_signed
17909 && (relocation + (1ULL << (howto->bitsize - 1))
17910 >= 1ULL << howto->bitsize))
17911 r = bfd_reloc_overflow;
17912 }
17913 break;
17914
17915 case R_PPC64_REL16DX_HA:
17916 if (!offset_in_range (input_section, rel->r_offset, 4))
17917 r = bfd_reloc_outofrange;
17918 else
17919 {
17920 relocation += addend;
17921 relocation -= (rel->r_offset
17922 + input_section->output_offset
17923 + input_section->output_section->vma);
17924 relocation = (bfd_signed_vma) relocation >> 16;
17925 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17926 insn &= ~0x1fffc1;
17927 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17928 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17929 r = bfd_reloc_ok;
17930 if (relocation + 0x8000 > 0xffff)
17931 r = bfd_reloc_overflow;
17932 }
17933 break;
17934
17935 default:
17936 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17937 contents, rel->r_offset,
17938 relocation, addend);
17939 }
17940
17941 if (r != bfd_reloc_ok)
17942 {
17943 char *more_info = NULL;
17944 const char *reloc_name = howto->name;
17945
17946 if (reloc_dest != DEST_NORMAL)
17947 {
17948 more_info = bfd_malloc (strlen (reloc_name) + 8);
17949 if (more_info != NULL)
17950 {
17951 strcpy (more_info, reloc_name);
17952 strcat (more_info, (reloc_dest == DEST_OPD
17953 ? " (OPD)" : " (stub)"));
17954 reloc_name = more_info;
17955 }
17956 }
17957
17958 if (r == bfd_reloc_overflow)
17959 {
17960 /* On code like "if (foo) foo();" don't report overflow
17961 on a branch to zero when foo is undefined. */
17962 if (!warned
17963 && (reloc_dest == DEST_STUB
17964 || !(h != NULL
17965 && (h->elf.root.type == bfd_link_hash_undefweak
17966 || h->elf.root.type == bfd_link_hash_undefined)
17967 && is_branch_reloc (r_type))))
17968 info->callbacks->reloc_overflow
17969 (info, (struct bfd_link_hash_entry *) h, sym_name,
17970 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17971 rel->r_offset);
17972 }
17973 else
17974 {
17975 info->callbacks->einfo
17976 /* xgettext:c-format */
17977 (_("%H: %s against `%pT': error %d\n"),
17978 input_bfd, input_section, rel->r_offset,
17979 reloc_name, sym_name, (int) r);
17980 ret = false;
17981 }
17982 free (more_info);
17983 }
17984 copy_reloc:
17985 if (wrel != rel)
17986 *wrel = *rel;
17987 }
17988
17989 if (wrel != rel)
17990 {
17991 Elf_Internal_Shdr *rel_hdr;
17992 size_t deleted = rel - wrel;
17993
17994 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17995 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17996 if (rel_hdr->sh_size == 0)
17997 {
17998 /* It is too late to remove an empty reloc section. Leave
17999 one NONE reloc.
18000 ??? What is wrong with an empty section??? */
18001 rel_hdr->sh_size = rel_hdr->sh_entsize;
18002 deleted -= 1;
18003 }
18004 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
18005 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
18006 input_section->reloc_count -= deleted;
18007 }
18008
18009 /* If we're emitting relocations, then shortly after this function
18010 returns, reloc offsets and addends for this section will be
18011 adjusted. Worse, reloc symbol indices will be for the output
18012 file rather than the input. Save a copy of the relocs for
18013 opd_entry_value. */
18014 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
18015 {
18016 bfd_size_type amt;
18017 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
18018 rel = bfd_alloc (input_bfd, amt);
18019 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
18020 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
18021 if (rel == NULL)
18022 return false;
18023 memcpy (rel, relocs, amt);
18024 }
18025 return ret;
18026 }
18027
18028 /* Adjust the value of any local symbols in opd sections. */
18029
18030 static int
18031 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
18032 const char *name ATTRIBUTE_UNUSED,
18033 Elf_Internal_Sym *elfsym,
18034 asection *input_sec,
18035 struct elf_link_hash_entry *h)
18036 {
18037 struct _opd_sec_data *opd;
18038 long adjust;
18039 bfd_vma value;
18040
18041 if (h != NULL)
18042 return 1;
18043
18044 opd = get_opd_info (input_sec);
18045 if (opd == NULL || opd->adjust == NULL)
18046 return 1;
18047
18048 value = elfsym->st_value - input_sec->output_offset;
18049 if (!bfd_link_relocatable (info))
18050 value -= input_sec->output_section->vma;
18051
18052 adjust = opd->adjust[OPD_NDX (value)];
18053 if (adjust == -1)
18054 return 2;
18055
18056 elfsym->st_value += adjust;
18057 return 1;
18058 }
18059
18060 /* Finish up dynamic symbol handling. We set the contents of various
18061 dynamic sections here. */
18062
18063 static bool
18064 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
18065 struct bfd_link_info *info,
18066 struct elf_link_hash_entry *h,
18067 Elf_Internal_Sym *sym)
18068 {
18069 struct ppc_link_hash_table *htab;
18070 struct plt_entry *ent;
18071
18072 htab = ppc_hash_table (info);
18073 if (htab == NULL)
18074 return false;
18075
18076 if (!htab->opd_abi && !h->def_regular)
18077 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
18078 if (ent->plt.offset != (bfd_vma) -1)
18079 {
18080 /* Mark the symbol as undefined, rather than as
18081 defined in glink. Leave the value if there were
18082 any relocations where pointer equality matters
18083 (this is a clue for the dynamic linker, to make
18084 function pointer comparisons work between an
18085 application and shared library), otherwise set it
18086 to zero. */
18087 sym->st_shndx = SHN_UNDEF;
18088 if (!h->pointer_equality_needed)
18089 sym->st_value = 0;
18090 else if (!h->ref_regular_nonweak)
18091 {
18092 /* This breaks function pointer comparisons, but
18093 that is better than breaking tests for a NULL
18094 function pointer. */
18095 sym->st_value = 0;
18096 }
18097 break;
18098 }
18099
18100 if (h->needs_copy
18101 && (h->root.type == bfd_link_hash_defined
18102 || h->root.type == bfd_link_hash_defweak)
18103 && (h->root.u.def.section == htab->elf.sdynbss
18104 || h->root.u.def.section == htab->elf.sdynrelro))
18105 {
18106 /* This symbol needs a copy reloc. Set it up. */
18107 Elf_Internal_Rela rela;
18108 asection *srel;
18109 bfd_byte *loc;
18110
18111 if (h->dynindx == -1)
18112 abort ();
18113
18114 rela.r_offset = defined_sym_val (h);
18115 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
18116 rela.r_addend = 0;
18117 if (h->root.u.def.section == htab->elf.sdynrelro)
18118 srel = htab->elf.sreldynrelro;
18119 else
18120 srel = htab->elf.srelbss;
18121 loc = srel->contents;
18122 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
18123 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
18124 }
18125
18126 return true;
18127 }
18128
18129 /* Used to decide how to sort relocs in an optimal manner for the
18130 dynamic linker, before writing them out. */
18131
18132 static enum elf_reloc_type_class
18133 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
18134 const asection *rel_sec,
18135 const Elf_Internal_Rela *rela)
18136 {
18137 enum elf_ppc64_reloc_type r_type;
18138 struct ppc_link_hash_table *htab = ppc_hash_table (info);
18139
18140 if (rel_sec == htab->elf.irelplt)
18141 return reloc_class_ifunc;
18142
18143 r_type = ELF64_R_TYPE (rela->r_info);
18144 switch (r_type)
18145 {
18146 case R_PPC64_RELATIVE:
18147 return reloc_class_relative;
18148 case R_PPC64_JMP_SLOT:
18149 return reloc_class_plt;
18150 case R_PPC64_COPY:
18151 return reloc_class_copy;
18152 default:
18153 return reloc_class_normal;
18154 }
18155 }
18156
18157 /* Finish up the dynamic sections. */
18158
18159 static bool
18160 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
18161 struct bfd_link_info *info)
18162 {
18163 struct ppc_link_hash_table *htab;
18164 bfd *dynobj;
18165 asection *sdyn;
18166
18167 htab = ppc_hash_table (info);
18168 if (htab == NULL)
18169 return false;
18170
18171 dynobj = htab->elf.dynobj;
18172 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
18173
18174 if (htab->elf.dynamic_sections_created)
18175 {
18176 Elf64_External_Dyn *dyncon, *dynconend;
18177
18178 if (sdyn == NULL || htab->elf.sgot == NULL)
18179 abort ();
18180
18181 dyncon = (Elf64_External_Dyn *) sdyn->contents;
18182 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
18183 for (; dyncon < dynconend; dyncon++)
18184 {
18185 Elf_Internal_Dyn dyn;
18186 asection *s;
18187
18188 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
18189
18190 switch (dyn.d_tag)
18191 {
18192 default:
18193 continue;
18194
18195 case DT_PPC64_GLINK:
18196 s = htab->glink;
18197 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18198 /* We stupidly defined DT_PPC64_GLINK to be the start
18199 of glink rather than the first entry point, which is
18200 what ld.so needs, and now have a bigger stub to
18201 support automatic multiple TOCs. */
18202 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
18203 break;
18204
18205 case DT_PPC64_OPD:
18206 s = bfd_get_section_by_name (output_bfd, ".opd");
18207 if (s == NULL)
18208 continue;
18209 dyn.d_un.d_ptr = s->vma;
18210 break;
18211
18212 case DT_PPC64_OPT:
18213 if ((htab->do_multi_toc && htab->multi_toc_needed)
18214 || htab->notoc_plt)
18215 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
18216 if (htab->has_plt_localentry0)
18217 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
18218 break;
18219
18220 case DT_PPC64_OPDSZ:
18221 s = bfd_get_section_by_name (output_bfd, ".opd");
18222 if (s == NULL)
18223 continue;
18224 dyn.d_un.d_val = s->size;
18225 break;
18226
18227 case DT_PLTGOT:
18228 s = htab->elf.splt;
18229 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18230 break;
18231
18232 case DT_JMPREL:
18233 s = htab->elf.srelplt;
18234 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
18235 break;
18236
18237 case DT_PLTRELSZ:
18238 dyn.d_un.d_val = htab->elf.srelplt->size;
18239 break;
18240
18241 case DT_TEXTREL:
18242 if (htab->elf.ifunc_resolvers)
18243 info->callbacks->einfo
18244 (_("%P: warning: text relocations and GNU indirect "
18245 "functions may result in a segfault at runtime\n"));
18246 continue;
18247 }
18248
18249 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
18250 }
18251 }
18252
18253 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
18254 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
18255 {
18256 /* Fill in the first entry in the global offset table.
18257 We use it to hold the link-time TOCbase. */
18258 bfd_put_64 (output_bfd,
18259 elf_gp (output_bfd) + TOC_BASE_OFF,
18260 htab->elf.sgot->contents);
18261
18262 /* Set .got entry size. */
18263 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
18264 = 8;
18265 }
18266
18267 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
18268 && htab->elf.splt->output_section != bfd_abs_section_ptr)
18269 {
18270 /* Set .plt entry size. */
18271 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
18272 = PLT_ENTRY_SIZE (htab);
18273 }
18274
18275 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
18276 brlt ourselves if emitrelocations. */
18277 if (htab->brlt != NULL
18278 && htab->brlt->reloc_count != 0
18279 && !_bfd_elf_link_output_relocs (output_bfd,
18280 htab->brlt,
18281 elf_section_data (htab->brlt)->rela.hdr,
18282 elf_section_data (htab->brlt)->relocs,
18283 NULL))
18284 return false;
18285
18286 if (htab->glink != NULL
18287 && htab->glink->reloc_count != 0
18288 && !_bfd_elf_link_output_relocs (output_bfd,
18289 htab->glink,
18290 elf_section_data (htab->glink)->rela.hdr,
18291 elf_section_data (htab->glink)->relocs,
18292 NULL))
18293 return false;
18294
18295
18296 if (htab->glink_eh_frame != NULL
18297 && htab->glink_eh_frame->size != 0
18298 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
18299 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
18300 htab->glink_eh_frame,
18301 htab->glink_eh_frame->contents))
18302 return false;
18303
18304 /* We need to handle writing out multiple GOT sections ourselves,
18305 since we didn't add them to DYNOBJ. We know dynobj is the first
18306 bfd. */
18307 while ((dynobj = dynobj->link.next) != NULL)
18308 {
18309 asection *s;
18310
18311 if (!is_ppc64_elf (dynobj))
18312 continue;
18313
18314 s = ppc64_elf_tdata (dynobj)->got;
18315 if (s != NULL
18316 && s->size != 0
18317 && s->output_section != bfd_abs_section_ptr
18318 && !bfd_set_section_contents (output_bfd, s->output_section,
18319 s->contents, s->output_offset,
18320 s->size))
18321 return false;
18322 s = ppc64_elf_tdata (dynobj)->relgot;
18323 if (s != NULL
18324 && s->size != 0
18325 && s->output_section != bfd_abs_section_ptr
18326 && !bfd_set_section_contents (output_bfd, s->output_section,
18327 s->contents, s->output_offset,
18328 s->size))
18329 return false;
18330 }
18331
18332 return true;
18333 }
18334
18335 #include "elf64-target.h"
18336
18337 /* FreeBSD support */
18338
18339 #undef TARGET_LITTLE_SYM
18340 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
18341 #undef TARGET_LITTLE_NAME
18342 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
18343
18344 #undef TARGET_BIG_SYM
18345 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
18346 #undef TARGET_BIG_NAME
18347 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
18348
18349 #undef ELF_OSABI
18350 #define ELF_OSABI ELFOSABI_FREEBSD
18351
18352 #undef elf64_bed
18353 #define elf64_bed elf64_powerpc_fbsd_bed
18354
18355 #include "elf64-target.h"