Automatic date update in version.in
[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 /* The assembler should generate a full set of section symbols even
29 when they appear unused. The linux kernel build tool recordmcount
30 needs them. */
31 #define TARGET_KEEP_UNUSED_SECTION_SYMBOLS true
32
33 #include "sysdep.h"
34 #include <stdarg.h>
35 #include "bfd.h"
36 #include "bfdlink.h"
37 #include "libbfd.h"
38 #include "elf-bfd.h"
39 #include "elf/ppc64.h"
40 #include "elf64-ppc.h"
41 #include "dwarf2.h"
42
43 /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
44 #define OCTETS_PER_BYTE(ABFD, SEC) 1
45
46 static bfd_reloc_status_type ppc64_elf_ha_reloc
47 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
48 static bfd_reloc_status_type ppc64_elf_branch_reloc
49 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
50 static bfd_reloc_status_type ppc64_elf_brtaken_reloc
51 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52 static bfd_reloc_status_type ppc64_elf_sectoff_reloc
53 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
54 static bfd_reloc_status_type ppc64_elf_sectoff_ha_reloc
55 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
56 static bfd_reloc_status_type ppc64_elf_toc_reloc
57 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
58 static bfd_reloc_status_type ppc64_elf_toc_ha_reloc
59 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
60 static bfd_reloc_status_type ppc64_elf_toc64_reloc
61 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
62 static bfd_reloc_status_type ppc64_elf_prefix_reloc
63 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
64 static bfd_reloc_status_type ppc64_elf_unhandled_reloc
65 (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
66 static bfd_vma opd_entry_value
67 (asection *, bfd_vma, asection **, bfd_vma *, bool);
68
69 #define TARGET_LITTLE_SYM powerpc_elf64_le_vec
70 #define TARGET_LITTLE_NAME "elf64-powerpcle"
71 #define TARGET_BIG_SYM powerpc_elf64_vec
72 #define TARGET_BIG_NAME "elf64-powerpc"
73 #define ELF_ARCH bfd_arch_powerpc
74 #define ELF_TARGET_ID PPC64_ELF_DATA
75 #define ELF_MACHINE_CODE EM_PPC64
76 #define ELF_MAXPAGESIZE 0x10000
77 #define ELF_COMMONPAGESIZE 0x1000
78 #define ELF_RELROPAGESIZE ELF_MAXPAGESIZE
79 #define elf_info_to_howto ppc64_elf_info_to_howto
80
81 #define elf_backend_want_got_sym 0
82 #define elf_backend_want_plt_sym 0
83 #define elf_backend_plt_alignment 3
84 #define elf_backend_plt_not_loaded 1
85 #define elf_backend_got_header_size 8
86 #define elf_backend_want_dynrelro 1
87 #define elf_backend_can_gc_sections 1
88 #define elf_backend_can_refcount 1
89 #define elf_backend_rela_normal 1
90 #define elf_backend_dtrel_excludes_plt 1
91 #define elf_backend_default_execstack 0
92
93 #define bfd_elf64_mkobject ppc64_elf_mkobject
94 #define bfd_elf64_bfd_reloc_type_lookup ppc64_elf_reloc_type_lookup
95 #define bfd_elf64_bfd_reloc_name_lookup ppc64_elf_reloc_name_lookup
96 #define bfd_elf64_bfd_merge_private_bfd_data ppc64_elf_merge_private_bfd_data
97 #define bfd_elf64_bfd_print_private_bfd_data ppc64_elf_print_private_bfd_data
98 #define bfd_elf64_new_section_hook ppc64_elf_new_section_hook
99 #define bfd_elf64_bfd_link_hash_table_create ppc64_elf_link_hash_table_create
100 #define bfd_elf64_get_synthetic_symtab ppc64_elf_get_synthetic_symtab
101 #define bfd_elf64_bfd_link_just_syms ppc64_elf_link_just_syms
102 #define bfd_elf64_bfd_gc_sections ppc64_elf_gc_sections
103
104 #define elf_backend_object_p ppc64_elf_object_p
105 #define elf_backend_grok_prstatus ppc64_elf_grok_prstatus
106 #define elf_backend_grok_psinfo ppc64_elf_grok_psinfo
107 #define elf_backend_write_core_note ppc64_elf_write_core_note
108 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections
109 #define elf_backend_copy_indirect_symbol ppc64_elf_copy_indirect_symbol
110 #define elf_backend_add_symbol_hook ppc64_elf_add_symbol_hook
111 #define elf_backend_check_directives ppc64_elf_before_check_relocs
112 #define elf_backend_notice_as_needed ppc64_elf_notice_as_needed
113 #define elf_backend_archive_symbol_lookup ppc64_elf_archive_symbol_lookup
114 #define elf_backend_check_relocs ppc64_elf_check_relocs
115 #define elf_backend_relocs_compatible _bfd_elf_relocs_compatible
116 #define elf_backend_gc_keep ppc64_elf_gc_keep
117 #define elf_backend_gc_mark_dynamic_ref ppc64_elf_gc_mark_dynamic_ref
118 #define elf_backend_gc_mark_hook ppc64_elf_gc_mark_hook
119 #define elf_backend_adjust_dynamic_symbol ppc64_elf_adjust_dynamic_symbol
120 #define elf_backend_hide_symbol ppc64_elf_hide_symbol
121 #define elf_backend_maybe_function_sym ppc64_elf_maybe_function_sym
122 #define elf_backend_always_size_sections ppc64_elf_edit
123 #define elf_backend_size_dynamic_sections ppc64_elf_size_dynamic_sections
124 #define elf_backend_hash_symbol ppc64_elf_hash_symbol
125 #define elf_backend_init_index_section _bfd_elf_init_2_index_sections
126 #define elf_backend_action_discarded ppc64_elf_action_discarded
127 #define elf_backend_relocate_section ppc64_elf_relocate_section
128 #define elf_backend_finish_dynamic_symbol ppc64_elf_finish_dynamic_symbol
129 #define elf_backend_reloc_type_class ppc64_elf_reloc_type_class
130 #define elf_backend_finish_dynamic_sections ppc64_elf_finish_dynamic_sections
131 #define elf_backend_link_output_symbol_hook ppc64_elf_output_symbol_hook
132 #define elf_backend_special_sections ppc64_elf_special_sections
133 #define elf_backend_section_flags ppc64_elf_section_flags
134 #define elf_backend_merge_symbol_attribute ppc64_elf_merge_symbol_attribute
135 #define elf_backend_merge_symbol ppc64_elf_merge_symbol
136 #define elf_backend_get_reloc_section bfd_get_section_by_name
137
138 /* The name of the dynamic interpreter. This is put in the .interp
139 section. */
140 #define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
141
142 /* The size in bytes of an entry in the procedure linkage table. */
143 #define PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 8)
144 #define LOCAL_PLT_ENTRY_SIZE(htab) (htab->opd_abi ? 16 : 8)
145
146 /* The initial size of the plt reserved for the dynamic linker. */
147 #define PLT_INITIAL_ENTRY_SIZE(htab) (htab->opd_abi ? 24 : 16)
148
149 /* Offsets to some stack save slots. */
150 #define STK_LR 16
151 #define STK_TOC(htab) (htab->opd_abi ? 40 : 24)
152 /* This one is dodgy. ELFv2 does not have a linker word, so use the
153 CR save slot. Used only by optimised __tls_get_addr call stub,
154 relying on __tls_get_addr_opt not saving CR.. */
155 #define STK_LINKER(htab) (htab->opd_abi ? 32 : 8)
156
157 /* TOC base pointers offset from start of TOC. */
158 #define TOC_BASE_OFF 0x8000
159 /* TOC base alignment. */
160 #define TOC_BASE_ALIGN 256
161
162 /* Offset of tp and dtp pointers from start of TLS block. */
163 #define TP_OFFSET 0x7000
164 #define DTP_OFFSET 0x8000
165
166 /* .plt call stub instructions. The normal stub is like this, but
167 sometimes the .plt entry crosses a 64k boundary and we need to
168 insert an addi to adjust r11. */
169 #define STD_R2_0R1 0xf8410000 /* std %r2,0+40(%r1) */
170 #define ADDIS_R11_R2 0x3d620000 /* addis %r11,%r2,xxx@ha */
171 #define LD_R12_0R11 0xe98b0000 /* ld %r12,xxx+0@l(%r11) */
172 #define MTCTR_R12 0x7d8903a6 /* mtctr %r12 */
173 #define LD_R2_0R11 0xe84b0000 /* ld %r2,xxx+8@l(%r11) */
174 #define LD_R11_0R11 0xe96b0000 /* ld %r11,xxx+16@l(%r11) */
175 #define BCTR 0x4e800420 /* bctr */
176
177 #define ADDI_R11_R11 0x396b0000 /* addi %r11,%r11,off@l */
178 #define ADDI_R12_R11 0x398b0000 /* addi %r12,%r11,off@l */
179 #define ADDI_R12_R12 0x398c0000 /* addi %r12,%r12,off@l */
180 #define ADDIS_R2_R2 0x3c420000 /* addis %r2,%r2,off@ha */
181 #define ADDI_R2_R2 0x38420000 /* addi %r2,%r2,off@l */
182
183 #define XOR_R2_R12_R12 0x7d826278 /* xor %r2,%r12,%r12 */
184 #define ADD_R11_R11_R2 0x7d6b1214 /* add %r11,%r11,%r2 */
185 #define XOR_R11_R12_R12 0x7d8b6278 /* xor %r11,%r12,%r12 */
186 #define ADD_R2_R2_R11 0x7c425a14 /* add %r2,%r2,%r11 */
187 #define CMPLDI_R2_0 0x28220000 /* cmpldi %r2,0 */
188 #define BNECTR 0x4ca20420 /* bnectr+ */
189 #define BNECTR_P4 0x4ce20420 /* bnectr+ */
190
191 #define LD_R12_0R2 0xe9820000 /* ld %r12,xxx+0(%r2) */
192 #define LD_R11_0R2 0xe9620000 /* ld %r11,xxx+0(%r2) */
193 #define LD_R2_0R2 0xe8420000 /* ld %r2,xxx+0(%r2) */
194
195 #define LD_R2_0R1 0xe8410000 /* ld %r2,0(%r1) */
196 #define LD_R2_0R12 0xe84c0000 /* ld %r2,0(%r12) */
197 #define ADD_R2_R2_R12 0x7c426214 /* add %r2,%r2,%r12 */
198
199 #define LI_R11_0 0x39600000 /* li %r11,0 */
200 #define LIS_R2 0x3c400000 /* lis %r2,xxx@ha */
201 #define LIS_R11 0x3d600000 /* lis %r11,xxx@ha */
202 #define LIS_R12 0x3d800000 /* lis %r12,xxx@ha */
203 #define ADDIS_R2_R12 0x3c4c0000 /* addis %r2,%r12,xxx@ha */
204 #define ADDIS_R12_R2 0x3d820000 /* addis %r12,%r2,xxx@ha */
205 #define ADDIS_R12_R11 0x3d8b0000 /* addis %r12,%r11,xxx@ha */
206 #define ADDIS_R12_R12 0x3d8c0000 /* addis %r12,%r12,xxx@ha */
207 #define ORIS_R12_R12_0 0x658c0000 /* oris %r12,%r12,xxx@hi */
208 #define ORI_R11_R11_0 0x616b0000 /* ori %r11,%r11,xxx@l */
209 #define ORI_R12_R12_0 0x618c0000 /* ori %r12,%r12,xxx@l */
210 #define LD_R12_0R12 0xe98c0000 /* ld %r12,xxx@l(%r12) */
211 #define SLDI_R11_R11_34 0x796b1746 /* sldi %r11,%r11,34 */
212 #define SLDI_R12_R12_32 0x799c07c6 /* sldi %r12,%r12,32 */
213 #define LDX_R12_R11_R12 0x7d8b602a /* ldx %r12,%r11,%r12 */
214 #define ADD_R12_R11_R12 0x7d8b6214 /* add %r12,%r11,%r12 */
215 #define PADDI_R12_PC 0x0610000039800000ULL
216 #define PLD_R12_PC 0x04100000e5800000ULL
217 #define PNOP 0x0700000000000000ULL
218
219 /* __glink_PLTresolve stub instructions. We enter with the index in
220 R0 for ELFv1, and the address of a glink branch in R12 for ELFv2. */
221 #define GLINK_PLTRESOLVE_SIZE(htab) \
222 (8u + (htab->opd_abi ? 11 * 4 : htab->has_plt_localentry0 ? 14 * 4 : 13 * 4))
223 /* 0: */
224 /* .quad plt0-1f */
225 /* __glink: */
226 #define MFLR_R12 0x7d8802a6 /* mflr %12 */
227 #define BCL_20_31 0x429f0005 /* bcl 20,31,1f */
228 /* 1: */
229 #define MFLR_R11 0x7d6802a6 /* mflr %11 */
230 /* ld %2,(0b-1b)(%11) */
231 #define MTLR_R12 0x7d8803a6 /* mtlr %12 */
232 #define ADD_R11_R2_R11 0x7d625a14 /* add %11,%2,%11 */
233 /* ld %12,0(%11) */
234 /* ld %2,8(%11) */
235 /* mtctr %12 */
236 /* ld %11,16(%11) */
237 /* bctr */
238
239 #define MFLR_R0 0x7c0802a6 /* mflr %r0 */
240 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
241 #define SUB_R12_R12_R11 0x7d8b6050 /* subf %r12,%r11,%r12 */
242 #define ADDI_R0_R12 0x380c0000 /* addi %r0,%r12,0 */
243 #define SRDI_R0_R0_2 0x7800f082 /* rldicl %r0,%r0,62,2 */
244 #define LD_R0_0R11 0xe80b0000 /* ld %r0,0(%r11) */
245 #define ADD_R11_R0_R11 0x7d605a14 /* add %r11,%r0,%r11 */
246
247 /* Pad with this. */
248 #define NOP 0x60000000
249
250 /* Some other nops. */
251 #define CROR_151515 0x4def7b82
252 #define CROR_313131 0x4ffffb82
253
254 /* .glink entries for the first 32k functions are two instructions. */
255 #define LI_R0_0 0x38000000 /* li %r0,0 */
256 #define B_DOT 0x48000000 /* b . */
257
258 /* After that, we need two instructions to load the index, followed by
259 a branch. */
260 #define LIS_R0_0 0x3c000000 /* lis %r0,0 */
261 #define ORI_R0_R0_0 0x60000000 /* ori %r0,%r0,0 */
262
263 /* Instructions used by the save and restore reg functions. */
264 #define STD_R0_0R1 0xf8010000 /* std %r0,0(%r1) */
265 #define STD_R0_0R12 0xf80c0000 /* std %r0,0(%r12) */
266 #define LD_R0_0R1 0xe8010000 /* ld %r0,0(%r1) */
267 #define LD_R0_0R12 0xe80c0000 /* ld %r0,0(%r12) */
268 #define STFD_FR0_0R1 0xd8010000 /* stfd %fr0,0(%r1) */
269 #define LFD_FR0_0R1 0xc8010000 /* lfd %fr0,0(%r1) */
270 #define LI_R12_0 0x39800000 /* li %r12,0 */
271 #define STVX_VR0_R12_R0 0x7c0c01ce /* stvx %v0,%r12,%r0 */
272 #define LVX_VR0_R12_R0 0x7c0c00ce /* lvx %v0,%r12,%r0 */
273 #define MTLR_R0 0x7c0803a6 /* mtlr %r0 */
274 #define BLR 0x4e800020 /* blr */
275
276 /* Since .opd is an array of descriptors and each entry will end up
277 with identical R_PPC64_RELATIVE relocs, there is really no need to
278 propagate .opd relocs; The dynamic linker should be taught to
279 relocate .opd without reloc entries. */
280 #ifndef NO_OPD_RELOCS
281 #define NO_OPD_RELOCS 0
282 #endif
283
284 #ifndef ARRAY_SIZE
285 #define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
286 #endif
287
288 static inline int
289 abiversion (bfd *abfd)
290 {
291 return elf_elfheader (abfd)->e_flags & EF_PPC64_ABI;
292 }
293
294 static inline void
295 set_abiversion (bfd *abfd, int ver)
296 {
297 elf_elfheader (abfd)->e_flags &= ~EF_PPC64_ABI;
298 elf_elfheader (abfd)->e_flags |= ver & EF_PPC64_ABI;
299 }
300 \f
301 /* Relocation HOWTO's. */
302 /* Like other ELF RELA targets that don't apply multiple
303 field-altering relocations to the same localation, src_mask is
304 always zero and pcrel_offset is the same as pc_relative.
305 PowerPC can always use a zero bitpos, even when the field is not at
306 the LSB. For example, a REL24 could use rightshift=2, bisize=24
307 and bitpos=2 which matches the ABI description, or as we do here,
308 rightshift=0, bitsize=26 and bitpos=0. */
309 #define HOW(type, size, bitsize, mask, rightshift, pc_relative, \
310 complain, special_func) \
311 HOWTO (type, rightshift, size, bitsize, pc_relative, 0, \
312 complain_overflow_ ## complain, special_func, \
313 #type, false, 0, mask, pc_relative)
314
315 static reloc_howto_type *ppc64_elf_howto_table[(int) R_PPC64_max];
316
317 static reloc_howto_type ppc64_elf_howto_raw[] =
318 {
319 /* This reloc does nothing. */
320 HOW (R_PPC64_NONE, 3, 0, 0, 0, false, dont,
321 bfd_elf_generic_reloc),
322
323 /* A standard 32 bit relocation. */
324 HOW (R_PPC64_ADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
325 bfd_elf_generic_reloc),
326
327 /* An absolute 26 bit branch; the lower two bits must be zero.
328 FIXME: we don't check that, we just clear them. */
329 HOW (R_PPC64_ADDR24, 2, 26, 0x03fffffc, 0, false, bitfield,
330 bfd_elf_generic_reloc),
331
332 /* A standard 16 bit relocation. */
333 HOW (R_PPC64_ADDR16, 1, 16, 0xffff, 0, false, bitfield,
334 bfd_elf_generic_reloc),
335
336 /* A 16 bit relocation without overflow. */
337 HOW (R_PPC64_ADDR16_LO, 1, 16, 0xffff, 0, false, dont,
338 bfd_elf_generic_reloc),
339
340 /* Bits 16-31 of an address. */
341 HOW (R_PPC64_ADDR16_HI, 1, 16, 0xffff, 16, false, signed,
342 bfd_elf_generic_reloc),
343
344 /* Bits 16-31 of an address, plus 1 if the contents of the low 16
345 bits, treated as a signed number, is negative. */
346 HOW (R_PPC64_ADDR16_HA, 1, 16, 0xffff, 16, false, signed,
347 ppc64_elf_ha_reloc),
348
349 /* An absolute 16 bit branch; the lower two bits must be zero.
350 FIXME: we don't check that, we just clear them. */
351 HOW (R_PPC64_ADDR14, 2, 16, 0x0000fffc, 0, false, signed,
352 ppc64_elf_branch_reloc),
353
354 /* An absolute 16 bit branch, for which bit 10 should be set to
355 indicate that the branch is expected to be taken. The lower two
356 bits must be zero. */
357 HOW (R_PPC64_ADDR14_BRTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
358 ppc64_elf_brtaken_reloc),
359
360 /* An absolute 16 bit branch, for which bit 10 should be set to
361 indicate that the branch is not expected to be taken. The lower
362 two bits must be zero. */
363 HOW (R_PPC64_ADDR14_BRNTAKEN, 2, 16, 0x0000fffc, 0, false, signed,
364 ppc64_elf_brtaken_reloc),
365
366 /* A relative 26 bit branch; the lower two bits must be zero. */
367 HOW (R_PPC64_REL24, 2, 26, 0x03fffffc, 0, true, signed,
368 ppc64_elf_branch_reloc),
369
370 /* A variant of R_PPC64_REL24, used when r2 is not the toc pointer. */
371 HOW (R_PPC64_REL24_NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
372 ppc64_elf_branch_reloc),
373
374 /* Another variant, when p10 insns can't be used on stubs. */
375 HOW (R_PPC64_REL24_P9NOTOC, 2, 26, 0x03fffffc, 0, true, signed,
376 ppc64_elf_branch_reloc),
377
378 /* A relative 16 bit branch; the lower two bits must be zero. */
379 HOW (R_PPC64_REL14, 2, 16, 0x0000fffc, 0, true, signed,
380 ppc64_elf_branch_reloc),
381
382 /* A relative 16 bit branch. Bit 10 should be set to indicate that
383 the branch is expected to be taken. The lower two bits must be
384 zero. */
385 HOW (R_PPC64_REL14_BRTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
386 ppc64_elf_brtaken_reloc),
387
388 /* A relative 16 bit branch. Bit 10 should be set to indicate that
389 the branch is not expected to be taken. The lower two bits must
390 be zero. */
391 HOW (R_PPC64_REL14_BRNTAKEN, 2, 16, 0x0000fffc, 0, true, signed,
392 ppc64_elf_brtaken_reloc),
393
394 /* Like R_PPC64_ADDR16, but referring to the GOT table entry for the
395 symbol. */
396 HOW (R_PPC64_GOT16, 1, 16, 0xffff, 0, false, signed,
397 ppc64_elf_unhandled_reloc),
398
399 /* Like R_PPC64_ADDR16_LO, but referring to the GOT table entry for
400 the symbol. */
401 HOW (R_PPC64_GOT16_LO, 1, 16, 0xffff, 0, false, dont,
402 ppc64_elf_unhandled_reloc),
403
404 /* Like R_PPC64_ADDR16_HI, but referring to the GOT table entry for
405 the symbol. */
406 HOW (R_PPC64_GOT16_HI, 1, 16, 0xffff, 16, false, signed,
407 ppc64_elf_unhandled_reloc),
408
409 /* Like R_PPC64_ADDR16_HA, but referring to the GOT table entry for
410 the symbol. */
411 HOW (R_PPC64_GOT16_HA, 1, 16, 0xffff, 16, false, signed,
412 ppc64_elf_unhandled_reloc),
413
414 /* This is used only by the dynamic linker. The symbol should exist
415 both in the object being run and in some shared library. The
416 dynamic linker copies the data addressed by the symbol from the
417 shared library into the object, because the object being
418 run has to have the data at some particular address. */
419 HOW (R_PPC64_COPY, 0, 0, 0, 0, false, dont,
420 ppc64_elf_unhandled_reloc),
421
422 /* Like R_PPC64_ADDR64, but used when setting global offset table
423 entries. */
424 HOW (R_PPC64_GLOB_DAT, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
425 ppc64_elf_unhandled_reloc),
426
427 /* Created by the link editor. Marks a procedure linkage table
428 entry for a symbol. */
429 HOW (R_PPC64_JMP_SLOT, 0, 0, 0, 0, false, dont,
430 ppc64_elf_unhandled_reloc),
431
432 /* Used only by the dynamic linker. When the object is run, this
433 doubleword64 is set to the load address of the object, plus the
434 addend. */
435 HOW (R_PPC64_RELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
436 bfd_elf_generic_reloc),
437
438 /* Like R_PPC64_ADDR32, but may be unaligned. */
439 HOW (R_PPC64_UADDR32, 2, 32, 0xffffffff, 0, false, bitfield,
440 bfd_elf_generic_reloc),
441
442 /* Like R_PPC64_ADDR16, but may be unaligned. */
443 HOW (R_PPC64_UADDR16, 1, 16, 0xffff, 0, false, bitfield,
444 bfd_elf_generic_reloc),
445
446 /* 32-bit PC relative. */
447 HOW (R_PPC64_REL32, 2, 32, 0xffffffff, 0, true, signed,
448 bfd_elf_generic_reloc),
449
450 /* 32-bit relocation to the symbol's procedure linkage table. */
451 HOW (R_PPC64_PLT32, 2, 32, 0xffffffff, 0, false, bitfield,
452 ppc64_elf_unhandled_reloc),
453
454 /* 32-bit PC relative relocation to the symbol's procedure linkage table.
455 FIXME: R_PPC64_PLTREL32 not supported. */
456 HOW (R_PPC64_PLTREL32, 2, 32, 0xffffffff, 0, true, signed,
457 ppc64_elf_unhandled_reloc),
458
459 /* Like R_PPC64_ADDR16_LO, but referring to the PLT table entry for
460 the symbol. */
461 HOW (R_PPC64_PLT16_LO, 1, 16, 0xffff, 0, false, dont,
462 ppc64_elf_unhandled_reloc),
463
464 /* Like R_PPC64_ADDR16_HI, but referring to the PLT table entry for
465 the symbol. */
466 HOW (R_PPC64_PLT16_HI, 1, 16, 0xffff, 16, false, signed,
467 ppc64_elf_unhandled_reloc),
468
469 /* Like R_PPC64_ADDR16_HA, but referring to the PLT table entry for
470 the symbol. */
471 HOW (R_PPC64_PLT16_HA, 1, 16, 0xffff, 16, false, signed,
472 ppc64_elf_unhandled_reloc),
473
474 /* 16-bit section relative relocation. */
475 HOW (R_PPC64_SECTOFF, 1, 16, 0xffff, 0, false, signed,
476 ppc64_elf_sectoff_reloc),
477
478 /* Like R_PPC64_SECTOFF, but no overflow warning. */
479 HOW (R_PPC64_SECTOFF_LO, 1, 16, 0xffff, 0, false, dont,
480 ppc64_elf_sectoff_reloc),
481
482 /* 16-bit upper half section relative relocation. */
483 HOW (R_PPC64_SECTOFF_HI, 1, 16, 0xffff, 16, false, signed,
484 ppc64_elf_sectoff_reloc),
485
486 /* 16-bit upper half adjusted section relative relocation. */
487 HOW (R_PPC64_SECTOFF_HA, 1, 16, 0xffff, 16, false, signed,
488 ppc64_elf_sectoff_ha_reloc),
489
490 /* Like R_PPC64_REL24 without touching the two least significant bits. */
491 HOW (R_PPC64_REL30, 2, 30, 0xfffffffc, 2, true, dont,
492 bfd_elf_generic_reloc),
493
494 /* Relocs in the 64-bit PowerPC ELF ABI, not in the 32-bit ABI. */
495
496 /* A standard 64-bit relocation. */
497 HOW (R_PPC64_ADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
498 bfd_elf_generic_reloc),
499
500 /* The bits 32-47 of an address. */
501 HOW (R_PPC64_ADDR16_HIGHER, 1, 16, 0xffff, 32, false, dont,
502 bfd_elf_generic_reloc),
503
504 /* The bits 32-47 of an address, plus 1 if the contents of the low
505 16 bits, treated as a signed number, is negative. */
506 HOW (R_PPC64_ADDR16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
507 ppc64_elf_ha_reloc),
508
509 /* The bits 48-63 of an address. */
510 HOW (R_PPC64_ADDR16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
511 bfd_elf_generic_reloc),
512
513 /* The bits 48-63 of an address, plus 1 if the contents of the low
514 16 bits, treated as a signed number, is negative. */
515 HOW (R_PPC64_ADDR16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
516 ppc64_elf_ha_reloc),
517
518 /* Like ADDR64, but may be unaligned. */
519 HOW (R_PPC64_UADDR64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
520 bfd_elf_generic_reloc),
521
522 /* 64-bit relative relocation. */
523 HOW (R_PPC64_REL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
524 bfd_elf_generic_reloc),
525
526 /* 64-bit relocation to the symbol's procedure linkage table. */
527 HOW (R_PPC64_PLT64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
528 ppc64_elf_unhandled_reloc),
529
530 /* 64-bit PC relative relocation to the symbol's procedure linkage
531 table. */
532 /* FIXME: R_PPC64_PLTREL64 not supported. */
533 HOW (R_PPC64_PLTREL64, 4, 64, 0xffffffffffffffffULL, 0, true, dont,
534 ppc64_elf_unhandled_reloc),
535
536 /* 16 bit TOC-relative relocation. */
537 /* R_PPC64_TOC16 47 half16* S + A - .TOC. */
538 HOW (R_PPC64_TOC16, 1, 16, 0xffff, 0, false, signed,
539 ppc64_elf_toc_reloc),
540
541 /* 16 bit TOC-relative relocation without overflow. */
542 /* R_PPC64_TOC16_LO 48 half16 #lo (S + A - .TOC.) */
543 HOW (R_PPC64_TOC16_LO, 1, 16, 0xffff, 0, false, dont,
544 ppc64_elf_toc_reloc),
545
546 /* 16 bit TOC-relative relocation, high 16 bits. */
547 /* R_PPC64_TOC16_HI 49 half16 #hi (S + A - .TOC.) */
548 HOW (R_PPC64_TOC16_HI, 1, 16, 0xffff, 16, false, signed,
549 ppc64_elf_toc_reloc),
550
551 /* 16 bit TOC-relative relocation, high 16 bits, plus 1 if the
552 contents of the low 16 bits, treated as a signed number, is
553 negative. */
554 /* R_PPC64_TOC16_HA 50 half16 #ha (S + A - .TOC.) */
555 HOW (R_PPC64_TOC16_HA, 1, 16, 0xffff, 16, false, signed,
556 ppc64_elf_toc_ha_reloc),
557
558 /* 64-bit relocation; insert value of TOC base (.TOC.). */
559 /* R_PPC64_TOC 51 doubleword64 .TOC. */
560 HOW (R_PPC64_TOC, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
561 ppc64_elf_toc64_reloc),
562
563 /* Like R_PPC64_GOT16, but also informs the link editor that the
564 value to relocate may (!) refer to a PLT entry which the link
565 editor (a) may replace with the symbol value. If the link editor
566 is unable to fully resolve the symbol, it may (b) create a PLT
567 entry and store the address to the new PLT entry in the GOT.
568 This permits lazy resolution of function symbols at run time.
569 The link editor may also skip all of this and just (c) emit a
570 R_PPC64_GLOB_DAT to tie the symbol to the GOT entry. */
571 /* FIXME: R_PPC64_PLTGOT16 not implemented. */
572 HOW (R_PPC64_PLTGOT16, 1, 16, 0xffff, 0, false,signed,
573 ppc64_elf_unhandled_reloc),
574
575 /* Like R_PPC64_PLTGOT16, but without overflow. */
576 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
577 HOW (R_PPC64_PLTGOT16_LO, 1, 16, 0xffff, 0, false, dont,
578 ppc64_elf_unhandled_reloc),
579
580 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address. */
581 /* FIXME: R_PPC64_PLTGOT16_HI not implemented. */
582 HOW (R_PPC64_PLTGOT16_HI, 1, 16, 0xffff, 16, false, signed,
583 ppc64_elf_unhandled_reloc),
584
585 /* Like R_PPC64_PLT_GOT16, but using bits 16-31 of the address, plus
586 1 if the contents of the low 16 bits, treated as a signed number,
587 is negative. */
588 /* FIXME: R_PPC64_PLTGOT16_HA not implemented. */
589 HOW (R_PPC64_PLTGOT16_HA, 1, 16, 0xffff, 16, false, signed,
590 ppc64_elf_unhandled_reloc),
591
592 /* Like R_PPC64_ADDR16, but for instructions with a DS field. */
593 HOW (R_PPC64_ADDR16_DS, 1, 16, 0xfffc, 0, false, signed,
594 bfd_elf_generic_reloc),
595
596 /* Like R_PPC64_ADDR16_LO, but for instructions with a DS field. */
597 HOW (R_PPC64_ADDR16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
598 bfd_elf_generic_reloc),
599
600 /* Like R_PPC64_GOT16, but for instructions with a DS field. */
601 HOW (R_PPC64_GOT16_DS, 1, 16, 0xfffc, 0, false, signed,
602 ppc64_elf_unhandled_reloc),
603
604 /* Like R_PPC64_GOT16_LO, but for instructions with a DS field. */
605 HOW (R_PPC64_GOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
606 ppc64_elf_unhandled_reloc),
607
608 /* Like R_PPC64_PLT16_LO, but for instructions with a DS field. */
609 HOW (R_PPC64_PLT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
610 ppc64_elf_unhandled_reloc),
611
612 /* Like R_PPC64_SECTOFF, but for instructions with a DS field. */
613 HOW (R_PPC64_SECTOFF_DS, 1, 16, 0xfffc, 0, false, signed,
614 ppc64_elf_sectoff_reloc),
615
616 /* Like R_PPC64_SECTOFF_LO, but for instructions with a DS field. */
617 HOW (R_PPC64_SECTOFF_LO_DS, 1, 16, 0xfffc, 0, false, dont,
618 ppc64_elf_sectoff_reloc),
619
620 /* Like R_PPC64_TOC16, but for instructions with a DS field. */
621 HOW (R_PPC64_TOC16_DS, 1, 16, 0xfffc, 0, false, signed,
622 ppc64_elf_toc_reloc),
623
624 /* Like R_PPC64_TOC16_LO, but for instructions with a DS field. */
625 HOW (R_PPC64_TOC16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
626 ppc64_elf_toc_reloc),
627
628 /* Like R_PPC64_PLTGOT16, but for instructions with a DS field. */
629 /* FIXME: R_PPC64_PLTGOT16_DS not implemented. */
630 HOW (R_PPC64_PLTGOT16_DS, 1, 16, 0xfffc, 0, false, signed,
631 ppc64_elf_unhandled_reloc),
632
633 /* Like R_PPC64_PLTGOT16_LO, but for instructions with a DS field. */
634 /* FIXME: R_PPC64_PLTGOT16_LO not implemented. */
635 HOW (R_PPC64_PLTGOT16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
636 ppc64_elf_unhandled_reloc),
637
638 /* Marker relocs for TLS. */
639 HOW (R_PPC64_TLS, 2, 32, 0, 0, false, dont,
640 bfd_elf_generic_reloc),
641
642 HOW (R_PPC64_TLSGD, 2, 32, 0, 0, false, dont,
643 bfd_elf_generic_reloc),
644
645 HOW (R_PPC64_TLSLD, 2, 32, 0, 0, false, dont,
646 bfd_elf_generic_reloc),
647
648 /* Marker reloc for optimizing r2 save in prologue rather than on
649 each plt call stub. */
650 HOW (R_PPC64_TOCSAVE, 2, 32, 0, 0, false, dont,
651 bfd_elf_generic_reloc),
652
653 /* Marker relocs on inline plt call instructions. */
654 HOW (R_PPC64_PLTSEQ, 2, 32, 0, 0, false, dont,
655 bfd_elf_generic_reloc),
656
657 HOW (R_PPC64_PLTCALL, 2, 32, 0, 0, false, dont,
658 bfd_elf_generic_reloc),
659
660 /* Computes the load module index of the load module that contains the
661 definition of its TLS sym. */
662 HOW (R_PPC64_DTPMOD64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
663 ppc64_elf_unhandled_reloc),
664
665 /* Computes a dtv-relative displacement, the difference between the value
666 of sym+add and the base address of the thread-local storage block that
667 contains the definition of sym, minus 0x8000. */
668 HOW (R_PPC64_DTPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
669 ppc64_elf_unhandled_reloc),
670
671 /* A 16 bit dtprel reloc. */
672 HOW (R_PPC64_DTPREL16, 1, 16, 0xffff, 0, false, signed,
673 ppc64_elf_unhandled_reloc),
674
675 /* Like DTPREL16, but no overflow. */
676 HOW (R_PPC64_DTPREL16_LO, 1, 16, 0xffff, 0, false, dont,
677 ppc64_elf_unhandled_reloc),
678
679 /* Like DTPREL16_LO, but next higher group of 16 bits. */
680 HOW (R_PPC64_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
681 ppc64_elf_unhandled_reloc),
682
683 /* Like DTPREL16_HI, but adjust for low 16 bits. */
684 HOW (R_PPC64_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
685 ppc64_elf_unhandled_reloc),
686
687 /* Like DTPREL16_HI, but next higher group of 16 bits. */
688 HOW (R_PPC64_DTPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
689 ppc64_elf_unhandled_reloc),
690
691 /* Like DTPREL16_HIGHER, but adjust for low 16 bits. */
692 HOW (R_PPC64_DTPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
693 ppc64_elf_unhandled_reloc),
694
695 /* Like DTPREL16_HIGHER, but next higher group of 16 bits. */
696 HOW (R_PPC64_DTPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
697 ppc64_elf_unhandled_reloc),
698
699 /* Like DTPREL16_HIGHEST, but adjust for low 16 bits. */
700 HOW (R_PPC64_DTPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
701 ppc64_elf_unhandled_reloc),
702
703 /* Like DTPREL16, but for insns with a DS field. */
704 HOW (R_PPC64_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
705 ppc64_elf_unhandled_reloc),
706
707 /* Like DTPREL16_DS, but no overflow. */
708 HOW (R_PPC64_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
709 ppc64_elf_unhandled_reloc),
710
711 /* Computes a tp-relative displacement, the difference between the value of
712 sym+add and the value of the thread pointer (r13). */
713 HOW (R_PPC64_TPREL64, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
714 ppc64_elf_unhandled_reloc),
715
716 /* A 16 bit tprel reloc. */
717 HOW (R_PPC64_TPREL16, 1, 16, 0xffff, 0, false, signed,
718 ppc64_elf_unhandled_reloc),
719
720 /* Like TPREL16, but no overflow. */
721 HOW (R_PPC64_TPREL16_LO, 1, 16, 0xffff, 0, false, dont,
722 ppc64_elf_unhandled_reloc),
723
724 /* Like TPREL16_LO, but next higher group of 16 bits. */
725 HOW (R_PPC64_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
726 ppc64_elf_unhandled_reloc),
727
728 /* Like TPREL16_HI, but adjust for low 16 bits. */
729 HOW (R_PPC64_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
730 ppc64_elf_unhandled_reloc),
731
732 /* Like TPREL16_HI, but next higher group of 16 bits. */
733 HOW (R_PPC64_TPREL16_HIGHER, 1, 16, 0xffff, 32, false, dont,
734 ppc64_elf_unhandled_reloc),
735
736 /* Like TPREL16_HIGHER, but adjust for low 16 bits. */
737 HOW (R_PPC64_TPREL16_HIGHERA, 1, 16, 0xffff, 32, false, dont,
738 ppc64_elf_unhandled_reloc),
739
740 /* Like TPREL16_HIGHER, but next higher group of 16 bits. */
741 HOW (R_PPC64_TPREL16_HIGHEST, 1, 16, 0xffff, 48, false, dont,
742 ppc64_elf_unhandled_reloc),
743
744 /* Like TPREL16_HIGHEST, but adjust for low 16 bits. */
745 HOW (R_PPC64_TPREL16_HIGHESTA, 1, 16, 0xffff, 48, false, dont,
746 ppc64_elf_unhandled_reloc),
747
748 /* Like TPREL16, but for insns with a DS field. */
749 HOW (R_PPC64_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
750 ppc64_elf_unhandled_reloc),
751
752 /* Like TPREL16_DS, but no overflow. */
753 HOW (R_PPC64_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
754 ppc64_elf_unhandled_reloc),
755
756 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
757 with values (sym+add)@dtpmod and (sym+add)@dtprel, and computes the offset
758 to the first entry relative to the TOC base (r2). */
759 HOW (R_PPC64_GOT_TLSGD16, 1, 16, 0xffff, 0, false, signed,
760 ppc64_elf_unhandled_reloc),
761
762 /* Like GOT_TLSGD16, but no overflow. */
763 HOW (R_PPC64_GOT_TLSGD16_LO, 1, 16, 0xffff, 0, false, dont,
764 ppc64_elf_unhandled_reloc),
765
766 /* Like GOT_TLSGD16_LO, but next higher group of 16 bits. */
767 HOW (R_PPC64_GOT_TLSGD16_HI, 1, 16, 0xffff, 16, false, signed,
768 ppc64_elf_unhandled_reloc),
769
770 /* Like GOT_TLSGD16_HI, but adjust for low 16 bits. */
771 HOW (R_PPC64_GOT_TLSGD16_HA, 1, 16, 0xffff, 16, false, signed,
772 ppc64_elf_unhandled_reloc),
773
774 /* Allocates two contiguous entries in the GOT to hold a tls_index structure,
775 with values (sym+add)@dtpmod and zero, and computes the offset to the
776 first entry relative to the TOC base (r2). */
777 HOW (R_PPC64_GOT_TLSLD16, 1, 16, 0xffff, 0, false, signed,
778 ppc64_elf_unhandled_reloc),
779
780 /* Like GOT_TLSLD16, but no overflow. */
781 HOW (R_PPC64_GOT_TLSLD16_LO, 1, 16, 0xffff, 0, false, dont,
782 ppc64_elf_unhandled_reloc),
783
784 /* Like GOT_TLSLD16_LO, but next higher group of 16 bits. */
785 HOW (R_PPC64_GOT_TLSLD16_HI, 1, 16, 0xffff, 16, false, signed,
786 ppc64_elf_unhandled_reloc),
787
788 /* Like GOT_TLSLD16_HI, but adjust for low 16 bits. */
789 HOW (R_PPC64_GOT_TLSLD16_HA, 1, 16, 0xffff, 16, false, signed,
790 ppc64_elf_unhandled_reloc),
791
792 /* Allocates an entry in the GOT with value (sym+add)@dtprel, and computes
793 the offset to the entry relative to the TOC base (r2). */
794 HOW (R_PPC64_GOT_DTPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
795 ppc64_elf_unhandled_reloc),
796
797 /* Like GOT_DTPREL16_DS, but no overflow. */
798 HOW (R_PPC64_GOT_DTPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
799 ppc64_elf_unhandled_reloc),
800
801 /* Like GOT_DTPREL16_LO_DS, but next higher group of 16 bits. */
802 HOW (R_PPC64_GOT_DTPREL16_HI, 1, 16, 0xffff, 16, false, signed,
803 ppc64_elf_unhandled_reloc),
804
805 /* Like GOT_DTPREL16_HI, but adjust for low 16 bits. */
806 HOW (R_PPC64_GOT_DTPREL16_HA, 1, 16, 0xffff, 16, false, signed,
807 ppc64_elf_unhandled_reloc),
808
809 /* Allocates an entry in the GOT with value (sym+add)@tprel, and computes the
810 offset to the entry relative to the TOC base (r2). */
811 HOW (R_PPC64_GOT_TPREL16_DS, 1, 16, 0xfffc, 0, false, signed,
812 ppc64_elf_unhandled_reloc),
813
814 /* Like GOT_TPREL16_DS, but no overflow. */
815 HOW (R_PPC64_GOT_TPREL16_LO_DS, 1, 16, 0xfffc, 0, false, dont,
816 ppc64_elf_unhandled_reloc),
817
818 /* Like GOT_TPREL16_LO_DS, but next higher group of 16 bits. */
819 HOW (R_PPC64_GOT_TPREL16_HI, 1, 16, 0xffff, 16, false, signed,
820 ppc64_elf_unhandled_reloc),
821
822 /* Like GOT_TPREL16_HI, but adjust for low 16 bits. */
823 HOW (R_PPC64_GOT_TPREL16_HA, 1, 16, 0xffff, 16, false, signed,
824 ppc64_elf_unhandled_reloc),
825
826 HOW (R_PPC64_JMP_IREL, 0, 0, 0, 0, false, dont,
827 ppc64_elf_unhandled_reloc),
828
829 HOW (R_PPC64_IRELATIVE, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
830 bfd_elf_generic_reloc),
831
832 /* A 16 bit relative relocation. */
833 HOW (R_PPC64_REL16, 1, 16, 0xffff, 0, true, signed,
834 bfd_elf_generic_reloc),
835
836 /* A 16 bit relative relocation without overflow. */
837 HOW (R_PPC64_REL16_LO, 1, 16, 0xffff, 0, true, dont,
838 bfd_elf_generic_reloc),
839
840 /* The high order 16 bits of a relative address. */
841 HOW (R_PPC64_REL16_HI, 1, 16, 0xffff, 16, true, signed,
842 bfd_elf_generic_reloc),
843
844 /* The high order 16 bits of a relative address, plus 1 if the contents of
845 the low 16 bits, treated as a signed number, is negative. */
846 HOW (R_PPC64_REL16_HA, 1, 16, 0xffff, 16, true, signed,
847 ppc64_elf_ha_reloc),
848
849 HOW (R_PPC64_REL16_HIGH, 1, 16, 0xffff, 16, true, dont,
850 bfd_elf_generic_reloc),
851
852 HOW (R_PPC64_REL16_HIGHA, 1, 16, 0xffff, 16, true, dont,
853 ppc64_elf_ha_reloc),
854
855 HOW (R_PPC64_REL16_HIGHER, 1, 16, 0xffff, 32, true, dont,
856 bfd_elf_generic_reloc),
857
858 HOW (R_PPC64_REL16_HIGHERA, 1, 16, 0xffff, 32, true, dont,
859 ppc64_elf_ha_reloc),
860
861 HOW (R_PPC64_REL16_HIGHEST, 1, 16, 0xffff, 48, true, dont,
862 bfd_elf_generic_reloc),
863
864 HOW (R_PPC64_REL16_HIGHESTA, 1, 16, 0xffff, 48, true, dont,
865 ppc64_elf_ha_reloc),
866
867 /* Like R_PPC64_REL16_HA but for split field in addpcis. */
868 HOW (R_PPC64_REL16DX_HA, 2, 16, 0x1fffc1, 16, true, signed,
869 ppc64_elf_ha_reloc),
870
871 /* A split-field reloc for addpcis, non-relative (gas internal use only). */
872 HOW (R_PPC64_16DX_HA, 2, 16, 0x1fffc1, 16, false, signed,
873 ppc64_elf_ha_reloc),
874
875 /* Like R_PPC64_ADDR16_HI, but no overflow. */
876 HOW (R_PPC64_ADDR16_HIGH, 1, 16, 0xffff, 16, false, dont,
877 bfd_elf_generic_reloc),
878
879 /* Like R_PPC64_ADDR16_HA, but no overflow. */
880 HOW (R_PPC64_ADDR16_HIGHA, 1, 16, 0xffff, 16, false, dont,
881 ppc64_elf_ha_reloc),
882
883 /* Like R_PPC64_DTPREL16_HI, but no overflow. */
884 HOW (R_PPC64_DTPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
885 ppc64_elf_unhandled_reloc),
886
887 /* Like R_PPC64_DTPREL16_HA, but no overflow. */
888 HOW (R_PPC64_DTPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
889 ppc64_elf_unhandled_reloc),
890
891 /* Like R_PPC64_TPREL16_HI, but no overflow. */
892 HOW (R_PPC64_TPREL16_HIGH, 1, 16, 0xffff, 16, false, dont,
893 ppc64_elf_unhandled_reloc),
894
895 /* Like R_PPC64_TPREL16_HA, but no overflow. */
896 HOW (R_PPC64_TPREL16_HIGHA, 1, 16, 0xffff, 16, false, dont,
897 ppc64_elf_unhandled_reloc),
898
899 /* Marker reloc on ELFv2 large-model function entry. */
900 HOW (R_PPC64_ENTRY, 2, 32, 0, 0, false, dont,
901 bfd_elf_generic_reloc),
902
903 /* Like ADDR64, but use local entry point of function. */
904 HOW (R_PPC64_ADDR64_LOCAL, 4, 64, 0xffffffffffffffffULL, 0, false, dont,
905 bfd_elf_generic_reloc),
906
907 HOW (R_PPC64_PLTSEQ_NOTOC, 2, 32, 0, 0, false, dont,
908 bfd_elf_generic_reloc),
909
910 HOW (R_PPC64_PLTCALL_NOTOC, 2, 32, 0, 0, false, dont,
911 bfd_elf_generic_reloc),
912
913 HOW (R_PPC64_PCREL_OPT, 2, 32, 0, 0, false, dont,
914 bfd_elf_generic_reloc),
915
916 HOW (R_PPC64_D34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
917 ppc64_elf_prefix_reloc),
918
919 HOW (R_PPC64_D34_LO, 4, 34, 0x3ffff0000ffffULL, 0, false, dont,
920 ppc64_elf_prefix_reloc),
921
922 HOW (R_PPC64_D34_HI30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
923 ppc64_elf_prefix_reloc),
924
925 HOW (R_PPC64_D34_HA30, 4, 34, 0x3ffff0000ffffULL, 34, false, dont,
926 ppc64_elf_prefix_reloc),
927
928 HOW (R_PPC64_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
929 ppc64_elf_prefix_reloc),
930
931 HOW (R_PPC64_GOT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
932 ppc64_elf_unhandled_reloc),
933
934 HOW (R_PPC64_PLT_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
935 ppc64_elf_unhandled_reloc),
936
937 HOW (R_PPC64_PLT_PCREL34_NOTOC, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
938 ppc64_elf_unhandled_reloc),
939
940 HOW (R_PPC64_TPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
941 ppc64_elf_unhandled_reloc),
942
943 HOW (R_PPC64_DTPREL34, 4, 34, 0x3ffff0000ffffULL, 0, false, signed,
944 ppc64_elf_unhandled_reloc),
945
946 HOW (R_PPC64_GOT_TLSGD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
947 ppc64_elf_unhandled_reloc),
948
949 HOW (R_PPC64_GOT_TLSLD_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
950 ppc64_elf_unhandled_reloc),
951
952 HOW (R_PPC64_GOT_TPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
953 ppc64_elf_unhandled_reloc),
954
955 HOW (R_PPC64_GOT_DTPREL_PCREL34, 4, 34, 0x3ffff0000ffffULL, 0, true, signed,
956 ppc64_elf_unhandled_reloc),
957
958 HOW (R_PPC64_ADDR16_HIGHER34, 1, 16, 0xffff, 34, false, dont,
959 bfd_elf_generic_reloc),
960
961 HOW (R_PPC64_ADDR16_HIGHERA34, 1, 16, 0xffff, 34, false, dont,
962 ppc64_elf_ha_reloc),
963
964 HOW (R_PPC64_ADDR16_HIGHEST34, 1, 16, 0xffff, 50, false, dont,
965 bfd_elf_generic_reloc),
966
967 HOW (R_PPC64_ADDR16_HIGHESTA34, 1, 16, 0xffff, 50, false, dont,
968 ppc64_elf_ha_reloc),
969
970 HOW (R_PPC64_REL16_HIGHER34, 1, 16, 0xffff, 34, true, dont,
971 bfd_elf_generic_reloc),
972
973 HOW (R_PPC64_REL16_HIGHERA34, 1, 16, 0xffff, 34, true, dont,
974 ppc64_elf_ha_reloc),
975
976 HOW (R_PPC64_REL16_HIGHEST34, 1, 16, 0xffff, 50, true, dont,
977 bfd_elf_generic_reloc),
978
979 HOW (R_PPC64_REL16_HIGHESTA34, 1, 16, 0xffff, 50, true, dont,
980 ppc64_elf_ha_reloc),
981
982 HOW (R_PPC64_D28, 4, 28, 0xfff0000ffffULL, 0, false, signed,
983 ppc64_elf_prefix_reloc),
984
985 HOW (R_PPC64_PCREL28, 4, 28, 0xfff0000ffffULL, 0, true, signed,
986 ppc64_elf_prefix_reloc),
987
988 /* GNU extension to record C++ vtable hierarchy. */
989 HOW (R_PPC64_GNU_VTINHERIT, 0, 0, 0, 0, false, dont,
990 NULL),
991
992 /* GNU extension to record C++ vtable member usage. */
993 HOW (R_PPC64_GNU_VTENTRY, 0, 0, 0, 0, false, dont,
994 NULL),
995 };
996
997 \f
998 /* Initialize the ppc64_elf_howto_table, so that linear accesses can
999 be done. */
1000
1001 static void
1002 ppc_howto_init (void)
1003 {
1004 unsigned int i, type;
1005
1006 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1007 {
1008 type = ppc64_elf_howto_raw[i].type;
1009 BFD_ASSERT (type < ARRAY_SIZE (ppc64_elf_howto_table));
1010 ppc64_elf_howto_table[type] = &ppc64_elf_howto_raw[i];
1011 }
1012 }
1013
1014 static reloc_howto_type *
1015 ppc64_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
1016 {
1017 enum elf_ppc64_reloc_type r = R_PPC64_NONE;
1018
1019 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1020 /* Initialize howto table if needed. */
1021 ppc_howto_init ();
1022
1023 switch (code)
1024 {
1025 default:
1026 /* xgettext:c-format */
1027 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd,
1028 (int) code);
1029 bfd_set_error (bfd_error_bad_value);
1030 return NULL;
1031
1032 case BFD_RELOC_NONE: r = R_PPC64_NONE;
1033 break;
1034 case BFD_RELOC_32: r = R_PPC64_ADDR32;
1035 break;
1036 case BFD_RELOC_PPC_BA26: r = R_PPC64_ADDR24;
1037 break;
1038 case BFD_RELOC_16: r = R_PPC64_ADDR16;
1039 break;
1040 case BFD_RELOC_LO16: r = R_PPC64_ADDR16_LO;
1041 break;
1042 case BFD_RELOC_HI16: r = R_PPC64_ADDR16_HI;
1043 break;
1044 case BFD_RELOC_PPC64_ADDR16_HIGH: r = R_PPC64_ADDR16_HIGH;
1045 break;
1046 case BFD_RELOC_HI16_S: r = R_PPC64_ADDR16_HA;
1047 break;
1048 case BFD_RELOC_PPC64_ADDR16_HIGHA: r = R_PPC64_ADDR16_HIGHA;
1049 break;
1050 case BFD_RELOC_PPC_BA16: r = R_PPC64_ADDR14;
1051 break;
1052 case BFD_RELOC_PPC_BA16_BRTAKEN: r = R_PPC64_ADDR14_BRTAKEN;
1053 break;
1054 case BFD_RELOC_PPC_BA16_BRNTAKEN: r = R_PPC64_ADDR14_BRNTAKEN;
1055 break;
1056 case BFD_RELOC_PPC_B26: r = R_PPC64_REL24;
1057 break;
1058 case BFD_RELOC_PPC64_REL24_NOTOC: r = R_PPC64_REL24_NOTOC;
1059 break;
1060 case BFD_RELOC_PPC64_REL24_P9NOTOC: r = R_PPC64_REL24_P9NOTOC;
1061 break;
1062 case BFD_RELOC_PPC_B16: r = R_PPC64_REL14;
1063 break;
1064 case BFD_RELOC_PPC_B16_BRTAKEN: r = R_PPC64_REL14_BRTAKEN;
1065 break;
1066 case BFD_RELOC_PPC_B16_BRNTAKEN: r = R_PPC64_REL14_BRNTAKEN;
1067 break;
1068 case BFD_RELOC_16_GOTOFF: r = R_PPC64_GOT16;
1069 break;
1070 case BFD_RELOC_LO16_GOTOFF: r = R_PPC64_GOT16_LO;
1071 break;
1072 case BFD_RELOC_HI16_GOTOFF: r = R_PPC64_GOT16_HI;
1073 break;
1074 case BFD_RELOC_HI16_S_GOTOFF: r = R_PPC64_GOT16_HA;
1075 break;
1076 case BFD_RELOC_PPC_COPY: r = R_PPC64_COPY;
1077 break;
1078 case BFD_RELOC_PPC_GLOB_DAT: r = R_PPC64_GLOB_DAT;
1079 break;
1080 case BFD_RELOC_32_PCREL: r = R_PPC64_REL32;
1081 break;
1082 case BFD_RELOC_32_PLTOFF: r = R_PPC64_PLT32;
1083 break;
1084 case BFD_RELOC_32_PLT_PCREL: r = R_PPC64_PLTREL32;
1085 break;
1086 case BFD_RELOC_LO16_PLTOFF: r = R_PPC64_PLT16_LO;
1087 break;
1088 case BFD_RELOC_HI16_PLTOFF: r = R_PPC64_PLT16_HI;
1089 break;
1090 case BFD_RELOC_HI16_S_PLTOFF: r = R_PPC64_PLT16_HA;
1091 break;
1092 case BFD_RELOC_16_BASEREL: r = R_PPC64_SECTOFF;
1093 break;
1094 case BFD_RELOC_LO16_BASEREL: r = R_PPC64_SECTOFF_LO;
1095 break;
1096 case BFD_RELOC_HI16_BASEREL: r = R_PPC64_SECTOFF_HI;
1097 break;
1098 case BFD_RELOC_HI16_S_BASEREL: r = R_PPC64_SECTOFF_HA;
1099 break;
1100 case BFD_RELOC_CTOR: r = R_PPC64_ADDR64;
1101 break;
1102 case BFD_RELOC_64: r = R_PPC64_ADDR64;
1103 break;
1104 case BFD_RELOC_PPC64_HIGHER: r = R_PPC64_ADDR16_HIGHER;
1105 break;
1106 case BFD_RELOC_PPC64_HIGHER_S: r = R_PPC64_ADDR16_HIGHERA;
1107 break;
1108 case BFD_RELOC_PPC64_HIGHEST: r = R_PPC64_ADDR16_HIGHEST;
1109 break;
1110 case BFD_RELOC_PPC64_HIGHEST_S: r = R_PPC64_ADDR16_HIGHESTA;
1111 break;
1112 case BFD_RELOC_64_PCREL: r = R_PPC64_REL64;
1113 break;
1114 case BFD_RELOC_64_PLTOFF: r = R_PPC64_PLT64;
1115 break;
1116 case BFD_RELOC_64_PLT_PCREL: r = R_PPC64_PLTREL64;
1117 break;
1118 case BFD_RELOC_PPC_TOC16: r = R_PPC64_TOC16;
1119 break;
1120 case BFD_RELOC_PPC64_TOC16_LO: r = R_PPC64_TOC16_LO;
1121 break;
1122 case BFD_RELOC_PPC64_TOC16_HI: r = R_PPC64_TOC16_HI;
1123 break;
1124 case BFD_RELOC_PPC64_TOC16_HA: r = R_PPC64_TOC16_HA;
1125 break;
1126 case BFD_RELOC_PPC64_TOC: r = R_PPC64_TOC;
1127 break;
1128 case BFD_RELOC_PPC64_PLTGOT16: r = R_PPC64_PLTGOT16;
1129 break;
1130 case BFD_RELOC_PPC64_PLTGOT16_LO: r = R_PPC64_PLTGOT16_LO;
1131 break;
1132 case BFD_RELOC_PPC64_PLTGOT16_HI: r = R_PPC64_PLTGOT16_HI;
1133 break;
1134 case BFD_RELOC_PPC64_PLTGOT16_HA: r = R_PPC64_PLTGOT16_HA;
1135 break;
1136 case BFD_RELOC_PPC64_ADDR16_DS: r = R_PPC64_ADDR16_DS;
1137 break;
1138 case BFD_RELOC_PPC64_ADDR16_LO_DS: r = R_PPC64_ADDR16_LO_DS;
1139 break;
1140 case BFD_RELOC_PPC64_GOT16_DS: r = R_PPC64_GOT16_DS;
1141 break;
1142 case BFD_RELOC_PPC64_GOT16_LO_DS: r = R_PPC64_GOT16_LO_DS;
1143 break;
1144 case BFD_RELOC_PPC64_PLT16_LO_DS: r = R_PPC64_PLT16_LO_DS;
1145 break;
1146 case BFD_RELOC_PPC64_SECTOFF_DS: r = R_PPC64_SECTOFF_DS;
1147 break;
1148 case BFD_RELOC_PPC64_SECTOFF_LO_DS: r = R_PPC64_SECTOFF_LO_DS;
1149 break;
1150 case BFD_RELOC_PPC64_TOC16_DS: r = R_PPC64_TOC16_DS;
1151 break;
1152 case BFD_RELOC_PPC64_TOC16_LO_DS: r = R_PPC64_TOC16_LO_DS;
1153 break;
1154 case BFD_RELOC_PPC64_PLTGOT16_DS: r = R_PPC64_PLTGOT16_DS;
1155 break;
1156 case BFD_RELOC_PPC64_PLTGOT16_LO_DS: r = R_PPC64_PLTGOT16_LO_DS;
1157 break;
1158 case BFD_RELOC_PPC64_TLS_PCREL:
1159 case BFD_RELOC_PPC_TLS: r = R_PPC64_TLS;
1160 break;
1161 case BFD_RELOC_PPC_TLSGD: r = R_PPC64_TLSGD;
1162 break;
1163 case BFD_RELOC_PPC_TLSLD: r = R_PPC64_TLSLD;
1164 break;
1165 case BFD_RELOC_PPC_DTPMOD: r = R_PPC64_DTPMOD64;
1166 break;
1167 case BFD_RELOC_PPC_TPREL16: r = R_PPC64_TPREL16;
1168 break;
1169 case BFD_RELOC_PPC_TPREL16_LO: r = R_PPC64_TPREL16_LO;
1170 break;
1171 case BFD_RELOC_PPC_TPREL16_HI: r = R_PPC64_TPREL16_HI;
1172 break;
1173 case BFD_RELOC_PPC64_TPREL16_HIGH: r = R_PPC64_TPREL16_HIGH;
1174 break;
1175 case BFD_RELOC_PPC_TPREL16_HA: r = R_PPC64_TPREL16_HA;
1176 break;
1177 case BFD_RELOC_PPC64_TPREL16_HIGHA: r = R_PPC64_TPREL16_HIGHA;
1178 break;
1179 case BFD_RELOC_PPC_TPREL: r = R_PPC64_TPREL64;
1180 break;
1181 case BFD_RELOC_PPC_DTPREL16: r = R_PPC64_DTPREL16;
1182 break;
1183 case BFD_RELOC_PPC_DTPREL16_LO: r = R_PPC64_DTPREL16_LO;
1184 break;
1185 case BFD_RELOC_PPC_DTPREL16_HI: r = R_PPC64_DTPREL16_HI;
1186 break;
1187 case BFD_RELOC_PPC64_DTPREL16_HIGH: r = R_PPC64_DTPREL16_HIGH;
1188 break;
1189 case BFD_RELOC_PPC_DTPREL16_HA: r = R_PPC64_DTPREL16_HA;
1190 break;
1191 case BFD_RELOC_PPC64_DTPREL16_HIGHA: r = R_PPC64_DTPREL16_HIGHA;
1192 break;
1193 case BFD_RELOC_PPC_DTPREL: r = R_PPC64_DTPREL64;
1194 break;
1195 case BFD_RELOC_PPC_GOT_TLSGD16: r = R_PPC64_GOT_TLSGD16;
1196 break;
1197 case BFD_RELOC_PPC_GOT_TLSGD16_LO: r = R_PPC64_GOT_TLSGD16_LO;
1198 break;
1199 case BFD_RELOC_PPC_GOT_TLSGD16_HI: r = R_PPC64_GOT_TLSGD16_HI;
1200 break;
1201 case BFD_RELOC_PPC_GOT_TLSGD16_HA: r = R_PPC64_GOT_TLSGD16_HA;
1202 break;
1203 case BFD_RELOC_PPC_GOT_TLSLD16: r = R_PPC64_GOT_TLSLD16;
1204 break;
1205 case BFD_RELOC_PPC_GOT_TLSLD16_LO: r = R_PPC64_GOT_TLSLD16_LO;
1206 break;
1207 case BFD_RELOC_PPC_GOT_TLSLD16_HI: r = R_PPC64_GOT_TLSLD16_HI;
1208 break;
1209 case BFD_RELOC_PPC_GOT_TLSLD16_HA: r = R_PPC64_GOT_TLSLD16_HA;
1210 break;
1211 case BFD_RELOC_PPC_GOT_TPREL16: r = R_PPC64_GOT_TPREL16_DS;
1212 break;
1213 case BFD_RELOC_PPC_GOT_TPREL16_LO: r = R_PPC64_GOT_TPREL16_LO_DS;
1214 break;
1215 case BFD_RELOC_PPC_GOT_TPREL16_HI: r = R_PPC64_GOT_TPREL16_HI;
1216 break;
1217 case BFD_RELOC_PPC_GOT_TPREL16_HA: r = R_PPC64_GOT_TPREL16_HA;
1218 break;
1219 case BFD_RELOC_PPC_GOT_DTPREL16: r = R_PPC64_GOT_DTPREL16_DS;
1220 break;
1221 case BFD_RELOC_PPC_GOT_DTPREL16_LO: r = R_PPC64_GOT_DTPREL16_LO_DS;
1222 break;
1223 case BFD_RELOC_PPC_GOT_DTPREL16_HI: r = R_PPC64_GOT_DTPREL16_HI;
1224 break;
1225 case BFD_RELOC_PPC_GOT_DTPREL16_HA: r = R_PPC64_GOT_DTPREL16_HA;
1226 break;
1227 case BFD_RELOC_PPC64_TPREL16_DS: r = R_PPC64_TPREL16_DS;
1228 break;
1229 case BFD_RELOC_PPC64_TPREL16_LO_DS: r = R_PPC64_TPREL16_LO_DS;
1230 break;
1231 case BFD_RELOC_PPC64_TPREL16_HIGHER: r = R_PPC64_TPREL16_HIGHER;
1232 break;
1233 case BFD_RELOC_PPC64_TPREL16_HIGHERA: r = R_PPC64_TPREL16_HIGHERA;
1234 break;
1235 case BFD_RELOC_PPC64_TPREL16_HIGHEST: r = R_PPC64_TPREL16_HIGHEST;
1236 break;
1237 case BFD_RELOC_PPC64_TPREL16_HIGHESTA: r = R_PPC64_TPREL16_HIGHESTA;
1238 break;
1239 case BFD_RELOC_PPC64_DTPREL16_DS: r = R_PPC64_DTPREL16_DS;
1240 break;
1241 case BFD_RELOC_PPC64_DTPREL16_LO_DS: r = R_PPC64_DTPREL16_LO_DS;
1242 break;
1243 case BFD_RELOC_PPC64_DTPREL16_HIGHER: r = R_PPC64_DTPREL16_HIGHER;
1244 break;
1245 case BFD_RELOC_PPC64_DTPREL16_HIGHERA: r = R_PPC64_DTPREL16_HIGHERA;
1246 break;
1247 case BFD_RELOC_PPC64_DTPREL16_HIGHEST: r = R_PPC64_DTPREL16_HIGHEST;
1248 break;
1249 case BFD_RELOC_PPC64_DTPREL16_HIGHESTA: r = R_PPC64_DTPREL16_HIGHESTA;
1250 break;
1251 case BFD_RELOC_16_PCREL: r = R_PPC64_REL16;
1252 break;
1253 case BFD_RELOC_LO16_PCREL: r = R_PPC64_REL16_LO;
1254 break;
1255 case BFD_RELOC_HI16_PCREL: r = R_PPC64_REL16_HI;
1256 break;
1257 case BFD_RELOC_HI16_S_PCREL: r = R_PPC64_REL16_HA;
1258 break;
1259 case BFD_RELOC_PPC64_REL16_HIGH: r = R_PPC64_REL16_HIGH;
1260 break;
1261 case BFD_RELOC_PPC64_REL16_HIGHA: r = R_PPC64_REL16_HIGHA;
1262 break;
1263 case BFD_RELOC_PPC64_REL16_HIGHER: r = R_PPC64_REL16_HIGHER;
1264 break;
1265 case BFD_RELOC_PPC64_REL16_HIGHERA: r = R_PPC64_REL16_HIGHERA;
1266 break;
1267 case BFD_RELOC_PPC64_REL16_HIGHEST: r = R_PPC64_REL16_HIGHEST;
1268 break;
1269 case BFD_RELOC_PPC64_REL16_HIGHESTA: r = R_PPC64_REL16_HIGHESTA;
1270 break;
1271 case BFD_RELOC_PPC_16DX_HA: r = R_PPC64_16DX_HA;
1272 break;
1273 case BFD_RELOC_PPC_REL16DX_HA: r = R_PPC64_REL16DX_HA;
1274 break;
1275 case BFD_RELOC_PPC64_ENTRY: r = R_PPC64_ENTRY;
1276 break;
1277 case BFD_RELOC_PPC64_ADDR64_LOCAL: r = R_PPC64_ADDR64_LOCAL;
1278 break;
1279 case BFD_RELOC_PPC64_D34: r = R_PPC64_D34;
1280 break;
1281 case BFD_RELOC_PPC64_D34_LO: r = R_PPC64_D34_LO;
1282 break;
1283 case BFD_RELOC_PPC64_D34_HI30: r = R_PPC64_D34_HI30;
1284 break;
1285 case BFD_RELOC_PPC64_D34_HA30: r = R_PPC64_D34_HA30;
1286 break;
1287 case BFD_RELOC_PPC64_PCREL34: r = R_PPC64_PCREL34;
1288 break;
1289 case BFD_RELOC_PPC64_GOT_PCREL34: r = R_PPC64_GOT_PCREL34;
1290 break;
1291 case BFD_RELOC_PPC64_PLT_PCREL34: r = R_PPC64_PLT_PCREL34;
1292 break;
1293 case BFD_RELOC_PPC64_TPREL34: r = R_PPC64_TPREL34;
1294 break;
1295 case BFD_RELOC_PPC64_DTPREL34: r = R_PPC64_DTPREL34;
1296 break;
1297 case BFD_RELOC_PPC64_GOT_TLSGD_PCREL34: r = R_PPC64_GOT_TLSGD_PCREL34;
1298 break;
1299 case BFD_RELOC_PPC64_GOT_TLSLD_PCREL34: r = R_PPC64_GOT_TLSLD_PCREL34;
1300 break;
1301 case BFD_RELOC_PPC64_GOT_TPREL_PCREL34: r = R_PPC64_GOT_TPREL_PCREL34;
1302 break;
1303 case BFD_RELOC_PPC64_GOT_DTPREL_PCREL34: r = R_PPC64_GOT_DTPREL_PCREL34;
1304 break;
1305 case BFD_RELOC_PPC64_ADDR16_HIGHER34: r = R_PPC64_ADDR16_HIGHER34;
1306 break;
1307 case BFD_RELOC_PPC64_ADDR16_HIGHERA34: r = R_PPC64_ADDR16_HIGHERA34;
1308 break;
1309 case BFD_RELOC_PPC64_ADDR16_HIGHEST34: r = R_PPC64_ADDR16_HIGHEST34;
1310 break;
1311 case BFD_RELOC_PPC64_ADDR16_HIGHESTA34: r = R_PPC64_ADDR16_HIGHESTA34;
1312 break;
1313 case BFD_RELOC_PPC64_REL16_HIGHER34: r = R_PPC64_REL16_HIGHER34;
1314 break;
1315 case BFD_RELOC_PPC64_REL16_HIGHERA34: r = R_PPC64_REL16_HIGHERA34;
1316 break;
1317 case BFD_RELOC_PPC64_REL16_HIGHEST34: r = R_PPC64_REL16_HIGHEST34;
1318 break;
1319 case BFD_RELOC_PPC64_REL16_HIGHESTA34: r = R_PPC64_REL16_HIGHESTA34;
1320 break;
1321 case BFD_RELOC_PPC64_D28: r = R_PPC64_D28;
1322 break;
1323 case BFD_RELOC_PPC64_PCREL28: r = R_PPC64_PCREL28;
1324 break;
1325 case BFD_RELOC_VTABLE_INHERIT: r = R_PPC64_GNU_VTINHERIT;
1326 break;
1327 case BFD_RELOC_VTABLE_ENTRY: r = R_PPC64_GNU_VTENTRY;
1328 break;
1329 }
1330
1331 return ppc64_elf_howto_table[r];
1332 };
1333
1334 static reloc_howto_type *
1335 ppc64_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
1336 {
1337 unsigned int i;
1338 static char *compat_map[][2] = {
1339 { "R_PPC64_GOT_TLSGD34", "R_PPC64_GOT_TLSGD_PCREL34" },
1340 { "R_PPC64_GOT_TLSLD34", "R_PPC64_GOT_TLSLD_PCREL34" },
1341 { "R_PPC64_GOT_TPREL34", "R_PPC64_GOT_TPREL_PCREL34" },
1342 { "R_PPC64_GOT_DTPREL34", "R_PPC64_GOT_DTPREL_PCREL34" }
1343 };
1344
1345 for (i = 0; i < ARRAY_SIZE (ppc64_elf_howto_raw); i++)
1346 if (ppc64_elf_howto_raw[i].name != NULL
1347 && strcasecmp (ppc64_elf_howto_raw[i].name, r_name) == 0)
1348 return &ppc64_elf_howto_raw[i];
1349
1350 /* Handle old names of relocations in case they were used by
1351 .reloc directives.
1352 FIXME: Remove this soon. Mapping the reloc names is very likely
1353 completely unnecessary. */
1354 for (i = 0; i < ARRAY_SIZE (compat_map); i++)
1355 if (strcasecmp (compat_map[i][0], r_name) == 0)
1356 {
1357 _bfd_error_handler (_("warning: %s should be used rather than %s"),
1358 compat_map[i][1], compat_map[i][0]);
1359 return ppc64_elf_reloc_name_lookup (abfd, compat_map[i][1]);
1360 }
1361
1362 return NULL;
1363 }
1364
1365 /* Set the howto pointer for a PowerPC ELF reloc. */
1366
1367 static bool
1368 ppc64_elf_info_to_howto (bfd *abfd, arelent *cache_ptr,
1369 Elf_Internal_Rela *dst)
1370 {
1371 unsigned int type;
1372
1373 /* Initialize howto table if needed. */
1374 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
1375 ppc_howto_init ();
1376
1377 type = ELF64_R_TYPE (dst->r_info);
1378 if (type >= ARRAY_SIZE (ppc64_elf_howto_table))
1379 {
1380 /* xgettext:c-format */
1381 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1382 abfd, type);
1383 bfd_set_error (bfd_error_bad_value);
1384 return false;
1385 }
1386 cache_ptr->howto = ppc64_elf_howto_table[type];
1387 if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
1388 {
1389 /* xgettext:c-format */
1390 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1391 abfd, type);
1392 bfd_set_error (bfd_error_bad_value);
1393 return false;
1394 }
1395
1396 return true;
1397 }
1398
1399 /* Handle the R_PPC64_ADDR16_HA and similar relocs. */
1400
1401 static bfd_reloc_status_type
1402 ppc64_elf_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1403 void *data, asection *input_section,
1404 bfd *output_bfd, char **error_message)
1405 {
1406 enum elf_ppc64_reloc_type r_type;
1407 long insn;
1408 bfd_size_type octets;
1409 bfd_vma value;
1410
1411 /* If this is a relocatable link (output_bfd test tells us), just
1412 call the generic function. Any adjustment will be done at final
1413 link time. */
1414 if (output_bfd != NULL)
1415 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1416 input_section, output_bfd, error_message);
1417
1418 /* Adjust the addend for sign extension of the low 16 (or 34) bits.
1419 We won't actually be using the low bits, so trashing them
1420 doesn't matter. */
1421 r_type = reloc_entry->howto->type;
1422 if (r_type == R_PPC64_ADDR16_HIGHERA34
1423 || r_type == R_PPC64_ADDR16_HIGHESTA34
1424 || r_type == R_PPC64_REL16_HIGHERA34
1425 || r_type == R_PPC64_REL16_HIGHESTA34)
1426 reloc_entry->addend += 1ULL << 33;
1427 else
1428 reloc_entry->addend += 1U << 15;
1429 if (r_type != R_PPC64_REL16DX_HA)
1430 return bfd_reloc_continue;
1431
1432 value = 0;
1433 if (!bfd_is_com_section (symbol->section))
1434 value = symbol->value;
1435 value += (reloc_entry->addend
1436 + symbol->section->output_offset
1437 + symbol->section->output_section->vma);
1438 value -= (reloc_entry->address
1439 + input_section->output_offset
1440 + input_section->output_section->vma);
1441 value = (bfd_signed_vma) value >> 16;
1442
1443 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1444 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1445 input_section, octets))
1446 return bfd_reloc_outofrange;
1447
1448 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1449 insn &= ~0x1fffc1;
1450 insn |= (value & 0xffc1) | ((value & 0x3e) << 15);
1451 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1452 if (value + 0x8000 > 0xffff)
1453 return bfd_reloc_overflow;
1454 return bfd_reloc_ok;
1455 }
1456
1457 static bfd_reloc_status_type
1458 ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1459 void *data, asection *input_section,
1460 bfd *output_bfd, char **error_message)
1461 {
1462 if (output_bfd != NULL)
1463 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1464 input_section, output_bfd, error_message);
1465
1466 if (strcmp (symbol->section->name, ".opd") == 0
1467 && (symbol->section->owner->flags & DYNAMIC) == 0)
1468 {
1469 bfd_vma dest = opd_entry_value (symbol->section,
1470 symbol->value + reloc_entry->addend,
1471 NULL, NULL, false);
1472 if (dest != (bfd_vma) -1)
1473 reloc_entry->addend = dest - (symbol->value
1474 + symbol->section->output_section->vma
1475 + symbol->section->output_offset);
1476 }
1477 else
1478 {
1479 elf_symbol_type *elfsym = (elf_symbol_type *) symbol;
1480
1481 if (symbol->section->owner != abfd
1482 && symbol->section->owner != NULL
1483 && abiversion (symbol->section->owner) >= 2)
1484 {
1485 unsigned int i;
1486
1487 for (i = 0; i < symbol->section->owner->symcount; ++i)
1488 {
1489 asymbol *symdef = symbol->section->owner->outsymbols[i];
1490
1491 if (strcmp (symdef->name, symbol->name) == 0)
1492 {
1493 elfsym = (elf_symbol_type *) symdef;
1494 break;
1495 }
1496 }
1497 }
1498 reloc_entry->addend
1499 += PPC64_LOCAL_ENTRY_OFFSET (elfsym->internal_elf_sym.st_other);
1500 }
1501 return bfd_reloc_continue;
1502 }
1503
1504 static bfd_reloc_status_type
1505 ppc64_elf_brtaken_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1506 void *data, asection *input_section,
1507 bfd *output_bfd, char **error_message)
1508 {
1509 long insn;
1510 enum elf_ppc64_reloc_type r_type;
1511 bfd_size_type octets;
1512 /* Assume 'at' branch hints. */
1513 bool is_isa_v2 = true;
1514
1515 /* If this is a relocatable link (output_bfd test tells us), just
1516 call the generic function. Any adjustment will be done at final
1517 link time. */
1518 if (output_bfd != NULL)
1519 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1520 input_section, output_bfd, error_message);
1521
1522 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1523 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1524 input_section, octets))
1525 return bfd_reloc_outofrange;
1526
1527 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1528 insn &= ~(0x01 << 21);
1529 r_type = reloc_entry->howto->type;
1530 if (r_type == R_PPC64_ADDR14_BRTAKEN
1531 || r_type == R_PPC64_REL14_BRTAKEN)
1532 insn |= 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
1533
1534 if (is_isa_v2)
1535 {
1536 /* Set 'a' bit. This is 0b00010 in BO field for branch
1537 on CR(BI) insns (BO == 001at or 011at), and 0b01000
1538 for branch on CTR insns (BO == 1a00t or 1a01t). */
1539 if ((insn & (0x14 << 21)) == (0x04 << 21))
1540 insn |= 0x02 << 21;
1541 else if ((insn & (0x14 << 21)) == (0x10 << 21))
1542 insn |= 0x08 << 21;
1543 else
1544 goto out;
1545 }
1546 else
1547 {
1548 bfd_vma target = 0;
1549 bfd_vma from;
1550
1551 if (!bfd_is_com_section (symbol->section))
1552 target = symbol->value;
1553 target += symbol->section->output_section->vma;
1554 target += symbol->section->output_offset;
1555 target += reloc_entry->addend;
1556
1557 from = (reloc_entry->address
1558 + input_section->output_offset
1559 + input_section->output_section->vma);
1560
1561 /* Invert 'y' bit if not the default. */
1562 if ((bfd_signed_vma) (target - from) < 0)
1563 insn ^= 0x01 << 21;
1564 }
1565 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets);
1566 out:
1567 return ppc64_elf_branch_reloc (abfd, reloc_entry, symbol, data,
1568 input_section, output_bfd, error_message);
1569 }
1570
1571 static bfd_reloc_status_type
1572 ppc64_elf_sectoff_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1573 void *data, asection *input_section,
1574 bfd *output_bfd, char **error_message)
1575 {
1576 /* If this is a relocatable link (output_bfd test tells us), just
1577 call the generic function. Any adjustment will be done at final
1578 link time. */
1579 if (output_bfd != NULL)
1580 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1581 input_section, output_bfd, error_message);
1582
1583 /* Subtract the symbol section base address. */
1584 reloc_entry->addend -= symbol->section->output_section->vma;
1585 return bfd_reloc_continue;
1586 }
1587
1588 static bfd_reloc_status_type
1589 ppc64_elf_sectoff_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1590 void *data, asection *input_section,
1591 bfd *output_bfd, char **error_message)
1592 {
1593 /* If this is a relocatable link (output_bfd test tells us), just
1594 call the generic function. Any adjustment will be done at final
1595 link time. */
1596 if (output_bfd != NULL)
1597 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1598 input_section, output_bfd, error_message);
1599
1600 /* Subtract the symbol section base address. */
1601 reloc_entry->addend -= symbol->section->output_section->vma;
1602
1603 /* Adjust the addend for sign extension of the low 16 bits. */
1604 reloc_entry->addend += 0x8000;
1605 return bfd_reloc_continue;
1606 }
1607
1608 static bfd_reloc_status_type
1609 ppc64_elf_toc_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1610 void *data, asection *input_section,
1611 bfd *output_bfd, char **error_message)
1612 {
1613 bfd_vma TOCstart;
1614
1615 /* If this is a relocatable link (output_bfd test tells us), just
1616 call the generic function. Any adjustment will be done at final
1617 link time. */
1618 if (output_bfd != NULL)
1619 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1620 input_section, output_bfd, error_message);
1621
1622 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1623 if (TOCstart == 0)
1624 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1625
1626 /* Subtract the TOC base address. */
1627 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1628 return bfd_reloc_continue;
1629 }
1630
1631 static bfd_reloc_status_type
1632 ppc64_elf_toc_ha_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1633 void *data, asection *input_section,
1634 bfd *output_bfd, char **error_message)
1635 {
1636 bfd_vma TOCstart;
1637
1638 /* If this is a relocatable link (output_bfd test tells us), just
1639 call the generic function. Any adjustment will be done at final
1640 link time. */
1641 if (output_bfd != NULL)
1642 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1643 input_section, output_bfd, error_message);
1644
1645 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1646 if (TOCstart == 0)
1647 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1648
1649 /* Subtract the TOC base address. */
1650 reloc_entry->addend -= TOCstart + TOC_BASE_OFF;
1651
1652 /* Adjust the addend for sign extension of the low 16 bits. */
1653 reloc_entry->addend += 0x8000;
1654 return bfd_reloc_continue;
1655 }
1656
1657 static bfd_reloc_status_type
1658 ppc64_elf_toc64_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1659 void *data, asection *input_section,
1660 bfd *output_bfd, char **error_message)
1661 {
1662 bfd_vma TOCstart;
1663 bfd_size_type octets;
1664
1665 /* If this is a relocatable link (output_bfd test tells us), just
1666 call the generic function. Any adjustment will be done at final
1667 link time. */
1668 if (output_bfd != NULL)
1669 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1670 input_section, output_bfd, error_message);
1671
1672 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1673 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1674 input_section, octets))
1675 return bfd_reloc_outofrange;
1676
1677 TOCstart = _bfd_get_gp_value (input_section->output_section->owner);
1678 if (TOCstart == 0)
1679 TOCstart = ppc64_elf_set_toc (NULL, input_section->output_section->owner);
1680
1681 bfd_put_64 (abfd, TOCstart + TOC_BASE_OFF, (bfd_byte *) data + octets);
1682 return bfd_reloc_ok;
1683 }
1684
1685 static bfd_reloc_status_type
1686 ppc64_elf_prefix_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1687 void *data, asection *input_section,
1688 bfd *output_bfd, char **error_message)
1689 {
1690 uint64_t insn;
1691 bfd_vma targ;
1692 bfd_size_type octets;
1693
1694 if (output_bfd != NULL)
1695 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1696 input_section, output_bfd, error_message);
1697
1698 octets = reloc_entry->address * OCTETS_PER_BYTE (abfd, input_section);
1699 if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
1700 input_section, octets))
1701 return bfd_reloc_outofrange;
1702
1703 insn = bfd_get_32 (abfd, (bfd_byte *) data + octets);
1704 insn <<= 32;
1705 insn |= bfd_get_32 (abfd, (bfd_byte *) data + octets + 4);
1706
1707 targ = (symbol->section->output_section->vma
1708 + symbol->section->output_offset
1709 + reloc_entry->addend);
1710 if (!bfd_is_com_section (symbol->section))
1711 targ += symbol->value;
1712 if (reloc_entry->howto->type == R_PPC64_D34_HA30)
1713 targ += 1ULL << 33;
1714 if (reloc_entry->howto->pc_relative)
1715 {
1716 bfd_vma from = (reloc_entry->address
1717 + input_section->output_offset
1718 + input_section->output_section->vma);
1719 targ -=from;
1720 }
1721 targ >>= reloc_entry->howto->rightshift;
1722 insn &= ~reloc_entry->howto->dst_mask;
1723 insn |= ((targ << 16) | (targ & 0xffff)) & reloc_entry->howto->dst_mask;
1724 bfd_put_32 (abfd, insn >> 32, (bfd_byte *) data + octets);
1725 bfd_put_32 (abfd, insn, (bfd_byte *) data + octets + 4);
1726 if (reloc_entry->howto->complain_on_overflow == complain_overflow_signed
1727 && (targ + (1ULL << (reloc_entry->howto->bitsize - 1))
1728 >= 1ULL << reloc_entry->howto->bitsize))
1729 return bfd_reloc_overflow;
1730 return bfd_reloc_ok;
1731 }
1732
1733 static bfd_reloc_status_type
1734 ppc64_elf_unhandled_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
1735 void *data, asection *input_section,
1736 bfd *output_bfd, char **error_message)
1737 {
1738 /* If this is a relocatable link (output_bfd test tells us), just
1739 call the generic function. Any adjustment will be done at final
1740 link time. */
1741 if (output_bfd != NULL)
1742 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
1743 input_section, output_bfd, error_message);
1744
1745 if (error_message != NULL)
1746 {
1747 static char *message;
1748 free (message);
1749 if (asprintf (&message, _("generic linker can't handle %s"),
1750 reloc_entry->howto->name) < 0)
1751 message = NULL;
1752 *error_message = message;
1753 }
1754 return bfd_reloc_dangerous;
1755 }
1756
1757 /* Track GOT entries needed for a given symbol. We might need more
1758 than one got entry per symbol. */
1759 struct got_entry
1760 {
1761 struct got_entry *next;
1762
1763 /* The symbol addend that we'll be placing in the GOT. */
1764 bfd_vma addend;
1765
1766 /* Unlike other ELF targets, we use separate GOT entries for the same
1767 symbol referenced from different input files. This is to support
1768 automatic multiple TOC/GOT sections, where the TOC base can vary
1769 from one input file to another. After partitioning into TOC groups
1770 we merge entries within the group.
1771
1772 Point to the BFD owning this GOT entry. */
1773 bfd *owner;
1774
1775 /* Zero for non-tls entries, or TLS_TLS and one of TLS_GD, TLS_LD,
1776 TLS_TPREL or TLS_DTPREL for tls entries. */
1777 unsigned char tls_type;
1778
1779 /* Non-zero if got.ent points to real entry. */
1780 unsigned char is_indirect;
1781
1782 /* Reference count until size_dynamic_sections, GOT offset thereafter. */
1783 union
1784 {
1785 bfd_signed_vma refcount;
1786 bfd_vma offset;
1787 struct got_entry *ent;
1788 } got;
1789 };
1790
1791 /* The same for PLT. */
1792 struct plt_entry
1793 {
1794 struct plt_entry *next;
1795
1796 bfd_vma addend;
1797
1798 union
1799 {
1800 bfd_signed_vma refcount;
1801 bfd_vma offset;
1802 } plt;
1803 };
1804
1805 struct ppc64_elf_obj_tdata
1806 {
1807 struct elf_obj_tdata elf;
1808
1809 /* Shortcuts to dynamic linker sections. */
1810 asection *got;
1811 asection *relgot;
1812
1813 /* Used during garbage collection. We attach global symbols defined
1814 on removed .opd entries to this section so that the sym is removed. */
1815 asection *deleted_section;
1816
1817 /* TLS local dynamic got entry handling. Support for multiple GOT
1818 sections means we potentially need one of these for each input bfd. */
1819 struct got_entry tlsld_got;
1820
1821 union
1822 {
1823 /* A copy of relocs before they are modified for --emit-relocs. */
1824 Elf_Internal_Rela *relocs;
1825
1826 /* Section contents. */
1827 bfd_byte *contents;
1828 } opd;
1829
1830 /* Nonzero if this bfd has small toc/got relocs, ie. that expect
1831 the reloc to be in the range -32768 to 32767. */
1832 unsigned int has_small_toc_reloc : 1;
1833
1834 /* Set if toc/got ha relocs detected not using r2, or lo reloc
1835 instruction not one we handle. */
1836 unsigned int unexpected_toc_insn : 1;
1837
1838 /* Set if PLT/GOT/TOC relocs that can be optimised are present in
1839 this file. */
1840 unsigned int has_optrel : 1;
1841 };
1842
1843 #define ppc64_elf_tdata(bfd) \
1844 ((struct ppc64_elf_obj_tdata *) (bfd)->tdata.any)
1845
1846 #define ppc64_tlsld_got(bfd) \
1847 (&ppc64_elf_tdata (bfd)->tlsld_got)
1848
1849 #define is_ppc64_elf(bfd) \
1850 (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
1851 && elf_object_id (bfd) == PPC64_ELF_DATA)
1852
1853 /* Override the generic function because we store some extras. */
1854
1855 static bool
1856 ppc64_elf_mkobject (bfd *abfd)
1857 {
1858 return bfd_elf_allocate_object (abfd, sizeof (struct ppc64_elf_obj_tdata),
1859 PPC64_ELF_DATA);
1860 }
1861
1862 /* Fix bad default arch selected for a 64 bit input bfd when the
1863 default is 32 bit. Also select arch based on apuinfo. */
1864
1865 static bool
1866 ppc64_elf_object_p (bfd *abfd)
1867 {
1868 if (!abfd->arch_info->the_default)
1869 return true;
1870
1871 if (abfd->arch_info->bits_per_word == 32)
1872 {
1873 Elf_Internal_Ehdr *i_ehdr = elf_elfheader (abfd);
1874
1875 if (i_ehdr->e_ident[EI_CLASS] == ELFCLASS64)
1876 {
1877 /* Relies on arch after 32 bit default being 64 bit default. */
1878 abfd->arch_info = abfd->arch_info->next;
1879 BFD_ASSERT (abfd->arch_info->bits_per_word == 64);
1880 }
1881 }
1882 return _bfd_elf_ppc_set_arch (abfd);
1883 }
1884
1885 /* Support for core dump NOTE sections. */
1886
1887 static bool
1888 ppc64_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
1889 {
1890 size_t offset, size;
1891
1892 if (note->descsz != 504)
1893 return false;
1894
1895 /* pr_cursig */
1896 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
1897
1898 /* pr_pid */
1899 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 32);
1900
1901 /* pr_reg */
1902 offset = 112;
1903 size = 384;
1904
1905 /* Make a ".reg/999" section. */
1906 return _bfd_elfcore_make_pseudosection (abfd, ".reg",
1907 size, note->descpos + offset);
1908 }
1909
1910 static bool
1911 ppc64_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
1912 {
1913 if (note->descsz != 136)
1914 return false;
1915
1916 elf_tdata (abfd)->core->pid
1917 = bfd_get_32 (abfd, note->descdata + 24);
1918 elf_tdata (abfd)->core->program
1919 = _bfd_elfcore_strndup (abfd, note->descdata + 40, 16);
1920 elf_tdata (abfd)->core->command
1921 = _bfd_elfcore_strndup (abfd, note->descdata + 56, 80);
1922
1923 return true;
1924 }
1925
1926 static char *
1927 ppc64_elf_write_core_note (bfd *abfd, char *buf, int *bufsiz, int note_type,
1928 ...)
1929 {
1930 switch (note_type)
1931 {
1932 default:
1933 return NULL;
1934
1935 case NT_PRPSINFO:
1936 {
1937 char data[136] ATTRIBUTE_NONSTRING;
1938 va_list ap;
1939
1940 va_start (ap, note_type);
1941 memset (data, 0, sizeof (data));
1942 strncpy (data + 40, va_arg (ap, const char *), 16);
1943 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1944 DIAGNOSTIC_PUSH;
1945 /* GCC 8.0 and 8.1 warn about 80 equals destination size with
1946 -Wstringop-truncation:
1947 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85643
1948 */
1949 DIAGNOSTIC_IGNORE_STRINGOP_TRUNCATION;
1950 #endif
1951 strncpy (data + 56, va_arg (ap, const char *), 80);
1952 #if GCC_VERSION == 8000 || GCC_VERSION == 8001
1953 DIAGNOSTIC_POP;
1954 #endif
1955 va_end (ap);
1956 return elfcore_write_note (abfd, buf, bufsiz,
1957 "CORE", note_type, data, sizeof (data));
1958 }
1959
1960 case NT_PRSTATUS:
1961 {
1962 char data[504];
1963 va_list ap;
1964 long pid;
1965 int cursig;
1966 const void *greg;
1967
1968 va_start (ap, note_type);
1969 memset (data, 0, 112);
1970 pid = va_arg (ap, long);
1971 bfd_put_32 (abfd, pid, data + 32);
1972 cursig = va_arg (ap, int);
1973 bfd_put_16 (abfd, cursig, data + 12);
1974 greg = va_arg (ap, const void *);
1975 memcpy (data + 112, greg, 384);
1976 memset (data + 496, 0, 8);
1977 va_end (ap);
1978 return elfcore_write_note (abfd, buf, bufsiz,
1979 "CORE", note_type, data, sizeof (data));
1980 }
1981 }
1982 }
1983
1984 /* Add extra PPC sections. */
1985
1986 static const struct bfd_elf_special_section ppc64_elf_special_sections[] =
1987 {
1988 { STRING_COMMA_LEN (".plt"), 0, SHT_NOBITS, 0 },
1989 { STRING_COMMA_LEN (".sbss"), -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1990 { STRING_COMMA_LEN (".sdata"), -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1991 { STRING_COMMA_LEN (".toc"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1992 { STRING_COMMA_LEN (".toc1"), 0, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
1993 { STRING_COMMA_LEN (".tocbss"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
1994 { NULL, 0, 0, 0, 0 }
1995 };
1996
1997 enum _ppc64_sec_type {
1998 sec_normal = 0,
1999 sec_opd = 1,
2000 sec_toc = 2
2001 };
2002
2003 struct _ppc64_elf_section_data
2004 {
2005 struct bfd_elf_section_data elf;
2006
2007 union
2008 {
2009 /* An array with one entry for each opd function descriptor,
2010 and some spares since opd entries may be either 16 or 24 bytes. */
2011 #define OPD_NDX(OFF) ((OFF) >> 4)
2012 struct _opd_sec_data
2013 {
2014 /* Points to the function code section for local opd entries. */
2015 asection **func_sec;
2016
2017 /* After editing .opd, adjust references to opd local syms. */
2018 long *adjust;
2019 } opd;
2020
2021 /* An array for toc sections, indexed by offset/8. */
2022 struct _toc_sec_data
2023 {
2024 /* Specifies the relocation symbol index used at a given toc offset. */
2025 unsigned *symndx;
2026
2027 /* And the relocation addend. */
2028 bfd_vma *add;
2029 } toc;
2030 } u;
2031
2032 enum _ppc64_sec_type sec_type:2;
2033
2034 /* Flag set when small branches are detected. Used to
2035 select suitable defaults for the stub group size. */
2036 unsigned int has_14bit_branch:1;
2037
2038 /* Flag set when PLTCALL relocs are detected. */
2039 unsigned int has_pltcall:1;
2040
2041 /* Flag set when section has PLT/GOT/TOC relocations that can be
2042 optimised. */
2043 unsigned int has_optrel:1;
2044 };
2045
2046 #define ppc64_elf_section_data(sec) \
2047 ((struct _ppc64_elf_section_data *) elf_section_data (sec))
2048
2049 static bool
2050 ppc64_elf_new_section_hook (bfd *abfd, asection *sec)
2051 {
2052 if (!sec->used_by_bfd)
2053 {
2054 struct _ppc64_elf_section_data *sdata;
2055 size_t amt = sizeof (*sdata);
2056
2057 sdata = bfd_zalloc (abfd, amt);
2058 if (sdata == NULL)
2059 return false;
2060 sec->used_by_bfd = sdata;
2061 }
2062
2063 return _bfd_elf_new_section_hook (abfd, sec);
2064 }
2065
2066 static bool
2067 ppc64_elf_section_flags (const Elf_Internal_Shdr *hdr)
2068 {
2069 const char *name = hdr->bfd_section->name;
2070
2071 if (startswith (name, ".sbss")
2072 || startswith (name, ".sdata"))
2073 hdr->bfd_section->flags |= SEC_SMALL_DATA;
2074
2075 return true;
2076 }
2077
2078 static struct _opd_sec_data *
2079 get_opd_info (asection * sec)
2080 {
2081 if (sec != NULL
2082 && ppc64_elf_section_data (sec) != NULL
2083 && ppc64_elf_section_data (sec)->sec_type == sec_opd)
2084 return &ppc64_elf_section_data (sec)->u.opd;
2085 return NULL;
2086 }
2087 \f
2088 /* Parameters for the qsort hook. */
2089 static bool synthetic_relocatable;
2090 static const asection *synthetic_opd;
2091
2092 /* qsort comparison function for ppc64_elf_get_synthetic_symtab. */
2093
2094 static int
2095 compare_symbols (const void *ap, const void *bp)
2096 {
2097 const asymbol *a = *(const asymbol **) ap;
2098 const asymbol *b = *(const asymbol **) bp;
2099
2100 /* Section symbols first. */
2101 if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
2102 return -1;
2103 if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
2104 return 1;
2105
2106 /* then .opd symbols. */
2107 if (synthetic_opd != NULL)
2108 {
2109 if (strcmp (a->section->name, ".opd") == 0
2110 && strcmp (b->section->name, ".opd") != 0)
2111 return -1;
2112 if (strcmp (a->section->name, ".opd") != 0
2113 && strcmp (b->section->name, ".opd") == 0)
2114 return 1;
2115 }
2116
2117 /* then other code symbols. */
2118 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2119 == (SEC_CODE | SEC_ALLOC))
2120 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2121 != (SEC_CODE | SEC_ALLOC)))
2122 return -1;
2123
2124 if (((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2125 != (SEC_CODE | SEC_ALLOC))
2126 && ((b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
2127 == (SEC_CODE | SEC_ALLOC)))
2128 return 1;
2129
2130 if (synthetic_relocatable)
2131 {
2132 if (a->section->id < b->section->id)
2133 return -1;
2134
2135 if (a->section->id > b->section->id)
2136 return 1;
2137 }
2138
2139 if (a->value + a->section->vma < b->value + b->section->vma)
2140 return -1;
2141
2142 if (a->value + a->section->vma > b->value + b->section->vma)
2143 return 1;
2144
2145 /* For syms with the same value, prefer strong dynamic global function
2146 syms over other syms. */
2147 if ((a->flags & BSF_GLOBAL) != 0 && (b->flags & BSF_GLOBAL) == 0)
2148 return -1;
2149
2150 if ((a->flags & BSF_GLOBAL) == 0 && (b->flags & BSF_GLOBAL) != 0)
2151 return 1;
2152
2153 if ((a->flags & BSF_FUNCTION) != 0 && (b->flags & BSF_FUNCTION) == 0)
2154 return -1;
2155
2156 if ((a->flags & BSF_FUNCTION) == 0 && (b->flags & BSF_FUNCTION) != 0)
2157 return 1;
2158
2159 if ((a->flags & BSF_WEAK) == 0 && (b->flags & BSF_WEAK) != 0)
2160 return -1;
2161
2162 if ((a->flags & BSF_WEAK) != 0 && (b->flags & BSF_WEAK) == 0)
2163 return 1;
2164
2165 if ((a->flags & BSF_DYNAMIC) != 0 && (b->flags & BSF_DYNAMIC) == 0)
2166 return -1;
2167
2168 if ((a->flags & BSF_DYNAMIC) == 0 && (b->flags & BSF_DYNAMIC) != 0)
2169 return 1;
2170
2171 /* Finally, sort on where the symbol is in memory. The symbols will
2172 be in at most two malloc'd blocks, one for static syms, one for
2173 dynamic syms, and we distinguish the two blocks above by testing
2174 BSF_DYNAMIC. Since we are sorting the symbol pointers which were
2175 originally in the same order as the symbols (and we're not
2176 sorting the symbols themselves), this ensures a stable sort. */
2177 if (a < b)
2178 return -1;
2179 if (a > b)
2180 return 1;
2181 return 0;
2182 }
2183
2184 /* Search SYMS for a symbol of the given VALUE. */
2185
2186 static asymbol *
2187 sym_exists_at (asymbol **syms, size_t lo, size_t hi, unsigned int id,
2188 bfd_vma value)
2189 {
2190 size_t mid;
2191
2192 if (id == (unsigned) -1)
2193 {
2194 while (lo < hi)
2195 {
2196 mid = (lo + hi) >> 1;
2197 if (syms[mid]->value + syms[mid]->section->vma < value)
2198 lo = mid + 1;
2199 else if (syms[mid]->value + syms[mid]->section->vma > value)
2200 hi = mid;
2201 else
2202 return syms[mid];
2203 }
2204 }
2205 else
2206 {
2207 while (lo < hi)
2208 {
2209 mid = (lo + hi) >> 1;
2210 if (syms[mid]->section->id < id)
2211 lo = mid + 1;
2212 else if (syms[mid]->section->id > id)
2213 hi = mid;
2214 else if (syms[mid]->value < value)
2215 lo = mid + 1;
2216 else if (syms[mid]->value > value)
2217 hi = mid;
2218 else
2219 return syms[mid];
2220 }
2221 }
2222 return NULL;
2223 }
2224
2225 static bool
2226 section_covers_vma (bfd *abfd ATTRIBUTE_UNUSED, asection *section, void *ptr)
2227 {
2228 bfd_vma vma = *(bfd_vma *) ptr;
2229 return ((section->flags & SEC_ALLOC) != 0
2230 && section->vma <= vma
2231 && vma < section->vma + section->size);
2232 }
2233
2234 /* Create synthetic symbols, effectively restoring "dot-symbol" function
2235 entry syms. Also generate @plt symbols for the glink branch table.
2236 Returns count of synthetic symbols in RET or -1 on error. */
2237
2238 static long
2239 ppc64_elf_get_synthetic_symtab (bfd *abfd,
2240 long static_count, asymbol **static_syms,
2241 long dyn_count, asymbol **dyn_syms,
2242 asymbol **ret)
2243 {
2244 asymbol *s;
2245 size_t i, j, count;
2246 char *names;
2247 size_t symcount, codesecsym, codesecsymend, secsymend, opdsymend;
2248 asection *opd = NULL;
2249 bool relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
2250 asymbol **syms;
2251 int abi = abiversion (abfd);
2252
2253 *ret = NULL;
2254
2255 if (abi < 2)
2256 {
2257 opd = bfd_get_section_by_name (abfd, ".opd");
2258 if (opd == NULL && abi == 1)
2259 return 0;
2260 }
2261
2262 syms = NULL;
2263 codesecsym = 0;
2264 codesecsymend = 0;
2265 secsymend = 0;
2266 opdsymend = 0;
2267 symcount = 0;
2268 if (opd != NULL)
2269 {
2270 symcount = static_count;
2271 if (!relocatable)
2272 symcount += dyn_count;
2273 if (symcount == 0)
2274 return 0;
2275
2276 syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
2277 if (syms == NULL)
2278 return -1;
2279
2280 if (!relocatable && static_count != 0 && dyn_count != 0)
2281 {
2282 /* Use both symbol tables. */
2283 memcpy (syms, static_syms, static_count * sizeof (*syms));
2284 memcpy (syms + static_count, dyn_syms,
2285 (dyn_count + 1) * sizeof (*syms));
2286 }
2287 else if (!relocatable && static_count == 0)
2288 memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
2289 else
2290 memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
2291
2292 /* Trim uninteresting symbols. Interesting symbols are section,
2293 function, and notype symbols. */
2294 for (i = 0, j = 0; i < symcount; ++i)
2295 if ((syms[i]->flags & (BSF_FILE | BSF_OBJECT | BSF_THREAD_LOCAL
2296 | BSF_RELC | BSF_SRELC)) == 0)
2297 syms[j++] = syms[i];
2298 symcount = j;
2299
2300 synthetic_relocatable = relocatable;
2301 synthetic_opd = opd;
2302 qsort (syms, symcount, sizeof (*syms), compare_symbols);
2303
2304 if (!relocatable && symcount > 1)
2305 {
2306 /* Trim duplicate syms, since we may have merged the normal
2307 and dynamic symbols. Actually, we only care about syms
2308 that have different values, so trim any with the same
2309 value. Don't consider ifunc and ifunc resolver symbols
2310 duplicates however, because GDB wants to know whether a
2311 text symbol is an ifunc resolver. */
2312 for (i = 1, j = 1; i < symcount; ++i)
2313 {
2314 const asymbol *s0 = syms[i - 1];
2315 const asymbol *s1 = syms[i];
2316
2317 if ((s0->value + s0->section->vma
2318 != s1->value + s1->section->vma)
2319 || ((s0->flags & BSF_GNU_INDIRECT_FUNCTION)
2320 != (s1->flags & BSF_GNU_INDIRECT_FUNCTION)))
2321 syms[j++] = syms[i];
2322 }
2323 symcount = j;
2324 }
2325
2326 i = 0;
2327 /* Note that here and in compare_symbols we can't compare opd and
2328 sym->section directly. With separate debug info files, the
2329 symbols will be extracted from the debug file while abfd passed
2330 to this function is the real binary. */
2331 if ((syms[i]->flags & BSF_SECTION_SYM) != 0
2332 && strcmp (syms[i]->section->name, ".opd") == 0)
2333 ++i;
2334 codesecsym = i;
2335
2336 for (; i < symcount; ++i)
2337 if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC
2338 | SEC_THREAD_LOCAL))
2339 != (SEC_CODE | SEC_ALLOC))
2340 || (syms[i]->flags & BSF_SECTION_SYM) == 0)
2341 break;
2342 codesecsymend = i;
2343
2344 for (; i < symcount; ++i)
2345 if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
2346 break;
2347 secsymend = i;
2348
2349 for (; i < symcount; ++i)
2350 if (strcmp (syms[i]->section->name, ".opd") != 0)
2351 break;
2352 opdsymend = i;
2353
2354 for (; i < symcount; ++i)
2355 if (((syms[i]->section->flags
2356 & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL)))
2357 != (SEC_CODE | SEC_ALLOC))
2358 break;
2359 symcount = i;
2360 }
2361 count = 0;
2362
2363 if (relocatable)
2364 {
2365 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2366 arelent *r;
2367 size_t size;
2368 size_t relcount;
2369
2370 if (opdsymend == secsymend)
2371 goto done;
2372
2373 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2374 relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
2375 if (relcount == 0)
2376 goto done;
2377
2378 if (!(*slurp_relocs) (abfd, opd, static_syms, false))
2379 {
2380 count = -1;
2381 goto done;
2382 }
2383
2384 size = 0;
2385 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2386 {
2387 asymbol *sym;
2388
2389 while (r < opd->relocation + relcount
2390 && r->address < syms[i]->value + opd->vma)
2391 ++r;
2392
2393 if (r == opd->relocation + relcount)
2394 break;
2395
2396 if (r->address != syms[i]->value + opd->vma)
2397 continue;
2398
2399 if (r->howto->type != R_PPC64_ADDR64)
2400 continue;
2401
2402 sym = *r->sym_ptr_ptr;
2403 if (!sym_exists_at (syms, opdsymend, symcount,
2404 sym->section->id, sym->value + r->addend))
2405 {
2406 ++count;
2407 size += sizeof (asymbol);
2408 size += strlen (syms[i]->name) + 2;
2409 }
2410 }
2411
2412 if (size == 0)
2413 goto done;
2414 s = *ret = bfd_malloc (size);
2415 if (s == NULL)
2416 {
2417 count = -1;
2418 goto done;
2419 }
2420
2421 names = (char *) (s + count);
2422
2423 for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
2424 {
2425 asymbol *sym;
2426
2427 while (r < opd->relocation + relcount
2428 && r->address < syms[i]->value + opd->vma)
2429 ++r;
2430
2431 if (r == opd->relocation + relcount)
2432 break;
2433
2434 if (r->address != syms[i]->value + opd->vma)
2435 continue;
2436
2437 if (r->howto->type != R_PPC64_ADDR64)
2438 continue;
2439
2440 sym = *r->sym_ptr_ptr;
2441 if (!sym_exists_at (syms, opdsymend, symcount,
2442 sym->section->id, sym->value + r->addend))
2443 {
2444 size_t len;
2445
2446 *s = *syms[i];
2447 s->flags |= BSF_SYNTHETIC;
2448 s->section = sym->section;
2449 s->value = sym->value + r->addend;
2450 s->name = names;
2451 *names++ = '.';
2452 len = strlen (syms[i]->name);
2453 memcpy (names, syms[i]->name, len + 1);
2454 names += len + 1;
2455 /* Have udata.p point back to the original symbol this
2456 synthetic symbol was derived from. */
2457 s->udata.p = syms[i];
2458 s++;
2459 }
2460 }
2461 }
2462 else
2463 {
2464 bool (*slurp_relocs) (bfd *, asection *, asymbol **, bool);
2465 bfd_byte *contents = NULL;
2466 size_t size;
2467 size_t plt_count = 0;
2468 bfd_vma glink_vma = 0, resolv_vma = 0;
2469 asection *dynamic, *glink = NULL, *relplt = NULL;
2470 arelent *p;
2471
2472 if (opd != NULL && !bfd_malloc_and_get_section (abfd, opd, &contents))
2473 {
2474 free_contents_and_exit_err:
2475 count = -1;
2476 free_contents_and_exit:
2477 free (contents);
2478 goto done;
2479 }
2480
2481 size = 0;
2482 for (i = secsymend; i < opdsymend; ++i)
2483 {
2484 bfd_vma ent;
2485
2486 /* Ignore bogus symbols. */
2487 if (syms[i]->value > opd->size - 8)
2488 continue;
2489
2490 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2491 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2492 {
2493 ++count;
2494 size += sizeof (asymbol);
2495 size += strlen (syms[i]->name) + 2;
2496 }
2497 }
2498
2499 /* Get start of .glink stubs from DT_PPC64_GLINK. */
2500 if (dyn_count != 0
2501 && (dynamic = bfd_get_section_by_name (abfd, ".dynamic")) != NULL)
2502 {
2503 bfd_byte *dynbuf, *extdyn, *extdynend;
2504 size_t extdynsize;
2505 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *);
2506
2507 if (!bfd_malloc_and_get_section (abfd, dynamic, &dynbuf))
2508 goto free_contents_and_exit_err;
2509
2510 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn;
2511 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in;
2512
2513 extdyn = dynbuf;
2514 extdynend = extdyn + dynamic->size;
2515 for (; extdyn < extdynend; extdyn += extdynsize)
2516 {
2517 Elf_Internal_Dyn dyn;
2518 (*swap_dyn_in) (abfd, extdyn, &dyn);
2519
2520 if (dyn.d_tag == DT_NULL)
2521 break;
2522
2523 if (dyn.d_tag == DT_PPC64_GLINK)
2524 {
2525 /* The first glink stub starts at DT_PPC64_GLINK plus 32.
2526 See comment in ppc64_elf_finish_dynamic_sections. */
2527 glink_vma = dyn.d_un.d_val + 8 * 4;
2528 /* The .glink section usually does not survive the final
2529 link; search for the section (usually .text) where the
2530 glink stubs now reside. */
2531 glink = bfd_sections_find_if (abfd, section_covers_vma,
2532 &glink_vma);
2533 break;
2534 }
2535 }
2536
2537 free (dynbuf);
2538 }
2539
2540 if (glink != NULL)
2541 {
2542 /* Determine __glink trampoline by reading the relative branch
2543 from the first glink stub. */
2544 bfd_byte buf[4];
2545 unsigned int off = 0;
2546
2547 while (bfd_get_section_contents (abfd, glink, buf,
2548 glink_vma + off - glink->vma, 4))
2549 {
2550 unsigned int insn = bfd_get_32 (abfd, buf);
2551 insn ^= B_DOT;
2552 if ((insn & ~0x3fffffc) == 0)
2553 {
2554 resolv_vma
2555 = glink_vma + off + (insn ^ 0x2000000) - 0x2000000;
2556 break;
2557 }
2558 off += 4;
2559 if (off > 4)
2560 break;
2561 }
2562
2563 if (resolv_vma)
2564 size += sizeof (asymbol) + sizeof ("__glink_PLTresolve");
2565
2566 relplt = bfd_get_section_by_name (abfd, ".rela.plt");
2567 if (relplt != NULL)
2568 {
2569 slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
2570 if (!(*slurp_relocs) (abfd, relplt, dyn_syms, true))
2571 goto free_contents_and_exit_err;
2572
2573 plt_count = relplt->size / sizeof (Elf64_External_Rela);
2574 size += plt_count * sizeof (asymbol);
2575
2576 p = relplt->relocation;
2577 for (i = 0; i < plt_count; i++, p++)
2578 {
2579 size += strlen ((*p->sym_ptr_ptr)->name) + sizeof ("@plt");
2580 if (p->addend != 0)
2581 size += sizeof ("+0x") - 1 + 16;
2582 }
2583 }
2584 }
2585
2586 if (size == 0)
2587 goto free_contents_and_exit;
2588 s = *ret = bfd_malloc (size);
2589 if (s == NULL)
2590 goto free_contents_and_exit_err;
2591
2592 names = (char *) (s + count + plt_count + (resolv_vma != 0));
2593
2594 for (i = secsymend; i < opdsymend; ++i)
2595 {
2596 bfd_vma ent;
2597
2598 if (syms[i]->value > opd->size - 8)
2599 continue;
2600
2601 ent = bfd_get_64 (abfd, contents + syms[i]->value);
2602 if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
2603 {
2604 size_t lo, hi;
2605 size_t len;
2606 asection *sec = abfd->sections;
2607
2608 *s = *syms[i];
2609 lo = codesecsym;
2610 hi = codesecsymend;
2611 while (lo < hi)
2612 {
2613 size_t mid = (lo + hi) >> 1;
2614 if (syms[mid]->section->vma < ent)
2615 lo = mid + 1;
2616 else if (syms[mid]->section->vma > ent)
2617 hi = mid;
2618 else
2619 {
2620 sec = syms[mid]->section;
2621 break;
2622 }
2623 }
2624
2625 if (lo >= hi && lo > codesecsym)
2626 sec = syms[lo - 1]->section;
2627
2628 for (; sec != NULL; sec = sec->next)
2629 {
2630 if (sec->vma > ent)
2631 break;
2632 /* SEC_LOAD may not be set if SEC is from a separate debug
2633 info file. */
2634 if ((sec->flags & SEC_ALLOC) == 0)
2635 break;
2636 if ((sec->flags & SEC_CODE) != 0)
2637 s->section = sec;
2638 }
2639 s->flags |= BSF_SYNTHETIC;
2640 s->value = ent - s->section->vma;
2641 s->name = names;
2642 *names++ = '.';
2643 len = strlen (syms[i]->name);
2644 memcpy (names, syms[i]->name, len + 1);
2645 names += len + 1;
2646 /* Have udata.p point back to the original symbol this
2647 synthetic symbol was derived from. */
2648 s->udata.p = syms[i];
2649 s++;
2650 }
2651 }
2652 free (contents);
2653
2654 if (glink != NULL && relplt != NULL)
2655 {
2656 if (resolv_vma)
2657 {
2658 /* Add a symbol for the main glink trampoline. */
2659 memset (s, 0, sizeof *s);
2660 s->the_bfd = abfd;
2661 s->flags = BSF_GLOBAL | BSF_SYNTHETIC;
2662 s->section = glink;
2663 s->value = resolv_vma - glink->vma;
2664 s->name = names;
2665 memcpy (names, "__glink_PLTresolve",
2666 sizeof ("__glink_PLTresolve"));
2667 names += sizeof ("__glink_PLTresolve");
2668 s++;
2669 count++;
2670 }
2671
2672 /* FIXME: It would be very much nicer to put sym@plt on the
2673 stub rather than on the glink branch table entry. The
2674 objdump disassembler would then use a sensible symbol
2675 name on plt calls. The difficulty in doing so is
2676 a) finding the stubs, and,
2677 b) matching stubs against plt entries, and,
2678 c) there can be multiple stubs for a given plt entry.
2679
2680 Solving (a) could be done by code scanning, but older
2681 ppc64 binaries used different stubs to current code.
2682 (b) is the tricky one since you need to known the toc
2683 pointer for at least one function that uses a pic stub to
2684 be able to calculate the plt address referenced.
2685 (c) means gdb would need to set multiple breakpoints (or
2686 find the glink branch itself) when setting breakpoints
2687 for pending shared library loads. */
2688 p = relplt->relocation;
2689 for (i = 0; i < plt_count; i++, p++)
2690 {
2691 size_t len;
2692
2693 *s = **p->sym_ptr_ptr;
2694 /* Undefined syms won't have BSF_LOCAL or BSF_GLOBAL set. Since
2695 we are defining a symbol, ensure one of them is set. */
2696 if ((s->flags & BSF_LOCAL) == 0)
2697 s->flags |= BSF_GLOBAL;
2698 s->flags |= BSF_SYNTHETIC;
2699 s->section = glink;
2700 s->value = glink_vma - glink->vma;
2701 s->name = names;
2702 s->udata.p = NULL;
2703 len = strlen ((*p->sym_ptr_ptr)->name);
2704 memcpy (names, (*p->sym_ptr_ptr)->name, len);
2705 names += len;
2706 if (p->addend != 0)
2707 {
2708 memcpy (names, "+0x", sizeof ("+0x") - 1);
2709 names += sizeof ("+0x") - 1;
2710 bfd_sprintf_vma (abfd, names, p->addend);
2711 names += strlen (names);
2712 }
2713 memcpy (names, "@plt", sizeof ("@plt"));
2714 names += sizeof ("@plt");
2715 s++;
2716 if (abi < 2)
2717 {
2718 glink_vma += 8;
2719 if (i >= 0x8000)
2720 glink_vma += 4;
2721 }
2722 else
2723 glink_vma += 4;
2724 }
2725 count += plt_count;
2726 }
2727 }
2728
2729 done:
2730 free (syms);
2731 return count;
2732 }
2733 \f
2734 /* The following functions are specific to the ELF linker, while
2735 functions above are used generally. Those named ppc64_elf_* are
2736 called by the main ELF linker code. They appear in this file more
2737 or less in the order in which they are called. eg.
2738 ppc64_elf_check_relocs is called early in the link process,
2739 ppc64_elf_finish_dynamic_sections is one of the last functions
2740 called.
2741
2742 PowerPC64-ELF uses a similar scheme to PowerPC64-XCOFF in that
2743 functions have both a function code symbol and a function descriptor
2744 symbol. A call to foo in a relocatable object file looks like:
2745
2746 . .text
2747 . x:
2748 . bl .foo
2749 . nop
2750
2751 The function definition in another object file might be:
2752
2753 . .section .opd
2754 . foo: .quad .foo
2755 . .quad .TOC.@tocbase
2756 . .quad 0
2757 .
2758 . .text
2759 . .foo: blr
2760
2761 When the linker resolves the call during a static link, the branch
2762 unsurprisingly just goes to .foo and the .opd information is unused.
2763 If the function definition is in a shared library, things are a little
2764 different: The call goes via a plt call stub, the opd information gets
2765 copied to the plt, and the linker patches the nop.
2766
2767 . x:
2768 . bl .foo_stub
2769 . ld 2,40(1)
2770 .
2771 .
2772 . .foo_stub:
2773 . std 2,40(1) # in practice, the call stub
2774 . addis 11,2,Lfoo@toc@ha # is slightly optimized, but
2775 . addi 11,11,Lfoo@toc@l # this is the general idea
2776 . ld 12,0(11)
2777 . ld 2,8(11)
2778 . mtctr 12
2779 . ld 11,16(11)
2780 . bctr
2781 .
2782 . .section .plt
2783 . Lfoo: reloc (R_PPC64_JMP_SLOT, foo)
2784
2785 The "reloc ()" notation is supposed to indicate that the linker emits
2786 an R_PPC64_JMP_SLOT reloc against foo. The dynamic linker does the opd
2787 copying.
2788
2789 What are the difficulties here? Well, firstly, the relocations
2790 examined by the linker in check_relocs are against the function code
2791 sym .foo, while the dynamic relocation in the plt is emitted against
2792 the function descriptor symbol, foo. Somewhere along the line, we need
2793 to carefully copy dynamic link information from one symbol to the other.
2794 Secondly, the generic part of the elf linker will make .foo a dynamic
2795 symbol as is normal for most other backends. We need foo dynamic
2796 instead, at least for an application final link. However, when
2797 creating a shared library containing foo, we need to have both symbols
2798 dynamic so that references to .foo are satisfied during the early
2799 stages of linking. Otherwise the linker might decide to pull in a
2800 definition from some other object, eg. a static library.
2801
2802 Update: As of August 2004, we support a new convention. Function
2803 calls may use the function descriptor symbol, ie. "bl foo". This
2804 behaves exactly as "bl .foo". */
2805
2806 /* Of those relocs that might be copied as dynamic relocs, this
2807 function selects those that must be copied when linking a shared
2808 library or PIE, even when the symbol is local. */
2809
2810 static int
2811 must_be_dyn_reloc (struct bfd_link_info *info,
2812 enum elf_ppc64_reloc_type r_type)
2813 {
2814 switch (r_type)
2815 {
2816 default:
2817 /* Only relative relocs can be resolved when the object load
2818 address isn't fixed. DTPREL64 is excluded because the
2819 dynamic linker needs to differentiate global dynamic from
2820 local dynamic __tls_index pairs when PPC64_OPT_TLS is set. */
2821 return 1;
2822
2823 case R_PPC64_REL32:
2824 case R_PPC64_REL64:
2825 case R_PPC64_REL30:
2826 case R_PPC64_TOC16:
2827 case R_PPC64_TOC16_DS:
2828 case R_PPC64_TOC16_LO:
2829 case R_PPC64_TOC16_HI:
2830 case R_PPC64_TOC16_HA:
2831 case R_PPC64_TOC16_LO_DS:
2832 return 0;
2833
2834 case R_PPC64_TPREL16:
2835 case R_PPC64_TPREL16_LO:
2836 case R_PPC64_TPREL16_HI:
2837 case R_PPC64_TPREL16_HA:
2838 case R_PPC64_TPREL16_DS:
2839 case R_PPC64_TPREL16_LO_DS:
2840 case R_PPC64_TPREL16_HIGH:
2841 case R_PPC64_TPREL16_HIGHA:
2842 case R_PPC64_TPREL16_HIGHER:
2843 case R_PPC64_TPREL16_HIGHERA:
2844 case R_PPC64_TPREL16_HIGHEST:
2845 case R_PPC64_TPREL16_HIGHESTA:
2846 case R_PPC64_TPREL64:
2847 case R_PPC64_TPREL34:
2848 /* These relocations are relative but in a shared library the
2849 linker doesn't know the thread pointer base. */
2850 return bfd_link_dll (info);
2851 }
2852 }
2853
2854 /* If ELIMINATE_COPY_RELOCS is non-zero, the linker will try to avoid
2855 copying dynamic variables from a shared lib into an app's .dynbss
2856 section, and instead use a dynamic relocation to point into the
2857 shared lib. With code that gcc generates it is vital that this be
2858 enabled; In the PowerPC64 ELFv1 ABI the address of a function is
2859 actually the address of a function descriptor which resides in the
2860 .opd section. gcc uses the descriptor directly rather than going
2861 via the GOT as some other ABIs do, which means that initialized
2862 function pointers reference the descriptor. Thus, a function
2863 pointer initialized to the address of a function in a shared
2864 library will either require a .dynbss copy and a copy reloc, or a
2865 dynamic reloc. Using a .dynbss copy redefines the function
2866 descriptor symbol to point to the copy. This presents a problem as
2867 a PLT entry for that function is also initialized from the function
2868 descriptor symbol and the copy may not be initialized first. */
2869 #define ELIMINATE_COPY_RELOCS 1
2870
2871 /* Section name for stubs is the associated section name plus this
2872 string. */
2873 #define STUB_SUFFIX ".stub"
2874
2875 /* Linker stubs.
2876 ppc_stub_long_branch:
2877 Used when a 14 bit branch (or even a 24 bit branch) can't reach its
2878 destination, but a 24 bit branch in a stub section will reach.
2879 . b dest
2880
2881 ppc_stub_plt_branch:
2882 Similar to the above, but a 24 bit branch in the stub section won't
2883 reach its destination.
2884 . addis %r12,%r2,xxx@toc@ha
2885 . ld %r12,xxx@toc@l(%r12)
2886 . mtctr %r12
2887 . bctr
2888
2889 ppc_stub_plt_call:
2890 Used to call a function in a shared library. If it so happens that
2891 the plt entry referenced crosses a 64k boundary, then an extra
2892 "addi %r11,%r11,xxx@toc@l" will be inserted before the "mtctr".
2893 An r2save variant starts with "std %r2,40(%r1)".
2894 . addis %r11,%r2,xxx@toc@ha
2895 . ld %r12,xxx+0@toc@l(%r11)
2896 . mtctr %r12
2897 . ld %r2,xxx+8@toc@l(%r11)
2898 . ld %r11,xxx+16@toc@l(%r11)
2899 . bctr
2900
2901 ppc_stub_long_branch and ppc_stub_plt_branch may also have additional
2902 code to adjust the value and save r2 to support multiple toc sections.
2903 A ppc_stub_long_branch with an r2 offset looks like:
2904 . std %r2,40(%r1)
2905 . addis %r2,%r2,off@ha
2906 . addi %r2,%r2,off@l
2907 . b dest
2908
2909 A ppc_stub_plt_branch with an r2 offset looks like:
2910 . std %r2,40(%r1)
2911 . addis %r12,%r2,xxx@toc@ha
2912 . ld %r12,xxx@toc@l(%r12)
2913 . addis %r2,%r2,off@ha
2914 . addi %r2,%r2,off@l
2915 . mtctr %r12
2916 . bctr
2917
2918 All of the above stubs are shown as their ELFv1 variants. ELFv2
2919 variants exist too, simpler for plt calls since a new toc pointer
2920 and static chain are not loaded by the stub. In addition, ELFv2
2921 has some more complex stubs to handle calls marked with NOTOC
2922 relocs from functions where r2 is not a valid toc pointer.
2923 ppc_stub_long_branch_p9notoc:
2924 . mflr %r12
2925 . bcl 20,31,1f
2926 . 1:
2927 . mflr %r11
2928 . mtlr %r12
2929 . addis %r12,%r11,dest-1b@ha
2930 . addi %r12,%r12,dest-1b@l
2931 . b dest
2932
2933 ppc_stub_plt_branch_p9notoc:
2934 . mflr %r12
2935 . bcl 20,31,1f
2936 . 1:
2937 . mflr %r11
2938 . mtlr %r12
2939 . lis %r12,xxx-1b@highest
2940 . ori %r12,%r12,xxx-1b@higher
2941 . sldi %r12,%r12,32
2942 . oris %r12,%r12,xxx-1b@high
2943 . ori %r12,%r12,xxx-1b@l
2944 . add %r12,%r11,%r12
2945 . mtctr %r12
2946 . bctr
2947
2948 ppc_stub_plt_call_p9notoc:
2949 . mflr %r12
2950 . bcl 20,31,1f
2951 . 1:
2952 . mflr %r11
2953 . mtlr %r12
2954 . lis %r12,xxx-1b@highest
2955 . ori %r12,%r12,xxx-1b@higher
2956 . sldi %r12,%r12,32
2957 . oris %r12,%r12,xxx-1b@high
2958 . ori %r12,%r12,xxx-1b@l
2959 . ldx %r12,%r11,%r12
2960 . mtctr %r12
2961 . bctr
2962
2963 There are also ELFv1 power10 variants of these stubs.
2964 ppc_stub_long_branch_notoc:
2965 . pla %r12,dest@pcrel
2966 . b dest
2967 ppc_stub_plt_branch_notoc:
2968 . lis %r11,(dest-1f)@highesta34
2969 . ori %r11,%r11,(dest-1f)@highera34
2970 . sldi %r11,%r11,34
2971 . 1: pla %r12,dest@pcrel
2972 . add %r12,%r11,%r12
2973 . mtctr %r12
2974 . bctr
2975 ppc_stub_plt_call_notoc:
2976 . lis %r11,(xxx-1f)@highesta34
2977 . ori %r11,%r11,(xxx-1f)@highera34
2978 . sldi %r11,%r11,34
2979 . 1: pla %r12,xxx@pcrel
2980 . ldx %r12,%r11,%r12
2981 . mtctr %r12
2982 . bctr
2983
2984 In cases where the high instructions would add zero, they are
2985 omitted and following instructions modified in some cases.
2986 For example, a power10 ppc_stub_plt_call_notoc might simplify down
2987 to
2988 . pld %r12,xxx@pcrel
2989 . mtctr %r12
2990 . bctr
2991
2992 Stub variants may be merged. For example, if printf is called from
2993 code with the tocsave optimization (ie. r2 saved in function
2994 prologue) and therefore calls use a ppc_stub_plt_call linkage stub,
2995 and from other code without the tocsave optimization requiring a
2996 ppc_stub_plt_call_r2save linkage stub, a single stub of the latter
2997 type will be created. Calls with the tocsave optimization will
2998 enter this stub after the instruction saving r2. A similar
2999 situation exists when calls are marked with R_PPC64_REL24_NOTOC
3000 relocations. These require a ppc_stub_plt_call_notoc linkage stub
3001 to call an external function like printf. If other calls to printf
3002 require a ppc_stub_plt_call linkage stub then a single
3003 ppc_stub_plt_call_notoc linkage stub may be used for both types of
3004 call. */
3005
3006 enum ppc_stub_main_type
3007 {
3008 ppc_stub_none,
3009 ppc_stub_long_branch,
3010 ppc_stub_plt_branch,
3011 ppc_stub_plt_call,
3012 ppc_stub_global_entry,
3013 ppc_stub_save_res
3014 };
3015
3016 /* ppc_stub_long_branch, ppc_stub_plt_branch and ppc_stub_plt_call have
3017 these variations. */
3018
3019 enum ppc_stub_sub_type
3020 {
3021 ppc_stub_toc,
3022 ppc_stub_notoc,
3023 ppc_stub_p9notoc
3024 };
3025
3026 struct ppc_stub_type
3027 {
3028 ENUM_BITFIELD (ppc_stub_main_type) main : 3;
3029 ENUM_BITFIELD (ppc_stub_sub_type) sub : 2;
3030 unsigned int r2save : 1;
3031 };
3032
3033 /* Information on stub grouping. */
3034 struct map_stub
3035 {
3036 /* The stub section. */
3037 asection *stub_sec;
3038 /* This is the section to which stubs in the group will be attached. */
3039 asection *link_sec;
3040 /* Next group. */
3041 struct map_stub *next;
3042 /* Whether to emit a copy of register save/restore functions in this
3043 group. */
3044 int needs_save_res;
3045 /* Current offset within stubs after the insn restoring lr in a
3046 _notoc or _both stub using bcl for pc-relative addressing, or
3047 after the insn restoring lr in a __tls_get_addr_opt plt stub. */
3048 unsigned int lr_restore;
3049 /* Accumulated size of EH info emitted to describe return address
3050 if stubs modify lr. Does not include 17 byte FDE header. */
3051 unsigned int eh_size;
3052 /* Offset in glink_eh_frame to the start of EH info for this group. */
3053 unsigned int eh_base;
3054 };
3055
3056 struct ppc_stub_hash_entry
3057 {
3058 /* Base hash table entry structure. */
3059 struct bfd_hash_entry root;
3060
3061 struct ppc_stub_type type;
3062
3063 /* Group information. */
3064 struct map_stub *group;
3065
3066 /* Offset within stub_sec of the beginning of this stub. */
3067 bfd_vma stub_offset;
3068
3069 /* Given the symbol's value and its section we can determine its final
3070 value when building the stubs (so the stub knows where to jump. */
3071 bfd_vma target_value;
3072 asection *target_section;
3073
3074 /* The symbol table entry, if any, that this was derived from. */
3075 struct ppc_link_hash_entry *h;
3076 struct plt_entry *plt_ent;
3077
3078 /* Symbol type. */
3079 unsigned char symtype;
3080
3081 /* Symbol st_other. */
3082 unsigned char other;
3083 };
3084
3085 struct ppc_branch_hash_entry
3086 {
3087 /* Base hash table entry structure. */
3088 struct bfd_hash_entry root;
3089
3090 /* Offset within branch lookup table. */
3091 unsigned int offset;
3092
3093 /* Generation marker. */
3094 unsigned int iter;
3095 };
3096
3097 /* Used to track dynamic relocations for local symbols. */
3098 struct ppc_dyn_relocs
3099 {
3100 struct ppc_dyn_relocs *next;
3101
3102 /* The input section of the reloc. */
3103 asection *sec;
3104
3105 /* Total number of relocs copied for the input section. */
3106 unsigned int count : 31;
3107
3108 /* Whether this entry is for STT_GNU_IFUNC symbols. */
3109 unsigned int ifunc : 1;
3110 };
3111
3112 struct ppc_link_hash_entry
3113 {
3114 struct elf_link_hash_entry elf;
3115
3116 union
3117 {
3118 /* A pointer to the most recently used stub hash entry against this
3119 symbol. */
3120 struct ppc_stub_hash_entry *stub_cache;
3121
3122 /* A pointer to the next symbol starting with a '.' */
3123 struct ppc_link_hash_entry *next_dot_sym;
3124 } u;
3125
3126 /* Link between function code and descriptor symbols. */
3127 struct ppc_link_hash_entry *oh;
3128
3129 /* Flag function code and descriptor symbols. */
3130 unsigned int is_func:1;
3131 unsigned int is_func_descriptor:1;
3132 unsigned int fake:1;
3133
3134 /* Whether global opd/toc sym has been adjusted or not.
3135 After ppc64_elf_edit_opd/ppc64_elf_edit_toc has run, this flag
3136 should be set for all globals defined in any opd/toc section. */
3137 unsigned int adjust_done:1;
3138
3139 /* Set if this is an out-of-line register save/restore function,
3140 with non-standard calling convention. */
3141 unsigned int save_res:1;
3142
3143 /* Set if a duplicate symbol with non-zero localentry is detected,
3144 even when the duplicate symbol does not provide a definition. */
3145 unsigned int non_zero_localentry:1;
3146
3147 /* Contexts in which symbol is used in the GOT (or TOC).
3148 Bits are or'd into the mask as the corresponding relocs are
3149 encountered during check_relocs, with TLS_TLS being set when any
3150 of the other TLS bits are set. tls_optimize clears bits when
3151 optimizing to indicate the corresponding GOT entry type is not
3152 needed. If set, TLS_TLS is never cleared. tls_optimize may also
3153 set TLS_GDIE when a GD reloc turns into an IE one.
3154 These flags are also kept for local symbols. */
3155 #define TLS_TLS 1 /* Any TLS reloc. */
3156 #define TLS_GD 2 /* GD reloc. */
3157 #define TLS_LD 4 /* LD reloc. */
3158 #define TLS_TPREL 8 /* TPREL reloc, => IE. */
3159 #define TLS_DTPREL 16 /* DTPREL reloc, => LD. */
3160 #define TLS_MARK 32 /* __tls_get_addr call marked. */
3161 #define TLS_GDIE 64 /* GOT TPREL reloc resulting from GD->IE. */
3162 #define TLS_EXPLICIT 256 /* TOC section TLS reloc, not stored. */
3163 unsigned char tls_mask;
3164
3165 /* The above field is also used to mark function symbols. In which
3166 case TLS_TLS will be 0. */
3167 #define PLT_IFUNC 2 /* STT_GNU_IFUNC. */
3168 #define PLT_KEEP 4 /* inline plt call requires plt entry. */
3169 #define NON_GOT 256 /* local symbol plt, not stored. */
3170 };
3171
3172 static inline struct ppc_link_hash_entry *
3173 ppc_elf_hash_entry (struct elf_link_hash_entry *ent)
3174 {
3175 return (struct ppc_link_hash_entry *) ent;
3176 }
3177
3178 static inline struct elf_link_hash_entry *
3179 elf_hash_entry (struct ppc_link_hash_entry *ent)
3180 {
3181 return (struct elf_link_hash_entry *) ent;
3182 }
3183
3184 /* ppc64 ELF linker hash table. */
3185
3186 struct ppc_link_hash_table
3187 {
3188 struct elf_link_hash_table elf;
3189
3190 /* The stub hash table. */
3191 struct bfd_hash_table stub_hash_table;
3192
3193 /* Another hash table for plt_branch stubs. */
3194 struct bfd_hash_table branch_hash_table;
3195
3196 /* Hash table for function prologue tocsave. */
3197 htab_t tocsave_htab;
3198
3199 /* Various options and other info passed from the linker. */
3200 struct ppc64_elf_params *params;
3201
3202 /* The size of sec_info below. */
3203 unsigned int sec_info_arr_size;
3204
3205 /* Per-section array of extra section info. Done this way rather
3206 than as part of ppc64_elf_section_data so we have the info for
3207 non-ppc64 sections. */
3208 struct
3209 {
3210 /* Along with elf_gp, specifies the TOC pointer used by this section. */
3211 bfd_vma toc_off;
3212
3213 union
3214 {
3215 /* The section group that this section belongs to. */
3216 struct map_stub *group;
3217 /* A temp section list pointer. */
3218 asection *list;
3219 } u;
3220 } *sec_info;
3221
3222 /* Linked list of groups. */
3223 struct map_stub *group;
3224
3225 /* Temp used when calculating TOC pointers. */
3226 bfd_vma toc_curr;
3227 bfd *toc_bfd;
3228 asection *toc_first_sec;
3229
3230 /* Used when adding symbols. */
3231 struct ppc_link_hash_entry *dot_syms;
3232
3233 /* Shortcuts to get to dynamic linker sections. */
3234 asection *glink;
3235 asection *global_entry;
3236 asection *sfpr;
3237 asection *pltlocal;
3238 asection *relpltlocal;
3239 asection *brlt;
3240 asection *relbrlt;
3241 asection *glink_eh_frame;
3242
3243 /* Shortcut to .__tls_get_addr and __tls_get_addr. */
3244 struct ppc_link_hash_entry *tls_get_addr;
3245 struct ppc_link_hash_entry *tls_get_addr_fd;
3246 struct ppc_link_hash_entry *tga_desc;
3247 struct ppc_link_hash_entry *tga_desc_fd;
3248 struct map_stub *tga_group;
3249
3250 /* The size of reliplt used by got entry relocs. */
3251 bfd_size_type got_reli_size;
3252
3253 /* Statistics. */
3254 unsigned long stub_count[ppc_stub_save_res];
3255
3256 /* Number of stubs against global syms. */
3257 unsigned long stub_globals;
3258
3259 /* Set if we're linking code with function descriptors. */
3260 unsigned int opd_abi:1;
3261
3262 /* Support for multiple toc sections. */
3263 unsigned int do_multi_toc:1;
3264 unsigned int multi_toc_needed:1;
3265 unsigned int second_toc_pass:1;
3266 unsigned int do_toc_opt:1;
3267
3268 /* Set if tls optimization is enabled. */
3269 unsigned int do_tls_opt:1;
3270
3271 /* Set if inline plt calls should be converted to direct calls. */
3272 unsigned int can_convert_all_inline_plt:1;
3273
3274 /* Set on error. */
3275 unsigned int stub_error:1;
3276
3277 /* Whether func_desc_adjust needs to be run over symbols. */
3278 unsigned int need_func_desc_adj:1;
3279
3280 /* Whether plt calls for ELFv2 localentry:0 funcs have been optimized. */
3281 unsigned int has_plt_localentry0:1;
3282
3283 /* Whether calls are made via the PLT from NOTOC functions. */
3284 unsigned int notoc_plt:1;
3285
3286 /* Whether any code linked seems to be Power10. */
3287 unsigned int has_power10_relocs:1;
3288
3289 /* Incremented every time we size stubs. */
3290 unsigned int stub_iteration;
3291 };
3292
3293 /* Rename some of the generic section flags to better document how they
3294 are used here. */
3295
3296 /* Nonzero if this section has TLS related relocations. */
3297 #define has_tls_reloc sec_flg0
3298
3299 /* Nonzero if this section has a call to __tls_get_addr lacking marker
3300 relocations. */
3301 #define nomark_tls_get_addr sec_flg1
3302
3303 /* Nonzero if this section has any toc or got relocs. */
3304 #define has_toc_reloc sec_flg2
3305
3306 /* Nonzero if this section has a call to another section that uses
3307 the toc or got. */
3308 #define makes_toc_func_call sec_flg3
3309
3310 /* Recursion protection when determining above flag. */
3311 #define call_check_in_progress sec_flg4
3312 #define call_check_done sec_flg5
3313
3314 /* Get the ppc64 ELF linker hash table from a link_info structure. */
3315
3316 #define ppc_hash_table(p) \
3317 ((is_elf_hash_table ((p)->hash) \
3318 && elf_hash_table_id (elf_hash_table (p)) == PPC64_ELF_DATA) \
3319 ? (struct ppc_link_hash_table *) (p)->hash : NULL)
3320
3321 #define ppc_stub_hash_lookup(table, string, create, copy) \
3322 ((struct ppc_stub_hash_entry *) \
3323 bfd_hash_lookup ((table), (string), (create), (copy)))
3324
3325 #define ppc_branch_hash_lookup(table, string, create, copy) \
3326 ((struct ppc_branch_hash_entry *) \
3327 bfd_hash_lookup ((table), (string), (create), (copy)))
3328
3329 /* Create an entry in the stub hash table. */
3330
3331 static struct bfd_hash_entry *
3332 stub_hash_newfunc (struct bfd_hash_entry *entry,
3333 struct bfd_hash_table *table,
3334 const char *string)
3335 {
3336 /* Allocate the structure if it has not already been allocated by a
3337 subclass. */
3338 if (entry == NULL)
3339 {
3340 entry = bfd_hash_allocate (table, sizeof (struct ppc_stub_hash_entry));
3341 if (entry == NULL)
3342 return entry;
3343 }
3344
3345 /* Call the allocation method of the superclass. */
3346 entry = bfd_hash_newfunc (entry, table, string);
3347 if (entry != NULL)
3348 {
3349 struct ppc_stub_hash_entry *eh;
3350
3351 /* Initialize the local fields. */
3352 eh = (struct ppc_stub_hash_entry *) entry;
3353 eh->type.main = ppc_stub_none;
3354 eh->type.sub = ppc_stub_toc;
3355 eh->type.r2save = 0;
3356 eh->group = NULL;
3357 eh->stub_offset = 0;
3358 eh->target_value = 0;
3359 eh->target_section = NULL;
3360 eh->h = NULL;
3361 eh->plt_ent = NULL;
3362 eh->other = 0;
3363 }
3364
3365 return entry;
3366 }
3367
3368 /* Create an entry in the branch hash table. */
3369
3370 static struct bfd_hash_entry *
3371 branch_hash_newfunc (struct bfd_hash_entry *entry,
3372 struct bfd_hash_table *table,
3373 const char *string)
3374 {
3375 /* Allocate the structure if it has not already been allocated by a
3376 subclass. */
3377 if (entry == NULL)
3378 {
3379 entry = bfd_hash_allocate (table, sizeof (struct ppc_branch_hash_entry));
3380 if (entry == NULL)
3381 return entry;
3382 }
3383
3384 /* Call the allocation method of the superclass. */
3385 entry = bfd_hash_newfunc (entry, table, string);
3386 if (entry != NULL)
3387 {
3388 struct ppc_branch_hash_entry *eh;
3389
3390 /* Initialize the local fields. */
3391 eh = (struct ppc_branch_hash_entry *) entry;
3392 eh->offset = 0;
3393 eh->iter = 0;
3394 }
3395
3396 return entry;
3397 }
3398
3399 /* Create an entry in a ppc64 ELF linker hash table. */
3400
3401 static struct bfd_hash_entry *
3402 link_hash_newfunc (struct bfd_hash_entry *entry,
3403 struct bfd_hash_table *table,
3404 const char *string)
3405 {
3406 /* Allocate the structure if it has not already been allocated by a
3407 subclass. */
3408 if (entry == NULL)
3409 {
3410 entry = bfd_hash_allocate (table, sizeof (struct ppc_link_hash_entry));
3411 if (entry == NULL)
3412 return entry;
3413 }
3414
3415 /* Call the allocation method of the superclass. */
3416 entry = _bfd_elf_link_hash_newfunc (entry, table, string);
3417 if (entry != NULL)
3418 {
3419 struct ppc_link_hash_entry *eh = (struct ppc_link_hash_entry *) entry;
3420
3421 memset (&eh->u.stub_cache, 0,
3422 (sizeof (struct ppc_link_hash_entry)
3423 - offsetof (struct ppc_link_hash_entry, u.stub_cache)));
3424
3425 /* When making function calls, old ABI code references function entry
3426 points (dot symbols), while new ABI code references the function
3427 descriptor symbol. We need to make any combination of reference and
3428 definition work together, without breaking archive linking.
3429
3430 For a defined function "foo" and an undefined call to "bar":
3431 An old object defines "foo" and ".foo", references ".bar" (possibly
3432 "bar" too).
3433 A new object defines "foo" and references "bar".
3434
3435 A new object thus has no problem with its undefined symbols being
3436 satisfied by definitions in an old object. On the other hand, the
3437 old object won't have ".bar" satisfied by a new object.
3438
3439 Keep a list of newly added dot-symbols. */
3440
3441 if (string[0] == '.')
3442 {
3443 struct ppc_link_hash_table *htab;
3444
3445 htab = (struct ppc_link_hash_table *) table;
3446 eh->u.next_dot_sym = htab->dot_syms;
3447 htab->dot_syms = eh;
3448 }
3449 }
3450
3451 return entry;
3452 }
3453
3454 struct tocsave_entry
3455 {
3456 asection *sec;
3457 bfd_vma offset;
3458 };
3459
3460 static hashval_t
3461 tocsave_htab_hash (const void *p)
3462 {
3463 const struct tocsave_entry *e = (const struct tocsave_entry *) p;
3464 return ((bfd_vma) (intptr_t) e->sec ^ e->offset) >> 3;
3465 }
3466
3467 static int
3468 tocsave_htab_eq (const void *p1, const void *p2)
3469 {
3470 const struct tocsave_entry *e1 = (const struct tocsave_entry *) p1;
3471 const struct tocsave_entry *e2 = (const struct tocsave_entry *) p2;
3472 return e1->sec == e2->sec && e1->offset == e2->offset;
3473 }
3474
3475 /* Destroy a ppc64 ELF linker hash table. */
3476
3477 static void
3478 ppc64_elf_link_hash_table_free (bfd *obfd)
3479 {
3480 struct ppc_link_hash_table *htab;
3481
3482 htab = (struct ppc_link_hash_table *) obfd->link.hash;
3483 if (htab->tocsave_htab)
3484 htab_delete (htab->tocsave_htab);
3485 bfd_hash_table_free (&htab->branch_hash_table);
3486 bfd_hash_table_free (&htab->stub_hash_table);
3487 _bfd_elf_link_hash_table_free (obfd);
3488 }
3489
3490 /* Create a ppc64 ELF linker hash table. */
3491
3492 static struct bfd_link_hash_table *
3493 ppc64_elf_link_hash_table_create (bfd *abfd)
3494 {
3495 struct ppc_link_hash_table *htab;
3496 size_t amt = sizeof (struct ppc_link_hash_table);
3497
3498 htab = bfd_zmalloc (amt);
3499 if (htab == NULL)
3500 return NULL;
3501
3502 if (!_bfd_elf_link_hash_table_init (&htab->elf, abfd, link_hash_newfunc,
3503 sizeof (struct ppc_link_hash_entry),
3504 PPC64_ELF_DATA))
3505 {
3506 free (htab);
3507 return NULL;
3508 }
3509
3510 /* Init the stub hash table too. */
3511 if (!bfd_hash_table_init (&htab->stub_hash_table, stub_hash_newfunc,
3512 sizeof (struct ppc_stub_hash_entry)))
3513 {
3514 _bfd_elf_link_hash_table_free (abfd);
3515 return NULL;
3516 }
3517
3518 /* And the branch hash table. */
3519 if (!bfd_hash_table_init (&htab->branch_hash_table, branch_hash_newfunc,
3520 sizeof (struct ppc_branch_hash_entry)))
3521 {
3522 bfd_hash_table_free (&htab->stub_hash_table);
3523 _bfd_elf_link_hash_table_free (abfd);
3524 return NULL;
3525 }
3526
3527 htab->tocsave_htab = htab_try_create (1024,
3528 tocsave_htab_hash,
3529 tocsave_htab_eq,
3530 NULL);
3531 if (htab->tocsave_htab == NULL)
3532 {
3533 ppc64_elf_link_hash_table_free (abfd);
3534 return NULL;
3535 }
3536 htab->elf.root.hash_table_free = ppc64_elf_link_hash_table_free;
3537
3538 /* Initializing two fields of the union is just cosmetic. We really
3539 only care about glist, but when compiled on a 32-bit host the
3540 bfd_vma fields are larger. Setting the bfd_vma to zero makes
3541 debugger inspection of these fields look nicer. */
3542 htab->elf.init_got_refcount.refcount = 0;
3543 htab->elf.init_got_refcount.glist = NULL;
3544 htab->elf.init_plt_refcount.refcount = 0;
3545 htab->elf.init_plt_refcount.glist = NULL;
3546 htab->elf.init_got_offset.offset = 0;
3547 htab->elf.init_got_offset.glist = NULL;
3548 htab->elf.init_plt_offset.offset = 0;
3549 htab->elf.init_plt_offset.glist = NULL;
3550
3551 return &htab->elf.root;
3552 }
3553
3554 /* Create sections for linker generated code. */
3555
3556 static bool
3557 create_linkage_sections (bfd *dynobj, struct bfd_link_info *info)
3558 {
3559 struct ppc_link_hash_table *htab;
3560 flagword flags;
3561
3562 htab = ppc_hash_table (info);
3563
3564 flags = (SEC_ALLOC | SEC_LOAD | SEC_CODE | SEC_READONLY
3565 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3566 if (htab->params->save_restore_funcs)
3567 {
3568 /* Create .sfpr for code to save and restore fp regs. */
3569 htab->sfpr = bfd_make_section_anyway_with_flags (dynobj, ".sfpr",
3570 flags);
3571 if (htab->sfpr == NULL
3572 || !bfd_set_section_alignment (htab->sfpr, 2))
3573 return false;
3574 }
3575
3576 if (bfd_link_relocatable (info))
3577 return true;
3578
3579 /* Create .glink for lazy dynamic linking support. */
3580 htab->glink = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3581 flags);
3582 if (htab->glink == NULL
3583 || !bfd_set_section_alignment (htab->glink, 3))
3584 return false;
3585
3586 /* The part of .glink used by global entry stubs, separate so that
3587 it can be aligned appropriately without affecting htab->glink. */
3588 htab->global_entry = bfd_make_section_anyway_with_flags (dynobj, ".glink",
3589 flags);
3590 if (htab->global_entry == NULL
3591 || !bfd_set_section_alignment (htab->global_entry, 2))
3592 return false;
3593
3594 if (!info->no_ld_generated_unwind_info)
3595 {
3596 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY | SEC_HAS_CONTENTS
3597 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3598 htab->glink_eh_frame = bfd_make_section_anyway_with_flags (dynobj,
3599 ".eh_frame",
3600 flags);
3601 if (htab->glink_eh_frame == NULL
3602 || !bfd_set_section_alignment (htab->glink_eh_frame, 2))
3603 return false;
3604 }
3605
3606 flags = SEC_ALLOC | SEC_LINKER_CREATED;
3607 htab->elf.iplt = bfd_make_section_anyway_with_flags (dynobj, ".iplt", flags);
3608 if (htab->elf.iplt == NULL
3609 || !bfd_set_section_alignment (htab->elf.iplt, 3))
3610 return false;
3611
3612 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3613 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3614 htab->elf.irelplt
3615 = bfd_make_section_anyway_with_flags (dynobj, ".rela.iplt", flags);
3616 if (htab->elf.irelplt == NULL
3617 || !bfd_set_section_alignment (htab->elf.irelplt, 3))
3618 return false;
3619
3620 /* Create branch lookup table for plt_branch stubs. */
3621 flags = (SEC_ALLOC | SEC_LOAD
3622 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3623 htab->brlt = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3624 flags);
3625 if (htab->brlt == NULL
3626 || !bfd_set_section_alignment (htab->brlt, 3))
3627 return false;
3628
3629 /* Local plt entries, put in .branch_lt but a separate section for
3630 convenience. */
3631 htab->pltlocal = bfd_make_section_anyway_with_flags (dynobj, ".branch_lt",
3632 flags);
3633 if (htab->pltlocal == NULL
3634 || !bfd_set_section_alignment (htab->pltlocal, 3))
3635 return false;
3636
3637 if (!bfd_link_pic (info))
3638 return true;
3639
3640 flags = (SEC_ALLOC | SEC_LOAD | SEC_READONLY
3641 | SEC_HAS_CONTENTS | SEC_IN_MEMORY | SEC_LINKER_CREATED);
3642 htab->relbrlt
3643 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3644 if (htab->relbrlt == NULL
3645 || !bfd_set_section_alignment (htab->relbrlt, 3))
3646 return false;
3647
3648 htab->relpltlocal
3649 = bfd_make_section_anyway_with_flags (dynobj, ".rela.branch_lt", flags);
3650 if (htab->relpltlocal == NULL
3651 || !bfd_set_section_alignment (htab->relpltlocal, 3))
3652 return false;
3653
3654 return true;
3655 }
3656
3657 /* Satisfy the ELF linker by filling in some fields in our fake bfd. */
3658
3659 bool
3660 ppc64_elf_init_stub_bfd (struct bfd_link_info *info,
3661 struct ppc64_elf_params *params)
3662 {
3663 struct ppc_link_hash_table *htab;
3664
3665 elf_elfheader (params->stub_bfd)->e_ident[EI_CLASS] = ELFCLASS64;
3666
3667 /* Always hook our dynamic sections into the first bfd, which is the
3668 linker created stub bfd. This ensures that the GOT header is at
3669 the start of the output TOC section. */
3670 htab = ppc_hash_table (info);
3671 htab->elf.dynobj = params->stub_bfd;
3672 htab->params = params;
3673
3674 return create_linkage_sections (htab->elf.dynobj, info);
3675 }
3676
3677 /* Build a name for an entry in the stub hash table. */
3678
3679 static char *
3680 ppc_stub_name (const asection *input_section,
3681 const asection *sym_sec,
3682 const struct ppc_link_hash_entry *h,
3683 const Elf_Internal_Rela *rel)
3684 {
3685 char *stub_name;
3686 ssize_t len;
3687
3688 /* rel->r_addend is actually 64 bit, but who uses more than +/- 2^31
3689 offsets from a sym as a branch target? In fact, we could
3690 probably assume the addend is always zero. */
3691 BFD_ASSERT (((int) rel->r_addend & 0xffffffff) == rel->r_addend);
3692
3693 if (h)
3694 {
3695 len = 8 + 1 + strlen (h->elf.root.root.string) + 1 + 8 + 1;
3696 stub_name = bfd_malloc (len);
3697 if (stub_name == NULL)
3698 return stub_name;
3699
3700 len = sprintf (stub_name, "%08x.%s+%x",
3701 input_section->id & 0xffffffff,
3702 h->elf.root.root.string,
3703 (int) rel->r_addend & 0xffffffff);
3704 }
3705 else
3706 {
3707 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1;
3708 stub_name = bfd_malloc (len);
3709 if (stub_name == NULL)
3710 return stub_name;
3711
3712 len = sprintf (stub_name, "%08x.%x:%x+%x",
3713 input_section->id & 0xffffffff,
3714 sym_sec->id & 0xffffffff,
3715 (int) ELF64_R_SYM (rel->r_info) & 0xffffffff,
3716 (int) rel->r_addend & 0xffffffff);
3717 }
3718 if (len > 2 && stub_name[len - 2] == '+' && stub_name[len - 1] == '0')
3719 stub_name[len - 2] = 0;
3720 return stub_name;
3721 }
3722
3723 /* If mixing power10 with non-power10 code and --power10-stubs is not
3724 specified (or is auto) then there may be multiple stub types for any
3725 given symbol. Up to three classes of stubs are stored in separate
3726 stub_hash_table entries having the same key string. The entries
3727 will always be adjacent on entry->root.next chain, even if hash
3728 table resizing occurs. This function selects the correct entry to
3729 use. */
3730
3731 static struct ppc_stub_hash_entry *
3732 select_alt_stub (struct ppc_stub_hash_entry *entry,
3733 enum elf_ppc64_reloc_type r_type)
3734 {
3735 enum ppc_stub_sub_type subt;
3736
3737 switch (r_type)
3738 {
3739 case R_PPC64_REL24_NOTOC:
3740 subt = ppc_stub_notoc;
3741 break;
3742 case R_PPC64_REL24_P9NOTOC:
3743 subt = ppc_stub_p9notoc;
3744 break;
3745 default:
3746 subt = ppc_stub_toc;
3747 break;
3748 }
3749
3750 while (entry != NULL && entry->type.sub != subt)
3751 {
3752 const char *stub_name = entry->root.string;
3753
3754 entry = (struct ppc_stub_hash_entry *) entry->root.next;
3755 if (entry != NULL
3756 && entry->root.string != stub_name)
3757 entry = NULL;
3758 }
3759
3760 return entry;
3761 }
3762
3763 /* Look up an entry in the stub hash. Stub entries are cached because
3764 creating the stub name takes a bit of time. */
3765
3766 static struct ppc_stub_hash_entry *
3767 ppc_get_stub_entry (const asection *input_section,
3768 const asection *sym_sec,
3769 struct ppc_link_hash_entry *h,
3770 const Elf_Internal_Rela *rel,
3771 struct ppc_link_hash_table *htab)
3772 {
3773 struct ppc_stub_hash_entry *stub_entry;
3774 struct map_stub *group;
3775
3776 /* If this input section is part of a group of sections sharing one
3777 stub section, then use the id of the first section in the group.
3778 Stub names need to include a section id, as there may well be
3779 more than one stub used to reach say, printf, and we need to
3780 distinguish between them. */
3781 group = htab->sec_info[input_section->id].u.group;
3782 if (group == NULL)
3783 return NULL;
3784
3785 if (h != NULL && h->u.stub_cache != NULL
3786 && h->u.stub_cache->h == h
3787 && h->u.stub_cache->group == group)
3788 {
3789 stub_entry = h->u.stub_cache;
3790 }
3791 else
3792 {
3793 char *stub_name;
3794
3795 stub_name = ppc_stub_name (group->link_sec, sym_sec, h, rel);
3796 if (stub_name == NULL)
3797 return NULL;
3798
3799 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
3800 stub_name, false, false);
3801 if (h != NULL)
3802 h->u.stub_cache = stub_entry;
3803
3804 free (stub_name);
3805 }
3806
3807 if (stub_entry != NULL && htab->params->power10_stubs == -1)
3808 stub_entry = select_alt_stub (stub_entry, ELF64_R_TYPE (rel->r_info));
3809
3810 return stub_entry;
3811 }
3812
3813 /* Add a new stub entry to the stub hash. Not all fields of the new
3814 stub entry are initialised. */
3815
3816 static struct ppc_stub_hash_entry *
3817 ppc_add_stub (const char *stub_name,
3818 asection *section,
3819 struct bfd_link_info *info)
3820 {
3821 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3822 struct map_stub *group;
3823 asection *link_sec;
3824 asection *stub_sec;
3825 struct ppc_stub_hash_entry *stub_entry;
3826
3827 group = htab->sec_info[section->id].u.group;
3828 link_sec = group->link_sec;
3829 stub_sec = group->stub_sec;
3830 if (stub_sec == NULL)
3831 {
3832 size_t namelen;
3833 bfd_size_type len;
3834 char *s_name;
3835
3836 namelen = strlen (link_sec->name);
3837 len = namelen + sizeof (STUB_SUFFIX);
3838 s_name = bfd_alloc (htab->params->stub_bfd, len);
3839 if (s_name == NULL)
3840 return NULL;
3841
3842 memcpy (s_name, link_sec->name, namelen);
3843 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
3844 stub_sec = (*htab->params->add_stub_section) (s_name, link_sec);
3845 if (stub_sec == NULL)
3846 return NULL;
3847 group->stub_sec = stub_sec;
3848 }
3849
3850 /* Enter this entry into the linker stub hash table. */
3851 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table, stub_name,
3852 true, false);
3853 if (stub_entry == NULL)
3854 {
3855 /* xgettext:c-format */
3856 _bfd_error_handler (_("%pB: cannot create stub entry %s"),
3857 section->owner, stub_name);
3858 return NULL;
3859 }
3860
3861 stub_entry->group = group;
3862 stub_entry->stub_offset = 0;
3863 return stub_entry;
3864 }
3865
3866 /* A stub has already been created, but it may not be the required
3867 type. We shouldn't be transitioning from plt_call to long_branch
3868 stubs or vice versa, but we might be upgrading from plt_call to
3869 plt_call with r2save for example. */
3870
3871 static bool
3872 ppc_merge_stub (struct ppc_link_hash_table *htab,
3873 struct ppc_stub_hash_entry *stub_entry,
3874 struct ppc_stub_type stub_type,
3875 enum elf_ppc64_reloc_type r_type)
3876 {
3877 struct ppc_stub_type old_type = stub_entry->type;
3878
3879 if (old_type.main == ppc_stub_save_res)
3880 return true;
3881
3882 if (htab->params->power10_stubs == -1)
3883 {
3884 /* For --power10-stubs=auto, don't merge _notoc and other
3885 varieties of stubs. */
3886 struct ppc_stub_hash_entry *alt_stub;
3887
3888 alt_stub = select_alt_stub (stub_entry, r_type);
3889 if (alt_stub == NULL)
3890 {
3891 alt_stub = ((struct ppc_stub_hash_entry *)
3892 stub_hash_newfunc (NULL,
3893 &htab->stub_hash_table,
3894 stub_entry->root.string));
3895 if (alt_stub == NULL)
3896 return false;
3897
3898 *alt_stub = *stub_entry;
3899 stub_entry->root.next = &alt_stub->root;
3900
3901 /* Sort notoc stubs first, then toc stubs, then p9notoc.
3902 Not that it matters, this just puts smaller stubs first. */
3903 if (stub_type.sub == ppc_stub_notoc)
3904 alt_stub = stub_entry;
3905 else if (stub_type.sub == ppc_stub_p9notoc
3906 && alt_stub->root.next
3907 && alt_stub->root.next->string == alt_stub->root.string)
3908 {
3909 struct ppc_stub_hash_entry *next
3910 = (struct ppc_stub_hash_entry *) alt_stub->root.next;
3911 alt_stub->type = next->type;
3912 alt_stub = next;
3913 }
3914 alt_stub->type = stub_type;
3915 return true;
3916 }
3917 stub_entry = alt_stub;
3918 }
3919
3920 old_type = stub_entry->type;
3921 if (old_type.main == ppc_stub_plt_branch)
3922 old_type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
3923
3924 if (old_type.main != stub_type.main
3925 || (old_type.sub != stub_type.sub
3926 && old_type.sub != ppc_stub_toc
3927 && stub_type.sub != ppc_stub_toc))
3928 abort ();
3929
3930 stub_entry->type.sub |= stub_type.sub;
3931 stub_entry->type.r2save |= stub_type.r2save;
3932 return true;
3933 }
3934
3935 /* Create .got and .rela.got sections in ABFD, and .got in dynobj if
3936 not already done. */
3937
3938 static bool
3939 create_got_section (bfd *abfd, struct bfd_link_info *info)
3940 {
3941 asection *got, *relgot;
3942 flagword flags;
3943 struct ppc_link_hash_table *htab = ppc_hash_table (info);
3944
3945 if (!is_ppc64_elf (abfd))
3946 return false;
3947 if (htab == NULL)
3948 return false;
3949
3950 if (!htab->elf.sgot
3951 && !_bfd_elf_create_got_section (htab->elf.dynobj, info))
3952 return false;
3953
3954 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3955 | SEC_LINKER_CREATED);
3956
3957 got = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3958 if (!got
3959 || !bfd_set_section_alignment (got, 3))
3960 return false;
3961
3962 relgot = bfd_make_section_anyway_with_flags (abfd, ".rela.got",
3963 flags | SEC_READONLY);
3964 if (!relgot
3965 || !bfd_set_section_alignment (relgot, 3))
3966 return false;
3967
3968 ppc64_elf_tdata (abfd)->got = got;
3969 ppc64_elf_tdata (abfd)->relgot = relgot;
3970 return true;
3971 }
3972
3973 /* Follow indirect and warning symbol links. */
3974
3975 static inline struct bfd_link_hash_entry *
3976 follow_link (struct bfd_link_hash_entry *h)
3977 {
3978 while (h->type == bfd_link_hash_indirect
3979 || h->type == bfd_link_hash_warning)
3980 h = h->u.i.link;
3981 return h;
3982 }
3983
3984 static inline struct elf_link_hash_entry *
3985 elf_follow_link (struct elf_link_hash_entry *h)
3986 {
3987 return (struct elf_link_hash_entry *) follow_link (&h->root);
3988 }
3989
3990 static inline struct ppc_link_hash_entry *
3991 ppc_follow_link (struct ppc_link_hash_entry *h)
3992 {
3993 return ppc_elf_hash_entry (elf_follow_link (&h->elf));
3994 }
3995
3996 /* Merge PLT info on FROM with that on TO. */
3997
3998 static void
3999 move_plt_plist (struct ppc_link_hash_entry *from,
4000 struct ppc_link_hash_entry *to)
4001 {
4002 if (from->elf.plt.plist != NULL)
4003 {
4004 if (to->elf.plt.plist != NULL)
4005 {
4006 struct plt_entry **entp;
4007 struct plt_entry *ent;
4008
4009 for (entp = &from->elf.plt.plist; (ent = *entp) != NULL; )
4010 {
4011 struct plt_entry *dent;
4012
4013 for (dent = to->elf.plt.plist; dent != NULL; dent = dent->next)
4014 if (dent->addend == ent->addend)
4015 {
4016 dent->plt.refcount += ent->plt.refcount;
4017 *entp = ent->next;
4018 break;
4019 }
4020 if (dent == NULL)
4021 entp = &ent->next;
4022 }
4023 *entp = to->elf.plt.plist;
4024 }
4025
4026 to->elf.plt.plist = from->elf.plt.plist;
4027 from->elf.plt.plist = NULL;
4028 }
4029 }
4030
4031 /* Copy the extra info we tack onto an elf_link_hash_entry. */
4032
4033 static void
4034 ppc64_elf_copy_indirect_symbol (struct bfd_link_info *info,
4035 struct elf_link_hash_entry *dir,
4036 struct elf_link_hash_entry *ind)
4037 {
4038 struct ppc_link_hash_entry *edir, *eind;
4039
4040 edir = ppc_elf_hash_entry (dir);
4041 eind = ppc_elf_hash_entry (ind);
4042
4043 edir->is_func |= eind->is_func;
4044 edir->is_func_descriptor |= eind->is_func_descriptor;
4045 edir->tls_mask |= eind->tls_mask;
4046 if (eind->oh != NULL)
4047 edir->oh = ppc_follow_link (eind->oh);
4048
4049 if (edir->elf.versioned != versioned_hidden)
4050 edir->elf.ref_dynamic |= eind->elf.ref_dynamic;
4051 edir->elf.ref_regular |= eind->elf.ref_regular;
4052 edir->elf.ref_regular_nonweak |= eind->elf.ref_regular_nonweak;
4053 edir->elf.non_got_ref |= eind->elf.non_got_ref;
4054 edir->elf.needs_plt |= eind->elf.needs_plt;
4055 edir->elf.pointer_equality_needed |= eind->elf.pointer_equality_needed;
4056
4057 /* If we were called to copy over info for a weak sym, don't copy
4058 dyn_relocs, plt/got info, or dynindx. We used to copy dyn_relocs
4059 in order to simplify readonly_dynrelocs and save a field in the
4060 symbol hash entry, but that means dyn_relocs can't be used in any
4061 tests about a specific symbol, or affect other symbol flags which
4062 are then tested. */
4063 if (eind->elf.root.type != bfd_link_hash_indirect)
4064 return;
4065
4066 /* Copy over any dynamic relocs we may have on the indirect sym. */
4067 if (ind->dyn_relocs != NULL)
4068 {
4069 if (dir->dyn_relocs != NULL)
4070 {
4071 struct elf_dyn_relocs **pp;
4072 struct elf_dyn_relocs *p;
4073
4074 /* Add reloc counts against the indirect sym to the direct sym
4075 list. Merge any entries against the same section. */
4076 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; )
4077 {
4078 struct elf_dyn_relocs *q;
4079
4080 for (q = dir->dyn_relocs; q != NULL; q = q->next)
4081 if (q->sec == p->sec)
4082 {
4083 q->pc_count += p->pc_count;
4084 q->count += p->count;
4085 *pp = p->next;
4086 break;
4087 }
4088 if (q == NULL)
4089 pp = &p->next;
4090 }
4091 *pp = dir->dyn_relocs;
4092 }
4093
4094 dir->dyn_relocs = ind->dyn_relocs;
4095 ind->dyn_relocs = NULL;
4096 }
4097
4098 /* Copy over got entries that we may have already seen to the
4099 symbol which just became indirect. */
4100 if (eind->elf.got.glist != NULL)
4101 {
4102 if (edir->elf.got.glist != NULL)
4103 {
4104 struct got_entry **entp;
4105 struct got_entry *ent;
4106
4107 for (entp = &eind->elf.got.glist; (ent = *entp) != NULL; )
4108 {
4109 struct got_entry *dent;
4110
4111 for (dent = edir->elf.got.glist; dent != NULL; dent = dent->next)
4112 if (dent->addend == ent->addend
4113 && dent->owner == ent->owner
4114 && dent->tls_type == ent->tls_type)
4115 {
4116 dent->got.refcount += ent->got.refcount;
4117 *entp = ent->next;
4118 break;
4119 }
4120 if (dent == NULL)
4121 entp = &ent->next;
4122 }
4123 *entp = edir->elf.got.glist;
4124 }
4125
4126 edir->elf.got.glist = eind->elf.got.glist;
4127 eind->elf.got.glist = NULL;
4128 }
4129
4130 /* And plt entries. */
4131 move_plt_plist (eind, edir);
4132
4133 if (eind->elf.dynindx != -1)
4134 {
4135 if (edir->elf.dynindx != -1)
4136 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
4137 edir->elf.dynstr_index);
4138 edir->elf.dynindx = eind->elf.dynindx;
4139 edir->elf.dynstr_index = eind->elf.dynstr_index;
4140 eind->elf.dynindx = -1;
4141 eind->elf.dynstr_index = 0;
4142 }
4143 }
4144
4145 /* Find the function descriptor hash entry from the given function code
4146 hash entry FH. Link the entries via their OH fields. */
4147
4148 static struct ppc_link_hash_entry *
4149 lookup_fdh (struct ppc_link_hash_entry *fh, struct ppc_link_hash_table *htab)
4150 {
4151 struct ppc_link_hash_entry *fdh = fh->oh;
4152
4153 if (fdh == NULL)
4154 {
4155 const char *fd_name = fh->elf.root.root.string + 1;
4156
4157 fdh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, fd_name,
4158 false, false, false));
4159 if (fdh == NULL)
4160 return fdh;
4161
4162 fdh->is_func_descriptor = 1;
4163 fdh->oh = fh;
4164 fh->is_func = 1;
4165 fh->oh = fdh;
4166 }
4167
4168 fdh = ppc_follow_link (fdh);
4169 fdh->is_func_descriptor = 1;
4170 fdh->oh = fh;
4171 return fdh;
4172 }
4173
4174 /* Make a fake function descriptor sym for the undefined code sym FH. */
4175
4176 static struct ppc_link_hash_entry *
4177 make_fdh (struct bfd_link_info *info,
4178 struct ppc_link_hash_entry *fh)
4179 {
4180 bfd *abfd = fh->elf.root.u.undef.abfd;
4181 struct bfd_link_hash_entry *bh = NULL;
4182 struct ppc_link_hash_entry *fdh;
4183 flagword flags = (fh->elf.root.type == bfd_link_hash_undefweak
4184 ? BSF_WEAK
4185 : BSF_GLOBAL);
4186
4187 if (!_bfd_generic_link_add_one_symbol (info, abfd,
4188 fh->elf.root.root.string + 1,
4189 flags, bfd_und_section_ptr, 0,
4190 NULL, false, false, &bh))
4191 return NULL;
4192
4193 fdh = (struct ppc_link_hash_entry *) bh;
4194 fdh->elf.non_elf = 0;
4195 fdh->fake = 1;
4196 fdh->is_func_descriptor = 1;
4197 fdh->oh = fh;
4198 fh->is_func = 1;
4199 fh->oh = fdh;
4200 return fdh;
4201 }
4202
4203 /* Fix function descriptor symbols defined in .opd sections to be
4204 function type. */
4205
4206 static bool
4207 ppc64_elf_add_symbol_hook (bfd *ibfd,
4208 struct bfd_link_info *info,
4209 Elf_Internal_Sym *isym,
4210 const char **name,
4211 flagword *flags ATTRIBUTE_UNUSED,
4212 asection **sec,
4213 bfd_vma *value)
4214 {
4215 if (*sec != NULL
4216 && strcmp ((*sec)->name, ".opd") == 0)
4217 {
4218 asection *code_sec;
4219
4220 if (!(ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC
4221 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))
4222 isym->st_info = ELF_ST_INFO (ELF_ST_BIND (isym->st_info), STT_FUNC);
4223
4224 /* If the symbol is a function defined in .opd, and the function
4225 code is in a discarded group, let it appear to be undefined. */
4226 if (!bfd_link_relocatable (info)
4227 && (*sec)->reloc_count != 0
4228 && opd_entry_value (*sec, *value, &code_sec, NULL,
4229 false) != (bfd_vma) -1
4230 && discarded_section (code_sec))
4231 {
4232 *sec = bfd_und_section_ptr;
4233 isym->st_shndx = SHN_UNDEF;
4234 }
4235 }
4236 else if (*sec != NULL
4237 && strcmp ((*sec)->name, ".toc") == 0
4238 && ELF_ST_TYPE (isym->st_info) == STT_OBJECT)
4239 {
4240 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4241 if (htab != NULL)
4242 htab->params->object_in_toc = 1;
4243 }
4244
4245 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4246 {
4247 if (abiversion (ibfd) == 0)
4248 set_abiversion (ibfd, 2);
4249 else if (abiversion (ibfd) == 1)
4250 {
4251 _bfd_error_handler (_("symbol '%s' has invalid st_other"
4252 " for ABI version 1"), *name);
4253 bfd_set_error (bfd_error_bad_value);
4254 return false;
4255 }
4256 }
4257
4258 return true;
4259 }
4260
4261 /* Merge non-visibility st_other attributes: local entry point. */
4262
4263 static void
4264 ppc64_elf_merge_symbol_attribute (struct elf_link_hash_entry *h,
4265 unsigned int st_other,
4266 bool definition,
4267 bool dynamic)
4268 {
4269 if (definition && (!dynamic || !h->def_regular))
4270 h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
4271 | ELF_ST_VISIBILITY (h->other));
4272 }
4273
4274 /* Hook called on merging a symbol. We use this to clear "fake" since
4275 we now have a real symbol. */
4276
4277 static bool
4278 ppc64_elf_merge_symbol (struct elf_link_hash_entry *h,
4279 const Elf_Internal_Sym *isym,
4280 asection **psec ATTRIBUTE_UNUSED,
4281 bool newdef ATTRIBUTE_UNUSED,
4282 bool olddef ATTRIBUTE_UNUSED,
4283 bfd *oldbfd ATTRIBUTE_UNUSED,
4284 const asection *oldsec ATTRIBUTE_UNUSED)
4285 {
4286 ppc_elf_hash_entry (h)->fake = 0;
4287 if ((STO_PPC64_LOCAL_MASK & isym->st_other) != 0)
4288 ppc_elf_hash_entry (h)->non_zero_localentry = 1;
4289 return true;
4290 }
4291
4292 /* This function makes an old ABI object reference to ".bar" cause the
4293 inclusion of a new ABI object archive that defines "bar".
4294 NAME is a symbol defined in an archive. Return a symbol in the hash
4295 table that might be satisfied by the archive symbols. */
4296
4297 static struct bfd_link_hash_entry *
4298 ppc64_elf_archive_symbol_lookup (bfd *abfd,
4299 struct bfd_link_info *info,
4300 const char *name)
4301 {
4302 struct bfd_link_hash_entry *h;
4303 char *dot_name;
4304 size_t len;
4305
4306 h = _bfd_elf_archive_symbol_lookup (abfd, info, name);
4307 if (h != NULL
4308 && ppc_hash_table (info) != NULL
4309 /* Don't return this sym if it is a fake function descriptor
4310 created by add_symbol_adjust. */
4311 && !((struct ppc_link_hash_entry *) h)->fake)
4312 return h;
4313
4314 if (name[0] == '.')
4315 return h;
4316
4317 len = strlen (name);
4318 dot_name = bfd_alloc (abfd, len + 2);
4319 if (dot_name == NULL)
4320 return (struct bfd_link_hash_entry *) -1;
4321 dot_name[0] = '.';
4322 memcpy (dot_name + 1, name, len + 1);
4323 h = _bfd_elf_archive_symbol_lookup (abfd, info, dot_name);
4324 bfd_release (abfd, dot_name);
4325 if (h != NULL)
4326 return h;
4327
4328 if (strcmp (name, "__tls_get_addr_opt") == 0)
4329 h = _bfd_elf_archive_symbol_lookup (abfd, info, "__tls_get_addr_desc");
4330 return h;
4331 }
4332
4333 /* This function satisfies all old ABI object references to ".bar" if a
4334 new ABI object defines "bar". Well, at least, undefined dot symbols
4335 are made weak. This stops later archive searches from including an
4336 object if we already have a function descriptor definition. It also
4337 prevents the linker complaining about undefined symbols.
4338 We also check and correct mismatched symbol visibility here. The
4339 most restrictive visibility of the function descriptor and the
4340 function entry symbol is used. */
4341
4342 static bool
4343 add_symbol_adjust (struct ppc_link_hash_entry *eh, struct bfd_link_info *info)
4344 {
4345 struct ppc_link_hash_table *htab;
4346 struct ppc_link_hash_entry *fdh;
4347
4348 if (eh->elf.root.type == bfd_link_hash_warning)
4349 eh = (struct ppc_link_hash_entry *) eh->elf.root.u.i.link;
4350
4351 if (eh->elf.root.type == bfd_link_hash_indirect)
4352 return true;
4353
4354 if (eh->elf.root.root.string[0] != '.')
4355 abort ();
4356
4357 htab = ppc_hash_table (info);
4358 if (htab == NULL)
4359 return false;
4360
4361 fdh = lookup_fdh (eh, htab);
4362 if (fdh == NULL
4363 && !bfd_link_relocatable (info)
4364 && (eh->elf.root.type == bfd_link_hash_undefined
4365 || eh->elf.root.type == bfd_link_hash_undefweak)
4366 && eh->elf.ref_regular)
4367 {
4368 /* Make an undefined function descriptor sym, in order to
4369 pull in an --as-needed shared lib. Archives are handled
4370 elsewhere. */
4371 fdh = make_fdh (info, eh);
4372 if (fdh == NULL)
4373 return false;
4374 }
4375
4376 if (fdh != NULL)
4377 {
4378 unsigned entry_vis = ELF_ST_VISIBILITY (eh->elf.other) - 1;
4379 unsigned descr_vis = ELF_ST_VISIBILITY (fdh->elf.other) - 1;
4380
4381 /* Make both descriptor and entry symbol have the most
4382 constraining visibility of either symbol. */
4383 if (entry_vis < descr_vis)
4384 fdh->elf.other += entry_vis - descr_vis;
4385 else if (entry_vis > descr_vis)
4386 eh->elf.other += descr_vis - entry_vis;
4387
4388 /* Propagate reference flags from entry symbol to function
4389 descriptor symbol. */
4390 fdh->elf.root.non_ir_ref_regular |= eh->elf.root.non_ir_ref_regular;
4391 fdh->elf.root.non_ir_ref_dynamic |= eh->elf.root.non_ir_ref_dynamic;
4392 fdh->elf.ref_regular |= eh->elf.ref_regular;
4393 fdh->elf.ref_regular_nonweak |= eh->elf.ref_regular_nonweak;
4394
4395 if (!fdh->elf.forced_local
4396 && fdh->elf.dynindx == -1
4397 && fdh->elf.versioned != versioned_hidden
4398 && (bfd_link_dll (info)
4399 || fdh->elf.def_dynamic
4400 || fdh->elf.ref_dynamic)
4401 && (eh->elf.ref_regular
4402 || eh->elf.def_regular))
4403 {
4404 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
4405 return false;
4406 }
4407 }
4408
4409 return true;
4410 }
4411
4412 /* Set up opd section info and abiversion for IBFD, and process list
4413 of dot-symbols we made in link_hash_newfunc. */
4414
4415 static bool
4416 ppc64_elf_before_check_relocs (bfd *ibfd, struct bfd_link_info *info)
4417 {
4418 struct ppc_link_hash_table *htab;
4419 struct ppc_link_hash_entry **p, *eh;
4420 asection *opd = bfd_get_section_by_name (ibfd, ".opd");
4421
4422 if (opd != NULL && opd->size != 0)
4423 {
4424 BFD_ASSERT (ppc64_elf_section_data (opd)->sec_type == sec_normal);
4425 ppc64_elf_section_data (opd)->sec_type = sec_opd;
4426
4427 if (abiversion (ibfd) == 0)
4428 set_abiversion (ibfd, 1);
4429 else if (abiversion (ibfd) >= 2)
4430 {
4431 /* xgettext:c-format */
4432 _bfd_error_handler (_("%pB .opd not allowed in ABI version %d"),
4433 ibfd, abiversion (ibfd));
4434 bfd_set_error (bfd_error_bad_value);
4435 return false;
4436 }
4437 }
4438
4439 if (is_ppc64_elf (info->output_bfd))
4440 {
4441 /* For input files without an explicit abiversion in e_flags
4442 we should have flagged any with symbol st_other bits set
4443 as ELFv1 and above flagged those with .opd as ELFv2.
4444 Set the output abiversion if not yet set, and for any input
4445 still ambiguous, take its abiversion from the output.
4446 Differences in ABI are reported later. */
4447 if (abiversion (info->output_bfd) == 0)
4448 set_abiversion (info->output_bfd, abiversion (ibfd));
4449 else if (abiversion (ibfd) == 0)
4450 set_abiversion (ibfd, abiversion (info->output_bfd));
4451 }
4452
4453 htab = ppc_hash_table (info);
4454 if (htab == NULL)
4455 return true;
4456
4457 if (opd != NULL && opd->size != 0
4458 && (ibfd->flags & DYNAMIC) == 0
4459 && (opd->flags & SEC_RELOC) != 0
4460 && opd->reloc_count != 0
4461 && !bfd_is_abs_section (opd->output_section)
4462 && info->gc_sections)
4463 {
4464 /* Garbage collection needs some extra help with .opd sections.
4465 We don't want to necessarily keep everything referenced by
4466 relocs in .opd, as that would keep all functions. Instead,
4467 if we reference an .opd symbol (a function descriptor), we
4468 want to keep the function code symbol's section. This is
4469 easy for global symbols, but for local syms we need to keep
4470 information about the associated function section. */
4471 bfd_size_type amt;
4472 asection **opd_sym_map;
4473 Elf_Internal_Shdr *symtab_hdr;
4474 Elf_Internal_Rela *relocs, *rel_end, *rel;
4475
4476 amt = OPD_NDX (opd->size) * sizeof (*opd_sym_map);
4477 opd_sym_map = bfd_zalloc (ibfd, amt);
4478 if (opd_sym_map == NULL)
4479 return false;
4480 ppc64_elf_section_data (opd)->u.opd.func_sec = opd_sym_map;
4481 relocs = _bfd_elf_link_read_relocs (ibfd, opd, NULL, NULL,
4482 info->keep_memory);
4483 if (relocs == NULL)
4484 return false;
4485 symtab_hdr = &elf_symtab_hdr (ibfd);
4486 rel_end = relocs + opd->reloc_count - 1;
4487 for (rel = relocs; rel < rel_end; rel++)
4488 {
4489 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
4490 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
4491
4492 if (r_type == R_PPC64_ADDR64
4493 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC
4494 && r_symndx < symtab_hdr->sh_info)
4495 {
4496 Elf_Internal_Sym *isym;
4497 asection *s;
4498
4499 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache, ibfd,
4500 r_symndx);
4501 if (isym == NULL)
4502 {
4503 if (elf_section_data (opd)->relocs != relocs)
4504 free (relocs);
4505 return false;
4506 }
4507
4508 s = bfd_section_from_elf_index (ibfd, isym->st_shndx);
4509 if (s != NULL && s != opd)
4510 opd_sym_map[OPD_NDX (rel->r_offset)] = s;
4511 }
4512 }
4513 if (elf_section_data (opd)->relocs != relocs)
4514 free (relocs);
4515 }
4516
4517 p = &htab->dot_syms;
4518 while ((eh = *p) != NULL)
4519 {
4520 *p = NULL;
4521 if (&eh->elf == htab->elf.hgot)
4522 ;
4523 else if (htab->elf.hgot == NULL
4524 && strcmp (eh->elf.root.root.string, ".TOC.") == 0)
4525 htab->elf.hgot = &eh->elf;
4526 else if (abiversion (ibfd) <= 1)
4527 {
4528 htab->need_func_desc_adj = 1;
4529 if (!add_symbol_adjust (eh, info))
4530 return false;
4531 }
4532 p = &eh->u.next_dot_sym;
4533 }
4534 return true;
4535 }
4536
4537 /* Undo hash table changes when an --as-needed input file is determined
4538 not to be needed. */
4539
4540 static bool
4541 ppc64_elf_notice_as_needed (bfd *ibfd,
4542 struct bfd_link_info *info,
4543 enum notice_asneeded_action act)
4544 {
4545 if (act == notice_not_needed)
4546 {
4547 struct ppc_link_hash_table *htab = ppc_hash_table (info);
4548
4549 if (htab == NULL)
4550 return false;
4551
4552 htab->dot_syms = NULL;
4553 }
4554 return _bfd_elf_notice_as_needed (ibfd, info, act);
4555 }
4556
4557 /* If --just-symbols against a final linked binary, then assume we need
4558 toc adjusting stubs when calling functions defined there. */
4559
4560 static void
4561 ppc64_elf_link_just_syms (asection *sec, struct bfd_link_info *info)
4562 {
4563 if ((sec->flags & SEC_CODE) != 0
4564 && (sec->owner->flags & (EXEC_P | DYNAMIC)) != 0
4565 && is_ppc64_elf (sec->owner))
4566 {
4567 if (abiversion (sec->owner) >= 2
4568 || bfd_get_section_by_name (sec->owner, ".opd") != NULL)
4569 sec->has_toc_reloc = 1;
4570 }
4571 _bfd_elf_link_just_syms (sec, info);
4572 }
4573
4574 static struct plt_entry **
4575 update_local_sym_info (bfd *abfd, Elf_Internal_Shdr *symtab_hdr,
4576 unsigned long r_symndx, bfd_vma r_addend, int tls_type)
4577 {
4578 struct got_entry **local_got_ents = elf_local_got_ents (abfd);
4579 struct plt_entry **local_plt;
4580 unsigned char *local_got_tls_masks;
4581
4582 if (local_got_ents == NULL)
4583 {
4584 bfd_size_type size = symtab_hdr->sh_info;
4585
4586 size *= (sizeof (*local_got_ents)
4587 + sizeof (*local_plt)
4588 + sizeof (*local_got_tls_masks));
4589 local_got_ents = bfd_zalloc (abfd, size);
4590 if (local_got_ents == NULL)
4591 return NULL;
4592 elf_local_got_ents (abfd) = local_got_ents;
4593 }
4594
4595 if ((tls_type & (NON_GOT | TLS_EXPLICIT)) == 0)
4596 {
4597 struct got_entry *ent;
4598
4599 for (ent = local_got_ents[r_symndx]; ent != NULL; ent = ent->next)
4600 if (ent->addend == r_addend
4601 && ent->owner == abfd
4602 && ent->tls_type == tls_type)
4603 break;
4604 if (ent == NULL)
4605 {
4606 size_t amt = sizeof (*ent);
4607 ent = bfd_alloc (abfd, amt);
4608 if (ent == NULL)
4609 return false;
4610 ent->next = local_got_ents[r_symndx];
4611 ent->addend = r_addend;
4612 ent->owner = abfd;
4613 ent->tls_type = tls_type;
4614 ent->is_indirect = false;
4615 ent->got.refcount = 0;
4616 local_got_ents[r_symndx] = ent;
4617 }
4618 ent->got.refcount += 1;
4619 }
4620
4621 local_plt = (struct plt_entry **) (local_got_ents + symtab_hdr->sh_info);
4622 local_got_tls_masks = (unsigned char *) (local_plt + symtab_hdr->sh_info);
4623 local_got_tls_masks[r_symndx] |= tls_type & 0xff;
4624
4625 return local_plt + r_symndx;
4626 }
4627
4628 static bool
4629 update_plt_info (bfd *abfd, struct plt_entry **plist, bfd_vma addend)
4630 {
4631 struct plt_entry *ent;
4632
4633 for (ent = *plist; ent != NULL; ent = ent->next)
4634 if (ent->addend == addend)
4635 break;
4636 if (ent == NULL)
4637 {
4638 size_t amt = sizeof (*ent);
4639 ent = bfd_alloc (abfd, amt);
4640 if (ent == NULL)
4641 return false;
4642 ent->next = *plist;
4643 ent->addend = addend;
4644 ent->plt.refcount = 0;
4645 *plist = ent;
4646 }
4647 ent->plt.refcount += 1;
4648 return true;
4649 }
4650
4651 static bool
4652 is_branch_reloc (enum elf_ppc64_reloc_type r_type)
4653 {
4654 return (r_type == R_PPC64_REL24
4655 || r_type == R_PPC64_REL24_NOTOC
4656 || r_type == R_PPC64_REL24_P9NOTOC
4657 || r_type == R_PPC64_REL14
4658 || r_type == R_PPC64_REL14_BRTAKEN
4659 || r_type == R_PPC64_REL14_BRNTAKEN
4660 || r_type == R_PPC64_ADDR24
4661 || r_type == R_PPC64_ADDR14
4662 || r_type == R_PPC64_ADDR14_BRTAKEN
4663 || r_type == R_PPC64_ADDR14_BRNTAKEN
4664 || r_type == R_PPC64_PLTCALL
4665 || r_type == R_PPC64_PLTCALL_NOTOC);
4666 }
4667
4668 /* Relocs on inline plt call sequence insns prior to the call. */
4669
4670 static bool
4671 is_plt_seq_reloc (enum elf_ppc64_reloc_type r_type)
4672 {
4673 return (r_type == R_PPC64_PLT16_HA
4674 || r_type == R_PPC64_PLT16_HI
4675 || r_type == R_PPC64_PLT16_LO
4676 || r_type == R_PPC64_PLT16_LO_DS
4677 || r_type == R_PPC64_PLT_PCREL34
4678 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4679 || r_type == R_PPC64_PLTSEQ
4680 || r_type == R_PPC64_PLTSEQ_NOTOC);
4681 }
4682
4683 /* Of relocs which might appear paired with TLSGD and TLSLD marker
4684 relocs, return true for those that operate on a dword. */
4685
4686 static bool
4687 is_8byte_reloc (enum elf_ppc64_reloc_type r_type)
4688 {
4689 return (r_type == R_PPC64_PLT_PCREL34
4690 || r_type == R_PPC64_PLT_PCREL34_NOTOC
4691 || r_type == R_PPC64_PLTCALL);
4692 }
4693
4694 /* Like bfd_reloc_offset_in_range but without a howto. Return true
4695 iff a field of SIZE bytes at OFFSET is within SEC limits. */
4696
4697 static bool
4698 offset_in_range (asection *sec, bfd_vma offset, size_t size)
4699 {
4700 return offset <= sec->size && size <= sec->size - offset;
4701 }
4702
4703 /* Look through the relocs for a section during the first phase, and
4704 calculate needed space in the global offset table, procedure
4705 linkage table, and dynamic reloc sections. */
4706
4707 static bool
4708 ppc64_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
4709 asection *sec, const Elf_Internal_Rela *relocs)
4710 {
4711 struct ppc_link_hash_table *htab;
4712 Elf_Internal_Shdr *symtab_hdr;
4713 struct elf_link_hash_entry **sym_hashes;
4714 const Elf_Internal_Rela *rel;
4715 const Elf_Internal_Rela *rel_end;
4716 asection *sreloc;
4717 struct elf_link_hash_entry *tga, *dottga;
4718 bool is_opd;
4719
4720 if (bfd_link_relocatable (info))
4721 return true;
4722
4723 BFD_ASSERT (is_ppc64_elf (abfd));
4724
4725 htab = ppc_hash_table (info);
4726 if (htab == NULL)
4727 return false;
4728
4729 tga = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
4730 false, false, true);
4731 dottga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
4732 false, false, true);
4733 symtab_hdr = &elf_symtab_hdr (abfd);
4734 sym_hashes = elf_sym_hashes (abfd);
4735 sreloc = NULL;
4736 is_opd = ppc64_elf_section_data (sec)->sec_type == sec_opd;
4737 rel_end = relocs + sec->reloc_count;
4738 for (rel = relocs; rel < rel_end; rel++)
4739 {
4740 unsigned long r_symndx;
4741 struct elf_link_hash_entry *h;
4742 enum elf_ppc64_reloc_type r_type;
4743 int tls_type;
4744 struct _ppc64_elf_section_data *ppc64_sec;
4745 struct plt_entry **ifunc, **plt_list;
4746
4747 r_symndx = ELF64_R_SYM (rel->r_info);
4748 if (r_symndx < symtab_hdr->sh_info)
4749 h = NULL;
4750 else
4751 {
4752 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4753 h = elf_follow_link (h);
4754
4755 if (h == htab->elf.hgot)
4756 sec->has_toc_reloc = 1;
4757 }
4758
4759 r_type = ELF64_R_TYPE (rel->r_info);
4760 switch (r_type)
4761 {
4762 case R_PPC64_D34:
4763 case R_PPC64_D34_LO:
4764 case R_PPC64_D34_HI30:
4765 case R_PPC64_D34_HA30:
4766 case R_PPC64_D28:
4767 case R_PPC64_TPREL34:
4768 case R_PPC64_DTPREL34:
4769 case R_PPC64_PCREL34:
4770 case R_PPC64_GOT_PCREL34:
4771 case R_PPC64_GOT_TLSGD_PCREL34:
4772 case R_PPC64_GOT_TLSLD_PCREL34:
4773 case R_PPC64_GOT_TPREL_PCREL34:
4774 case R_PPC64_GOT_DTPREL_PCREL34:
4775 case R_PPC64_PLT_PCREL34:
4776 case R_PPC64_PLT_PCREL34_NOTOC:
4777 case R_PPC64_PCREL28:
4778 htab->has_power10_relocs = 1;
4779 break;
4780 default:
4781 break;
4782 }
4783
4784 switch (r_type)
4785 {
4786 case R_PPC64_PLT16_HA:
4787 case R_PPC64_GOT_TLSLD16_HA:
4788 case R_PPC64_GOT_TLSGD16_HA:
4789 case R_PPC64_GOT_TPREL16_HA:
4790 case R_PPC64_GOT_DTPREL16_HA:
4791 case R_PPC64_GOT16_HA:
4792 case R_PPC64_TOC16_HA:
4793 case R_PPC64_PLT16_LO:
4794 case R_PPC64_PLT16_LO_DS:
4795 case R_PPC64_GOT_TLSLD16_LO:
4796 case R_PPC64_GOT_TLSGD16_LO:
4797 case R_PPC64_GOT_TPREL16_LO_DS:
4798 case R_PPC64_GOT_DTPREL16_LO_DS:
4799 case R_PPC64_GOT16_LO:
4800 case R_PPC64_GOT16_LO_DS:
4801 case R_PPC64_TOC16_LO:
4802 case R_PPC64_TOC16_LO_DS:
4803 case R_PPC64_GOT_PCREL34:
4804 ppc64_elf_tdata (abfd)->has_optrel = 1;
4805 ppc64_elf_section_data (sec)->has_optrel = 1;
4806 break;
4807 default:
4808 break;
4809 }
4810
4811 ifunc = NULL;
4812 if (h != NULL)
4813 {
4814 if (h->type == STT_GNU_IFUNC)
4815 {
4816 h->needs_plt = 1;
4817 ifunc = &h->plt.plist;
4818 }
4819 }
4820 else
4821 {
4822 Elf_Internal_Sym *isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
4823 abfd, r_symndx);
4824 if (isym == NULL)
4825 return false;
4826
4827 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC)
4828 {
4829 ifunc = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4830 rel->r_addend,
4831 NON_GOT | PLT_IFUNC);
4832 if (ifunc == NULL)
4833 return false;
4834 }
4835 }
4836
4837 tls_type = 0;
4838 switch (r_type)
4839 {
4840 case R_PPC64_TLSGD:
4841 case R_PPC64_TLSLD:
4842 /* These special tls relocs tie a call to __tls_get_addr with
4843 its parameter symbol. */
4844 if (h != NULL)
4845 ppc_elf_hash_entry (h)->tls_mask |= TLS_TLS | TLS_MARK;
4846 else
4847 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4848 rel->r_addend,
4849 NON_GOT | TLS_TLS | TLS_MARK))
4850 return false;
4851 sec->has_tls_reloc = 1;
4852 break;
4853
4854 case R_PPC64_GOT_TLSLD16:
4855 case R_PPC64_GOT_TLSLD16_LO:
4856 case R_PPC64_GOT_TLSLD16_HI:
4857 case R_PPC64_GOT_TLSLD16_HA:
4858 case R_PPC64_GOT_TLSLD_PCREL34:
4859 tls_type = TLS_TLS | TLS_LD;
4860 goto dogottls;
4861
4862 case R_PPC64_GOT_TLSGD16:
4863 case R_PPC64_GOT_TLSGD16_LO:
4864 case R_PPC64_GOT_TLSGD16_HI:
4865 case R_PPC64_GOT_TLSGD16_HA:
4866 case R_PPC64_GOT_TLSGD_PCREL34:
4867 tls_type = TLS_TLS | TLS_GD;
4868 goto dogottls;
4869
4870 case R_PPC64_GOT_TPREL16_DS:
4871 case R_PPC64_GOT_TPREL16_LO_DS:
4872 case R_PPC64_GOT_TPREL16_HI:
4873 case R_PPC64_GOT_TPREL16_HA:
4874 case R_PPC64_GOT_TPREL_PCREL34:
4875 if (bfd_link_dll (info))
4876 info->flags |= DF_STATIC_TLS;
4877 tls_type = TLS_TLS | TLS_TPREL;
4878 goto dogottls;
4879
4880 case R_PPC64_GOT_DTPREL16_DS:
4881 case R_PPC64_GOT_DTPREL16_LO_DS:
4882 case R_PPC64_GOT_DTPREL16_HI:
4883 case R_PPC64_GOT_DTPREL16_HA:
4884 case R_PPC64_GOT_DTPREL_PCREL34:
4885 tls_type = TLS_TLS | TLS_DTPREL;
4886 dogottls:
4887 sec->has_tls_reloc = 1;
4888 goto dogot;
4889
4890 case R_PPC64_GOT16:
4891 case R_PPC64_GOT16_LO:
4892 case R_PPC64_GOT16_HI:
4893 case R_PPC64_GOT16_HA:
4894 case R_PPC64_GOT16_DS:
4895 case R_PPC64_GOT16_LO_DS:
4896 case R_PPC64_GOT_PCREL34:
4897 dogot:
4898 /* This symbol requires a global offset table entry. */
4899 sec->has_toc_reloc = 1;
4900 if (r_type == R_PPC64_GOT_TLSLD16
4901 || r_type == R_PPC64_GOT_TLSGD16
4902 || r_type == R_PPC64_GOT_TPREL16_DS
4903 || r_type == R_PPC64_GOT_DTPREL16_DS
4904 || r_type == R_PPC64_GOT16
4905 || r_type == R_PPC64_GOT16_DS)
4906 {
4907 htab->do_multi_toc = 1;
4908 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
4909 }
4910
4911 if (ppc64_elf_tdata (abfd)->got == NULL
4912 && !create_got_section (abfd, info))
4913 return false;
4914
4915 if (h != NULL)
4916 {
4917 struct ppc_link_hash_entry *eh;
4918 struct got_entry *ent;
4919
4920 eh = ppc_elf_hash_entry (h);
4921 for (ent = eh->elf.got.glist; ent != NULL; ent = ent->next)
4922 if (ent->addend == rel->r_addend
4923 && ent->owner == abfd
4924 && ent->tls_type == tls_type)
4925 break;
4926 if (ent == NULL)
4927 {
4928 size_t amt = sizeof (*ent);
4929 ent = bfd_alloc (abfd, amt);
4930 if (ent == NULL)
4931 return false;
4932 ent->next = eh->elf.got.glist;
4933 ent->addend = rel->r_addend;
4934 ent->owner = abfd;
4935 ent->tls_type = tls_type;
4936 ent->is_indirect = false;
4937 ent->got.refcount = 0;
4938 eh->elf.got.glist = ent;
4939 }
4940 ent->got.refcount += 1;
4941 eh->tls_mask |= tls_type;
4942 }
4943 else
4944 /* This is a global offset table entry for a local symbol. */
4945 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
4946 rel->r_addend, tls_type))
4947 return false;
4948 break;
4949
4950 case R_PPC64_PLT16_HA:
4951 case R_PPC64_PLT16_HI:
4952 case R_PPC64_PLT16_LO:
4953 case R_PPC64_PLT16_LO_DS:
4954 case R_PPC64_PLT_PCREL34:
4955 case R_PPC64_PLT_PCREL34_NOTOC:
4956 case R_PPC64_PLT32:
4957 case R_PPC64_PLT64:
4958 /* This symbol requires a procedure linkage table entry. */
4959 plt_list = ifunc;
4960 if (h != NULL)
4961 {
4962 h->needs_plt = 1;
4963 if (h->root.root.string[0] == '.'
4964 && h->root.root.string[1] != '\0')
4965 ppc_elf_hash_entry (h)->is_func = 1;
4966 ppc_elf_hash_entry (h)->tls_mask |= PLT_KEEP;
4967 plt_list = &h->plt.plist;
4968 }
4969 if (plt_list == NULL)
4970 plt_list = update_local_sym_info (abfd, symtab_hdr, r_symndx,
4971 rel->r_addend,
4972 NON_GOT | PLT_KEEP);
4973 if (!update_plt_info (abfd, plt_list, rel->r_addend))
4974 return false;
4975 break;
4976
4977 /* The following relocations don't need to propagate the
4978 relocation if linking a shared object since they are
4979 section relative. */
4980 case R_PPC64_SECTOFF:
4981 case R_PPC64_SECTOFF_LO:
4982 case R_PPC64_SECTOFF_HI:
4983 case R_PPC64_SECTOFF_HA:
4984 case R_PPC64_SECTOFF_DS:
4985 case R_PPC64_SECTOFF_LO_DS:
4986 case R_PPC64_DTPREL16:
4987 case R_PPC64_DTPREL16_LO:
4988 case R_PPC64_DTPREL16_HI:
4989 case R_PPC64_DTPREL16_HA:
4990 case R_PPC64_DTPREL16_DS:
4991 case R_PPC64_DTPREL16_LO_DS:
4992 case R_PPC64_DTPREL16_HIGH:
4993 case R_PPC64_DTPREL16_HIGHA:
4994 case R_PPC64_DTPREL16_HIGHER:
4995 case R_PPC64_DTPREL16_HIGHERA:
4996 case R_PPC64_DTPREL16_HIGHEST:
4997 case R_PPC64_DTPREL16_HIGHESTA:
4998 break;
4999
5000 /* Nor do these. */
5001 case R_PPC64_REL16:
5002 case R_PPC64_REL16_LO:
5003 case R_PPC64_REL16_HI:
5004 case R_PPC64_REL16_HA:
5005 case R_PPC64_REL16_HIGH:
5006 case R_PPC64_REL16_HIGHA:
5007 case R_PPC64_REL16_HIGHER:
5008 case R_PPC64_REL16_HIGHERA:
5009 case R_PPC64_REL16_HIGHEST:
5010 case R_PPC64_REL16_HIGHESTA:
5011 case R_PPC64_REL16_HIGHER34:
5012 case R_PPC64_REL16_HIGHERA34:
5013 case R_PPC64_REL16_HIGHEST34:
5014 case R_PPC64_REL16_HIGHESTA34:
5015 case R_PPC64_REL16DX_HA:
5016 break;
5017
5018 /* Not supported as a dynamic relocation. */
5019 case R_PPC64_ADDR64_LOCAL:
5020 if (bfd_link_pic (info))
5021 {
5022 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
5023 ppc_howto_init ();
5024 /* xgettext:c-format */
5025 info->callbacks->einfo (_("%H: %s reloc unsupported "
5026 "in shared libraries and PIEs\n"),
5027 abfd, sec, rel->r_offset,
5028 ppc64_elf_howto_table[r_type]->name);
5029 bfd_set_error (bfd_error_bad_value);
5030 return false;
5031 }
5032 break;
5033
5034 case R_PPC64_TOC16:
5035 case R_PPC64_TOC16_DS:
5036 htab->do_multi_toc = 1;
5037 ppc64_elf_tdata (abfd)->has_small_toc_reloc = 1;
5038 /* Fall through. */
5039 case R_PPC64_TOC16_LO:
5040 case R_PPC64_TOC16_HI:
5041 case R_PPC64_TOC16_HA:
5042 case R_PPC64_TOC16_LO_DS:
5043 sec->has_toc_reloc = 1;
5044 if (h != NULL && bfd_link_executable (info))
5045 {
5046 /* We may need a copy reloc. */
5047 h->non_got_ref = 1;
5048 /* Strongly prefer a copy reloc over a dynamic reloc.
5049 glibc ld.so as of 2019-08 will error out if one of
5050 these relocations is emitted. */
5051 h->needs_copy = 1;
5052 goto dodyn;
5053 }
5054 break;
5055
5056 /* Marker reloc. */
5057 case R_PPC64_ENTRY:
5058 break;
5059
5060 /* This relocation describes the C++ object vtable hierarchy.
5061 Reconstruct it for later use during GC. */
5062 case R_PPC64_GNU_VTINHERIT:
5063 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5064 return false;
5065 break;
5066
5067 /* This relocation describes which C++ vtable entries are actually
5068 used. Record for later use during GC. */
5069 case R_PPC64_GNU_VTENTRY:
5070 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5071 return false;
5072 break;
5073
5074 case R_PPC64_REL14:
5075 case R_PPC64_REL14_BRTAKEN:
5076 case R_PPC64_REL14_BRNTAKEN:
5077 {
5078 asection *dest = NULL;
5079
5080 /* Heuristic: If jumping outside our section, chances are
5081 we are going to need a stub. */
5082 if (h != NULL)
5083 {
5084 /* If the sym is weak it may be overridden later, so
5085 don't assume we know where a weak sym lives. */
5086 if (h->root.type == bfd_link_hash_defined)
5087 dest = h->root.u.def.section;
5088 }
5089 else
5090 {
5091 Elf_Internal_Sym *isym;
5092
5093 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5094 abfd, r_symndx);
5095 if (isym == NULL)
5096 return false;
5097
5098 dest = bfd_section_from_elf_index (abfd, isym->st_shndx);
5099 }
5100
5101 if (dest != sec)
5102 ppc64_elf_section_data (sec)->has_14bit_branch = 1;
5103 }
5104 goto rel24;
5105
5106 case R_PPC64_PLTCALL:
5107 case R_PPC64_PLTCALL_NOTOC:
5108 ppc64_elf_section_data (sec)->has_pltcall = 1;
5109 /* Fall through. */
5110
5111 case R_PPC64_REL24:
5112 case R_PPC64_REL24_NOTOC:
5113 case R_PPC64_REL24_P9NOTOC:
5114 rel24:
5115 plt_list = ifunc;
5116 if (h != NULL)
5117 {
5118 h->needs_plt = 1;
5119 if (h->root.root.string[0] == '.'
5120 && h->root.root.string[1] != '\0')
5121 ppc_elf_hash_entry (h)->is_func = 1;
5122
5123 if (h == tga || h == dottga)
5124 {
5125 sec->has_tls_reloc = 1;
5126 if (rel != relocs
5127 && (ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSGD
5128 || ELF64_R_TYPE (rel[-1].r_info) == R_PPC64_TLSLD))
5129 /* We have a new-style __tls_get_addr call with
5130 a marker reloc. */
5131 ;
5132 else
5133 /* Mark this section as having an old-style call. */
5134 sec->nomark_tls_get_addr = 1;
5135 }
5136 plt_list = &h->plt.plist;
5137 }
5138
5139 /* We may need a .plt entry if the function this reloc
5140 refers to is in a shared lib. */
5141 if (plt_list
5142 && !update_plt_info (abfd, plt_list, rel->r_addend))
5143 return false;
5144 break;
5145
5146 case R_PPC64_ADDR14:
5147 case R_PPC64_ADDR14_BRNTAKEN:
5148 case R_PPC64_ADDR14_BRTAKEN:
5149 case R_PPC64_ADDR24:
5150 goto dodyn;
5151
5152 case R_PPC64_TPREL64:
5153 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_TPREL;
5154 if (bfd_link_dll (info))
5155 info->flags |= DF_STATIC_TLS;
5156 goto dotlstoc;
5157
5158 case R_PPC64_DTPMOD64:
5159 if (rel + 1 < rel_end
5160 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
5161 && rel[1].r_offset == rel->r_offset + 8)
5162 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_GD;
5163 else
5164 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_LD;
5165 goto dotlstoc;
5166
5167 case R_PPC64_DTPREL64:
5168 tls_type = TLS_EXPLICIT | TLS_TLS | TLS_DTPREL;
5169 if (rel != relocs
5170 && rel[-1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPMOD64)
5171 && rel[-1].r_offset == rel->r_offset - 8)
5172 /* This is the second reloc of a dtpmod, dtprel pair.
5173 Don't mark with TLS_DTPREL. */
5174 goto dodyn;
5175
5176 dotlstoc:
5177 sec->has_tls_reloc = 1;
5178 if (h != NULL)
5179 ppc_elf_hash_entry (h)->tls_mask |= tls_type & 0xff;
5180 else
5181 if (!update_local_sym_info (abfd, symtab_hdr, r_symndx,
5182 rel->r_addend, tls_type))
5183 return false;
5184
5185 ppc64_sec = ppc64_elf_section_data (sec);
5186 if (ppc64_sec->sec_type != sec_toc)
5187 {
5188 bfd_size_type amt;
5189
5190 /* One extra to simplify get_tls_mask. */
5191 amt = sec->size * sizeof (unsigned) / 8 + sizeof (unsigned);
5192 ppc64_sec->u.toc.symndx = bfd_zalloc (abfd, amt);
5193 if (ppc64_sec->u.toc.symndx == NULL)
5194 return false;
5195 amt = sec->size * sizeof (bfd_vma) / 8;
5196 ppc64_sec->u.toc.add = bfd_zalloc (abfd, amt);
5197 if (ppc64_sec->u.toc.add == NULL)
5198 return false;
5199 BFD_ASSERT (ppc64_sec->sec_type == sec_normal);
5200 ppc64_sec->sec_type = sec_toc;
5201 }
5202 BFD_ASSERT (rel->r_offset % 8 == 0);
5203 ppc64_sec->u.toc.symndx[rel->r_offset / 8] = r_symndx;
5204 ppc64_sec->u.toc.add[rel->r_offset / 8] = rel->r_addend;
5205
5206 /* Mark the second slot of a GD or LD entry.
5207 -1 to indicate GD and -2 to indicate LD. */
5208 if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_GD))
5209 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -1;
5210 else if (tls_type == (TLS_EXPLICIT | TLS_TLS | TLS_LD))
5211 ppc64_sec->u.toc.symndx[rel->r_offset / 8 + 1] = -2;
5212 goto dodyn;
5213
5214 case R_PPC64_TPREL16_HI:
5215 case R_PPC64_TPREL16_HA:
5216 case R_PPC64_TPREL16_HIGH:
5217 case R_PPC64_TPREL16_HIGHA:
5218 case R_PPC64_TPREL16_HIGHER:
5219 case R_PPC64_TPREL16_HIGHERA:
5220 case R_PPC64_TPREL16_HIGHEST:
5221 case R_PPC64_TPREL16_HIGHESTA:
5222 sec->has_tls_reloc = 1;
5223 /* Fall through. */
5224 case R_PPC64_TPREL34:
5225 case R_PPC64_TPREL16:
5226 case R_PPC64_TPREL16_DS:
5227 case R_PPC64_TPREL16_LO:
5228 case R_PPC64_TPREL16_LO_DS:
5229 if (bfd_link_dll (info))
5230 info->flags |= DF_STATIC_TLS;
5231 goto dodyn;
5232
5233 case R_PPC64_ADDR64:
5234 if (is_opd
5235 && rel + 1 < rel_end
5236 && ELF64_R_TYPE ((rel + 1)->r_info) == R_PPC64_TOC)
5237 {
5238 if (h != NULL)
5239 ppc_elf_hash_entry (h)->is_func = 1;
5240 }
5241 /* Fall through. */
5242
5243 case R_PPC64_ADDR16:
5244 case R_PPC64_ADDR16_DS:
5245 case R_PPC64_ADDR16_HA:
5246 case R_PPC64_ADDR16_HI:
5247 case R_PPC64_ADDR16_HIGH:
5248 case R_PPC64_ADDR16_HIGHA:
5249 case R_PPC64_ADDR16_HIGHER:
5250 case R_PPC64_ADDR16_HIGHERA:
5251 case R_PPC64_ADDR16_HIGHEST:
5252 case R_PPC64_ADDR16_HIGHESTA:
5253 case R_PPC64_ADDR16_LO:
5254 case R_PPC64_ADDR16_LO_DS:
5255 case R_PPC64_D34:
5256 case R_PPC64_D34_LO:
5257 case R_PPC64_D34_HI30:
5258 case R_PPC64_D34_HA30:
5259 case R_PPC64_ADDR16_HIGHER34:
5260 case R_PPC64_ADDR16_HIGHERA34:
5261 case R_PPC64_ADDR16_HIGHEST34:
5262 case R_PPC64_ADDR16_HIGHESTA34:
5263 case R_PPC64_D28:
5264 if (h != NULL && !bfd_link_pic (info) && abiversion (abfd) != 1
5265 && rel->r_addend == 0)
5266 {
5267 /* We may need a .plt entry if this reloc refers to a
5268 function in a shared lib. */
5269 if (!update_plt_info (abfd, &h->plt.plist, 0))
5270 return false;
5271 h->pointer_equality_needed = 1;
5272 }
5273 /* Fall through. */
5274
5275 case R_PPC64_REL30:
5276 case R_PPC64_REL32:
5277 case R_PPC64_REL64:
5278 case R_PPC64_ADDR32:
5279 case R_PPC64_UADDR16:
5280 case R_PPC64_UADDR32:
5281 case R_PPC64_UADDR64:
5282 case R_PPC64_TOC:
5283 if (h != NULL && bfd_link_executable (info))
5284 /* We may need a copy reloc. */
5285 h->non_got_ref = 1;
5286
5287 /* Don't propagate .opd relocs. */
5288 if (NO_OPD_RELOCS && is_opd)
5289 break;
5290
5291 /* If we are creating a shared library, and this is a reloc
5292 against a global symbol, or a non PC relative reloc
5293 against a local symbol, then we need to copy the reloc
5294 into the shared library. However, if we are linking with
5295 -Bsymbolic, we do not need to copy a reloc against a
5296 global symbol which is defined in an object we are
5297 including in the link (i.e., DEF_REGULAR is set). At
5298 this point we have not seen all the input files, so it is
5299 possible that DEF_REGULAR is not set now but will be set
5300 later (it is never cleared). In case of a weak definition,
5301 DEF_REGULAR may be cleared later by a strong definition in
5302 a shared library. We account for that possibility below by
5303 storing information in the dyn_relocs field of the hash
5304 table entry. A similar situation occurs when creating
5305 shared libraries and symbol visibility changes render the
5306 symbol local.
5307
5308 If on the other hand, we are creating an executable, we
5309 may need to keep relocations for symbols satisfied by a
5310 dynamic library if we manage to avoid copy relocs for the
5311 symbol. */
5312 dodyn:
5313 if ((h != NULL
5314 && (h->root.type == bfd_link_hash_defweak
5315 || !h->def_regular))
5316 || (h != NULL
5317 && !bfd_link_executable (info)
5318 && !SYMBOLIC_BIND (info, h))
5319 || (bfd_link_pic (info)
5320 && must_be_dyn_reloc (info, r_type))
5321 || (!bfd_link_pic (info)
5322 && ifunc != NULL))
5323 {
5324 /* We must copy these reloc types into the output file.
5325 Create a reloc section in dynobj and make room for
5326 this reloc. */
5327 if (sreloc == NULL)
5328 {
5329 sreloc = _bfd_elf_make_dynamic_reloc_section
5330 (sec, htab->elf.dynobj, 3, abfd, /*rela?*/ true);
5331
5332 if (sreloc == NULL)
5333 return false;
5334 }
5335
5336 /* If this is a global symbol, we count the number of
5337 relocations we need for this symbol. */
5338 if (h != NULL)
5339 {
5340 struct elf_dyn_relocs *p;
5341 struct elf_dyn_relocs **head;
5342
5343 head = &h->dyn_relocs;
5344 p = *head;
5345 if (p == NULL || p->sec != sec)
5346 {
5347 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5348 if (p == NULL)
5349 return false;
5350 p->next = *head;
5351 *head = p;
5352 p->sec = sec;
5353 p->count = 0;
5354 p->pc_count = 0;
5355 }
5356 p->count += 1;
5357 if (!must_be_dyn_reloc (info, r_type))
5358 p->pc_count += 1;
5359 }
5360 else
5361 {
5362 /* Track dynamic relocs needed for local syms too.
5363 We really need local syms available to do this
5364 easily. Oh well. */
5365 struct ppc_dyn_relocs *p;
5366 struct ppc_dyn_relocs **head;
5367 bool is_ifunc;
5368 asection *s;
5369 void *vpp;
5370 Elf_Internal_Sym *isym;
5371
5372 isym = bfd_sym_from_r_symndx (&htab->elf.sym_cache,
5373 abfd, r_symndx);
5374 if (isym == NULL)
5375 return false;
5376
5377 s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5378 if (s == NULL)
5379 s = sec;
5380
5381 vpp = &elf_section_data (s)->local_dynrel;
5382 head = (struct ppc_dyn_relocs **) vpp;
5383 is_ifunc = ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC;
5384 p = *head;
5385 if (p != NULL && p->sec == sec && p->ifunc != is_ifunc)
5386 p = p->next;
5387 if (p == NULL || p->sec != sec || p->ifunc != is_ifunc)
5388 {
5389 p = bfd_alloc (htab->elf.dynobj, sizeof *p);
5390 if (p == NULL)
5391 return false;
5392 p->next = *head;
5393 *head = p;
5394 p->sec = sec;
5395 p->ifunc = is_ifunc;
5396 p->count = 0;
5397 }
5398 p->count += 1;
5399 }
5400 }
5401 break;
5402
5403 default:
5404 break;
5405 }
5406 }
5407
5408 return true;
5409 }
5410
5411 /* Merge backend specific data from an object file to the output
5412 object file when linking. */
5413
5414 static bool
5415 ppc64_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
5416 {
5417 bfd *obfd = info->output_bfd;
5418 unsigned long iflags, oflags;
5419
5420 if ((ibfd->flags & BFD_LINKER_CREATED) != 0)
5421 return true;
5422
5423 if (!is_ppc64_elf (ibfd) || !is_ppc64_elf (obfd))
5424 return true;
5425
5426 if (!_bfd_generic_verify_endian_match (ibfd, info))
5427 return false;
5428
5429 iflags = elf_elfheader (ibfd)->e_flags;
5430 oflags = elf_elfheader (obfd)->e_flags;
5431
5432 if (iflags & ~EF_PPC64_ABI)
5433 {
5434 _bfd_error_handler
5435 /* xgettext:c-format */
5436 (_("%pB uses unknown e_flags 0x%lx"), ibfd, iflags);
5437 bfd_set_error (bfd_error_bad_value);
5438 return false;
5439 }
5440 else if (iflags != oflags && iflags != 0)
5441 {
5442 _bfd_error_handler
5443 /* xgettext:c-format */
5444 (_("%pB: ABI version %ld is not compatible with ABI version %ld output"),
5445 ibfd, iflags, oflags);
5446 bfd_set_error (bfd_error_bad_value);
5447 return false;
5448 }
5449
5450 if (!_bfd_elf_ppc_merge_fp_attributes (ibfd, info))
5451 return false;
5452
5453 /* Merge Tag_compatibility attributes and any common GNU ones. */
5454 return _bfd_elf_merge_object_attributes (ibfd, info);
5455 }
5456
5457 static bool
5458 ppc64_elf_print_private_bfd_data (bfd *abfd, void *ptr)
5459 {
5460 /* Print normal ELF private data. */
5461 _bfd_elf_print_private_bfd_data (abfd, ptr);
5462
5463 if (elf_elfheader (abfd)->e_flags != 0)
5464 {
5465 FILE *file = ptr;
5466
5467 fprintf (file, _("private flags = 0x%lx:"),
5468 elf_elfheader (abfd)->e_flags);
5469
5470 if ((elf_elfheader (abfd)->e_flags & EF_PPC64_ABI) != 0)
5471 fprintf (file, _(" [abiv%ld]"),
5472 elf_elfheader (abfd)->e_flags & EF_PPC64_ABI);
5473 fputc ('\n', file);
5474 }
5475
5476 return true;
5477 }
5478
5479 /* OFFSET in OPD_SEC specifies a function descriptor. Return the address
5480 of the code entry point, and its section, which must be in the same
5481 object as OPD_SEC. Returns (bfd_vma) -1 on error. */
5482
5483 static bfd_vma
5484 opd_entry_value (asection *opd_sec,
5485 bfd_vma offset,
5486 asection **code_sec,
5487 bfd_vma *code_off,
5488 bool in_code_sec)
5489 {
5490 bfd *opd_bfd = opd_sec->owner;
5491 Elf_Internal_Rela *relocs;
5492 Elf_Internal_Rela *lo, *hi, *look;
5493 bfd_vma val;
5494
5495 /* No relocs implies we are linking a --just-symbols object, or looking
5496 at a final linked executable with addr2line or somesuch. */
5497 if (opd_sec->reloc_count == 0)
5498 {
5499 bfd_byte *contents = ppc64_elf_tdata (opd_bfd)->opd.contents;
5500
5501 if (contents == NULL)
5502 {
5503 if (!bfd_malloc_and_get_section (opd_bfd, opd_sec, &contents))
5504 return (bfd_vma) -1;
5505 ppc64_elf_tdata (opd_bfd)->opd.contents = contents;
5506 }
5507
5508 /* PR 17512: file: 64b9dfbb. */
5509 if (offset + 7 >= opd_sec->size || offset + 7 < offset)
5510 return (bfd_vma) -1;
5511
5512 val = bfd_get_64 (opd_bfd, contents + offset);
5513 if (code_sec != NULL)
5514 {
5515 asection *sec, *likely = NULL;
5516
5517 if (in_code_sec)
5518 {
5519 sec = *code_sec;
5520 if (sec->vma <= val
5521 && val < sec->vma + sec->size)
5522 likely = sec;
5523 else
5524 val = -1;
5525 }
5526 else
5527 for (sec = opd_bfd->sections; sec != NULL; sec = sec->next)
5528 if (sec->vma <= val
5529 && (sec->flags & SEC_LOAD) != 0
5530 && (sec->flags & SEC_ALLOC) != 0)
5531 likely = sec;
5532 if (likely != NULL)
5533 {
5534 *code_sec = likely;
5535 if (code_off != NULL)
5536 *code_off = val - likely->vma;
5537 }
5538 }
5539 return val;
5540 }
5541
5542 BFD_ASSERT (is_ppc64_elf (opd_bfd));
5543
5544 relocs = ppc64_elf_tdata (opd_bfd)->opd.relocs;
5545 if (relocs == NULL)
5546 relocs = _bfd_elf_link_read_relocs (opd_bfd, opd_sec, NULL, NULL, true);
5547 /* PR 17512: file: df8e1fd6. */
5548 if (relocs == NULL)
5549 return (bfd_vma) -1;
5550
5551 /* Go find the opd reloc at the sym address. */
5552 lo = relocs;
5553 hi = lo + opd_sec->reloc_count - 1; /* ignore last reloc */
5554 val = (bfd_vma) -1;
5555 while (lo < hi)
5556 {
5557 look = lo + (hi - lo) / 2;
5558 if (look->r_offset < offset)
5559 lo = look + 1;
5560 else if (look->r_offset > offset)
5561 hi = look;
5562 else
5563 {
5564 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (opd_bfd);
5565
5566 if (ELF64_R_TYPE (look->r_info) == R_PPC64_ADDR64
5567 && ELF64_R_TYPE ((look + 1)->r_info) == R_PPC64_TOC)
5568 {
5569 unsigned long symndx = ELF64_R_SYM (look->r_info);
5570 asection *sec = NULL;
5571
5572 if (symndx >= symtab_hdr->sh_info
5573 && elf_sym_hashes (opd_bfd) != NULL)
5574 {
5575 struct elf_link_hash_entry **sym_hashes;
5576 struct elf_link_hash_entry *rh;
5577
5578 sym_hashes = elf_sym_hashes (opd_bfd);
5579 rh = sym_hashes[symndx - symtab_hdr->sh_info];
5580 if (rh != NULL)
5581 {
5582 rh = elf_follow_link (rh);
5583 if (rh->root.type != bfd_link_hash_defined
5584 && rh->root.type != bfd_link_hash_defweak)
5585 break;
5586 if (rh->root.u.def.section->owner == opd_bfd)
5587 {
5588 val = rh->root.u.def.value;
5589 sec = rh->root.u.def.section;
5590 }
5591 }
5592 }
5593
5594 if (sec == NULL)
5595 {
5596 Elf_Internal_Sym *sym;
5597
5598 if (symndx < symtab_hdr->sh_info)
5599 {
5600 sym = (Elf_Internal_Sym *) symtab_hdr->contents;
5601 if (sym == NULL)
5602 {
5603 size_t symcnt = symtab_hdr->sh_info;
5604 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5605 symcnt, 0,
5606 NULL, NULL, NULL);
5607 if (sym == NULL)
5608 break;
5609 symtab_hdr->contents = (bfd_byte *) sym;
5610 }
5611 sym += symndx;
5612 }
5613 else
5614 {
5615 sym = bfd_elf_get_elf_syms (opd_bfd, symtab_hdr,
5616 1, symndx,
5617 NULL, NULL, NULL);
5618 if (sym == NULL)
5619 break;
5620 }
5621 sec = bfd_section_from_elf_index (opd_bfd, sym->st_shndx);
5622 if (sec == NULL)
5623 break;
5624 BFD_ASSERT ((sec->flags & SEC_MERGE) == 0);
5625 val = sym->st_value;
5626 }
5627
5628 val += look->r_addend;
5629 if (code_off != NULL)
5630 *code_off = val;
5631 if (code_sec != NULL)
5632 {
5633 if (in_code_sec && *code_sec != sec)
5634 return -1;
5635 else
5636 *code_sec = sec;
5637 }
5638 if (sec->output_section != NULL)
5639 val += sec->output_section->vma + sec->output_offset;
5640 }
5641 break;
5642 }
5643 }
5644
5645 return val;
5646 }
5647
5648 /* If the ELF symbol SYM might be a function in SEC, return the
5649 function size and set *CODE_OFF to the function's entry point,
5650 otherwise return zero. */
5651
5652 static bfd_size_type
5653 ppc64_elf_maybe_function_sym (const asymbol *sym, asection *sec,
5654 bfd_vma *code_off)
5655 {
5656 bfd_size_type size;
5657 elf_symbol_type * elf_sym = (elf_symbol_type *) sym;
5658
5659 if ((sym->flags & (BSF_SECTION_SYM | BSF_FILE | BSF_OBJECT
5660 | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC)) != 0)
5661 return 0;
5662
5663 size = (sym->flags & BSF_SYNTHETIC) ? 0 : elf_sym->internal_elf_sym.st_size;
5664
5665 /* In theory we should check that the symbol's type satisfies
5666 _bfd_elf_is_function_type(), but there are some function-like
5667 symbols which would fail this test. (eg _start). Instead
5668 we check for hidden, local, notype symbols with zero size.
5669 This type of symbol is generated by the annobin plugin for gcc
5670 and clang, and should not be considered to be a function symbol. */
5671 if (size == 0
5672 && ((sym->flags & (BSF_SYNTHETIC | BSF_LOCAL)) == BSF_LOCAL)
5673 && ELF_ST_TYPE (elf_sym->internal_elf_sym.st_info) == STT_NOTYPE
5674 && ELF_ST_VISIBILITY (elf_sym->internal_elf_sym.st_other) == STV_HIDDEN)
5675 return 0;
5676
5677 if (strcmp (sym->section->name, ".opd") == 0)
5678 {
5679 struct _opd_sec_data *opd = get_opd_info (sym->section);
5680 bfd_vma symval = sym->value;
5681
5682 if (opd != NULL
5683 && opd->adjust != NULL
5684 && elf_section_data (sym->section)->relocs != NULL)
5685 {
5686 /* opd_entry_value will use cached relocs that have been
5687 adjusted, but with raw symbols. That means both local
5688 and global symbols need adjusting. */
5689 long adjust = opd->adjust[OPD_NDX (symval)];
5690 if (adjust == -1)
5691 return 0;
5692 symval += adjust;
5693 }
5694
5695 if (opd_entry_value (sym->section, symval,
5696 &sec, code_off, true) == (bfd_vma) -1)
5697 return 0;
5698 /* An old ABI binary with dot-syms has a size of 24 on the .opd
5699 symbol. This size has nothing to do with the code size of the
5700 function, which is what we're supposed to return, but the
5701 code size isn't available without looking up the dot-sym.
5702 However, doing that would be a waste of time particularly
5703 since elf_find_function will look at the dot-sym anyway.
5704 Now, elf_find_function will keep the largest size of any
5705 function sym found at the code address of interest, so return
5706 1 here to avoid it incorrectly caching a larger function size
5707 for a small function. This does mean we return the wrong
5708 size for a new-ABI function of size 24, but all that does is
5709 disable caching for such functions. */
5710 if (size == 24)
5711 size = 1;
5712 }
5713 else
5714 {
5715 if (sym->section != sec)
5716 return 0;
5717 *code_off = sym->value;
5718 }
5719
5720 /* Do not return 0 for the function's size. */
5721 return size ? size : 1;
5722 }
5723
5724 /* Return true if symbol is a strong function defined in an ELFv2
5725 object with st_other localentry bits of zero, ie. its local entry
5726 point coincides with its global entry point. */
5727
5728 static bool
5729 is_elfv2_localentry0 (struct elf_link_hash_entry *h)
5730 {
5731 return (h != NULL
5732 && h->type == STT_FUNC
5733 && h->root.type == bfd_link_hash_defined
5734 && (STO_PPC64_LOCAL_MASK & h->other) == 0
5735 && !ppc_elf_hash_entry (h)->non_zero_localentry
5736 && is_ppc64_elf (h->root.u.def.section->owner)
5737 && abiversion (h->root.u.def.section->owner) >= 2);
5738 }
5739
5740 /* Return true if symbol is defined in a regular object file. */
5741
5742 static bool
5743 is_static_defined (struct elf_link_hash_entry *h)
5744 {
5745 return ((h->root.type == bfd_link_hash_defined
5746 || h->root.type == bfd_link_hash_defweak)
5747 && h->root.u.def.section != NULL
5748 && h->root.u.def.section->output_section != NULL);
5749 }
5750
5751 /* If FDH is a function descriptor symbol, return the associated code
5752 entry symbol if it is defined. Return NULL otherwise. */
5753
5754 static struct ppc_link_hash_entry *
5755 defined_code_entry (struct ppc_link_hash_entry *fdh)
5756 {
5757 if (fdh->is_func_descriptor)
5758 {
5759 struct ppc_link_hash_entry *fh = ppc_follow_link (fdh->oh);
5760 if (fh->elf.root.type == bfd_link_hash_defined
5761 || fh->elf.root.type == bfd_link_hash_defweak)
5762 return fh;
5763 }
5764 return NULL;
5765 }
5766
5767 /* If FH is a function code entry symbol, return the associated
5768 function descriptor symbol if it is defined. Return NULL otherwise. */
5769
5770 static struct ppc_link_hash_entry *
5771 defined_func_desc (struct ppc_link_hash_entry *fh)
5772 {
5773 if (fh->oh != NULL
5774 && fh->oh->is_func_descriptor)
5775 {
5776 struct ppc_link_hash_entry *fdh = ppc_follow_link (fh->oh);
5777 if (fdh->elf.root.type == bfd_link_hash_defined
5778 || fdh->elf.root.type == bfd_link_hash_defweak)
5779 return fdh;
5780 }
5781 return NULL;
5782 }
5783
5784 /* Given H is a symbol that satisfies is_static_defined, return the
5785 value in the output file. */
5786
5787 static bfd_vma
5788 defined_sym_val (struct elf_link_hash_entry *h)
5789 {
5790 return (h->root.u.def.section->output_section->vma
5791 + h->root.u.def.section->output_offset
5792 + h->root.u.def.value);
5793 }
5794
5795 /* Return true if H matches __tls_get_addr or one of its variants. */
5796
5797 static bool
5798 is_tls_get_addr (struct elf_link_hash_entry *h,
5799 struct ppc_link_hash_table *htab)
5800 {
5801 return (h == elf_hash_entry (htab->tls_get_addr_fd)
5802 || h == elf_hash_entry (htab->tga_desc_fd)
5803 || h == elf_hash_entry (htab->tls_get_addr)
5804 || h == elf_hash_entry (htab->tga_desc));
5805 }
5806
5807 static bool func_desc_adjust (struct elf_link_hash_entry *, void *);
5808
5809 /* Garbage collect sections, after first dealing with dot-symbols. */
5810
5811 static bool
5812 ppc64_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
5813 {
5814 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5815
5816 if (htab != NULL && htab->need_func_desc_adj)
5817 {
5818 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
5819 htab->need_func_desc_adj = 0;
5820 }
5821 return bfd_elf_gc_sections (abfd, info);
5822 }
5823
5824 /* Mark all our entry sym sections, both opd and code section. */
5825
5826 static void
5827 ppc64_elf_gc_keep (struct bfd_link_info *info)
5828 {
5829 struct ppc_link_hash_table *htab = ppc_hash_table (info);
5830 struct bfd_sym_chain *sym;
5831
5832 if (htab == NULL)
5833 return;
5834
5835 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
5836 {
5837 struct ppc_link_hash_entry *eh, *fh;
5838 asection *sec;
5839
5840 eh = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym->name,
5841 false, false, true));
5842 if (eh == NULL)
5843 continue;
5844 if (eh->elf.root.type != bfd_link_hash_defined
5845 && eh->elf.root.type != bfd_link_hash_defweak)
5846 continue;
5847
5848 fh = defined_code_entry (eh);
5849 if (fh != NULL)
5850 {
5851 sec = fh->elf.root.u.def.section;
5852 sec->flags |= SEC_KEEP;
5853 }
5854 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5855 && opd_entry_value (eh->elf.root.u.def.section,
5856 eh->elf.root.u.def.value,
5857 &sec, NULL, false) != (bfd_vma) -1)
5858 sec->flags |= SEC_KEEP;
5859
5860 sec = eh->elf.root.u.def.section;
5861 sec->flags |= SEC_KEEP;
5862 }
5863 }
5864
5865 /* Mark sections containing dynamically referenced symbols. When
5866 building shared libraries, we must assume that any visible symbol is
5867 referenced. */
5868
5869 static bool
5870 ppc64_elf_gc_mark_dynamic_ref (struct elf_link_hash_entry *h, void *inf)
5871 {
5872 struct bfd_link_info *info = (struct bfd_link_info *) inf;
5873 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
5874 struct ppc_link_hash_entry *fdh;
5875 struct bfd_elf_dynamic_list *d = info->dynamic_list;
5876
5877 /* Dynamic linking info is on the func descriptor sym. */
5878 fdh = defined_func_desc (eh);
5879 if (fdh != NULL)
5880 eh = fdh;
5881
5882 if ((eh->elf.root.type == bfd_link_hash_defined
5883 || eh->elf.root.type == bfd_link_hash_defweak)
5884 && (!eh->elf.start_stop
5885 || eh->elf.root.ldscript_def
5886 || !info->start_stop_gc)
5887 && ((eh->elf.ref_dynamic && !eh->elf.forced_local)
5888 || ((eh->elf.def_regular || ELF_COMMON_DEF_P (&eh->elf))
5889 && ELF_ST_VISIBILITY (eh->elf.other) != STV_INTERNAL
5890 && ELF_ST_VISIBILITY (eh->elf.other) != STV_HIDDEN
5891 && (!bfd_link_executable (info)
5892 || info->gc_keep_exported
5893 || info->export_dynamic
5894 || (eh->elf.dynamic
5895 && d != NULL
5896 && (*d->match) (&d->head, NULL,
5897 eh->elf.root.root.string)))
5898 && (eh->elf.versioned >= versioned
5899 || !bfd_hide_sym_by_version (info->version_info,
5900 eh->elf.root.root.string)))))
5901 {
5902 asection *code_sec;
5903 struct ppc_link_hash_entry *fh;
5904
5905 eh->elf.root.u.def.section->flags |= SEC_KEEP;
5906
5907 /* Function descriptor syms cause the associated
5908 function code sym section to be marked. */
5909 fh = defined_code_entry (eh);
5910 if (fh != NULL)
5911 {
5912 code_sec = fh->elf.root.u.def.section;
5913 code_sec->flags |= SEC_KEEP;
5914 }
5915 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5916 && opd_entry_value (eh->elf.root.u.def.section,
5917 eh->elf.root.u.def.value,
5918 &code_sec, NULL, false) != (bfd_vma) -1)
5919 code_sec->flags |= SEC_KEEP;
5920 }
5921
5922 return true;
5923 }
5924
5925 /* Return the section that should be marked against GC for a given
5926 relocation. */
5927
5928 static asection *
5929 ppc64_elf_gc_mark_hook (asection *sec,
5930 struct bfd_link_info *info,
5931 Elf_Internal_Rela *rel,
5932 struct elf_link_hash_entry *h,
5933 Elf_Internal_Sym *sym)
5934 {
5935 asection *rsec;
5936
5937 /* Syms return NULL if we're marking .opd, so we avoid marking all
5938 function sections, as all functions are referenced in .opd. */
5939 rsec = NULL;
5940 if (get_opd_info (sec) != NULL)
5941 return rsec;
5942
5943 if (h != NULL)
5944 {
5945 enum elf_ppc64_reloc_type r_type;
5946 struct ppc_link_hash_entry *eh, *fh, *fdh;
5947
5948 r_type = ELF64_R_TYPE (rel->r_info);
5949 switch (r_type)
5950 {
5951 case R_PPC64_GNU_VTINHERIT:
5952 case R_PPC64_GNU_VTENTRY:
5953 break;
5954
5955 default:
5956 switch (h->root.type)
5957 {
5958 case bfd_link_hash_defined:
5959 case bfd_link_hash_defweak:
5960 eh = ppc_elf_hash_entry (h);
5961 fdh = defined_func_desc (eh);
5962 if (fdh != NULL)
5963 {
5964 /* -mcall-aixdesc code references the dot-symbol on
5965 a call reloc. Mark the function descriptor too
5966 against garbage collection. */
5967 fdh->elf.mark = 1;
5968 if (fdh->elf.is_weakalias)
5969 weakdef (&fdh->elf)->mark = 1;
5970 eh = fdh;
5971 }
5972
5973 /* Function descriptor syms cause the associated
5974 function code sym section to be marked. */
5975 fh = defined_code_entry (eh);
5976 if (fh != NULL)
5977 {
5978 /* They also mark their opd section. */
5979 eh->elf.root.u.def.section->gc_mark = 1;
5980
5981 rsec = fh->elf.root.u.def.section;
5982 }
5983 else if (get_opd_info (eh->elf.root.u.def.section) != NULL
5984 && opd_entry_value (eh->elf.root.u.def.section,
5985 eh->elf.root.u.def.value,
5986 &rsec, NULL, false) != (bfd_vma) -1)
5987 eh->elf.root.u.def.section->gc_mark = 1;
5988 else
5989 rsec = h->root.u.def.section;
5990 break;
5991
5992 case bfd_link_hash_common:
5993 rsec = h->root.u.c.p->section;
5994 break;
5995
5996 default:
5997 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
5998 }
5999 }
6000 }
6001 else
6002 {
6003 struct _opd_sec_data *opd;
6004
6005 rsec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
6006 opd = get_opd_info (rsec);
6007 if (opd != NULL && opd->func_sec != NULL)
6008 {
6009 rsec->gc_mark = 1;
6010
6011 rsec = opd->func_sec[OPD_NDX (sym->st_value + rel->r_addend)];
6012 }
6013 }
6014
6015 return rsec;
6016 }
6017
6018 /* The maximum size of .sfpr. */
6019 #define SFPR_MAX (218*4)
6020
6021 struct sfpr_def_parms
6022 {
6023 const char name[12];
6024 unsigned char lo, hi;
6025 bfd_byte *(*write_ent) (bfd *, bfd_byte *, int);
6026 bfd_byte *(*write_tail) (bfd *, bfd_byte *, int);
6027 };
6028
6029 /* Auto-generate _save*, _rest* functions in .sfpr.
6030 If STUB_SEC is non-null, define alias symbols in STUB_SEC
6031 instead. */
6032
6033 static bool
6034 sfpr_define (struct bfd_link_info *info,
6035 const struct sfpr_def_parms *parm,
6036 asection *stub_sec)
6037 {
6038 struct ppc_link_hash_table *htab = ppc_hash_table (info);
6039 unsigned int i;
6040 size_t len = strlen (parm->name);
6041 bool writing = false;
6042 char sym[16];
6043
6044 if (htab == NULL)
6045 return false;
6046
6047 memcpy (sym, parm->name, len);
6048 sym[len + 2] = 0;
6049
6050 for (i = parm->lo; i <= parm->hi; i++)
6051 {
6052 struct ppc_link_hash_entry *h;
6053
6054 sym[len + 0] = i / 10 + '0';
6055 sym[len + 1] = i % 10 + '0';
6056 h = ppc_elf_hash_entry (elf_link_hash_lookup (&htab->elf, sym,
6057 writing, true, true));
6058 if (stub_sec != NULL)
6059 {
6060 if (h != NULL
6061 && h->elf.root.type == bfd_link_hash_defined
6062 && h->elf.root.u.def.section == htab->sfpr)
6063 {
6064 struct elf_link_hash_entry *s;
6065 char buf[32];
6066 sprintf (buf, "%08x.%s", stub_sec->id & 0xffffffff, sym);
6067 s = elf_link_hash_lookup (&htab->elf, buf, true, true, false);
6068 if (s == NULL)
6069 return false;
6070 if (s->root.type == bfd_link_hash_new)
6071 {
6072 s->root.type = bfd_link_hash_defined;
6073 s->root.u.def.section = stub_sec;
6074 s->root.u.def.value = (stub_sec->size - htab->sfpr->size
6075 + h->elf.root.u.def.value);
6076 s->ref_regular = 1;
6077 s->def_regular = 1;
6078 s->ref_regular_nonweak = 1;
6079 s->forced_local = 1;
6080 s->non_elf = 0;
6081 s->root.linker_def = 1;
6082 }
6083 }
6084 continue;
6085 }
6086 if (h != NULL)
6087 {
6088 h->save_res = 1;
6089 if (!h->elf.def_regular)
6090 {
6091 h->elf.root.type = bfd_link_hash_defined;
6092 h->elf.root.u.def.section = htab->sfpr;
6093 h->elf.root.u.def.value = htab->sfpr->size;
6094 h->elf.type = STT_FUNC;
6095 h->elf.def_regular = 1;
6096 h->elf.non_elf = 0;
6097 _bfd_elf_link_hash_hide_symbol (info, &h->elf, true);
6098 writing = true;
6099 if (htab->sfpr->contents == NULL)
6100 {
6101 htab->sfpr->contents
6102 = bfd_alloc (htab->elf.dynobj, SFPR_MAX);
6103 if (htab->sfpr->contents == NULL)
6104 return false;
6105 }
6106 }
6107 }
6108 if (writing)
6109 {
6110 bfd_byte *p = htab->sfpr->contents + htab->sfpr->size;
6111 if (i != parm->hi)
6112 p = (*parm->write_ent) (htab->elf.dynobj, p, i);
6113 else
6114 p = (*parm->write_tail) (htab->elf.dynobj, p, i);
6115 htab->sfpr->size = p - htab->sfpr->contents;
6116 }
6117 }
6118
6119 return true;
6120 }
6121
6122 static bfd_byte *
6123 savegpr0 (bfd *abfd, bfd_byte *p, int r)
6124 {
6125 bfd_put_32 (abfd, STD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6126 return p + 4;
6127 }
6128
6129 static bfd_byte *
6130 savegpr0_tail (bfd *abfd, bfd_byte *p, int r)
6131 {
6132 p = savegpr0 (abfd, p, r);
6133 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6134 p = p + 4;
6135 bfd_put_32 (abfd, BLR, p);
6136 return p + 4;
6137 }
6138
6139 static bfd_byte *
6140 restgpr0 (bfd *abfd, bfd_byte *p, int r)
6141 {
6142 bfd_put_32 (abfd, LD_R0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6143 return p + 4;
6144 }
6145
6146 static bfd_byte *
6147 restgpr0_tail (bfd *abfd, bfd_byte *p, int r)
6148 {
6149 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6150 p = p + 4;
6151 p = restgpr0 (abfd, p, r);
6152 bfd_put_32 (abfd, MTLR_R0, p);
6153 p = p + 4;
6154 if (r == 29)
6155 {
6156 p = restgpr0 (abfd, p, 30);
6157 p = restgpr0 (abfd, p, 31);
6158 }
6159 bfd_put_32 (abfd, BLR, p);
6160 return p + 4;
6161 }
6162
6163 static bfd_byte *
6164 savegpr1 (bfd *abfd, bfd_byte *p, int r)
6165 {
6166 bfd_put_32 (abfd, STD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6167 return p + 4;
6168 }
6169
6170 static bfd_byte *
6171 savegpr1_tail (bfd *abfd, bfd_byte *p, int r)
6172 {
6173 p = savegpr1 (abfd, p, r);
6174 bfd_put_32 (abfd, BLR, p);
6175 return p + 4;
6176 }
6177
6178 static bfd_byte *
6179 restgpr1 (bfd *abfd, bfd_byte *p, int r)
6180 {
6181 bfd_put_32 (abfd, LD_R0_0R12 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6182 return p + 4;
6183 }
6184
6185 static bfd_byte *
6186 restgpr1_tail (bfd *abfd, bfd_byte *p, int r)
6187 {
6188 p = restgpr1 (abfd, p, r);
6189 bfd_put_32 (abfd, BLR, p);
6190 return p + 4;
6191 }
6192
6193 static bfd_byte *
6194 savefpr (bfd *abfd, bfd_byte *p, int r)
6195 {
6196 bfd_put_32 (abfd, STFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6197 return p + 4;
6198 }
6199
6200 static bfd_byte *
6201 savefpr0_tail (bfd *abfd, bfd_byte *p, int r)
6202 {
6203 p = savefpr (abfd, p, r);
6204 bfd_put_32 (abfd, STD_R0_0R1 + STK_LR, p);
6205 p = p + 4;
6206 bfd_put_32 (abfd, BLR, p);
6207 return p + 4;
6208 }
6209
6210 static bfd_byte *
6211 restfpr (bfd *abfd, bfd_byte *p, int r)
6212 {
6213 bfd_put_32 (abfd, LFD_FR0_0R1 + (r << 21) + (1 << 16) - (32 - r) * 8, p);
6214 return p + 4;
6215 }
6216
6217 static bfd_byte *
6218 restfpr0_tail (bfd *abfd, bfd_byte *p, int r)
6219 {
6220 bfd_put_32 (abfd, LD_R0_0R1 + STK_LR, p);
6221 p = p + 4;
6222 p = restfpr (abfd, p, r);
6223 bfd_put_32 (abfd, MTLR_R0, p);
6224 p = p + 4;
6225 if (r == 29)
6226 {
6227 p = restfpr (abfd, p, 30);
6228 p = restfpr (abfd, p, 31);
6229 }
6230 bfd_put_32 (abfd, BLR, p);
6231 return p + 4;
6232 }
6233
6234 static bfd_byte *
6235 savefpr1_tail (bfd *abfd, bfd_byte *p, int r)
6236 {
6237 p = savefpr (abfd, p, r);
6238 bfd_put_32 (abfd, BLR, p);
6239 return p + 4;
6240 }
6241
6242 static bfd_byte *
6243 restfpr1_tail (bfd *abfd, bfd_byte *p, int r)
6244 {
6245 p = restfpr (abfd, p, r);
6246 bfd_put_32 (abfd, BLR, p);
6247 return p + 4;
6248 }
6249
6250 static bfd_byte *
6251 savevr (bfd *abfd, bfd_byte *p, int r)
6252 {
6253 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6254 p = p + 4;
6255 bfd_put_32 (abfd, STVX_VR0_R12_R0 + (r << 21), p);
6256 return p + 4;
6257 }
6258
6259 static bfd_byte *
6260 savevr_tail (bfd *abfd, bfd_byte *p, int r)
6261 {
6262 p = savevr (abfd, p, r);
6263 bfd_put_32 (abfd, BLR, p);
6264 return p + 4;
6265 }
6266
6267 static bfd_byte *
6268 restvr (bfd *abfd, bfd_byte *p, int r)
6269 {
6270 bfd_put_32 (abfd, LI_R12_0 + (1 << 16) - (32 - r) * 16, p);
6271 p = p + 4;
6272 bfd_put_32 (abfd, LVX_VR0_R12_R0 + (r << 21), p);
6273 return p + 4;
6274 }
6275
6276 static bfd_byte *
6277 restvr_tail (bfd *abfd, bfd_byte *p, int r)
6278 {
6279 p = restvr (abfd, p, r);
6280 bfd_put_32 (abfd, BLR, p);
6281 return p + 4;
6282 }
6283
6284 #define STDU_R1_0R1 0xf8210001
6285 #define ADDI_R1_R1 0x38210000
6286
6287 /* Emit prologue of wrapper preserving regs around a call to
6288 __tls_get_addr_opt. */
6289
6290 static bfd_byte *
6291 tls_get_addr_prologue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6292 {
6293 unsigned int i;
6294
6295 bfd_put_32 (obfd, MFLR_R0, p);
6296 p += 4;
6297 bfd_put_32 (obfd, STD_R0_0R1 + 16, p);
6298 p += 4;
6299
6300 if (htab->opd_abi)
6301 {
6302 for (i = 4; i < 12; i++)
6303 {
6304 bfd_put_32 (obfd,
6305 STD_R0_0R1 | i << 21 | (-(13 - i) * 8 & 0xffff), p);
6306 p += 4;
6307 }
6308 bfd_put_32 (obfd, STDU_R1_0R1 | (-128 & 0xffff), p);
6309 p += 4;
6310 }
6311 else
6312 {
6313 for (i = 4; i < 12; i++)
6314 {
6315 bfd_put_32 (obfd,
6316 STD_R0_0R1 | i << 21 | (-(12 - i) * 8 & 0xffff), p);
6317 p += 4;
6318 }
6319 bfd_put_32 (obfd, STDU_R1_0R1 | (-96 & 0xffff), p);
6320 p += 4;
6321 }
6322 return p;
6323 }
6324
6325 /* Emit epilogue of wrapper preserving regs around a call to
6326 __tls_get_addr_opt. */
6327
6328 static bfd_byte *
6329 tls_get_addr_epilogue (bfd *obfd, bfd_byte *p, struct ppc_link_hash_table *htab)
6330 {
6331 unsigned int i;
6332
6333 if (htab->opd_abi)
6334 {
6335 for (i = 4; i < 12; i++)
6336 {
6337 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (128 - (13 - i) * 8), p);
6338 p += 4;
6339 }
6340 bfd_put_32 (obfd, ADDI_R1_R1 | 128, p);
6341 p += 4;
6342 }
6343 else
6344 {
6345 for (i = 4; i < 12; i++)
6346 {
6347 bfd_put_32 (obfd, LD_R0_0R1 | i << 21 | (96 - (12 - i) * 8), p);
6348 p += 4;
6349 }
6350 bfd_put_32 (obfd, ADDI_R1_R1 | 96, p);
6351 p += 4;
6352 }
6353 bfd_put_32 (obfd, LD_R0_0R1 | 16, p);
6354 p += 4;
6355 bfd_put_32 (obfd, MTLR_R0, p);
6356 p += 4;
6357 bfd_put_32 (obfd, BLR, p);
6358 p += 4;
6359 return p;
6360 }
6361
6362 /* Called via elf_link_hash_traverse to transfer dynamic linking
6363 information on function code symbol entries to their corresponding
6364 function descriptor symbol entries. Must not be called twice for
6365 any given code symbol. */
6366
6367 static bool
6368 func_desc_adjust (struct elf_link_hash_entry *h, void *inf)
6369 {
6370 struct bfd_link_info *info;
6371 struct ppc_link_hash_table *htab;
6372 struct ppc_link_hash_entry *fh;
6373 struct ppc_link_hash_entry *fdh;
6374 bool force_local;
6375
6376 fh = ppc_elf_hash_entry (h);
6377 if (fh->elf.root.type == bfd_link_hash_indirect)
6378 return true;
6379
6380 if (!fh->is_func)
6381 return true;
6382
6383 if (fh->elf.root.root.string[0] != '.'
6384 || fh->elf.root.root.string[1] == '\0')
6385 return true;
6386
6387 info = inf;
6388 htab = ppc_hash_table (info);
6389 if (htab == NULL)
6390 return false;
6391
6392 /* Find the corresponding function descriptor symbol. */
6393 fdh = lookup_fdh (fh, htab);
6394
6395 /* Resolve undefined references to dot-symbols as the value
6396 in the function descriptor, if we have one in a regular object.
6397 This is to satisfy cases like ".quad .foo". Calls to functions
6398 in dynamic objects are handled elsewhere. */
6399 if ((fh->elf.root.type == bfd_link_hash_undefined
6400 || fh->elf.root.type == bfd_link_hash_undefweak)
6401 && (fdh->elf.root.type == bfd_link_hash_defined
6402 || fdh->elf.root.type == bfd_link_hash_defweak)
6403 && get_opd_info (fdh->elf.root.u.def.section) != NULL
6404 && opd_entry_value (fdh->elf.root.u.def.section,
6405 fdh->elf.root.u.def.value,
6406 &fh->elf.root.u.def.section,
6407 &fh->elf.root.u.def.value, false) != (bfd_vma) -1)
6408 {
6409 fh->elf.root.type = fdh->elf.root.type;
6410 fh->elf.forced_local = 1;
6411 fh->elf.def_regular = fdh->elf.def_regular;
6412 fh->elf.def_dynamic = fdh->elf.def_dynamic;
6413 }
6414
6415 if (!fh->elf.dynamic)
6416 {
6417 struct plt_entry *ent;
6418
6419 for (ent = fh->elf.plt.plist; ent != NULL; ent = ent->next)
6420 if (ent->plt.refcount > 0)
6421 break;
6422 if (ent == NULL)
6423 {
6424 if (fdh != NULL && fdh->fake)
6425 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6426 return true;
6427 }
6428 }
6429
6430 /* Create a descriptor as undefined if necessary. */
6431 if (fdh == NULL
6432 && !bfd_link_executable (info)
6433 && (fh->elf.root.type == bfd_link_hash_undefined
6434 || fh->elf.root.type == bfd_link_hash_undefweak))
6435 {
6436 fdh = make_fdh (info, fh);
6437 if (fdh == NULL)
6438 return false;
6439 }
6440
6441 /* We can't support overriding of symbols on a fake descriptor. */
6442 if (fdh != NULL
6443 && fdh->fake
6444 && (fh->elf.root.type == bfd_link_hash_defined
6445 || fh->elf.root.type == bfd_link_hash_defweak))
6446 _bfd_elf_link_hash_hide_symbol (info, &fdh->elf, true);
6447
6448 /* Transfer dynamic linking information to the function descriptor. */
6449 if (fdh != NULL)
6450 {
6451 fdh->elf.ref_regular |= fh->elf.ref_regular;
6452 fdh->elf.ref_dynamic |= fh->elf.ref_dynamic;
6453 fdh->elf.ref_regular_nonweak |= fh->elf.ref_regular_nonweak;
6454 fdh->elf.non_got_ref |= fh->elf.non_got_ref;
6455 fdh->elf.dynamic |= fh->elf.dynamic;
6456 fdh->elf.needs_plt |= (fh->elf.needs_plt
6457 || fh->elf.type == STT_FUNC
6458 || fh->elf.type == STT_GNU_IFUNC);
6459 move_plt_plist (fh, fdh);
6460
6461 if (!fdh->elf.forced_local
6462 && fh->elf.dynindx != -1)
6463 if (!bfd_elf_link_record_dynamic_symbol (info, &fdh->elf))
6464 return false;
6465 }
6466
6467 /* Now that the info is on the function descriptor, clear the
6468 function code sym info. Any function code syms for which we
6469 don't have a definition in a regular file, we force local.
6470 This prevents a shared library from exporting syms that have
6471 been imported from another library. Function code syms that
6472 are really in the library we must leave global to prevent the
6473 linker dragging in a definition from a static library. */
6474 force_local = (!fh->elf.def_regular
6475 || fdh == NULL
6476 || !fdh->elf.def_regular
6477 || fdh->elf.forced_local);
6478 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6479
6480 return true;
6481 }
6482
6483 static const struct sfpr_def_parms save_res_funcs[] =
6484 {
6485 { "_savegpr0_", 14, 31, savegpr0, savegpr0_tail },
6486 { "_restgpr0_", 14, 29, restgpr0, restgpr0_tail },
6487 { "_restgpr0_", 30, 31, restgpr0, restgpr0_tail },
6488 { "_savegpr1_", 14, 31, savegpr1, savegpr1_tail },
6489 { "_restgpr1_", 14, 31, restgpr1, restgpr1_tail },
6490 { "_savefpr_", 14, 31, savefpr, savefpr0_tail },
6491 { "_restfpr_", 14, 29, restfpr, restfpr0_tail },
6492 { "_restfpr_", 30, 31, restfpr, restfpr0_tail },
6493 { "._savef", 14, 31, savefpr, savefpr1_tail },
6494 { "._restf", 14, 31, restfpr, restfpr1_tail },
6495 { "_savevr_", 20, 31, savevr, savevr_tail },
6496 { "_restvr_", 20, 31, restvr, restvr_tail }
6497 };
6498
6499 /* Called near the start of bfd_elf_size_dynamic_sections. We use
6500 this hook to a) run the edit functions in this file, b) provide
6501 some gcc support functions, and c) transfer dynamic linking
6502 information gathered so far on function code symbol entries, to
6503 their corresponding function descriptor symbol entries. */
6504
6505 static bool
6506 ppc64_elf_edit (bfd *obfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
6507 {
6508 struct ppc_link_hash_table *htab;
6509
6510 htab = ppc_hash_table (info);
6511 if (htab == NULL)
6512 return false;
6513
6514 /* Call back into the linker, which then runs the edit functions. */
6515 htab->params->edit ();
6516
6517 /* Provide any missing _save* and _rest* functions. */
6518 if (htab->sfpr != NULL)
6519 {
6520 unsigned int i;
6521
6522 htab->sfpr->size = 0;
6523 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
6524 if (!sfpr_define (info, &save_res_funcs[i], NULL))
6525 return false;
6526 if (htab->sfpr->size == 0)
6527 htab->sfpr->flags |= SEC_EXCLUDE;
6528 }
6529
6530 if (bfd_link_relocatable (info))
6531 return true;
6532
6533 if (htab->elf.hgot != NULL)
6534 {
6535 _bfd_elf_link_hash_hide_symbol (info, htab->elf.hgot, true);
6536 /* Make .TOC. defined so as to prevent it being made dynamic.
6537 The wrong value here is fixed later in ppc64_elf_set_toc. */
6538 if (!htab->elf.hgot->def_regular
6539 || htab->elf.hgot->root.type != bfd_link_hash_defined)
6540 {
6541 htab->elf.hgot->root.type = bfd_link_hash_defined;
6542 htab->elf.hgot->root.u.def.value = 0;
6543 htab->elf.hgot->root.u.def.section = bfd_abs_section_ptr;
6544 htab->elf.hgot->def_regular = 1;
6545 htab->elf.hgot->root.linker_def = 1;
6546 }
6547 htab->elf.hgot->type = STT_OBJECT;
6548 htab->elf.hgot->other
6549 = (htab->elf.hgot->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
6550 }
6551
6552 return true;
6553 }
6554
6555 /* Return true if we have dynamic relocs against H or any of its weak
6556 aliases, that apply to read-only sections. Cannot be used after
6557 size_dynamic_sections. */
6558
6559 static bool
6560 alias_readonly_dynrelocs (struct elf_link_hash_entry *h)
6561 {
6562 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
6563 do
6564 {
6565 if (_bfd_elf_readonly_dynrelocs (&eh->elf))
6566 return true;
6567 eh = ppc_elf_hash_entry (eh->elf.u.alias);
6568 }
6569 while (eh != NULL && &eh->elf != h);
6570
6571 return false;
6572 }
6573
6574 /* Return whether EH has pc-relative dynamic relocs. */
6575
6576 static bool
6577 pc_dynrelocs (struct ppc_link_hash_entry *eh)
6578 {
6579 struct elf_dyn_relocs *p;
6580
6581 for (p = eh->elf.dyn_relocs; p != NULL; p = p->next)
6582 if (p->pc_count != 0)
6583 return true;
6584 return false;
6585 }
6586
6587 /* Return true if a global entry stub will be created for H. Valid
6588 for ELFv2 before plt entries have been allocated. */
6589
6590 static bool
6591 global_entry_stub (struct elf_link_hash_entry *h)
6592 {
6593 struct plt_entry *pent;
6594
6595 if (!h->pointer_equality_needed
6596 || h->def_regular)
6597 return false;
6598
6599 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
6600 if (pent->plt.refcount > 0
6601 && pent->addend == 0)
6602 return true;
6603
6604 return false;
6605 }
6606
6607 /* Adjust a symbol defined by a dynamic object and referenced by a
6608 regular object. The current definition is in some section of the
6609 dynamic object, but we're not including those sections. We have to
6610 change the definition to something the rest of the link can
6611 understand. */
6612
6613 static bool
6614 ppc64_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
6615 struct elf_link_hash_entry *h)
6616 {
6617 struct ppc_link_hash_table *htab;
6618 asection *s, *srel;
6619
6620 htab = ppc_hash_table (info);
6621 if (htab == NULL)
6622 return false;
6623
6624 /* Deal with function syms. */
6625 if (h->type == STT_FUNC
6626 || h->type == STT_GNU_IFUNC
6627 || h->needs_plt)
6628 {
6629 bool local = (ppc_elf_hash_entry (h)->save_res
6630 || SYMBOL_CALLS_LOCAL (info, h)
6631 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
6632 /* Discard dyn_relocs when non-pic if we've decided that a
6633 function symbol is local and not an ifunc. We keep dynamic
6634 relocs for ifuncs when local rather than always emitting a
6635 plt call stub for them and defining the symbol on the call
6636 stub. We can't do that for ELFv1 anyway (a function symbol
6637 is defined on a descriptor, not code) and it can be faster at
6638 run-time due to not needing to bounce through a stub. The
6639 dyn_relocs for ifuncs will be applied even in a static
6640 executable. */
6641 if (!bfd_link_pic (info)
6642 && h->type != STT_GNU_IFUNC
6643 && local)
6644 h->dyn_relocs = NULL;
6645
6646 /* Clear procedure linkage table information for any symbol that
6647 won't need a .plt entry. */
6648 struct plt_entry *ent;
6649 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
6650 if (ent->plt.refcount > 0)
6651 break;
6652 if (ent == NULL
6653 || (h->type != STT_GNU_IFUNC
6654 && local
6655 && (htab->can_convert_all_inline_plt
6656 || (ppc_elf_hash_entry (h)->tls_mask
6657 & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)))
6658 {
6659 h->plt.plist = NULL;
6660 h->needs_plt = 0;
6661 h->pointer_equality_needed = 0;
6662 }
6663 else if (abiversion (info->output_bfd) >= 2)
6664 {
6665 /* Taking a function's address in a read/write section
6666 doesn't require us to define the function symbol in the
6667 executable on a global entry stub. A dynamic reloc can
6668 be used instead. The reason we prefer a few more dynamic
6669 relocs is that calling via a global entry stub costs a
6670 few more instructions, and pointer_equality_needed causes
6671 extra work in ld.so when resolving these symbols. */
6672 if (global_entry_stub (h))
6673 {
6674 if (!_bfd_elf_readonly_dynrelocs (h))
6675 {
6676 h->pointer_equality_needed = 0;
6677 /* If we haven't seen a branch reloc and the symbol
6678 isn't an ifunc then we don't need a plt entry. */
6679 if (!h->needs_plt)
6680 h->plt.plist = NULL;
6681 }
6682 else if (!bfd_link_pic (info))
6683 /* We are going to be defining the function symbol on the
6684 plt stub, so no dyn_relocs needed when non-pic. */
6685 h->dyn_relocs = NULL;
6686 }
6687
6688 /* ELFv2 function symbols can't have copy relocs. */
6689 return true;
6690 }
6691 else if (!h->needs_plt
6692 && !_bfd_elf_readonly_dynrelocs (h))
6693 {
6694 /* If we haven't seen a branch reloc and the symbol isn't an
6695 ifunc then we don't need a plt entry. */
6696 h->plt.plist = NULL;
6697 h->pointer_equality_needed = 0;
6698 return true;
6699 }
6700 }
6701 else
6702 h->plt.plist = NULL;
6703
6704 /* If this is a weak symbol, and there is a real definition, the
6705 processor independent code will have arranged for us to see the
6706 real definition first, and we can just use the same value. */
6707 if (h->is_weakalias)
6708 {
6709 struct elf_link_hash_entry *def = weakdef (h);
6710 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
6711 h->root.u.def.section = def->root.u.def.section;
6712 h->root.u.def.value = def->root.u.def.value;
6713 if (def->root.u.def.section == htab->elf.sdynbss
6714 || def->root.u.def.section == htab->elf.sdynrelro)
6715 h->dyn_relocs = NULL;
6716 return true;
6717 }
6718
6719 /* If we are creating a shared library, we must presume that the
6720 only references to the symbol are via the global offset table.
6721 For such cases we need not do anything here; the relocations will
6722 be handled correctly by relocate_section. */
6723 if (!bfd_link_executable (info))
6724 return true;
6725
6726 /* If there are no references to this symbol that do not use the
6727 GOT, we don't need to generate a copy reloc. */
6728 if (!h->non_got_ref)
6729 return true;
6730
6731 /* Don't generate a copy reloc for symbols defined in the executable. */
6732 if (!h->def_dynamic || !h->ref_regular || h->def_regular
6733
6734 /* If -z nocopyreloc was given, don't generate them either. */
6735 || info->nocopyreloc
6736
6737 /* If we don't find any dynamic relocs in read-only sections, then
6738 we'll be keeping the dynamic relocs and avoiding the copy reloc. */
6739 || (ELIMINATE_COPY_RELOCS
6740 && !h->needs_copy
6741 && !alias_readonly_dynrelocs (h))
6742
6743 /* Protected variables do not work with .dynbss. The copy in
6744 .dynbss won't be used by the shared library with the protected
6745 definition for the variable. Text relocations are preferable
6746 to an incorrect program. */
6747 || h->protected_def)
6748 return true;
6749
6750 if (h->type == STT_FUNC
6751 || h->type == STT_GNU_IFUNC)
6752 {
6753 /* .dynbss copies of function symbols only work if we have
6754 ELFv1 dot-symbols. ELFv1 compilers since 2004 default to not
6755 use dot-symbols and set the function symbol size to the text
6756 size of the function rather than the size of the descriptor.
6757 That's wrong for copying a descriptor. */
6758 if (ppc_elf_hash_entry (h)->oh == NULL
6759 || !(h->size == 24 || h->size == 16))
6760 return true;
6761
6762 /* We should never get here, but unfortunately there are old
6763 versions of gcc (circa gcc-3.2) that improperly for the
6764 ELFv1 ABI put initialized function pointers, vtable refs and
6765 suchlike in read-only sections. Allow them to proceed, but
6766 warn that this might break at runtime. */
6767 info->callbacks->einfo
6768 (_("%P: copy reloc against `%pT' requires lazy plt linking; "
6769 "avoid setting LD_BIND_NOW=1 or upgrade gcc\n"),
6770 h->root.root.string);
6771 }
6772
6773 /* This is a reference to a symbol defined by a dynamic object which
6774 is not a function. */
6775
6776 /* We must allocate the symbol in our .dynbss section, which will
6777 become part of the .bss section of the executable. There will be
6778 an entry for this symbol in the .dynsym section. The dynamic
6779 object will contain position independent code, so all references
6780 from the dynamic object to this symbol will go through the global
6781 offset table. The dynamic linker will use the .dynsym entry to
6782 determine the address it must put in the global offset table, so
6783 both the dynamic object and the regular object will refer to the
6784 same memory location for the variable. */
6785 if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
6786 {
6787 s = htab->elf.sdynrelro;
6788 srel = htab->elf.sreldynrelro;
6789 }
6790 else
6791 {
6792 s = htab->elf.sdynbss;
6793 srel = htab->elf.srelbss;
6794 }
6795 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
6796 {
6797 /* We must generate a R_PPC64_COPY reloc to tell the dynamic
6798 linker to copy the initial value out of the dynamic object
6799 and into the runtime process image. */
6800 srel->size += sizeof (Elf64_External_Rela);
6801 h->needs_copy = 1;
6802 }
6803
6804 /* We no longer want dyn_relocs. */
6805 h->dyn_relocs = NULL;
6806 return _bfd_elf_adjust_dynamic_copy (info, h, s);
6807 }
6808
6809 /* If given a function descriptor symbol, hide both the function code
6810 sym and the descriptor. */
6811 static void
6812 ppc64_elf_hide_symbol (struct bfd_link_info *info,
6813 struct elf_link_hash_entry *h,
6814 bool force_local)
6815 {
6816 struct ppc_link_hash_entry *eh;
6817 _bfd_elf_link_hash_hide_symbol (info, h, force_local);
6818
6819 if (ppc_hash_table (info) == NULL)
6820 return;
6821
6822 eh = ppc_elf_hash_entry (h);
6823 if (eh->is_func_descriptor)
6824 {
6825 struct ppc_link_hash_entry *fh = eh->oh;
6826
6827 if (fh == NULL)
6828 {
6829 const char *p, *q;
6830 struct elf_link_hash_table *htab = elf_hash_table (info);
6831 char save;
6832
6833 /* We aren't supposed to use alloca in BFD because on
6834 systems which do not have alloca the version in libiberty
6835 calls xmalloc, which might cause the program to crash
6836 when it runs out of memory. This function doesn't have a
6837 return status, so there's no way to gracefully return an
6838 error. So cheat. We know that string[-1] can be safely
6839 accessed; It's either a string in an ELF string table,
6840 or allocated in an objalloc structure. */
6841
6842 p = eh->elf.root.root.string - 1;
6843 save = *p;
6844 *(char *) p = '.';
6845 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6846 false, false));
6847 *(char *) p = save;
6848
6849 /* Unfortunately, if it so happens that the string we were
6850 looking for was allocated immediately before this string,
6851 then we overwrote the string terminator. That's the only
6852 reason the lookup should fail. */
6853 if (fh == NULL)
6854 {
6855 q = eh->elf.root.root.string + strlen (eh->elf.root.root.string);
6856 while (q >= eh->elf.root.root.string && *q == *p)
6857 --q, --p;
6858 if (q < eh->elf.root.root.string && *p == '.')
6859 fh = ppc_elf_hash_entry (elf_link_hash_lookup (htab, p, false,
6860 false, false));
6861 }
6862 if (fh != NULL)
6863 {
6864 eh->oh = fh;
6865 fh->oh = eh;
6866 }
6867 }
6868 if (fh != NULL)
6869 _bfd_elf_link_hash_hide_symbol (info, &fh->elf, force_local);
6870 }
6871 }
6872
6873 static bool
6874 get_sym_h (struct elf_link_hash_entry **hp,
6875 Elf_Internal_Sym **symp,
6876 asection **symsecp,
6877 unsigned char **tls_maskp,
6878 Elf_Internal_Sym **locsymsp,
6879 unsigned long r_symndx,
6880 bfd *ibfd)
6881 {
6882 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
6883
6884 if (r_symndx >= symtab_hdr->sh_info)
6885 {
6886 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
6887 struct elf_link_hash_entry *h;
6888
6889 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6890 h = elf_follow_link (h);
6891
6892 if (hp != NULL)
6893 *hp = h;
6894
6895 if (symp != NULL)
6896 *symp = NULL;
6897
6898 if (symsecp != NULL)
6899 {
6900 asection *symsec = NULL;
6901 if (h->root.type == bfd_link_hash_defined
6902 || h->root.type == bfd_link_hash_defweak)
6903 symsec = h->root.u.def.section;
6904 *symsecp = symsec;
6905 }
6906
6907 if (tls_maskp != NULL)
6908 *tls_maskp = &ppc_elf_hash_entry (h)->tls_mask;
6909 }
6910 else
6911 {
6912 Elf_Internal_Sym *sym;
6913 Elf_Internal_Sym *locsyms = *locsymsp;
6914
6915 if (locsyms == NULL)
6916 {
6917 locsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
6918 if (locsyms == NULL)
6919 locsyms = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
6920 symtab_hdr->sh_info,
6921 0, NULL, NULL, NULL);
6922 if (locsyms == NULL)
6923 return false;
6924 *locsymsp = locsyms;
6925 }
6926 sym = locsyms + r_symndx;
6927
6928 if (hp != NULL)
6929 *hp = NULL;
6930
6931 if (symp != NULL)
6932 *symp = sym;
6933
6934 if (symsecp != NULL)
6935 *symsecp = bfd_section_from_elf_index (ibfd, sym->st_shndx);
6936
6937 if (tls_maskp != NULL)
6938 {
6939 struct got_entry **lgot_ents;
6940 unsigned char *tls_mask;
6941
6942 tls_mask = NULL;
6943 lgot_ents = elf_local_got_ents (ibfd);
6944 if (lgot_ents != NULL)
6945 {
6946 struct plt_entry **local_plt = (struct plt_entry **)
6947 (lgot_ents + symtab_hdr->sh_info);
6948 unsigned char *lgot_masks = (unsigned char *)
6949 (local_plt + symtab_hdr->sh_info);
6950 tls_mask = &lgot_masks[r_symndx];
6951 }
6952 *tls_maskp = tls_mask;
6953 }
6954 }
6955 return true;
6956 }
6957
6958 /* Returns TLS_MASKP for the given REL symbol. Function return is 0 on
6959 error, 2 on a toc GD type suitable for optimization, 3 on a toc LD
6960 type suitable for optimization, and 1 otherwise. */
6961
6962 static int
6963 get_tls_mask (unsigned char **tls_maskp,
6964 unsigned long *toc_symndx,
6965 bfd_vma *toc_addend,
6966 Elf_Internal_Sym **locsymsp,
6967 const Elf_Internal_Rela *rel,
6968 bfd *ibfd)
6969 {
6970 unsigned long r_symndx;
6971 int next_r;
6972 struct elf_link_hash_entry *h;
6973 Elf_Internal_Sym *sym;
6974 asection *sec;
6975 bfd_vma off;
6976
6977 r_symndx = ELF64_R_SYM (rel->r_info);
6978 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
6979 return 0;
6980
6981 if ((*tls_maskp != NULL
6982 && (**tls_maskp & TLS_TLS) != 0
6983 && **tls_maskp != (TLS_TLS | TLS_MARK))
6984 || sec == NULL
6985 || ppc64_elf_section_data (sec) == NULL
6986 || ppc64_elf_section_data (sec)->sec_type != sec_toc)
6987 return 1;
6988
6989 /* Look inside a TOC section too. */
6990 if (h != NULL)
6991 {
6992 BFD_ASSERT (h->root.type == bfd_link_hash_defined);
6993 off = h->root.u.def.value;
6994 }
6995 else
6996 off = sym->st_value;
6997 off += rel->r_addend;
6998 BFD_ASSERT (off % 8 == 0);
6999 r_symndx = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8];
7000 next_r = ppc64_elf_section_data (sec)->u.toc.symndx[off / 8 + 1];
7001 if (toc_symndx != NULL)
7002 *toc_symndx = r_symndx;
7003 if (toc_addend != NULL)
7004 *toc_addend = ppc64_elf_section_data (sec)->u.toc.add[off / 8];
7005 if (!get_sym_h (&h, &sym, &sec, tls_maskp, locsymsp, r_symndx, ibfd))
7006 return 0;
7007 if ((h == NULL || is_static_defined (h))
7008 && (next_r == -1 || next_r == -2))
7009 return 1 - next_r;
7010 return 1;
7011 }
7012
7013 /* Find (or create) an entry in the tocsave hash table. */
7014
7015 static struct tocsave_entry *
7016 tocsave_find (struct ppc_link_hash_table *htab,
7017 enum insert_option insert,
7018 Elf_Internal_Sym **local_syms,
7019 const Elf_Internal_Rela *irela,
7020 bfd *ibfd)
7021 {
7022 unsigned long r_indx;
7023 struct elf_link_hash_entry *h;
7024 Elf_Internal_Sym *sym;
7025 struct tocsave_entry ent, *p;
7026 hashval_t hash;
7027 struct tocsave_entry **slot;
7028
7029 r_indx = ELF64_R_SYM (irela->r_info);
7030 if (!get_sym_h (&h, &sym, &ent.sec, NULL, local_syms, r_indx, ibfd))
7031 return NULL;
7032 if (ent.sec == NULL || ent.sec->output_section == NULL)
7033 {
7034 _bfd_error_handler
7035 (_("%pB: undefined symbol on R_PPC64_TOCSAVE relocation"), ibfd);
7036 return NULL;
7037 }
7038
7039 if (h != NULL)
7040 ent.offset = h->root.u.def.value;
7041 else
7042 ent.offset = sym->st_value;
7043 ent.offset += irela->r_addend;
7044
7045 hash = tocsave_htab_hash (&ent);
7046 slot = ((struct tocsave_entry **)
7047 htab_find_slot_with_hash (htab->tocsave_htab, &ent, hash, insert));
7048 if (slot == NULL)
7049 return NULL;
7050
7051 if (*slot == NULL)
7052 {
7053 p = (struct tocsave_entry *) bfd_alloc (ibfd, sizeof (*p));
7054 if (p == NULL)
7055 return NULL;
7056 *p = ent;
7057 *slot = p;
7058 }
7059 return *slot;
7060 }
7061
7062 /* Adjust all global syms defined in opd sections. In gcc generated
7063 code for the old ABI, these will already have been done. */
7064
7065 static bool
7066 adjust_opd_syms (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
7067 {
7068 struct ppc_link_hash_entry *eh;
7069 asection *sym_sec;
7070 struct _opd_sec_data *opd;
7071
7072 if (h->root.type == bfd_link_hash_indirect)
7073 return true;
7074
7075 if (h->root.type != bfd_link_hash_defined
7076 && h->root.type != bfd_link_hash_defweak)
7077 return true;
7078
7079 eh = ppc_elf_hash_entry (h);
7080 if (eh->adjust_done)
7081 return true;
7082
7083 sym_sec = eh->elf.root.u.def.section;
7084 opd = get_opd_info (sym_sec);
7085 if (opd != NULL && opd->adjust != NULL)
7086 {
7087 long adjust = opd->adjust[OPD_NDX (eh->elf.root.u.def.value)];
7088 if (adjust == -1)
7089 {
7090 /* This entry has been deleted. */
7091 asection *dsec = ppc64_elf_tdata (sym_sec->owner)->deleted_section;
7092 if (dsec == NULL)
7093 {
7094 for (dsec = sym_sec->owner->sections; dsec; dsec = dsec->next)
7095 if (discarded_section (dsec))
7096 {
7097 ppc64_elf_tdata (sym_sec->owner)->deleted_section = dsec;
7098 break;
7099 }
7100 }
7101 eh->elf.root.u.def.value = 0;
7102 eh->elf.root.u.def.section = dsec;
7103 }
7104 else
7105 eh->elf.root.u.def.value += adjust;
7106 eh->adjust_done = 1;
7107 }
7108 return true;
7109 }
7110
7111 /* Handles decrementing dynamic reloc counts for the reloc specified by
7112 R_INFO in section SEC. If LOCAL_SYMS is NULL, then H and SYM
7113 have already been determined. */
7114
7115 static bool
7116 dec_dynrel_count (bfd_vma r_info,
7117 asection *sec,
7118 struct bfd_link_info *info,
7119 Elf_Internal_Sym **local_syms,
7120 struct elf_link_hash_entry *h,
7121 Elf_Internal_Sym *sym)
7122 {
7123 enum elf_ppc64_reloc_type r_type;
7124 asection *sym_sec = NULL;
7125
7126 /* Can this reloc be dynamic? This switch, and later tests here
7127 should be kept in sync with the code in check_relocs. */
7128 r_type = ELF64_R_TYPE (r_info);
7129 switch (r_type)
7130 {
7131 default:
7132 return true;
7133
7134 case R_PPC64_TOC16:
7135 case R_PPC64_TOC16_DS:
7136 case R_PPC64_TOC16_LO:
7137 case R_PPC64_TOC16_HI:
7138 case R_PPC64_TOC16_HA:
7139 case R_PPC64_TOC16_LO_DS:
7140 if (h == NULL)
7141 return true;
7142 break;
7143
7144 case R_PPC64_TPREL16:
7145 case R_PPC64_TPREL16_LO:
7146 case R_PPC64_TPREL16_HI:
7147 case R_PPC64_TPREL16_HA:
7148 case R_PPC64_TPREL16_DS:
7149 case R_PPC64_TPREL16_LO_DS:
7150 case R_PPC64_TPREL16_HIGH:
7151 case R_PPC64_TPREL16_HIGHA:
7152 case R_PPC64_TPREL16_HIGHER:
7153 case R_PPC64_TPREL16_HIGHERA:
7154 case R_PPC64_TPREL16_HIGHEST:
7155 case R_PPC64_TPREL16_HIGHESTA:
7156 case R_PPC64_TPREL64:
7157 case R_PPC64_TPREL34:
7158 case R_PPC64_DTPMOD64:
7159 case R_PPC64_DTPREL64:
7160 case R_PPC64_ADDR64:
7161 case R_PPC64_REL30:
7162 case R_PPC64_REL32:
7163 case R_PPC64_REL64:
7164 case R_PPC64_ADDR14:
7165 case R_PPC64_ADDR14_BRNTAKEN:
7166 case R_PPC64_ADDR14_BRTAKEN:
7167 case R_PPC64_ADDR16:
7168 case R_PPC64_ADDR16_DS:
7169 case R_PPC64_ADDR16_HA:
7170 case R_PPC64_ADDR16_HI:
7171 case R_PPC64_ADDR16_HIGH:
7172 case R_PPC64_ADDR16_HIGHA:
7173 case R_PPC64_ADDR16_HIGHER:
7174 case R_PPC64_ADDR16_HIGHERA:
7175 case R_PPC64_ADDR16_HIGHEST:
7176 case R_PPC64_ADDR16_HIGHESTA:
7177 case R_PPC64_ADDR16_LO:
7178 case R_PPC64_ADDR16_LO_DS:
7179 case R_PPC64_ADDR24:
7180 case R_PPC64_ADDR32:
7181 case R_PPC64_UADDR16:
7182 case R_PPC64_UADDR32:
7183 case R_PPC64_UADDR64:
7184 case R_PPC64_TOC:
7185 case R_PPC64_D34:
7186 case R_PPC64_D34_LO:
7187 case R_PPC64_D34_HI30:
7188 case R_PPC64_D34_HA30:
7189 case R_PPC64_ADDR16_HIGHER34:
7190 case R_PPC64_ADDR16_HIGHERA34:
7191 case R_PPC64_ADDR16_HIGHEST34:
7192 case R_PPC64_ADDR16_HIGHESTA34:
7193 case R_PPC64_D28:
7194 break;
7195 }
7196
7197 if (local_syms != NULL)
7198 {
7199 unsigned long r_symndx;
7200 bfd *ibfd = sec->owner;
7201
7202 r_symndx = ELF64_R_SYM (r_info);
7203 if (!get_sym_h (&h, &sym, &sym_sec, NULL, local_syms, r_symndx, ibfd))
7204 return false;
7205 }
7206
7207 if ((h != NULL
7208 && (h->root.type == bfd_link_hash_defweak
7209 || !h->def_regular))
7210 || (h != NULL
7211 && !bfd_link_executable (info)
7212 && !SYMBOLIC_BIND (info, h))
7213 || (bfd_link_pic (info)
7214 && must_be_dyn_reloc (info, r_type))
7215 || (!bfd_link_pic (info)
7216 && (h != NULL
7217 ? h->type == STT_GNU_IFUNC
7218 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)))
7219 ;
7220 else
7221 return true;
7222
7223 if (h != NULL)
7224 {
7225 struct elf_dyn_relocs *p;
7226 struct elf_dyn_relocs **pp;
7227 pp = &h->dyn_relocs;
7228
7229 /* elf_gc_sweep may have already removed all dyn relocs associated
7230 with local syms for a given section. Also, symbol flags are
7231 changed by elf_gc_sweep_symbol, confusing the test above. Don't
7232 report a dynreloc miscount. */
7233 if (*pp == NULL && info->gc_sections)
7234 return true;
7235
7236 while ((p = *pp) != NULL)
7237 {
7238 if (p->sec == sec)
7239 {
7240 if (!must_be_dyn_reloc (info, r_type))
7241 p->pc_count -= 1;
7242 p->count -= 1;
7243 if (p->count == 0)
7244 *pp = p->next;
7245 return true;
7246 }
7247 pp = &p->next;
7248 }
7249 }
7250 else
7251 {
7252 struct ppc_dyn_relocs *p;
7253 struct ppc_dyn_relocs **pp;
7254 void *vpp;
7255 bool is_ifunc;
7256
7257 if (local_syms == NULL)
7258 sym_sec = bfd_section_from_elf_index (sec->owner, sym->st_shndx);
7259 if (sym_sec == NULL)
7260 sym_sec = sec;
7261
7262 vpp = &elf_section_data (sym_sec)->local_dynrel;
7263 pp = (struct ppc_dyn_relocs **) vpp;
7264
7265 if (*pp == NULL && info->gc_sections)
7266 return true;
7267
7268 is_ifunc = ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC;
7269 while ((p = *pp) != NULL)
7270 {
7271 if (p->sec == sec && p->ifunc == is_ifunc)
7272 {
7273 p->count -= 1;
7274 if (p->count == 0)
7275 *pp = p->next;
7276 return true;
7277 }
7278 pp = &p->next;
7279 }
7280 }
7281
7282 /* xgettext:c-format */
7283 _bfd_error_handler (_("dynreloc miscount for %pB, section %pA"),
7284 sec->owner, sec);
7285 bfd_set_error (bfd_error_bad_value);
7286 return false;
7287 }
7288
7289 /* Remove unused Official Procedure Descriptor entries. Currently we
7290 only remove those associated with functions in discarded link-once
7291 sections, or weakly defined functions that have been overridden. It
7292 would be possible to remove many more entries for statically linked
7293 applications. */
7294
7295 bool
7296 ppc64_elf_edit_opd (struct bfd_link_info *info)
7297 {
7298 bfd *ibfd;
7299 bool some_edited = false;
7300 asection *need_pad = NULL;
7301 struct ppc_link_hash_table *htab;
7302
7303 htab = ppc_hash_table (info);
7304 if (htab == NULL)
7305 return false;
7306
7307 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7308 {
7309 asection *sec;
7310 Elf_Internal_Rela *relstart, *rel, *relend;
7311 Elf_Internal_Shdr *symtab_hdr;
7312 Elf_Internal_Sym *local_syms;
7313 struct _opd_sec_data *opd;
7314 bool need_edit, add_aux_fields, broken;
7315 bfd_size_type cnt_16b = 0;
7316
7317 if (!is_ppc64_elf (ibfd))
7318 continue;
7319
7320 sec = bfd_get_section_by_name (ibfd, ".opd");
7321 if (sec == NULL || sec->size == 0)
7322 continue;
7323
7324 if (sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)
7325 continue;
7326
7327 if (sec->output_section == bfd_abs_section_ptr)
7328 continue;
7329
7330 /* Look through the section relocs. */
7331 if ((sec->flags & SEC_RELOC) == 0 || sec->reloc_count == 0)
7332 continue;
7333
7334 local_syms = NULL;
7335 symtab_hdr = &elf_symtab_hdr (ibfd);
7336
7337 /* Read the relocations. */
7338 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7339 info->keep_memory);
7340 if (relstart == NULL)
7341 return false;
7342
7343 /* First run through the relocs to check they are sane, and to
7344 determine whether we need to edit this opd section. */
7345 need_edit = false;
7346 broken = false;
7347 need_pad = sec;
7348 relend = relstart + sec->reloc_count;
7349 for (rel = relstart; rel < relend; )
7350 {
7351 enum elf_ppc64_reloc_type r_type;
7352 unsigned long r_symndx;
7353 asection *sym_sec;
7354 struct elf_link_hash_entry *h;
7355 Elf_Internal_Sym *sym;
7356 bfd_vma offset;
7357
7358 /* .opd contains an array of 16 or 24 byte entries. We're
7359 only interested in the reloc pointing to a function entry
7360 point. */
7361 offset = rel->r_offset;
7362 if (rel + 1 == relend
7363 || rel[1].r_offset != offset + 8)
7364 {
7365 /* If someone messes with .opd alignment then after a
7366 "ld -r" we might have padding in the middle of .opd.
7367 Also, there's nothing to prevent someone putting
7368 something silly in .opd with the assembler. No .opd
7369 optimization for them! */
7370 broken_opd:
7371 _bfd_error_handler
7372 (_("%pB: .opd is not a regular array of opd entries"), ibfd);
7373 broken = true;
7374 break;
7375 }
7376
7377 if ((r_type = ELF64_R_TYPE (rel->r_info)) != R_PPC64_ADDR64
7378 || (r_type = ELF64_R_TYPE ((rel + 1)->r_info)) != R_PPC64_TOC)
7379 {
7380 _bfd_error_handler
7381 /* xgettext:c-format */
7382 (_("%pB: unexpected reloc type %u in .opd section"),
7383 ibfd, r_type);
7384 broken = true;
7385 break;
7386 }
7387
7388 r_symndx = ELF64_R_SYM (rel->r_info);
7389 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7390 r_symndx, ibfd))
7391 goto error_ret;
7392
7393 if (sym_sec == NULL || sym_sec->owner == NULL)
7394 {
7395 const char *sym_name;
7396 if (h != NULL)
7397 sym_name = h->root.root.string;
7398 else
7399 sym_name = bfd_elf_sym_name (ibfd, symtab_hdr, sym,
7400 sym_sec);
7401
7402 _bfd_error_handler
7403 /* xgettext:c-format */
7404 (_("%pB: undefined sym `%s' in .opd section"),
7405 ibfd, sym_name);
7406 broken = true;
7407 break;
7408 }
7409
7410 /* opd entries are always for functions defined in the
7411 current input bfd. If the symbol isn't defined in the
7412 input bfd, then we won't be using the function in this
7413 bfd; It must be defined in a linkonce section in another
7414 bfd, or is weak. It's also possible that we are
7415 discarding the function due to a linker script /DISCARD/,
7416 which we test for via the output_section. */
7417 if (sym_sec->owner != ibfd
7418 || sym_sec->output_section == bfd_abs_section_ptr)
7419 need_edit = true;
7420
7421 rel += 2;
7422 if (rel + 1 == relend
7423 || (rel + 2 < relend
7424 && ELF64_R_TYPE (rel[2].r_info) == R_PPC64_TOC))
7425 ++rel;
7426
7427 if (rel == relend)
7428 {
7429 if (sec->size == offset + 24)
7430 {
7431 need_pad = NULL;
7432 break;
7433 }
7434 if (sec->size == offset + 16)
7435 {
7436 cnt_16b++;
7437 break;
7438 }
7439 goto broken_opd;
7440 }
7441 else if (rel + 1 < relend
7442 && ELF64_R_TYPE (rel[0].r_info) == R_PPC64_ADDR64
7443 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOC)
7444 {
7445 if (rel[0].r_offset == offset + 16)
7446 cnt_16b++;
7447 else if (rel[0].r_offset != offset + 24)
7448 goto broken_opd;
7449 }
7450 else
7451 goto broken_opd;
7452 }
7453
7454 add_aux_fields = htab->params->non_overlapping_opd && cnt_16b > 0;
7455
7456 if (!broken && (need_edit || add_aux_fields))
7457 {
7458 Elf_Internal_Rela *write_rel;
7459 Elf_Internal_Shdr *rel_hdr;
7460 bfd_byte *rptr, *wptr;
7461 bfd_byte *new_contents;
7462 bfd_size_type amt;
7463
7464 new_contents = NULL;
7465 amt = OPD_NDX (sec->size) * sizeof (long);
7466 opd = &ppc64_elf_section_data (sec)->u.opd;
7467 opd->adjust = bfd_zalloc (sec->owner, amt);
7468 if (opd->adjust == NULL)
7469 return false;
7470
7471 /* This seems a waste of time as input .opd sections are all
7472 zeros as generated by gcc, but I suppose there's no reason
7473 this will always be so. We might start putting something in
7474 the third word of .opd entries. */
7475 if ((sec->flags & SEC_IN_MEMORY) == 0)
7476 {
7477 bfd_byte *loc;
7478 if (!bfd_malloc_and_get_section (ibfd, sec, &loc))
7479 {
7480 free (loc);
7481 error_ret:
7482 if (symtab_hdr->contents != (unsigned char *) local_syms)
7483 free (local_syms);
7484 if (elf_section_data (sec)->relocs != relstart)
7485 free (relstart);
7486 return false;
7487 }
7488 sec->contents = loc;
7489 sec->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7490 }
7491
7492 elf_section_data (sec)->relocs = relstart;
7493
7494 new_contents = sec->contents;
7495 if (add_aux_fields)
7496 {
7497 new_contents = bfd_malloc (sec->size + cnt_16b * 8);
7498 if (new_contents == NULL)
7499 return false;
7500 need_pad = NULL;
7501 }
7502 wptr = new_contents;
7503 rptr = sec->contents;
7504 write_rel = relstart;
7505 for (rel = relstart; rel < relend; )
7506 {
7507 unsigned long r_symndx;
7508 asection *sym_sec;
7509 struct elf_link_hash_entry *h;
7510 struct ppc_link_hash_entry *fdh = NULL;
7511 Elf_Internal_Sym *sym;
7512 long opd_ent_size;
7513 Elf_Internal_Rela *next_rel;
7514 bool skip;
7515
7516 r_symndx = ELF64_R_SYM (rel->r_info);
7517 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7518 r_symndx, ibfd))
7519 goto error_ret;
7520
7521 next_rel = rel + 2;
7522 if (next_rel + 1 == relend
7523 || (next_rel + 2 < relend
7524 && ELF64_R_TYPE (next_rel[2].r_info) == R_PPC64_TOC))
7525 ++next_rel;
7526
7527 /* See if the .opd entry is full 24 byte or
7528 16 byte (with fd_aux entry overlapped with next
7529 fd_func). */
7530 opd_ent_size = 24;
7531 if (next_rel == relend)
7532 {
7533 if (sec->size == rel->r_offset + 16)
7534 opd_ent_size = 16;
7535 }
7536 else if (next_rel->r_offset == rel->r_offset + 16)
7537 opd_ent_size = 16;
7538
7539 if (h != NULL
7540 && h->root.root.string[0] == '.')
7541 {
7542 fdh = ppc_elf_hash_entry (h)->oh;
7543 if (fdh != NULL)
7544 {
7545 fdh = ppc_follow_link (fdh);
7546 if (fdh->elf.root.type != bfd_link_hash_defined
7547 && fdh->elf.root.type != bfd_link_hash_defweak)
7548 fdh = NULL;
7549 }
7550 }
7551
7552 skip = (sym_sec->owner != ibfd
7553 || sym_sec->output_section == bfd_abs_section_ptr);
7554 if (skip)
7555 {
7556 if (fdh != NULL && sym_sec->owner == ibfd)
7557 {
7558 /* Arrange for the function descriptor sym
7559 to be dropped. */
7560 fdh->elf.root.u.def.value = 0;
7561 fdh->elf.root.u.def.section = sym_sec;
7562 }
7563 opd->adjust[OPD_NDX (rel->r_offset)] = -1;
7564
7565 if (NO_OPD_RELOCS || bfd_link_relocatable (info))
7566 rel = next_rel;
7567 else
7568 while (1)
7569 {
7570 if (!dec_dynrel_count (rel->r_info, sec, info,
7571 NULL, h, sym))
7572 goto error_ret;
7573
7574 if (++rel == next_rel)
7575 break;
7576
7577 r_symndx = ELF64_R_SYM (rel->r_info);
7578 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
7579 r_symndx, ibfd))
7580 goto error_ret;
7581 }
7582 }
7583 else
7584 {
7585 /* We'll be keeping this opd entry. */
7586 long adjust;
7587
7588 if (fdh != NULL)
7589 {
7590 /* Redefine the function descriptor symbol to
7591 this location in the opd section. It is
7592 necessary to update the value here rather
7593 than using an array of adjustments as we do
7594 for local symbols, because various places
7595 in the generic ELF code use the value
7596 stored in u.def.value. */
7597 fdh->elf.root.u.def.value = wptr - new_contents;
7598 fdh->adjust_done = 1;
7599 }
7600
7601 /* Local syms are a bit tricky. We could
7602 tweak them as they can be cached, but
7603 we'd need to look through the local syms
7604 for the function descriptor sym which we
7605 don't have at the moment. So keep an
7606 array of adjustments. */
7607 adjust = (wptr - new_contents) - (rptr - sec->contents);
7608 opd->adjust[OPD_NDX (rel->r_offset)] = adjust;
7609
7610 if (wptr != rptr)
7611 memcpy (wptr, rptr, opd_ent_size);
7612 wptr += opd_ent_size;
7613 if (add_aux_fields && opd_ent_size == 16)
7614 {
7615 memset (wptr, '\0', 8);
7616 wptr += 8;
7617 }
7618
7619 /* We need to adjust any reloc offsets to point to the
7620 new opd entries. */
7621 for ( ; rel != next_rel; ++rel)
7622 {
7623 rel->r_offset += adjust;
7624 if (write_rel != rel)
7625 memcpy (write_rel, rel, sizeof (*rel));
7626 ++write_rel;
7627 }
7628 }
7629
7630 rptr += opd_ent_size;
7631 }
7632
7633 sec->size = wptr - new_contents;
7634 sec->reloc_count = write_rel - relstart;
7635 if (add_aux_fields)
7636 {
7637 free (sec->contents);
7638 sec->contents = new_contents;
7639 }
7640
7641 /* Fudge the header size too, as this is used later in
7642 elf_bfd_final_link if we are emitting relocs. */
7643 rel_hdr = _bfd_elf_single_rel_hdr (sec);
7644 rel_hdr->sh_size = sec->reloc_count * rel_hdr->sh_entsize;
7645 some_edited = true;
7646 }
7647 else if (elf_section_data (sec)->relocs != relstart)
7648 free (relstart);
7649
7650 if (local_syms != NULL
7651 && symtab_hdr->contents != (unsigned char *) local_syms)
7652 {
7653 if (!info->keep_memory)
7654 free (local_syms);
7655 else
7656 symtab_hdr->contents = (unsigned char *) local_syms;
7657 }
7658 }
7659
7660 if (some_edited)
7661 elf_link_hash_traverse (elf_hash_table (info), adjust_opd_syms, NULL);
7662
7663 /* If we are doing a final link and the last .opd entry is just 16 byte
7664 long, add a 8 byte padding after it. */
7665 if (need_pad != NULL && !bfd_link_relocatable (info))
7666 {
7667 bfd_byte *p;
7668
7669 if ((need_pad->flags & SEC_IN_MEMORY) == 0)
7670 {
7671 BFD_ASSERT (need_pad->size > 0);
7672
7673 p = bfd_malloc (need_pad->size + 8);
7674 if (p == NULL)
7675 return false;
7676
7677 if (!bfd_get_section_contents (need_pad->owner, need_pad,
7678 p, 0, need_pad->size))
7679 return false;
7680
7681 need_pad->contents = p;
7682 need_pad->flags |= (SEC_IN_MEMORY | SEC_HAS_CONTENTS);
7683 }
7684 else
7685 {
7686 p = bfd_realloc (need_pad->contents, need_pad->size + 8);
7687 if (p == NULL)
7688 return false;
7689
7690 need_pad->contents = p;
7691 }
7692
7693 memset (need_pad->contents + need_pad->size, 0, 8);
7694 need_pad->size += 8;
7695 }
7696
7697 return true;
7698 }
7699
7700 /* Analyze inline PLT call relocations to see whether calls to locally
7701 defined functions can be converted to direct calls. */
7702
7703 bool
7704 ppc64_elf_inline_plt (struct bfd_link_info *info)
7705 {
7706 struct ppc_link_hash_table *htab;
7707 bfd *ibfd;
7708 asection *sec;
7709 bfd_vma low_vma, high_vma, limit;
7710
7711 htab = ppc_hash_table (info);
7712 if (htab == NULL)
7713 return false;
7714
7715 /* A bl insn can reach -0x2000000 to 0x1fffffc. The limit is
7716 reduced somewhat to cater for possible stubs that might be added
7717 between the call and its destination. */
7718 if (htab->params->group_size < 0)
7719 {
7720 limit = -htab->params->group_size;
7721 if (limit == 1)
7722 limit = 0x1e00000;
7723 }
7724 else
7725 {
7726 limit = htab->params->group_size;
7727 if (limit == 1)
7728 limit = 0x1c00000;
7729 }
7730
7731 low_vma = -1;
7732 high_vma = 0;
7733 for (sec = info->output_bfd->sections; sec != NULL; sec = sec->next)
7734 if ((sec->flags & (SEC_ALLOC | SEC_CODE)) == (SEC_ALLOC | SEC_CODE))
7735 {
7736 if (low_vma > sec->vma)
7737 low_vma = sec->vma;
7738 if (high_vma < sec->vma + sec->size)
7739 high_vma = sec->vma + sec->size;
7740 }
7741
7742 /* If a "bl" can reach anywhere in local code sections, then we can
7743 convert all inline PLT sequences to direct calls when the symbol
7744 is local. */
7745 if (high_vma - low_vma < limit)
7746 {
7747 htab->can_convert_all_inline_plt = 1;
7748 return true;
7749 }
7750
7751 /* Otherwise, go looking through relocs for cases where a direct
7752 call won't reach. Mark the symbol on any such reloc to disable
7753 the optimization and keep the PLT entry as it seems likely that
7754 this will be better than creating trampolines. Note that this
7755 will disable the optimization for all inline PLT calls to a
7756 particular symbol, not just those that won't reach. The
7757 difficulty in doing a more precise optimization is that the
7758 linker needs to make a decision depending on whether a
7759 particular R_PPC64_PLTCALL insn can be turned into a direct
7760 call, for each of the R_PPC64_PLTSEQ and R_PPC64_PLT16* insns in
7761 the sequence, and there is nothing that ties those relocs
7762 together except their symbol. */
7763
7764 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
7765 {
7766 Elf_Internal_Shdr *symtab_hdr;
7767 Elf_Internal_Sym *local_syms;
7768
7769 if (!is_ppc64_elf (ibfd))
7770 continue;
7771
7772 local_syms = NULL;
7773 symtab_hdr = &elf_symtab_hdr (ibfd);
7774
7775 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
7776 if (ppc64_elf_section_data (sec)->has_pltcall
7777 && !bfd_is_abs_section (sec->output_section))
7778 {
7779 Elf_Internal_Rela *relstart, *rel, *relend;
7780
7781 /* Read the relocations. */
7782 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
7783 info->keep_memory);
7784 if (relstart == NULL)
7785 return false;
7786
7787 relend = relstart + sec->reloc_count;
7788 for (rel = relstart; rel < relend; rel++)
7789 {
7790 enum elf_ppc64_reloc_type r_type;
7791 unsigned long r_symndx;
7792 asection *sym_sec;
7793 struct elf_link_hash_entry *h;
7794 Elf_Internal_Sym *sym;
7795 unsigned char *tls_maskp;
7796
7797 r_type = ELF64_R_TYPE (rel->r_info);
7798 if (r_type != R_PPC64_PLTCALL
7799 && r_type != R_PPC64_PLTCALL_NOTOC)
7800 continue;
7801
7802 r_symndx = ELF64_R_SYM (rel->r_info);
7803 if (!get_sym_h (&h, &sym, &sym_sec, &tls_maskp, &local_syms,
7804 r_symndx, ibfd))
7805 {
7806 if (elf_section_data (sec)->relocs != relstart)
7807 free (relstart);
7808 if (symtab_hdr->contents != (bfd_byte *) local_syms)
7809 free (local_syms);
7810 return false;
7811 }
7812
7813 if (sym_sec != NULL && sym_sec->output_section != NULL)
7814 {
7815 bfd_vma from, to;
7816 if (h != NULL)
7817 to = h->root.u.def.value;
7818 else
7819 to = sym->st_value;
7820 to += (rel->r_addend
7821 + sym_sec->output_offset
7822 + sym_sec->output_section->vma);
7823 from = (rel->r_offset
7824 + sec->output_offset
7825 + sec->output_section->vma);
7826 if (to - from + limit < 2 * limit
7827 && !(r_type == R_PPC64_PLTCALL_NOTOC
7828 && (((h ? h->other : sym->st_other)
7829 & STO_PPC64_LOCAL_MASK)
7830 > 1 << STO_PPC64_LOCAL_BIT)))
7831 *tls_maskp &= ~PLT_KEEP;
7832 }
7833 }
7834 if (elf_section_data (sec)->relocs != relstart)
7835 free (relstart);
7836 }
7837
7838 if (local_syms != NULL
7839 && symtab_hdr->contents != (unsigned char *) local_syms)
7840 {
7841 if (!info->keep_memory)
7842 free (local_syms);
7843 else
7844 symtab_hdr->contents = (unsigned char *) local_syms;
7845 }
7846 }
7847
7848 return true;
7849 }
7850
7851 /* Set htab->tls_get_addr and various other info specific to TLS.
7852 This needs to run before dynamic symbols are processed in
7853 bfd_elf_size_dynamic_sections. */
7854
7855 bool
7856 ppc64_elf_tls_setup (struct bfd_link_info *info)
7857 {
7858 struct ppc_link_hash_table *htab;
7859 struct elf_link_hash_entry *tga, *tga_fd, *desc, *desc_fd;
7860
7861 htab = ppc_hash_table (info);
7862 if (htab == NULL)
7863 return false;
7864
7865 /* Move dynamic linking info to the function descriptor sym. */
7866 if (htab->need_func_desc_adj)
7867 {
7868 elf_link_hash_traverse (&htab->elf, func_desc_adjust, info);
7869 htab->need_func_desc_adj = 0;
7870 }
7871
7872 if (abiversion (info->output_bfd) == 1)
7873 htab->opd_abi = 1;
7874
7875 if (htab->params->no_multi_toc)
7876 htab->do_multi_toc = 0;
7877 else if (!htab->do_multi_toc)
7878 htab->params->no_multi_toc = 1;
7879
7880 /* Default to --no-plt-localentry, as this option can cause problems
7881 with symbol interposition. For example, glibc libpthread.so and
7882 libc.so duplicate many pthread symbols, with a fallback
7883 implementation in libc.so. In some cases the fallback does more
7884 work than the pthread implementation. __pthread_condattr_destroy
7885 is one such symbol: the libpthread.so implementation is
7886 localentry:0 while the libc.so implementation is localentry:8.
7887 An app that "cleverly" uses dlopen to only load necessary
7888 libraries at runtime may omit loading libpthread.so when not
7889 running multi-threaded, which then results in the libc.so
7890 fallback symbols being used and ld.so complaining. Now there
7891 are workarounds in ld (see non_zero_localentry) to detect the
7892 pthread situation, but that may not be the only case where
7893 --plt-localentry can cause trouble. */
7894 if (htab->params->plt_localentry0 < 0)
7895 htab->params->plt_localentry0 = 0;
7896 if (htab->params->plt_localentry0 && htab->has_power10_relocs)
7897 {
7898 /* The issue is that __glink_PLTresolve saves r2, which is done
7899 because glibc ld.so _dl_runtime_resolve restores r2 to support
7900 a glibc plt call optimisation where global entry code is
7901 skipped on calls that resolve to the same binary. The
7902 __glink_PLTresolve save of r2 is incompatible with code
7903 making tail calls, because the tail call might go via the
7904 resolver and thus overwrite the proper saved r2. */
7905 _bfd_error_handler (_("warning: --plt-localentry is incompatible with "
7906 "power10 pc-relative code"));
7907 htab->params->plt_localentry0 = 0;
7908 }
7909 if (htab->params->plt_localentry0
7910 && elf_link_hash_lookup (&htab->elf, "GLIBC_2.26",
7911 false, false, false) == NULL)
7912 _bfd_error_handler
7913 (_("warning: --plt-localentry is especially dangerous without "
7914 "ld.so support to detect ABI violations"));
7915
7916 tga = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr",
7917 false, false, true);
7918 htab->tls_get_addr = ppc_elf_hash_entry (tga);
7919 tga_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
7920 false, false, true);
7921 htab->tls_get_addr_fd = ppc_elf_hash_entry (tga_fd);
7922
7923 desc = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_desc",
7924 false, false, true);
7925 htab->tga_desc = ppc_elf_hash_entry (desc);
7926 desc_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_desc",
7927 false, false, true);
7928 htab->tga_desc_fd = ppc_elf_hash_entry (desc_fd);
7929
7930 if (htab->params->tls_get_addr_opt)
7931 {
7932 struct elf_link_hash_entry *opt, *opt_fd;
7933
7934 opt = elf_link_hash_lookup (&htab->elf, ".__tls_get_addr_opt",
7935 false, false, true);
7936 opt_fd = elf_link_hash_lookup (&htab->elf, "__tls_get_addr_opt",
7937 false, false, true);
7938 if (opt_fd != NULL
7939 && (opt_fd->root.type == bfd_link_hash_defined
7940 || opt_fd->root.type == bfd_link_hash_defweak))
7941 {
7942 /* If glibc supports an optimized __tls_get_addr call stub,
7943 signalled by the presence of __tls_get_addr_opt, and we'll
7944 be calling __tls_get_addr via a plt call stub, then
7945 make __tls_get_addr point to __tls_get_addr_opt. */
7946 if (!(htab->elf.dynamic_sections_created
7947 && tga_fd != NULL
7948 && (tga_fd->type == STT_FUNC
7949 || tga_fd->needs_plt)
7950 && !(SYMBOL_CALLS_LOCAL (info, tga_fd)
7951 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, tga_fd))))
7952 tga_fd = NULL;
7953 if (!(htab->elf.dynamic_sections_created
7954 && desc_fd != NULL
7955 && (desc_fd->type == STT_FUNC
7956 || desc_fd->needs_plt)
7957 && !(SYMBOL_CALLS_LOCAL (info, desc_fd)
7958 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, desc_fd))))
7959 desc_fd = NULL;
7960
7961 if (tga_fd != NULL || desc_fd != NULL)
7962 {
7963 struct plt_entry *ent = NULL;
7964
7965 if (tga_fd != NULL)
7966 for (ent = tga_fd->plt.plist; ent != NULL; ent = ent->next)
7967 if (ent->plt.refcount > 0)
7968 break;
7969 if (ent == NULL && desc_fd != NULL)
7970 for (ent = desc_fd->plt.plist; ent != NULL; ent = ent->next)
7971 if (ent->plt.refcount > 0)
7972 break;
7973 if (ent != NULL)
7974 {
7975 if (tga_fd != NULL)
7976 {
7977 tga_fd->root.type = bfd_link_hash_indirect;
7978 tga_fd->root.u.i.link = &opt_fd->root;
7979 tga_fd->root.u.i.warning = NULL;
7980 ppc64_elf_copy_indirect_symbol (info, opt_fd, tga_fd);
7981 }
7982 if (desc_fd != NULL)
7983 {
7984 desc_fd->root.type = bfd_link_hash_indirect;
7985 desc_fd->root.u.i.link = &opt_fd->root;
7986 desc_fd->root.u.i.warning = NULL;
7987 ppc64_elf_copy_indirect_symbol (info, opt_fd, desc_fd);
7988 }
7989 opt_fd->mark = 1;
7990 if (opt_fd->dynindx != -1)
7991 {
7992 /* Use __tls_get_addr_opt in dynamic relocations. */
7993 opt_fd->dynindx = -1;
7994 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
7995 opt_fd->dynstr_index);
7996 if (!bfd_elf_link_record_dynamic_symbol (info, opt_fd))
7997 return false;
7998 }
7999 if (tga_fd != NULL)
8000 {
8001 htab->tls_get_addr_fd = ppc_elf_hash_entry (opt_fd);
8002 tga = elf_hash_entry (htab->tls_get_addr);
8003 if (opt != NULL && tga != NULL)
8004 {
8005 tga->root.type = bfd_link_hash_indirect;
8006 tga->root.u.i.link = &opt->root;
8007 tga->root.u.i.warning = NULL;
8008 ppc64_elf_copy_indirect_symbol (info, opt, tga);
8009 opt->mark = 1;
8010 _bfd_elf_link_hash_hide_symbol (info, opt,
8011 tga->forced_local);
8012 htab->tls_get_addr = ppc_elf_hash_entry (opt);
8013 }
8014 htab->tls_get_addr_fd->oh = htab->tls_get_addr;
8015 htab->tls_get_addr_fd->is_func_descriptor = 1;
8016 if (htab->tls_get_addr != NULL)
8017 {
8018 htab->tls_get_addr->oh = htab->tls_get_addr_fd;
8019 htab->tls_get_addr->is_func = 1;
8020 }
8021 }
8022 if (desc_fd != NULL)
8023 {
8024 htab->tga_desc_fd = ppc_elf_hash_entry (opt_fd);
8025 if (opt != NULL && desc != NULL)
8026 {
8027 desc->root.type = bfd_link_hash_indirect;
8028 desc->root.u.i.link = &opt->root;
8029 desc->root.u.i.warning = NULL;
8030 ppc64_elf_copy_indirect_symbol (info, opt, desc);
8031 opt->mark = 1;
8032 _bfd_elf_link_hash_hide_symbol (info, opt,
8033 desc->forced_local);
8034 htab->tga_desc = ppc_elf_hash_entry (opt);
8035 }
8036 htab->tga_desc_fd->oh = htab->tga_desc;
8037 htab->tga_desc_fd->is_func_descriptor = 1;
8038 if (htab->tga_desc != NULL)
8039 {
8040 htab->tga_desc->oh = htab->tga_desc_fd;
8041 htab->tga_desc->is_func = 1;
8042 }
8043 }
8044 }
8045 }
8046 }
8047 else if (htab->params->tls_get_addr_opt < 0)
8048 htab->params->tls_get_addr_opt = 0;
8049 }
8050
8051 if (htab->tga_desc_fd != NULL
8052 && htab->params->tls_get_addr_opt
8053 && htab->params->no_tls_get_addr_regsave == -1)
8054 htab->params->no_tls_get_addr_regsave = 0;
8055
8056 return true;
8057 }
8058
8059 /* Return TRUE iff REL is a branch reloc with a global symbol matching
8060 any of HASH1, HASH2, HASH3, or HASH4. */
8061
8062 static bool
8063 branch_reloc_hash_match (bfd *ibfd,
8064 Elf_Internal_Rela *rel,
8065 struct ppc_link_hash_entry *hash1,
8066 struct ppc_link_hash_entry *hash2,
8067 struct ppc_link_hash_entry *hash3,
8068 struct ppc_link_hash_entry *hash4)
8069 {
8070 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
8071 enum elf_ppc64_reloc_type r_type = ELF64_R_TYPE (rel->r_info);
8072 unsigned int r_symndx = ELF64_R_SYM (rel->r_info);
8073
8074 if (r_symndx >= symtab_hdr->sh_info && is_branch_reloc (r_type))
8075 {
8076 struct elf_link_hash_entry **sym_hashes = elf_sym_hashes (ibfd);
8077 struct elf_link_hash_entry *h;
8078
8079 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
8080 h = elf_follow_link (h);
8081 if (h == elf_hash_entry (hash1)
8082 || h == elf_hash_entry (hash2)
8083 || h == elf_hash_entry (hash3)
8084 || h == elf_hash_entry (hash4))
8085 return true;
8086 }
8087 return false;
8088 }
8089
8090 /* Run through all the TLS relocs looking for optimization
8091 opportunities. The linker has been hacked (see ppc64elf.em) to do
8092 a preliminary section layout so that we know the TLS segment
8093 offsets. We can't optimize earlier because some optimizations need
8094 to know the tp offset, and we need to optimize before allocating
8095 dynamic relocations. */
8096
8097 bool
8098 ppc64_elf_tls_optimize (struct bfd_link_info *info)
8099 {
8100 bfd *ibfd;
8101 asection *sec;
8102 struct ppc_link_hash_table *htab;
8103 unsigned char *toc_ref;
8104 int pass;
8105
8106 if (!bfd_link_executable (info))
8107 return true;
8108
8109 htab = ppc_hash_table (info);
8110 if (htab == NULL)
8111 return false;
8112
8113 htab->do_tls_opt = 1;
8114
8115 /* Make two passes over the relocs. On the first pass, mark toc
8116 entries involved with tls relocs, and check that tls relocs
8117 involved in setting up a tls_get_addr call are indeed followed by
8118 such a call. If they are not, we can't do any tls optimization.
8119 On the second pass twiddle tls_mask flags to notify
8120 relocate_section that optimization can be done, and adjust got
8121 and plt refcounts. */
8122 toc_ref = NULL;
8123 for (pass = 0; pass < 2; ++pass)
8124 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8125 {
8126 Elf_Internal_Sym *locsyms = NULL;
8127 asection *toc = bfd_get_section_by_name (ibfd, ".toc");
8128
8129 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8130 if (sec->has_tls_reloc && !bfd_is_abs_section (sec->output_section))
8131 {
8132 Elf_Internal_Rela *relstart, *rel, *relend;
8133 bool found_tls_get_addr_arg = 0;
8134
8135 /* Read the relocations. */
8136 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
8137 info->keep_memory);
8138 if (relstart == NULL)
8139 {
8140 free (toc_ref);
8141 return false;
8142 }
8143
8144 relend = relstart + sec->reloc_count;
8145 for (rel = relstart; rel < relend; rel++)
8146 {
8147 enum elf_ppc64_reloc_type r_type;
8148 unsigned long r_symndx;
8149 struct elf_link_hash_entry *h;
8150 Elf_Internal_Sym *sym;
8151 asection *sym_sec;
8152 unsigned char *tls_mask;
8153 unsigned int tls_set, tls_clear, tls_type = 0;
8154 bfd_vma value;
8155 bool ok_tprel, is_local;
8156 long toc_ref_index = 0;
8157 int expecting_tls_get_addr = 0;
8158 bool ret = false;
8159
8160 r_symndx = ELF64_R_SYM (rel->r_info);
8161 if (!get_sym_h (&h, &sym, &sym_sec, &tls_mask, &locsyms,
8162 r_symndx, ibfd))
8163 {
8164 err_free_rel:
8165 if (elf_section_data (sec)->relocs != relstart)
8166 free (relstart);
8167 free (toc_ref);
8168 if (elf_symtab_hdr (ibfd).contents
8169 != (unsigned char *) locsyms)
8170 free (locsyms);
8171 return ret;
8172 }
8173
8174 if (h != NULL)
8175 {
8176 if (h->root.type == bfd_link_hash_defined
8177 || h->root.type == bfd_link_hash_defweak)
8178 value = h->root.u.def.value;
8179 else if (h->root.type == bfd_link_hash_undefweak)
8180 value = 0;
8181 else
8182 {
8183 found_tls_get_addr_arg = 0;
8184 continue;
8185 }
8186 }
8187 else
8188 /* Symbols referenced by TLS relocs must be of type
8189 STT_TLS. So no need for .opd local sym adjust. */
8190 value = sym->st_value;
8191
8192 ok_tprel = false;
8193 is_local = SYMBOL_REFERENCES_LOCAL (info, h);
8194 if (is_local)
8195 {
8196 if (h != NULL
8197 && h->root.type == bfd_link_hash_undefweak)
8198 ok_tprel = true;
8199 else if (sym_sec != NULL
8200 && sym_sec->output_section != NULL)
8201 {
8202 value += sym_sec->output_offset;
8203 value += sym_sec->output_section->vma;
8204 value -= htab->elf.tls_sec->vma + TP_OFFSET;
8205 /* Note that even though the prefix insns
8206 allow a 1<<33 offset we use the same test
8207 as for addis;addi. There may be a mix of
8208 pcrel and non-pcrel code and the decision
8209 to optimise is per symbol, not per TLS
8210 sequence. */
8211 ok_tprel = value + 0x80008000ULL < 1ULL << 32;
8212 }
8213 }
8214
8215 r_type = ELF64_R_TYPE (rel->r_info);
8216 /* If this section has old-style __tls_get_addr calls
8217 without marker relocs, then check that each
8218 __tls_get_addr call reloc is preceded by a reloc
8219 that conceivably belongs to the __tls_get_addr arg
8220 setup insn. If we don't find matching arg setup
8221 relocs, don't do any tls optimization. */
8222 if (pass == 0
8223 && sec->nomark_tls_get_addr
8224 && h != NULL
8225 && is_tls_get_addr (h, htab)
8226 && !found_tls_get_addr_arg
8227 && is_branch_reloc (r_type))
8228 {
8229 info->callbacks->minfo (_("%H __tls_get_addr lost arg, "
8230 "TLS optimization disabled\n"),
8231 ibfd, sec, rel->r_offset);
8232 ret = true;
8233 goto err_free_rel;
8234 }
8235
8236 found_tls_get_addr_arg = 0;
8237 switch (r_type)
8238 {
8239 case R_PPC64_GOT_TLSLD16:
8240 case R_PPC64_GOT_TLSLD16_LO:
8241 case R_PPC64_GOT_TLSLD_PCREL34:
8242 expecting_tls_get_addr = 1;
8243 found_tls_get_addr_arg = 1;
8244 /* Fall through. */
8245
8246 case R_PPC64_GOT_TLSLD16_HI:
8247 case R_PPC64_GOT_TLSLD16_HA:
8248 /* These relocs should never be against a symbol
8249 defined in a shared lib. Leave them alone if
8250 that turns out to be the case. */
8251 if (!is_local)
8252 continue;
8253
8254 /* LD -> LE */
8255 tls_set = 0;
8256 tls_clear = TLS_LD;
8257 tls_type = TLS_TLS | TLS_LD;
8258 break;
8259
8260 case R_PPC64_GOT_TLSGD16:
8261 case R_PPC64_GOT_TLSGD16_LO:
8262 case R_PPC64_GOT_TLSGD_PCREL34:
8263 expecting_tls_get_addr = 1;
8264 found_tls_get_addr_arg = 1;
8265 /* Fall through. */
8266
8267 case R_PPC64_GOT_TLSGD16_HI:
8268 case R_PPC64_GOT_TLSGD16_HA:
8269 if (ok_tprel)
8270 /* GD -> LE */
8271 tls_set = 0;
8272 else
8273 /* GD -> IE */
8274 tls_set = TLS_TLS | TLS_GDIE;
8275 tls_clear = TLS_GD;
8276 tls_type = TLS_TLS | TLS_GD;
8277 break;
8278
8279 case R_PPC64_GOT_TPREL_PCREL34:
8280 case R_PPC64_GOT_TPREL16_DS:
8281 case R_PPC64_GOT_TPREL16_LO_DS:
8282 case R_PPC64_GOT_TPREL16_HI:
8283 case R_PPC64_GOT_TPREL16_HA:
8284 if (ok_tprel)
8285 {
8286 /* IE -> LE */
8287 tls_set = 0;
8288 tls_clear = TLS_TPREL;
8289 tls_type = TLS_TLS | TLS_TPREL;
8290 break;
8291 }
8292 continue;
8293
8294 case R_PPC64_TLSLD:
8295 if (!is_local)
8296 continue;
8297 /* Fall through. */
8298 case R_PPC64_TLSGD:
8299 if (rel + 1 < relend
8300 && is_plt_seq_reloc (ELF64_R_TYPE (rel[1].r_info)))
8301 {
8302 if (pass != 0
8303 && (ELF64_R_TYPE (rel[1].r_info)
8304 != R_PPC64_PLTSEQ)
8305 && (ELF64_R_TYPE (rel[1].r_info)
8306 != R_PPC64_PLTSEQ_NOTOC))
8307 {
8308 r_symndx = ELF64_R_SYM (rel[1].r_info);
8309 if (!get_sym_h (&h, NULL, NULL, NULL, &locsyms,
8310 r_symndx, ibfd))
8311 goto err_free_rel;
8312 if (h != NULL)
8313 {
8314 struct plt_entry *ent = NULL;
8315
8316 for (ent = h->plt.plist;
8317 ent != NULL;
8318 ent = ent->next)
8319 if (ent->addend == rel[1].r_addend)
8320 break;
8321
8322 if (ent != NULL
8323 && ent->plt.refcount > 0)
8324 ent->plt.refcount -= 1;
8325 }
8326 }
8327 continue;
8328 }
8329 found_tls_get_addr_arg = 1;
8330 /* Fall through. */
8331
8332 case R_PPC64_TLS:
8333 case R_PPC64_TOC16:
8334 case R_PPC64_TOC16_LO:
8335 if (sym_sec == NULL || sym_sec != toc)
8336 continue;
8337
8338 /* Mark this toc entry as referenced by a TLS
8339 code sequence. We can do that now in the
8340 case of R_PPC64_TLS, and after checking for
8341 tls_get_addr for the TOC16 relocs. */
8342 if (toc_ref == NULL)
8343 toc_ref
8344 = bfd_zmalloc (toc->output_section->rawsize / 8);
8345 if (toc_ref == NULL)
8346 goto err_free_rel;
8347
8348 if (h != NULL)
8349 value = h->root.u.def.value;
8350 else
8351 value = sym->st_value;
8352 value += rel->r_addend;
8353 if (value % 8 != 0)
8354 continue;
8355 BFD_ASSERT (value < toc->size
8356 && toc->output_offset % 8 == 0);
8357 toc_ref_index = (value + toc->output_offset) / 8;
8358 if (r_type == R_PPC64_TLS
8359 || r_type == R_PPC64_TLSGD
8360 || r_type == R_PPC64_TLSLD)
8361 {
8362 toc_ref[toc_ref_index] = 1;
8363 continue;
8364 }
8365
8366 if (pass != 0 && toc_ref[toc_ref_index] == 0)
8367 continue;
8368
8369 tls_set = 0;
8370 tls_clear = 0;
8371 expecting_tls_get_addr = 2;
8372 break;
8373
8374 case R_PPC64_TPREL64:
8375 if (pass == 0
8376 || sec != toc
8377 || toc_ref == NULL
8378 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8379 continue;
8380 if (ok_tprel)
8381 {
8382 /* IE -> LE */
8383 tls_set = TLS_EXPLICIT;
8384 tls_clear = TLS_TPREL;
8385 break;
8386 }
8387 continue;
8388
8389 case R_PPC64_DTPMOD64:
8390 if (pass == 0
8391 || sec != toc
8392 || toc_ref == NULL
8393 || !toc_ref[(rel->r_offset + toc->output_offset) / 8])
8394 continue;
8395 if (rel + 1 < relend
8396 && (rel[1].r_info
8397 == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64))
8398 && rel[1].r_offset == rel->r_offset + 8)
8399 {
8400 if (ok_tprel)
8401 /* GD -> LE */
8402 tls_set = TLS_EXPLICIT | TLS_GD;
8403 else
8404 /* GD -> IE */
8405 tls_set = TLS_EXPLICIT | TLS_GD | TLS_GDIE;
8406 tls_clear = TLS_GD;
8407 }
8408 else
8409 {
8410 if (!is_local)
8411 continue;
8412
8413 /* LD -> LE */
8414 tls_set = TLS_EXPLICIT;
8415 tls_clear = TLS_LD;
8416 }
8417 break;
8418
8419 case R_PPC64_TPREL16_HA:
8420 if (pass == 0)
8421 {
8422 unsigned char buf[4];
8423 unsigned int insn;
8424 bfd_vma off = rel->r_offset & ~3;
8425 if (!bfd_get_section_contents (ibfd, sec, buf,
8426 off, 4))
8427 goto err_free_rel;
8428 insn = bfd_get_32 (ibfd, buf);
8429 /* addis rt,13,imm */
8430 if ((insn & ((0x3fu << 26) | 0x1f << 16))
8431 != ((15u << 26) | (13 << 16)))
8432 {
8433 /* xgettext:c-format */
8434 info->callbacks->minfo
8435 (_("%H: warning: %s unexpected insn %#x.\n"),
8436 ibfd, sec, off, "R_PPC64_TPREL16_HA", insn);
8437 htab->do_tls_opt = 0;
8438 }
8439 }
8440 continue;
8441
8442 case R_PPC64_TPREL16_HI:
8443 case R_PPC64_TPREL16_HIGH:
8444 case R_PPC64_TPREL16_HIGHA:
8445 case R_PPC64_TPREL16_HIGHER:
8446 case R_PPC64_TPREL16_HIGHERA:
8447 case R_PPC64_TPREL16_HIGHEST:
8448 case R_PPC64_TPREL16_HIGHESTA:
8449 /* These can all be used in sequences along with
8450 TPREL16_LO or TPREL16_LO_DS in ways we aren't
8451 able to verify easily. */
8452 htab->do_tls_opt = 0;
8453 continue;
8454
8455 default:
8456 continue;
8457 }
8458
8459 if (pass == 0)
8460 {
8461 if (!expecting_tls_get_addr
8462 || !sec->nomark_tls_get_addr)
8463 continue;
8464
8465 if (rel + 1 < relend
8466 && branch_reloc_hash_match (ibfd, rel + 1,
8467 htab->tls_get_addr_fd,
8468 htab->tga_desc_fd,
8469 htab->tls_get_addr,
8470 htab->tga_desc))
8471 {
8472 if (expecting_tls_get_addr == 2)
8473 {
8474 /* Check for toc tls entries. */
8475 unsigned char *toc_tls;
8476 int retval;
8477
8478 retval = get_tls_mask (&toc_tls, NULL, NULL,
8479 &locsyms,
8480 rel, ibfd);
8481 if (retval == 0)
8482 goto err_free_rel;
8483 if (toc_tls != NULL)
8484 {
8485 if ((*toc_tls & TLS_TLS) != 0
8486 && ((*toc_tls & (TLS_GD | TLS_LD)) != 0))
8487 found_tls_get_addr_arg = 1;
8488 if (retval > 1)
8489 toc_ref[toc_ref_index] = 1;
8490 }
8491 }
8492 continue;
8493 }
8494
8495 /* Uh oh, we didn't find the expected call. We
8496 could just mark this symbol to exclude it
8497 from tls optimization but it's safer to skip
8498 the entire optimization. */
8499 /* xgettext:c-format */
8500 info->callbacks->minfo (_("%H arg lost __tls_get_addr, "
8501 "TLS optimization disabled\n"),
8502 ibfd, sec, rel->r_offset);
8503 ret = true;
8504 goto err_free_rel;
8505 }
8506
8507 /* If we don't have old-style __tls_get_addr calls
8508 without TLSGD/TLSLD marker relocs, and we haven't
8509 found a new-style __tls_get_addr call with a
8510 marker for this symbol, then we either have a
8511 broken object file or an -mlongcall style
8512 indirect call to __tls_get_addr without a marker.
8513 Disable optimization in this case. */
8514 if ((tls_clear & (TLS_GD | TLS_LD)) != 0
8515 && (tls_set & TLS_EXPLICIT) == 0
8516 && !sec->nomark_tls_get_addr
8517 && ((*tls_mask & (TLS_TLS | TLS_MARK))
8518 != (TLS_TLS | TLS_MARK)))
8519 continue;
8520
8521 if (expecting_tls_get_addr == 1 + !sec->nomark_tls_get_addr)
8522 {
8523 struct plt_entry *ent = NULL;
8524
8525 if (htab->tls_get_addr_fd != NULL)
8526 for (ent = htab->tls_get_addr_fd->elf.plt.plist;
8527 ent != NULL;
8528 ent = ent->next)
8529 if (ent->addend == 0)
8530 break;
8531
8532 if (ent == NULL && htab->tga_desc_fd != NULL)
8533 for (ent = htab->tga_desc_fd->elf.plt.plist;
8534 ent != NULL;
8535 ent = ent->next)
8536 if (ent->addend == 0)
8537 break;
8538
8539 if (ent == NULL && htab->tls_get_addr != NULL)
8540 for (ent = htab->tls_get_addr->elf.plt.plist;
8541 ent != NULL;
8542 ent = ent->next)
8543 if (ent->addend == 0)
8544 break;
8545
8546 if (ent == NULL && htab->tga_desc != NULL)
8547 for (ent = htab->tga_desc->elf.plt.plist;
8548 ent != NULL;
8549 ent = ent->next)
8550 if (ent->addend == 0)
8551 break;
8552
8553 if (ent != NULL
8554 && ent->plt.refcount > 0)
8555 ent->plt.refcount -= 1;
8556 }
8557
8558 if (tls_clear == 0)
8559 continue;
8560
8561 if ((tls_set & TLS_EXPLICIT) == 0)
8562 {
8563 struct got_entry *ent;
8564
8565 /* Adjust got entry for this reloc. */
8566 if (h != NULL)
8567 ent = h->got.glist;
8568 else
8569 ent = elf_local_got_ents (ibfd)[r_symndx];
8570
8571 for (; ent != NULL; ent = ent->next)
8572 if (ent->addend == rel->r_addend
8573 && ent->owner == ibfd
8574 && ent->tls_type == tls_type)
8575 break;
8576 if (ent == NULL)
8577 abort ();
8578
8579 if (tls_set == 0)
8580 {
8581 /* We managed to get rid of a got entry. */
8582 if (ent->got.refcount > 0)
8583 ent->got.refcount -= 1;
8584 }
8585 }
8586 else
8587 {
8588 /* If we got rid of a DTPMOD/DTPREL reloc pair then
8589 we'll lose one or two dyn relocs. */
8590 if (!dec_dynrel_count (rel->r_info, sec, info,
8591 NULL, h, sym))
8592 return false;
8593
8594 if (tls_set == (TLS_EXPLICIT | TLS_GD))
8595 {
8596 if (!dec_dynrel_count ((rel + 1)->r_info, sec, info,
8597 NULL, h, sym))
8598 return false;
8599 }
8600 }
8601
8602 *tls_mask |= tls_set & 0xff;
8603 *tls_mask &= ~tls_clear;
8604 }
8605
8606 if (elf_section_data (sec)->relocs != relstart)
8607 free (relstart);
8608 }
8609
8610 if (locsyms != NULL
8611 && (elf_symtab_hdr (ibfd).contents != (unsigned char *) locsyms))
8612 {
8613 if (!info->keep_memory)
8614 free (locsyms);
8615 else
8616 elf_symtab_hdr (ibfd).contents = (unsigned char *) locsyms;
8617 }
8618 }
8619
8620 free (toc_ref);
8621 return true;
8622 }
8623
8624 /* Called via elf_link_hash_traverse from ppc64_elf_edit_toc to adjust
8625 the values of any global symbols in a toc section that has been
8626 edited. Globals in toc sections should be a rarity, so this function
8627 sets a flag if any are found in toc sections other than the one just
8628 edited, so that further hash table traversals can be avoided. */
8629
8630 struct adjust_toc_info
8631 {
8632 asection *toc;
8633 unsigned long *skip;
8634 bool global_toc_syms;
8635 };
8636
8637 enum toc_skip_enum { ref_from_discarded = 1, can_optimize = 2 };
8638
8639 static bool
8640 adjust_toc_syms (struct elf_link_hash_entry *h, void *inf)
8641 {
8642 struct ppc_link_hash_entry *eh;
8643 struct adjust_toc_info *toc_inf = (struct adjust_toc_info *) inf;
8644 unsigned long i;
8645
8646 if (h->root.type != bfd_link_hash_defined
8647 && h->root.type != bfd_link_hash_defweak)
8648 return true;
8649
8650 eh = ppc_elf_hash_entry (h);
8651 if (eh->adjust_done)
8652 return true;
8653
8654 if (eh->elf.root.u.def.section == toc_inf->toc)
8655 {
8656 if (eh->elf.root.u.def.value > toc_inf->toc->rawsize)
8657 i = toc_inf->toc->rawsize >> 3;
8658 else
8659 i = eh->elf.root.u.def.value >> 3;
8660
8661 if ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0)
8662 {
8663 _bfd_error_handler
8664 (_("%s defined on removed toc entry"), eh->elf.root.root.string);
8665 do
8666 ++i;
8667 while ((toc_inf->skip[i] & (ref_from_discarded | can_optimize)) != 0);
8668 eh->elf.root.u.def.value = (bfd_vma) i << 3;
8669 }
8670
8671 eh->elf.root.u.def.value -= toc_inf->skip[i];
8672 eh->adjust_done = 1;
8673 }
8674 else if (strcmp (eh->elf.root.u.def.section->name, ".toc") == 0)
8675 toc_inf->global_toc_syms = true;
8676
8677 return true;
8678 }
8679
8680 /* Return TRUE iff INSN with a relocation of R_TYPE is one we expect
8681 on a _LO variety toc/got reloc. */
8682
8683 static bool
8684 ok_lo_toc_insn (unsigned int insn, enum elf_ppc64_reloc_type r_type)
8685 {
8686 return ((insn & (0x3fu << 26)) == 12u << 26 /* addic */
8687 || (insn & (0x3fu << 26)) == 14u << 26 /* addi */
8688 || (insn & (0x3fu << 26)) == 32u << 26 /* lwz */
8689 || (insn & (0x3fu << 26)) == 34u << 26 /* lbz */
8690 || (insn & (0x3fu << 26)) == 36u << 26 /* stw */
8691 || (insn & (0x3fu << 26)) == 38u << 26 /* stb */
8692 || (insn & (0x3fu << 26)) == 40u << 26 /* lhz */
8693 || (insn & (0x3fu << 26)) == 42u << 26 /* lha */
8694 || (insn & (0x3fu << 26)) == 44u << 26 /* sth */
8695 || (insn & (0x3fu << 26)) == 46u << 26 /* lmw */
8696 || (insn & (0x3fu << 26)) == 47u << 26 /* stmw */
8697 || (insn & (0x3fu << 26)) == 48u << 26 /* lfs */
8698 || (insn & (0x3fu << 26)) == 50u << 26 /* lfd */
8699 || (insn & (0x3fu << 26)) == 52u << 26 /* stfs */
8700 || (insn & (0x3fu << 26)) == 54u << 26 /* stfd */
8701 || (insn & (0x3fu << 26)) == 56u << 26 /* lq,lfq */
8702 || ((insn & (0x3fu << 26)) == 57u << 26 /* lxsd,lxssp,lfdp */
8703 /* Exclude lfqu by testing reloc. If relocs are ever
8704 defined for the reduced D field in psq_lu then those
8705 will need testing too. */
8706 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8707 || ((insn & (0x3fu << 26)) == 58u << 26 /* ld,lwa */
8708 && (insn & 1) == 0)
8709 || (insn & (0x3fu << 26)) == 60u << 26 /* stfq */
8710 || ((insn & (0x3fu << 26)) == 61u << 26 /* lxv,stx{v,sd,ssp},stfdp */
8711 /* Exclude stfqu. psq_stu as above for psq_lu. */
8712 && r_type != R_PPC64_TOC16_LO && r_type != R_PPC64_GOT16_LO)
8713 || ((insn & (0x3fu << 26)) == 62u << 26 /* std,stq */
8714 && (insn & 1) == 0));
8715 }
8716
8717 /* PCREL_OPT in one instance flags to the linker that a pair of insns:
8718 pld ra,symbol@got@pcrel
8719 load/store rt,off(ra)
8720 or
8721 pla ra,symbol@pcrel
8722 load/store rt,off(ra)
8723 may be translated to
8724 pload/pstore rt,symbol+off@pcrel
8725 nop.
8726 This function returns true if the optimization is possible, placing
8727 the prefix insn in *PINSN1, a NOP in *PINSN2 and the offset in *POFF.
8728
8729 On entry to this function, the linker has already determined that
8730 the pld can be replaced with pla: *PINSN1 is that pla insn,
8731 while *PINSN2 is the second instruction. */
8732
8733 static bool
8734 xlate_pcrel_opt (uint64_t *pinsn1, uint64_t *pinsn2, bfd_signed_vma *poff)
8735 {
8736 uint64_t insn1 = *pinsn1;
8737 uint64_t insn2 = *pinsn2;
8738 bfd_signed_vma off;
8739
8740 if ((insn2 & (63ULL << 58)) == 1ULL << 58)
8741 {
8742 /* Check that regs match. */
8743 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8744 return false;
8745
8746 /* P8LS or PMLS form, non-pcrel. */
8747 if ((insn2 & (-1ULL << 50) & ~(1ULL << 56)) != (1ULL << 58))
8748 return false;
8749
8750 *pinsn1 = (insn2 & ~(31 << 16) & ~0x3ffff0000ffffULL) | (1ULL << 52);
8751 *pinsn2 = PNOP;
8752 off = ((insn2 >> 16) & 0x3ffff0000ULL) | (insn2 & 0xffff);
8753 *poff = (off ^ 0x200000000ULL) - 0x200000000ULL;
8754 return true;
8755 }
8756
8757 insn2 >>= 32;
8758
8759 /* Check that regs match. */
8760 if (((insn2 >> 16) & 31) != ((insn1 >> 21) & 31))
8761 return false;
8762
8763 switch ((insn2 >> 26) & 63)
8764 {
8765 default:
8766 return false;
8767
8768 case 32: /* lwz */
8769 case 34: /* lbz */
8770 case 36: /* stw */
8771 case 38: /* stb */
8772 case 40: /* lhz */
8773 case 42: /* lha */
8774 case 44: /* sth */
8775 case 48: /* lfs */
8776 case 50: /* lfd */
8777 case 52: /* stfs */
8778 case 54: /* stfd */
8779 /* These are the PMLS cases, where we just need to tack a prefix
8780 on the insn. */
8781 insn1 = ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
8782 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8783 off = insn2 & 0xffff;
8784 break;
8785
8786 case 58: /* lwa, ld */
8787 if ((insn2 & 1) != 0)
8788 return false;
8789 insn1 = ((1ULL << 58) | (1ULL << 52)
8790 | (insn2 & 2 ? 41ULL << 26 : 57ULL << 26)
8791 | (insn2 & (31ULL << 21)));
8792 off = insn2 & 0xfffc;
8793 break;
8794
8795 case 57: /* lxsd, lxssp */
8796 if ((insn2 & 3) < 2)
8797 return false;
8798 insn1 = ((1ULL << 58) | (1ULL << 52)
8799 | ((40ULL | (insn2 & 3)) << 26)
8800 | (insn2 & (31ULL << 21)));
8801 off = insn2 & 0xfffc;
8802 break;
8803
8804 case 61: /* stxsd, stxssp, lxv, stxv */
8805 if ((insn2 & 3) == 0)
8806 return false;
8807 else if ((insn2 & 3) >= 2)
8808 {
8809 insn1 = ((1ULL << 58) | (1ULL << 52)
8810 | ((44ULL | (insn2 & 3)) << 26)
8811 | (insn2 & (31ULL << 21)));
8812 off = insn2 & 0xfffc;
8813 }
8814 else
8815 {
8816 insn1 = ((1ULL << 58) | (1ULL << 52)
8817 | ((50ULL | (insn2 & 4) | ((insn2 & 8) >> 3)) << 26)
8818 | (insn2 & (31ULL << 21)));
8819 off = insn2 & 0xfff0;
8820 }
8821 break;
8822
8823 case 56: /* lq */
8824 insn1 = ((1ULL << 58) | (1ULL << 52)
8825 | (insn2 & ((63ULL << 26) | (31ULL << 21))));
8826 off = insn2 & 0xffff;
8827 break;
8828
8829 case 6: /* lxvp, stxvp */
8830 if ((insn2 & 0xe) != 0)
8831 return false;
8832 insn1 = ((1ULL << 58) | (1ULL << 52)
8833 | ((insn2 & 1) == 0 ? 58ULL << 26 : 62ULL << 26)
8834 | (insn2 & (31ULL << 21)));
8835 off = insn2 & 0xfff0;
8836 break;
8837
8838 case 62: /* std, stq */
8839 if ((insn2 & 1) != 0)
8840 return false;
8841 insn1 = ((1ULL << 58) | (1ULL << 52)
8842 | ((insn2 & 2) == 0 ? 61ULL << 26 : 60ULL << 26)
8843 | (insn2 & (31ULL << 21)));
8844 off = insn2 & 0xfffc;
8845 break;
8846 }
8847
8848 *pinsn1 = insn1;
8849 *pinsn2 = (uint64_t) NOP << 32;
8850 *poff = (off ^ 0x8000) - 0x8000;
8851 return true;
8852 }
8853
8854 /* Examine all relocs referencing .toc sections in order to remove
8855 unused .toc entries. */
8856
8857 bool
8858 ppc64_elf_edit_toc (struct bfd_link_info *info)
8859 {
8860 bfd *ibfd;
8861 struct adjust_toc_info toc_inf;
8862 struct ppc_link_hash_table *htab = ppc_hash_table (info);
8863
8864 htab->do_toc_opt = 1;
8865 toc_inf.global_toc_syms = true;
8866 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
8867 {
8868 asection *toc, *sec;
8869 Elf_Internal_Shdr *symtab_hdr;
8870 Elf_Internal_Sym *local_syms;
8871 Elf_Internal_Rela *relstart, *rel, *toc_relocs;
8872 unsigned long *skip, *drop;
8873 unsigned char *used;
8874 unsigned char *keep, last, some_unused;
8875
8876 if (!is_ppc64_elf (ibfd))
8877 continue;
8878
8879 toc = bfd_get_section_by_name (ibfd, ".toc");
8880 if (toc == NULL
8881 || toc->size == 0
8882 || toc->sec_info_type == SEC_INFO_TYPE_JUST_SYMS
8883 || discarded_section (toc))
8884 continue;
8885
8886 toc_relocs = NULL;
8887 local_syms = NULL;
8888 symtab_hdr = &elf_symtab_hdr (ibfd);
8889
8890 /* Look at sections dropped from the final link. */
8891 skip = NULL;
8892 relstart = NULL;
8893 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
8894 {
8895 if (sec->reloc_count == 0
8896 || !discarded_section (sec)
8897 || get_opd_info (sec)
8898 || (sec->flags & SEC_ALLOC) == 0
8899 || (sec->flags & SEC_DEBUGGING) != 0)
8900 continue;
8901
8902 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL, false);
8903 if (relstart == NULL)
8904 goto error_ret;
8905
8906 /* Run through the relocs to see which toc entries might be
8907 unused. */
8908 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
8909 {
8910 enum elf_ppc64_reloc_type r_type;
8911 unsigned long r_symndx;
8912 asection *sym_sec;
8913 struct elf_link_hash_entry *h;
8914 Elf_Internal_Sym *sym;
8915 bfd_vma val;
8916
8917 r_type = ELF64_R_TYPE (rel->r_info);
8918 switch (r_type)
8919 {
8920 default:
8921 continue;
8922
8923 case R_PPC64_TOC16:
8924 case R_PPC64_TOC16_LO:
8925 case R_PPC64_TOC16_HI:
8926 case R_PPC64_TOC16_HA:
8927 case R_PPC64_TOC16_DS:
8928 case R_PPC64_TOC16_LO_DS:
8929 break;
8930 }
8931
8932 r_symndx = ELF64_R_SYM (rel->r_info);
8933 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
8934 r_symndx, ibfd))
8935 goto error_ret;
8936
8937 if (sym_sec != toc)
8938 continue;
8939
8940 if (h != NULL)
8941 val = h->root.u.def.value;
8942 else
8943 val = sym->st_value;
8944 val += rel->r_addend;
8945
8946 if (val >= toc->size)
8947 continue;
8948
8949 /* Anything in the toc ought to be aligned to 8 bytes.
8950 If not, don't mark as unused. */
8951 if (val & 7)
8952 continue;
8953
8954 if (skip == NULL)
8955 {
8956 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
8957 if (skip == NULL)
8958 goto error_ret;
8959 }
8960
8961 skip[val >> 3] = ref_from_discarded;
8962 }
8963
8964 if (elf_section_data (sec)->relocs != relstart)
8965 free (relstart);
8966 }
8967
8968 /* For largetoc loads of address constants, we can convert
8969 . addis rx,2,addr@got@ha
8970 . ld ry,addr@got@l(rx)
8971 to
8972 . addis rx,2,addr@toc@ha
8973 . addi ry,rx,addr@toc@l
8974 when addr is within 2G of the toc pointer. This then means
8975 that the word storing "addr" in the toc is no longer needed. */
8976
8977 if (!ppc64_elf_tdata (ibfd)->has_small_toc_reloc
8978 && toc->output_section->rawsize < (bfd_vma) 1 << 31
8979 && toc->reloc_count != 0)
8980 {
8981 /* Read toc relocs. */
8982 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
8983 info->keep_memory);
8984 if (toc_relocs == NULL)
8985 goto error_ret;
8986
8987 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
8988 {
8989 enum elf_ppc64_reloc_type r_type;
8990 unsigned long r_symndx;
8991 asection *sym_sec;
8992 struct elf_link_hash_entry *h;
8993 Elf_Internal_Sym *sym;
8994 bfd_vma val, addr;
8995
8996 r_type = ELF64_R_TYPE (rel->r_info);
8997 if (r_type != R_PPC64_ADDR64)
8998 continue;
8999
9000 r_symndx = ELF64_R_SYM (rel->r_info);
9001 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9002 r_symndx, ibfd))
9003 goto error_ret;
9004
9005 if (sym_sec == NULL
9006 || sym_sec->output_section == NULL
9007 || discarded_section (sym_sec))
9008 continue;
9009
9010 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9011 continue;
9012
9013 if (h != NULL)
9014 {
9015 if (h->type == STT_GNU_IFUNC)
9016 continue;
9017 val = h->root.u.def.value;
9018 }
9019 else
9020 {
9021 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
9022 continue;
9023 val = sym->st_value;
9024 }
9025 val += rel->r_addend;
9026 val += sym_sec->output_section->vma + sym_sec->output_offset;
9027
9028 /* We don't yet know the exact toc pointer value, but we
9029 know it will be somewhere in the toc section. Don't
9030 optimize if the difference from any possible toc
9031 pointer is outside [ff..f80008000, 7fff7fff]. */
9032 addr = toc->output_section->vma + TOC_BASE_OFF;
9033 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9034 continue;
9035
9036 addr = toc->output_section->vma + toc->output_section->rawsize;
9037 if (val - addr + (bfd_vma) 0x80008000 >= (bfd_vma) 1 << 32)
9038 continue;
9039
9040 if (skip == NULL)
9041 {
9042 skip = bfd_zmalloc (sizeof (*skip) * (toc->size + 15) / 8);
9043 if (skip == NULL)
9044 goto error_ret;
9045 }
9046
9047 skip[rel->r_offset >> 3]
9048 |= can_optimize | ((rel - toc_relocs) << 2);
9049 }
9050 }
9051
9052 if (skip == NULL)
9053 continue;
9054
9055 used = bfd_zmalloc (sizeof (*used) * (toc->size + 7) / 8);
9056 if (used == NULL)
9057 {
9058 error_ret:
9059 if (symtab_hdr->contents != (unsigned char *) local_syms)
9060 free (local_syms);
9061 if (sec != NULL
9062 && elf_section_data (sec)->relocs != relstart)
9063 free (relstart);
9064 if (elf_section_data (toc)->relocs != toc_relocs)
9065 free (toc_relocs);
9066 free (skip);
9067 return false;
9068 }
9069
9070 /* Now check all kept sections that might reference the toc.
9071 Check the toc itself last. */
9072 for (sec = (ibfd->sections == toc && toc->next ? toc->next
9073 : ibfd->sections);
9074 sec != NULL;
9075 sec = (sec == toc ? NULL
9076 : sec->next == NULL ? toc
9077 : sec->next == toc && toc->next ? toc->next
9078 : sec->next))
9079 {
9080 int repeat;
9081
9082 if (sec->reloc_count == 0
9083 || discarded_section (sec)
9084 || get_opd_info (sec)
9085 || (sec->flags & SEC_ALLOC) == 0
9086 || (sec->flags & SEC_DEBUGGING) != 0)
9087 continue;
9088
9089 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9090 info->keep_memory);
9091 if (relstart == NULL)
9092 {
9093 free (used);
9094 goto error_ret;
9095 }
9096
9097 /* Mark toc entries referenced as used. */
9098 do
9099 {
9100 repeat = 0;
9101 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9102 {
9103 enum elf_ppc64_reloc_type r_type;
9104 unsigned long r_symndx;
9105 asection *sym_sec;
9106 struct elf_link_hash_entry *h;
9107 Elf_Internal_Sym *sym;
9108 bfd_vma val;
9109
9110 r_type = ELF64_R_TYPE (rel->r_info);
9111 switch (r_type)
9112 {
9113 case R_PPC64_TOC16:
9114 case R_PPC64_TOC16_LO:
9115 case R_PPC64_TOC16_HI:
9116 case R_PPC64_TOC16_HA:
9117 case R_PPC64_TOC16_DS:
9118 case R_PPC64_TOC16_LO_DS:
9119 /* In case we're taking addresses of toc entries. */
9120 case R_PPC64_ADDR64:
9121 break;
9122
9123 default:
9124 continue;
9125 }
9126
9127 r_symndx = ELF64_R_SYM (rel->r_info);
9128 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9129 r_symndx, ibfd))
9130 {
9131 free (used);
9132 goto error_ret;
9133 }
9134
9135 if (sym_sec != toc)
9136 continue;
9137
9138 if (h != NULL)
9139 val = h->root.u.def.value;
9140 else
9141 val = sym->st_value;
9142 val += rel->r_addend;
9143
9144 if (val >= toc->size)
9145 continue;
9146
9147 if ((skip[val >> 3] & can_optimize) != 0)
9148 {
9149 bfd_vma off;
9150 unsigned char opc;
9151
9152 switch (r_type)
9153 {
9154 case R_PPC64_TOC16_HA:
9155 break;
9156
9157 case R_PPC64_TOC16_LO_DS:
9158 off = rel->r_offset;
9159 off += (bfd_big_endian (ibfd) ? -2 : 3);
9160 if (!bfd_get_section_contents (ibfd, sec, &opc,
9161 off, 1))
9162 {
9163 free (used);
9164 goto error_ret;
9165 }
9166 if ((opc & (0x3f << 2)) == (58u << 2))
9167 break;
9168 /* Fall through. */
9169
9170 default:
9171 /* Wrong sort of reloc, or not a ld. We may
9172 as well clear ref_from_discarded too. */
9173 skip[val >> 3] = 0;
9174 }
9175 }
9176
9177 if (sec != toc)
9178 used[val >> 3] = 1;
9179 /* For the toc section, we only mark as used if this
9180 entry itself isn't unused. */
9181 else if ((used[rel->r_offset >> 3]
9182 || !(skip[rel->r_offset >> 3] & ref_from_discarded))
9183 && !used[val >> 3])
9184 {
9185 /* Do all the relocs again, to catch reference
9186 chains. */
9187 repeat = 1;
9188 used[val >> 3] = 1;
9189 }
9190 }
9191 }
9192 while (repeat);
9193
9194 if (elf_section_data (sec)->relocs != relstart)
9195 free (relstart);
9196 }
9197
9198 /* Merge the used and skip arrays. Assume that TOC
9199 doublewords not appearing as either used or unused belong
9200 to an entry more than one doubleword in size. */
9201 for (drop = skip, keep = used, last = 0, some_unused = 0;
9202 drop < skip + (toc->size + 7) / 8;
9203 ++drop, ++keep)
9204 {
9205 if (*keep)
9206 {
9207 *drop &= ~ref_from_discarded;
9208 if ((*drop & can_optimize) != 0)
9209 some_unused = 1;
9210 last = 0;
9211 }
9212 else if ((*drop & ref_from_discarded) != 0)
9213 {
9214 some_unused = 1;
9215 last = ref_from_discarded;
9216 }
9217 else
9218 *drop = last;
9219 }
9220
9221 free (used);
9222
9223 if (some_unused)
9224 {
9225 bfd_byte *contents, *src;
9226 unsigned long off;
9227 Elf_Internal_Sym *sym;
9228 bool local_toc_syms = false;
9229
9230 /* Shuffle the toc contents, and at the same time convert the
9231 skip array from booleans into offsets. */
9232 if (!bfd_malloc_and_get_section (ibfd, toc, &contents))
9233 goto error_ret;
9234
9235 elf_section_data (toc)->this_hdr.contents = contents;
9236
9237 for (src = contents, off = 0, drop = skip;
9238 src < contents + toc->size;
9239 src += 8, ++drop)
9240 {
9241 if ((*drop & (can_optimize | ref_from_discarded)) != 0)
9242 off += 8;
9243 else if (off != 0)
9244 {
9245 *drop = off;
9246 memcpy (src - off, src, 8);
9247 }
9248 }
9249 *drop = off;
9250 toc->rawsize = toc->size;
9251 toc->size = src - contents - off;
9252
9253 /* Adjust addends for relocs against the toc section sym,
9254 and optimize any accesses we can. */
9255 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9256 {
9257 if (sec->reloc_count == 0
9258 || discarded_section (sec))
9259 continue;
9260
9261 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9262 info->keep_memory);
9263 if (relstart == NULL)
9264 goto error_ret;
9265
9266 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9267 {
9268 enum elf_ppc64_reloc_type r_type;
9269 unsigned long r_symndx;
9270 asection *sym_sec;
9271 struct elf_link_hash_entry *h;
9272 bfd_vma val;
9273
9274 r_type = ELF64_R_TYPE (rel->r_info);
9275 switch (r_type)
9276 {
9277 default:
9278 continue;
9279
9280 case R_PPC64_TOC16:
9281 case R_PPC64_TOC16_LO:
9282 case R_PPC64_TOC16_HI:
9283 case R_PPC64_TOC16_HA:
9284 case R_PPC64_TOC16_DS:
9285 case R_PPC64_TOC16_LO_DS:
9286 case R_PPC64_ADDR64:
9287 break;
9288 }
9289
9290 r_symndx = ELF64_R_SYM (rel->r_info);
9291 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9292 r_symndx, ibfd))
9293 goto error_ret;
9294
9295 if (sym_sec != toc)
9296 continue;
9297
9298 if (h != NULL)
9299 val = h->root.u.def.value;
9300 else
9301 {
9302 val = sym->st_value;
9303 if (val != 0)
9304 local_toc_syms = true;
9305 }
9306
9307 val += rel->r_addend;
9308
9309 if (val > toc->rawsize)
9310 val = toc->rawsize;
9311 else if ((skip[val >> 3] & ref_from_discarded) != 0)
9312 continue;
9313 else if ((skip[val >> 3] & can_optimize) != 0)
9314 {
9315 Elf_Internal_Rela *tocrel
9316 = toc_relocs + (skip[val >> 3] >> 2);
9317 unsigned long tsym = ELF64_R_SYM (tocrel->r_info);
9318
9319 switch (r_type)
9320 {
9321 case R_PPC64_TOC16_HA:
9322 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_TOC16_HA);
9323 break;
9324
9325 case R_PPC64_TOC16_LO_DS:
9326 rel->r_info = ELF64_R_INFO (tsym, R_PPC64_LO_DS_OPT);
9327 break;
9328
9329 default:
9330 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
9331 ppc_howto_init ();
9332 info->callbacks->einfo
9333 /* xgettext:c-format */
9334 (_("%H: %s references "
9335 "optimized away TOC entry\n"),
9336 ibfd, sec, rel->r_offset,
9337 ppc64_elf_howto_table[r_type]->name);
9338 bfd_set_error (bfd_error_bad_value);
9339 goto error_ret;
9340 }
9341 rel->r_addend = tocrel->r_addend;
9342 elf_section_data (sec)->relocs = relstart;
9343 continue;
9344 }
9345
9346 if (h != NULL || sym->st_value != 0)
9347 continue;
9348
9349 rel->r_addend -= skip[val >> 3];
9350 elf_section_data (sec)->relocs = relstart;
9351 }
9352
9353 if (elf_section_data (sec)->relocs != relstart)
9354 free (relstart);
9355 }
9356
9357 /* We shouldn't have local or global symbols defined in the TOC,
9358 but handle them anyway. */
9359 if (local_syms != NULL)
9360 for (sym = local_syms;
9361 sym < local_syms + symtab_hdr->sh_info;
9362 ++sym)
9363 if (sym->st_value != 0
9364 && bfd_section_from_elf_index (ibfd, sym->st_shndx) == toc)
9365 {
9366 unsigned long i;
9367
9368 if (sym->st_value > toc->rawsize)
9369 i = toc->rawsize >> 3;
9370 else
9371 i = sym->st_value >> 3;
9372
9373 if ((skip[i] & (ref_from_discarded | can_optimize)) != 0)
9374 {
9375 if (local_toc_syms)
9376 _bfd_error_handler
9377 (_("%s defined on removed toc entry"),
9378 bfd_elf_sym_name (ibfd, symtab_hdr, sym, NULL));
9379 do
9380 ++i;
9381 while ((skip[i] & (ref_from_discarded | can_optimize)));
9382 sym->st_value = (bfd_vma) i << 3;
9383 }
9384
9385 sym->st_value -= skip[i];
9386 symtab_hdr->contents = (unsigned char *) local_syms;
9387 }
9388
9389 /* Adjust any global syms defined in this toc input section. */
9390 if (toc_inf.global_toc_syms)
9391 {
9392 toc_inf.toc = toc;
9393 toc_inf.skip = skip;
9394 toc_inf.global_toc_syms = false;
9395 elf_link_hash_traverse (elf_hash_table (info), adjust_toc_syms,
9396 &toc_inf);
9397 }
9398
9399 if (toc->reloc_count != 0)
9400 {
9401 Elf_Internal_Shdr *rel_hdr;
9402 Elf_Internal_Rela *wrel;
9403 bfd_size_type sz;
9404
9405 /* Remove unused toc relocs, and adjust those we keep. */
9406 if (toc_relocs == NULL)
9407 toc_relocs = _bfd_elf_link_read_relocs (ibfd, toc, NULL, NULL,
9408 info->keep_memory);
9409 if (toc_relocs == NULL)
9410 goto error_ret;
9411
9412 wrel = toc_relocs;
9413 for (rel = toc_relocs; rel < toc_relocs + toc->reloc_count; ++rel)
9414 if ((skip[rel->r_offset >> 3]
9415 & (ref_from_discarded | can_optimize)) == 0)
9416 {
9417 wrel->r_offset = rel->r_offset - skip[rel->r_offset >> 3];
9418 wrel->r_info = rel->r_info;
9419 wrel->r_addend = rel->r_addend;
9420 ++wrel;
9421 }
9422 else if (!dec_dynrel_count (rel->r_info, toc, info,
9423 &local_syms, NULL, NULL))
9424 goto error_ret;
9425
9426 elf_section_data (toc)->relocs = toc_relocs;
9427 toc->reloc_count = wrel - toc_relocs;
9428 rel_hdr = _bfd_elf_single_rel_hdr (toc);
9429 sz = rel_hdr->sh_entsize;
9430 rel_hdr->sh_size = toc->reloc_count * sz;
9431 }
9432 }
9433 else if (elf_section_data (toc)->relocs != toc_relocs)
9434 free (toc_relocs);
9435
9436 if (local_syms != NULL
9437 && symtab_hdr->contents != (unsigned char *) local_syms)
9438 {
9439 if (!info->keep_memory)
9440 free (local_syms);
9441 else
9442 symtab_hdr->contents = (unsigned char *) local_syms;
9443 }
9444 free (skip);
9445 }
9446
9447 /* Look for cases where we can change an indirect GOT access to
9448 a GOT relative or PC relative access, possibly reducing the
9449 number of GOT entries. */
9450 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
9451 {
9452 asection *sec;
9453 Elf_Internal_Shdr *symtab_hdr;
9454 Elf_Internal_Sym *local_syms;
9455 Elf_Internal_Rela *relstart, *rel;
9456 bfd_vma got;
9457
9458 if (!is_ppc64_elf (ibfd))
9459 continue;
9460
9461 if (!ppc64_elf_tdata (ibfd)->has_optrel)
9462 continue;
9463
9464 sec = ppc64_elf_tdata (ibfd)->got;
9465 got = 0;
9466 if (sec != NULL)
9467 got = sec->output_section->vma + sec->output_offset + 0x8000;
9468
9469 local_syms = NULL;
9470 symtab_hdr = &elf_symtab_hdr (ibfd);
9471
9472 for (sec = ibfd->sections; sec != NULL; sec = sec->next)
9473 {
9474 if (sec->reloc_count == 0
9475 || !ppc64_elf_section_data (sec)->has_optrel
9476 || discarded_section (sec))
9477 continue;
9478
9479 relstart = _bfd_elf_link_read_relocs (ibfd, sec, NULL, NULL,
9480 info->keep_memory);
9481 if (relstart == NULL)
9482 {
9483 got_error_ret:
9484 if (symtab_hdr->contents != (unsigned char *) local_syms)
9485 free (local_syms);
9486 if (sec != NULL
9487 && elf_section_data (sec)->relocs != relstart)
9488 free (relstart);
9489 return false;
9490 }
9491
9492 for (rel = relstart; rel < relstart + sec->reloc_count; ++rel)
9493 {
9494 enum elf_ppc64_reloc_type r_type;
9495 unsigned long r_symndx;
9496 Elf_Internal_Sym *sym;
9497 asection *sym_sec;
9498 struct elf_link_hash_entry *h;
9499 struct got_entry *ent;
9500 bfd_vma val, pc;
9501 unsigned char buf[8];
9502 unsigned int insn;
9503 enum {no_check, check_lo, check_ha} insn_check;
9504
9505 r_type = ELF64_R_TYPE (rel->r_info);
9506 switch (r_type)
9507 {
9508 default:
9509 insn_check = no_check;
9510 break;
9511
9512 case R_PPC64_PLT16_HA:
9513 case R_PPC64_GOT_TLSLD16_HA:
9514 case R_PPC64_GOT_TLSGD16_HA:
9515 case R_PPC64_GOT_TPREL16_HA:
9516 case R_PPC64_GOT_DTPREL16_HA:
9517 case R_PPC64_GOT16_HA:
9518 case R_PPC64_TOC16_HA:
9519 insn_check = check_ha;
9520 break;
9521
9522 case R_PPC64_PLT16_LO:
9523 case R_PPC64_PLT16_LO_DS:
9524 case R_PPC64_GOT_TLSLD16_LO:
9525 case R_PPC64_GOT_TLSGD16_LO:
9526 case R_PPC64_GOT_TPREL16_LO_DS:
9527 case R_PPC64_GOT_DTPREL16_LO_DS:
9528 case R_PPC64_GOT16_LO:
9529 case R_PPC64_GOT16_LO_DS:
9530 case R_PPC64_TOC16_LO:
9531 case R_PPC64_TOC16_LO_DS:
9532 insn_check = check_lo;
9533 break;
9534 }
9535
9536 if (insn_check != no_check)
9537 {
9538 bfd_vma off = rel->r_offset & ~3;
9539
9540 if (!bfd_get_section_contents (ibfd, sec, buf, off, 4))
9541 goto got_error_ret;
9542
9543 insn = bfd_get_32 (ibfd, buf);
9544 if (insn_check == check_lo
9545 ? !ok_lo_toc_insn (insn, r_type)
9546 : ((insn & ((0x3fu << 26) | 0x1f << 16))
9547 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9548 {
9549 char str[12];
9550
9551 ppc64_elf_tdata (ibfd)->unexpected_toc_insn = 1;
9552 sprintf (str, "%#08x", insn);
9553 info->callbacks->einfo
9554 /* xgettext:c-format */
9555 (_("%H: got/toc optimization is not supported for"
9556 " %s instruction\n"),
9557 ibfd, sec, rel->r_offset & ~3, str);
9558 continue;
9559 }
9560 }
9561
9562 switch (r_type)
9563 {
9564 /* Note that we don't delete GOT entries for
9565 R_PPC64_GOT16_DS since we'd need a lot more
9566 analysis. For starters, the preliminary layout is
9567 before the GOT, PLT, dynamic sections and stubs are
9568 laid out. Then we'd need to allow for changes in
9569 distance between sections caused by alignment. */
9570 default:
9571 continue;
9572
9573 case R_PPC64_GOT16_HA:
9574 case R_PPC64_GOT16_LO_DS:
9575 case R_PPC64_GOT_PCREL34:
9576 break;
9577 }
9578
9579 r_symndx = ELF64_R_SYM (rel->r_info);
9580 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
9581 r_symndx, ibfd))
9582 goto got_error_ret;
9583
9584 if (sym_sec == NULL
9585 || sym_sec->output_section == NULL
9586 || discarded_section (sym_sec))
9587 continue;
9588
9589 if ((h ? h->type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC)
9590 continue;
9591
9592 if (!SYMBOL_REFERENCES_LOCAL (info, h))
9593 continue;
9594
9595 if (h != NULL)
9596 val = h->root.u.def.value;
9597 else
9598 val = sym->st_value;
9599 val += rel->r_addend;
9600 val += sym_sec->output_section->vma + sym_sec->output_offset;
9601
9602 /* Fudge factor to allow for the fact that the preliminary layout
9603 isn't exact. Reduce limits by this factor. */
9604 #define LIMIT_ADJUST(LIMIT) ((LIMIT) - (LIMIT) / 16)
9605
9606 switch (r_type)
9607 {
9608 default:
9609 continue;
9610
9611 case R_PPC64_GOT16_HA:
9612 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9613 >= LIMIT_ADJUST (0x100000000ULL))
9614 continue;
9615
9616 if (!bfd_get_section_contents (ibfd, sec, buf,
9617 rel->r_offset & ~3, 4))
9618 goto got_error_ret;
9619 insn = bfd_get_32 (ibfd, buf);
9620 if (((insn & ((0x3fu << 26) | 0x1f << 16))
9621 != ((15u << 26) | (2 << 16)) /* addis rt,2,imm */))
9622 continue;
9623 break;
9624
9625 case R_PPC64_GOT16_LO_DS:
9626 if (val - got + LIMIT_ADJUST (0x80008000ULL)
9627 >= LIMIT_ADJUST (0x100000000ULL))
9628 continue;
9629 if (!bfd_get_section_contents (ibfd, sec, buf,
9630 rel->r_offset & ~3, 4))
9631 goto got_error_ret;
9632 insn = bfd_get_32 (ibfd, buf);
9633 if ((insn & (0x3fu << 26 | 0x3)) != 58u << 26 /* ld */)
9634 continue;
9635 break;
9636
9637 case R_PPC64_GOT_PCREL34:
9638 pc = rel->r_offset;
9639 pc += sec->output_section->vma + sec->output_offset;
9640 if (val - pc + LIMIT_ADJUST (1ULL << 33)
9641 >= LIMIT_ADJUST (1ULL << 34))
9642 continue;
9643 if (!bfd_get_section_contents (ibfd, sec, buf,
9644 rel->r_offset & ~3, 8))
9645 goto got_error_ret;
9646 insn = bfd_get_32 (ibfd, buf);
9647 if ((insn & (-1u << 18)) != ((1u << 26) | (1u << 20)))
9648 continue;
9649 insn = bfd_get_32 (ibfd, buf + 4);
9650 if ((insn & (0x3fu << 26)) != 57u << 26)
9651 continue;
9652 break;
9653 }
9654 #undef LIMIT_ADJUST
9655
9656 if (h != NULL)
9657 ent = h->got.glist;
9658 else
9659 {
9660 struct got_entry **local_got_ents = elf_local_got_ents (ibfd);
9661 ent = local_got_ents[r_symndx];
9662 }
9663 for (; ent != NULL; ent = ent->next)
9664 if (ent->addend == rel->r_addend
9665 && ent->owner == ibfd
9666 && ent->tls_type == 0)
9667 break;
9668 BFD_ASSERT (ent && ent->got.refcount > 0);
9669 ent->got.refcount -= 1;
9670 }
9671
9672 if (elf_section_data (sec)->relocs != relstart)
9673 free (relstart);
9674 }
9675
9676 if (local_syms != NULL
9677 && symtab_hdr->contents != (unsigned char *) local_syms)
9678 {
9679 if (!info->keep_memory)
9680 free (local_syms);
9681 else
9682 symtab_hdr->contents = (unsigned char *) local_syms;
9683 }
9684 }
9685
9686 return true;
9687 }
9688
9689 /* Return true iff input section I references the TOC using
9690 instructions limited to +/-32k offsets. */
9691
9692 bool
9693 ppc64_elf_has_small_toc_reloc (asection *i)
9694 {
9695 return (is_ppc64_elf (i->owner)
9696 && ppc64_elf_tdata (i->owner)->has_small_toc_reloc);
9697 }
9698
9699 /* Allocate space for one GOT entry. */
9700
9701 static void
9702 allocate_got (struct elf_link_hash_entry *h,
9703 struct bfd_link_info *info,
9704 struct got_entry *gent)
9705 {
9706 struct ppc_link_hash_table *htab = ppc_hash_table (info);
9707 struct ppc_link_hash_entry *eh = ppc_elf_hash_entry (h);
9708 int entsize = (gent->tls_type & eh->tls_mask & (TLS_GD | TLS_LD)
9709 ? 16 : 8);
9710 int rentsize = (gent->tls_type & eh->tls_mask & TLS_GD
9711 ? 2 : 1) * sizeof (Elf64_External_Rela);
9712 asection *got = ppc64_elf_tdata (gent->owner)->got;
9713
9714 gent->got.offset = got->size;
9715 got->size += entsize;
9716
9717 if (h->type == STT_GNU_IFUNC)
9718 {
9719 htab->elf.irelplt->size += rentsize;
9720 htab->got_reli_size += rentsize;
9721 }
9722 else if (((bfd_link_pic (info)
9723 && !(gent->tls_type != 0
9724 && bfd_link_executable (info)
9725 && SYMBOL_REFERENCES_LOCAL (info, h)))
9726 || (htab->elf.dynamic_sections_created
9727 && h->dynindx != -1
9728 && !SYMBOL_REFERENCES_LOCAL (info, h)))
9729 && !UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9730 {
9731 asection *relgot = ppc64_elf_tdata (gent->owner)->relgot;
9732 relgot->size += rentsize;
9733 }
9734 }
9735
9736 /* This function merges got entries in the same toc group. */
9737
9738 static void
9739 merge_got_entries (struct got_entry **pent)
9740 {
9741 struct got_entry *ent, *ent2;
9742
9743 for (ent = *pent; ent != NULL; ent = ent->next)
9744 if (!ent->is_indirect)
9745 for (ent2 = ent->next; ent2 != NULL; ent2 = ent2->next)
9746 if (!ent2->is_indirect
9747 && ent2->addend == ent->addend
9748 && ent2->tls_type == ent->tls_type
9749 && elf_gp (ent2->owner) == elf_gp (ent->owner))
9750 {
9751 ent2->is_indirect = true;
9752 ent2->got.ent = ent;
9753 }
9754 }
9755
9756 /* If H is undefined, make it dynamic if that makes sense. */
9757
9758 static bool
9759 ensure_undef_dynamic (struct bfd_link_info *info,
9760 struct elf_link_hash_entry *h)
9761 {
9762 struct elf_link_hash_table *htab = elf_hash_table (info);
9763
9764 if (htab->dynamic_sections_created
9765 && ((info->dynamic_undefined_weak != 0
9766 && h->root.type == bfd_link_hash_undefweak)
9767 || h->root.type == bfd_link_hash_undefined)
9768 && h->dynindx == -1
9769 && !h->forced_local
9770 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
9771 return bfd_elf_link_record_dynamic_symbol (info, h);
9772 return true;
9773 }
9774
9775 /* Choose whether to use htab->iplt or htab->pltlocal rather than the
9776 usual htab->elf.splt section for a PLT entry. */
9777
9778 static inline
9779 bool use_local_plt (struct bfd_link_info *info,
9780 struct elf_link_hash_entry *h)
9781 {
9782 return (h == NULL
9783 || h->dynindx == -1
9784 || !elf_hash_table (info)->dynamic_sections_created);
9785 }
9786
9787 /* Allocate space in .plt, .got and associated reloc sections for
9788 dynamic relocs. */
9789
9790 static bool
9791 allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
9792 {
9793 struct bfd_link_info *info;
9794 struct ppc_link_hash_table *htab;
9795 asection *s;
9796 struct ppc_link_hash_entry *eh;
9797 struct got_entry **pgent, *gent;
9798
9799 if (h->root.type == bfd_link_hash_indirect)
9800 return true;
9801
9802 info = (struct bfd_link_info *) inf;
9803 htab = ppc_hash_table (info);
9804 if (htab == NULL)
9805 return false;
9806
9807 eh = ppc_elf_hash_entry (h);
9808 /* Run through the TLS GD got entries first if we're changing them
9809 to TPREL. */
9810 if ((eh->tls_mask & (TLS_TLS | TLS_GDIE)) == (TLS_TLS | TLS_GDIE))
9811 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9812 if (gent->got.refcount > 0
9813 && (gent->tls_type & TLS_GD) != 0)
9814 {
9815 /* This was a GD entry that has been converted to TPREL. If
9816 there happens to be a TPREL entry we can use that one. */
9817 struct got_entry *ent;
9818 for (ent = h->got.glist; ent != NULL; ent = ent->next)
9819 if (ent->got.refcount > 0
9820 && (ent->tls_type & TLS_TPREL) != 0
9821 && ent->addend == gent->addend
9822 && ent->owner == gent->owner)
9823 {
9824 gent->got.refcount = 0;
9825 break;
9826 }
9827
9828 /* If not, then we'll be using our own TPREL entry. */
9829 if (gent->got.refcount != 0)
9830 gent->tls_type = TLS_TLS | TLS_TPREL;
9831 }
9832
9833 /* Remove any list entry that won't generate a word in the GOT before
9834 we call merge_got_entries. Otherwise we risk merging to empty
9835 entries. */
9836 pgent = &h->got.glist;
9837 while ((gent = *pgent) != NULL)
9838 if (gent->got.refcount > 0)
9839 {
9840 if ((gent->tls_type & TLS_LD) != 0
9841 && SYMBOL_REFERENCES_LOCAL (info, h))
9842 {
9843 ppc64_tlsld_got (gent->owner)->got.refcount += 1;
9844 *pgent = gent->next;
9845 }
9846 else
9847 pgent = &gent->next;
9848 }
9849 else
9850 *pgent = gent->next;
9851
9852 if (!htab->do_multi_toc)
9853 merge_got_entries (&h->got.glist);
9854
9855 for (gent = h->got.glist; gent != NULL; gent = gent->next)
9856 if (!gent->is_indirect)
9857 {
9858 /* Ensure we catch all the cases where this symbol should
9859 be made dynamic. */
9860 if (!ensure_undef_dynamic (info, h))
9861 return false;
9862
9863 if (!is_ppc64_elf (gent->owner))
9864 abort ();
9865
9866 allocate_got (h, info, gent);
9867 }
9868
9869 /* If no dynamic sections we can't have dynamic relocs, except for
9870 IFUNCs which are handled even in static executables. */
9871 if (!htab->elf.dynamic_sections_created
9872 && h->type != STT_GNU_IFUNC)
9873 h->dyn_relocs = NULL;
9874
9875 /* Discard relocs on undefined symbols that must be local. */
9876 else if (h->root.type == bfd_link_hash_undefined
9877 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
9878 h->dyn_relocs = NULL;
9879
9880 /* Also discard relocs on undefined weak syms with non-default
9881 visibility, or when dynamic_undefined_weak says so. */
9882 else if (UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
9883 h->dyn_relocs = NULL;
9884
9885 if (h->dyn_relocs != NULL)
9886 {
9887 struct elf_dyn_relocs *p, **pp;
9888
9889 /* In the shared -Bsymbolic case, discard space allocated for
9890 dynamic pc-relative relocs against symbols which turn out to
9891 be defined in regular objects. For the normal shared case,
9892 discard space for relocs that have become local due to symbol
9893 visibility changes. */
9894 if (bfd_link_pic (info))
9895 {
9896 /* Relocs that use pc_count are those that appear on a call
9897 insn, or certain REL relocs (see must_be_dyn_reloc) that
9898 can be generated via assembly. We want calls to
9899 protected symbols to resolve directly to the function
9900 rather than going via the plt. If people want function
9901 pointer comparisons to work as expected then they should
9902 avoid writing weird assembly. */
9903 if (SYMBOL_CALLS_LOCAL (info, h))
9904 {
9905 for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
9906 {
9907 p->count -= p->pc_count;
9908 p->pc_count = 0;
9909 if (p->count == 0)
9910 *pp = p->next;
9911 else
9912 pp = &p->next;
9913 }
9914 }
9915
9916 if (h->dyn_relocs != NULL)
9917 {
9918 /* Ensure we catch all the cases where this symbol
9919 should be made dynamic. */
9920 if (!ensure_undef_dynamic (info, h))
9921 return false;
9922 }
9923 }
9924
9925 /* For a fixed position executable, discard space for
9926 relocs against symbols which are not dynamic. */
9927 else if (h->type != STT_GNU_IFUNC)
9928 {
9929 if ((h->dynamic_adjusted
9930 || (h->ref_regular
9931 && h->root.type == bfd_link_hash_undefweak
9932 && (info->dynamic_undefined_weak > 0
9933 || !_bfd_elf_readonly_dynrelocs (h))))
9934 && !h->def_regular
9935 && !ELF_COMMON_DEF_P (h))
9936 {
9937 /* Ensure we catch all the cases where this symbol
9938 should be made dynamic. */
9939 if (!ensure_undef_dynamic (info, h))
9940 return false;
9941
9942 /* But if that didn't work out, discard dynamic relocs. */
9943 if (h->dynindx == -1)
9944 h->dyn_relocs = NULL;
9945 }
9946 else
9947 h->dyn_relocs = NULL;
9948 }
9949
9950 /* Finally, allocate space. */
9951 for (p = h->dyn_relocs; p != NULL; p = p->next)
9952 {
9953 asection *sreloc = elf_section_data (p->sec)->sreloc;
9954 if (eh->elf.type == STT_GNU_IFUNC)
9955 sreloc = htab->elf.irelplt;
9956 sreloc->size += p->count * sizeof (Elf64_External_Rela);
9957 }
9958 }
9959
9960 /* We might need a PLT entry when the symbol
9961 a) is dynamic, or
9962 b) is an ifunc, or
9963 c) has plt16 relocs and has been processed by adjust_dynamic_symbol, or
9964 d) has plt16 relocs and we are linking statically. */
9965 if ((htab->elf.dynamic_sections_created && h->dynindx != -1)
9966 || h->type == STT_GNU_IFUNC
9967 || (h->needs_plt && h->dynamic_adjusted)
9968 || (h->needs_plt
9969 && h->def_regular
9970 && !htab->elf.dynamic_sections_created
9971 && !htab->can_convert_all_inline_plt
9972 && (ppc_elf_hash_entry (h)->tls_mask
9973 & (TLS_TLS | PLT_KEEP)) == PLT_KEEP))
9974 {
9975 struct plt_entry *pent;
9976 bool doneone = false;
9977 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
9978 if (pent->plt.refcount > 0)
9979 {
9980 if (!ensure_undef_dynamic (info, h))
9981 return false;
9982
9983 if (use_local_plt (info, h))
9984 {
9985 if (h->type == STT_GNU_IFUNC)
9986 {
9987 s = htab->elf.iplt;
9988 pent->plt.offset = s->size;
9989 s->size += PLT_ENTRY_SIZE (htab);
9990 s = htab->elf.irelplt;
9991 }
9992 else
9993 {
9994 s = htab->pltlocal;
9995 pent->plt.offset = s->size;
9996 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
9997 s = bfd_link_pic (info) ? htab->relpltlocal : NULL;
9998 }
9999 }
10000 else
10001 {
10002 /* If this is the first .plt entry, make room for the special
10003 first entry. */
10004 s = htab->elf.splt;
10005 if (s->size == 0)
10006 s->size += PLT_INITIAL_ENTRY_SIZE (htab);
10007
10008 pent->plt.offset = s->size;
10009
10010 /* Make room for this entry. */
10011 s->size += PLT_ENTRY_SIZE (htab);
10012
10013 /* Make room for the .glink code. */
10014 s = htab->glink;
10015 if (s->size == 0)
10016 s->size += GLINK_PLTRESOLVE_SIZE (htab);
10017 if (htab->opd_abi)
10018 {
10019 /* We need bigger stubs past index 32767. */
10020 if (s->size >= GLINK_PLTRESOLVE_SIZE (htab) + 32768*2*4)
10021 s->size += 4;
10022 s->size += 2*4;
10023 }
10024 else
10025 s->size += 4;
10026
10027 /* We also need to make an entry in the .rela.plt section. */
10028 s = htab->elf.srelplt;
10029 }
10030 if (s != NULL)
10031 s->size += sizeof (Elf64_External_Rela);
10032 doneone = true;
10033 }
10034 else
10035 pent->plt.offset = (bfd_vma) -1;
10036 if (!doneone)
10037 {
10038 h->plt.plist = NULL;
10039 h->needs_plt = 0;
10040 }
10041 }
10042 else
10043 {
10044 h->plt.plist = NULL;
10045 h->needs_plt = 0;
10046 }
10047
10048 return true;
10049 }
10050
10051 #define PPC_LO(v) ((v) & 0xffff)
10052 #define PPC_HI(v) (((v) >> 16) & 0xffff)
10053 #define PPC_HA(v) PPC_HI ((v) + 0x8000)
10054 #define D34(v) \
10055 ((((v) & 0x3ffff0000ULL) << 16) | (v & 0xffff))
10056 #define HA34(v) ((v + (1ULL << 33)) >> 34)
10057
10058 /* Called via elf_link_hash_traverse from ppc64_elf_size_dynamic_sections
10059 to set up space for global entry stubs. These are put in glink,
10060 after the branch table. */
10061
10062 static bool
10063 size_global_entry_stubs (struct elf_link_hash_entry *h, void *inf)
10064 {
10065 struct bfd_link_info *info;
10066 struct ppc_link_hash_table *htab;
10067 struct plt_entry *pent;
10068 asection *s, *plt;
10069
10070 if (h->root.type == bfd_link_hash_indirect)
10071 return true;
10072
10073 if (!h->pointer_equality_needed)
10074 return true;
10075
10076 if (h->def_regular)
10077 return true;
10078
10079 info = inf;
10080 htab = ppc_hash_table (info);
10081 if (htab == NULL)
10082 return false;
10083
10084 s = htab->global_entry;
10085 plt = htab->elf.splt;
10086 for (pent = h->plt.plist; pent != NULL; pent = pent->next)
10087 if (pent->plt.offset != (bfd_vma) -1
10088 && pent->addend == 0)
10089 {
10090 /* For ELFv2, if this symbol is not defined in a regular file
10091 and we are not generating a shared library or pie, then we
10092 need to define the symbol in the executable on a call stub.
10093 This is to avoid text relocations. */
10094 bfd_vma off, stub_align, stub_off, stub_size;
10095 unsigned int align_power;
10096
10097 stub_size = 16;
10098 stub_off = s->size;
10099 if (htab->params->plt_stub_align >= 0)
10100 align_power = htab->params->plt_stub_align;
10101 else
10102 align_power = -htab->params->plt_stub_align;
10103 /* Setting section alignment is delayed until we know it is
10104 non-empty. Otherwise the .text output section will be
10105 aligned at least to plt_stub_align even when no global
10106 entry stubs are needed. */
10107 if (s->alignment_power < align_power)
10108 s->alignment_power = align_power;
10109 stub_align = (bfd_vma) 1 << align_power;
10110 if (htab->params->plt_stub_align >= 0
10111 || ((((stub_off + stub_size - 1) & -stub_align)
10112 - (stub_off & -stub_align))
10113 > ((stub_size - 1) & -stub_align)))
10114 stub_off = (stub_off + stub_align - 1) & -stub_align;
10115 off = pent->plt.offset + plt->output_offset + plt->output_section->vma;
10116 off -= stub_off + s->output_offset + s->output_section->vma;
10117 /* Note that for --plt-stub-align negative we have a possible
10118 dependency between stub offset and size. Break that
10119 dependency by assuming the max stub size when calculating
10120 the stub offset. */
10121 if (PPC_HA (off) == 0)
10122 stub_size -= 4;
10123 h->root.type = bfd_link_hash_defined;
10124 h->root.u.def.section = s;
10125 h->root.u.def.value = stub_off;
10126 s->size = stub_off + stub_size;
10127 break;
10128 }
10129 return true;
10130 }
10131
10132 /* Set the sizes of the dynamic sections. */
10133
10134 static bool
10135 ppc64_elf_size_dynamic_sections (bfd *output_bfd,
10136 struct bfd_link_info *info)
10137 {
10138 struct ppc_link_hash_table *htab;
10139 bfd *dynobj;
10140 asection *s;
10141 bool relocs;
10142 bfd *ibfd;
10143 struct got_entry *first_tlsld;
10144
10145 htab = ppc_hash_table (info);
10146 if (htab == NULL)
10147 return false;
10148
10149 dynobj = htab->elf.dynobj;
10150 if (dynobj == NULL)
10151 abort ();
10152
10153 if (htab->elf.dynamic_sections_created)
10154 {
10155 /* Set the contents of the .interp section to the interpreter. */
10156 if (bfd_link_executable (info) && !info->nointerp)
10157 {
10158 s = bfd_get_linker_section (dynobj, ".interp");
10159 if (s == NULL)
10160 abort ();
10161 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
10162 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
10163 }
10164 }
10165
10166 /* Set up .got offsets for local syms, and space for local dynamic
10167 relocs. */
10168 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10169 {
10170 struct got_entry **lgot_ents;
10171 struct got_entry **end_lgot_ents;
10172 struct plt_entry **local_plt;
10173 struct plt_entry **end_local_plt;
10174 unsigned char *lgot_masks;
10175 bfd_size_type locsymcount;
10176 Elf_Internal_Shdr *symtab_hdr;
10177
10178 if (!is_ppc64_elf (ibfd))
10179 continue;
10180
10181 for (s = ibfd->sections; s != NULL; s = s->next)
10182 {
10183 struct ppc_dyn_relocs *p;
10184
10185 for (p = elf_section_data (s)->local_dynrel; p != NULL; p = p->next)
10186 {
10187 if (!bfd_is_abs_section (p->sec)
10188 && bfd_is_abs_section (p->sec->output_section))
10189 {
10190 /* Input section has been discarded, either because
10191 it is a copy of a linkonce section or due to
10192 linker script /DISCARD/, so we'll be discarding
10193 the relocs too. */
10194 }
10195 else if (p->count != 0)
10196 {
10197 asection *srel = elf_section_data (p->sec)->sreloc;
10198 if (p->ifunc)
10199 srel = htab->elf.irelplt;
10200 srel->size += p->count * sizeof (Elf64_External_Rela);
10201 if ((p->sec->output_section->flags & SEC_READONLY) != 0)
10202 info->flags |= DF_TEXTREL;
10203 }
10204 }
10205 }
10206
10207 lgot_ents = elf_local_got_ents (ibfd);
10208 if (!lgot_ents)
10209 continue;
10210
10211 symtab_hdr = &elf_symtab_hdr (ibfd);
10212 locsymcount = symtab_hdr->sh_info;
10213 end_lgot_ents = lgot_ents + locsymcount;
10214 local_plt = (struct plt_entry **) end_lgot_ents;
10215 end_local_plt = local_plt + locsymcount;
10216 lgot_masks = (unsigned char *) end_local_plt;
10217 s = ppc64_elf_tdata (ibfd)->got;
10218 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
10219 {
10220 struct got_entry **pent, *ent;
10221
10222 pent = lgot_ents;
10223 while ((ent = *pent) != NULL)
10224 if (ent->got.refcount > 0)
10225 {
10226 if ((ent->tls_type & *lgot_masks & TLS_LD) != 0)
10227 {
10228 ppc64_tlsld_got (ibfd)->got.refcount += 1;
10229 *pent = ent->next;
10230 }
10231 else
10232 {
10233 unsigned int ent_size = 8;
10234 unsigned int rel_size = sizeof (Elf64_External_Rela);
10235
10236 ent->got.offset = s->size;
10237 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
10238 {
10239 ent_size *= 2;
10240 rel_size *= 2;
10241 }
10242 s->size += ent_size;
10243 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10244 {
10245 htab->elf.irelplt->size += rel_size;
10246 htab->got_reli_size += rel_size;
10247 }
10248 else if (bfd_link_pic (info)
10249 && !(ent->tls_type != 0
10250 && bfd_link_executable (info)))
10251 {
10252 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10253 srel->size += rel_size;
10254 }
10255 pent = &ent->next;
10256 }
10257 }
10258 else
10259 *pent = ent->next;
10260 }
10261
10262 /* Allocate space for plt calls to local syms. */
10263 lgot_masks = (unsigned char *) end_local_plt;
10264 for (; local_plt < end_local_plt; ++local_plt, ++lgot_masks)
10265 {
10266 struct plt_entry *ent;
10267
10268 for (ent = *local_plt; ent != NULL; ent = ent->next)
10269 if (ent->plt.refcount > 0)
10270 {
10271 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
10272 {
10273 s = htab->elf.iplt;
10274 ent->plt.offset = s->size;
10275 s->size += PLT_ENTRY_SIZE (htab);
10276 htab->elf.irelplt->size += sizeof (Elf64_External_Rela);
10277 }
10278 else if (htab->can_convert_all_inline_plt
10279 || (*lgot_masks & (TLS_TLS | PLT_KEEP)) != PLT_KEEP)
10280 ent->plt.offset = (bfd_vma) -1;
10281 else
10282 {
10283 s = htab->pltlocal;
10284 ent->plt.offset = s->size;
10285 s->size += LOCAL_PLT_ENTRY_SIZE (htab);
10286 if (bfd_link_pic (info))
10287 htab->relpltlocal->size += sizeof (Elf64_External_Rela);
10288 }
10289 }
10290 else
10291 ent->plt.offset = (bfd_vma) -1;
10292 }
10293 }
10294
10295 /* Allocate global sym .plt and .got entries, and space for global
10296 sym dynamic relocs. */
10297 elf_link_hash_traverse (&htab->elf, allocate_dynrelocs, info);
10298
10299 if (!htab->opd_abi && !bfd_link_pic (info))
10300 elf_link_hash_traverse (&htab->elf, size_global_entry_stubs, info);
10301
10302 first_tlsld = NULL;
10303 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10304 {
10305 struct got_entry *ent;
10306
10307 if (!is_ppc64_elf (ibfd))
10308 continue;
10309
10310 ent = ppc64_tlsld_got (ibfd);
10311 if (ent->got.refcount > 0)
10312 {
10313 if (!htab->do_multi_toc && first_tlsld != NULL)
10314 {
10315 ent->is_indirect = true;
10316 ent->got.ent = first_tlsld;
10317 }
10318 else
10319 {
10320 if (first_tlsld == NULL)
10321 first_tlsld = ent;
10322 s = ppc64_elf_tdata (ibfd)->got;
10323 ent->got.offset = s->size;
10324 ent->owner = ibfd;
10325 s->size += 16;
10326 if (bfd_link_dll (info))
10327 {
10328 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
10329 srel->size += sizeof (Elf64_External_Rela);
10330 }
10331 }
10332 }
10333 else
10334 ent->got.offset = (bfd_vma) -1;
10335 }
10336
10337 /* We now have determined the sizes of the various dynamic sections.
10338 Allocate memory for them. */
10339 relocs = false;
10340 for (s = dynobj->sections; s != NULL; s = s->next)
10341 {
10342 if ((s->flags & SEC_LINKER_CREATED) == 0)
10343 continue;
10344
10345 if (s == htab->brlt || s == htab->relbrlt)
10346 /* These haven't been allocated yet; don't strip. */
10347 continue;
10348 else if (s == htab->elf.sgot
10349 || s == htab->elf.splt
10350 || s == htab->elf.iplt
10351 || s == htab->pltlocal
10352 || s == htab->glink
10353 || s == htab->global_entry
10354 || s == htab->elf.sdynbss
10355 || s == htab->elf.sdynrelro)
10356 {
10357 /* Strip this section if we don't need it; see the
10358 comment below. */
10359 }
10360 else if (s == htab->glink_eh_frame)
10361 {
10362 if (!bfd_is_abs_section (s->output_section))
10363 /* Not sized yet. */
10364 continue;
10365 }
10366 else if (startswith (s->name, ".rela"))
10367 {
10368 if (s->size != 0)
10369 {
10370 if (s != htab->elf.srelplt)
10371 relocs = true;
10372
10373 /* We use the reloc_count field as a counter if we need
10374 to copy relocs into the output file. */
10375 s->reloc_count = 0;
10376 }
10377 }
10378 else
10379 {
10380 /* It's not one of our sections, so don't allocate space. */
10381 continue;
10382 }
10383
10384 if (s->size == 0)
10385 {
10386 /* If we don't need this section, strip it from the
10387 output file. This is mostly to handle .rela.bss and
10388 .rela.plt. We must create both sections in
10389 create_dynamic_sections, because they must be created
10390 before the linker maps input sections to output
10391 sections. The linker does that before
10392 adjust_dynamic_symbol is called, and it is that
10393 function which decides whether anything needs to go
10394 into these sections. */
10395 s->flags |= SEC_EXCLUDE;
10396 continue;
10397 }
10398
10399 if (bfd_is_abs_section (s->output_section))
10400 _bfd_error_handler (_("warning: discarding dynamic section %s"),
10401 s->name);
10402
10403 if ((s->flags & SEC_HAS_CONTENTS) == 0)
10404 continue;
10405
10406 /* Allocate memory for the section contents. We use bfd_zalloc
10407 here in case unused entries are not reclaimed before the
10408 section's contents are written out. This should not happen,
10409 but this way if it does we get a R_PPC64_NONE reloc in .rela
10410 sections instead of garbage.
10411 We also rely on the section contents being zero when writing
10412 the GOT and .dynrelro. */
10413 s->contents = bfd_zalloc (dynobj, s->size);
10414 if (s->contents == NULL)
10415 return false;
10416 }
10417
10418 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
10419 {
10420 if (!is_ppc64_elf (ibfd))
10421 continue;
10422
10423 s = ppc64_elf_tdata (ibfd)->got;
10424 if (s != NULL && s != htab->elf.sgot)
10425 {
10426 if (s->size == 0)
10427 s->flags |= SEC_EXCLUDE;
10428 else
10429 {
10430 s->contents = bfd_zalloc (ibfd, s->size);
10431 if (s->contents == NULL)
10432 return false;
10433 }
10434 }
10435 s = ppc64_elf_tdata (ibfd)->relgot;
10436 if (s != NULL)
10437 {
10438 if (s->size == 0)
10439 s->flags |= SEC_EXCLUDE;
10440 else
10441 {
10442 s->contents = bfd_zalloc (ibfd, s->size);
10443 if (s->contents == NULL)
10444 return false;
10445 relocs = true;
10446 s->reloc_count = 0;
10447 }
10448 }
10449 }
10450
10451 if (htab->elf.dynamic_sections_created)
10452 {
10453 bool tls_opt;
10454
10455 /* Add some entries to the .dynamic section. We fill in the
10456 values later, in ppc64_elf_finish_dynamic_sections, but we
10457 must add the entries now so that we get the correct size for
10458 the .dynamic section. The DT_DEBUG entry is filled in by the
10459 dynamic linker and used by the debugger. */
10460 #define add_dynamic_entry(TAG, VAL) \
10461 _bfd_elf_add_dynamic_entry (info, TAG, VAL)
10462
10463 if (bfd_link_executable (info))
10464 {
10465 if (!add_dynamic_entry (DT_DEBUG, 0))
10466 return false;
10467 }
10468
10469 if (htab->elf.splt != NULL && htab->elf.splt->size != 0)
10470 {
10471 if (!add_dynamic_entry (DT_PLTGOT, 0)
10472 || !add_dynamic_entry (DT_PLTRELSZ, 0)
10473 || !add_dynamic_entry (DT_PLTREL, DT_RELA)
10474 || !add_dynamic_entry (DT_JMPREL, 0)
10475 || !add_dynamic_entry (DT_PPC64_GLINK, 0))
10476 return false;
10477 }
10478
10479 if (NO_OPD_RELOCS && abiversion (output_bfd) <= 1)
10480 {
10481 if (!add_dynamic_entry (DT_PPC64_OPD, 0)
10482 || !add_dynamic_entry (DT_PPC64_OPDSZ, 0))
10483 return false;
10484 }
10485
10486 tls_opt = (htab->params->tls_get_addr_opt
10487 && ((htab->tls_get_addr_fd != NULL
10488 && htab->tls_get_addr_fd->elf.plt.plist != NULL)
10489 || (htab->tga_desc_fd != NULL
10490 && htab->tga_desc_fd->elf.plt.plist != NULL)));
10491 if (tls_opt || !htab->opd_abi)
10492 {
10493 if (!add_dynamic_entry (DT_PPC64_OPT, tls_opt ? PPC64_OPT_TLS : 0))
10494 return false;
10495 }
10496
10497 if (relocs)
10498 {
10499 if (!add_dynamic_entry (DT_RELA, 0)
10500 || !add_dynamic_entry (DT_RELASZ, 0)
10501 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf64_External_Rela)))
10502 return false;
10503
10504 /* If any dynamic relocs apply to a read-only section,
10505 then we need a DT_TEXTREL entry. */
10506 if ((info->flags & DF_TEXTREL) == 0)
10507 elf_link_hash_traverse (&htab->elf,
10508 _bfd_elf_maybe_set_textrel, info);
10509
10510 if ((info->flags & DF_TEXTREL) != 0)
10511 {
10512 if (!add_dynamic_entry (DT_TEXTREL, 0))
10513 return false;
10514 }
10515 }
10516 }
10517 #undef add_dynamic_entry
10518
10519 return true;
10520 }
10521
10522 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */
10523
10524 static bool
10525 ppc64_elf_hash_symbol (struct elf_link_hash_entry *h)
10526 {
10527 if (h->plt.plist != NULL
10528 && !h->def_regular
10529 && !h->pointer_equality_needed)
10530 return false;
10531
10532 return _bfd_elf_hash_symbol (h);
10533 }
10534
10535 /* Determine the type of stub needed, if any, for a call. */
10536
10537 static inline enum ppc_stub_main_type
10538 ppc_type_of_stub (asection *input_sec,
10539 const Elf_Internal_Rela *rel,
10540 struct ppc_link_hash_entry **hash,
10541 struct plt_entry **plt_ent,
10542 bfd_vma destination,
10543 unsigned long local_off)
10544 {
10545 struct ppc_link_hash_entry *h = *hash;
10546 bfd_vma location;
10547 bfd_vma branch_offset;
10548 bfd_vma max_branch_offset;
10549 enum elf_ppc64_reloc_type r_type;
10550
10551 if (h != NULL)
10552 {
10553 struct plt_entry *ent;
10554 struct ppc_link_hash_entry *fdh = h;
10555 if (h->oh != NULL
10556 && h->oh->is_func_descriptor)
10557 {
10558 fdh = ppc_follow_link (h->oh);
10559 *hash = fdh;
10560 }
10561
10562 for (ent = fdh->elf.plt.plist; ent != NULL; ent = ent->next)
10563 if (ent->addend == rel->r_addend
10564 && ent->plt.offset != (bfd_vma) -1)
10565 {
10566 *plt_ent = ent;
10567 return ppc_stub_plt_call;
10568 }
10569
10570 /* Here, we know we don't have a plt entry. If we don't have a
10571 either a defined function descriptor or a defined entry symbol
10572 in a regular object file, then it is pointless trying to make
10573 any other type of stub. */
10574 if (!is_static_defined (&fdh->elf)
10575 && !is_static_defined (&h->elf))
10576 return ppc_stub_none;
10577 }
10578 else if (elf_local_got_ents (input_sec->owner) != NULL)
10579 {
10580 Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (input_sec->owner);
10581 struct plt_entry **local_plt = (struct plt_entry **)
10582 elf_local_got_ents (input_sec->owner) + symtab_hdr->sh_info;
10583 unsigned long r_symndx = ELF64_R_SYM (rel->r_info);
10584
10585 if (local_plt[r_symndx] != NULL)
10586 {
10587 struct plt_entry *ent;
10588
10589 for (ent = local_plt[r_symndx]; ent != NULL; ent = ent->next)
10590 if (ent->addend == rel->r_addend
10591 && ent->plt.offset != (bfd_vma) -1)
10592 {
10593 *plt_ent = ent;
10594 return ppc_stub_plt_call;
10595 }
10596 }
10597 }
10598
10599 /* Determine where the call point is. */
10600 location = (input_sec->output_offset
10601 + input_sec->output_section->vma
10602 + rel->r_offset);
10603
10604 branch_offset = destination - location;
10605 r_type = ELF64_R_TYPE (rel->r_info);
10606
10607 /* Determine if a long branch stub is needed. */
10608 max_branch_offset = 1 << 25;
10609 if (r_type == R_PPC64_REL14
10610 || r_type == R_PPC64_REL14_BRTAKEN
10611 || r_type == R_PPC64_REL14_BRNTAKEN)
10612 max_branch_offset = 1 << 15;
10613
10614 if (branch_offset + max_branch_offset >= 2 * max_branch_offset - local_off)
10615 /* We need a stub. Figure out whether a long_branch or plt_branch
10616 is needed later. */
10617 return ppc_stub_long_branch;
10618
10619 return ppc_stub_none;
10620 }
10621
10622 /* Gets the address of a label (1:) in r11 and builds an offset in r12,
10623 then adds it to r11 (LOAD false) or loads r12 from r11+r12 (LOAD true).
10624 . mflr %r12
10625 . bcl 20,31,1f
10626 .1: mflr %r11
10627 . mtlr %r12
10628 . lis %r12,xxx-1b@highest
10629 . ori %r12,%r12,xxx-1b@higher
10630 . sldi %r12,%r12,32
10631 . oris %r12,%r12,xxx-1b@high
10632 . ori %r12,%r12,xxx-1b@l
10633 . add/ldx %r12,%r11,%r12 */
10634
10635 static bfd_byte *
10636 build_offset (bfd *abfd, bfd_byte *p, bfd_vma off, bool load)
10637 {
10638 bfd_put_32 (abfd, MFLR_R12, p);
10639 p += 4;
10640 bfd_put_32 (abfd, BCL_20_31, p);
10641 p += 4;
10642 bfd_put_32 (abfd, MFLR_R11, p);
10643 p += 4;
10644 bfd_put_32 (abfd, MTLR_R12, p);
10645 p += 4;
10646 if (off + 0x8000 < 0x10000)
10647 {
10648 if (load)
10649 bfd_put_32 (abfd, LD_R12_0R11 + PPC_LO (off), p);
10650 else
10651 bfd_put_32 (abfd, ADDI_R12_R11 + PPC_LO (off), p);
10652 p += 4;
10653 }
10654 else if (off + 0x80008000ULL < 0x100000000ULL)
10655 {
10656 bfd_put_32 (abfd, ADDIS_R12_R11 + PPC_HA (off), p);
10657 p += 4;
10658 if (load)
10659 bfd_put_32 (abfd, LD_R12_0R12 + PPC_LO (off), p);
10660 else
10661 bfd_put_32 (abfd, ADDI_R12_R12 + PPC_LO (off), p);
10662 p += 4;
10663 }
10664 else
10665 {
10666 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10667 {
10668 bfd_put_32 (abfd, LI_R12_0 + ((off >> 32) & 0xffff), p);
10669 p += 4;
10670 }
10671 else
10672 {
10673 bfd_put_32 (abfd, LIS_R12 + ((off >> 48) & 0xffff), p);
10674 p += 4;
10675 if (((off >> 32) & 0xffff) != 0)
10676 {
10677 bfd_put_32 (abfd, ORI_R12_R12_0 + ((off >> 32) & 0xffff), p);
10678 p += 4;
10679 }
10680 }
10681 if (((off >> 32) & 0xffffffffULL) != 0)
10682 {
10683 bfd_put_32 (abfd, SLDI_R12_R12_32, p);
10684 p += 4;
10685 }
10686 if (PPC_HI (off) != 0)
10687 {
10688 bfd_put_32 (abfd, ORIS_R12_R12_0 + PPC_HI (off), p);
10689 p += 4;
10690 }
10691 if (PPC_LO (off) != 0)
10692 {
10693 bfd_put_32 (abfd, ORI_R12_R12_0 + PPC_LO (off), p);
10694 p += 4;
10695 }
10696 if (load)
10697 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10698 else
10699 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10700 p += 4;
10701 }
10702 return p;
10703 }
10704
10705 static unsigned int
10706 size_offset (bfd_vma off)
10707 {
10708 unsigned int size;
10709 if (off + 0x8000 < 0x10000)
10710 size = 4;
10711 else if (off + 0x80008000ULL < 0x100000000ULL)
10712 size = 8;
10713 else
10714 {
10715 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10716 size = 4;
10717 else
10718 {
10719 size = 4;
10720 if (((off >> 32) & 0xffff) != 0)
10721 size += 4;
10722 }
10723 if (((off >> 32) & 0xffffffffULL) != 0)
10724 size += 4;
10725 if (PPC_HI (off) != 0)
10726 size += 4;
10727 if (PPC_LO (off) != 0)
10728 size += 4;
10729 size += 4;
10730 }
10731 return size + 16;
10732 }
10733
10734 static unsigned int
10735 num_relocs_for_offset (bfd_vma off)
10736 {
10737 unsigned int num_rel;
10738 if (off + 0x8000 < 0x10000)
10739 num_rel = 1;
10740 else if (off + 0x80008000ULL < 0x100000000ULL)
10741 num_rel = 2;
10742 else
10743 {
10744 num_rel = 1;
10745 if (off + 0x800000000000ULL >= 0x1000000000000ULL
10746 && ((off >> 32) & 0xffff) != 0)
10747 num_rel += 1;
10748 if (PPC_HI (off) != 0)
10749 num_rel += 1;
10750 if (PPC_LO (off) != 0)
10751 num_rel += 1;
10752 }
10753 return num_rel;
10754 }
10755
10756 static Elf_Internal_Rela *
10757 emit_relocs_for_offset (struct bfd_link_info *info, Elf_Internal_Rela *r,
10758 bfd_vma roff, bfd_vma targ, bfd_vma off)
10759 {
10760 bfd_vma relative_targ = targ - (roff - 8);
10761 if (bfd_big_endian (info->output_bfd))
10762 roff += 2;
10763 r->r_offset = roff;
10764 r->r_addend = relative_targ + roff;
10765 if (off + 0x8000 < 0x10000)
10766 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16);
10767 else if (off + 0x80008000ULL < 0x100000000ULL)
10768 {
10769 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HA);
10770 ++r;
10771 roff += 4;
10772 r->r_offset = roff;
10773 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10774 r->r_addend = relative_targ + roff;
10775 }
10776 else
10777 {
10778 if (off + 0x800000000000ULL < 0x1000000000000ULL)
10779 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10780 else
10781 {
10782 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHEST);
10783 if (((off >> 32) & 0xffff) != 0)
10784 {
10785 ++r;
10786 roff += 4;
10787 r->r_offset = roff;
10788 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHER);
10789 r->r_addend = relative_targ + roff;
10790 }
10791 }
10792 if (((off >> 32) & 0xffffffffULL) != 0)
10793 roff += 4;
10794 if (PPC_HI (off) != 0)
10795 {
10796 ++r;
10797 roff += 4;
10798 r->r_offset = roff;
10799 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGH);
10800 r->r_addend = relative_targ + roff;
10801 }
10802 if (PPC_LO (off) != 0)
10803 {
10804 ++r;
10805 roff += 4;
10806 r->r_offset = roff;
10807 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_LO);
10808 r->r_addend = relative_targ + roff;
10809 }
10810 }
10811 return r;
10812 }
10813
10814 static bfd_byte *
10815 build_power10_offset (bfd *abfd, bfd_byte *p, bfd_vma off, int odd,
10816 bool load)
10817 {
10818 uint64_t insn;
10819 if (off - odd + (1ULL << 33) < 1ULL << 34)
10820 {
10821 off -= odd;
10822 if (odd)
10823 {
10824 bfd_put_32 (abfd, NOP, p);
10825 p += 4;
10826 }
10827 if (load)
10828 insn = PLD_R12_PC;
10829 else
10830 insn = PADDI_R12_PC;
10831 insn |= D34 (off);
10832 bfd_put_32 (abfd, insn >> 32, p);
10833 p += 4;
10834 bfd_put_32 (abfd, insn, p);
10835 }
10836 /* The minimum value for paddi is -0x200000000. The minimum value
10837 for li is -0x8000, which when shifted by 34 and added gives a
10838 minimum value of -0x2000200000000. The maximum value is
10839 0x1ffffffff+0x7fff<<34 which is 0x2000200000000-1. */
10840 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10841 {
10842 off -= 8 - odd;
10843 bfd_put_32 (abfd, LI_R11_0 | (HA34 (off) & 0xffff), p);
10844 p += 4;
10845 if (!odd)
10846 {
10847 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10848 p += 4;
10849 }
10850 insn = PADDI_R12_PC | D34 (off);
10851 bfd_put_32 (abfd, insn >> 32, p);
10852 p += 4;
10853 bfd_put_32 (abfd, insn, p);
10854 p += 4;
10855 if (odd)
10856 {
10857 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10858 p += 4;
10859 }
10860 if (load)
10861 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10862 else
10863 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10864 }
10865 else
10866 {
10867 off -= odd + 8;
10868 bfd_put_32 (abfd, LIS_R11 | ((HA34 (off) >> 16) & 0x3fff), p);
10869 p += 4;
10870 bfd_put_32 (abfd, ORI_R11_R11_0 | (HA34 (off) & 0xffff), p);
10871 p += 4;
10872 if (odd)
10873 {
10874 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10875 p += 4;
10876 }
10877 insn = PADDI_R12_PC | D34 (off);
10878 bfd_put_32 (abfd, insn >> 32, p);
10879 p += 4;
10880 bfd_put_32 (abfd, insn, p);
10881 p += 4;
10882 if (!odd)
10883 {
10884 bfd_put_32 (abfd, SLDI_R11_R11_34, p);
10885 p += 4;
10886 }
10887 if (load)
10888 bfd_put_32 (abfd, LDX_R12_R11_R12, p);
10889 else
10890 bfd_put_32 (abfd, ADD_R12_R11_R12, p);
10891 }
10892 p += 4;
10893 return p;
10894 }
10895
10896 static unsigned int
10897 size_power10_offset (bfd_vma off, int odd)
10898 {
10899 if (off - odd + (1ULL << 33) < 1ULL << 34)
10900 return odd + 8;
10901 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10902 return 20;
10903 else
10904 return 24;
10905 }
10906
10907 static unsigned int
10908 num_relocs_for_power10_offset (bfd_vma off, int odd)
10909 {
10910 if (off - odd + (1ULL << 33) < 1ULL << 34)
10911 return 1;
10912 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10913 return 2;
10914 else
10915 return 3;
10916 }
10917
10918 static Elf_Internal_Rela *
10919 emit_relocs_for_power10_offset (struct bfd_link_info *info,
10920 Elf_Internal_Rela *r, bfd_vma roff,
10921 bfd_vma targ, bfd_vma off, int odd)
10922 {
10923 if (off - odd + (1ULL << 33) < 1ULL << 34)
10924 roff += odd;
10925 else if (off - (8 - odd) + (0x20002ULL << 32) < 0x40004ULL << 32)
10926 {
10927 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10928 r->r_offset = roff + d_offset;
10929 r->r_addend = targ + 8 - odd - d_offset;
10930 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10931 ++r;
10932 roff += 8 - odd;
10933 }
10934 else
10935 {
10936 int d_offset = bfd_big_endian (info->output_bfd) ? 2 : 0;
10937 r->r_offset = roff + d_offset;
10938 r->r_addend = targ + 8 + odd - d_offset;
10939 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHESTA34);
10940 ++r;
10941 roff += 4;
10942 r->r_offset = roff + d_offset;
10943 r->r_addend = targ + 4 + odd - d_offset;
10944 r->r_info = ELF64_R_INFO (0, R_PPC64_REL16_HIGHERA34);
10945 ++r;
10946 roff += 4 + odd;
10947 }
10948 r->r_offset = roff;
10949 r->r_addend = targ;
10950 r->r_info = ELF64_R_INFO (0, R_PPC64_PCREL34);
10951 return r;
10952 }
10953
10954 /* Emit .eh_frame opcode to advance pc by DELTA. */
10955
10956 static bfd_byte *
10957 eh_advance (bfd *abfd, bfd_byte *eh, unsigned int delta)
10958 {
10959 delta /= 4;
10960 if (delta < 64)
10961 *eh++ = DW_CFA_advance_loc + delta;
10962 else if (delta < 256)
10963 {
10964 *eh++ = DW_CFA_advance_loc1;
10965 *eh++ = delta;
10966 }
10967 else if (delta < 65536)
10968 {
10969 *eh++ = DW_CFA_advance_loc2;
10970 bfd_put_16 (abfd, delta, eh);
10971 eh += 2;
10972 }
10973 else
10974 {
10975 *eh++ = DW_CFA_advance_loc4;
10976 bfd_put_32 (abfd, delta, eh);
10977 eh += 4;
10978 }
10979 return eh;
10980 }
10981
10982 /* Size of required .eh_frame opcode to advance pc by DELTA. */
10983
10984 static unsigned int
10985 eh_advance_size (unsigned int delta)
10986 {
10987 if (delta < 64 * 4)
10988 /* DW_CFA_advance_loc+[1..63]. */
10989 return 1;
10990 if (delta < 256 * 4)
10991 /* DW_CFA_advance_loc1, byte. */
10992 return 2;
10993 if (delta < 65536 * 4)
10994 /* DW_CFA_advance_loc2, 2 bytes. */
10995 return 3;
10996 /* DW_CFA_advance_loc4, 4 bytes. */
10997 return 5;
10998 }
10999
11000 /* With power7 weakly ordered memory model, it is possible for ld.so
11001 to update a plt entry in one thread and have another thread see a
11002 stale zero toc entry. To avoid this we need some sort of acquire
11003 barrier in the call stub. One solution is to make the load of the
11004 toc word seem to appear to depend on the load of the function entry
11005 word. Another solution is to test for r2 being zero, and branch to
11006 the appropriate glink entry if so.
11007
11008 . fake dep barrier compare
11009 . ld 12,xxx(2) ld 12,xxx(2)
11010 . mtctr 12 mtctr 12
11011 . xor 11,12,12 ld 2,xxx+8(2)
11012 . add 2,2,11 cmpldi 2,0
11013 . ld 2,xxx+8(2) bnectr+
11014 . bctr b <glink_entry>
11015
11016 The solution involving the compare turns out to be faster, so
11017 that's what we use unless the branch won't reach. */
11018
11019 #define ALWAYS_USE_FAKE_DEP 0
11020 #define ALWAYS_EMIT_R2SAVE 0
11021
11022 static inline unsigned int
11023 plt_stub_size (struct ppc_link_hash_table *htab,
11024 struct ppc_stub_hash_entry *stub_entry,
11025 bfd_vma off,
11026 unsigned int odd)
11027 {
11028 unsigned size;
11029
11030 if (stub_entry->type.sub == ppc_stub_notoc)
11031 {
11032 size = 8 + size_power10_offset (off, odd);
11033 if (stub_entry->type.r2save)
11034 size += 4;
11035 }
11036 else if (stub_entry->type.sub == ppc_stub_p9notoc)
11037 {
11038 size = 8 + size_offset (off - 8);
11039 if (stub_entry->type.r2save)
11040 size += 4;
11041 }
11042 else
11043 {
11044 size = 12;
11045 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11046 size += 4;
11047 if (PPC_HA (off) != 0)
11048 size += 4;
11049 if (htab->opd_abi)
11050 {
11051 size += 4;
11052 if (htab->params->plt_static_chain)
11053 size += 4;
11054 if (htab->params->plt_thread_safe
11055 && htab->elf.dynamic_sections_created
11056 && stub_entry->h != NULL
11057 && stub_entry->h->elf.dynindx != -1)
11058 size += 8;
11059 if (PPC_HA (off + 8 + 8 * htab->params->plt_static_chain)
11060 != PPC_HA (off))
11061 size += 4;
11062 }
11063 }
11064 if (stub_entry->h != NULL
11065 && is_tls_get_addr (&stub_entry->h->elf, htab)
11066 && htab->params->tls_get_addr_opt)
11067 {
11068 if (!htab->params->no_tls_get_addr_regsave)
11069 {
11070 size += 30 * 4;
11071 if (stub_entry->type.r2save)
11072 size += 4;
11073 }
11074 else
11075 {
11076 size += 7 * 4;
11077 if (stub_entry->type.r2save)
11078 size += 6 * 4;
11079 }
11080 }
11081 return size;
11082 }
11083
11084 /* Depending on the sign of plt_stub_align:
11085 If positive, return the padding to align to a 2**plt_stub_align
11086 boundary.
11087 If negative, if this stub would cross fewer 2**plt_stub_align
11088 boundaries if we align, then return the padding needed to do so. */
11089
11090 static inline unsigned int
11091 plt_stub_pad (struct ppc_link_hash_table *htab,
11092 struct ppc_stub_hash_entry *stub_entry,
11093 bfd_vma plt_off,
11094 unsigned int odd)
11095 {
11096 int stub_align;
11097 unsigned stub_size;
11098 bfd_vma stub_off = stub_entry->group->stub_sec->size;
11099
11100 if (htab->params->plt_stub_align >= 0)
11101 {
11102 stub_align = 1 << htab->params->plt_stub_align;
11103 if ((stub_off & (stub_align - 1)) != 0)
11104 return stub_align - (stub_off & (stub_align - 1));
11105 return 0;
11106 }
11107
11108 stub_align = 1 << -htab->params->plt_stub_align;
11109 stub_size = plt_stub_size (htab, stub_entry, plt_off, odd);
11110 if (((stub_off + stub_size - 1) & -stub_align) - (stub_off & -stub_align)
11111 > ((stub_size - 1) & -stub_align))
11112 return stub_align - (stub_off & (stub_align - 1));
11113 return 0;
11114 }
11115
11116 /* Build a toc using .plt call stub. */
11117
11118 static inline bfd_byte *
11119 build_plt_stub (struct ppc_link_hash_table *htab,
11120 struct ppc_stub_hash_entry *stub_entry,
11121 bfd_byte *p, bfd_vma offset, Elf_Internal_Rela *r)
11122 {
11123 bfd *obfd = htab->params->stub_bfd;
11124 bool plt_load_toc = htab->opd_abi;
11125 bool plt_static_chain = htab->params->plt_static_chain;
11126 bool plt_thread_safe = (htab->params->plt_thread_safe
11127 && htab->elf.dynamic_sections_created
11128 && stub_entry->h != NULL
11129 && stub_entry->h->elf.dynindx != -1);
11130 bool use_fake_dep = plt_thread_safe;
11131 bfd_vma cmp_branch_off = 0;
11132
11133 if (!ALWAYS_USE_FAKE_DEP
11134 && plt_load_toc
11135 && plt_thread_safe
11136 && !(stub_entry->h != NULL
11137 && is_tls_get_addr (&stub_entry->h->elf, htab)
11138 && htab->params->tls_get_addr_opt))
11139 {
11140 bfd_vma pltoff = stub_entry->plt_ent->plt.offset & ~1;
11141 bfd_vma pltindex = ((pltoff - PLT_INITIAL_ENTRY_SIZE (htab))
11142 / PLT_ENTRY_SIZE (htab));
11143 bfd_vma glinkoff = GLINK_PLTRESOLVE_SIZE (htab) + pltindex * 8;
11144 bfd_vma to, from;
11145
11146 if (pltindex > 32768)
11147 glinkoff += (pltindex - 32768) * 4;
11148 to = (glinkoff
11149 + htab->glink->output_offset
11150 + htab->glink->output_section->vma);
11151 from = (p - stub_entry->group->stub_sec->contents
11152 + 4 * (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11153 + 4 * (PPC_HA (offset) != 0)
11154 + 4 * (PPC_HA (offset + 8 + 8 * plt_static_chain)
11155 != PPC_HA (offset))
11156 + 4 * (plt_static_chain != 0)
11157 + 20
11158 + stub_entry->group->stub_sec->output_offset
11159 + stub_entry->group->stub_sec->output_section->vma);
11160 cmp_branch_off = to - from;
11161 use_fake_dep = cmp_branch_off + (1 << 25) >= (1 << 26);
11162 }
11163
11164 if (PPC_HA (offset) != 0)
11165 {
11166 if (r != NULL)
11167 {
11168 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11169 r[0].r_offset += 4;
11170 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11171 r[1].r_offset = r[0].r_offset + 4;
11172 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11173 r[1].r_addend = r[0].r_addend;
11174 if (plt_load_toc)
11175 {
11176 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11177 {
11178 r[2].r_offset = r[1].r_offset + 4;
11179 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO);
11180 r[2].r_addend = r[0].r_addend;
11181 }
11182 else
11183 {
11184 r[2].r_offset = r[1].r_offset + 8 + 8 * use_fake_dep;
11185 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11186 r[2].r_addend = r[0].r_addend + 8;
11187 if (plt_static_chain)
11188 {
11189 r[3].r_offset = r[2].r_offset + 4;
11190 r[3].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11191 r[3].r_addend = r[0].r_addend + 16;
11192 }
11193 }
11194 }
11195 }
11196 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11197 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11198 if (plt_load_toc)
11199 {
11200 bfd_put_32 (obfd, ADDIS_R11_R2 | PPC_HA (offset), p), p += 4;
11201 bfd_put_32 (obfd, LD_R12_0R11 | PPC_LO (offset), p), p += 4;
11202 }
11203 else
11204 {
11205 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (offset), p), p += 4;
11206 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (offset), p), p += 4;
11207 }
11208 if (plt_load_toc
11209 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11210 {
11211 bfd_put_32 (obfd, ADDI_R11_R11 | PPC_LO (offset), p), p += 4;
11212 offset = 0;
11213 }
11214 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11215 if (plt_load_toc)
11216 {
11217 if (use_fake_dep)
11218 {
11219 bfd_put_32 (obfd, XOR_R2_R12_R12, p), p += 4;
11220 bfd_put_32 (obfd, ADD_R11_R11_R2, p), p += 4;
11221 }
11222 bfd_put_32 (obfd, LD_R2_0R11 | PPC_LO (offset + 8), p), p += 4;
11223 if (plt_static_chain)
11224 bfd_put_32 (obfd, LD_R11_0R11 | PPC_LO (offset + 16), p), p += 4;
11225 }
11226 }
11227 else
11228 {
11229 if (r != NULL)
11230 {
11231 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11232 r[0].r_offset += 4;
11233 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11234 if (plt_load_toc)
11235 {
11236 if (PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11237 {
11238 r[1].r_offset = r[0].r_offset + 4;
11239 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16);
11240 r[1].r_addend = r[0].r_addend;
11241 }
11242 else
11243 {
11244 r[1].r_offset = r[0].r_offset + 8 + 8 * use_fake_dep;
11245 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11246 r[1].r_addend = r[0].r_addend + 8 + 8 * plt_static_chain;
11247 if (plt_static_chain)
11248 {
11249 r[2].r_offset = r[1].r_offset + 4;
11250 r[2].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11251 r[2].r_addend = r[0].r_addend + 8;
11252 }
11253 }
11254 }
11255 }
11256 if (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save)
11257 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p), p += 4;
11258 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (offset), p), p += 4;
11259 if (plt_load_toc
11260 && PPC_HA (offset + 8 + 8 * plt_static_chain) != PPC_HA (offset))
11261 {
11262 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (offset), p), p += 4;
11263 offset = 0;
11264 }
11265 bfd_put_32 (obfd, MTCTR_R12, p), p += 4;
11266 if (plt_load_toc)
11267 {
11268 if (use_fake_dep)
11269 {
11270 bfd_put_32 (obfd, XOR_R11_R12_R12, p), p += 4;
11271 bfd_put_32 (obfd, ADD_R2_R2_R11, p), p += 4;
11272 }
11273 if (plt_static_chain)
11274 bfd_put_32 (obfd, LD_R11_0R2 | PPC_LO (offset + 16), p), p += 4;
11275 bfd_put_32 (obfd, LD_R2_0R2 | PPC_LO (offset + 8), p), p += 4;
11276 }
11277 }
11278 if (plt_load_toc && plt_thread_safe && !use_fake_dep)
11279 {
11280 bfd_put_32 (obfd, CMPLDI_R2_0, p), p += 4;
11281 bfd_put_32 (obfd, BNECTR_P4, p), p += 4;
11282 bfd_put_32 (obfd, B_DOT | (cmp_branch_off & 0x3fffffc), p), p += 4;
11283 }
11284 else
11285 bfd_put_32 (obfd, BCTR, p), p += 4;
11286 return p;
11287 }
11288
11289 /* Build a special .plt call stub for __tls_get_addr. */
11290
11291 #define LD_R0_0R3 0xe8030000
11292 #define LD_R12_0R3 0xe9830000
11293 #define MR_R0_R3 0x7c601b78
11294 #define CMPDI_R0_0 0x2c200000
11295 #define ADD_R3_R12_R13 0x7c6c6a14
11296 #define BEQLR 0x4d820020
11297 #define MR_R3_R0 0x7c030378
11298 #define BCTRL 0x4e800421
11299
11300 static bfd_byte *
11301 build_tls_get_addr_head (struct ppc_link_hash_table *htab,
11302 struct ppc_stub_hash_entry *stub_entry,
11303 bfd_byte *p)
11304 {
11305 bfd *obfd = htab->params->stub_bfd;
11306
11307 bfd_put_32 (obfd, LD_R0_0R3 + 0, p), p += 4;
11308 bfd_put_32 (obfd, LD_R12_0R3 + 8, p), p += 4;
11309 bfd_put_32 (obfd, CMPDI_R0_0, p), p += 4;
11310 bfd_put_32 (obfd, MR_R0_R3, p), p += 4;
11311 bfd_put_32 (obfd, ADD_R3_R12_R13, p), p += 4;
11312 bfd_put_32 (obfd, BEQLR, p), p += 4;
11313 bfd_put_32 (obfd, MR_R3_R0, p), p += 4;
11314
11315 if (!htab->params->no_tls_get_addr_regsave)
11316 p = tls_get_addr_prologue (obfd, p, htab);
11317 else if (stub_entry->type.r2save)
11318 {
11319 bfd_put_32 (obfd, MFLR_R0, p);
11320 p += 4;
11321 bfd_put_32 (obfd, STD_R0_0R1 + STK_LINKER (htab), p);
11322 p += 4;
11323 }
11324 return p;
11325 }
11326
11327 static bfd_byte *
11328 build_tls_get_addr_tail (struct ppc_link_hash_table *htab,
11329 struct ppc_stub_hash_entry *stub_entry,
11330 bfd_byte *p,
11331 bfd_byte *loc)
11332 {
11333 bfd *obfd = htab->params->stub_bfd;
11334
11335 if (!htab->params->no_tls_get_addr_regsave)
11336 {
11337 bfd_put_32 (obfd, BCTRL, p - 4);
11338
11339 if (stub_entry->type.r2save)
11340 {
11341 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11342 p += 4;
11343 }
11344 p = tls_get_addr_epilogue (obfd, p, htab);
11345 }
11346 else if (stub_entry->type.r2save)
11347 {
11348 bfd_put_32 (obfd, BCTRL, p - 4);
11349
11350 bfd_put_32 (obfd, LD_R2_0R1 + STK_TOC (htab), p);
11351 p += 4;
11352 bfd_put_32 (obfd, LD_R0_0R1 + STK_LINKER (htab), p);
11353 p += 4;
11354 bfd_put_32 (obfd, MTLR_R0, p);
11355 p += 4;
11356 bfd_put_32 (obfd, BLR, p);
11357 p += 4;
11358 }
11359
11360 if (htab->glink_eh_frame != NULL
11361 && htab->glink_eh_frame->size != 0)
11362 {
11363 bfd_byte *base, *eh;
11364
11365 base = htab->glink_eh_frame->contents + stub_entry->group->eh_base + 17;
11366 eh = base + stub_entry->group->eh_size;
11367
11368 if (!htab->params->no_tls_get_addr_regsave)
11369 {
11370 unsigned int cfa_updt, delta, i;
11371
11372 /* After the bctrl, lr has been modified so we need to emit
11373 .eh_frame info saying the return address is on the stack. In
11374 fact we must put the EH info at or before the call rather
11375 than after it, because the EH info for a call needs to be
11376 specified by that point.
11377 See libgcc/unwind-dw2.c execute_cfa_program.
11378 Any stack pointer update must be described immediately after
11379 the instruction making the change, and since the stdu occurs
11380 after saving regs we put all the reg saves and the cfa
11381 change there. */
11382 cfa_updt = stub_entry->stub_offset + 18 * 4;
11383 delta = cfa_updt - stub_entry->group->lr_restore;
11384 stub_entry->group->lr_restore
11385 = stub_entry->stub_offset + (p - loc) - 4;
11386 eh = eh_advance (htab->elf.dynobj, eh, delta);
11387 *eh++ = DW_CFA_def_cfa_offset;
11388 if (htab->opd_abi)
11389 {
11390 *eh++ = 128;
11391 *eh++ = 1;
11392 }
11393 else
11394 *eh++ = 96;
11395 *eh++ = DW_CFA_offset_extended_sf;
11396 *eh++ = 65;
11397 *eh++ = (-16 / 8) & 0x7f;
11398 for (i = 4; i < 12; i++)
11399 {
11400 *eh++ = DW_CFA_offset + i;
11401 *eh++ = (htab->opd_abi ? 13 : 12) - i;
11402 }
11403 *eh++ = (DW_CFA_advance_loc
11404 + (stub_entry->group->lr_restore - 8 - cfa_updt) / 4);
11405 *eh++ = DW_CFA_def_cfa_offset;
11406 *eh++ = 0;
11407 for (i = 4; i < 12; i++)
11408 *eh++ = DW_CFA_restore + i;
11409 *eh++ = DW_CFA_advance_loc + 2;
11410 *eh++ = DW_CFA_restore_extended;
11411 *eh++ = 65;
11412 stub_entry->group->eh_size = eh - base;
11413 }
11414 else if (stub_entry->type.r2save)
11415 {
11416 unsigned int lr_used, delta;
11417
11418 lr_used = stub_entry->stub_offset + (p - 20 - loc);
11419 delta = lr_used - stub_entry->group->lr_restore;
11420 stub_entry->group->lr_restore = lr_used + 16;
11421 eh = eh_advance (htab->elf.dynobj, eh, delta);
11422 *eh++ = DW_CFA_offset_extended_sf;
11423 *eh++ = 65;
11424 *eh++ = -(STK_LINKER (htab) / 8) & 0x7f;
11425 *eh++ = DW_CFA_advance_loc + 4;
11426 *eh++ = DW_CFA_restore_extended;
11427 *eh++ = 65;
11428 stub_entry->group->eh_size = eh - base;
11429 }
11430 }
11431 return p;
11432 }
11433
11434 static Elf_Internal_Rela *
11435 get_relocs (asection *sec, int count)
11436 {
11437 Elf_Internal_Rela *relocs;
11438 struct bfd_elf_section_data *elfsec_data;
11439
11440 elfsec_data = elf_section_data (sec);
11441 relocs = elfsec_data->relocs;
11442 if (relocs == NULL)
11443 {
11444 bfd_size_type relsize;
11445 relsize = sec->reloc_count * sizeof (*relocs);
11446 relocs = bfd_alloc (sec->owner, relsize);
11447 if (relocs == NULL)
11448 return NULL;
11449 elfsec_data->relocs = relocs;
11450 elfsec_data->rela.hdr = bfd_zalloc (sec->owner,
11451 sizeof (Elf_Internal_Shdr));
11452 if (elfsec_data->rela.hdr == NULL)
11453 return NULL;
11454 elfsec_data->rela.hdr->sh_size = (sec->reloc_count
11455 * sizeof (Elf64_External_Rela));
11456 elfsec_data->rela.hdr->sh_entsize = sizeof (Elf64_External_Rela);
11457 sec->reloc_count = 0;
11458 }
11459 relocs += sec->reloc_count;
11460 sec->reloc_count += count;
11461 return relocs;
11462 }
11463
11464 /* Convert the relocs R[0] thru R[-NUM_REL+1], which are all no-symbol
11465 forms, to the equivalent relocs against the global symbol given by
11466 STUB_ENTRY->H. */
11467
11468 static bool
11469 use_global_in_relocs (struct ppc_link_hash_table *htab,
11470 struct ppc_stub_hash_entry *stub_entry,
11471 Elf_Internal_Rela *r, unsigned int num_rel)
11472 {
11473 struct elf_link_hash_entry **hashes;
11474 unsigned long symndx;
11475 struct ppc_link_hash_entry *h;
11476 bfd_vma symval;
11477
11478 /* Relocs are always against symbols in their own object file. Fake
11479 up global sym hashes for the stub bfd (which has no symbols). */
11480 hashes = elf_sym_hashes (htab->params->stub_bfd);
11481 if (hashes == NULL)
11482 {
11483 bfd_size_type hsize;
11484
11485 /* When called the first time, stub_globals will contain the
11486 total number of symbols seen during stub sizing. After
11487 allocating, stub_globals is used as an index to fill the
11488 hashes array. */
11489 hsize = (htab->stub_globals + 1) * sizeof (*hashes);
11490 hashes = bfd_zalloc (htab->params->stub_bfd, hsize);
11491 if (hashes == NULL)
11492 return false;
11493 elf_sym_hashes (htab->params->stub_bfd) = hashes;
11494 htab->stub_globals = 1;
11495 }
11496 symndx = htab->stub_globals++;
11497 h = stub_entry->h;
11498 hashes[symndx] = &h->elf;
11499 if (h->oh != NULL && h->oh->is_func)
11500 h = ppc_follow_link (h->oh);
11501 BFD_ASSERT (h->elf.root.type == bfd_link_hash_defined
11502 || h->elf.root.type == bfd_link_hash_defweak);
11503 symval = defined_sym_val (&h->elf);
11504 while (num_rel-- != 0)
11505 {
11506 r->r_info = ELF64_R_INFO (symndx, ELF64_R_TYPE (r->r_info));
11507 if (h->elf.root.u.def.section != stub_entry->target_section)
11508 {
11509 /* H is an opd symbol. The addend must be zero, and the
11510 branch reloc is the only one we can convert. */
11511 r->r_addend = 0;
11512 break;
11513 }
11514 else
11515 r->r_addend -= symval;
11516 --r;
11517 }
11518 return true;
11519 }
11520
11521 static bfd_vma
11522 get_r2off (struct bfd_link_info *info,
11523 struct ppc_stub_hash_entry *stub_entry)
11524 {
11525 struct ppc_link_hash_table *htab = ppc_hash_table (info);
11526 bfd_vma r2off = htab->sec_info[stub_entry->target_section->id].toc_off;
11527
11528 if (r2off == 0)
11529 {
11530 /* Support linking -R objects. Get the toc pointer from the
11531 opd entry. */
11532 char buf[8];
11533 if (!htab->opd_abi)
11534 return r2off;
11535 asection *opd = stub_entry->h->elf.root.u.def.section;
11536 bfd_vma opd_off = stub_entry->h->elf.root.u.def.value;
11537
11538 if (strcmp (opd->name, ".opd") != 0
11539 || opd->reloc_count != 0)
11540 {
11541 info->callbacks->einfo
11542 (_("%P: cannot find opd entry toc for `%pT'\n"),
11543 stub_entry->h->elf.root.root.string);
11544 bfd_set_error (bfd_error_bad_value);
11545 return (bfd_vma) -1;
11546 }
11547 if (!bfd_get_section_contents (opd->owner, opd, buf, opd_off + 8, 8))
11548 return (bfd_vma) -1;
11549 r2off = bfd_get_64 (opd->owner, buf);
11550 r2off -= elf_gp (info->output_bfd);
11551 }
11552 r2off -= htab->sec_info[stub_entry->group->link_sec->id].toc_off;
11553 return r2off;
11554 }
11555
11556 static bool
11557 ppc_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
11558 {
11559 struct ppc_stub_hash_entry *stub_entry;
11560 struct ppc_branch_hash_entry *br_entry;
11561 struct bfd_link_info *info;
11562 struct ppc_link_hash_table *htab;
11563 bfd *obfd;
11564 bfd_byte *loc;
11565 bfd_byte *p, *relp;
11566 bfd_vma targ, off;
11567 Elf_Internal_Rela *r;
11568 asection *plt;
11569 int num_rel;
11570 int odd;
11571 bool is_tga;
11572
11573 /* Massage our args to the form they really have. */
11574 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
11575 info = in_arg;
11576
11577 /* Fail if the target section could not be assigned to an output
11578 section. The user should fix his linker script. */
11579 if (stub_entry->target_section != NULL
11580 && stub_entry->target_section->output_section == NULL
11581 && info->non_contiguous_regions)
11582 info->callbacks->einfo (_("%F%P: Could not assign '%pA' to an output section. "
11583 "Retry without --enable-non-contiguous-regions.\n"),
11584 stub_entry->target_section);
11585
11586 /* Same for the group. */
11587 if (stub_entry->group->stub_sec != NULL
11588 && stub_entry->group->stub_sec->output_section == NULL
11589 && info->non_contiguous_regions)
11590 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
11591 "output section. Retry without "
11592 "--enable-non-contiguous-regions.\n"),
11593 stub_entry->group->stub_sec,
11594 stub_entry->target_section);
11595
11596 htab = ppc_hash_table (info);
11597 if (htab == NULL)
11598 return false;
11599
11600 BFD_ASSERT (stub_entry->stub_offset >= stub_entry->group->stub_sec->size);
11601 loc = stub_entry->group->stub_sec->contents + stub_entry->stub_offset;
11602
11603 htab->stub_count[stub_entry->type.main - 1] += 1;
11604 if (stub_entry->type.main == ppc_stub_long_branch
11605 && stub_entry->type.sub == ppc_stub_toc)
11606 {
11607 /* Branches are relative. This is where we are going to. */
11608 targ = (stub_entry->target_value
11609 + stub_entry->target_section->output_offset
11610 + stub_entry->target_section->output_section->vma);
11611 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11612
11613 /* And this is where we are coming from. */
11614 off = (stub_entry->stub_offset
11615 + stub_entry->group->stub_sec->output_offset
11616 + stub_entry->group->stub_sec->output_section->vma);
11617 off = targ - off;
11618
11619 p = loc;
11620 obfd = htab->params->stub_bfd;
11621 if (stub_entry->type.r2save)
11622 {
11623 bfd_vma r2off = get_r2off (info, stub_entry);
11624
11625 if (r2off == (bfd_vma) -1)
11626 {
11627 htab->stub_error = true;
11628 return false;
11629 }
11630 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11631 p += 4;
11632 if (PPC_HA (r2off) != 0)
11633 {
11634 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11635 p += 4;
11636 }
11637 if (PPC_LO (r2off) != 0)
11638 {
11639 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11640 p += 4;
11641 }
11642 off -= p - loc;
11643 }
11644 bfd_put_32 (obfd, B_DOT | (off & 0x3fffffc), p);
11645 p += 4;
11646
11647 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
11648 {
11649 _bfd_error_handler
11650 (_("long branch stub `%s' offset overflow"),
11651 stub_entry->root.string);
11652 htab->stub_error = true;
11653 return false;
11654 }
11655
11656 if (info->emitrelocations)
11657 {
11658 r = get_relocs (stub_entry->group->stub_sec, 1);
11659 if (r == NULL)
11660 return false;
11661 r->r_offset = p - 4 - stub_entry->group->stub_sec->contents;
11662 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11663 r->r_addend = targ;
11664 if (stub_entry->h != NULL
11665 && !use_global_in_relocs (htab, stub_entry, r, 1))
11666 return false;
11667 }
11668 }
11669 else if (stub_entry->type.main == ppc_stub_plt_branch
11670 && stub_entry->type.sub == ppc_stub_toc)
11671 {
11672 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
11673 stub_entry->root.string + 9,
11674 false, false);
11675 if (br_entry == NULL)
11676 {
11677 _bfd_error_handler (_("can't find branch stub `%s'"),
11678 stub_entry->root.string);
11679 htab->stub_error = true;
11680 return false;
11681 }
11682
11683 targ = (stub_entry->target_value
11684 + stub_entry->target_section->output_offset
11685 + stub_entry->target_section->output_section->vma);
11686 if (!stub_entry->type.r2save)
11687 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
11688
11689 bfd_put_64 (htab->brlt->owner, targ,
11690 htab->brlt->contents + br_entry->offset);
11691
11692 if (br_entry->iter == htab->stub_iteration)
11693 {
11694 br_entry->iter = 0;
11695
11696 if (htab->relbrlt != NULL)
11697 {
11698 /* Create a reloc for the branch lookup table entry. */
11699 Elf_Internal_Rela rela;
11700 bfd_byte *rl;
11701
11702 rela.r_offset = (br_entry->offset
11703 + htab->brlt->output_offset
11704 + htab->brlt->output_section->vma);
11705 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11706 rela.r_addend = targ;
11707
11708 rl = htab->relbrlt->contents;
11709 rl += (htab->relbrlt->reloc_count++
11710 * sizeof (Elf64_External_Rela));
11711 bfd_elf64_swap_reloca_out (htab->relbrlt->owner, &rela, rl);
11712 }
11713 else if (info->emitrelocations)
11714 {
11715 r = get_relocs (htab->brlt, 1);
11716 if (r == NULL)
11717 return false;
11718 /* brlt, being SEC_LINKER_CREATED does not go through the
11719 normal reloc processing. Symbols and offsets are not
11720 translated from input file to output file form, so
11721 set up the offset per the output file. */
11722 r->r_offset = (br_entry->offset
11723 + htab->brlt->output_offset
11724 + htab->brlt->output_section->vma);
11725 r->r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
11726 r->r_addend = targ;
11727 }
11728 }
11729
11730 targ = (br_entry->offset
11731 + htab->brlt->output_offset
11732 + htab->brlt->output_section->vma);
11733
11734 off = (elf_gp (info->output_bfd)
11735 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11736 off = targ - off;
11737
11738 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11739 {
11740 info->callbacks->einfo
11741 (_("%P: linkage table error against `%pT'\n"),
11742 stub_entry->root.string);
11743 bfd_set_error (bfd_error_bad_value);
11744 htab->stub_error = true;
11745 return false;
11746 }
11747
11748 if (info->emitrelocations)
11749 {
11750 r = get_relocs (stub_entry->group->stub_sec, 1 + (PPC_HA (off) != 0));
11751 if (r == NULL)
11752 return false;
11753 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
11754 if (bfd_big_endian (info->output_bfd))
11755 r[0].r_offset += 2;
11756 if (stub_entry->type.r2save)
11757 r[0].r_offset += 4;
11758 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_DS);
11759 r[0].r_addend = targ;
11760 if (PPC_HA (off) != 0)
11761 {
11762 r[0].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_HA);
11763 r[1].r_offset = r[0].r_offset + 4;
11764 r[1].r_info = ELF64_R_INFO (0, R_PPC64_TOC16_LO_DS);
11765 r[1].r_addend = r[0].r_addend;
11766 }
11767 }
11768
11769 p = loc;
11770 obfd = htab->params->stub_bfd;
11771 if (!stub_entry->type.r2save)
11772 {
11773 if (PPC_HA (off) != 0)
11774 {
11775 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11776 p += 4;
11777 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11778 }
11779 else
11780 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11781 }
11782 else
11783 {
11784 bfd_vma r2off = get_r2off (info, stub_entry);
11785
11786 if (r2off == (bfd_vma) -1)
11787 {
11788 htab->stub_error = true;
11789 return false;
11790 }
11791
11792 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11793 p += 4;
11794 if (PPC_HA (off) != 0)
11795 {
11796 bfd_put_32 (obfd, ADDIS_R12_R2 | PPC_HA (off), p);
11797 p += 4;
11798 bfd_put_32 (obfd, LD_R12_0R12 | PPC_LO (off), p);
11799 }
11800 else
11801 bfd_put_32 (obfd, LD_R12_0R2 | PPC_LO (off), p);
11802
11803 if (PPC_HA (r2off) != 0)
11804 {
11805 p += 4;
11806 bfd_put_32 (obfd, ADDIS_R2_R2 | PPC_HA (r2off), p);
11807 }
11808 if (PPC_LO (r2off) != 0)
11809 {
11810 p += 4;
11811 bfd_put_32 (obfd, ADDI_R2_R2 | PPC_LO (r2off), p);
11812 }
11813 }
11814 p += 4;
11815 bfd_put_32 (obfd, MTCTR_R12, p);
11816 p += 4;
11817 bfd_put_32 (obfd, BCTR, p);
11818 p += 4;
11819 }
11820 else if (stub_entry->type.sub >= ppc_stub_notoc)
11821 {
11822 bool is_plt = stub_entry->type.main == ppc_stub_plt_call;
11823 p = loc;
11824 off = (stub_entry->stub_offset
11825 + stub_entry->group->stub_sec->output_offset
11826 + stub_entry->group->stub_sec->output_section->vma);
11827 obfd = htab->params->stub_bfd;
11828 is_tga = (is_plt
11829 && stub_entry->h != NULL
11830 && is_tls_get_addr (&stub_entry->h->elf, htab)
11831 && htab->params->tls_get_addr_opt);
11832 if (is_tga)
11833 {
11834 p = build_tls_get_addr_head (htab, stub_entry, p);
11835 off += p - loc;
11836 }
11837 if (stub_entry->type.r2save)
11838 {
11839 off += 4;
11840 bfd_put_32 (obfd, STD_R2_0R1 + STK_TOC (htab), p);
11841 p += 4;
11842 }
11843 if (is_plt)
11844 {
11845 targ = stub_entry->plt_ent->plt.offset & ~1;
11846 if (targ >= (bfd_vma) -2)
11847 abort ();
11848
11849 plt = htab->elf.splt;
11850 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11851 {
11852 if (stub_entry->symtype == STT_GNU_IFUNC)
11853 plt = htab->elf.iplt;
11854 else
11855 plt = htab->pltlocal;
11856 }
11857 targ += plt->output_offset + plt->output_section->vma;
11858 }
11859 else
11860 targ = (stub_entry->target_value
11861 + stub_entry->target_section->output_offset
11862 + stub_entry->target_section->output_section->vma);
11863 odd = off & 4;
11864 off = targ - off;
11865
11866 relp = p;
11867 num_rel = 0;
11868 if (stub_entry->type.sub == ppc_stub_notoc)
11869 p = build_power10_offset (obfd, p, off, odd, is_plt);
11870 else
11871 {
11872 if (htab->glink_eh_frame != NULL
11873 && htab->glink_eh_frame->size != 0)
11874 {
11875 bfd_byte *base, *eh;
11876 unsigned int lr_used, delta;
11877
11878 base = (htab->glink_eh_frame->contents
11879 + stub_entry->group->eh_base + 17);
11880 eh = base + stub_entry->group->eh_size;
11881 lr_used = stub_entry->stub_offset + (p - loc) + 8;
11882 delta = lr_used - stub_entry->group->lr_restore;
11883 stub_entry->group->lr_restore = lr_used + 8;
11884 eh = eh_advance (htab->elf.dynobj, eh, delta);
11885 *eh++ = DW_CFA_register;
11886 *eh++ = 65;
11887 *eh++ = 12;
11888 *eh++ = DW_CFA_advance_loc + 2;
11889 *eh++ = DW_CFA_restore_extended;
11890 *eh++ = 65;
11891 stub_entry->group->eh_size = eh - base;
11892 }
11893
11894 /* The notoc stubs calculate their target (either a PLT entry or
11895 the global entry point of a function) relative to the PC
11896 returned by the "bcl" two instructions past the start of the
11897 sequence emitted by build_offset. The offset is therefore 8
11898 less than calculated from the start of the sequence. */
11899 off -= 8;
11900 p = build_offset (obfd, p, off, is_plt);
11901 }
11902
11903 if (stub_entry->type.main == ppc_stub_long_branch)
11904 {
11905 bfd_vma from;
11906 num_rel = 1;
11907 from = (stub_entry->stub_offset
11908 + stub_entry->group->stub_sec->output_offset
11909 + stub_entry->group->stub_sec->output_section->vma
11910 + (p - loc));
11911 bfd_put_32 (obfd, B_DOT | ((targ - from) & 0x3fffffc), p);
11912 }
11913 else
11914 {
11915 bfd_put_32 (obfd, MTCTR_R12, p);
11916 p += 4;
11917 bfd_put_32 (obfd, BCTR, p);
11918 }
11919 p += 4;
11920
11921 if (is_tga)
11922 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
11923
11924 if (info->emitrelocations)
11925 {
11926 bfd_vma roff = relp - stub_entry->group->stub_sec->contents;
11927 if (stub_entry->type.sub == ppc_stub_notoc)
11928 num_rel += num_relocs_for_power10_offset (off, odd);
11929 else
11930 {
11931 num_rel += num_relocs_for_offset (off);
11932 roff += 16;
11933 }
11934 r = get_relocs (stub_entry->group->stub_sec, num_rel);
11935 if (r == NULL)
11936 return false;
11937 if (stub_entry->type.sub == ppc_stub_notoc)
11938 r = emit_relocs_for_power10_offset (info, r, roff, targ, off, odd);
11939 else
11940 r = emit_relocs_for_offset (info, r, roff, targ, off);
11941 if (stub_entry->type.main == ppc_stub_long_branch)
11942 {
11943 ++r;
11944 roff = p - 4 - stub_entry->group->stub_sec->contents;
11945 r->r_offset = roff;
11946 r->r_info = ELF64_R_INFO (0, R_PPC64_REL24);
11947 r->r_addend = targ;
11948 if (stub_entry->h != NULL
11949 && !use_global_in_relocs (htab, stub_entry, r, num_rel))
11950 return false;
11951 }
11952 }
11953 }
11954 else if (stub_entry->type.main == ppc_stub_plt_call)
11955 {
11956 if (stub_entry->h != NULL
11957 && stub_entry->h->is_func_descriptor
11958 && stub_entry->h->oh != NULL)
11959 {
11960 struct ppc_link_hash_entry *fh = ppc_follow_link (stub_entry->h->oh);
11961
11962 /* If the old-ABI "dot-symbol" is undefined make it weak so
11963 we don't get a link error from RELOC_FOR_GLOBAL_SYMBOL. */
11964 if (fh->elf.root.type == bfd_link_hash_undefined
11965 && (stub_entry->h->elf.root.type == bfd_link_hash_defined
11966 || stub_entry->h->elf.root.type == bfd_link_hash_defweak))
11967 fh->elf.root.type = bfd_link_hash_undefweak;
11968 }
11969
11970 /* Now build the stub. */
11971 targ = stub_entry->plt_ent->plt.offset & ~1;
11972 if (targ >= (bfd_vma) -2)
11973 abort ();
11974
11975 plt = htab->elf.splt;
11976 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
11977 {
11978 if (stub_entry->symtype == STT_GNU_IFUNC)
11979 plt = htab->elf.iplt;
11980 else
11981 plt = htab->pltlocal;
11982 }
11983 targ += plt->output_offset + plt->output_section->vma;
11984
11985 off = (elf_gp (info->output_bfd)
11986 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
11987 off = targ - off;
11988
11989 if (off + 0x80008000 > 0xffffffff || (off & 7) != 0)
11990 {
11991 info->callbacks->einfo
11992 /* xgettext:c-format */
11993 (_("%P: linkage table error against `%pT'\n"),
11994 stub_entry->h != NULL
11995 ? stub_entry->h->elf.root.root.string
11996 : "<local sym>");
11997 bfd_set_error (bfd_error_bad_value);
11998 htab->stub_error = true;
11999 return false;
12000 }
12001
12002 r = NULL;
12003 if (info->emitrelocations)
12004 {
12005 r = get_relocs (stub_entry->group->stub_sec,
12006 ((PPC_HA (off) != 0)
12007 + (htab->opd_abi
12008 ? 2 + (htab->params->plt_static_chain
12009 && PPC_HA (off + 16) == PPC_HA (off))
12010 : 1)));
12011 if (r == NULL)
12012 return false;
12013 r[0].r_offset = loc - stub_entry->group->stub_sec->contents;
12014 if (bfd_big_endian (info->output_bfd))
12015 r[0].r_offset += 2;
12016 r[0].r_addend = targ;
12017 }
12018 p = loc;
12019 obfd = htab->params->stub_bfd;
12020 is_tga = (stub_entry->h != NULL
12021 && is_tls_get_addr (&stub_entry->h->elf, htab)
12022 && htab->params->tls_get_addr_opt);
12023 if (is_tga)
12024 {
12025 p = build_tls_get_addr_head (htab, stub_entry, p);
12026 if (r != NULL)
12027 r[0].r_offset += p - loc;
12028 }
12029 p = build_plt_stub (htab, stub_entry, p, off, r);
12030 if (is_tga)
12031 p = build_tls_get_addr_tail (htab, stub_entry, p, loc);
12032 }
12033 else if (stub_entry->type.main == ppc_stub_save_res)
12034 return true;
12035 else
12036 {
12037 BFD_FAIL ();
12038 return false;
12039 }
12040
12041 stub_entry->group->stub_sec->size = stub_entry->stub_offset + (p - loc);
12042
12043 if (htab->params->emit_stub_syms)
12044 {
12045 struct elf_link_hash_entry *h;
12046 size_t len1, len2;
12047 char *name;
12048 const char *const stub_str[] = { "long_branch",
12049 "plt_branch",
12050 "plt_call" };
12051
12052 len1 = strlen (stub_str[stub_entry->type.main - 1]);
12053 len2 = strlen (stub_entry->root.string);
12054 name = bfd_malloc (len1 + len2 + 2);
12055 if (name == NULL)
12056 return false;
12057 memcpy (name, stub_entry->root.string, 9);
12058 memcpy (name + 9, stub_str[stub_entry->type.main - 1], len1);
12059 memcpy (name + len1 + 9, stub_entry->root.string + 8, len2 - 8 + 1);
12060 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
12061 if (h == NULL)
12062 return false;
12063 if (h->root.type == bfd_link_hash_new)
12064 {
12065 h->root.type = bfd_link_hash_defined;
12066 h->root.u.def.section = stub_entry->group->stub_sec;
12067 h->root.u.def.value = stub_entry->stub_offset;
12068 h->ref_regular = 1;
12069 h->def_regular = 1;
12070 h->ref_regular_nonweak = 1;
12071 h->forced_local = 1;
12072 h->non_elf = 0;
12073 h->root.linker_def = 1;
12074 }
12075 }
12076
12077 return true;
12078 }
12079
12080 /* As above, but don't actually build the stub. Just bump offset so
12081 we know stub section sizes, and select plt_branch stubs where
12082 long_branch stubs won't do. */
12083
12084 static bool
12085 ppc_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
12086 {
12087 struct ppc_stub_hash_entry *stub_entry;
12088 struct bfd_link_info *info;
12089 struct ppc_link_hash_table *htab;
12090 asection *plt;
12091 bfd_vma targ, off, r2off;
12092 unsigned int size, extra, lr_used, delta, odd;
12093
12094 /* Massage our args to the form they really have. */
12095 stub_entry = (struct ppc_stub_hash_entry *) gen_entry;
12096 info = in_arg;
12097
12098 htab = ppc_hash_table (info);
12099 if (htab == NULL)
12100 return false;
12101
12102 /* Fail if the target section could not be assigned to an output
12103 section. The user should fix his linker script. */
12104 if (stub_entry->target_section != NULL
12105 && stub_entry->target_section->output_section == NULL
12106 && info->non_contiguous_regions)
12107 info->callbacks->einfo (_("%F%P: Could not assign %pA to an output section. "
12108 "Retry without --enable-non-contiguous-regions.\n"),
12109 stub_entry->target_section);
12110
12111 /* Same for the group. */
12112 if (stub_entry->group->stub_sec != NULL
12113 && stub_entry->group->stub_sec->output_section == NULL
12114 && info->non_contiguous_regions)
12115 info->callbacks->einfo (_("%F%P: Could not assign group %pA target %pA to an "
12116 "output section. Retry without "
12117 "--enable-non-contiguous-regions.\n"),
12118 stub_entry->group->stub_sec,
12119 stub_entry->target_section);
12120
12121 /* Make a note of the offset within the stubs for this entry. */
12122 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12123
12124 if (stub_entry->h != NULL
12125 && stub_entry->h->save_res
12126 && stub_entry->h->elf.root.type == bfd_link_hash_defined
12127 && stub_entry->h->elf.root.u.def.section == htab->sfpr)
12128 {
12129 /* Don't make stubs to out-of-line register save/restore
12130 functions. Instead, emit copies of the functions. */
12131 stub_entry->group->needs_save_res = 1;
12132 stub_entry->type.main = ppc_stub_save_res;
12133 stub_entry->type.sub = ppc_stub_toc;
12134 stub_entry->type.r2save = 0;
12135 return true;
12136 }
12137
12138 if (stub_entry->type.main == ppc_stub_plt_branch)
12139 {
12140 /* Reset the stub type from the plt branch variant in case we now
12141 can reach with a shorter stub. */
12142 stub_entry->type.main += ppc_stub_long_branch - ppc_stub_plt_branch;
12143 }
12144
12145 if (stub_entry->type.main == ppc_stub_long_branch
12146 && stub_entry->type.sub == ppc_stub_toc)
12147 {
12148 targ = (stub_entry->target_value
12149 + stub_entry->target_section->output_offset
12150 + stub_entry->target_section->output_section->vma);
12151 targ += PPC64_LOCAL_ENTRY_OFFSET (stub_entry->other);
12152 off = (stub_entry->stub_offset
12153 + stub_entry->group->stub_sec->output_offset
12154 + stub_entry->group->stub_sec->output_section->vma);
12155
12156 size = 4;
12157 r2off = 0;
12158 if (stub_entry->type.r2save)
12159 {
12160 r2off = get_r2off (info, stub_entry);
12161 if (r2off == (bfd_vma) -1)
12162 {
12163 htab->stub_error = true;
12164 return false;
12165 }
12166 size = 8;
12167 if (PPC_HA (r2off) != 0)
12168 size += 4;
12169 if (PPC_LO (r2off) != 0)
12170 size += 4;
12171 off += size - 4;
12172 }
12173 off = targ - off;
12174
12175 /* If the branch offset is too big, use a ppc_stub_plt_branch.
12176 Do the same for -R objects without function descriptors. */
12177 if ((stub_entry->type.r2save
12178 && r2off == 0
12179 && htab->sec_info[stub_entry->target_section->id].toc_off == 0)
12180 || off + (1 << 25) >= (bfd_vma) (1 << 26))
12181 {
12182 struct ppc_branch_hash_entry *br_entry;
12183
12184 br_entry = ppc_branch_hash_lookup (&htab->branch_hash_table,
12185 stub_entry->root.string + 9,
12186 true, false);
12187 if (br_entry == NULL)
12188 {
12189 _bfd_error_handler (_("can't build branch stub `%s'"),
12190 stub_entry->root.string);
12191 htab->stub_error = true;
12192 return false;
12193 }
12194
12195 if (br_entry->iter != htab->stub_iteration)
12196 {
12197 br_entry->iter = htab->stub_iteration;
12198 br_entry->offset = htab->brlt->size;
12199 htab->brlt->size += 8;
12200
12201 if (htab->relbrlt != NULL)
12202 htab->relbrlt->size += sizeof (Elf64_External_Rela);
12203 else if (info->emitrelocations)
12204 {
12205 htab->brlt->reloc_count += 1;
12206 htab->brlt->flags |= SEC_RELOC;
12207 }
12208 }
12209
12210 targ = (br_entry->offset
12211 + htab->brlt->output_offset
12212 + htab->brlt->output_section->vma);
12213 off = (elf_gp (info->output_bfd)
12214 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12215 off = targ - off;
12216
12217 if (info->emitrelocations)
12218 {
12219 stub_entry->group->stub_sec->reloc_count
12220 += 1 + (PPC_HA (off) != 0);
12221 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12222 }
12223
12224 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12225 if (!stub_entry->type.r2save)
12226 {
12227 size = 12;
12228 if (PPC_HA (off) != 0)
12229 size = 16;
12230 }
12231 else
12232 {
12233 size = 16;
12234 if (PPC_HA (off) != 0)
12235 size += 4;
12236
12237 if (PPC_HA (r2off) != 0)
12238 size += 4;
12239 if (PPC_LO (r2off) != 0)
12240 size += 4;
12241 }
12242 }
12243 else if (info->emitrelocations)
12244 {
12245 stub_entry->group->stub_sec->reloc_count += 1;
12246 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12247 }
12248 }
12249 else if (stub_entry->type.main == ppc_stub_long_branch)
12250 {
12251 off = (stub_entry->stub_offset
12252 + stub_entry->group->stub_sec->output_offset
12253 + stub_entry->group->stub_sec->output_section->vma);
12254 size = 0;
12255 if (stub_entry->type.r2save)
12256 size = 4;
12257 off += size;
12258 targ = (stub_entry->target_value
12259 + stub_entry->target_section->output_offset
12260 + stub_entry->target_section->output_section->vma);
12261 odd = off & 4;
12262 off = targ - off;
12263
12264 if (info->emitrelocations)
12265 {
12266 unsigned int num_rel;
12267 if (stub_entry->type.sub == ppc_stub_notoc)
12268 num_rel = num_relocs_for_power10_offset (off, odd);
12269 else
12270 num_rel = num_relocs_for_offset (off - 8);
12271 stub_entry->group->stub_sec->reloc_count += num_rel;
12272 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12273 }
12274
12275 if (stub_entry->type.sub == ppc_stub_notoc)
12276 extra = size_power10_offset (off, odd);
12277 else
12278 extra = size_offset (off - 8);
12279 /* Include branch insn plus those in the offset sequence. */
12280 size += 4 + extra;
12281 /* The branch insn is at the end, or "extra" bytes along. So
12282 its offset will be "extra" bytes less that that already
12283 calculated. */
12284 off -= extra;
12285
12286 if (stub_entry->type.sub != ppc_stub_notoc)
12287 {
12288 /* After the bcl, lr has been modified so we need to emit
12289 .eh_frame info saying the return address is in r12. */
12290 lr_used = stub_entry->stub_offset + 8;
12291 if (stub_entry->type.r2save)
12292 lr_used += 4;
12293 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12294 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12295 DW_CFA_restore_extended 65. */
12296 delta = lr_used - stub_entry->group->lr_restore;
12297 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12298 stub_entry->group->lr_restore = lr_used + 8;
12299 }
12300
12301 /* If the branch can't reach, use a plt_branch. */
12302 if (off + (1 << 25) >= (bfd_vma) (1 << 26))
12303 {
12304 stub_entry->type.main += ppc_stub_plt_branch - ppc_stub_long_branch;
12305 size += 4;
12306 }
12307 else if (info->emitrelocations)
12308 stub_entry->group->stub_sec->reloc_count +=1;
12309 }
12310 else if (stub_entry->type.sub >= ppc_stub_notoc)
12311 {
12312 BFD_ASSERT (stub_entry->type.main == ppc_stub_plt_call);
12313 lr_used = 0;
12314 if (stub_entry->h != NULL
12315 && is_tls_get_addr (&stub_entry->h->elf, htab)
12316 && htab->params->tls_get_addr_opt)
12317 {
12318 lr_used += 7 * 4;
12319 if (!htab->params->no_tls_get_addr_regsave)
12320 lr_used += 11 * 4;
12321 else if (stub_entry->type.r2save)
12322 lr_used += 2 * 4;
12323 }
12324 if (stub_entry->type.r2save)
12325 lr_used += 4;
12326 targ = stub_entry->plt_ent->plt.offset & ~1;
12327 if (targ >= (bfd_vma) -2)
12328 abort ();
12329
12330 plt = htab->elf.splt;
12331 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12332 {
12333 if (stub_entry->symtype == STT_GNU_IFUNC)
12334 plt = htab->elf.iplt;
12335 else
12336 plt = htab->pltlocal;
12337 }
12338 targ += plt->output_offset + plt->output_section->vma;
12339 off = (stub_entry->stub_offset
12340 + stub_entry->group->stub_sec->output_offset
12341 + stub_entry->group->stub_sec->output_section->vma
12342 + lr_used);
12343 odd = off & 4;
12344 off = targ - off;
12345
12346 if (htab->params->plt_stub_align != 0)
12347 {
12348 unsigned pad = plt_stub_pad (htab, stub_entry, off, odd);
12349
12350 stub_entry->group->stub_sec->size += pad;
12351 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12352 off -= pad;
12353 odd ^= pad & 4;
12354 }
12355
12356 if (info->emitrelocations)
12357 {
12358 unsigned int num_rel;
12359 if (stub_entry->type.sub == ppc_stub_notoc)
12360 num_rel = num_relocs_for_power10_offset (off, odd);
12361 else
12362 num_rel = num_relocs_for_offset (off - 8);
12363 stub_entry->group->stub_sec->reloc_count += num_rel;
12364 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12365 }
12366
12367 size = plt_stub_size (htab, stub_entry, off, odd);
12368
12369 if (stub_entry->type.sub != ppc_stub_notoc)
12370 {
12371 /* After the bcl, lr has been modified so we need to emit
12372 .eh_frame info saying the return address is in r12. */
12373 lr_used += stub_entry->stub_offset + 8;
12374 /* The eh_frame info will consist of a DW_CFA_advance_loc or
12375 variant, DW_CFA_register, 65, 12, DW_CFA_advance_loc+2,
12376 DW_CFA_restore_extended 65. */
12377 delta = lr_used - stub_entry->group->lr_restore;
12378 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12379 stub_entry->group->lr_restore = lr_used + 8;
12380 }
12381 if (stub_entry->h != NULL
12382 && is_tls_get_addr (&stub_entry->h->elf, htab)
12383 && htab->params->tls_get_addr_opt)
12384 {
12385 if (!htab->params->no_tls_get_addr_regsave)
12386 {
12387 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12388 delta = cfa_updt - stub_entry->group->lr_restore;
12389 stub_entry->group->eh_size += eh_advance_size (delta);
12390 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12391 stub_entry->group->lr_restore
12392 = stub_entry->stub_offset + size - 4;
12393 }
12394 else if (stub_entry->type.r2save)
12395 {
12396 lr_used = stub_entry->stub_offset + size - 20;
12397 delta = lr_used - stub_entry->group->lr_restore;
12398 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12399 stub_entry->group->lr_restore
12400 = stub_entry->stub_offset + size - 4;
12401 }
12402 }
12403 }
12404 else if (stub_entry->type.main == ppc_stub_plt_call)
12405 {
12406 targ = stub_entry->plt_ent->plt.offset & ~(bfd_vma) 1;
12407 if (targ >= (bfd_vma) -2)
12408 abort ();
12409 plt = htab->elf.splt;
12410 if (use_local_plt (info, elf_hash_entry (stub_entry->h)))
12411 {
12412 if (stub_entry->symtype == STT_GNU_IFUNC)
12413 plt = htab->elf.iplt;
12414 else
12415 plt = htab->pltlocal;
12416 }
12417 targ += plt->output_offset + plt->output_section->vma;
12418
12419 off = (elf_gp (info->output_bfd)
12420 + htab->sec_info[stub_entry->group->link_sec->id].toc_off);
12421 off = targ - off;
12422
12423 if (htab->params->plt_stub_align != 0)
12424 {
12425 unsigned pad = plt_stub_pad (htab, stub_entry, off, 0);
12426
12427 stub_entry->group->stub_sec->size += pad;
12428 stub_entry->stub_offset = stub_entry->group->stub_sec->size;
12429 }
12430
12431 if (info->emitrelocations)
12432 {
12433 stub_entry->group->stub_sec->reloc_count
12434 += ((PPC_HA (off) != 0)
12435 + (htab->opd_abi
12436 ? 2 + (htab->params->plt_static_chain
12437 && PPC_HA (off + 16) == PPC_HA (off))
12438 : 1));
12439 stub_entry->group->stub_sec->flags |= SEC_RELOC;
12440 }
12441
12442 size = plt_stub_size (htab, stub_entry, off, 0);
12443
12444 if (stub_entry->h != NULL
12445 && is_tls_get_addr (&stub_entry->h->elf, htab)
12446 && htab->params->tls_get_addr_opt
12447 && stub_entry->type.r2save)
12448 {
12449 if (!htab->params->no_tls_get_addr_regsave)
12450 {
12451 /* Adjustments to r1 need to be described. */
12452 unsigned int cfa_updt = stub_entry->stub_offset + 18 * 4;
12453 delta = cfa_updt - stub_entry->group->lr_restore;
12454 stub_entry->group->eh_size += eh_advance_size (delta);
12455 stub_entry->group->eh_size += htab->opd_abi ? 36 : 35;
12456 }
12457 else
12458 {
12459 lr_used = stub_entry->stub_offset + size - 20;
12460 /* The eh_frame info will consist of a DW_CFA_advance_loc
12461 or variant, DW_CFA_offset_externed_sf, 65, -stackoff,
12462 DW_CFA_advance_loc+4, DW_CFA_restore_extended, 65. */
12463 delta = lr_used - stub_entry->group->lr_restore;
12464 stub_entry->group->eh_size += eh_advance_size (delta) + 6;
12465 }
12466 stub_entry->group->lr_restore = stub_entry->stub_offset + size - 4;
12467 }
12468 }
12469 else
12470 {
12471 BFD_FAIL ();
12472 return false;
12473 }
12474
12475 stub_entry->group->stub_sec->size += size;
12476 return true;
12477 }
12478
12479 /* Set up various things so that we can make a list of input sections
12480 for each output section included in the link. Returns -1 on error,
12481 0 when no stubs will be needed, and 1 on success. */
12482
12483 int
12484 ppc64_elf_setup_section_lists (struct bfd_link_info *info)
12485 {
12486 unsigned int id;
12487 size_t amt;
12488 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12489
12490 if (htab == NULL)
12491 return -1;
12492
12493 htab->sec_info_arr_size = _bfd_section_id;
12494 amt = sizeof (*htab->sec_info) * (htab->sec_info_arr_size);
12495 htab->sec_info = bfd_zmalloc (amt);
12496 if (htab->sec_info == NULL)
12497 return -1;
12498
12499 /* Set toc_off for com, und, abs and ind sections. */
12500 for (id = 0; id < 3; id++)
12501 htab->sec_info[id].toc_off = TOC_BASE_OFF;
12502
12503 return 1;
12504 }
12505
12506 /* Set up for first pass at multitoc partitioning. */
12507
12508 void
12509 ppc64_elf_start_multitoc_partition (struct bfd_link_info *info)
12510 {
12511 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12512
12513 htab->toc_curr = ppc64_elf_set_toc (info, info->output_bfd);
12514 htab->toc_bfd = NULL;
12515 htab->toc_first_sec = NULL;
12516 }
12517
12518 /* The linker repeatedly calls this function for each TOC input section
12519 and linker generated GOT section. Group input bfds such that the toc
12520 within a group is less than 64k in size. */
12521
12522 bool
12523 ppc64_elf_next_toc_section (struct bfd_link_info *info, asection *isec)
12524 {
12525 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12526 bfd_vma addr, off, limit;
12527
12528 if (htab == NULL)
12529 return false;
12530
12531 if (!htab->second_toc_pass)
12532 {
12533 /* Keep track of the first .toc or .got section for this input bfd. */
12534 bool new_bfd = htab->toc_bfd != isec->owner;
12535
12536 if (new_bfd)
12537 {
12538 htab->toc_bfd = isec->owner;
12539 htab->toc_first_sec = isec;
12540 }
12541
12542 addr = isec->output_offset + isec->output_section->vma;
12543 off = addr - htab->toc_curr;
12544 limit = 0x80008000;
12545 if (ppc64_elf_tdata (isec->owner)->has_small_toc_reloc)
12546 limit = 0x10000;
12547 if (off + isec->size > limit)
12548 {
12549 addr = (htab->toc_first_sec->output_offset
12550 + htab->toc_first_sec->output_section->vma);
12551 htab->toc_curr = addr;
12552 htab->toc_curr &= -TOC_BASE_ALIGN;
12553 }
12554
12555 /* toc_curr is the base address of this toc group. Set elf_gp
12556 for the input section to be the offset relative to the
12557 output toc base plus 0x8000. Making the input elf_gp an
12558 offset allows us to move the toc as a whole without
12559 recalculating input elf_gp. */
12560 off = htab->toc_curr - elf_gp (info->output_bfd);
12561 off += TOC_BASE_OFF;
12562
12563 /* Die if someone uses a linker script that doesn't keep input
12564 file .toc and .got together. */
12565 if (new_bfd
12566 && elf_gp (isec->owner) != 0
12567 && elf_gp (isec->owner) != off)
12568 return false;
12569
12570 elf_gp (isec->owner) = off;
12571 return true;
12572 }
12573
12574 /* During the second pass toc_first_sec points to the start of
12575 a toc group, and toc_curr is used to track the old elf_gp.
12576 We use toc_bfd to ensure we only look at each bfd once. */
12577 if (htab->toc_bfd == isec->owner)
12578 return true;
12579 htab->toc_bfd = isec->owner;
12580
12581 if (htab->toc_first_sec == NULL
12582 || htab->toc_curr != elf_gp (isec->owner))
12583 {
12584 htab->toc_curr = elf_gp (isec->owner);
12585 htab->toc_first_sec = isec;
12586 }
12587 addr = (htab->toc_first_sec->output_offset
12588 + htab->toc_first_sec->output_section->vma);
12589 off = addr - elf_gp (info->output_bfd) + TOC_BASE_OFF;
12590 elf_gp (isec->owner) = off;
12591
12592 return true;
12593 }
12594
12595 /* Called via elf_link_hash_traverse to merge GOT entries for global
12596 symbol H. */
12597
12598 static bool
12599 merge_global_got (struct elf_link_hash_entry *h, void *inf ATTRIBUTE_UNUSED)
12600 {
12601 if (h->root.type == bfd_link_hash_indirect)
12602 return true;
12603
12604 merge_got_entries (&h->got.glist);
12605
12606 return true;
12607 }
12608
12609 /* Called via elf_link_hash_traverse to allocate GOT entries for global
12610 symbol H. */
12611
12612 static bool
12613 reallocate_got (struct elf_link_hash_entry *h, void *inf)
12614 {
12615 struct got_entry *gent;
12616
12617 if (h->root.type == bfd_link_hash_indirect)
12618 return true;
12619
12620 for (gent = h->got.glist; gent != NULL; gent = gent->next)
12621 if (!gent->is_indirect)
12622 allocate_got (h, (struct bfd_link_info *) inf, gent);
12623 return true;
12624 }
12625
12626 /* Called on the first multitoc pass after the last call to
12627 ppc64_elf_next_toc_section. This function removes duplicate GOT
12628 entries. */
12629
12630 bool
12631 ppc64_elf_layout_multitoc (struct bfd_link_info *info)
12632 {
12633 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12634 struct bfd *ibfd, *ibfd2;
12635 bool done_something;
12636
12637 htab->multi_toc_needed = htab->toc_curr != elf_gp (info->output_bfd);
12638
12639 if (!htab->do_multi_toc)
12640 return false;
12641
12642 /* Merge global sym got entries within a toc group. */
12643 elf_link_hash_traverse (&htab->elf, merge_global_got, info);
12644
12645 /* And tlsld_got. */
12646 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12647 {
12648 struct got_entry *ent, *ent2;
12649
12650 if (!is_ppc64_elf (ibfd))
12651 continue;
12652
12653 ent = ppc64_tlsld_got (ibfd);
12654 if (!ent->is_indirect
12655 && ent->got.offset != (bfd_vma) -1)
12656 {
12657 for (ibfd2 = ibfd->link.next; ibfd2 != NULL; ibfd2 = ibfd2->link.next)
12658 {
12659 if (!is_ppc64_elf (ibfd2))
12660 continue;
12661
12662 ent2 = ppc64_tlsld_got (ibfd2);
12663 if (!ent2->is_indirect
12664 && ent2->got.offset != (bfd_vma) -1
12665 && elf_gp (ibfd2) == elf_gp (ibfd))
12666 {
12667 ent2->is_indirect = true;
12668 ent2->got.ent = ent;
12669 }
12670 }
12671 }
12672 }
12673
12674 /* Zap sizes of got sections. */
12675 htab->elf.irelplt->rawsize = htab->elf.irelplt->size;
12676 htab->elf.irelplt->size -= htab->got_reli_size;
12677 htab->got_reli_size = 0;
12678
12679 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12680 {
12681 asection *got, *relgot;
12682
12683 if (!is_ppc64_elf (ibfd))
12684 continue;
12685
12686 got = ppc64_elf_tdata (ibfd)->got;
12687 if (got != NULL)
12688 {
12689 got->rawsize = got->size;
12690 got->size = 0;
12691 relgot = ppc64_elf_tdata (ibfd)->relgot;
12692 relgot->rawsize = relgot->size;
12693 relgot->size = 0;
12694 }
12695 }
12696
12697 /* Now reallocate the got, local syms first. We don't need to
12698 allocate section contents again since we never increase size. */
12699 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12700 {
12701 struct got_entry **lgot_ents;
12702 struct got_entry **end_lgot_ents;
12703 struct plt_entry **local_plt;
12704 struct plt_entry **end_local_plt;
12705 unsigned char *lgot_masks;
12706 bfd_size_type locsymcount;
12707 Elf_Internal_Shdr *symtab_hdr;
12708 asection *s;
12709
12710 if (!is_ppc64_elf (ibfd))
12711 continue;
12712
12713 lgot_ents = elf_local_got_ents (ibfd);
12714 if (!lgot_ents)
12715 continue;
12716
12717 symtab_hdr = &elf_symtab_hdr (ibfd);
12718 locsymcount = symtab_hdr->sh_info;
12719 end_lgot_ents = lgot_ents + locsymcount;
12720 local_plt = (struct plt_entry **) end_lgot_ents;
12721 end_local_plt = local_plt + locsymcount;
12722 lgot_masks = (unsigned char *) end_local_plt;
12723 s = ppc64_elf_tdata (ibfd)->got;
12724 for (; lgot_ents < end_lgot_ents; ++lgot_ents, ++lgot_masks)
12725 {
12726 struct got_entry *ent;
12727
12728 for (ent = *lgot_ents; ent != NULL; ent = ent->next)
12729 {
12730 unsigned int ent_size = 8;
12731 unsigned int rel_size = sizeof (Elf64_External_Rela);
12732
12733 ent->got.offset = s->size;
12734 if ((ent->tls_type & *lgot_masks & TLS_GD) != 0)
12735 {
12736 ent_size *= 2;
12737 rel_size *= 2;
12738 }
12739 s->size += ent_size;
12740 if ((*lgot_masks & (TLS_TLS | PLT_IFUNC)) == PLT_IFUNC)
12741 {
12742 htab->elf.irelplt->size += rel_size;
12743 htab->got_reli_size += rel_size;
12744 }
12745 else if (bfd_link_pic (info)
12746 && !(ent->tls_type != 0
12747 && bfd_link_executable (info)))
12748 {
12749 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12750 srel->size += rel_size;
12751 }
12752 }
12753 }
12754 }
12755
12756 elf_link_hash_traverse (&htab->elf, reallocate_got, info);
12757
12758 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12759 {
12760 struct got_entry *ent;
12761
12762 if (!is_ppc64_elf (ibfd))
12763 continue;
12764
12765 ent = ppc64_tlsld_got (ibfd);
12766 if (!ent->is_indirect
12767 && ent->got.offset != (bfd_vma) -1)
12768 {
12769 asection *s = ppc64_elf_tdata (ibfd)->got;
12770 ent->got.offset = s->size;
12771 s->size += 16;
12772 if (bfd_link_dll (info))
12773 {
12774 asection *srel = ppc64_elf_tdata (ibfd)->relgot;
12775 srel->size += sizeof (Elf64_External_Rela);
12776 }
12777 }
12778 }
12779
12780 done_something = htab->elf.irelplt->rawsize != htab->elf.irelplt->size;
12781 if (!done_something)
12782 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
12783 {
12784 asection *got;
12785
12786 if (!is_ppc64_elf (ibfd))
12787 continue;
12788
12789 got = ppc64_elf_tdata (ibfd)->got;
12790 if (got != NULL)
12791 {
12792 done_something = got->rawsize != got->size;
12793 if (done_something)
12794 break;
12795 }
12796 }
12797
12798 if (done_something)
12799 (*htab->params->layout_sections_again) ();
12800
12801 /* Set up for second pass over toc sections to recalculate elf_gp
12802 on input sections. */
12803 htab->toc_bfd = NULL;
12804 htab->toc_first_sec = NULL;
12805 htab->second_toc_pass = true;
12806 return done_something;
12807 }
12808
12809 /* Called after second pass of multitoc partitioning. */
12810
12811 void
12812 ppc64_elf_finish_multitoc_partition (struct bfd_link_info *info)
12813 {
12814 struct ppc_link_hash_table *htab = ppc_hash_table (info);
12815
12816 /* After the second pass, toc_curr tracks the TOC offset used
12817 for code sections below in ppc64_elf_next_input_section. */
12818 htab->toc_curr = TOC_BASE_OFF;
12819 }
12820
12821 /* No toc references were found in ISEC. If the code in ISEC makes no
12822 calls, then there's no need to use toc adjusting stubs when branching
12823 into ISEC. Actually, indirect calls from ISEC are OK as they will
12824 load r2. Returns -1 on error, 0 for no stub needed, 1 for stub
12825 needed, and 2 if a cyclical call-graph was found but no other reason
12826 for a stub was detected. If called from the top level, a return of
12827 2 means the same as a return of 0. */
12828
12829 static int
12830 toc_adjusting_stub_needed (struct bfd_link_info *info, asection *isec)
12831 {
12832 int ret;
12833
12834 /* Mark this section as checked. */
12835 isec->call_check_done = 1;
12836
12837 /* We know none of our code bearing sections will need toc stubs. */
12838 if ((isec->flags & SEC_LINKER_CREATED) != 0)
12839 return 0;
12840
12841 if (isec->size == 0)
12842 return 0;
12843
12844 if (isec->output_section == NULL)
12845 return 0;
12846
12847 ret = 0;
12848 if (isec->reloc_count != 0)
12849 {
12850 Elf_Internal_Rela *relstart, *rel;
12851 Elf_Internal_Sym *local_syms;
12852 struct ppc_link_hash_table *htab;
12853
12854 relstart = _bfd_elf_link_read_relocs (isec->owner, isec, NULL, NULL,
12855 info->keep_memory);
12856 if (relstart == NULL)
12857 return -1;
12858
12859 /* Look for branches to outside of this section. */
12860 local_syms = NULL;
12861 htab = ppc_hash_table (info);
12862 if (htab == NULL)
12863 return -1;
12864
12865 for (rel = relstart; rel < relstart + isec->reloc_count; ++rel)
12866 {
12867 enum elf_ppc64_reloc_type r_type;
12868 unsigned long r_symndx;
12869 struct elf_link_hash_entry *h;
12870 struct ppc_link_hash_entry *eh;
12871 Elf_Internal_Sym *sym;
12872 asection *sym_sec;
12873 struct _opd_sec_data *opd;
12874 bfd_vma sym_value;
12875 bfd_vma dest;
12876
12877 r_type = ELF64_R_TYPE (rel->r_info);
12878 if (r_type != R_PPC64_REL24
12879 && r_type != R_PPC64_REL24_NOTOC
12880 && r_type != R_PPC64_REL24_P9NOTOC
12881 && r_type != R_PPC64_REL14
12882 && r_type != R_PPC64_REL14_BRTAKEN
12883 && r_type != R_PPC64_REL14_BRNTAKEN
12884 && r_type != R_PPC64_PLTCALL
12885 && r_type != R_PPC64_PLTCALL_NOTOC)
12886 continue;
12887
12888 r_symndx = ELF64_R_SYM (rel->r_info);
12889 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms, r_symndx,
12890 isec->owner))
12891 {
12892 ret = -1;
12893 break;
12894 }
12895
12896 /* Calls to dynamic lib functions go through a plt call stub
12897 that uses r2. */
12898 eh = ppc_elf_hash_entry (h);
12899 if (eh != NULL
12900 && (eh->elf.plt.plist != NULL
12901 || (eh->oh != NULL
12902 && ppc_follow_link (eh->oh)->elf.plt.plist != NULL)))
12903 {
12904 ret = 1;
12905 break;
12906 }
12907
12908 if (sym_sec == NULL)
12909 /* Ignore other undefined symbols. */
12910 continue;
12911
12912 /* Assume branches to other sections not included in the
12913 link need stubs too, to cover -R and absolute syms. */
12914 if (sym_sec->output_section == NULL)
12915 {
12916 ret = 1;
12917 break;
12918 }
12919
12920 if (h == NULL)
12921 sym_value = sym->st_value;
12922 else
12923 {
12924 if (h->root.type != bfd_link_hash_defined
12925 && h->root.type != bfd_link_hash_defweak)
12926 abort ();
12927 sym_value = h->root.u.def.value;
12928 }
12929 sym_value += rel->r_addend;
12930
12931 /* If this branch reloc uses an opd sym, find the code section. */
12932 opd = get_opd_info (sym_sec);
12933 if (opd != NULL)
12934 {
12935 if (h == NULL && opd->adjust != NULL)
12936 {
12937 long adjust;
12938
12939 adjust = opd->adjust[OPD_NDX (sym_value)];
12940 if (adjust == -1)
12941 /* Assume deleted functions won't ever be called. */
12942 continue;
12943 sym_value += adjust;
12944 }
12945
12946 dest = opd_entry_value (sym_sec, sym_value,
12947 &sym_sec, NULL, false);
12948 if (dest == (bfd_vma) -1)
12949 continue;
12950 }
12951 else
12952 dest = (sym_value
12953 + sym_sec->output_offset
12954 + sym_sec->output_section->vma);
12955
12956 /* Ignore branch to self. */
12957 if (sym_sec == isec)
12958 continue;
12959
12960 /* If the called function uses the toc, we need a stub. */
12961 if (sym_sec->has_toc_reloc
12962 || sym_sec->makes_toc_func_call)
12963 {
12964 ret = 1;
12965 break;
12966 }
12967
12968 /* Assume any branch that needs a long branch stub might in fact
12969 need a plt_branch stub. A plt_branch stub uses r2. */
12970 else if (dest - (isec->output_offset
12971 + isec->output_section->vma
12972 + rel->r_offset) + (1 << 25)
12973 >= (2u << 25) - PPC64_LOCAL_ENTRY_OFFSET (h
12974 ? h->other
12975 : sym->st_other))
12976 {
12977 ret = 1;
12978 break;
12979 }
12980
12981 /* If calling back to a section in the process of being
12982 tested, we can't say for sure that no toc adjusting stubs
12983 are needed, so don't return zero. */
12984 else if (sym_sec->call_check_in_progress)
12985 ret = 2;
12986
12987 /* Branches to another section that itself doesn't have any TOC
12988 references are OK. Recursively call ourselves to check. */
12989 else if (!sym_sec->call_check_done)
12990 {
12991 int recur;
12992
12993 /* Mark current section as indeterminate, so that other
12994 sections that call back to current won't be marked as
12995 known. */
12996 isec->call_check_in_progress = 1;
12997 recur = toc_adjusting_stub_needed (info, sym_sec);
12998 isec->call_check_in_progress = 0;
12999
13000 if (recur != 0)
13001 {
13002 ret = recur;
13003 if (recur != 2)
13004 break;
13005 }
13006 }
13007 }
13008
13009 if (elf_symtab_hdr (isec->owner).contents
13010 != (unsigned char *) local_syms)
13011 free (local_syms);
13012 if (elf_section_data (isec)->relocs != relstart)
13013 free (relstart);
13014 }
13015
13016 if ((ret & 1) == 0
13017 && isec->map_head.s != NULL
13018 && (strcmp (isec->output_section->name, ".init") == 0
13019 || strcmp (isec->output_section->name, ".fini") == 0))
13020 {
13021 if (isec->map_head.s->has_toc_reloc
13022 || isec->map_head.s->makes_toc_func_call)
13023 ret = 1;
13024 else if (!isec->map_head.s->call_check_done)
13025 {
13026 int recur;
13027 isec->call_check_in_progress = 1;
13028 recur = toc_adjusting_stub_needed (info, isec->map_head.s);
13029 isec->call_check_in_progress = 0;
13030 if (recur != 0)
13031 ret = recur;
13032 }
13033 }
13034
13035 if (ret == 1)
13036 isec->makes_toc_func_call = 1;
13037
13038 return ret;
13039 }
13040
13041 /* The linker repeatedly calls this function for each input section,
13042 in the order that input sections are linked into output sections.
13043 Build lists of input sections to determine groupings between which
13044 we may insert linker stubs. */
13045
13046 bool
13047 ppc64_elf_next_input_section (struct bfd_link_info *info, asection *isec)
13048 {
13049 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13050
13051 if (htab == NULL)
13052 return false;
13053
13054 if ((isec->output_section->flags & SEC_CODE) != 0
13055 && isec->output_section->id < htab->sec_info_arr_size)
13056 {
13057 /* This happens to make the list in reverse order,
13058 which is what we want. */
13059 htab->sec_info[isec->id].u.list
13060 = htab->sec_info[isec->output_section->id].u.list;
13061 htab->sec_info[isec->output_section->id].u.list = isec;
13062 }
13063
13064 if (htab->multi_toc_needed)
13065 {
13066 /* Analyse sections that aren't already flagged as needing a
13067 valid toc pointer. Exclude .fixup for the linux kernel.
13068 .fixup contains branches, but only back to the function that
13069 hit an exception. */
13070 if (!(isec->has_toc_reloc
13071 || (isec->flags & SEC_CODE) == 0
13072 || strcmp (isec->name, ".fixup") == 0
13073 || isec->call_check_done))
13074 {
13075 if (toc_adjusting_stub_needed (info, isec) < 0)
13076 return false;
13077 }
13078 /* Make all sections use the TOC assigned for this object file.
13079 This will be wrong for pasted sections; We fix that in
13080 check_pasted_section(). */
13081 if (elf_gp (isec->owner) != 0)
13082 htab->toc_curr = elf_gp (isec->owner);
13083 }
13084
13085 htab->sec_info[isec->id].toc_off = htab->toc_curr;
13086 return true;
13087 }
13088
13089 /* Check that all .init and .fini sections use the same toc, if they
13090 have toc relocs. */
13091
13092 static bool
13093 check_pasted_section (struct bfd_link_info *info, const char *name)
13094 {
13095 asection *o = bfd_get_section_by_name (info->output_bfd, name);
13096
13097 if (o != NULL)
13098 {
13099 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13100 bfd_vma toc_off = 0;
13101 asection *i;
13102
13103 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13104 if (i->has_toc_reloc)
13105 {
13106 if (toc_off == 0)
13107 toc_off = htab->sec_info[i->id].toc_off;
13108 else if (toc_off != htab->sec_info[i->id].toc_off)
13109 return false;
13110 }
13111
13112 if (toc_off == 0)
13113 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13114 if (i->makes_toc_func_call)
13115 {
13116 toc_off = htab->sec_info[i->id].toc_off;
13117 break;
13118 }
13119
13120 /* Make sure the whole pasted function uses the same toc offset. */
13121 if (toc_off != 0)
13122 for (i = o->map_head.s; i != NULL; i = i->map_head.s)
13123 htab->sec_info[i->id].toc_off = toc_off;
13124 }
13125 return true;
13126 }
13127
13128 bool
13129 ppc64_elf_check_init_fini (struct bfd_link_info *info)
13130 {
13131 return (check_pasted_section (info, ".init")
13132 & check_pasted_section (info, ".fini"));
13133 }
13134
13135 /* See whether we can group stub sections together. Grouping stub
13136 sections may result in fewer stubs. More importantly, we need to
13137 put all .init* and .fini* stubs at the beginning of the .init or
13138 .fini output sections respectively, because glibc splits the
13139 _init and _fini functions into multiple parts. Putting a stub in
13140 the middle of a function is not a good idea. */
13141
13142 static bool
13143 group_sections (struct bfd_link_info *info,
13144 bfd_size_type stub_group_size,
13145 bool stubs_always_before_branch)
13146 {
13147 struct ppc_link_hash_table *htab;
13148 asection *osec;
13149 bool suppress_size_errors;
13150
13151 htab = ppc_hash_table (info);
13152 if (htab == NULL)
13153 return false;
13154
13155 suppress_size_errors = false;
13156 if (stub_group_size == 1)
13157 {
13158 /* Default values. */
13159 if (stubs_always_before_branch)
13160 stub_group_size = 0x1e00000;
13161 else
13162 stub_group_size = 0x1c00000;
13163 suppress_size_errors = true;
13164 }
13165
13166 for (osec = info->output_bfd->sections; osec != NULL; osec = osec->next)
13167 {
13168 asection *tail;
13169
13170 if (osec->id >= htab->sec_info_arr_size)
13171 continue;
13172
13173 tail = htab->sec_info[osec->id].u.list;
13174 while (tail != NULL)
13175 {
13176 asection *curr;
13177 asection *prev;
13178 bfd_size_type total;
13179 bool big_sec;
13180 bfd_vma curr_toc;
13181 struct map_stub *group;
13182 bfd_size_type group_size;
13183
13184 curr = tail;
13185 total = tail->size;
13186 group_size = (ppc64_elf_section_data (tail) != NULL
13187 && ppc64_elf_section_data (tail)->has_14bit_branch
13188 ? stub_group_size >> 10 : stub_group_size);
13189
13190 big_sec = total > group_size;
13191 if (big_sec && !suppress_size_errors)
13192 /* xgettext:c-format */
13193 _bfd_error_handler (_("%pB section %pA exceeds stub group size"),
13194 tail->owner, tail);
13195 curr_toc = htab->sec_info[tail->id].toc_off;
13196
13197 while ((prev = htab->sec_info[curr->id].u.list) != NULL
13198 && ((total += curr->output_offset - prev->output_offset)
13199 < (ppc64_elf_section_data (prev) != NULL
13200 && ppc64_elf_section_data (prev)->has_14bit_branch
13201 ? (group_size = stub_group_size >> 10) : group_size))
13202 && htab->sec_info[prev->id].toc_off == curr_toc)
13203 curr = prev;
13204
13205 /* OK, the size from the start of CURR to the end is less
13206 than group_size and thus can be handled by one stub
13207 section. (or the tail section is itself larger than
13208 group_size, in which case we may be toast.) We should
13209 really be keeping track of the total size of stubs added
13210 here, as stubs contribute to the final output section
13211 size. That's a little tricky, and this way will only
13212 break if stubs added make the total size more than 2^25,
13213 ie. for the default stub_group_size, if stubs total more
13214 than 2097152 bytes, or nearly 75000 plt call stubs. */
13215 group = bfd_alloc (curr->owner, sizeof (*group));
13216 if (group == NULL)
13217 return false;
13218 group->link_sec = curr;
13219 group->stub_sec = NULL;
13220 group->needs_save_res = 0;
13221 group->lr_restore = 0;
13222 group->eh_size = 0;
13223 group->eh_base = 0;
13224 group->next = htab->group;
13225 htab->group = group;
13226 do
13227 {
13228 prev = htab->sec_info[tail->id].u.list;
13229 /* Set up this stub group. */
13230 htab->sec_info[tail->id].u.group = group;
13231 }
13232 while (tail != curr && (tail = prev) != NULL);
13233
13234 /* But wait, there's more! Input sections up to group_size
13235 bytes before the stub section can be handled by it too.
13236 Don't do this if we have a really large section after the
13237 stubs, as adding more stubs increases the chance that
13238 branches may not reach into the stub section. */
13239 if (!stubs_always_before_branch && !big_sec)
13240 {
13241 total = 0;
13242 while (prev != NULL
13243 && ((total += tail->output_offset - prev->output_offset)
13244 < (ppc64_elf_section_data (prev) != NULL
13245 && ppc64_elf_section_data (prev)->has_14bit_branch
13246 ? (group_size = stub_group_size >> 10)
13247 : group_size))
13248 && htab->sec_info[prev->id].toc_off == curr_toc)
13249 {
13250 tail = prev;
13251 prev = htab->sec_info[tail->id].u.list;
13252 htab->sec_info[tail->id].u.group = group;
13253 }
13254 }
13255 tail = prev;
13256 }
13257 }
13258 return true;
13259 }
13260
13261 static const unsigned char glink_eh_frame_cie[] =
13262 {
13263 0, 0, 0, 16, /* length. */
13264 0, 0, 0, 0, /* id. */
13265 1, /* CIE version. */
13266 'z', 'R', 0, /* Augmentation string. */
13267 4, /* Code alignment. */
13268 0x78, /* Data alignment. */
13269 65, /* RA reg. */
13270 1, /* Augmentation size. */
13271 DW_EH_PE_pcrel | DW_EH_PE_sdata4, /* FDE encoding. */
13272 DW_CFA_def_cfa, 1, 0 /* def_cfa: r1 offset 0. */
13273 };
13274
13275 /* Stripping output sections is normally done before dynamic section
13276 symbols have been allocated. This function is called later, and
13277 handles cases like htab->brlt which is mapped to its own output
13278 section. */
13279
13280 static void
13281 maybe_strip_output (struct bfd_link_info *info, asection *isec)
13282 {
13283 if (isec->size == 0
13284 && isec->output_section->size == 0
13285 && !(isec->output_section->flags & SEC_KEEP)
13286 && !bfd_section_removed_from_list (info->output_bfd,
13287 isec->output_section)
13288 && elf_section_data (isec->output_section)->dynindx == 0)
13289 {
13290 isec->output_section->flags |= SEC_EXCLUDE;
13291 bfd_section_list_remove (info->output_bfd, isec->output_section);
13292 info->output_bfd->section_count--;
13293 }
13294 }
13295
13296 /* Determine and set the size of the stub section for a final link.
13297
13298 The basic idea here is to examine all the relocations looking for
13299 PC-relative calls to a target that is unreachable with a "bl"
13300 instruction. */
13301
13302 bool
13303 ppc64_elf_size_stubs (struct bfd_link_info *info)
13304 {
13305 bfd_size_type stub_group_size;
13306 bool stubs_always_before_branch;
13307 struct ppc_link_hash_table *htab = ppc_hash_table (info);
13308
13309 if (htab == NULL)
13310 return false;
13311
13312 if (htab->params->power10_stubs == -1 && !htab->has_power10_relocs)
13313 htab->params->power10_stubs = 0;
13314
13315 if (htab->params->plt_thread_safe == -1 && !bfd_link_executable (info))
13316 htab->params->plt_thread_safe = 1;
13317 if (!htab->opd_abi)
13318 htab->params->plt_thread_safe = 0;
13319 else if (htab->params->plt_thread_safe == -1)
13320 {
13321 static const char *const thread_starter[] =
13322 {
13323 "pthread_create",
13324 /* libstdc++ */
13325 "_ZNSt6thread15_M_start_threadESt10shared_ptrINS_10_Impl_baseEE",
13326 /* librt */
13327 "aio_init", "aio_read", "aio_write", "aio_fsync", "lio_listio",
13328 "mq_notify", "create_timer",
13329 /* libanl */
13330 "getaddrinfo_a",
13331 /* libgomp */
13332 "GOMP_parallel",
13333 "GOMP_parallel_start",
13334 "GOMP_parallel_loop_static",
13335 "GOMP_parallel_loop_static_start",
13336 "GOMP_parallel_loop_dynamic",
13337 "GOMP_parallel_loop_dynamic_start",
13338 "GOMP_parallel_loop_guided",
13339 "GOMP_parallel_loop_guided_start",
13340 "GOMP_parallel_loop_runtime",
13341 "GOMP_parallel_loop_runtime_start",
13342 "GOMP_parallel_sections",
13343 "GOMP_parallel_sections_start",
13344 /* libgo */
13345 "__go_go",
13346 };
13347 unsigned i;
13348
13349 for (i = 0; i < ARRAY_SIZE (thread_starter); i++)
13350 {
13351 struct elf_link_hash_entry *h;
13352 h = elf_link_hash_lookup (&htab->elf, thread_starter[i],
13353 false, false, true);
13354 htab->params->plt_thread_safe = h != NULL && h->ref_regular;
13355 if (htab->params->plt_thread_safe)
13356 break;
13357 }
13358 }
13359 stubs_always_before_branch = htab->params->group_size < 0;
13360 if (htab->params->group_size < 0)
13361 stub_group_size = -htab->params->group_size;
13362 else
13363 stub_group_size = htab->params->group_size;
13364
13365 if (!group_sections (info, stub_group_size, stubs_always_before_branch))
13366 return false;
13367
13368 htab->tga_group = NULL;
13369 if (!htab->params->no_tls_get_addr_regsave
13370 && htab->tga_desc_fd != NULL
13371 && (htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefined
13372 || htab->tga_desc_fd->elf.root.type == bfd_link_hash_undefweak)
13373 && htab->tls_get_addr_fd != NULL
13374 && is_static_defined (&htab->tls_get_addr_fd->elf))
13375 {
13376 asection *sym_sec, *code_sec, *stub_sec;
13377 bfd_vma sym_value;
13378 struct _opd_sec_data *opd;
13379
13380 sym_sec = htab->tls_get_addr_fd->elf.root.u.def.section;
13381 sym_value = defined_sym_val (&htab->tls_get_addr_fd->elf);
13382 code_sec = sym_sec;
13383 opd = get_opd_info (sym_sec);
13384 if (opd != NULL)
13385 opd_entry_value (sym_sec, sym_value, &code_sec, NULL, false);
13386 htab->tga_group = htab->sec_info[code_sec->id].u.group;
13387 stub_sec = (*htab->params->add_stub_section) (".tga_desc.stub",
13388 htab->tga_group->link_sec);
13389 if (stub_sec == NULL)
13390 return false;
13391 htab->tga_group->stub_sec = stub_sec;
13392
13393 htab->tga_desc_fd->elf.root.type = bfd_link_hash_defined;
13394 htab->tga_desc_fd->elf.root.u.def.section = stub_sec;
13395 htab->tga_desc_fd->elf.root.u.def.value = 0;
13396 htab->tga_desc_fd->elf.type = STT_FUNC;
13397 htab->tga_desc_fd->elf.def_regular = 1;
13398 htab->tga_desc_fd->elf.non_elf = 0;
13399 _bfd_elf_link_hash_hide_symbol (info, &htab->tga_desc_fd->elf, true);
13400 }
13401
13402 #define STUB_SHRINK_ITER 20
13403 /* Loop until no stubs added. After iteration 20 of this loop we may
13404 exit on a stub section shrinking. This is to break out of a
13405 pathological case where adding stubs on one iteration decreases
13406 section gaps (perhaps due to alignment), which then requires
13407 fewer or smaller stubs on the next iteration. */
13408
13409 while (1)
13410 {
13411 bfd *input_bfd;
13412 unsigned int bfd_indx;
13413 struct map_stub *group;
13414
13415 htab->stub_iteration += 1;
13416
13417 for (input_bfd = info->input_bfds, bfd_indx = 0;
13418 input_bfd != NULL;
13419 input_bfd = input_bfd->link.next, bfd_indx++)
13420 {
13421 Elf_Internal_Shdr *symtab_hdr;
13422 asection *section;
13423 Elf_Internal_Sym *local_syms = NULL;
13424
13425 if (!is_ppc64_elf (input_bfd))
13426 continue;
13427
13428 /* We'll need the symbol table in a second. */
13429 symtab_hdr = &elf_symtab_hdr (input_bfd);
13430 if (symtab_hdr->sh_info == 0)
13431 continue;
13432
13433 /* Walk over each section attached to the input bfd. */
13434 for (section = input_bfd->sections;
13435 section != NULL;
13436 section = section->next)
13437 {
13438 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
13439
13440 /* If there aren't any relocs, then there's nothing more
13441 to do. */
13442 if ((section->flags & SEC_RELOC) == 0
13443 || (section->flags & SEC_ALLOC) == 0
13444 || (section->flags & SEC_LOAD) == 0
13445 || (section->flags & SEC_CODE) == 0
13446 || section->reloc_count == 0)
13447 continue;
13448
13449 /* If this section is a link-once section that will be
13450 discarded, then don't create any stubs. */
13451 if (section->output_section == NULL
13452 || section->output_section->owner != info->output_bfd)
13453 continue;
13454
13455 /* Get the relocs. */
13456 internal_relocs
13457 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
13458 info->keep_memory);
13459 if (internal_relocs == NULL)
13460 goto error_ret_free_local;
13461
13462 /* Now examine each relocation. */
13463 irela = internal_relocs;
13464 irelaend = irela + section->reloc_count;
13465 for (; irela < irelaend; irela++)
13466 {
13467 enum elf_ppc64_reloc_type r_type;
13468 unsigned int r_indx;
13469 struct ppc_stub_type stub_type;
13470 struct ppc_stub_hash_entry *stub_entry;
13471 asection *sym_sec, *code_sec;
13472 bfd_vma sym_value, code_value;
13473 bfd_vma destination;
13474 unsigned long local_off;
13475 bool ok_dest;
13476 struct ppc_link_hash_entry *hash;
13477 struct ppc_link_hash_entry *fdh;
13478 struct elf_link_hash_entry *h;
13479 Elf_Internal_Sym *sym;
13480 char *stub_name;
13481 const asection *id_sec;
13482 struct _opd_sec_data *opd;
13483 struct plt_entry *plt_ent;
13484
13485 r_type = ELF64_R_TYPE (irela->r_info);
13486 r_indx = ELF64_R_SYM (irela->r_info);
13487
13488 if (r_type >= R_PPC64_max)
13489 {
13490 bfd_set_error (bfd_error_bad_value);
13491 goto error_ret_free_internal;
13492 }
13493
13494 /* Only look for stubs on branch instructions. */
13495 if (r_type != R_PPC64_REL24
13496 && r_type != R_PPC64_REL24_NOTOC
13497 && r_type != R_PPC64_REL24_P9NOTOC
13498 && r_type != R_PPC64_REL14
13499 && r_type != R_PPC64_REL14_BRTAKEN
13500 && r_type != R_PPC64_REL14_BRNTAKEN)
13501 continue;
13502
13503 /* Now determine the call target, its name, value,
13504 section. */
13505 if (!get_sym_h (&h, &sym, &sym_sec, NULL, &local_syms,
13506 r_indx, input_bfd))
13507 goto error_ret_free_internal;
13508 hash = ppc_elf_hash_entry (h);
13509
13510 ok_dest = false;
13511 fdh = NULL;
13512 sym_value = 0;
13513 if (hash == NULL)
13514 {
13515 sym_value = sym->st_value;
13516 if (sym_sec != NULL
13517 && sym_sec->output_section != NULL)
13518 ok_dest = true;
13519 }
13520 else if (hash->elf.root.type == bfd_link_hash_defined
13521 || hash->elf.root.type == bfd_link_hash_defweak)
13522 {
13523 sym_value = hash->elf.root.u.def.value;
13524 if (sym_sec->output_section != NULL)
13525 ok_dest = true;
13526 }
13527 else if (hash->elf.root.type == bfd_link_hash_undefweak
13528 || hash->elf.root.type == bfd_link_hash_undefined)
13529 {
13530 /* Recognise an old ABI func code entry sym, and
13531 use the func descriptor sym instead if it is
13532 defined. */
13533 if (hash->elf.root.root.string[0] == '.'
13534 && hash->oh != NULL)
13535 {
13536 fdh = ppc_follow_link (hash->oh);
13537 if (fdh->elf.root.type == bfd_link_hash_defined
13538 || fdh->elf.root.type == bfd_link_hash_defweak)
13539 {
13540 sym_sec = fdh->elf.root.u.def.section;
13541 sym_value = fdh->elf.root.u.def.value;
13542 if (sym_sec->output_section != NULL)
13543 ok_dest = true;
13544 }
13545 else
13546 fdh = NULL;
13547 }
13548 }
13549 else
13550 {
13551 bfd_set_error (bfd_error_bad_value);
13552 goto error_ret_free_internal;
13553 }
13554
13555 destination = 0;
13556 local_off = 0;
13557 if (ok_dest)
13558 {
13559 sym_value += irela->r_addend;
13560 destination = (sym_value
13561 + sym_sec->output_offset
13562 + sym_sec->output_section->vma);
13563 local_off = PPC64_LOCAL_ENTRY_OFFSET (hash
13564 ? hash->elf.other
13565 : sym->st_other);
13566 }
13567
13568 code_sec = sym_sec;
13569 code_value = sym_value;
13570 opd = get_opd_info (sym_sec);
13571 if (opd != NULL)
13572 {
13573 bfd_vma dest;
13574
13575 if (hash == NULL && opd->adjust != NULL)
13576 {
13577 long adjust = opd->adjust[OPD_NDX (sym_value)];
13578 if (adjust == -1)
13579 continue;
13580 code_value += adjust;
13581 sym_value += adjust;
13582 }
13583 dest = opd_entry_value (sym_sec, sym_value,
13584 &code_sec, &code_value, false);
13585 if (dest != (bfd_vma) -1)
13586 {
13587 destination = dest;
13588 if (fdh != NULL)
13589 {
13590 /* Fixup old ABI sym to point at code
13591 entry. */
13592 hash->elf.root.type = bfd_link_hash_defweak;
13593 hash->elf.root.u.def.section = code_sec;
13594 hash->elf.root.u.def.value = code_value;
13595 }
13596 }
13597 }
13598
13599 /* Determine what (if any) linker stub is needed. */
13600 plt_ent = NULL;
13601 stub_type.main = ppc_type_of_stub (section, irela, &hash,
13602 &plt_ent, destination,
13603 local_off);
13604 stub_type.sub = ppc_stub_toc;
13605 stub_type.r2save = 0;
13606
13607 if (r_type == R_PPC64_REL24_NOTOC
13608 || r_type == R_PPC64_REL24_P9NOTOC)
13609 {
13610 enum ppc_stub_sub_type notoc = ppc_stub_notoc;
13611 if (htab->params->power10_stubs == 0
13612 || (r_type == R_PPC64_REL24_P9NOTOC
13613 && htab->params->power10_stubs != 1))
13614 notoc = ppc_stub_p9notoc;
13615 if (stub_type.main == ppc_stub_plt_call)
13616 stub_type.sub = notoc;
13617 else if (stub_type.main == ppc_stub_long_branch
13618 || (code_sec != NULL
13619 && code_sec->output_section != NULL
13620 && (((hash ? hash->elf.other : sym->st_other)
13621 & STO_PPC64_LOCAL_MASK)
13622 > 1 << STO_PPC64_LOCAL_BIT)))
13623 {
13624 stub_type.main = ppc_stub_long_branch;
13625 stub_type.sub = notoc;
13626 stub_type.r2save = 0;
13627 }
13628 }
13629 else if (stub_type.main != ppc_stub_plt_call)
13630 {
13631 /* Check whether we need a TOC adjusting stub.
13632 Since the linker pastes together pieces from
13633 different object files when creating the
13634 _init and _fini functions, it may be that a
13635 call to what looks like a local sym is in
13636 fact a call needing a TOC adjustment. */
13637 if ((code_sec != NULL
13638 && code_sec->output_section != NULL
13639 && (code_sec->has_toc_reloc
13640 || code_sec->makes_toc_func_call)
13641 && (htab->sec_info[code_sec->id].toc_off
13642 != htab->sec_info[section->id].toc_off))
13643 || (((hash ? hash->elf.other : sym->st_other)
13644 & STO_PPC64_LOCAL_MASK)
13645 == 1 << STO_PPC64_LOCAL_BIT))
13646 {
13647 stub_type.main = ppc_stub_long_branch;
13648 stub_type.sub = ppc_stub_toc;
13649 stub_type.r2save = 1;
13650 }
13651 }
13652
13653 if (stub_type.main == ppc_stub_none)
13654 continue;
13655
13656 /* __tls_get_addr calls might be eliminated. */
13657 if (stub_type.main != ppc_stub_plt_call
13658 && hash != NULL
13659 && is_tls_get_addr (&hash->elf, htab)
13660 && section->has_tls_reloc
13661 && irela != internal_relocs)
13662 {
13663 /* Get tls info. */
13664 unsigned char *tls_mask;
13665
13666 if (!get_tls_mask (&tls_mask, NULL, NULL, &local_syms,
13667 irela - 1, input_bfd))
13668 goto error_ret_free_internal;
13669 if ((*tls_mask & TLS_TLS) != 0
13670 && (*tls_mask & (TLS_GD | TLS_LD)) == 0)
13671 continue;
13672 }
13673
13674 if (stub_type.main == ppc_stub_plt_call
13675 && stub_type.sub == ppc_stub_toc)
13676 {
13677 if (!htab->opd_abi
13678 && htab->params->plt_localentry0 != 0
13679 && is_elfv2_localentry0 (&hash->elf))
13680 htab->has_plt_localentry0 = 1;
13681 else if (irela + 1 < irelaend
13682 && irela[1].r_offset == irela->r_offset + 4
13683 && (ELF64_R_TYPE (irela[1].r_info)
13684 == R_PPC64_TOCSAVE))
13685 {
13686 if (!tocsave_find (htab, INSERT,
13687 &local_syms, irela + 1, input_bfd))
13688 goto error_ret_free_internal;
13689 }
13690 else
13691 stub_type.r2save = 1;
13692 }
13693
13694 /* Support for grouping stub sections. */
13695 id_sec = htab->sec_info[section->id].u.group->link_sec;
13696
13697 /* Get the name of this stub. */
13698 stub_name = ppc_stub_name (id_sec, sym_sec, hash, irela);
13699 if (!stub_name)
13700 goto error_ret_free_internal;
13701
13702 stub_entry = ppc_stub_hash_lookup (&htab->stub_hash_table,
13703 stub_name, false, false);
13704 if (stub_entry != NULL)
13705 {
13706 free (stub_name);
13707 if (!ppc_merge_stub (htab, stub_entry, stub_type, r_type))
13708 {
13709 /* xgettext:c-format */
13710 _bfd_error_handler
13711 (_("%pB: cannot create stub entry %s"),
13712 section->owner, stub_entry->root.string);
13713 goto error_ret_free_internal;
13714 }
13715 continue;
13716 }
13717
13718 stub_entry = ppc_add_stub (stub_name, section, info);
13719 if (stub_entry == NULL)
13720 {
13721 free (stub_name);
13722 error_ret_free_internal:
13723 if (elf_section_data (section)->relocs == NULL)
13724 free (internal_relocs);
13725 error_ret_free_local:
13726 if (symtab_hdr->contents
13727 != (unsigned char *) local_syms)
13728 free (local_syms);
13729 return false;
13730 }
13731
13732 stub_entry->type = stub_type;
13733 if (stub_type.main == ppc_stub_plt_call)
13734 {
13735 stub_entry->target_value = sym_value;
13736 stub_entry->target_section = sym_sec;
13737 }
13738 else
13739 {
13740 stub_entry->target_value = code_value;
13741 stub_entry->target_section = code_sec;
13742 }
13743 stub_entry->h = hash;
13744 stub_entry->plt_ent = plt_ent;
13745 stub_entry->symtype
13746 = hash ? hash->elf.type : ELF_ST_TYPE (sym->st_info);
13747 stub_entry->other = hash ? hash->elf.other : sym->st_other;
13748
13749 if (hash != NULL
13750 && (hash->elf.root.type == bfd_link_hash_defined
13751 || hash->elf.root.type == bfd_link_hash_defweak))
13752 htab->stub_globals += 1;
13753 }
13754
13755 /* We're done with the internal relocs, free them. */
13756 if (elf_section_data (section)->relocs != internal_relocs)
13757 free (internal_relocs);
13758 }
13759
13760 if (local_syms != NULL
13761 && symtab_hdr->contents != (unsigned char *) local_syms)
13762 {
13763 if (!info->keep_memory)
13764 free (local_syms);
13765 else
13766 symtab_hdr->contents = (unsigned char *) local_syms;
13767 }
13768 }
13769
13770 /* We may have added some stubs. Find out the new size of the
13771 stub sections. */
13772 for (group = htab->group; group != NULL; group = group->next)
13773 {
13774 group->lr_restore = 0;
13775 group->eh_size = 0;
13776 if (group->stub_sec != NULL)
13777 {
13778 asection *stub_sec = group->stub_sec;
13779
13780 if (htab->stub_iteration <= STUB_SHRINK_ITER
13781 || stub_sec->rawsize < stub_sec->size)
13782 /* Past STUB_SHRINK_ITER, rawsize is the max size seen. */
13783 stub_sec->rawsize = stub_sec->size;
13784 stub_sec->size = 0;
13785 stub_sec->reloc_count = 0;
13786 stub_sec->flags &= ~SEC_RELOC;
13787 }
13788 }
13789 if (htab->tga_group != NULL)
13790 {
13791 /* See emit_tga_desc and emit_tga_desc_eh_frame. */
13792 htab->tga_group->eh_size
13793 = 1 + 2 + (htab->opd_abi != 0) + 3 + 8 * 2 + 3 + 8 + 3;
13794 htab->tga_group->lr_restore = 23 * 4;
13795 htab->tga_group->stub_sec->size = 24 * 4;
13796 }
13797
13798 if (htab->stub_iteration <= STUB_SHRINK_ITER
13799 || htab->brlt->rawsize < htab->brlt->size)
13800 htab->brlt->rawsize = htab->brlt->size;
13801 htab->brlt->size = 0;
13802 htab->brlt->reloc_count = 0;
13803 htab->brlt->flags &= ~SEC_RELOC;
13804 if (htab->relbrlt != NULL)
13805 htab->relbrlt->size = 0;
13806
13807 bfd_hash_traverse (&htab->stub_hash_table, ppc_size_one_stub, info);
13808
13809 for (group = htab->group; group != NULL; group = group->next)
13810 if (group->needs_save_res)
13811 group->stub_sec->size += htab->sfpr->size;
13812
13813 if (info->emitrelocations
13814 && htab->glink != NULL && htab->glink->size != 0)
13815 {
13816 htab->glink->reloc_count = 1;
13817 htab->glink->flags |= SEC_RELOC;
13818 }
13819
13820 if (htab->glink_eh_frame != NULL
13821 && !bfd_is_abs_section (htab->glink_eh_frame->output_section)
13822 && htab->glink_eh_frame->output_section->size > 8)
13823 {
13824 size_t size = 0, align = 4;
13825
13826 for (group = htab->group; group != NULL; group = group->next)
13827 if (group->eh_size != 0)
13828 size += (group->eh_size + 17 + align - 1) & -align;
13829 if (htab->glink != NULL && htab->glink->size != 0)
13830 size += (24 + align - 1) & -align;
13831 if (size != 0)
13832 size += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
13833 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13834 size = (size + align - 1) & -align;
13835 htab->glink_eh_frame->rawsize = htab->glink_eh_frame->size;
13836 htab->glink_eh_frame->size = size;
13837 }
13838
13839 if (htab->params->plt_stub_align != 0)
13840 for (group = htab->group; group != NULL; group = group->next)
13841 if (group->stub_sec != NULL)
13842 {
13843 int align = abs (htab->params->plt_stub_align);
13844 group->stub_sec->size
13845 = (group->stub_sec->size + (1 << align) - 1) & -(1 << align);
13846 }
13847
13848 for (group = htab->group; group != NULL; group = group->next)
13849 if (group->stub_sec != NULL
13850 && group->stub_sec->rawsize != group->stub_sec->size
13851 && (htab->stub_iteration <= STUB_SHRINK_ITER
13852 || group->stub_sec->rawsize < group->stub_sec->size))
13853 break;
13854
13855 if (group == NULL
13856 && (htab->brlt->rawsize == htab->brlt->size
13857 || (htab->stub_iteration > STUB_SHRINK_ITER
13858 && htab->brlt->rawsize > htab->brlt->size))
13859 && (htab->glink_eh_frame == NULL
13860 || htab->glink_eh_frame->rawsize == htab->glink_eh_frame->size)
13861 && (htab->tga_group == NULL
13862 || htab->stub_iteration > 1))
13863 break;
13864
13865 /* Ask the linker to do its stuff. */
13866 (*htab->params->layout_sections_again) ();
13867 }
13868
13869 if (htab->glink_eh_frame != NULL
13870 && htab->glink_eh_frame->size != 0)
13871 {
13872 bfd_vma val;
13873 bfd_byte *p, *last_fde;
13874 size_t last_fde_len, size, align, pad;
13875 struct map_stub *group;
13876
13877 /* It is necessary to at least have a rough outline of the
13878 linker generated CIEs and FDEs written before
13879 bfd_elf_discard_info is run, in order for these FDEs to be
13880 indexed in .eh_frame_hdr. */
13881 p = bfd_zalloc (htab->glink_eh_frame->owner, htab->glink_eh_frame->size);
13882 if (p == NULL)
13883 return false;
13884 htab->glink_eh_frame->contents = p;
13885 last_fde = p;
13886 align = 4;
13887
13888 memcpy (p, glink_eh_frame_cie, sizeof (glink_eh_frame_cie));
13889 /* CIE length (rewrite in case little-endian). */
13890 last_fde_len = ((sizeof (glink_eh_frame_cie) + align - 1) & -align) - 4;
13891 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13892 p += last_fde_len + 4;
13893
13894 for (group = htab->group; group != NULL; group = group->next)
13895 if (group->eh_size != 0)
13896 {
13897 group->eh_base = p - htab->glink_eh_frame->contents;
13898 last_fde = p;
13899 last_fde_len = ((group->eh_size + 17 + 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 stub section, written later. */
13908 p += 4;
13909 /* stub section size. */
13910 bfd_put_32 (htab->elf.dynobj, group->stub_sec->size, p);
13911 p += 4;
13912 /* Augmentation. */
13913 p += 1;
13914 /* Make sure we don't have all nops. This is enough for
13915 elf-eh-frame.c to detect the last non-nop opcode. */
13916 p[group->eh_size - 1] = DW_CFA_advance_loc + 1;
13917 p = last_fde + last_fde_len + 4;
13918 }
13919 if (htab->glink != NULL && htab->glink->size != 0)
13920 {
13921 last_fde = p;
13922 last_fde_len = ((24 + align - 1) & -align) - 4;
13923 /* FDE length. */
13924 bfd_put_32 (htab->elf.dynobj, last_fde_len, p);
13925 p += 4;
13926 /* CIE pointer. */
13927 val = p - htab->glink_eh_frame->contents;
13928 bfd_put_32 (htab->elf.dynobj, val, p);
13929 p += 4;
13930 /* Offset to .glink, written later. */
13931 p += 4;
13932 /* .glink size. */
13933 bfd_put_32 (htab->elf.dynobj, htab->glink->size - 8, p);
13934 p += 4;
13935 /* Augmentation. */
13936 p += 1;
13937
13938 *p++ = DW_CFA_advance_loc + (htab->has_plt_localentry0 ? 3 : 2);
13939 *p++ = DW_CFA_register;
13940 *p++ = 65;
13941 *p++ = htab->opd_abi ? 12 : 0;
13942 *p++ = DW_CFA_advance_loc + (htab->opd_abi ? 4 : 2);
13943 *p++ = DW_CFA_restore_extended;
13944 *p++ = 65;
13945 p += ((24 + align - 1) & -align) - 24;
13946 }
13947 /* Subsume any padding into the last FDE if user .eh_frame
13948 sections are aligned more than glink_eh_frame. Otherwise any
13949 zero padding will be seen as a terminator. */
13950 align = 1ul << htab->glink_eh_frame->output_section->alignment_power;
13951 size = p - htab->glink_eh_frame->contents;
13952 pad = ((size + align - 1) & -align) - size;
13953 htab->glink_eh_frame->size = size + pad;
13954 bfd_put_32 (htab->elf.dynobj, last_fde_len + pad, last_fde);
13955 }
13956
13957 maybe_strip_output (info, htab->brlt);
13958 if (htab->relbrlt != NULL)
13959 maybe_strip_output (info, htab->relbrlt);
13960 if (htab->glink_eh_frame != NULL)
13961 maybe_strip_output (info, htab->glink_eh_frame);
13962
13963 return true;
13964 }
13965
13966 /* Called after we have determined section placement. If sections
13967 move, we'll be called again. Provide a value for TOCstart. */
13968
13969 bfd_vma
13970 ppc64_elf_set_toc (struct bfd_link_info *info, bfd *obfd)
13971 {
13972 asection *s;
13973 bfd_vma TOCstart, adjust;
13974
13975 if (info != NULL)
13976 {
13977 struct elf_link_hash_entry *h;
13978 struct elf_link_hash_table *htab = elf_hash_table (info);
13979
13980 if (is_elf_hash_table (&htab->root)
13981 && htab->hgot != NULL)
13982 h = htab->hgot;
13983 else
13984 {
13985 h = (struct elf_link_hash_entry *)
13986 bfd_link_hash_lookup (&htab->root, ".TOC.", false, false, true);
13987 if (is_elf_hash_table (&htab->root))
13988 htab->hgot = h;
13989 }
13990 if (h != NULL
13991 && h->root.type == bfd_link_hash_defined
13992 && !h->root.linker_def
13993 && (!is_elf_hash_table (&htab->root)
13994 || h->def_regular))
13995 {
13996 TOCstart = defined_sym_val (h) - TOC_BASE_OFF;
13997 _bfd_set_gp_value (obfd, TOCstart);
13998 return TOCstart;
13999 }
14000 }
14001
14002 /* The TOC consists of sections .got, .toc, .tocbss, .plt in that
14003 order. The TOC starts where the first of these sections starts. */
14004 s = bfd_get_section_by_name (obfd, ".got");
14005 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14006 s = bfd_get_section_by_name (obfd, ".toc");
14007 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14008 s = bfd_get_section_by_name (obfd, ".tocbss");
14009 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14010 s = bfd_get_section_by_name (obfd, ".plt");
14011 if (s == NULL || (s->flags & SEC_EXCLUDE) != 0)
14012 {
14013 /* This may happen for
14014 o references to TOC base (SYM@toc / TOC[tc0]) without a
14015 .toc directive
14016 o bad linker script
14017 o --gc-sections and empty TOC sections
14018
14019 FIXME: Warn user? */
14020
14021 /* Look for a likely section. We probably won't even be
14022 using TOCstart. */
14023 for (s = obfd->sections; s != NULL; s = s->next)
14024 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_READONLY
14025 | SEC_EXCLUDE))
14026 == (SEC_ALLOC | SEC_SMALL_DATA))
14027 break;
14028 if (s == NULL)
14029 for (s = obfd->sections; s != NULL; s = s->next)
14030 if ((s->flags & (SEC_ALLOC | SEC_SMALL_DATA | SEC_EXCLUDE))
14031 == (SEC_ALLOC | SEC_SMALL_DATA))
14032 break;
14033 if (s == NULL)
14034 for (s = obfd->sections; s != NULL; s = s->next)
14035 if ((s->flags & (SEC_ALLOC | SEC_READONLY | SEC_EXCLUDE))
14036 == SEC_ALLOC)
14037 break;
14038 if (s == NULL)
14039 for (s = obfd->sections; s != NULL; s = s->next)
14040 if ((s->flags & (SEC_ALLOC | SEC_EXCLUDE)) == SEC_ALLOC)
14041 break;
14042 }
14043
14044 TOCstart = 0;
14045 if (s != NULL)
14046 TOCstart = s->output_section->vma + s->output_offset;
14047
14048 /* Force alignment. */
14049 adjust = TOCstart & (TOC_BASE_ALIGN - 1);
14050 TOCstart -= adjust;
14051 _bfd_set_gp_value (obfd, TOCstart);
14052
14053 if (info != NULL && s != NULL)
14054 {
14055 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14056
14057 if (htab != NULL)
14058 {
14059 if (htab->elf.hgot != NULL)
14060 {
14061 htab->elf.hgot->root.u.def.value = TOC_BASE_OFF - adjust;
14062 htab->elf.hgot->root.u.def.section = s;
14063 }
14064 }
14065 else
14066 {
14067 struct bfd_link_hash_entry *bh = NULL;
14068 _bfd_generic_link_add_one_symbol (info, obfd, ".TOC.", BSF_GLOBAL,
14069 s, TOC_BASE_OFF - adjust,
14070 NULL, false, false, &bh);
14071 }
14072 }
14073 return TOCstart;
14074 }
14075
14076 /* Called via elf_link_hash_traverse from ppc64_elf_build_stubs to
14077 write out any global entry stubs, and PLT relocations. */
14078
14079 static bool
14080 build_global_entry_stubs_and_plt (struct elf_link_hash_entry *h, void *inf)
14081 {
14082 struct bfd_link_info *info;
14083 struct ppc_link_hash_table *htab;
14084 struct plt_entry *ent;
14085 asection *s;
14086
14087 if (h->root.type == bfd_link_hash_indirect)
14088 return true;
14089
14090 info = inf;
14091 htab = ppc_hash_table (info);
14092 if (htab == NULL)
14093 return false;
14094
14095 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14096 if (ent->plt.offset != (bfd_vma) -1)
14097 {
14098 /* This symbol has an entry in the procedure linkage
14099 table. Set it up. */
14100 Elf_Internal_Rela rela;
14101 asection *plt, *relplt;
14102 bfd_byte *loc;
14103
14104 if (use_local_plt (info, h))
14105 {
14106 if (!(h->def_regular
14107 && (h->root.type == bfd_link_hash_defined
14108 || h->root.type == bfd_link_hash_defweak)))
14109 continue;
14110 if (h->type == STT_GNU_IFUNC)
14111 {
14112 plt = htab->elf.iplt;
14113 relplt = htab->elf.irelplt;
14114 htab->elf.ifunc_resolvers = true;
14115 if (htab->opd_abi)
14116 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14117 else
14118 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14119 }
14120 else
14121 {
14122 plt = htab->pltlocal;
14123 if (bfd_link_pic (info))
14124 {
14125 relplt = htab->relpltlocal;
14126 if (htab->opd_abi)
14127 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14128 else
14129 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14130 }
14131 else
14132 relplt = NULL;
14133 }
14134 rela.r_addend = defined_sym_val (h) + ent->addend;
14135
14136 if (relplt == NULL)
14137 {
14138 loc = plt->contents + ent->plt.offset;
14139 bfd_put_64 (info->output_bfd, rela.r_addend, loc);
14140 if (htab->opd_abi)
14141 {
14142 bfd_vma toc = elf_gp (info->output_bfd);
14143 toc += htab->sec_info[h->root.u.def.section->id].toc_off;
14144 bfd_put_64 (info->output_bfd, toc, loc + 8);
14145 }
14146 }
14147 else
14148 {
14149 rela.r_offset = (plt->output_section->vma
14150 + plt->output_offset
14151 + ent->plt.offset);
14152 loc = relplt->contents + (relplt->reloc_count++
14153 * sizeof (Elf64_External_Rela));
14154 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14155 }
14156 }
14157 else
14158 {
14159 rela.r_offset = (htab->elf.splt->output_section->vma
14160 + htab->elf.splt->output_offset
14161 + ent->plt.offset);
14162 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_JMP_SLOT);
14163 rela.r_addend = ent->addend;
14164 loc = (htab->elf.srelplt->contents
14165 + ((ent->plt.offset - PLT_INITIAL_ENTRY_SIZE (htab))
14166 / PLT_ENTRY_SIZE (htab) * sizeof (Elf64_External_Rela)));
14167 if (h->type == STT_GNU_IFUNC && is_static_defined (h))
14168 htab->elf.ifunc_resolvers = true;
14169 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14170 }
14171 }
14172
14173 if (!h->pointer_equality_needed)
14174 return true;
14175
14176 if (h->def_regular)
14177 return true;
14178
14179 s = htab->global_entry;
14180 if (s == NULL || s->size == 0)
14181 return true;
14182
14183 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
14184 if (ent->plt.offset != (bfd_vma) -1
14185 && ent->addend == 0)
14186 {
14187 bfd_byte *p;
14188 asection *plt;
14189 bfd_vma off;
14190
14191 p = s->contents + h->root.u.def.value;
14192 plt = htab->elf.splt;
14193 if (use_local_plt (info, h))
14194 {
14195 if (h->type == STT_GNU_IFUNC)
14196 plt = htab->elf.iplt;
14197 else
14198 plt = htab->pltlocal;
14199 }
14200 off = ent->plt.offset + plt->output_offset + plt->output_section->vma;
14201 off -= h->root.u.def.value + s->output_offset + s->output_section->vma;
14202
14203 if (off + 0x80008000 > 0xffffffff || (off & 3) != 0)
14204 {
14205 info->callbacks->einfo
14206 (_("%P: linkage table error against `%pT'\n"),
14207 h->root.root.string);
14208 bfd_set_error (bfd_error_bad_value);
14209 htab->stub_error = true;
14210 }
14211
14212 htab->stub_count[ppc_stub_global_entry - 1] += 1;
14213 if (htab->params->emit_stub_syms)
14214 {
14215 size_t len = strlen (h->root.root.string);
14216 char *name = bfd_malloc (sizeof "12345678.global_entry." + len);
14217
14218 if (name == NULL)
14219 return false;
14220
14221 sprintf (name, "%08x.global_entry.%s", s->id, h->root.root.string);
14222 h = elf_link_hash_lookup (&htab->elf, name, true, false, false);
14223 if (h == NULL)
14224 return false;
14225 if (h->root.type == bfd_link_hash_new)
14226 {
14227 h->root.type = bfd_link_hash_defined;
14228 h->root.u.def.section = s;
14229 h->root.u.def.value = p - s->contents;
14230 h->ref_regular = 1;
14231 h->def_regular = 1;
14232 h->ref_regular_nonweak = 1;
14233 h->forced_local = 1;
14234 h->non_elf = 0;
14235 h->root.linker_def = 1;
14236 }
14237 }
14238
14239 if (PPC_HA (off) != 0)
14240 {
14241 bfd_put_32 (s->owner, ADDIS_R12_R12 | PPC_HA (off), p);
14242 p += 4;
14243 }
14244 bfd_put_32 (s->owner, LD_R12_0R12 | PPC_LO (off), p);
14245 p += 4;
14246 bfd_put_32 (s->owner, MTCTR_R12, p);
14247 p += 4;
14248 bfd_put_32 (s->owner, BCTR, p);
14249 break;
14250 }
14251 return true;
14252 }
14253
14254 /* Write PLT relocs for locals. */
14255
14256 static bool
14257 write_plt_relocs_for_local_syms (struct bfd_link_info *info)
14258 {
14259 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14260 bfd *ibfd;
14261
14262 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
14263 {
14264 struct got_entry **lgot_ents, **end_lgot_ents;
14265 struct plt_entry **local_plt, **lplt, **end_local_plt;
14266 Elf_Internal_Shdr *symtab_hdr;
14267 bfd_size_type locsymcount;
14268 Elf_Internal_Sym *local_syms = NULL;
14269 struct plt_entry *ent;
14270
14271 if (!is_ppc64_elf (ibfd))
14272 continue;
14273
14274 lgot_ents = elf_local_got_ents (ibfd);
14275 if (!lgot_ents)
14276 continue;
14277
14278 symtab_hdr = &elf_symtab_hdr (ibfd);
14279 locsymcount = symtab_hdr->sh_info;
14280 end_lgot_ents = lgot_ents + locsymcount;
14281 local_plt = (struct plt_entry **) end_lgot_ents;
14282 end_local_plt = local_plt + locsymcount;
14283 for (lplt = local_plt; lplt < end_local_plt; ++lplt)
14284 for (ent = *lplt; ent != NULL; ent = ent->next)
14285 if (ent->plt.offset != (bfd_vma) -1)
14286 {
14287 Elf_Internal_Sym *sym;
14288 asection *sym_sec;
14289 asection *plt, *relplt;
14290 bfd_byte *loc;
14291 bfd_vma val;
14292
14293 if (!get_sym_h (NULL, &sym, &sym_sec, NULL, &local_syms,
14294 lplt - local_plt, ibfd))
14295 {
14296 if (symtab_hdr->contents != (unsigned char *) local_syms)
14297 free (local_syms);
14298 return false;
14299 }
14300
14301 val = sym->st_value + ent->addend;
14302 if (sym_sec != NULL && sym_sec->output_section != NULL)
14303 val += sym_sec->output_offset + sym_sec->output_section->vma;
14304
14305 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14306 {
14307 htab->elf.ifunc_resolvers = true;
14308 plt = htab->elf.iplt;
14309 relplt = htab->elf.irelplt;
14310 }
14311 else
14312 {
14313 plt = htab->pltlocal;
14314 relplt = bfd_link_pic (info) ? htab->relpltlocal : NULL;
14315 }
14316
14317 if (relplt == NULL)
14318 {
14319 loc = plt->contents + ent->plt.offset;
14320 bfd_put_64 (info->output_bfd, val, loc);
14321 if (htab->opd_abi)
14322 {
14323 bfd_vma toc = elf_gp (ibfd);
14324 bfd_put_64 (info->output_bfd, toc, loc + 8);
14325 }
14326 }
14327 else
14328 {
14329 Elf_Internal_Rela rela;
14330 rela.r_offset = (ent->plt.offset
14331 + plt->output_offset
14332 + plt->output_section->vma);
14333 if (ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
14334 {
14335 if (htab->opd_abi)
14336 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_IREL);
14337 else
14338 rela.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
14339 }
14340 else
14341 {
14342 if (htab->opd_abi)
14343 rela.r_info = ELF64_R_INFO (0, R_PPC64_JMP_SLOT);
14344 else
14345 rela.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
14346 }
14347 rela.r_addend = val;
14348 loc = relplt->contents + (relplt->reloc_count++
14349 * sizeof (Elf64_External_Rela));
14350 bfd_elf64_swap_reloca_out (info->output_bfd, &rela, loc);
14351 }
14352 }
14353
14354 if (local_syms != NULL
14355 && symtab_hdr->contents != (unsigned char *) local_syms)
14356 {
14357 if (!info->keep_memory)
14358 free (local_syms);
14359 else
14360 symtab_hdr->contents = (unsigned char *) local_syms;
14361 }
14362 }
14363 return true;
14364 }
14365
14366 /* Emit the static wrapper function preserving registers around a
14367 __tls_get_addr_opt call. */
14368
14369 static bool
14370 emit_tga_desc (struct ppc_link_hash_table *htab)
14371 {
14372 asection *stub_sec = htab->tga_group->stub_sec;
14373 unsigned int cfa_updt = 11 * 4;
14374 bfd_byte *p;
14375 bfd_vma to, from, delta;
14376
14377 BFD_ASSERT (htab->tga_desc_fd->elf.root.type == bfd_link_hash_defined
14378 && htab->tga_desc_fd->elf.root.u.def.section == stub_sec
14379 && htab->tga_desc_fd->elf.root.u.def.value == 0);
14380 to = defined_sym_val (&htab->tls_get_addr_fd->elf);
14381 from = defined_sym_val (&htab->tga_desc_fd->elf) + cfa_updt;
14382 delta = to - from;
14383 if (delta + (1 << 25) >= 1 << 26)
14384 {
14385 _bfd_error_handler (_("__tls_get_addr call offset overflow"));
14386 htab->stub_error = true;
14387 return false;
14388 }
14389
14390 p = stub_sec->contents;
14391 p = tls_get_addr_prologue (htab->elf.dynobj, p, htab);
14392 bfd_put_32 (stub_sec->owner, B_DOT | 1 | (delta & 0x3fffffc), p);
14393 p += 4;
14394 p = tls_get_addr_epilogue (htab->elf.dynobj, p, htab);
14395 return stub_sec->size == (bfd_size_type) (p - stub_sec->contents);
14396 }
14397
14398 /* Emit eh_frame describing the static wrapper function. */
14399
14400 static bfd_byte *
14401 emit_tga_desc_eh_frame (struct ppc_link_hash_table *htab, bfd_byte *p)
14402 {
14403 unsigned int cfa_updt = 11 * 4;
14404 unsigned int i;
14405
14406 *p++ = DW_CFA_advance_loc + cfa_updt / 4;
14407 *p++ = DW_CFA_def_cfa_offset;
14408 if (htab->opd_abi)
14409 {
14410 *p++ = 128;
14411 *p++ = 1;
14412 }
14413 else
14414 *p++ = 96;
14415 *p++ = DW_CFA_offset_extended_sf;
14416 *p++ = 65;
14417 *p++ = (-16 / 8) & 0x7f;
14418 for (i = 4; i < 12; i++)
14419 {
14420 *p++ = DW_CFA_offset + i;
14421 *p++ = (htab->opd_abi ? 13 : 12) - i;
14422 }
14423 *p++ = DW_CFA_advance_loc + 10;
14424 *p++ = DW_CFA_def_cfa_offset;
14425 *p++ = 0;
14426 for (i = 4; i < 12; i++)
14427 *p++ = DW_CFA_restore + i;
14428 *p++ = DW_CFA_advance_loc + 2;
14429 *p++ = DW_CFA_restore_extended;
14430 *p++ = 65;
14431 return p;
14432 }
14433
14434 /* Build all the stubs associated with the current output file.
14435 The stubs are kept in a hash table attached to the main linker
14436 hash table. This function is called via gldelf64ppc_finish. */
14437
14438 bool
14439 ppc64_elf_build_stubs (struct bfd_link_info *info,
14440 char **stats)
14441 {
14442 struct ppc_link_hash_table *htab = ppc_hash_table (info);
14443 struct map_stub *group;
14444 asection *stub_sec;
14445 bfd_byte *p;
14446 int stub_sec_count = 0;
14447
14448 if (htab == NULL)
14449 return false;
14450
14451 /* Allocate memory to hold the linker stubs. */
14452 for (group = htab->group; group != NULL; group = group->next)
14453 {
14454 group->eh_size = 0;
14455 group->lr_restore = 0;
14456 if ((stub_sec = group->stub_sec) != NULL
14457 && stub_sec->size != 0)
14458 {
14459 stub_sec->contents = bfd_zalloc (htab->params->stub_bfd,
14460 stub_sec->size);
14461 if (stub_sec->contents == NULL)
14462 return false;
14463 stub_sec->size = 0;
14464 }
14465 }
14466
14467 if (htab->glink != NULL && htab->glink->size != 0)
14468 {
14469 unsigned int indx;
14470 bfd_vma plt0;
14471
14472 /* Build the .glink plt call stub. */
14473 if (htab->params->emit_stub_syms)
14474 {
14475 struct elf_link_hash_entry *h;
14476 h = elf_link_hash_lookup (&htab->elf, "__glink_PLTresolve",
14477 true, false, false);
14478 if (h == NULL)
14479 return false;
14480 if (h->root.type == bfd_link_hash_new)
14481 {
14482 h->root.type = bfd_link_hash_defined;
14483 h->root.u.def.section = htab->glink;
14484 h->root.u.def.value = 8;
14485 h->ref_regular = 1;
14486 h->def_regular = 1;
14487 h->ref_regular_nonweak = 1;
14488 h->forced_local = 1;
14489 h->non_elf = 0;
14490 h->root.linker_def = 1;
14491 }
14492 }
14493 plt0 = (htab->elf.splt->output_section->vma
14494 + htab->elf.splt->output_offset
14495 - 16);
14496 if (info->emitrelocations)
14497 {
14498 Elf_Internal_Rela *r = get_relocs (htab->glink, 1);
14499 if (r == NULL)
14500 return false;
14501 r->r_offset = (htab->glink->output_offset
14502 + htab->glink->output_section->vma);
14503 r->r_info = ELF64_R_INFO (0, R_PPC64_REL64);
14504 r->r_addend = plt0;
14505 }
14506 p = htab->glink->contents;
14507 plt0 -= htab->glink->output_section->vma + htab->glink->output_offset;
14508 bfd_put_64 (htab->glink->owner, plt0, p);
14509 p += 8;
14510 if (htab->opd_abi)
14511 {
14512 bfd_put_32 (htab->glink->owner, MFLR_R12, p);
14513 p += 4;
14514 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14515 p += 4;
14516 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14517 p += 4;
14518 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | (-16 & 0xfffc), p);
14519 p += 4;
14520 bfd_put_32 (htab->glink->owner, MTLR_R12, p);
14521 p += 4;
14522 bfd_put_32 (htab->glink->owner, ADD_R11_R2_R11, p);
14523 p += 4;
14524 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14525 p += 4;
14526 bfd_put_32 (htab->glink->owner, LD_R2_0R11 | 8, p);
14527 p += 4;
14528 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14529 p += 4;
14530 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 16, p);
14531 p += 4;
14532 }
14533 else
14534 {
14535 unsigned int insn;
14536
14537 /* 0:
14538 . .quad plt0-1f # plt0 entry relative to 1:
14539 #
14540 # We get here with r12 initially @ a glink branch
14541 # Load the address of _dl_runtime_resolve from plt0 and
14542 # jump to it, with r0 set to the index of the PLT entry
14543 # to be resolved and r11 the link map.
14544 __glink_PLTresolve:
14545 . std %r2,24(%r1) # optional
14546 . mflr %r0
14547 . bcl 20,31,1f
14548 1:
14549 . mflr %r11
14550 . mtlr %r0
14551 . ld %r0,(0b-1b)(%r11)
14552 . sub %r12,%r12,%r11
14553 . add %r11,%r0,%r11
14554 . addi %r0,%r12,1b-2f
14555 . ld %r12,0(%r11)
14556 . srdi %r0,%r0,2
14557 . mtctr %r12
14558 . ld %r11,8(%r11)
14559 . bctr
14560 2:
14561 . b __glink_PLTresolve
14562 . ...
14563 . b __glink_PLTresolve */
14564
14565 if (htab->has_plt_localentry0)
14566 {
14567 bfd_put_32 (htab->glink->owner, STD_R2_0R1 + 24, p);
14568 p += 4;
14569 }
14570 bfd_put_32 (htab->glink->owner, MFLR_R0, p);
14571 p += 4;
14572 bfd_put_32 (htab->glink->owner, BCL_20_31, p);
14573 p += 4;
14574 bfd_put_32 (htab->glink->owner, MFLR_R11, p);
14575 p += 4;
14576 bfd_put_32 (htab->glink->owner, MTLR_R0, p);
14577 p += 4;
14578 if (htab->has_plt_localentry0)
14579 insn = LD_R0_0R11 | (-20 & 0xfffc);
14580 else
14581 insn = LD_R0_0R11 | (-16 & 0xfffc);
14582 bfd_put_32 (htab->glink->owner, insn, p);
14583 p += 4;
14584 bfd_put_32 (htab->glink->owner, SUB_R12_R12_R11, p);
14585 p += 4;
14586 bfd_put_32 (htab->glink->owner, ADD_R11_R0_R11, p);
14587 p += 4;
14588 bfd_put_32 (htab->glink->owner, ADDI_R0_R12 | (-44 & 0xffff), p);
14589 p += 4;
14590 bfd_put_32 (htab->glink->owner, LD_R12_0R11, p);
14591 p += 4;
14592 bfd_put_32 (htab->glink->owner, SRDI_R0_R0_2, p);
14593 p += 4;
14594 bfd_put_32 (htab->glink->owner, MTCTR_R12, p);
14595 p += 4;
14596 bfd_put_32 (htab->glink->owner, LD_R11_0R11 | 8, p);
14597 p += 4;
14598 }
14599 bfd_put_32 (htab->glink->owner, BCTR, p);
14600 p += 4;
14601 BFD_ASSERT (p == htab->glink->contents + GLINK_PLTRESOLVE_SIZE (htab));
14602
14603 /* Build the .glink lazy link call stubs. */
14604 indx = 0;
14605 while (p < htab->glink->contents + htab->glink->size)
14606 {
14607 if (htab->opd_abi)
14608 {
14609 if (indx < 0x8000)
14610 {
14611 bfd_put_32 (htab->glink->owner, LI_R0_0 | indx, p);
14612 p += 4;
14613 }
14614 else
14615 {
14616 bfd_put_32 (htab->glink->owner, LIS_R0_0 | PPC_HI (indx), p);
14617 p += 4;
14618 bfd_put_32 (htab->glink->owner, ORI_R0_R0_0 | PPC_LO (indx),
14619 p);
14620 p += 4;
14621 }
14622 }
14623 bfd_put_32 (htab->glink->owner,
14624 B_DOT | ((htab->glink->contents - p + 8) & 0x3fffffc), p);
14625 indx++;
14626 p += 4;
14627 }
14628 }
14629
14630 if (htab->tga_group != NULL)
14631 {
14632 htab->tga_group->lr_restore = 23 * 4;
14633 htab->tga_group->stub_sec->size = 24 * 4;
14634 if (!emit_tga_desc (htab))
14635 return false;
14636 if (htab->glink_eh_frame != NULL
14637 && htab->glink_eh_frame->size != 0)
14638 {
14639 size_t align = 4;
14640
14641 p = htab->glink_eh_frame->contents;
14642 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14643 p += 17;
14644 htab->tga_group->eh_size = emit_tga_desc_eh_frame (htab, p) - p;
14645 }
14646 }
14647
14648 /* Build .glink global entry stubs, and PLT relocs for globals. */
14649 elf_link_hash_traverse (&htab->elf, build_global_entry_stubs_and_plt, info);
14650
14651 if (!write_plt_relocs_for_local_syms (info))
14652 return false;
14653
14654 if (htab->brlt != NULL && htab->brlt->size != 0)
14655 {
14656 htab->brlt->contents = bfd_zalloc (htab->brlt->owner,
14657 htab->brlt->size);
14658 if (htab->brlt->contents == NULL)
14659 return false;
14660 }
14661 if (htab->relbrlt != NULL && htab->relbrlt->size != 0)
14662 {
14663 htab->relbrlt->contents = bfd_zalloc (htab->relbrlt->owner,
14664 htab->relbrlt->size);
14665 if (htab->relbrlt->contents == NULL)
14666 return false;
14667 }
14668
14669 /* Build the stubs as directed by the stub hash table. */
14670 bfd_hash_traverse (&htab->stub_hash_table, ppc_build_one_stub, info);
14671
14672 for (group = htab->group; group != NULL; group = group->next)
14673 if (group->needs_save_res)
14674 group->stub_sec->size += htab->sfpr->size;
14675
14676 if (htab->relbrlt != NULL)
14677 htab->relbrlt->reloc_count = 0;
14678
14679 if (htab->params->plt_stub_align != 0)
14680 for (group = htab->group; group != NULL; group = group->next)
14681 if ((stub_sec = group->stub_sec) != NULL)
14682 {
14683 int align = abs (htab->params->plt_stub_align);
14684 stub_sec->size = (stub_sec->size + (1 << align) - 1) & -(1 << align);
14685 }
14686
14687 for (group = htab->group; group != NULL; group = group->next)
14688 if (group->needs_save_res)
14689 {
14690 stub_sec = group->stub_sec;
14691 memcpy (stub_sec->contents + stub_sec->size - htab->sfpr->size,
14692 htab->sfpr->contents, htab->sfpr->size);
14693 if (htab->params->emit_stub_syms)
14694 {
14695 unsigned int i;
14696
14697 for (i = 0; i < ARRAY_SIZE (save_res_funcs); i++)
14698 if (!sfpr_define (info, &save_res_funcs[i], stub_sec))
14699 return false;
14700 }
14701 }
14702
14703 if (htab->glink_eh_frame != NULL
14704 && htab->glink_eh_frame->size != 0)
14705 {
14706 bfd_vma val;
14707 size_t align = 4;
14708
14709 p = htab->glink_eh_frame->contents;
14710 p += (sizeof (glink_eh_frame_cie) + align - 1) & -align;
14711
14712 for (group = htab->group; group != NULL; group = group->next)
14713 if (group->eh_size != 0)
14714 {
14715 /* Offset to stub section. */
14716 val = (group->stub_sec->output_section->vma
14717 + group->stub_sec->output_offset);
14718 val -= (htab->glink_eh_frame->output_section->vma
14719 + htab->glink_eh_frame->output_offset
14720 + (p + 8 - htab->glink_eh_frame->contents));
14721 if (val + 0x80000000 > 0xffffffff)
14722 {
14723 _bfd_error_handler
14724 (_("%s offset too large for .eh_frame sdata4 encoding"),
14725 group->stub_sec->name);
14726 return false;
14727 }
14728 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14729 p += (group->eh_size + 17 + 3) & -4;
14730 }
14731 if (htab->glink != NULL && htab->glink->size != 0)
14732 {
14733 /* Offset to .glink. */
14734 val = (htab->glink->output_section->vma
14735 + htab->glink->output_offset
14736 + 8);
14737 val -= (htab->glink_eh_frame->output_section->vma
14738 + htab->glink_eh_frame->output_offset
14739 + (p + 8 - htab->glink_eh_frame->contents));
14740 if (val + 0x80000000 > 0xffffffff)
14741 {
14742 _bfd_error_handler
14743 (_("%s offset too large for .eh_frame sdata4 encoding"),
14744 htab->glink->name);
14745 return false;
14746 }
14747 bfd_put_32 (htab->elf.dynobj, val, p + 8);
14748 p += (24 + align - 1) & -align;
14749 }
14750 }
14751
14752 for (group = htab->group; group != NULL; group = group->next)
14753 if ((stub_sec = group->stub_sec) != NULL)
14754 {
14755 stub_sec_count += 1;
14756 if (stub_sec->rawsize != stub_sec->size
14757 && (htab->stub_iteration <= STUB_SHRINK_ITER
14758 || stub_sec->rawsize < stub_sec->size))
14759 break;
14760 }
14761
14762 if (group != NULL)
14763 {
14764 htab->stub_error = true;
14765 _bfd_error_handler (_("stubs don't match calculated size"));
14766 }
14767
14768 if (htab->stub_error)
14769 return false;
14770
14771 if (stats != NULL)
14772 {
14773 char *groupmsg;
14774 if (asprintf (&groupmsg,
14775 ngettext ("linker stubs in %u group\n",
14776 "linker stubs in %u groups\n",
14777 stub_sec_count),
14778 stub_sec_count) < 0)
14779 *stats = NULL;
14780 else
14781 {
14782 if (asprintf (stats, _("%s"
14783 " branch %lu\n"
14784 " long branch %lu\n"
14785 " plt call %lu\n"
14786 " global entry %lu"),
14787 groupmsg,
14788 htab->stub_count[ppc_stub_long_branch - 1],
14789 htab->stub_count[ppc_stub_plt_branch - 1],
14790 htab->stub_count[ppc_stub_plt_call - 1],
14791 htab->stub_count[ppc_stub_global_entry - 1]) < 0)
14792 *stats = NULL;
14793 free (groupmsg);
14794 }
14795 }
14796 return true;
14797 }
14798
14799 /* What to do when ld finds relocations against symbols defined in
14800 discarded sections. */
14801
14802 static unsigned int
14803 ppc64_elf_action_discarded (asection *sec)
14804 {
14805 if (strcmp (".opd", sec->name) == 0)
14806 return 0;
14807
14808 if (strcmp (".toc", sec->name) == 0)
14809 return 0;
14810
14811 if (strcmp (".toc1", sec->name) == 0)
14812 return 0;
14813
14814 return _bfd_elf_default_action_discarded (sec);
14815 }
14816
14817 /* These are the dynamic relocations supported by glibc. */
14818
14819 static bool
14820 ppc64_glibc_dynamic_reloc (enum elf_ppc64_reloc_type r_type)
14821 {
14822 switch (r_type)
14823 {
14824 case R_PPC64_RELATIVE:
14825 case R_PPC64_NONE:
14826 case R_PPC64_ADDR64:
14827 case R_PPC64_GLOB_DAT:
14828 case R_PPC64_IRELATIVE:
14829 case R_PPC64_JMP_IREL:
14830 case R_PPC64_JMP_SLOT:
14831 case R_PPC64_DTPMOD64:
14832 case R_PPC64_DTPREL64:
14833 case R_PPC64_TPREL64:
14834 case R_PPC64_TPREL16_LO_DS:
14835 case R_PPC64_TPREL16_DS:
14836 case R_PPC64_TPREL16:
14837 case R_PPC64_TPREL16_LO:
14838 case R_PPC64_TPREL16_HI:
14839 case R_PPC64_TPREL16_HIGH:
14840 case R_PPC64_TPREL16_HA:
14841 case R_PPC64_TPREL16_HIGHA:
14842 case R_PPC64_TPREL16_HIGHER:
14843 case R_PPC64_TPREL16_HIGHEST:
14844 case R_PPC64_TPREL16_HIGHERA:
14845 case R_PPC64_TPREL16_HIGHESTA:
14846 case R_PPC64_ADDR16_LO_DS:
14847 case R_PPC64_ADDR16_LO:
14848 case R_PPC64_ADDR16_HI:
14849 case R_PPC64_ADDR16_HIGH:
14850 case R_PPC64_ADDR16_HA:
14851 case R_PPC64_ADDR16_HIGHA:
14852 case R_PPC64_REL30:
14853 case R_PPC64_COPY:
14854 case R_PPC64_UADDR64:
14855 case R_PPC64_UADDR32:
14856 case R_PPC64_ADDR32:
14857 case R_PPC64_ADDR24:
14858 case R_PPC64_ADDR16:
14859 case R_PPC64_UADDR16:
14860 case R_PPC64_ADDR16_DS:
14861 case R_PPC64_ADDR16_HIGHER:
14862 case R_PPC64_ADDR16_HIGHEST:
14863 case R_PPC64_ADDR16_HIGHERA:
14864 case R_PPC64_ADDR16_HIGHESTA:
14865 case R_PPC64_ADDR14:
14866 case R_PPC64_ADDR14_BRTAKEN:
14867 case R_PPC64_ADDR14_BRNTAKEN:
14868 case R_PPC64_REL32:
14869 case R_PPC64_REL64:
14870 return true;
14871
14872 default:
14873 return false;
14874 }
14875 }
14876
14877 /* The RELOCATE_SECTION function is called by the ELF backend linker
14878 to handle the relocations for a section.
14879
14880 The relocs are always passed as Rela structures; if the section
14881 actually uses Rel structures, the r_addend field will always be
14882 zero.
14883
14884 This function is responsible for adjust the section contents as
14885 necessary, and (if using Rela relocs and generating a
14886 relocatable output file) adjusting the reloc addend as
14887 necessary.
14888
14889 This function does not have to worry about setting the reloc
14890 address or the reloc symbol index.
14891
14892 LOCAL_SYMS is a pointer to the swapped in local symbols.
14893
14894 LOCAL_SECTIONS is an array giving the section in the input file
14895 corresponding to the st_shndx field of each local symbol.
14896
14897 The global hash table entry for the global symbols can be found
14898 via elf_sym_hashes (input_bfd).
14899
14900 When generating relocatable output, this function must handle
14901 STB_LOCAL/STT_SECTION symbols specially. The output symbol is
14902 going to be the section symbol corresponding to the output
14903 section, which means that the addend must be adjusted
14904 accordingly. */
14905
14906 static int
14907 ppc64_elf_relocate_section (bfd *output_bfd,
14908 struct bfd_link_info *info,
14909 bfd *input_bfd,
14910 asection *input_section,
14911 bfd_byte *contents,
14912 Elf_Internal_Rela *relocs,
14913 Elf_Internal_Sym *local_syms,
14914 asection **local_sections)
14915 {
14916 struct ppc_link_hash_table *htab;
14917 Elf_Internal_Shdr *symtab_hdr;
14918 struct elf_link_hash_entry **sym_hashes;
14919 Elf_Internal_Rela *rel;
14920 Elf_Internal_Rela *wrel;
14921 Elf_Internal_Rela *relend;
14922 Elf_Internal_Rela outrel;
14923 bfd_byte *loc;
14924 struct got_entry **local_got_ents;
14925 bfd_vma TOCstart;
14926 bool ret = true;
14927 bool is_opd;
14928 /* Assume 'at' branch hints. */
14929 bool is_isa_v2 = true;
14930 bool warned_dynamic = false;
14931 bfd_vma d_offset = (bfd_big_endian (input_bfd) ? 2 : 0);
14932
14933 /* Initialize howto table if needed. */
14934 if (!ppc64_elf_howto_table[R_PPC64_ADDR32])
14935 ppc_howto_init ();
14936
14937 htab = ppc_hash_table (info);
14938 if (htab == NULL)
14939 return false;
14940
14941 /* Don't relocate stub sections. */
14942 if (input_section->owner == htab->params->stub_bfd)
14943 return true;
14944
14945 if (!is_ppc64_elf (input_bfd))
14946 {
14947 bfd_set_error (bfd_error_wrong_format);
14948 return false;
14949 }
14950
14951 local_got_ents = elf_local_got_ents (input_bfd);
14952 TOCstart = elf_gp (output_bfd);
14953 symtab_hdr = &elf_symtab_hdr (input_bfd);
14954 sym_hashes = elf_sym_hashes (input_bfd);
14955 is_opd = ppc64_elf_section_data (input_section)->sec_type == sec_opd;
14956
14957 rel = wrel = relocs;
14958 relend = relocs + input_section->reloc_count;
14959 for (; rel < relend; wrel++, rel++)
14960 {
14961 enum elf_ppc64_reloc_type r_type;
14962 bfd_vma addend;
14963 bfd_reloc_status_type r;
14964 Elf_Internal_Sym *sym;
14965 asection *sec;
14966 struct elf_link_hash_entry *h_elf;
14967 struct ppc_link_hash_entry *h;
14968 struct ppc_link_hash_entry *fdh;
14969 const char *sym_name;
14970 unsigned long r_symndx, toc_symndx;
14971 bfd_vma toc_addend;
14972 unsigned char tls_mask, tls_gd, tls_type;
14973 unsigned char sym_type;
14974 bfd_vma relocation;
14975 bool unresolved_reloc, save_unresolved_reloc;
14976 bool warned;
14977 enum { DEST_NORMAL, DEST_OPD, DEST_STUB } reloc_dest;
14978 unsigned int insn;
14979 unsigned int mask;
14980 struct ppc_stub_hash_entry *stub_entry;
14981 bfd_vma max_br_offset;
14982 bfd_vma from;
14983 Elf_Internal_Rela orig_rel;
14984 reloc_howto_type *howto;
14985 struct reloc_howto_struct alt_howto;
14986 uint64_t pinsn;
14987 bfd_vma offset;
14988
14989 again:
14990 orig_rel = *rel;
14991
14992 r_type = ELF64_R_TYPE (rel->r_info);
14993 r_symndx = ELF64_R_SYM (rel->r_info);
14994
14995 /* For old style R_PPC64_TOC relocs with a zero symbol, use the
14996 symbol of the previous ADDR64 reloc. The symbol gives us the
14997 proper TOC base to use. */
14998 if (rel->r_info == ELF64_R_INFO (0, R_PPC64_TOC)
14999 && wrel != relocs
15000 && ELF64_R_TYPE (wrel[-1].r_info) == R_PPC64_ADDR64
15001 && is_opd)
15002 r_symndx = ELF64_R_SYM (wrel[-1].r_info);
15003
15004 sym = NULL;
15005 sec = NULL;
15006 h_elf = NULL;
15007 sym_name = NULL;
15008 unresolved_reloc = false;
15009 warned = false;
15010
15011 if (r_symndx < symtab_hdr->sh_info)
15012 {
15013 /* It's a local symbol. */
15014 struct _opd_sec_data *opd;
15015
15016 sym = local_syms + r_symndx;
15017 sec = local_sections[r_symndx];
15018 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym, sec);
15019 sym_type = ELF64_ST_TYPE (sym->st_info);
15020 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
15021 opd = get_opd_info (sec);
15022 if (opd != NULL && opd->adjust != NULL)
15023 {
15024 long adjust = opd->adjust[OPD_NDX (sym->st_value
15025 + rel->r_addend)];
15026 if (adjust == -1)
15027 relocation = 0;
15028 else
15029 {
15030 /* If this is a relocation against the opd section sym
15031 and we have edited .opd, adjust the reloc addend so
15032 that ld -r and ld --emit-relocs output is correct.
15033 If it is a reloc against some other .opd symbol,
15034 then the symbol value will be adjusted later. */
15035 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
15036 rel->r_addend += adjust;
15037 else
15038 relocation += adjust;
15039 }
15040 }
15041 }
15042 else
15043 {
15044 bool ignored;
15045
15046 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
15047 r_symndx, symtab_hdr, sym_hashes,
15048 h_elf, sec, relocation,
15049 unresolved_reloc, warned, ignored);
15050 sym_name = h_elf->root.root.string;
15051 sym_type = h_elf->type;
15052 if (sec != NULL
15053 && sec->owner == output_bfd
15054 && strcmp (sec->name, ".opd") == 0)
15055 {
15056 /* This is a symbol defined in a linker script. All
15057 such are defined in output sections, even those
15058 defined by simple assignment from a symbol defined in
15059 an input section. Transfer the symbol to an
15060 appropriate input .opd section, so that a branch to
15061 this symbol will be mapped to the location specified
15062 by the opd entry. */
15063 struct bfd_link_order *lo;
15064 for (lo = sec->map_head.link_order; lo != NULL; lo = lo->next)
15065 if (lo->type == bfd_indirect_link_order)
15066 {
15067 asection *isec = lo->u.indirect.section;
15068 if (h_elf->root.u.def.value >= isec->output_offset
15069 && h_elf->root.u.def.value < (isec->output_offset
15070 + isec->size))
15071 {
15072 h_elf->root.u.def.value -= isec->output_offset;
15073 h_elf->root.u.def.section = isec;
15074 sec = isec;
15075 break;
15076 }
15077 }
15078 }
15079 }
15080 h = ppc_elf_hash_entry (h_elf);
15081
15082 if (sec != NULL && discarded_section (sec))
15083 {
15084 _bfd_clear_contents (ppc64_elf_howto_table[r_type],
15085 input_bfd, input_section,
15086 contents, rel->r_offset);
15087 wrel->r_offset = rel->r_offset;
15088 wrel->r_info = 0;
15089 wrel->r_addend = 0;
15090
15091 /* For ld -r, remove relocations in debug sections against
15092 symbols defined in discarded sections. Not done for
15093 non-debug to preserve relocs in .eh_frame which the
15094 eh_frame editing code expects to be present. */
15095 if (bfd_link_relocatable (info)
15096 && (input_section->flags & SEC_DEBUGGING))
15097 wrel--;
15098
15099 continue;
15100 }
15101
15102 if (bfd_link_relocatable (info))
15103 goto copy_reloc;
15104
15105 if (h != NULL && &h->elf == htab->elf.hgot)
15106 {
15107 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15108 sec = bfd_abs_section_ptr;
15109 unresolved_reloc = false;
15110 }
15111
15112 /* TLS optimizations. Replace instruction sequences and relocs
15113 based on information we collected in tls_optimize. We edit
15114 RELOCS so that --emit-relocs will output something sensible
15115 for the final instruction stream. */
15116 tls_mask = 0;
15117 tls_gd = 0;
15118 toc_symndx = 0;
15119 if (h != NULL)
15120 tls_mask = h->tls_mask;
15121 else if (local_got_ents != NULL)
15122 {
15123 struct plt_entry **local_plt = (struct plt_entry **)
15124 (local_got_ents + symtab_hdr->sh_info);
15125 unsigned char *lgot_masks = (unsigned char *)
15126 (local_plt + symtab_hdr->sh_info);
15127 tls_mask = lgot_masks[r_symndx];
15128 }
15129 if (((tls_mask & TLS_TLS) == 0 || tls_mask == (TLS_TLS | TLS_MARK))
15130 && (r_type == R_PPC64_TLS
15131 || r_type == R_PPC64_TLSGD
15132 || r_type == R_PPC64_TLSLD))
15133 {
15134 /* Check for toc tls entries. */
15135 unsigned char *toc_tls;
15136
15137 if (!get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15138 &local_syms, rel, input_bfd))
15139 return false;
15140
15141 if (toc_tls)
15142 tls_mask = *toc_tls;
15143 }
15144
15145 /* Check that tls relocs are used with tls syms, and non-tls
15146 relocs are used with non-tls syms. */
15147 if (r_symndx != STN_UNDEF
15148 && r_type != R_PPC64_NONE
15149 && (h == NULL
15150 || h->elf.root.type == bfd_link_hash_defined
15151 || h->elf.root.type == bfd_link_hash_defweak)
15152 && IS_PPC64_TLS_RELOC (r_type) != (sym_type == STT_TLS))
15153 {
15154 if ((tls_mask & TLS_TLS) != 0
15155 && (r_type == R_PPC64_TLS
15156 || r_type == R_PPC64_TLSGD
15157 || r_type == R_PPC64_TLSLD))
15158 /* R_PPC64_TLS is OK against a symbol in the TOC. */
15159 ;
15160 else
15161 info->callbacks->einfo
15162 (!IS_PPC64_TLS_RELOC (r_type)
15163 /* xgettext:c-format */
15164 ? _("%H: %s used with TLS symbol `%pT'\n")
15165 /* xgettext:c-format */
15166 : _("%H: %s used with non-TLS symbol `%pT'\n"),
15167 input_bfd, input_section, rel->r_offset,
15168 ppc64_elf_howto_table[r_type]->name,
15169 sym_name);
15170 }
15171
15172 /* Ensure reloc mapping code below stays sane. */
15173 if (R_PPC64_TOC16_LO_DS != R_PPC64_TOC16_DS + 1
15174 || R_PPC64_TOC16_LO != R_PPC64_TOC16 + 1
15175 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TLSGD16 & 3)
15176 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TLSGD16_LO & 3)
15177 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TLSGD16_HI & 3)
15178 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TLSGD16_HA & 3)
15179 || (R_PPC64_GOT_TLSLD16 & 3) != (R_PPC64_GOT_TPREL16_DS & 3)
15180 || (R_PPC64_GOT_TLSLD16_LO & 3) != (R_PPC64_GOT_TPREL16_LO_DS & 3)
15181 || (R_PPC64_GOT_TLSLD16_HI & 3) != (R_PPC64_GOT_TPREL16_HI & 3)
15182 || (R_PPC64_GOT_TLSLD16_HA & 3) != (R_PPC64_GOT_TPREL16_HA & 3))
15183 abort ();
15184
15185 switch (r_type)
15186 {
15187 default:
15188 break;
15189
15190 case R_PPC64_LO_DS_OPT:
15191 if (offset_in_range (input_section, rel->r_offset - d_offset, 4))
15192 {
15193 insn = bfd_get_32 (input_bfd,
15194 contents + rel->r_offset - d_offset);
15195 if ((insn & (0x3fu << 26)) != 58u << 26)
15196 abort ();
15197 insn += (14u << 26) - (58u << 26);
15198 bfd_put_32 (input_bfd, insn,
15199 contents + rel->r_offset - d_offset);
15200 r_type = R_PPC64_TOC16_LO;
15201 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15202 }
15203 break;
15204
15205 case R_PPC64_TOC16:
15206 case R_PPC64_TOC16_LO:
15207 case R_PPC64_TOC16_DS:
15208 case R_PPC64_TOC16_LO_DS:
15209 {
15210 /* Check for toc tls entries. */
15211 unsigned char *toc_tls;
15212 int retval;
15213
15214 retval = get_tls_mask (&toc_tls, &toc_symndx, &toc_addend,
15215 &local_syms, rel, input_bfd);
15216 if (retval == 0)
15217 return false;
15218
15219 if (toc_tls)
15220 {
15221 tls_mask = *toc_tls;
15222 if (r_type == R_PPC64_TOC16_DS
15223 || r_type == R_PPC64_TOC16_LO_DS)
15224 {
15225 if ((tls_mask & TLS_TLS) != 0
15226 && (tls_mask & (TLS_DTPREL | TLS_TPREL)) == 0)
15227 goto toctprel;
15228 }
15229 else
15230 {
15231 /* If we found a GD reloc pair, then we might be
15232 doing a GD->IE transition. */
15233 if (retval == 2)
15234 {
15235 tls_gd = TLS_GDIE;
15236 if ((tls_mask & TLS_TLS) != 0
15237 && (tls_mask & TLS_GD) == 0)
15238 goto tls_ldgd_opt;
15239 }
15240 else if (retval == 3)
15241 {
15242 if ((tls_mask & TLS_TLS) != 0
15243 && (tls_mask & TLS_LD) == 0)
15244 goto tls_ldgd_opt;
15245 }
15246 }
15247 }
15248 }
15249 break;
15250
15251 case R_PPC64_GOT_TPREL16_HI:
15252 case R_PPC64_GOT_TPREL16_HA:
15253 if ((tls_mask & TLS_TLS) != 0
15254 && (tls_mask & TLS_TPREL) == 0
15255 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15256 {
15257 rel->r_offset -= d_offset;
15258 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15259 r_type = R_PPC64_NONE;
15260 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15261 }
15262 break;
15263
15264 case R_PPC64_GOT_TPREL16_DS:
15265 case R_PPC64_GOT_TPREL16_LO_DS:
15266 if ((tls_mask & TLS_TLS) != 0
15267 && (tls_mask & TLS_TPREL) == 0
15268 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
15269 {
15270 toctprel:
15271 insn = bfd_get_32 (input_bfd,
15272 contents + rel->r_offset - d_offset);
15273 insn &= 31 << 21;
15274 insn |= 0x3c0d0000; /* addis 0,13,0 */
15275 bfd_put_32 (input_bfd, insn,
15276 contents + rel->r_offset - d_offset);
15277 r_type = R_PPC64_TPREL16_HA;
15278 if (toc_symndx != 0)
15279 {
15280 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15281 rel->r_addend = toc_addend;
15282 /* We changed the symbol. Start over in order to
15283 get h, sym, sec etc. right. */
15284 goto again;
15285 }
15286 else
15287 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15288 }
15289 break;
15290
15291 case R_PPC64_GOT_TPREL_PCREL34:
15292 if ((tls_mask & TLS_TLS) != 0
15293 && (tls_mask & TLS_TPREL) == 0
15294 && offset_in_range (input_section, rel->r_offset, 8))
15295 {
15296 /* pld ra,sym@got@tprel@pcrel -> paddi ra,r13,sym@tprel */
15297 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15298 pinsn <<= 32;
15299 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15300 pinsn += ((2ULL << 56) + (-1ULL << 52)
15301 + (14ULL << 26) - (57ULL << 26) + (13ULL << 16));
15302 bfd_put_32 (input_bfd, pinsn >> 32,
15303 contents + rel->r_offset);
15304 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15305 contents + rel->r_offset + 4);
15306 r_type = R_PPC64_TPREL34;
15307 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15308 }
15309 break;
15310
15311 case R_PPC64_TLS:
15312 if ((tls_mask & TLS_TLS) != 0
15313 && (tls_mask & TLS_TPREL) == 0
15314 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15315 {
15316 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
15317 insn = _bfd_elf_ppc_at_tls_transform (insn, 13);
15318 if (insn == 0)
15319 break;
15320 if ((rel->r_offset & 3) == 0)
15321 {
15322 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
15323 /* Was PPC64_TLS which sits on insn boundary, now
15324 PPC64_TPREL16_LO which is at low-order half-word. */
15325 rel->r_offset += d_offset;
15326 r_type = R_PPC64_TPREL16_LO;
15327 if (toc_symndx != 0)
15328 {
15329 rel->r_info = ELF64_R_INFO (toc_symndx, r_type);
15330 rel->r_addend = toc_addend;
15331 /* We changed the symbol. Start over in order to
15332 get h, sym, sec etc. right. */
15333 goto again;
15334 }
15335 else
15336 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15337 }
15338 else if ((rel->r_offset & 3) == 1)
15339 {
15340 /* For pcrel IE to LE we already have the full
15341 offset and thus don't need an addi here. A nop
15342 or mr will do. */
15343 if ((insn & (0x3fu << 26)) == 14 << 26)
15344 {
15345 /* Extract regs from addi rt,ra,si. */
15346 unsigned int rt = (insn >> 21) & 0x1f;
15347 unsigned int ra = (insn >> 16) & 0x1f;
15348 if (rt == ra)
15349 insn = NOP;
15350 else
15351 {
15352 /* Build or ra,rs,rb with rb==rs, ie. mr ra,rs. */
15353 insn = (rt << 16) | (ra << 21) | (ra << 11);
15354 insn |= (31u << 26) | (444u << 1);
15355 }
15356 }
15357 bfd_put_32 (input_bfd, insn, contents + rel->r_offset - 1);
15358 }
15359 }
15360 break;
15361
15362 case R_PPC64_GOT_TLSGD16_HI:
15363 case R_PPC64_GOT_TLSGD16_HA:
15364 tls_gd = TLS_GDIE;
15365 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15366 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15367 goto tls_gdld_hi;
15368 break;
15369
15370 case R_PPC64_GOT_TLSLD16_HI:
15371 case R_PPC64_GOT_TLSLD16_HA:
15372 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15373 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15374 {
15375 tls_gdld_hi:
15376 if ((tls_mask & tls_gd) != 0)
15377 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 3)) & 3)
15378 + R_PPC64_GOT_TPREL16_DS);
15379 else
15380 {
15381 rel->r_offset -= d_offset;
15382 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
15383 r_type = R_PPC64_NONE;
15384 }
15385 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15386 }
15387 break;
15388
15389 case R_PPC64_GOT_TLSGD16:
15390 case R_PPC64_GOT_TLSGD16_LO:
15391 tls_gd = TLS_GDIE;
15392 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15393 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15394 goto tls_ldgd_opt;
15395 break;
15396
15397 case R_PPC64_GOT_TLSLD16:
15398 case R_PPC64_GOT_TLSLD16_LO:
15399 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15400 && offset_in_range (input_section, rel->r_offset & ~3, 4))
15401 {
15402 unsigned int insn1, insn2;
15403
15404 tls_ldgd_opt:
15405 offset = (bfd_vma) -1;
15406 /* If not using the newer R_PPC64_TLSGD/LD to mark
15407 __tls_get_addr calls, we must trust that the call
15408 stays with its arg setup insns, ie. that the next
15409 reloc is the __tls_get_addr call associated with
15410 the current reloc. Edit both insns. */
15411 if (input_section->nomark_tls_get_addr
15412 && rel + 1 < relend
15413 && branch_reloc_hash_match (input_bfd, rel + 1,
15414 htab->tls_get_addr_fd,
15415 htab->tga_desc_fd,
15416 htab->tls_get_addr,
15417 htab->tga_desc))
15418 offset = rel[1].r_offset;
15419 /* We read the low GOT_TLS (or TOC16) insn because we
15420 need to keep the destination reg. It may be
15421 something other than the usual r3, and moved to r3
15422 before the call by intervening code. */
15423 insn1 = bfd_get_32 (input_bfd,
15424 contents + rel->r_offset - d_offset);
15425 if ((tls_mask & tls_gd) != 0)
15426 {
15427 /* IE */
15428 insn1 &= (0x1f << 21) | (0x1f << 16);
15429 insn1 |= 58u << 26; /* ld */
15430 insn2 = 0x7c636a14; /* add 3,3,13 */
15431 if (offset != (bfd_vma) -1)
15432 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15433 if (r_type == R_PPC64_TOC16
15434 || r_type == R_PPC64_TOC16_LO)
15435 r_type += R_PPC64_TOC16_DS - R_PPC64_TOC16;
15436 else
15437 r_type = (((r_type - (R_PPC64_GOT_TLSGD16 & 1)) & 1)
15438 + R_PPC64_GOT_TPREL16_DS);
15439 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15440 }
15441 else
15442 {
15443 /* LE */
15444 insn1 &= 0x1f << 21;
15445 insn1 |= 0x3c0d0000; /* addis r,13,0 */
15446 insn2 = 0x38630000; /* addi 3,3,0 */
15447 if (tls_gd == 0)
15448 {
15449 /* Was an LD reloc. */
15450 r_symndx = STN_UNDEF;
15451 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15452 }
15453 else if (toc_symndx != 0)
15454 {
15455 r_symndx = toc_symndx;
15456 rel->r_addend = toc_addend;
15457 }
15458 r_type = R_PPC64_TPREL16_HA;
15459 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15460 if (offset != (bfd_vma) -1)
15461 {
15462 rel[1].r_info = ELF64_R_INFO (r_symndx,
15463 R_PPC64_TPREL16_LO);
15464 rel[1].r_offset = offset + d_offset;
15465 rel[1].r_addend = rel->r_addend;
15466 }
15467 }
15468 bfd_put_32 (input_bfd, insn1,
15469 contents + rel->r_offset - d_offset);
15470 if (offset != (bfd_vma) -1
15471 && offset_in_range (input_section, offset, 4))
15472 {
15473 bfd_put_32 (input_bfd, insn2, contents + offset);
15474 if (offset_in_range (input_section, offset + 4, 4))
15475 {
15476 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15477 if (insn2 == LD_R2_0R1 + STK_TOC (htab))
15478 bfd_put_32 (input_bfd, NOP, contents + offset + 4);
15479 }
15480 }
15481 if ((tls_mask & tls_gd) == 0
15482 && (tls_gd == 0 || toc_symndx != 0))
15483 {
15484 /* We changed the symbol. Start over in order
15485 to get h, sym, sec etc. right. */
15486 goto again;
15487 }
15488 }
15489 break;
15490
15491 case R_PPC64_GOT_TLSGD_PCREL34:
15492 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15493 && offset_in_range (input_section, rel->r_offset, 8))
15494 {
15495 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15496 pinsn <<= 32;
15497 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15498 if ((tls_mask & TLS_GDIE) != 0)
15499 {
15500 /* IE, pla -> pld */
15501 pinsn += (-2ULL << 56) + (57ULL << 26) - (14ULL << 26);
15502 r_type = R_PPC64_GOT_TPREL_PCREL34;
15503 }
15504 else
15505 {
15506 /* LE, pla pcrel -> paddi r13 */
15507 pinsn += (-1ULL << 52) + (13ULL << 16);
15508 r_type = R_PPC64_TPREL34;
15509 }
15510 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15511 bfd_put_32 (input_bfd, pinsn >> 32,
15512 contents + rel->r_offset);
15513 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15514 contents + rel->r_offset + 4);
15515 }
15516 break;
15517
15518 case R_PPC64_GOT_TLSLD_PCREL34:
15519 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15520 && offset_in_range (input_section, rel->r_offset, 8))
15521 {
15522 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15523 pinsn <<= 32;
15524 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15525 pinsn += (-1ULL << 52) + (13ULL << 16);
15526 bfd_put_32 (input_bfd, pinsn >> 32,
15527 contents + rel->r_offset);
15528 bfd_put_32 (input_bfd, pinsn & 0xffffffff,
15529 contents + rel->r_offset + 4);
15530 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15531 r_symndx = STN_UNDEF;
15532 r_type = R_PPC64_TPREL34;
15533 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15534 goto again;
15535 }
15536 break;
15537
15538 case R_PPC64_TLSGD:
15539 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_GD) == 0
15540 && rel + 1 < relend
15541 && offset_in_range (input_section, rel->r_offset,
15542 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15543 ? 8 : 4))
15544 {
15545 unsigned int insn2;
15546 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15547
15548 offset = rel->r_offset;
15549 if (is_plt_seq_reloc (r_type1))
15550 {
15551 bfd_put_32 (output_bfd, NOP, contents + offset);
15552 if (r_type1 == R_PPC64_PLT_PCREL34
15553 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15554 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15555 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15556 break;
15557 }
15558
15559 if (r_type1 == R_PPC64_PLTCALL)
15560 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15561
15562 if ((tls_mask & TLS_GDIE) != 0)
15563 {
15564 /* IE */
15565 r_type = R_PPC64_NONE;
15566 insn2 = 0x7c636a14; /* add 3,3,13 */
15567 }
15568 else
15569 {
15570 /* LE */
15571 if (toc_symndx != 0)
15572 {
15573 r_symndx = toc_symndx;
15574 rel->r_addend = toc_addend;
15575 }
15576 if (r_type1 == R_PPC64_REL24_NOTOC
15577 || r_type1 == R_PPC64_REL24_P9NOTOC
15578 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15579 {
15580 r_type = R_PPC64_NONE;
15581 insn2 = NOP;
15582 }
15583 else
15584 {
15585 rel->r_offset = offset + d_offset;
15586 r_type = R_PPC64_TPREL16_LO;
15587 insn2 = 0x38630000; /* addi 3,3,0 */
15588 }
15589 }
15590 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15591 /* Zap the reloc on the _tls_get_addr call too. */
15592 BFD_ASSERT (offset == rel[1].r_offset);
15593 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15594 bfd_put_32 (input_bfd, insn2, contents + offset);
15595 if ((tls_mask & TLS_GDIE) == 0
15596 && toc_symndx != 0
15597 && r_type != R_PPC64_NONE)
15598 goto again;
15599 }
15600 break;
15601
15602 case R_PPC64_TLSLD:
15603 if ((tls_mask & TLS_TLS) != 0 && (tls_mask & TLS_LD) == 0
15604 && rel + 1 < relend
15605 && offset_in_range (input_section, rel->r_offset,
15606 is_8byte_reloc (ELF64_R_TYPE (rel[1].r_info))
15607 ? 8 : 4))
15608 {
15609 unsigned int insn2;
15610 enum elf_ppc64_reloc_type r_type1 = ELF64_R_TYPE (rel[1].r_info);
15611
15612 offset = rel->r_offset;
15613 if (is_plt_seq_reloc (r_type1))
15614 {
15615 bfd_put_32 (output_bfd, NOP, contents + offset);
15616 if (r_type1 == R_PPC64_PLT_PCREL34
15617 || r_type1 == R_PPC64_PLT_PCREL34_NOTOC)
15618 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15619 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15620 break;
15621 }
15622
15623 if (r_type1 == R_PPC64_PLTCALL)
15624 bfd_put_32 (output_bfd, NOP, contents + offset + 4);
15625
15626 if (r_type1 == R_PPC64_REL24_NOTOC
15627 || r_type1 == R_PPC64_REL24_P9NOTOC
15628 || r_type1 == R_PPC64_PLTCALL_NOTOC)
15629 {
15630 r_type = R_PPC64_NONE;
15631 insn2 = NOP;
15632 }
15633 else
15634 {
15635 rel->r_offset = offset + d_offset;
15636 r_symndx = STN_UNDEF;
15637 r_type = R_PPC64_TPREL16_LO;
15638 rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
15639 insn2 = 0x38630000; /* addi 3,3,0 */
15640 }
15641 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15642 /* Zap the reloc on the _tls_get_addr call too. */
15643 BFD_ASSERT (offset == rel[1].r_offset);
15644 rel[1].r_info = ELF64_R_INFO (STN_UNDEF, R_PPC64_NONE);
15645 bfd_put_32 (input_bfd, insn2, contents + offset);
15646 if (r_type != R_PPC64_NONE)
15647 goto again;
15648 }
15649 break;
15650
15651 case R_PPC64_DTPMOD64:
15652 if (rel + 1 < relend
15653 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_DTPREL64)
15654 && rel[1].r_offset == rel->r_offset + 8)
15655 {
15656 if ((tls_mask & TLS_GD) == 0
15657 && offset_in_range (input_section, rel->r_offset, 8))
15658 {
15659 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_NONE);
15660 if ((tls_mask & TLS_GDIE) != 0)
15661 r_type = R_PPC64_TPREL64;
15662 else
15663 {
15664 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15665 r_type = R_PPC64_NONE;
15666 }
15667 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15668 }
15669 }
15670 else
15671 {
15672 if ((tls_mask & TLS_LD) == 0
15673 && offset_in_range (input_section, rel->r_offset, 8))
15674 {
15675 bfd_put_64 (output_bfd, 1, contents + rel->r_offset);
15676 r_type = R_PPC64_NONE;
15677 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15678 }
15679 }
15680 break;
15681
15682 case R_PPC64_TPREL64:
15683 if ((tls_mask & TLS_TPREL) == 0)
15684 {
15685 r_type = R_PPC64_NONE;
15686 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15687 }
15688 break;
15689
15690 case R_PPC64_ENTRY:
15691 relocation = TOCstart + htab->sec_info[input_section->id].toc_off;
15692 if (!bfd_link_pic (info)
15693 && !info->traditional_format
15694 && relocation + 0x80008000 <= 0xffffffff
15695 && offset_in_range (input_section, rel->r_offset, 8))
15696 {
15697 unsigned int insn1, insn2;
15698
15699 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15700 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15701 if ((insn1 & ~0xfffc) == LD_R2_0R12
15702 && insn2 == ADD_R2_R2_R12)
15703 {
15704 bfd_put_32 (input_bfd,
15705 LIS_R2 + PPC_HA (relocation),
15706 contents + rel->r_offset);
15707 bfd_put_32 (input_bfd,
15708 ADDI_R2_R2 + PPC_LO (relocation),
15709 contents + rel->r_offset + 4);
15710 }
15711 }
15712 else
15713 {
15714 relocation -= (rel->r_offset
15715 + input_section->output_offset
15716 + input_section->output_section->vma);
15717 if (relocation + 0x80008000 <= 0xffffffff
15718 && offset_in_range (input_section, rel->r_offset, 8))
15719 {
15720 unsigned int insn1, insn2;
15721
15722 insn1 = bfd_get_32 (input_bfd, contents + rel->r_offset);
15723 insn2 = bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
15724 if ((insn1 & ~0xfffc) == LD_R2_0R12
15725 && insn2 == ADD_R2_R2_R12)
15726 {
15727 bfd_put_32 (input_bfd,
15728 ADDIS_R2_R12 + PPC_HA (relocation),
15729 contents + rel->r_offset);
15730 bfd_put_32 (input_bfd,
15731 ADDI_R2_R2 + PPC_LO (relocation),
15732 contents + rel->r_offset + 4);
15733 }
15734 }
15735 }
15736 break;
15737
15738 case R_PPC64_REL16_HA:
15739 /* If we are generating a non-PIC executable, edit
15740 . 0: addis 2,12,.TOC.-0b@ha
15741 . addi 2,2,.TOC.-0b@l
15742 used by ELFv2 global entry points to set up r2, to
15743 . lis 2,.TOC.@ha
15744 . addi 2,2,.TOC.@l
15745 if .TOC. is in range. */
15746 if (!bfd_link_pic (info)
15747 && !info->traditional_format
15748 && !htab->opd_abi
15749 && rel->r_addend == d_offset
15750 && h != NULL && &h->elf == htab->elf.hgot
15751 && rel + 1 < relend
15752 && rel[1].r_info == ELF64_R_INFO (r_symndx, R_PPC64_REL16_LO)
15753 && rel[1].r_offset == rel->r_offset + 4
15754 && rel[1].r_addend == rel->r_addend + 4
15755 && relocation + 0x80008000 <= 0xffffffff
15756 && offset_in_range (input_section, rel->r_offset - d_offset, 8))
15757 {
15758 unsigned int insn1, insn2;
15759 offset = rel->r_offset - d_offset;
15760 insn1 = bfd_get_32 (input_bfd, contents + offset);
15761 insn2 = bfd_get_32 (input_bfd, contents + offset + 4);
15762 if ((insn1 & 0xffff0000) == ADDIS_R2_R12
15763 && (insn2 & 0xffff0000) == ADDI_R2_R2)
15764 {
15765 r_type = R_PPC64_ADDR16_HA;
15766 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
15767 rel->r_addend -= d_offset;
15768 rel[1].r_info = ELF64_R_INFO (r_symndx, R_PPC64_ADDR16_LO);
15769 rel[1].r_addend -= d_offset + 4;
15770 bfd_put_32 (input_bfd, LIS_R2, contents + offset);
15771 }
15772 }
15773 break;
15774 }
15775
15776 /* Handle other relocations that tweak non-addend part of insn. */
15777 insn = 0;
15778 max_br_offset = 1 << 25;
15779 addend = rel->r_addend;
15780 reloc_dest = DEST_NORMAL;
15781 switch (r_type)
15782 {
15783 default:
15784 break;
15785
15786 case R_PPC64_TOCSAVE:
15787 if (relocation + addend == (rel->r_offset
15788 + input_section->output_offset
15789 + input_section->output_section->vma)
15790 && tocsave_find (htab, NO_INSERT,
15791 &local_syms, rel, input_bfd)
15792 && offset_in_range (input_section, rel->r_offset, 4))
15793 {
15794 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
15795 if (insn == NOP
15796 || insn == CROR_151515 || insn == CROR_313131)
15797 bfd_put_32 (input_bfd,
15798 STD_R2_0R1 + STK_TOC (htab),
15799 contents + rel->r_offset);
15800 }
15801 break;
15802
15803 /* Branch taken prediction relocations. */
15804 case R_PPC64_ADDR14_BRTAKEN:
15805 case R_PPC64_REL14_BRTAKEN:
15806 insn = 0x01 << 21; /* 'y' or 't' bit, lowest bit of BO field. */
15807 /* Fall through. */
15808
15809 /* Branch not taken prediction relocations. */
15810 case R_PPC64_ADDR14_BRNTAKEN:
15811 case R_PPC64_REL14_BRNTAKEN:
15812 if (!offset_in_range (input_section, rel->r_offset, 4))
15813 break;
15814 insn |= bfd_get_32 (input_bfd,
15815 contents + rel->r_offset) & ~(0x01 << 21);
15816 /* Fall through. */
15817
15818 case R_PPC64_REL14:
15819 max_br_offset = 1 << 15;
15820 /* Fall through. */
15821
15822 case R_PPC64_REL24:
15823 case R_PPC64_REL24_NOTOC:
15824 case R_PPC64_REL24_P9NOTOC:
15825 case R_PPC64_PLTCALL:
15826 case R_PPC64_PLTCALL_NOTOC:
15827 /* Calls to functions with a different TOC, such as calls to
15828 shared objects, need to alter the TOC pointer. This is
15829 done using a linkage stub. A REL24 branching to these
15830 linkage stubs needs to be followed by a nop, as the nop
15831 will be replaced with an instruction to restore the TOC
15832 base pointer. */
15833 fdh = h;
15834 if (h != NULL
15835 && h->oh != NULL
15836 && h->oh->is_func_descriptor)
15837 fdh = ppc_follow_link (h->oh);
15838 stub_entry = ppc_get_stub_entry (input_section, sec, fdh, &orig_rel,
15839 htab);
15840 if ((r_type == R_PPC64_PLTCALL
15841 || r_type == R_PPC64_PLTCALL_NOTOC)
15842 && stub_entry != NULL
15843 && stub_entry->type.main == ppc_stub_plt_call)
15844 stub_entry = NULL;
15845
15846 if (stub_entry != NULL
15847 && (stub_entry->type.main == ppc_stub_plt_call
15848 || stub_entry->type.r2save))
15849 {
15850 bool can_plt_call = false;
15851
15852 if (r_type == R_PPC64_REL24_NOTOC
15853 || r_type == R_PPC64_REL24_P9NOTOC)
15854 {
15855 /* NOTOC calls don't need to restore r2. */
15856 can_plt_call = true;
15857 }
15858 else if (stub_entry->type.main == ppc_stub_plt_call
15859 && !htab->opd_abi
15860 && htab->params->plt_localentry0 != 0
15861 && h != NULL
15862 && is_elfv2_localentry0 (&h->elf))
15863 {
15864 /* The function doesn't use or change r2. */
15865 can_plt_call = true;
15866 }
15867
15868 /* All of these stubs may modify r2, so there must be a
15869 branch and link followed by a nop. The nop is
15870 replaced by an insn to restore r2. */
15871 else if (offset_in_range (input_section, rel->r_offset, 8))
15872 {
15873 unsigned long br;
15874
15875 br = bfd_get_32 (input_bfd,
15876 contents + rel->r_offset);
15877 if ((br & 1) != 0)
15878 {
15879 unsigned long nop;
15880
15881 nop = bfd_get_32 (input_bfd,
15882 contents + rel->r_offset + 4);
15883 if (nop == LD_R2_0R1 + STK_TOC (htab))
15884 can_plt_call = true;
15885 else if (nop == NOP
15886 || nop == CROR_151515
15887 || nop == CROR_313131)
15888 {
15889 if (h != NULL
15890 && is_tls_get_addr (&h->elf, htab)
15891 && htab->params->tls_get_addr_opt)
15892 {
15893 /* Special stub used, leave nop alone. */
15894 }
15895 else
15896 bfd_put_32 (input_bfd,
15897 LD_R2_0R1 + STK_TOC (htab),
15898 contents + rel->r_offset + 4);
15899 can_plt_call = true;
15900 }
15901 }
15902 }
15903
15904 if (!can_plt_call && h != NULL)
15905 {
15906 const char *name = h->elf.root.root.string;
15907
15908 if (*name == '.')
15909 ++name;
15910
15911 if (startswith (name, "__libc_start_main")
15912 && (name[17] == 0 || name[17] == '@'))
15913 {
15914 /* Allow crt1 branch to go via a toc adjusting
15915 stub. Other calls that never return could do
15916 the same, if we could detect such. */
15917 can_plt_call = true;
15918 }
15919 }
15920
15921 if (!can_plt_call)
15922 {
15923 /* g++ as of 20130507 emits self-calls without a
15924 following nop. This is arguably wrong since we
15925 have conflicting information. On the one hand a
15926 global symbol and on the other a local call
15927 sequence, but don't error for this special case.
15928 It isn't possible to cheaply verify we have
15929 exactly such a call. Allow all calls to the same
15930 section. */
15931 asection *code_sec = sec;
15932
15933 if (get_opd_info (sec) != NULL)
15934 {
15935 bfd_vma off = (relocation + addend
15936 - sec->output_section->vma
15937 - sec->output_offset);
15938
15939 opd_entry_value (sec, off, &code_sec, NULL, false);
15940 }
15941 if (code_sec == input_section)
15942 can_plt_call = true;
15943 }
15944
15945 if (!can_plt_call)
15946 {
15947 if (stub_entry->type.main == ppc_stub_plt_call)
15948 info->callbacks->einfo
15949 /* xgettext:c-format */
15950 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15951 "(plt call stub)\n"),
15952 input_bfd, input_section, rel->r_offset, sym_name);
15953 else
15954 info->callbacks->einfo
15955 /* xgettext:c-format */
15956 (_("%H: call to `%pT' lacks nop, can't restore toc; "
15957 "(toc save/adjust stub)\n"),
15958 input_bfd, input_section, rel->r_offset, sym_name);
15959
15960 bfd_set_error (bfd_error_bad_value);
15961 ret = false;
15962 }
15963
15964 if (can_plt_call
15965 && stub_entry->type.main == ppc_stub_plt_call)
15966 unresolved_reloc = false;
15967 }
15968
15969 if ((stub_entry == NULL
15970 || stub_entry->type.main == ppc_stub_long_branch
15971 || stub_entry->type.main == ppc_stub_plt_branch)
15972 && get_opd_info (sec) != NULL)
15973 {
15974 /* The branch destination is the value of the opd entry. */
15975 bfd_vma off = (relocation + addend
15976 - sec->output_section->vma
15977 - sec->output_offset);
15978 bfd_vma dest = opd_entry_value (sec, off, NULL, NULL, false);
15979 if (dest != (bfd_vma) -1)
15980 {
15981 relocation = dest;
15982 addend = 0;
15983 reloc_dest = DEST_OPD;
15984 }
15985 }
15986
15987 /* If the branch is out of reach we ought to have a long
15988 branch stub. */
15989 from = (rel->r_offset
15990 + input_section->output_offset
15991 + input_section->output_section->vma);
15992
15993 relocation += PPC64_LOCAL_ENTRY_OFFSET (fdh
15994 ? fdh->elf.other
15995 : sym->st_other);
15996
15997 if (stub_entry != NULL
15998 && (stub_entry->type.main == ppc_stub_long_branch
15999 || stub_entry->type.main == ppc_stub_plt_branch))
16000 {
16001 if (stub_entry->type.sub == ppc_stub_toc
16002 && !stub_entry->type.r2save
16003 && (r_type == R_PPC64_ADDR14_BRTAKEN
16004 || r_type == R_PPC64_ADDR14_BRNTAKEN
16005 || (relocation + addend - from + max_br_offset
16006 < 2 * max_br_offset)))
16007 /* Don't use the stub if this branch is in range. */
16008 stub_entry = NULL;
16009
16010 if (stub_entry != NULL
16011 && stub_entry->type.sub >= ppc_stub_notoc
16012 && ((r_type != R_PPC64_REL24_NOTOC
16013 && r_type != R_PPC64_REL24_P9NOTOC)
16014 || ((fdh ? fdh->elf.other : sym->st_other)
16015 & STO_PPC64_LOCAL_MASK) <= 1 << STO_PPC64_LOCAL_BIT)
16016 && (relocation + addend - from + max_br_offset
16017 < 2 * max_br_offset))
16018 stub_entry = NULL;
16019
16020 if (stub_entry != NULL
16021 && stub_entry->type.r2save
16022 && (r_type == R_PPC64_REL24_NOTOC
16023 || r_type == R_PPC64_REL24_P9NOTOC)
16024 && (relocation + addend - from + max_br_offset
16025 < 2 * max_br_offset))
16026 stub_entry = NULL;
16027 }
16028
16029 if (stub_entry != NULL)
16030 {
16031 /* Munge up the value and addend so that we call the stub
16032 rather than the procedure directly. */
16033 asection *stub_sec = stub_entry->group->stub_sec;
16034
16035 if (stub_entry->type.main == ppc_stub_save_res)
16036 relocation += (stub_sec->output_offset
16037 + stub_sec->output_section->vma
16038 + stub_sec->size - htab->sfpr->size
16039 - htab->sfpr->output_offset
16040 - htab->sfpr->output_section->vma);
16041 else
16042 relocation = (stub_entry->stub_offset
16043 + stub_sec->output_offset
16044 + stub_sec->output_section->vma);
16045 addend = 0;
16046 reloc_dest = DEST_STUB;
16047
16048 if (((stub_entry->type.r2save
16049 && (r_type == R_PPC64_REL24_NOTOC
16050 || r_type == R_PPC64_REL24_P9NOTOC))
16051 || ((stub_entry->type.main == ppc_stub_plt_call
16052 && (ALWAYS_EMIT_R2SAVE || stub_entry->type.r2save))
16053 && rel + 1 < relend
16054 && rel[1].r_offset == rel->r_offset + 4
16055 && ELF64_R_TYPE (rel[1].r_info) == R_PPC64_TOCSAVE))
16056 && !(stub_entry->type.main == ppc_stub_plt_call
16057 && htab->params->tls_get_addr_opt
16058 && h != NULL
16059 && is_tls_get_addr (&h->elf, htab)))
16060 {
16061 /* Skip over the r2 store at the start of the stub. */
16062 relocation += 4;
16063 }
16064
16065 if ((r_type == R_PPC64_REL24_NOTOC
16066 || r_type == R_PPC64_REL24_P9NOTOC)
16067 && stub_entry->type.main == ppc_stub_plt_call
16068 && stub_entry->type.sub >= ppc_stub_notoc)
16069 htab->notoc_plt = 1;
16070 }
16071
16072 if (insn != 0)
16073 {
16074 if (is_isa_v2)
16075 {
16076 /* Set 'a' bit. This is 0b00010 in BO field for branch
16077 on CR(BI) insns (BO == 001at or 011at), and 0b01000
16078 for branch on CTR insns (BO == 1a00t or 1a01t). */
16079 if ((insn & (0x14 << 21)) == (0x04 << 21))
16080 insn |= 0x02 << 21;
16081 else if ((insn & (0x14 << 21)) == (0x10 << 21))
16082 insn |= 0x08 << 21;
16083 else
16084 break;
16085 }
16086 else
16087 {
16088 /* Invert 'y' bit if not the default. */
16089 if ((bfd_signed_vma) (relocation + addend - from) < 0)
16090 insn ^= 0x01 << 21;
16091 }
16092
16093 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
16094 }
16095
16096 /* NOP out calls to undefined weak functions.
16097 We can thus call a weak function without first
16098 checking whether the function is defined. */
16099 else if (h != NULL
16100 && h->elf.root.type == bfd_link_hash_undefweak
16101 && h->elf.dynindx == -1
16102 && (r_type == R_PPC64_REL24
16103 || r_type == R_PPC64_REL24_NOTOC
16104 || r_type == R_PPC64_REL24_P9NOTOC)
16105 && relocation == 0
16106 && addend == 0
16107 && offset_in_range (input_section, rel->r_offset, 4))
16108 {
16109 bfd_put_32 (input_bfd, NOP, contents + rel->r_offset);
16110 goto copy_reloc;
16111 }
16112 break;
16113
16114 case R_PPC64_GOT16_DS:
16115 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16116 || !htab->do_toc_opt)
16117 break;
16118 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16119 if (relocation + addend - from + 0x8000 < 0x10000
16120 && sec != NULL
16121 && sec->output_section != NULL
16122 && !discarded_section (sec)
16123 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16124 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16125 {
16126 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16127 if ((insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16128 {
16129 insn += (14u << 26) - (58u << 26);
16130 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16131 r_type = R_PPC64_TOC16;
16132 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16133 }
16134 }
16135 break;
16136
16137 case R_PPC64_GOT16_LO_DS:
16138 case R_PPC64_GOT16_HA:
16139 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16140 || !htab->do_toc_opt)
16141 break;
16142 from = TOCstart + htab->sec_info[input_section->id].toc_off;
16143 if (relocation + addend - from + 0x80008000ULL < 0x100000000ULL
16144 && sec != NULL
16145 && sec->output_section != NULL
16146 && !discarded_section (sec)
16147 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16148 && offset_in_range (input_section, rel->r_offset & ~3, 4))
16149 {
16150 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
16151 if (r_type == R_PPC64_GOT16_LO_DS
16152 && (insn & (0x3fu << 26 | 0x3)) == 58u << 26 /* ld */)
16153 {
16154 insn += (14u << 26) - (58u << 26);
16155 bfd_put_32 (input_bfd, insn, contents + (rel->r_offset & ~3));
16156 r_type = R_PPC64_TOC16_LO;
16157 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16158 }
16159 else if (r_type == R_PPC64_GOT16_HA
16160 && (insn & (0x3fu << 26)) == 15u << 26 /* addis */)
16161 {
16162 r_type = R_PPC64_TOC16_HA;
16163 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16164 }
16165 }
16166 break;
16167
16168 case R_PPC64_GOT_PCREL34:
16169 if ((h ? h->elf.type : ELF_ST_TYPE (sym->st_info)) == STT_GNU_IFUNC
16170 || !htab->do_toc_opt)
16171 break;
16172 from = (rel->r_offset
16173 + input_section->output_section->vma
16174 + input_section->output_offset);
16175 if (!(relocation - from + (1ULL << 33) < 1ULL << 34
16176 && sec != NULL
16177 && sec->output_section != NULL
16178 && !discarded_section (sec)
16179 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16180 && offset_in_range (input_section, rel->r_offset, 8)))
16181 break;
16182
16183 offset = rel->r_offset;
16184 pinsn = bfd_get_32 (input_bfd, contents + offset);
16185 pinsn <<= 32;
16186 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16187 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16188 != ((1ULL << 58) | (1ULL << 52) | (57ULL << 26) /* pld */))
16189 break;
16190
16191 /* Replace with paddi. */
16192 pinsn += (2ULL << 56) + (14ULL << 26) - (57ULL << 26);
16193 r_type = R_PPC64_PCREL34;
16194 rel->r_info = ELF64_R_INFO (r_symndx, r_type);
16195 bfd_put_32 (input_bfd, pinsn >> 32, contents + offset);
16196 bfd_put_32 (input_bfd, pinsn, contents + offset + 4);
16197 /* Fall through. */
16198
16199 case R_PPC64_PCREL34:
16200 if (!htab->params->no_pcrel_opt
16201 && rel + 1 < relend
16202 && rel[1].r_offset == rel->r_offset
16203 && rel[1].r_info == ELF64_R_INFO (0, R_PPC64_PCREL_OPT)
16204 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf))
16205 && offset_in_range (input_section, rel->r_offset, 8))
16206 {
16207 offset = rel->r_offset;
16208 pinsn = bfd_get_32 (input_bfd, contents + offset);
16209 pinsn <<= 32;
16210 pinsn |= bfd_get_32 (input_bfd, contents + offset + 4);
16211 if ((pinsn & ((-1ULL << 50) | (63ULL << 26)))
16212 == ((1ULL << 58) | (2ULL << 56) | (1ULL << 52)
16213 | (14ULL << 26) /* paddi */))
16214 {
16215 bfd_vma off2 = rel[1].r_addend;
16216 if (off2 == 0)
16217 /* zero means next insn. */
16218 off2 = 8;
16219 off2 += offset;
16220 if (offset_in_range (input_section, off2, 4))
16221 {
16222 uint64_t pinsn2;
16223 bfd_signed_vma addend_off;
16224 pinsn2 = bfd_get_32 (input_bfd, contents + off2);
16225 pinsn2 <<= 32;
16226 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16227 {
16228 if (!offset_in_range (input_section, off2, 8))
16229 break;
16230 pinsn2 |= bfd_get_32 (input_bfd,
16231 contents + off2 + 4);
16232 }
16233 if (xlate_pcrel_opt (&pinsn, &pinsn2, &addend_off))
16234 {
16235 addend += addend_off;
16236 rel->r_addend = addend;
16237 bfd_put_32 (input_bfd, pinsn >> 32,
16238 contents + offset);
16239 bfd_put_32 (input_bfd, pinsn,
16240 contents + offset + 4);
16241 bfd_put_32 (input_bfd, pinsn2 >> 32,
16242 contents + off2);
16243 if ((pinsn2 & (63ULL << 58)) == 1ULL << 58)
16244 bfd_put_32 (input_bfd, pinsn2,
16245 contents + off2 + 4);
16246 }
16247 }
16248 }
16249 }
16250 break;
16251 }
16252
16253 tls_type = 0;
16254 save_unresolved_reloc = unresolved_reloc;
16255 switch (r_type)
16256 {
16257 default:
16258 /* xgettext:c-format */
16259 _bfd_error_handler (_("%pB: %s unsupported"),
16260 input_bfd, ppc64_elf_howto_table[r_type]->name);
16261
16262 bfd_set_error (bfd_error_bad_value);
16263 ret = false;
16264 goto copy_reloc;
16265
16266 case R_PPC64_NONE:
16267 case R_PPC64_TLS:
16268 case R_PPC64_TLSGD:
16269 case R_PPC64_TLSLD:
16270 case R_PPC64_TOCSAVE:
16271 case R_PPC64_GNU_VTINHERIT:
16272 case R_PPC64_GNU_VTENTRY:
16273 case R_PPC64_ENTRY:
16274 case R_PPC64_PCREL_OPT:
16275 goto copy_reloc;
16276
16277 /* GOT16 relocations. Like an ADDR16 using the symbol's
16278 address in the GOT as relocation value instead of the
16279 symbol's value itself. Also, create a GOT entry for the
16280 symbol and put the symbol value there. */
16281 case R_PPC64_GOT_TLSGD16:
16282 case R_PPC64_GOT_TLSGD16_LO:
16283 case R_PPC64_GOT_TLSGD16_HI:
16284 case R_PPC64_GOT_TLSGD16_HA:
16285 case R_PPC64_GOT_TLSGD_PCREL34:
16286 tls_type = TLS_TLS | TLS_GD;
16287 goto dogot;
16288
16289 case R_PPC64_GOT_TLSLD16:
16290 case R_PPC64_GOT_TLSLD16_LO:
16291 case R_PPC64_GOT_TLSLD16_HI:
16292 case R_PPC64_GOT_TLSLD16_HA:
16293 case R_PPC64_GOT_TLSLD_PCREL34:
16294 tls_type = TLS_TLS | TLS_LD;
16295 goto dogot;
16296
16297 case R_PPC64_GOT_TPREL16_DS:
16298 case R_PPC64_GOT_TPREL16_LO_DS:
16299 case R_PPC64_GOT_TPREL16_HI:
16300 case R_PPC64_GOT_TPREL16_HA:
16301 case R_PPC64_GOT_TPREL_PCREL34:
16302 tls_type = TLS_TLS | TLS_TPREL;
16303 goto dogot;
16304
16305 case R_PPC64_GOT_DTPREL16_DS:
16306 case R_PPC64_GOT_DTPREL16_LO_DS:
16307 case R_PPC64_GOT_DTPREL16_HI:
16308 case R_PPC64_GOT_DTPREL16_HA:
16309 case R_PPC64_GOT_DTPREL_PCREL34:
16310 tls_type = TLS_TLS | TLS_DTPREL;
16311 goto dogot;
16312
16313 case R_PPC64_GOT16:
16314 case R_PPC64_GOT16_LO:
16315 case R_PPC64_GOT16_HI:
16316 case R_PPC64_GOT16_HA:
16317 case R_PPC64_GOT16_DS:
16318 case R_PPC64_GOT16_LO_DS:
16319 case R_PPC64_GOT_PCREL34:
16320 dogot:
16321 {
16322 /* Relocation is to the entry for this symbol in the global
16323 offset table. */
16324 asection *got;
16325 bfd_vma *offp;
16326 bfd_vma off;
16327 unsigned long indx = 0;
16328 struct got_entry *ent;
16329
16330 if (tls_type == (TLS_TLS | TLS_LD)
16331 && (h == NULL || SYMBOL_REFERENCES_LOCAL (info, &h->elf)))
16332 ent = ppc64_tlsld_got (input_bfd);
16333 else
16334 {
16335 if (h != NULL)
16336 {
16337 if (!htab->elf.dynamic_sections_created
16338 || h->elf.dynindx == -1
16339 || SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16340 || UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16341 /* This is actually a static link, or it is a
16342 -Bsymbolic link and the symbol is defined
16343 locally, or the symbol was forced to be local
16344 because of a version file. */
16345 ;
16346 else
16347 {
16348 indx = h->elf.dynindx;
16349 unresolved_reloc = false;
16350 }
16351 ent = h->elf.got.glist;
16352 }
16353 else
16354 {
16355 if (local_got_ents == NULL)
16356 abort ();
16357 ent = local_got_ents[r_symndx];
16358 }
16359
16360 for (; ent != NULL; ent = ent->next)
16361 if (ent->addend == orig_rel.r_addend
16362 && ent->owner == input_bfd
16363 && ent->tls_type == tls_type)
16364 break;
16365 }
16366
16367 if (ent == NULL)
16368 abort ();
16369 if (ent->is_indirect)
16370 ent = ent->got.ent;
16371 offp = &ent->got.offset;
16372 got = ppc64_elf_tdata (ent->owner)->got;
16373 if (got == NULL)
16374 abort ();
16375
16376 /* The offset must always be a multiple of 8. We use the
16377 least significant bit to record whether we have already
16378 processed this entry. */
16379 off = *offp;
16380 if ((off & 1) != 0)
16381 off &= ~1;
16382 else
16383 {
16384 /* Generate relocs for the dynamic linker, except in
16385 the case of TLSLD where we'll use one entry per
16386 module. */
16387 asection *relgot;
16388 bool ifunc;
16389
16390 *offp = off | 1;
16391 relgot = NULL;
16392 ifunc = (h != NULL
16393 ? h->elf.type == STT_GNU_IFUNC
16394 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC);
16395 if (ifunc)
16396 {
16397 relgot = htab->elf.irelplt;
16398 if (indx == 0 || is_static_defined (&h->elf))
16399 htab->elf.ifunc_resolvers = true;
16400 }
16401 else if (indx != 0
16402 || (bfd_link_pic (info)
16403 && (h == NULL
16404 || !UNDEFWEAK_NO_DYNAMIC_RELOC (info, &h->elf))
16405 && !(tls_type != 0
16406 && bfd_link_executable (info)
16407 && (h == NULL
16408 || SYMBOL_REFERENCES_LOCAL (info,
16409 &h->elf)))))
16410 relgot = ppc64_elf_tdata (ent->owner)->relgot;
16411 if (relgot != NULL)
16412 {
16413 outrel.r_offset = (got->output_section->vma
16414 + got->output_offset
16415 + off);
16416 outrel.r_addend = orig_rel.r_addend;
16417 if (tls_type & (TLS_LD | TLS_GD))
16418 {
16419 outrel.r_addend = 0;
16420 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPMOD64);
16421 if (tls_type == (TLS_TLS | TLS_GD))
16422 {
16423 loc = relgot->contents;
16424 loc += (relgot->reloc_count++
16425 * sizeof (Elf64_External_Rela));
16426 bfd_elf64_swap_reloca_out (output_bfd,
16427 &outrel, loc);
16428 outrel.r_offset += 8;
16429 outrel.r_addend = orig_rel.r_addend;
16430 outrel.r_info
16431 = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16432 }
16433 }
16434 else if (tls_type == (TLS_TLS | TLS_DTPREL))
16435 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_DTPREL64);
16436 else if (tls_type == (TLS_TLS | TLS_TPREL))
16437 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_TPREL64);
16438 else if (indx != 0)
16439 outrel.r_info = ELF64_R_INFO (indx, R_PPC64_GLOB_DAT);
16440 else
16441 {
16442 if (ifunc)
16443 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16444 else
16445 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16446
16447 /* Write the .got section contents for the sake
16448 of prelink. */
16449 loc = got->contents + off;
16450 bfd_put_64 (output_bfd, outrel.r_addend + relocation,
16451 loc);
16452 }
16453
16454 if (indx == 0 && tls_type != (TLS_TLS | TLS_LD))
16455 {
16456 outrel.r_addend += relocation;
16457 if (tls_type & (TLS_GD | TLS_DTPREL | TLS_TPREL))
16458 {
16459 if (htab->elf.tls_sec == NULL)
16460 outrel.r_addend = 0;
16461 else
16462 outrel.r_addend -= htab->elf.tls_sec->vma;
16463 }
16464 }
16465 loc = relgot->contents;
16466 loc += (relgot->reloc_count++
16467 * sizeof (Elf64_External_Rela));
16468 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16469 }
16470
16471 /* Init the .got section contents here if we're not
16472 emitting a reloc. */
16473 else
16474 {
16475 relocation += orig_rel.r_addend;
16476 if (tls_type != 0)
16477 {
16478 if (htab->elf.tls_sec == NULL)
16479 relocation = 0;
16480 else
16481 {
16482 if (tls_type & TLS_LD)
16483 relocation = 0;
16484 else
16485 relocation -= htab->elf.tls_sec->vma + DTP_OFFSET;
16486 if (tls_type & TLS_TPREL)
16487 relocation += DTP_OFFSET - TP_OFFSET;
16488 }
16489
16490 if (tls_type & (TLS_GD | TLS_LD))
16491 {
16492 bfd_put_64 (output_bfd, relocation,
16493 got->contents + off + 8);
16494 relocation = 1;
16495 }
16496 }
16497 bfd_put_64 (output_bfd, relocation,
16498 got->contents + off);
16499 }
16500 }
16501
16502 if (off >= (bfd_vma) -2)
16503 abort ();
16504
16505 relocation = got->output_section->vma + got->output_offset + off;
16506 addend = 0;
16507 if (!(r_type == R_PPC64_GOT_PCREL34
16508 || r_type == R_PPC64_GOT_TLSGD_PCREL34
16509 || r_type == R_PPC64_GOT_TLSLD_PCREL34
16510 || r_type == R_PPC64_GOT_TPREL_PCREL34
16511 || r_type == R_PPC64_GOT_DTPREL_PCREL34))
16512 addend = -(TOCstart + htab->sec_info[input_section->id].toc_off);
16513 }
16514 break;
16515
16516 case R_PPC64_PLT16_HA:
16517 case R_PPC64_PLT16_HI:
16518 case R_PPC64_PLT16_LO:
16519 case R_PPC64_PLT16_LO_DS:
16520 case R_PPC64_PLT_PCREL34:
16521 case R_PPC64_PLT_PCREL34_NOTOC:
16522 case R_PPC64_PLT32:
16523 case R_PPC64_PLT64:
16524 case R_PPC64_PLTSEQ:
16525 case R_PPC64_PLTSEQ_NOTOC:
16526 case R_PPC64_PLTCALL:
16527 case R_PPC64_PLTCALL_NOTOC:
16528 /* Relocation is to the entry for this symbol in the
16529 procedure linkage table. */
16530 unresolved_reloc = true;
16531 {
16532 struct plt_entry **plt_list = NULL;
16533 if (h != NULL)
16534 plt_list = &h->elf.plt.plist;
16535 else if (local_got_ents != NULL)
16536 {
16537 struct plt_entry **local_plt = (struct plt_entry **)
16538 (local_got_ents + symtab_hdr->sh_info);
16539 plt_list = local_plt + r_symndx;
16540 }
16541 if (plt_list)
16542 {
16543 struct plt_entry *ent;
16544
16545 for (ent = *plt_list; ent != NULL; ent = ent->next)
16546 if (ent->plt.offset != (bfd_vma) -1
16547 && ent->addend == orig_rel.r_addend)
16548 {
16549 asection *plt;
16550 bfd_vma got;
16551
16552 plt = htab->elf.splt;
16553 if (use_local_plt (info, elf_hash_entry (h)))
16554 {
16555 if (h != NULL
16556 ? h->elf.type == STT_GNU_IFUNC
16557 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16558 plt = htab->elf.iplt;
16559 else
16560 plt = htab->pltlocal;
16561 }
16562 relocation = (plt->output_section->vma
16563 + plt->output_offset
16564 + ent->plt.offset);
16565 if (r_type == R_PPC64_PLT16_HA
16566 || r_type == R_PPC64_PLT16_HI
16567 || r_type == R_PPC64_PLT16_LO
16568 || r_type == R_PPC64_PLT16_LO_DS)
16569 {
16570 got = (elf_gp (output_bfd)
16571 + htab->sec_info[input_section->id].toc_off);
16572 relocation -= got;
16573 }
16574 addend = 0;
16575 unresolved_reloc = false;
16576 break;
16577 }
16578 }
16579 }
16580 break;
16581
16582 case R_PPC64_TOC:
16583 /* Relocation value is TOC base. */
16584 relocation = TOCstart;
16585 if (r_symndx == STN_UNDEF)
16586 relocation += htab->sec_info[input_section->id].toc_off;
16587 else if (unresolved_reloc)
16588 ;
16589 else if (sec != NULL && sec->id < htab->sec_info_arr_size)
16590 relocation += htab->sec_info[sec->id].toc_off;
16591 else
16592 unresolved_reloc = true;
16593 goto dodyn;
16594
16595 /* TOC16 relocs. We want the offset relative to the TOC base,
16596 which is the address of the start of the TOC plus 0x8000.
16597 The TOC consists of sections .got, .toc, .tocbss, and .plt,
16598 in this order. */
16599 case R_PPC64_TOC16:
16600 case R_PPC64_TOC16_LO:
16601 case R_PPC64_TOC16_HI:
16602 case R_PPC64_TOC16_DS:
16603 case R_PPC64_TOC16_LO_DS:
16604 case R_PPC64_TOC16_HA:
16605 addend -= TOCstart + htab->sec_info[input_section->id].toc_off;
16606 if (h != NULL)
16607 goto dodyn;
16608 break;
16609
16610 /* Relocate against the beginning of the section. */
16611 case R_PPC64_SECTOFF:
16612 case R_PPC64_SECTOFF_LO:
16613 case R_PPC64_SECTOFF_HI:
16614 case R_PPC64_SECTOFF_DS:
16615 case R_PPC64_SECTOFF_LO_DS:
16616 case R_PPC64_SECTOFF_HA:
16617 if (sec != NULL)
16618 addend -= sec->output_section->vma;
16619 break;
16620
16621 case R_PPC64_REL16:
16622 case R_PPC64_REL16_LO:
16623 case R_PPC64_REL16_HI:
16624 case R_PPC64_REL16_HA:
16625 case R_PPC64_REL16_HIGH:
16626 case R_PPC64_REL16_HIGHA:
16627 case R_PPC64_REL16_HIGHER:
16628 case R_PPC64_REL16_HIGHERA:
16629 case R_PPC64_REL16_HIGHEST:
16630 case R_PPC64_REL16_HIGHESTA:
16631 case R_PPC64_REL16_HIGHER34:
16632 case R_PPC64_REL16_HIGHERA34:
16633 case R_PPC64_REL16_HIGHEST34:
16634 case R_PPC64_REL16_HIGHESTA34:
16635 case R_PPC64_REL16DX_HA:
16636 case R_PPC64_REL14:
16637 case R_PPC64_REL14_BRNTAKEN:
16638 case R_PPC64_REL14_BRTAKEN:
16639 case R_PPC64_REL24:
16640 case R_PPC64_REL24_NOTOC:
16641 case R_PPC64_REL24_P9NOTOC:
16642 case R_PPC64_PCREL34:
16643 case R_PPC64_PCREL28:
16644 break;
16645
16646 case R_PPC64_TPREL16:
16647 case R_PPC64_TPREL16_LO:
16648 case R_PPC64_TPREL16_HI:
16649 case R_PPC64_TPREL16_HA:
16650 case R_PPC64_TPREL16_DS:
16651 case R_PPC64_TPREL16_LO_DS:
16652 case R_PPC64_TPREL16_HIGH:
16653 case R_PPC64_TPREL16_HIGHA:
16654 case R_PPC64_TPREL16_HIGHER:
16655 case R_PPC64_TPREL16_HIGHERA:
16656 case R_PPC64_TPREL16_HIGHEST:
16657 case R_PPC64_TPREL16_HIGHESTA:
16658 if (h != NULL
16659 && h->elf.root.type == bfd_link_hash_undefweak
16660 && h->elf.dynindx == -1
16661 && offset_in_range (input_section, rel->r_offset - d_offset, 4))
16662 {
16663 /* Make this relocation against an undefined weak symbol
16664 resolve to zero. This is really just a tweak, since
16665 code using weak externs ought to check that they are
16666 defined before using them. */
16667 bfd_byte *p = contents + rel->r_offset - d_offset;
16668
16669 insn = bfd_get_32 (input_bfd, p);
16670 insn = _bfd_elf_ppc_at_tprel_transform (insn, 13);
16671 if (insn != 0)
16672 bfd_put_32 (input_bfd, insn, p);
16673 break;
16674 }
16675 /* Fall through. */
16676
16677 case R_PPC64_TPREL34:
16678 if (htab->elf.tls_sec != NULL)
16679 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16680 /* The TPREL16 relocs shouldn't really be used in shared
16681 libs or with non-local symbols as that will result in
16682 DT_TEXTREL being set, but support them anyway. */
16683 goto dodyn;
16684
16685 case R_PPC64_DTPREL16:
16686 case R_PPC64_DTPREL16_LO:
16687 case R_PPC64_DTPREL16_HI:
16688 case R_PPC64_DTPREL16_HA:
16689 case R_PPC64_DTPREL16_DS:
16690 case R_PPC64_DTPREL16_LO_DS:
16691 case R_PPC64_DTPREL16_HIGH:
16692 case R_PPC64_DTPREL16_HIGHA:
16693 case R_PPC64_DTPREL16_HIGHER:
16694 case R_PPC64_DTPREL16_HIGHERA:
16695 case R_PPC64_DTPREL16_HIGHEST:
16696 case R_PPC64_DTPREL16_HIGHESTA:
16697 case R_PPC64_DTPREL34:
16698 if (htab->elf.tls_sec != NULL)
16699 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16700 break;
16701
16702 case R_PPC64_ADDR64_LOCAL:
16703 addend += PPC64_LOCAL_ENTRY_OFFSET (h != NULL
16704 ? h->elf.other
16705 : sym->st_other);
16706 break;
16707
16708 case R_PPC64_DTPMOD64:
16709 relocation = 1;
16710 addend = 0;
16711 goto dodyn;
16712
16713 case R_PPC64_TPREL64:
16714 if (htab->elf.tls_sec != NULL)
16715 addend -= htab->elf.tls_sec->vma + TP_OFFSET;
16716 goto dodyn;
16717
16718 case R_PPC64_DTPREL64:
16719 if (htab->elf.tls_sec != NULL)
16720 addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
16721 /* Fall through. */
16722
16723 /* Relocations that may need to be propagated if this is a
16724 dynamic object. */
16725 case R_PPC64_REL30:
16726 case R_PPC64_REL32:
16727 case R_PPC64_REL64:
16728 case R_PPC64_ADDR14:
16729 case R_PPC64_ADDR14_BRNTAKEN:
16730 case R_PPC64_ADDR14_BRTAKEN:
16731 case R_PPC64_ADDR16:
16732 case R_PPC64_ADDR16_DS:
16733 case R_PPC64_ADDR16_HA:
16734 case R_PPC64_ADDR16_HI:
16735 case R_PPC64_ADDR16_HIGH:
16736 case R_PPC64_ADDR16_HIGHA:
16737 case R_PPC64_ADDR16_HIGHER:
16738 case R_PPC64_ADDR16_HIGHERA:
16739 case R_PPC64_ADDR16_HIGHEST:
16740 case R_PPC64_ADDR16_HIGHESTA:
16741 case R_PPC64_ADDR16_LO:
16742 case R_PPC64_ADDR16_LO_DS:
16743 case R_PPC64_ADDR16_HIGHER34:
16744 case R_PPC64_ADDR16_HIGHERA34:
16745 case R_PPC64_ADDR16_HIGHEST34:
16746 case R_PPC64_ADDR16_HIGHESTA34:
16747 case R_PPC64_ADDR24:
16748 case R_PPC64_ADDR32:
16749 case R_PPC64_ADDR64:
16750 case R_PPC64_UADDR16:
16751 case R_PPC64_UADDR32:
16752 case R_PPC64_UADDR64:
16753 case R_PPC64_D34:
16754 case R_PPC64_D34_LO:
16755 case R_PPC64_D34_HI30:
16756 case R_PPC64_D34_HA30:
16757 case R_PPC64_D28:
16758 dodyn:
16759 if ((input_section->flags & SEC_ALLOC) == 0)
16760 break;
16761
16762 if (NO_OPD_RELOCS && is_opd)
16763 break;
16764
16765 if (bfd_link_pic (info)
16766 ? ((h == NULL
16767 || h->elf.dyn_relocs != NULL)
16768 && ((h != NULL && pc_dynrelocs (h))
16769 || must_be_dyn_reloc (info, r_type)))
16770 : (h != NULL
16771 ? h->elf.dyn_relocs != NULL
16772 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16773 {
16774 bool skip, relocate;
16775 asection *sreloc;
16776 bfd_vma out_off;
16777 long indx = 0;
16778
16779 /* When generating a dynamic object, these relocations
16780 are copied into the output file to be resolved at run
16781 time. */
16782
16783 skip = false;
16784 relocate = false;
16785
16786 out_off = _bfd_elf_section_offset (output_bfd, info,
16787 input_section, rel->r_offset);
16788 if (out_off == (bfd_vma) -1)
16789 skip = true;
16790 else if (out_off == (bfd_vma) -2)
16791 skip = true, relocate = true;
16792 out_off += (input_section->output_section->vma
16793 + input_section->output_offset);
16794 outrel.r_offset = out_off;
16795 outrel.r_addend = rel->r_addend;
16796
16797 /* Optimize unaligned reloc use. */
16798 if ((r_type == R_PPC64_ADDR64 && (out_off & 7) != 0)
16799 || (r_type == R_PPC64_UADDR64 && (out_off & 7) == 0))
16800 r_type ^= R_PPC64_ADDR64 ^ R_PPC64_UADDR64;
16801 else if ((r_type == R_PPC64_ADDR32 && (out_off & 3) != 0)
16802 || (r_type == R_PPC64_UADDR32 && (out_off & 3) == 0))
16803 r_type ^= R_PPC64_ADDR32 ^ R_PPC64_UADDR32;
16804 else if ((r_type == R_PPC64_ADDR16 && (out_off & 1) != 0)
16805 || (r_type == R_PPC64_UADDR16 && (out_off & 1) == 0))
16806 r_type ^= R_PPC64_ADDR16 ^ R_PPC64_UADDR16;
16807
16808 if (skip)
16809 memset (&outrel, 0, sizeof outrel);
16810 else if (h != NULL
16811 && !SYMBOL_REFERENCES_LOCAL (info, &h->elf)
16812 && !is_opd
16813 && r_type != R_PPC64_TOC)
16814 {
16815 indx = h->elf.dynindx;
16816 BFD_ASSERT (indx != -1);
16817 outrel.r_info = ELF64_R_INFO (indx, r_type);
16818 }
16819 else
16820 {
16821 /* This symbol is local, or marked to become local,
16822 or this is an opd section reloc which must point
16823 at a local function. */
16824 outrel.r_addend += relocation;
16825 if (r_type == R_PPC64_ADDR64 || r_type == R_PPC64_TOC)
16826 {
16827 if (is_opd && h != NULL)
16828 {
16829 /* Lie about opd entries. This case occurs
16830 when building shared libraries and we
16831 reference a function in another shared
16832 lib. The same thing happens for a weak
16833 definition in an application that's
16834 overridden by a strong definition in a
16835 shared lib. (I believe this is a generic
16836 bug in binutils handling of weak syms.)
16837 In these cases we won't use the opd
16838 entry in this lib. */
16839 unresolved_reloc = false;
16840 }
16841 if (!is_opd
16842 && r_type == R_PPC64_ADDR64
16843 && (h != NULL
16844 ? h->elf.type == STT_GNU_IFUNC
16845 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC))
16846 outrel.r_info = ELF64_R_INFO (0, R_PPC64_IRELATIVE);
16847 else
16848 {
16849 outrel.r_info = ELF64_R_INFO (0, R_PPC64_RELATIVE);
16850
16851 /* We need to relocate .opd contents for ld.so.
16852 Prelink also wants simple and consistent rules
16853 for relocs. This make all RELATIVE relocs have
16854 *r_offset equal to r_addend. */
16855 relocate = true;
16856 }
16857 }
16858 else
16859 {
16860 if (h != NULL
16861 ? h->elf.type == STT_GNU_IFUNC
16862 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16863 {
16864 info->callbacks->einfo
16865 /* xgettext:c-format */
16866 (_("%H: %s for indirect "
16867 "function `%pT' unsupported\n"),
16868 input_bfd, input_section, rel->r_offset,
16869 ppc64_elf_howto_table[r_type]->name,
16870 sym_name);
16871 ret = false;
16872 }
16873 else if (r_symndx == STN_UNDEF || bfd_is_abs_section (sec))
16874 ;
16875 else if (sec == NULL || sec->owner == NULL)
16876 {
16877 bfd_set_error (bfd_error_bad_value);
16878 return false;
16879 }
16880 else
16881 {
16882 asection *osec = sec->output_section;
16883
16884 if ((osec->flags & SEC_THREAD_LOCAL) != 0)
16885 {
16886 /* TLS symbol values are relative to the
16887 TLS segment. Dynamic relocations for
16888 local TLS symbols therefore can't be
16889 reduced to a relocation against their
16890 section symbol because it holds the
16891 address of the section, not a value
16892 relative to the TLS segment. We could
16893 change the .tdata dynamic section symbol
16894 to be zero value but STN_UNDEF works
16895 and is used elsewhere, eg. for TPREL64
16896 GOT relocs against local TLS symbols. */
16897 osec = htab->elf.tls_sec;
16898 indx = 0;
16899 }
16900 else
16901 {
16902 indx = elf_section_data (osec)->dynindx;
16903 if (indx == 0)
16904 {
16905 if ((osec->flags & SEC_READONLY) == 0
16906 && htab->elf.data_index_section != NULL)
16907 osec = htab->elf.data_index_section;
16908 else
16909 osec = htab->elf.text_index_section;
16910 indx = elf_section_data (osec)->dynindx;
16911 }
16912 BFD_ASSERT (indx != 0);
16913 }
16914
16915 /* We are turning this relocation into one
16916 against a section symbol, so subtract out
16917 the output section's address but not the
16918 offset of the input section in the output
16919 section. */
16920 outrel.r_addend -= osec->vma;
16921 }
16922
16923 outrel.r_info = ELF64_R_INFO (indx, r_type);
16924 }
16925 }
16926
16927 sreloc = elf_section_data (input_section)->sreloc;
16928 if (h != NULL
16929 ? h->elf.type == STT_GNU_IFUNC
16930 : ELF_ST_TYPE (sym->st_info) == STT_GNU_IFUNC)
16931 {
16932 sreloc = htab->elf.irelplt;
16933 if (indx == 0 || is_static_defined (&h->elf))
16934 htab->elf.ifunc_resolvers = true;
16935 }
16936 if (sreloc == NULL)
16937 abort ();
16938
16939 if (sreloc->reloc_count * sizeof (Elf64_External_Rela)
16940 >= sreloc->size)
16941 abort ();
16942 loc = sreloc->contents;
16943 loc += sreloc->reloc_count++ * sizeof (Elf64_External_Rela);
16944 bfd_elf64_swap_reloca_out (output_bfd, &outrel, loc);
16945
16946 if (!warned_dynamic
16947 && !ppc64_glibc_dynamic_reloc (ELF64_R_TYPE (outrel.r_info)))
16948 {
16949 info->callbacks->einfo
16950 /* xgettext:c-format */
16951 (_("%X%P: %pB: %s against %pT "
16952 "is not supported by glibc as a dynamic relocation\n"),
16953 input_bfd,
16954 ppc64_elf_howto_table[ELF64_R_TYPE (outrel.r_info)]->name,
16955 sym_name);
16956 warned_dynamic = true;
16957 }
16958
16959 /* If this reloc is against an external symbol, it will
16960 be computed at runtime, so there's no need to do
16961 anything now. However, for the sake of prelink ensure
16962 that the section contents are a known value. */
16963 if (!relocate)
16964 {
16965 unresolved_reloc = false;
16966 /* The value chosen here is quite arbitrary as ld.so
16967 ignores section contents except for the special
16968 case of .opd where the contents might be accessed
16969 before relocation. Choose zero, as that won't
16970 cause reloc overflow. */
16971 relocation = 0;
16972 addend = 0;
16973 /* Use *r_offset == r_addend for R_PPC64_ADDR64 relocs
16974 to improve backward compatibility with older
16975 versions of ld. */
16976 if (r_type == R_PPC64_ADDR64)
16977 addend = outrel.r_addend;
16978 /* Adjust pc_relative relocs to have zero in *r_offset. */
16979 else if (ppc64_elf_howto_table[r_type]->pc_relative)
16980 addend = outrel.r_offset;
16981 }
16982 }
16983 break;
16984
16985 case R_PPC64_COPY:
16986 case R_PPC64_GLOB_DAT:
16987 case R_PPC64_JMP_SLOT:
16988 case R_PPC64_JMP_IREL:
16989 case R_PPC64_RELATIVE:
16990 /* We shouldn't ever see these dynamic relocs in relocatable
16991 files. */
16992 /* Fall through. */
16993
16994 case R_PPC64_PLTGOT16:
16995 case R_PPC64_PLTGOT16_DS:
16996 case R_PPC64_PLTGOT16_HA:
16997 case R_PPC64_PLTGOT16_HI:
16998 case R_PPC64_PLTGOT16_LO:
16999 case R_PPC64_PLTGOT16_LO_DS:
17000 case R_PPC64_PLTREL32:
17001 case R_PPC64_PLTREL64:
17002 /* These ones haven't been implemented yet. */
17003
17004 info->callbacks->einfo
17005 /* xgettext:c-format */
17006 (_("%P: %pB: %s is not supported for `%pT'\n"),
17007 input_bfd,
17008 ppc64_elf_howto_table[r_type]->name, sym_name);
17009
17010 bfd_set_error (bfd_error_invalid_operation);
17011 ret = false;
17012 goto copy_reloc;
17013 }
17014
17015 /* Multi-instruction sequences that access the TOC can be
17016 optimized, eg. addis ra,r2,0; addi rb,ra,x;
17017 to nop; addi rb,r2,x; */
17018 switch (r_type)
17019 {
17020 default:
17021 break;
17022
17023 case R_PPC64_GOT_TLSLD16_HI:
17024 case R_PPC64_GOT_TLSGD16_HI:
17025 case R_PPC64_GOT_TPREL16_HI:
17026 case R_PPC64_GOT_DTPREL16_HI:
17027 case R_PPC64_GOT16_HI:
17028 case R_PPC64_TOC16_HI:
17029 /* These relocs would only be useful if building up an
17030 offset to later add to r2, perhaps in an indexed
17031 addressing mode instruction. Don't try to optimize.
17032 Unfortunately, the possibility of someone building up an
17033 offset like this or even with the HA relocs, means that
17034 we need to check the high insn when optimizing the low
17035 insn. */
17036 break;
17037
17038 case R_PPC64_PLTCALL_NOTOC:
17039 if (!unresolved_reloc)
17040 htab->notoc_plt = 1;
17041 /* Fall through. */
17042 case R_PPC64_PLTCALL:
17043 if (unresolved_reloc
17044 && offset_in_range (input_section, rel->r_offset,
17045 r_type == R_PPC64_PLTCALL ? 8 : 4))
17046 {
17047 /* No plt entry. Make this into a direct call. */
17048 bfd_byte *p = contents + rel->r_offset;
17049 insn = bfd_get_32 (input_bfd, p);
17050 insn &= 1;
17051 bfd_put_32 (input_bfd, B_DOT | insn, p);
17052 if (r_type == R_PPC64_PLTCALL)
17053 bfd_put_32 (input_bfd, NOP, p + 4);
17054 unresolved_reloc = save_unresolved_reloc;
17055 r_type = R_PPC64_REL24;
17056 }
17057 break;
17058
17059 case R_PPC64_PLTSEQ_NOTOC:
17060 case R_PPC64_PLTSEQ:
17061 if (unresolved_reloc)
17062 {
17063 unresolved_reloc = false;
17064 goto nop_it;
17065 }
17066 break;
17067
17068 case R_PPC64_PLT_PCREL34_NOTOC:
17069 if (!unresolved_reloc)
17070 htab->notoc_plt = 1;
17071 /* Fall through. */
17072 case R_PPC64_PLT_PCREL34:
17073 if (unresolved_reloc
17074 && offset_in_range (input_section, rel->r_offset, 8))
17075 {
17076 bfd_byte *p = contents + rel->r_offset;
17077 bfd_put_32 (input_bfd, PNOP >> 32, p);
17078 bfd_put_32 (input_bfd, PNOP, p + 4);
17079 unresolved_reloc = false;
17080 goto copy_reloc;
17081 }
17082 break;
17083
17084 case R_PPC64_PLT16_HA:
17085 if (unresolved_reloc)
17086 {
17087 unresolved_reloc = false;
17088 goto nop_it;
17089 }
17090 /* Fall through. */
17091 case R_PPC64_GOT_TLSLD16_HA:
17092 case R_PPC64_GOT_TLSGD16_HA:
17093 case R_PPC64_GOT_TPREL16_HA:
17094 case R_PPC64_GOT_DTPREL16_HA:
17095 case R_PPC64_GOT16_HA:
17096 case R_PPC64_TOC16_HA:
17097 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17098 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn)
17099 {
17100 bfd_byte *p;
17101 nop_it:
17102 if (offset_in_range (input_section, rel->r_offset & ~3, 4))
17103 {
17104 p = contents + (rel->r_offset & ~3);
17105 bfd_put_32 (input_bfd, NOP, p);
17106 goto copy_reloc;
17107 }
17108 }
17109 break;
17110
17111 case R_PPC64_PLT16_LO:
17112 case R_PPC64_PLT16_LO_DS:
17113 if (unresolved_reloc)
17114 {
17115 unresolved_reloc = false;
17116 goto nop_it;
17117 }
17118 /* Fall through. */
17119 case R_PPC64_GOT_TLSLD16_LO:
17120 case R_PPC64_GOT_TLSGD16_LO:
17121 case R_PPC64_GOT_TPREL16_LO_DS:
17122 case R_PPC64_GOT_DTPREL16_LO_DS:
17123 case R_PPC64_GOT16_LO:
17124 case R_PPC64_GOT16_LO_DS:
17125 case R_PPC64_TOC16_LO:
17126 case R_PPC64_TOC16_LO_DS:
17127 if (htab->do_toc_opt && relocation + addend + 0x8000 < 0x10000
17128 && !ppc64_elf_tdata (input_bfd)->unexpected_toc_insn
17129 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17130 {
17131 bfd_byte *p = contents + (rel->r_offset & ~3);
17132 insn = bfd_get_32 (input_bfd, p);
17133 if ((insn & (0x3fu << 26)) == 12u << 26 /* addic */)
17134 {
17135 /* Transform addic to addi when we change reg. */
17136 insn &= ~((0x3fu << 26) | (0x1f << 16));
17137 insn |= (14u << 26) | (2 << 16);
17138 }
17139 else
17140 {
17141 insn &= ~(0x1f << 16);
17142 insn |= 2 << 16;
17143 }
17144 bfd_put_32 (input_bfd, insn, p);
17145 }
17146 break;
17147
17148 case R_PPC64_TPREL16_HA:
17149 if (htab->do_tls_opt
17150 && relocation + addend + 0x8000 < 0x10000
17151 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17152 {
17153 bfd_byte *p = contents + (rel->r_offset & ~3);
17154 bfd_put_32 (input_bfd, NOP, p);
17155 goto copy_reloc;
17156 }
17157 break;
17158
17159 case R_PPC64_TPREL16_LO:
17160 case R_PPC64_TPREL16_LO_DS:
17161 if (htab->do_tls_opt
17162 && relocation + addend + 0x8000 < 0x10000
17163 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17164 {
17165 bfd_byte *p = contents + (rel->r_offset & ~3);
17166 insn = bfd_get_32 (input_bfd, p);
17167 insn &= ~(0x1f << 16);
17168 insn |= 13 << 16;
17169 bfd_put_32 (input_bfd, insn, p);
17170 }
17171 break;
17172 }
17173
17174 /* Do any further special processing. */
17175 switch (r_type)
17176 {
17177 default:
17178 break;
17179
17180 case R_PPC64_REL16_HA:
17181 case R_PPC64_REL16_HIGHA:
17182 case R_PPC64_REL16_HIGHERA:
17183 case R_PPC64_REL16_HIGHESTA:
17184 case R_PPC64_REL16DX_HA:
17185 case R_PPC64_ADDR16_HA:
17186 case R_PPC64_ADDR16_HIGHA:
17187 case R_PPC64_ADDR16_HIGHERA:
17188 case R_PPC64_ADDR16_HIGHESTA:
17189 case R_PPC64_TOC16_HA:
17190 case R_PPC64_SECTOFF_HA:
17191 case R_PPC64_TPREL16_HA:
17192 case R_PPC64_TPREL16_HIGHA:
17193 case R_PPC64_TPREL16_HIGHERA:
17194 case R_PPC64_TPREL16_HIGHESTA:
17195 case R_PPC64_DTPREL16_HA:
17196 case R_PPC64_DTPREL16_HIGHA:
17197 case R_PPC64_DTPREL16_HIGHERA:
17198 case R_PPC64_DTPREL16_HIGHESTA:
17199 /* It's just possible that this symbol is a weak symbol
17200 that's not actually defined anywhere. In that case,
17201 'sec' would be NULL, and we should leave the symbol
17202 alone (it will be set to zero elsewhere in the link). */
17203 if (sec == NULL)
17204 break;
17205 /* Fall through. */
17206
17207 case R_PPC64_GOT16_HA:
17208 case R_PPC64_PLTGOT16_HA:
17209 case R_PPC64_PLT16_HA:
17210 case R_PPC64_GOT_TLSGD16_HA:
17211 case R_PPC64_GOT_TLSLD16_HA:
17212 case R_PPC64_GOT_TPREL16_HA:
17213 case R_PPC64_GOT_DTPREL16_HA:
17214 /* Add 0x10000 if sign bit in 0:15 is set.
17215 Bits 0:15 are not used. */
17216 addend += 0x8000;
17217 break;
17218
17219 case R_PPC64_D34_HA30:
17220 case R_PPC64_ADDR16_HIGHERA34:
17221 case R_PPC64_ADDR16_HIGHESTA34:
17222 case R_PPC64_REL16_HIGHERA34:
17223 case R_PPC64_REL16_HIGHESTA34:
17224 if (sec != NULL)
17225 addend += 1ULL << 33;
17226 break;
17227
17228 case R_PPC64_ADDR16_DS:
17229 case R_PPC64_ADDR16_LO_DS:
17230 case R_PPC64_GOT16_DS:
17231 case R_PPC64_GOT16_LO_DS:
17232 case R_PPC64_PLT16_LO_DS:
17233 case R_PPC64_SECTOFF_DS:
17234 case R_PPC64_SECTOFF_LO_DS:
17235 case R_PPC64_TOC16_DS:
17236 case R_PPC64_TOC16_LO_DS:
17237 case R_PPC64_PLTGOT16_DS:
17238 case R_PPC64_PLTGOT16_LO_DS:
17239 case R_PPC64_GOT_TPREL16_DS:
17240 case R_PPC64_GOT_TPREL16_LO_DS:
17241 case R_PPC64_GOT_DTPREL16_DS:
17242 case R_PPC64_GOT_DTPREL16_LO_DS:
17243 case R_PPC64_TPREL16_DS:
17244 case R_PPC64_TPREL16_LO_DS:
17245 case R_PPC64_DTPREL16_DS:
17246 case R_PPC64_DTPREL16_LO_DS:
17247 if (!offset_in_range (input_section, rel->r_offset & ~3, 4))
17248 break;
17249 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17250 mask = 3;
17251 /* If this reloc is against an lq, lxv, or stxv insn, then
17252 the value must be a multiple of 16. This is somewhat of
17253 a hack, but the "correct" way to do this by defining _DQ
17254 forms of all the _DS relocs bloats all reloc switches in
17255 this file. It doesn't make much sense to use these
17256 relocs in data, so testing the insn should be safe. */
17257 if ((insn & (0x3fu << 26)) == (56u << 26)
17258 || ((insn & (0x3fu << 26)) == (61u << 26) && (insn & 3) == 1))
17259 mask = 15;
17260 relocation += addend;
17261 addend = insn & (mask ^ 3);
17262 if ((relocation & mask) != 0)
17263 {
17264 relocation ^= relocation & mask;
17265 info->callbacks->einfo
17266 /* xgettext:c-format */
17267 (_("%H: error: %s not a multiple of %u\n"),
17268 input_bfd, input_section, rel->r_offset,
17269 ppc64_elf_howto_table[r_type]->name,
17270 mask + 1);
17271 bfd_set_error (bfd_error_bad_value);
17272 ret = false;
17273 goto copy_reloc;
17274 }
17275 break;
17276 }
17277
17278 /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
17279 because such sections are not SEC_ALLOC and thus ld.so will
17280 not process them. */
17281 howto = ppc64_elf_howto_table[(int) r_type];
17282 if (unresolved_reloc
17283 && !((input_section->flags & SEC_DEBUGGING) != 0
17284 && h->elf.def_dynamic)
17285 && _bfd_elf_section_offset (output_bfd, info, input_section,
17286 rel->r_offset) != (bfd_vma) -1)
17287 {
17288 info->callbacks->einfo
17289 /* xgettext:c-format */
17290 (_("%H: unresolvable %s against `%pT'\n"),
17291 input_bfd, input_section, rel->r_offset,
17292 howto->name,
17293 h->elf.root.root.string);
17294 ret = false;
17295 }
17296
17297 /* 16-bit fields in insns mostly have signed values, but a
17298 few insns have 16-bit unsigned values. Really, we should
17299 have different reloc types. */
17300 if (howto->complain_on_overflow != complain_overflow_dont
17301 && howto->dst_mask == 0xffff
17302 && (input_section->flags & SEC_CODE) != 0
17303 && offset_in_range (input_section, rel->r_offset & ~3, 4))
17304 {
17305 enum complain_overflow complain = complain_overflow_signed;
17306
17307 insn = bfd_get_32 (input_bfd, contents + (rel->r_offset & ~3));
17308 if ((insn & (0x3fu << 26)) == 10u << 26 /* cmpli */)
17309 complain = complain_overflow_bitfield;
17310 else if (howto->rightshift == 0
17311 ? ((insn & (0x3fu << 26)) == 28u << 26 /* andi */
17312 || (insn & (0x3fu << 26)) == 24u << 26 /* ori */
17313 || (insn & (0x3fu << 26)) == 26u << 26 /* xori */)
17314 : ((insn & (0x3fu << 26)) == 29u << 26 /* andis */
17315 || (insn & (0x3fu << 26)) == 25u << 26 /* oris */
17316 || (insn & (0x3fu << 26)) == 27u << 26 /* xoris */))
17317 complain = complain_overflow_unsigned;
17318 if (howto->complain_on_overflow != complain)
17319 {
17320 alt_howto = *howto;
17321 alt_howto.complain_on_overflow = complain;
17322 howto = &alt_howto;
17323 }
17324 }
17325
17326 switch (r_type)
17327 {
17328 /* Split field relocs aren't handled by _bfd_final_link_relocate. */
17329 case R_PPC64_D34:
17330 case R_PPC64_D34_LO:
17331 case R_PPC64_D34_HI30:
17332 case R_PPC64_D34_HA30:
17333 case R_PPC64_PCREL34:
17334 case R_PPC64_GOT_PCREL34:
17335 case R_PPC64_TPREL34:
17336 case R_PPC64_DTPREL34:
17337 case R_PPC64_GOT_TLSGD_PCREL34:
17338 case R_PPC64_GOT_TLSLD_PCREL34:
17339 case R_PPC64_GOT_TPREL_PCREL34:
17340 case R_PPC64_GOT_DTPREL_PCREL34:
17341 case R_PPC64_PLT_PCREL34:
17342 case R_PPC64_PLT_PCREL34_NOTOC:
17343 case R_PPC64_D28:
17344 case R_PPC64_PCREL28:
17345 if (!offset_in_range (input_section, rel->r_offset, 8))
17346 r = bfd_reloc_outofrange;
17347 else
17348 {
17349 relocation += addend;
17350 if (howto->pc_relative)
17351 relocation -= (rel->r_offset
17352 + input_section->output_offset
17353 + input_section->output_section->vma);
17354 relocation >>= howto->rightshift;
17355
17356 pinsn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17357 pinsn <<= 32;
17358 pinsn |= bfd_get_32 (input_bfd, contents + rel->r_offset + 4);
17359
17360 pinsn &= ~howto->dst_mask;
17361 pinsn |= (((relocation << 16) | (relocation & 0xffff))
17362 & howto->dst_mask);
17363 bfd_put_32 (input_bfd, pinsn >> 32, contents + rel->r_offset);
17364 bfd_put_32 (input_bfd, pinsn, contents + rel->r_offset + 4);
17365 r = bfd_reloc_ok;
17366 if (howto->complain_on_overflow == complain_overflow_signed
17367 && (relocation + (1ULL << (howto->bitsize - 1))
17368 >= 1ULL << howto->bitsize))
17369 r = bfd_reloc_overflow;
17370 }
17371 break;
17372
17373 case R_PPC64_REL16DX_HA:
17374 if (!offset_in_range (input_section, rel->r_offset, 4))
17375 r = bfd_reloc_outofrange;
17376 else
17377 {
17378 relocation += addend;
17379 relocation -= (rel->r_offset
17380 + input_section->output_offset
17381 + input_section->output_section->vma);
17382 relocation = (bfd_signed_vma) relocation >> 16;
17383 insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
17384 insn &= ~0x1fffc1;
17385 insn |= (relocation & 0xffc1) | ((relocation & 0x3e) << 15);
17386 bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
17387 r = bfd_reloc_ok;
17388 if (relocation + 0x8000 > 0xffff)
17389 r = bfd_reloc_overflow;
17390 }
17391 break;
17392
17393 default:
17394 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
17395 contents, rel->r_offset,
17396 relocation, addend);
17397 }
17398
17399 if (r != bfd_reloc_ok)
17400 {
17401 char *more_info = NULL;
17402 const char *reloc_name = howto->name;
17403
17404 if (reloc_dest != DEST_NORMAL)
17405 {
17406 more_info = bfd_malloc (strlen (reloc_name) + 8);
17407 if (more_info != NULL)
17408 {
17409 strcpy (more_info, reloc_name);
17410 strcat (more_info, (reloc_dest == DEST_OPD
17411 ? " (OPD)" : " (stub)"));
17412 reloc_name = more_info;
17413 }
17414 }
17415
17416 if (r == bfd_reloc_overflow)
17417 {
17418 /* On code like "if (foo) foo();" don't report overflow
17419 on a branch to zero when foo is undefined. */
17420 if (!warned
17421 && (reloc_dest == DEST_STUB
17422 || !(h != NULL
17423 && (h->elf.root.type == bfd_link_hash_undefweak
17424 || h->elf.root.type == bfd_link_hash_undefined)
17425 && is_branch_reloc (r_type))))
17426 info->callbacks->reloc_overflow
17427 (info, (struct bfd_link_hash_entry *) h, sym_name,
17428 reloc_name, orig_rel.r_addend, input_bfd, input_section,
17429 rel->r_offset);
17430 }
17431 else
17432 {
17433 info->callbacks->einfo
17434 /* xgettext:c-format */
17435 (_("%H: %s against `%pT': error %d\n"),
17436 input_bfd, input_section, rel->r_offset,
17437 reloc_name, sym_name, (int) r);
17438 ret = false;
17439 }
17440 free (more_info);
17441 }
17442 copy_reloc:
17443 if (wrel != rel)
17444 *wrel = *rel;
17445 }
17446
17447 if (wrel != rel)
17448 {
17449 Elf_Internal_Shdr *rel_hdr;
17450 size_t deleted = rel - wrel;
17451
17452 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
17453 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17454 if (rel_hdr->sh_size == 0)
17455 {
17456 /* It is too late to remove an empty reloc section. Leave
17457 one NONE reloc.
17458 ??? What is wrong with an empty section??? */
17459 rel_hdr->sh_size = rel_hdr->sh_entsize;
17460 deleted -= 1;
17461 }
17462 rel_hdr = _bfd_elf_single_rel_hdr (input_section);
17463 rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
17464 input_section->reloc_count -= deleted;
17465 }
17466
17467 /* If we're emitting relocations, then shortly after this function
17468 returns, reloc offsets and addends for this section will be
17469 adjusted. Worse, reloc symbol indices will be for the output
17470 file rather than the input. Save a copy of the relocs for
17471 opd_entry_value. */
17472 if (is_opd && (info->emitrelocations || bfd_link_relocatable (info)))
17473 {
17474 bfd_size_type amt;
17475 amt = input_section->reloc_count * sizeof (Elf_Internal_Rela);
17476 rel = bfd_alloc (input_bfd, amt);
17477 BFD_ASSERT (ppc64_elf_tdata (input_bfd)->opd.relocs == NULL);
17478 ppc64_elf_tdata (input_bfd)->opd.relocs = rel;
17479 if (rel == NULL)
17480 return false;
17481 memcpy (rel, relocs, amt);
17482 }
17483 return ret;
17484 }
17485
17486 /* Adjust the value of any local symbols in opd sections. */
17487
17488 static int
17489 ppc64_elf_output_symbol_hook (struct bfd_link_info *info,
17490 const char *name ATTRIBUTE_UNUSED,
17491 Elf_Internal_Sym *elfsym,
17492 asection *input_sec,
17493 struct elf_link_hash_entry *h)
17494 {
17495 struct _opd_sec_data *opd;
17496 long adjust;
17497 bfd_vma value;
17498
17499 if (h != NULL)
17500 return 1;
17501
17502 opd = get_opd_info (input_sec);
17503 if (opd == NULL || opd->adjust == NULL)
17504 return 1;
17505
17506 value = elfsym->st_value - input_sec->output_offset;
17507 if (!bfd_link_relocatable (info))
17508 value -= input_sec->output_section->vma;
17509
17510 adjust = opd->adjust[OPD_NDX (value)];
17511 if (adjust == -1)
17512 return 2;
17513
17514 elfsym->st_value += adjust;
17515 return 1;
17516 }
17517
17518 /* Finish up dynamic symbol handling. We set the contents of various
17519 dynamic sections here. */
17520
17521 static bool
17522 ppc64_elf_finish_dynamic_symbol (bfd *output_bfd,
17523 struct bfd_link_info *info,
17524 struct elf_link_hash_entry *h,
17525 Elf_Internal_Sym *sym)
17526 {
17527 struct ppc_link_hash_table *htab;
17528 struct plt_entry *ent;
17529
17530 htab = ppc_hash_table (info);
17531 if (htab == NULL)
17532 return false;
17533
17534 if (!htab->opd_abi && !h->def_regular)
17535 for (ent = h->plt.plist; ent != NULL; ent = ent->next)
17536 if (ent->plt.offset != (bfd_vma) -1)
17537 {
17538 /* Mark the symbol as undefined, rather than as
17539 defined in glink. Leave the value if there were
17540 any relocations where pointer equality matters
17541 (this is a clue for the dynamic linker, to make
17542 function pointer comparisons work between an
17543 application and shared library), otherwise set it
17544 to zero. */
17545 sym->st_shndx = SHN_UNDEF;
17546 if (!h->pointer_equality_needed)
17547 sym->st_value = 0;
17548 else if (!h->ref_regular_nonweak)
17549 {
17550 /* This breaks function pointer comparisons, but
17551 that is better than breaking tests for a NULL
17552 function pointer. */
17553 sym->st_value = 0;
17554 }
17555 break;
17556 }
17557
17558 if (h->needs_copy
17559 && (h->root.type == bfd_link_hash_defined
17560 || h->root.type == bfd_link_hash_defweak)
17561 && (h->root.u.def.section == htab->elf.sdynbss
17562 || h->root.u.def.section == htab->elf.sdynrelro))
17563 {
17564 /* This symbol needs a copy reloc. Set it up. */
17565 Elf_Internal_Rela rela;
17566 asection *srel;
17567 bfd_byte *loc;
17568
17569 if (h->dynindx == -1)
17570 abort ();
17571
17572 rela.r_offset = defined_sym_val (h);
17573 rela.r_info = ELF64_R_INFO (h->dynindx, R_PPC64_COPY);
17574 rela.r_addend = 0;
17575 if (h->root.u.def.section == htab->elf.sdynrelro)
17576 srel = htab->elf.sreldynrelro;
17577 else
17578 srel = htab->elf.srelbss;
17579 loc = srel->contents;
17580 loc += srel->reloc_count++ * sizeof (Elf64_External_Rela);
17581 bfd_elf64_swap_reloca_out (output_bfd, &rela, loc);
17582 }
17583
17584 return true;
17585 }
17586
17587 /* Used to decide how to sort relocs in an optimal manner for the
17588 dynamic linker, before writing them out. */
17589
17590 static enum elf_reloc_type_class
17591 ppc64_elf_reloc_type_class (const struct bfd_link_info *info,
17592 const asection *rel_sec,
17593 const Elf_Internal_Rela *rela)
17594 {
17595 enum elf_ppc64_reloc_type r_type;
17596 struct ppc_link_hash_table *htab = ppc_hash_table (info);
17597
17598 if (rel_sec == htab->elf.irelplt)
17599 return reloc_class_ifunc;
17600
17601 r_type = ELF64_R_TYPE (rela->r_info);
17602 switch (r_type)
17603 {
17604 case R_PPC64_RELATIVE:
17605 return reloc_class_relative;
17606 case R_PPC64_JMP_SLOT:
17607 return reloc_class_plt;
17608 case R_PPC64_COPY:
17609 return reloc_class_copy;
17610 default:
17611 return reloc_class_normal;
17612 }
17613 }
17614
17615 /* Finish up the dynamic sections. */
17616
17617 static bool
17618 ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
17619 struct bfd_link_info *info)
17620 {
17621 struct ppc_link_hash_table *htab;
17622 bfd *dynobj;
17623 asection *sdyn;
17624
17625 htab = ppc_hash_table (info);
17626 if (htab == NULL)
17627 return false;
17628
17629 dynobj = htab->elf.dynobj;
17630 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
17631
17632 if (htab->elf.dynamic_sections_created)
17633 {
17634 Elf64_External_Dyn *dyncon, *dynconend;
17635
17636 if (sdyn == NULL || htab->elf.sgot == NULL)
17637 abort ();
17638
17639 dyncon = (Elf64_External_Dyn *) sdyn->contents;
17640 dynconend = (Elf64_External_Dyn *) (sdyn->contents + sdyn->size);
17641 for (; dyncon < dynconend; dyncon++)
17642 {
17643 Elf_Internal_Dyn dyn;
17644 asection *s;
17645
17646 bfd_elf64_swap_dyn_in (dynobj, dyncon, &dyn);
17647
17648 switch (dyn.d_tag)
17649 {
17650 default:
17651 continue;
17652
17653 case DT_PPC64_GLINK:
17654 s = htab->glink;
17655 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17656 /* We stupidly defined DT_PPC64_GLINK to be the start
17657 of glink rather than the first entry point, which is
17658 what ld.so needs, and now have a bigger stub to
17659 support automatic multiple TOCs. */
17660 dyn.d_un.d_ptr += GLINK_PLTRESOLVE_SIZE (htab) - 8 * 4;
17661 break;
17662
17663 case DT_PPC64_OPD:
17664 s = bfd_get_section_by_name (output_bfd, ".opd");
17665 if (s == NULL)
17666 continue;
17667 dyn.d_un.d_ptr = s->vma;
17668 break;
17669
17670 case DT_PPC64_OPT:
17671 if ((htab->do_multi_toc && htab->multi_toc_needed)
17672 || htab->notoc_plt)
17673 dyn.d_un.d_val |= PPC64_OPT_MULTI_TOC;
17674 if (htab->has_plt_localentry0)
17675 dyn.d_un.d_val |= PPC64_OPT_LOCALENTRY;
17676 break;
17677
17678 case DT_PPC64_OPDSZ:
17679 s = bfd_get_section_by_name (output_bfd, ".opd");
17680 if (s == NULL)
17681 continue;
17682 dyn.d_un.d_val = s->size;
17683 break;
17684
17685 case DT_PLTGOT:
17686 s = htab->elf.splt;
17687 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17688 break;
17689
17690 case DT_JMPREL:
17691 s = htab->elf.srelplt;
17692 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
17693 break;
17694
17695 case DT_PLTRELSZ:
17696 dyn.d_un.d_val = htab->elf.srelplt->size;
17697 break;
17698
17699 case DT_TEXTREL:
17700 if (htab->elf.ifunc_resolvers)
17701 info->callbacks->einfo
17702 (_("%P: warning: text relocations and GNU indirect "
17703 "functions may result in a segfault at runtime\n"));
17704 continue;
17705 }
17706
17707 bfd_elf64_swap_dyn_out (output_bfd, &dyn, dyncon);
17708 }
17709 }
17710
17711 if (htab->elf.sgot != NULL && htab->elf.sgot->size != 0
17712 && htab->elf.sgot->output_section != bfd_abs_section_ptr)
17713 {
17714 /* Fill in the first entry in the global offset table.
17715 We use it to hold the link-time TOCbase. */
17716 bfd_put_64 (output_bfd,
17717 elf_gp (output_bfd) + TOC_BASE_OFF,
17718 htab->elf.sgot->contents);
17719
17720 /* Set .got entry size. */
17721 elf_section_data (htab->elf.sgot->output_section)->this_hdr.sh_entsize
17722 = 8;
17723 }
17724
17725 if (htab->elf.splt != NULL && htab->elf.splt->size != 0
17726 && htab->elf.splt->output_section != bfd_abs_section_ptr)
17727 {
17728 /* Set .plt entry size. */
17729 elf_section_data (htab->elf.splt->output_section)->this_hdr.sh_entsize
17730 = PLT_ENTRY_SIZE (htab);
17731 }
17732
17733 /* brlt is SEC_LINKER_CREATED, so we need to write out relocs for
17734 brlt ourselves if emitrelocations. */
17735 if (htab->brlt != NULL
17736 && htab->brlt->reloc_count != 0
17737 && !_bfd_elf_link_output_relocs (output_bfd,
17738 htab->brlt,
17739 elf_section_data (htab->brlt)->rela.hdr,
17740 elf_section_data (htab->brlt)->relocs,
17741 NULL))
17742 return false;
17743
17744 if (htab->glink != NULL
17745 && htab->glink->reloc_count != 0
17746 && !_bfd_elf_link_output_relocs (output_bfd,
17747 htab->glink,
17748 elf_section_data (htab->glink)->rela.hdr,
17749 elf_section_data (htab->glink)->relocs,
17750 NULL))
17751 return false;
17752
17753
17754 if (htab->glink_eh_frame != NULL
17755 && htab->glink_eh_frame->size != 0
17756 && htab->glink_eh_frame->sec_info_type == SEC_INFO_TYPE_EH_FRAME
17757 && !_bfd_elf_write_section_eh_frame (output_bfd, info,
17758 htab->glink_eh_frame,
17759 htab->glink_eh_frame->contents))
17760 return false;
17761
17762 /* We need to handle writing out multiple GOT sections ourselves,
17763 since we didn't add them to DYNOBJ. We know dynobj is the first
17764 bfd. */
17765 while ((dynobj = dynobj->link.next) != NULL)
17766 {
17767 asection *s;
17768
17769 if (!is_ppc64_elf (dynobj))
17770 continue;
17771
17772 s = ppc64_elf_tdata (dynobj)->got;
17773 if (s != NULL
17774 && s->size != 0
17775 && s->output_section != bfd_abs_section_ptr
17776 && !bfd_set_section_contents (output_bfd, s->output_section,
17777 s->contents, s->output_offset,
17778 s->size))
17779 return false;
17780 s = ppc64_elf_tdata (dynobj)->relgot;
17781 if (s != NULL
17782 && s->size != 0
17783 && s->output_section != bfd_abs_section_ptr
17784 && !bfd_set_section_contents (output_bfd, s->output_section,
17785 s->contents, s->output_offset,
17786 s->size))
17787 return false;
17788 }
17789
17790 return true;
17791 }
17792
17793 #include "elf64-target.h"
17794
17795 /* FreeBSD support */
17796
17797 #undef TARGET_LITTLE_SYM
17798 #define TARGET_LITTLE_SYM powerpc_elf64_fbsd_le_vec
17799 #undef TARGET_LITTLE_NAME
17800 #define TARGET_LITTLE_NAME "elf64-powerpcle-freebsd"
17801
17802 #undef TARGET_BIG_SYM
17803 #define TARGET_BIG_SYM powerpc_elf64_fbsd_vec
17804 #undef TARGET_BIG_NAME
17805 #define TARGET_BIG_NAME "elf64-powerpc-freebsd"
17806
17807 #undef ELF_OSABI
17808 #define ELF_OSABI ELFOSABI_FREEBSD
17809
17810 #undef elf64_bed
17811 #define elf64_bed elf64_powerpc_fbsd_bed
17812
17813 #include "elf64-target.h"