bfd: use https for bugzilla
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
1 /* PowerPC64-specific support for 64-bit ELF.
2 Copyright (C) 1999-2021 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 /* Don't generate unused section symbols. */
29 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS false
30
31 #include "sysdep.h"
32 #include <stdarg.h>
33 #include "bfd.h"
34 #include "bfdlink.h"
35 #include "libbfd.h"
36 #include "elf-bfd.h"
37 #include "elf/ppc64.h"
38 #include "elf64-ppc.h"
39 #include "dwarf2.h"
40
41 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
42 #define OCTETS_PER_BYTE(ABFD, SEC) 1
43
44 static bfd_reloc_status_type ppc64_elf_ha_reloc
45 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46 static bfd_reloc_status_type ppc64_elf_branch_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_toc_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type ppc64_elf_toc64_reloc
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type ppc64_elf_prefix_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_vma opd_entry_value
65 (asection *, bfd_vma, asection **, bfd_vma *, bool);
66
67 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
68 #define TARGET_LITTLE_NAME "elf64-powerpcle"
69 #define TARGET_BIG_SYM powerpc_elf64_vec
70 #define TARGET_BIG_NAME "elf64-powerpc"
71 #define ELF_ARCH bfd_arch_powerpc
72 #define ELF_TARGET_ID PPC64_ELF_DATA
73 #define ELF_MACHINE_CODE EM_PPC64
74 #define ELF_MAXPAGESIZE 0x10000
75 #define ELF_COMMONPAGESIZE 0x1000
76 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
77 #define elf_info_to_howto ppc64_elf_info_to_howto
78
79 #define elf_backend_want_got_sym 0
80 #define elf_backend_want_plt_sym 0
81 #define elf_backend_plt_alignment 3
82 #define elf_backend_plt_not_loaded 1
83 #define elf_backend_got_header_size 8
84 #define elf_backend_want_dynrelro 1
85 #define elf_backend_can_gc_sections 1
86 #define elf_backend_can_refcount 1
87 #define elf_backend_rela_normal 1
88 #define elf_backend_dtrel_excludes_plt 1
89 #define elf_backend_default_execstack 0
90
91 #define bfd_elf64_mkobject ppc64_elf_mkobject
92 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
93 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
94 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
95 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
96 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
97 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
98 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
99 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
100 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
101
102 #define elf_backend_object_p ppc64_elf_object_p
103 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
104 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
105 #define elf_backend_write_core_note ppc64_elf_write_core_note
106 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
107 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
108 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
109 #define elf_backend_check_directives ppc64_elf_before_check_relocs
110 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
111 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
112 #define elf_backend_check_relocs ppc64_elf_check_relocs
113 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
114 #define elf_backend_gc_keep ppc64_elf_gc_keep
115 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
116 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
117 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
118 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
119 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
120 #define elf_backend_always_size_sections ppc64_elf_edit
121 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
122 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
123 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
124 #define elf_backend_action_discarded ppc64_elf_action_discarded
125 #define elf_backend_relocate_section ppc64_elf_relocate_section
126 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
127 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
128 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
129 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
130 #define elf_backend_special_sections ppc64_elf_special_sections
131 #define elf_backend_section_flags ppc64_elf_section_flags
132 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
133 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
134 #define elf_backend_get_reloc_section bfd_get_section_by_name
135
136 /* The name of the dynamic interpreter. This is put in the .interp
137 section. */
138 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
139
140 /* The size in bytes of an entry in the procedure linkage table. */
141 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
142 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
143
144 /* The initial size of the plt reserved for the dynamic linker. */
145 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
146
147 /* Offsets to some stack save slots. */
148 #define STK_LR 16
149 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
150 /* This one is dodgy. ELFv2 does not have a linker word, so use the
151 CR save slot. Used only by optimised __tls_get_addr call stub,
152 relying on __tls_get_addr_opt not saving CR.. */
153 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
154
155 /* TOC base pointers offset from start of TOC. */
156 #define TOC_BASE_OFF 0x8000
157 /* TOC base alignment. */
158 #define TOC_BASE_ALIGN 256
159
160 /* Offset of tp and dtp pointers from start of TLS block. */
161 #define TP_OFFSET 0x7000
162 #define DTP_OFFSET 0x8000
163
164 /* .plt call stub instructions. The normal stub is like this, but
165 sometimes the .plt entry crosses a 64k boundary and we need to
166 insert an addi to adjust r11. */
167 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
168 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
169 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
170 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
171 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
172 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
173 #define BCTR 0x4e800420 /* bctr */
174
175 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
176 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
177 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
178 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
179 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
180
181 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
182 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
183 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
184 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
185 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
186 #define BNECTR 0x4ca20420 /* bnectr+ */
187 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
188
189 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
190 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
191 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
192
193 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
194 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
195 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
196
197 #define LI_R11_0 0x39600000 /* li %r11,0 */
198 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
199 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
200 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
201 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
202 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
203 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
204 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
205 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
206 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
207 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
208 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
209 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
210 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
211 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
212 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
213 #define PADDI_R12_PC 0x0610000039800000ULL
214 #define PLD_R12_PC 0x04100000e5800000ULL
215 #define PNOP 0x0700000000000000ULL
216
217 /* __glink_PLTresolve stub instructions. We enter with the index in
218 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
219 #define GLINK_PLTRESOLVE_SIZE(htab) \
220 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
221 /* 0: */
222 /* .quad plt0-1f */
223 /* __glink: */
224 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
225 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
226 /* 1: */
227 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
228 /* ld %2,(0b-1b)(%11) */
229 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
230 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
231 /* ld %12,0(%11) */
232 /* ld %2,8(%11) */
233 /* mtctr %12 */
234 /* ld %11,16(%11) */
235 /* bctr */
236
237 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
238 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
239 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
240 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
241 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
242 #define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
243 #define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
244
245 /* Pad with this. */
246 #define NOP 0x60000000
247
248 /* Some other nops. */
249 #define CROR_151515 0x4def7b82
250 #define CROR_313131 0x4ffffb82
251
252 /* .glink entries for the first 32k functions are two instructions. */
253 #define LI_R0_0 0x38000000 /* li %r0,0 */
254 #define B_DOT 0x48000000 /* b . */
255
256 /* After that, we need two instructions to load the index, followed by
257 a branch. */
258 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
259 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
260
261 /* Instructions used by the save and restore reg functions. */
262 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
263 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
264 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
265 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
266 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
267 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
268 #define LI_R12_0 0x39800000 /* li %r12,0 */
269 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
270 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
271 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
272 #define BLR 0x4e800020 /* blr */
273
274 /* Since .opd is an array of descriptors and each entry will end up
275 with identical R_PPC64_RELATIVE relocs, there is really no need to
276 propagate .opd relocs; The dynamic linker should be taught to
277 relocate .opd without reloc entries. */
278 #ifndef NO_OPD_RELOCS
279 #define NO_OPD_RELOCS 0
280 #endif
281
282 #ifndef ARRAY_SIZE
283 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
284 #endif
285
286 static inline int
287 abiversion (bfd *abfd)
288 {
289 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
290 }
291
292 static inline void
293 set_abiversion (bfd *abfd, int ver)
294 {
295 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
296 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
297 }
298 \f
299 /* Relocation HOWTO's. */
300 /* Like other ELF RELA targets that don't apply multiple
301 field-altering relocations to the same localation, src_mask is
302 always zero and pcrel_offset is the same as pc_relative.
303 PowerPC can always use a zero bitpos, even when the field is not at
304 the LSB. For example, a REL24 could use rightshift=2, bisize=24
305 and bitpos=2 which matches the ABI description, or as we do here,
306 rightshift=0, bitsize=26 and bitpos=0. */
307 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
308 complain, special_func) \
309 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
310 complain_overflow_ ## complain, special_func, \
311 #type, false, 0, mask, pc_relative)
312
313 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
314
315 static reloc_howto_type ppc64_elf_howto_raw[] =
316 {
317 /* This reloc does nothing. */
318 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
319 bfd_elf_generic_reloc),
320
321 /* A standard 32 bit relocation. */
322 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
323 bfd_elf_generic_reloc),
324
325 /* An absolute 26 bit branch; the lower two bits must be zero.
326 FIXME: we don't check that, we just clear them. */
327 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
328 bfd_elf_generic_reloc),
329
330 /* A standard 16 bit relocation. */
331 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
332 bfd_elf_generic_reloc),
333
334 /* A 16 bit relocation without overflow. */
335 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
336 bfd_elf_generic_reloc),
337
338 /* Bits 16-31 of an address. */
339 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
340 bfd_elf_generic_reloc),
341
342 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
343 bits, treated as a signed number, is negative. */
344 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
345 ppc64_elf_ha_reloc),
346
347 /* An absolute 16 bit branch; the lower two bits must be zero.
348 FIXME: we don't check that, we just clear them. */
349 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
350 ppc64_elf_branch_reloc),
351
352 /* An absolute 16 bit branch, for which bit 10 should be set to
353 indicate that the branch is expected to be taken. The lower two
354 bits must be zero. */
355 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
356 ppc64_elf_brtaken_reloc),
357
358 /* An absolute 16 bit branch, for which bit 10 should be set to
359 indicate that the branch is not expected to be taken. The lower
360 two bits must be zero. */
361 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
362 ppc64_elf_brtaken_reloc),
363
364 /* A relative 26 bit branch; the lower two bits must be zero. */
365 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
366 ppc64_elf_branch_reloc),
367
368 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
369 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
370 ppc64_elf_branch_reloc),
371
372 /* A relative 16 bit branch; the lower two bits must be zero. */
373 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
374 ppc64_elf_branch_reloc),
375
376 /* A relative 16 bit branch. Bit 10 should be set to indicate that
377 the branch is expected to be taken. The lower two bits must be
378 zero. */
379 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
380 ppc64_elf_brtaken_reloc),
381
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is not expected to be taken. The lower two bits must
384 be zero. */
385 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
386 ppc64_elf_brtaken_reloc),
387
388 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
389 symbol. */
390 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
391 ppc64_elf_unhandled_reloc),
392
393 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
394 the symbol. */
395 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
396 ppc64_elf_unhandled_reloc),
397
398 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
399 the symbol. */
400 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
401 ppc64_elf_unhandled_reloc),
402
403 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
404 the symbol. */
405 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
406 ppc64_elf_unhandled_reloc),
407
408 /* This is used only by the dynamic linker. The symbol should exist
409 both in the object being run and in some shared library. The
410 dynamic linker copies the data addressed by the symbol from the
411 shared library into the object, because the object being
412 run has to have the data at some particular address. */
413 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
414 ppc64_elf_unhandled_reloc),
415
416 /* Like R_PPC64_ADDR64, but used when setting global offset table
417 entries. */
418 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
419 ppc64_elf_unhandled_reloc),
420
421 /* Created by the link editor. Marks a procedure linkage table
422 entry for a symbol. */
423 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
424 ppc64_elf_unhandled_reloc),
425
426 /* Used only by the dynamic linker. When the object is run, this
427 doubleword64 is set to the load address of the object, plus the
428 addend. */
429 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
430 bfd_elf_generic_reloc),
431
432 /* Like R_PPC64_ADDR32, but may be unaligned. */
433 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
434 bfd_elf_generic_reloc),
435
436 /* Like R_PPC64_ADDR16, but may be unaligned. */
437 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
438 bfd_elf_generic_reloc),
439
440 /* 32-bit PC relative. */
441 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
442 bfd_elf_generic_reloc),
443
444 /* 32-bit relocation to the symbol's procedure linkage table. */
445 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
446 ppc64_elf_unhandled_reloc),
447
448 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
449 FIXME: R_PPC64_PLTREL32 not supported. */
450 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
451 ppc64_elf_unhandled_reloc),
452
453 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
454 the symbol. */
455 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
456 ppc64_elf_unhandled_reloc),
457
458 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
459 the symbol. */
460 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
461 ppc64_elf_unhandled_reloc),
462
463 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
464 the symbol. */
465 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
466 ppc64_elf_unhandled_reloc),
467
468 /* 16-bit section relative relocation. */
469 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
470 ppc64_elf_sectoff_reloc),
471
472 /* Like R_PPC64_SECTOFF, but no overflow warning. */
473 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
474 ppc64_elf_sectoff_reloc),
475
476 /* 16-bit upper half section relative relocation. */
477 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
478 ppc64_elf_sectoff_reloc),
479
480 /* 16-bit upper half adjusted section relative relocation. */
481 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
482 ppc64_elf_sectoff_ha_reloc),
483
484 /* Like R_PPC64_REL24 without touching the two least significant bits. */
485 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
486 bfd_elf_generic_reloc),
487
488 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
489
490 /* A standard 64-bit relocation. */
491 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
492 bfd_elf_generic_reloc),
493
494 /* The bits 32-47 of an address. */
495 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
496 bfd_elf_generic_reloc),
497
498 /* The bits 32-47 of an address, plus 1 if the contents of the low
499 16 bits, treated as a signed number, is negative. */
500 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
501 ppc64_elf_ha_reloc),
502
503 /* The bits 48-63 of an address. */
504 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
505 bfd_elf_generic_reloc),
506
507 /* The bits 48-63 of an address, plus 1 if the contents of the low
508 16 bits, treated as a signed number, is negative. */
509 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
510 ppc64_elf_ha_reloc),
511
512 /* Like ADDR64, but may be unaligned. */
513 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
514 bfd_elf_generic_reloc),
515
516 /* 64-bit relative relocation. */
517 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
518 bfd_elf_generic_reloc),
519
520 /* 64-bit relocation to the symbol's procedure linkage table. */
521 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
522 ppc64_elf_unhandled_reloc),
523
524 /* 64-bit PC relative relocation to the symbol's procedure linkage
525 table. */
526 /* FIXME: R_PPC64_PLTREL64 not supported. */
527 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
528 ppc64_elf_unhandled_reloc),
529
530 /* 16 bit TOC-relative relocation. */
531 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
532 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
533 ppc64_elf_toc_reloc),
534
535 /* 16 bit TOC-relative relocation without overflow. */
536 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
537 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
538 ppc64_elf_toc_reloc),
539
540 /* 16 bit TOC-relative relocation, high 16 bits. */
541 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
542 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
543 ppc64_elf_toc_reloc),
544
545 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
546 contents of the low 16 bits, treated as a signed number, is
547 negative. */
548 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
549 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
550 ppc64_elf_toc_ha_reloc),
551
552 /* 64-bit relocation; insert value of TOC base (.TOC.). */
553 /* R_PPC64_TOC 51 doubleword64 .TOC. */
554 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
555 ppc64_elf_toc64_reloc),
556
557 /* Like R_PPC64_GOT16, but also informs the link editor that the
558 value to relocate may (!) refer to a PLT entry which the link
559 editor (a) may replace with the symbol value. If the link editor
560 is unable to fully resolve the symbol, it may (b) create a PLT
561 entry and store the address to the new PLT entry in the GOT.
562 This permits lazy resolution of function symbols at run time.
563 The link editor may also skip all of this and just (c) emit a
564 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
565 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
566 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
567 ppc64_elf_unhandled_reloc),
568
569 /* Like R_PPC64_PLTGOT16, but without overflow. */
570 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
571 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
572 ppc64_elf_unhandled_reloc),
573
574 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
575 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
576 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
577 ppc64_elf_unhandled_reloc),
578
579 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
580 1 if the contents of the low 16 bits, treated as a signed number,
581 is negative. */
582 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
583 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
584 ppc64_elf_unhandled_reloc),
585
586 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
587 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
588 bfd_elf_generic_reloc),
589
590 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
591 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
592 bfd_elf_generic_reloc),
593
594 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
595 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
596 ppc64_elf_unhandled_reloc),
597
598 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
599 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
600 ppc64_elf_unhandled_reloc),
601
602 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
603 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
604 ppc64_elf_unhandled_reloc),
605
606 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
607 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
608 ppc64_elf_sectoff_reloc),
609
610 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
611 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
612 ppc64_elf_sectoff_reloc),
613
614 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
615 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
616 ppc64_elf_toc_reloc),
617
618 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
619 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
620 ppc64_elf_toc_reloc),
621
622 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
623 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
624 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
625 ppc64_elf_unhandled_reloc),
626
627 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
628 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
629 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
630 ppc64_elf_unhandled_reloc),
631
632 /* Marker relocs for TLS. */
633 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
634 bfd_elf_generic_reloc),
635
636 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
637 bfd_elf_generic_reloc),
638
639 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
640 bfd_elf_generic_reloc),
641
642 /* Marker reloc for optimizing r2 save in prologue rather than on
643 each plt call stub. */
644 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
645 bfd_elf_generic_reloc),
646
647 /* Marker relocs on inline plt call instructions. */
648 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
649 bfd_elf_generic_reloc),
650
651 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
652 bfd_elf_generic_reloc),
653
654 /* Computes the load module index of the load module that contains the
655 definition of its TLS sym. */
656 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
657 ppc64_elf_unhandled_reloc),
658
659 /* Computes a dtv-relative displacement, the difference between the value
660 of sym+add and the base address of the thread-local storage block that
661 contains the definition of sym, minus 0x8000. */
662 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
663 ppc64_elf_unhandled_reloc),
664
665 /* A 16 bit dtprel reloc. */
666 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
667 ppc64_elf_unhandled_reloc),
668
669 /* Like DTPREL16, but no overflow. */
670 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
671 ppc64_elf_unhandled_reloc),
672
673 /* Like DTPREL16_LO, but next higher group of 16 bits. */
674 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
675 ppc64_elf_unhandled_reloc),
676
677 /* Like DTPREL16_HI, but adjust for low 16 bits. */
678 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
679 ppc64_elf_unhandled_reloc),
680
681 /* Like DTPREL16_HI, but next higher group of 16 bits. */
682 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
683 ppc64_elf_unhandled_reloc),
684
685 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
686 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
687 ppc64_elf_unhandled_reloc),
688
689 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
690 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
691 ppc64_elf_unhandled_reloc),
692
693 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
694 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
695 ppc64_elf_unhandled_reloc),
696
697 /* Like DTPREL16, but for insns with a DS field. */
698 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
699 ppc64_elf_unhandled_reloc),
700
701 /* Like DTPREL16_DS, but no overflow. */
702 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
703 ppc64_elf_unhandled_reloc),
704
705 /* Computes a tp-relative displacement, the difference between the value of
706 sym+add and the value of the thread pointer (r13). */
707 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
708 ppc64_elf_unhandled_reloc),
709
710 /* A 16 bit tprel reloc. */
711 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
712 ppc64_elf_unhandled_reloc),
713
714 /* Like TPREL16, but no overflow. */
715 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
716 ppc64_elf_unhandled_reloc),
717
718 /* Like TPREL16_LO, but next higher group of 16 bits. */
719 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
720 ppc64_elf_unhandled_reloc),
721
722 /* Like TPREL16_HI, but adjust for low 16 bits. */
723 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
724 ppc64_elf_unhandled_reloc),
725
726 /* Like TPREL16_HI, but next higher group of 16 bits. */
727 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
728 ppc64_elf_unhandled_reloc),
729
730 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
731 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
732 ppc64_elf_unhandled_reloc),
733
734 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
735 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
736 ppc64_elf_unhandled_reloc),
737
738 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
739 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
740 ppc64_elf_unhandled_reloc),
741
742 /* Like TPREL16, but for insns with a DS field. */
743 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
744 ppc64_elf_unhandled_reloc),
745
746 /* Like TPREL16_DS, but no overflow. */
747 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
748 ppc64_elf_unhandled_reloc),
749
750 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
751 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
752 to the first entry relative to the TOC base (r2). */
753 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
754 ppc64_elf_unhandled_reloc),
755
756 /* Like GOT_TLSGD16, but no overflow. */
757 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
758 ppc64_elf_unhandled_reloc),
759
760 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
761 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
762 ppc64_elf_unhandled_reloc),
763
764 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
765 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
766 ppc64_elf_unhandled_reloc),
767
768 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
769 with values (sym+add)@dtpmod and zero, and computes the offset to the
770 first entry relative to the TOC base (r2). */
771 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
772 ppc64_elf_unhandled_reloc),
773
774 /* Like GOT_TLSLD16, but no overflow. */
775 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
776 ppc64_elf_unhandled_reloc),
777
778 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
779 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
780 ppc64_elf_unhandled_reloc),
781
782 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
783 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
784 ppc64_elf_unhandled_reloc),
785
786 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
787 the offset to the entry relative to the TOC base (r2). */
788 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
789 ppc64_elf_unhandled_reloc),
790
791 /* Like GOT_DTPREL16_DS, but no overflow. */
792 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
793 ppc64_elf_unhandled_reloc),
794
795 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
796 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
797 ppc64_elf_unhandled_reloc),
798
799 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
800 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
801 ppc64_elf_unhandled_reloc),
802
803 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
804 offset to the entry relative to the TOC base (r2). */
805 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
806 ppc64_elf_unhandled_reloc),
807
808 /* Like GOT_TPREL16_DS, but no overflow. */
809 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
810 ppc64_elf_unhandled_reloc),
811
812 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
813 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
814 ppc64_elf_unhandled_reloc),
815
816 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
817 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
818 ppc64_elf_unhandled_reloc),
819
820 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
821 ppc64_elf_unhandled_reloc),
822
823 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
824 bfd_elf_generic_reloc),
825
826 /* A 16 bit relative relocation. */
827 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
828 bfd_elf_generic_reloc),
829
830 /* A 16 bit relative relocation without overflow. */
831 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
832 bfd_elf_generic_reloc),
833
834 /* The high order 16 bits of a relative address. */
835 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
836 bfd_elf_generic_reloc),
837
838 /* The high order 16 bits of a relative address, plus 1 if the contents of
839 the low 16 bits, treated as a signed number, is negative. */
840 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
841 ppc64_elf_ha_reloc),
842
843 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
844 bfd_elf_generic_reloc),
845
846 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
847 ppc64_elf_ha_reloc),
848
849 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
850 bfd_elf_generic_reloc),
851
852 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
853 ppc64_elf_ha_reloc),
854
855 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
856 bfd_elf_generic_reloc),
857
858 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
859 ppc64_elf_ha_reloc),
860
861 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
862 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
863 ppc64_elf_ha_reloc),
864
865 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
866 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
867 ppc64_elf_ha_reloc),
868
869 /* Like R_PPC64_ADDR16_HI, but no overflow. */
870 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
871 bfd_elf_generic_reloc),
872
873 /* Like R_PPC64_ADDR16_HA, but no overflow. */
874 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
875 ppc64_elf_ha_reloc),
876
877 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
878 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
879 ppc64_elf_unhandled_reloc),
880
881 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
882 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
883 ppc64_elf_unhandled_reloc),
884
885 /* Like R_PPC64_TPREL16_HI, but no overflow. */
886 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
887 ppc64_elf_unhandled_reloc),
888
889 /* Like R_PPC64_TPREL16_HA, but no overflow. */
890 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
891 ppc64_elf_unhandled_reloc),
892
893 /* Marker reloc on ELFv2 large-model function entry. */
894 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
895 bfd_elf_generic_reloc),
896
897 /* Like ADDR64, but use local entry point of function. */
898 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
899 bfd_elf_generic_reloc),
900
901 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
902 bfd_elf_generic_reloc),
903
904 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
905 bfd_elf_generic_reloc),
906
907 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
908 bfd_elf_generic_reloc),
909
910 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
911 ppc64_elf_prefix_reloc),
912
913 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
914 ppc64_elf_prefix_reloc),
915
916 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
917 ppc64_elf_prefix_reloc),
918
919 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
920 ppc64_elf_prefix_reloc),
921
922 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
923 ppc64_elf_prefix_reloc),
924
925 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
926 ppc64_elf_unhandled_reloc),
927
928 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
929 ppc64_elf_unhandled_reloc),
930
931 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
932 ppc64_elf_unhandled_reloc),
933
934 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
935 ppc64_elf_unhandled_reloc),
936
937 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
938 ppc64_elf_unhandled_reloc),
939
940 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
941 ppc64_elf_unhandled_reloc),
942
943 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
944 ppc64_elf_unhandled_reloc),
945
946 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
947 ppc64_elf_unhandled_reloc),
948
949 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
950 ppc64_elf_unhandled_reloc),
951
952 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
953 bfd_elf_generic_reloc),
954
955 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
956 ppc64_elf_ha_reloc),
957
958 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
959 bfd_elf_generic_reloc),
960
961 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
962 ppc64_elf_ha_reloc),
963
964 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
965 bfd_elf_generic_reloc),
966
967 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
968 ppc64_elf_ha_reloc),
969
970 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
971 bfd_elf_generic_reloc),
972
973 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
974 ppc64_elf_ha_reloc),
975
976 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
977 ppc64_elf_prefix_reloc),
978
979 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
980 ppc64_elf_prefix_reloc),
981
982 /* GNU extension to record C++ vtable hierarchy. */
983 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
984 NULL),
985
986 /* GNU extension to record C++ vtable member usage. */
987 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
988 NULL),
989 };
990
991 \f
992 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
993 be done. */
994
995 static void
996 ppc_howto_init (void)
997 {
998 unsigned int i, type;
999
1000 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1001 {
1002 type = ppc64_elf_howto_raw[i].type;
1003 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1004 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1005 }
1006 }
1007
1008 static reloc_howto_type *
1009 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1010 {
1011 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1012
1013 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1014 /* Initialize howto table if needed. */
1015 ppc_howto_init ();
1016
1017 switch (code)
1018 {
1019 default:
1020 /* xgettext:c-format */
1021 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1022 (int) code);
1023 bfd_set_error (bfd_error_bad_value);
1024 return NULL;
1025
1026 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1027 break;
1028 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1029 break;
1030 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1031 break;
1032 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1033 break;
1034 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1035 break;
1036 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1037 break;
1038 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1039 break;
1040 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1041 break;
1042 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1043 break;
1044 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1045 break;
1046 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1047 break;
1048 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1049 break;
1050 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1051 break;
1052 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1053 break;
1054 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1055 break;
1056 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1057 break;
1058 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1059 break;
1060 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1061 break;
1062 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1063 break;
1064 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1065 break;
1066 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1067 break;
1068 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1069 break;
1070 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1071 break;
1072 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1073 break;
1074 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1075 break;
1076 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1077 break;
1078 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1079 break;
1080 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1081 break;
1082 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1083 break;
1084 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1085 break;
1086 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1087 break;
1088 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1089 break;
1090 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1091 break;
1092 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1093 break;
1094 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1095 break;
1096 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1097 break;
1098 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1099 break;
1100 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1101 break;
1102 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1103 break;
1104 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1105 break;
1106 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1107 break;
1108 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1109 break;
1110 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1111 break;
1112 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1113 break;
1114 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1115 break;
1116 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1117 break;
1118 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1119 break;
1120 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1121 break;
1122 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1123 break;
1124 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1125 break;
1126 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1127 break;
1128 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1129 break;
1130 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1131 break;
1132 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1133 break;
1134 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1135 break;
1136 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1137 break;
1138 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1139 break;
1140 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1141 break;
1142 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1143 break;
1144 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1145 break;
1146 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1147 break;
1148 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1149 break;
1150 case BFD_RELOC_PPC64_TLS_PCREL:
1151 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1152 break;
1153 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1154 break;
1155 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1156 break;
1157 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1158 break;
1159 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1160 break;
1161 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1162 break;
1163 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1164 break;
1165 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1166 break;
1167 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1168 break;
1169 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1170 break;
1171 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1172 break;
1173 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1174 break;
1175 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1176 break;
1177 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1178 break;
1179 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1180 break;
1181 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1182 break;
1183 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1184 break;
1185 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1186 break;
1187 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1188 break;
1189 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1190 break;
1191 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1192 break;
1193 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1210 break;
1211 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1212 break;
1213 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1214 break;
1215 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1216 break;
1217 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1218 break;
1219 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1220 break;
1221 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1222 break;
1223 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1224 break;
1225 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1226 break;
1227 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1228 break;
1229 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1230 break;
1231 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1232 break;
1233 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1234 break;
1235 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1236 break;
1237 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1238 break;
1239 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1240 break;
1241 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1242 break;
1243 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1244 break;
1245 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1246 break;
1247 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1248 break;
1249 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1250 break;
1251 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1252 break;
1253 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1254 break;
1255 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1256 break;
1257 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1258 break;
1259 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1260 break;
1261 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1262 break;
1263 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1264 break;
1265 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1266 break;
1267 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1268 break;
1269 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1270 break;
1271 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1272 break;
1273 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1274 break;
1275 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1276 break;
1277 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1278 break;
1279 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1280 break;
1281 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1282 break;
1283 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1284 break;
1285 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1286 break;
1287 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1288 break;
1289 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1290 break;
1291 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1292 break;
1293 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1294 break;
1295 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1296 break;
1297 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1298 break;
1299 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1300 break;
1301 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1302 break;
1303 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1304 break;
1305 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1306 break;
1307 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1308 break;
1309 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1310 break;
1311 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1312 break;
1313 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1314 break;
1315 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1316 break;
1317 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1318 break;
1319 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1320 break;
1321 }
1322
1323 return ppc64_elf_howto_table[r];
1324 };
1325
1326 static reloc_howto_type *
1327 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1328 {
1329 unsigned int i;
1330 static char *compat_map[][2] = {
1331 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1332 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1333 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1334 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1335 };
1336
1337 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1338 if (ppc64_elf_howto_raw[i].name != NULL
1339 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1340 return &ppc64_elf_howto_raw[i];
1341
1342 /* Handle old names of relocations in case they were used by
1343 .reloc directives.
1344 FIXME: Remove this soon. Mapping the reloc names is very likely
1345 completely unnecessary. */
1346 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1347 if (strcasecmp (compat_map[i][0], r_name) == 0)
1348 {
1349 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1350 compat_map[i][1], compat_map[i][0]);
1351 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1352 }
1353
1354 return NULL;
1355 }
1356
1357 /* Set the howto pointer for a PowerPC ELF reloc. */
1358
1359 static bool
1360 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1361 Elf_Internal_Rela *dst)
1362 {
1363 unsigned int type;
1364
1365 /* Initialize howto table if needed. */
1366 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1367 ppc_howto_init ();
1368
1369 type = ELF64_R_TYPE (dst->r_info);
1370 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1371 {
1372 /* xgettext:c-format */
1373 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1374 abfd, type);
1375 bfd_set_error (bfd_error_bad_value);
1376 return false;
1377 }
1378 cache_ptr->howto = ppc64_elf_howto_table[type];
1379 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1380 {
1381 /* xgettext:c-format */
1382 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1383 abfd, type);
1384 bfd_set_error (bfd_error_bad_value);
1385 return false;
1386 }
1387
1388 return true;
1389 }
1390
1391 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1392
1393 static bfd_reloc_status_type
1394 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1395 void *data, asection *input_section,
1396 bfd *output_bfd, char **error_message)
1397 {
1398 enum elf_ppc64_reloc_type r_type;
1399 long insn;
1400 bfd_size_type octets;
1401 bfd_vma value;
1402
1403 /* If this is a relocatable link (output_bfd test tells us), just
1404 call the generic function. Any adjustment will be done at final
1405 link time. */
1406 if (output_bfd != NULL)
1407 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1408 input_section, output_bfd, error_message);
1409
1410 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1411 We won't actually be using the low bits, so trashing them
1412 doesn't matter. */
1413 r_type = reloc_entry->howto->type;
1414 if (r_type == R_PPC64_ADDR16_HIGHERA34
1415 || r_type == R_PPC64_ADDR16_HIGHESTA34
1416 || r_type == R_PPC64_REL16_HIGHERA34
1417 || r_type == R_PPC64_REL16_HIGHESTA34)
1418 reloc_entry->addend += 1ULL << 33;
1419 else
1420 reloc_entry->addend += 1U << 15;
1421 if (r_type != R_PPC64_REL16DX_HA)
1422 return bfd_reloc_continue;
1423
1424 value = 0;
1425 if (!bfd_is_com_section (symbol->section))
1426 value = symbol->value;
1427 value += (reloc_entry->addend
1428 + symbol->section->output_offset
1429 + symbol->section->output_section->vma);
1430 value -= (reloc_entry->address
1431 + input_section->output_offset
1432 + input_section->output_section->vma);
1433 value = (bfd_signed_vma) value >> 16;
1434
1435 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1436 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1437 insn &= ~0x1fffc1;
1438 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1439 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1440 if (value + 0x8000 > 0xffff)
1441 return bfd_reloc_overflow;
1442 return bfd_reloc_ok;
1443 }
1444
1445 static bfd_reloc_status_type
1446 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1447 void *data, asection *input_section,
1448 bfd *output_bfd, char **error_message)
1449 {
1450 if (output_bfd != NULL)
1451 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1452 input_section, output_bfd, error_message);
1453
1454 if (strcmp (symbol->section->name, ".opd") == 0
1455 && (symbol->section->owner->flags & DYNAMIC) == 0)
1456 {
1457 bfd_vma dest = opd_entry_value (symbol->section,
1458 symbol->value + reloc_entry->addend,
1459 NULL, NULL, false);
1460 if (dest != (bfd_vma) -1)
1461 reloc_entry->addend = dest - (symbol->value
1462 + symbol->section->output_section->vma
1463 + symbol->section->output_offset);
1464 }
1465 else
1466 {
1467 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1468
1469 if (symbol->section->owner != abfd
1470 && symbol->section->owner != NULL
1471 && abiversion (symbol->section->owner) >= 2)
1472 {
1473 unsigned int i;
1474
1475 for (i = 0; i < symbol->section->owner->symcount; ++i)
1476 {
1477 asymbol *symdef = symbol->section->owner->outsymbols[i];
1478
1479 if (strcmp (symdef->name, symbol->name) == 0)
1480 {
1481 elfsym = (elf_symbol_type *) symdef;
1482 break;
1483 }
1484 }
1485 }
1486 reloc_entry->addend
1487 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1488 }
1489 return bfd_reloc_continue;
1490 }
1491
1492 static bfd_reloc_status_type
1493 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1494 void *data, asection *input_section,
1495 bfd *output_bfd, char **error_message)
1496 {
1497 long insn;
1498 enum elf_ppc64_reloc_type r_type;
1499 bfd_size_type octets;
1500 /* Assume 'at' branch hints. */
1501 bool is_isa_v2 = true;
1502
1503 /* If this is a relocatable link (output_bfd test tells us), just
1504 call the generic function. Any adjustment will be done at final
1505 link time. */
1506 if (output_bfd != NULL)
1507 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1508 input_section, output_bfd, error_message);
1509
1510 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1511 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1512 insn &= ~(0x01 << 21);
1513 r_type = reloc_entry->howto->type;
1514 if (r_type == R_PPC64_ADDR14_BRTAKEN
1515 || r_type == R_PPC64_REL14_BRTAKEN)
1516 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1517
1518 if (is_isa_v2)
1519 {
1520 /* Set 'a' bit. This is 0b00010 in BO field for branch
1521 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1522 for branch on CTR insns (BO == 1a00t or 1a01t). */
1523 if ((insn & (0x14 << 21)) == (0x04 << 21))
1524 insn |= 0x02 << 21;
1525 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1526 insn |= 0x08 << 21;
1527 else
1528 goto out;
1529 }
1530 else
1531 {
1532 bfd_vma target = 0;
1533 bfd_vma from;
1534
1535 if (!bfd_is_com_section (symbol->section))
1536 target = symbol->value;
1537 target += symbol->section->output_section->vma;
1538 target += symbol->section->output_offset;
1539 target += reloc_entry->addend;
1540
1541 from = (reloc_entry->address
1542 + input_section->output_offset
1543 + input_section->output_section->vma);
1544
1545 /* Invert 'y' bit if not the default. */
1546 if ((bfd_signed_vma) (target - from) < 0)
1547 insn ^= 0x01 << 21;
1548 }
1549 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1550 out:
1551 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1552 input_section, output_bfd, error_message);
1553 }
1554
1555 static bfd_reloc_status_type
1556 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1557 void *data, asection *input_section,
1558 bfd *output_bfd, char **error_message)
1559 {
1560 /* If this is a relocatable link (output_bfd test tells us), just
1561 call the generic function. Any adjustment will be done at final
1562 link time. */
1563 if (output_bfd != NULL)
1564 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1565 input_section, output_bfd, error_message);
1566
1567 /* Subtract the symbol section base address. */
1568 reloc_entry->addend -= symbol->section->output_section->vma;
1569 return bfd_reloc_continue;
1570 }
1571
1572 static bfd_reloc_status_type
1573 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1574 void *data, asection *input_section,
1575 bfd *output_bfd, char **error_message)
1576 {
1577 /* If this is a relocatable link (output_bfd test tells us), just
1578 call the generic function. Any adjustment will be done at final
1579 link time. */
1580 if (output_bfd != NULL)
1581 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1582 input_section, output_bfd, error_message);
1583
1584 /* Subtract the symbol section base address. */
1585 reloc_entry->addend -= symbol->section->output_section->vma;
1586
1587 /* Adjust the addend for sign extension of the low 16 bits. */
1588 reloc_entry->addend += 0x8000;
1589 return bfd_reloc_continue;
1590 }
1591
1592 static bfd_reloc_status_type
1593 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1594 void *data, asection *input_section,
1595 bfd *output_bfd, char **error_message)
1596 {
1597 bfd_vma TOCstart;
1598
1599 /* If this is a relocatable link (output_bfd test tells us), just
1600 call the generic function. Any adjustment will be done at final
1601 link time. */
1602 if (output_bfd != NULL)
1603 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1604 input_section, output_bfd, error_message);
1605
1606 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1607 if (TOCstart == 0)
1608 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1609
1610 /* Subtract the TOC base address. */
1611 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1612 return bfd_reloc_continue;
1613 }
1614
1615 static bfd_reloc_status_type
1616 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1617 void *data, asection *input_section,
1618 bfd *output_bfd, char **error_message)
1619 {
1620 bfd_vma TOCstart;
1621
1622 /* If this is a relocatable link (output_bfd test tells us), just
1623 call the generic function. Any adjustment will be done at final
1624 link time. */
1625 if (output_bfd != NULL)
1626 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1627 input_section, output_bfd, error_message);
1628
1629 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1630 if (TOCstart == 0)
1631 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1632
1633 /* Subtract the TOC base address. */
1634 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1635
1636 /* Adjust the addend for sign extension of the low 16 bits. */
1637 reloc_entry->addend += 0x8000;
1638 return bfd_reloc_continue;
1639 }
1640
1641 static bfd_reloc_status_type
1642 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1643 void *data, asection *input_section,
1644 bfd *output_bfd, char **error_message)
1645 {
1646 bfd_vma TOCstart;
1647 bfd_size_type octets;
1648
1649 /* If this is a relocatable link (output_bfd test tells us), just
1650 call the generic function. Any adjustment will be done at final
1651 link time. */
1652 if (output_bfd != NULL)
1653 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1654 input_section, output_bfd, error_message);
1655
1656 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1657 if (TOCstart == 0)
1658 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1659
1660 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1661 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1662 return bfd_reloc_ok;
1663 }
1664
1665 static bfd_reloc_status_type
1666 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1667 void *data, asection *input_section,
1668 bfd *output_bfd, char **error_message)
1669 {
1670 uint64_t insn;
1671 bfd_vma targ;
1672
1673 if (output_bfd != NULL)
1674 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1675 input_section, output_bfd, error_message);
1676
1677 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1678 insn <<= 32;
1679 insn |= bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address + 4);
1680
1681 targ = (symbol->section->output_section->vma
1682 + symbol->section->output_offset
1683 + reloc_entry->addend);
1684 if (!bfd_is_com_section (symbol->section))
1685 targ += symbol->value;
1686 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1687 targ += 1ULL << 33;
1688 if (reloc_entry->howto->pc_relative)
1689 {
1690 bfd_vma from = (reloc_entry->address
1691 + input_section->output_offset
1692 + input_section->output_section->vma);
1693 targ -=from;
1694 }
1695 targ >>= reloc_entry->howto->rightshift;
1696 insn &= ~reloc_entry->howto->dst_mask;
1697 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1698 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + reloc_entry->address);
1699 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address + 4);
1700 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1701 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1702 >= 1ULL << reloc_entry->howto->bitsize))
1703 return bfd_reloc_overflow;
1704 return bfd_reloc_ok;
1705 }
1706
1707 static bfd_reloc_status_type
1708 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1709 void *data, asection *input_section,
1710 bfd *output_bfd, char **error_message)
1711 {
1712 /* If this is a relocatable link (output_bfd test tells us), just
1713 call the generic function. Any adjustment will be done at final
1714 link time. */
1715 if (output_bfd != NULL)
1716 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1717 input_section, output_bfd, error_message);
1718
1719 if (error_message != NULL)
1720 {
1721 static char *message;
1722 free (message);
1723 if (asprintf (&message, _("generic linker can't handle %s"),
1724 reloc_entry->howto->name) < 0)
1725 message = NULL;
1726 *error_message = message;
1727 }
1728 return bfd_reloc_dangerous;
1729 }
1730
1731 /* Track GOT entries needed for a given symbol. We might need more
1732 than one got entry per symbol. */
1733 struct got_entry
1734 {
1735 struct got_entry *next;
1736
1737 /* The symbol addend that we'll be placing in the GOT. */
1738 bfd_vma addend;
1739
1740 /* Unlike other ELF targets, we use separate GOT entries for the same
1741 symbol referenced from different input files. This is to support
1742 automatic multiple TOC/GOT sections, where the TOC base can vary
1743 from one input file to another. After partitioning into TOC groups
1744 we merge entries within the group.
1745
1746 Point to the BFD owning this GOT entry. */
1747 bfd *owner;
1748
1749 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1750 TLS_TPREL or TLS_DTPREL for tls entries. */
1751 unsigned char tls_type;
1752
1753 /* Non-zero if got.ent points to real entry. */
1754 unsigned char is_indirect;
1755
1756 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1757 union
1758 {
1759 bfd_signed_vma refcount;
1760 bfd_vma offset;
1761 struct got_entry *ent;
1762 } got;
1763 };
1764
1765 /* The same for PLT. */
1766 struct plt_entry
1767 {
1768 struct plt_entry *next;
1769
1770 bfd_vma addend;
1771
1772 union
1773 {
1774 bfd_signed_vma refcount;
1775 bfd_vma offset;
1776 } plt;
1777 };
1778
1779 struct ppc64_elf_obj_tdata
1780 {
1781 struct elf_obj_tdata elf;
1782
1783 /* Shortcuts to dynamic linker sections. */
1784 asection *got;
1785 asection *relgot;
1786
1787 /* Used during garbage collection. We attach global symbols defined
1788 on removed .opd entries to this section so that the sym is removed. */
1789 asection *deleted_section;
1790
1791 /* TLS local dynamic got entry handling. Support for multiple GOT
1792 sections means we potentially need one of these for each input bfd. */
1793 struct got_entry tlsld_got;
1794
1795 union
1796 {
1797 /* A copy of relocs before they are modified for --emit-relocs. */
1798 Elf_Internal_Rela *relocs;
1799
1800 /* Section contents. */
1801 bfd_byte *contents;
1802 } opd;
1803
1804 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1805 the reloc to be in the range -32768 to 32767. */
1806 unsigned int has_small_toc_reloc : 1;
1807
1808 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1809 instruction not one we handle. */
1810 unsigned int unexpected_toc_insn : 1;
1811
1812 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1813 this file. */
1814 unsigned int has_optrel : 1;
1815 };
1816
1817 #define ppc64_elf_tdata(bfd) \
1818 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1819
1820 #define ppc64_tlsld_got(bfd) \
1821 (&ppc64_elf_tdata (bfd)->tlsld_got)
1822
1823 #define is_ppc64_elf(bfd) \
1824 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1825 && elf_object_id (bfd) == PPC64_ELF_DATA)
1826
1827 /* Override the generic function because we store some extras. */
1828
1829 static bool
1830 ppc64_elf_mkobject (bfd *abfd)
1831 {
1832 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1833 PPC64_ELF_DATA);
1834 }
1835
1836 /* Fix bad default arch selected for a 64 bit input bfd when the
1837 default is 32 bit. Also select arch based on apuinfo. */
1838
1839 static bool
1840 ppc64_elf_object_p (bfd *abfd)
1841 {
1842 if (!abfd->arch_info->the_default)
1843 return true;
1844
1845 if (abfd->arch_info->bits_per_word == 32)
1846 {
1847 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1848
1849 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1850 {
1851 /* Relies on arch after 32 bit default being 64 bit default. */
1852 abfd->arch_info = abfd->arch_info->next;
1853 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1854 }
1855 }
1856 return _bfd_elf_ppc_set_arch (abfd);
1857 }
1858
1859 /* Support for core dump NOTE sections. */
1860
1861 static bool
1862 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1863 {
1864 size_t offset, size;
1865
1866 if (note->descsz != 504)
1867 return false;
1868
1869 /* pr_cursig */
1870 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1871
1872 /* pr_pid */
1873 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1874
1875 /* pr_reg */
1876 offset = 112;
1877 size = 384;
1878
1879 /* Make a ".reg/999" section. */
1880 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1881 size, note->descpos + offset);
1882 }
1883
1884 static bool
1885 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1886 {
1887 if (note->descsz != 136)
1888 return false;
1889
1890 elf_tdata (abfd)->core->pid
1891 = bfd_get_32 (abfd, note->descdata + 24);
1892 elf_tdata (abfd)->core->program
1893 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1894 elf_tdata (abfd)->core->command
1895 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1896
1897 return true;
1898 }
1899
1900 static char *
1901 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1902 ...)
1903 {
1904 switch (note_type)
1905 {
1906 default:
1907 return NULL;
1908
1909 case NT_PRPSINFO:
1910 {
1911 char data[136] ATTRIBUTE_NONSTRING;
1912 va_list ap;
1913
1914 va_start (ap, note_type);
1915 memset (data, 0, sizeof (data));
1916 strncpy (data + 40, va_arg (ap, const char *), 16);
1917 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1918 DIAGNOSTIC_PUSH;
1919 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1920 -Wstringop-truncation:
1921 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1922 */
1923 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1924 #endif
1925 strncpy (data + 56, va_arg (ap, const char *), 80);
1926 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1927 DIAGNOSTIC_POP;
1928 #endif
1929 va_end (ap);
1930 return elfcore_write_note (abfd, buf, bufsiz,
1931 "CORE", note_type, data, sizeof (data));
1932 }
1933
1934 case NT_PRSTATUS:
1935 {
1936 char data[504];
1937 va_list ap;
1938 long pid;
1939 int cursig;
1940 const void *greg;
1941
1942 va_start (ap, note_type);
1943 memset (data, 0, 112);
1944 pid = va_arg (ap, long);
1945 bfd_put_32 (abfd, pid, data + 32);
1946 cursig = va_arg (ap, int);
1947 bfd_put_16 (abfd, cursig, data + 12);
1948 greg = va_arg (ap, const void *);
1949 memcpy (data + 112, greg, 384);
1950 memset (data + 496, 0, 8);
1951 va_end (ap);
1952 return elfcore_write_note (abfd, buf, bufsiz,
1953 "CORE", note_type, data, sizeof (data));
1954 }
1955 }
1956 }
1957
1958 /* Add extra PPC sections. */
1959
1960 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1961 {
1962 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1963 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1964 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1965 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1966 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1967 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1968 { NULL, 0, 0, 0, 0 }
1969 };
1970
1971 enum _ppc64_sec_type {
1972 sec_normal = 0,
1973 sec_opd = 1,
1974 sec_toc = 2
1975 };
1976
1977 struct _ppc64_elf_section_data
1978 {
1979 struct bfd_elf_section_data elf;
1980
1981 union
1982 {
1983 /* An array with one entry for each opd function descriptor,
1984 and some spares since opd entries may be either 16 or 24 bytes. */
1985 #define OPD_NDX(OFF) ((OFF) >> 4)
1986 struct _opd_sec_data
1987 {
1988 /* Points to the function code section for local opd entries. */
1989 asection **func_sec;
1990
1991 /* After editing .opd, adjust references to opd local syms. */
1992 long *adjust;
1993 } opd;
1994
1995 /* An array for toc sections, indexed by offset/8. */
1996 struct _toc_sec_data
1997 {
1998 /* Specifies the relocation symbol index used at a given toc offset. */
1999 unsigned *symndx;
2000
2001 /* And the relocation addend. */
2002 bfd_vma *add;
2003 } toc;
2004 } u;
2005
2006 enum _ppc64_sec_type sec_type:2;
2007
2008 /* Flag set when small branches are detected. Used to
2009 select suitable defaults for the stub group size. */
2010 unsigned int has_14bit_branch:1;
2011
2012 /* Flag set when PLTCALL relocs are detected. */
2013 unsigned int has_pltcall:1;
2014
2015 /* Flag set when section has PLT/GOT/TOC relocations that can be
2016 optimised. */
2017 unsigned int has_optrel:1;
2018 };
2019
2020 #define ppc64_elf_section_data(sec) \
2021 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2022
2023 static bool
2024 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2025 {
2026 if (!sec->used_by_bfd)
2027 {
2028 struct _ppc64_elf_section_data *sdata;
2029 size_t amt = sizeof (*sdata);
2030
2031 sdata = bfd_zalloc (abfd, amt);
2032 if (sdata == NULL)
2033 return false;
2034 sec->used_by_bfd = sdata;
2035 }
2036
2037 return _bfd_elf_new_section_hook (abfd, sec);
2038 }
2039
2040 static bool
2041 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2042 {
2043 const char *name = hdr->bfd_section->name;
2044
2045 if (startswith (name, ".sbss")
2046 || startswith (name, ".sdata"))
2047 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2048
2049 return true;
2050 }
2051
2052 static struct _opd_sec_data *
2053 get_opd_info (asection * sec)
2054 {
2055 if (sec != NULL
2056 && ppc64_elf_section_data (sec) != NULL
2057 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2058 return &ppc64_elf_section_data (sec)->u.opd;
2059 return NULL;
2060 }
2061 \f
2062 /* Parameters for the qsort hook. */
2063 static bool synthetic_relocatable;
2064 static const asection *synthetic_opd;
2065
2066 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2067
2068 static int
2069 compare_symbols (const void *ap, const void *bp)
2070 {
2071 const asymbol *a = *(const asymbol **) ap;
2072 const asymbol *b = *(const asymbol **) bp;
2073
2074 /* Section symbols first. */
2075 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2076 return -1;
2077 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2078 return 1;
2079
2080 /* then .opd symbols. */
2081 if (synthetic_opd != NULL)
2082 {
2083 if (strcmp (a->section->name, ".opd") == 0
2084 && strcmp (b->section->name, ".opd") != 0)
2085 return -1;
2086 if (strcmp (a->section->name, ".opd") != 0
2087 && strcmp (b->section->name, ".opd") == 0)
2088 return 1;
2089 }
2090
2091 /* then other code symbols. */
2092 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2093 == (SEC_CODE | SEC_ALLOC))
2094 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2095 != (SEC_CODE | SEC_ALLOC)))
2096 return -1;
2097
2098 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2099 != (SEC_CODE | SEC_ALLOC))
2100 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2101 == (SEC_CODE | SEC_ALLOC)))
2102 return 1;
2103
2104 if (synthetic_relocatable)
2105 {
2106 if (a->section->id < b->section->id)
2107 return -1;
2108
2109 if (a->section->id > b->section->id)
2110 return 1;
2111 }
2112
2113 if (a->value + a->section->vma < b->value + b->section->vma)
2114 return -1;
2115
2116 if (a->value + a->section->vma > b->value + b->section->vma)
2117 return 1;
2118
2119 /* For syms with the same value, prefer strong dynamic global function
2120 syms over other syms. */
2121 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2122 return -1;
2123
2124 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2125 return 1;
2126
2127 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2128 return -1;
2129
2130 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2131 return 1;
2132
2133 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2134 return -1;
2135
2136 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2137 return 1;
2138
2139 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2140 return -1;
2141
2142 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2143 return 1;
2144
2145 /* Finally, sort on where the symbol is in memory. The symbols will
2146 be in at most two malloc'd blocks, one for static syms, one for
2147 dynamic syms, and we distinguish the two blocks above by testing
2148 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2149 originally in the same order as the symbols (and we're not
2150 sorting the symbols themselves), this ensures a stable sort. */
2151 if (a < b)
2152 return -1;
2153 if (a > b)
2154 return 1;
2155 return 0;
2156 }
2157
2158 /* Search SYMS for a symbol of the given VALUE. */
2159
2160 static asymbol *
2161 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2162 bfd_vma value)
2163 {
2164 size_t mid;
2165
2166 if (id == (unsigned) -1)
2167 {
2168 while (lo < hi)
2169 {
2170 mid = (lo + hi) >> 1;
2171 if (syms[mid]->value + syms[mid]->section->vma < value)
2172 lo = mid + 1;
2173 else if (syms[mid]->value + syms[mid]->section->vma > value)
2174 hi = mid;
2175 else
2176 return syms[mid];
2177 }
2178 }
2179 else
2180 {
2181 while (lo < hi)
2182 {
2183 mid = (lo + hi) >> 1;
2184 if (syms[mid]->section->id < id)
2185 lo = mid + 1;
2186 else if (syms[mid]->section->id > id)
2187 hi = mid;
2188 else if (syms[mid]->value < value)
2189 lo = mid + 1;
2190 else if (syms[mid]->value > value)
2191 hi = mid;
2192 else
2193 return syms[mid];
2194 }
2195 }
2196 return NULL;
2197 }
2198
2199 static bool
2200 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2201 {
2202 bfd_vma vma = *(bfd_vma *) ptr;
2203 return ((section->flags & SEC_ALLOC) != 0
2204 && section->vma <= vma
2205 && vma < section->vma + section->size);
2206 }
2207
2208 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2209 entry syms. Also generate @plt symbols for the glink branch table.
2210 Returns count of synthetic symbols in RET or -1 on error. */
2211
2212 static long
2213 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2214 long static_count, asymbol **static_syms,
2215 long dyn_count, asymbol **dyn_syms,
2216 asymbol **ret)
2217 {
2218 asymbol *s;
2219 size_t i, j, count;
2220 char *names;
2221 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2222 asection *opd = NULL;
2223 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2224 asymbol **syms;
2225 int abi = abiversion (abfd);
2226
2227 *ret = NULL;
2228
2229 if (abi < 2)
2230 {
2231 opd = bfd_get_section_by_name (abfd, ".opd");
2232 if (opd == NULL && abi == 1)
2233 return 0;
2234 }
2235
2236 syms = NULL;
2237 codesecsym = 0;
2238 codesecsymend = 0;
2239 secsymend = 0;
2240 opdsymend = 0;
2241 symcount = 0;
2242 if (opd != NULL)
2243 {
2244 symcount = static_count;
2245 if (!relocatable)
2246 symcount += dyn_count;
2247 if (symcount == 0)
2248 return 0;
2249
2250 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2251 if (syms == NULL)
2252 return -1;
2253
2254 if (!relocatable && static_count != 0 && dyn_count != 0)
2255 {
2256 /* Use both symbol tables. */
2257 memcpy (syms, static_syms, static_count * sizeof (*syms));
2258 memcpy (syms + static_count, dyn_syms,
2259 (dyn_count + 1) * sizeof (*syms));
2260 }
2261 else if (!relocatable && static_count == 0)
2262 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2263 else
2264 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2265
2266 /* Trim uninteresting symbols. Interesting symbols are section,
2267 function, and notype symbols. */
2268 for (i = 0, j = 0; i < symcount; ++i)
2269 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2270 | BSF_RELC | BSF_SRELC)) == 0)
2271 syms[j++] = syms[i];
2272 symcount = j;
2273
2274 synthetic_relocatable = relocatable;
2275 synthetic_opd = opd;
2276 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2277
2278 if (!relocatable && symcount > 1)
2279 {
2280 /* Trim duplicate syms, since we may have merged the normal
2281 and dynamic symbols. Actually, we only care about syms
2282 that have different values, so trim any with the same
2283 value. Don't consider ifunc and ifunc resolver symbols
2284 duplicates however, because GDB wants to know whether a
2285 text symbol is an ifunc resolver. */
2286 for (i = 1, j = 1; i < symcount; ++i)
2287 {
2288 const asymbol *s0 = syms[i - 1];
2289 const asymbol *s1 = syms[i];
2290
2291 if ((s0->value + s0->section->vma
2292 != s1->value + s1->section->vma)
2293 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2294 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2295 syms[j++] = syms[i];
2296 }
2297 symcount = j;
2298 }
2299
2300 i = 0;
2301 /* Note that here and in compare_symbols we can't compare opd and
2302 sym->section directly. With separate debug info files, the
2303 symbols will be extracted from the debug file while abfd passed
2304 to this function is the real binary. */
2305 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2306 && strcmp (syms[i]->section->name, ".opd") == 0)
2307 ++i;
2308 codesecsym = i;
2309
2310 for (; i < symcount; ++i)
2311 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2312 | SEC_THREAD_LOCAL))
2313 != (SEC_CODE | SEC_ALLOC))
2314 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2315 break;
2316 codesecsymend = i;
2317
2318 for (; i < symcount; ++i)
2319 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2320 break;
2321 secsymend = i;
2322
2323 for (; i < symcount; ++i)
2324 if (strcmp (syms[i]->section->name, ".opd") != 0)
2325 break;
2326 opdsymend = i;
2327
2328 for (; i < symcount; ++i)
2329 if (((syms[i]->section->flags
2330 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2331 != (SEC_CODE | SEC_ALLOC))
2332 break;
2333 symcount = i;
2334 }
2335 count = 0;
2336
2337 if (relocatable)
2338 {
2339 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2340 arelent *r;
2341 size_t size;
2342 size_t relcount;
2343
2344 if (opdsymend == secsymend)
2345 goto done;
2346
2347 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2348 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2349 if (relcount == 0)
2350 goto done;
2351
2352 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2353 {
2354 count = -1;
2355 goto done;
2356 }
2357
2358 size = 0;
2359 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2360 {
2361 asymbol *sym;
2362
2363 while (r < opd->relocation + relcount
2364 && r->address < syms[i]->value + opd->vma)
2365 ++r;
2366
2367 if (r == opd->relocation + relcount)
2368 break;
2369
2370 if (r->address != syms[i]->value + opd->vma)
2371 continue;
2372
2373 if (r->howto->type != R_PPC64_ADDR64)
2374 continue;
2375
2376 sym = *r->sym_ptr_ptr;
2377 if (!sym_exists_at (syms, opdsymend, symcount,
2378 sym->section->id, sym->value + r->addend))
2379 {
2380 ++count;
2381 size += sizeof (asymbol);
2382 size += strlen (syms[i]->name) + 2;
2383 }
2384 }
2385
2386 if (size == 0)
2387 goto done;
2388 s = *ret = bfd_malloc (size);
2389 if (s == NULL)
2390 {
2391 count = -1;
2392 goto done;
2393 }
2394
2395 names = (char *) (s + count);
2396
2397 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2398 {
2399 asymbol *sym;
2400
2401 while (r < opd->relocation + relcount
2402 && r->address < syms[i]->value + opd->vma)
2403 ++r;
2404
2405 if (r == opd->relocation + relcount)
2406 break;
2407
2408 if (r->address != syms[i]->value + opd->vma)
2409 continue;
2410
2411 if (r->howto->type != R_PPC64_ADDR64)
2412 continue;
2413
2414 sym = *r->sym_ptr_ptr;
2415 if (!sym_exists_at (syms, opdsymend, symcount,
2416 sym->section->id, sym->value + r->addend))
2417 {
2418 size_t len;
2419
2420 *s = *syms[i];
2421 s->flags |= BSF_SYNTHETIC;
2422 s->section = sym->section;
2423 s->value = sym->value + r->addend;
2424 s->name = names;
2425 *names++ = '.';
2426 len = strlen (syms[i]->name);
2427 memcpy (names, syms[i]->name, len + 1);
2428 names += len + 1;
2429 /* Have udata.p point back to the original symbol this
2430 synthetic symbol was derived from. */
2431 s->udata.p = syms[i];
2432 s++;
2433 }
2434 }
2435 }
2436 else
2437 {
2438 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2439 bfd_byte *contents = NULL;
2440 size_t size;
2441 size_t plt_count = 0;
2442 bfd_vma glink_vma = 0, resolv_vma = 0;
2443 asection *dynamic, *glink = NULL, *relplt = NULL;
2444 arelent *p;
2445
2446 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2447 {
2448 free_contents_and_exit_err:
2449 count = -1;
2450 free_contents_and_exit:
2451 free (contents);
2452 goto done;
2453 }
2454
2455 size = 0;
2456 for (i = secsymend; i < opdsymend; ++i)
2457 {
2458 bfd_vma ent;
2459
2460 /* Ignore bogus symbols. */
2461 if (syms[i]->value > opd->size - 8)
2462 continue;
2463
2464 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2465 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2466 {
2467 ++count;
2468 size += sizeof (asymbol);
2469 size += strlen (syms[i]->name) + 2;
2470 }
2471 }
2472
2473 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2474 if (dyn_count != 0
2475 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2476 {
2477 bfd_byte *dynbuf, *extdyn, *extdynend;
2478 size_t extdynsize;
2479 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2480
2481 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2482 goto free_contents_and_exit_err;
2483
2484 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2485 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2486
2487 extdyn = dynbuf;
2488 extdynend = extdyn + dynamic->size;
2489 for (; extdyn < extdynend; extdyn += extdynsize)
2490 {
2491 Elf_Internal_Dyn dyn;
2492 (*swap_dyn_in) (abfd, extdyn, &dyn);
2493
2494 if (dyn.d_tag == DT_NULL)
2495 break;
2496
2497 if (dyn.d_tag == DT_PPC64_GLINK)
2498 {
2499 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2500 See comment in ppc64_elf_finish_dynamic_sections. */
2501 glink_vma = dyn.d_un.d_val + 8 * 4;
2502 /* The .glink section usually does not survive the final
2503 link; search for the section (usually .text) where the
2504 glink stubs now reside. */
2505 glink = bfd_sections_find_if (abfd, section_covers_vma,
2506 &glink_vma);
2507 break;
2508 }
2509 }
2510
2511 free (dynbuf);
2512 }
2513
2514 if (glink != NULL)
2515 {
2516 /* Determine __glink trampoline by reading the relative branch
2517 from the first glink stub. */
2518 bfd_byte buf[4];
2519 unsigned int off = 0;
2520
2521 while (bfd_get_section_contents (abfd, glink, buf,
2522 glink_vma + off - glink->vma, 4))
2523 {
2524 unsigned int insn = bfd_get_32 (abfd, buf);
2525 insn ^= B_DOT;
2526 if ((insn & ~0x3fffffc) == 0)
2527 {
2528 resolv_vma
2529 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2530 break;
2531 }
2532 off += 4;
2533 if (off > 4)
2534 break;
2535 }
2536
2537 if (resolv_vma)
2538 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2539
2540 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2541 if (relplt != NULL)
2542 {
2543 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2544 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2545 goto free_contents_and_exit_err;
2546
2547 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2548 size += plt_count * sizeof (asymbol);
2549
2550 p = relplt->relocation;
2551 for (i = 0; i < plt_count; i++, p++)
2552 {
2553 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2554 if (p->addend != 0)
2555 size += sizeof ("+0x") - 1 + 16;
2556 }
2557 }
2558 }
2559
2560 if (size == 0)
2561 goto free_contents_and_exit;
2562 s = *ret = bfd_malloc (size);
2563 if (s == NULL)
2564 goto free_contents_and_exit_err;
2565
2566 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2567
2568 for (i = secsymend; i < opdsymend; ++i)
2569 {
2570 bfd_vma ent;
2571
2572 if (syms[i]->value > opd->size - 8)
2573 continue;
2574
2575 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2576 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2577 {
2578 size_t lo, hi;
2579 size_t len;
2580 asection *sec = abfd->sections;
2581
2582 *s = *syms[i];
2583 lo = codesecsym;
2584 hi = codesecsymend;
2585 while (lo < hi)
2586 {
2587 size_t mid = (lo + hi) >> 1;
2588 if (syms[mid]->section->vma < ent)
2589 lo = mid + 1;
2590 else if (syms[mid]->section->vma > ent)
2591 hi = mid;
2592 else
2593 {
2594 sec = syms[mid]->section;
2595 break;
2596 }
2597 }
2598
2599 if (lo >= hi && lo > codesecsym)
2600 sec = syms[lo - 1]->section;
2601
2602 for (; sec != NULL; sec = sec->next)
2603 {
2604 if (sec->vma > ent)
2605 break;
2606 /* SEC_LOAD may not be set if SEC is from a separate debug
2607 info file. */
2608 if ((sec->flags & SEC_ALLOC) == 0)
2609 break;
2610 if ((sec->flags & SEC_CODE) != 0)
2611 s->section = sec;
2612 }
2613 s->flags |= BSF_SYNTHETIC;
2614 s->value = ent - s->section->vma;
2615 s->name = names;
2616 *names++ = '.';
2617 len = strlen (syms[i]->name);
2618 memcpy (names, syms[i]->name, len + 1);
2619 names += len + 1;
2620 /* Have udata.p point back to the original symbol this
2621 synthetic symbol was derived from. */
2622 s->udata.p = syms[i];
2623 s++;
2624 }
2625 }
2626 free (contents);
2627
2628 if (glink != NULL && relplt != NULL)
2629 {
2630 if (resolv_vma)
2631 {
2632 /* Add a symbol for the main glink trampoline. */
2633 memset (s, 0, sizeof *s);
2634 s->the_bfd = abfd;
2635 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2636 s->section = glink;
2637 s->value = resolv_vma - glink->vma;
2638 s->name = names;
2639 memcpy (names, "__glink_PLTresolve",
2640 sizeof ("__glink_PLTresolve"));
2641 names += sizeof ("__glink_PLTresolve");
2642 s++;
2643 count++;
2644 }
2645
2646 /* FIXME: It would be very much nicer to put sym@plt on the
2647 stub rather than on the glink branch table entry. The
2648 objdump disassembler would then use a sensible symbol
2649 name on plt calls. The difficulty in doing so is
2650 a) finding the stubs, and,
2651 b) matching stubs against plt entries, and,
2652 c) there can be multiple stubs for a given plt entry.
2653
2654 Solving (a) could be done by code scanning, but older
2655 ppc64 binaries used different stubs to current code.
2656 (b) is the tricky one since you need to known the toc
2657 pointer for at least one function that uses a pic stub to
2658 be able to calculate the plt address referenced.
2659 (c) means gdb would need to set multiple breakpoints (or
2660 find the glink branch itself) when setting breakpoints
2661 for pending shared library loads. */
2662 p = relplt->relocation;
2663 for (i = 0; i < plt_count; i++, p++)
2664 {
2665 size_t len;
2666
2667 *s = **p->sym_ptr_ptr;
2668 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2669 we are defining a symbol, ensure one of them is set. */
2670 if ((s->flags & BSF_LOCAL) == 0)
2671 s->flags |= BSF_GLOBAL;
2672 s->flags |= BSF_SYNTHETIC;
2673 s->section = glink;
2674 s->value = glink_vma - glink->vma;
2675 s->name = names;
2676 s->udata.p = NULL;
2677 len = strlen ((*p->sym_ptr_ptr)->name);
2678 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2679 names += len;
2680 if (p->addend != 0)
2681 {
2682 memcpy (names, "+0x", sizeof ("+0x") - 1);
2683 names += sizeof ("+0x") - 1;
2684 bfd_sprintf_vma (abfd, names, p->addend);
2685 names += strlen (names);
2686 }
2687 memcpy (names, "@plt", sizeof ("@plt"));
2688 names += sizeof ("@plt");
2689 s++;
2690 if (abi < 2)
2691 {
2692 glink_vma += 8;
2693 if (i >= 0x8000)
2694 glink_vma += 4;
2695 }
2696 else
2697 glink_vma += 4;
2698 }
2699 count += plt_count;
2700 }
2701 }
2702
2703 done:
2704 free (syms);
2705 return count;
2706 }
2707 \f
2708 /* The following functions are specific to the ELF linker, while
2709 functions above are used generally. Those named ppc64_elf_* are
2710 called by the main ELF linker code. They appear in this file more
2711 or less in the order in which they are called. eg.
2712 ppc64_elf_check_relocs is called early in the link process,
2713 ppc64_elf_finish_dynamic_sections is one of the last functions
2714 called.
2715
2716 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2717 functions have both a function code symbol and a function descriptor
2718 symbol. A call to foo in a relocatable object file looks like:
2719
2720 . .text
2721 . x:
2722 . bl .foo
2723 . nop
2724
2725 The function definition in another object file might be:
2726
2727 . .section .opd
2728 . foo: .quad .foo
2729 . .quad .TOC.@tocbase
2730 . .quad 0
2731 .
2732 . .text
2733 . .foo: blr
2734
2735 When the linker resolves the call during a static link, the branch
2736 unsurprisingly just goes to .foo and the .opd information is unused.
2737 If the function definition is in a shared library, things are a little
2738 different: The call goes via a plt call stub, the opd information gets
2739 copied to the plt, and the linker patches the nop.
2740
2741 . x:
2742 . bl .foo_stub
2743 . ld 2,40(1)
2744 .
2745 .
2746 . .foo_stub:
2747 . std 2,40(1) # in practice, the call stub
2748 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2749 . addi 11,11,Lfoo@toc@l # this is the general idea
2750 . ld 12,0(11)
2751 . ld 2,8(11)
2752 . mtctr 12
2753 . ld 11,16(11)
2754 . bctr
2755 .
2756 . .section .plt
2757 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2758
2759 The "reloc ()" notation is supposed to indicate that the linker emits
2760 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2761 copying.
2762
2763 What are the difficulties here? Well, firstly, the relocations
2764 examined by the linker in check_relocs are against the function code
2765 sym .foo, while the dynamic relocation in the plt is emitted against
2766 the function descriptor symbol, foo. Somewhere along the line, we need
2767 to carefully copy dynamic link information from one symbol to the other.
2768 Secondly, the generic part of the elf linker will make .foo a dynamic
2769 symbol as is normal for most other backends. We need foo dynamic
2770 instead, at least for an application final link. However, when
2771 creating a shared library containing foo, we need to have both symbols
2772 dynamic so that references to .foo are satisfied during the early
2773 stages of linking. Otherwise the linker might decide to pull in a
2774 definition from some other object, eg. a static library.
2775
2776 Update: As of August 2004, we support a new convention. Function
2777 calls may use the function descriptor symbol, ie. "bl foo". This
2778 behaves exactly as "bl .foo". */
2779
2780 /* Of those relocs that might be copied as dynamic relocs, this
2781 function selects those that must be copied when linking a shared
2782 library or PIE, even when the symbol is local. */
2783
2784 static int
2785 must_be_dyn_reloc (struct bfd_link_info *info,
2786 enum elf_ppc64_reloc_type r_type)
2787 {
2788 switch (r_type)
2789 {
2790 default:
2791 /* Only relative relocs can be resolved when the object load
2792 address isn't fixed. DTPREL64 is excluded because the
2793 dynamic linker needs to differentiate global dynamic from
2794 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2795 return 1;
2796
2797 case R_PPC64_REL32:
2798 case R_PPC64_REL64:
2799 case R_PPC64_REL30:
2800 case R_PPC64_TOC16:
2801 case R_PPC64_TOC16_DS:
2802 case R_PPC64_TOC16_LO:
2803 case R_PPC64_TOC16_HI:
2804 case R_PPC64_TOC16_HA:
2805 case R_PPC64_TOC16_LO_DS:
2806 return 0;
2807
2808 case R_PPC64_TPREL16:
2809 case R_PPC64_TPREL16_LO:
2810 case R_PPC64_TPREL16_HI:
2811 case R_PPC64_TPREL16_HA:
2812 case R_PPC64_TPREL16_DS:
2813 case R_PPC64_TPREL16_LO_DS:
2814 case R_PPC64_TPREL16_HIGH:
2815 case R_PPC64_TPREL16_HIGHA:
2816 case R_PPC64_TPREL16_HIGHER:
2817 case R_PPC64_TPREL16_HIGHERA:
2818 case R_PPC64_TPREL16_HIGHEST:
2819 case R_PPC64_TPREL16_HIGHESTA:
2820 case R_PPC64_TPREL64:
2821 case R_PPC64_TPREL34:
2822 /* These relocations are relative but in a shared library the
2823 linker doesn't know the thread pointer base. */
2824 return bfd_link_dll (info);
2825 }
2826 }
2827
2828 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2829 copying dynamic variables from a shared lib into an app's .dynbss
2830 section, and instead use a dynamic relocation to point into the
2831 shared lib. With code that gcc generates it is vital that this be
2832 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2833 actually the address of a function descriptor which resides in the
2834 .opd section. gcc uses the descriptor directly rather than going
2835 via the GOT as some other ABIs do, which means that initialized
2836 function pointers reference the descriptor. Thus, a function
2837 pointer initialized to the address of a function in a shared
2838 library will either require a .dynbss copy and a copy reloc, or a
2839 dynamic reloc. Using a .dynbss copy redefines the function
2840 descriptor symbol to point to the copy. This presents a problem as
2841 a PLT entry for that function is also initialized from the function
2842 descriptor symbol and the copy may not be initialized first. */
2843 #define ELIMINATE_COPY_RELOCS 1
2844
2845 /* Section name for stubs is the associated section name plus this
2846 string. */
2847 #define STUB_SUFFIX ".stub"
2848
2849 /* Linker stubs.
2850 ppc_stub_long_branch:
2851 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2852 destination, but a 24 bit branch in a stub section will reach.
2853 . b dest
2854
2855 ppc_stub_plt_branch:
2856 Similar to the above, but a 24 bit branch in the stub section won't
2857 reach its destination.
2858 . addis %r12,%r2,xxx@toc@ha
2859 . ld %r12,xxx@toc@l(%r12)
2860 . mtctr %r12
2861 . bctr
2862
2863 ppc_stub_plt_call:
2864 Used to call a function in a shared library. If it so happens that
2865 the plt entry referenced crosses a 64k boundary, then an extra
2866 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2867 ppc_stub_plt_call_r2save starts with "std %r2,40(%r1)".
2868 . addis %r11,%r2,xxx@toc@ha
2869 . ld %r12,xxx+0@toc@l(%r11)
2870 . mtctr %r12
2871 . ld %r2,xxx+8@toc@l(%r11)
2872 . ld %r11,xxx+16@toc@l(%r11)
2873 . bctr
2874
2875 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2876 code to adjust the value and save r2 to support multiple toc sections.
2877 A ppc_stub_long_branch with an r2 offset looks like:
2878 . std %r2,40(%r1)
2879 . addis %r2,%r2,off@ha
2880 . addi %r2,%r2,off@l
2881 . b dest
2882
2883 A ppc_stub_plt_branch with an r2 offset looks like:
2884 . std %r2,40(%r1)
2885 . addis %r12,%r2,xxx@toc@ha
2886 . ld %r12,xxx@toc@l(%r12)
2887 . addis %r2,%r2,off@ha
2888 . addi %r2,%r2,off@l
2889 . mtctr %r12
2890 . bctr
2891
2892 All of the above stubs are shown as their ELFv1 variants. ELFv2
2893 variants exist too, simpler for plt calls since a new toc pointer
2894 and static chain are not loaded by the stub. In addition, ELFv2
2895 has some more complex stubs to handle calls marked with NOTOC
2896 relocs from functions where r2 is not a valid toc pointer. These
2897 come in two flavours, the ones shown below, and _both variants that
2898 start with "std %r2,24(%r1)" to save r2 in the unlikely event that
2899 one call is from a function where r2 is used as the toc pointer but
2900 needs a toc adjusting stub for small-model multi-toc, and another
2901 call is from a function where r2 is not valid.
2902 ppc_stub_long_branch_notoc:
2903 . mflr %r12
2904 . bcl 20,31,1f
2905 . 1:
2906 . mflr %r11
2907 . mtlr %r12
2908 . addis %r12,%r11,dest-1b@ha
2909 . addi %r12,%r12,dest-1b@l
2910 . b dest
2911
2912 ppc_stub_plt_branch_notoc:
2913 . mflr %r12
2914 . bcl 20,31,1f
2915 . 1:
2916 . mflr %r11
2917 . mtlr %r12
2918 . lis %r12,xxx-1b@highest
2919 . ori %r12,%r12,xxx-1b@higher
2920 . sldi %r12,%r12,32
2921 . oris %r12,%r12,xxx-1b@high
2922 . ori %r12,%r12,xxx-1b@l
2923 . add %r12,%r11,%r12
2924 . mtctr %r12
2925 . bctr
2926
2927 ppc_stub_plt_call_notoc:
2928 . mflr %r12
2929 . bcl 20,31,1f
2930 . 1:
2931 . mflr %r11
2932 . mtlr %r12
2933 . lis %r12,xxx-1b@highest
2934 . ori %r12,%r12,xxx-1b@higher
2935 . sldi %r12,%r12,32
2936 . oris %r12,%r12,xxx-1b@high
2937 . ori %r12,%r12,xxx-1b@l
2938 . ldx %r12,%r11,%r12
2939 . mtctr %r12
2940 . bctr
2941
2942 There are also ELFv1 power10 variants of these stubs.
2943 ppc_stub_long_branch_notoc:
2944 . pla %r12,dest@pcrel
2945 . b dest
2946 ppc_stub_plt_branch_notoc:
2947 . lis %r11,(dest-1f)@highesta34
2948 . ori %r11,%r11,(dest-1f)@highera34
2949 . sldi %r11,%r11,34
2950 . 1: pla %r12,dest@pcrel
2951 . add %r12,%r11,%r12
2952 . mtctr %r12
2953 . bctr
2954 ppc_stub_plt_call_notoc:
2955 . lis %r11,(xxx-1f)@highesta34
2956 . ori %r11,%r11,(xxx-1f)@highera34
2957 . sldi %r11,%r11,34
2958 . 1: pla %r12,xxx@pcrel
2959 . ldx %r12,%r11,%r12
2960 . mtctr %r12
2961 . bctr
2962
2963 In cases where the high instructions would add zero, they are
2964 omitted and following instructions modified in some cases.
2965 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2966 to
2967 . pld %r12,xxx@pcrel
2968 . mtctr %r12
2969 . bctr
2970
2971 For a given stub group (a set of sections all using the same toc
2972 pointer value) there will be just one stub type used for any
2973 particular function symbol. For example, if printf is called from
2974 code with the tocsave optimization (ie. r2 saved in function
2975 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2976 and from other code without the tocsave optimization requiring a
2977 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2978 type will be created. Calls with the tocsave optimization will
2979 enter this stub after the instruction saving r2. A similar
2980 situation exists when calls are marked with R_PPC64_REL24_NOTOC
2981 relocations. These require a ppc_stub_plt_call_notoc linkage stub
2982 to call an external function like printf. If other calls to printf
2983 require a ppc_stub_plt_call linkage stub then a single
2984 ppc_stub_plt_call_notoc linkage stub will be used for both types of
2985 call. If other calls to printf require a ppc_stub_plt_call_r2save
2986 linkage stub then a single ppc_stub_plt_call_both linkage stub will
2987 be created and calls not requiring r2 to be saved will enter the
2988 stub after the r2 save instruction. There is an analogous
2989 hierarchy of long branch and plt branch stubs for local call
2990 linkage. */
2991
2992 enum ppc_stub_type
2993 {
2994 ppc_stub_none,
2995 ppc_stub_long_branch,
2996 ppc_stub_long_branch_r2off,
2997 ppc_stub_long_branch_notoc,
2998 ppc_stub_long_branch_both, /* r2off and notoc variants both needed. */
2999 ppc_stub_plt_branch,
3000 ppc_stub_plt_branch_r2off,
3001 ppc_stub_plt_branch_notoc,
3002 ppc_stub_plt_branch_both,
3003 ppc_stub_plt_call,
3004 ppc_stub_plt_call_r2save,
3005 ppc_stub_plt_call_notoc,
3006 ppc_stub_plt_call_both,
3007 ppc_stub_global_entry,
3008 ppc_stub_save_res
3009 };
3010
3011 /* Information on stub grouping. */
3012 struct map_stub
3013 {
3014 /* The stub section. */
3015 asection *stub_sec;
3016 /* This is the section to which stubs in the group will be attached. */
3017 asection *link_sec;
3018 /* Next group. */
3019 struct map_stub *next;
3020 /* Whether to emit a copy of register save/restore functions in this
3021 group. */
3022 int needs_save_res;
3023 /* Current offset within stubs after the insn restoring lr in a
3024 _notoc or _both stub using bcl for pc-relative addressing, or
3025 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3026 unsigned int lr_restore;
3027 /* Accumulated size of EH info emitted to describe return address
3028 if stubs modify lr. Does not include 17 byte FDE header. */
3029 unsigned int eh_size;
3030 /* Offset in glink_eh_frame to the start of EH info for this group. */
3031 unsigned int eh_base;
3032 };
3033
3034 struct ppc_stub_hash_entry
3035 {
3036 /* Base hash table entry structure. */
3037 struct bfd_hash_entry root;
3038
3039 enum ppc_stub_type stub_type;
3040
3041 /* Group information. */
3042 struct map_stub *group;
3043
3044 /* Offset within stub_sec of the beginning of this stub. */
3045 bfd_vma stub_offset;
3046
3047 /* Given the symbol's value and its section we can determine its final
3048 value when building the stubs (so the stub knows where to jump. */
3049 bfd_vma target_value;
3050 asection *target_section;
3051
3052 /* The symbol table entry, if any, that this was derived from. */
3053 struct ppc_link_hash_entry *h;
3054 struct plt_entry *plt_ent;
3055
3056 /* Symbol type. */
3057 unsigned char symtype;
3058
3059 /* Symbol st_other. */
3060 unsigned char other;
3061 };
3062
3063 struct ppc_branch_hash_entry
3064 {
3065 /* Base hash table entry structure. */
3066 struct bfd_hash_entry root;
3067
3068 /* Offset within branch lookup table. */
3069 unsigned int offset;
3070
3071 /* Generation marker. */
3072 unsigned int iter;
3073 };
3074
3075 /* Used to track dynamic relocations for local symbols. */
3076 struct ppc_dyn_relocs
3077 {
3078 struct ppc_dyn_relocs *next;
3079
3080 /* The input section of the reloc. */
3081 asection *sec;
3082
3083 /* Total number of relocs copied for the input section. */
3084 unsigned int count : 31;
3085
3086 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3087 unsigned int ifunc : 1;
3088 };
3089
3090 struct ppc_link_hash_entry
3091 {
3092 struct elf_link_hash_entry elf;
3093
3094 union
3095 {
3096 /* A pointer to the most recently used stub hash entry against this
3097 symbol. */
3098 struct ppc_stub_hash_entry *stub_cache;
3099
3100 /* A pointer to the next symbol starting with a '.' */
3101 struct ppc_link_hash_entry *next_dot_sym;
3102 } u;
3103
3104 /* Link between function code and descriptor symbols. */
3105 struct ppc_link_hash_entry *oh;
3106
3107 /* Flag function code and descriptor symbols. */
3108 unsigned int is_func:1;
3109 unsigned int is_func_descriptor:1;
3110 unsigned int fake:1;
3111
3112 /* Whether global opd/toc sym has been adjusted or not.
3113 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3114 should be set for all globals defined in any opd/toc section. */
3115 unsigned int adjust_done:1;
3116
3117 /* Set if this is an out-of-line register save/restore function,
3118 with non-standard calling convention. */
3119 unsigned int save_res:1;
3120
3121 /* Set if a duplicate symbol with non-zero localentry is detected,
3122 even when the duplicate symbol does not provide a definition. */
3123 unsigned int non_zero_localentry:1;
3124
3125 /* Contexts in which symbol is used in the GOT (or TOC).
3126 Bits are or'd into the mask as the corresponding relocs are
3127 encountered during check_relocs, with TLS_TLS being set when any
3128 of the other TLS bits are set. tls_optimize clears bits when
3129 optimizing to indicate the corresponding GOT entry type is not
3130 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3131 set TLS_GDIE when a GD reloc turns into an IE one.
3132 These flags are also kept for local symbols. */
3133 #define TLS_TLS 1 /* Any TLS reloc. */
3134 #define TLS_GD 2 /* GD reloc. */
3135 #define TLS_LD 4 /* LD reloc. */
3136 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3137 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3138 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3139 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3140 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3141 unsigned char tls_mask;
3142
3143 /* The above field is also used to mark function symbols. In which
3144 case TLS_TLS will be 0. */
3145 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3146 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3147 #define NON_GOT 256 /* local symbol plt, not stored. */
3148 };
3149
3150 static inline struct ppc_link_hash_entry *
3151 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3152 {
3153 return (struct ppc_link_hash_entry *) ent;
3154 }
3155
3156 static inline struct elf_link_hash_entry *
3157 elf_hash_entry (struct ppc_link_hash_entry *ent)
3158 {
3159 return (struct elf_link_hash_entry *) ent;
3160 }
3161
3162 /* ppc64 ELF linker hash table. */
3163
3164 struct ppc_link_hash_table
3165 {
3166 struct elf_link_hash_table elf;
3167
3168 /* The stub hash table. */
3169 struct bfd_hash_table stub_hash_table;
3170
3171 /* Another hash table for plt_branch stubs. */
3172 struct bfd_hash_table branch_hash_table;
3173
3174 /* Hash table for function prologue tocsave. */
3175 htab_t tocsave_htab;
3176
3177 /* Various options and other info passed from the linker. */
3178 struct ppc64_elf_params *params;
3179
3180 /* The size of sec_info below. */
3181 unsigned int sec_info_arr_size;
3182
3183 /* Per-section array of extra section info. Done this way rather
3184 than as part of ppc64_elf_section_data so we have the info for
3185 non-ppc64 sections. */
3186 struct
3187 {
3188 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3189 bfd_vma toc_off;
3190
3191 union
3192 {
3193 /* The section group that this section belongs to. */
3194 struct map_stub *group;
3195 /* A temp section list pointer. */
3196 asection *list;
3197 } u;
3198 } *sec_info;
3199
3200 /* Linked list of groups. */
3201 struct map_stub *group;
3202
3203 /* Temp used when calculating TOC pointers. */
3204 bfd_vma toc_curr;
3205 bfd *toc_bfd;
3206 asection *toc_first_sec;
3207
3208 /* Used when adding symbols. */
3209 struct ppc_link_hash_entry *dot_syms;
3210
3211 /* Shortcuts to get to dynamic linker sections. */
3212 asection *glink;
3213 asection *global_entry;
3214 asection *sfpr;
3215 asection *pltlocal;
3216 asection *relpltlocal;
3217 asection *brlt;
3218 asection *relbrlt;
3219 asection *glink_eh_frame;
3220
3221 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3222 struct ppc_link_hash_entry *tls_get_addr;
3223 struct ppc_link_hash_entry *tls_get_addr_fd;
3224 struct ppc_link_hash_entry *tga_desc;
3225 struct ppc_link_hash_entry *tga_desc_fd;
3226 struct map_stub *tga_group;
3227
3228 /* The size of reliplt used by got entry relocs. */
3229 bfd_size_type got_reli_size;
3230
3231 /* Statistics. */
3232 unsigned long stub_count[ppc_stub_global_entry];
3233
3234 /* Number of stubs against global syms. */
3235 unsigned long stub_globals;
3236
3237 /* Set if we're linking code with function descriptors. */
3238 unsigned int opd_abi:1;
3239
3240 /* Support for multiple toc sections. */
3241 unsigned int do_multi_toc:1;
3242 unsigned int multi_toc_needed:1;
3243 unsigned int second_toc_pass:1;
3244 unsigned int do_toc_opt:1;
3245
3246 /* Set if tls optimization is enabled. */
3247 unsigned int do_tls_opt:1;
3248
3249 /* Set if inline plt calls should be converted to direct calls. */
3250 unsigned int can_convert_all_inline_plt:1;
3251
3252 /* Set on error. */
3253 unsigned int stub_error:1;
3254
3255 /* Whether func_desc_adjust needs to be run over symbols. */
3256 unsigned int need_func_desc_adj:1;
3257
3258 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3259 unsigned int has_plt_localentry0:1;
3260
3261 /* Whether calls are made via the PLT from NOTOC functions. */
3262 unsigned int notoc_plt:1;
3263
3264 /* Whether any code linked seems to be Power10. */
3265 unsigned int has_power10_relocs:1;
3266
3267 /* Incremented every time we size stubs. */
3268 unsigned int stub_iteration;
3269 };
3270
3271 /* Rename some of the generic section flags to better document how they
3272 are used here. */
3273
3274 /* Nonzero if this section has TLS related relocations. */
3275 #define has_tls_reloc sec_flg0
3276
3277 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3278 relocations. */
3279 #define nomark_tls_get_addr sec_flg1
3280
3281 /* Nonzero if this section has any toc or got relocs. */
3282 #define has_toc_reloc sec_flg2
3283
3284 /* Nonzero if this section has a call to another section that uses
3285 the toc or got. */
3286 #define makes_toc_func_call sec_flg3
3287
3288 /* Recursion protection when determining above flag. */
3289 #define call_check_in_progress sec_flg4
3290 #define call_check_done sec_flg5
3291
3292 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3293
3294 #define ppc_hash_table(p) \
3295 ((is_elf_hash_table ((p)->hash) \
3296 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3297 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3298
3299 #define ppc_stub_hash_lookup(table, string, create, copy) \
3300 ((struct ppc_stub_hash_entry *) \
3301 bfd_hash_lookup ((table), (string), (create), (copy)))
3302
3303 #define ppc_branch_hash_lookup(table, string, create, copy) \
3304 ((struct ppc_branch_hash_entry *) \
3305 bfd_hash_lookup ((table), (string), (create), (copy)))
3306
3307 /* Create an entry in the stub hash table. */
3308
3309 static struct bfd_hash_entry *
3310 stub_hash_newfunc (struct bfd_hash_entry *entry,
3311 struct bfd_hash_table *table,
3312 const char *string)
3313 {
3314 /* Allocate the structure if it has not already been allocated by a
3315 subclass. */
3316 if (entry == NULL)
3317 {
3318 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3319 if (entry == NULL)
3320 return entry;
3321 }
3322
3323 /* Call the allocation method of the superclass. */
3324 entry = bfd_hash_newfunc (entry, table, string);
3325 if (entry != NULL)
3326 {
3327 struct ppc_stub_hash_entry *eh;
3328
3329 /* Initialize the local fields. */
3330 eh = (struct ppc_stub_hash_entry *) entry;
3331 eh->stub_type = ppc_stub_none;
3332 eh->group = NULL;
3333 eh->stub_offset = 0;
3334 eh->target_value = 0;
3335 eh->target_section = NULL;
3336 eh->h = NULL;
3337 eh->plt_ent = NULL;
3338 eh->other = 0;
3339 }
3340
3341 return entry;
3342 }
3343
3344 /* Create an entry in the branch hash table. */
3345
3346 static struct bfd_hash_entry *
3347 branch_hash_newfunc (struct bfd_hash_entry *entry,
3348 struct bfd_hash_table *table,
3349 const char *string)
3350 {
3351 /* Allocate the structure if it has not already been allocated by a
3352 subclass. */
3353 if (entry == NULL)
3354 {
3355 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3356 if (entry == NULL)
3357 return entry;
3358 }
3359
3360 /* Call the allocation method of the superclass. */
3361 entry = bfd_hash_newfunc (entry, table, string);
3362 if (entry != NULL)
3363 {
3364 struct ppc_branch_hash_entry *eh;
3365
3366 /* Initialize the local fields. */
3367 eh = (struct ppc_branch_hash_entry *) entry;
3368 eh->offset = 0;
3369 eh->iter = 0;
3370 }
3371
3372 return entry;
3373 }
3374
3375 /* Create an entry in a ppc64 ELF linker hash table. */
3376
3377 static struct bfd_hash_entry *
3378 link_hash_newfunc (struct bfd_hash_entry *entry,
3379 struct bfd_hash_table *table,
3380 const char *string)
3381 {
3382 /* Allocate the structure if it has not already been allocated by a
3383 subclass. */
3384 if (entry == NULL)
3385 {
3386 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3387 if (entry == NULL)
3388 return entry;
3389 }
3390
3391 /* Call the allocation method of the superclass. */
3392 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3393 if (entry != NULL)
3394 {
3395 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3396
3397 memset (&eh->u.stub_cache, 0,
3398 (sizeof (struct ppc_link_hash_entry)
3399 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3400
3401 /* When making function calls, old ABI code references function entry
3402 points (dot symbols), while new ABI code references the function
3403 descriptor symbol. We need to make any combination of reference and
3404 definition work together, without breaking archive linking.
3405
3406 For a defined function "foo" and an undefined call to "bar":
3407 An old object defines "foo" and ".foo", references ".bar" (possibly
3408 "bar" too).
3409 A new object defines "foo" and references "bar".
3410
3411 A new object thus has no problem with its undefined symbols being
3412 satisfied by definitions in an old object. On the other hand, the
3413 old object won't have ".bar" satisfied by a new object.
3414
3415 Keep a list of newly added dot-symbols. */
3416
3417 if (string[0] == '.')
3418 {
3419 struct ppc_link_hash_table *htab;
3420
3421 htab = (struct ppc_link_hash_table *) table;
3422 eh->u.next_dot_sym = htab->dot_syms;
3423 htab->dot_syms = eh;
3424 }
3425 }
3426
3427 return entry;
3428 }
3429
3430 struct tocsave_entry
3431 {
3432 asection *sec;
3433 bfd_vma offset;
3434 };
3435
3436 static hashval_t
3437 tocsave_htab_hash (const void *p)
3438 {
3439 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3440 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3441 }
3442
3443 static int
3444 tocsave_htab_eq (const void *p1, const void *p2)
3445 {
3446 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3447 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3448 return e1->sec == e2->sec && e1->offset == e2->offset;
3449 }
3450
3451 /* Destroy a ppc64 ELF linker hash table. */
3452
3453 static void
3454 ppc64_elf_link_hash_table_free (bfd *obfd)
3455 {
3456 struct ppc_link_hash_table *htab;
3457
3458 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3459 if (htab->tocsave_htab)
3460 htab_delete (htab->tocsave_htab);
3461 bfd_hash_table_free (&htab->branch_hash_table);
3462 bfd_hash_table_free (&htab->stub_hash_table);
3463 _bfd_elf_link_hash_table_free (obfd);
3464 }
3465
3466 /* Create a ppc64 ELF linker hash table. */
3467
3468 static struct bfd_link_hash_table *
3469 ppc64_elf_link_hash_table_create (bfd *abfd)
3470 {
3471 struct ppc_link_hash_table *htab;
3472 size_t amt = sizeof (struct ppc_link_hash_table);
3473
3474 htab = bfd_zmalloc (amt);
3475 if (htab == NULL)
3476 return NULL;
3477
3478 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3479 sizeof (struct ppc_link_hash_entry),
3480 PPC64_ELF_DATA))
3481 {
3482 free (htab);
3483 return NULL;
3484 }
3485
3486 /* Init the stub hash table too. */
3487 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3488 sizeof (struct ppc_stub_hash_entry)))
3489 {
3490 _bfd_elf_link_hash_table_free (abfd);
3491 return NULL;
3492 }
3493
3494 /* And the branch hash table. */
3495 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3496 sizeof (struct ppc_branch_hash_entry)))
3497 {
3498 bfd_hash_table_free (&htab->stub_hash_table);
3499 _bfd_elf_link_hash_table_free (abfd);
3500 return NULL;
3501 }
3502
3503 htab->tocsave_htab = htab_try_create (1024,
3504 tocsave_htab_hash,
3505 tocsave_htab_eq,
3506 NULL);
3507 if (htab->tocsave_htab == NULL)
3508 {
3509 ppc64_elf_link_hash_table_free (abfd);
3510 return NULL;
3511 }
3512 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3513
3514 /* Initializing two fields of the union is just cosmetic. We really
3515 only care about glist, but when compiled on a 32-bit host the
3516 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3517 debugger inspection of these fields look nicer. */
3518 htab->elf.init_got_refcount.refcount = 0;
3519 htab->elf.init_got_refcount.glist = NULL;
3520 htab->elf.init_plt_refcount.refcount = 0;
3521 htab->elf.init_plt_refcount.glist = NULL;
3522 htab->elf.init_got_offset.offset = 0;
3523 htab->elf.init_got_offset.glist = NULL;
3524 htab->elf.init_plt_offset.offset = 0;
3525 htab->elf.init_plt_offset.glist = NULL;
3526
3527 return &htab->elf.root;
3528 }
3529
3530 /* Create sections for linker generated code. */
3531
3532 static bool
3533 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3534 {
3535 struct ppc_link_hash_table *htab;
3536 flagword flags;
3537
3538 htab = ppc_hash_table (info);
3539
3540 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3541 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3542 if (htab->params->save_restore_funcs)
3543 {
3544 /* Create .sfpr for code to save and restore fp regs. */
3545 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3546 flags);
3547 if (htab->sfpr == NULL
3548 || !bfd_set_section_alignment (htab->sfpr, 2))
3549 return false;
3550 }
3551
3552 if (bfd_link_relocatable (info))
3553 return true;
3554
3555 /* Create .glink for lazy dynamic linking support. */
3556 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3557 flags);
3558 if (htab->glink == NULL
3559 || !bfd_set_section_alignment (htab->glink, 3))
3560 return false;
3561
3562 /* The part of .glink used by global entry stubs, separate so that
3563 it can be aligned appropriately without affecting htab->glink. */
3564 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3565 flags);
3566 if (htab->global_entry == NULL
3567 || !bfd_set_section_alignment (htab->global_entry, 2))
3568 return false;
3569
3570 if (!info->no_ld_generated_unwind_info)
3571 {
3572 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3573 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3574 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3575 ".eh_frame",
3576 flags);
3577 if (htab->glink_eh_frame == NULL
3578 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3579 return false;
3580 }
3581
3582 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3583 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3584 if (htab->elf.iplt == NULL
3585 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3586 return false;
3587
3588 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3589 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3590 htab->elf.irelplt
3591 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3592 if (htab->elf.irelplt == NULL
3593 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3594 return false;
3595
3596 /* Create branch lookup table for plt_branch stubs. */
3597 flags = (SEC_ALLOC | SEC_LOAD
3598 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3599 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3600 flags);
3601 if (htab->brlt == NULL
3602 || !bfd_set_section_alignment (htab->brlt, 3))
3603 return false;
3604
3605 /* Local plt entries, put in .branch_lt but a separate section for
3606 convenience. */
3607 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3608 flags);
3609 if (htab->pltlocal == NULL
3610 || !bfd_set_section_alignment (htab->pltlocal, 3))
3611 return false;
3612
3613 if (!bfd_link_pic (info))
3614 return true;
3615
3616 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3617 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3618 htab->relbrlt
3619 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3620 if (htab->relbrlt == NULL
3621 || !bfd_set_section_alignment (htab->relbrlt, 3))
3622 return false;
3623
3624 htab->relpltlocal
3625 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3626 if (htab->relpltlocal == NULL
3627 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3628 return false;
3629
3630 return true;
3631 }
3632
3633 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3634
3635 bool
3636 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3637 struct ppc64_elf_params *params)
3638 {
3639 struct ppc_link_hash_table *htab;
3640
3641 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3642
3643 /* Always hook our dynamic sections into the first bfd, which is the
3644 linker created stub bfd. This ensures that the GOT header is at
3645 the start of the output TOC section. */
3646 htab = ppc_hash_table (info);
3647 htab->elf.dynobj = params->stub_bfd;
3648 htab->params = params;
3649
3650 return create_linkage_sections (htab->elf.dynobj, info);
3651 }
3652
3653 /* Build a name for an entry in the stub hash table. */
3654
3655 static char *
3656 ppc_stub_name (const asection *input_section,
3657 const asection *sym_sec,
3658 const struct ppc_link_hash_entry *h,
3659 const Elf_Internal_Rela *rel)
3660 {
3661 char *stub_name;
3662 ssize_t len;
3663
3664 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3665 offsets from a sym as a branch target? In fact, we could
3666 probably assume the addend is always zero. */
3667 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3668
3669 if (h)
3670 {
3671 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3672 stub_name = bfd_malloc (len);
3673 if (stub_name == NULL)
3674 return stub_name;
3675
3676 len = sprintf (stub_name, "%08x.%s+%x",
3677 input_section->id & 0xffffffff,
3678 h->elf.root.root.string,
3679 (int) rel->r_addend & 0xffffffff);
3680 }
3681 else
3682 {
3683 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3684 stub_name = bfd_malloc (len);
3685 if (stub_name == NULL)
3686 return stub_name;
3687
3688 len = sprintf (stub_name, "%08x.%x:%x+%x",
3689 input_section->id & 0xffffffff,
3690 sym_sec->id & 0xffffffff,
3691 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3692 (int) rel->r_addend & 0xffffffff);
3693 }
3694 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3695 stub_name[len - 2] = 0;
3696 return stub_name;
3697 }
3698
3699 /* If mixing power10 with non-power10 code and --power10-stubs is not
3700 specified (or is auto) then calls using @notoc relocations that
3701 need a stub will utilize power10 instructions in the stub, and
3702 calls without @notoc relocations will not use power10 instructions.
3703 The two classes of stubs are stored in separate stub_hash_table
3704 entries having the same key string. The two entries will always be
3705 adjacent on entry->root.next chain, even if hash table resizing
3706 occurs. This function selects the correct entry to use. */
3707
3708 static struct ppc_stub_hash_entry *
3709 select_alt_stub (struct ppc_stub_hash_entry *entry, bool notoc)
3710 {
3711 bool have_notoc;
3712
3713 have_notoc = (entry->stub_type == ppc_stub_plt_call_notoc
3714 || entry->stub_type == ppc_stub_plt_branch_notoc
3715 || entry->stub_type == ppc_stub_long_branch_notoc);
3716
3717 if (have_notoc != notoc)
3718 {
3719 const char *stub_name = entry->root.string;
3720
3721 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3722 if (entry != NULL
3723 && entry->root.string != stub_name)
3724 entry = NULL;
3725 }
3726
3727 return entry;
3728 }
3729
3730 /* Look up an entry in the stub hash. Stub entries are cached because
3731 creating the stub name takes a bit of time. */
3732
3733 static struct ppc_stub_hash_entry *
3734 ppc_get_stub_entry (const asection *input_section,
3735 const asection *sym_sec,
3736 struct ppc_link_hash_entry *h,
3737 const Elf_Internal_Rela *rel,
3738 struct ppc_link_hash_table *htab)
3739 {
3740 struct ppc_stub_hash_entry *stub_entry;
3741 struct map_stub *group;
3742
3743 /* If this input section is part of a group of sections sharing one
3744 stub section, then use the id of the first section in the group.
3745 Stub names need to include a section id, as there may well be
3746 more than one stub used to reach say, printf, and we need to
3747 distinguish between them. */
3748 group = htab->sec_info[input_section->id].u.group;
3749 if (group == NULL)
3750 return NULL;
3751
3752 if (h != NULL && h->u.stub_cache != NULL
3753 && h->u.stub_cache->h == h
3754 && h->u.stub_cache->group == group)
3755 {
3756 stub_entry = h->u.stub_cache;
3757 }
3758 else
3759 {
3760 char *stub_name;
3761
3762 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3763 if (stub_name == NULL)
3764 return NULL;
3765
3766 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3767 stub_name, false, false);
3768 if (h != NULL)
3769 h->u.stub_cache = stub_entry;
3770
3771 free (stub_name);
3772 }
3773
3774 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3775 {
3776 bool notoc = ELF64_R_TYPE (rel->r_info) == R_PPC64_REL24_NOTOC;
3777
3778 stub_entry = select_alt_stub (stub_entry, notoc);
3779 }
3780
3781 return stub_entry;
3782 }
3783
3784 /* Add a new stub entry to the stub hash. Not all fields of the new
3785 stub entry are initialised. */
3786
3787 static struct ppc_stub_hash_entry *
3788 ppc_add_stub (const char *stub_name,
3789 asection *section,
3790 struct bfd_link_info *info)
3791 {
3792 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3793 struct map_stub *group;
3794 asection *link_sec;
3795 asection *stub_sec;
3796 struct ppc_stub_hash_entry *stub_entry;
3797
3798 group = htab->sec_info[section->id].u.group;
3799 link_sec = group->link_sec;
3800 stub_sec = group->stub_sec;
3801 if (stub_sec == NULL)
3802 {
3803 size_t namelen;
3804 bfd_size_type len;
3805 char *s_name;
3806
3807 namelen = strlen (link_sec->name);
3808 len = namelen + sizeof (STUB_SUFFIX);
3809 s_name = bfd_alloc (htab->params->stub_bfd, len);
3810 if (s_name == NULL)
3811 return NULL;
3812
3813 memcpy (s_name, link_sec->name, namelen);
3814 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3815 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3816 if (stub_sec == NULL)
3817 return NULL;
3818 group->stub_sec = stub_sec;
3819 }
3820
3821 /* Enter this entry into the linker stub hash table. */
3822 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3823 true, false);
3824 if (stub_entry == NULL)
3825 {
3826 /* xgettext:c-format */
3827 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3828 section->owner, stub_name);
3829 return NULL;
3830 }
3831
3832 stub_entry->group = group;
3833 stub_entry->stub_offset = 0;
3834 return stub_entry;
3835 }
3836
3837 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3838 not already done. */
3839
3840 static bool
3841 create_got_section (bfd *abfd, struct bfd_link_info *info)
3842 {
3843 asection *got, *relgot;
3844 flagword flags;
3845 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3846
3847 if (!is_ppc64_elf (abfd))
3848 return false;
3849 if (htab == NULL)
3850 return false;
3851
3852 if (!htab->elf.sgot
3853 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3854 return false;
3855
3856 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3857 | SEC_LINKER_CREATED);
3858
3859 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3860 if (!got
3861 || !bfd_set_section_alignment (got, 3))
3862 return false;
3863
3864 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3865 flags | SEC_READONLY);
3866 if (!relgot
3867 || !bfd_set_section_alignment (relgot, 3))
3868 return false;
3869
3870 ppc64_elf_tdata (abfd)->got = got;
3871 ppc64_elf_tdata (abfd)->relgot = relgot;
3872 return true;
3873 }
3874
3875 /* Follow indirect and warning symbol links. */
3876
3877 static inline struct bfd_link_hash_entry *
3878 follow_link (struct bfd_link_hash_entry *h)
3879 {
3880 while (h->type == bfd_link_hash_indirect
3881 || h->type == bfd_link_hash_warning)
3882 h = h->u.i.link;
3883 return h;
3884 }
3885
3886 static inline struct elf_link_hash_entry *
3887 elf_follow_link (struct elf_link_hash_entry *h)
3888 {
3889 return (struct elf_link_hash_entry *) follow_link (&h->root);
3890 }
3891
3892 static inline struct ppc_link_hash_entry *
3893 ppc_follow_link (struct ppc_link_hash_entry *h)
3894 {
3895 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
3896 }
3897
3898 /* Merge PLT info on FROM with that on TO. */
3899
3900 static void
3901 move_plt_plist (struct ppc_link_hash_entry *from,
3902 struct ppc_link_hash_entry *to)
3903 {
3904 if (from->elf.plt.plist != NULL)
3905 {
3906 if (to->elf.plt.plist != NULL)
3907 {
3908 struct plt_entry **entp;
3909 struct plt_entry *ent;
3910
3911 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
3912 {
3913 struct plt_entry *dent;
3914
3915 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
3916 if (dent->addend == ent->addend)
3917 {
3918 dent->plt.refcount += ent->plt.refcount;
3919 *entp = ent->next;
3920 break;
3921 }
3922 if (dent == NULL)
3923 entp = &ent->next;
3924 }
3925 *entp = to->elf.plt.plist;
3926 }
3927
3928 to->elf.plt.plist = from->elf.plt.plist;
3929 from->elf.plt.plist = NULL;
3930 }
3931 }
3932
3933 /* Copy the extra info we tack onto an elf_link_hash_entry. */
3934
3935 static void
3936 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
3937 struct elf_link_hash_entry *dir,
3938 struct elf_link_hash_entry *ind)
3939 {
3940 struct ppc_link_hash_entry *edir, *eind;
3941
3942 edir = ppc_elf_hash_entry (dir);
3943 eind = ppc_elf_hash_entry (ind);
3944
3945 edir->is_func |= eind->is_func;
3946 edir->is_func_descriptor |= eind->is_func_descriptor;
3947 edir->tls_mask |= eind->tls_mask;
3948 if (eind->oh != NULL)
3949 edir->oh = ppc_follow_link (eind->oh);
3950
3951 if (edir->elf.versioned != versioned_hidden)
3952 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
3953 edir->elf.ref_regular |= eind->elf.ref_regular;
3954 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
3955 edir->elf.non_got_ref |= eind->elf.non_got_ref;
3956 edir->elf.needs_plt |= eind->elf.needs_plt;
3957 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
3958
3959 /* If we were called to copy over info for a weak sym, don't copy
3960 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
3961 in order to simplify readonly_dynrelocs and save a field in the
3962 symbol hash entry, but that means dyn_relocs can't be used in any
3963 tests about a specific symbol, or affect other symbol flags which
3964 are then tested. */
3965 if (eind->elf.root.type != bfd_link_hash_indirect)
3966 return;
3967
3968 /* Copy over any dynamic relocs we may have on the indirect sym. */
3969 if (ind->dyn_relocs != NULL)
3970 {
3971 if (dir->dyn_relocs != NULL)
3972 {
3973 struct elf_dyn_relocs **pp;
3974 struct elf_dyn_relocs *p;
3975
3976 /* Add reloc counts against the indirect sym to the direct sym
3977 list. Merge any entries against the same section. */
3978 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
3979 {
3980 struct elf_dyn_relocs *q;
3981
3982 for (q = dir->dyn_relocs; q != NULL; q = q->next)
3983 if (q->sec == p->sec)
3984 {
3985 q->pc_count += p->pc_count;
3986 q->count += p->count;
3987 *pp = p->next;
3988 break;
3989 }
3990 if (q == NULL)
3991 pp = &p->next;
3992 }
3993 *pp = dir->dyn_relocs;
3994 }
3995
3996 dir->dyn_relocs = ind->dyn_relocs;
3997 ind->dyn_relocs = NULL;
3998 }
3999
4000 /* Copy over got entries that we may have already seen to the
4001 symbol which just became indirect. */
4002 if (eind->elf.got.glist != NULL)
4003 {
4004 if (edir->elf.got.glist != NULL)
4005 {
4006 struct got_entry **entp;
4007 struct got_entry *ent;
4008
4009 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4010 {
4011 struct got_entry *dent;
4012
4013 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4014 if (dent->addend == ent->addend
4015 && dent->owner == ent->owner
4016 && dent->tls_type == ent->tls_type)
4017 {
4018 dent->got.refcount += ent->got.refcount;
4019 *entp = ent->next;
4020 break;
4021 }
4022 if (dent == NULL)
4023 entp = &ent->next;
4024 }
4025 *entp = edir->elf.got.glist;
4026 }
4027
4028 edir->elf.got.glist = eind->elf.got.glist;
4029 eind->elf.got.glist = NULL;
4030 }
4031
4032 /* And plt entries. */
4033 move_plt_plist (eind, edir);
4034
4035 if (eind->elf.dynindx != -1)
4036 {
4037 if (edir->elf.dynindx != -1)
4038 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4039 edir->elf.dynstr_index);
4040 edir->elf.dynindx = eind->elf.dynindx;
4041 edir->elf.dynstr_index = eind->elf.dynstr_index;
4042 eind->elf.dynindx = -1;
4043 eind->elf.dynstr_index = 0;
4044 }
4045 }
4046
4047 /* Find the function descriptor hash entry from the given function code
4048 hash entry FH. Link the entries via their OH fields. */
4049
4050 static struct ppc_link_hash_entry *
4051 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4052 {
4053 struct ppc_link_hash_entry *fdh = fh->oh;
4054
4055 if (fdh == NULL)
4056 {
4057 const char *fd_name = fh->elf.root.root.string + 1;
4058
4059 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4060 false, false, false));
4061 if (fdh == NULL)
4062 return fdh;
4063
4064 fdh->is_func_descriptor = 1;
4065 fdh->oh = fh;
4066 fh->is_func = 1;
4067 fh->oh = fdh;
4068 }
4069
4070 fdh = ppc_follow_link (fdh);
4071 fdh->is_func_descriptor = 1;
4072 fdh->oh = fh;
4073 return fdh;
4074 }
4075
4076 /* Make a fake function descriptor sym for the undefined code sym FH. */
4077
4078 static struct ppc_link_hash_entry *
4079 make_fdh (struct bfd_link_info *info,
4080 struct ppc_link_hash_entry *fh)
4081 {
4082 bfd *abfd = fh->elf.root.u.undef.abfd;
4083 struct bfd_link_hash_entry *bh = NULL;
4084 struct ppc_link_hash_entry *fdh;
4085 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4086 ? BSF_WEAK
4087 : BSF_GLOBAL);
4088
4089 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4090 fh->elf.root.root.string + 1,
4091 flags, bfd_und_section_ptr, 0,
4092 NULL, false, false, &bh))
4093 return NULL;
4094
4095 fdh = (struct ppc_link_hash_entry *) bh;
4096 fdh->elf.non_elf = 0;
4097 fdh->fake = 1;
4098 fdh->is_func_descriptor = 1;
4099 fdh->oh = fh;
4100 fh->is_func = 1;
4101 fh->oh = fdh;
4102 return fdh;
4103 }
4104
4105 /* Fix function descriptor symbols defined in .opd sections to be
4106 function type. */
4107
4108 static bool
4109 ppc64_elf_add_symbol_hook (bfd *ibfd,
4110 struct bfd_link_info *info,
4111 Elf_Internal_Sym *isym,
4112 const char **name,
4113 flagword *flags ATTRIBUTE_UNUSED,
4114 asection **sec,
4115 bfd_vma *value)
4116 {
4117 if (*sec != NULL
4118 && strcmp ((*sec)->name, ".opd") == 0)
4119 {
4120 asection *code_sec;
4121
4122 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4123 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4124 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4125
4126 /* If the symbol is a function defined in .opd, and the function
4127 code is in a discarded group, let it appear to be undefined. */
4128 if (!bfd_link_relocatable (info)
4129 && (*sec)->reloc_count != 0
4130 && opd_entry_value (*sec, *value, &code_sec, NULL,
4131 false) != (bfd_vma) -1
4132 && discarded_section (code_sec))
4133 {
4134 *sec = bfd_und_section_ptr;
4135 isym->st_shndx = SHN_UNDEF;
4136 }
4137 }
4138 else if (*sec != NULL
4139 && strcmp ((*sec)->name, ".toc") == 0
4140 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4141 {
4142 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4143 if (htab != NULL)
4144 htab->params->object_in_toc = 1;
4145 }
4146
4147 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4148 {
4149 if (abiversion (ibfd) == 0)
4150 set_abiversion (ibfd, 2);
4151 else if (abiversion (ibfd) == 1)
4152 {
4153 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4154 " for ABI version 1"), *name);
4155 bfd_set_error (bfd_error_bad_value);
4156 return false;
4157 }
4158 }
4159
4160 return true;
4161 }
4162
4163 /* Merge non-visibility st_other attributes: local entry point. */
4164
4165 static void
4166 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4167 unsigned int st_other,
4168 bool definition,
4169 bool dynamic)
4170 {
4171 if (definition && (!dynamic || !h->def_regular))
4172 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4173 | ELF_ST_VISIBILITY (h->other));
4174 }
4175
4176 /* Hook called on merging a symbol. We use this to clear "fake" since
4177 we now have a real symbol. */
4178
4179 static bool
4180 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4181 const Elf_Internal_Sym *isym,
4182 asection **psec ATTRIBUTE_UNUSED,
4183 bool newdef ATTRIBUTE_UNUSED,
4184 bool olddef ATTRIBUTE_UNUSED,
4185 bfd *oldbfd ATTRIBUTE_UNUSED,
4186 const asection *oldsec ATTRIBUTE_UNUSED)
4187 {
4188 ppc_elf_hash_entry (h)->fake = 0;
4189 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4190 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4191 return true;
4192 }
4193
4194 /* This function makes an old ABI object reference to ".bar" cause the
4195 inclusion of a new ABI object archive that defines "bar".
4196 NAME is a symbol defined in an archive. Return a symbol in the hash
4197 table that might be satisfied by the archive symbols. */
4198
4199 static struct elf_link_hash_entry *
4200 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4201 struct bfd_link_info *info,
4202 const char *name)
4203 {
4204 struct elf_link_hash_entry *h;
4205 char *dot_name;
4206 size_t len;
4207
4208 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4209 if (h != NULL
4210 /* Don't return this sym if it is a fake function descriptor
4211 created by add_symbol_adjust. */
4212 && !ppc_elf_hash_entry (h)->fake)
4213 return h;
4214
4215 if (name[0] == '.')
4216 return h;
4217
4218 len = strlen (name);
4219 dot_name = bfd_alloc (abfd, len + 2);
4220 if (dot_name == NULL)
4221 return (struct elf_link_hash_entry *) -1;
4222 dot_name[0] = '.';
4223 memcpy (dot_name + 1, name, len + 1);
4224 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4225 bfd_release (abfd, dot_name);
4226 if (h != NULL)
4227 return h;
4228
4229 if (strcmp (name, "__tls_get_addr_opt") == 0)
4230 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4231 return h;
4232 }
4233
4234 /* This function satisfies all old ABI object references to ".bar" if a
4235 new ABI object defines "bar". Well, at least, undefined dot symbols
4236 are made weak. This stops later archive searches from including an
4237 object if we already have a function descriptor definition. It also
4238 prevents the linker complaining about undefined symbols.
4239 We also check and correct mismatched symbol visibility here. The
4240 most restrictive visibility of the function descriptor and the
4241 function entry symbol is used. */
4242
4243 static bool
4244 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4245 {
4246 struct ppc_link_hash_table *htab;
4247 struct ppc_link_hash_entry *fdh;
4248
4249 if (eh->elf.root.type == bfd_link_hash_warning)
4250 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4251
4252 if (eh->elf.root.type == bfd_link_hash_indirect)
4253 return true;
4254
4255 if (eh->elf.root.root.string[0] != '.')
4256 abort ();
4257
4258 htab = ppc_hash_table (info);
4259 if (htab == NULL)
4260 return false;
4261
4262 fdh = lookup_fdh (eh, htab);
4263 if (fdh == NULL
4264 && !bfd_link_relocatable (info)
4265 && (eh->elf.root.type == bfd_link_hash_undefined
4266 || eh->elf.root.type == bfd_link_hash_undefweak)
4267 && eh->elf.ref_regular)
4268 {
4269 /* Make an undefined function descriptor sym, in order to
4270 pull in an --as-needed shared lib. Archives are handled
4271 elsewhere. */
4272 fdh = make_fdh (info, eh);
4273 if (fdh == NULL)
4274 return false;
4275 }
4276
4277 if (fdh != NULL)
4278 {
4279 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4280 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4281
4282 /* Make both descriptor and entry symbol have the most
4283 constraining visibility of either symbol. */
4284 if (entry_vis < descr_vis)
4285 fdh->elf.other += entry_vis - descr_vis;
4286 else if (entry_vis > descr_vis)
4287 eh->elf.other += descr_vis - entry_vis;
4288
4289 /* Propagate reference flags from entry symbol to function
4290 descriptor symbol. */
4291 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4292 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4293 fdh->elf.ref_regular |= eh->elf.ref_regular;
4294 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4295
4296 if (!fdh->elf.forced_local
4297 && fdh->elf.dynindx == -1
4298 && fdh->elf.versioned != versioned_hidden
4299 && (bfd_link_dll (info)
4300 || fdh->elf.def_dynamic
4301 || fdh->elf.ref_dynamic)
4302 && (eh->elf.ref_regular
4303 || eh->elf.def_regular))
4304 {
4305 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4306 return false;
4307 }
4308 }
4309
4310 return true;
4311 }
4312
4313 /* Set up opd section info and abiversion for IBFD, and process list
4314 of dot-symbols we made in link_hash_newfunc. */
4315
4316 static bool
4317 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4318 {
4319 struct ppc_link_hash_table *htab;
4320 struct ppc_link_hash_entry **p, *eh;
4321 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4322
4323 if (opd != NULL && opd->size != 0)
4324 {
4325 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4326 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4327
4328 if (abiversion (ibfd) == 0)
4329 set_abiversion (ibfd, 1);
4330 else if (abiversion (ibfd) >= 2)
4331 {
4332 /* xgettext:c-format */
4333 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4334 ibfd, abiversion (ibfd));
4335 bfd_set_error (bfd_error_bad_value);
4336 return false;
4337 }
4338 }
4339
4340 if (is_ppc64_elf (info->output_bfd))
4341 {
4342 /* For input files without an explicit abiversion in e_flags
4343 we should have flagged any with symbol st_other bits set
4344 as ELFv1 and above flagged those with .opd as ELFv2.
4345 Set the output abiversion if not yet set, and for any input
4346 still ambiguous, take its abiversion from the output.
4347 Differences in ABI are reported later. */
4348 if (abiversion (info->output_bfd) == 0)
4349 set_abiversion (info->output_bfd, abiversion (ibfd));
4350 else if (abiversion (ibfd) == 0)
4351 set_abiversion (ibfd, abiversion (info->output_bfd));
4352 }
4353
4354 htab = ppc_hash_table (info);
4355 if (htab == NULL)
4356 return true;
4357
4358 if (opd != NULL && opd->size != 0
4359 && (ibfd->flags & DYNAMIC) == 0
4360 && (opd->flags & SEC_RELOC) != 0
4361 && opd->reloc_count != 0
4362 && !bfd_is_abs_section (opd->output_section)
4363 && info->gc_sections)
4364 {
4365 /* Garbage collection needs some extra help with .opd sections.
4366 We don't want to necessarily keep everything referenced by
4367 relocs in .opd, as that would keep all functions. Instead,
4368 if we reference an .opd symbol (a function descriptor), we
4369 want to keep the function code symbol's section. This is
4370 easy for global symbols, but for local syms we need to keep
4371 information about the associated function section. */
4372 bfd_size_type amt;
4373 asection **opd_sym_map;
4374 Elf_Internal_Shdr *symtab_hdr;
4375 Elf_Internal_Rela *relocs, *rel_end, *rel;
4376
4377 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4378 opd_sym_map = bfd_zalloc (ibfd, amt);
4379 if (opd_sym_map == NULL)
4380 return false;
4381 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4382 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4383 info->keep_memory);
4384 if (relocs == NULL)
4385 return false;
4386 symtab_hdr = &elf_symtab_hdr (ibfd);
4387 rel_end = relocs + opd->reloc_count - 1;
4388 for (rel = relocs; rel < rel_end; rel++)
4389 {
4390 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4391 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4392
4393 if (r_type == R_PPC64_ADDR64
4394 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4395 && r_symndx < symtab_hdr->sh_info)
4396 {
4397 Elf_Internal_Sym *isym;
4398 asection *s;
4399
4400 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4401 r_symndx);
4402 if (isym == NULL)
4403 {
4404 if (elf_section_data (opd)->relocs != relocs)
4405 free (relocs);
4406 return false;
4407 }
4408
4409 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4410 if (s != NULL && s != opd)
4411 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4412 }
4413 }
4414 if (elf_section_data (opd)->relocs != relocs)
4415 free (relocs);
4416 }
4417
4418 p = &htab->dot_syms;
4419 while ((eh = *p) != NULL)
4420 {
4421 *p = NULL;
4422 if (&eh->elf == htab->elf.hgot)
4423 ;
4424 else if (htab->elf.hgot == NULL
4425 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4426 htab->elf.hgot = &eh->elf;
4427 else if (abiversion (ibfd) <= 1)
4428 {
4429 htab->need_func_desc_adj = 1;
4430 if (!add_symbol_adjust (eh, info))
4431 return false;
4432 }
4433 p = &eh->u.next_dot_sym;
4434 }
4435 return true;
4436 }
4437
4438 /* Undo hash table changes when an --as-needed input file is determined
4439 not to be needed. */
4440
4441 static bool
4442 ppc64_elf_notice_as_needed (bfd *ibfd,
4443 struct bfd_link_info *info,
4444 enum notice_asneeded_action act)
4445 {
4446 if (act == notice_not_needed)
4447 {
4448 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4449
4450 if (htab == NULL)
4451 return false;
4452
4453 htab->dot_syms = NULL;
4454 }
4455 return _bfd_elf_notice_as_needed (ibfd, info, act);
4456 }
4457
4458 /* If --just-symbols against a final linked binary, then assume we need
4459 toc adjusting stubs when calling functions defined there. */
4460
4461 static void
4462 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4463 {
4464 if ((sec->flags & SEC_CODE) != 0
4465 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4466 && is_ppc64_elf (sec->owner))
4467 {
4468 if (abiversion (sec->owner) >= 2
4469 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4470 sec->has_toc_reloc = 1;
4471 }
4472 _bfd_elf_link_just_syms (sec, info);
4473 }
4474
4475 static struct plt_entry **
4476 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4477 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4478 {
4479 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4480 struct plt_entry **local_plt;
4481 unsigned char *local_got_tls_masks;
4482
4483 if (local_got_ents == NULL)
4484 {
4485 bfd_size_type size = symtab_hdr->sh_info;
4486
4487 size *= (sizeof (*local_got_ents)
4488 + sizeof (*local_plt)
4489 + sizeof (*local_got_tls_masks));
4490 local_got_ents = bfd_zalloc (abfd, size);
4491 if (local_got_ents == NULL)
4492 return NULL;
4493 elf_local_got_ents (abfd) = local_got_ents;
4494 }
4495
4496 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4497 {
4498 struct got_entry *ent;
4499
4500 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4501 if (ent->addend == r_addend
4502 && ent->owner == abfd
4503 && ent->tls_type == tls_type)
4504 break;
4505 if (ent == NULL)
4506 {
4507 size_t amt = sizeof (*ent);
4508 ent = bfd_alloc (abfd, amt);
4509 if (ent == NULL)
4510 return false;
4511 ent->next = local_got_ents[r_symndx];
4512 ent->addend = r_addend;
4513 ent->owner = abfd;
4514 ent->tls_type = tls_type;
4515 ent->is_indirect = false;
4516 ent->got.refcount = 0;
4517 local_got_ents[r_symndx] = ent;
4518 }
4519 ent->got.refcount += 1;
4520 }
4521
4522 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4523 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4524 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4525
4526 return local_plt + r_symndx;
4527 }
4528
4529 static bool
4530 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4531 {
4532 struct plt_entry *ent;
4533
4534 for (ent = *plist; ent != NULL; ent = ent->next)
4535 if (ent->addend == addend)
4536 break;
4537 if (ent == NULL)
4538 {
4539 size_t amt = sizeof (*ent);
4540 ent = bfd_alloc (abfd, amt);
4541 if (ent == NULL)
4542 return false;
4543 ent->next = *plist;
4544 ent->addend = addend;
4545 ent->plt.refcount = 0;
4546 *plist = ent;
4547 }
4548 ent->plt.refcount += 1;
4549 return true;
4550 }
4551
4552 static bool
4553 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4554 {
4555 return (r_type == R_PPC64_REL24
4556 || r_type == R_PPC64_REL24_NOTOC
4557 || r_type == R_PPC64_REL14
4558 || r_type == R_PPC64_REL14_BRTAKEN
4559 || r_type == R_PPC64_REL14_BRNTAKEN
4560 || r_type == R_PPC64_ADDR24
4561 || r_type == R_PPC64_ADDR14
4562 || r_type == R_PPC64_ADDR14_BRTAKEN
4563 || r_type == R_PPC64_ADDR14_BRNTAKEN
4564 || r_type == R_PPC64_PLTCALL
4565 || r_type == R_PPC64_PLTCALL_NOTOC);
4566 }
4567
4568 /* Relocs on inline plt call sequence insns prior to the call. */
4569
4570 static bool
4571 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4572 {
4573 return (r_type == R_PPC64_PLT16_HA
4574 || r_type == R_PPC64_PLT16_HI
4575 || r_type == R_PPC64_PLT16_LO
4576 || r_type == R_PPC64_PLT16_LO_DS
4577 || r_type == R_PPC64_PLT_PCREL34
4578 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4579 || r_type == R_PPC64_PLTSEQ
4580 || r_type == R_PPC64_PLTSEQ_NOTOC);
4581 }
4582
4583 /* Look through the relocs for a section during the first phase, and
4584 calculate needed space in the global offset table, procedure
4585 linkage table, and dynamic reloc sections. */
4586
4587 static bool
4588 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4589 asection *sec, const Elf_Internal_Rela *relocs)
4590 {
4591 struct ppc_link_hash_table *htab;
4592 Elf_Internal_Shdr *symtab_hdr;
4593 struct elf_link_hash_entry **sym_hashes;
4594 const Elf_Internal_Rela *rel;
4595 const Elf_Internal_Rela *rel_end;
4596 asection *sreloc;
4597 struct elf_link_hash_entry *tga, *dottga;
4598 bool is_opd;
4599
4600 if (bfd_link_relocatable (info))
4601 return true;
4602
4603 BFD_ASSERT (is_ppc64_elf (abfd));
4604
4605 htab = ppc_hash_table (info);
4606 if (htab == NULL)
4607 return false;
4608
4609 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4610 false, false, true);
4611 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4612 false, false, true);
4613 symtab_hdr = &elf_symtab_hdr (abfd);
4614 sym_hashes = elf_sym_hashes (abfd);
4615 sreloc = NULL;
4616 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4617 rel_end = relocs + sec->reloc_count;
4618 for (rel = relocs; rel < rel_end; rel++)
4619 {
4620 unsigned long r_symndx;
4621 struct elf_link_hash_entry *h;
4622 enum elf_ppc64_reloc_type r_type;
4623 int tls_type;
4624 struct _ppc64_elf_section_data *ppc64_sec;
4625 struct plt_entry **ifunc, **plt_list;
4626
4627 r_symndx = ELF64_R_SYM (rel->r_info);
4628 if (r_symndx < symtab_hdr->sh_info)
4629 h = NULL;
4630 else
4631 {
4632 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4633 h = elf_follow_link (h);
4634
4635 if (h == htab->elf.hgot)
4636 sec->has_toc_reloc = 1;
4637 }
4638
4639 r_type = ELF64_R_TYPE (rel->r_info);
4640 switch (r_type)
4641 {
4642 case R_PPC64_D34:
4643 case R_PPC64_D34_LO:
4644 case R_PPC64_D34_HI30:
4645 case R_PPC64_D34_HA30:
4646 case R_PPC64_D28:
4647 case R_PPC64_TPREL34:
4648 case R_PPC64_DTPREL34:
4649 case R_PPC64_PCREL34:
4650 case R_PPC64_GOT_PCREL34:
4651 case R_PPC64_GOT_TLSGD_PCREL34:
4652 case R_PPC64_GOT_TLSLD_PCREL34:
4653 case R_PPC64_GOT_TPREL_PCREL34:
4654 case R_PPC64_GOT_DTPREL_PCREL34:
4655 case R_PPC64_PLT_PCREL34:
4656 case R_PPC64_PLT_PCREL34_NOTOC:
4657 case R_PPC64_PCREL28:
4658 htab->has_power10_relocs = 1;
4659 break;
4660 default:
4661 break;
4662 }
4663
4664 switch (r_type)
4665 {
4666 case R_PPC64_PLT16_HA:
4667 case R_PPC64_GOT_TLSLD16_HA:
4668 case R_PPC64_GOT_TLSGD16_HA:
4669 case R_PPC64_GOT_TPREL16_HA:
4670 case R_PPC64_GOT_DTPREL16_HA:
4671 case R_PPC64_GOT16_HA:
4672 case R_PPC64_TOC16_HA:
4673 case R_PPC64_PLT16_LO:
4674 case R_PPC64_PLT16_LO_DS:
4675 case R_PPC64_GOT_TLSLD16_LO:
4676 case R_PPC64_GOT_TLSGD16_LO:
4677 case R_PPC64_GOT_TPREL16_LO_DS:
4678 case R_PPC64_GOT_DTPREL16_LO_DS:
4679 case R_PPC64_GOT16_LO:
4680 case R_PPC64_GOT16_LO_DS:
4681 case R_PPC64_TOC16_LO:
4682 case R_PPC64_TOC16_LO_DS:
4683 case R_PPC64_GOT_PCREL34:
4684 ppc64_elf_tdata (abfd)->has_optrel = 1;
4685 ppc64_elf_section_data (sec)->has_optrel = 1;
4686 break;
4687 default:
4688 break;
4689 }
4690
4691 ifunc = NULL;
4692 if (h != NULL)
4693 {
4694 if (h->type == STT_GNU_IFUNC)
4695 {
4696 h->needs_plt = 1;
4697 ifunc = &h->plt.plist;
4698 }
4699 }
4700 else
4701 {
4702 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
4703 abfd, r_symndx);
4704 if (isym == NULL)
4705 return false;
4706
4707 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4708 {
4709 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4710 rel->r_addend,
4711 NON_GOT | PLT_IFUNC);
4712 if (ifunc == NULL)
4713 return false;
4714 }
4715 }
4716
4717 tls_type = 0;
4718 switch (r_type)
4719 {
4720 case R_PPC64_TLSGD:
4721 case R_PPC64_TLSLD:
4722 /* These special tls relocs tie a call to __tls_get_addr with
4723 its parameter symbol. */
4724 if (h != NULL)
4725 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4726 else
4727 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4728 rel->r_addend,
4729 NON_GOT | TLS_TLS | TLS_MARK))
4730 return false;
4731 sec->has_tls_reloc = 1;
4732 break;
4733
4734 case R_PPC64_GOT_TLSLD16:
4735 case R_PPC64_GOT_TLSLD16_LO:
4736 case R_PPC64_GOT_TLSLD16_HI:
4737 case R_PPC64_GOT_TLSLD16_HA:
4738 case R_PPC64_GOT_TLSLD_PCREL34:
4739 tls_type = TLS_TLS | TLS_LD;
4740 goto dogottls;
4741
4742 case R_PPC64_GOT_TLSGD16:
4743 case R_PPC64_GOT_TLSGD16_LO:
4744 case R_PPC64_GOT_TLSGD16_HI:
4745 case R_PPC64_GOT_TLSGD16_HA:
4746 case R_PPC64_GOT_TLSGD_PCREL34:
4747 tls_type = TLS_TLS | TLS_GD;
4748 goto dogottls;
4749
4750 case R_PPC64_GOT_TPREL16_DS:
4751 case R_PPC64_GOT_TPREL16_LO_DS:
4752 case R_PPC64_GOT_TPREL16_HI:
4753 case R_PPC64_GOT_TPREL16_HA:
4754 case R_PPC64_GOT_TPREL_PCREL34:
4755 if (bfd_link_dll (info))
4756 info->flags |= DF_STATIC_TLS;
4757 tls_type = TLS_TLS | TLS_TPREL;
4758 goto dogottls;
4759
4760 case R_PPC64_GOT_DTPREL16_DS:
4761 case R_PPC64_GOT_DTPREL16_LO_DS:
4762 case R_PPC64_GOT_DTPREL16_HI:
4763 case R_PPC64_GOT_DTPREL16_HA:
4764 case R_PPC64_GOT_DTPREL_PCREL34:
4765 tls_type = TLS_TLS | TLS_DTPREL;
4766 dogottls:
4767 sec->has_tls_reloc = 1;
4768 goto dogot;
4769
4770 case R_PPC64_GOT16:
4771 case R_PPC64_GOT16_LO:
4772 case R_PPC64_GOT16_HI:
4773 case R_PPC64_GOT16_HA:
4774 case R_PPC64_GOT16_DS:
4775 case R_PPC64_GOT16_LO_DS:
4776 case R_PPC64_GOT_PCREL34:
4777 dogot:
4778 /* This symbol requires a global offset table entry. */
4779 sec->has_toc_reloc = 1;
4780 if (r_type == R_PPC64_GOT_TLSLD16
4781 || r_type == R_PPC64_GOT_TLSGD16
4782 || r_type == R_PPC64_GOT_TPREL16_DS
4783 || r_type == R_PPC64_GOT_DTPREL16_DS
4784 || r_type == R_PPC64_GOT16
4785 || r_type == R_PPC64_GOT16_DS)
4786 {
4787 htab->do_multi_toc = 1;
4788 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4789 }
4790
4791 if (ppc64_elf_tdata (abfd)->got == NULL
4792 && !create_got_section (abfd, info))
4793 return false;
4794
4795 if (h != NULL)
4796 {
4797 struct ppc_link_hash_entry *eh;
4798 struct got_entry *ent;
4799
4800 eh = ppc_elf_hash_entry (h);
4801 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4802 if (ent->addend == rel->r_addend
4803 && ent->owner == abfd
4804 && ent->tls_type == tls_type)
4805 break;
4806 if (ent == NULL)
4807 {
4808 size_t amt = sizeof (*ent);
4809 ent = bfd_alloc (abfd, amt);
4810 if (ent == NULL)
4811 return false;
4812 ent->next = eh->elf.got.glist;
4813 ent->addend = rel->r_addend;
4814 ent->owner = abfd;
4815 ent->tls_type = tls_type;
4816 ent->is_indirect = false;
4817 ent->got.refcount = 0;
4818 eh->elf.got.glist = ent;
4819 }
4820 ent->got.refcount += 1;
4821 eh->tls_mask |= tls_type;
4822 }
4823 else
4824 /* This is a global offset table entry for a local symbol. */
4825 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4826 rel->r_addend, tls_type))
4827 return false;
4828 break;
4829
4830 case R_PPC64_PLT16_HA:
4831 case R_PPC64_PLT16_HI:
4832 case R_PPC64_PLT16_LO:
4833 case R_PPC64_PLT16_LO_DS:
4834 case R_PPC64_PLT_PCREL34:
4835 case R_PPC64_PLT_PCREL34_NOTOC:
4836 case R_PPC64_PLT32:
4837 case R_PPC64_PLT64:
4838 /* This symbol requires a procedure linkage table entry. */
4839 plt_list = ifunc;
4840 if (h != NULL)
4841 {
4842 h->needs_plt = 1;
4843 if (h->root.root.string[0] == '.'
4844 && h->root.root.string[1] != '\0')
4845 ppc_elf_hash_entry (h)->is_func = 1;
4846 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
4847 plt_list = &h->plt.plist;
4848 }
4849 if (plt_list == NULL)
4850 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4851 rel->r_addend,
4852 NON_GOT | PLT_KEEP);
4853 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4854 return false;
4855 break;
4856
4857 /* The following relocations don't need to propagate the
4858 relocation if linking a shared object since they are
4859 section relative. */
4860 case R_PPC64_SECTOFF:
4861 case R_PPC64_SECTOFF_LO:
4862 case R_PPC64_SECTOFF_HI:
4863 case R_PPC64_SECTOFF_HA:
4864 case R_PPC64_SECTOFF_DS:
4865 case R_PPC64_SECTOFF_LO_DS:
4866 case R_PPC64_DTPREL16:
4867 case R_PPC64_DTPREL16_LO:
4868 case R_PPC64_DTPREL16_HI:
4869 case R_PPC64_DTPREL16_HA:
4870 case R_PPC64_DTPREL16_DS:
4871 case R_PPC64_DTPREL16_LO_DS:
4872 case R_PPC64_DTPREL16_HIGH:
4873 case R_PPC64_DTPREL16_HIGHA:
4874 case R_PPC64_DTPREL16_HIGHER:
4875 case R_PPC64_DTPREL16_HIGHERA:
4876 case R_PPC64_DTPREL16_HIGHEST:
4877 case R_PPC64_DTPREL16_HIGHESTA:
4878 break;
4879
4880 /* Nor do these. */
4881 case R_PPC64_REL16:
4882 case R_PPC64_REL16_LO:
4883 case R_PPC64_REL16_HI:
4884 case R_PPC64_REL16_HA:
4885 case R_PPC64_REL16_HIGH:
4886 case R_PPC64_REL16_HIGHA:
4887 case R_PPC64_REL16_HIGHER:
4888 case R_PPC64_REL16_HIGHERA:
4889 case R_PPC64_REL16_HIGHEST:
4890 case R_PPC64_REL16_HIGHESTA:
4891 case R_PPC64_REL16_HIGHER34:
4892 case R_PPC64_REL16_HIGHERA34:
4893 case R_PPC64_REL16_HIGHEST34:
4894 case R_PPC64_REL16_HIGHESTA34:
4895 case R_PPC64_REL16DX_HA:
4896 break;
4897
4898 /* Not supported as a dynamic relocation. */
4899 case R_PPC64_ADDR64_LOCAL:
4900 if (bfd_link_pic (info))
4901 {
4902 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
4903 ppc_howto_init ();
4904 /* xgettext:c-format */
4905 info->callbacks->einfo (_("%H: %s reloc unsupported "
4906 "in shared libraries and PIEs\n"),
4907 abfd, sec, rel->r_offset,
4908 ppc64_elf_howto_table[r_type]->name);
4909 bfd_set_error (bfd_error_bad_value);
4910 return false;
4911 }
4912 break;
4913
4914 case R_PPC64_TOC16:
4915 case R_PPC64_TOC16_DS:
4916 htab->do_multi_toc = 1;
4917 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4918 /* Fall through. */
4919 case R_PPC64_TOC16_LO:
4920 case R_PPC64_TOC16_HI:
4921 case R_PPC64_TOC16_HA:
4922 case R_PPC64_TOC16_LO_DS:
4923 sec->has_toc_reloc = 1;
4924 if (h != NULL && bfd_link_executable (info))
4925 {
4926 /* We may need a copy reloc. */
4927 h->non_got_ref = 1;
4928 /* Strongly prefer a copy reloc over a dynamic reloc.
4929 glibc ld.so as of 2019-08 will error out if one of
4930 these relocations is emitted. */
4931 h->needs_copy = 1;
4932 goto dodyn;
4933 }
4934 break;
4935
4936 /* Marker reloc. */
4937 case R_PPC64_ENTRY:
4938 break;
4939
4940 /* This relocation describes the C++ object vtable hierarchy.
4941 Reconstruct it for later use during GC. */
4942 case R_PPC64_GNU_VTINHERIT:
4943 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4944 return false;
4945 break;
4946
4947 /* This relocation describes which C++ vtable entries are actually
4948 used. Record for later use during GC. */
4949 case R_PPC64_GNU_VTENTRY:
4950 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4951 return false;
4952 break;
4953
4954 case R_PPC64_REL14:
4955 case R_PPC64_REL14_BRTAKEN:
4956 case R_PPC64_REL14_BRNTAKEN:
4957 {
4958 asection *dest = NULL;
4959
4960 /* Heuristic: If jumping outside our section, chances are
4961 we are going to need a stub. */
4962 if (h != NULL)
4963 {
4964 /* If the sym is weak it may be overridden later, so
4965 don't assume we know where a weak sym lives. */
4966 if (h->root.type == bfd_link_hash_defined)
4967 dest = h->root.u.def.section;
4968 }
4969 else
4970 {
4971 Elf_Internal_Sym *isym;
4972
4973 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
4974 abfd, r_symndx);
4975 if (isym == NULL)
4976 return false;
4977
4978 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
4979 }
4980
4981 if (dest != sec)
4982 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
4983 }
4984 goto rel24;
4985
4986 case R_PPC64_PLTCALL:
4987 case R_PPC64_PLTCALL_NOTOC:
4988 ppc64_elf_section_data (sec)->has_pltcall = 1;
4989 /* Fall through. */
4990
4991 case R_PPC64_REL24:
4992 case R_PPC64_REL24_NOTOC:
4993 rel24:
4994 plt_list = ifunc;
4995 if (h != NULL)
4996 {
4997 h->needs_plt = 1;
4998 if (h->root.root.string[0] == '.'
4999 && h->root.root.string[1] != '\0')
5000 ppc_elf_hash_entry (h)->is_func = 1;
5001
5002 if (h == tga || h == dottga)
5003 {
5004 sec->has_tls_reloc = 1;
5005 if (rel != relocs
5006 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5007 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5008 /* We have a new-style __tls_get_addr call with
5009 a marker reloc. */
5010 ;
5011 else
5012 /* Mark this section as having an old-style call. */
5013 sec->nomark_tls_get_addr = 1;
5014 }
5015 plt_list = &h->plt.plist;
5016 }
5017
5018 /* We may need a .plt entry if the function this reloc
5019 refers to is in a shared lib. */
5020 if (plt_list
5021 && !update_plt_info (abfd, plt_list, rel->r_addend))
5022 return false;
5023 break;
5024
5025 case R_PPC64_ADDR14:
5026 case R_PPC64_ADDR14_BRNTAKEN:
5027 case R_PPC64_ADDR14_BRTAKEN:
5028 case R_PPC64_ADDR24:
5029 goto dodyn;
5030
5031 case R_PPC64_TPREL64:
5032 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5033 if (bfd_link_dll (info))
5034 info->flags |= DF_STATIC_TLS;
5035 goto dotlstoc;
5036
5037 case R_PPC64_DTPMOD64:
5038 if (rel + 1 < rel_end
5039 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5040 && rel[1].r_offset == rel->r_offset + 8)
5041 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5042 else
5043 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5044 goto dotlstoc;
5045
5046 case R_PPC64_DTPREL64:
5047 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5048 if (rel != relocs
5049 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5050 && rel[-1].r_offset == rel->r_offset - 8)
5051 /* This is the second reloc of a dtpmod, dtprel pair.
5052 Don't mark with TLS_DTPREL. */
5053 goto dodyn;
5054
5055 dotlstoc:
5056 sec->has_tls_reloc = 1;
5057 if (h != NULL)
5058 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5059 else
5060 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5061 rel->r_addend, tls_type))
5062 return false;
5063
5064 ppc64_sec = ppc64_elf_section_data (sec);
5065 if (ppc64_sec->sec_type != sec_toc)
5066 {
5067 bfd_size_type amt;
5068
5069 /* One extra to simplify get_tls_mask. */
5070 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5071 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5072 if (ppc64_sec->u.toc.symndx == NULL)
5073 return false;
5074 amt = sec->size * sizeof (bfd_vma) / 8;
5075 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5076 if (ppc64_sec->u.toc.add == NULL)
5077 return false;
5078 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5079 ppc64_sec->sec_type = sec_toc;
5080 }
5081 BFD_ASSERT (rel->r_offset % 8 == 0);
5082 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5083 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5084
5085 /* Mark the second slot of a GD or LD entry.
5086 -1 to indicate GD and -2 to indicate LD. */
5087 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5088 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5089 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5090 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5091 goto dodyn;
5092
5093 case R_PPC64_TPREL16_HI:
5094 case R_PPC64_TPREL16_HA:
5095 case R_PPC64_TPREL16_HIGH:
5096 case R_PPC64_TPREL16_HIGHA:
5097 case R_PPC64_TPREL16_HIGHER:
5098 case R_PPC64_TPREL16_HIGHERA:
5099 case R_PPC64_TPREL16_HIGHEST:
5100 case R_PPC64_TPREL16_HIGHESTA:
5101 sec->has_tls_reloc = 1;
5102 /* Fall through. */
5103 case R_PPC64_TPREL34:
5104 case R_PPC64_TPREL16:
5105 case R_PPC64_TPREL16_DS:
5106 case R_PPC64_TPREL16_LO:
5107 case R_PPC64_TPREL16_LO_DS:
5108 if (bfd_link_dll (info))
5109 info->flags |= DF_STATIC_TLS;
5110 goto dodyn;
5111
5112 case R_PPC64_ADDR64:
5113 if (is_opd
5114 && rel + 1 < rel_end
5115 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5116 {
5117 if (h != NULL)
5118 ppc_elf_hash_entry (h)->is_func = 1;
5119 }
5120 /* Fall through. */
5121
5122 case R_PPC64_ADDR16:
5123 case R_PPC64_ADDR16_DS:
5124 case R_PPC64_ADDR16_HA:
5125 case R_PPC64_ADDR16_HI:
5126 case R_PPC64_ADDR16_HIGH:
5127 case R_PPC64_ADDR16_HIGHA:
5128 case R_PPC64_ADDR16_HIGHER:
5129 case R_PPC64_ADDR16_HIGHERA:
5130 case R_PPC64_ADDR16_HIGHEST:
5131 case R_PPC64_ADDR16_HIGHESTA:
5132 case R_PPC64_ADDR16_LO:
5133 case R_PPC64_ADDR16_LO_DS:
5134 case R_PPC64_D34:
5135 case R_PPC64_D34_LO:
5136 case R_PPC64_D34_HI30:
5137 case R_PPC64_D34_HA30:
5138 case R_PPC64_ADDR16_HIGHER34:
5139 case R_PPC64_ADDR16_HIGHERA34:
5140 case R_PPC64_ADDR16_HIGHEST34:
5141 case R_PPC64_ADDR16_HIGHESTA34:
5142 case R_PPC64_D28:
5143 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5144 && rel->r_addend == 0)
5145 {
5146 /* We may need a .plt entry if this reloc refers to a
5147 function in a shared lib. */
5148 if (!update_plt_info (abfd, &h->plt.plist, 0))
5149 return false;
5150 h->pointer_equality_needed = 1;
5151 }
5152 /* Fall through. */
5153
5154 case R_PPC64_REL30:
5155 case R_PPC64_REL32:
5156 case R_PPC64_REL64:
5157 case R_PPC64_ADDR32:
5158 case R_PPC64_UADDR16:
5159 case R_PPC64_UADDR32:
5160 case R_PPC64_UADDR64:
5161 case R_PPC64_TOC:
5162 if (h != NULL && bfd_link_executable (info))
5163 /* We may need a copy reloc. */
5164 h->non_got_ref = 1;
5165
5166 /* Don't propagate .opd relocs. */
5167 if (NO_OPD_RELOCS && is_opd)
5168 break;
5169
5170 /* If we are creating a shared library, and this is a reloc
5171 against a global symbol, or a non PC relative reloc
5172 against a local symbol, then we need to copy the reloc
5173 into the shared library. However, if we are linking with
5174 -Bsymbolic, we do not need to copy a reloc against a
5175 global symbol which is defined in an object we are
5176 including in the link (i.e., DEF_REGULAR is set). At
5177 this point we have not seen all the input files, so it is
5178 possible that DEF_REGULAR is not set now but will be set
5179 later (it is never cleared). In case of a weak definition,
5180 DEF_REGULAR may be cleared later by a strong definition in
5181 a shared library. We account for that possibility below by
5182 storing information in the dyn_relocs field of the hash
5183 table entry. A similar situation occurs when creating
5184 shared libraries and symbol visibility changes render the
5185 symbol local.
5186
5187 If on the other hand, we are creating an executable, we
5188 may need to keep relocations for symbols satisfied by a
5189 dynamic library if we manage to avoid copy relocs for the
5190 symbol. */
5191 dodyn:
5192 if ((h != NULL
5193 && (h->root.type == bfd_link_hash_defweak
5194 || !h->def_regular))
5195 || (h != NULL
5196 && !bfd_link_executable (info)
5197 && !SYMBOLIC_BIND (info, h))
5198 || (bfd_link_pic (info)
5199 && must_be_dyn_reloc (info, r_type))
5200 || (!bfd_link_pic (info)
5201 && ifunc != NULL))
5202 {
5203 /* We must copy these reloc types into the output file.
5204 Create a reloc section in dynobj and make room for
5205 this reloc. */
5206 if (sreloc == NULL)
5207 {
5208 sreloc = _bfd_elf_make_dynamic_reloc_section
5209 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5210
5211 if (sreloc == NULL)
5212 return false;
5213 }
5214
5215 /* If this is a global symbol, we count the number of
5216 relocations we need for this symbol. */
5217 if (h != NULL)
5218 {
5219 struct elf_dyn_relocs *p;
5220 struct elf_dyn_relocs **head;
5221
5222 head = &h->dyn_relocs;
5223 p = *head;
5224 if (p == NULL || p->sec != sec)
5225 {
5226 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5227 if (p == NULL)
5228 return false;
5229 p->next = *head;
5230 *head = p;
5231 p->sec = sec;
5232 p->count = 0;
5233 p->pc_count = 0;
5234 }
5235 p->count += 1;
5236 if (!must_be_dyn_reloc (info, r_type))
5237 p->pc_count += 1;
5238 }
5239 else
5240 {
5241 /* Track dynamic relocs needed for local syms too.
5242 We really need local syms available to do this
5243 easily. Oh well. */
5244 struct ppc_dyn_relocs *p;
5245 struct ppc_dyn_relocs **head;
5246 bool is_ifunc;
5247 asection *s;
5248 void *vpp;
5249 Elf_Internal_Sym *isym;
5250
5251 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5252 abfd, r_symndx);
5253 if (isym == NULL)
5254 return false;
5255
5256 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5257 if (s == NULL)
5258 s = sec;
5259
5260 vpp = &elf_section_data (s)->local_dynrel;
5261 head = (struct ppc_dyn_relocs **) vpp;
5262 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5263 p = *head;
5264 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5265 p = p->next;
5266 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5267 {
5268 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5269 if (p == NULL)
5270 return false;
5271 p->next = *head;
5272 *head = p;
5273 p->sec = sec;
5274 p->ifunc = is_ifunc;
5275 p->count = 0;
5276 }
5277 p->count += 1;
5278 }
5279 }
5280 break;
5281
5282 default:
5283 break;
5284 }
5285 }
5286
5287 return true;
5288 }
5289
5290 /* Merge backend specific data from an object file to the output
5291 object file when linking. */
5292
5293 static bool
5294 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5295 {
5296 bfd *obfd = info->output_bfd;
5297 unsigned long iflags, oflags;
5298
5299 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5300 return true;
5301
5302 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5303 return true;
5304
5305 if (!_bfd_generic_verify_endian_match (ibfd, info))
5306 return false;
5307
5308 iflags = elf_elfheader (ibfd)->e_flags;
5309 oflags = elf_elfheader (obfd)->e_flags;
5310
5311 if (iflags & ~EF_PPC64_ABI)
5312 {
5313 _bfd_error_handler
5314 /* xgettext:c-format */
5315 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5316 bfd_set_error (bfd_error_bad_value);
5317 return false;
5318 }
5319 else if (iflags != oflags && iflags != 0)
5320 {
5321 _bfd_error_handler
5322 /* xgettext:c-format */
5323 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5324 ibfd, iflags, oflags);
5325 bfd_set_error (bfd_error_bad_value);
5326 return false;
5327 }
5328
5329 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5330 return false;
5331
5332 /* Merge Tag_compatibility attributes and any common GNU ones. */
5333 return _bfd_elf_merge_object_attributes (ibfd, info);
5334 }
5335
5336 static bool
5337 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5338 {
5339 /* Print normal ELF private data. */
5340 _bfd_elf_print_private_bfd_data (abfd, ptr);
5341
5342 if (elf_elfheader (abfd)->e_flags != 0)
5343 {
5344 FILE *file = ptr;
5345
5346 fprintf (file, _("private flags = 0x%lx:"),
5347 elf_elfheader (abfd)->e_flags);
5348
5349 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5350 fprintf (file, _(" [abiv%ld]"),
5351 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5352 fputc ('\n', file);
5353 }
5354
5355 return true;
5356 }
5357
5358 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5359 of the code entry point, and its section, which must be in the same
5360 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5361
5362 static bfd_vma
5363 opd_entry_value (asection *opd_sec,
5364 bfd_vma offset,
5365 asection **code_sec,
5366 bfd_vma *code_off,
5367 bool in_code_sec)
5368 {
5369 bfd *opd_bfd = opd_sec->owner;
5370 Elf_Internal_Rela *relocs;
5371 Elf_Internal_Rela *lo, *hi, *look;
5372 bfd_vma val;
5373
5374 /* No relocs implies we are linking a --just-symbols object, or looking
5375 at a final linked executable with addr2line or somesuch. */
5376 if (opd_sec->reloc_count == 0)
5377 {
5378 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5379
5380 if (contents == NULL)
5381 {
5382 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5383 return (bfd_vma) -1;
5384 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5385 }
5386
5387 /* PR 17512: file: 64b9dfbb. */
5388 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5389 return (bfd_vma) -1;
5390
5391 val = bfd_get_64 (opd_bfd, contents + offset);
5392 if (code_sec != NULL)
5393 {
5394 asection *sec, *likely = NULL;
5395
5396 if (in_code_sec)
5397 {
5398 sec = *code_sec;
5399 if (sec->vma <= val
5400 && val < sec->vma + sec->size)
5401 likely = sec;
5402 else
5403 val = -1;
5404 }
5405 else
5406 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5407 if (sec->vma <= val
5408 && (sec->flags & SEC_LOAD) != 0
5409 && (sec->flags & SEC_ALLOC) != 0)
5410 likely = sec;
5411 if (likely != NULL)
5412 {
5413 *code_sec = likely;
5414 if (code_off != NULL)
5415 *code_off = val - likely->vma;
5416 }
5417 }
5418 return val;
5419 }
5420
5421 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5422
5423 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5424 if (relocs == NULL)
5425 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5426 /* PR 17512: file: df8e1fd6. */
5427 if (relocs == NULL)
5428 return (bfd_vma) -1;
5429
5430 /* Go find the opd reloc at the sym address. */
5431 lo = relocs;
5432 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5433 val = (bfd_vma) -1;
5434 while (lo < hi)
5435 {
5436 look = lo + (hi - lo) / 2;
5437 if (look->r_offset < offset)
5438 lo = look + 1;
5439 else if (look->r_offset > offset)
5440 hi = look;
5441 else
5442 {
5443 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5444
5445 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5446 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5447 {
5448 unsigned long symndx = ELF64_R_SYM (look->r_info);
5449 asection *sec = NULL;
5450
5451 if (symndx >= symtab_hdr->sh_info
5452 && elf_sym_hashes (opd_bfd) != NULL)
5453 {
5454 struct elf_link_hash_entry **sym_hashes;
5455 struct elf_link_hash_entry *rh;
5456
5457 sym_hashes = elf_sym_hashes (opd_bfd);
5458 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5459 if (rh != NULL)
5460 {
5461 rh = elf_follow_link (rh);
5462 if (rh->root.type != bfd_link_hash_defined
5463 && rh->root.type != bfd_link_hash_defweak)
5464 break;
5465 if (rh->root.u.def.section->owner == opd_bfd)
5466 {
5467 val = rh->root.u.def.value;
5468 sec = rh->root.u.def.section;
5469 }
5470 }
5471 }
5472
5473 if (sec == NULL)
5474 {
5475 Elf_Internal_Sym *sym;
5476
5477 if (symndx < symtab_hdr->sh_info)
5478 {
5479 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5480 if (sym == NULL)
5481 {
5482 size_t symcnt = symtab_hdr->sh_info;
5483 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5484 symcnt, 0,
5485 NULL, NULL, NULL);
5486 if (sym == NULL)
5487 break;
5488 symtab_hdr->contents = (bfd_byte *) sym;
5489 }
5490 sym += symndx;
5491 }
5492 else
5493 {
5494 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5495 1, symndx,
5496 NULL, NULL, NULL);
5497 if (sym == NULL)
5498 break;
5499 }
5500 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5501 if (sec == NULL)
5502 break;
5503 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5504 val = sym->st_value;
5505 }
5506
5507 val += look->r_addend;
5508 if (code_off != NULL)
5509 *code_off = val;
5510 if (code_sec != NULL)
5511 {
5512 if (in_code_sec && *code_sec != sec)
5513 return -1;
5514 else
5515 *code_sec = sec;
5516 }
5517 if (sec->output_section != NULL)
5518 val += sec->output_section->vma + sec->output_offset;
5519 }
5520 break;
5521 }
5522 }
5523
5524 return val;
5525 }
5526
5527 /* If the ELF symbol SYM might be a function in SEC, return the
5528 function size and set *CODE_OFF to the function's entry point,
5529 otherwise return zero. */
5530
5531 static bfd_size_type
5532 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5533 bfd_vma *code_off)
5534 {
5535 bfd_size_type size;
5536
5537 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5538 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5539 return 0;
5540
5541 size = 0;
5542 if (!(sym->flags & BSF_SYNTHETIC))
5543 size = ((elf_symbol_type *) sym)->internal_elf_sym.st_size;
5544
5545 if (strcmp (sym->section->name, ".opd") == 0)
5546 {
5547 struct _opd_sec_data *opd = get_opd_info (sym->section);
5548 bfd_vma symval = sym->value;
5549
5550 if (opd != NULL
5551 && opd->adjust != NULL
5552 && elf_section_data (sym->section)->relocs != NULL)
5553 {
5554 /* opd_entry_value will use cached relocs that have been
5555 adjusted, but with raw symbols. That means both local
5556 and global symbols need adjusting. */
5557 long adjust = opd->adjust[OPD_NDX (symval)];
5558 if (adjust == -1)
5559 return 0;
5560 symval += adjust;
5561 }
5562
5563 if (opd_entry_value (sym->section, symval,
5564 &sec, code_off, true) == (bfd_vma) -1)
5565 return 0;
5566 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5567 symbol. This size has nothing to do with the code size of the
5568 function, which is what we're supposed to return, but the
5569 code size isn't available without looking up the dot-sym.
5570 However, doing that would be a waste of time particularly
5571 since elf_find_function will look at the dot-sym anyway.
5572 Now, elf_find_function will keep the largest size of any
5573 function sym found at the code address of interest, so return
5574 1 here to avoid it incorrectly caching a larger function size
5575 for a small function. This does mean we return the wrong
5576 size for a new-ABI function of size 24, but all that does is
5577 disable caching for such functions. */
5578 if (size == 24)
5579 size = 1;
5580 }
5581 else
5582 {
5583 if (sym->section != sec)
5584 return 0;
5585 *code_off = sym->value;
5586 }
5587 if (size == 0)
5588 size = 1;
5589 return size;
5590 }
5591
5592 /* Return true if symbol is a strong function defined in an ELFv2
5593 object with st_other localentry bits of zero, ie. its local entry
5594 point coincides with its global entry point. */
5595
5596 static bool
5597 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5598 {
5599 return (h != NULL
5600 && h->type == STT_FUNC
5601 && h->root.type == bfd_link_hash_defined
5602 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5603 && !ppc_elf_hash_entry (h)->non_zero_localentry
5604 && is_ppc64_elf (h->root.u.def.section->owner)
5605 && abiversion (h->root.u.def.section->owner) >= 2);
5606 }
5607
5608 /* Return true if symbol is defined in a regular object file. */
5609
5610 static bool
5611 is_static_defined (struct elf_link_hash_entry *h)
5612 {
5613 return ((h->root.type == bfd_link_hash_defined
5614 || h->root.type == bfd_link_hash_defweak)
5615 && h->root.u.def.section != NULL
5616 && h->root.u.def.section->output_section != NULL);
5617 }
5618
5619 /* If FDH is a function descriptor symbol, return the associated code
5620 entry symbol if it is defined. Return NULL otherwise. */
5621
5622 static struct ppc_link_hash_entry *
5623 defined_code_entry (struct ppc_link_hash_entry *fdh)
5624 {
5625 if (fdh->is_func_descriptor)
5626 {
5627 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5628 if (fh->elf.root.type == bfd_link_hash_defined
5629 || fh->elf.root.type == bfd_link_hash_defweak)
5630 return fh;
5631 }
5632 return NULL;
5633 }
5634
5635 /* If FH is a function code entry symbol, return the associated
5636 function descriptor symbol if it is defined. Return NULL otherwise. */
5637
5638 static struct ppc_link_hash_entry *
5639 defined_func_desc (struct ppc_link_hash_entry *fh)
5640 {
5641 if (fh->oh != NULL
5642 && fh->oh->is_func_descriptor)
5643 {
5644 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5645 if (fdh->elf.root.type == bfd_link_hash_defined
5646 || fdh->elf.root.type == bfd_link_hash_defweak)
5647 return fdh;
5648 }
5649 return NULL;
5650 }
5651
5652 /* Given H is a symbol that satisfies is_static_defined, return the
5653 value in the output file. */
5654
5655 static bfd_vma
5656 defined_sym_val (struct elf_link_hash_entry *h)
5657 {
5658 return (h->root.u.def.section->output_section->vma
5659 + h->root.u.def.section->output_offset
5660 + h->root.u.def.value);
5661 }
5662
5663 /* Return true if H matches __tls_get_addr or one of its variants. */
5664
5665 static bool
5666 is_tls_get_addr (struct elf_link_hash_entry *h,
5667 struct ppc_link_hash_table *htab)
5668 {
5669 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5670 || h == elf_hash_entry (htab->tga_desc_fd)
5671 || h == elf_hash_entry (htab->tls_get_addr)
5672 || h == elf_hash_entry (htab->tga_desc));
5673 }
5674
5675 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5676
5677 /* Garbage collect sections, after first dealing with dot-symbols. */
5678
5679 static bool
5680 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5681 {
5682 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5683
5684 if (htab != NULL && htab->need_func_desc_adj)
5685 {
5686 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5687 htab->need_func_desc_adj = 0;
5688 }
5689 return bfd_elf_gc_sections (abfd, info);
5690 }
5691
5692 /* Mark all our entry sym sections, both opd and code section. */
5693
5694 static void
5695 ppc64_elf_gc_keep (struct bfd_link_info *info)
5696 {
5697 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5698 struct bfd_sym_chain *sym;
5699
5700 if (htab == NULL)
5701 return;
5702
5703 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5704 {
5705 struct ppc_link_hash_entry *eh, *fh;
5706 asection *sec;
5707
5708 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5709 false, false, true));
5710 if (eh == NULL)
5711 continue;
5712 if (eh->elf.root.type != bfd_link_hash_defined
5713 && eh->elf.root.type != bfd_link_hash_defweak)
5714 continue;
5715
5716 fh = defined_code_entry (eh);
5717 if (fh != NULL)
5718 {
5719 sec = fh->elf.root.u.def.section;
5720 sec->flags |= SEC_KEEP;
5721 }
5722 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5723 && opd_entry_value (eh->elf.root.u.def.section,
5724 eh->elf.root.u.def.value,
5725 &sec, NULL, false) != (bfd_vma) -1)
5726 sec->flags |= SEC_KEEP;
5727
5728 sec = eh->elf.root.u.def.section;
5729 sec->flags |= SEC_KEEP;
5730 }
5731 }
5732
5733 /* Mark sections containing dynamically referenced symbols. When
5734 building shared libraries, we must assume that any visible symbol is
5735 referenced. */
5736
5737 static bool
5738 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5739 {
5740 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5741 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5742 struct ppc_link_hash_entry *fdh;
5743 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5744
5745 /* Dynamic linking info is on the func descriptor sym. */
5746 fdh = defined_func_desc (eh);
5747 if (fdh != NULL)
5748 eh = fdh;
5749
5750 if ((eh->elf.root.type == bfd_link_hash_defined
5751 || eh->elf.root.type == bfd_link_hash_defweak)
5752 && (!eh->elf.start_stop
5753 || eh->elf.root.ldscript_def
5754 || !info->start_stop_gc)
5755 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5756 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5757 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5758 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5759 && (!bfd_link_executable (info)
5760 || info->gc_keep_exported
5761 || info->export_dynamic
5762 || (eh->elf.dynamic
5763 && d != NULL
5764 && (*d->match) (&d->head, NULL,
5765 eh->elf.root.root.string)))
5766 && (eh->elf.versioned >= versioned
5767 || !bfd_hide_sym_by_version (info->version_info,
5768 eh->elf.root.root.string)))))
5769 {
5770 asection *code_sec;
5771 struct ppc_link_hash_entry *fh;
5772
5773 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5774
5775 /* Function descriptor syms cause the associated
5776 function code sym section to be marked. */
5777 fh = defined_code_entry (eh);
5778 if (fh != NULL)
5779 {
5780 code_sec = fh->elf.root.u.def.section;
5781 code_sec->flags |= SEC_KEEP;
5782 }
5783 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5784 && opd_entry_value (eh->elf.root.u.def.section,
5785 eh->elf.root.u.def.value,
5786 &code_sec, NULL, false) != (bfd_vma) -1)
5787 code_sec->flags |= SEC_KEEP;
5788 }
5789
5790 return true;
5791 }
5792
5793 /* Return the section that should be marked against GC for a given
5794 relocation. */
5795
5796 static asection *
5797 ppc64_elf_gc_mark_hook (asection *sec,
5798 struct bfd_link_info *info,
5799 Elf_Internal_Rela *rel,
5800 struct elf_link_hash_entry *h,
5801 Elf_Internal_Sym *sym)
5802 {
5803 asection *rsec;
5804
5805 /* Syms return NULL if we're marking .opd, so we avoid marking all
5806 function sections, as all functions are referenced in .opd. */
5807 rsec = NULL;
5808 if (get_opd_info (sec) != NULL)
5809 return rsec;
5810
5811 if (h != NULL)
5812 {
5813 enum elf_ppc64_reloc_type r_type;
5814 struct ppc_link_hash_entry *eh, *fh, *fdh;
5815
5816 r_type = ELF64_R_TYPE (rel->r_info);
5817 switch (r_type)
5818 {
5819 case R_PPC64_GNU_VTINHERIT:
5820 case R_PPC64_GNU_VTENTRY:
5821 break;
5822
5823 default:
5824 switch (h->root.type)
5825 {
5826 case bfd_link_hash_defined:
5827 case bfd_link_hash_defweak:
5828 eh = ppc_elf_hash_entry (h);
5829 fdh = defined_func_desc (eh);
5830 if (fdh != NULL)
5831 {
5832 /* -mcall-aixdesc code references the dot-symbol on
5833 a call reloc. Mark the function descriptor too
5834 against garbage collection. */
5835 fdh->elf.mark = 1;
5836 if (fdh->elf.is_weakalias)
5837 weakdef (&fdh->elf)->mark = 1;
5838 eh = fdh;
5839 }
5840
5841 /* Function descriptor syms cause the associated
5842 function code sym section to be marked. */
5843 fh = defined_code_entry (eh);
5844 if (fh != NULL)
5845 {
5846 /* They also mark their opd section. */
5847 eh->elf.root.u.def.section->gc_mark = 1;
5848
5849 rsec = fh->elf.root.u.def.section;
5850 }
5851 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5852 && opd_entry_value (eh->elf.root.u.def.section,
5853 eh->elf.root.u.def.value,
5854 &rsec, NULL, false) != (bfd_vma) -1)
5855 eh->elf.root.u.def.section->gc_mark = 1;
5856 else
5857 rsec = h->root.u.def.section;
5858 break;
5859
5860 case bfd_link_hash_common:
5861 rsec = h->root.u.c.p->section;
5862 break;
5863
5864 default:
5865 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5866 }
5867 }
5868 }
5869 else
5870 {
5871 struct _opd_sec_data *opd;
5872
5873 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
5874 opd = get_opd_info (rsec);
5875 if (opd != NULL && opd->func_sec != NULL)
5876 {
5877 rsec->gc_mark = 1;
5878
5879 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
5880 }
5881 }
5882
5883 return rsec;
5884 }
5885
5886 /* The maximum size of .sfpr. */
5887 #define SFPR_MAX (218*4)
5888
5889 struct sfpr_def_parms
5890 {
5891 const char name[12];
5892 unsigned char lo, hi;
5893 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
5894 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
5895 };
5896
5897 /* Auto-generate _save*, _rest* functions in .sfpr.
5898 If STUB_SEC is non-null, define alias symbols in STUB_SEC
5899 instead. */
5900
5901 static bool
5902 sfpr_define (struct bfd_link_info *info,
5903 const struct sfpr_def_parms *parm,
5904 asection *stub_sec)
5905 {
5906 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5907 unsigned int i;
5908 size_t len = strlen (parm->name);
5909 bool writing = false;
5910 char sym[16];
5911
5912 if (htab == NULL)
5913 return false;
5914
5915 memcpy (sym, parm->name, len);
5916 sym[len + 2] = 0;
5917
5918 for (i = parm->lo; i <= parm->hi; i++)
5919 {
5920 struct ppc_link_hash_entry *h;
5921
5922 sym[len + 0] = i / 10 + '0';
5923 sym[len + 1] = i % 10 + '0';
5924 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
5925 writing, true, true));
5926 if (stub_sec != NULL)
5927 {
5928 if (h != NULL
5929 && h->elf.root.type == bfd_link_hash_defined
5930 && h->elf.root.u.def.section == htab->sfpr)
5931 {
5932 struct elf_link_hash_entry *s;
5933 char buf[32];
5934 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
5935 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
5936 if (s == NULL)
5937 return false;
5938 if (s->root.type == bfd_link_hash_new)
5939 {
5940 s->root.type = bfd_link_hash_defined;
5941 s->root.u.def.section = stub_sec;
5942 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
5943 + h->elf.root.u.def.value);
5944 s->ref_regular = 1;
5945 s->def_regular = 1;
5946 s->ref_regular_nonweak = 1;
5947 s->forced_local = 1;
5948 s->non_elf = 0;
5949 s->root.linker_def = 1;
5950 }
5951 }
5952 continue;
5953 }
5954 if (h != NULL)
5955 {
5956 h->save_res = 1;
5957 if (!h->elf.def_regular)
5958 {
5959 h->elf.root.type = bfd_link_hash_defined;
5960 h->elf.root.u.def.section = htab->sfpr;
5961 h->elf.root.u.def.value = htab->sfpr->size;
5962 h->elf.type = STT_FUNC;
5963 h->elf.def_regular = 1;
5964 h->elf.non_elf = 0;
5965 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
5966 writing = true;
5967 if (htab->sfpr->contents == NULL)
5968 {
5969 htab->sfpr->contents
5970 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
5971 if (htab->sfpr->contents == NULL)
5972 return false;
5973 }
5974 }
5975 }
5976 if (writing)
5977 {
5978 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
5979 if (i != parm->hi)
5980 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
5981 else
5982 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
5983 htab->sfpr->size = p - htab->sfpr->contents;
5984 }
5985 }
5986
5987 return true;
5988 }
5989
5990 static bfd_byte *
5991 savegpr0 (bfd *abfd, bfd_byte *p, int r)
5992 {
5993 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
5994 return p + 4;
5995 }
5996
5997 static bfd_byte *
5998 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
5999 {
6000 p = savegpr0 (abfd, p, r);
6001 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6002 p = p + 4;
6003 bfd_put_32 (abfd, BLR, p);
6004 return p + 4;
6005 }
6006
6007 static bfd_byte *
6008 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6009 {
6010 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6011 return p + 4;
6012 }
6013
6014 static bfd_byte *
6015 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6016 {
6017 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6018 p = p + 4;
6019 p = restgpr0 (abfd, p, r);
6020 bfd_put_32 (abfd, MTLR_R0, p);
6021 p = p + 4;
6022 if (r == 29)
6023 {
6024 p = restgpr0 (abfd, p, 30);
6025 p = restgpr0 (abfd, p, 31);
6026 }
6027 bfd_put_32 (abfd, BLR, p);
6028 return p + 4;
6029 }
6030
6031 static bfd_byte *
6032 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6033 {
6034 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6035 return p + 4;
6036 }
6037
6038 static bfd_byte *
6039 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6040 {
6041 p = savegpr1 (abfd, p, r);
6042 bfd_put_32 (abfd, BLR, p);
6043 return p + 4;
6044 }
6045
6046 static bfd_byte *
6047 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6048 {
6049 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6050 return p + 4;
6051 }
6052
6053 static bfd_byte *
6054 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6055 {
6056 p = restgpr1 (abfd, p, r);
6057 bfd_put_32 (abfd, BLR, p);
6058 return p + 4;
6059 }
6060
6061 static bfd_byte *
6062 savefpr (bfd *abfd, bfd_byte *p, int r)
6063 {
6064 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6065 return p + 4;
6066 }
6067
6068 static bfd_byte *
6069 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6070 {
6071 p = savefpr (abfd, p, r);
6072 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6073 p = p + 4;
6074 bfd_put_32 (abfd, BLR, p);
6075 return p + 4;
6076 }
6077
6078 static bfd_byte *
6079 restfpr (bfd *abfd, bfd_byte *p, int r)
6080 {
6081 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6082 return p + 4;
6083 }
6084
6085 static bfd_byte *
6086 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6087 {
6088 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6089 p = p + 4;
6090 p = restfpr (abfd, p, r);
6091 bfd_put_32 (abfd, MTLR_R0, p);
6092 p = p + 4;
6093 if (r == 29)
6094 {
6095 p = restfpr (abfd, p, 30);
6096 p = restfpr (abfd, p, 31);
6097 }
6098 bfd_put_32 (abfd, BLR, p);
6099 return p + 4;
6100 }
6101
6102 static bfd_byte *
6103 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6104 {
6105 p = savefpr (abfd, p, r);
6106 bfd_put_32 (abfd, BLR, p);
6107 return p + 4;
6108 }
6109
6110 static bfd_byte *
6111 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6112 {
6113 p = restfpr (abfd, p, r);
6114 bfd_put_32 (abfd, BLR, p);
6115 return p + 4;
6116 }
6117
6118 static bfd_byte *
6119 savevr (bfd *abfd, bfd_byte *p, int r)
6120 {
6121 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6122 p = p + 4;
6123 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6124 return p + 4;
6125 }
6126
6127 static bfd_byte *
6128 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6129 {
6130 p = savevr (abfd, p, r);
6131 bfd_put_32 (abfd, BLR, p);
6132 return p + 4;
6133 }
6134
6135 static bfd_byte *
6136 restvr (bfd *abfd, bfd_byte *p, int r)
6137 {
6138 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6139 p = p + 4;
6140 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6141 return p + 4;
6142 }
6143
6144 static bfd_byte *
6145 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6146 {
6147 p = restvr (abfd, p, r);
6148 bfd_put_32 (abfd, BLR, p);
6149 return p + 4;
6150 }
6151
6152 #define STDU_R1_0R1 0xf8210001
6153 #define ADDI_R1_R1 0x38210000
6154
6155 /* Emit prologue of wrapper preserving regs around a call to
6156 __tls_get_addr_opt. */
6157
6158 static bfd_byte *
6159 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6160 {
6161 unsigned int i;
6162
6163 bfd_put_32 (obfd, MFLR_R0, p);
6164 p += 4;
6165 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6166 p += 4;
6167
6168 if (htab->opd_abi)
6169 {
6170 for (i = 4; i < 12; i++)
6171 {
6172 bfd_put_32 (obfd,
6173 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6174 p += 4;
6175 }
6176 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6177 p += 4;
6178 }
6179 else
6180 {
6181 for (i = 4; i < 12; i++)
6182 {
6183 bfd_put_32 (obfd,
6184 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6185 p += 4;
6186 }
6187 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6188 p += 4;
6189 }
6190 return p;
6191 }
6192
6193 /* Emit epilogue of wrapper preserving regs around a call to
6194 __tls_get_addr_opt. */
6195
6196 static bfd_byte *
6197 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6198 {
6199 unsigned int i;
6200
6201 if (htab->opd_abi)
6202 {
6203 for (i = 4; i < 12; i++)
6204 {
6205 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6206 p += 4;
6207 }
6208 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6209 p += 4;
6210 }
6211 else
6212 {
6213 for (i = 4; i < 12; i++)
6214 {
6215 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6216 p += 4;
6217 }
6218 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6219 p += 4;
6220 }
6221 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6222 p += 4;
6223 bfd_put_32 (obfd, MTLR_R0, p);
6224 p += 4;
6225 bfd_put_32 (obfd, BLR, p);
6226 p += 4;
6227 return p;
6228 }
6229
6230 /* Called via elf_link_hash_traverse to transfer dynamic linking
6231 information on function code symbol entries to their corresponding
6232 function descriptor symbol entries. */
6233
6234 static bool
6235 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6236 {
6237 struct bfd_link_info *info;
6238 struct ppc_link_hash_table *htab;
6239 struct ppc_link_hash_entry *fh;
6240 struct ppc_link_hash_entry *fdh;
6241 bool force_local;
6242
6243 fh = ppc_elf_hash_entry (h);
6244 if (fh->elf.root.type == bfd_link_hash_indirect)
6245 return true;
6246
6247 if (!fh->is_func)
6248 return true;
6249
6250 if (fh->elf.root.root.string[0] != '.'
6251 || fh->elf.root.root.string[1] == '\0')
6252 return true;
6253
6254 info = inf;
6255 htab = ppc_hash_table (info);
6256 if (htab == NULL)
6257 return false;
6258
6259 /* Find the corresponding function descriptor symbol. */
6260 fdh = lookup_fdh (fh, htab);
6261
6262 /* Resolve undefined references to dot-symbols as the value
6263 in the function descriptor, if we have one in a regular object.
6264 This is to satisfy cases like ".quad .foo". Calls to functions
6265 in dynamic objects are handled elsewhere. */
6266 if ((fh->elf.root.type == bfd_link_hash_undefined
6267 || fh->elf.root.type == bfd_link_hash_undefweak)
6268 && (fdh->elf.root.type == bfd_link_hash_defined
6269 || fdh->elf.root.type == bfd_link_hash_defweak)
6270 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6271 && opd_entry_value (fdh->elf.root.u.def.section,
6272 fdh->elf.root.u.def.value,
6273 &fh->elf.root.u.def.section,
6274 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6275 {
6276 fh->elf.root.type = fdh->elf.root.type;
6277 fh->elf.forced_local = 1;
6278 fh->elf.def_regular = fdh->elf.def_regular;
6279 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6280 }
6281
6282 if (!fh->elf.dynamic)
6283 {
6284 struct plt_entry *ent;
6285
6286 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6287 if (ent->plt.refcount > 0)
6288 break;
6289 if (ent == NULL)
6290 return true;
6291 }
6292
6293 /* Create a descriptor as undefined if necessary. */
6294 if (fdh == NULL
6295 && !bfd_link_executable (info)
6296 && (fh->elf.root.type == bfd_link_hash_undefined
6297 || fh->elf.root.type == bfd_link_hash_undefweak))
6298 {
6299 fdh = make_fdh (info, fh);
6300 if (fdh == NULL)
6301 return false;
6302 }
6303
6304 /* We can't support overriding of symbols on a fake descriptor. */
6305 if (fdh != NULL
6306 && fdh->fake
6307 && (fh->elf.root.type == bfd_link_hash_defined
6308 || fh->elf.root.type == bfd_link_hash_defweak))
6309 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6310
6311 /* Transfer dynamic linking information to the function descriptor. */
6312 if (fdh != NULL)
6313 {
6314 fdh->elf.ref_regular |= fh->elf.ref_regular;
6315 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6316 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6317 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6318 fdh->elf.dynamic |= fh->elf.dynamic;
6319 fdh->elf.needs_plt |= (fh->elf.needs_plt
6320 || fh->elf.type == STT_FUNC
6321 || fh->elf.type == STT_GNU_IFUNC);
6322 move_plt_plist (fh, fdh);
6323
6324 if (!fdh->elf.forced_local
6325 && fh->elf.dynindx != -1)
6326 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6327 return false;
6328 }
6329
6330 /* Now that the info is on the function descriptor, clear the
6331 function code sym info. Any function code syms for which we
6332 don't have a definition in a regular file, we force local.
6333 This prevents a shared library from exporting syms that have
6334 been imported from another library. Function code syms that
6335 are really in the library we must leave global to prevent the
6336 linker dragging in a definition from a static library. */
6337 force_local = (!fh->elf.def_regular
6338 || fdh == NULL
6339 || !fdh->elf.def_regular
6340 || fdh->elf.forced_local);
6341 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6342
6343 return true;
6344 }
6345
6346 static const struct sfpr_def_parms save_res_funcs[] =
6347 {
6348 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6349 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6350 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6351 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6352 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6353 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6354 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6355 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6356 { "._savef", 14, 31, savefpr, savefpr1_tail },
6357 { "._restf", 14, 31, restfpr, restfpr1_tail },
6358 { "_savevr_", 20, 31, savevr, savevr_tail },
6359 { "_restvr_", 20, 31, restvr, restvr_tail }
6360 };
6361
6362 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6363 this hook to a) run the edit functions in this file, b) provide
6364 some gcc support functions, and c) transfer dynamic linking
6365 information gathered so far on function code symbol entries, to
6366 their corresponding function descriptor symbol entries. */
6367
6368 static bool
6369 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6370 {
6371 struct ppc_link_hash_table *htab;
6372
6373 htab = ppc_hash_table (info);
6374 if (htab == NULL)
6375 return false;
6376
6377 /* Call back into the linker, which then runs the edit functions. */
6378 htab->params->edit ();
6379
6380 /* Provide any missing _save* and _rest* functions. */
6381 if (htab->sfpr != NULL)
6382 {
6383 unsigned int i;
6384
6385 htab->sfpr->size = 0;
6386 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6387 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6388 return false;
6389 if (htab->sfpr->size == 0)
6390 htab->sfpr->flags |= SEC_EXCLUDE;
6391 }
6392
6393 if (bfd_link_relocatable (info))
6394 return true;
6395
6396 if (htab->elf.hgot != NULL)
6397 {
6398 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6399 /* Make .TOC. defined so as to prevent it being made dynamic.
6400 The wrong value here is fixed later in ppc64_elf_set_toc. */
6401 if (!htab->elf.hgot->def_regular
6402 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6403 {
6404 htab->elf.hgot->root.type = bfd_link_hash_defined;
6405 htab->elf.hgot->root.u.def.value = 0;
6406 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6407 htab->elf.hgot->def_regular = 1;
6408 htab->elf.hgot->root.linker_def = 1;
6409 }
6410 htab->elf.hgot->type = STT_OBJECT;
6411 htab->elf.hgot->other
6412 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6413 }
6414
6415 if (htab->need_func_desc_adj)
6416 {
6417 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
6418 htab->need_func_desc_adj = 0;
6419 }
6420
6421 return true;
6422 }
6423
6424 /* Return true if we have dynamic relocs against H or any of its weak
6425 aliases, that apply to read-only sections. Cannot be used after
6426 size_dynamic_sections. */
6427
6428 static bool
6429 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6430 {
6431 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6432 do
6433 {
6434 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6435 return true;
6436 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6437 }
6438 while (eh != NULL && &eh->elf != h);
6439
6440 return false;
6441 }
6442
6443 /* Return whether EH has pc-relative dynamic relocs. */
6444
6445 static bool
6446 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6447 {
6448 struct elf_dyn_relocs *p;
6449
6450 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
6451 if (p->pc_count != 0)
6452 return true;
6453 return false;
6454 }
6455
6456 /* Return true if a global entry stub will be created for H. Valid
6457 for ELFv2 before plt entries have been allocated. */
6458
6459 static bool
6460 global_entry_stub (struct elf_link_hash_entry *h)
6461 {
6462 struct plt_entry *pent;
6463
6464 if (!h->pointer_equality_needed
6465 || h->def_regular)
6466 return false;
6467
6468 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6469 if (pent->plt.refcount > 0
6470 && pent->addend == 0)
6471 return true;
6472
6473 return false;
6474 }
6475
6476 /* Adjust a symbol defined by a dynamic object and referenced by a
6477 regular object. The current definition is in some section of the
6478 dynamic object, but we're not including those sections. We have to
6479 change the definition to something the rest of the link can
6480 understand. */
6481
6482 static bool
6483 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6484 struct elf_link_hash_entry *h)
6485 {
6486 struct ppc_link_hash_table *htab;
6487 asection *s, *srel;
6488
6489 htab = ppc_hash_table (info);
6490 if (htab == NULL)
6491 return false;
6492
6493 /* Deal with function syms. */
6494 if (h->type == STT_FUNC
6495 || h->type == STT_GNU_IFUNC
6496 || h->needs_plt)
6497 {
6498 bool local = (ppc_elf_hash_entry (h)->save_res
6499 || SYMBOL_CALLS_LOCAL (info, h)
6500 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6501 /* Discard dyn_relocs when non-pic if we've decided that a
6502 function symbol is local and not an ifunc. We keep dynamic
6503 relocs for ifuncs when local rather than always emitting a
6504 plt call stub for them and defining the symbol on the call
6505 stub. We can't do that for ELFv1 anyway (a function symbol
6506 is defined on a descriptor, not code) and it can be faster at
6507 run-time due to not needing to bounce through a stub. The
6508 dyn_relocs for ifuncs will be applied even in a static
6509 executable. */
6510 if (!bfd_link_pic (info)
6511 && h->type != STT_GNU_IFUNC
6512 && local)
6513 h->dyn_relocs = NULL;
6514
6515 /* Clear procedure linkage table information for any symbol that
6516 won't need a .plt entry. */
6517 struct plt_entry *ent;
6518 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6519 if (ent->plt.refcount > 0)
6520 break;
6521 if (ent == NULL
6522 || (h->type != STT_GNU_IFUNC
6523 && local
6524 && (htab->can_convert_all_inline_plt
6525 || (ppc_elf_hash_entry (h)->tls_mask
6526 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6527 {
6528 h->plt.plist = NULL;
6529 h->needs_plt = 0;
6530 h->pointer_equality_needed = 0;
6531 }
6532 else if (abiversion (info->output_bfd) >= 2)
6533 {
6534 /* Taking a function's address in a read/write section
6535 doesn't require us to define the function symbol in the
6536 executable on a global entry stub. A dynamic reloc can
6537 be used instead. The reason we prefer a few more dynamic
6538 relocs is that calling via a global entry stub costs a
6539 few more instructions, and pointer_equality_needed causes
6540 extra work in ld.so when resolving these symbols. */
6541 if (global_entry_stub (h))
6542 {
6543 if (!_bfd_elf_readonly_dynrelocs (h))
6544 {
6545 h->pointer_equality_needed = 0;
6546 /* If we haven't seen a branch reloc and the symbol
6547 isn't an ifunc then we don't need a plt entry. */
6548 if (!h->needs_plt)
6549 h->plt.plist = NULL;
6550 }
6551 else if (!bfd_link_pic (info))
6552 /* We are going to be defining the function symbol on the
6553 plt stub, so no dyn_relocs needed when non-pic. */
6554 h->dyn_relocs = NULL;
6555 }
6556
6557 /* ELFv2 function symbols can't have copy relocs. */
6558 return true;
6559 }
6560 else if (!h->needs_plt
6561 && !_bfd_elf_readonly_dynrelocs (h))
6562 {
6563 /* If we haven't seen a branch reloc and the symbol isn't an
6564 ifunc then we don't need a plt entry. */
6565 h->plt.plist = NULL;
6566 h->pointer_equality_needed = 0;
6567 return true;
6568 }
6569 }
6570 else
6571 h->plt.plist = NULL;
6572
6573 /* If this is a weak symbol, and there is a real definition, the
6574 processor independent code will have arranged for us to see the
6575 real definition first, and we can just use the same value. */
6576 if (h->is_weakalias)
6577 {
6578 struct elf_link_hash_entry *def = weakdef (h);
6579 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6580 h->root.u.def.section = def->root.u.def.section;
6581 h->root.u.def.value = def->root.u.def.value;
6582 if (def->root.u.def.section == htab->elf.sdynbss
6583 || def->root.u.def.section == htab->elf.sdynrelro)
6584 h->dyn_relocs = NULL;
6585 return true;
6586 }
6587
6588 /* If we are creating a shared library, we must presume that the
6589 only references to the symbol are via the global offset table.
6590 For such cases we need not do anything here; the relocations will
6591 be handled correctly by relocate_section. */
6592 if (!bfd_link_executable (info))
6593 return true;
6594
6595 /* If there are no references to this symbol that do not use the
6596 GOT, we don't need to generate a copy reloc. */
6597 if (!h->non_got_ref)
6598 return true;
6599
6600 /* Don't generate a copy reloc for symbols defined in the executable. */
6601 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6602
6603 /* If -z nocopyreloc was given, don't generate them either. */
6604 || info->nocopyreloc
6605
6606 /* If we don't find any dynamic relocs in read-only sections, then
6607 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6608 || (ELIMINATE_COPY_RELOCS
6609 && !h->needs_copy
6610 && !alias_readonly_dynrelocs (h))
6611
6612 /* Protected variables do not work with .dynbss. The copy in
6613 .dynbss won't be used by the shared library with the protected
6614 definition for the variable. Text relocations are preferable
6615 to an incorrect program. */
6616 || h->protected_def)
6617 return true;
6618
6619 if (h->type == STT_FUNC
6620 || h->type == STT_GNU_IFUNC)
6621 {
6622 /* .dynbss copies of function symbols only work if we have
6623 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6624 use dot-symbols and set the function symbol size to the text
6625 size of the function rather than the size of the descriptor.
6626 That's wrong for copying a descriptor. */
6627 if (ppc_elf_hash_entry (h)->oh == NULL
6628 || !(h->size == 24 || h->size == 16))
6629 return true;
6630
6631 /* We should never get here, but unfortunately there are old
6632 versions of gcc (circa gcc-3.2) that improperly for the
6633 ELFv1 ABI put initialized function pointers, vtable refs and
6634 suchlike in read-only sections. Allow them to proceed, but
6635 warn that this might break at runtime. */
6636 info->callbacks->einfo
6637 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6638 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6639 h->root.root.string);
6640 }
6641
6642 /* This is a reference to a symbol defined by a dynamic object which
6643 is not a function. */
6644
6645 /* We must allocate the symbol in our .dynbss section, which will
6646 become part of the .bss section of the executable. There will be
6647 an entry for this symbol in the .dynsym section. The dynamic
6648 object will contain position independent code, so all references
6649 from the dynamic object to this symbol will go through the global
6650 offset table. The dynamic linker will use the .dynsym entry to
6651 determine the address it must put in the global offset table, so
6652 both the dynamic object and the regular object will refer to the
6653 same memory location for the variable. */
6654 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6655 {
6656 s = htab->elf.sdynrelro;
6657 srel = htab->elf.sreldynrelro;
6658 }
6659 else
6660 {
6661 s = htab->elf.sdynbss;
6662 srel = htab->elf.srelbss;
6663 }
6664 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6665 {
6666 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6667 linker to copy the initial value out of the dynamic object
6668 and into the runtime process image. */
6669 srel->size += sizeof (Elf64_External_Rela);
6670 h->needs_copy = 1;
6671 }
6672
6673 /* We no longer want dyn_relocs. */
6674 h->dyn_relocs = NULL;
6675 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6676 }
6677
6678 /* If given a function descriptor symbol, hide both the function code
6679 sym and the descriptor. */
6680 static void
6681 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6682 struct elf_link_hash_entry *h,
6683 bool force_local)
6684 {
6685 struct ppc_link_hash_entry *eh;
6686 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6687
6688 if (ppc_hash_table (info) == NULL)
6689 return;
6690
6691 eh = ppc_elf_hash_entry (h);
6692 if (eh->is_func_descriptor)
6693 {
6694 struct ppc_link_hash_entry *fh = eh->oh;
6695
6696 if (fh == NULL)
6697 {
6698 const char *p, *q;
6699 struct elf_link_hash_table *htab = elf_hash_table (info);
6700 char save;
6701
6702 /* We aren't supposed to use alloca in BFD because on
6703 systems which do not have alloca the version in libiberty
6704 calls xmalloc, which might cause the program to crash
6705 when it runs out of memory. This function doesn't have a
6706 return status, so there's no way to gracefully return an
6707 error. So cheat. We know that string[-1] can be safely
6708 accessed; It's either a string in an ELF string table,
6709 or allocated in an objalloc structure. */
6710
6711 p = eh->elf.root.root.string - 1;
6712 save = *p;
6713 *(char *) p = '.';
6714 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6715 false, false));
6716 *(char *) p = save;
6717
6718 /* Unfortunately, if it so happens that the string we were
6719 looking for was allocated immediately before this string,
6720 then we overwrote the string terminator. That's the only
6721 reason the lookup should fail. */
6722 if (fh == NULL)
6723 {
6724 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6725 while (q >= eh->elf.root.root.string && *q == *p)
6726 --q, --p;
6727 if (q < eh->elf.root.root.string && *p == '.')
6728 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6729 false, false));
6730 }
6731 if (fh != NULL)
6732 {
6733 eh->oh = fh;
6734 fh->oh = eh;
6735 }
6736 }
6737 if (fh != NULL)
6738 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6739 }
6740 }
6741
6742 static bool
6743 get_sym_h (struct elf_link_hash_entry **hp,
6744 Elf_Internal_Sym **symp,
6745 asection **symsecp,
6746 unsigned char **tls_maskp,
6747 Elf_Internal_Sym **locsymsp,
6748 unsigned long r_symndx,
6749 bfd *ibfd)
6750 {
6751 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6752
6753 if (r_symndx >= symtab_hdr->sh_info)
6754 {
6755 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6756 struct elf_link_hash_entry *h;
6757
6758 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6759 h = elf_follow_link (h);
6760
6761 if (hp != NULL)
6762 *hp = h;
6763
6764 if (symp != NULL)
6765 *symp = NULL;
6766
6767 if (symsecp != NULL)
6768 {
6769 asection *symsec = NULL;
6770 if (h->root.type == bfd_link_hash_defined
6771 || h->root.type == bfd_link_hash_defweak)
6772 symsec = h->root.u.def.section;
6773 *symsecp = symsec;
6774 }
6775
6776 if (tls_maskp != NULL)
6777 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6778 }
6779 else
6780 {
6781 Elf_Internal_Sym *sym;
6782 Elf_Internal_Sym *locsyms = *locsymsp;
6783
6784 if (locsyms == NULL)
6785 {
6786 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6787 if (locsyms == NULL)
6788 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6789 symtab_hdr->sh_info,
6790 0, NULL, NULL, NULL);
6791 if (locsyms == NULL)
6792 return false;
6793 *locsymsp = locsyms;
6794 }
6795 sym = locsyms + r_symndx;
6796
6797 if (hp != NULL)
6798 *hp = NULL;
6799
6800 if (symp != NULL)
6801 *symp = sym;
6802
6803 if (symsecp != NULL)
6804 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6805
6806 if (tls_maskp != NULL)
6807 {
6808 struct got_entry **lgot_ents;
6809 unsigned char *tls_mask;
6810
6811 tls_mask = NULL;
6812 lgot_ents = elf_local_got_ents (ibfd);
6813 if (lgot_ents != NULL)
6814 {
6815 struct plt_entry **local_plt = (struct plt_entry **)
6816 (lgot_ents + symtab_hdr->sh_info);
6817 unsigned char *lgot_masks = (unsigned char *)
6818 (local_plt + symtab_hdr->sh_info);
6819 tls_mask = &lgot_masks[r_symndx];
6820 }
6821 *tls_maskp = tls_mask;
6822 }
6823 }
6824 return true;
6825 }
6826
6827 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6828 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6829 type suitable for optimization, and 1 otherwise. */
6830
6831 static int
6832 get_tls_mask (unsigned char **tls_maskp,
6833 unsigned long *toc_symndx,
6834 bfd_vma *toc_addend,
6835 Elf_Internal_Sym **locsymsp,
6836 const Elf_Internal_Rela *rel,
6837 bfd *ibfd)
6838 {
6839 unsigned long r_symndx;
6840 int next_r;
6841 struct elf_link_hash_entry *h;
6842 Elf_Internal_Sym *sym;
6843 asection *sec;
6844 bfd_vma off;
6845
6846 r_symndx = ELF64_R_SYM (rel->r_info);
6847 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6848 return 0;
6849
6850 if ((*tls_maskp != NULL
6851 && (**tls_maskp & TLS_TLS) != 0
6852 && **tls_maskp != (TLS_TLS | TLS_MARK))
6853 || sec == NULL
6854 || ppc64_elf_section_data (sec) == NULL
6855 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6856 return 1;
6857
6858 /* Look inside a TOC section too. */
6859 if (h != NULL)
6860 {
6861 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6862 off = h->root.u.def.value;
6863 }
6864 else
6865 off = sym->st_value;
6866 off += rel->r_addend;
6867 BFD_ASSERT (off % 8 == 0);
6868 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
6869 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
6870 if (toc_symndx != NULL)
6871 *toc_symndx = r_symndx;
6872 if (toc_addend != NULL)
6873 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
6874 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6875 return 0;
6876 if ((h == NULL || is_static_defined (h))
6877 && (next_r == -1 || next_r == -2))
6878 return 1 - next_r;
6879 return 1;
6880 }
6881
6882 /* Find (or create) an entry in the tocsave hash table. */
6883
6884 static struct tocsave_entry *
6885 tocsave_find (struct ppc_link_hash_table *htab,
6886 enum insert_option insert,
6887 Elf_Internal_Sym **local_syms,
6888 const Elf_Internal_Rela *irela,
6889 bfd *ibfd)
6890 {
6891 unsigned long r_indx;
6892 struct elf_link_hash_entry *h;
6893 Elf_Internal_Sym *sym;
6894 struct tocsave_entry ent, *p;
6895 hashval_t hash;
6896 struct tocsave_entry **slot;
6897
6898 r_indx = ELF64_R_SYM (irela->r_info);
6899 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
6900 return NULL;
6901 if (ent.sec == NULL || ent.sec->output_section == NULL)
6902 {
6903 _bfd_error_handler
6904 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
6905 return NULL;
6906 }
6907
6908 if (h != NULL)
6909 ent.offset = h->root.u.def.value;
6910 else
6911 ent.offset = sym->st_value;
6912 ent.offset += irela->r_addend;
6913
6914 hash = tocsave_htab_hash (&ent);
6915 slot = ((struct tocsave_entry **)
6916 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
6917 if (slot == NULL)
6918 return NULL;
6919
6920 if (*slot == NULL)
6921 {
6922 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
6923 if (p == NULL)
6924 return NULL;
6925 *p = ent;
6926 *slot = p;
6927 }
6928 return *slot;
6929 }
6930
6931 /* Adjust all global syms defined in opd sections. In gcc generated
6932 code for the old ABI, these will already have been done. */
6933
6934 static bool
6935 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
6936 {
6937 struct ppc_link_hash_entry *eh;
6938 asection *sym_sec;
6939 struct _opd_sec_data *opd;
6940
6941 if (h->root.type == bfd_link_hash_indirect)
6942 return true;
6943
6944 if (h->root.type != bfd_link_hash_defined
6945 && h->root.type != bfd_link_hash_defweak)
6946 return true;
6947
6948 eh = ppc_elf_hash_entry (h);
6949 if (eh->adjust_done)
6950 return true;
6951
6952 sym_sec = eh->elf.root.u.def.section;
6953 opd = get_opd_info (sym_sec);
6954 if (opd != NULL && opd->adjust != NULL)
6955 {
6956 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
6957 if (adjust == -1)
6958 {
6959 /* This entry has been deleted. */
6960 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
6961 if (dsec == NULL)
6962 {
6963 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
6964 if (discarded_section (dsec))
6965 {
6966 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
6967 break;
6968 }
6969 }
6970 eh->elf.root.u.def.value = 0;
6971 eh->elf.root.u.def.section = dsec;
6972 }
6973 else
6974 eh->elf.root.u.def.value += adjust;
6975 eh->adjust_done = 1;
6976 }
6977 return true;
6978 }
6979
6980 /* Handles decrementing dynamic reloc counts for the reloc specified by
6981 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
6982 have already been determined. */
6983
6984 static bool
6985 dec_dynrel_count (bfd_vma r_info,
6986 asection *sec,
6987 struct bfd_link_info *info,
6988 Elf_Internal_Sym **local_syms,
6989 struct elf_link_hash_entry *h,
6990 Elf_Internal_Sym *sym)
6991 {
6992 enum elf_ppc64_reloc_type r_type;
6993 asection *sym_sec = NULL;
6994
6995 /* Can this reloc be dynamic? This switch, and later tests here
6996 should be kept in sync with the code in check_relocs. */
6997 r_type = ELF64_R_TYPE (r_info);
6998 switch (r_type)
6999 {
7000 default:
7001 return true;
7002
7003 case R_PPC64_TOC16:
7004 case R_PPC64_TOC16_DS:
7005 case R_PPC64_TOC16_LO:
7006 case R_PPC64_TOC16_HI:
7007 case R_PPC64_TOC16_HA:
7008 case R_PPC64_TOC16_LO_DS:
7009 if (h == NULL)
7010 return true;
7011 break;
7012
7013 case R_PPC64_TPREL16:
7014 case R_PPC64_TPREL16_LO:
7015 case R_PPC64_TPREL16_HI:
7016 case R_PPC64_TPREL16_HA:
7017 case R_PPC64_TPREL16_DS:
7018 case R_PPC64_TPREL16_LO_DS:
7019 case R_PPC64_TPREL16_HIGH:
7020 case R_PPC64_TPREL16_HIGHA:
7021 case R_PPC64_TPREL16_HIGHER:
7022 case R_PPC64_TPREL16_HIGHERA:
7023 case R_PPC64_TPREL16_HIGHEST:
7024 case R_PPC64_TPREL16_HIGHESTA:
7025 case R_PPC64_TPREL64:
7026 case R_PPC64_TPREL34:
7027 case R_PPC64_DTPMOD64:
7028 case R_PPC64_DTPREL64:
7029 case R_PPC64_ADDR64:
7030 case R_PPC64_REL30:
7031 case R_PPC64_REL32:
7032 case R_PPC64_REL64:
7033 case R_PPC64_ADDR14:
7034 case R_PPC64_ADDR14_BRNTAKEN:
7035 case R_PPC64_ADDR14_BRTAKEN:
7036 case R_PPC64_ADDR16:
7037 case R_PPC64_ADDR16_DS:
7038 case R_PPC64_ADDR16_HA:
7039 case R_PPC64_ADDR16_HI:
7040 case R_PPC64_ADDR16_HIGH:
7041 case R_PPC64_ADDR16_HIGHA:
7042 case R_PPC64_ADDR16_HIGHER:
7043 case R_PPC64_ADDR16_HIGHERA:
7044 case R_PPC64_ADDR16_HIGHEST:
7045 case R_PPC64_ADDR16_HIGHESTA:
7046 case R_PPC64_ADDR16_LO:
7047 case R_PPC64_ADDR16_LO_DS:
7048 case R_PPC64_ADDR24:
7049 case R_PPC64_ADDR32:
7050 case R_PPC64_UADDR16:
7051 case R_PPC64_UADDR32:
7052 case R_PPC64_UADDR64:
7053 case R_PPC64_TOC:
7054 case R_PPC64_D34:
7055 case R_PPC64_D34_LO:
7056 case R_PPC64_D34_HI30:
7057 case R_PPC64_D34_HA30:
7058 case R_PPC64_ADDR16_HIGHER34:
7059 case R_PPC64_ADDR16_HIGHERA34:
7060 case R_PPC64_ADDR16_HIGHEST34:
7061 case R_PPC64_ADDR16_HIGHESTA34:
7062 case R_PPC64_D28:
7063 break;
7064 }
7065
7066 if (local_syms != NULL)
7067 {
7068 unsigned long r_symndx;
7069 bfd *ibfd = sec->owner;
7070
7071 r_symndx = ELF64_R_SYM (r_info);
7072 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7073 return false;
7074 }
7075
7076 if ((h != NULL
7077 && (h->root.type == bfd_link_hash_defweak
7078 || !h->def_regular))
7079 || (h != NULL
7080 && !bfd_link_executable (info)
7081 && !SYMBOLIC_BIND (info, h))
7082 || (bfd_link_pic (info)
7083 && must_be_dyn_reloc (info, r_type))
7084 || (!bfd_link_pic (info)
7085 && (h != NULL
7086 ? h->type == STT_GNU_IFUNC
7087 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7088 ;
7089 else
7090 return true;
7091
7092 if (h != NULL)
7093 {
7094 struct elf_dyn_relocs *p;
7095 struct elf_dyn_relocs **pp;
7096 pp = &h->dyn_relocs;
7097
7098 /* elf_gc_sweep may have already removed all dyn relocs associated
7099 with local syms for a given section. Also, symbol flags are
7100 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7101 report a dynreloc miscount. */
7102 if (*pp == NULL && info->gc_sections)
7103 return true;
7104
7105 while ((p = *pp) != NULL)
7106 {
7107 if (p->sec == sec)
7108 {
7109 if (!must_be_dyn_reloc (info, r_type))
7110 p->pc_count -= 1;
7111 p->count -= 1;
7112 if (p->count == 0)
7113 *pp = p->next;
7114 return true;
7115 }
7116 pp = &p->next;
7117 }
7118 }
7119 else
7120 {
7121 struct ppc_dyn_relocs *p;
7122 struct ppc_dyn_relocs **pp;
7123 void *vpp;
7124 bool is_ifunc;
7125
7126 if (local_syms == NULL)
7127 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7128 if (sym_sec == NULL)
7129 sym_sec = sec;
7130
7131 vpp = &elf_section_data (sym_sec)->local_dynrel;
7132 pp = (struct ppc_dyn_relocs **) vpp;
7133
7134 if (*pp == NULL && info->gc_sections)
7135 return true;
7136
7137 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7138 while ((p = *pp) != NULL)
7139 {
7140 if (p->sec == sec && p->ifunc == is_ifunc)
7141 {
7142 p->count -= 1;
7143 if (p->count == 0)
7144 *pp = p->next;
7145 return true;
7146 }
7147 pp = &p->next;
7148 }
7149 }
7150
7151 /* xgettext:c-format */
7152 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7153 sec->owner, sec);
7154 bfd_set_error (bfd_error_bad_value);
7155 return false;
7156 }
7157
7158 /* Remove unused Official Procedure Descriptor entries. Currently we
7159 only remove those associated with functions in discarded link-once
7160 sections, or weakly defined functions that have been overridden. It
7161 would be possible to remove many more entries for statically linked
7162 applications. */
7163
7164 bool
7165 ppc64_elf_edit_opd (struct bfd_link_info *info)
7166 {
7167 bfd *ibfd;
7168 bool some_edited = false;
7169 asection *need_pad = NULL;
7170 struct ppc_link_hash_table *htab;
7171
7172 htab = ppc_hash_table (info);
7173 if (htab == NULL)
7174 return false;
7175
7176 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7177 {
7178 asection *sec;
7179 Elf_Internal_Rela *relstart, *rel, *relend;
7180 Elf_Internal_Shdr *symtab_hdr;
7181 Elf_Internal_Sym *local_syms;
7182 struct _opd_sec_data *opd;
7183 bool need_edit, add_aux_fields, broken;
7184 bfd_size_type cnt_16b = 0;
7185
7186 if (!is_ppc64_elf (ibfd))
7187 continue;
7188
7189 sec = bfd_get_section_by_name (ibfd, ".opd");
7190 if (sec == NULL || sec->size == 0)
7191 continue;
7192
7193 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7194 continue;
7195
7196 if (sec->output_section == bfd_abs_section_ptr)
7197 continue;
7198
7199 /* Look through the section relocs. */
7200 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7201 continue;
7202
7203 local_syms = NULL;
7204 symtab_hdr = &elf_symtab_hdr (ibfd);
7205
7206 /* Read the relocations. */
7207 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7208 info->keep_memory);
7209 if (relstart == NULL)
7210 return false;
7211
7212 /* First run through the relocs to check they are sane, and to
7213 determine whether we need to edit this opd section. */
7214 need_edit = false;
7215 broken = false;
7216 need_pad = sec;
7217 relend = relstart + sec->reloc_count;
7218 for (rel = relstart; rel < relend; )
7219 {
7220 enum elf_ppc64_reloc_type r_type;
7221 unsigned long r_symndx;
7222 asection *sym_sec;
7223 struct elf_link_hash_entry *h;
7224 Elf_Internal_Sym *sym;
7225 bfd_vma offset;
7226
7227 /* .opd contains an array of 16 or 24 byte entries. We're
7228 only interested in the reloc pointing to a function entry
7229 point. */
7230 offset = rel->r_offset;
7231 if (rel + 1 == relend
7232 || rel[1].r_offset != offset + 8)
7233 {
7234 /* If someone messes with .opd alignment then after a
7235 "ld -r" we might have padding in the middle of .opd.
7236 Also, there's nothing to prevent someone putting
7237 something silly in .opd with the assembler. No .opd
7238 optimization for them! */
7239 broken_opd:
7240 _bfd_error_handler
7241 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7242 broken = true;
7243 break;
7244 }
7245
7246 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7247 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7248 {
7249 _bfd_error_handler
7250 /* xgettext:c-format */
7251 (_("%pB: unexpected reloc type %u in .opd section"),
7252 ibfd, r_type);
7253 broken = true;
7254 break;
7255 }
7256
7257 r_symndx = ELF64_R_SYM (rel->r_info);
7258 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7259 r_symndx, ibfd))
7260 goto error_ret;
7261
7262 if (sym_sec == NULL || sym_sec->owner == NULL)
7263 {
7264 const char *sym_name;
7265 if (h != NULL)
7266 sym_name = h->root.root.string;
7267 else
7268 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7269 sym_sec);
7270
7271 _bfd_error_handler
7272 /* xgettext:c-format */
7273 (_("%pB: undefined sym `%s' in .opd section"),
7274 ibfd, sym_name);
7275 broken = true;
7276 break;
7277 }
7278
7279 /* opd entries are always for functions defined in the
7280 current input bfd. If the symbol isn't defined in the
7281 input bfd, then we won't be using the function in this
7282 bfd; It must be defined in a linkonce section in another
7283 bfd, or is weak. It's also possible that we are
7284 discarding the function due to a linker script /DISCARD/,
7285 which we test for via the output_section. */
7286 if (sym_sec->owner != ibfd
7287 || sym_sec->output_section == bfd_abs_section_ptr)
7288 need_edit = true;
7289
7290 rel += 2;
7291 if (rel + 1 == relend
7292 || (rel + 2 < relend
7293 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7294 ++rel;
7295
7296 if (rel == relend)
7297 {
7298 if (sec->size == offset + 24)
7299 {
7300 need_pad = NULL;
7301 break;
7302 }
7303 if (sec->size == offset + 16)
7304 {
7305 cnt_16b++;
7306 break;
7307 }
7308 goto broken_opd;
7309 }
7310 else if (rel + 1 < relend
7311 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7312 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7313 {
7314 if (rel[0].r_offset == offset + 16)
7315 cnt_16b++;
7316 else if (rel[0].r_offset != offset + 24)
7317 goto broken_opd;
7318 }
7319 else
7320 goto broken_opd;
7321 }
7322
7323 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7324
7325 if (!broken && (need_edit || add_aux_fields))
7326 {
7327 Elf_Internal_Rela *write_rel;
7328 Elf_Internal_Shdr *rel_hdr;
7329 bfd_byte *rptr, *wptr;
7330 bfd_byte *new_contents;
7331 bfd_size_type amt;
7332
7333 new_contents = NULL;
7334 amt = OPD_NDX (sec->size) * sizeof (long);
7335 opd = &ppc64_elf_section_data (sec)->u.opd;
7336 opd->adjust = bfd_zalloc (sec->owner, amt);
7337 if (opd->adjust == NULL)
7338 return false;
7339
7340 /* This seems a waste of time as input .opd sections are all
7341 zeros as generated by gcc, but I suppose there's no reason
7342 this will always be so. We might start putting something in
7343 the third word of .opd entries. */
7344 if ((sec->flags & SEC_IN_MEMORY) == 0)
7345 {
7346 bfd_byte *loc;
7347 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7348 {
7349 free (loc);
7350 error_ret:
7351 if (symtab_hdr->contents != (unsigned char *) local_syms)
7352 free (local_syms);
7353 if (elf_section_data (sec)->relocs != relstart)
7354 free (relstart);
7355 return false;
7356 }
7357 sec->contents = loc;
7358 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7359 }
7360
7361 elf_section_data (sec)->relocs = relstart;
7362
7363 new_contents = sec->contents;
7364 if (add_aux_fields)
7365 {
7366 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7367 if (new_contents == NULL)
7368 return false;
7369 need_pad = NULL;
7370 }
7371 wptr = new_contents;
7372 rptr = sec->contents;
7373 write_rel = relstart;
7374 for (rel = relstart; rel < relend; )
7375 {
7376 unsigned long r_symndx;
7377 asection *sym_sec;
7378 struct elf_link_hash_entry *h;
7379 struct ppc_link_hash_entry *fdh = NULL;
7380 Elf_Internal_Sym *sym;
7381 long opd_ent_size;
7382 Elf_Internal_Rela *next_rel;
7383 bool skip;
7384
7385 r_symndx = ELF64_R_SYM (rel->r_info);
7386 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7387 r_symndx, ibfd))
7388 goto error_ret;
7389
7390 next_rel = rel + 2;
7391 if (next_rel + 1 == relend
7392 || (next_rel + 2 < relend
7393 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7394 ++next_rel;
7395
7396 /* See if the .opd entry is full 24 byte or
7397 16 byte (with fd_aux entry overlapped with next
7398 fd_func). */
7399 opd_ent_size = 24;
7400 if (next_rel == relend)
7401 {
7402 if (sec->size == rel->r_offset + 16)
7403 opd_ent_size = 16;
7404 }
7405 else if (next_rel->r_offset == rel->r_offset + 16)
7406 opd_ent_size = 16;
7407
7408 if (h != NULL
7409 && h->root.root.string[0] == '.')
7410 {
7411 fdh = ppc_elf_hash_entry (h)->oh;
7412 if (fdh != NULL)
7413 {
7414 fdh = ppc_follow_link (fdh);
7415 if (fdh->elf.root.type != bfd_link_hash_defined
7416 && fdh->elf.root.type != bfd_link_hash_defweak)
7417 fdh = NULL;
7418 }
7419 }
7420
7421 skip = (sym_sec->owner != ibfd
7422 || sym_sec->output_section == bfd_abs_section_ptr);
7423 if (skip)
7424 {
7425 if (fdh != NULL && sym_sec->owner == ibfd)
7426 {
7427 /* Arrange for the function descriptor sym
7428 to be dropped. */
7429 fdh->elf.root.u.def.value = 0;
7430 fdh->elf.root.u.def.section = sym_sec;
7431 }
7432 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7433
7434 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7435 rel = next_rel;
7436 else
7437 while (1)
7438 {
7439 if (!dec_dynrel_count (rel->r_info, sec, info,
7440 NULL, h, sym))
7441 goto error_ret;
7442
7443 if (++rel == next_rel)
7444 break;
7445
7446 r_symndx = ELF64_R_SYM (rel->r_info);
7447 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7448 r_symndx, ibfd))
7449 goto error_ret;
7450 }
7451 }
7452 else
7453 {
7454 /* We'll be keeping this opd entry. */
7455 long adjust;
7456
7457 if (fdh != NULL)
7458 {
7459 /* Redefine the function descriptor symbol to
7460 this location in the opd section. It is
7461 necessary to update the value here rather
7462 than using an array of adjustments as we do
7463 for local symbols, because various places
7464 in the generic ELF code use the value
7465 stored in u.def.value. */
7466 fdh->elf.root.u.def.value = wptr - new_contents;
7467 fdh->adjust_done = 1;
7468 }
7469
7470 /* Local syms are a bit tricky. We could
7471 tweak them as they can be cached, but
7472 we'd need to look through the local syms
7473 for the function descriptor sym which we
7474 don't have at the moment. So keep an
7475 array of adjustments. */
7476 adjust = (wptr - new_contents) - (rptr - sec->contents);
7477 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7478
7479 if (wptr != rptr)
7480 memcpy (wptr, rptr, opd_ent_size);
7481 wptr += opd_ent_size;
7482 if (add_aux_fields && opd_ent_size == 16)
7483 {
7484 memset (wptr, '\0', 8);
7485 wptr += 8;
7486 }
7487
7488 /* We need to adjust any reloc offsets to point to the
7489 new opd entries. */
7490 for ( ; rel != next_rel; ++rel)
7491 {
7492 rel->r_offset += adjust;
7493 if (write_rel != rel)
7494 memcpy (write_rel, rel, sizeof (*rel));
7495 ++write_rel;
7496 }
7497 }
7498
7499 rptr += opd_ent_size;
7500 }
7501
7502 sec->size = wptr - new_contents;
7503 sec->reloc_count = write_rel - relstart;
7504 if (add_aux_fields)
7505 {
7506 free (sec->contents);
7507 sec->contents = new_contents;
7508 }
7509
7510 /* Fudge the header size too, as this is used later in
7511 elf_bfd_final_link if we are emitting relocs. */
7512 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7513 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7514 some_edited = true;
7515 }
7516 else if (elf_section_data (sec)->relocs != relstart)
7517 free (relstart);
7518
7519 if (local_syms != NULL
7520 && symtab_hdr->contents != (unsigned char *) local_syms)
7521 {
7522 if (!info->keep_memory)
7523 free (local_syms);
7524 else
7525 symtab_hdr->contents = (unsigned char *) local_syms;
7526 }
7527 }
7528
7529 if (some_edited)
7530 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7531
7532 /* If we are doing a final link and the last .opd entry is just 16 byte
7533 long, add a 8 byte padding after it. */
7534 if (need_pad != NULL && !bfd_link_relocatable (info))
7535 {
7536 bfd_byte *p;
7537
7538 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7539 {
7540 BFD_ASSERT (need_pad->size > 0);
7541
7542 p = bfd_malloc (need_pad->size + 8);
7543 if (p == NULL)
7544 return false;
7545
7546 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7547 p, 0, need_pad->size))
7548 return false;
7549
7550 need_pad->contents = p;
7551 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7552 }
7553 else
7554 {
7555 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7556 if (p == NULL)
7557 return false;
7558
7559 need_pad->contents = p;
7560 }
7561
7562 memset (need_pad->contents + need_pad->size, 0, 8);
7563 need_pad->size += 8;
7564 }
7565
7566 return true;
7567 }
7568
7569 /* Analyze inline PLT call relocations to see whether calls to locally
7570 defined functions can be converted to direct calls. */
7571
7572 bool
7573 ppc64_elf_inline_plt (struct bfd_link_info *info)
7574 {
7575 struct ppc_link_hash_table *htab;
7576 bfd *ibfd;
7577 asection *sec;
7578 bfd_vma low_vma, high_vma, limit;
7579
7580 htab = ppc_hash_table (info);
7581 if (htab == NULL)
7582 return false;
7583
7584 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7585 reduced somewhat to cater for possible stubs that might be added
7586 between the call and its destination. */
7587 if (htab->params->group_size < 0)
7588 {
7589 limit = -htab->params->group_size;
7590 if (limit == 1)
7591 limit = 0x1e00000;
7592 }
7593 else
7594 {
7595 limit = htab->params->group_size;
7596 if (limit == 1)
7597 limit = 0x1c00000;
7598 }
7599
7600 low_vma = -1;
7601 high_vma = 0;
7602 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7603 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7604 {
7605 if (low_vma > sec->vma)
7606 low_vma = sec->vma;
7607 if (high_vma < sec->vma + sec->size)
7608 high_vma = sec->vma + sec->size;
7609 }
7610
7611 /* If a "bl" can reach anywhere in local code sections, then we can
7612 convert all inline PLT sequences to direct calls when the symbol
7613 is local. */
7614 if (high_vma - low_vma < limit)
7615 {
7616 htab->can_convert_all_inline_plt = 1;
7617 return true;
7618 }
7619
7620 /* Otherwise, go looking through relocs for cases where a direct
7621 call won't reach. Mark the symbol on any such reloc to disable
7622 the optimization and keep the PLT entry as it seems likely that
7623 this will be better than creating trampolines. Note that this
7624 will disable the optimization for all inline PLT calls to a
7625 particular symbol, not just those that won't reach. The
7626 difficulty in doing a more precise optimization is that the
7627 linker needs to make a decision depending on whether a
7628 particular R_PPC64_PLTCALL insn can be turned into a direct
7629 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7630 the sequence, and there is nothing that ties those relocs
7631 together except their symbol. */
7632
7633 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7634 {
7635 Elf_Internal_Shdr *symtab_hdr;
7636 Elf_Internal_Sym *local_syms;
7637
7638 if (!is_ppc64_elf (ibfd))
7639 continue;
7640
7641 local_syms = NULL;
7642 symtab_hdr = &elf_symtab_hdr (ibfd);
7643
7644 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7645 if (ppc64_elf_section_data (sec)->has_pltcall
7646 && !bfd_is_abs_section (sec->output_section))
7647 {
7648 Elf_Internal_Rela *relstart, *rel, *relend;
7649
7650 /* Read the relocations. */
7651 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7652 info->keep_memory);
7653 if (relstart == NULL)
7654 return false;
7655
7656 relend = relstart + sec->reloc_count;
7657 for (rel = relstart; rel < relend; rel++)
7658 {
7659 enum elf_ppc64_reloc_type r_type;
7660 unsigned long r_symndx;
7661 asection *sym_sec;
7662 struct elf_link_hash_entry *h;
7663 Elf_Internal_Sym *sym;
7664 unsigned char *tls_maskp;
7665
7666 r_type = ELF64_R_TYPE (rel->r_info);
7667 if (r_type != R_PPC64_PLTCALL
7668 && r_type != R_PPC64_PLTCALL_NOTOC)
7669 continue;
7670
7671 r_symndx = ELF64_R_SYM (rel->r_info);
7672 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7673 r_symndx, ibfd))
7674 {
7675 if (elf_section_data (sec)->relocs != relstart)
7676 free (relstart);
7677 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7678 free (local_syms);
7679 return false;
7680 }
7681
7682 if (sym_sec != NULL && sym_sec->output_section != NULL)
7683 {
7684 bfd_vma from, to;
7685 if (h != NULL)
7686 to = h->root.u.def.value;
7687 else
7688 to = sym->st_value;
7689 to += (rel->r_addend
7690 + sym_sec->output_offset
7691 + sym_sec->output_section->vma);
7692 from = (rel->r_offset
7693 + sec->output_offset
7694 + sec->output_section->vma);
7695 if (to - from + limit < 2 * limit
7696 && !(r_type == R_PPC64_PLTCALL_NOTOC
7697 && (((h ? h->other : sym->st_other)
7698 & STO_PPC64_LOCAL_MASK)
7699 > 1 << STO_PPC64_LOCAL_BIT)))
7700 *tls_maskp &= ~PLT_KEEP;
7701 }
7702 }
7703 if (elf_section_data (sec)->relocs != relstart)
7704 free (relstart);
7705 }
7706
7707 if (local_syms != NULL
7708 && symtab_hdr->contents != (unsigned char *) local_syms)
7709 {
7710 if (!info->keep_memory)
7711 free (local_syms);
7712 else
7713 symtab_hdr->contents = (unsigned char *) local_syms;
7714 }
7715 }
7716
7717 return true;
7718 }
7719
7720 /* Set htab->tls_get_addr and various other info specific to TLS.
7721 This needs to run before dynamic symbols are processed in
7722 bfd_elf_size_dynamic_sections. */
7723
7724 bool
7725 ppc64_elf_tls_setup (struct bfd_link_info *info)
7726 {
7727 struct ppc_link_hash_table *htab;
7728 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7729
7730 htab = ppc_hash_table (info);
7731 if (htab == NULL)
7732 return false;
7733
7734 if (abiversion (info->output_bfd) == 1)
7735 htab->opd_abi = 1;
7736
7737 if (htab->params->no_multi_toc)
7738 htab->do_multi_toc = 0;
7739 else if (!htab->do_multi_toc)
7740 htab->params->no_multi_toc = 1;
7741
7742 /* Default to --no-plt-localentry, as this option can cause problems
7743 with symbol interposition. For example, glibc libpthread.so and
7744 libc.so duplicate many pthread symbols, with a fallback
7745 implementation in libc.so. In some cases the fallback does more
7746 work than the pthread implementation. __pthread_condattr_destroy
7747 is one such symbol: the libpthread.so implementation is
7748 localentry:0 while the libc.so implementation is localentry:8.
7749 An app that "cleverly" uses dlopen to only load necessary
7750 libraries at runtime may omit loading libpthread.so when not
7751 running multi-threaded, which then results in the libc.so
7752 fallback symbols being used and ld.so complaining. Now there
7753 are workarounds in ld (see non_zero_localentry) to detect the
7754 pthread situation, but that may not be the only case where
7755 --plt-localentry can cause trouble. */
7756 if (htab->params->plt_localentry0 < 0)
7757 htab->params->plt_localentry0 = 0;
7758 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7759 {
7760 /* The issue is that __glink_PLTresolve saves r2, which is done
7761 because glibc ld.so _dl_runtime_resolve restores r2 to support
7762 a glibc plt call optimisation where global entry code is
7763 skipped on calls that resolve to the same binary. The
7764 __glink_PLTresolve save of r2 is incompatible with code
7765 making tail calls, because the tail call might go via the
7766 resolver and thus overwrite the proper saved r2. */
7767 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7768 "power10 pc-relative code"));
7769 htab->params->plt_localentry0 = 0;
7770 }
7771 if (htab->params->plt_localentry0
7772 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7773 false, false, false) == NULL)
7774 _bfd_error_handler
7775 (_("warning: --plt-localentry is especially dangerous without "
7776 "ld.so support to detect ABI violations"));
7777
7778 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7779 false, false, true);
7780 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7781
7782 /* Move dynamic linking info to the function descriptor sym. */
7783 if (tga != NULL)
7784 func_desc_adjust (tga, info);
7785 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7786 false, false, true);
7787 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7788
7789 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7790 false, false, true);
7791 htab->tga_desc = ppc_elf_hash_entry (desc);
7792 if (desc != NULL)
7793 func_desc_adjust (desc, info);
7794 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7795 false, false, true);
7796 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7797
7798 if (htab->params->tls_get_addr_opt)
7799 {
7800 struct elf_link_hash_entry *opt, *opt_fd;
7801
7802 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7803 false, false, true);
7804 if (opt != NULL)
7805 func_desc_adjust (opt, info);
7806 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7807 false, false, true);
7808 if (opt_fd != NULL
7809 && (opt_fd->root.type == bfd_link_hash_defined
7810 || opt_fd->root.type == bfd_link_hash_defweak))
7811 {
7812 /* If glibc supports an optimized __tls_get_addr call stub,
7813 signalled by the presence of __tls_get_addr_opt, and we'll
7814 be calling __tls_get_addr via a plt call stub, then
7815 make __tls_get_addr point to __tls_get_addr_opt. */
7816 if (!(htab->elf.dynamic_sections_created
7817 && tga_fd != NULL
7818 && (tga_fd->type == STT_FUNC
7819 || tga_fd->needs_plt)
7820 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7821 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7822 tga_fd = NULL;
7823 if (!(htab->elf.dynamic_sections_created
7824 && desc_fd != NULL
7825 && (desc_fd->type == STT_FUNC
7826 || desc_fd->needs_plt)
7827 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7828 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7829 desc_fd = NULL;
7830
7831 if (tga_fd != NULL || desc_fd != NULL)
7832 {
7833 struct plt_entry *ent = NULL;
7834
7835 if (tga_fd != NULL)
7836 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7837 if (ent->plt.refcount > 0)
7838 break;
7839 if (ent == NULL && desc_fd != NULL)
7840 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7841 if (ent->plt.refcount > 0)
7842 break;
7843 if (ent != NULL)
7844 {
7845 if (tga_fd != NULL)
7846 {
7847 tga_fd->root.type = bfd_link_hash_indirect;
7848 tga_fd->root.u.i.link = &opt_fd->root;
7849 tga_fd->root.u.i.warning = NULL;
7850 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7851 }
7852 if (desc_fd != NULL)
7853 {
7854 desc_fd->root.type = bfd_link_hash_indirect;
7855 desc_fd->root.u.i.link = &opt_fd->root;
7856 desc_fd->root.u.i.warning = NULL;
7857 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7858 }
7859 opt_fd->mark = 1;
7860 if (opt_fd->dynindx != -1)
7861 {
7862 /* Use __tls_get_addr_opt in dynamic relocations. */
7863 opt_fd->dynindx = -1;
7864 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7865 opt_fd->dynstr_index);
7866 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7867 return false;
7868 }
7869 if (tga_fd != NULL)
7870 {
7871 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
7872 tga = elf_hash_entry (htab->tls_get_addr);
7873 if (opt != NULL && tga != NULL)
7874 {
7875 tga->root.type = bfd_link_hash_indirect;
7876 tga->root.u.i.link = &opt->root;
7877 tga->root.u.i.warning = NULL;
7878 ppc64_elf_copy_indirect_symbol (info, opt, tga);
7879 opt->mark = 1;
7880 _bfd_elf_link_hash_hide_symbol (info, opt,
7881 tga->forced_local);
7882 htab->tls_get_addr = ppc_elf_hash_entry (opt);
7883 }
7884 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
7885 htab->tls_get_addr_fd->is_func_descriptor = 1;
7886 if (htab->tls_get_addr != NULL)
7887 {
7888 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
7889 htab->tls_get_addr->is_func = 1;
7890 }
7891 }
7892 if (desc_fd != NULL)
7893 {
7894 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
7895 if (opt != NULL && desc != NULL)
7896 {
7897 desc->root.type = bfd_link_hash_indirect;
7898 desc->root.u.i.link = &opt->root;
7899 desc->root.u.i.warning = NULL;
7900 ppc64_elf_copy_indirect_symbol (info, opt, desc);
7901 opt->mark = 1;
7902 _bfd_elf_link_hash_hide_symbol (info, opt,
7903 desc->forced_local);
7904 htab->tga_desc = ppc_elf_hash_entry (opt);
7905 }
7906 htab->tga_desc_fd->oh = htab->tga_desc;
7907 htab->tga_desc_fd->is_func_descriptor = 1;
7908 if (htab->tga_desc != NULL)
7909 {
7910 htab->tga_desc->oh = htab->tga_desc_fd;
7911 htab->tga_desc->is_func = 1;
7912 }
7913 }
7914 }
7915 }
7916 }
7917 else if (htab->params->tls_get_addr_opt < 0)
7918 htab->params->tls_get_addr_opt = 0;
7919 }
7920
7921 if (htab->tga_desc_fd != NULL
7922 && htab->params->tls_get_addr_opt
7923 && htab->params->no_tls_get_addr_regsave == -1)
7924 htab->params->no_tls_get_addr_regsave = 0;
7925
7926 return true;
7927 }
7928
7929 /* Return TRUE iff REL is a branch reloc with a global symbol matching
7930 any of HASH1, HASH2, HASH3, or HASH4. */
7931
7932 static bool
7933 branch_reloc_hash_match (bfd *ibfd,
7934 Elf_Internal_Rela *rel,
7935 struct ppc_link_hash_entry *hash1,
7936 struct ppc_link_hash_entry *hash2,
7937 struct ppc_link_hash_entry *hash3,
7938 struct ppc_link_hash_entry *hash4)
7939 {
7940 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
7941 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
7942 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
7943
7944 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
7945 {
7946 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
7947 struct elf_link_hash_entry *h;
7948
7949 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7950 h = elf_follow_link (h);
7951 if (h == elf_hash_entry (hash1)
7952 || h == elf_hash_entry (hash2)
7953 || h == elf_hash_entry (hash3)
7954 || h == elf_hash_entry (hash4))
7955 return true;
7956 }
7957 return false;
7958 }
7959
7960 /* Run through all the TLS relocs looking for optimization
7961 opportunities. The linker has been hacked (see ppc64elf.em) to do
7962 a preliminary section layout so that we know the TLS segment
7963 offsets. We can't optimize earlier because some optimizations need
7964 to know the tp offset, and we need to optimize before allocating
7965 dynamic relocations. */
7966
7967 bool
7968 ppc64_elf_tls_optimize (struct bfd_link_info *info)
7969 {
7970 bfd *ibfd;
7971 asection *sec;
7972 struct ppc_link_hash_table *htab;
7973 unsigned char *toc_ref;
7974 int pass;
7975
7976 if (!bfd_link_executable (info))
7977 return true;
7978
7979 htab = ppc_hash_table (info);
7980 if (htab == NULL)
7981 return false;
7982
7983 htab->do_tls_opt = 1;
7984
7985 /* Make two passes over the relocs. On the first pass, mark toc
7986 entries involved with tls relocs, and check that tls relocs
7987 involved in setting up a tls_get_addr call are indeed followed by
7988 such a call. If they are not, we can't do any tls optimization.
7989 On the second pass twiddle tls_mask flags to notify
7990 relocate_section that optimization can be done, and adjust got
7991 and plt refcounts. */
7992 toc_ref = NULL;
7993 for (pass = 0; pass < 2; ++pass)
7994 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7995 {
7996 Elf_Internal_Sym *locsyms = NULL;
7997 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
7998
7999 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8000 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8001 {
8002 Elf_Internal_Rela *relstart, *rel, *relend;
8003 bool found_tls_get_addr_arg = 0;
8004
8005 /* Read the relocations. */
8006 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8007 info->keep_memory);
8008 if (relstart == NULL)
8009 {
8010 free (toc_ref);
8011 return false;
8012 }
8013
8014 relend = relstart + sec->reloc_count;
8015 for (rel = relstart; rel < relend; rel++)
8016 {
8017 enum elf_ppc64_reloc_type r_type;
8018 unsigned long r_symndx;
8019 struct elf_link_hash_entry *h;
8020 Elf_Internal_Sym *sym;
8021 asection *sym_sec;
8022 unsigned char *tls_mask;
8023 unsigned int tls_set, tls_clear, tls_type = 0;
8024 bfd_vma value;
8025 bool ok_tprel, is_local;
8026 long toc_ref_index = 0;
8027 int expecting_tls_get_addr = 0;
8028 bool ret = false;
8029
8030 r_symndx = ELF64_R_SYM (rel->r_info);
8031 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8032 r_symndx, ibfd))
8033 {
8034 err_free_rel:
8035 if (elf_section_data (sec)->relocs != relstart)
8036 free (relstart);
8037 free (toc_ref);
8038 if (elf_symtab_hdr (ibfd).contents
8039 != (unsigned char *) locsyms)
8040 free (locsyms);
8041 return ret;
8042 }
8043
8044 if (h != NULL)
8045 {
8046 if (h->root.type == bfd_link_hash_defined
8047 || h->root.type == bfd_link_hash_defweak)
8048 value = h->root.u.def.value;
8049 else if (h->root.type == bfd_link_hash_undefweak)
8050 value = 0;
8051 else
8052 {
8053 found_tls_get_addr_arg = 0;
8054 continue;
8055 }
8056 }
8057 else
8058 /* Symbols referenced by TLS relocs must be of type
8059 STT_TLS. So no need for .opd local sym adjust. */
8060 value = sym->st_value;
8061
8062 ok_tprel = false;
8063 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8064 if (is_local)
8065 {
8066 if (h != NULL
8067 && h->root.type == bfd_link_hash_undefweak)
8068 ok_tprel = true;
8069 else if (sym_sec != NULL
8070 && sym_sec->output_section != NULL)
8071 {
8072 value += sym_sec->output_offset;
8073 value += sym_sec->output_section->vma;
8074 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8075 /* Note that even though the prefix insns
8076 allow a 1<<33 offset we use the same test
8077 as for addis;addi. There may be a mix of
8078 pcrel and non-pcrel code and the decision
8079 to optimise is per symbol, not per TLS
8080 sequence. */
8081 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8082 }
8083 }
8084
8085 r_type = ELF64_R_TYPE (rel->r_info);
8086 /* If this section has old-style __tls_get_addr calls
8087 without marker relocs, then check that each
8088 __tls_get_addr call reloc is preceded by a reloc
8089 that conceivably belongs to the __tls_get_addr arg
8090 setup insn. If we don't find matching arg setup
8091 relocs, don't do any tls optimization. */
8092 if (pass == 0
8093 && sec->nomark_tls_get_addr
8094 && h != NULL
8095 && is_tls_get_addr (h, htab)
8096 && !found_tls_get_addr_arg
8097 && is_branch_reloc (r_type))
8098 {
8099 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8100 "TLS optimization disabled\n"),
8101 ibfd, sec, rel->r_offset);
8102 ret = true;
8103 goto err_free_rel;
8104 }
8105
8106 found_tls_get_addr_arg = 0;
8107 switch (r_type)
8108 {
8109 case R_PPC64_GOT_TLSLD16:
8110 case R_PPC64_GOT_TLSLD16_LO:
8111 case R_PPC64_GOT_TLSLD_PCREL34:
8112 expecting_tls_get_addr = 1;
8113 found_tls_get_addr_arg = 1;
8114 /* Fall through. */
8115
8116 case R_PPC64_GOT_TLSLD16_HI:
8117 case R_PPC64_GOT_TLSLD16_HA:
8118 /* These relocs should never be against a symbol
8119 defined in a shared lib. Leave them alone if
8120 that turns out to be the case. */
8121 if (!is_local)
8122 continue;
8123
8124 /* LD -> LE */
8125 tls_set = 0;
8126 tls_clear = TLS_LD;
8127 tls_type = TLS_TLS | TLS_LD;
8128 break;
8129
8130 case R_PPC64_GOT_TLSGD16:
8131 case R_PPC64_GOT_TLSGD16_LO:
8132 case R_PPC64_GOT_TLSGD_PCREL34:
8133 expecting_tls_get_addr = 1;
8134 found_tls_get_addr_arg = 1;
8135 /* Fall through. */
8136
8137 case R_PPC64_GOT_TLSGD16_HI:
8138 case R_PPC64_GOT_TLSGD16_HA:
8139 if (ok_tprel)
8140 /* GD -> LE */
8141 tls_set = 0;
8142 else
8143 /* GD -> IE */
8144 tls_set = TLS_TLS | TLS_GDIE;
8145 tls_clear = TLS_GD;
8146 tls_type = TLS_TLS | TLS_GD;
8147 break;
8148
8149 case R_PPC64_GOT_TPREL_PCREL34:
8150 case R_PPC64_GOT_TPREL16_DS:
8151 case R_PPC64_GOT_TPREL16_LO_DS:
8152 case R_PPC64_GOT_TPREL16_HI:
8153 case R_PPC64_GOT_TPREL16_HA:
8154 if (ok_tprel)
8155 {
8156 /* IE -> LE */
8157 tls_set = 0;
8158 tls_clear = TLS_TPREL;
8159 tls_type = TLS_TLS | TLS_TPREL;
8160 break;
8161 }
8162 continue;
8163
8164 case R_PPC64_TLSLD:
8165 if (!is_local)
8166 continue;
8167 /* Fall through. */
8168 case R_PPC64_TLSGD:
8169 if (rel + 1 < relend
8170 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8171 {
8172 if (pass != 0
8173 && (ELF64_R_TYPE (rel[1].r_info)
8174 != R_PPC64_PLTSEQ)
8175 && (ELF64_R_TYPE (rel[1].r_info)
8176 != R_PPC64_PLTSEQ_NOTOC))
8177 {
8178 r_symndx = ELF64_R_SYM (rel[1].r_info);
8179 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8180 r_symndx, ibfd))
8181 goto err_free_rel;
8182 if (h != NULL)
8183 {
8184 struct plt_entry *ent = NULL;
8185
8186 for (ent = h->plt.plist;
8187 ent != NULL;
8188 ent = ent->next)
8189 if (ent->addend == rel[1].r_addend)
8190 break;
8191
8192 if (ent != NULL
8193 && ent->plt.refcount > 0)
8194 ent->plt.refcount -= 1;
8195 }
8196 }
8197 continue;
8198 }
8199 found_tls_get_addr_arg = 1;
8200 /* Fall through. */
8201
8202 case R_PPC64_TLS:
8203 case R_PPC64_TOC16:
8204 case R_PPC64_TOC16_LO:
8205 if (sym_sec == NULL || sym_sec != toc)
8206 continue;
8207
8208 /* Mark this toc entry as referenced by a TLS
8209 code sequence. We can do that now in the
8210 case of R_PPC64_TLS, and after checking for
8211 tls_get_addr for the TOC16 relocs. */
8212 if (toc_ref == NULL)
8213 toc_ref
8214 = bfd_zmalloc (toc->output_section->rawsize / 8);
8215 if (toc_ref == NULL)
8216 goto err_free_rel;
8217
8218 if (h != NULL)
8219 value = h->root.u.def.value;
8220 else
8221 value = sym->st_value;
8222 value += rel->r_addend;
8223 if (value % 8 != 0)
8224 continue;
8225 BFD_ASSERT (value < toc->size
8226 && toc->output_offset % 8 == 0);
8227 toc_ref_index = (value + toc->output_offset) / 8;
8228 if (r_type == R_PPC64_TLS
8229 || r_type == R_PPC64_TLSGD
8230 || r_type == R_PPC64_TLSLD)
8231 {
8232 toc_ref[toc_ref_index] = 1;
8233 continue;
8234 }
8235
8236 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8237 continue;
8238
8239 tls_set = 0;
8240 tls_clear = 0;
8241 expecting_tls_get_addr = 2;
8242 break;
8243
8244 case R_PPC64_TPREL64:
8245 if (pass == 0
8246 || sec != toc
8247 || toc_ref == NULL
8248 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8249 continue;
8250 if (ok_tprel)
8251 {
8252 /* IE -> LE */
8253 tls_set = TLS_EXPLICIT;
8254 tls_clear = TLS_TPREL;
8255 break;
8256 }
8257 continue;
8258
8259 case R_PPC64_DTPMOD64:
8260 if (pass == 0
8261 || sec != toc
8262 || toc_ref == NULL
8263 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8264 continue;
8265 if (rel + 1 < relend
8266 && (rel[1].r_info
8267 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8268 && rel[1].r_offset == rel->r_offset + 8)
8269 {
8270 if (ok_tprel)
8271 /* GD -> LE */
8272 tls_set = TLS_EXPLICIT | TLS_GD;
8273 else
8274 /* GD -> IE */
8275 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8276 tls_clear = TLS_GD;
8277 }
8278 else
8279 {
8280 if (!is_local)
8281 continue;
8282
8283 /* LD -> LE */
8284 tls_set = TLS_EXPLICIT;
8285 tls_clear = TLS_LD;
8286 }
8287 break;
8288
8289 case R_PPC64_TPREL16_HA:
8290 if (pass == 0)
8291 {
8292 unsigned char buf[4];
8293 unsigned int insn;
8294 bfd_vma off = rel->r_offset & ~3;
8295 if (!bfd_get_section_contents (ibfd, sec, buf,
8296 off, 4))
8297 goto err_free_rel;
8298 insn = bfd_get_32 (ibfd, buf);
8299 /* addis rt,13,imm */
8300 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8301 != ((15u << 26) | (13 << 16)))
8302 {
8303 /* xgettext:c-format */
8304 info->callbacks->minfo
8305 (_("%H: warning: %s unexpected insn %#x.\n"),
8306 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8307 htab->do_tls_opt = 0;
8308 }
8309 }
8310 continue;
8311
8312 case R_PPC64_TPREL16_HI:
8313 case R_PPC64_TPREL16_HIGH:
8314 case R_PPC64_TPREL16_HIGHA:
8315 case R_PPC64_TPREL16_HIGHER:
8316 case R_PPC64_TPREL16_HIGHERA:
8317 case R_PPC64_TPREL16_HIGHEST:
8318 case R_PPC64_TPREL16_HIGHESTA:
8319 /* These can all be used in sequences along with
8320 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8321 able to verify easily. */
8322 htab->do_tls_opt = 0;
8323 continue;
8324
8325 default:
8326 continue;
8327 }
8328
8329 if (pass == 0)
8330 {
8331 if (!expecting_tls_get_addr
8332 || !sec->nomark_tls_get_addr)
8333 continue;
8334
8335 if (rel + 1 < relend
8336 && branch_reloc_hash_match (ibfd, rel + 1,
8337 htab->tls_get_addr_fd,
8338 htab->tga_desc_fd,
8339 htab->tls_get_addr,
8340 htab->tga_desc))
8341 {
8342 if (expecting_tls_get_addr == 2)
8343 {
8344 /* Check for toc tls entries. */
8345 unsigned char *toc_tls;
8346 int retval;
8347
8348 retval = get_tls_mask (&toc_tls, NULL, NULL,
8349 &locsyms,
8350 rel, ibfd);
8351 if (retval == 0)
8352 goto err_free_rel;
8353 if (toc_tls != NULL)
8354 {
8355 if ((*toc_tls & TLS_TLS) != 0
8356 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8357 found_tls_get_addr_arg = 1;
8358 if (retval > 1)
8359 toc_ref[toc_ref_index] = 1;
8360 }
8361 }
8362 continue;
8363 }
8364
8365 /* Uh oh, we didn't find the expected call. We
8366 could just mark this symbol to exclude it
8367 from tls optimization but it's safer to skip
8368 the entire optimization. */
8369 /* xgettext:c-format */
8370 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8371 "TLS optimization disabled\n"),
8372 ibfd, sec, rel->r_offset);
8373 ret = true;
8374 goto err_free_rel;
8375 }
8376
8377 /* If we don't have old-style __tls_get_addr calls
8378 without TLSGD/TLSLD marker relocs, and we haven't
8379 found a new-style __tls_get_addr call with a
8380 marker for this symbol, then we either have a
8381 broken object file or an -mlongcall style
8382 indirect call to __tls_get_addr without a marker.
8383 Disable optimization in this case. */
8384 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8385 && (tls_set & TLS_EXPLICIT) == 0
8386 && !sec->nomark_tls_get_addr
8387 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8388 != (TLS_TLS | TLS_MARK)))
8389 continue;
8390
8391 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8392 {
8393 struct plt_entry *ent = NULL;
8394
8395 if (htab->tls_get_addr_fd != NULL)
8396 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8397 ent != NULL;
8398 ent = ent->next)
8399 if (ent->addend == 0)
8400 break;
8401
8402 if (ent == NULL && htab->tga_desc_fd != NULL)
8403 for (ent = htab->tga_desc_fd->elf.plt.plist;
8404 ent != NULL;
8405 ent = ent->next)
8406 if (ent->addend == 0)
8407 break;
8408
8409 if (ent == NULL && htab->tls_get_addr != NULL)
8410 for (ent = htab->tls_get_addr->elf.plt.plist;
8411 ent != NULL;
8412 ent = ent->next)
8413 if (ent->addend == 0)
8414 break;
8415
8416 if (ent == NULL && htab->tga_desc != NULL)
8417 for (ent = htab->tga_desc->elf.plt.plist;
8418 ent != NULL;
8419 ent = ent->next)
8420 if (ent->addend == 0)
8421 break;
8422
8423 if (ent != NULL
8424 && ent->plt.refcount > 0)
8425 ent->plt.refcount -= 1;
8426 }
8427
8428 if (tls_clear == 0)
8429 continue;
8430
8431 if ((tls_set & TLS_EXPLICIT) == 0)
8432 {
8433 struct got_entry *ent;
8434
8435 /* Adjust got entry for this reloc. */
8436 if (h != NULL)
8437 ent = h->got.glist;
8438 else
8439 ent = elf_local_got_ents (ibfd)[r_symndx];
8440
8441 for (; ent != NULL; ent = ent->next)
8442 if (ent->addend == rel->r_addend
8443 && ent->owner == ibfd
8444 && ent->tls_type == tls_type)
8445 break;
8446 if (ent == NULL)
8447 abort ();
8448
8449 if (tls_set == 0)
8450 {
8451 /* We managed to get rid of a got entry. */
8452 if (ent->got.refcount > 0)
8453 ent->got.refcount -= 1;
8454 }
8455 }
8456 else
8457 {
8458 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8459 we'll lose one or two dyn relocs. */
8460 if (!dec_dynrel_count (rel->r_info, sec, info,
8461 NULL, h, sym))
8462 return false;
8463
8464 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8465 {
8466 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8467 NULL, h, sym))
8468 return false;
8469 }
8470 }
8471
8472 *tls_mask |= tls_set & 0xff;
8473 *tls_mask &= ~tls_clear;
8474 }
8475
8476 if (elf_section_data (sec)->relocs != relstart)
8477 free (relstart);
8478 }
8479
8480 if (locsyms != NULL
8481 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8482 {
8483 if (!info->keep_memory)
8484 free (locsyms);
8485 else
8486 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8487 }
8488 }
8489
8490 free (toc_ref);
8491 return true;
8492 }
8493
8494 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8495 the values of any global symbols in a toc section that has been
8496 edited. Globals in toc sections should be a rarity, so this function
8497 sets a flag if any are found in toc sections other than the one just
8498 edited, so that further hash table traversals can be avoided. */
8499
8500 struct adjust_toc_info
8501 {
8502 asection *toc;
8503 unsigned long *skip;
8504 bool global_toc_syms;
8505 };
8506
8507 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8508
8509 static bool
8510 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8511 {
8512 struct ppc_link_hash_entry *eh;
8513 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8514 unsigned long i;
8515
8516 if (h->root.type != bfd_link_hash_defined
8517 && h->root.type != bfd_link_hash_defweak)
8518 return true;
8519
8520 eh = ppc_elf_hash_entry (h);
8521 if (eh->adjust_done)
8522 return true;
8523
8524 if (eh->elf.root.u.def.section == toc_inf->toc)
8525 {
8526 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8527 i = toc_inf->toc->rawsize >> 3;
8528 else
8529 i = eh->elf.root.u.def.value >> 3;
8530
8531 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8532 {
8533 _bfd_error_handler
8534 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8535 do
8536 ++i;
8537 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8538 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8539 }
8540
8541 eh->elf.root.u.def.value -= toc_inf->skip[i];
8542 eh->adjust_done = 1;
8543 }
8544 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8545 toc_inf->global_toc_syms = true;
8546
8547 return true;
8548 }
8549
8550 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8551 on a _LO variety toc/got reloc. */
8552
8553 static bool
8554 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8555 {
8556 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8557 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8558 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8559 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8560 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8561 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8562 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8563 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8564 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8565 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8566 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8567 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8568 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8569 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8570 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8571 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8572 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8573 /* Exclude lfqu by testing reloc. If relocs are ever
8574 defined for the reduced D field in psq_lu then those
8575 will need testing too. */
8576 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8577 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8578 && (insn & 1) == 0)
8579 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8580 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8581 /* Exclude stfqu. psq_stu as above for psq_lu. */
8582 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8583 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8584 && (insn & 1) == 0));
8585 }
8586
8587 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8588 pld ra,symbol@got@pcrel
8589 load/store rt,off(ra)
8590 or
8591 pla ra,symbol@pcrel
8592 load/store rt,off(ra)
8593 may be translated to
8594 pload/pstore rt,symbol+off@pcrel
8595 nop.
8596 This function returns true if the optimization is possible, placing
8597 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8598
8599 On entry to this function, the linker has already determined that
8600 the pld can be replaced with pla: *PINSN1 is that pla insn,
8601 while *PINSN2 is the second instruction. */
8602
8603 static bool
8604 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8605 {
8606 uint64_t insn1 = *pinsn1;
8607 uint64_t insn2 = *pinsn2;
8608 bfd_signed_vma off;
8609
8610 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8611 {
8612 /* Check that regs match. */
8613 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8614 return false;
8615
8616 /* P8LS or PMLS form, non-pcrel. */
8617 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8618 return false;
8619
8620 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8621 *pinsn2 = PNOP;
8622 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8623 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8624 return true;
8625 }
8626
8627 insn2 >>= 32;
8628
8629 /* Check that regs match. */
8630 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8631 return false;
8632
8633 switch ((insn2 >> 26) & 63)
8634 {
8635 default:
8636 return false;
8637
8638 case 32: /* lwz */
8639 case 34: /* lbz */
8640 case 36: /* stw */
8641 case 38: /* stb */
8642 case 40: /* lhz */
8643 case 42: /* lha */
8644 case 44: /* sth */
8645 case 48: /* lfs */
8646 case 50: /* lfd */
8647 case 52: /* stfs */
8648 case 54: /* stfd */
8649 /* These are the PMLS cases, where we just need to tack a prefix
8650 on the insn. */
8651 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8652 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8653 off = insn2 & 0xffff;
8654 break;
8655
8656 case 58: /* lwa, ld */
8657 if ((insn2 & 1) != 0)
8658 return false;
8659 insn1 = ((1ULL << 58) | (1ULL << 52)
8660 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8661 | (insn2 & (31ULL << 21)));
8662 off = insn2 & 0xfffc;
8663 break;
8664
8665 case 57: /* lxsd, lxssp */
8666 if ((insn2 & 3) < 2)
8667 return false;
8668 insn1 = ((1ULL << 58) | (1ULL << 52)
8669 | ((40ULL | (insn2 & 3)) << 26)
8670 | (insn2 & (31ULL << 21)));
8671 off = insn2 & 0xfffc;
8672 break;
8673
8674 case 61: /* stxsd, stxssp, lxv, stxv */
8675 if ((insn2 & 3) == 0)
8676 return false;
8677 else if ((insn2 & 3) >= 2)
8678 {
8679 insn1 = ((1ULL << 58) | (1ULL << 52)
8680 | ((44ULL | (insn2 & 3)) << 26)
8681 | (insn2 & (31ULL << 21)));
8682 off = insn2 & 0xfffc;
8683 }
8684 else
8685 {
8686 insn1 = ((1ULL << 58) | (1ULL << 52)
8687 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8688 | (insn2 & (31ULL << 21)));
8689 off = insn2 & 0xfff0;
8690 }
8691 break;
8692
8693 case 56: /* lq */
8694 insn1 = ((1ULL << 58) | (1ULL << 52)
8695 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8696 off = insn2 & 0xffff;
8697 break;
8698
8699 case 6: /* lxvp, stxvp */
8700 if ((insn2 & 0xe) != 0)
8701 return false;
8702 insn1 = ((1ULL << 58) | (1ULL << 52)
8703 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8704 | (insn2 & (31ULL << 21)));
8705 off = insn2 & 0xfff0;
8706 break;
8707
8708 case 62: /* std, stq */
8709 if ((insn2 & 1) != 0)
8710 return false;
8711 insn1 = ((1ULL << 58) | (1ULL << 52)
8712 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8713 | (insn2 & (31ULL << 21)));
8714 off = insn2 & 0xfffc;
8715 break;
8716 }
8717
8718 *pinsn1 = insn1;
8719 *pinsn2 = (uint64_t) NOP << 32;
8720 *poff = (off ^ 0x8000) - 0x8000;
8721 return true;
8722 }
8723
8724 /* Examine all relocs referencing .toc sections in order to remove
8725 unused .toc entries. */
8726
8727 bool
8728 ppc64_elf_edit_toc (struct bfd_link_info *info)
8729 {
8730 bfd *ibfd;
8731 struct adjust_toc_info toc_inf;
8732 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8733
8734 htab->do_toc_opt = 1;
8735 toc_inf.global_toc_syms = true;
8736 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8737 {
8738 asection *toc, *sec;
8739 Elf_Internal_Shdr *symtab_hdr;
8740 Elf_Internal_Sym *local_syms;
8741 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8742 unsigned long *skip, *drop;
8743 unsigned char *used;
8744 unsigned char *keep, last, some_unused;
8745
8746 if (!is_ppc64_elf (ibfd))
8747 continue;
8748
8749 toc = bfd_get_section_by_name (ibfd, ".toc");
8750 if (toc == NULL
8751 || toc->size == 0
8752 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8753 || discarded_section (toc))
8754 continue;
8755
8756 toc_relocs = NULL;
8757 local_syms = NULL;
8758 symtab_hdr = &elf_symtab_hdr (ibfd);
8759
8760 /* Look at sections dropped from the final link. */
8761 skip = NULL;
8762 relstart = NULL;
8763 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8764 {
8765 if (sec->reloc_count == 0
8766 || !discarded_section (sec)
8767 || get_opd_info (sec)
8768 || (sec->flags & SEC_ALLOC) == 0
8769 || (sec->flags & SEC_DEBUGGING) != 0)
8770 continue;
8771
8772 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8773 if (relstart == NULL)
8774 goto error_ret;
8775
8776 /* Run through the relocs to see which toc entries might be
8777 unused. */
8778 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8779 {
8780 enum elf_ppc64_reloc_type r_type;
8781 unsigned long r_symndx;
8782 asection *sym_sec;
8783 struct elf_link_hash_entry *h;
8784 Elf_Internal_Sym *sym;
8785 bfd_vma val;
8786
8787 r_type = ELF64_R_TYPE (rel->r_info);
8788 switch (r_type)
8789 {
8790 default:
8791 continue;
8792
8793 case R_PPC64_TOC16:
8794 case R_PPC64_TOC16_LO:
8795 case R_PPC64_TOC16_HI:
8796 case R_PPC64_TOC16_HA:
8797 case R_PPC64_TOC16_DS:
8798 case R_PPC64_TOC16_LO_DS:
8799 break;
8800 }
8801
8802 r_symndx = ELF64_R_SYM (rel->r_info);
8803 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8804 r_symndx, ibfd))
8805 goto error_ret;
8806
8807 if (sym_sec != toc)
8808 continue;
8809
8810 if (h != NULL)
8811 val = h->root.u.def.value;
8812 else
8813 val = sym->st_value;
8814 val += rel->r_addend;
8815
8816 if (val >= toc->size)
8817 continue;
8818
8819 /* Anything in the toc ought to be aligned to 8 bytes.
8820 If not, don't mark as unused. */
8821 if (val & 7)
8822 continue;
8823
8824 if (skip == NULL)
8825 {
8826 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8827 if (skip == NULL)
8828 goto error_ret;
8829 }
8830
8831 skip[val >> 3] = ref_from_discarded;
8832 }
8833
8834 if (elf_section_data (sec)->relocs != relstart)
8835 free (relstart);
8836 }
8837
8838 /* For largetoc loads of address constants, we can convert
8839 . addis rx,2,addr@got@ha
8840 . ld ry,addr@got@l(rx)
8841 to
8842 . addis rx,2,addr@toc@ha
8843 . addi ry,rx,addr@toc@l
8844 when addr is within 2G of the toc pointer. This then means
8845 that the word storing "addr" in the toc is no longer needed. */
8846
8847 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8848 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8849 && toc->reloc_count != 0)
8850 {
8851 /* Read toc relocs. */
8852 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8853 info->keep_memory);
8854 if (toc_relocs == NULL)
8855 goto error_ret;
8856
8857 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8858 {
8859 enum elf_ppc64_reloc_type r_type;
8860 unsigned long r_symndx;
8861 asection *sym_sec;
8862 struct elf_link_hash_entry *h;
8863 Elf_Internal_Sym *sym;
8864 bfd_vma val, addr;
8865
8866 r_type = ELF64_R_TYPE (rel->r_info);
8867 if (r_type != R_PPC64_ADDR64)
8868 continue;
8869
8870 r_symndx = ELF64_R_SYM (rel->r_info);
8871 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8872 r_symndx, ibfd))
8873 goto error_ret;
8874
8875 if (sym_sec == NULL
8876 || sym_sec->output_section == NULL
8877 || discarded_section (sym_sec))
8878 continue;
8879
8880 if (!SYMBOL_REFERENCES_LOCAL (info, h))
8881 continue;
8882
8883 if (h != NULL)
8884 {
8885 if (h->type == STT_GNU_IFUNC)
8886 continue;
8887 val = h->root.u.def.value;
8888 }
8889 else
8890 {
8891 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
8892 continue;
8893 val = sym->st_value;
8894 }
8895 val += rel->r_addend;
8896 val += sym_sec->output_section->vma + sym_sec->output_offset;
8897
8898 /* We don't yet know the exact toc pointer value, but we
8899 know it will be somewhere in the toc section. Don't
8900 optimize if the difference from any possible toc
8901 pointer is outside [ff..f80008000, 7fff7fff]. */
8902 addr = toc->output_section->vma + TOC_BASE_OFF;
8903 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8904 continue;
8905
8906 addr = toc->output_section->vma + toc->output_section->rawsize;
8907 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
8908 continue;
8909
8910 if (skip == NULL)
8911 {
8912 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8913 if (skip == NULL)
8914 goto error_ret;
8915 }
8916
8917 skip[rel->r_offset >> 3]
8918 |= can_optimize | ((rel - toc_relocs) << 2);
8919 }
8920 }
8921
8922 if (skip == NULL)
8923 continue;
8924
8925 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
8926 if (used == NULL)
8927 {
8928 error_ret:
8929 if (symtab_hdr->contents != (unsigned char *) local_syms)
8930 free (local_syms);
8931 if (sec != NULL
8932 && elf_section_data (sec)->relocs != relstart)
8933 free (relstart);
8934 if (elf_section_data (toc)->relocs != toc_relocs)
8935 free (toc_relocs);
8936 free (skip);
8937 return false;
8938 }
8939
8940 /* Now check all kept sections that might reference the toc.
8941 Check the toc itself last. */
8942 for (sec = (ibfd->sections == toc && toc->next ? toc->next
8943 : ibfd->sections);
8944 sec != NULL;
8945 sec = (sec == toc ? NULL
8946 : sec->next == NULL ? toc
8947 : sec->next == toc && toc->next ? toc->next
8948 : sec->next))
8949 {
8950 int repeat;
8951
8952 if (sec->reloc_count == 0
8953 || discarded_section (sec)
8954 || get_opd_info (sec)
8955 || (sec->flags & SEC_ALLOC) == 0
8956 || (sec->flags & SEC_DEBUGGING) != 0)
8957 continue;
8958
8959 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8960 info->keep_memory);
8961 if (relstart == NULL)
8962 {
8963 free (used);
8964 goto error_ret;
8965 }
8966
8967 /* Mark toc entries referenced as used. */
8968 do
8969 {
8970 repeat = 0;
8971 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8972 {
8973 enum elf_ppc64_reloc_type r_type;
8974 unsigned long r_symndx;
8975 asection *sym_sec;
8976 struct elf_link_hash_entry *h;
8977 Elf_Internal_Sym *sym;
8978 bfd_vma val;
8979
8980 r_type = ELF64_R_TYPE (rel->r_info);
8981 switch (r_type)
8982 {
8983 case R_PPC64_TOC16:
8984 case R_PPC64_TOC16_LO:
8985 case R_PPC64_TOC16_HI:
8986 case R_PPC64_TOC16_HA:
8987 case R_PPC64_TOC16_DS:
8988 case R_PPC64_TOC16_LO_DS:
8989 /* In case we're taking addresses of toc entries. */
8990 case R_PPC64_ADDR64:
8991 break;
8992
8993 default:
8994 continue;
8995 }
8996
8997 r_symndx = ELF64_R_SYM (rel->r_info);
8998 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8999 r_symndx, ibfd))
9000 {
9001 free (used);
9002 goto error_ret;
9003 }
9004
9005 if (sym_sec != toc)
9006 continue;
9007
9008 if (h != NULL)
9009 val = h->root.u.def.value;
9010 else
9011 val = sym->st_value;
9012 val += rel->r_addend;
9013
9014 if (val >= toc->size)
9015 continue;
9016
9017 if ((skip[val >> 3] & can_optimize) != 0)
9018 {
9019 bfd_vma off;
9020 unsigned char opc;
9021
9022 switch (r_type)
9023 {
9024 case R_PPC64_TOC16_HA:
9025 break;
9026
9027 case R_PPC64_TOC16_LO_DS:
9028 off = rel->r_offset;
9029 off += (bfd_big_endian (ibfd) ? -2 : 3);
9030 if (!bfd_get_section_contents (ibfd, sec, &opc,
9031 off, 1))
9032 {
9033 free (used);
9034 goto error_ret;
9035 }
9036 if ((opc & (0x3f << 2)) == (58u << 2))
9037 break;
9038 /* Fall through. */
9039
9040 default:
9041 /* Wrong sort of reloc, or not a ld. We may
9042 as well clear ref_from_discarded too. */
9043 skip[val >> 3] = 0;
9044 }
9045 }
9046
9047 if (sec != toc)
9048 used[val >> 3] = 1;
9049 /* For the toc section, we only mark as used if this
9050 entry itself isn't unused. */
9051 else if ((used[rel->r_offset >> 3]
9052 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9053 && !used[val >> 3])
9054 {
9055 /* Do all the relocs again, to catch reference
9056 chains. */
9057 repeat = 1;
9058 used[val >> 3] = 1;
9059 }
9060 }
9061 }
9062 while (repeat);
9063
9064 if (elf_section_data (sec)->relocs != relstart)
9065 free (relstart);
9066 }
9067
9068 /* Merge the used and skip arrays. Assume that TOC
9069 doublewords not appearing as either used or unused belong
9070 to an entry more than one doubleword in size. */
9071 for (drop = skip, keep = used, last = 0, some_unused = 0;
9072 drop < skip + (toc->size + 7) / 8;
9073 ++drop, ++keep)
9074 {
9075 if (*keep)
9076 {
9077 *drop &= ~ref_from_discarded;
9078 if ((*drop & can_optimize) != 0)
9079 some_unused = 1;
9080 last = 0;
9081 }
9082 else if ((*drop & ref_from_discarded) != 0)
9083 {
9084 some_unused = 1;
9085 last = ref_from_discarded;
9086 }
9087 else
9088 *drop = last;
9089 }
9090
9091 free (used);
9092
9093 if (some_unused)
9094 {
9095 bfd_byte *contents, *src;
9096 unsigned long off;
9097 Elf_Internal_Sym *sym;
9098 bool local_toc_syms = false;
9099
9100 /* Shuffle the toc contents, and at the same time convert the
9101 skip array from booleans into offsets. */
9102 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9103 goto error_ret;
9104
9105 elf_section_data (toc)->this_hdr.contents = contents;
9106
9107 for (src = contents, off = 0, drop = skip;
9108 src < contents + toc->size;
9109 src += 8, ++drop)
9110 {
9111 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9112 off += 8;
9113 else if (off != 0)
9114 {
9115 *drop = off;
9116 memcpy (src - off, src, 8);
9117 }
9118 }
9119 *drop = off;
9120 toc->rawsize = toc->size;
9121 toc->size = src - contents - off;
9122
9123 /* Adjust addends for relocs against the toc section sym,
9124 and optimize any accesses we can. */
9125 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9126 {
9127 if (sec->reloc_count == 0
9128 || discarded_section (sec))
9129 continue;
9130
9131 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9132 info->keep_memory);
9133 if (relstart == NULL)
9134 goto error_ret;
9135
9136 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9137 {
9138 enum elf_ppc64_reloc_type r_type;
9139 unsigned long r_symndx;
9140 asection *sym_sec;
9141 struct elf_link_hash_entry *h;
9142 bfd_vma val;
9143
9144 r_type = ELF64_R_TYPE (rel->r_info);
9145 switch (r_type)
9146 {
9147 default:
9148 continue;
9149
9150 case R_PPC64_TOC16:
9151 case R_PPC64_TOC16_LO:
9152 case R_PPC64_TOC16_HI:
9153 case R_PPC64_TOC16_HA:
9154 case R_PPC64_TOC16_DS:
9155 case R_PPC64_TOC16_LO_DS:
9156 case R_PPC64_ADDR64:
9157 break;
9158 }
9159
9160 r_symndx = ELF64_R_SYM (rel->r_info);
9161 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9162 r_symndx, ibfd))
9163 goto error_ret;
9164
9165 if (sym_sec != toc)
9166 continue;
9167
9168 if (h != NULL)
9169 val = h->root.u.def.value;
9170 else
9171 {
9172 val = sym->st_value;
9173 if (val != 0)
9174 local_toc_syms = true;
9175 }
9176
9177 val += rel->r_addend;
9178
9179 if (val > toc->rawsize)
9180 val = toc->rawsize;
9181 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9182 continue;
9183 else if ((skip[val >> 3] & can_optimize) != 0)
9184 {
9185 Elf_Internal_Rela *tocrel
9186 = toc_relocs + (skip[val >> 3] >> 2);
9187 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9188
9189 switch (r_type)
9190 {
9191 case R_PPC64_TOC16_HA:
9192 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9193 break;
9194
9195 case R_PPC64_TOC16_LO_DS:
9196 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9197 break;
9198
9199 default:
9200 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9201 ppc_howto_init ();
9202 info->callbacks->einfo
9203 /* xgettext:c-format */
9204 (_("%H: %s references "
9205 "optimized away TOC entry\n"),
9206 ibfd, sec, rel->r_offset,
9207 ppc64_elf_howto_table[r_type]->name);
9208 bfd_set_error (bfd_error_bad_value);
9209 goto error_ret;
9210 }
9211 rel->r_addend = tocrel->r_addend;
9212 elf_section_data (sec)->relocs = relstart;
9213 continue;
9214 }
9215
9216 if (h != NULL || sym->st_value != 0)
9217 continue;
9218
9219 rel->r_addend -= skip[val >> 3];
9220 elf_section_data (sec)->relocs = relstart;
9221 }
9222
9223 if (elf_section_data (sec)->relocs != relstart)
9224 free (relstart);
9225 }
9226
9227 /* We shouldn't have local or global symbols defined in the TOC,
9228 but handle them anyway. */
9229 if (local_syms != NULL)
9230 for (sym = local_syms;
9231 sym < local_syms + symtab_hdr->sh_info;
9232 ++sym)
9233 if (sym->st_value != 0
9234 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9235 {
9236 unsigned long i;
9237
9238 if (sym->st_value > toc->rawsize)
9239 i = toc->rawsize >> 3;
9240 else
9241 i = sym->st_value >> 3;
9242
9243 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9244 {
9245 if (local_toc_syms)
9246 _bfd_error_handler
9247 (_("%s defined on removed toc entry"),
9248 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9249 do
9250 ++i;
9251 while ((skip[i] & (ref_from_discarded | can_optimize)));
9252 sym->st_value = (bfd_vma) i << 3;
9253 }
9254
9255 sym->st_value -= skip[i];
9256 symtab_hdr->contents = (unsigned char *) local_syms;
9257 }
9258
9259 /* Adjust any global syms defined in this toc input section. */
9260 if (toc_inf.global_toc_syms)
9261 {
9262 toc_inf.toc = toc;
9263 toc_inf.skip = skip;
9264 toc_inf.global_toc_syms = false;
9265 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9266 &toc_inf);
9267 }
9268
9269 if (toc->reloc_count != 0)
9270 {
9271 Elf_Internal_Shdr *rel_hdr;
9272 Elf_Internal_Rela *wrel;
9273 bfd_size_type sz;
9274
9275 /* Remove unused toc relocs, and adjust those we keep. */
9276 if (toc_relocs == NULL)
9277 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9278 info->keep_memory);
9279 if (toc_relocs == NULL)
9280 goto error_ret;
9281
9282 wrel = toc_relocs;
9283 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9284 if ((skip[rel->r_offset >> 3]
9285 & (ref_from_discarded | can_optimize)) == 0)
9286 {
9287 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9288 wrel->r_info = rel->r_info;
9289 wrel->r_addend = rel->r_addend;
9290 ++wrel;
9291 }
9292 else if (!dec_dynrel_count (rel->r_info, toc, info,
9293 &local_syms, NULL, NULL))
9294 goto error_ret;
9295
9296 elf_section_data (toc)->relocs = toc_relocs;
9297 toc->reloc_count = wrel - toc_relocs;
9298 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9299 sz = rel_hdr->sh_entsize;
9300 rel_hdr->sh_size = toc->reloc_count * sz;
9301 }
9302 }
9303 else if (elf_section_data (toc)->relocs != toc_relocs)
9304 free (toc_relocs);
9305
9306 if (local_syms != NULL
9307 && symtab_hdr->contents != (unsigned char *) local_syms)
9308 {
9309 if (!info->keep_memory)
9310 free (local_syms);
9311 else
9312 symtab_hdr->contents = (unsigned char *) local_syms;
9313 }
9314 free (skip);
9315 }
9316
9317 /* Look for cases where we can change an indirect GOT access to
9318 a GOT relative or PC relative access, possibly reducing the
9319 number of GOT entries. */
9320 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9321 {
9322 asection *sec;
9323 Elf_Internal_Shdr *symtab_hdr;
9324 Elf_Internal_Sym *local_syms;
9325 Elf_Internal_Rela *relstart, *rel;
9326 bfd_vma got;
9327
9328 if (!is_ppc64_elf (ibfd))
9329 continue;
9330
9331 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9332 continue;
9333
9334 sec = ppc64_elf_tdata (ibfd)->got;
9335 got = 0;
9336 if (sec != NULL)
9337 got = sec->output_section->vma + sec->output_offset + 0x8000;
9338
9339 local_syms = NULL;
9340 symtab_hdr = &elf_symtab_hdr (ibfd);
9341
9342 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9343 {
9344 if (sec->reloc_count == 0
9345 || !ppc64_elf_section_data (sec)->has_optrel
9346 || discarded_section (sec))
9347 continue;
9348
9349 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9350 info->keep_memory);
9351 if (relstart == NULL)
9352 {
9353 got_error_ret:
9354 if (symtab_hdr->contents != (unsigned char *) local_syms)
9355 free (local_syms);
9356 if (sec != NULL
9357 && elf_section_data (sec)->relocs != relstart)
9358 free (relstart);
9359 return false;
9360 }
9361
9362 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9363 {
9364 enum elf_ppc64_reloc_type r_type;
9365 unsigned long r_symndx;
9366 Elf_Internal_Sym *sym;
9367 asection *sym_sec;
9368 struct elf_link_hash_entry *h;
9369 struct got_entry *ent;
9370 bfd_vma val, pc;
9371 unsigned char buf[8];
9372 unsigned int insn;
9373 enum {no_check, check_lo, check_ha} insn_check;
9374
9375 r_type = ELF64_R_TYPE (rel->r_info);
9376 switch (r_type)
9377 {
9378 default:
9379 insn_check = no_check;
9380 break;
9381
9382 case R_PPC64_PLT16_HA:
9383 case R_PPC64_GOT_TLSLD16_HA:
9384 case R_PPC64_GOT_TLSGD16_HA:
9385 case R_PPC64_GOT_TPREL16_HA:
9386 case R_PPC64_GOT_DTPREL16_HA:
9387 case R_PPC64_GOT16_HA:
9388 case R_PPC64_TOC16_HA:
9389 insn_check = check_ha;
9390 break;
9391
9392 case R_PPC64_PLT16_LO:
9393 case R_PPC64_PLT16_LO_DS:
9394 case R_PPC64_GOT_TLSLD16_LO:
9395 case R_PPC64_GOT_TLSGD16_LO:
9396 case R_PPC64_GOT_TPREL16_LO_DS:
9397 case R_PPC64_GOT_DTPREL16_LO_DS:
9398 case R_PPC64_GOT16_LO:
9399 case R_PPC64_GOT16_LO_DS:
9400 case R_PPC64_TOC16_LO:
9401 case R_PPC64_TOC16_LO_DS:
9402 insn_check = check_lo;
9403 break;
9404 }
9405
9406 if (insn_check != no_check)
9407 {
9408 bfd_vma off = rel->r_offset & ~3;
9409
9410 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9411 goto got_error_ret;
9412
9413 insn = bfd_get_32 (ibfd, buf);
9414 if (insn_check == check_lo
9415 ? !ok_lo_toc_insn (insn, r_type)
9416 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9417 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9418 {
9419 char str[12];
9420
9421 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9422 sprintf (str, "%#08x", insn);
9423 info->callbacks->einfo
9424 /* xgettext:c-format */
9425 (_("%H: got/toc optimization is not supported for"
9426 " %s instruction\n"),
9427 ibfd, sec, rel->r_offset & ~3, str);
9428 continue;
9429 }
9430 }
9431
9432 switch (r_type)
9433 {
9434 /* Note that we don't delete GOT entries for
9435 R_PPC64_GOT16_DS since we'd need a lot more
9436 analysis. For starters, the preliminary layout is
9437 before the GOT, PLT, dynamic sections and stubs are
9438 laid out. Then we'd need to allow for changes in
9439 distance between sections caused by alignment. */
9440 default:
9441 continue;
9442
9443 case R_PPC64_GOT16_HA:
9444 case R_PPC64_GOT16_LO_DS:
9445 case R_PPC64_GOT_PCREL34:
9446 break;
9447 }
9448
9449 r_symndx = ELF64_R_SYM (rel->r_info);
9450 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9451 r_symndx, ibfd))
9452 goto got_error_ret;
9453
9454 if (sym_sec == NULL
9455 || sym_sec->output_section == NULL
9456 || discarded_section (sym_sec))
9457 continue;
9458
9459 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9460 continue;
9461
9462 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9463 continue;
9464
9465 if (h != NULL)
9466 val = h->root.u.def.value;
9467 else
9468 val = sym->st_value;
9469 val += rel->r_addend;
9470 val += sym_sec->output_section->vma + sym_sec->output_offset;
9471
9472 /* Fudge factor to allow for the fact that the preliminary layout
9473 isn't exact. Reduce limits by this factor. */
9474 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9475
9476 switch (r_type)
9477 {
9478 default:
9479 continue;
9480
9481 case R_PPC64_GOT16_HA:
9482 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9483 >= LIMIT_ADJUST (0x100000000ULL))
9484 continue;
9485
9486 if (!bfd_get_section_contents (ibfd, sec, buf,
9487 rel->r_offset & ~3, 4))
9488 goto got_error_ret;
9489 insn = bfd_get_32 (ibfd, buf);
9490 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9491 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9492 continue;
9493 break;
9494
9495 case R_PPC64_GOT16_LO_DS:
9496 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9497 >= LIMIT_ADJUST (0x100000000ULL))
9498 continue;
9499 if (!bfd_get_section_contents (ibfd, sec, buf,
9500 rel->r_offset & ~3, 4))
9501 goto got_error_ret;
9502 insn = bfd_get_32 (ibfd, buf);
9503 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9504 continue;
9505 break;
9506
9507 case R_PPC64_GOT_PCREL34:
9508 pc = rel->r_offset;
9509 pc += sec->output_section->vma + sec->output_offset;
9510 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9511 >= LIMIT_ADJUST (1ULL << 34))
9512 continue;
9513 if (!bfd_get_section_contents (ibfd, sec, buf,
9514 rel->r_offset & ~3, 8))
9515 goto got_error_ret;
9516 insn = bfd_get_32 (ibfd, buf);
9517 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9518 continue;
9519 insn = bfd_get_32 (ibfd, buf + 4);
9520 if ((insn & (0x3fu << 26)) != 57u << 26)
9521 continue;
9522 break;
9523 }
9524 #undef LIMIT_ADJUST
9525
9526 if (h != NULL)
9527 ent = h->got.glist;
9528 else
9529 {
9530 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9531 ent = local_got_ents[r_symndx];
9532 }
9533 for (; ent != NULL; ent = ent->next)
9534 if (ent->addend == rel->r_addend
9535 && ent->owner == ibfd
9536 && ent->tls_type == 0)
9537 break;
9538 BFD_ASSERT (ent && ent->got.refcount > 0);
9539 ent->got.refcount -= 1;
9540 }
9541
9542 if (elf_section_data (sec)->relocs != relstart)
9543 free (relstart);
9544 }
9545
9546 if (local_syms != NULL
9547 && symtab_hdr->contents != (unsigned char *) local_syms)
9548 {
9549 if (!info->keep_memory)
9550 free (local_syms);
9551 else
9552 symtab_hdr->contents = (unsigned char *) local_syms;
9553 }
9554 }
9555
9556 return true;
9557 }
9558
9559 /* Return true iff input section I references the TOC using
9560 instructions limited to +/-32k offsets. */
9561
9562 bool
9563 ppc64_elf_has_small_toc_reloc (asection *i)
9564 {
9565 return (is_ppc64_elf (i->owner)
9566 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9567 }
9568
9569 /* Allocate space for one GOT entry. */
9570
9571 static void
9572 allocate_got (struct elf_link_hash_entry *h,
9573 struct bfd_link_info *info,
9574 struct got_entry *gent)
9575 {
9576 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9577 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9578 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9579 ? 16 : 8);
9580 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9581 ? 2 : 1) * sizeof (Elf64_External_Rela);
9582 asection *got = ppc64_elf_tdata (gent->owner)->got;
9583
9584 gent->got.offset = got->size;
9585 got->size += entsize;
9586
9587 if (h->type == STT_GNU_IFUNC)
9588 {
9589 htab->elf.irelplt->size += rentsize;
9590 htab->got_reli_size += rentsize;
9591 }
9592 else if (((bfd_link_pic (info)
9593 && !(gent->tls_type != 0
9594 && bfd_link_executable (info)
9595 && SYMBOL_REFERENCES_LOCAL (info, h)))
9596 || (htab->elf.dynamic_sections_created
9597 && h->dynindx != -1
9598 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9599 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9600 {
9601 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9602 relgot->size += rentsize;
9603 }
9604 }
9605
9606 /* This function merges got entries in the same toc group. */
9607
9608 static void
9609 merge_got_entries (struct got_entry **pent)
9610 {
9611 struct got_entry *ent, *ent2;
9612
9613 for (ent = *pent; ent != NULL; ent = ent->next)
9614 if (!ent->is_indirect)
9615 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9616 if (!ent2->is_indirect
9617 && ent2->addend == ent->addend
9618 && ent2->tls_type == ent->tls_type
9619 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9620 {
9621 ent2->is_indirect = true;
9622 ent2->got.ent = ent;
9623 }
9624 }
9625
9626 /* If H is undefined, make it dynamic if that makes sense. */
9627
9628 static bool
9629 ensure_undef_dynamic (struct bfd_link_info *info,
9630 struct elf_link_hash_entry *h)
9631 {
9632 struct elf_link_hash_table *htab = elf_hash_table (info);
9633
9634 if (htab->dynamic_sections_created
9635 && ((info->dynamic_undefined_weak != 0
9636 && h->root.type == bfd_link_hash_undefweak)
9637 || h->root.type == bfd_link_hash_undefined)
9638 && h->dynindx == -1
9639 && !h->forced_local
9640 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9641 return bfd_elf_link_record_dynamic_symbol (info, h);
9642 return true;
9643 }
9644
9645 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9646 usual htab->elf.splt section for a PLT entry. */
9647
9648 static inline
9649 bool use_local_plt (struct bfd_link_info *info,
9650 struct elf_link_hash_entry *h)
9651 {
9652 return (h == NULL
9653 || h->dynindx == -1
9654 || !elf_hash_table (info)->dynamic_sections_created);
9655 }
9656
9657 /* Allocate space in .plt, .got and associated reloc sections for
9658 dynamic relocs. */
9659
9660 static bool
9661 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9662 {
9663 struct bfd_link_info *info;
9664 struct ppc_link_hash_table *htab;
9665 asection *s;
9666 struct ppc_link_hash_entry *eh;
9667 struct got_entry **pgent, *gent;
9668
9669 if (h->root.type == bfd_link_hash_indirect)
9670 return true;
9671
9672 info = (struct bfd_link_info *) inf;
9673 htab = ppc_hash_table (info);
9674 if (htab == NULL)
9675 return false;
9676
9677 eh = ppc_elf_hash_entry (h);
9678 /* Run through the TLS GD got entries first if we're changing them
9679 to TPREL. */
9680 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9681 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9682 if (gent->got.refcount > 0
9683 && (gent->tls_type & TLS_GD) != 0)
9684 {
9685 /* This was a GD entry that has been converted to TPREL. If
9686 there happens to be a TPREL entry we can use that one. */
9687 struct got_entry *ent;
9688 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9689 if (ent->got.refcount > 0
9690 && (ent->tls_type & TLS_TPREL) != 0
9691 && ent->addend == gent->addend
9692 && ent->owner == gent->owner)
9693 {
9694 gent->got.refcount = 0;
9695 break;
9696 }
9697
9698 /* If not, then we'll be using our own TPREL entry. */
9699 if (gent->got.refcount != 0)
9700 gent->tls_type = TLS_TLS | TLS_TPREL;
9701 }
9702
9703 /* Remove any list entry that won't generate a word in the GOT before
9704 we call merge_got_entries. Otherwise we risk merging to empty
9705 entries. */
9706 pgent = &h->got.glist;
9707 while ((gent = *pgent) != NULL)
9708 if (gent->got.refcount > 0)
9709 {
9710 if ((gent->tls_type & TLS_LD) != 0
9711 && SYMBOL_REFERENCES_LOCAL (info, h))
9712 {
9713 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9714 *pgent = gent->next;
9715 }
9716 else
9717 pgent = &gent->next;
9718 }
9719 else
9720 *pgent = gent->next;
9721
9722 if (!htab->do_multi_toc)
9723 merge_got_entries (&h->got.glist);
9724
9725 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9726 if (!gent->is_indirect)
9727 {
9728 /* Ensure we catch all the cases where this symbol should
9729 be made dynamic. */
9730 if (!ensure_undef_dynamic (info, h))
9731 return false;
9732
9733 if (!is_ppc64_elf (gent->owner))
9734 abort ();
9735
9736 allocate_got (h, info, gent);
9737 }
9738
9739 /* If no dynamic sections we can't have dynamic relocs, except for
9740 IFUNCs which are handled even in static executables. */
9741 if (!htab->elf.dynamic_sections_created
9742 && h->type != STT_GNU_IFUNC)
9743 h->dyn_relocs = NULL;
9744
9745 /* Discard relocs on undefined symbols that must be local. */
9746 else if (h->root.type == bfd_link_hash_undefined
9747 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9748 h->dyn_relocs = NULL;
9749
9750 /* Also discard relocs on undefined weak syms with non-default
9751 visibility, or when dynamic_undefined_weak says so. */
9752 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9753 h->dyn_relocs = NULL;
9754
9755 if (h->dyn_relocs != NULL)
9756 {
9757 struct elf_dyn_relocs *p, **pp;
9758
9759 /* In the shared -Bsymbolic case, discard space allocated for
9760 dynamic pc-relative relocs against symbols which turn out to
9761 be defined in regular objects. For the normal shared case,
9762 discard space for relocs that have become local due to symbol
9763 visibility changes. */
9764 if (bfd_link_pic (info))
9765 {
9766 /* Relocs that use pc_count are those that appear on a call
9767 insn, or certain REL relocs (see must_be_dyn_reloc) that
9768 can be generated via assembly. We want calls to
9769 protected symbols to resolve directly to the function
9770 rather than going via the plt. If people want function
9771 pointer comparisons to work as expected then they should
9772 avoid writing weird assembly. */
9773 if (SYMBOL_CALLS_LOCAL (info, h))
9774 {
9775 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
9776 {
9777 p->count -= p->pc_count;
9778 p->pc_count = 0;
9779 if (p->count == 0)
9780 *pp = p->next;
9781 else
9782 pp = &p->next;
9783 }
9784 }
9785
9786 if (h->dyn_relocs != NULL)
9787 {
9788 /* Ensure we catch all the cases where this symbol
9789 should be made dynamic. */
9790 if (!ensure_undef_dynamic (info, h))
9791 return false;
9792 }
9793 }
9794
9795 /* For a fixed position executable, discard space for
9796 relocs against symbols which are not dynamic. */
9797 else if (h->type != STT_GNU_IFUNC)
9798 {
9799 if (h->dynamic_adjusted
9800 && !h->def_regular
9801 && !ELF_COMMON_DEF_P (h))
9802 {
9803 /* Ensure we catch all the cases where this symbol
9804 should be made dynamic. */
9805 if (!ensure_undef_dynamic (info, h))
9806 return false;
9807
9808 /* But if that didn't work out, discard dynamic relocs. */
9809 if (h->dynindx == -1)
9810 h->dyn_relocs = NULL;
9811 }
9812 else
9813 h->dyn_relocs = NULL;
9814 }
9815
9816 /* Finally, allocate space. */
9817 for (p = h->dyn_relocs; p != NULL; p = p->next)
9818 {
9819 asection *sreloc = elf_section_data (p->sec)->sreloc;
9820 if (eh->elf.type == STT_GNU_IFUNC)
9821 sreloc = htab->elf.irelplt;
9822 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9823 }
9824 }
9825
9826 /* We might need a PLT entry when the symbol
9827 a) is dynamic, or
9828 b) is an ifunc, or
9829 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9830 d) has plt16 relocs and we are linking statically. */
9831 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9832 || h->type == STT_GNU_IFUNC
9833 || (h->needs_plt && h->dynamic_adjusted)
9834 || (h->needs_plt
9835 && h->def_regular
9836 && !htab->elf.dynamic_sections_created
9837 && !htab->can_convert_all_inline_plt
9838 && (ppc_elf_hash_entry (h)->tls_mask
9839 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9840 {
9841 struct plt_entry *pent;
9842 bool doneone = false;
9843 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9844 if (pent->plt.refcount > 0)
9845 {
9846 if (use_local_plt (info, h))
9847 {
9848 if (h->type == STT_GNU_IFUNC)
9849 {
9850 s = htab->elf.iplt;
9851 pent->plt.offset = s->size;
9852 s->size += PLT_ENTRY_SIZE (htab);
9853 s = htab->elf.irelplt;
9854 }
9855 else
9856 {
9857 s = htab->pltlocal;
9858 pent->plt.offset = s->size;
9859 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9860 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9861 }
9862 }
9863 else
9864 {
9865 /* If this is the first .plt entry, make room for the special
9866 first entry. */
9867 s = htab->elf.splt;
9868 if (s->size == 0)
9869 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
9870
9871 pent->plt.offset = s->size;
9872
9873 /* Make room for this entry. */
9874 s->size += PLT_ENTRY_SIZE (htab);
9875
9876 /* Make room for the .glink code. */
9877 s = htab->glink;
9878 if (s->size == 0)
9879 s->size += GLINK_PLTRESOLVE_SIZE (htab);
9880 if (htab->opd_abi)
9881 {
9882 /* We need bigger stubs past index 32767. */
9883 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
9884 s->size += 4;
9885 s->size += 2*4;
9886 }
9887 else
9888 s->size += 4;
9889
9890 /* We also need to make an entry in the .rela.plt section. */
9891 s = htab->elf.srelplt;
9892 }
9893 if (s != NULL)
9894 s->size += sizeof (Elf64_External_Rela);
9895 doneone = true;
9896 }
9897 else
9898 pent->plt.offset = (bfd_vma) -1;
9899 if (!doneone)
9900 {
9901 h->plt.plist = NULL;
9902 h->needs_plt = 0;
9903 }
9904 }
9905 else
9906 {
9907 h->plt.plist = NULL;
9908 h->needs_plt = 0;
9909 }
9910
9911 return true;
9912 }
9913
9914 #define PPC_LO(v) ((v) & 0xffff)
9915 #define PPC_HI(v) (((v) >> 16) & 0xffff)
9916 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
9917 #define D34(v) \
9918 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
9919 #define HA34(v) ((v + (1ULL << 33)) >> 34)
9920
9921 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
9922 to set up space for global entry stubs. These are put in glink,
9923 after the branch table. */
9924
9925 static bool
9926 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
9927 {
9928 struct bfd_link_info *info;
9929 struct ppc_link_hash_table *htab;
9930 struct plt_entry *pent;
9931 asection *s, *plt;
9932
9933 if (h->root.type == bfd_link_hash_indirect)
9934 return true;
9935
9936 if (!h->pointer_equality_needed)
9937 return true;
9938
9939 if (h->def_regular)
9940 return true;
9941
9942 info = inf;
9943 htab = ppc_hash_table (info);
9944 if (htab == NULL)
9945 return false;
9946
9947 s = htab->global_entry;
9948 plt = htab->elf.splt;
9949 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9950 if (pent->plt.offset != (bfd_vma) -1
9951 && pent->addend == 0)
9952 {
9953 /* For ELFv2, if this symbol is not defined in a regular file
9954 and we are not generating a shared library or pie, then we
9955 need to define the symbol in the executable on a call stub.
9956 This is to avoid text relocations. */
9957 bfd_vma off, stub_align, stub_off, stub_size;
9958 unsigned int align_power;
9959
9960 stub_size = 16;
9961 stub_off = s->size;
9962 if (htab->params->plt_stub_align >= 0)
9963 align_power = htab->params->plt_stub_align;
9964 else
9965 align_power = -htab->params->plt_stub_align;
9966 /* Setting section alignment is delayed until we know it is
9967 non-empty. Otherwise the .text output section will be
9968 aligned at least to plt_stub_align even when no global
9969 entry stubs are needed. */
9970 if (s->alignment_power < align_power)
9971 s->alignment_power = align_power;
9972 stub_align = (bfd_vma) 1 << align_power;
9973 if (htab->params->plt_stub_align >= 0
9974 || ((((stub_off + stub_size - 1) & -stub_align)
9975 - (stub_off & -stub_align))
9976 > ((stub_size - 1) & -stub_align)))
9977 stub_off = (stub_off + stub_align - 1) & -stub_align;
9978 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
9979 off -= stub_off + s->output_offset + s->output_section->vma;
9980 /* Note that for --plt-stub-align negative we have a possible
9981 dependency between stub offset and size. Break that
9982 dependency by assuming the max stub size when calculating
9983 the stub offset. */
9984 if (PPC_HA (off) == 0)
9985 stub_size -= 4;
9986 h->root.type = bfd_link_hash_defined;
9987 h->root.u.def.section = s;
9988 h->root.u.def.value = stub_off;
9989 s->size = stub_off + stub_size;
9990 break;
9991 }
9992 return true;
9993 }
9994
9995 /* Set the sizes of the dynamic sections. */
9996
9997 static bool
9998 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
9999 struct bfd_link_info *info)
10000 {
10001 struct ppc_link_hash_table *htab;
10002 bfd *dynobj;
10003 asection *s;
10004 bool relocs;
10005 bfd *ibfd;
10006 struct got_entry *first_tlsld;
10007
10008 htab = ppc_hash_table (info);
10009 if (htab == NULL)
10010 return false;
10011
10012 dynobj = htab->elf.dynobj;
10013 if (dynobj == NULL)
10014 abort ();
10015
10016 if (htab->elf.dynamic_sections_created)
10017 {
10018 /* Set the contents of the .interp section to the interpreter. */
10019 if (bfd_link_executable (info) && !info->nointerp)
10020 {
10021 s = bfd_get_linker_section (dynobj, ".interp");
10022 if (s == NULL)
10023 abort ();
10024 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10025 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10026 }
10027 }
10028
10029 /* Set up .got offsets for local syms, and space for local dynamic
10030 relocs. */
10031 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10032 {
10033 struct got_entry **lgot_ents;
10034 struct got_entry **end_lgot_ents;
10035 struct plt_entry **local_plt;
10036 struct plt_entry **end_local_plt;
10037 unsigned char *lgot_masks;
10038 bfd_size_type locsymcount;
10039 Elf_Internal_Shdr *symtab_hdr;
10040
10041 if (!is_ppc64_elf (ibfd))
10042 continue;
10043
10044 for (s = ibfd->sections; s != NULL; s = s->next)
10045 {
10046 struct ppc_dyn_relocs *p;
10047
10048 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10049 {
10050 if (!bfd_is_abs_section (p->sec)
10051 && bfd_is_abs_section (p->sec->output_section))
10052 {
10053 /* Input section has been discarded, either because
10054 it is a copy of a linkonce section or due to
10055 linker script /DISCARD/, so we'll be discarding
10056 the relocs too. */
10057 }
10058 else if (p->count != 0)
10059 {
10060 asection *srel = elf_section_data (p->sec)->sreloc;
10061 if (p->ifunc)
10062 srel = htab->elf.irelplt;
10063 srel->size += p->count * sizeof (Elf64_External_Rela);
10064 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10065 info->flags |= DF_TEXTREL;
10066 }
10067 }
10068 }
10069
10070 lgot_ents = elf_local_got_ents (ibfd);
10071 if (!lgot_ents)
10072 continue;
10073
10074 symtab_hdr = &elf_symtab_hdr (ibfd);
10075 locsymcount = symtab_hdr->sh_info;
10076 end_lgot_ents = lgot_ents + locsymcount;
10077 local_plt = (struct plt_entry **) end_lgot_ents;
10078 end_local_plt = local_plt + locsymcount;
10079 lgot_masks = (unsigned char *) end_local_plt;
10080 s = ppc64_elf_tdata (ibfd)->got;
10081 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10082 {
10083 struct got_entry **pent, *ent;
10084
10085 pent = lgot_ents;
10086 while ((ent = *pent) != NULL)
10087 if (ent->got.refcount > 0)
10088 {
10089 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10090 {
10091 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10092 *pent = ent->next;
10093 }
10094 else
10095 {
10096 unsigned int ent_size = 8;
10097 unsigned int rel_size = sizeof (Elf64_External_Rela);
10098
10099 ent->got.offset = s->size;
10100 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10101 {
10102 ent_size *= 2;
10103 rel_size *= 2;
10104 }
10105 s->size += ent_size;
10106 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10107 {
10108 htab->elf.irelplt->size += rel_size;
10109 htab->got_reli_size += rel_size;
10110 }
10111 else if (bfd_link_pic (info)
10112 && !(ent->tls_type != 0
10113 && bfd_link_executable (info)))
10114 {
10115 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10116 srel->size += rel_size;
10117 }
10118 pent = &ent->next;
10119 }
10120 }
10121 else
10122 *pent = ent->next;
10123 }
10124
10125 /* Allocate space for plt calls to local syms. */
10126 lgot_masks = (unsigned char *) end_local_plt;
10127 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10128 {
10129 struct plt_entry *ent;
10130
10131 for (ent = *local_plt; ent != NULL; ent = ent->next)
10132 if (ent->plt.refcount > 0)
10133 {
10134 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10135 {
10136 s = htab->elf.iplt;
10137 ent->plt.offset = s->size;
10138 s->size += PLT_ENTRY_SIZE (htab);
10139 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10140 }
10141 else if (htab->can_convert_all_inline_plt
10142 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10143 ent->plt.offset = (bfd_vma) -1;
10144 else
10145 {
10146 s = htab->pltlocal;
10147 ent->plt.offset = s->size;
10148 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10149 if (bfd_link_pic (info))
10150 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10151 }
10152 }
10153 else
10154 ent->plt.offset = (bfd_vma) -1;
10155 }
10156 }
10157
10158 /* Allocate global sym .plt and .got entries, and space for global
10159 sym dynamic relocs. */
10160 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10161
10162 if (!htab->opd_abi && !bfd_link_pic (info))
10163 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10164
10165 first_tlsld = NULL;
10166 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10167 {
10168 struct got_entry *ent;
10169
10170 if (!is_ppc64_elf (ibfd))
10171 continue;
10172
10173 ent = ppc64_tlsld_got (ibfd);
10174 if (ent->got.refcount > 0)
10175 {
10176 if (!htab->do_multi_toc && first_tlsld != NULL)
10177 {
10178 ent->is_indirect = true;
10179 ent->got.ent = first_tlsld;
10180 }
10181 else
10182 {
10183 if (first_tlsld == NULL)
10184 first_tlsld = ent;
10185 s = ppc64_elf_tdata (ibfd)->got;
10186 ent->got.offset = s->size;
10187 ent->owner = ibfd;
10188 s->size += 16;
10189 if (bfd_link_dll (info))
10190 {
10191 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10192 srel->size += sizeof (Elf64_External_Rela);
10193 }
10194 }
10195 }
10196 else
10197 ent->got.offset = (bfd_vma) -1;
10198 }
10199
10200 /* We now have determined the sizes of the various dynamic sections.
10201 Allocate memory for them. */
10202 relocs = false;
10203 for (s = dynobj->sections; s != NULL; s = s->next)
10204 {
10205 if ((s->flags & SEC_LINKER_CREATED) == 0)
10206 continue;
10207
10208 if (s == htab->brlt || s == htab->relbrlt)
10209 /* These haven't been allocated yet; don't strip. */
10210 continue;
10211 else if (s == htab->elf.sgot
10212 || s == htab->elf.splt
10213 || s == htab->elf.iplt
10214 || s == htab->pltlocal
10215 || s == htab->glink
10216 || s == htab->global_entry
10217 || s == htab->elf.sdynbss
10218 || s == htab->elf.sdynrelro)
10219 {
10220 /* Strip this section if we don't need it; see the
10221 comment below. */
10222 }
10223 else if (s == htab->glink_eh_frame)
10224 {
10225 if (!bfd_is_abs_section (s->output_section))
10226 /* Not sized yet. */
10227 continue;
10228 }
10229 else if (startswith (s->name, ".rela"))
10230 {
10231 if (s->size != 0)
10232 {
10233 if (s != htab->elf.srelplt)
10234 relocs = true;
10235
10236 /* We use the reloc_count field as a counter if we need
10237 to copy relocs into the output file. */
10238 s->reloc_count = 0;
10239 }
10240 }
10241 else
10242 {
10243 /* It's not one of our sections, so don't allocate space. */
10244 continue;
10245 }
10246
10247 if (s->size == 0)
10248 {
10249 /* If we don't need this section, strip it from the
10250 output file. This is mostly to handle .rela.bss and
10251 .rela.plt. We must create both sections in
10252 create_dynamic_sections, because they must be created
10253 before the linker maps input sections to output
10254 sections. The linker does that before
10255 adjust_dynamic_symbol is called, and it is that
10256 function which decides whether anything needs to go
10257 into these sections. */
10258 s->flags |= SEC_EXCLUDE;
10259 continue;
10260 }
10261
10262 if (bfd_is_abs_section (s->output_section))
10263 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10264 s->name);
10265
10266 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10267 continue;
10268
10269 /* Allocate memory for the section contents. We use bfd_zalloc
10270 here in case unused entries are not reclaimed before the
10271 section's contents are written out. This should not happen,
10272 but this way if it does we get a R_PPC64_NONE reloc in .rela
10273 sections instead of garbage.
10274 We also rely on the section contents being zero when writing
10275 the GOT and .dynrelro. */
10276 s->contents = bfd_zalloc (dynobj, s->size);
10277 if (s->contents == NULL)
10278 return false;
10279 }
10280
10281 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10282 {
10283 if (!is_ppc64_elf (ibfd))
10284 continue;
10285
10286 s = ppc64_elf_tdata (ibfd)->got;
10287 if (s != NULL && s != htab->elf.sgot)
10288 {
10289 if (s->size == 0)
10290 s->flags |= SEC_EXCLUDE;
10291 else
10292 {
10293 s->contents = bfd_zalloc (ibfd, s->size);
10294 if (s->contents == NULL)
10295 return false;
10296 }
10297 }
10298 s = ppc64_elf_tdata (ibfd)->relgot;
10299 if (s != NULL)
10300 {
10301 if (s->size == 0)
10302 s->flags |= SEC_EXCLUDE;
10303 else
10304 {
10305 s->contents = bfd_zalloc (ibfd, s->size);
10306 if (s->contents == NULL)
10307 return false;
10308 relocs = true;
10309 s->reloc_count = 0;
10310 }
10311 }
10312 }
10313
10314 if (htab->elf.dynamic_sections_created)
10315 {
10316 bool tls_opt;
10317
10318 /* Add some entries to the .dynamic section. We fill in the
10319 values later, in ppc64_elf_finish_dynamic_sections, but we
10320 must add the entries now so that we get the correct size for
10321 the .dynamic section. The DT_DEBUG entry is filled in by the
10322 dynamic linker and used by the debugger. */
10323 #define add_dynamic_entry(TAG, VAL) \
10324 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10325
10326 if (bfd_link_executable (info))
10327 {
10328 if (!add_dynamic_entry (DT_DEBUG, 0))
10329 return false;
10330 }
10331
10332 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10333 {
10334 if (!add_dynamic_entry (DT_PLTGOT, 0)
10335 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10336 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10337 || !add_dynamic_entry (DT_JMPREL, 0)
10338 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10339 return false;
10340 }
10341
10342 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10343 {
10344 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10345 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10346 return false;
10347 }
10348
10349 tls_opt = (htab->params->tls_get_addr_opt
10350 && ((htab->tls_get_addr_fd != NULL
10351 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10352 || (htab->tga_desc_fd != NULL
10353 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10354 if (tls_opt || !htab->opd_abi)
10355 {
10356 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10357 return false;
10358 }
10359
10360 if (relocs)
10361 {
10362 if (!add_dynamic_entry (DT_RELA, 0)
10363 || !add_dynamic_entry (DT_RELASZ, 0)
10364 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10365 return false;
10366
10367 /* If any dynamic relocs apply to a read-only section,
10368 then we need a DT_TEXTREL entry. */
10369 if ((info->flags & DF_TEXTREL) == 0)
10370 elf_link_hash_traverse (&htab->elf,
10371 _bfd_elf_maybe_set_textrel, info);
10372
10373 if ((info->flags & DF_TEXTREL) != 0)
10374 {
10375 if (!add_dynamic_entry (DT_TEXTREL, 0))
10376 return false;
10377 }
10378 }
10379 }
10380 #undef add_dynamic_entry
10381
10382 return true;
10383 }
10384
10385 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10386
10387 static bool
10388 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10389 {
10390 if (h->plt.plist != NULL
10391 && !h->def_regular
10392 && !h->pointer_equality_needed)
10393 return false;
10394
10395 return _bfd_elf_hash_symbol (h);
10396 }
10397
10398 /* Determine the type of stub needed, if any, for a call. */
10399
10400 static inline enum ppc_stub_type
10401 ppc_type_of_stub (asection *input_sec,
10402 const Elf_Internal_Rela *rel,
10403 struct ppc_link_hash_entry **hash,
10404 struct plt_entry **plt_ent,
10405 bfd_vma destination,
10406 unsigned long local_off)
10407 {
10408 struct ppc_link_hash_entry *h = *hash;
10409 bfd_vma location;
10410 bfd_vma branch_offset;
10411 bfd_vma max_branch_offset;
10412 enum elf_ppc64_reloc_type r_type;
10413
10414 if (h != NULL)
10415 {
10416 struct plt_entry *ent;
10417 struct ppc_link_hash_entry *fdh = h;
10418 if (h->oh != NULL
10419 && h->oh->is_func_descriptor)
10420 {
10421 fdh = ppc_follow_link (h->oh);
10422 *hash = fdh;
10423 }
10424
10425 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10426 if (ent->addend == rel->r_addend
10427 && ent->plt.offset != (bfd_vma) -1)
10428 {
10429 *plt_ent = ent;
10430 return ppc_stub_plt_call;
10431 }
10432
10433 /* Here, we know we don't have a plt entry. If we don't have a
10434 either a defined function descriptor or a defined entry symbol
10435 in a regular object file, then it is pointless trying to make
10436 any other type of stub. */
10437 if (!is_static_defined (&fdh->elf)
10438 && !is_static_defined (&h->elf))
10439 return ppc_stub_none;
10440 }
10441 else if (elf_local_got_ents (input_sec->owner) != NULL)
10442 {
10443 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10444 struct plt_entry **local_plt = (struct plt_entry **)
10445 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10446 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10447
10448 if (local_plt[r_symndx] != NULL)
10449 {
10450 struct plt_entry *ent;
10451
10452 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10453 if (ent->addend == rel->r_addend
10454 && ent->plt.offset != (bfd_vma) -1)
10455 {
10456 *plt_ent = ent;
10457 return ppc_stub_plt_call;
10458 }
10459 }
10460 }
10461
10462 /* Determine where the call point is. */
10463 location = (input_sec->output_offset
10464 + input_sec->output_section->vma
10465 + rel->r_offset);
10466
10467 branch_offset = destination - location;
10468 r_type = ELF64_R_TYPE (rel->r_info);
10469
10470 /* Determine if a long branch stub is needed. */
10471 max_branch_offset = 1 << 25;
10472 if (r_type == R_PPC64_REL14
10473 || r_type == R_PPC64_REL14_BRTAKEN
10474 || r_type == R_PPC64_REL14_BRNTAKEN)
10475 max_branch_offset = 1 << 15;
10476
10477 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10478 /* We need a stub. Figure out whether a long_branch or plt_branch
10479 is needed later. */
10480 return ppc_stub_long_branch;
10481
10482 return ppc_stub_none;
10483 }
10484
10485 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10486 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10487 . mflr %r12
10488 . bcl 20,31,1f
10489 .1: mflr %r11
10490 . mtlr %r12
10491 . lis %r12,xxx-1b@highest
10492 . ori %r12,%r12,xxx-1b@higher
10493 . sldi %r12,%r12,32
10494 . oris %r12,%r12,xxx-1b@high
10495 . ori %r12,%r12,xxx-1b@l
10496 . add/ldx %r12,%r11,%r12 */
10497
10498 static bfd_byte *
10499 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10500 {
10501 bfd_put_32 (abfd, MFLR_R12, p);
10502 p += 4;
10503 bfd_put_32 (abfd, BCL_20_31, p);
10504 p += 4;
10505 bfd_put_32 (abfd, MFLR_R11, p);
10506 p += 4;
10507 bfd_put_32 (abfd, MTLR_R12, p);
10508 p += 4;
10509 if (off + 0x8000 < 0x10000)
10510 {
10511 if (load)
10512 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10513 else
10514 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10515 p += 4;
10516 }
10517 else if (off + 0x80008000ULL < 0x100000000ULL)
10518 {
10519 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10520 p += 4;
10521 if (load)
10522 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10523 else
10524 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10525 p += 4;
10526 }
10527 else
10528 {
10529 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10530 {
10531 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10532 p += 4;
10533 }
10534 else
10535 {
10536 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10537 p += 4;
10538 if (((off >> 32) & 0xffff) != 0)
10539 {
10540 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10541 p += 4;
10542 }
10543 }
10544 if (((off >> 32) & 0xffffffffULL) != 0)
10545 {
10546 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10547 p += 4;
10548 }
10549 if (PPC_HI (off) != 0)
10550 {
10551 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10552 p += 4;
10553 }
10554 if (PPC_LO (off) != 0)
10555 {
10556 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10557 p += 4;
10558 }
10559 if (load)
10560 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10561 else
10562 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10563 p += 4;
10564 }
10565 return p;
10566 }
10567
10568 static unsigned int
10569 size_offset (bfd_vma off)
10570 {
10571 unsigned int size;
10572 if (off + 0x8000 < 0x10000)
10573 size = 4;
10574 else if (off + 0x80008000ULL < 0x100000000ULL)
10575 size = 8;
10576 else
10577 {
10578 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10579 size = 4;
10580 else
10581 {
10582 size = 4;
10583 if (((off >> 32) & 0xffff) != 0)
10584 size += 4;
10585 }
10586 if (((off >> 32) & 0xffffffffULL) != 0)
10587 size += 4;
10588 if (PPC_HI (off) != 0)
10589 size += 4;
10590 if (PPC_LO (off) != 0)
10591 size += 4;
10592 size += 4;
10593 }
10594 return size + 16;
10595 }
10596
10597 static unsigned int
10598 num_relocs_for_offset (bfd_vma off)
10599 {
10600 unsigned int num_rel;
10601 if (off + 0x8000 < 0x10000)
10602 num_rel = 1;
10603 else if (off + 0x80008000ULL < 0x100000000ULL)
10604 num_rel = 2;
10605 else
10606 {
10607 num_rel = 1;
10608 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10609 && ((off >> 32) & 0xffff) != 0)
10610 num_rel += 1;
10611 if (PPC_HI (off) != 0)
10612 num_rel += 1;
10613 if (PPC_LO (off) != 0)
10614 num_rel += 1;
10615 }
10616 return num_rel;
10617 }
10618
10619 static Elf_Internal_Rela *
10620 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10621 bfd_vma roff, bfd_vma targ, bfd_vma off)
10622 {
10623 bfd_vma relative_targ = targ - (roff - 8);
10624 if (bfd_big_endian (info->output_bfd))
10625 roff += 2;
10626 r->r_offset = roff;
10627 r->r_addend = relative_targ + roff;
10628 if (off + 0x8000 < 0x10000)
10629 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10630 else if (off + 0x80008000ULL < 0x100000000ULL)
10631 {
10632 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10633 ++r;
10634 roff += 4;
10635 r->r_offset = roff;
10636 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10637 r->r_addend = relative_targ + roff;
10638 }
10639 else
10640 {
10641 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10642 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10643 else
10644 {
10645 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10646 if (((off >> 32) & 0xffff) != 0)
10647 {
10648 ++r;
10649 roff += 4;
10650 r->r_offset = roff;
10651 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10652 r->r_addend = relative_targ + roff;
10653 }
10654 }
10655 if (((off >> 32) & 0xffffffffULL) != 0)
10656 roff += 4;
10657 if (PPC_HI (off) != 0)
10658 {
10659 ++r;
10660 roff += 4;
10661 r->r_offset = roff;
10662 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10663 r->r_addend = relative_targ + roff;
10664 }
10665 if (PPC_LO (off) != 0)
10666 {
10667 ++r;
10668 roff += 4;
10669 r->r_offset = roff;
10670 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10671 r->r_addend = relative_targ + roff;
10672 }
10673 }
10674 return r;
10675 }
10676
10677 static bfd_byte *
10678 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10679 bool load)
10680 {
10681 uint64_t insn;
10682 if (off - odd + (1ULL << 33) < 1ULL << 34)
10683 {
10684 off -= odd;
10685 if (odd)
10686 {
10687 bfd_put_32 (abfd, NOP, p);
10688 p += 4;
10689 }
10690 if (load)
10691 insn = PLD_R12_PC;
10692 else
10693 insn = PADDI_R12_PC;
10694 insn |= D34 (off);
10695 bfd_put_32 (abfd, insn >> 32, p);
10696 p += 4;
10697 bfd_put_32 (abfd, insn, p);
10698 }
10699 /* The minimum value for paddi is -0x200000000. The minimum value
10700 for li is -0x8000, which when shifted by 34 and added gives a
10701 minimum value of -0x2000200000000. The maximum value is
10702 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10703 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10704 {
10705 off -= 8 - odd;
10706 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10707 p += 4;
10708 if (!odd)
10709 {
10710 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10711 p += 4;
10712 }
10713 insn = PADDI_R12_PC | D34 (off);
10714 bfd_put_32 (abfd, insn >> 32, p);
10715 p += 4;
10716 bfd_put_32 (abfd, insn, p);
10717 p += 4;
10718 if (odd)
10719 {
10720 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10721 p += 4;
10722 }
10723 if (load)
10724 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10725 else
10726 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10727 }
10728 else
10729 {
10730 off -= odd + 8;
10731 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10732 p += 4;
10733 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10734 p += 4;
10735 if (odd)
10736 {
10737 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10738 p += 4;
10739 }
10740 insn = PADDI_R12_PC | D34 (off);
10741 bfd_put_32 (abfd, insn >> 32, p);
10742 p += 4;
10743 bfd_put_32 (abfd, insn, p);
10744 p += 4;
10745 if (!odd)
10746 {
10747 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10748 p += 4;
10749 }
10750 if (load)
10751 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10752 else
10753 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10754 }
10755 p += 4;
10756 return p;
10757 }
10758
10759 static unsigned int
10760 size_power10_offset (bfd_vma off, int odd)
10761 {
10762 if (off - odd + (1ULL << 33) < 1ULL << 34)
10763 return odd + 8;
10764 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10765 return 20;
10766 else
10767 return 24;
10768 }
10769
10770 static unsigned int
10771 num_relocs_for_power10_offset (bfd_vma off, int odd)
10772 {
10773 if (off - odd + (1ULL << 33) < 1ULL << 34)
10774 return 1;
10775 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10776 return 2;
10777 else
10778 return 3;
10779 }
10780
10781 static Elf_Internal_Rela *
10782 emit_relocs_for_power10_offset (struct bfd_link_info *info,
10783 Elf_Internal_Rela *r, bfd_vma roff,
10784 bfd_vma targ, bfd_vma off, int odd)
10785 {
10786 if (off - odd + (1ULL << 33) < 1ULL << 34)
10787 roff += odd;
10788 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10789 {
10790 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10791 r->r_offset = roff + d_offset;
10792 r->r_addend = targ + 8 - odd - d_offset;
10793 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10794 ++r;
10795 roff += 8 - odd;
10796 }
10797 else
10798 {
10799 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10800 r->r_offset = roff + d_offset;
10801 r->r_addend = targ + 8 + odd - d_offset;
10802 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10803 ++r;
10804 roff += 4;
10805 r->r_offset = roff + d_offset;
10806 r->r_addend = targ + 4 + odd - d_offset;
10807 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10808 ++r;
10809 roff += 4 + odd;
10810 }
10811 r->r_offset = roff;
10812 r->r_addend = targ;
10813 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10814 return r;
10815 }
10816
10817 /* Emit .eh_frame opcode to advance pc by DELTA. */
10818
10819 static bfd_byte *
10820 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10821 {
10822 delta /= 4;
10823 if (delta < 64)
10824 *eh++ = DW_CFA_advance_loc + delta;
10825 else if (delta < 256)
10826 {
10827 *eh++ = DW_CFA_advance_loc1;
10828 *eh++ = delta;
10829 }
10830 else if (delta < 65536)
10831 {
10832 *eh++ = DW_CFA_advance_loc2;
10833 bfd_put_16 (abfd, delta, eh);
10834 eh += 2;
10835 }
10836 else
10837 {
10838 *eh++ = DW_CFA_advance_loc4;
10839 bfd_put_32 (abfd, delta, eh);
10840 eh += 4;
10841 }
10842 return eh;
10843 }
10844
10845 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10846
10847 static unsigned int
10848 eh_advance_size (unsigned int delta)
10849 {
10850 if (delta < 64 * 4)
10851 /* DW_CFA_advance_loc+[1..63]. */
10852 return 1;
10853 if (delta < 256 * 4)
10854 /* DW_CFA_advance_loc1, byte. */
10855 return 2;
10856 if (delta < 65536 * 4)
10857 /* DW_CFA_advance_loc2, 2 bytes. */
10858 return 3;
10859 /* DW_CFA_advance_loc4, 4 bytes. */
10860 return 5;
10861 }
10862
10863 /* With power7 weakly ordered memory model, it is possible for ld.so
10864 to update a plt entry in one thread and have another thread see a
10865 stale zero toc entry. To avoid this we need some sort of acquire
10866 barrier in the call stub. One solution is to make the load of the
10867 toc word seem to appear to depend on the load of the function entry
10868 word. Another solution is to test for r2 being zero, and branch to
10869 the appropriate glink entry if so.
10870
10871 . fake dep barrier compare
10872 . ld 12,xxx(2) ld 12,xxx(2)
10873 . mtctr 12 mtctr 12
10874 . xor 11,12,12 ld 2,xxx+8(2)
10875 . add 2,2,11 cmpldi 2,0
10876 . ld 2,xxx+8(2) bnectr+
10877 . bctr b <glink_entry>
10878
10879 The solution involving the compare turns out to be faster, so
10880 that's what we use unless the branch won't reach. */
10881
10882 #define ALWAYS_USE_FAKE_DEP 0
10883 #define ALWAYS_EMIT_R2SAVE 0
10884
10885 static inline unsigned int
10886 plt_stub_size (struct ppc_link_hash_table *htab,
10887 struct ppc_stub_hash_entry *stub_entry,
10888 bfd_vma off,
10889 unsigned int odd)
10890 {
10891 unsigned size;
10892
10893 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
10894 {
10895 if (htab->params->power10_stubs != 0)
10896 size = 8 + size_power10_offset (off, odd);
10897 else
10898 size = 8 + size_offset (off - 8);
10899 if (stub_entry->stub_type > ppc_stub_plt_call_notoc)
10900 size += 4;
10901 }
10902 else
10903 {
10904 size = 12;
10905 if (ALWAYS_EMIT_R2SAVE
10906 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
10907 size += 4;
10908 if (PPC_HA (off) != 0)
10909 size += 4;
10910 if (htab->opd_abi)
10911 {
10912 size += 4;
10913 if (htab->params->plt_static_chain)
10914 size += 4;
10915 if (htab->params->plt_thread_safe
10916 && htab->elf.dynamic_sections_created
10917 && stub_entry->h != NULL
10918 && stub_entry->h->elf.dynindx != -1)
10919 size += 8;
10920 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
10921 != PPC_HA (off))
10922 size += 4;
10923 }
10924 }
10925 if (stub_entry->h != NULL
10926 && is_tls_get_addr (&stub_entry->h->elf, htab)
10927 && htab->params->tls_get_addr_opt)
10928 {
10929 if (!htab->params->no_tls_get_addr_regsave)
10930 {
10931 size += 30 * 4;
10932 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10933 || stub_entry->stub_type == ppc_stub_plt_call_both)
10934 size += 4;
10935 }
10936 else
10937 {
10938 size += 7 * 4;
10939 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
10940 || stub_entry->stub_type == ppc_stub_plt_call_both)
10941 size += 6 * 4;
10942 }
10943 }
10944 return size;
10945 }
10946
10947 /* Depending on the sign of plt_stub_align:
10948 If positive, return the padding to align to a 2**plt_stub_align
10949 boundary.
10950 If negative, if this stub would cross fewer 2**plt_stub_align
10951 boundaries if we align, then return the padding needed to do so. */
10952
10953 static inline unsigned int
10954 plt_stub_pad (struct ppc_link_hash_table *htab,
10955 struct ppc_stub_hash_entry *stub_entry,
10956 bfd_vma plt_off,
10957 unsigned int odd)
10958 {
10959 int stub_align;
10960 unsigned stub_size;
10961 bfd_vma stub_off = stub_entry->group->stub_sec->size;
10962
10963 if (htab->params->plt_stub_align >= 0)
10964 {
10965 stub_align = 1 << htab->params->plt_stub_align;
10966 if ((stub_off & (stub_align - 1)) != 0)
10967 return stub_align - (stub_off & (stub_align - 1));
10968 return 0;
10969 }
10970
10971 stub_align = 1 << -htab->params->plt_stub_align;
10972 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
10973 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
10974 > ((stub_size - 1) & -stub_align))
10975 return stub_align - (stub_off & (stub_align - 1));
10976 return 0;
10977 }
10978
10979 /* Build a .plt call stub. */
10980
10981 static inline bfd_byte *
10982 build_plt_stub (struct ppc_link_hash_table *htab,
10983 struct ppc_stub_hash_entry *stub_entry,
10984 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
10985 {
10986 bfd *obfd = htab->params->stub_bfd;
10987 bool plt_load_toc = htab->opd_abi;
10988 bool plt_static_chain = htab->params->plt_static_chain;
10989 bool plt_thread_safe = (htab->params->plt_thread_safe
10990 && htab->elf.dynamic_sections_created
10991 && stub_entry->h != NULL
10992 && stub_entry->h->elf.dynindx != -1);
10993 bool use_fake_dep = plt_thread_safe;
10994 bfd_vma cmp_branch_off = 0;
10995
10996 if (!ALWAYS_USE_FAKE_DEP
10997 && plt_load_toc
10998 && plt_thread_safe
10999 && !(stub_entry->h != NULL
11000 && is_tls_get_addr (&stub_entry->h->elf, htab)
11001 && htab->params->tls_get_addr_opt))
11002 {
11003 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11004 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11005 / PLT_ENTRY_SIZE (htab));
11006 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11007 bfd_vma to, from;
11008
11009 if (pltindex > 32768)
11010 glinkoff += (pltindex - 32768) * 4;
11011 to = (glinkoff
11012 + htab->glink->output_offset
11013 + htab->glink->output_section->vma);
11014 from = (p - stub_entry->group->stub_sec->contents
11015 + 4 * (ALWAYS_EMIT_R2SAVE
11016 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11017 + 4 * (PPC_HA (offset) != 0)
11018 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11019 != PPC_HA (offset))
11020 + 4 * (plt_static_chain != 0)
11021 + 20
11022 + stub_entry->group->stub_sec->output_offset
11023 + stub_entry->group->stub_sec->output_section->vma);
11024 cmp_branch_off = to - from;
11025 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11026 }
11027
11028 if (PPC_HA (offset) != 0)
11029 {
11030 if (r != NULL)
11031 {
11032 if (ALWAYS_EMIT_R2SAVE
11033 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11034 r[0].r_offset += 4;
11035 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11036 r[1].r_offset = r[0].r_offset + 4;
11037 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11038 r[1].r_addend = r[0].r_addend;
11039 if (plt_load_toc)
11040 {
11041 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11042 {
11043 r[2].r_offset = r[1].r_offset + 4;
11044 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11045 r[2].r_addend = r[0].r_addend;
11046 }
11047 else
11048 {
11049 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11050 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11051 r[2].r_addend = r[0].r_addend + 8;
11052 if (plt_static_chain)
11053 {
11054 r[3].r_offset = r[2].r_offset + 4;
11055 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11056 r[3].r_addend = r[0].r_addend + 16;
11057 }
11058 }
11059 }
11060 }
11061 if (ALWAYS_EMIT_R2SAVE
11062 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11063 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11064 if (plt_load_toc)
11065 {
11066 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11067 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11068 }
11069 else
11070 {
11071 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11072 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11073 }
11074 if (plt_load_toc
11075 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11076 {
11077 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11078 offset = 0;
11079 }
11080 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11081 if (plt_load_toc)
11082 {
11083 if (use_fake_dep)
11084 {
11085 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11086 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11087 }
11088 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11089 if (plt_static_chain)
11090 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11091 }
11092 }
11093 else
11094 {
11095 if (r != NULL)
11096 {
11097 if (ALWAYS_EMIT_R2SAVE
11098 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11099 r[0].r_offset += 4;
11100 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11101 if (plt_load_toc)
11102 {
11103 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11104 {
11105 r[1].r_offset = r[0].r_offset + 4;
11106 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11107 r[1].r_addend = r[0].r_addend;
11108 }
11109 else
11110 {
11111 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11112 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11113 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11114 if (plt_static_chain)
11115 {
11116 r[2].r_offset = r[1].r_offset + 4;
11117 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11118 r[2].r_addend = r[0].r_addend + 8;
11119 }
11120 }
11121 }
11122 }
11123 if (ALWAYS_EMIT_R2SAVE
11124 || stub_entry->stub_type == ppc_stub_plt_call_r2save)
11125 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11126 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11127 if (plt_load_toc
11128 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11129 {
11130 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11131 offset = 0;
11132 }
11133 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11134 if (plt_load_toc)
11135 {
11136 if (use_fake_dep)
11137 {
11138 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11139 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11140 }
11141 if (plt_static_chain)
11142 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11143 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11144 }
11145 }
11146 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11147 {
11148 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11149 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11150 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11151 }
11152 else
11153 bfd_put_32 (obfd, BCTR, p), p += 4;
11154 return p;
11155 }
11156
11157 /* Build a special .plt call stub for __tls_get_addr. */
11158
11159 #define LD_R0_0R3 0xe8030000
11160 #define LD_R12_0R3 0xe9830000
11161 #define MR_R0_R3 0x7c601b78
11162 #define CMPDI_R0_0 0x2c200000
11163 #define ADD_R3_R12_R13 0x7c6c6a14
11164 #define BEQLR 0x4d820020
11165 #define MR_R3_R0 0x7c030378
11166 #define BCTRL 0x4e800421
11167
11168 static bfd_byte *
11169 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11170 struct ppc_stub_hash_entry *stub_entry,
11171 bfd_byte *p)
11172 {
11173 bfd *obfd = htab->params->stub_bfd;
11174
11175 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11176 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11177 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11178 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11179 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11180 bfd_put_32 (obfd, BEQLR, p), p += 4;
11181 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11182
11183 if (!htab->params->no_tls_get_addr_regsave)
11184 p = tls_get_addr_prologue (obfd, p, htab);
11185 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11186 || stub_entry->stub_type == ppc_stub_plt_call_both)
11187 {
11188 bfd_put_32 (obfd, MFLR_R0, p);
11189 p += 4;
11190 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11191 p += 4;
11192 }
11193 return p;
11194 }
11195
11196 static bfd_byte *
11197 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11198 struct ppc_stub_hash_entry *stub_entry,
11199 bfd_byte *p,
11200 bfd_byte *loc)
11201 {
11202 bfd *obfd = htab->params->stub_bfd;
11203
11204 if (!htab->params->no_tls_get_addr_regsave)
11205 {
11206 bfd_put_32 (obfd, BCTRL, p - 4);
11207
11208 if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11209 || stub_entry->stub_type == ppc_stub_plt_call_both)
11210 {
11211 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11212 p += 4;
11213 }
11214 p = tls_get_addr_epilogue (obfd, p, htab);
11215 }
11216 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11217 || stub_entry->stub_type == ppc_stub_plt_call_both)
11218 {
11219 bfd_put_32 (obfd, BCTRL, p - 4);
11220
11221 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11222 p += 4;
11223 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11224 p += 4;
11225 bfd_put_32 (obfd, MTLR_R0, p);
11226 p += 4;
11227 bfd_put_32 (obfd, BLR, p);
11228 p += 4;
11229 }
11230
11231 if (htab->glink_eh_frame != NULL
11232 && htab->glink_eh_frame->size != 0)
11233 {
11234 bfd_byte *base, *eh;
11235
11236 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11237 eh = base + stub_entry->group->eh_size;
11238
11239 if (!htab->params->no_tls_get_addr_regsave)
11240 {
11241 unsigned int cfa_updt, delta, i;
11242
11243 /* After the bctrl, lr has been modified so we need to emit
11244 .eh_frame info saying the return address is on the stack. In
11245 fact we must put the EH info at or before the call rather
11246 than after it, because the EH info for a call needs to be
11247 specified by that point.
11248 See libgcc/unwind-dw2.c execute_cfa_program.
11249 Any stack pointer update must be described immediately after
11250 the instruction making the change, and since the stdu occurs
11251 after saving regs we put all the reg saves and the cfa
11252 change there. */
11253 cfa_updt = stub_entry->stub_offset + 18 * 4;
11254 delta = cfa_updt - stub_entry->group->lr_restore;
11255 stub_entry->group->lr_restore
11256 = stub_entry->stub_offset + (p - loc) - 4;
11257 eh = eh_advance (htab->elf.dynobj, eh, delta);
11258 *eh++ = DW_CFA_def_cfa_offset;
11259 if (htab->opd_abi)
11260 {
11261 *eh++ = 128;
11262 *eh++ = 1;
11263 }
11264 else
11265 *eh++ = 96;
11266 *eh++ = DW_CFA_offset_extended_sf;
11267 *eh++ = 65;
11268 *eh++ = (-16 / 8) & 0x7f;
11269 for (i = 4; i < 12; i++)
11270 {
11271 *eh++ = DW_CFA_offset + i;
11272 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11273 }
11274 *eh++ = (DW_CFA_advance_loc
11275 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11276 *eh++ = DW_CFA_def_cfa_offset;
11277 *eh++ = 0;
11278 for (i = 4; i < 12; i++)
11279 *eh++ = DW_CFA_restore + i;
11280 *eh++ = DW_CFA_advance_loc + 2;
11281 *eh++ = DW_CFA_restore_extended;
11282 *eh++ = 65;
11283 stub_entry->group->eh_size = eh - base;
11284 }
11285 else if (stub_entry->stub_type == ppc_stub_plt_call_r2save
11286 || stub_entry->stub_type == ppc_stub_plt_call_both)
11287 {
11288 unsigned int lr_used, delta;
11289
11290 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11291 delta = lr_used - stub_entry->group->lr_restore;
11292 stub_entry->group->lr_restore = lr_used + 16;
11293 eh = eh_advance (htab->elf.dynobj, eh, delta);
11294 *eh++ = DW_CFA_offset_extended_sf;
11295 *eh++ = 65;
11296 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11297 *eh++ = DW_CFA_advance_loc + 4;
11298 *eh++ = DW_CFA_restore_extended;
11299 *eh++ = 65;
11300 stub_entry->group->eh_size = eh - base;
11301 }
11302 }
11303 return p;
11304 }
11305
11306 static Elf_Internal_Rela *
11307 get_relocs (asection *sec, int count)
11308 {
11309 Elf_Internal_Rela *relocs;
11310 struct bfd_elf_section_data *elfsec_data;
11311
11312 elfsec_data = elf_section_data (sec);
11313 relocs = elfsec_data->relocs;
11314 if (relocs == NULL)
11315 {
11316 bfd_size_type relsize;
11317 relsize = sec->reloc_count * sizeof (*relocs);
11318 relocs = bfd_alloc (sec->owner, relsize);
11319 if (relocs == NULL)
11320 return NULL;
11321 elfsec_data->relocs = relocs;
11322 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11323 sizeof (Elf_Internal_Shdr));
11324 if (elfsec_data->rela.hdr == NULL)
11325 return NULL;
11326 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11327 * sizeof (Elf64_External_Rela));
11328 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11329 sec->reloc_count = 0;
11330 }
11331 relocs += sec->reloc_count;
11332 sec->reloc_count += count;
11333 return relocs;
11334 }
11335
11336 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11337 forms, to the equivalent relocs against the global symbol given by
11338 STUB_ENTRY->H. */
11339
11340 static bool
11341 use_global_in_relocs (struct ppc_link_hash_table *htab,
11342 struct ppc_stub_hash_entry *stub_entry,
11343 Elf_Internal_Rela *r, unsigned int num_rel)
11344 {
11345 struct elf_link_hash_entry **hashes;
11346 unsigned long symndx;
11347 struct ppc_link_hash_entry *h;
11348 bfd_vma symval;
11349
11350 /* Relocs are always against symbols in their own object file. Fake
11351 up global sym hashes for the stub bfd (which has no symbols). */
11352 hashes = elf_sym_hashes (htab->params->stub_bfd);
11353 if (hashes == NULL)
11354 {
11355 bfd_size_type hsize;
11356
11357 /* When called the first time, stub_globals will contain the
11358 total number of symbols seen during stub sizing. After
11359 allocating, stub_globals is used as an index to fill the
11360 hashes array. */
11361 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11362 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11363 if (hashes == NULL)
11364 return false;
11365 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11366 htab->stub_globals = 1;
11367 }
11368 symndx = htab->stub_globals++;
11369 h = stub_entry->h;
11370 hashes[symndx] = &h->elf;
11371 if (h->oh != NULL && h->oh->is_func)
11372 h = ppc_follow_link (h->oh);
11373 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11374 || h->elf.root.type == bfd_link_hash_defweak);
11375 symval = defined_sym_val (&h->elf);
11376 while (num_rel-- != 0)
11377 {
11378 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11379 if (h->elf.root.u.def.section != stub_entry->target_section)
11380 {
11381 /* H is an opd symbol. The addend must be zero, and the
11382 branch reloc is the only one we can convert. */
11383 r->r_addend = 0;
11384 break;
11385 }
11386 else
11387 r->r_addend -= symval;
11388 --r;
11389 }
11390 return true;
11391 }
11392
11393 static bfd_vma
11394 get_r2off (struct bfd_link_info *info,
11395 struct ppc_stub_hash_entry *stub_entry)
11396 {
11397 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11398 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11399
11400 if (r2off == 0)
11401 {
11402 /* Support linking -R objects. Get the toc pointer from the
11403 opd entry. */
11404 char buf[8];
11405 if (!htab->opd_abi)
11406 return r2off;
11407 asection *opd = stub_entry->h->elf.root.u.def.section;
11408 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11409
11410 if (strcmp (opd->name, ".opd") != 0
11411 || opd->reloc_count != 0)
11412 {
11413 info->callbacks->einfo
11414 (_("%P: cannot find opd entry toc for `%pT'\n"),
11415 stub_entry->h->elf.root.root.string);
11416 bfd_set_error (bfd_error_bad_value);
11417 return (bfd_vma) -1;
11418 }
11419 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11420 return (bfd_vma) -1;
11421 r2off = bfd_get_64 (opd->owner, buf);
11422 r2off -= elf_gp (info->output_bfd);
11423 }
11424 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11425 return r2off;
11426 }
11427
11428 static bool
11429 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11430 {
11431 struct ppc_stub_hash_entry *stub_entry;
11432 struct ppc_branch_hash_entry *br_entry;
11433 struct bfd_link_info *info;
11434 struct ppc_link_hash_table *htab;
11435 bfd *obfd;
11436 bfd_byte *loc;
11437 bfd_byte *p, *relp;
11438 bfd_vma targ, off;
11439 Elf_Internal_Rela *r;
11440 asection *plt;
11441 int num_rel;
11442 int odd;
11443 bool is_tga;
11444
11445 /* Massage our args to the form they really have. */
11446 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11447 info = in_arg;
11448
11449 /* Fail if the target section could not be assigned to an output
11450 section. The user should fix his linker script. */
11451 if (stub_entry->target_section != NULL
11452 && stub_entry->target_section->output_section == NULL
11453 && info->non_contiguous_regions)
11454 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11455 "Retry without --enable-non-contiguous-regions.\n"),
11456 stub_entry->target_section);
11457
11458 /* Same for the group. */
11459 if (stub_entry->group->stub_sec != NULL
11460 && stub_entry->group->stub_sec->output_section == NULL
11461 && info->non_contiguous_regions)
11462 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11463 "output section. Retry without "
11464 "--enable-non-contiguous-regions.\n"),
11465 stub_entry->group->stub_sec,
11466 stub_entry->target_section);
11467
11468 htab = ppc_hash_table (info);
11469 if (htab == NULL)
11470 return false;
11471
11472 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11473 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11474
11475 htab->stub_count[stub_entry->stub_type - 1] += 1;
11476 switch (stub_entry->stub_type)
11477 {
11478 case ppc_stub_long_branch:
11479 case ppc_stub_long_branch_r2off:
11480 /* Branches are relative. This is where we are going to. */
11481 targ = (stub_entry->target_value
11482 + stub_entry->target_section->output_offset
11483 + stub_entry->target_section->output_section->vma);
11484 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11485
11486 /* And this is where we are coming from. */
11487 off = (stub_entry->stub_offset
11488 + stub_entry->group->stub_sec->output_offset
11489 + stub_entry->group->stub_sec->output_section->vma);
11490 off = targ - off;
11491
11492 p = loc;
11493 obfd = htab->params->stub_bfd;
11494 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
11495 {
11496 bfd_vma r2off = get_r2off (info, stub_entry);
11497
11498 if (r2off == (bfd_vma) -1)
11499 {
11500 htab->stub_error = true;
11501 return false;
11502 }
11503 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11504 p += 4;
11505 if (PPC_HA (r2off) != 0)
11506 {
11507 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11508 p += 4;
11509 }
11510 if (PPC_LO (r2off) != 0)
11511 {
11512 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11513 p += 4;
11514 }
11515 off -= p - loc;
11516 }
11517 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11518 p += 4;
11519
11520 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11521 {
11522 _bfd_error_handler
11523 (_("long branch stub `%s' offset overflow"),
11524 stub_entry->root.string);
11525 htab->stub_error = true;
11526 return false;
11527 }
11528
11529 if (info->emitrelocations)
11530 {
11531 r = get_relocs (stub_entry->group->stub_sec, 1);
11532 if (r == NULL)
11533 return false;
11534 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11535 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11536 r->r_addend = targ;
11537 if (stub_entry->h != NULL
11538 && !use_global_in_relocs (htab, stub_entry, r, 1))
11539 return false;
11540 }
11541 break;
11542
11543 case ppc_stub_plt_branch:
11544 case ppc_stub_plt_branch_r2off:
11545 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11546 stub_entry->root.string + 9,
11547 false, false);
11548 if (br_entry == NULL)
11549 {
11550 _bfd_error_handler (_("can't find branch stub `%s'"),
11551 stub_entry->root.string);
11552 htab->stub_error = true;
11553 return false;
11554 }
11555
11556 targ = (stub_entry->target_value
11557 + stub_entry->target_section->output_offset
11558 + stub_entry->target_section->output_section->vma);
11559 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11560 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11561
11562 bfd_put_64 (htab->brlt->owner, targ,
11563 htab->brlt->contents + br_entry->offset);
11564
11565 if (br_entry->iter == htab->stub_iteration)
11566 {
11567 br_entry->iter = 0;
11568
11569 if (htab->relbrlt != NULL)
11570 {
11571 /* Create a reloc for the branch lookup table entry. */
11572 Elf_Internal_Rela rela;
11573 bfd_byte *rl;
11574
11575 rela.r_offset = (br_entry->offset
11576 + htab->brlt->output_offset
11577 + htab->brlt->output_section->vma);
11578 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11579 rela.r_addend = targ;
11580
11581 rl = htab->relbrlt->contents;
11582 rl += (htab->relbrlt->reloc_count++
11583 * sizeof (Elf64_External_Rela));
11584 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11585 }
11586 else if (info->emitrelocations)
11587 {
11588 r = get_relocs (htab->brlt, 1);
11589 if (r == NULL)
11590 return false;
11591 /* brlt, being SEC_LINKER_CREATED does not go through the
11592 normal reloc processing. Symbols and offsets are not
11593 translated from input file to output file form, so
11594 set up the offset per the output file. */
11595 r->r_offset = (br_entry->offset
11596 + htab->brlt->output_offset
11597 + htab->brlt->output_section->vma);
11598 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11599 r->r_addend = targ;
11600 }
11601 }
11602
11603 targ = (br_entry->offset
11604 + htab->brlt->output_offset
11605 + htab->brlt->output_section->vma);
11606
11607 off = (elf_gp (info->output_bfd)
11608 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11609 off = targ - off;
11610
11611 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11612 {
11613 info->callbacks->einfo
11614 (_("%P: linkage table error against `%pT'\n"),
11615 stub_entry->root.string);
11616 bfd_set_error (bfd_error_bad_value);
11617 htab->stub_error = true;
11618 return false;
11619 }
11620
11621 if (info->emitrelocations)
11622 {
11623 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11624 if (r == NULL)
11625 return false;
11626 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11627 if (bfd_big_endian (info->output_bfd))
11628 r[0].r_offset += 2;
11629 if (stub_entry->stub_type == ppc_stub_plt_branch_r2off)
11630 r[0].r_offset += 4;
11631 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11632 r[0].r_addend = targ;
11633 if (PPC_HA (off) != 0)
11634 {
11635 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11636 r[1].r_offset = r[0].r_offset + 4;
11637 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11638 r[1].r_addend = r[0].r_addend;
11639 }
11640 }
11641
11642 p = loc;
11643 obfd = htab->params->stub_bfd;
11644 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
11645 {
11646 if (PPC_HA (off) != 0)
11647 {
11648 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11649 p += 4;
11650 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11651 }
11652 else
11653 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11654 }
11655 else
11656 {
11657 bfd_vma r2off = get_r2off (info, stub_entry);
11658
11659 if (r2off == (bfd_vma) -1)
11660 {
11661 htab->stub_error = true;
11662 return false;
11663 }
11664
11665 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11666 p += 4;
11667 if (PPC_HA (off) != 0)
11668 {
11669 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11670 p += 4;
11671 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11672 }
11673 else
11674 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11675
11676 if (PPC_HA (r2off) != 0)
11677 {
11678 p += 4;
11679 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11680 }
11681 if (PPC_LO (r2off) != 0)
11682 {
11683 p += 4;
11684 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11685 }
11686 }
11687 p += 4;
11688 bfd_put_32 (obfd, MTCTR_R12, p);
11689 p += 4;
11690 bfd_put_32 (obfd, BCTR, p);
11691 p += 4;
11692 break;
11693
11694 case ppc_stub_long_branch_notoc:
11695 case ppc_stub_long_branch_both:
11696 case ppc_stub_plt_branch_notoc:
11697 case ppc_stub_plt_branch_both:
11698 case ppc_stub_plt_call_notoc:
11699 case ppc_stub_plt_call_both:
11700 p = loc;
11701 off = (stub_entry->stub_offset
11702 + stub_entry->group->stub_sec->output_offset
11703 + stub_entry->group->stub_sec->output_section->vma);
11704 obfd = htab->params->stub_bfd;
11705 is_tga = ((stub_entry->stub_type == ppc_stub_plt_call_notoc
11706 || stub_entry->stub_type == ppc_stub_plt_call_both)
11707 && stub_entry->h != NULL
11708 && is_tls_get_addr (&stub_entry->h->elf, htab)
11709 && htab->params->tls_get_addr_opt);
11710 if (is_tga)
11711 {
11712 p = build_tls_get_addr_head (htab, stub_entry, p);
11713 off += p - loc;
11714 }
11715 if (stub_entry->stub_type == ppc_stub_long_branch_both
11716 || stub_entry->stub_type == ppc_stub_plt_branch_both
11717 || stub_entry->stub_type == ppc_stub_plt_call_both)
11718 {
11719 off += 4;
11720 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11721 p += 4;
11722 }
11723 if (stub_entry->stub_type >= ppc_stub_plt_call_notoc)
11724 {
11725 targ = stub_entry->plt_ent->plt.offset & ~1;
11726 if (targ >= (bfd_vma) -2)
11727 abort ();
11728
11729 plt = htab->elf.splt;
11730 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11731 {
11732 if (stub_entry->symtype == STT_GNU_IFUNC)
11733 plt = htab->elf.iplt;
11734 else
11735 plt = htab->pltlocal;
11736 }
11737 targ += plt->output_offset + plt->output_section->vma;
11738 }
11739 else
11740 targ = (stub_entry->target_value
11741 + stub_entry->target_section->output_offset
11742 + stub_entry->target_section->output_section->vma);
11743 odd = off & 4;
11744 off = targ - off;
11745
11746 relp = p;
11747 num_rel = 0;
11748 if (htab->params->power10_stubs != 0)
11749 {
11750 bool load = stub_entry->stub_type >= ppc_stub_plt_call_notoc;
11751 p = build_power10_offset (obfd, p, off, odd, load);
11752 }
11753 else
11754 {
11755 if (htab->glink_eh_frame != NULL
11756 && htab->glink_eh_frame->size != 0)
11757 {
11758 bfd_byte *base, *eh;
11759 unsigned int lr_used, delta;
11760
11761 base = (htab->glink_eh_frame->contents
11762 + stub_entry->group->eh_base + 17);
11763 eh = base + stub_entry->group->eh_size;
11764 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11765 delta = lr_used - stub_entry->group->lr_restore;
11766 stub_entry->group->lr_restore = lr_used + 8;
11767 eh = eh_advance (htab->elf.dynobj, eh, delta);
11768 *eh++ = DW_CFA_register;
11769 *eh++ = 65;
11770 *eh++ = 12;
11771 *eh++ = DW_CFA_advance_loc + 2;
11772 *eh++ = DW_CFA_restore_extended;
11773 *eh++ = 65;
11774 stub_entry->group->eh_size = eh - base;
11775 }
11776
11777 /* The notoc stubs calculate their target (either a PLT entry or
11778 the global entry point of a function) relative to the PC
11779 returned by the "bcl" two instructions past the start of the
11780 sequence emitted by build_offset. The offset is therefore 8
11781 less than calculated from the start of the sequence. */
11782 off -= 8;
11783 p = build_offset (obfd, p, off,
11784 stub_entry->stub_type >= ppc_stub_plt_call_notoc);
11785 }
11786
11787 if (stub_entry->stub_type <= ppc_stub_long_branch_both)
11788 {
11789 bfd_vma from;
11790 num_rel = 1;
11791 from = (stub_entry->stub_offset
11792 + stub_entry->group->stub_sec->output_offset
11793 + stub_entry->group->stub_sec->output_section->vma
11794 + (p - loc));
11795 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
11796 }
11797 else
11798 {
11799 bfd_put_32 (obfd, MTCTR_R12, p);
11800 p += 4;
11801 bfd_put_32 (obfd, BCTR, p);
11802 }
11803 p += 4;
11804
11805 if (is_tga)
11806 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11807
11808 if (info->emitrelocations)
11809 {
11810 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11811 if (htab->params->power10_stubs != 0)
11812 num_rel += num_relocs_for_power10_offset (off, odd);
11813 else
11814 {
11815 num_rel += num_relocs_for_offset (off);
11816 roff += 16;
11817 }
11818 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11819 if (r == NULL)
11820 return false;
11821 if (htab->params->power10_stubs != 0)
11822 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
11823 else
11824 r = emit_relocs_for_offset (info, r, roff, targ, off);
11825 if (stub_entry->stub_type == ppc_stub_long_branch_notoc
11826 || stub_entry->stub_type == ppc_stub_long_branch_both)
11827 {
11828 ++r;
11829 roff = p - 4 - stub_entry->group->stub_sec->contents;
11830 r->r_offset = roff;
11831 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11832 r->r_addend = targ;
11833 if (stub_entry->h != NULL
11834 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11835 return false;
11836 }
11837 }
11838 break;
11839
11840 case ppc_stub_plt_call:
11841 case ppc_stub_plt_call_r2save:
11842 if (stub_entry->h != NULL
11843 && stub_entry->h->is_func_descriptor
11844 && stub_entry->h->oh != NULL)
11845 {
11846 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11847
11848 /* If the old-ABI "dot-symbol" is undefined make it weak so
11849 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11850 if (fh->elf.root.type == bfd_link_hash_undefined
11851 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11852 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11853 fh->elf.root.type = bfd_link_hash_undefweak;
11854 }
11855
11856 /* Now build the stub. */
11857 targ = stub_entry->plt_ent->plt.offset & ~1;
11858 if (targ >= (bfd_vma) -2)
11859 abort ();
11860
11861 plt = htab->elf.splt;
11862 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11863 {
11864 if (stub_entry->symtype == STT_GNU_IFUNC)
11865 plt = htab->elf.iplt;
11866 else
11867 plt = htab->pltlocal;
11868 }
11869 targ += plt->output_offset + plt->output_section->vma;
11870
11871 off = (elf_gp (info->output_bfd)
11872 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11873 off = targ - off;
11874
11875 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11876 {
11877 info->callbacks->einfo
11878 /* xgettext:c-format */
11879 (_("%P: linkage table error against `%pT'\n"),
11880 stub_entry->h != NULL
11881 ? stub_entry->h->elf.root.root.string
11882 : "<local sym>");
11883 bfd_set_error (bfd_error_bad_value);
11884 htab->stub_error = true;
11885 return false;
11886 }
11887
11888 r = NULL;
11889 if (info->emitrelocations)
11890 {
11891 r = get_relocs (stub_entry->group->stub_sec,
11892 ((PPC_HA (off) != 0)
11893 + (htab->opd_abi
11894 ? 2 + (htab->params->plt_static_chain
11895 && PPC_HA (off + 16) == PPC_HA (off))
11896 : 1)));
11897 if (r == NULL)
11898 return false;
11899 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11900 if (bfd_big_endian (info->output_bfd))
11901 r[0].r_offset += 2;
11902 r[0].r_addend = targ;
11903 }
11904 p = loc;
11905 obfd = htab->params->stub_bfd;
11906 is_tga = (stub_entry->h != NULL
11907 && is_tls_get_addr (&stub_entry->h->elf, htab)
11908 && htab->params->tls_get_addr_opt);
11909 if (is_tga)
11910 {
11911 p = build_tls_get_addr_head (htab, stub_entry, p);
11912 if (r != NULL)
11913 r[0].r_offset += p - loc;
11914 }
11915 p = build_plt_stub (htab, stub_entry, p, off, r);
11916 if (is_tga)
11917 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11918 break;
11919
11920 case ppc_stub_save_res:
11921 return true;
11922
11923 default:
11924 BFD_FAIL ();
11925 return false;
11926 }
11927
11928 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
11929
11930 if (htab->params->emit_stub_syms)
11931 {
11932 struct elf_link_hash_entry *h;
11933 size_t len1, len2;
11934 char *name;
11935 const char *const stub_str[] = { "long_branch",
11936 "long_branch",
11937 "long_branch",
11938 "long_branch",
11939 "plt_branch",
11940 "plt_branch",
11941 "plt_branch",
11942 "plt_branch",
11943 "plt_call",
11944 "plt_call",
11945 "plt_call",
11946 "plt_call" };
11947
11948 len1 = strlen (stub_str[stub_entry->stub_type - 1]);
11949 len2 = strlen (stub_entry->root.string);
11950 name = bfd_malloc (len1 + len2 + 2);
11951 if (name == NULL)
11952 return false;
11953 memcpy (name, stub_entry->root.string, 9);
11954 memcpy (name + 9, stub_str[stub_entry->stub_type - 1], len1);
11955 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
11956 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
11957 if (h == NULL)
11958 return false;
11959 if (h->root.type == bfd_link_hash_new)
11960 {
11961 h->root.type = bfd_link_hash_defined;
11962 h->root.u.def.section = stub_entry->group->stub_sec;
11963 h->root.u.def.value = stub_entry->stub_offset;
11964 h->ref_regular = 1;
11965 h->def_regular = 1;
11966 h->ref_regular_nonweak = 1;
11967 h->forced_local = 1;
11968 h->non_elf = 0;
11969 h->root.linker_def = 1;
11970 }
11971 }
11972
11973 return true;
11974 }
11975
11976 /* As above, but don't actually build the stub. Just bump offset so
11977 we know stub section sizes, and select plt_branch stubs where
11978 long_branch stubs won't do. */
11979
11980 static bool
11981 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11982 {
11983 struct ppc_stub_hash_entry *stub_entry;
11984 struct bfd_link_info *info;
11985 struct ppc_link_hash_table *htab;
11986 asection *plt;
11987 bfd_vma targ, off, r2off;
11988 unsigned int size, extra, lr_used, delta, odd;
11989
11990 /* Massage our args to the form they really have. */
11991 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11992 info = in_arg;
11993
11994 htab = ppc_hash_table (info);
11995 if (htab == NULL)
11996 return false;
11997
11998 /* Fail if the target section could not be assigned to an output
11999 section. The user should fix his linker script. */
12000 if (stub_entry->target_section != NULL
12001 && stub_entry->target_section->output_section == NULL
12002 && info->non_contiguous_regions)
12003 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12004 "Retry without --enable-non-contiguous-regions.\n"),
12005 stub_entry->target_section);
12006
12007 /* Same for the group. */
12008 if (stub_entry->group->stub_sec != NULL
12009 && stub_entry->group->stub_sec->output_section == NULL
12010 && info->non_contiguous_regions)
12011 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12012 "output section. Retry without "
12013 "--enable-non-contiguous-regions.\n"),
12014 stub_entry->group->stub_sec,
12015 stub_entry->target_section);
12016
12017 /* Make a note of the offset within the stubs for this entry. */
12018 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12019
12020 if (stub_entry->h != NULL
12021 && stub_entry->h->save_res
12022 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12023 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12024 {
12025 /* Don't make stubs to out-of-line register save/restore
12026 functions. Instead, emit copies of the functions. */
12027 stub_entry->group->needs_save_res = 1;
12028 stub_entry->stub_type = ppc_stub_save_res;
12029 return true;
12030 }
12031
12032 switch (stub_entry->stub_type)
12033 {
12034 case ppc_stub_plt_branch:
12035 case ppc_stub_plt_branch_r2off:
12036 /* Reset the stub type from the plt branch variant in case we now
12037 can reach with a shorter stub. */
12038 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12039 /* Fall through. */
12040 case ppc_stub_long_branch:
12041 case ppc_stub_long_branch_r2off:
12042 targ = (stub_entry->target_value
12043 + stub_entry->target_section->output_offset
12044 + stub_entry->target_section->output_section->vma);
12045 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12046 off = (stub_entry->stub_offset
12047 + stub_entry->group->stub_sec->output_offset
12048 + stub_entry->group->stub_sec->output_section->vma);
12049
12050 size = 4;
12051 r2off = 0;
12052 if (stub_entry->stub_type == ppc_stub_long_branch_r2off)
12053 {
12054 r2off = get_r2off (info, stub_entry);
12055 if (r2off == (bfd_vma) -1)
12056 {
12057 htab->stub_error = true;
12058 return false;
12059 }
12060 size = 8;
12061 if (PPC_HA (r2off) != 0)
12062 size += 4;
12063 if (PPC_LO (r2off) != 0)
12064 size += 4;
12065 off += size - 4;
12066 }
12067 off = targ - off;
12068
12069 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12070 Do the same for -R objects without function descriptors. */
12071 if ((stub_entry->stub_type == ppc_stub_long_branch_r2off
12072 && r2off == 0
12073 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12074 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12075 {
12076 struct ppc_branch_hash_entry *br_entry;
12077
12078 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12079 stub_entry->root.string + 9,
12080 true, false);
12081 if (br_entry == NULL)
12082 {
12083 _bfd_error_handler (_("can't build branch stub `%s'"),
12084 stub_entry->root.string);
12085 htab->stub_error = true;
12086 return false;
12087 }
12088
12089 if (br_entry->iter != htab->stub_iteration)
12090 {
12091 br_entry->iter = htab->stub_iteration;
12092 br_entry->offset = htab->brlt->size;
12093 htab->brlt->size += 8;
12094
12095 if (htab->relbrlt != NULL)
12096 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12097 else if (info->emitrelocations)
12098 {
12099 htab->brlt->reloc_count += 1;
12100 htab->brlt->flags |= SEC_RELOC;
12101 }
12102 }
12103
12104 targ = (br_entry->offset
12105 + htab->brlt->output_offset
12106 + htab->brlt->output_section->vma);
12107 off = (elf_gp (info->output_bfd)
12108 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12109 off = targ - off;
12110
12111 if (info->emitrelocations)
12112 {
12113 stub_entry->group->stub_sec->reloc_count
12114 += 1 + (PPC_HA (off) != 0);
12115 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12116 }
12117
12118 stub_entry->stub_type += ppc_stub_plt_branch - ppc_stub_long_branch;
12119 if (stub_entry->stub_type != ppc_stub_plt_branch_r2off)
12120 {
12121 size = 12;
12122 if (PPC_HA (off) != 0)
12123 size = 16;
12124 }
12125 else
12126 {
12127 size = 16;
12128 if (PPC_HA (off) != 0)
12129 size += 4;
12130
12131 if (PPC_HA (r2off) != 0)
12132 size += 4;
12133 if (PPC_LO (r2off) != 0)
12134 size += 4;
12135 }
12136 }
12137 else if (info->emitrelocations)
12138 {
12139 stub_entry->group->stub_sec->reloc_count += 1;
12140 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12141 }
12142 break;
12143
12144 case ppc_stub_plt_branch_notoc:
12145 case ppc_stub_plt_branch_both:
12146 stub_entry->stub_type += ppc_stub_long_branch - ppc_stub_plt_branch;
12147 /* Fall through. */
12148 case ppc_stub_long_branch_notoc:
12149 case ppc_stub_long_branch_both:
12150 off = (stub_entry->stub_offset
12151 + stub_entry->group->stub_sec->output_offset
12152 + stub_entry->group->stub_sec->output_section->vma);
12153 size = 0;
12154 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12155 size = 4;
12156 off += size;
12157 targ = (stub_entry->target_value
12158 + stub_entry->target_section->output_offset
12159 + stub_entry->target_section->output_section->vma);
12160 odd = off & 4;
12161 off = targ - off;
12162
12163 if (info->emitrelocations)
12164 {
12165 unsigned int num_rel;
12166 if (htab->params->power10_stubs != 0)
12167 num_rel = num_relocs_for_power10_offset (off, odd);
12168 else
12169 num_rel = num_relocs_for_offset (off - 8);
12170 stub_entry->group->stub_sec->reloc_count += num_rel;
12171 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12172 }
12173
12174 if (htab->params->power10_stubs != 0)
12175 extra = size_power10_offset (off, odd);
12176 else
12177 extra = size_offset (off - 8);
12178 /* Include branch insn plus those in the offset sequence. */
12179 size += 4 + extra;
12180 /* The branch insn is at the end, or "extra" bytes along. So
12181 its offset will be "extra" bytes less that that already
12182 calculated. */
12183 off -= extra;
12184
12185 if (htab->params->power10_stubs == 0)
12186 {
12187 /* After the bcl, lr has been modified so we need to emit
12188 .eh_frame info saying the return address is in r12. */
12189 lr_used = stub_entry->stub_offset + 8;
12190 if (stub_entry->stub_type == ppc_stub_long_branch_both)
12191 lr_used += 4;
12192 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12193 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12194 DW_CFA_restore_extended 65. */
12195 delta = lr_used - stub_entry->group->lr_restore;
12196 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12197 stub_entry->group->lr_restore = lr_used + 8;
12198 }
12199
12200 /* If the branch can't reach, use a plt_branch. */
12201 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12202 {
12203 stub_entry->stub_type += (ppc_stub_plt_branch_notoc
12204 - ppc_stub_long_branch_notoc);
12205 size += 4;
12206 }
12207 else if (info->emitrelocations)
12208 stub_entry->group->stub_sec->reloc_count +=1;
12209 break;
12210
12211 case ppc_stub_plt_call_notoc:
12212 case ppc_stub_plt_call_both:
12213 lr_used = 0;
12214 if (stub_entry->h != NULL
12215 && is_tls_get_addr (&stub_entry->h->elf, htab)
12216 && htab->params->tls_get_addr_opt)
12217 {
12218 lr_used += 7 * 4;
12219 if (!htab->params->no_tls_get_addr_regsave)
12220 lr_used += 11 * 4;
12221 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12222 lr_used += 2 * 4;
12223 }
12224 if (stub_entry->stub_type == ppc_stub_plt_call_both)
12225 lr_used += 4;
12226 targ = stub_entry->plt_ent->plt.offset & ~1;
12227 if (targ >= (bfd_vma) -2)
12228 abort ();
12229
12230 plt = htab->elf.splt;
12231 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12232 {
12233 if (stub_entry->symtype == STT_GNU_IFUNC)
12234 plt = htab->elf.iplt;
12235 else
12236 plt = htab->pltlocal;
12237 }
12238 targ += plt->output_offset + plt->output_section->vma;
12239 off = (stub_entry->stub_offset
12240 + stub_entry->group->stub_sec->output_offset
12241 + stub_entry->group->stub_sec->output_section->vma
12242 + lr_used);
12243 odd = off & 4;
12244 off = targ - off;
12245
12246 if (htab->params->plt_stub_align != 0)
12247 {
12248 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
12249
12250 stub_entry->group->stub_sec->size += pad;
12251 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12252 off -= pad;
12253 odd ^= pad & 4;
12254 }
12255
12256 if (info->emitrelocations)
12257 {
12258 unsigned int num_rel;
12259 if (htab->params->power10_stubs != 0)
12260 num_rel = num_relocs_for_power10_offset (off, odd);
12261 else
12262 num_rel = num_relocs_for_offset (off - 8);
12263 stub_entry->group->stub_sec->reloc_count += num_rel;
12264 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12265 }
12266
12267 size = plt_stub_size (htab, stub_entry, off, odd);
12268
12269 if (htab->params->power10_stubs == 0)
12270 {
12271 /* After the bcl, lr has been modified so we need to emit
12272 .eh_frame info saying the return address is in r12. */
12273 lr_used += stub_entry->stub_offset + 8;
12274 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12275 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12276 DW_CFA_restore_extended 65. */
12277 delta = lr_used - stub_entry->group->lr_restore;
12278 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12279 stub_entry->group->lr_restore = lr_used + 8;
12280 }
12281 if ((stub_entry->stub_type == ppc_stub_plt_call_notoc
12282 || stub_entry->stub_type == ppc_stub_plt_call_both)
12283 && stub_entry->h != NULL
12284 && is_tls_get_addr (&stub_entry->h->elf, htab)
12285 && htab->params->tls_get_addr_opt)
12286 {
12287 if (!htab->params->no_tls_get_addr_regsave)
12288 {
12289 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12290 delta = cfa_updt - stub_entry->group->lr_restore;
12291 stub_entry->group->eh_size += eh_advance_size (delta);
12292 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12293 stub_entry->group->lr_restore
12294 = stub_entry->stub_offset + size - 4;
12295 }
12296 else if (stub_entry->stub_type == ppc_stub_plt_call_both)
12297 {
12298 lr_used = stub_entry->stub_offset + size - 20;
12299 delta = lr_used - stub_entry->group->lr_restore;
12300 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12301 stub_entry->group->lr_restore
12302 = stub_entry->stub_offset + size - 4;
12303 }
12304 }
12305 break;
12306
12307 case ppc_stub_plt_call:
12308 case ppc_stub_plt_call_r2save:
12309 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12310 if (targ >= (bfd_vma) -2)
12311 abort ();
12312 plt = htab->elf.splt;
12313 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12314 {
12315 if (stub_entry->symtype == STT_GNU_IFUNC)
12316 plt = htab->elf.iplt;
12317 else
12318 plt = htab->pltlocal;
12319 }
12320 targ += plt->output_offset + plt->output_section->vma;
12321
12322 off = (elf_gp (info->output_bfd)
12323 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12324 off = targ - off;
12325
12326 if (htab->params->plt_stub_align != 0)
12327 {
12328 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
12329
12330 stub_entry->group->stub_sec->size += pad;
12331 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12332 }
12333
12334 if (info->emitrelocations)
12335 {
12336 stub_entry->group->stub_sec->reloc_count
12337 += ((PPC_HA (off) != 0)
12338 + (htab->opd_abi
12339 ? 2 + (htab->params->plt_static_chain
12340 && PPC_HA (off + 16) == PPC_HA (off))
12341 : 1));
12342 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12343 }
12344
12345 size = plt_stub_size (htab, stub_entry, off, 0);
12346
12347 if (stub_entry->h != NULL
12348 && is_tls_get_addr (&stub_entry->h->elf, htab)
12349 && htab->params->tls_get_addr_opt
12350 && stub_entry->stub_type == ppc_stub_plt_call_r2save)
12351 {
12352 if (!htab->params->no_tls_get_addr_regsave)
12353 {
12354 /* Adjustments to r1 need to be described. */
12355 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12356 delta = cfa_updt - stub_entry->group->lr_restore;
12357 stub_entry->group->eh_size += eh_advance_size (delta);
12358 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12359 }
12360 else
12361 {
12362 lr_used = stub_entry->stub_offset + size - 20;
12363 /* The eh_frame info will consist of a DW_CFA_advance_loc
12364 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12365 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12366 delta = lr_used - stub_entry->group->lr_restore;
12367 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12368 }
12369 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
12370 }
12371 break;
12372
12373 default:
12374 BFD_FAIL ();
12375 return false;
12376 }
12377
12378 stub_entry->group->stub_sec->size += size;
12379 return true;
12380 }
12381
12382 /* Set up various things so that we can make a list of input sections
12383 for each output section included in the link. Returns -1 on error,
12384 0 when no stubs will be needed, and 1 on success. */
12385
12386 int
12387 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12388 {
12389 unsigned int id;
12390 size_t amt;
12391 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12392
12393 if (htab == NULL)
12394 return -1;
12395
12396 htab->sec_info_arr_size = _bfd_section_id;
12397 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12398 htab->sec_info = bfd_zmalloc (amt);
12399 if (htab->sec_info == NULL)
12400 return -1;
12401
12402 /* Set toc_off for com, und, abs and ind sections. */
12403 for (id = 0; id < 3; id++)
12404 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12405
12406 return 1;
12407 }
12408
12409 /* Set up for first pass at multitoc partitioning. */
12410
12411 void
12412 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12413 {
12414 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12415
12416 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12417 htab->toc_bfd = NULL;
12418 htab->toc_first_sec = NULL;
12419 }
12420
12421 /* The linker repeatedly calls this function for each TOC input section
12422 and linker generated GOT section. Group input bfds such that the toc
12423 within a group is less than 64k in size. */
12424
12425 bool
12426 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12427 {
12428 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12429 bfd_vma addr, off, limit;
12430
12431 if (htab == NULL)
12432 return false;
12433
12434 if (!htab->second_toc_pass)
12435 {
12436 /* Keep track of the first .toc or .got section for this input bfd. */
12437 bool new_bfd = htab->toc_bfd != isec->owner;
12438
12439 if (new_bfd)
12440 {
12441 htab->toc_bfd = isec->owner;
12442 htab->toc_first_sec = isec;
12443 }
12444
12445 addr = isec->output_offset + isec->output_section->vma;
12446 off = addr - htab->toc_curr;
12447 limit = 0x80008000;
12448 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12449 limit = 0x10000;
12450 if (off + isec->size > limit)
12451 {
12452 addr = (htab->toc_first_sec->output_offset
12453 + htab->toc_first_sec->output_section->vma);
12454 htab->toc_curr = addr;
12455 htab->toc_curr &= -TOC_BASE_ALIGN;
12456 }
12457
12458 /* toc_curr is the base address of this toc group. Set elf_gp
12459 for the input section to be the offset relative to the
12460 output toc base plus 0x8000. Making the input elf_gp an
12461 offset allows us to move the toc as a whole without
12462 recalculating input elf_gp. */
12463 off = htab->toc_curr - elf_gp (info->output_bfd);
12464 off += TOC_BASE_OFF;
12465
12466 /* Die if someone uses a linker script that doesn't keep input
12467 file .toc and .got together. */
12468 if (new_bfd
12469 && elf_gp (isec->owner) != 0
12470 && elf_gp (isec->owner) != off)
12471 return false;
12472
12473 elf_gp (isec->owner) = off;
12474 return true;
12475 }
12476
12477 /* During the second pass toc_first_sec points to the start of
12478 a toc group, and toc_curr is used to track the old elf_gp.
12479 We use toc_bfd to ensure we only look at each bfd once. */
12480 if (htab->toc_bfd == isec->owner)
12481 return true;
12482 htab->toc_bfd = isec->owner;
12483
12484 if (htab->toc_first_sec == NULL
12485 || htab->toc_curr != elf_gp (isec->owner))
12486 {
12487 htab->toc_curr = elf_gp (isec->owner);
12488 htab->toc_first_sec = isec;
12489 }
12490 addr = (htab->toc_first_sec->output_offset
12491 + htab->toc_first_sec->output_section->vma);
12492 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12493 elf_gp (isec->owner) = off;
12494
12495 return true;
12496 }
12497
12498 /* Called via elf_link_hash_traverse to merge GOT entries for global
12499 symbol H. */
12500
12501 static bool
12502 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12503 {
12504 if (h->root.type == bfd_link_hash_indirect)
12505 return true;
12506
12507 merge_got_entries (&h->got.glist);
12508
12509 return true;
12510 }
12511
12512 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12513 symbol H. */
12514
12515 static bool
12516 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12517 {
12518 struct got_entry *gent;
12519
12520 if (h->root.type == bfd_link_hash_indirect)
12521 return true;
12522
12523 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12524 if (!gent->is_indirect)
12525 allocate_got (h, (struct bfd_link_info *) inf, gent);
12526 return true;
12527 }
12528
12529 /* Called on the first multitoc pass after the last call to
12530 ppc64_elf_next_toc_section. This function removes duplicate GOT
12531 entries. */
12532
12533 bool
12534 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12535 {
12536 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12537 struct bfd *ibfd, *ibfd2;
12538 bool done_something;
12539
12540 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12541
12542 if (!htab->do_multi_toc)
12543 return false;
12544
12545 /* Merge global sym got entries within a toc group. */
12546 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12547
12548 /* And tlsld_got. */
12549 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12550 {
12551 struct got_entry *ent, *ent2;
12552
12553 if (!is_ppc64_elf (ibfd))
12554 continue;
12555
12556 ent = ppc64_tlsld_got (ibfd);
12557 if (!ent->is_indirect
12558 && ent->got.offset != (bfd_vma) -1)
12559 {
12560 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12561 {
12562 if (!is_ppc64_elf (ibfd2))
12563 continue;
12564
12565 ent2 = ppc64_tlsld_got (ibfd2);
12566 if (!ent2->is_indirect
12567 && ent2->got.offset != (bfd_vma) -1
12568 && elf_gp (ibfd2) == elf_gp (ibfd))
12569 {
12570 ent2->is_indirect = true;
12571 ent2->got.ent = ent;
12572 }
12573 }
12574 }
12575 }
12576
12577 /* Zap sizes of got sections. */
12578 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12579 htab->elf.irelplt->size -= htab->got_reli_size;
12580 htab->got_reli_size = 0;
12581
12582 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12583 {
12584 asection *got, *relgot;
12585
12586 if (!is_ppc64_elf (ibfd))
12587 continue;
12588
12589 got = ppc64_elf_tdata (ibfd)->got;
12590 if (got != NULL)
12591 {
12592 got->rawsize = got->size;
12593 got->size = 0;
12594 relgot = ppc64_elf_tdata (ibfd)->relgot;
12595 relgot->rawsize = relgot->size;
12596 relgot->size = 0;
12597 }
12598 }
12599
12600 /* Now reallocate the got, local syms first. We don't need to
12601 allocate section contents again since we never increase size. */
12602 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12603 {
12604 struct got_entry **lgot_ents;
12605 struct got_entry **end_lgot_ents;
12606 struct plt_entry **local_plt;
12607 struct plt_entry **end_local_plt;
12608 unsigned char *lgot_masks;
12609 bfd_size_type locsymcount;
12610 Elf_Internal_Shdr *symtab_hdr;
12611 asection *s;
12612
12613 if (!is_ppc64_elf (ibfd))
12614 continue;
12615
12616 lgot_ents = elf_local_got_ents (ibfd);
12617 if (!lgot_ents)
12618 continue;
12619
12620 symtab_hdr = &elf_symtab_hdr (ibfd);
12621 locsymcount = symtab_hdr->sh_info;
12622 end_lgot_ents = lgot_ents + locsymcount;
12623 local_plt = (struct plt_entry **) end_lgot_ents;
12624 end_local_plt = local_plt + locsymcount;
12625 lgot_masks = (unsigned char *) end_local_plt;
12626 s = ppc64_elf_tdata (ibfd)->got;
12627 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12628 {
12629 struct got_entry *ent;
12630
12631 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12632 {
12633 unsigned int ent_size = 8;
12634 unsigned int rel_size = sizeof (Elf64_External_Rela);
12635
12636 ent->got.offset = s->size;
12637 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12638 {
12639 ent_size *= 2;
12640 rel_size *= 2;
12641 }
12642 s->size += ent_size;
12643 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12644 {
12645 htab->elf.irelplt->size += rel_size;
12646 htab->got_reli_size += rel_size;
12647 }
12648 else if (bfd_link_pic (info)
12649 && !(ent->tls_type != 0
12650 && bfd_link_executable (info)))
12651 {
12652 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12653 srel->size += rel_size;
12654 }
12655 }
12656 }
12657 }
12658
12659 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12660
12661 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12662 {
12663 struct got_entry *ent;
12664
12665 if (!is_ppc64_elf (ibfd))
12666 continue;
12667
12668 ent = ppc64_tlsld_got (ibfd);
12669 if (!ent->is_indirect
12670 && ent->got.offset != (bfd_vma) -1)
12671 {
12672 asection *s = ppc64_elf_tdata (ibfd)->got;
12673 ent->got.offset = s->size;
12674 s->size += 16;
12675 if (bfd_link_dll (info))
12676 {
12677 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12678 srel->size += sizeof (Elf64_External_Rela);
12679 }
12680 }
12681 }
12682
12683 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12684 if (!done_something)
12685 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12686 {
12687 asection *got;
12688
12689 if (!is_ppc64_elf (ibfd))
12690 continue;
12691
12692 got = ppc64_elf_tdata (ibfd)->got;
12693 if (got != NULL)
12694 {
12695 done_something = got->rawsize != got->size;
12696 if (done_something)
12697 break;
12698 }
12699 }
12700
12701 if (done_something)
12702 (*htab->params->layout_sections_again) ();
12703
12704 /* Set up for second pass over toc sections to recalculate elf_gp
12705 on input sections. */
12706 htab->toc_bfd = NULL;
12707 htab->toc_first_sec = NULL;
12708 htab->second_toc_pass = true;
12709 return done_something;
12710 }
12711
12712 /* Called after second pass of multitoc partitioning. */
12713
12714 void
12715 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12716 {
12717 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12718
12719 /* After the second pass, toc_curr tracks the TOC offset used
12720 for code sections below in ppc64_elf_next_input_section. */
12721 htab->toc_curr = TOC_BASE_OFF;
12722 }
12723
12724 /* No toc references were found in ISEC. If the code in ISEC makes no
12725 calls, then there's no need to use toc adjusting stubs when branching
12726 into ISEC. Actually, indirect calls from ISEC are OK as they will
12727 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12728 needed, and 2 if a cyclical call-graph was found but no other reason
12729 for a stub was detected. If called from the top level, a return of
12730 2 means the same as a return of 0. */
12731
12732 static int
12733 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12734 {
12735 int ret;
12736
12737 /* Mark this section as checked. */
12738 isec->call_check_done = 1;
12739
12740 /* We know none of our code bearing sections will need toc stubs. */
12741 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12742 return 0;
12743
12744 if (isec->size == 0)
12745 return 0;
12746
12747 if (isec->output_section == NULL)
12748 return 0;
12749
12750 ret = 0;
12751 if (isec->reloc_count != 0)
12752 {
12753 Elf_Internal_Rela *relstart, *rel;
12754 Elf_Internal_Sym *local_syms;
12755 struct ppc_link_hash_table *htab;
12756
12757 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12758 info->keep_memory);
12759 if (relstart == NULL)
12760 return -1;
12761
12762 /* Look for branches to outside of this section. */
12763 local_syms = NULL;
12764 htab = ppc_hash_table (info);
12765 if (htab == NULL)
12766 return -1;
12767
12768 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12769 {
12770 enum elf_ppc64_reloc_type r_type;
12771 unsigned long r_symndx;
12772 struct elf_link_hash_entry *h;
12773 struct ppc_link_hash_entry *eh;
12774 Elf_Internal_Sym *sym;
12775 asection *sym_sec;
12776 struct _opd_sec_data *opd;
12777 bfd_vma sym_value;
12778 bfd_vma dest;
12779
12780 r_type = ELF64_R_TYPE (rel->r_info);
12781 if (r_type != R_PPC64_REL24
12782 && r_type != R_PPC64_REL24_NOTOC
12783 && r_type != R_PPC64_REL14
12784 && r_type != R_PPC64_REL14_BRTAKEN
12785 && r_type != R_PPC64_REL14_BRNTAKEN
12786 && r_type != R_PPC64_PLTCALL
12787 && r_type != R_PPC64_PLTCALL_NOTOC)
12788 continue;
12789
12790 r_symndx = ELF64_R_SYM (rel->r_info);
12791 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12792 isec->owner))
12793 {
12794 ret = -1;
12795 break;
12796 }
12797
12798 /* Calls to dynamic lib functions go through a plt call stub
12799 that uses r2. */
12800 eh = ppc_elf_hash_entry (h);
12801 if (eh != NULL
12802 && (eh->elf.plt.plist != NULL
12803 || (eh->oh != NULL
12804 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12805 {
12806 ret = 1;
12807 break;
12808 }
12809
12810 if (sym_sec == NULL)
12811 /* Ignore other undefined symbols. */
12812 continue;
12813
12814 /* Assume branches to other sections not included in the
12815 link need stubs too, to cover -R and absolute syms. */
12816 if (sym_sec->output_section == NULL)
12817 {
12818 ret = 1;
12819 break;
12820 }
12821
12822 if (h == NULL)
12823 sym_value = sym->st_value;
12824 else
12825 {
12826 if (h->root.type != bfd_link_hash_defined
12827 && h->root.type != bfd_link_hash_defweak)
12828 abort ();
12829 sym_value = h->root.u.def.value;
12830 }
12831 sym_value += rel->r_addend;
12832
12833 /* If this branch reloc uses an opd sym, find the code section. */
12834 opd = get_opd_info (sym_sec);
12835 if (opd != NULL)
12836 {
12837 if (h == NULL && opd->adjust != NULL)
12838 {
12839 long adjust;
12840
12841 adjust = opd->adjust[OPD_NDX (sym_value)];
12842 if (adjust == -1)
12843 /* Assume deleted functions won't ever be called. */
12844 continue;
12845 sym_value += adjust;
12846 }
12847
12848 dest = opd_entry_value (sym_sec, sym_value,
12849 &sym_sec, NULL, false);
12850 if (dest == (bfd_vma) -1)
12851 continue;
12852 }
12853 else
12854 dest = (sym_value
12855 + sym_sec->output_offset
12856 + sym_sec->output_section->vma);
12857
12858 /* Ignore branch to self. */
12859 if (sym_sec == isec)
12860 continue;
12861
12862 /* If the called function uses the toc, we need a stub. */
12863 if (sym_sec->has_toc_reloc
12864 || sym_sec->makes_toc_func_call)
12865 {
12866 ret = 1;
12867 break;
12868 }
12869
12870 /* Assume any branch that needs a long branch stub might in fact
12871 need a plt_branch stub. A plt_branch stub uses r2. */
12872 else if (dest - (isec->output_offset
12873 + isec->output_section->vma
12874 + rel->r_offset) + (1 << 25)
12875 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12876 ? h->other
12877 : sym->st_other))
12878 {
12879 ret = 1;
12880 break;
12881 }
12882
12883 /* If calling back to a section in the process of being
12884 tested, we can't say for sure that no toc adjusting stubs
12885 are needed, so don't return zero. */
12886 else if (sym_sec->call_check_in_progress)
12887 ret = 2;
12888
12889 /* Branches to another section that itself doesn't have any TOC
12890 references are OK. Recursively call ourselves to check. */
12891 else if (!sym_sec->call_check_done)
12892 {
12893 int recur;
12894
12895 /* Mark current section as indeterminate, so that other
12896 sections that call back to current won't be marked as
12897 known. */
12898 isec->call_check_in_progress = 1;
12899 recur = toc_adjusting_stub_needed (info, sym_sec);
12900 isec->call_check_in_progress = 0;
12901
12902 if (recur != 0)
12903 {
12904 ret = recur;
12905 if (recur != 2)
12906 break;
12907 }
12908 }
12909 }
12910
12911 if (elf_symtab_hdr (isec->owner).contents
12912 != (unsigned char *) local_syms)
12913 free (local_syms);
12914 if (elf_section_data (isec)->relocs != relstart)
12915 free (relstart);
12916 }
12917
12918 if ((ret & 1) == 0
12919 && isec->map_head.s != NULL
12920 && (strcmp (isec->output_section->name, ".init") == 0
12921 || strcmp (isec->output_section->name, ".fini") == 0))
12922 {
12923 if (isec->map_head.s->has_toc_reloc
12924 || isec->map_head.s->makes_toc_func_call)
12925 ret = 1;
12926 else if (!isec->map_head.s->call_check_done)
12927 {
12928 int recur;
12929 isec->call_check_in_progress = 1;
12930 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
12931 isec->call_check_in_progress = 0;
12932 if (recur != 0)
12933 ret = recur;
12934 }
12935 }
12936
12937 if (ret == 1)
12938 isec->makes_toc_func_call = 1;
12939
12940 return ret;
12941 }
12942
12943 /* The linker repeatedly calls this function for each input section,
12944 in the order that input sections are linked into output sections.
12945 Build lists of input sections to determine groupings between which
12946 we may insert linker stubs. */
12947
12948 bool
12949 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
12950 {
12951 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12952
12953 if (htab == NULL)
12954 return false;
12955
12956 if ((isec->output_section->flags & SEC_CODE) != 0
12957 && isec->output_section->id < htab->sec_info_arr_size)
12958 {
12959 /* This happens to make the list in reverse order,
12960 which is what we want. */
12961 htab->sec_info[isec->id].u.list
12962 = htab->sec_info[isec->output_section->id].u.list;
12963 htab->sec_info[isec->output_section->id].u.list = isec;
12964 }
12965
12966 if (htab->multi_toc_needed)
12967 {
12968 /* Analyse sections that aren't already flagged as needing a
12969 valid toc pointer. Exclude .fixup for the linux kernel.
12970 .fixup contains branches, but only back to the function that
12971 hit an exception. */
12972 if (!(isec->has_toc_reloc
12973 || (isec->flags & SEC_CODE) == 0
12974 || strcmp (isec->name, ".fixup") == 0
12975 || isec->call_check_done))
12976 {
12977 if (toc_adjusting_stub_needed (info, isec) < 0)
12978 return false;
12979 }
12980 /* Make all sections use the TOC assigned for this object file.
12981 This will be wrong for pasted sections; We fix that in
12982 check_pasted_section(). */
12983 if (elf_gp (isec->owner) != 0)
12984 htab->toc_curr = elf_gp (isec->owner);
12985 }
12986
12987 htab->sec_info[isec->id].toc_off = htab->toc_curr;
12988 return true;
12989 }
12990
12991 /* Check that all .init and .fini sections use the same toc, if they
12992 have toc relocs. */
12993
12994 static bool
12995 check_pasted_section (struct bfd_link_info *info, const char *name)
12996 {
12997 asection *o = bfd_get_section_by_name (info->output_bfd, name);
12998
12999 if (o != NULL)
13000 {
13001 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13002 bfd_vma toc_off = 0;
13003 asection *i;
13004
13005 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13006 if (i->has_toc_reloc)
13007 {
13008 if (toc_off == 0)
13009 toc_off = htab->sec_info[i->id].toc_off;
13010 else if (toc_off != htab->sec_info[i->id].toc_off)
13011 return false;
13012 }
13013
13014 if (toc_off == 0)
13015 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13016 if (i->makes_toc_func_call)
13017 {
13018 toc_off = htab->sec_info[i->id].toc_off;
13019 break;
13020 }
13021
13022 /* Make sure the whole pasted function uses the same toc offset. */
13023 if (toc_off != 0)
13024 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13025 htab->sec_info[i->id].toc_off = toc_off;
13026 }
13027 return true;
13028 }
13029
13030 bool
13031 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13032 {
13033 return (check_pasted_section (info, ".init")
13034 & check_pasted_section (info, ".fini"));
13035 }
13036
13037 /* See whether we can group stub sections together. Grouping stub
13038 sections may result in fewer stubs. More importantly, we need to
13039 put all .init* and .fini* stubs at the beginning of the .init or
13040 .fini output sections respectively, because glibc splits the
13041 _init and _fini functions into multiple parts. Putting a stub in
13042 the middle of a function is not a good idea. */
13043
13044 static bool
13045 group_sections (struct bfd_link_info *info,
13046 bfd_size_type stub_group_size,
13047 bool stubs_always_before_branch)
13048 {
13049 struct ppc_link_hash_table *htab;
13050 asection *osec;
13051 bool suppress_size_errors;
13052
13053 htab = ppc_hash_table (info);
13054 if (htab == NULL)
13055 return false;
13056
13057 suppress_size_errors = false;
13058 if (stub_group_size == 1)
13059 {
13060 /* Default values. */
13061 if (stubs_always_before_branch)
13062 stub_group_size = 0x1e00000;
13063 else
13064 stub_group_size = 0x1c00000;
13065 suppress_size_errors = true;
13066 }
13067
13068 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13069 {
13070 asection *tail;
13071
13072 if (osec->id >= htab->sec_info_arr_size)
13073 continue;
13074
13075 tail = htab->sec_info[osec->id].u.list;
13076 while (tail != NULL)
13077 {
13078 asection *curr;
13079 asection *prev;
13080 bfd_size_type total;
13081 bool big_sec;
13082 bfd_vma curr_toc;
13083 struct map_stub *group;
13084 bfd_size_type group_size;
13085
13086 curr = tail;
13087 total = tail->size;
13088 group_size = (ppc64_elf_section_data (tail) != NULL
13089 && ppc64_elf_section_data (tail)->has_14bit_branch
13090 ? stub_group_size >> 10 : stub_group_size);
13091
13092 big_sec = total > group_size;
13093 if (big_sec && !suppress_size_errors)
13094 /* xgettext:c-format */
13095 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13096 tail->owner, tail);
13097 curr_toc = htab->sec_info[tail->id].toc_off;
13098
13099 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13100 && ((total += curr->output_offset - prev->output_offset)
13101 < (ppc64_elf_section_data (prev) != NULL
13102 && ppc64_elf_section_data (prev)->has_14bit_branch
13103 ? (group_size = stub_group_size >> 10) : group_size))
13104 && htab->sec_info[prev->id].toc_off == curr_toc)
13105 curr = prev;
13106
13107 /* OK, the size from the start of CURR to the end is less
13108 than group_size and thus can be handled by one stub
13109 section. (or the tail section is itself larger than
13110 group_size, in which case we may be toast.) We should
13111 really be keeping track of the total size of stubs added
13112 here, as stubs contribute to the final output section
13113 size. That's a little tricky, and this way will only
13114 break if stubs added make the total size more than 2^25,
13115 ie. for the default stub_group_size, if stubs total more
13116 than 2097152 bytes, or nearly 75000 plt call stubs. */
13117 group = bfd_alloc (curr->owner, sizeof (*group));
13118 if (group == NULL)
13119 return false;
13120 group->link_sec = curr;
13121 group->stub_sec = NULL;
13122 group->needs_save_res = 0;
13123 group->lr_restore = 0;
13124 group->eh_size = 0;
13125 group->eh_base = 0;
13126 group->next = htab->group;
13127 htab->group = group;
13128 do
13129 {
13130 prev = htab->sec_info[tail->id].u.list;
13131 /* Set up this stub group. */
13132 htab->sec_info[tail->id].u.group = group;
13133 }
13134 while (tail != curr && (tail = prev) != NULL);
13135
13136 /* But wait, there's more! Input sections up to group_size
13137 bytes before the stub section can be handled by it too.
13138 Don't do this if we have a really large section after the
13139 stubs, as adding more stubs increases the chance that
13140 branches may not reach into the stub section. */
13141 if (!stubs_always_before_branch && !big_sec)
13142 {
13143 total = 0;
13144 while (prev != NULL
13145 && ((total += tail->output_offset - prev->output_offset)
13146 < (ppc64_elf_section_data (prev) != NULL
13147 && ppc64_elf_section_data (prev)->has_14bit_branch
13148 ? (group_size = stub_group_size >> 10)
13149 : group_size))
13150 && htab->sec_info[prev->id].toc_off == curr_toc)
13151 {
13152 tail = prev;
13153 prev = htab->sec_info[tail->id].u.list;
13154 htab->sec_info[tail->id].u.group = group;
13155 }
13156 }
13157 tail = prev;
13158 }
13159 }
13160 return true;
13161 }
13162
13163 static const unsigned char glink_eh_frame_cie[] =
13164 {
13165 0, 0, 0, 16, /* length. */
13166 0, 0, 0, 0, /* id. */
13167 1, /* CIE version. */
13168 'z', 'R', 0, /* Augmentation string. */
13169 4, /* Code alignment. */
13170 0x78, /* Data alignment. */
13171 65, /* RA reg. */
13172 1, /* Augmentation size. */
13173 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13174 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13175 };
13176
13177 /* Stripping output sections is normally done before dynamic section
13178 symbols have been allocated. This function is called later, and
13179 handles cases like htab->brlt which is mapped to its own output
13180 section. */
13181
13182 static void
13183 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13184 {
13185 if (isec->size == 0
13186 && isec->output_section->size == 0
13187 && !(isec->output_section->flags & SEC_KEEP)
13188 && !bfd_section_removed_from_list (info->output_bfd,
13189 isec->output_section)
13190 && elf_section_data (isec->output_section)->dynindx == 0)
13191 {
13192 isec->output_section->flags |= SEC_EXCLUDE;
13193 bfd_section_list_remove (info->output_bfd, isec->output_section);
13194 info->output_bfd->section_count--;
13195 }
13196 }
13197
13198 /* Determine and set the size of the stub section for a final link.
13199
13200 The basic idea here is to examine all the relocations looking for
13201 PC-relative calls to a target that is unreachable with a "bl"
13202 instruction. */
13203
13204 bool
13205 ppc64_elf_size_stubs (struct bfd_link_info *info)
13206 {
13207 bfd_size_type stub_group_size;
13208 bool stubs_always_before_branch;
13209 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13210
13211 if (htab == NULL)
13212 return false;
13213
13214 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13215 htab->params->power10_stubs = 0;
13216
13217 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13218 htab->params->plt_thread_safe = 1;
13219 if (!htab->opd_abi)
13220 htab->params->plt_thread_safe = 0;
13221 else if (htab->params->plt_thread_safe == -1)
13222 {
13223 static const char *const thread_starter[] =
13224 {
13225 "pthread_create",
13226 /* libstdc++ */
13227 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13228 /* librt */
13229 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13230 "mq_notify", "create_timer",
13231 /* libanl */
13232 "getaddrinfo_a",
13233 /* libgomp */
13234 "GOMP_parallel",
13235 "GOMP_parallel_start",
13236 "GOMP_parallel_loop_static",
13237 "GOMP_parallel_loop_static_start",
13238 "GOMP_parallel_loop_dynamic",
13239 "GOMP_parallel_loop_dynamic_start",
13240 "GOMP_parallel_loop_guided",
13241 "GOMP_parallel_loop_guided_start",
13242 "GOMP_parallel_loop_runtime",
13243 "GOMP_parallel_loop_runtime_start",
13244 "GOMP_parallel_sections",
13245 "GOMP_parallel_sections_start",
13246 /* libgo */
13247 "__go_go",
13248 };
13249 unsigned i;
13250
13251 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13252 {
13253 struct elf_link_hash_entry *h;
13254 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13255 false, false, true);
13256 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13257 if (htab->params->plt_thread_safe)
13258 break;
13259 }
13260 }
13261 stubs_always_before_branch = htab->params->group_size < 0;
13262 if (htab->params->group_size < 0)
13263 stub_group_size = -htab->params->group_size;
13264 else
13265 stub_group_size = htab->params->group_size;
13266
13267 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13268 return false;
13269
13270 htab->tga_group = NULL;
13271 if (!htab->params->no_tls_get_addr_regsave
13272 && htab->tga_desc_fd != NULL
13273 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13274 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13275 && htab->tls_get_addr_fd != NULL
13276 && is_static_defined (&htab->tls_get_addr_fd->elf))
13277 {
13278 asection *sym_sec, *code_sec, *stub_sec;
13279 bfd_vma sym_value;
13280 struct _opd_sec_data *opd;
13281
13282 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13283 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13284 code_sec = sym_sec;
13285 opd = get_opd_info (sym_sec);
13286 if (opd != NULL)
13287 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13288 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13289 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13290 htab->tga_group->link_sec);
13291 if (stub_sec == NULL)
13292 return false;
13293 htab->tga_group->stub_sec = stub_sec;
13294
13295 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13296 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13297 htab->tga_desc_fd->elf.root.u.def.value = 0;
13298 htab->tga_desc_fd->elf.type = STT_FUNC;
13299 htab->tga_desc_fd->elf.def_regular = 1;
13300 htab->tga_desc_fd->elf.non_elf = 0;
13301 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13302 }
13303
13304 #define STUB_SHRINK_ITER 20
13305 /* Loop until no stubs added. After iteration 20 of this loop we may
13306 exit on a stub section shrinking. This is to break out of a
13307 pathological case where adding stubs on one iteration decreases
13308 section gaps (perhaps due to alignment), which then requires
13309 fewer or smaller stubs on the next iteration. */
13310
13311 while (1)
13312 {
13313 bfd *input_bfd;
13314 unsigned int bfd_indx;
13315 struct map_stub *group;
13316
13317 htab->stub_iteration += 1;
13318
13319 for (input_bfd = info->input_bfds, bfd_indx = 0;
13320 input_bfd != NULL;
13321 input_bfd = input_bfd->link.next, bfd_indx++)
13322 {
13323 Elf_Internal_Shdr *symtab_hdr;
13324 asection *section;
13325 Elf_Internal_Sym *local_syms = NULL;
13326
13327 if (!is_ppc64_elf (input_bfd))
13328 continue;
13329
13330 /* We'll need the symbol table in a second. */
13331 symtab_hdr = &elf_symtab_hdr (input_bfd);
13332 if (symtab_hdr->sh_info == 0)
13333 continue;
13334
13335 /* Walk over each section attached to the input bfd. */
13336 for (section = input_bfd->sections;
13337 section != NULL;
13338 section = section->next)
13339 {
13340 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13341
13342 /* If there aren't any relocs, then there's nothing more
13343 to do. */
13344 if ((section->flags & SEC_RELOC) == 0
13345 || (section->flags & SEC_ALLOC) == 0
13346 || (section->flags & SEC_LOAD) == 0
13347 || (section->flags & SEC_CODE) == 0
13348 || section->reloc_count == 0)
13349 continue;
13350
13351 /* If this section is a link-once section that will be
13352 discarded, then don't create any stubs. */
13353 if (section->output_section == NULL
13354 || section->output_section->owner != info->output_bfd)
13355 continue;
13356
13357 /* Get the relocs. */
13358 internal_relocs
13359 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13360 info->keep_memory);
13361 if (internal_relocs == NULL)
13362 goto error_ret_free_local;
13363
13364 /* Now examine each relocation. */
13365 irela = internal_relocs;
13366 irelaend = irela + section->reloc_count;
13367 for (; irela < irelaend; irela++)
13368 {
13369 enum elf_ppc64_reloc_type r_type;
13370 unsigned int r_indx;
13371 enum ppc_stub_type stub_type;
13372 struct ppc_stub_hash_entry *stub_entry;
13373 asection *sym_sec, *code_sec;
13374 bfd_vma sym_value, code_value;
13375 bfd_vma destination;
13376 unsigned long local_off;
13377 bool ok_dest;
13378 struct ppc_link_hash_entry *hash;
13379 struct ppc_link_hash_entry *fdh;
13380 struct elf_link_hash_entry *h;
13381 Elf_Internal_Sym *sym;
13382 char *stub_name;
13383 const asection *id_sec;
13384 struct _opd_sec_data *opd;
13385 struct plt_entry *plt_ent;
13386
13387 r_type = ELF64_R_TYPE (irela->r_info);
13388 r_indx = ELF64_R_SYM (irela->r_info);
13389
13390 if (r_type >= R_PPC64_max)
13391 {
13392 bfd_set_error (bfd_error_bad_value);
13393 goto error_ret_free_internal;
13394 }
13395
13396 /* Only look for stubs on branch instructions. */
13397 if (r_type != R_PPC64_REL24
13398 && r_type != R_PPC64_REL24_NOTOC
13399 && r_type != R_PPC64_REL14
13400 && r_type != R_PPC64_REL14_BRTAKEN
13401 && r_type != R_PPC64_REL14_BRNTAKEN)
13402 continue;
13403
13404 /* Now determine the call target, its name, value,
13405 section. */
13406 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13407 r_indx, input_bfd))
13408 goto error_ret_free_internal;
13409 hash = ppc_elf_hash_entry (h);
13410
13411 ok_dest = false;
13412 fdh = NULL;
13413 sym_value = 0;
13414 if (hash == NULL)
13415 {
13416 sym_value = sym->st_value;
13417 if (sym_sec != NULL
13418 && sym_sec->output_section != NULL)
13419 ok_dest = true;
13420 }
13421 else if (hash->elf.root.type == bfd_link_hash_defined
13422 || hash->elf.root.type == bfd_link_hash_defweak)
13423 {
13424 sym_value = hash->elf.root.u.def.value;
13425 if (sym_sec->output_section != NULL)
13426 ok_dest = true;
13427 }
13428 else if (hash->elf.root.type == bfd_link_hash_undefweak
13429 || hash->elf.root.type == bfd_link_hash_undefined)
13430 {
13431 /* Recognise an old ABI func code entry sym, and
13432 use the func descriptor sym instead if it is
13433 defined. */
13434 if (hash->elf.root.root.string[0] == '.'
13435 && hash->oh != NULL)
13436 {
13437 fdh = ppc_follow_link (hash->oh);
13438 if (fdh->elf.root.type == bfd_link_hash_defined
13439 || fdh->elf.root.type == bfd_link_hash_defweak)
13440 {
13441 sym_sec = fdh->elf.root.u.def.section;
13442 sym_value = fdh->elf.root.u.def.value;
13443 if (sym_sec->output_section != NULL)
13444 ok_dest = true;
13445 }
13446 else
13447 fdh = NULL;
13448 }
13449 }
13450 else
13451 {
13452 bfd_set_error (bfd_error_bad_value);
13453 goto error_ret_free_internal;
13454 }
13455
13456 destination = 0;
13457 local_off = 0;
13458 if (ok_dest)
13459 {
13460 sym_value += irela->r_addend;
13461 destination = (sym_value
13462 + sym_sec->output_offset
13463 + sym_sec->output_section->vma);
13464 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13465 ? hash->elf.other
13466 : sym->st_other);
13467 }
13468
13469 code_sec = sym_sec;
13470 code_value = sym_value;
13471 opd = get_opd_info (sym_sec);
13472 if (opd != NULL)
13473 {
13474 bfd_vma dest;
13475
13476 if (hash == NULL && opd->adjust != NULL)
13477 {
13478 long adjust = opd->adjust[OPD_NDX (sym_value)];
13479 if (adjust == -1)
13480 continue;
13481 code_value += adjust;
13482 sym_value += adjust;
13483 }
13484 dest = opd_entry_value (sym_sec, sym_value,
13485 &code_sec, &code_value, false);
13486 if (dest != (bfd_vma) -1)
13487 {
13488 destination = dest;
13489 if (fdh != NULL)
13490 {
13491 /* Fixup old ABI sym to point at code
13492 entry. */
13493 hash->elf.root.type = bfd_link_hash_defweak;
13494 hash->elf.root.u.def.section = code_sec;
13495 hash->elf.root.u.def.value = code_value;
13496 }
13497 }
13498 }
13499
13500 /* Determine what (if any) linker stub is needed. */
13501 plt_ent = NULL;
13502 stub_type = ppc_type_of_stub (section, irela, &hash,
13503 &plt_ent, destination,
13504 local_off);
13505
13506 if (r_type == R_PPC64_REL24_NOTOC)
13507 {
13508 if (stub_type == ppc_stub_plt_call)
13509 stub_type = ppc_stub_plt_call_notoc;
13510 else if (stub_type == ppc_stub_long_branch
13511 || (code_sec != NULL
13512 && code_sec->output_section != NULL
13513 && (((hash ? hash->elf.other : sym->st_other)
13514 & STO_PPC64_LOCAL_MASK)
13515 > 1 << STO_PPC64_LOCAL_BIT)))
13516 stub_type = ppc_stub_long_branch_notoc;
13517 }
13518 else if (stub_type != ppc_stub_plt_call)
13519 {
13520 /* Check whether we need a TOC adjusting stub.
13521 Since the linker pastes together pieces from
13522 different object files when creating the
13523 _init and _fini functions, it may be that a
13524 call to what looks like a local sym is in
13525 fact a call needing a TOC adjustment. */
13526 if ((code_sec != NULL
13527 && code_sec->output_section != NULL
13528 && (code_sec->has_toc_reloc
13529 || code_sec->makes_toc_func_call)
13530 && (htab->sec_info[code_sec->id].toc_off
13531 != htab->sec_info[section->id].toc_off))
13532 || (((hash ? hash->elf.other : sym->st_other)
13533 & STO_PPC64_LOCAL_MASK)
13534 == 1 << STO_PPC64_LOCAL_BIT))
13535 stub_type = ppc_stub_long_branch_r2off;
13536 }
13537
13538 if (stub_type == ppc_stub_none)
13539 continue;
13540
13541 /* __tls_get_addr calls might be eliminated. */
13542 if (stub_type != ppc_stub_plt_call
13543 && stub_type != ppc_stub_plt_call_notoc
13544 && hash != NULL
13545 && is_tls_get_addr (&hash->elf, htab)
13546 && section->has_tls_reloc
13547 && irela != internal_relocs)
13548 {
13549 /* Get tls info. */
13550 unsigned char *tls_mask;
13551
13552 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13553 irela - 1, input_bfd))
13554 goto error_ret_free_internal;
13555 if ((*tls_mask & TLS_TLS) != 0
13556 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
13557 continue;
13558 }
13559
13560 if (stub_type == ppc_stub_plt_call)
13561 {
13562 if (!htab->opd_abi
13563 && htab->params->plt_localentry0 != 0
13564 && is_elfv2_localentry0 (&hash->elf))
13565 htab->has_plt_localentry0 = 1;
13566 else if (irela + 1 < irelaend
13567 && irela[1].r_offset == irela->r_offset + 4
13568 && (ELF64_R_TYPE (irela[1].r_info)
13569 == R_PPC64_TOCSAVE))
13570 {
13571 if (!tocsave_find (htab, INSERT,
13572 &local_syms, irela + 1, input_bfd))
13573 goto error_ret_free_internal;
13574 }
13575 else
13576 stub_type = ppc_stub_plt_call_r2save;
13577 }
13578
13579 /* Support for grouping stub sections. */
13580 id_sec = htab->sec_info[section->id].u.group->link_sec;
13581
13582 /* Get the name of this stub. */
13583 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13584 if (!stub_name)
13585 goto error_ret_free_internal;
13586
13587 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13588 stub_name, false, false);
13589 if (stub_entry != NULL)
13590 {
13591 enum ppc_stub_type old_type;
13592
13593 /* A stub has already been created, but it may
13594 not be the required type. We shouldn't be
13595 transitioning from plt_call to long_branch
13596 stubs or vice versa, but we might be
13597 upgrading from plt_call to plt_call_r2save or
13598 from long_branch to long_branch_r2off. */
13599 free (stub_name);
13600 if (htab->params->power10_stubs == -1)
13601 {
13602 /* For --power10-stubs=auto, don't merge _notoc
13603 and other varieties of stubs. (The _both
13604 variety won't be created.) */
13605 bool notoc = r_type == R_PPC64_REL24_NOTOC;
13606 struct ppc_stub_hash_entry *alt_stub
13607 = select_alt_stub (stub_entry, notoc);
13608
13609 if (alt_stub == NULL)
13610 {
13611 alt_stub = (struct ppc_stub_hash_entry *)
13612 stub_hash_newfunc (NULL,
13613 &htab->stub_hash_table,
13614 stub_entry->root.string);
13615 if (alt_stub == NULL)
13616 {
13617 /* xgettext:c-format */
13618 _bfd_error_handler
13619 (_("%pB: cannot create stub entry %s"),
13620 section->owner, stub_entry->root.string);
13621 goto error_ret_free_internal;
13622 }
13623 *alt_stub = *stub_entry;
13624 stub_entry->root.next = &alt_stub->root;
13625 if (notoc)
13626 /* Sort notoc stubs first, for no good
13627 reason. */
13628 alt_stub = stub_entry;
13629 alt_stub->stub_type = stub_type;
13630 }
13631 stub_entry = alt_stub;
13632 }
13633 old_type = stub_entry->stub_type;
13634 switch (old_type)
13635 {
13636 default:
13637 abort ();
13638
13639 case ppc_stub_save_res:
13640 continue;
13641
13642 case ppc_stub_plt_call:
13643 case ppc_stub_plt_call_r2save:
13644 case ppc_stub_plt_call_notoc:
13645 case ppc_stub_plt_call_both:
13646 if (stub_type == ppc_stub_plt_call)
13647 continue;
13648 else if (stub_type == ppc_stub_plt_call_r2save)
13649 {
13650 if (old_type == ppc_stub_plt_call_notoc)
13651 stub_type = ppc_stub_plt_call_both;
13652 }
13653 else if (stub_type == ppc_stub_plt_call_notoc)
13654 {
13655 if (old_type == ppc_stub_plt_call_r2save)
13656 stub_type = ppc_stub_plt_call_both;
13657 }
13658 else
13659 abort ();
13660 break;
13661
13662 case ppc_stub_plt_branch:
13663 case ppc_stub_plt_branch_r2off:
13664 case ppc_stub_plt_branch_notoc:
13665 case ppc_stub_plt_branch_both:
13666 old_type += (ppc_stub_long_branch
13667 - ppc_stub_plt_branch);
13668 /* Fall through. */
13669 case ppc_stub_long_branch:
13670 case ppc_stub_long_branch_r2off:
13671 case ppc_stub_long_branch_notoc:
13672 case ppc_stub_long_branch_both:
13673 if (stub_type == ppc_stub_long_branch)
13674 continue;
13675 else if (stub_type == ppc_stub_long_branch_r2off)
13676 {
13677 if (old_type == ppc_stub_long_branch_notoc)
13678 stub_type = ppc_stub_long_branch_both;
13679 }
13680 else if (stub_type == ppc_stub_long_branch_notoc)
13681 {
13682 if (old_type == ppc_stub_long_branch_r2off)
13683 stub_type = ppc_stub_long_branch_both;
13684 }
13685 else
13686 abort ();
13687 break;
13688 }
13689 if (old_type < stub_type)
13690 stub_entry->stub_type = stub_type;
13691 continue;
13692 }
13693
13694 stub_entry = ppc_add_stub (stub_name, section, info);
13695 if (stub_entry == NULL)
13696 {
13697 free (stub_name);
13698 error_ret_free_internal:
13699 if (elf_section_data (section)->relocs == NULL)
13700 free (internal_relocs);
13701 error_ret_free_local:
13702 if (symtab_hdr->contents
13703 != (unsigned char *) local_syms)
13704 free (local_syms);
13705 return false;
13706 }
13707
13708 stub_entry->stub_type = stub_type;
13709 if (stub_type >= ppc_stub_plt_call
13710 && stub_type <= ppc_stub_plt_call_both)
13711 {
13712 stub_entry->target_value = sym_value;
13713 stub_entry->target_section = sym_sec;
13714 }
13715 else
13716 {
13717 stub_entry->target_value = code_value;
13718 stub_entry->target_section = code_sec;
13719 }
13720 stub_entry->h = hash;
13721 stub_entry->plt_ent = plt_ent;
13722 stub_entry->symtype
13723 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13724 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13725
13726 if (hash != NULL
13727 && (hash->elf.root.type == bfd_link_hash_defined
13728 || hash->elf.root.type == bfd_link_hash_defweak))
13729 htab->stub_globals += 1;
13730 }
13731
13732 /* We're done with the internal relocs, free them. */
13733 if (elf_section_data (section)->relocs != internal_relocs)
13734 free (internal_relocs);
13735 }
13736
13737 if (local_syms != NULL
13738 && symtab_hdr->contents != (unsigned char *) local_syms)
13739 {
13740 if (!info->keep_memory)
13741 free (local_syms);
13742 else
13743 symtab_hdr->contents = (unsigned char *) local_syms;
13744 }
13745 }
13746
13747 /* We may have added some stubs. Find out the new size of the
13748 stub sections. */
13749 for (group = htab->group; group != NULL; group = group->next)
13750 {
13751 group->lr_restore = 0;
13752 group->eh_size = 0;
13753 if (group->stub_sec != NULL)
13754 {
13755 asection *stub_sec = group->stub_sec;
13756
13757 if (htab->stub_iteration <= STUB_SHRINK_ITER
13758 || stub_sec->rawsize < stub_sec->size)
13759 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13760 stub_sec->rawsize = stub_sec->size;
13761 stub_sec->size = 0;
13762 stub_sec->reloc_count = 0;
13763 stub_sec->flags &= ~SEC_RELOC;
13764 }
13765 }
13766 if (htab->tga_group != NULL)
13767 {
13768 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13769 htab->tga_group->eh_size
13770 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13771 htab->tga_group->lr_restore = 23 * 4;
13772 htab->tga_group->stub_sec->size = 24 * 4;
13773 }
13774
13775 if (htab->stub_iteration <= STUB_SHRINK_ITER
13776 || htab->brlt->rawsize < htab->brlt->size)
13777 htab->brlt->rawsize = htab->brlt->size;
13778 htab->brlt->size = 0;
13779 htab->brlt->reloc_count = 0;
13780 htab->brlt->flags &= ~SEC_RELOC;
13781 if (htab->relbrlt != NULL)
13782 htab->relbrlt->size = 0;
13783
13784 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13785
13786 for (group = htab->group; group != NULL; group = group->next)
13787 if (group->needs_save_res)
13788 group->stub_sec->size += htab->sfpr->size;
13789
13790 if (info->emitrelocations
13791 && htab->glink != NULL && htab->glink->size != 0)
13792 {
13793 htab->glink->reloc_count = 1;
13794 htab->glink->flags |= SEC_RELOC;
13795 }
13796
13797 if (htab->glink_eh_frame != NULL
13798 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13799 && htab->glink_eh_frame->output_section->size > 8)
13800 {
13801 size_t size = 0, align = 4;
13802
13803 for (group = htab->group; group != NULL; group = group->next)
13804 if (group->eh_size != 0)
13805 size += (group->eh_size + 17 + align - 1) & -align;
13806 if (htab->glink != NULL && htab->glink->size != 0)
13807 size += (24 + align - 1) & -align;
13808 if (size != 0)
13809 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13810 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13811 size = (size + align - 1) & -align;
13812 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13813 htab->glink_eh_frame->size = size;
13814 }
13815
13816 if (htab->params->plt_stub_align != 0)
13817 for (group = htab->group; group != NULL; group = group->next)
13818 if (group->stub_sec != NULL)
13819 {
13820 int align = abs (htab->params->plt_stub_align);
13821 group->stub_sec->size
13822 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13823 }
13824
13825 for (group = htab->group; group != NULL; group = group->next)
13826 if (group->stub_sec != NULL
13827 && group->stub_sec->rawsize != group->stub_sec->size
13828 && (htab->stub_iteration <= STUB_SHRINK_ITER
13829 || group->stub_sec->rawsize < group->stub_sec->size))
13830 break;
13831
13832 if (group == NULL
13833 && (htab->brlt->rawsize == htab->brlt->size
13834 || (htab->stub_iteration > STUB_SHRINK_ITER
13835 && htab->brlt->rawsize > htab->brlt->size))
13836 && (htab->glink_eh_frame == NULL
13837 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13838 && (htab->tga_group == NULL
13839 || htab->stub_iteration > 1))
13840 break;
13841
13842 /* Ask the linker to do its stuff. */
13843 (*htab->params->layout_sections_again) ();
13844 }
13845
13846 if (htab->glink_eh_frame != NULL
13847 && htab->glink_eh_frame->size != 0)
13848 {
13849 bfd_vma val;
13850 bfd_byte *p, *last_fde;
13851 size_t last_fde_len, size, align, pad;
13852 struct map_stub *group;
13853
13854 /* It is necessary to at least have a rough outline of the
13855 linker generated CIEs and FDEs written before
13856 bfd_elf_discard_info is run, in order for these FDEs to be
13857 indexed in .eh_frame_hdr. */
13858 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13859 if (p == NULL)
13860 return false;
13861 htab->glink_eh_frame->contents = p;
13862 last_fde = p;
13863 align = 4;
13864
13865 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13866 /* CIE length (rewrite in case little-endian). */
13867 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13868 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13869 p += last_fde_len + 4;
13870
13871 for (group = htab->group; group != NULL; group = group->next)
13872 if (group->eh_size != 0)
13873 {
13874 group->eh_base = p - htab->glink_eh_frame->contents;
13875 last_fde = p;
13876 last_fde_len = ((group->eh_size + 17 + align - 1) & -align) - 4;
13877 /* FDE length. */
13878 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13879 p += 4;
13880 /* CIE pointer. */
13881 val = p - htab->glink_eh_frame->contents;
13882 bfd_put_32 (htab->elf.dynobj, val, p);
13883 p += 4;
13884 /* Offset to stub section, written later. */
13885 p += 4;
13886 /* stub section size. */
13887 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13888 p += 4;
13889 /* Augmentation. */
13890 p += 1;
13891 /* Make sure we don't have all nops. This is enough for
13892 elf-eh-frame.c to detect the last non-nop opcode. */
13893 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13894 p = last_fde + last_fde_len + 4;
13895 }
13896 if (htab->glink != NULL && htab->glink->size != 0)
13897 {
13898 last_fde = p;
13899 last_fde_len = ((24 + align - 1) & -align) - 4;
13900 /* FDE length. */
13901 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13902 p += 4;
13903 /* CIE pointer. */
13904 val = p - htab->glink_eh_frame->contents;
13905 bfd_put_32 (htab->elf.dynobj, val, p);
13906 p += 4;
13907 /* Offset to .glink, written later. */
13908 p += 4;
13909 /* .glink size. */
13910 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13911 p += 4;
13912 /* Augmentation. */
13913 p += 1;
13914
13915 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
13916 *p++ = DW_CFA_register;
13917 *p++ = 65;
13918 *p++ = htab->opd_abi ? 12 : 0;
13919 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
13920 *p++ = DW_CFA_restore_extended;
13921 *p++ = 65;
13922 p += ((24 + align - 1) & -align) - 24;
13923 }
13924 /* Subsume any padding into the last FDE if user .eh_frame
13925 sections are aligned more than glink_eh_frame. Otherwise any
13926 zero padding will be seen as a terminator. */
13927 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13928 size = p - htab->glink_eh_frame->contents;
13929 pad = ((size + align - 1) & -align) - size;
13930 htab->glink_eh_frame->size = size + pad;
13931 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13932 }
13933
13934 maybe_strip_output (info, htab->brlt);
13935 if (htab->relbrlt != NULL)
13936 maybe_strip_output (info, htab->relbrlt);
13937 if (htab->glink_eh_frame != NULL)
13938 maybe_strip_output (info, htab->glink_eh_frame);
13939
13940 return true;
13941 }
13942
13943 /* Called after we have determined section placement. If sections
13944 move, we'll be called again. Provide a value for TOCstart. */
13945
13946 bfd_vma
13947 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13948 {
13949 asection *s;
13950 bfd_vma TOCstart, adjust;
13951
13952 if (info != NULL)
13953 {
13954 struct elf_link_hash_entry *h;
13955 struct elf_link_hash_table *htab = elf_hash_table (info);
13956
13957 if (is_elf_hash_table (htab)
13958 && htab->hgot != NULL)
13959 h = htab->hgot;
13960 else
13961 {
13962 h = elf_link_hash_lookup (htab, ".TOC.", false, false, true);
13963 if (is_elf_hash_table (htab))
13964 htab->hgot = h;
13965 }
13966 if (h != NULL
13967 && h->root.type == bfd_link_hash_defined
13968 && !h->root.linker_def
13969 && (!is_elf_hash_table (htab)
13970 || h->def_regular))
13971 {
13972 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
13973 _bfd_set_gp_value (obfd, TOCstart);
13974 return TOCstart;
13975 }
13976 }
13977
13978 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
13979 order. The TOC starts where the first of these sections starts. */
13980 s = bfd_get_section_by_name (obfd, ".got");
13981 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13982 s = bfd_get_section_by_name (obfd, ".toc");
13983 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13984 s = bfd_get_section_by_name (obfd, ".tocbss");
13985 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13986 s = bfd_get_section_by_name (obfd, ".plt");
13987 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
13988 {
13989 /* This may happen for
13990 o references to TOC base (SYM@toc / TOC[tc0]) without a
13991 .toc directive
13992 o bad linker script
13993 o --gc-sections and empty TOC sections
13994
13995 FIXME: Warn user? */
13996
13997 /* Look for a likely section. We probably won't even be
13998 using TOCstart. */
13999 for (s = obfd->sections; s != NULL; s = s->next)
14000 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14001 | SEC_EXCLUDE))
14002 == (SEC_ALLOC | SEC_SMALL_DATA))
14003 break;
14004 if (s == NULL)
14005 for (s = obfd->sections; s != NULL; s = s->next)
14006 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14007 == (SEC_ALLOC | SEC_SMALL_DATA))
14008 break;
14009 if (s == NULL)
14010 for (s = obfd->sections; s != NULL; s = s->next)
14011 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14012 == SEC_ALLOC)
14013 break;
14014 if (s == NULL)
14015 for (s = obfd->sections; s != NULL; s = s->next)
14016 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14017 break;
14018 }
14019
14020 TOCstart = 0;
14021 if (s != NULL)
14022 TOCstart = s->output_section->vma + s->output_offset;
14023
14024 /* Force alignment. */
14025 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14026 TOCstart -= adjust;
14027 _bfd_set_gp_value (obfd, TOCstart);
14028
14029 if (info != NULL && s != NULL)
14030 {
14031 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14032
14033 if (htab != NULL)
14034 {
14035 if (htab->elf.hgot != NULL)
14036 {
14037 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14038 htab->elf.hgot->root.u.def.section = s;
14039 }
14040 }
14041 else
14042 {
14043 struct bfd_link_hash_entry *bh = NULL;
14044 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14045 s, TOC_BASE_OFF - adjust,
14046 NULL, false, false, &bh);
14047 }
14048 }
14049 return TOCstart;
14050 }
14051
14052 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14053 write out any global entry stubs, and PLT relocations. */
14054
14055 static bool
14056 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14057 {
14058 struct bfd_link_info *info;
14059 struct ppc_link_hash_table *htab;
14060 struct plt_entry *ent;
14061 asection *s;
14062
14063 if (h->root.type == bfd_link_hash_indirect)
14064 return true;
14065
14066 info = inf;
14067 htab = ppc_hash_table (info);
14068 if (htab == NULL)
14069 return false;
14070
14071 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14072 if (ent->plt.offset != (bfd_vma) -1)
14073 {
14074 /* This symbol has an entry in the procedure linkage
14075 table. Set it up. */
14076 Elf_Internal_Rela rela;
14077 asection *plt, *relplt;
14078 bfd_byte *loc;
14079
14080 if (use_local_plt (info, h))
14081 {
14082 if (!(h->def_regular
14083 && (h->root.type == bfd_link_hash_defined
14084 || h->root.type == bfd_link_hash_defweak)))
14085 continue;
14086 if (h->type == STT_GNU_IFUNC)
14087 {
14088 plt = htab->elf.iplt;
14089 relplt = htab->elf.irelplt;
14090 htab->elf.ifunc_resolvers = true;
14091 if (htab->opd_abi)
14092 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14093 else
14094 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14095 }
14096 else
14097 {
14098 plt = htab->pltlocal;
14099 if (bfd_link_pic (info))
14100 {
14101 relplt = htab->relpltlocal;
14102 if (htab->opd_abi)
14103 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14104 else
14105 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14106 }
14107 else
14108 relplt = NULL;
14109 }
14110 rela.r_addend = defined_sym_val (h) + ent->addend;
14111
14112 if (relplt == NULL)
14113 {
14114 loc = plt->contents + ent->plt.offset;
14115 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14116 if (htab->opd_abi)
14117 {
14118 bfd_vma toc = elf_gp (info->output_bfd);
14119 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14120 bfd_put_64 (info->output_bfd, toc, loc + 8);
14121 }
14122 }
14123 else
14124 {
14125 rela.r_offset = (plt->output_section->vma
14126 + plt->output_offset
14127 + ent->plt.offset);
14128 loc = relplt->contents + (relplt->reloc_count++
14129 * sizeof (Elf64_External_Rela));
14130 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14131 }
14132 }
14133 else
14134 {
14135 rela.r_offset = (htab->elf.splt->output_section->vma
14136 + htab->elf.splt->output_offset
14137 + ent->plt.offset);
14138 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14139 rela.r_addend = ent->addend;
14140 loc = (htab->elf.srelplt->contents
14141 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14142 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14143 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14144 htab->elf.ifunc_resolvers = true;
14145 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14146 }
14147 }
14148
14149 if (!h->pointer_equality_needed)
14150 return true;
14151
14152 if (h->def_regular)
14153 return true;
14154
14155 s = htab->global_entry;
14156 if (s == NULL || s->size == 0)
14157 return true;
14158
14159 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14160 if (ent->plt.offset != (bfd_vma) -1
14161 && ent->addend == 0)
14162 {
14163 bfd_byte *p;
14164 asection *plt;
14165 bfd_vma off;
14166
14167 p = s->contents + h->root.u.def.value;
14168 plt = htab->elf.splt;
14169 if (use_local_plt (info, h))
14170 {
14171 if (h->type == STT_GNU_IFUNC)
14172 plt = htab->elf.iplt;
14173 else
14174 plt = htab->pltlocal;
14175 }
14176 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14177 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14178
14179 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14180 {
14181 info->callbacks->einfo
14182 (_("%P: linkage table error against `%pT'\n"),
14183 h->root.root.string);
14184 bfd_set_error (bfd_error_bad_value);
14185 htab->stub_error = true;
14186 }
14187
14188 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14189 if (htab->params->emit_stub_syms)
14190 {
14191 size_t len = strlen (h->root.root.string);
14192 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14193
14194 if (name == NULL)
14195 return false;
14196
14197 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14198 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14199 if (h == NULL)
14200 return false;
14201 if (h->root.type == bfd_link_hash_new)
14202 {
14203 h->root.type = bfd_link_hash_defined;
14204 h->root.u.def.section = s;
14205 h->root.u.def.value = p - s->contents;
14206 h->ref_regular = 1;
14207 h->def_regular = 1;
14208 h->ref_regular_nonweak = 1;
14209 h->forced_local = 1;
14210 h->non_elf = 0;
14211 h->root.linker_def = 1;
14212 }
14213 }
14214
14215 if (PPC_HA (off) != 0)
14216 {
14217 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14218 p += 4;
14219 }
14220 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14221 p += 4;
14222 bfd_put_32 (s->owner, MTCTR_R12, p);
14223 p += 4;
14224 bfd_put_32 (s->owner, BCTR, p);
14225 break;
14226 }
14227 return true;
14228 }
14229
14230 /* Write PLT relocs for locals. */
14231
14232 static bool
14233 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14234 {
14235 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14236 bfd *ibfd;
14237
14238 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14239 {
14240 struct got_entry **lgot_ents, **end_lgot_ents;
14241 struct plt_entry **local_plt, **lplt, **end_local_plt;
14242 Elf_Internal_Shdr *symtab_hdr;
14243 bfd_size_type locsymcount;
14244 Elf_Internal_Sym *local_syms = NULL;
14245 struct plt_entry *ent;
14246
14247 if (!is_ppc64_elf (ibfd))
14248 continue;
14249
14250 lgot_ents = elf_local_got_ents (ibfd);
14251 if (!lgot_ents)
14252 continue;
14253
14254 symtab_hdr = &elf_symtab_hdr (ibfd);
14255 locsymcount = symtab_hdr->sh_info;
14256 end_lgot_ents = lgot_ents + locsymcount;
14257 local_plt = (struct plt_entry **) end_lgot_ents;
14258 end_local_plt = local_plt + locsymcount;
14259 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14260 for (ent = *lplt; ent != NULL; ent = ent->next)
14261 if (ent->plt.offset != (bfd_vma) -1)
14262 {
14263 Elf_Internal_Sym *sym;
14264 asection *sym_sec;
14265 asection *plt, *relplt;
14266 bfd_byte *loc;
14267 bfd_vma val;
14268
14269 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14270 lplt - local_plt, ibfd))
14271 {
14272 if (symtab_hdr->contents != (unsigned char *) local_syms)
14273 free (local_syms);
14274 return false;
14275 }
14276
14277 val = sym->st_value + ent->addend;
14278 if (sym_sec != NULL && sym_sec->output_section != NULL)
14279 val += sym_sec->output_offset + sym_sec->output_section->vma;
14280
14281 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14282 {
14283 htab->elf.ifunc_resolvers = true;
14284 plt = htab->elf.iplt;
14285 relplt = htab->elf.irelplt;
14286 }
14287 else
14288 {
14289 plt = htab->pltlocal;
14290 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14291 }
14292
14293 if (relplt == NULL)
14294 {
14295 loc = plt->contents + ent->plt.offset;
14296 bfd_put_64 (info->output_bfd, val, loc);
14297 if (htab->opd_abi)
14298 {
14299 bfd_vma toc = elf_gp (ibfd);
14300 bfd_put_64 (info->output_bfd, toc, loc + 8);
14301 }
14302 }
14303 else
14304 {
14305 Elf_Internal_Rela rela;
14306 rela.r_offset = (ent->plt.offset
14307 + plt->output_offset
14308 + plt->output_section->vma);
14309 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14310 {
14311 if (htab->opd_abi)
14312 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14313 else
14314 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14315 }
14316 else
14317 {
14318 if (htab->opd_abi)
14319 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14320 else
14321 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14322 }
14323 rela.r_addend = val;
14324 loc = relplt->contents + (relplt->reloc_count++
14325 * sizeof (Elf64_External_Rela));
14326 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14327 }
14328 }
14329
14330 if (local_syms != NULL
14331 && symtab_hdr->contents != (unsigned char *) local_syms)
14332 {
14333 if (!info->keep_memory)
14334 free (local_syms);
14335 else
14336 symtab_hdr->contents = (unsigned char *) local_syms;
14337 }
14338 }
14339 return true;
14340 }
14341
14342 /* Emit the static wrapper function preserving registers around a
14343 __tls_get_addr_opt call. */
14344
14345 static bool
14346 emit_tga_desc (struct ppc_link_hash_table *htab)
14347 {
14348 asection *stub_sec = htab->tga_group->stub_sec;
14349 unsigned int cfa_updt = 11 * 4;
14350 bfd_byte *p;
14351 bfd_vma to, from, delta;
14352
14353 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14354 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14355 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14356 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14357 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14358 delta = to - from;
14359 if (delta + (1 << 25) >= 1 << 26)
14360 {
14361 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14362 htab->stub_error = true;
14363 return false;
14364 }
14365
14366 p = stub_sec->contents;
14367 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14368 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14369 p += 4;
14370 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14371 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14372 }
14373
14374 /* Emit eh_frame describing the static wrapper function. */
14375
14376 static bfd_byte *
14377 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14378 {
14379 unsigned int cfa_updt = 11 * 4;
14380 unsigned int i;
14381
14382 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14383 *p++ = DW_CFA_def_cfa_offset;
14384 if (htab->opd_abi)
14385 {
14386 *p++ = 128;
14387 *p++ = 1;
14388 }
14389 else
14390 *p++ = 96;
14391 *p++ = DW_CFA_offset_extended_sf;
14392 *p++ = 65;
14393 *p++ = (-16 / 8) & 0x7f;
14394 for (i = 4; i < 12; i++)
14395 {
14396 *p++ = DW_CFA_offset + i;
14397 *p++ = (htab->opd_abi ? 13 : 12) - i;
14398 }
14399 *p++ = DW_CFA_advance_loc + 10;
14400 *p++ = DW_CFA_def_cfa_offset;
14401 *p++ = 0;
14402 for (i = 4; i < 12; i++)
14403 *p++ = DW_CFA_restore + i;
14404 *p++ = DW_CFA_advance_loc + 2;
14405 *p++ = DW_CFA_restore_extended;
14406 *p++ = 65;
14407 return p;
14408 }
14409
14410 /* Build all the stubs associated with the current output file.
14411 The stubs are kept in a hash table attached to the main linker
14412 hash table. This function is called via gldelf64ppc_finish. */
14413
14414 bool
14415 ppc64_elf_build_stubs (struct bfd_link_info *info,
14416 char **stats)
14417 {
14418 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14419 struct map_stub *group;
14420 asection *stub_sec;
14421 bfd_byte *p;
14422 int stub_sec_count = 0;
14423
14424 if (htab == NULL)
14425 return false;
14426
14427 /* Allocate memory to hold the linker stubs. */
14428 for (group = htab->group; group != NULL; group = group->next)
14429 {
14430 group->eh_size = 0;
14431 group->lr_restore = 0;
14432 if ((stub_sec = group->stub_sec) != NULL
14433 && stub_sec->size != 0)
14434 {
14435 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14436 stub_sec->size);
14437 if (stub_sec->contents == NULL)
14438 return false;
14439 stub_sec->size = 0;
14440 }
14441 }
14442
14443 if (htab->glink != NULL && htab->glink->size != 0)
14444 {
14445 unsigned int indx;
14446 bfd_vma plt0;
14447
14448 /* Build the .glink plt call stub. */
14449 if (htab->params->emit_stub_syms)
14450 {
14451 struct elf_link_hash_entry *h;
14452 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14453 true, false, false);
14454 if (h == NULL)
14455 return false;
14456 if (h->root.type == bfd_link_hash_new)
14457 {
14458 h->root.type = bfd_link_hash_defined;
14459 h->root.u.def.section = htab->glink;
14460 h->root.u.def.value = 8;
14461 h->ref_regular = 1;
14462 h->def_regular = 1;
14463 h->ref_regular_nonweak = 1;
14464 h->forced_local = 1;
14465 h->non_elf = 0;
14466 h->root.linker_def = 1;
14467 }
14468 }
14469 plt0 = (htab->elf.splt->output_section->vma
14470 + htab->elf.splt->output_offset
14471 - 16);
14472 if (info->emitrelocations)
14473 {
14474 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14475 if (r == NULL)
14476 return false;
14477 r->r_offset = (htab->glink->output_offset
14478 + htab->glink->output_section->vma);
14479 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14480 r->r_addend = plt0;
14481 }
14482 p = htab->glink->contents;
14483 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14484 bfd_put_64 (htab->glink->owner, plt0, p);
14485 p += 8;
14486 if (htab->opd_abi)
14487 {
14488 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14489 p += 4;
14490 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14491 p += 4;
14492 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14493 p += 4;
14494 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14495 p += 4;
14496 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14497 p += 4;
14498 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14499 p += 4;
14500 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14501 p += 4;
14502 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14503 p += 4;
14504 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14505 p += 4;
14506 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14507 p += 4;
14508 }
14509 else
14510 {
14511 unsigned int insn;
14512
14513 /* 0:
14514 . .quad plt0-1f # plt0 entry relative to 1:
14515 #
14516 # We get here with r12 initially @ a glink branch
14517 # Load the address of _dl_runtime_resolve from plt0 and
14518 # jump to it, with r0 set to the index of the PLT entry
14519 # to be resolved and r11 the link map.
14520 __glink_PLTresolve:
14521 . std %r2,24(%r1) # optional
14522 . mflr %r0
14523 . bcl 20,31,1f
14524 1:
14525 . mflr %r11
14526 . mtlr %r0
14527 . ld %r0,(0b-1b)(%r11)
14528 . sub %r12,%r12,%r11
14529 . add %r11,%r0,%r11
14530 . addi %r0,%r12,1b-2f
14531 . ld %r12,0(%r11)
14532 . srdi %r0,%r0,2
14533 . mtctr %r12
14534 . ld %r11,8(%r11)
14535 . bctr
14536 2:
14537 . b __glink_PLTresolve
14538 . ...
14539 . b __glink_PLTresolve */
14540
14541 if (htab->has_plt_localentry0)
14542 {
14543 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14544 p += 4;
14545 }
14546 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14547 p += 4;
14548 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14549 p += 4;
14550 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14551 p += 4;
14552 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14553 p += 4;
14554 if (htab->has_plt_localentry0)
14555 insn = LD_R0_0R11 | (-20 & 0xfffc);
14556 else
14557 insn = LD_R0_0R11 | (-16 & 0xfffc);
14558 bfd_put_32 (htab->glink->owner, insn, p);
14559 p += 4;
14560 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14561 p += 4;
14562 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
14563 p += 4;
14564 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
14565 p += 4;
14566 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14567 p += 4;
14568 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14569 p += 4;
14570 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14571 p += 4;
14572 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14573 p += 4;
14574 }
14575 bfd_put_32 (htab->glink->owner, BCTR, p);
14576 p += 4;
14577 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14578
14579 /* Build the .glink lazy link call stubs. */
14580 indx = 0;
14581 while (p < htab->glink->contents + htab->glink->size)
14582 {
14583 if (htab->opd_abi)
14584 {
14585 if (indx < 0x8000)
14586 {
14587 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14588 p += 4;
14589 }
14590 else
14591 {
14592 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14593 p += 4;
14594 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14595 p);
14596 p += 4;
14597 }
14598 }
14599 bfd_put_32 (htab->glink->owner,
14600 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14601 indx++;
14602 p += 4;
14603 }
14604 }
14605
14606 if (htab->tga_group != NULL)
14607 {
14608 htab->tga_group->lr_restore = 23 * 4;
14609 htab->tga_group->stub_sec->size = 24 * 4;
14610 if (!emit_tga_desc (htab))
14611 return false;
14612 if (htab->glink_eh_frame != NULL
14613 && htab->glink_eh_frame->size != 0)
14614 {
14615 size_t align = 4;
14616
14617 p = htab->glink_eh_frame->contents;
14618 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14619 p += 17;
14620 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14621 }
14622 }
14623
14624 /* Build .glink global entry stubs, and PLT relocs for globals. */
14625 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14626
14627 if (!write_plt_relocs_for_local_syms (info))
14628 return false;
14629
14630 if (htab->brlt != NULL && htab->brlt->size != 0)
14631 {
14632 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
14633 htab->brlt->size);
14634 if (htab->brlt->contents == NULL)
14635 return false;
14636 }
14637 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
14638 {
14639 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
14640 htab->relbrlt->size);
14641 if (htab->relbrlt->contents == NULL)
14642 return false;
14643 }
14644
14645 /* Build the stubs as directed by the stub hash table. */
14646 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14647
14648 for (group = htab->group; group != NULL; group = group->next)
14649 if (group->needs_save_res)
14650 group->stub_sec->size += htab->sfpr->size;
14651
14652 if (htab->relbrlt != NULL)
14653 htab->relbrlt->reloc_count = 0;
14654
14655 if (htab->params->plt_stub_align != 0)
14656 for (group = htab->group; group != NULL; group = group->next)
14657 if ((stub_sec = group->stub_sec) != NULL)
14658 {
14659 int align = abs (htab->params->plt_stub_align);
14660 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14661 }
14662
14663 for (group = htab->group; group != NULL; group = group->next)
14664 if (group->needs_save_res)
14665 {
14666 stub_sec = group->stub_sec;
14667 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14668 htab->sfpr->contents, htab->sfpr->size);
14669 if (htab->params->emit_stub_syms)
14670 {
14671 unsigned int i;
14672
14673 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14674 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14675 return false;
14676 }
14677 }
14678
14679 if (htab->glink_eh_frame != NULL
14680 && htab->glink_eh_frame->size != 0)
14681 {
14682 bfd_vma val;
14683 size_t align = 4;
14684
14685 p = htab->glink_eh_frame->contents;
14686 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14687
14688 for (group = htab->group; group != NULL; group = group->next)
14689 if (group->eh_size != 0)
14690 {
14691 /* Offset to stub section. */
14692 val = (group->stub_sec->output_section->vma
14693 + group->stub_sec->output_offset);
14694 val -= (htab->glink_eh_frame->output_section->vma
14695 + htab->glink_eh_frame->output_offset
14696 + (p + 8 - htab->glink_eh_frame->contents));
14697 if (val + 0x80000000 > 0xffffffff)
14698 {
14699 _bfd_error_handler
14700 (_("%s offset too large for .eh_frame sdata4 encoding"),
14701 group->stub_sec->name);
14702 return false;
14703 }
14704 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14705 p += (group->eh_size + 17 + 3) & -4;
14706 }
14707 if (htab->glink != NULL && htab->glink->size != 0)
14708 {
14709 /* Offset to .glink. */
14710 val = (htab->glink->output_section->vma
14711 + htab->glink->output_offset
14712 + 8);
14713 val -= (htab->glink_eh_frame->output_section->vma
14714 + htab->glink_eh_frame->output_offset
14715 + (p + 8 - htab->glink_eh_frame->contents));
14716 if (val + 0x80000000 > 0xffffffff)
14717 {
14718 _bfd_error_handler
14719 (_("%s offset too large for .eh_frame sdata4 encoding"),
14720 htab->glink->name);
14721 return false;
14722 }
14723 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14724 p += (24 + align - 1) & -align;
14725 }
14726 }
14727
14728 for (group = htab->group; group != NULL; group = group->next)
14729 if ((stub_sec = group->stub_sec) != NULL)
14730 {
14731 stub_sec_count += 1;
14732 if (stub_sec->rawsize != stub_sec->size
14733 && (htab->stub_iteration <= STUB_SHRINK_ITER
14734 || stub_sec->rawsize < stub_sec->size))
14735 break;
14736 }
14737
14738 if (group != NULL)
14739 {
14740 htab->stub_error = true;
14741 _bfd_error_handler (_("stubs don't match calculated size"));
14742 }
14743
14744 if (htab->stub_error)
14745 return false;
14746
14747 if (stats != NULL)
14748 {
14749 char *groupmsg;
14750 if (asprintf (&groupmsg,
14751 ngettext ("linker stubs in %u group\n",
14752 "linker stubs in %u groups\n",
14753 stub_sec_count),
14754 stub_sec_count) < 0)
14755 *stats = NULL;
14756 else
14757 {
14758 if (asprintf (stats, _("%s"
14759 " branch %lu\n"
14760 " branch toc adj %lu\n"
14761 " branch notoc %lu\n"
14762 " branch both %lu\n"
14763 " long branch %lu\n"
14764 " long toc adj %lu\n"
14765 " long notoc %lu\n"
14766 " long both %lu\n"
14767 " plt call %lu\n"
14768 " plt call save %lu\n"
14769 " plt call notoc %lu\n"
14770 " plt call both %lu\n"
14771 " global entry %lu"),
14772 groupmsg,
14773 htab->stub_count[ppc_stub_long_branch - 1],
14774 htab->stub_count[ppc_stub_long_branch_r2off - 1],
14775 htab->stub_count[ppc_stub_long_branch_notoc - 1],
14776 htab->stub_count[ppc_stub_long_branch_both - 1],
14777 htab->stub_count[ppc_stub_plt_branch - 1],
14778 htab->stub_count[ppc_stub_plt_branch_r2off - 1],
14779 htab->stub_count[ppc_stub_plt_branch_notoc - 1],
14780 htab->stub_count[ppc_stub_plt_branch_both - 1],
14781 htab->stub_count[ppc_stub_plt_call - 1],
14782 htab->stub_count[ppc_stub_plt_call_r2save - 1],
14783 htab->stub_count[ppc_stub_plt_call_notoc - 1],
14784 htab->stub_count[ppc_stub_plt_call_both - 1],
14785 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14786 *stats = NULL;
14787 free (groupmsg);
14788 }
14789 }
14790 return true;
14791 }
14792
14793 /* What to do when ld finds relocations against symbols defined in
14794 discarded sections. */
14795
14796 static unsigned int
14797 ppc64_elf_action_discarded (asection *sec)
14798 {
14799 if (strcmp (".opd", sec->name) == 0)
14800 return 0;
14801
14802 if (strcmp (".toc", sec->name) == 0)
14803 return 0;
14804
14805 if (strcmp (".toc1", sec->name) == 0)
14806 return 0;
14807
14808 return _bfd_elf_default_action_discarded (sec);
14809 }
14810
14811 /* These are the dynamic relocations supported by glibc. */
14812
14813 static bool
14814 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14815 {
14816 switch (r_type)
14817 {
14818 case R_PPC64_RELATIVE:
14819 case R_PPC64_NONE:
14820 case R_PPC64_ADDR64:
14821 case R_PPC64_GLOB_DAT:
14822 case R_PPC64_IRELATIVE:
14823 case R_PPC64_JMP_IREL:
14824 case R_PPC64_JMP_SLOT:
14825 case R_PPC64_DTPMOD64:
14826 case R_PPC64_DTPREL64:
14827 case R_PPC64_TPREL64:
14828 case R_PPC64_TPREL16_LO_DS:
14829 case R_PPC64_TPREL16_DS:
14830 case R_PPC64_TPREL16:
14831 case R_PPC64_TPREL16_LO:
14832 case R_PPC64_TPREL16_HI:
14833 case R_PPC64_TPREL16_HIGH:
14834 case R_PPC64_TPREL16_HA:
14835 case R_PPC64_TPREL16_HIGHA:
14836 case R_PPC64_TPREL16_HIGHER:
14837 case R_PPC64_TPREL16_HIGHEST:
14838 case R_PPC64_TPREL16_HIGHERA:
14839 case R_PPC64_TPREL16_HIGHESTA:
14840 case R_PPC64_ADDR16_LO_DS:
14841 case R_PPC64_ADDR16_LO:
14842 case R_PPC64_ADDR16_HI:
14843 case R_PPC64_ADDR16_HIGH:
14844 case R_PPC64_ADDR16_HA:
14845 case R_PPC64_ADDR16_HIGHA:
14846 case R_PPC64_REL30:
14847 case R_PPC64_COPY:
14848 case R_PPC64_UADDR64:
14849 case R_PPC64_UADDR32:
14850 case R_PPC64_ADDR32:
14851 case R_PPC64_ADDR24:
14852 case R_PPC64_ADDR16:
14853 case R_PPC64_UADDR16:
14854 case R_PPC64_ADDR16_DS:
14855 case R_PPC64_ADDR16_HIGHER:
14856 case R_PPC64_ADDR16_HIGHEST:
14857 case R_PPC64_ADDR16_HIGHERA:
14858 case R_PPC64_ADDR16_HIGHESTA:
14859 case R_PPC64_ADDR14:
14860 case R_PPC64_ADDR14_BRTAKEN:
14861 case R_PPC64_ADDR14_BRNTAKEN:
14862 case R_PPC64_REL32:
14863 case R_PPC64_REL64:
14864 return true;
14865
14866 default:
14867 return false;
14868 }
14869 }
14870
14871 /* The RELOCATE_SECTION function is called by the ELF backend linker
14872 to handle the relocations for a section.
14873
14874 The relocs are always passed as Rela structures; if the section
14875 actually uses Rel structures, the r_addend field will always be
14876 zero.
14877
14878 This function is responsible for adjust the section contents as
14879 necessary, and (if using Rela relocs and generating a
14880 relocatable output file) adjusting the reloc addend as
14881 necessary.
14882
14883 This function does not have to worry about setting the reloc
14884 address or the reloc symbol index.
14885
14886 LOCAL_SYMS is a pointer to the swapped in local symbols.
14887
14888 LOCAL_SECTIONS is an array giving the section in the input file
14889 corresponding to the st_shndx field of each local symbol.
14890
14891 The global hash table entry for the global symbols can be found
14892 via elf_sym_hashes (input_bfd).
14893
14894 When generating relocatable output, this function must handle
14895 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14896 going to be the section symbol corresponding to the output
14897 section, which means that the addend must be adjusted
14898 accordingly. */
14899
14900 static int
14901 ppc64_elf_relocate_section (bfd *output_bfd,
14902 struct bfd_link_info *info,
14903 bfd *input_bfd,
14904 asection *input_section,
14905 bfd_byte *contents,
14906 Elf_Internal_Rela *relocs,
14907 Elf_Internal_Sym *local_syms,
14908 asection **local_sections)
14909 {
14910 struct ppc_link_hash_table *htab;
14911 Elf_Internal_Shdr *symtab_hdr;
14912 struct elf_link_hash_entry **sym_hashes;
14913 Elf_Internal_Rela *rel;
14914 Elf_Internal_Rela *wrel;
14915 Elf_Internal_Rela *relend;
14916 Elf_Internal_Rela outrel;
14917 bfd_byte *loc;
14918 struct got_entry **local_got_ents;
14919 bfd_vma TOCstart;
14920 bool ret = true;
14921 bool is_opd;
14922 /* Assume 'at' branch hints. */
14923 bool is_isa_v2 = true;
14924 bool warned_dynamic = false;
14925 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14926
14927 /* Initialize howto table if needed. */
14928 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14929 ppc_howto_init ();
14930
14931 htab = ppc_hash_table (info);
14932 if (htab == NULL)
14933 return false;
14934
14935 /* Don't relocate stub sections. */
14936 if (input_section->owner == htab->params->stub_bfd)
14937 return true;
14938
14939 if (!is_ppc64_elf (input_bfd))
14940 {
14941 bfd_set_error (bfd_error_wrong_format);
14942 return false;
14943 }
14944
14945 local_got_ents = elf_local_got_ents (input_bfd);
14946 TOCstart = elf_gp (output_bfd);
14947 symtab_hdr = &elf_symtab_hdr (input_bfd);
14948 sym_hashes = elf_sym_hashes (input_bfd);
14949 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14950
14951 rel = wrel = relocs;
14952 relend = relocs + input_section->reloc_count;
14953 for (; rel < relend; wrel++, rel++)
14954 {
14955 enum elf_ppc64_reloc_type r_type;
14956 bfd_vma addend;
14957 bfd_reloc_status_type r;
14958 Elf_Internal_Sym *sym;
14959 asection *sec;
14960 struct elf_link_hash_entry *h_elf;
14961 struct ppc_link_hash_entry *h;
14962 struct ppc_link_hash_entry *fdh;
14963 const char *sym_name;
14964 unsigned long r_symndx, toc_symndx;
14965 bfd_vma toc_addend;
14966 unsigned char tls_mask, tls_gd, tls_type;
14967 unsigned char sym_type;
14968 bfd_vma relocation;
14969 bool unresolved_reloc, save_unresolved_reloc;
14970 bool warned;
14971 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14972 unsigned int insn;
14973 unsigned int mask;
14974 struct ppc_stub_hash_entry *stub_entry;
14975 bfd_vma max_br_offset;
14976 bfd_vma from;
14977 Elf_Internal_Rela orig_rel;
14978 reloc_howto_type *howto;
14979 struct reloc_howto_struct alt_howto;
14980 uint64_t pinsn;
14981 bfd_vma offset;
14982
14983 again:
14984 orig_rel = *rel;
14985
14986 r_type = ELF64_R_TYPE (rel->r_info);
14987 r_symndx = ELF64_R_SYM (rel->r_info);
14988
14989 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14990 symbol of the previous ADDR64 reloc. The symbol gives us the
14991 proper TOC base to use. */
14992 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14993 && wrel != relocs
14994 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
14995 && is_opd)
14996 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
14997
14998 sym = NULL;
14999 sec = NULL;
15000 h_elf = NULL;
15001 sym_name = NULL;
15002 unresolved_reloc = false;
15003 warned = false;
15004
15005 if (r_symndx < symtab_hdr->sh_info)
15006 {
15007 /* It's a local symbol. */
15008 struct _opd_sec_data *opd;
15009
15010 sym = local_syms + r_symndx;
15011 sec = local_sections[r_symndx];
15012 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15013 sym_type = ELF64_ST_TYPE (sym->st_info);
15014 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15015 opd = get_opd_info (sec);
15016 if (opd != NULL && opd->adjust != NULL)
15017 {
15018 long adjust = opd->adjust[OPD_NDX (sym->st_value
15019 + rel->r_addend)];
15020 if (adjust == -1)
15021 relocation = 0;
15022 else
15023 {
15024 /* If this is a relocation against the opd section sym
15025 and we have edited .opd, adjust the reloc addend so
15026 that ld -r and ld --emit-relocs output is correct.
15027 If it is a reloc against some other .opd symbol,
15028 then the symbol value will be adjusted later. */
15029 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15030 rel->r_addend += adjust;
15031 else
15032 relocation += adjust;
15033 }
15034 }
15035 }
15036 else
15037 {
15038 bool ignored;
15039
15040 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15041 r_symndx, symtab_hdr, sym_hashes,
15042 h_elf, sec, relocation,
15043 unresolved_reloc, warned, ignored);
15044 sym_name = h_elf->root.root.string;
15045 sym_type = h_elf->type;
15046 if (sec != NULL
15047 && sec->owner == output_bfd
15048 && strcmp (sec->name, ".opd") == 0)
15049 {
15050 /* This is a symbol defined in a linker script. All
15051 such are defined in output sections, even those
15052 defined by simple assignment from a symbol defined in
15053 an input section. Transfer the symbol to an
15054 appropriate input .opd section, so that a branch to
15055 this symbol will be mapped to the location specified
15056 by the opd entry. */
15057 struct bfd_link_order *lo;
15058 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15059 if (lo->type == bfd_indirect_link_order)
15060 {
15061 asection *isec = lo->u.indirect.section;
15062 if (h_elf->root.u.def.value >= isec->output_offset
15063 && h_elf->root.u.def.value < (isec->output_offset
15064 + isec->size))
15065 {
15066 h_elf->root.u.def.value -= isec->output_offset;
15067 h_elf->root.u.def.section = isec;
15068 sec = isec;
15069 break;
15070 }
15071 }
15072 }
15073 }
15074 h = ppc_elf_hash_entry (h_elf);
15075
15076 if (sec != NULL && discarded_section (sec))
15077 {
15078 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15079 input_bfd, input_section,
15080 contents, rel->r_offset);
15081 wrel->r_offset = rel->r_offset;
15082 wrel->r_info = 0;
15083 wrel->r_addend = 0;
15084
15085 /* For ld -r, remove relocations in debug sections against
15086 symbols defined in discarded sections. Not done for
15087 non-debug to preserve relocs in .eh_frame which the
15088 eh_frame editing code expects to be present. */
15089 if (bfd_link_relocatable (info)
15090 && (input_section->flags & SEC_DEBUGGING))
15091 wrel--;
15092
15093 continue;
15094 }
15095
15096 if (bfd_link_relocatable (info))
15097 goto copy_reloc;
15098
15099 if (h != NULL && &h->elf == htab->elf.hgot)
15100 {
15101 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15102 sec = bfd_abs_section_ptr;
15103 unresolved_reloc = false;
15104 }
15105
15106 /* TLS optimizations. Replace instruction sequences and relocs
15107 based on information we collected in tls_optimize. We edit
15108 RELOCS so that --emit-relocs will output something sensible
15109 for the final instruction stream. */
15110 tls_mask = 0;
15111 tls_gd = 0;
15112 toc_symndx = 0;
15113 if (h != NULL)
15114 tls_mask = h->tls_mask;
15115 else if (local_got_ents != NULL)
15116 {
15117 struct plt_entry **local_plt = (struct plt_entry **)
15118 (local_got_ents + symtab_hdr->sh_info);
15119 unsigned char *lgot_masks = (unsigned char *)
15120 (local_plt + symtab_hdr->sh_info);
15121 tls_mask = lgot_masks[r_symndx];
15122 }
15123 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15124 && (r_type == R_PPC64_TLS
15125 || r_type == R_PPC64_TLSGD
15126 || r_type == R_PPC64_TLSLD))
15127 {
15128 /* Check for toc tls entries. */
15129 unsigned char *toc_tls;
15130
15131 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15132 &local_syms, rel, input_bfd))
15133 return false;
15134
15135 if (toc_tls)
15136 tls_mask = *toc_tls;
15137 }
15138
15139 /* Check that tls relocs are used with tls syms, and non-tls
15140 relocs are used with non-tls syms. */
15141 if (r_symndx != STN_UNDEF
15142 && r_type != R_PPC64_NONE
15143 && (h == NULL
15144 || h->elf.root.type == bfd_link_hash_defined
15145 || h->elf.root.type == bfd_link_hash_defweak)
15146 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15147 {
15148 if ((tls_mask & TLS_TLS) != 0
15149 && (r_type == R_PPC64_TLS
15150 || r_type == R_PPC64_TLSGD
15151 || r_type == R_PPC64_TLSLD))
15152 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15153 ;
15154 else
15155 info->callbacks->einfo
15156 (!IS_PPC64_TLS_RELOC (r_type)
15157 /* xgettext:c-format */
15158 ? _("%H: %s used with TLS symbol `%pT'\n")
15159 /* xgettext:c-format */
15160 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15161 input_bfd, input_section, rel->r_offset,
15162 ppc64_elf_howto_table[r_type]->name,
15163 sym_name);
15164 }
15165
15166 /* Ensure reloc mapping code below stays sane. */
15167 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15168 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15169 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15170 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15171 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15172 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15173 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15174 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15175 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15176 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15177 abort ();
15178
15179 switch (r_type)
15180 {
15181 default:
15182 break;
15183
15184 case R_PPC64_LO_DS_OPT:
15185 insn = bfd_get_32 (input_bfd, contents + rel->r_offset - d_offset);
15186 if ((insn & (0x3fu << 26)) != 58u << 26)
15187 abort ();
15188 insn += (14u << 26) - (58u << 26);
15189 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - d_offset);
15190 r_type = R_PPC64_TOC16_LO;
15191 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15192 break;
15193
15194 case R_PPC64_TOC16:
15195 case R_PPC64_TOC16_LO:
15196 case R_PPC64_TOC16_DS:
15197 case R_PPC64_TOC16_LO_DS:
15198 {
15199 /* Check for toc tls entries. */
15200 unsigned char *toc_tls;
15201 int retval;
15202
15203 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15204 &local_syms, rel, input_bfd);
15205 if (retval == 0)
15206 return false;
15207
15208 if (toc_tls)
15209 {
15210 tls_mask = *toc_tls;
15211 if (r_type == R_PPC64_TOC16_DS
15212 || r_type == R_PPC64_TOC16_LO_DS)
15213 {
15214 if ((tls_mask & TLS_TLS) != 0
15215 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15216 goto toctprel;
15217 }
15218 else
15219 {
15220 /* If we found a GD reloc pair, then we might be
15221 doing a GD->IE transition. */
15222 if (retval == 2)
15223 {
15224 tls_gd = TLS_GDIE;
15225 if ((tls_mask & TLS_TLS) != 0
15226 && (tls_mask & TLS_GD) == 0)
15227 goto tls_ldgd_opt;
15228 }
15229 else if (retval == 3)
15230 {
15231 if ((tls_mask & TLS_TLS) != 0
15232 && (tls_mask & TLS_LD) == 0)
15233 goto tls_ldgd_opt;
15234 }
15235 }
15236 }
15237 }
15238 break;
15239
15240 case R_PPC64_GOT_TPREL16_HI:
15241 case R_PPC64_GOT_TPREL16_HA:
15242 if ((tls_mask & TLS_TLS) != 0
15243 && (tls_mask & TLS_TPREL) == 0)
15244 {
15245 rel->r_offset -= d_offset;
15246 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15247 r_type = R_PPC64_NONE;
15248 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15249 }
15250 break;
15251
15252 case R_PPC64_GOT_TPREL16_DS:
15253 case R_PPC64_GOT_TPREL16_LO_DS:
15254 if ((tls_mask & TLS_TLS) != 0
15255 && (tls_mask & TLS_TPREL) == 0)
15256 {
15257 toctprel:
15258 insn = bfd_get_32 (input_bfd,
15259 contents + rel->r_offset - d_offset);
15260 insn &= 31 << 21;
15261 insn |= 0x3c0d0000; /* addis 0,13,0 */
15262 bfd_put_32 (input_bfd, insn,
15263 contents + rel->r_offset - d_offset);
15264 r_type = R_PPC64_TPREL16_HA;
15265 if (toc_symndx != 0)
15266 {
15267 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15268 rel->r_addend = toc_addend;
15269 /* We changed the symbol. Start over in order to
15270 get h, sym, sec etc. right. */
15271 goto again;
15272 }
15273 else
15274 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15275 }
15276 break;
15277
15278 case R_PPC64_GOT_TPREL_PCREL34:
15279 if ((tls_mask & TLS_TLS) != 0
15280 && (tls_mask & TLS_TPREL) == 0)
15281 {
15282 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15283 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15284 pinsn <<= 32;
15285 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15286 pinsn += ((2ULL << 56) + (-1ULL << 52)
15287 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15288 bfd_put_32 (input_bfd, pinsn >> 32,
15289 contents + rel->r_offset);
15290 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15291 contents + rel->r_offset + 4);
15292 r_type = R_PPC64_TPREL34;
15293 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15294 }
15295 break;
15296
15297 case R_PPC64_TLS:
15298 if ((tls_mask & TLS_TLS) != 0
15299 && (tls_mask & TLS_TPREL) == 0)
15300 {
15301 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15302 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15303 if (insn == 0)
15304 break;
15305 if ((rel->r_offset & 3) == 0)
15306 {
15307 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15308 /* Was PPC64_TLS which sits on insn boundary, now
15309 PPC64_TPREL16_LO which is at low-order half-word. */
15310 rel->r_offset += d_offset;
15311 r_type = R_PPC64_TPREL16_LO;
15312 if (toc_symndx != 0)
15313 {
15314 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15315 rel->r_addend = toc_addend;
15316 /* We changed the symbol. Start over in order to
15317 get h, sym, sec etc. right. */
15318 goto again;
15319 }
15320 else
15321 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15322 }
15323 else if ((rel->r_offset & 3) == 1)
15324 {
15325 /* For pcrel IE to LE we already have the full
15326 offset and thus don't need an addi here. A nop
15327 or mr will do. */
15328 if ((insn & (0x3fu << 26)) == 14 << 26)
15329 {
15330 /* Extract regs from addi rt,ra,si. */
15331 unsigned int rt = (insn >> 21) & 0x1f;
15332 unsigned int ra = (insn >> 16) & 0x1f;
15333 if (rt == ra)
15334 insn = NOP;
15335 else
15336 {
15337 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15338 insn = (rt << 16) | (ra << 21) | (ra << 11);
15339 insn |= (31u << 26) | (444u << 1);
15340 }
15341 }
15342 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15343 }
15344 }
15345 break;
15346
15347 case R_PPC64_GOT_TLSGD16_HI:
15348 case R_PPC64_GOT_TLSGD16_HA:
15349 tls_gd = TLS_GDIE;
15350 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15351 goto tls_gdld_hi;
15352 break;
15353
15354 case R_PPC64_GOT_TLSLD16_HI:
15355 case R_PPC64_GOT_TLSLD16_HA:
15356 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15357 {
15358 tls_gdld_hi:
15359 if ((tls_mask & tls_gd) != 0)
15360 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15361 + R_PPC64_GOT_TPREL16_DS);
15362 else
15363 {
15364 rel->r_offset -= d_offset;
15365 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15366 r_type = R_PPC64_NONE;
15367 }
15368 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15369 }
15370 break;
15371
15372 case R_PPC64_GOT_TLSGD16:
15373 case R_PPC64_GOT_TLSGD16_LO:
15374 tls_gd = TLS_GDIE;
15375 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15376 goto tls_ldgd_opt;
15377 break;
15378
15379 case R_PPC64_GOT_TLSLD16:
15380 case R_PPC64_GOT_TLSLD16_LO:
15381 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15382 {
15383 unsigned int insn1, insn2;
15384
15385 tls_ldgd_opt:
15386 offset = (bfd_vma) -1;
15387 /* If not using the newer R_PPC64_TLSGD/LD to mark
15388 __tls_get_addr calls, we must trust that the call
15389 stays with its arg setup insns, ie. that the next
15390 reloc is the __tls_get_addr call associated with
15391 the current reloc. Edit both insns. */
15392 if (input_section->nomark_tls_get_addr
15393 && rel + 1 < relend
15394 && branch_reloc_hash_match (input_bfd, rel + 1,
15395 htab->tls_get_addr_fd,
15396 htab->tga_desc_fd,
15397 htab->tls_get_addr,
15398 htab->tga_desc))
15399 offset = rel[1].r_offset;
15400 /* We read the low GOT_TLS (or TOC16) insn because we
15401 need to keep the destination reg. It may be
15402 something other than the usual r3, and moved to r3
15403 before the call by intervening code. */
15404 insn1 = bfd_get_32 (input_bfd,
15405 contents + rel->r_offset - d_offset);
15406 if ((tls_mask & tls_gd) != 0)
15407 {
15408 /* IE */
15409 insn1 &= (0x1f << 21) | (0x1f << 16);
15410 insn1 |= 58u << 26; /* ld */
15411 insn2 = 0x7c636a14; /* add 3,3,13 */
15412 if (offset != (bfd_vma) -1)
15413 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15414 if (r_type == R_PPC64_TOC16
15415 || r_type == R_PPC64_TOC16_LO)
15416 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15417 else
15418 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15419 + R_PPC64_GOT_TPREL16_DS);
15420 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15421 }
15422 else
15423 {
15424 /* LE */
15425 insn1 &= 0x1f << 21;
15426 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15427 insn2 = 0x38630000; /* addi 3,3,0 */
15428 if (tls_gd == 0)
15429 {
15430 /* Was an LD reloc. */
15431 r_symndx = STN_UNDEF;
15432 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15433 }
15434 else if (toc_symndx != 0)
15435 {
15436 r_symndx = toc_symndx;
15437 rel->r_addend = toc_addend;
15438 }
15439 r_type = R_PPC64_TPREL16_HA;
15440 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15441 if (offset != (bfd_vma) -1)
15442 {
15443 rel[1].r_info = ELF64_R_INFO (r_symndx,
15444 R_PPC64_TPREL16_LO);
15445 rel[1].r_offset = offset + d_offset;
15446 rel[1].r_addend = rel->r_addend;
15447 }
15448 }
15449 bfd_put_32 (input_bfd, insn1,
15450 contents + rel->r_offset - d_offset);
15451 if (offset != (bfd_vma) -1)
15452 {
15453 bfd_put_32 (input_bfd, insn2, contents + offset);
15454 if (offset + 8 <= input_section->size)
15455 {
15456 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15457 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15458 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15459 }
15460 }
15461 if ((tls_mask & tls_gd) == 0
15462 && (tls_gd == 0 || toc_symndx != 0))
15463 {
15464 /* We changed the symbol. Start over in order
15465 to get h, sym, sec etc. right. */
15466 goto again;
15467 }
15468 }
15469 break;
15470
15471 case R_PPC64_GOT_TLSGD_PCREL34:
15472 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0)
15473 {
15474 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15475 pinsn <<= 32;
15476 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15477 if ((tls_mask & TLS_GDIE) != 0)
15478 {
15479 /* IE, pla -> pld */
15480 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
15481 r_type = R_PPC64_GOT_TPREL_PCREL34;
15482 }
15483 else
15484 {
15485 /* LE, pla pcrel -> paddi r13 */
15486 pinsn += (-1ULL << 52) + (13ULL << 16);
15487 r_type = R_PPC64_TPREL34;
15488 }
15489 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15490 bfd_put_32 (input_bfd, pinsn >> 32,
15491 contents + rel->r_offset);
15492 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15493 contents + rel->r_offset + 4);
15494 }
15495 break;
15496
15497 case R_PPC64_GOT_TLSLD_PCREL34:
15498 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0)
15499 {
15500 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15501 pinsn <<= 32;
15502 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15503 pinsn += (-1ULL << 52) + (13ULL << 16);
15504 bfd_put_32 (input_bfd, pinsn >> 32,
15505 contents + rel->r_offset);
15506 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15507 contents + rel->r_offset + 4);
15508 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15509 r_symndx = STN_UNDEF;
15510 r_type = R_PPC64_TPREL34;
15511 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15512 goto again;
15513 }
15514 break;
15515
15516 case R_PPC64_TLSGD:
15517 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15518 && rel + 1 < relend)
15519 {
15520 unsigned int insn2;
15521 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15522
15523 offset = rel->r_offset;
15524 if (is_plt_seq_reloc (r_type1))
15525 {
15526 bfd_put_32 (output_bfd, NOP, contents + offset);
15527 if (r_type1 == R_PPC64_PLT_PCREL34
15528 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15529 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15530 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15531 break;
15532 }
15533
15534 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15535 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15536
15537 if ((tls_mask & TLS_GDIE) != 0)
15538 {
15539 /* IE */
15540 r_type = R_PPC64_NONE;
15541 insn2 = 0x7c636a14; /* add 3,3,13 */
15542 }
15543 else
15544 {
15545 /* LE */
15546 if (toc_symndx != 0)
15547 {
15548 r_symndx = toc_symndx;
15549 rel->r_addend = toc_addend;
15550 }
15551 if (r_type1 == R_PPC64_REL24_NOTOC
15552 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15553 {
15554 r_type = R_PPC64_NONE;
15555 insn2 = NOP;
15556 }
15557 else
15558 {
15559 rel->r_offset = offset + d_offset;
15560 r_type = R_PPC64_TPREL16_LO;
15561 insn2 = 0x38630000; /* addi 3,3,0 */
15562 }
15563 }
15564 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15565 /* Zap the reloc on the _tls_get_addr call too. */
15566 BFD_ASSERT (offset == rel[1].r_offset);
15567 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15568 bfd_put_32 (input_bfd, insn2, contents + offset);
15569 if ((tls_mask & TLS_GDIE) == 0
15570 && toc_symndx != 0
15571 && r_type != R_PPC64_NONE)
15572 goto again;
15573 }
15574 break;
15575
15576 case R_PPC64_TLSLD:
15577 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15578 && rel + 1 < relend)
15579 {
15580 unsigned int insn2;
15581 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15582
15583 offset = rel->r_offset;
15584 if (is_plt_seq_reloc (r_type1))
15585 {
15586 bfd_put_32 (output_bfd, NOP, contents + offset);
15587 if (r_type1 == R_PPC64_PLT_PCREL34
15588 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15589 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15590 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15591 break;
15592 }
15593
15594 if (ELF64_R_TYPE (rel[1].r_info) == R_PPC64_PLTCALL)
15595 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15596
15597 if (r_type1 == R_PPC64_REL24_NOTOC
15598 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15599 {
15600 r_type = R_PPC64_NONE;
15601 insn2 = NOP;
15602 }
15603 else
15604 {
15605 rel->r_offset = offset + d_offset;
15606 r_symndx = STN_UNDEF;
15607 r_type = R_PPC64_TPREL16_LO;
15608 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15609 insn2 = 0x38630000; /* addi 3,3,0 */
15610 }
15611 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15612 /* Zap the reloc on the _tls_get_addr call too. */
15613 BFD_ASSERT (offset == rel[1].r_offset);
15614 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15615 bfd_put_32 (input_bfd, insn2, contents + offset);
15616 if (r_type != R_PPC64_NONE)
15617 goto again;
15618 }
15619 break;
15620
15621 case R_PPC64_DTPMOD64:
15622 if (rel + 1 < relend
15623 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15624 && rel[1].r_offset == rel->r_offset + 8)
15625 {
15626 if ((tls_mask & TLS_GD) == 0)
15627 {
15628 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
15629 if ((tls_mask & TLS_GDIE) != 0)
15630 r_type = R_PPC64_TPREL64;
15631 else
15632 {
15633 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15634 r_type = R_PPC64_NONE;
15635 }
15636 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15637 }
15638 }
15639 else
15640 {
15641 if ((tls_mask & TLS_LD) == 0)
15642 {
15643 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15644 r_type = R_PPC64_NONE;
15645 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15646 }
15647 }
15648 break;
15649
15650 case R_PPC64_TPREL64:
15651 if ((tls_mask & TLS_TPREL) == 0)
15652 {
15653 r_type = R_PPC64_NONE;
15654 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15655 }
15656 break;
15657
15658 case R_PPC64_ENTRY:
15659 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15660 if (!bfd_link_pic (info)
15661 && !info->traditional_format
15662 && relocation + 0x80008000 <= 0xffffffff)
15663 {
15664 unsigned int insn1, insn2;
15665
15666 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15667 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15668 if ((insn1 & ~0xfffc) == LD_R2_0R12
15669 && insn2 == ADD_R2_R2_R12)
15670 {
15671 bfd_put_32 (input_bfd,
15672 LIS_R2 + PPC_HA (relocation),
15673 contents + rel->r_offset);
15674 bfd_put_32 (input_bfd,
15675 ADDI_R2_R2 + PPC_LO (relocation),
15676 contents + rel->r_offset + 4);
15677 }
15678 }
15679 else
15680 {
15681 relocation -= (rel->r_offset
15682 + input_section->output_offset
15683 + input_section->output_section->vma);
15684 if (relocation + 0x80008000 <= 0xffffffff)
15685 {
15686 unsigned int insn1, insn2;
15687
15688 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15689 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15690 if ((insn1 & ~0xfffc) == LD_R2_0R12
15691 && insn2 == ADD_R2_R2_R12)
15692 {
15693 bfd_put_32 (input_bfd,
15694 ADDIS_R2_R12 + PPC_HA (relocation),
15695 contents + rel->r_offset);
15696 bfd_put_32 (input_bfd,
15697 ADDI_R2_R2 + PPC_LO (relocation),
15698 contents + rel->r_offset + 4);
15699 }
15700 }
15701 }
15702 break;
15703
15704 case R_PPC64_REL16_HA:
15705 /* If we are generating a non-PIC executable, edit
15706 . 0: addis 2,12,.TOC.-0b@ha
15707 . addi 2,2,.TOC.-0b@l
15708 used by ELFv2 global entry points to set up r2, to
15709 . lis 2,.TOC.@ha
15710 . addi 2,2,.TOC.@l
15711 if .TOC. is in range. */
15712 if (!bfd_link_pic (info)
15713 && !info->traditional_format
15714 && !htab->opd_abi
15715 && rel->r_addend == d_offset
15716 && h != NULL && &h->elf == htab->elf.hgot
15717 && rel + 1 < relend
15718 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15719 && rel[1].r_offset == rel->r_offset + 4
15720 && rel[1].r_addend == rel->r_addend + 4
15721 && relocation + 0x80008000 <= 0xffffffff)
15722 {
15723 unsigned int insn1, insn2;
15724 offset = rel->r_offset - d_offset;
15725 insn1 = bfd_get_32 (input_bfd, contents + offset);
15726 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15727 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15728 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15729 {
15730 r_type = R_PPC64_ADDR16_HA;
15731 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15732 rel->r_addend -= d_offset;
15733 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15734 rel[1].r_addend -= d_offset + 4;
15735 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15736 }
15737 }
15738 break;
15739 }
15740
15741 /* Handle other relocations that tweak non-addend part of insn. */
15742 insn = 0;
15743 max_br_offset = 1 << 25;
15744 addend = rel->r_addend;
15745 reloc_dest = DEST_NORMAL;
15746 switch (r_type)
15747 {
15748 default:
15749 break;
15750
15751 case R_PPC64_TOCSAVE:
15752 if (relocation + addend == (rel->r_offset
15753 + input_section->output_offset
15754 + input_section->output_section->vma)
15755 && tocsave_find (htab, NO_INSERT,
15756 &local_syms, rel, input_bfd))
15757 {
15758 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15759 if (insn == NOP
15760 || insn == CROR_151515 || insn == CROR_313131)
15761 bfd_put_32 (input_bfd,
15762 STD_R2_0R1 + STK_TOC (htab),
15763 contents + rel->r_offset);
15764 }
15765 break;
15766
15767 /* Branch taken prediction relocations. */
15768 case R_PPC64_ADDR14_BRTAKEN:
15769 case R_PPC64_REL14_BRTAKEN:
15770 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15771 /* Fall through. */
15772
15773 /* Branch not taken prediction relocations. */
15774 case R_PPC64_ADDR14_BRNTAKEN:
15775 case R_PPC64_REL14_BRNTAKEN:
15776 insn |= bfd_get_32 (input_bfd,
15777 contents + rel->r_offset) & ~(0x01 << 21);
15778 /* Fall through. */
15779
15780 case R_PPC64_REL14:
15781 max_br_offset = 1 << 15;
15782 /* Fall through. */
15783
15784 case R_PPC64_REL24:
15785 case R_PPC64_REL24_NOTOC:
15786 case R_PPC64_PLTCALL:
15787 case R_PPC64_PLTCALL_NOTOC:
15788 /* Calls to functions with a different TOC, such as calls to
15789 shared objects, need to alter the TOC pointer. This is
15790 done using a linkage stub. A REL24 branching to these
15791 linkage stubs needs to be followed by a nop, as the nop
15792 will be replaced with an instruction to restore the TOC
15793 base pointer. */
15794 fdh = h;
15795 if (h != NULL
15796 && h->oh != NULL
15797 && h->oh->is_func_descriptor)
15798 fdh = ppc_follow_link (h->oh);
15799 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15800 htab);
15801 if ((r_type == R_PPC64_PLTCALL
15802 || r_type == R_PPC64_PLTCALL_NOTOC)
15803 && stub_entry != NULL
15804 && stub_entry->stub_type >= ppc_stub_plt_call
15805 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15806 stub_entry = NULL;
15807
15808 if (stub_entry != NULL
15809 && ((stub_entry->stub_type >= ppc_stub_plt_call
15810 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15811 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15812 || stub_entry->stub_type == ppc_stub_plt_branch_both
15813 || stub_entry->stub_type == ppc_stub_long_branch_r2off
15814 || stub_entry->stub_type == ppc_stub_long_branch_both))
15815 {
15816 bool can_plt_call = false;
15817
15818 if (stub_entry->stub_type == ppc_stub_plt_call
15819 && !htab->opd_abi
15820 && htab->params->plt_localentry0 != 0
15821 && h != NULL
15822 && is_elfv2_localentry0 (&h->elf))
15823 {
15824 /* The function doesn't use or change r2. */
15825 can_plt_call = true;
15826 }
15827 else if (r_type == R_PPC64_REL24_NOTOC)
15828 {
15829 /* NOTOC calls don't need to restore r2. */
15830 can_plt_call = true;
15831 }
15832
15833 /* All of these stubs may modify r2, so there must be a
15834 branch and link followed by a nop. The nop is
15835 replaced by an insn to restore r2. */
15836 else if (rel->r_offset + 8 <= input_section->size)
15837 {
15838 unsigned long br;
15839
15840 br = bfd_get_32 (input_bfd,
15841 contents + rel->r_offset);
15842 if ((br & 1) != 0)
15843 {
15844 unsigned long nop;
15845
15846 nop = bfd_get_32 (input_bfd,
15847 contents + rel->r_offset + 4);
15848 if (nop == LD_R2_0R1 + STK_TOC (htab))
15849 can_plt_call = true;
15850 else if (nop == NOP
15851 || nop == CROR_151515
15852 || nop == CROR_313131)
15853 {
15854 if (h != NULL
15855 && is_tls_get_addr (&h->elf, htab)
15856 && htab->params->tls_get_addr_opt)
15857 {
15858 /* Special stub used, leave nop alone. */
15859 }
15860 else
15861 bfd_put_32 (input_bfd,
15862 LD_R2_0R1 + STK_TOC (htab),
15863 contents + rel->r_offset + 4);
15864 can_plt_call = true;
15865 }
15866 }
15867 }
15868
15869 if (!can_plt_call && h != NULL)
15870 {
15871 const char *name = h->elf.root.root.string;
15872
15873 if (*name == '.')
15874 ++name;
15875
15876 if (startswith (name, "__libc_start_main")
15877 && (name[17] == 0 || name[17] == '@'))
15878 {
15879 /* Allow crt1 branch to go via a toc adjusting
15880 stub. Other calls that never return could do
15881 the same, if we could detect such. */
15882 can_plt_call = true;
15883 }
15884 }
15885
15886 if (!can_plt_call)
15887 {
15888 /* g++ as of 20130507 emits self-calls without a
15889 following nop. This is arguably wrong since we
15890 have conflicting information. On the one hand a
15891 global symbol and on the other a local call
15892 sequence, but don't error for this special case.
15893 It isn't possible to cheaply verify we have
15894 exactly such a call. Allow all calls to the same
15895 section. */
15896 asection *code_sec = sec;
15897
15898 if (get_opd_info (sec) != NULL)
15899 {
15900 bfd_vma off = (relocation + addend
15901 - sec->output_section->vma
15902 - sec->output_offset);
15903
15904 opd_entry_value (sec, off, &code_sec, NULL, false);
15905 }
15906 if (code_sec == input_section)
15907 can_plt_call = true;
15908 }
15909
15910 if (!can_plt_call)
15911 {
15912 if (stub_entry->stub_type >= ppc_stub_plt_call
15913 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15914 info->callbacks->einfo
15915 /* xgettext:c-format */
15916 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15917 "(plt call stub)\n"),
15918 input_bfd, input_section, rel->r_offset, sym_name);
15919 else
15920 info->callbacks->einfo
15921 /* xgettext:c-format */
15922 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15923 "(toc save/adjust stub)\n"),
15924 input_bfd, input_section, rel->r_offset, sym_name);
15925
15926 bfd_set_error (bfd_error_bad_value);
15927 ret = false;
15928 }
15929
15930 if (can_plt_call
15931 && stub_entry->stub_type >= ppc_stub_plt_call
15932 && stub_entry->stub_type <= ppc_stub_plt_call_both)
15933 unresolved_reloc = false;
15934 }
15935
15936 if ((stub_entry == NULL
15937 || stub_entry->stub_type == ppc_stub_long_branch
15938 || stub_entry->stub_type == ppc_stub_plt_branch)
15939 && get_opd_info (sec) != NULL)
15940 {
15941 /* The branch destination is the value of the opd entry. */
15942 bfd_vma off = (relocation + addend
15943 - sec->output_section->vma
15944 - sec->output_offset);
15945 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
15946 if (dest != (bfd_vma) -1)
15947 {
15948 relocation = dest;
15949 addend = 0;
15950 reloc_dest = DEST_OPD;
15951 }
15952 }
15953
15954 /* If the branch is out of reach we ought to have a long
15955 branch stub. */
15956 from = (rel->r_offset
15957 + input_section->output_offset
15958 + input_section->output_section->vma);
15959
15960 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15961 ? fdh->elf.other
15962 : sym->st_other);
15963
15964 if (stub_entry != NULL
15965 && (stub_entry->stub_type == ppc_stub_long_branch
15966 || stub_entry->stub_type == ppc_stub_plt_branch)
15967 && (r_type == R_PPC64_ADDR14_BRTAKEN
15968 || r_type == R_PPC64_ADDR14_BRNTAKEN
15969 || (relocation + addend - from + max_br_offset
15970 < 2 * max_br_offset)))
15971 /* Don't use the stub if this branch is in range. */
15972 stub_entry = NULL;
15973
15974 if (stub_entry != NULL
15975 && (stub_entry->stub_type == ppc_stub_long_branch_notoc
15976 || stub_entry->stub_type == ppc_stub_long_branch_both
15977 || stub_entry->stub_type == ppc_stub_plt_branch_notoc
15978 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15979 && (r_type != R_PPC64_REL24_NOTOC
15980 || ((fdh ? fdh->elf.other : sym->st_other)
15981 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
15982 && (relocation + addend - from + max_br_offset
15983 < 2 * max_br_offset))
15984 stub_entry = NULL;
15985
15986 if (stub_entry != NULL
15987 && (stub_entry->stub_type == ppc_stub_long_branch_r2off
15988 || stub_entry->stub_type == ppc_stub_long_branch_both
15989 || stub_entry->stub_type == ppc_stub_plt_branch_r2off
15990 || stub_entry->stub_type == ppc_stub_plt_branch_both)
15991 && r_type == R_PPC64_REL24_NOTOC
15992 && (relocation + addend - from + max_br_offset
15993 < 2 * max_br_offset))
15994 stub_entry = NULL;
15995
15996 if (stub_entry != NULL)
15997 {
15998 /* Munge up the value and addend so that we call the stub
15999 rather than the procedure directly. */
16000 asection *stub_sec = stub_entry->group->stub_sec;
16001
16002 if (stub_entry->stub_type == ppc_stub_save_res)
16003 relocation += (stub_sec->output_offset
16004 + stub_sec->output_section->vma
16005 + stub_sec->size - htab->sfpr->size
16006 - htab->sfpr->output_offset
16007 - htab->sfpr->output_section->vma);
16008 else
16009 relocation = (stub_entry->stub_offset
16010 + stub_sec->output_offset
16011 + stub_sec->output_section->vma);
16012 addend = 0;
16013 reloc_dest = DEST_STUB;
16014
16015 if ((((stub_entry->stub_type == ppc_stub_plt_call
16016 && ALWAYS_EMIT_R2SAVE)
16017 || stub_entry->stub_type == ppc_stub_plt_call_r2save
16018 || stub_entry->stub_type == ppc_stub_plt_call_both)
16019 && rel + 1 < relend
16020 && rel[1].r_offset == rel->r_offset + 4
16021 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE)
16022 || ((stub_entry->stub_type == ppc_stub_long_branch_both
16023 || stub_entry->stub_type == ppc_stub_plt_branch_both
16024 || stub_entry->stub_type == ppc_stub_plt_call_both)
16025 && r_type == R_PPC64_REL24_NOTOC))
16026 {
16027 /* Skip over the r2 store at the start of the stub. */
16028 if (!(stub_entry->stub_type >= ppc_stub_plt_call
16029 && htab->params->tls_get_addr_opt
16030 && h != NULL
16031 && is_tls_get_addr (&h->elf, htab)))
16032 relocation += 4;
16033 }
16034
16035 if (r_type == R_PPC64_REL24_NOTOC
16036 && (stub_entry->stub_type == ppc_stub_plt_call_notoc
16037 || stub_entry->stub_type == ppc_stub_plt_call_both))
16038 htab->notoc_plt = 1;
16039 }
16040
16041 if (insn != 0)
16042 {
16043 if (is_isa_v2)
16044 {
16045 /* Set 'a' bit. This is 0b00010 in BO field for branch
16046 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16047 for branch on CTR insns (BO == 1a00t or 1a01t). */
16048 if ((insn & (0x14 << 21)) == (0x04 << 21))
16049 insn |= 0x02 << 21;
16050 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16051 insn |= 0x08 << 21;
16052 else
16053 break;
16054 }
16055 else
16056 {
16057 /* Invert 'y' bit if not the default. */
16058 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16059 insn ^= 0x01 << 21;
16060 }
16061
16062 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16063 }
16064
16065 /* NOP out calls to undefined weak functions.
16066 We can thus call a weak function without first
16067 checking whether the function is defined. */
16068 else if (h != NULL
16069 && h->elf.root.type == bfd_link_hash_undefweak
16070 && h->elf.dynindx == -1
16071 && (r_type == R_PPC64_REL24
16072 || r_type == R_PPC64_REL24_NOTOC)
16073 && relocation == 0
16074 && addend == 0)
16075 {
16076 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16077 goto copy_reloc;
16078 }
16079 break;
16080
16081 case R_PPC64_GOT16_DS:
16082 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16083 || !htab->do_toc_opt)
16084 break;
16085 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16086 if (relocation + addend - from + 0x8000 < 0x10000
16087 && sec != NULL
16088 && sec->output_section != NULL
16089 && !discarded_section (sec)
16090 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16091 {
16092 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16093 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16094 {
16095 insn += (14u << 26) - (58u << 26);
16096 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16097 r_type = R_PPC64_TOC16;
16098 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16099 }
16100 }
16101 break;
16102
16103 case R_PPC64_GOT16_LO_DS:
16104 case R_PPC64_GOT16_HA:
16105 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16106 || !htab->do_toc_opt)
16107 break;
16108 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16109 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16110 && sec != NULL
16111 && sec->output_section != NULL
16112 && !discarded_section (sec)
16113 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16114 {
16115 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16116 if (r_type == R_PPC64_GOT16_LO_DS
16117 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16118 {
16119 insn += (14u << 26) - (58u << 26);
16120 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16121 r_type = R_PPC64_TOC16_LO;
16122 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16123 }
16124 else if (r_type == R_PPC64_GOT16_HA
16125 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16126 {
16127 r_type = R_PPC64_TOC16_HA;
16128 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16129 }
16130 }
16131 break;
16132
16133 case R_PPC64_GOT_PCREL34:
16134 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16135 || !htab->do_toc_opt)
16136 break;
16137 from = (rel->r_offset
16138 + input_section->output_section->vma
16139 + input_section->output_offset);
16140 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16141 && sec != NULL
16142 && sec->output_section != NULL
16143 && !discarded_section (sec)
16144 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))))
16145 break;
16146
16147 offset = rel->r_offset;
16148 pinsn = bfd_get_32 (input_bfd, contents + offset);
16149 pinsn <<= 32;
16150 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16151 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16152 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16153 break;
16154
16155 /* Replace with paddi. */
16156 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16157 r_type = R_PPC64_PCREL34;
16158 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16159 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16160 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16161 /* Fall through. */
16162
16163 case R_PPC64_PCREL34:
16164 if (!htab->params->no_pcrel_opt
16165 && rel + 1 < relend
16166 && rel[1].r_offset == rel->r_offset
16167 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16168 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16169 {
16170 offset = rel->r_offset;
16171 pinsn = bfd_get_32 (input_bfd, contents + offset);
16172 pinsn <<= 32;
16173 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16174 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16175 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16176 | (14ULL << 26) /* paddi */))
16177 {
16178 bfd_vma off2 = rel[1].r_addend;
16179 if (off2 == 0)
16180 /* zero means next insn. */
16181 off2 = 8;
16182 off2 += offset;
16183 if (off2 + 4 <= input_section->size)
16184 {
16185 uint64_t pinsn2;
16186 bfd_signed_vma addend_off;
16187 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16188 pinsn2 <<= 32;
16189 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16190 {
16191 if (off2 + 8 > input_section->size)
16192 break;
16193 pinsn2 |= bfd_get_32 (input_bfd,
16194 contents + off2 + 4);
16195 }
16196 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16197 {
16198 addend += addend_off;
16199 rel->r_addend = addend;
16200 bfd_put_32 (input_bfd, pinsn >> 32,
16201 contents + offset);
16202 bfd_put_32 (input_bfd, pinsn,
16203 contents + offset + 4);
16204 bfd_put_32 (input_bfd, pinsn2 >> 32,
16205 contents + off2);
16206 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16207 bfd_put_32 (input_bfd, pinsn2,
16208 contents + off2 + 4);
16209 }
16210 }
16211 }
16212 }
16213 break;
16214 }
16215
16216 tls_type = 0;
16217 save_unresolved_reloc = unresolved_reloc;
16218 switch (r_type)
16219 {
16220 default:
16221 /* xgettext:c-format */
16222 _bfd_error_handler (_("%pB: %s unsupported"),
16223 input_bfd, ppc64_elf_howto_table[r_type]->name);
16224
16225 bfd_set_error (bfd_error_bad_value);
16226 ret = false;
16227 goto copy_reloc;
16228
16229 case R_PPC64_NONE:
16230 case R_PPC64_TLS:
16231 case R_PPC64_TLSGD:
16232 case R_PPC64_TLSLD:
16233 case R_PPC64_TOCSAVE:
16234 case R_PPC64_GNU_VTINHERIT:
16235 case R_PPC64_GNU_VTENTRY:
16236 case R_PPC64_ENTRY:
16237 case R_PPC64_PCREL_OPT:
16238 goto copy_reloc;
16239
16240 /* GOT16 relocations. Like an ADDR16 using the symbol's
16241 address in the GOT as relocation value instead of the
16242 symbol's value itself. Also, create a GOT entry for the
16243 symbol and put the symbol value there. */
16244 case R_PPC64_GOT_TLSGD16:
16245 case R_PPC64_GOT_TLSGD16_LO:
16246 case R_PPC64_GOT_TLSGD16_HI:
16247 case R_PPC64_GOT_TLSGD16_HA:
16248 case R_PPC64_GOT_TLSGD_PCREL34:
16249 tls_type = TLS_TLS | TLS_GD;
16250 goto dogot;
16251
16252 case R_PPC64_GOT_TLSLD16:
16253 case R_PPC64_GOT_TLSLD16_LO:
16254 case R_PPC64_GOT_TLSLD16_HI:
16255 case R_PPC64_GOT_TLSLD16_HA:
16256 case R_PPC64_GOT_TLSLD_PCREL34:
16257 tls_type = TLS_TLS | TLS_LD;
16258 goto dogot;
16259
16260 case R_PPC64_GOT_TPREL16_DS:
16261 case R_PPC64_GOT_TPREL16_LO_DS:
16262 case R_PPC64_GOT_TPREL16_HI:
16263 case R_PPC64_GOT_TPREL16_HA:
16264 case R_PPC64_GOT_TPREL_PCREL34:
16265 tls_type = TLS_TLS | TLS_TPREL;
16266 goto dogot;
16267
16268 case R_PPC64_GOT_DTPREL16_DS:
16269 case R_PPC64_GOT_DTPREL16_LO_DS:
16270 case R_PPC64_GOT_DTPREL16_HI:
16271 case R_PPC64_GOT_DTPREL16_HA:
16272 case R_PPC64_GOT_DTPREL_PCREL34:
16273 tls_type = TLS_TLS | TLS_DTPREL;
16274 goto dogot;
16275
16276 case R_PPC64_GOT16:
16277 case R_PPC64_GOT16_LO:
16278 case R_PPC64_GOT16_HI:
16279 case R_PPC64_GOT16_HA:
16280 case R_PPC64_GOT16_DS:
16281 case R_PPC64_GOT16_LO_DS:
16282 case R_PPC64_GOT_PCREL34:
16283 dogot:
16284 {
16285 /* Relocation is to the entry for this symbol in the global
16286 offset table. */
16287 asection *got;
16288 bfd_vma *offp;
16289 bfd_vma off;
16290 unsigned long indx = 0;
16291 struct got_entry *ent;
16292
16293 if (tls_type == (TLS_TLS | TLS_LD)
16294 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16295 ent = ppc64_tlsld_got (input_bfd);
16296 else
16297 {
16298 if (h != NULL)
16299 {
16300 if (!htab->elf.dynamic_sections_created
16301 || h->elf.dynindx == -1
16302 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16303 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16304 /* This is actually a static link, or it is a
16305 -Bsymbolic link and the symbol is defined
16306 locally, or the symbol was forced to be local
16307 because of a version file. */
16308 ;
16309 else
16310 {
16311 indx = h->elf.dynindx;
16312 unresolved_reloc = false;
16313 }
16314 ent = h->elf.got.glist;
16315 }
16316 else
16317 {
16318 if (local_got_ents == NULL)
16319 abort ();
16320 ent = local_got_ents[r_symndx];
16321 }
16322
16323 for (; ent != NULL; ent = ent->next)
16324 if (ent->addend == orig_rel.r_addend
16325 && ent->owner == input_bfd
16326 && ent->tls_type == tls_type)
16327 break;
16328 }
16329
16330 if (ent == NULL)
16331 abort ();
16332 if (ent->is_indirect)
16333 ent = ent->got.ent;
16334 offp = &ent->got.offset;
16335 got = ppc64_elf_tdata (ent->owner)->got;
16336 if (got == NULL)
16337 abort ();
16338
16339 /* The offset must always be a multiple of 8. We use the
16340 least significant bit to record whether we have already
16341 processed this entry. */
16342 off = *offp;
16343 if ((off & 1) != 0)
16344 off &= ~1;
16345 else
16346 {
16347 /* Generate relocs for the dynamic linker, except in
16348 the case of TLSLD where we'll use one entry per
16349 module. */
16350 asection *relgot;
16351 bool ifunc;
16352
16353 *offp = off | 1;
16354 relgot = NULL;
16355 ifunc = (h != NULL
16356 ? h->elf.type == STT_GNU_IFUNC
16357 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16358 if (ifunc)
16359 {
16360 relgot = htab->elf.irelplt;
16361 if (indx == 0 || is_static_defined (&h->elf))
16362 htab->elf.ifunc_resolvers = true;
16363 }
16364 else if (indx != 0
16365 || (bfd_link_pic (info)
16366 && (h == NULL
16367 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16368 && !(tls_type != 0
16369 && bfd_link_executable (info)
16370 && (h == NULL
16371 || SYMBOL_REFERENCES_LOCAL (info,
16372 &h->elf)))))
16373 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16374 if (relgot != NULL)
16375 {
16376 outrel.r_offset = (got->output_section->vma
16377 + got->output_offset
16378 + off);
16379 outrel.r_addend = orig_rel.r_addend;
16380 if (tls_type & (TLS_LD | TLS_GD))
16381 {
16382 outrel.r_addend = 0;
16383 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16384 if (tls_type == (TLS_TLS | TLS_GD))
16385 {
16386 loc = relgot->contents;
16387 loc += (relgot->reloc_count++
16388 * sizeof (Elf64_External_Rela));
16389 bfd_elf64_swap_reloca_out (output_bfd,
16390 &outrel, loc);
16391 outrel.r_offset += 8;
16392 outrel.r_addend = orig_rel.r_addend;
16393 outrel.r_info
16394 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16395 }
16396 }
16397 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16398 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16399 else if (tls_type == (TLS_TLS | TLS_TPREL))
16400 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16401 else if (indx != 0)
16402 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16403 else
16404 {
16405 if (ifunc)
16406 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16407 else
16408 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16409
16410 /* Write the .got section contents for the sake
16411 of prelink. */
16412 loc = got->contents + off;
16413 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16414 loc);
16415 }
16416
16417 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16418 {
16419 outrel.r_addend += relocation;
16420 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16421 {
16422 if (htab->elf.tls_sec == NULL)
16423 outrel.r_addend = 0;
16424 else
16425 outrel.r_addend -= htab->elf.tls_sec->vma;
16426 }
16427 }
16428 loc = relgot->contents;
16429 loc += (relgot->reloc_count++
16430 * sizeof (Elf64_External_Rela));
16431 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16432 }
16433
16434 /* Init the .got section contents here if we're not
16435 emitting a reloc. */
16436 else
16437 {
16438 relocation += orig_rel.r_addend;
16439 if (tls_type != 0)
16440 {
16441 if (htab->elf.tls_sec == NULL)
16442 relocation = 0;
16443 else
16444 {
16445 if (tls_type & TLS_LD)
16446 relocation = 0;
16447 else
16448 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
16449 if (tls_type & TLS_TPREL)
16450 relocation += DTP_OFFSET - TP_OFFSET;
16451 }
16452
16453 if (tls_type & (TLS_GD | TLS_LD))
16454 {
16455 bfd_put_64 (output_bfd, relocation,
16456 got->contents + off + 8);
16457 relocation = 1;
16458 }
16459 }
16460 bfd_put_64 (output_bfd, relocation,
16461 got->contents + off);
16462 }
16463 }
16464
16465 if (off >= (bfd_vma) -2)
16466 abort ();
16467
16468 relocation = got->output_section->vma + got->output_offset + off;
16469 addend = 0;
16470 if (!(r_type == R_PPC64_GOT_PCREL34
16471 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16472 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16473 || r_type == R_PPC64_GOT_TPREL_PCREL34
16474 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
16475 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
16476 }
16477 break;
16478
16479 case R_PPC64_PLT16_HA:
16480 case R_PPC64_PLT16_HI:
16481 case R_PPC64_PLT16_LO:
16482 case R_PPC64_PLT16_LO_DS:
16483 case R_PPC64_PLT_PCREL34:
16484 case R_PPC64_PLT_PCREL34_NOTOC:
16485 case R_PPC64_PLT32:
16486 case R_PPC64_PLT64:
16487 case R_PPC64_PLTSEQ:
16488 case R_PPC64_PLTSEQ_NOTOC:
16489 case R_PPC64_PLTCALL:
16490 case R_PPC64_PLTCALL_NOTOC:
16491 /* Relocation is to the entry for this symbol in the
16492 procedure linkage table. */
16493 unresolved_reloc = true;
16494 {
16495 struct plt_entry **plt_list = NULL;
16496 if (h != NULL)
16497 plt_list = &h->elf.plt.plist;
16498 else if (local_got_ents != NULL)
16499 {
16500 struct plt_entry **local_plt = (struct plt_entry **)
16501 (local_got_ents + symtab_hdr->sh_info);
16502 plt_list = local_plt + r_symndx;
16503 }
16504 if (plt_list)
16505 {
16506 struct plt_entry *ent;
16507
16508 for (ent = *plt_list; ent != NULL; ent = ent->next)
16509 if (ent->plt.offset != (bfd_vma) -1
16510 && ent->addend == orig_rel.r_addend)
16511 {
16512 asection *plt;
16513 bfd_vma got;
16514
16515 plt = htab->elf.splt;
16516 if (use_local_plt (info, elf_hash_entry (h)))
16517 {
16518 if (h != NULL
16519 ? h->elf.type == STT_GNU_IFUNC
16520 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16521 plt = htab->elf.iplt;
16522 else
16523 plt = htab->pltlocal;
16524 }
16525 relocation = (plt->output_section->vma
16526 + plt->output_offset
16527 + ent->plt.offset);
16528 if (r_type == R_PPC64_PLT16_HA
16529 || r_type == R_PPC64_PLT16_HI
16530 || r_type == R_PPC64_PLT16_LO
16531 || r_type == R_PPC64_PLT16_LO_DS)
16532 {
16533 got = (elf_gp (output_bfd)
16534 + htab->sec_info[input_section->id].toc_off);
16535 relocation -= got;
16536 }
16537 addend = 0;
16538 unresolved_reloc = false;
16539 break;
16540 }
16541 }
16542 }
16543 break;
16544
16545 case R_PPC64_TOC:
16546 /* Relocation value is TOC base. */
16547 relocation = TOCstart;
16548 if (r_symndx == STN_UNDEF)
16549 relocation += htab->sec_info[input_section->id].toc_off;
16550 else if (unresolved_reloc)
16551 ;
16552 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16553 relocation += htab->sec_info[sec->id].toc_off;
16554 else
16555 unresolved_reloc = true;
16556 goto dodyn;
16557
16558 /* TOC16 relocs. We want the offset relative to the TOC base,
16559 which is the address of the start of the TOC plus 0x8000.
16560 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16561 in this order. */
16562 case R_PPC64_TOC16:
16563 case R_PPC64_TOC16_LO:
16564 case R_PPC64_TOC16_HI:
16565 case R_PPC64_TOC16_DS:
16566 case R_PPC64_TOC16_LO_DS:
16567 case R_PPC64_TOC16_HA:
16568 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
16569 if (h != NULL)
16570 goto dodyn;
16571 break;
16572
16573 /* Relocate against the beginning of the section. */
16574 case R_PPC64_SECTOFF:
16575 case R_PPC64_SECTOFF_LO:
16576 case R_PPC64_SECTOFF_HI:
16577 case R_PPC64_SECTOFF_DS:
16578 case R_PPC64_SECTOFF_LO_DS:
16579 case R_PPC64_SECTOFF_HA:
16580 if (sec != NULL)
16581 addend -= sec->output_section->vma;
16582 break;
16583
16584 case R_PPC64_REL16:
16585 case R_PPC64_REL16_LO:
16586 case R_PPC64_REL16_HI:
16587 case R_PPC64_REL16_HA:
16588 case R_PPC64_REL16_HIGH:
16589 case R_PPC64_REL16_HIGHA:
16590 case R_PPC64_REL16_HIGHER:
16591 case R_PPC64_REL16_HIGHERA:
16592 case R_PPC64_REL16_HIGHEST:
16593 case R_PPC64_REL16_HIGHESTA:
16594 case R_PPC64_REL16_HIGHER34:
16595 case R_PPC64_REL16_HIGHERA34:
16596 case R_PPC64_REL16_HIGHEST34:
16597 case R_PPC64_REL16_HIGHESTA34:
16598 case R_PPC64_REL16DX_HA:
16599 case R_PPC64_REL14:
16600 case R_PPC64_REL14_BRNTAKEN:
16601 case R_PPC64_REL14_BRTAKEN:
16602 case R_PPC64_REL24:
16603 case R_PPC64_REL24_NOTOC:
16604 case R_PPC64_PCREL34:
16605 case R_PPC64_PCREL28:
16606 break;
16607
16608 case R_PPC64_TPREL16:
16609 case R_PPC64_TPREL16_LO:
16610 case R_PPC64_TPREL16_HI:
16611 case R_PPC64_TPREL16_HA:
16612 case R_PPC64_TPREL16_DS:
16613 case R_PPC64_TPREL16_LO_DS:
16614 case R_PPC64_TPREL16_HIGH:
16615 case R_PPC64_TPREL16_HIGHA:
16616 case R_PPC64_TPREL16_HIGHER:
16617 case R_PPC64_TPREL16_HIGHERA:
16618 case R_PPC64_TPREL16_HIGHEST:
16619 case R_PPC64_TPREL16_HIGHESTA:
16620 case R_PPC64_TPREL34:
16621 if (h != NULL
16622 && h->elf.root.type == bfd_link_hash_undefweak
16623 && h->elf.dynindx == -1)
16624 {
16625 /* Make this relocation against an undefined weak symbol
16626 resolve to zero. This is really just a tweak, since
16627 code using weak externs ought to check that they are
16628 defined before using them. */
16629 bfd_byte *p = contents + rel->r_offset - d_offset;
16630
16631 insn = bfd_get_32 (input_bfd, p);
16632 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16633 if (insn != 0)
16634 bfd_put_32 (input_bfd, insn, p);
16635 break;
16636 }
16637 if (htab->elf.tls_sec != NULL)
16638 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16639 /* The TPREL16 relocs shouldn't really be used in shared
16640 libs or with non-local symbols as that will result in
16641 DT_TEXTREL being set, but support them anyway. */
16642 goto dodyn;
16643
16644 case R_PPC64_DTPREL16:
16645 case R_PPC64_DTPREL16_LO:
16646 case R_PPC64_DTPREL16_HI:
16647 case R_PPC64_DTPREL16_HA:
16648 case R_PPC64_DTPREL16_DS:
16649 case R_PPC64_DTPREL16_LO_DS:
16650 case R_PPC64_DTPREL16_HIGH:
16651 case R_PPC64_DTPREL16_HIGHA:
16652 case R_PPC64_DTPREL16_HIGHER:
16653 case R_PPC64_DTPREL16_HIGHERA:
16654 case R_PPC64_DTPREL16_HIGHEST:
16655 case R_PPC64_DTPREL16_HIGHESTA:
16656 case R_PPC64_DTPREL34:
16657 if (htab->elf.tls_sec != NULL)
16658 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16659 break;
16660
16661 case R_PPC64_ADDR64_LOCAL:
16662 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16663 ? h->elf.other
16664 : sym->st_other);
16665 break;
16666
16667 case R_PPC64_DTPMOD64:
16668 relocation = 1;
16669 addend = 0;
16670 goto dodyn;
16671
16672 case R_PPC64_TPREL64:
16673 if (htab->elf.tls_sec != NULL)
16674 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16675 goto dodyn;
16676
16677 case R_PPC64_DTPREL64:
16678 if (htab->elf.tls_sec != NULL)
16679 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16680 /* Fall through. */
16681
16682 /* Relocations that may need to be propagated if this is a
16683 dynamic object. */
16684 case R_PPC64_REL30:
16685 case R_PPC64_REL32:
16686 case R_PPC64_REL64:
16687 case R_PPC64_ADDR14:
16688 case R_PPC64_ADDR14_BRNTAKEN:
16689 case R_PPC64_ADDR14_BRTAKEN:
16690 case R_PPC64_ADDR16:
16691 case R_PPC64_ADDR16_DS:
16692 case R_PPC64_ADDR16_HA:
16693 case R_PPC64_ADDR16_HI:
16694 case R_PPC64_ADDR16_HIGH:
16695 case R_PPC64_ADDR16_HIGHA:
16696 case R_PPC64_ADDR16_HIGHER:
16697 case R_PPC64_ADDR16_HIGHERA:
16698 case R_PPC64_ADDR16_HIGHEST:
16699 case R_PPC64_ADDR16_HIGHESTA:
16700 case R_PPC64_ADDR16_LO:
16701 case R_PPC64_ADDR16_LO_DS:
16702 case R_PPC64_ADDR16_HIGHER34:
16703 case R_PPC64_ADDR16_HIGHERA34:
16704 case R_PPC64_ADDR16_HIGHEST34:
16705 case R_PPC64_ADDR16_HIGHESTA34:
16706 case R_PPC64_ADDR24:
16707 case R_PPC64_ADDR32:
16708 case R_PPC64_ADDR64:
16709 case R_PPC64_UADDR16:
16710 case R_PPC64_UADDR32:
16711 case R_PPC64_UADDR64:
16712 case R_PPC64_D34:
16713 case R_PPC64_D34_LO:
16714 case R_PPC64_D34_HI30:
16715 case R_PPC64_D34_HA30:
16716 case R_PPC64_D28:
16717 dodyn:
16718 if ((input_section->flags & SEC_ALLOC) == 0)
16719 break;
16720
16721 if (NO_OPD_RELOCS && is_opd)
16722 break;
16723
16724 if (bfd_link_pic (info)
16725 ? ((h == NULL
16726 || h->elf.dyn_relocs != NULL)
16727 && ((h != NULL && pc_dynrelocs (h))
16728 || must_be_dyn_reloc (info, r_type)))
16729 : (h != NULL
16730 ? h->elf.dyn_relocs != NULL
16731 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16732 {
16733 bool skip, relocate;
16734 asection *sreloc;
16735 bfd_vma out_off;
16736 long indx = 0;
16737
16738 /* When generating a dynamic object, these relocations
16739 are copied into the output file to be resolved at run
16740 time. */
16741
16742 skip = false;
16743 relocate = false;
16744
16745 out_off = _bfd_elf_section_offset (output_bfd, info,
16746 input_section, rel->r_offset);
16747 if (out_off == (bfd_vma) -1)
16748 skip = true;
16749 else if (out_off == (bfd_vma) -2)
16750 skip = true, relocate = true;
16751 out_off += (input_section->output_section->vma
16752 + input_section->output_offset);
16753 outrel.r_offset = out_off;
16754 outrel.r_addend = rel->r_addend;
16755
16756 /* Optimize unaligned reloc use. */
16757 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16758 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16759 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16760 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16761 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16762 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16763 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16764 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16765 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16766
16767 if (skip)
16768 memset (&outrel, 0, sizeof outrel);
16769 else if (h != NULL
16770 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16771 && !is_opd
16772 && r_type != R_PPC64_TOC)
16773 {
16774 indx = h->elf.dynindx;
16775 BFD_ASSERT (indx != -1);
16776 outrel.r_info = ELF64_R_INFO (indx, r_type);
16777 }
16778 else
16779 {
16780 /* This symbol is local, or marked to become local,
16781 or this is an opd section reloc which must point
16782 at a local function. */
16783 outrel.r_addend += relocation;
16784 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16785 {
16786 if (is_opd && h != NULL)
16787 {
16788 /* Lie about opd entries. This case occurs
16789 when building shared libraries and we
16790 reference a function in another shared
16791 lib. The same thing happens for a weak
16792 definition in an application that's
16793 overridden by a strong definition in a
16794 shared lib. (I believe this is a generic
16795 bug in binutils handling of weak syms.)
16796 In these cases we won't use the opd
16797 entry in this lib. */
16798 unresolved_reloc = false;
16799 }
16800 if (!is_opd
16801 && r_type == R_PPC64_ADDR64
16802 && (h != NULL
16803 ? h->elf.type == STT_GNU_IFUNC
16804 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16805 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16806 else
16807 {
16808 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16809
16810 /* We need to relocate .opd contents for ld.so.
16811 Prelink also wants simple and consistent rules
16812 for relocs. This make all RELATIVE relocs have
16813 *r_offset equal to r_addend. */
16814 relocate = true;
16815 }
16816 }
16817 else
16818 {
16819 if (h != NULL
16820 ? h->elf.type == STT_GNU_IFUNC
16821 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16822 {
16823 info->callbacks->einfo
16824 /* xgettext:c-format */
16825 (_("%H: %s for indirect "
16826 "function `%pT' unsupported\n"),
16827 input_bfd, input_section, rel->r_offset,
16828 ppc64_elf_howto_table[r_type]->name,
16829 sym_name);
16830 ret = false;
16831 }
16832 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16833 ;
16834 else if (sec == NULL || sec->owner == NULL)
16835 {
16836 bfd_set_error (bfd_error_bad_value);
16837 return false;
16838 }
16839 else
16840 {
16841 asection *osec = sec->output_section;
16842
16843 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16844 {
16845 /* TLS symbol values are relative to the
16846 TLS segment. Dynamic relocations for
16847 local TLS symbols therefore can't be
16848 reduced to a relocation against their
16849 section symbol because it holds the
16850 address of the section, not a value
16851 relative to the TLS segment. We could
16852 change the .tdata dynamic section symbol
16853 to be zero value but STN_UNDEF works
16854 and is used elsewhere, eg. for TPREL64
16855 GOT relocs against local TLS symbols. */
16856 osec = htab->elf.tls_sec;
16857 indx = 0;
16858 }
16859 else
16860 {
16861 indx = elf_section_data (osec)->dynindx;
16862 if (indx == 0)
16863 {
16864 if ((osec->flags & SEC_READONLY) == 0
16865 && htab->elf.data_index_section != NULL)
16866 osec = htab->elf.data_index_section;
16867 else
16868 osec = htab->elf.text_index_section;
16869 indx = elf_section_data (osec)->dynindx;
16870 }
16871 BFD_ASSERT (indx != 0);
16872 }
16873
16874 /* We are turning this relocation into one
16875 against a section symbol, so subtract out
16876 the output section's address but not the
16877 offset of the input section in the output
16878 section. */
16879 outrel.r_addend -= osec->vma;
16880 }
16881
16882 outrel.r_info = ELF64_R_INFO (indx, r_type);
16883 }
16884 }
16885
16886 sreloc = elf_section_data (input_section)->sreloc;
16887 if (h != NULL
16888 ? h->elf.type == STT_GNU_IFUNC
16889 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16890 {
16891 sreloc = htab->elf.irelplt;
16892 if (indx == 0 || is_static_defined (&h->elf))
16893 htab->elf.ifunc_resolvers = true;
16894 }
16895 if (sreloc == NULL)
16896 abort ();
16897
16898 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16899 >= sreloc->size)
16900 abort ();
16901 loc = sreloc->contents;
16902 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16903 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16904
16905 if (!warned_dynamic
16906 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16907 {
16908 info->callbacks->einfo
16909 /* xgettext:c-format */
16910 (_("%X%P: %pB: %s against %pT "
16911 "is not supported by glibc as a dynamic relocation\n"),
16912 input_bfd,
16913 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16914 sym_name);
16915 warned_dynamic = true;
16916 }
16917
16918 /* If this reloc is against an external symbol, it will
16919 be computed at runtime, so there's no need to do
16920 anything now. However, for the sake of prelink ensure
16921 that the section contents are a known value. */
16922 if (!relocate)
16923 {
16924 unresolved_reloc = false;
16925 /* The value chosen here is quite arbitrary as ld.so
16926 ignores section contents except for the special
16927 case of .opd where the contents might be accessed
16928 before relocation. Choose zero, as that won't
16929 cause reloc overflow. */
16930 relocation = 0;
16931 addend = 0;
16932 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16933 to improve backward compatibility with older
16934 versions of ld. */
16935 if (r_type == R_PPC64_ADDR64)
16936 addend = outrel.r_addend;
16937 /* Adjust pc_relative relocs to have zero in *r_offset. */
16938 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16939 addend = outrel.r_offset;
16940 }
16941 }
16942 break;
16943
16944 case R_PPC64_COPY:
16945 case R_PPC64_GLOB_DAT:
16946 case R_PPC64_JMP_SLOT:
16947 case R_PPC64_JMP_IREL:
16948 case R_PPC64_RELATIVE:
16949 /* We shouldn't ever see these dynamic relocs in relocatable
16950 files. */
16951 /* Fall through. */
16952
16953 case R_PPC64_PLTGOT16:
16954 case R_PPC64_PLTGOT16_DS:
16955 case R_PPC64_PLTGOT16_HA:
16956 case R_PPC64_PLTGOT16_HI:
16957 case R_PPC64_PLTGOT16_LO:
16958 case R_PPC64_PLTGOT16_LO_DS:
16959 case R_PPC64_PLTREL32:
16960 case R_PPC64_PLTREL64:
16961 /* These ones haven't been implemented yet. */
16962
16963 info->callbacks->einfo
16964 /* xgettext:c-format */
16965 (_("%P: %pB: %s is not supported for `%pT'\n"),
16966 input_bfd,
16967 ppc64_elf_howto_table[r_type]->name, sym_name);
16968
16969 bfd_set_error (bfd_error_invalid_operation);
16970 ret = false;
16971 goto copy_reloc;
16972 }
16973
16974 /* Multi-instruction sequences that access the TOC can be
16975 optimized, eg. addis ra,r2,0; addi rb,ra,x;
16976 to nop; addi rb,r2,x; */
16977 switch (r_type)
16978 {
16979 default:
16980 break;
16981
16982 case R_PPC64_GOT_TLSLD16_HI:
16983 case R_PPC64_GOT_TLSGD16_HI:
16984 case R_PPC64_GOT_TPREL16_HI:
16985 case R_PPC64_GOT_DTPREL16_HI:
16986 case R_PPC64_GOT16_HI:
16987 case R_PPC64_TOC16_HI:
16988 /* These relocs would only be useful if building up an
16989 offset to later add to r2, perhaps in an indexed
16990 addressing mode instruction. Don't try to optimize.
16991 Unfortunately, the possibility of someone building up an
16992 offset like this or even with the HA relocs, means that
16993 we need to check the high insn when optimizing the low
16994 insn. */
16995 break;
16996
16997 case R_PPC64_PLTCALL_NOTOC:
16998 if (!unresolved_reloc)
16999 htab->notoc_plt = 1;
17000 /* Fall through. */
17001 case R_PPC64_PLTCALL:
17002 if (unresolved_reloc)
17003 {
17004 /* No plt entry. Make this into a direct call. */
17005 bfd_byte *p = contents + rel->r_offset;
17006 insn = bfd_get_32 (input_bfd, p);
17007 insn &= 1;
17008 bfd_put_32 (input_bfd, B_DOT | insn, p);
17009 if (r_type == R_PPC64_PLTCALL)
17010 bfd_put_32 (input_bfd, NOP, p + 4);
17011 unresolved_reloc = save_unresolved_reloc;
17012 r_type = R_PPC64_REL24;
17013 }
17014 break;
17015
17016 case R_PPC64_PLTSEQ_NOTOC:
17017 case R_PPC64_PLTSEQ:
17018 if (unresolved_reloc)
17019 {
17020 unresolved_reloc = false;
17021 goto nop_it;
17022 }
17023 break;
17024
17025 case R_PPC64_PLT_PCREL34_NOTOC:
17026 if (!unresolved_reloc)
17027 htab->notoc_plt = 1;
17028 /* Fall through. */
17029 case R_PPC64_PLT_PCREL34:
17030 if (unresolved_reloc)
17031 {
17032 bfd_byte *p = contents + rel->r_offset;
17033 bfd_put_32 (input_bfd, PNOP >> 32, p);
17034 bfd_put_32 (input_bfd, PNOP, p + 4);
17035 unresolved_reloc = false;
17036 goto copy_reloc;
17037 }
17038 break;
17039
17040 case R_PPC64_PLT16_HA:
17041 if (unresolved_reloc)
17042 {
17043 unresolved_reloc = false;
17044 goto nop_it;
17045 }
17046 /* Fall through. */
17047 case R_PPC64_GOT_TLSLD16_HA:
17048 case R_PPC64_GOT_TLSGD16_HA:
17049 case R_PPC64_GOT_TPREL16_HA:
17050 case R_PPC64_GOT_DTPREL16_HA:
17051 case R_PPC64_GOT16_HA:
17052 case R_PPC64_TOC16_HA:
17053 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17054 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
17055 {
17056 bfd_byte *p;
17057 nop_it:
17058 p = contents + (rel->r_offset & ~3);
17059 bfd_put_32 (input_bfd, NOP, p);
17060 goto copy_reloc;
17061 }
17062 break;
17063
17064 case R_PPC64_PLT16_LO:
17065 case R_PPC64_PLT16_LO_DS:
17066 if (unresolved_reloc)
17067 {
17068 unresolved_reloc = false;
17069 goto nop_it;
17070 }
17071 /* Fall through. */
17072 case R_PPC64_GOT_TLSLD16_LO:
17073 case R_PPC64_GOT_TLSGD16_LO:
17074 case R_PPC64_GOT_TPREL16_LO_DS:
17075 case R_PPC64_GOT_DTPREL16_LO_DS:
17076 case R_PPC64_GOT16_LO:
17077 case R_PPC64_GOT16_LO_DS:
17078 case R_PPC64_TOC16_LO:
17079 case R_PPC64_TOC16_LO_DS:
17080 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17081 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
17082 {
17083 bfd_byte *p = contents + (rel->r_offset & ~3);
17084 insn = bfd_get_32 (input_bfd, p);
17085 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17086 {
17087 /* Transform addic to addi when we change reg. */
17088 insn &= ~((0x3fu << 26) | (0x1f << 16));
17089 insn |= (14u << 26) | (2 << 16);
17090 }
17091 else
17092 {
17093 insn &= ~(0x1f << 16);
17094 insn |= 2 << 16;
17095 }
17096 bfd_put_32 (input_bfd, insn, p);
17097 }
17098 break;
17099
17100 case R_PPC64_TPREL16_HA:
17101 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17102 {
17103 bfd_byte *p = contents + (rel->r_offset & ~3);
17104 bfd_put_32 (input_bfd, NOP, p);
17105 goto copy_reloc;
17106 }
17107 break;
17108
17109 case R_PPC64_TPREL16_LO:
17110 case R_PPC64_TPREL16_LO_DS:
17111 if (htab->do_tls_opt && relocation + addend + 0x8000 < 0x10000)
17112 {
17113 bfd_byte *p = contents + (rel->r_offset & ~3);
17114 insn = bfd_get_32 (input_bfd, p);
17115 insn &= ~(0x1f << 16);
17116 insn |= 13 << 16;
17117 bfd_put_32 (input_bfd, insn, p);
17118 }
17119 break;
17120 }
17121
17122 /* Do any further special processing. */
17123 switch (r_type)
17124 {
17125 default:
17126 break;
17127
17128 case R_PPC64_REL16_HA:
17129 case R_PPC64_REL16_HIGHA:
17130 case R_PPC64_REL16_HIGHERA:
17131 case R_PPC64_REL16_HIGHESTA:
17132 case R_PPC64_REL16DX_HA:
17133 case R_PPC64_ADDR16_HA:
17134 case R_PPC64_ADDR16_HIGHA:
17135 case R_PPC64_ADDR16_HIGHERA:
17136 case R_PPC64_ADDR16_HIGHESTA:
17137 case R_PPC64_TOC16_HA:
17138 case R_PPC64_SECTOFF_HA:
17139 case R_PPC64_TPREL16_HA:
17140 case R_PPC64_TPREL16_HIGHA:
17141 case R_PPC64_TPREL16_HIGHERA:
17142 case R_PPC64_TPREL16_HIGHESTA:
17143 case R_PPC64_DTPREL16_HA:
17144 case R_PPC64_DTPREL16_HIGHA:
17145 case R_PPC64_DTPREL16_HIGHERA:
17146 case R_PPC64_DTPREL16_HIGHESTA:
17147 /* It's just possible that this symbol is a weak symbol
17148 that's not actually defined anywhere. In that case,
17149 'sec' would be NULL, and we should leave the symbol
17150 alone (it will be set to zero elsewhere in the link). */
17151 if (sec == NULL)
17152 break;
17153 /* Fall through. */
17154
17155 case R_PPC64_GOT16_HA:
17156 case R_PPC64_PLTGOT16_HA:
17157 case R_PPC64_PLT16_HA:
17158 case R_PPC64_GOT_TLSGD16_HA:
17159 case R_PPC64_GOT_TLSLD16_HA:
17160 case R_PPC64_GOT_TPREL16_HA:
17161 case R_PPC64_GOT_DTPREL16_HA:
17162 /* Add 0x10000 if sign bit in 0:15 is set.
17163 Bits 0:15 are not used. */
17164 addend += 0x8000;
17165 break;
17166
17167 case R_PPC64_D34_HA30:
17168 case R_PPC64_ADDR16_HIGHERA34:
17169 case R_PPC64_ADDR16_HIGHESTA34:
17170 case R_PPC64_REL16_HIGHERA34:
17171 case R_PPC64_REL16_HIGHESTA34:
17172 if (sec != NULL)
17173 addend += 1ULL << 33;
17174 break;
17175
17176 case R_PPC64_ADDR16_DS:
17177 case R_PPC64_ADDR16_LO_DS:
17178 case R_PPC64_GOT16_DS:
17179 case R_PPC64_GOT16_LO_DS:
17180 case R_PPC64_PLT16_LO_DS:
17181 case R_PPC64_SECTOFF_DS:
17182 case R_PPC64_SECTOFF_LO_DS:
17183 case R_PPC64_TOC16_DS:
17184 case R_PPC64_TOC16_LO_DS:
17185 case R_PPC64_PLTGOT16_DS:
17186 case R_PPC64_PLTGOT16_LO_DS:
17187 case R_PPC64_GOT_TPREL16_DS:
17188 case R_PPC64_GOT_TPREL16_LO_DS:
17189 case R_PPC64_GOT_DTPREL16_DS:
17190 case R_PPC64_GOT_DTPREL16_LO_DS:
17191 case R_PPC64_TPREL16_DS:
17192 case R_PPC64_TPREL16_LO_DS:
17193 case R_PPC64_DTPREL16_DS:
17194 case R_PPC64_DTPREL16_LO_DS:
17195 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17196 mask = 3;
17197 /* If this reloc is against an lq, lxv, or stxv insn, then
17198 the value must be a multiple of 16. This is somewhat of
17199 a hack, but the "correct" way to do this by defining _DQ
17200 forms of all the _DS relocs bloats all reloc switches in
17201 this file. It doesn't make much sense to use these
17202 relocs in data, so testing the insn should be safe. */
17203 if ((insn & (0x3fu << 26)) == (56u << 26)
17204 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17205 mask = 15;
17206 relocation += addend;
17207 addend = insn & (mask ^ 3);
17208 if ((relocation & mask) != 0)
17209 {
17210 relocation ^= relocation & mask;
17211 info->callbacks->einfo
17212 /* xgettext:c-format */
17213 (_("%H: error: %s not a multiple of %u\n"),
17214 input_bfd, input_section, rel->r_offset,
17215 ppc64_elf_howto_table[r_type]->name,
17216 mask + 1);
17217 bfd_set_error (bfd_error_bad_value);
17218 ret = false;
17219 goto copy_reloc;
17220 }
17221 break;
17222 }
17223
17224 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17225 because such sections are not SEC_ALLOC and thus ld.so will
17226 not process them. */
17227 howto = ppc64_elf_howto_table[(int) r_type];
17228 if (unresolved_reloc
17229 && !((input_section->flags & SEC_DEBUGGING) != 0
17230 && h->elf.def_dynamic)
17231 && _bfd_elf_section_offset (output_bfd, info, input_section,
17232 rel->r_offset) != (bfd_vma) -1)
17233 {
17234 info->callbacks->einfo
17235 /* xgettext:c-format */
17236 (_("%H: unresolvable %s against `%pT'\n"),
17237 input_bfd, input_section, rel->r_offset,
17238 howto->name,
17239 h->elf.root.root.string);
17240 ret = false;
17241 }
17242
17243 /* 16-bit fields in insns mostly have signed values, but a
17244 few insns have 16-bit unsigned values. Really, we should
17245 have different reloc types. */
17246 if (howto->complain_on_overflow != complain_overflow_dont
17247 && howto->dst_mask == 0xffff
17248 && (input_section->flags & SEC_CODE) != 0)
17249 {
17250 enum complain_overflow complain = complain_overflow_signed;
17251
17252 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17253 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17254 complain = complain_overflow_bitfield;
17255 else if (howto->rightshift == 0
17256 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17257 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17258 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17259 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17260 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17261 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17262 complain = complain_overflow_unsigned;
17263 if (howto->complain_on_overflow != complain)
17264 {
17265 alt_howto = *howto;
17266 alt_howto.complain_on_overflow = complain;
17267 howto = &alt_howto;
17268 }
17269 }
17270
17271 switch (r_type)
17272 {
17273 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17274 case R_PPC64_D34:
17275 case R_PPC64_D34_LO:
17276 case R_PPC64_D34_HI30:
17277 case R_PPC64_D34_HA30:
17278 case R_PPC64_PCREL34:
17279 case R_PPC64_GOT_PCREL34:
17280 case R_PPC64_TPREL34:
17281 case R_PPC64_DTPREL34:
17282 case R_PPC64_GOT_TLSGD_PCREL34:
17283 case R_PPC64_GOT_TLSLD_PCREL34:
17284 case R_PPC64_GOT_TPREL_PCREL34:
17285 case R_PPC64_GOT_DTPREL_PCREL34:
17286 case R_PPC64_PLT_PCREL34:
17287 case R_PPC64_PLT_PCREL34_NOTOC:
17288 case R_PPC64_D28:
17289 case R_PPC64_PCREL28:
17290 if (rel->r_offset + 8 > input_section->size)
17291 r = bfd_reloc_outofrange;
17292 else
17293 {
17294 relocation += addend;
17295 if (howto->pc_relative)
17296 relocation -= (rel->r_offset
17297 + input_section->output_offset
17298 + input_section->output_section->vma);
17299 relocation >>= howto->rightshift;
17300
17301 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17302 pinsn <<= 32;
17303 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17304
17305 pinsn &= ~howto->dst_mask;
17306 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17307 & howto->dst_mask);
17308 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17309 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17310 r = bfd_reloc_ok;
17311 if (howto->complain_on_overflow == complain_overflow_signed
17312 && (relocation + (1ULL << (howto->bitsize - 1))
17313 >= 1ULL << howto->bitsize))
17314 r = bfd_reloc_overflow;
17315 }
17316 break;
17317
17318 case R_PPC64_REL16DX_HA:
17319 if (rel->r_offset + 4 > input_section->size)
17320 r = bfd_reloc_outofrange;
17321 else
17322 {
17323 relocation += addend;
17324 relocation -= (rel->r_offset
17325 + input_section->output_offset
17326 + input_section->output_section->vma);
17327 relocation = (bfd_signed_vma) relocation >> 16;
17328 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17329 insn &= ~0x1fffc1;
17330 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17331 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17332 r = bfd_reloc_ok;
17333 if (relocation + 0x8000 > 0xffff)
17334 r = bfd_reloc_overflow;
17335 }
17336 break;
17337
17338 default:
17339 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17340 contents, rel->r_offset,
17341 relocation, addend);
17342 }
17343
17344 if (r != bfd_reloc_ok)
17345 {
17346 char *more_info = NULL;
17347 const char *reloc_name = howto->name;
17348
17349 if (reloc_dest != DEST_NORMAL)
17350 {
17351 more_info = bfd_malloc (strlen (reloc_name) + 8);
17352 if (more_info != NULL)
17353 {
17354 strcpy (more_info, reloc_name);
17355 strcat (more_info, (reloc_dest == DEST_OPD
17356 ? " (OPD)" : " (stub)"));
17357 reloc_name = more_info;
17358 }
17359 }
17360
17361 if (r == bfd_reloc_overflow)
17362 {
17363 /* On code like "if (foo) foo();" don't report overflow
17364 on a branch to zero when foo is undefined. */
17365 if (!warned
17366 && (reloc_dest == DEST_STUB
17367 || !(h != NULL
17368 && (h->elf.root.type == bfd_link_hash_undefweak
17369 || h->elf.root.type == bfd_link_hash_undefined)
17370 && is_branch_reloc (r_type))))
17371 info->callbacks->reloc_overflow
17372 (info, (struct bfd_link_hash_entry *) h, sym_name,
17373 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17374 rel->r_offset);
17375 }
17376 else
17377 {
17378 info->callbacks->einfo
17379 /* xgettext:c-format */
17380 (_("%H: %s against `%pT': error %d\n"),
17381 input_bfd, input_section, rel->r_offset,
17382 reloc_name, sym_name, (int) r);
17383 ret = false;
17384 }
17385 free (more_info);
17386 }
17387 copy_reloc:
17388 if (wrel != rel)
17389 *wrel = *rel;
17390 }
17391
17392 if (wrel != rel)
17393 {
17394 Elf_Internal_Shdr *rel_hdr;
17395 size_t deleted = rel - wrel;
17396
17397 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17398 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17399 if (rel_hdr->sh_size == 0)
17400 {
17401 /* It is too late to remove an empty reloc section. Leave
17402 one NONE reloc.
17403 ??? What is wrong with an empty section??? */
17404 rel_hdr->sh_size = rel_hdr->sh_entsize;
17405 deleted -= 1;
17406 }
17407 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17408 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17409 input_section->reloc_count -= deleted;
17410 }
17411
17412 /* If we're emitting relocations, then shortly after this function
17413 returns, reloc offsets and addends for this section will be
17414 adjusted. Worse, reloc symbol indices will be for the output
17415 file rather than the input. Save a copy of the relocs for
17416 opd_entry_value. */
17417 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
17418 {
17419 bfd_size_type amt;
17420 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17421 rel = bfd_alloc (input_bfd, amt);
17422 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17423 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
17424 if (rel == NULL)
17425 return false;
17426 memcpy (rel, relocs, amt);
17427 }
17428 return ret;
17429 }
17430
17431 /* Adjust the value of any local symbols in opd sections. */
17432
17433 static int
17434 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17435 const char *name ATTRIBUTE_UNUSED,
17436 Elf_Internal_Sym *elfsym,
17437 asection *input_sec,
17438 struct elf_link_hash_entry *h)
17439 {
17440 struct _opd_sec_data *opd;
17441 long adjust;
17442 bfd_vma value;
17443
17444 if (h != NULL)
17445 return 1;
17446
17447 opd = get_opd_info (input_sec);
17448 if (opd == NULL || opd->adjust == NULL)
17449 return 1;
17450
17451 value = elfsym->st_value - input_sec->output_offset;
17452 if (!bfd_link_relocatable (info))
17453 value -= input_sec->output_section->vma;
17454
17455 adjust = opd->adjust[OPD_NDX (value)];
17456 if (adjust == -1)
17457 return 2;
17458
17459 elfsym->st_value += adjust;
17460 return 1;
17461 }
17462
17463 /* Finish up dynamic symbol handling. We set the contents of various
17464 dynamic sections here. */
17465
17466 static bool
17467 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17468 struct bfd_link_info *info,
17469 struct elf_link_hash_entry *h,
17470 Elf_Internal_Sym *sym)
17471 {
17472 struct ppc_link_hash_table *htab;
17473 struct plt_entry *ent;
17474
17475 htab = ppc_hash_table (info);
17476 if (htab == NULL)
17477 return false;
17478
17479 if (!htab->opd_abi && !h->def_regular)
17480 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17481 if (ent->plt.offset != (bfd_vma) -1)
17482 {
17483 /* Mark the symbol as undefined, rather than as
17484 defined in glink. Leave the value if there were
17485 any relocations where pointer equality matters
17486 (this is a clue for the dynamic linker, to make
17487 function pointer comparisons work between an
17488 application and shared library), otherwise set it
17489 to zero. */
17490 sym->st_shndx = SHN_UNDEF;
17491 if (!h->pointer_equality_needed)
17492 sym->st_value = 0;
17493 else if (!h->ref_regular_nonweak)
17494 {
17495 /* This breaks function pointer comparisons, but
17496 that is better than breaking tests for a NULL
17497 function pointer. */
17498 sym->st_value = 0;
17499 }
17500 break;
17501 }
17502
17503 if (h->needs_copy
17504 && (h->root.type == bfd_link_hash_defined
17505 || h->root.type == bfd_link_hash_defweak)
17506 && (h->root.u.def.section == htab->elf.sdynbss
17507 || h->root.u.def.section == htab->elf.sdynrelro))
17508 {
17509 /* This symbol needs a copy reloc. Set it up. */
17510 Elf_Internal_Rela rela;
17511 asection *srel;
17512 bfd_byte *loc;
17513
17514 if (h->dynindx == -1)
17515 abort ();
17516
17517 rela.r_offset = defined_sym_val (h);
17518 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17519 rela.r_addend = 0;
17520 if (h->root.u.def.section == htab->elf.sdynrelro)
17521 srel = htab->elf.sreldynrelro;
17522 else
17523 srel = htab->elf.srelbss;
17524 loc = srel->contents;
17525 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
17526 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
17527 }
17528
17529 return true;
17530 }
17531
17532 /* Used to decide how to sort relocs in an optimal manner for the
17533 dynamic linker, before writing them out. */
17534
17535 static enum elf_reloc_type_class
17536 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17537 const asection *rel_sec,
17538 const Elf_Internal_Rela *rela)
17539 {
17540 enum elf_ppc64_reloc_type r_type;
17541 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17542
17543 if (rel_sec == htab->elf.irelplt)
17544 return reloc_class_ifunc;
17545
17546 r_type = ELF64_R_TYPE (rela->r_info);
17547 switch (r_type)
17548 {
17549 case R_PPC64_RELATIVE:
17550 return reloc_class_relative;
17551 case R_PPC64_JMP_SLOT:
17552 return reloc_class_plt;
17553 case R_PPC64_COPY:
17554 return reloc_class_copy;
17555 default:
17556 return reloc_class_normal;
17557 }
17558 }
17559
17560 /* Finish up the dynamic sections. */
17561
17562 static bool
17563 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17564 struct bfd_link_info *info)
17565 {
17566 struct ppc_link_hash_table *htab;
17567 bfd *dynobj;
17568 asection *sdyn;
17569
17570 htab = ppc_hash_table (info);
17571 if (htab == NULL)
17572 return false;
17573
17574 dynobj = htab->elf.dynobj;
17575 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17576
17577 if (htab->elf.dynamic_sections_created)
17578 {
17579 Elf64_External_Dyn *dyncon, *dynconend;
17580
17581 if (sdyn == NULL || htab->elf.sgot == NULL)
17582 abort ();
17583
17584 dyncon = (Elf64_External_Dyn *) sdyn->contents;
17585 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
17586 for (; dyncon < dynconend; dyncon++)
17587 {
17588 Elf_Internal_Dyn dyn;
17589 asection *s;
17590
17591 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17592
17593 switch (dyn.d_tag)
17594 {
17595 default:
17596 continue;
17597
17598 case DT_PPC64_GLINK:
17599 s = htab->glink;
17600 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17601 /* We stupidly defined DT_PPC64_GLINK to be the start
17602 of glink rather than the first entry point, which is
17603 what ld.so needs, and now have a bigger stub to
17604 support automatic multiple TOCs. */
17605 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
17606 break;
17607
17608 case DT_PPC64_OPD:
17609 s = bfd_get_section_by_name (output_bfd, ".opd");
17610 if (s == NULL)
17611 continue;
17612 dyn.d_un.d_ptr = s->vma;
17613 break;
17614
17615 case DT_PPC64_OPT:
17616 if ((htab->do_multi_toc && htab->multi_toc_needed)
17617 || htab->notoc_plt)
17618 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
17619 if (htab->has_plt_localentry0)
17620 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
17621 break;
17622
17623 case DT_PPC64_OPDSZ:
17624 s = bfd_get_section_by_name (output_bfd, ".opd");
17625 if (s == NULL)
17626 continue;
17627 dyn.d_un.d_val = s->size;
17628 break;
17629
17630 case DT_PLTGOT:
17631 s = htab->elf.splt;
17632 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17633 break;
17634
17635 case DT_JMPREL:
17636 s = htab->elf.srelplt;
17637 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17638 break;
17639
17640 case DT_PLTRELSZ:
17641 dyn.d_un.d_val = htab->elf.srelplt->size;
17642 break;
17643
17644 case DT_TEXTREL:
17645 if (htab->elf.ifunc_resolvers)
17646 info->callbacks->einfo
17647 (_("%P: warning: text relocations and GNU indirect "
17648 "functions may result in a segfault at runtime\n"));
17649 continue;
17650 }
17651
17652 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
17653 }
17654 }
17655
17656 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17657 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
17658 {
17659 /* Fill in the first entry in the global offset table.
17660 We use it to hold the link-time TOCbase. */
17661 bfd_put_64 (output_bfd,
17662 elf_gp (output_bfd) + TOC_BASE_OFF,
17663 htab->elf.sgot->contents);
17664
17665 /* Set .got entry size. */
17666 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17667 = 8;
17668 }
17669
17670 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17671 && htab->elf.splt->output_section != bfd_abs_section_ptr)
17672 {
17673 /* Set .plt entry size. */
17674 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
17675 = PLT_ENTRY_SIZE (htab);
17676 }
17677
17678 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17679 brlt ourselves if emitrelocations. */
17680 if (htab->brlt != NULL
17681 && htab->brlt->reloc_count != 0
17682 && !_bfd_elf_link_output_relocs (output_bfd,
17683 htab->brlt,
17684 elf_section_data (htab->brlt)->rela.hdr,
17685 elf_section_data (htab->brlt)->relocs,
17686 NULL))
17687 return false;
17688
17689 if (htab->glink != NULL
17690 && htab->glink->reloc_count != 0
17691 && !_bfd_elf_link_output_relocs (output_bfd,
17692 htab->glink,
17693 elf_section_data (htab->glink)->rela.hdr,
17694 elf_section_data (htab->glink)->relocs,
17695 NULL))
17696 return false;
17697
17698
17699 if (htab->glink_eh_frame != NULL
17700 && htab->glink_eh_frame->size != 0
17701 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17702 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17703 htab->glink_eh_frame,
17704 htab->glink_eh_frame->contents))
17705 return false;
17706
17707 /* We need to handle writing out multiple GOT sections ourselves,
17708 since we didn't add them to DYNOBJ. We know dynobj is the first
17709 bfd. */
17710 while ((dynobj = dynobj->link.next) != NULL)
17711 {
17712 asection *s;
17713
17714 if (!is_ppc64_elf (dynobj))
17715 continue;
17716
17717 s = ppc64_elf_tdata (dynobj)->got;
17718 if (s != NULL
17719 && s->size != 0
17720 && s->output_section != bfd_abs_section_ptr
17721 && !bfd_set_section_contents (output_bfd, s->output_section,
17722 s->contents, s->output_offset,
17723 s->size))
17724 return false;
17725 s = ppc64_elf_tdata (dynobj)->relgot;
17726 if (s != NULL
17727 && s->size != 0
17728 && s->output_section != bfd_abs_section_ptr
17729 && !bfd_set_section_contents (output_bfd, s->output_section,
17730 s->contents, s->output_offset,
17731 s->size))
17732 return false;
17733 }
17734
17735 return true;
17736 }
17737
17738 #include "elf64-target.h"
17739
17740 /* FreeBSD support */
17741
17742 #undef TARGET_LITTLE_SYM
17743 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
17744 #undef TARGET_LITTLE_NAME
17745 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
17746
17747 #undef TARGET_BIG_SYM
17748 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
17749 #undef TARGET_BIG_NAME
17750 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17751
17752 #undef ELF_OSABI
17753 #define ELF_OSABI ELFOSABI_FREEBSD
17754
17755 #undef elf64_bed
17756 #define elf64_bed elf64_powerpc_fbsd_bed
17757
17758 #include "elf64-target.h"
This page took 0.41703 seconds and 4 git commands to generate.